当前位置: 首页> 游戏> 网游 > 成品网页_web前端培训去哪好_如何做好网络营销_网络营销专业好就业吗

成品网页_web前端培训去哪好_如何做好网络营销_网络营销专业好就业吗

时间:2025/7/13 13:06:49来源:https://blog.csdn.net/jiacong_wang/article/details/145560682 浏览次数:0次
成品网页_web前端培训去哪好_如何做好网络营销_网络营销专业好就业吗

背景

当前大模型的权重加载和调用,主要是通过在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库说明

这里展示一段加载模型权重并打印网络结构的代码示例

from transformers import AutoModelForCausalLM
model_path = "model.safetensors"
model = AutoModelForCausalLM.from_pretrained(model_path)
print(model)

其中,AutoModelForCausalLM 是 Hugging Face 的 transformers 库中的一个类,用于加载预训练的因果语言模型。以下是 AutoModelForCausalLM.from_pretrained 方法的入参说明

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脚本

import torch
from configuration_deepseek import DeepseekV3Config
from modeling_deepseek import DeepseekV3ForCausalLMdevice = 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

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网络结构

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)
)
关键字:成品网页_web前端培训去哪好_如何做好网络营销_网络营销专业好就业吗

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: