ToB企服应用市场:ToB评测及商务社交产业平台

标题: llama-2大模型布局 [打印本页]

作者: 用多少眼泪才能让你相信    时间: 2024-12-21 03:06
标题: llama-2大模型布局
llama-2大模型布局

llama大模型是Meta公司开源的大语言模型,目前最新的为llama3.1参数量达到了405B,非常恐怖
以7B模型为例,梳理llama-2的模型布局,重点关注其对传统Transformer模型的布局修改
  1. LlamaConfig {
  2.   "_name_or_path": "llama2/llama-2-7b",
  3.   "architectures": [
  4.     "LlamaForCausalLM"
  5.   ],
  6.   "attention_bias": false,
  7.   "bos_token_id": 1,
  8.   "eos_token_id": 2,
  9.   "hidden_act": "silu",
  10.   "hidden_size": 4096,
  11.   "initializer_range": 0.02,
  12.   "intermediate_size": 11008,
  13.   "max_position_embeddings": 2048,
  14.   "model_type": "llama",
  15.   "num_attention_heads": 32,
  16.   "num_hidden_layers": 32,
  17.   "num_key_value_heads": 32,
  18.   "pad_token_id": 0,
  19.   "pretraining_tp": 1,
  20.   "rms_norm_eps": 1e-05,
  21.   "rope_scaling": null,
  22.   "rope_theta": 10000.0,
  23.   "tie_word_embeddings": false,
  24.   "torch_dtype": "bfloat16",
  25.   "transformers_version": "4.31.0",
  26.   "use_cache": true,
  27.   "vocab_size": 32000
  28. }
复制代码
从上述config文件可以看到,

Pytorch界说好模型后,print(model) 的输出:
  1. LlamaForCausalLM(
  2.   (model): LlamaModel(
  3.     (embed_tokens): Embedding(32000, 4096, padding_idx=0)
  4.     (layers): ModuleList(
  5.       (0-31): 32 x LlamaDecoderLayer(
  6.         (self_attn): LlamaAttention(
  7.           (q_proj): Linear(in_features=4096, out_features=4096, bias=False)
  8.           (k_proj): Linear(in_features=4096, out_features=4096, bias=False)
  9.           (v_proj): Linear(in_features=4096, out_features=4096, bias=False)
  10.           (o_proj): Linear(in_features=4096, out_features=4096, bias=False)
  11.           (rotary_emb): LlamaRotaryEmbedding()
  12.         )
  13.         (mlp): LlamaMLP(
  14.           (gate_proj): Linear(in_features=4096, out_features=11008, bias=False)
  15.           (up_proj): Linear(in_features=4096, out_features=11008, bias=False)
  16.           (down_proj): Linear(in_features=11008, out_features=4096, bias=False)
  17.           (act_fn): SiLUActivation()
  18.         )
  19.         (input_layernorm): LlamaRMSNorm()
  20.         (post_attention_layernorm): LlamaRMSNorm()
  21.       )
  22.     )
  23.     (norm): LlamaRMSNorm()
  24.   )
  25.   (lm_head): Linear(in_features=4096, out_features=32000, bias=False)
  26. )
复制代码
可以看到 LLama-2 7b 是由 32 个 LLamaDecoderLayer 堆叠而成;
LLamaDecoderLayer 布局图:

LLama中采用了 RMSNorm 而非传统的 LayerNorm;
LLamaAttention布局图:

LLama将传统Transformer中Transformer Block前的 position embedding 放入到了 Transformer Block中;
并且采用了 LlamaRotaryEmbedding ;
LLamaMLP布局:

对MLP效果也做了修改;
传统的MLP只包罗两个FC层,第一个FC层后有激活函数,第一个FC层起到了隐式地 gating 作用;
而,LLamaMLP中引入了显式地 gate_proj 层,厥后接激活函数,其效果与 up_proj 进行 element-wise 乘法,起到 gating 的作用;

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4