← 返回首页
🧠

Yi系列大模型:零一万物

📂 llm ⏱ 2 min 313 words

--- title: "Yi系列大模型:零一万物" description: "深入了解Yi系列模型的架构特点和高性能表现" tags: ["Yi", "零一万物", "中文LLM", "开源模型"] category: "llm" icon: "🧠"

Yi系列大模型:零一万物

Yi简介

Yi是零一万物(01.AI)开发的大语言模型系列。Yi以其出色的性能、高效的训练方法和优秀的开源生态著称,在多项基准测试中表现优异。

Yi的核心优势:

Yi架构

核心设计

# Yi架构配置
yi_config = {
    "hidden_size": 4096,
    "intermediate_size": 11008,
    "num_hidden_layers": 32,
    "num_attention_heads": 32,
    "num_key_value_heads": 32,
    "max_position_embeddings": 4096,
    "rope_theta": 10000.0,
    "vocab_size": 64000,
    "rms_norm_eps": 1e-6,
    "bos_token_id": 1,
    "eos_token_id": 2,
    "pad_token_id": 0,
    "tie_word_embeddings": False
}

# 关键特性
features = {
    "GQA": "分组查询注意力",
    "Flash Attention": "高效注意力",
    "RoPE": "旋转位置编码",
    "SwiGLU": "激活函数"
}

Yi版本

# Yi版本演进
versions = {
    "Yi-6B": {
        "参数": "6B",
        "上下文": "4K",
        "特点": "基础版本"
    },
    "Yi-9B": {
        "参数": "9B",
        "上下文": "4K",
        "特点": "中间版本"
    },
    "Yi-34B": {
        "参数": "34B",
        "上下文": "4K",
        "特点": "旗舰版本"
    },
    "Yi-1.5": {
        "参数": "6B/9B/34B",
        "上下文": "32K",
        "特点": "长上下文版本"
    },
    "Yi-VL": {
        "参数": "6B/34B",
        "上下文": "多模态",
        "特点": "视觉语言模型"
    }
}

使用Yi

基本推理

from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

# 加载Yi
model_name = "01-ai/Yi-1.5-9B-Chat-16K"

tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
    model_name,
    torch_dtype=torch.float16,
    device_map="auto",
    trust_remote_code=True
)

# 推理
messages = [
    {"role": "system", "content": "你是一个有帮助的助手"},
    {"role": "user", "content": "什么是大语言模型?"}
]

inputs = tokenizer.apply_chat_template(
    messages,
    add_generation_prompt=True,
    return_tensors="pt"
).to(model.device)

outputs = model.generate(inputs, max_new_tokens=512, do_sample=True, temperature=0.7)
response = tokenizer.decode(outputs[0][inputs.shape[1]:], skip_special_tokens=True)
print(response)

使用vLLM

from vllm import LLM, SamplingParams

# 部署Yi
llm = LLM(
    model="01-ai/Yi-1.5-9B-Chat-16K",
    max_model_len=16384,
    gpu_memory_utilization=0.9,
    trust_remote_code=True
)

sampling_params = SamplingParams(temperature=0.7, max_tokens=512)
outputs = llm.generate(["什么是深度学习?"], sampling_params)
print(outputs[0].outputs[0].text)

Yi-VL多模态

# Yi-VL支持图像理解
from transformers import AutoModelForCausalLM, AutoProcessor

model = AutoModelForCausalLM.from_pretrained(
    "01-ai/Yi-VL-9B",
    torch_dtype=torch.float16,
    device_map="auto",
    trust_remote_code=True
)

processor = AutoProcessor.from_pretrained("01-ai/Yi-VL-9B", trust_remote_code=True)

# 图像理解
messages = [
    {"role": "user", "content": [
        {"type": "image", "image": "https://example.com/image.jpg"},
        {"type": "text", "text": "描述这张图片"}
    ]}
]

response = model.chat(messages)
print(response)

微调Yi

LoRA微调

from peft import LoraConfig, get_peft_model

# LoRA配置
lora_config = LoraConfig(
    r=64,
    lora_alpha=16,
    target_modules=["q_proj", "k_proj", "v_proj", "o_proj"],
    lora_dropout=0.05,
    bias="none",
    task_type="CAUSAL_LM"
)

# 加载模型
model = AutoModelForCausalLM.from_pretrained(
    "01-ai/Yi-1.5-9B",
    torch_dtype=torch.float16
)

model = get_peft_model(model, lora_config)
model.print_trainable_parameters()

性能评估

# Yi性能
performance = {
    "Yi-34B": {
        "MMLU": "76.3",
        "HumanEval": "26.2",
        "C-Eval": "81.0",
        "CMMLU": "82.3",
        "优势": "中文能力出色"
    },
    "Yi-1.5-34B": {
        "MMLU": "79.3",
        "HumanEval": "41.4",
        "C-Eval": "85.5",
        "优势": "长上下文支持"
    }
}

最佳实践

  1. 选择版本:9B/34B适合不同场景
  2. 使用1.5版本:支持更长上下文
  3. 利用多模态:使用Yi-VL处理图像
  4. 量化部署:使用AWQ/GPTQ量化
  5. 使用长上下文:充分利用32K上下文能力

Yi凭借其优异的性能和开源生态,成为国内领先的开源LLM之一。