背景
当前大模型的权重加载和调用,主要是通过在HuggingFace官网下载并使用transformer的库来加以实现;其中大模型的权重文件较大(部分>100GB),若只是快速研究网络结构和数据流变化,则无需下载权重。本文基于这个背景,做了如下尝试,实现了在无需下载权重的情况下打印模型结构和网络的输入输出。
一、基本介绍
1 HuggingFace的文件说明
一般而言,在HuggingFace官网,打开对应的模型,然后点击Files and versions,就会出现模型权重文件和一些相对应的代码和json文件。
config.json文件 :模型的配置文件,包含模型的架构和参数配置信息。
configuration_deepseek.py文件 :DeepSeekv3模型的配置脚本,定义了模型的具体配置和参数。
model-00001-of-000163.safetensors文件 :模型的权重文件之一,存储了模型的部分参数(这里表示总共有163个权重文件,特别庞大)
model.safetensors.index.json文件 :模型权重文件的索引文件,记录了各个权重文件的分片信息
modeling_deepseek.py文件 :DeepSeekv3模型的实现脚本,包含模型的定义和相关函数
tokenizer.json文件 :分词器的配置文件,定义了分词器的词汇表和相关参数
tokenizer_config.json文件 :分词器的配置文件,包含分词器的配置信息
2 加载模型的Python库说明
这里展示一段加载模型权重并打印网络结构的代码示例
python
from transformers import AutoModelForCausalLM
model_path = "model.safetensors"
model = AutoModelForCausalLM.from_pretrained(model_path)
print(model)
其中,AutoModelForCausalLM 是 Hugging Face 的 transformers 库中的一个类,用于加载预训练的因果语言模型。以下是 AutoModelForCausalLM.from_pretrained 方法的入参说明
python
pretrained_model_name_or_path:预训练模型的名称或路径,可以是Hugging Face模型库中的模型名称,也可以是本地模型文件夹的路径。
config:自定义的模型配置对象,可以传入一个PretrainedConfig对象,用于手动配置模型。如果未提供,系统会从pretrained_model_name_or_path自动加载相应的配置。
state_dict:预加载的模型权重字典,用于初始化模型权重。
cache_dir:指定缓存目录,用于下载和存储模型文件。
from_tf:是否从TensorFlow模型加载权重。
force_download:是否强制重新下载模型权重。
resume_download:在下载过程中,如果发生中断,是否从中断点继续下载。
二、Deepseekv3的随机权重加载和网络结构分析
硬件说明:一台RTX4090显卡(24GB显存)
工程目录
其中,config.json、configuration_deepseek.py和modeling_deepseek.py都是从hugging face直接下载的。
2.1 编辑config.json
由于显存受限,因此这里将hidden_size、intermediate_size和moe_intermediate_size,使得61层的网络能够加载在单卡上。
hidden_size: 模型中隐藏层的维度,通常与模型的输入维度相同;原始为7168,现修改为256
intermediate_size: 模型中MLP的中间层维度;原始为18432,现修改为1024
moe_intermediate_size: 模型中MOE的中间层维度;原始为2048,现修改为128
2.2 编写py脚本
python
import torch
from configuration_deepseek import DeepseekV3Config
from modeling_deepseek import DeepseekV3ForCausalLM
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
config_file = "config.json"
model_config = DeepseekV3Config.from_pretrained(config_file)
model = DeepseekV3ForCausalLM(config=model_config).to(torch.float16).eval()
model = model.to(device)
# 打印模型结构
print(model)
dummy_input = torch.randint(low=0, high=129280, size=(4, 64), dtype=torch.long).to(device)
output = model(dummy_input)
# 打印输出张量的形状
print(output.logits.shape)
2.3 打印网络结构
显存占用情况
Deepseekv3网络结构和输入输出shape
python
DeepseekV3ForCausalLM(
(model): DeepseekV3Model(
(embed_tokens): Embedding(129280, 256)
(layers): ModuleList(
(0-2): 3 x DeepseekV3DecoderLayer(
(self_attn): DeepseekV3Attention(
(q_a_proj): Linear(in_features=256, out_features=1536, bias=False)
(q_a_layernorm): DeepseekV3RMSNorm()
(q_b_proj): Linear(in_features=1536, out_features=24576, bias=False)
(kv_a_proj_with_mqa): Linear(in_features=256, out_features=576, bias=False)
(kv_a_layernorm): DeepseekV3RMSNorm()
(kv_b_proj): Linear(in_features=512, out_features=32768, bias=False)
(o_proj): Linear(in_features=16384, out_features=256, bias=False)
(rotary_emb): DeepseekV3YarnRotaryEmbedding()
)
(mlp): DeepseekV3MLP(
(gate_proj): Linear(in_features=256, out_features=1024, bias=False)
(up_proj): Linear(in_features=256, out_features=1024, bias=False)
(down_proj): Linear(in_features=1024, out_features=256, bias=False)
(act_fn): SiLU()
)
(input_layernorm): DeepseekV3RMSNorm()
(post_attention_layernorm): DeepseekV3RMSNorm()
)
(3-60): 58 x DeepseekV3DecoderLayer(
(self_attn): DeepseekV3Attention(
(q_a_proj): Linear(in_features=256, out_features=1536, bias=False)
(q_a_layernorm): DeepseekV3RMSNorm()
(q_b_proj): Linear(in_features=1536, out_features=24576, bias=False)
(kv_a_proj_with_mqa): Linear(in_features=256, out_features=576, bias=False)
(kv_a_layernorm): DeepseekV3RMSNorm()
(kv_b_proj): Linear(in_features=512, out_features=32768, bias=False)
(o_proj): Linear(in_features=16384, out_features=256, bias=False)
(rotary_emb): DeepseekV3YarnRotaryEmbedding()
)
(mlp): DeepseekV3MoE(
(experts): ModuleList(
(0-255): 256 x DeepseekV3MLP(
(gate_proj): Linear(in_features=256, out_features=128, bias=False)
(up_proj): Linear(in_features=256, out_features=128, bias=False)
(down_proj): Linear(in_features=128, out_features=256, bias=False)
(act_fn): SiLU()
)
)
(gate): MoEGate()
(shared_experts): DeepseekV3MLP(
(gate_proj): Linear(in_features=256, out_features=128, bias=False)
(up_proj): Linear(in_features=256, out_features=128, bias=False)
(down_proj): Linear(in_features=128, out_features=256, bias=False)
(act_fn): SiLU()
)
)
(input_layernorm): DeepseekV3RMSNorm()
(post_attention_layernorm): DeepseekV3RMSNorm()
)
)
(norm): DeepseekV3RMSNorm()
)
(lm_head): Linear(in_features=256, out_features=129280, bias=False)
)
input shape: torch.Size([4, 64])
output shape: torch.Size([4, 64, 129280])
四、 参考链接
Deepseekv3权重路径
https://huggingface.co/deepseek-ai/DeepSeek-V3/tree/main
附:671B全量的Deepseekv3网络结构
python
DeepseekV3ForCausalLM(
(model): DeepseekV3Model(
(embed_tokens): Embedding(129280, 7168)
(layers): ModuleList(
(0-2): 3 x DeepseekV3DecoderLayer(
(self_attn): DeepseekV3Attention(
(q_a_proj): Linear(in_features=7168, out_features=1536, bias=False)
(q_a_layernorm): DeepseekV3RMSNorm()
(q_b_proj): Linear(in_features=1536, out_features=24576, bias=False)
(kv_a_proj_with_mqa): Linear(in_features=7168, out_features=576, bias=False)
(kv_a_layernorm): DeepseekV3RMSNorm()
(kv_b_proj): Linear(in_features=512, out_features=32768, bias=False)
(o_proj): Linear(in_features=16384, out_features=7168, bias=False)
(rotary_emb): DeepseekV3YarnRotaryEmbedding()
)
(mlp): DeepseekV3MLP(
(gate_proj): Linear(in_features=7168, out_features=18432, bias=False)
(up_proj): Linear(in_features=7168, out_features=18432, bias=False)
(down_proj): Linear(in_features=18432, out_features=7168, bias=False)
(act_fn): SiLU()
)
(input_layernorm): DeepseekV3RMSNorm()
(post_attention_layernorm): DeepseekV3RMSNorm()
)
(3-60): 58 x DeepseekV3DecoderLayer(
(self_attn): DeepseekV3Attention(
(q_a_proj): Linear(in_features=7168, out_features=1536, bias=False)
(q_a_layernorm): DeepseekV3RMSNorm()
(q_b_proj): Linear(in_features=1536, out_features=24576, bias=False)
(kv_a_proj_with_mqa): Linear(in_features=7168, out_features=576, bias=False)
(kv_a_layernorm): DeepseekV3RMSNorm()
(kv_b_proj): Linear(in_features=512, out_features=32768, bias=False)
(o_proj): Linear(in_features=16384, out_features=7168, bias=False)
(rotary_emb): DeepseekV3YarnRotaryEmbedding()
)
(mlp): DeepseekV3MoE(
(experts): ModuleList(
(0-255): 256 x DeepseekV3MLP(
(gate_proj): Linear(in_features=7168, out_features=2048, bias=False)
(up_proj): Linear(in_features=7168, out_features=2048, bias=False)
(down_proj): Linear(in_features=2048, out_features=7168, bias=False)
(act_fn): SiLU()
)
)
(gate): MoEGate()
(shared_experts): DeepseekV3MLP(
(gate_proj): Linear(in_features=7168, out_features=2048, bias=False)
(up_proj): Linear(in_features=7168, out_features=2048, bias=False)
(down_proj): Linear(in_features=2048, out_features=7168, bias=False)
(act_fn): SiLU()
)
)
(input_layernorm): DeepseekV3RMSNorm()
(post_attention_layernorm): DeepseekV3RMSNorm()
)
)
(norm): DeepseekV3RMSNorm()
)
(lm_head): Linear(in_features=7168, out_features=129280, bias=False)
)