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

简介: 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)

相关文章
|
iOS开发 MacOS
macos排查并禁用系统占用的8021端口
macos排查并禁用系统占用的8021端口
642 0
|
存储 算法 网络架构
物理层(二)
物理层(二)
1458 0
|
弹性计算
阿里云开mc我的世界服务器CPU内存配置选择及报价
阿里云服务器搭建Minecraft我的世界CPU内存配置怎么选择?公网带宽和系统盘选择多少合适?一般20人以内玩家、1.12版本的大型整合包、100个以内个轻量mod,2核4G配置就够用了,公网带宽选择3M或5M都可以,系统盘就高效云盘40GB够用了
1983 0
阿里云开mc我的世界服务器CPU内存配置选择及报价
|
前端开发 API 数据库
构建领域驱动的微服务
构建领域驱动的微服务
270 3
|
10月前
|
存储 网络协议 算法
ISIS协议详解
ISIS协议是一种链路状态路由协议,广泛用于大规模网络中。它最初基于OSI模型设计,后经扩展支持TCP/IP协议。ISIS通过SPF算法计算最短路径,使用NSAP地址进行设备寻址,具备灵活的区域划分和分层结构。协议通过L1、L2及L1/2路由器实现区域内部与骨干区域的通信,支持P2P和广播网络类型,具备邻接建立、LSDB同步、路由计算等核心机制,广泛应用于运营商和企业骨干网中。
三大微分中值定理证明方法(罗尔定理、拉格朗日中值定理、柯西中值定理)
三大微分中值定理证明方法(罗尔定理、拉格朗日中值定理、柯西中值定理)
2362 0
三大微分中值定理证明方法(罗尔定理、拉格朗日中值定理、柯西中值定理)
|
机器学习/深度学习 缓存 自然语言处理
阿里云百炼产品月刊【2024年12月】
12月,阿里云百炼带来多项技术革新与服务升级。本月重点包括VL模型部分规格降价,上线多个新模型,如视觉推理模型qvq-72b-preview、多语言文本统一排序模型gte-rerank和人物视频生成模型videoretalk等。应用模块新增音视频互动、互联网搜索及意图选择等功能,极大丰富了应用场景。此外,新增Context Cache功能和batch调用支持,提升了响应速度并降低了费用。
1931 0
|
自然语言处理 数据可视化 物联网
Qwen1.5-MoE开源,魔搭社区推理训练最佳实践教程来啦
通义千问团队推出Qwen系列的首个MoE模型,Qwen1.5-MoE-A2.7B。

热门文章

最新文章