{"id":2408,"date":"2024-03-10T23:44:22","date_gmt":"2024-03-10T15:44:22","guid":{"rendered":"https:\/\/www.aqwu.net\/wp\/?p=2408"},"modified":"2024-04-28T20:03:13","modified_gmt":"2024-04-28T12:03:13","slug":"%e4%ba%86%e8%a7%a3-llama-2-%e6%a8%a1%e5%9e%8b%e7%bb%93%e6%9e%84","status":"publish","type":"post","link":"https:\/\/www.aqwu.net\/wp\/?p=2408","title":{"rendered":"\u4e86\u89e3 LLaMA-2 \u6a21\u578b\u7ed3\u6784(1)"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>0. \u524d\u8a00<\/strong><\/h2>\n\n\n\n<p>Llama2\uff08\u6709\u65f6\u62fc\u5199\u4e3aLLaMA-2\uff09\u662f\u4e00\u4e2a\u7531Meta Platforms\uff08\u4ee5\u524d\u7684Facebook\uff09\u53d1\u5e03\u7684\u5927\u578b\u8bed\u8a00\u6a21\u578b\uff0c\u65e8\u5728\u7528\u4e8e\u81ea\u7136\u8bed\u8a00\u5904\u7406\uff08NLP\uff09\u4efb\u52a1\u3002\u5b83\u662fLLaMA\uff08Large Language Model Meta AI\uff09\u7cfb\u5217\u7684\u4e00\u90e8\u5206\uff0c\u65e8\u5728\u63d0\u4f9b\u9ad8\u6548\u4e14\u53ef\u6269\u5c55\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u4ee5\u652f\u6301\u5404\u79cdNLP\u548c\u751f\u6210\u4efb\u52a1\u3002\u5230\u6211\u6700\u540e\u7684\u66f4\u65b0\u65f6\uff0c\u5177\u4f53\u7684Llama2\u6a21\u578b\u7ed3\u6784\u7684\u7ec6\u8282\u53ef\u80fd\u8fd8\u672a\u5b8c\u5168\u516c\u5f00\u3002\u7136\u800c\uff0c\u53ef\u4ee5\u6839\u636e\u5176\u524d\u8eab\u548c\u7c7b\u4f3c\u7684\u5927\u578b\u8bed\u8a00\u6a21\u578b\u63a8\u6d4b\u5b83\u7684\u4e00\u822c\u7279\u5f81\u548c\u7ed3\u6784\u3002<\/p>\n\n\n\n<p>\u4e00\u822c\u800c\u8a00\uff0c\u5927\u578b\u8bed\u8a00\u6a21\u578b\u5982LLaMA\u7cfb\u5217\u548c\u5176\u4ed6\u7c7b\u4f3c\u7684\u6a21\u578b\uff08\u4f8b\u5982GPT-3\u3001GPT-4\u3001BERT\u7b49\uff09\u901a\u5e38\u57fa\u4e8e\u53d8\u6362\u5668\uff08Transformer\uff09\u67b6\u6784\u3002\u53d8\u6362\u5668\u67b6\u6784\u7531Vaswani\u7b49\u4eba\u57282017\u5e74\u5f15\u5165\uff0c\u5b83\u7684\u6838\u5fc3\u662f\u81ea\u6ce8\u610f\u529b\uff08self-attention\uff09\u673a\u5236\uff0c\u8fd9\u5141\u8bb8\u6a21\u578b\u5728\u5904\u7406\u8f93\u5165\u6570\u636e\u65f6\u52a8\u6001\u5730\u6743\u8861\u4fe1\u606f\u7684\u91cd\u8981\u6027\u3002<\/p>\n\n\n\n<p>\u53d8\u6362\u5668\u6a21\u578b\u901a\u5e38\u5305\u542b\u4ee5\u4e0b\u4e3b\u8981\u7ec4\u6210\u90e8\u5206\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u81ea\u6ce8\u610f\u529b\u5c42<\/strong>\uff1a\u4f7f\u6a21\u578b\u80fd\u591f\u5904\u7406\u8f93\u5165\u5e8f\u5217\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\uff0c\u5e76\u6839\u636e\u5176\u4ed6\u5143\u7d20\u7684\u5185\u5bb9\u52a8\u6001\u8c03\u6574\u5bf9\u6bcf\u4e2a\u5143\u7d20\u7684\u5173\u6ce8\u5ea6\u3002<\/li>\n\n\n\n<li><strong>\u524d\u9988\u7f51\u7edc\uff08FFN\uff09<\/strong>\uff1a\u5728\u81ea\u6ce8\u610f\u529b\u5c42\u4e4b\u540e\uff0c\u6bcf\u4e2a\u4f4d\u7f6e\u7684\u8f93\u51fa\u4f1a\u901a\u8fc7\u4e00\u4e2a\u524d\u9988\u7f51\u7edc\uff0c\u8fd9\u662f\u4e00\u79cd\u7b80\u5355\u7684\u795e\u7ecf\u7f51\u7edc\uff0c\u7528\u4e8e\u8fdb\u4e00\u6b65\u5904\u7406\u6570\u636e\u3002<\/li>\n\n\n\n<li><strong>\u5c42\u5f52\u4e00\u5316\u548c\u6b8b\u5dee\u8fde\u63a5<\/strong>\uff1a\u4e3a\u4e86\u63d0\u9ad8\u8bad\u7ec3\u8fc7\u7a0b\u7684\u7a33\u5b9a\u6027\u548c\u6548\u7387\uff0c\u901a\u5e38\u5728\u81ea\u6ce8\u610f\u529b\u5c42\u548c\u524d\u9988\u7f51\u7edc\u540e\u52a0\u5165\u5c42\u5f52\u4e00\u5316\u548c\u6b8b\u5dee\u8fde\u63a5\u3002<\/li>\n\n\n\n<li><strong>\u7f16\u7801\u5668\u548c\u89e3\u7801\u5668\u7ed3\u6784<\/strong>\uff1a\u867d\u7136\u4e00\u4e9b\u6a21\u578b\uff08\u5982GPT\u7cfb\u5217\uff09\u4ec5\u4f7f\u7528\u89e3\u7801\u5668\u7ed3\u6784\uff0c\u5176\u4ed6\u6a21\u578b\uff08\u5982BERT\uff09\u5219\u4f7f\u7528\u7f16\u7801\u5668\u7ed3\u6784\uff0c\u8fd8\u6709\u4e00\u4e9b\u6a21\u578b\uff08\u5982\u539f\u59cb\u7684Transformer\uff09\u540c\u65f6\u4f7f\u7528\u7f16\u7801\u5668\u548c\u89e3\u7801\u5668\u3002<\/li>\n<\/ul>\n\n\n\n<p>Llama2\u4f5c\u4e3a\u4e00\u4e2a\u5927\u578b\u8bed\u8a00\u6a21\u578b\uff0c\u53ef\u80fd\u91c7\u7528\u4e86\u8fd9\u4e9b\u57fa\u7840\u7ec4\u4ef6\u7684\u9ad8\u5ea6\u4f18\u5316\u7248\u672c\uff0c\u5e76\u7ed3\u5408\u4e86\u6700\u65b0\u7684\u6280\u672f\u8fdb\u5c55\uff0c\u6bd4\u5982\u7a00\u758f\u6ce8\u610f\u529b\u673a\u5236\u3001\u6548\u7387\u66f4\u9ad8\u7684\u8bad\u7ec3\u6280\u672f\u3001\u4ee5\u53ca\u66f4\u5148\u8fdb\u7684\u53c2\u6570\u5171\u4eab\u7b56\u7565\u7b49\uff0c\u4ee5\u63d0\u9ad8\u6a21\u578b\u7684\u6027\u80fd\u548c\u6548\u7387\u3002<\/p>\n\n\n\n<p>\u8bf7\u6ce8\u610f\uff0c\u5177\u4f53\u7684Llama2\u6a21\u578b\u7ed3\u6784\u7ec6\u8282\u53ef\u80fd\u9700\u8981\u67e5\u9605\u6700\u65b0\u7684\u6587\u732e\u6216Meta Platforms\u53d1\u5e03\u7684\u6280\u672f\u6587\u6863\u6765\u83b7\u53d6\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u73af\u5883\u51c6\u5907<\/strong><\/h2>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \" >conda create -yn llama2\nconda activate llama2\n \ngit clone https:\/\/github.com\/facebookresearch\/llama\n \ncd llama\nmkdir newsrc\nmkdir -p meta-llama\/Llama-2-7b-chat-hf\n<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2.\u4e0b\u8f7d\u6a21\u578b<\/strong><\/h2>\n\n\n\n<p>\u9700\u8981\u4e0b\u8f7d https:\/\/huggingface.co\/meta-llama\/Llama-2-7b-chat-hf\u76ee\u5f55\u4e0b\u7684\u6587\u4ef6<\/p>\n\n\n\n<p>\u6a21\u578b\u6587\u4ef6\u76ee\u5f55\u5185\u5bb9\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \" >ls -l meta-llama\/Llama-2-7b-chat-hf\/\ntotal 13163396\n-rwxrwxrwx 1 tony tony        614 Mar 10 23:51 config.json\n-rwxrwxrwx 1 tony tony        188 Mar 10 23:51 generation_config.json\n-rwxrwxrwx 1 tony tony       1519 Mar 10 23:51 gitattributes\n-rwxrwxrwx 1 tony tony 9976576152 Mar 11 00:58 model-00001-of-00002.safetensors\n-rwxrwxrwx 1 tony tony 3500296424 Mar 11 00:17 model-00002-of-00002.safetensors\n-rwxrwxrwx 1 tony tony      26788 Mar 10 23:51 model.safetensors.index.json\n-rwxrwxrwx 1 tony tony      26788 Mar 10 23:51 pytorch_model.bin.index.json\n-rwxrwxrwx 1 tony tony        414 Mar 10 23:51 special_tokens_map.json\n-rwxrwxrwx 1 tony tony    1842767 Mar 10 23:51 tokenizer.json\n-rwxrwxrwx 1 tony tony     499723 Mar 10 23:51 tokenizer.model\n-rwxrwxrwx 1 tony tony       1618 Mar 10 23:51 tokenizer_config.json<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u6a21\u578b\u7684\u52a0\u8f7d<\/strong><\/h2>\n\n\n\n<p>\u4e3a\u4e86\u8ba9\u4ee3\u7801\u80fd\u591f\u6267\u884c\uff0c\u60a8\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86<code>transformers<\/code>\u5e93\u3002\u5982\u679c\u5c1a\u672a\u5b89\u88c5\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fd0\u884c<code>pip install transformers<\/code>\u547d\u4ee4\u6765\u5b89\u88c5\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \" >pip install transformers<\/pre><\/div>\n\n\n\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u52a0\u8f7dLlama-2-7b-chat-hf\uff0c \u7a0b\u5e8f\u547d\u540d\u4e3a test01.py\uff0c\u6587\u4ef6\u4fdd\u5b58\u5230 newsrc \u76ee\u5f55\u4e0b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >from transformers import AutoModelForCausalLM\n\n# \u6307\u5b9a\u6a21\u578b\u8def\u5f84\nmodel_path = \"meta-llama\/Llama-2-7b-chat-hf\"\n\n# \u52a0\u8f7d\u6a21\u578b\nhf_model = AutoModelForCausalLM.from_pretrained(model_path)\n\n# \u6253\u5370\u6a21\u578b\u4fe1\u606f\nprint(hf_model)\n<\/pre><\/div>\n\n\n\n<p>\u5728\u6a21\u578b\u4e0b\u8f7d\u51c6\u5907\u597d\u540e\uff0c\u6211\u4eec\u8fd0\u884c test01.py<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \" >python newsrc\/test01.py\nLoading checkpoint shards: 100%|\u2588\u2588\u2588\u2588\u2588\u2588\u2588| 2\/2 [02:32&lt;00:00, 76.49s\/it]\nLlamaForCausalLM(\n  (model): LlamaModel(\n    (embed_tokens): Embedding(32000, 4096)\n    (layers): ModuleList(\n      (0-31): 32 x LlamaDecoderLayer(\n        (self_attn): LlamaSdpaAttention(\n          (q_proj): Linear(in_features=4096, out_features=4096, bias=False)\n          (k_proj): Linear(in_features=4096, out_features=4096, bias=False)\n          (v_proj): Linear(in_features=4096, out_features=4096, bias=False)\n          (o_proj): Linear(in_features=4096, out_features=4096, bias=False)\n          (rotary_emb): LlamaRotaryEmbedding()\n        )\n        (mlp): LlamaMLP(\n          (gate_proj): Linear(in_features=4096, out_features=11008, bias=False)\n          (up_proj): Linear(in_features=4096, out_features=11008, bias=False)\n          (down_proj): Linear(in_features=11008, out_features=4096, bias=False)\n          (act_fn): SiLU()\n        )\n        (input_layernorm): LlamaRMSNorm()\n        (post_attention_layernorm): LlamaRMSNorm()\n      )\n    )\n    (norm): LlamaRMSNorm()\n  )\n  (lm_head): Linear(in_features=4096, out_features=32000, bias=False)\n)<\/pre><\/div>\n\n\n\n<p>\u4ece\u63d0\u4f9b\u7684\u8f93\u51fa\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230Llama2\u6a21\u578b\u7684\u7ed3\u6784\u7ec6\u8282\u3002\u8fd9\u4e2a\u6a21\u578b\u662f\u4f7f\u7528<code>transformers<\/code>\u5e93\u52a0\u8f7d\u7684\uff0c\u5176\u8def\u5f84\u662f<code>meta-llama\/Llama-2-7b-chat-hf<\/code>\uff0c\u8868\u660e\u8fd9\u662f\u4e00\u4e2a\u4e13\u4e3a\u804a\u5929\u4efb\u52a1\u4f18\u5316\u76847\u4ebf\u53c2\u6570\u7248\u672c\u7684Llama2\u6a21\u578b\u3002\u4ee5\u4e0b\u662f\u6a21\u578b\u7ed3\u6784\u7684\u5173\u952e\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5d4c\u5165\u5c42\uff08Embedding\uff09<\/strong>\uff1a\u4f7f\u7528\u4e00\u4e2a\u5d4c\u5165\u5c42\u5c06\u8f93\u5165\u7684\u8bcd\u6807\uff08tokens\uff09\u8f6c\u6362\u4e3a\u56fa\u5b9a\u5927\u5c0f\u7684\u5411\u91cf\uff0c\u8fd9\u91cc\u7684\u7ef4\u5ea6\u662f4096\uff0c\u8bcd\u6c47\u8868\u5927\u5c0f\u4e3a32000\u3002<\/li>\n\n\n\n<li><strong>\u89e3\u7801\u5668\u5c42\uff08LlamaDecoderLayer\uff09<\/strong>\uff1a\u5305\u542b32\u4e2a\u89e3\u7801\u5668\u5c42\uff0c\u6bcf\u4e2a\u5c42\u90fd\u5177\u6709\u4ee5\u4e0b\u7ec4\u4ef6\uff1a\n<ul class=\"wp-block-list\">\n<li><strong>\u81ea\u6ce8\u610f\u529b\uff08LlamaSdpaAttention\uff09<\/strong>\uff1a\u4e00\u4e2a\u81ea\u6ce8\u610f\u529b\u673a\u5236\uff0c\u5305\u62ec\u56db\u4e2a\u7ebf\u6027\u53d8\u6362\uff08<code>q_proj<\/code>, <code>k_proj<\/code>, <code>v_proj<\/code>, <code>o_proj<\/code>\uff09\u4ee5\u53ca\u4e00\u4e2a\u65cb\u8f6c\u4f4d\u7f6e\u7f16\u7801\uff08<code>rotary_emb<\/code>\uff09\u3002<\/li>\n\n\n\n<li><strong>\u591a\u5c42\u611f\u77e5\u673a\uff08LlamaMLP\uff09<\/strong>\uff1a\u5305\u62ec\u4e00\u4e2a\u95e8\u63a7\u6295\u5f71\uff08<code>gate_proj<\/code>\uff09\uff0c\u4e0a\u5347\u6295\u5f71\uff08<code>up_proj<\/code>\uff09\uff0c\u4e0b\u964d\u6295\u5f71\uff08<code>down_proj<\/code>\uff09\uff0c\u4ee5\u53ca\u6fc0\u6d3b\u51fd\u6570\uff08<code>act_fn<\/code>\uff09\uff0c\u8fd9\u91cc\u4f7f\u7528\u4e86SiLU\uff08Sigmoid\u7ebf\u6027\u5355\u5143\uff09\u3002<\/li>\n\n\n\n<li><strong>\u5c42\u5f52\u4e00\u5316\uff08LlamaRMSNorm\uff09<\/strong>\uff1a\u5728\u8f93\u5165\u548c\u81ea\u6ce8\u610f\u529b\u540e\u4f7f\u7528RMS\u5f52\u4e00\u5316\uff0c\u6709\u5229\u4e8e\u6a21\u578b\u8bad\u7ec3\u7684\u7a33\u5b9a\u6027\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u8f93\u51fa\u5934\uff08Linear\uff09<\/strong>\uff1a\u6700\u540e\uff0c\u4e00\u4e2a\u7ebf\u6027\u5c42\u5c06\u89e3\u7801\u5668\u7684\u8f93\u51fa\u8f6c\u6362\u56de\u8bcd\u6c47\u8868\u7a7a\u95f4\uff0c\u7528\u4e8e\u751f\u6210\u6700\u7ec8\u7684\u8f93\u51fa\u8bcd\u6807\uff0c\u8fd9\u91cc\u8f93\u51fa\u5c42\u7684\u5927\u5c0f\u4e5f\u662f32000\uff0c\u4e0e\u8bcd\u6c47\u8868\u5927\u5c0f\u76f8\u540c\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u4e2a\u7ed3\u6784\u63ed\u793a\u4e86Llama2\u6a21\u578b\u5728\u5904\u7406\u81ea\u7136\u8bed\u8a00\u4efb\u52a1\u65f6\u7684\u9ad8\u7ea7\u80fd\u529b\uff0c\u901a\u8fc7\u5176\u6df1\u5c42\u7684\u7f51\u7edc\u7ed3\u6784\u3001\u590d\u6742\u7684\u81ea\u6ce8\u610f\u529b\u673a\u5236\u548c\u591a\u5c42\u611f\u77e5\u673a\u7ec4\u4ef6\uff0c\u5b83\u80fd\u591f\u6355\u6349\u548c\u751f\u6210\u590d\u6742\u7684\u8bed\u8a00\u6a21\u5f0f\u3002\u6b64\u5916\uff0c\u8be5\u6a21\u578b\u91c7\u7528\u4e86\u5148\u8fdb\u7684\u6b63\u5219\u5316\u6280\u672f\u548c\u6fc0\u6d3b\u51fd\u6570\uff0c\u8fdb\u4e00\u6b65\u63d0\u5347\u4e86\u6a21\u578b\u7684\u6027\u80fd\u548c\u8bad\u7ec3\u7a33\u5b9a\u6027\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u6a21\u578b\u7684\u53c2\u6570\u5927\u5c0f\u5982\u4f55\u8ba1\u7b97\uff1f<\/strong><\/h2>\n\n\n\n<p>\u8ba1\u7b97\u6a21\u578b\u7684\u53c2\u6570\u5927\u5c0f\u6d89\u53ca\u7edf\u8ba1\u6a21\u578b\u4e2d\u6240\u6709\u53ef\u8bad\u7ec3\u53c2\u6570\u7684\u603b\u6570\u3002\u5728\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\uff0c\u5c24\u5176\u662f\u50cfLlama2\u8fd9\u6837\u7684\u5927\u578b\u53d8\u6362\u5668\u6a21\u578b\u4e2d\uff0c\u8fd9\u4e9b\u53c2\u6570\u4e3b\u8981\u96c6\u4e2d\u5728\u51e0\u4e2a\u5173\u952e\u90e8\u5206\uff1a\u5d4c\u5165\u5c42\uff08embedding layers\uff09\u3001\u81ea\u6ce8\u610f\u529b\u5c42\uff08self-attention layers\uff09\u3001\u524d\u9988\u7f51\u7edc\uff08feed-forward networks\uff09\uff0c\u4ee5\u53ca\u53ef\u80fd\u7684\u5176\u4ed6\u7279\u6b8a\u5c42\u6216\u7ec4\u4ef6\u3002<\/p>\n\n\n\n<p>\u6bcf\u4e2a\u90e8\u5206\u7684\u53c2\u6570\u6570\u91cf\u53ef\u4ee5\u901a\u8fc7\u4e0b\u9762\u7684\u65b9\u6cd5\u8ba1\u7b97\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5d4c\u5165\u5c42\uff08Embedding Layers\uff09<\/strong>:\n<ul class=\"wp-block-list\">\n<li>\u5bf9\u4e8e\u8bcd\u5d4c\u5165\u5c42\uff0c\u53c2\u6570\u6570\u91cf\u901a\u5e38\u662f\u8bcd\u6c47\u8868\u5927\u5c0f\u4e58\u4ee5\u5d4c\u5165\u7ef4\u5ea6\u3002\u4f8b\u5982\uff0c\u5982\u679c\u8bcd\u6c47\u8868\u5927\u5c0f\u662f32,000\u4e14\u5d4c\u5165\u7ef4\u5ea6\u662f4,096\uff0c\u5219\u53c2\u6570\u6570\u91cf\u4e3a<code>32,000 * 4,096<\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u81ea\u6ce8\u610f\u529b\u5c42\uff08Self-Attention Layers\uff09<\/strong>:\n<ul class=\"wp-block-list\">\n<li>\u81ea\u6ce8\u610f\u529b\u5c42\u5305\u542b\u591a\u4e2a\u7ebf\u6027\u53d8\u6362\uff08\u4f8b\u5982\uff0c<code>q_proj<\/code>, <code>k_proj<\/code>, <code>v_proj<\/code>, <code>o_proj<\/code>\uff09\uff0c\u6bcf\u4e2a\u7684\u53c2\u6570\u6570\u91cf\u662f\u8f93\u5165\u7ef4\u5ea6\u4e58\u4ee5\u8f93\u51fa\u7ef4\u5ea6\u3002\u5982\u679c\u6ca1\u6709\u504f\u7f6e\u9879\uff0c\u90a3\u4e48\u5bf9\u4e8e\u6bcf\u4e2a\u6295\u5f71\uff0c\u53c2\u6570\u6570\u91cf\u4e3a<code>in_features * out_features<\/code>\u3002\u8003\u8651\u5230\u6709\u591a\u4e2a\u8fd9\u6837\u7684\u5c42\uff0c\u4e14\u6bcf\u4e2a\u89e3\u7801\u5668\u5c42\u90fd\u5305\u542b\u4e00\u4e2a\u81ea\u6ce8\u610f\u529b\u673a\u5236\uff0c\u603b\u53c2\u6570\u6570\u91cf\u4f1a\u76f8\u5e94\u4e58\u4ee5\u5c42\u7684\u6570\u91cf\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u524d\u9988\u7f51\u7edc\uff08Feed-Forward Networks\uff09<\/strong>:\n<ul class=\"wp-block-list\">\n<li>\u524d\u9988\u7f51\u7edc\u901a\u5e38\u5305\u62ec\u81f3\u5c11\u4e24\u4e2a\u7ebf\u6027\u53d8\u6362\uff0c\u53c2\u6570\u6570\u91cf\u662f\u4e24\u6b21\u53d8\u6362\u7ef4\u5ea6\u7684\u4e58\u79ef\u7684\u548c\u3002\u4f8b\u5982\uff0c\u5982\u679c\u7b2c\u4e00\u4e2a\u53d8\u6362\u5c06\u7ef4\u5ea6\u4ece4,096\u6620\u5c04\u523011,008\uff0c\u7136\u540e\u7b2c\u4e8c\u4e2a\u53d8\u6362\u53c8\u5c06\u5176\u6620\u5c04\u56de4,096\uff0c\u90a3\u4e48\u8fd9\u90e8\u5206\u7684\u53c2\u6570\u6570\u91cf\u4e3a<code>4,096 * 11,008 + 11,008 * 4,096<\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u5176\u4ed6\u5c42\u548c\u7ec4\u4ef6<\/strong>:\n<ul class=\"wp-block-list\">\n<li>\u4efb\u4f55\u989d\u5916\u7684\u5c42\u6216\u7ec4\u4ef6\uff08\u5982\u5c42\u5f52\u4e00\u5316\u3001\u7279\u6b8a\u7684\u6fc0\u6d3b\u51fd\u6570\u7b49\uff09\u4e5f\u4f1a\u6709\u81ea\u5df1\u7684\u53c2\u6570\uff0c\u867d\u7136\u76f8\u6bd4\u4e8e\u4e0a\u8ff0\u90e8\u5206\uff0c\u5b83\u4eec\u7684\u53c2\u6570\u6570\u91cf\u901a\u5e38\u8f83\u5c11\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p>\u7efc\u5408\u8fd9\u4e9b\u4fe1\u606f\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba1\u7b97\u51fa\u6574\u4e2a\u6a21\u578b\u7684\u5927\u81f4\u53c2\u6570\u5927\u5c0f\u3002\u4f8b\u5982\uff0c\u7ed9\u5b9a\u4e00\u4e2a\u5177\u670932\u4e2a\u89e3\u7801\u5668\u5c42\u7684\u6a21\u578b\uff0c\u6bcf\u4e2a\u5c42\u7684\u53c2\u6570\u6570\u91cf\u53ef\u4ee5\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\u8ba1\u7b97\uff0c\u7136\u540e\u5c06\u6240\u6709\u5c42\u7684\u53c2\u6570\u6570\u91cf\u76f8\u52a0\u4ee5\u5f97\u5230\u603b\u6570\u3002\u5bf9\u4e8eLlama2\u8fd9\u6837\u7684\u590d\u6742\u6a21\u578b\uff0c\u53c2\u6570\u6570\u91cf\u901a\u5e38\u8fbe\u5230\u6570\u4ebf\u751a\u81f3\u6570\u5341\u4ebf\u3002<\/p>\n\n\n\n<p>\u4e3a\u4e86\u63d0\u4f9b\u4e00\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\uff0c\u8ba9\u6211\u4eec\u5047\u8bbe\u4e00\u4e2a\u7b80\u5316\u7684\u8ba1\u7b97\uff0c\u4ec5\u8003\u8651\u5d4c\u5165\u5c42\u548c\u4e00\u4e2a\u81ea\u6ce8\u610f\u529b\u5c42\u7684\u53c2\u6570\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5d4c\u5165\u5c42\uff1a<code>32,000 * 4,096 = 131,072,000<\/code>\uff08\u8bcd\u5d4c\u5165\uff09<\/li>\n\n\n\n<li>\u81ea\u6ce8\u610f\u529b\u5c42\uff08\u5047\u8bbe\u6bcf\u4e2a\u89e3\u7801\u5668\u5c42\u6709\u76f8\u4f3c\u7684\u53c2\u6570\u6570\u91cf\uff09\uff1a\n<ul class=\"wp-block-list\">\n<li>\u4ec5\u8003\u8651\u7ebf\u6027\u53d8\u6362\uff08q, k, v, o\uff09\uff1a<code>4 * (4,096 * 4,096) = 67,108,864<\/code>\uff08\u5355\u5c42\uff09<\/li>\n\n\n\n<li>\u5982\u679c\u670932\u5c42\uff1a<code>67,108,864 * 32 = 2,147,483,648<\/code><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u53ea\u662f\u4e00\u4e2a\u975e\u5e38\u7c97\u7565\u7684\u4f30\u7b97\uff0c\u5b9e\u9645\u7684\u8ba1\u7b97\u9700\u8981\u8003\u8651\u6240\u6709\u5c42\u548c\u7ec4\u4ef6\u7684\u8be6\u7ec6\u53c2\u6570\u3002\u5982\u679c\u4f60\u9700\u8981\u4e00\u4e2a\u7cbe\u786e\u7684\u8ba1\u7b97\uff0c\u901a\u5e38\u6700\u76f4\u63a5\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u6a21\u578b\u7684\u6846\u67b6API\uff08\u4f8b\u5982PyTorch\u6216TensorFlow\uff09\u6765\u81ea\u52a8\u8ba1\u7b97\u6a21\u578b\u7684\u603b\u53c2\u6570\u6570\u91cf\u3002\u5728PyTorch\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528<code>sum(p.numel() for p in model.parameters())<\/code>\u6765\u8ba1\u7b97\u4e00\u4e2a\u6a21\u578b\u7684\u603b\u53c2\u6570\u6570\u91cf\u3002<\/p>\n\n\n\n<p>\u7a0b\u5e8f\u547d\u540d\u4e3a test02.py\uff0c\u6587\u4ef6\u4fdd\u5b58\u5230 newsrc \u76ee\u5f55\u4e0b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >from transformers import AutoModelForCausalLM\n\n# \u6307\u5b9a\u6a21\u578b\u8def\u5f84\nmodel_path = \"meta-llama\/Llama-2-7b-chat-hf\"\n\n# \u52a0\u8f7d\u6a21\u578b\nhf_model = AutoModelForCausalLM.from_pretrained(model_path)\n\n# \u8ba1\u7b97\u53c2\u6570\u6570\u91cf\ntotal_params = sum(p.numel() for p in hf_model.parameters())\n\n# \u8ba1\u7b97\u6a21\u578b\u5927\u5c0f\uff08\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\uff09\uff0c\u5047\u8bbe\u6bcf\u4e2a\u53c2\u6570\u4e3a32\u4f4d\u6d6e\u70b9\u6570\nmodel_size_bytes = total_params * 4  # 4\u5b57\u8282\/\u53c2\u6570\n\n# \u8f6c\u6362\u4e3a\u66f4\u6613\u4e8e\u7406\u89e3\u7684\u5355\u4f4d\uff1aMB\nmodel_size_mb = model_size_bytes \/ (1024 ** 2)\n\nprint(f\"Total Parameters: {total_params}\")\nprint(f\"Model Size: {model_size_mb:.2f} MB\")<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c test01.py<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \" >python newsrc\/test02.py\nLoading checkpoint shards: 100%|\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588\u2588| 2\/2 [01:35&lt;00:00, 47.99s\/it]\nTotal Parameters: 6738415616\nModel Size: 25705.02 MB<\/pre><\/div>\n\n\n\n<p>\u4ece\u63d0\u4f9b\u7684\u811a\u672c\u8fd0\u884c\u7ed3\u679c\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230Llama2\u6a21\u578b\u7684\u603b\u53c2\u6570\u6570\u91cf\u4e3a6,738,415,616\uff0c\u5373\u5927\u7ea66.74\u4ebf\u4e2a\u53c2\u6570\u3002\u6b64\u5916\uff0c\u6a21\u578b\u7684\u5927\u5c0f\u88ab\u62a5\u544a\u4e3a25,705.02MB\uff0c\u6216\u8005\u5927\u7ea625.7GB\u3002<\/p>\n\n\n\n<p>\u6a21\u578b\u7684\u5927\u5c0f\uff08\u4ee5MB\u6216GB\u4e3a\u5355\u4f4d\uff09\u57fa\u4e8e\u5176\u53c2\u6570\u7684\u5b58\u50a8\u9700\u6c42\u3002\u6bcf\u4e2a\u53c2\u6570\u901a\u5e38\u4ee5\u6d6e\u70b9\u6570\uff08\u598232\u4f4d\u6d6e\u70b9\u6570\u621664\u4f4d\u6d6e\u70b9\u6570\uff09\u7684\u5f62\u5f0f\u5b58\u50a8\u3002\u5728\u6df1\u5ea6\u5b66\u4e60\u4e2d\uff0c32\u4f4d\u6d6e\u70b9\u6570\uff08\u5373float32\uff09\u662f\u6700\u5e38\u7528\u7684\u53c2\u6570\u7c7b\u578b\uff0c\u6bcf\u4e2a\u53c2\u6570\u9700\u89814\u5b57\u8282\u7684\u5b58\u50a8\u7a7a\u95f4\u3002\u5982\u679c\u6211\u4eec\u5047\u8bbeLlama2\u6a21\u578b\u7684\u53c2\u6570\u4f7f\u7528\u7684\u662f32\u4f4d\u6d6e\u70b9\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5982\u4e0b\u8ba1\u7b97\u6a21\u578b\u7684\u7406\u8bba\u5927\u5c0f\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"759\" height=\"140\" src=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/03\/\u56fe\u7247.png\" alt=\"\" class=\"wp-image-2420\" srcset=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/03\/\u56fe\u7247.png 759w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/03\/\u56fe\u7247-300x55.png 300w\" sizes=\"auto, (max-width: 759px) 100vw, 759px\" \/><\/figure>\n\n\n\n<p>\u8ba9\u6211\u4eec\u6839\u636e\u7ed9\u51fa\u7684\u53c2\u6570\u6570\u91cf\u6765\u5b9e\u9645\u8ba1\u7b97\u4e00\u4e0b\u6a21\u578b\u5927\u5c0f\u3002<\/p>\n\n\n\n<p>\u6839\u636e\u8ba1\u7b97\uff0cLlama2\u6a21\u578b\u7684\u5927\u5c0f\u786e\u5b9e\u7ea6\u4e3a25,705.02MB\uff0c\u6216\u5927\u7ea625.7GB\uff0c\u8fd9\u4e0e\u4f60\u63d0\u4f9b\u7684\u811a\u672c\u8fd0\u884c\u7ed3\u679c\u4e00\u81f4\u3002\u8fd9\u4e2a\u5927\u5c0f\u8bf4\u660e\u4e86\u6a21\u578b\u53c2\u6570\u7684\u5e9e\u5927\u548c\u6a21\u578b\u7ed3\u6784\u7684\u590d\u6742\u6027\uff0c\u53cd\u6620\u4e86\u5176\u5728\u5904\u7406\u5404\u79cd\u81ea\u7136\u8bed\u8a00\u5904\u7406\u4efb\u52a1\u65f6\u7684\u5f3a\u5927\u80fd\u529b\u3002\u8fd9\u79cd\u6a21\u578b\u5927\u5c0f\u4e5f\u610f\u5473\u7740\u5728\u8d44\u6e90\u6709\u9650\u7684\u73af\u5883\u4e2d\u8fd0\u884c\u5b83\u53ef\u80fd\u4f1a\u9047\u5230\u6311\u6218\uff0c\u5c24\u5176\u662f\u5728\u5185\u5b58\u5bb9\u91cf\u53d7\u9650\u7684\u8bbe\u5907\u4e0a\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>0. \u524d\u8a00 Llama2\uff08\u6709\u65f6\u62fc\u5199\u4e3aLLaMA-2\uff09\u662f\u4e00\u4e2a\u7531Meta Platforms\uff08\u4ee5\u524d\u7684Faceboo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"set","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[313,289,443,442,312],"tags":[242,314],"class_list":["post-2408","post","type-post","status-publish","format-standard","hentry","category-chatgpt","category-gpt","category-llm","category-llms","category-openai","tag-chatgpt","tag-openai-api"],"views":4314,"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/2408","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2408"}],"version-history":[{"count":13,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/2408\/revisions"}],"predecessor-version":[{"id":2423,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/2408\/revisions\/2423"}],"wp:attachment":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}