幻方开源第二代MoE模型 DeepSeek-V2,魔搭社区推理、微调最佳实践教程

本文涉及的产品
交互式建模 PAI-DSW,5000CU*H 3个月
简介: 5月6日,幻方继1月份推出首个国产MoE模型,历时4个月,带来第二代MoE模型DeepSeek-V2,并开源了技术报告和模型权重,魔搭社区可下载体验。

导读

5月6日,幻方继1月份推出首个国产MoE模型,历时4个月,带来第二代MoE模型DeepSeek-V2,并开源了技术报告和模型权重,魔搭社区可下载体验。

技术报告:

https://github.com/deepseek-ai/DeepSeek-V2/blob/main/deepseek-v2-tech-report.pdf

DeepSeek-V2未遵循业界普遍采用的“类LLaMA的Dense结构”和“类Mistral的Sparse结构”,而采取了对模型框架的全面创新。该模型引入了MLA(Multi-head Latent Attention)架构,这是一种与MHA(Multi-Head Attention)相媲美的技术,能显著降低计算量和推理时的内存使用。同时,自研Sparse结构DeepSeekMoE极大降低了计算量,二者的结合使模型性能得到了大幅提升。(详情可查看技术报告和开源代码)

官方同步,DeepSeek-V2以236B总参数、21B激活,大致达到70B~110B Dense的模型能力,同时消耗的显存(KV Cache)只有同级别Dense模型的1/5~1/100,每token成本大幅降低。实际部署在8卡H800机器上,输入吞吐量超过每秒10万tokens,输出超过每秒5万tokens。

性能方面,在目前大模型主流榜单中,DeepSeek-V2均表现出色:

  • 中文综合能力(AlignBench)开源模型中最强,与GPT-4-Turbo,文心4.0等闭源模型在评测中处于同一梯队
  • 英文综合能力(MT-Bench)与最强的开源模型LLaMA3-70B同处第一梯队,超过最强MoE开源模型Mixtral 8x22B
  • 知识、数学、推理、编程等榜单结果也位居前列
  • 支持128K上下文窗口

和DeepSeek 67B相比,DeepSeek-V2节约了42.5%训练成本,推理的KV Cache节约了93.3%,最大吞吐是之前的576%。

image.png

模型链接和下载

DeepSeek-V2系列模型现已在魔搭ModelScope社区开源,包括:

DeepSeek-V2-Chat

https://modelscope.cn/models/deepseek-ai/DeepSeek-V2-Chat

DeepSeek-V2

https://modelscope.cn/models/deepseek-ai/DeepSeek-V2

社区支持直接下载模型的repo:

#模型下载
from modelscope import snapshot_download
model_dir = snapshot_download('deepseek-ai/DeepSeek-V2-Chat')

模型推理

推理代码:

import torch
from modelscope import AutoTokenizer, AutoModelForCausalLM, GenerationConfig, AutoConfig
model_name = "deepseek-ai/DeepSeek-V2-Chat"
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
# `max_memory` should be set based on your devices
max_memory = {i: "75GB" for i in range(8)}
model_config = AutoConfig.from_pretrained(model_name, trust_remote_code=True)
model_config._attn_implementation='eager'
model = AutoModelForCausalLM.from_pretrained(model_name, config=model_config, trust_remote_code=True, device_map="auto", torch_dtype=torch.bfloat16, max_memory=max_memory)
model.generation_config = GenerationConfig.from_pretrained(model_name)
model.generation_config.pad_token_id = model.generation_config.eos_token_id
messages = [
    {"role": "user", "content": "Write a piece of quicksort code in C++"}
]
input_tensor = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")
outputs = model.generate(input_tensor.to(model.device), max_new_tokens=100)
result = tokenizer.decode(outputs[0][input_tensor.shape[1]:], skip_special_tokens=True)
print(result)

推理生成样例:

Sure, here is a simple implementation of the quicksort algorithm in C++:
```cpp
#include <iostream>
#include <vector>
using namespace std;
int partition(vector<int>& arr, int low, int high) {
    int pivot = arr[high];
    int i = (low - 1);
    for (int j = low; j <= high - 1; j++) {
        if (arr[j] < pivot) {
            i++;
            swap(arr[i], arr[j]);
        }
    }
    swap(arr[i + 1], arr[high]);
    return (i + 1);
}
void quickSort(vector<int>& arr, int low, int high) {
    if (low < high) {
        int pi = partition(arr, low, high);
        quickSort(arr, low, pi - 1);
        quickSort(arr, pi + 1, high);
    }
}
int main() {
    vector<int> arr = {10, 7, 8, 9, 1, 5};
    int n = arr.size();
    quickSort(arr, 0, n - 1);
    cout << "Sorted array: \n";
    for(int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
    return 0;
}
```
This code sorts an array of integers using the quicksort algorithm. The `quickSort` function is the main function that recursively sorts the array. The `partition` function is used to partition the array around a pivot element and return the index of the pivot element. The elements smaller than the pivot are moved to its left and the elements larger are moved to its right.

推理占用:

image.png

模型微调和微调后推理

我们使用swift来对模型进行微调swift是魔搭社区官方提供的LLM微调推理框架。

微调代码开源地址:https://github.com/modelscope/swift

我们使用数据集 self-cognition进行微调,该数据集的任务是:改变模型的自我认知。

环境准备:

git clone https://github.com/modelscope/swift.git
cd swift
pip install -e .[llm]

微调脚本: (LoRA)

默认只对LLM部分的qkv进行lora微调,如果你想对LLM部分的所有linear进行微调,可以指定`--lora_target_modules ALL`。

# Experimental environment: 8*A100
# 8*80GB GPU memory
nproc_per_node=8
CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 \
NPROC_PER_NODE=$nproc_per_node \
swift sft \
    --model_type deepseek-v2-chat \
    --sft_type lora \
    --tuner_backend peft \
    --dtype bf16 \
    --output_dir output \
    --ddp_backend nccl \
    --self_cognition_sample 2000 \
    --model_name 小白 'Xiao Bai' \
    --model_author 魔搭 'Modelscope' \
    --train_dataset_sample -1 \
    --num_train_epochs 1 \
    --max_length 512 \
    --check_dataset_strategy warning \
    --lora_rank 8 \
    --lora_alpha 32 \
    --lora_dropout_p 0.05 \
    --lora_dtype AUTO \
    --lora_target_modules DEFAULT \
    --gradient_checkpointing false \
    --batch_size 2 \
    --weight_decay 0.1 \
    --learning_rate 1e-4 \
    --gradient_accumulation_steps $(expr 16 / $nproc_per_node) \
    --max_grad_norm 0.5 \
    --warmup_ratio 0.03 \
    --eval_steps 100 \
    --save_steps 100 \
    --save_total_limit 10 \
    --logging_steps 10 \
    --deepspeed default-zero3 \

微调后推理脚本: (这里的ckpt_dir需要修改为训练生成的checkpoint文件夹)

# Experimental environment: A10, 3090, V100
CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 \
swift infer \
    --ckpt_dir output/deepseek-v2-chat/vx-xxx/checkpoint-xxx \
    --load_dataset_config true \
    --eval_human true \
    --max_length 512

微调的可视化结果:

训练准确率

image.png

训练loss

image.png

微调后样例:

<<< 你是谁
我是一个由魔搭开发的人工智能程序,被称为小白。我的主要目的是通过文本交流为人们提供帮助、信息和娱乐。如果你有任何疑问或需要帮助,请随时提出。

资源占用

微调

image.png

点击直达链接:DeepSeek-V2-Chat · 模型库 (modelscope.cn)

相关文章
|
4天前
|
自然语言处理 数据可视化 物联网
Qwen1.5-MoE开源,魔搭社区推理训练最佳实践教程来啦
通义千问团队推出Qwen系列的首个MoE模型,Qwen1.5-MoE-A2.7B。
|
5月前
|
数据可视化 PyTorch 算法框架/工具
零一万物Yi-34B-Chat 微调模型及量化版开源!魔搭社区最佳实践教程!
11月24日,零一万物基正式发布并开源微调模型 Yi-34B-Chat,可申请免费商用。同时,零一万物还为开发者提供了 4bit/8bit 量化版模型,Yi-34B-Chat 4bit 量化版模型可以直接在消费级显卡(如RTX3090)上使用。魔搭社区已支持下载、推理训练体验,并推出相关教程,欢迎大家来玩!
|
4天前
|
自然语言处理 物联网 Swift
零一万物开源Yi-VL多模态大模型,魔搭社区推理&微调最佳实践来啦!
近期,零一万物Yi系列模型家族发布了其多模态大模型系列,Yi Vision Language(Yi-VL)多模态语言大模型正式面向全球开源。
|
4天前
|
数据可视化 物联网 测试技术
零一万物Yi-1.5系列模型发布并开源!34B/9B/6B 多尺寸魔搭社区推理微调最佳实践教程来啦!
Yi-1.5是Yi的升级版本。 它使用 500B tokens的高质量语料库在 Yi 上持续进行预训练,并在 3M 个多样化的微调样本上进行微调。
|
4天前
|
人工智能 测试技术 API
Phi-3:小模型,大未来!(附魔搭社区推理、微调实战教程)
近期, Microsoft 推出 Phi-3,这是 Microsoft 开发的一系列开放式 AI 模型。Phi-3 模型是一个功能强大、成本效益高的小语言模型?(SLM),在各种语言、推理、编码和数学基准测试中,在同级别参数模型中性能表现优秀。为开发者构建生成式人工智能应用程序时提供了更多实用的选择。
|
4天前
|
安全 测试技术 Swift
Llama 3开源!魔搭社区手把手带你推理,部署,微调和评估
Meta发布了 Meta Llama 3系列,是LLama系列开源大型语言模型的下一代。在接下来的几个月,Meta预计将推出新功能、更长的上下文窗口、额外的模型大小和增强的性能,并会分享 Llama 3 研究论文。
Llama 3开源!魔搭社区手把手带你推理,部署,微调和评估
社区供稿 | XTuner发布LLaVA-Llama-3-8B,支持单卡推理,评测和微调
日前,XTuner 团队基于 meta 最新发布的 Llama-3-8B-Instruct 模型训练并发布了最新版多模态大模型 LLaVA-Llama-3-8B, 在多个评测数据集上取得显著提升。
|
4天前
|
存储 自然语言处理 负载均衡
元象开源首个MoE大模型:4.2B激活参数,效果堪比13B模型,魔搭社区最佳实践来了
近日,元象发布其首个Moe大模型 XVERSE-MoE-A4.2B, 采用混合专家模型架构 (Mixture of Experts),激活参数4.2B,效果即可媲美13B模型。该模型全开源,无条件免费商用,支持中小企业、研究者和开发者可在元象高性能“全家桶”中按需选用,推动低成本部署。
|
4天前
|
数据可视化 物联网 Swift
澜舟科技开源孟子3-13B大模型,魔搭社区推理训练最佳实践!
4月1日,澜舟科技宣布开源Mengzi3-13B大模型,对学术研究完全开放,同时支持免费商用。
|
4天前
|
人工智能 知识图谱 Windows
Mistral 7B v0.2 基础模型开源,魔搭社区微调教程和评测来啦!
Mistral AI在3月24日突然发布并开源了 Mistral 7B v0.2模型,有如下几个特点
http://www.vxiaotou.com