{"id":3869,"date":"2024-05-21T19:06:09","date_gmt":"2024-05-21T11:06:09","guid":{"rendered":"https:\/\/www.aqwu.net\/wp\/?p=3869"},"modified":"2024-05-22T21:47:48","modified_gmt":"2024-05-22T13:47:48","slug":"llama3-%e4%bb%8e%e5%a4%b4%e5%bc%80%e5%a7%8b%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/www.aqwu.net\/wp\/?p=3869","title":{"rendered":"llama3 \u4ece\u5934\u5f00\u59cb\u5b9e\u73b0"},"content":{"rendered":"\n<p>\u539f\u6587\u53c2\u8003\u8fde\u63a5\uff1a<a href=\"https:\/\/github.com\/naklecha\/llama3-from-scratch\">https:\/\/github.com\/naklecha\/llama3-from-scratch<\/a><\/p>\n\n\n\n<p>\u5728\u672c\u7bc7\u6587\u7ae0\u4e2d\uff0c\u4ece\u5934\u5f00\u59cb\u5b9e\u73b0\u4e86 llama3\uff0c\u4e00\u6b21\u4e00\u4e2a\u5f20\u91cf\u548c\u77e9\u9635\u4e58\u6cd5\u3002<\/p>\n\n\n\n<p>\u6b64\u5916\uff0c\u5c06\u76f4\u63a5\u4ece Meta \u4e3a llama3 \u63d0\u4f9b\u7684\u6a21\u578b\u6587\u4ef6\u52a0\u8f7d\u5f20\u91cf\uff0c\u60a8\u9700\u8981\u5728\u8fd0\u884c\u6b64\u6587\u4ef6\u4e4b\u524d\u4e0b\u8f7d\u6743\u91cd\u3002\u4ee5\u4e0b\u662f\u4e0b\u8f7dllama3\u6743\u91cd\u7684\u5b98\u65b9\u94fe\u63a5\uff1a https:\/\/llama.meta.com\/llama-downloads\/<\/p>\n\n\n\n<p>\u5b9e\u9a8c\u662f\u5728 Windows 11 \u73af\u5883\u8fdb\u884c\u7684\uff0c\u4f7f\u7528 jupyter notebook \u8fdb\u884c\u8c03\u8bd5<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u521b\u5efa jupyter \u73af\u5883<\/strong><\/h2>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \">conda create -n jupyter_env python==3.11\nconda activate jupyter_env\n\nconda install -c conda-forge notebook\nconda install -c conda-forge jupyter_core\n<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.1 \u786e\u8ba4 python.exe \u8def\u5f84<\/strong><\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \">where python.exe\nC:\\Users\\tony1\\anaconda3\\envs\\jupyter_env\\python.exe<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.2 \u786e\u8ba4 pip.exe \u8def\u5f84<\/strong><\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \">where pip.exe\nC:\\Users\\tony1\\anaconda3\\envs\\jupyter_env\\Scripts\\pip.exe<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.3 \u786e\u8ba4 jupyter.exe \u8def\u5f84<\/strong><\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \">where jupyter.exe\nC:\\Users\\tony1\\anaconda3\\envs\\jupyter_env\\Scripts\\jupyter.exe<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.4 \u8fd0\u884c notebook<\/strong><\/h3>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \">jupyter notebook\n<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/strong><\/h2>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \">!pip install sentencepiece\n!pip install tiktoken\n!pip install torch\n!pip install blobfile\n!pip install matplotlib<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u5206\u8bcd\u5668(tokenizer)<\/strong><\/h2>\n\n\n\n<p>\u8fd9\u91cc\u4e0d\u6253\u7b97\u5b9e\u73b0 BPE \u5206\u8bcd\u5668\uff08\u4f46 Andrej Karpathy \u6709\u4e00\u4e2a\u975e\u5e38\u5e72\u51c0\u7684\u5b9e\u73b0\uff09<\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/karpathy\/minbpe\">https:\/\/github.com\/karpathy\/minbpe<\/a><\/p>\n\n\n\n<p>\u53ef\u4ee5\u4ece llama3 \u7684 tokenizer.json \u6587\u4ef6\u77e5\u9053\u5206\u8bcd\u5668\u7c7b\u578b\u662f BPE<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"669\" height=\"1024\" src=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-22-669x1024.png\" alt=\"\" class=\"wp-image-3881\" srcset=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-22-669x1024.png 669w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-22-196x300.png 196w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-22-768x1175.png 768w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-22-1004x1536.png 1004w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-22.png 1176w\" sizes=\"auto, (max-width: 669px) 100vw, 669px\" \/><\/figure>\n\n\n\n<p>\u5728\u6587\u4ef6 tokenizer.json \u7684\u524d\u9762\u8fd8\u6709\u5176\u4ed6\u7279\u6b8a\u7684 tokens<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"837\" height=\"1024\" src=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-21-837x1024.png\" alt=\"\" class=\"wp-image-3880\" srcset=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-21-837x1024.png 837w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-21-245x300.png 245w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-21-768x939.png 768w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-21-1256x1536.png 1256w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-21.png 1430w\" sizes=\"auto, (max-width: 837px) 100vw, 837px\" \/><\/figure>\n\n\n\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u542b\u6709\u4e86 tokenizer.json \u6587\u4ef6\u91cc\u9762\u6240\u6709\u7279\u5b9a\u7684\u5b57\u7b26<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">from pathlib import Path\nimport tiktoken\nfrom tiktoken.load import load_tiktoken_bpe\nimport torch\nimport json\nimport matplotlib.pyplot as plt\n\ntokenizer_path = \"Meta-Llama-3-8B\/tokenizer.model\"\nspecial_tokens = [\n            \"&lt;|begin_of_text|&gt;\",\n            \"&lt;|end_of_text|&gt;\",\n            \"&lt;|reserved_special_token_0|&gt;\",\n            \"&lt;|reserved_special_token_1|&gt;\",\n            \"&lt;|reserved_special_token_2|&gt;\",\n            \"&lt;|reserved_special_token_3|&gt;\",\n            \"&lt;|start_header_id|&gt;\",\n            \"&lt;|end_header_id|&gt;\",\n            \"&lt;|reserved_special_token_4|&gt;\",\n            \"&lt;|eot_id|&gt;\",  # end of turn\n        ] + [f\"&lt;|reserved_special_token_{i}|&gt;\" for i in range(5, 256 - 5)]\nmergeable_ranks = load_tiktoken_bpe(tokenizer_path)\ntokenizer = tiktoken.Encoding(\n    name=Path(tokenizer_path).name,\n    pat_str=r\"(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\\r\\n\\p{L}\\p{N}]?\\p{L}+|\\p{N}{1,3}| ?[^\\s\\p{L}\\p{N}]+[\\r\\n]*|\\s*[\\r\\n]+|\\s+(?!\\S)|\\s+\",\n    mergeable_ranks=mergeable_ranks,\n    special_tokens={token: len(mergeable_ranks) + i for i, token in enumerate(special_tokens)},\n)\n\nprint(tokenizer.n_vocab)\nprint(tokenizer.decode(tokenizer.encode(\"hello world!\")))<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \">128256\nhello world!<\/pre><\/div>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230 tokenizer.n_vocab \u7684\u503c\u4e3a 128256\uff0c\u548c config.json \u6587\u4ef6\u91cc\u7684 vocab_size \u4e00\u81f4<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"817\" height=\"1024\" src=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-23-817x1024.png\" alt=\"\" class=\"wp-image-3888\" srcset=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-23-817x1024.png 817w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-23-239x300.png 239w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-23-768x962.png 768w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-23-1226x1536.png 1226w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-23.png 1231w\" sizes=\"auto, (max-width: 817px) 100vw, 817px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u8bfb\u53d6\u6a21\u578b\u6587\u4ef6<\/strong><\/h2>\n\n\n\n<p>\u4ece\u5934\u5f00\u59cb\u5b9e\u73b0 llama3\uff0c\u6211\u4eec\u5c06\u4e00\u6b21\u8bfb\u53d6\u4e00\u4e2a\u5f20\u91cf\u6587\u4ef6\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \">model = torch.load(\"Meta-Llama-3-8B\/consolidated.00.pth\")\n# Print the first 20 keys from the model dictionary\nprint(json.dumps(list(model.keys())[:20], indent=4))\n\n# Print the last two keys from the model dictionary\nprint(json.dumps(list(model.keys())[-2:], indent=4))<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \">[\n    \"tok_embeddings.weight\",\n    \"layers.0.attention.wq.weight\",\n    \"layers.0.attention.wk.weight\",\n    \"layers.0.attention.wv.weight\",\n    \"layers.0.attention.wo.weight\",\n    \"layers.0.feed_forward.w1.weight\",\n    \"layers.0.feed_forward.w3.weight\",\n    \"layers.0.feed_forward.w2.weight\",\n    \"layers.0.attention_norm.weight\",\n    \"layers.0.ffn_norm.weight\",\n    \"layers.1.attention.wq.weight\",\n    \"layers.1.attention.wk.weight\",\n    \"layers.1.attention.wv.weight\",\n    \"layers.1.attention.wo.weight\",\n    \"layers.1.feed_forward.w1.weight\",\n    \"layers.1.feed_forward.w3.weight\",\n    \"layers.1.feed_forward.w2.weight\",\n    \"layers.1.attention_norm.weight\",\n    \"layers.1.ffn_norm.weight\",\n    \"layers.2.attention.wq.weight\"\n]\n[\n    \"norm.weight\",\n    \"output.weight\"\n]<\/pre><\/div>\n\n\n\n<p>\u8fd9\u4e9b\u952e\u540d\u4ee3\u8868\u4e86\u6a21\u578b\u4e2d\u4e0d\u540c\u90e8\u4ef6\u7684\u53c2\u6570\u3002\u4ee5\u4e0b\u662f\u5bf9\u6bcf\u4e2a\u952e\u7684\u89e3\u91ca\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>tok_embeddings.weight<\/strong> &#8211; \u4ee3\u8868\u8bcd\u5d4c\u5165\u5c42\u7684\u6743\u91cd\uff0c\u5b83\u8d1f\u8d23\u5c06\u8f93\u5165\u7684\u5355\u8bcd\uff08\u6216\u6807\u8bb0\uff09\u8f6c\u6362\u4e3a\u56fa\u5b9a\u5927\u5c0f\u7684\u5411\u91cf\u3002<\/li>\n\n\n\n<li><strong>layers.x.attention.wq.weight, wk.weight, wv.weight, wo.weight<\/strong> &#8211; \u8fd9\u4e9b\u5206\u522b\u8868\u793a\u5728Transformer\u7684\u7b2cx\u5c42\u4e2d\uff0c\u591a\u5934\u6ce8\u610f\u529b\uff08multi-head attention\uff09\u673a\u5236\u7684\u67e5\u8be2\uff08Query\uff09\u3001\u952e\uff08Key\uff09\u3001\u503c\uff08Value\uff09\u548c\u8f93\u51fa\uff08Output\uff09\u7684\u6743\u91cd\u3002<\/li>\n\n\n\n<li><strong>layers.x.feed_forward.w1.weight, w2.weight, w3.weight<\/strong> &#8211; \u8fd9\u4e9b\u4ee3\u8868\u5728\u7b2cx\u5c42\u7684\u524d\u9988\u795e\u7ecf\u7f51\u7edc\uff08feed-forward network, FFN\uff09\u4e2d\u7684\u4e0d\u540c\u6743\u91cd\u3002\u901a\u5e38\u5728BERT\u6216Transformer\u6a21\u578b\u4e2d\uff0cFFN\u5305\u542b\u4e24\u4e2a\u7ebf\u6027\u53d8\u6362\uff0c\u8fd9\u91ccw1\u548cw2\u53ef\u80fd\u4ee3\u8868\u8fd9\u4e24\u4e2a\u53d8\u6362\u7684\u6743\u91cd\uff0cw3\u53ef\u80fd\u662f\u6269\u5c55\u5b9e\u73b0\u7684\u4e00\u90e8\u5206\u3002<\/li>\n\n\n\n<li><strong>layers.x.attention_norm.weight, layers.x.ffn_norm.weight<\/strong> &#8211; \u8fd9\u4e9b\u662f\u5e94\u7528\u5728\u591a\u5934\u6ce8\u610f\u529b\u548c\u524d\u9988\u795e\u7ecf\u7f51\u7edc\u8f93\u51fa\u4e0a\u7684\u5c42\u5f52\u4e00\u5316\uff08layer normalization\uff09\u7684\u6743\u91cd\u3002<\/li>\n\n\n\n<li><strong>norm.weight<\/strong> &#8211; \u8fd9\u53ef\u80fd\u662f\u6a21\u578b\u6700\u540e\u7684\u5c42\u5f52\u4e00\u5316\u6743\u91cd\uff0c\u7528\u4e8e\u6574\u4e2a\u6a21\u578b\u8f93\u51fa\u7684\u6700\u7ec8\u5f52\u4e00\u5316\u5904\u7406\u3002<\/li>\n\n\n\n<li><strong>output.weight<\/strong> &#8211; \u8fd9\u662f\u6a21\u578b\u8f93\u51fa\u5c42\u7684\u6743\u91cd\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u4ee5\u4e0a\u6bcf\u4e2a\u53c2\u6570\u90fd\u5728\u6a21\u578b\u5185\u90e8\u627f\u62c5\u7740\u91cd\u8981\u7684\u89d2\u8272\uff0c\u786e\u4fdd\u4fe1\u606f\u80fd\u591f\u5728\u6a21\u578b\u5c42\u95f4\u6709\u6548\u4f20\u9012\u5e76\u8fdb\u884c\u9002\u5f53\u7684\u53d8\u6362\u3002<\/p>\n\n\n\n<p>\u4e0b\u9762\u662f\u67e5\u770b\u5176\u4ed6\u6a21\u578b\u53c2\u6570\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">with open(\"Meta-Llama-3-8B\/params.json\", \"r\") as f:\n    config = json.load(f)\nconfig<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \">{'dim': 4096,\n 'n_layers': 32,\n 'n_heads': 32,\n 'n_kv_heads': 8,\n 'vocab_size': 128256,\n 'multiple_of': 1024,\n 'ffn_dim_multiplier': 1.3,\n 'norm_eps': 1e-05,\n 'rope_theta': 500000.0}<\/pre><\/div>\n\n\n\n<p>\u8fd9\u4e9b\u53c2\u6570\u662f\u7528\u4e8e\u914d\u7f6e\u4e00\u4e2a\u57fa\u4e8eTransformer\u67b6\u6784\u7684\u795e\u7ecf\u7f51\u7edc\u6a21\u578b\u7684\u5404\u79cd\u8bbe\u7f6e\u3002\u6bcf\u4e2a\u53c2\u6570\u7684\u5177\u4f53\u4f5c\u7528\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>dim<\/strong>: \u8868\u793a\u6a21\u578b\u4e2d\u6bcf\u5c42\u7684\u7ef4\u5ea6\u6216\u9690\u85cf\u5c42\u7684\u5927\u5c0f\u3002\u5728\u8fd9\u91cc\uff0c\u5b83\u88ab\u8bbe\u7f6e\u4e3a4096\uff0c\u610f\u5473\u7740\u6bcf\u4e2aTransformer\u5c42\u7684\u5185\u90e8\u5411\u91cf\u5c06\u67094096\u4e2a\u7ef4\u5ea6\u3002<\/li>\n\n\n\n<li><strong>n_layers<\/strong>: \u6a21\u578b\u4e2d\u7684\u5c42\u6570\u3002\u8fd9\u4e2a\u53c2\u6570\u503c\u4e3a32\uff0c\u610f\u5473\u7740\u8fd9\u4e2a\u6a21\u578b\u670932\u5c42Transformer\u5c42\u3002<\/li>\n\n\n\n<li><strong>n_heads<\/strong>: \u591a\u5934\u6ce8\u610f\u529b\u673a\u5236\u4e2d\u5934\u7684\u6570\u91cf\u3002\u5728\u8fd9\u91cc\uff0c\u5b83\u662f32\uff0c\u8868\u793a\u6bcf\u4e2a\u6ce8\u610f\u529b\u5c42\u4f1a\u670932\u4e2a\u4e0d\u540c\u7684\u5934\u5e76\u884c\u5904\u7406\u4fe1\u606f\u3002<\/li>\n\n\n\n<li><strong>n_kv_heads<\/strong>: \u7279\u5b9a\u4e8e\u67d0\u4e9b\u6a21\u578b\u7684\u53c2\u6570\uff0c\u53ef\u80fd\u8868\u793a\u5728\u5904\u7406\u952e\uff08K\uff09\u548c\u503c\uff08V\uff09\u65f6\u4f7f\u7528\u7684\u6ce8\u610f\u529b\u5934\u7684\u6570\u91cf\u3002\u5728\u8fd9\u4e2a\u6a21\u578b\u4e2d\uff0c\u8fd9\u4e2a\u6570\u503c\u4e3a8\u3002<\/li>\n\n\n\n<li><strong>vocab_size<\/strong>: \u8bcd\u6c47\u8868\u7684\u5927\u5c0f\uff0c\u5373\u6a21\u578b\u53ef\u4ee5\u8bc6\u522b\u7684\u4e0d\u540c\u5355\u8bcd\u6216\u6807\u8bb0\u7684\u603b\u6570\u3002\u8fd9\u91cc\u8bbe\u7f6e\u4e3a128,256\u3002<\/li>\n\n\n\n<li><strong>multiple_of<\/strong>: \u8fd9\u901a\u5e38\u7528\u4e8e\u786e\u4fdd\u6a21\u578b\u7ef4\u5ea6\u7684\u4e00\u4e9b\u65b9\u9762\uff08\u5982\u5c42\u5927\u5c0f\uff09\u662f\u67d0\u4e2a\u6570\u503c\uff081024\uff09\u7684\u500d\u6570\uff0c\u8fd9\u6709\u52a9\u4e8e\u4f18\u5316\u8ba1\u7b97\u6548\u7387\u3002<\/li>\n\n\n\n<li><strong>ffn_dim_multiplier<\/strong>: \u524d\u9988\u7f51\u7edc\u7ef4\u5ea6\u4e58\u6570\u3002\u8fd9\u91cc\u662f1.3\uff0c\u610f\u5473\u7740\u524d\u9988\u5c42\u7684\u5185\u90e8\u7ef4\u5ea6\u662f\u8f93\u5165\/\u8f93\u51fa\u7ef4\u5ea6\uff08dim\uff09\u76841.3\u500d\uff0c\u5373\u8ba1\u7b97\u4e3a (4096 \\times 1.3).<\/li>\n\n\n\n<li><strong>norm_eps<\/strong>: \u5c42\u5f52\u4e00\u5316\u64cd\u4f5c\u4e2d\u4f7f\u7528\u7684\u4e00\u4e2a\u6781\u5c0f\u6570\uff08epsilon\uff09\uff0c\u7528\u4e8e\u907f\u514d\u9664\u4ee5\u96f6\u7684\u9519\u8bef\u3002\u8fd9\u91cc\u8bbe\u7f6e\u4e3a (10^{-5})\u3002<\/li>\n\n\n\n<li><strong>rope_theta<\/strong>: \u7279\u5b9a\u4e8e\u67d0\u4e9bTransformer\u6a21\u578b\u7684\u53c2\u6570\uff0c\u53ef\u80fd\u4e0e\u65cb\u8f6c\u4f4d\u7f6e\u7f16\u7801\uff08RoPE\uff09\u6709\u5173\u3002\u5728\u8fd9\u91cc\uff0c\u5b83\u8bbe\u7f6e\u4e3a500000.0\uff0c\u8fd9\u53ef\u80fd\u5f71\u54cd\u6a21\u578b\u5904\u7406\u4f4d\u7f6e\u4fe1\u606f\u7684\u65b9\u5f0f\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u8fd9\u4e9b\u53c2\u6570\u5171\u540c\u5b9a\u4e49\u4e86\u4e00\u4e2a\u590d\u6742\u7684\u6df1\u5ea6\u5b66\u4e60\u6a21\u578b\u7684\u7ed3\u6784\u548c\u884c\u4e3a\uff0c\u6bcf\u4e2a\u90fd\u5bf9\u6a21\u578b\u7684\u6027\u80fd\u548c\u80fd\u529b\u4ea7\u751f\u91cd\u8981\u5f71\u54cd\u3002<\/p>\n\n\n\n<p><strong>\u4fdd\u5b58\u8fd9\u4e9b\u53c2\u6570<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">dim = config[\"dim\"]\nn_layers = config[\"n_layers\"]\nn_heads = config[\"n_heads\"]\nn_kv_heads = config[\"n_kv_heads\"]\nvocab_size = config[\"vocab_size\"]\nmultiple_of = config[\"multiple_of\"]\nffn_dim_multiplier = config[\"ffn_dim_multiplier\"]\nnorm_eps = config[\"norm_eps\"]\nrope_theta = torch.tensor(config[\"rope_theta\"])<\/pre><\/div>\n\n\n\n<p>\u8fd9\u4e9b\u884c\u4ece<code>config<\/code>\u7684\u5b57\u5178\u4e2d\u63d0\u53d6\u7279\u5b9a\u7684\u6a21\u578b\u914d\u7f6e\u53c2\u6570\u3002\u8fd9\u5305\u62ec\u6a21\u578b\u7684\u7ef4\u5ea6\u3001\u5c42\u6570\u3001\u5934\u7684\u6570\u91cf\u7b49\uff0c\u4ee5\u53ca\u5c06<code>rope_theta<\/code>\u53c2\u6570\u8f6c\u6362\u4e3a\u4e00\u4e2aPyTorch\u5f20\u91cf\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. \u5c06\u6587\u672c\u8f6c\u6362\u4e3a\u6807\u8bb0<\/strong><\/h2>\n\n\n\n<p>\u5728\u8fd9\u91cc\uff0c\u6211\u4eec\u4f7f\u7528 tiktoken\uff08\u6211\u8ba4\u4e3a\u662f OpenAI \u5e93\uff09\u4f5c\u4e3a\u5206\u8bcd\u5668<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >prompt = \"the answer to the ultimate question of life, the universe, and everything is \"\ntokens = [128000] + tokenizer.encode(prompt)\nprint(tokens)\nprint(len(tokens))<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \" >[128000, 1820, 4320, 311, 279, 17139, 3488, 315, 2324, 11, 279, 15861, 11, 323, 4395, 374, 220]\n17<\/pre><\/div>\n\n\n\n<p>\u8fd9\u91cc <strong>128000<\/strong>\uff0c\u4ee3\u8868 bos_token_id\uff0cbos_token\uff0c&#8221;&lt;|begin_of_text|>&#8221;<\/p>\n\n\n\n<p><strong>17<\/strong> \u8868\u793atokens \u7684\u957f\u5ea6\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u5b9a\u4e49\u4e86\u4e00\u4e2a\u6587\u672c\u63d0\u793a\uff0c\u5e76\u4f7f\u7528<code>tokenizer.encode<\/code>\u65b9\u6cd5\u5c06\u5176\u8f6c\u6362\u4e3a\u6807\u8bb0\u3002\u6807\u8bb0<code>128000<\/code>\u88ab\u6dfb\u52a0\u5230\u7f16\u7801\u7684\u6807\u8bb0\u5217\u8868\u7684\u5f00\u5934\uff0c\u53ef\u80fd\u4ee3\u8868\u67d0\u79cd\u7279\u6b8a\u6216\u8d77\u59cb\u6807\u8bb0\u3002\u63a5\u7740\u6253\u5370\u8fd9\u4e9b\u6807\u8bb0\u53ca\u5176\u6570\u91cf\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u518d\u4ecetokens \u8fd8\u539f\u51fa\u6765\u539f\u6765\u7684\u5185\u5bb9:<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \">tokens = torch.tensor(tokens)\nprompt_split_as_tokens = [tokenizer.decode([token.item()]) for token in tokens]\nprint(prompt_split_as_tokens)<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \" >['&lt;|begin_of_text|&gt;', 'the', ' answer', ' to', ' the', ' ultimate', ' question', ' of', ' life', ',', ' the', ' universe', ',', ' and', ' everything', ' is', ' ']<\/pre><\/div>\n\n\n\n<p>\u5c06\u6807\u8bb0\u5217\u8868\u8f6c\u6362\u4e3aPyTorch\u5f20\u91cf\uff0c\u7136\u540e\u8fed\u4ee3\u6bcf\u4e2a\u6807\u8bb0\uff0c\u5c06\u5176\u89e3\u7801\u56de\u6587\u672c\u5f62\u5f0f\u5e76\u6253\u5370\u3002\u8fd9\u662f\u4e3a\u4e86\u9a8c\u8bc1\u6807\u8bb0\u662f\u5426\u6b63\u786e\u89e3\u7801\u56de\u5176\u539f\u59cb\u6587\u672c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. \u5c06\u4ee4\u724c\u8f6c\u6362\u4e3a\u5176\u5d4c\u5165<\/strong><\/h2>\n\n\n\n<p>\u5bf9\u4e0d\u8d77\uff0c\u8fd9\u662f\u4f7f\u7528\u5185\u7f6e\u795e\u7ecf\u7f51\u7edc\u6a21\u5757\u7684\u4ee3\u7801\u5e93\u4e2d\u552f\u4e00\u90e8\u5206<\/p>\n\n\n\n<p>\u65e0\u8bba\u5982\u4f55\uff0c\u6211\u4eec\u7684 [17&#215;1] \u4ee4\u724c\u73b0\u5728\u662f [17&#215;4096]\uff0c\u5373 17 \u4e2a\u957f\u5ea6\u4e3a 4096 \u7684\u5d4c\u5165\uff08\u6bcf\u4e2a\u4ee4\u724c\u4e00\u4e2a\uff09<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >embedding_layer = torch.nn.Embedding(vocab_size, dim)\nembedding_layer.weight.data.copy_(model[\"tok_embeddings.weight\"])\ntoken_embeddings_unnormalized = embedding_layer(tokens).to(torch.bfloat16)\ntoken_embeddings_unnormalized.shape\nprint(token_embeddings_unnormalized)\n<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >torch.Size([17, 4096])<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \" >tensor([[-1.4305e-04,  1.0777e-04, -1.9646e-04,  ...,  2.0218e-04,\n          1.4842e-05,  3.0136e-04],\n        [ 1.4267e-03, -5.1575e-03,  2.0905e-03,  ...,  1.0437e-02,\n         -9.7656e-03, -7.8735e-03],\n        [-1.1719e-02,  4.4861e-03,  1.1444e-03,  ...,  9.0790e-04,\n          1.0681e-02, -6.7444e-03],\n        ...,\n        [-4.3945e-03, -6.2866e-03,  9.8877e-03,  ...,  5.1575e-03,\n          2.3193e-03, -8.6212e-04],\n        [-2.9144e-03,  1.5335e-03, -2.4605e-04,  ...,  9.7046e-03,\n          4.2114e-03,  7.9956e-03],\n        [-2.1172e-04, -1.5545e-04,  1.5945e-03,  ..., -1.6632e-03,\n          1.1673e-03, -1.1826e-03]], dtype=torch.bfloat16,\n       grad_fn=&lt;ToCopyBackward0&gt;)<\/pre><\/div>\n\n\n\n<p>\u8fd9\u90e8\u5206\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u8bcd\u5d4c\u5165\u5c42\uff0c\u5176\u5927\u5c0f\u4e3a<code>vocab_size<\/code> x <code>dim<\/code>\u3002\u7136\u540e\u5c06\u9884\u8bad\u7ec3\u6a21\u578b\u7684\u8bcd\u5d4c\u5165\u6743\u91cd\u590d\u5236\u5230\u8fd9\u4e2a\u65b0\u5c42\u7684\u6743\u91cd\u4e2d\u3002\u4f7f\u7528\u8fd9\u4e2a\u8bcd\u5d4c\u5165\u5c42\u5c06\u8f93\u5165\u7684\u6807\u8bb0\u8f6c\u6362\u4e3a\u5d4c\u5165\u5411\u91cf\uff0c\u5e76\u5c06\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a<code>bfloat16<\/code>\uff08\u4e00\u79cd\u7528\u4e8e\u6df1\u5ea6\u5b66\u4e60\u4e2d\u8282\u7701\u5185\u5b58\u7684\u6d6e\u70b9\u683c\u5f0f\uff09\u3002\u6700\u540e\uff0c\u6253\u5370\u5d4c\u5165\u5411\u91cf\u7684\u5f62\u72b6(shapes)\u548c\u5185\u5bb9\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>7. \u4f7f\u7528 RMS \u89c4\u8303\u5316\u5bf9\u5d4c\u5165\u8fdb\u884c\u89c4\u8303\u5316<\/strong><\/h2>\n\n\n\n<p>\u8bf7\u6ce8\u610f\uff0c\u5728\u6b64\u6b65\u9aa4\u4e4b\u540e\uff0c\u5f62\u72b6(shapes)\u4e0d\u4f1a\u6539\u53d8\uff0c\u503c\u53ea\u662f\u5f52\u4e00\u5316<\/p>\n\n\n\n<p>\u8981\u8bb0\u4f4f\u7684\u4e8b\u60c5\uff0c\u6211\u4eec\u9700\u8981\u4e00\u4e2anorm_eps\uff08\u6765\u81ea config\uff09\uff0c\u56e0\u4e3a\u6211\u4eec\u4e0d\u60f3\u610f\u5916\u5730\u5c06 rms \u8bbe\u7f6e\u4e3a 0 \u5e76\u9664\u4ee5 0<\/p>\n\n\n\n<p>\u516c\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"718\" height=\"139\" src=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-24.png\" alt=\"\" class=\"wp-image-3910\" srcset=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-24.png 718w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-24-300x58.png 300w\" sizes=\"auto, (max-width: 718px) 100vw, 718px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" ># def rms_norm(tensor, norm_weights):\n#     rms = (tensor.pow(2).mean(-1, keepdim=True) + norm_eps)**0.5\n#     return tensor * (norm_weights \/ rms)\ndef rms_norm(tensor, norm_weights):\n    return (tensor * torch.rsqrt(tensor.pow(2).mean(-1, keepdim=True) + norm_eps)) * norm_weights<\/pre><\/div>\n\n\n\n<p>\u8fd9\u4e2aPython\u51fd\u6570<code>rms_norm<\/code>\u662f\u4e00\u4e2a\u5e94\u7528\u6839\u5747\u65b9\uff08RMS\uff09\u6807\u51c6\u5316\u6280\u672f\u6765\u6807\u51c6\u5316\u5f20\u91cf\u7684\u51fd\u6570\uff0c\u5e76\u4f7f\u7528\u6743\u91cd\u8fdb\u884c\u7f29\u653e\u3002<\/p>\n\n\n\n<p>\u51fd\u6570<code>rms_norm<\/code>\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tensor<\/code>\uff1a\u5f85\u6807\u51c6\u5316\u7684\u5f20\u91cf\u3002<\/li>\n\n\n\n<li><code>norm_weights<\/code>\uff1a\u7528\u4e8e\u7f29\u653e\u6807\u51c6\u5316\u5f20\u91cf\u7684\u6743\u91cd\u5f20\u91cf\u3002<\/li>\n<\/ul>\n\n\n\n<ol class=\"wp-block-list\"><\/ol>\n\n\n\n<p><strong>\u6807\u51c6\u5316\u8ba1\u7b97<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>tensor.pow(2)<\/code>: \u5c06\u5f20\u91cf\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5e73\u65b9\u3002<\/li>\n\n\n\n<li><code>.mean(-1, keepdim=True)<\/code>: \u8ba1\u7b97\u8fd9\u4e9b\u5e73\u65b9\u503c\u6cbf\u6700\u540e\u4e00\u4e2a\u7ef4\u5ea6\u7684\u5e73\u5747\u503c\uff0c\u5e76\u4fdd\u6301\u7ef4\u5ea6\u4ee5\u4fbf\u8fdb\u884c\u5e7f\u64ad\u3002<\/li>\n\n\n\n<li><code>+ norm_eps<\/code>: \u5728\u5e73\u5747\u503c\u4e0a\u52a0\u4e0a\u4e00\u4e2a\u5c0f\u6570\uff08<code>norm_eps<\/code>\uff09\uff0c\u9632\u6b62\u9664\u96f6\uff0c\u8fd9\u5bf9\u4e8e\u6570\u503c\u7a33\u5b9a\u6027\u662f\u5fc5\u9700\u7684\u3002<\/li>\n\n\n\n<li><code>torch.rsqrt(...)<\/code>: \u8ba1\u7b97\u8fd9\u4e2a\u7ed3\u679c\u7684\u5e73\u65b9\u6839\u7684\u5012\u6570\u3002<\/li>\n\n\n\n<li><code>tensor * torch.rsqrt(...)<\/code>: \u5c06\u539f\u59cb\u5f20\u91cf\u4e58\u4ee5\u8fd9\u4e2a\u503c\u8fdb\u884c\u6807\u51c6\u5316\u3002<\/li>\n\n\n\n<li><code>... * norm_weights<\/code>: \u6700\u540e\uff0c\u7528<code>norm_weights<\/code>\u7f29\u653e\u6807\u51c6\u5316\u540e\u7684\u5f20\u91cf\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u6574\u4e2a\u64cd\u4f5c\u786e\u4fdd\u4e86\u5f20\u91cf\u57fa\u4e8e\u5176\u6700\u540e\u4e00\u4e2a\u7ef4\u5ea6\u4e0a\u7684RMS\u503c\u88ab\u7f29\u5c0f\uff0c\u7136\u540e\u901a\u8fc7<code>norm_weights<\/code>\u8fdb\u884c\u8c03\u6574\uff0c\u8fd9\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u4e00\u79cd\u53c2\u6570\u5316\u7684\u6807\u51c6\u5316\u5f62\u5f0f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>8. \u6784\u5efa\u53d8\u538b\u5668\u7684\u7b2c\u4e00\u5c42\u00a0<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8.1 \u00a0\u89c4\u8303\u5316<\/strong><\/h3>\n\n\n\n<p>\u60a8\u5c06\u770b\u5230\u6211\u4ece\u6a21\u578b\u5b57\u5178\u8bbf\u95ee layer.0\uff08\u8fd9\u662f\u7b2c\u4e00\u5c42\uff09<\/p>\n\n\n\n<p>\u65e0\u8bba\u5982\u4f55\uff0c\u5728\u5f52\u4e00\u5316\u540e\uff0c\u6211\u4eec\u7684\u5f62\u72b6\u4ecd\u7136 [17&#215;4096] \u4e0e\u5d4c\u5165\u76f8\u540c\uff0c\u4f46\u5f52\u4e00\u5316<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"898\" height=\"720\" src=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-25.png\" alt=\"\" class=\"wp-image-3913\" srcset=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-25.png 898w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-25-300x241.png 300w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-25-768x616.png 768w\" sizes=\"auto, (max-width: 898px) 100vw, 898px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >token_embeddings = rms_norm(token_embeddings_unnormalized, model[\"layers.0.attention_norm.weight\"])\ntoken_embeddings.shape<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >torch.Size([17, 4096])<\/pre><\/div>\n\n\n\n<p>\u8fd9\u884c\u4ee3\u7801\u8c03\u7528\u4e86\u4e4b\u524d\u5b9a\u4e49\u7684<code>rms_norm<\/code>\u51fd\u6570\uff0c\u7528\u4e8e\u5bf9<code>token_embeddings_unnormalized<\/code>\uff08\u672a\u6807\u51c6\u5316\u7684token\u5d4c\u5165\uff09\u8fdb\u884c\u6807\u51c6\u5316\u5904\u7406\u3002\u6807\u51c6\u5316\u4f7f\u7528\u7684\u6743\u91cd\u662f\u4ece\u6a21\u578b\u4e2d\u63d0\u53d6\u7684\uff0c\u7279\u5b9a\u4e8e\u7b2c0\u5c42\u7684\u6ce8\u610f\u529b\u5c42\u7684\u5f52\u4e00\u5316\u6743\u91cd\uff08<code>model[\"layers.0.attention_norm.weight\"]<\/code>\uff09\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u51fd\u6570\u5c06\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5f20\u91cf<code>token_embeddings<\/code>\uff0c\u5176\u7ef4\u5ea6\u5e94\u4e0e\u8f93\u5165\u5f20\u91cf<code>token_embeddings_unnormalized<\/code>\u4fdd\u6301\u4e00\u81f4\u3002\u56e0\u6b64\uff0c\u5982\u679c\u60a8\u95ee\u53ca<code>token_embeddings.shape<\/code>\u7684\u503c\uff0c\u5b83\u5e94\u8be5\u4e0e<code>token_embeddings_unnormalized<\/code>\u7684\u5f62\u72b6\u76f8\u540c\u3002\u5177\u4f53\u5f62\u72b6\u53d6\u51b3\u4e8e\u8f93\u5165\u5f20\u91cf<code>token_embeddings_unnormalized<\/code>\u7684\u7ef4\u5ea6\uff0c\u901a\u5e38\u5728\u5904\u7406NLP\u4efb\u52a1\u65f6\uff0c\u8fd9\u4e2a\u5f20\u91cf\u7684\u5f62\u72b6\u4e3a<code>(batch_size, sequence_length, embedding_dim)<\/code>\uff0c\u5176\u4e2d<code>embedding_dim<\/code>\u662f\u5728\u524d\u9762\u5b9a\u4e49\u7684<code>dim<\/code>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8.2 \u6ce8\u610f\u5934\u4ece\u5934\u5f00\u59cb\u5b9e\u73b0<\/strong><\/h3>\n\n\n\n<p>\u8ba9\u6211\u4eec\u52a0\u8f7d\u53d8\u538b\u5668\u7b2c\u4e00\u5c42\u7684\u6ce8\u610f\u5934<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"537\" src=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-26-1024x537.png\" alt=\"\" class=\"wp-image-3917\" srcset=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-26-1024x537.png 1024w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-26-300x157.png 300w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-26-768x403.png 768w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-26.png 1222w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u5f53\u6211\u4eec\u4ece\u6a21\u578b\u4e2d\u52a0\u8f7d\u67e5\u8be2\uff08query\uff09\u3001\u952e\uff08key\uff09\u3001\u503c\uff08value\uff09\u548c\u8f93\u51fa\uff08output\uff09\u5411\u91cf\u65f6\uff0c\u6211\u4eec\u6ce8\u610f\u5230\u5b83\u4eec\u7684\u5f62\u72b6\u5206\u522b\u662f [4096&#215;4096]\u3001[1024&#215;4096]\u3001[1024&#215;4096] \u548c [4096&#215;4096]\u3002\u4e4d\u4e00\u770b\u8fd9\u5f88\u5947\u602a\uff0c\u56e0\u4e3a\u7406\u60f3\u60c5\u51b5\u4e0b\u6211\u4eec\u5e0c\u671b\u6bcf\u4e2a\u5934\u5206\u522b\u6709\u81ea\u5df1\u7684q\u3001k\u3001v\u548co\u3002<br>\u4ee3\u7801\u7684\u4f5c\u8005\u5c06\u5b83\u4eec\u6346\u7ed1\u5728\u4e00\u8d77\uff0c\u56e0\u4e3a\u8fd9\u6837\u505a\u5f88\u65b9\u4fbf\uff0c\u6709\u52a9\u4e8e\u5e76\u884c\u5316\u6ce8\u610f\u529b\u5934\u7684\u4e58\u6cd5\u8fd0\u7b97\u3002<br>\u6211\u5c06\u89e3\u5f00\u6240\u6709\u8fd9\u4e9b\u2026<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >print(\n    model[\"layers.0.attention.wq.weight\"].shape,\n    model[\"layers.0.attention.wk.weight\"].shape,\n    model[\"layers.0.attention.wv.weight\"].shape,\n    model[\"layers.0.attention.wo.weight\"].shape\n)<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >torch.Size([4096, 4096]) torch.Size([1024, 4096]) torch.Size([1024, 4096]) torch.Size([4096, 4096])<\/pre><\/div>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>8.3 \u89e3\u5305\u67e5\u8be2<\/strong><\/h3>\n\n\n\n<p>\u5728\u4e0b\u4e00\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u89e3\u5305\u6765\u81ea\u591a\u4e2a\u6ce8\u610f\u529b\u5934\u7684\u67e5\u8be2\uff0c\u751f\u6210\u7684\u5f62\u72b6\u4e3a [32x128x4096]<\/p>\n\n\n\n<p>\u8fd9\u91cc\uff0c32 \u662f llama3 \u4e2d\u7684\u6ce8\u610f\u529b\u5934\u6570\uff0c128 \u662f\u67e5\u8be2\u5411\u91cf\u7684\u5927\u5c0f\uff0c4096 \u662f\u4ee4\u724c\u5d4c\u5165\u7684\u5927\u5c0f<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" ># \u4ece\u6a21\u578b\u4e2d\u83b7\u53d6\u7b2c0\u5c42\u7684\u67e5\u8be2\u6743\u91cd\nq_layer0 = model[\"layers.0.attention.wq.weight\"]\n# \u8ba1\u7b97\u6bcf\u4e2a\u5934\u7684\u7ef4\u5ea6\uff0c\u603b\u7ef4\u5ea6\u9664\u4ee5\u5934\u7684\u6570\u91cf\nhead_dim = q_layer0.shape[0] \/\/ n_heads\n# \u5c06\u67e5\u8be2\u6743\u91cd\u91cd\u5851\u4e3a\u591a\u5934\u683c\u5f0f\uff08\u5934\u6570, \u5934\u7ef4\u5ea6, \u603b\u7ef4\u5ea6\uff09\nq_layer0 = q_layer0.view(n_heads, head_dim, dim)\n\nq_layer0.shape\nhead_dim<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >torch.Size([32, 128, 4096])\nhead_dim<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>9. \u5b9e\u73b0\u7b2c\u4e00\u5c42\u7684\u7b2c\u4e00\u4e2a\u5934<\/strong><\/h2>\n\n\n\n<p>\u8fd9\u91cc\u6211\u8bbf\u95ee\u67e5\u8be2\u6743\u91cd\u77e9\u9635\u7b2c\u4e00\u5c42\u7684\u7b2c\u4e00\u5934\uff0c\u8fd9\u4e2a\u67e5\u8be2\u6743\u91cd\u77e9\u9635\u7684\u5927\u5c0f\u662f[128&#215;4096]<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >\n# \u63d0\u53d6\u7b2c0\u4e2a\u5934\u7684\u67e5\u8be2\u6743\u91cd\nq_layer0_head0 = q_layer0[0]\n\n\nq_layer0_head0.shape<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >torch.Size([128, 4096])<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>10. \u5c06\u67e5\u8be2\u6743\u91cd\u4e0e\u4ee4\u724c\u5d4c\u5165\u76f8\u4e58\uff0c\u4ee5\u63a5\u6536\u5bf9\u4ee4\u724c\u7684\u67e5\u8be2<\/strong><\/h2>\n\n\n\n<p>\u5728\u8fd9\u91cc\uff0c\u60a8\u53ef\u4ee5\u770b\u5230\u751f\u6210\u7684\u5f62\u72b6\u662f [17&#215;128]\uff0c\u8fd9\u662f\u56e0\u4e3a\u6211\u4eec\u6709 17 \u4e2a\u6807\u8bb0\uff0c\u6bcf\u4e2a\u6807\u8bb0\u90fd\u6709\u4e00\u4e2a 128 \u957f\u5ea6\u7684\u67e5\u8be2\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" ># \u5bf9\u6bcf\u4e2a\u6807\u8bb0\u7684\u5d4c\u5165\u4f7f\u7528\u7b2c0\u4e2a\u5934\u7684\u67e5\u8be2\u6743\u91cd\u8fdb\u884c\u77e9\u9635\u4e58\u6cd5\uff0c\u5f97\u5230\u6bcf\u4e2a\u6807\u8bb0\u5bf9\u5e94\u7684\u67e5\u8be2\u8868\u793a\nq_per_token = torch.matmul(token_embeddings, q_layer0_head0.T)\n\nq_per_token.shape<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >torch.Size([17, 128])<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>11. \u5b9a\u4f4d\u7f16\u7801<\/strong><\/h2>\n\n\n\n<p>\u6211\u4eec\u73b0\u5728\u6b63\u5904\u4e8e\u4e00\u4e2a\u9636\u6bb5\uff0c\u6211\u4eec\u5728\u63d0\u793a\u4e2d\u4e3a\u6bcf\u4e2a\u6807\u8bb0\u90fd\u6709\u4e00\u4e2a\u67e5\u8be2\u5411\u91cf\uff0c\u4f46\u5982\u679c\u4f60\u4ed4\u7ec6\u60f3\u60f3\uff0c\u4e2a\u6027\u5316\u67e5\u8be2\u5411\u91cf\u4e0d\u77e5\u9053\u63d0\u793a\u4e2d\u7684\u4f4d\u7f6e\u3002<\/p>\n\n\n\n<p>\u67e5\u8be2\uff1a\u201c\u751f\u547d\u3001\u5b87\u5b99\u548c\u4e07\u7269\u7684\u7ec8\u6781\u95ee\u9898\u7684\u7b54\u6848\u662f\u201d<\/p>\n\n\n\n<p>\u5728\u6211\u4eec\u7684\u63d0\u793a\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u4f7f\u7528\u4e86 \u201cthe\u201d \u4e09\u6b21\uff0c\u6211\u4eec\u9700\u8981\u6240\u6709 3 \u4e2a \u201cthe\u201d \u6807\u8bb0\u7684\u67e5\u8be2\u5411\u91cf\u6839\u636e\u5b83\u4eec\u5728\u67e5\u8be2\u4e2d\u7684\u4f4d\u7f6e\u5177\u6709\u4e0d\u540c\u7684\u67e5\u8be2\u5411\u91cf\uff08\u6bcf\u4e2a\u5927\u5c0f\u4e3a [1&#215;128]\uff09\u3002\u6211\u4eec\u4f7f\u7528 RoPE\uff08\u65cb\u8f6c\u4f4d\u7f6e\u5d4c\u5165\uff09\u6267\u884c\u8fd9\u4e9b\u65cb\u8f6c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"RoPE\"><strong>RoPE<a href=\"http:\/\/localhost:8888\/notebooks\/llama3-from-scratch.ipynb#RoPE\"><\/a><\/strong><\/h3>\n\n\n\n<p>\u89c2\u770b\u6b64\u89c6\u9891\uff08\u8fd9\u662f\u6211\u89c2\u770b\u7684\uff09\u4ee5\u4e86\u89e3\u6570\u5b66\u3002 https:\/\/www.youtube.com\/watch?v=o29P0Kpobz0&amp;t=530s<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"837\" src=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-27-1024x837.png\" alt=\"\" class=\"wp-image-3929\" srcset=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-27-1024x837.png 1024w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-27-300x245.png 300w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-27-768x628.png 768w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-27.png 1151w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >q_per_token_split_into_pairs = q_per_token.float().view(q_per_token.shape[0], -1, 2)\nq_per_token_split_into_pairs.shape<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \" >torch.Size([17, 64, 2])<\/pre><\/div>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u6b65\u9aa4\u4e2d\uff0c\u6211\u4eec\u5c06\u67e5\u8be2\u5411\u91cf\u62c6\u5206\u4e3a\u5bf9\uff0c\u6211\u4eec\u5bf9\u6bcf\u5bf9\u5e94\u7528\u65cb\u8f6c\u89d2\u5ea6\u504f\u79fb\uff01<\/p>\n\n\n\n<p>\u6211\u4eec\u73b0\u5728\u6709\u4e00\u4e2a\u5927\u5c0f\u4e3a [17x64x2] \u7684\u5411\u91cf\uff0c\u8fd9\u662f\u63d0\u793a\u4e2d\u6bcf\u4e2a\u6807\u8bb0\u7684 128 \u4e2a\u957f\u5ea6\u67e5\u8be2\uff0c\u5206\u4e3a 64 \u5bf9\uff01\u8fd9 64 \u5bf9\u4e2d\u7684\u6bcf\u4e00\u5bf9\u90fd\u5c06\u7531 m*\uff08theta\uff09 \u65cb\u8f6c\uff0c\u5176\u4e2d m \u662f\u6211\u4eec\u65cb\u8f6c\u67e5\u8be2\u7684\u4ee4\u724c\u7684\u4f4d\u7f6e\uff01<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"447\" src=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-28-1024x447.png\" alt=\"\" class=\"wp-image-3932\" srcset=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-28-1024x447.png 1024w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-28-300x131.png 300w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-28-768x335.png 768w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-28.png 1534w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u5173\u4e8e <code>q_per_token_split_into_pairs.shape<\/code> \u7684\u4e2d\u6587\u89e3\u91ca\uff1a<\/p>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u9996\u5148\u5c06<code>q_per_token<\/code>\u5f20\u91cf\u7684\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u6d6e\u70b9\u578b\uff0c\u7136\u540e\u8c03\u7528<code>view<\/code>\u65b9\u6cd5\u6765\u6539\u53d8\u5f20\u91cf\u7684\u5f62\u72b6\u3002\u5177\u4f53\u7684\u53d8\u5f62\u64cd\u4f5c\u662f\u4fdd\u6301\u7b2c\u4e00\u4e2a\u7ef4\u5ea6\uff08\u901a\u5e38\u662f\u6279\u5904\u7406\u5927\u5c0f\uff09\u4e0d\u53d8\uff0c\u7b2c\u4e09\u4e2a\u7ef4\u5ea6\u8bbe\u7f6e\u4e3a 2\uff0c\u8fd9\u610f\u5473\u7740\u6bcf\u4e24\u4e2a\u5143\u7d20\u88ab\u5206\u4e3a\u4e00\u7ec4\uff0c\u800c\u7b2c\u4e8c\u4e2a\u7ef4\u5ea6\u4f7f\u7528 <code>-1<\/code> \u81ea\u52a8\u8ba1\u7b97\uff0c\u4ee5\u786e\u4fdd\u5143\u7d20\u603b\u6570\u4fdd\u6301\u4e0d\u53d8\u3002\u8fd9\u6837\u64cd\u4f5c\u901a\u5e38\u7528\u4e8e\u51c6\u5907\u6570\u636e\u4ee5\u9002\u914d\u67d0\u4e9b\u7279\u5b9a\u7684\u5904\u7406\u9700\u6c42\uff0c\u4f8b\u5982\u5728\u67d0\u4e9b\u795e\u7ecf\u7f51\u7edc\u64cd\u4f5c\u4e2d\u9700\u8981\u6210\u5bf9\u5904\u7406\u6570\u636e\u3002<\/p>\n\n\n\n<p>\u6700\u7ec8\u5f97\u5230\u7684 <code>q_per_token_split_into_pairs<\/code> \u7684\u5f62\u72b6\u4f1a\u662f\u4e00\u4e2a\u4e09\u7ef4\u5f20\u91cf\uff0c\u5176\u4e2d\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b2c\u4e00\u7ef4\u5ea6\u662f\u539f\u5f20\u91cf\u7684\u7b2c\u4e00\u7ef4\u5ea6\uff08\u5982\u6279\u6b21\u5927\u5c0f\uff09\u3002<\/li>\n\n\n\n<li>\u7b2c\u4e8c\u7ef4\u5ea6\u662f\u81ea\u52a8\u8ba1\u7b97\u51fa\u7684\uff0c\u786e\u4fdd\u6240\u6709\u5143\u7d20\u90fd\u88ab\u6070\u5f53\u5730\u5206\u914d\u5230\u5927\u5c0f\u4e3a2\u7684\u5c0f\u7ec4\u4e2d\u3002<\/li>\n\n\n\n<li>\u7b2c\u4e09\u7ef4\u5ea6\u56fa\u5b9a\u4e3a2\uff0c\u8868\u793a\u5206\u7ec4\u7684\u5927\u5c0f\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u6837\u7684\u53d8\u5f62\u64cd\u4f5c\u5bf9\u4e8e\u8fdb\u4e00\u6b65\u5904\u7406\u6570\u636e\uff0c\u5982\u5728\u5904\u7406\u53cc\u5411\u5173\u7cfb\u6216\u6210\u5bf9\u7279\u5f81\u65f6\uff0c\u975e\u5e38\u6709\u7528\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>12. \u4f7f\u7528\u590d\u6570\u7684\u70b9\u79ef\u65cb\u8f6c\u5411\u91cf<\/strong><\/h2>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u00a0<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"839\" src=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-29-1024x839.png\" alt=\"\" class=\"wp-image-3934\" srcset=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-29-1024x839.png 1024w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-29-300x246.png 300w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-29-768x629.png 768w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-29.png 1526w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >zero_to_one_split_into_64_parts = torch.tensor(range(64))\/64\nzero_to_one_split_into_64_parts<\/pre><\/div>\n\n\n\n<pre class=\"wp-block-preformatted\">tensor([0.0000, 0.0156, 0.0312, 0.0469, 0.0625, 0.0781, 0.0938, 0.1094, 0.1250,<br>        0.1406, 0.1562, 0.1719, 0.1875, 0.2031, 0.2188, 0.2344, 0.2500, 0.2656,<br>        0.2812, 0.2969, 0.3125, 0.3281, 0.3438, 0.3594, 0.3750, 0.3906, 0.4062,<br>        0.4219, 0.4375, 0.4531, 0.4688, 0.4844, 0.5000, 0.5156, 0.5312, 0.5469,<br>        0.5625, 0.5781, 0.5938, 0.6094, 0.6250, 0.6406, 0.6562, 0.6719, 0.6875,<br>        0.7031, 0.7188, 0.7344, 0.7500, 0.7656, 0.7812, 0.7969, 0.8125, 0.8281,<br>        0.8438, 0.8594, 0.8750, 0.8906, 0.9062, 0.9219, 0.9375, 0.9531, 0.9688,<br>        0.9844])<br><\/pre>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u4ece0\u52301\u5747\u5300\u5206\u621064\u90e8\u5206\u7684\u5f20\u91cf\u3002\u5177\u4f53\u89e3\u91ca\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u521b\u5efa\u5e8f\u5217<\/strong>\uff1a <code>range(64)<\/code>\u751f\u6210\u4e86\u4e00\u4e2a\u4ece0\u523063\u7684\u6574\u6570\u5e8f\u5217\u3002<\/li>\n\n\n\n<li><strong>\u8f6c\u6362\u4e3a\u5f20\u91cf<\/strong>\uff1a <code>torch.tensor(range(64))<\/code>\u5c06\u8fd9\u4e2a\u5e8f\u5217\u8f6c\u6362\u6210\u4e86\u4e00\u4e2aPyTorch\u5f20\u91cf\u3002<\/li>\n\n\n\n<li><strong>\u9664\u4ee564<\/strong>\uff1a <code>...\/64<\/code>\u64cd\u4f5c\u5c06\u5f20\u91cf\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u9664\u4ee564\uff0c\u56e0\u6b64\u5143\u7d20\u7684\u503c\u4ece0\u5f00\u59cb\uff080\/64\uff09\uff0c\u4ee51\/64\u4e3a\u6b65\u957f\uff0c\u6700\u7ec8\u5230\u8fbe63\/64\uff0c\u4e0d\u5305\u62ec1\uff08\u56e0\u4e3a\u662f\u4ece0\u5f00\u59cb\u768464\u4e2a\u6570\uff0c\u6700\u5927\u6570\u4e3a63\uff09\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u6240\u5f97\u5230\u7684\u5f20\u91cf<code>zero_to_one_split_into_64_parts<\/code>\u5305\u542b64\u4e2a\u6d6e\u70b9\u6570\uff0c\u8fd9\u4e9b\u6570\u503c\u5747\u5300\u5730\u5206\u5e03\u57280\u52301\u4e4b\u95f4\uff08\u4e0d\u5305\u62ec1\uff09\u3002\u8fd9\u6837\u7684\u6570\u636e\u5e38\u7528\u4e8e\u751f\u6210\u9700\u8981\u7b49\u95f4\u8ddd\u6570\u503c\u7684\u573a\u5408\uff0c\u4f8b\u5982\u5728\u67d0\u4e9b\u56fe\u5f62\u6e32\u67d3\u3001\u6570\u636e\u6807\u51c6\u5316\u6216\u673a\u5668\u5b66\u4e60\u6a21\u578b\u8f93\u5165\u9884\u5904\u7406\u4e2d\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" ># \u521b\u5efa\u4e00\u4e2a\u9891\u7387\u6570\u7ec4\u3002\u8fd9\u91cc\uff0czero_to_one_split_into_64_parts \u5305\u542b\u4ece0\u52301\uff08\u4e0d\u5305\u62ec1\uff09\u5747\u5300\u5206\u5e03\u768464\u4e2a\u6570\u503c\u3002\n# \u8fd9\u4e9b\u6570\u503c\u88ab\u7528\u6765\u8ba1\u7b97\u5e42\u51fd\u6570 rope_theta \u7684\u5e42\u6b21\uff0c\u7136\u540e\u53d6\u5012\u6570\uff0c\u5f97\u5230\u76f8\u5e94\u7684\u9891\u7387\u3002\n# rope_theta \u901a\u5e38\u7528\u4e8e\u8c03\u6574\u5468\u671f\u6027\u7f16\u7801\u7684\u5c3a\u5ea6\uff0c\u6b64\u5904\u7684\u64cd\u4f5c\u662f\u5c06\u5176\u8c03\u6574\u5230\u4e00\u4e2a\u7279\u5b9a\u7684\u9891\u7387\u8303\u56f4\u5185\u3002\nfreqs = 1.0 \/ (rope_theta ** zero_to_one_split_into_64_parts)\nfreqs<\/pre><\/div>\n\n\n\n<p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>rope_theta<\/code> \u662f\u4e00\u4e2a\u9884\u5b9a\u4e49\u7684\u6807\u91cf\uff0c\u7528\u4e8e\u8c03\u6574\u751f\u6210\u9891\u7387\u7684\u5c3a\u5ea6\u3002<\/li>\n\n\n\n<li><code>zero_to_one_split_into_64_parts<\/code> \u662f\u4e00\u4e2a\u4ece0\u589e\u52a0\u5230\u63a5\u8fd11\u768464\u4e2a\u503c\u7684\u6570\u7ec4\uff0c\u6bcf\u4e2a\u6570\u503c\u90fd\u7b49\u95f4\u9694\u5206\u5e03\u3002<\/li>\n\n\n\n<li><code>rope_theta ** zero_to_one_split_into_64_parts<\/code> \u8ba1\u7b97\u4e86<code>rope_theta<\/code>\u7684\u5404\u4e2a\u5e42\u6b21\uff0c\u5e42\u7684\u5e95\u6570\u662f<code>rope_theta<\/code>\uff0c\u6307\u6570\u662f<code>zero_to_one_split_into_64_parts<\/code>\u4e2d\u7684\u503c\u3002\u8fd9\u6837\u53ef\u4ee5\u751f\u6210\u4e00\u7cfb\u5217\u7ecf\u8fc7\u975e\u7ebf\u6027\u53d8\u6362\u7684\u5c3a\u5ea6\u3002<\/li>\n\n\n\n<li>\u53d6\u5012\u6570\uff08<code>1.0 \/ ...<\/code>\uff09\u5c06\u8fd9\u4e9b\u5e42\u6b21\u8f6c\u6362\u6210\u5bf9\u5e94\u7684\u9891\u7387\u503c\uff0c\u8fd9\u4e9b\u9891\u7387\u901a\u5e38\u7528\u4e8e\u67d0\u4e9b\u5468\u671f\u6027\u7f16\u7801\u8fc7\u7a0b\uff0c\u5982\u5728\u67d0\u4e9b\u7c7b\u578b\u7684\u795e\u7ecf\u7f51\u7edc\uff08\u5c24\u5176\u662f\u90a3\u4e9b\u5904\u7406\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u7684\uff09\u4e2d\u3002<\/li>\n<\/ul>\n\n\n\n<p><code>freqs<\/code>\u5f20\u91cf\u5305\u542b\u4e86\u6839\u636e<code>rope_theta<\/code>\u548c\u5747\u5300\u95f4\u9694\u7684\u6307\u6570\u503c\u8c03\u6574\u540e\u7684\u4e00\u7cfb\u5217\u9891\u7387\u3002\u8fd9\u4e9b\u9891\u7387\u53ef\u7528\u4e8e\u8fdb\u4e00\u6b65\u7684\u6570\u636e\u5904\u7406\u6216\u7279\u5f81\u5de5\u7a0b\uff0c\u7279\u522b\u662f\u5728\u5904\u7406\u4e0e\u5468\u671f\u6027\u6216\u6ce2\u5f62\u76f8\u5173\u7684\u4efb\u52a1\u65f6\u3002<\/p>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \" >tensor([1.0000e+00, 8.1462e-01, 6.6360e-01, 5.4058e-01, 4.4037e-01, 3.5873e-01,\n        2.9223e-01, 2.3805e-01, 1.9392e-01, 1.5797e-01, 1.2869e-01, 1.0483e-01,\n        8.5397e-02, 6.9566e-02, 5.6670e-02, 4.6164e-02, 3.7606e-02, 3.0635e-02,\n        2.4955e-02, 2.0329e-02, 1.6560e-02, 1.3490e-02, 1.0990e-02, 8.9523e-03,\n        7.2927e-03, 5.9407e-03, 4.8394e-03, 3.9423e-03, 3.2114e-03, 2.6161e-03,\n        2.1311e-03, 1.7360e-03, 1.4142e-03, 1.1520e-03, 9.3847e-04, 7.6450e-04,\n        6.2277e-04, 5.0732e-04, 4.1327e-04, 3.3666e-04, 2.7425e-04, 2.2341e-04,\n        1.8199e-04, 1.4825e-04, 1.2077e-04, 9.8381e-05, 8.0143e-05, 6.5286e-05,\n        5.3183e-05, 4.3324e-05, 3.5292e-05, 2.8750e-05, 2.3420e-05, 1.9078e-05,\n        1.5542e-05, 1.2660e-05, 1.0313e-05, 8.4015e-06, 6.8440e-06, 5.5752e-06,\n        4.5417e-06, 3.6997e-06, 3.0139e-06, 2.4551e-06])<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" ># \u8ba1\u7b97\u6bcf\u4e2a\u6807\u8bb0\u5bf9\u5e94\u7684\u9891\u7387\u3002\u4f7f\u7528torch.outer\u51fd\u6570\u521b\u5efa\u4e00\u4e2a\u5916\u79ef\u77e9\u9635\uff0c\u884c\u6570\u4e3a17\uff0c\u5217\u4f7f\u7528\u524d\u9762\u8ba1\u7b97\u7684\u9891\u7387freqs\u3002\n# \u8fd9\u6837\uff0c\u6bcf\u4e2a\u6807\u8bb0\u90fd\u4f1a\u6709\u5bf9\u5e94\u7684\u4e00\u7ec4\u9891\u7387\u503c\u3002\nfreqs_for_each_token = torch.outer(torch.arange(17), freqs)\n\n# \u4f7f\u7528torch.polar\u5c06\u9891\u7387\u8f6c\u6362\u4e3a\u590d\u6570\u5f62\u5f0f\u3002torch.polar\u63a5\u6536\u5e45\u503c\uff08\u8fd9\u91cc\u4e3a1\uff0c\u8868\u793a\u5355\u4f4d\u5706\u4e0a\u7684\u70b9\uff09\u548c\u76f8\u4f4d\uff08\u9891\u7387\uff09\uff0c\u8f93\u51fa\u76f8\u5e94\u7684\u590d\u6570\u3002\n# \u8fd9\u91cc\u6bcf\u4e2a\u9891\u7387\u503c\u88ab\u8f6c\u6362\u6210\u4e86\u590d\u5e73\u9762\u4e0a\u7684\u4e00\u4e2a\u70b9\u3002\nfreqs_cis = torch.polar(torch.ones_like(freqs_for_each_token), freqs_for_each_token)\n\n# \u6253\u5370freqs_cis\u5f20\u91cf\u7684\u5f62\u72b6\uff0c\u5b83\u5e94\u8be5\u662f\u4e00\u4e2a\u4e8c\u7ef4\u5f20\u91cf\uff0c\u5176\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u4e00\u4e2a\u590d\u6570\u3002\nfreqs_cis.shape\n\n<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \" >torch.Size([17, 64])<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" ># \u67e5\u770bfreqs_cis\u7684\u7b2c\u4e09\u884c\uff0c\u5373\u7b2c\u4e09\u4e2a\u6807\u8bb0\u5bf9\u5e94\u7684\u6240\u6709\u590d\u6570\u9891\u7387\u3002\nvalue = freqs_cis[3]\n\n# \u4f7f\u7528matplotlib\u7ed8\u56fe\u5c55\u793a\u8fd9\u4e00\u884c\u7684\u590d\u6570\u9891\u7387\u3002\u7ed8\u5236\u4ece\u539f\u70b9\u5230\u6bcf\u4e2a\u590d\u6570\u70b9\u7684\u7ebf\uff0c\u5e76\u5728\u6bcf\u4e2a\u70b9\u5904\u6807\u6ce8\u7d22\u5f15\u3002\nplt.figure()\nfor i, element in enumerate(value[:17]):\n    plt.plot([0, element.real], [0, element.imag], color='blue', linewidth=1, label=f\"Index: {i}\")\n    plt.annotate(f\"{i}\", xy=(element.real, element.imag), color='red')\nplt.xlabel('Real')\nplt.ylabel('Imaginary')\nplt.title('Plot of one row of freqs_cis')\nplt.show()\n<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"784\" src=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-30-1024x784.png\" alt=\"\" class=\"wp-image-3942\" srcset=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-30-1024x784.png 1024w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-30-300x230.png 300w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-30-768x588.png 768w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-30.png 1504w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u8fd9\u6bb5\u4ee3\u7801\u9996\u5148\u4f7f\u7528<code>torch.outer<\/code>\u8ba1\u7b9717\u4e2a\u6807\u8bb0\u6bcf\u4e2a\u5bf9\u5e94\u7684\u9891\u7387\u96c6\u5408\u3002\u7136\u540e\uff0c\u4f7f\u7528<code>torch.polar<\/code>\u51fd\u6570\u5c06\u8fd9\u4e9b\u9891\u7387\u8f6c\u6362\u4e3a\u590d\u6570\u8868\u793a\uff0c\u5176\u4e2d\u5b9e\u90e8\u548c\u865a\u90e8\u5206\u522b\u5bf9\u5e94\u590d\u5e73\u9762\u4e0a\u7684\u6a2a\u5750\u6807\u548c\u7eb5\u5750\u6807\u3002\u6700\u540e\uff0c\u4f7f\u7528matplotlib\u7ed8\u56fe\u5e93\u5728\u590d\u5e73\u9762\u4e0a\u7ed8\u5236\u51fa\u8fd9\u4e9b\u590d\u6570\u70b9\uff0c\u5e76\u901a\u8fc7\u76f4\u7ebf\u5c06\u5b83\u4eec\u4e0e\u539f\u70b9\u76f8\u8fde\uff0c\u5f62\u8c61\u5730\u5c55\u793a\u4e86\u8fd9\u4e9b\u590d\u6570\u7684\u5206\u5e03\u548c\u76f8\u4f4d\u53d8\u5316\u3002\u8fd9\u53ef\u4ee5\u7528\u4e8e\u53ef\u89c6\u5316\u5206\u6790\u9891\u7387\u5206\u5e03\u7684\u7279\u6027\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u6d89\u53ca\u4fe1\u53f7\u5904\u7406\u6216\u5468\u671f\u6027\u6570\u636e\u7684\u573a\u666f\u4e2d\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>13. \u6709\u4e86\u6bcf\u4e2a\u4ee4\u724c\u7684\u67e5\u8be2\u5143\u7d20\u7684\u590d\u6570\uff08\u89d2\u5ea6\u53d8\u5316\u5411\u91cf\uff09<\/strong><\/h2>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u5c06\u67e5\u8be2\uff08\u6211\u4eec\u5206\u6210\u5bf9\u7684\u67e5\u8be2\uff09\u8f6c\u6362\u4e3a\u590d\u6570\uff0c\u7136\u540e\u70b9\u79ef\u6839\u636e\u4f4d\u7f6e\u65cb\u8f6c\u67e5\u8be2<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" ># \u5c06\u6210\u5bf9\u7684\u5b9e\u6570\u5f20\u91cf\u8f6c\u6362\u4e3a\u590d\u6570\u5f20\u91cf\uff0c\u6bcf\u5bf9\u5b9e\u6570\u5bf9\u5e94\u4e00\u4e2a\u590d\u6570\uff08\u7b2c\u4e00\u4e2a\u662f\u5b9e\u90e8\uff0c\u7b2c\u4e8c\u4e2a\u662f\u865a\u90e8\uff09\nq_per_token_as_complex_numbers = torch.view_as_complex(q_per_token_split_into_pairs)\nq_per_token_as_complex_numbers.shape<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \" >torch.Size([17, 64])<\/pre><\/div>\n\n\n\n<p>\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>torch.view_as_complex(q_per_token_split_into_pairs)<\/code>\uff1a\u8be5\u51fd\u6570\u5c06\u8f93\u5165\u7684\u5b9e\u6570\u5f20\u91cf\u89c6\u4e3a\u590d\u6570\u5f20\u91cf\uff0c\u5176\u4e2d\u8f93\u5165\u5f20\u91cf\u7684\u6700\u540e\u4e00\u4e2a\u7ef4\u5ea6\u5927\u5c0f\u5fc5\u987b\u662f2\uff0c\u5206\u522b\u4ee3\u8868\u590d\u6570\u7684\u5b9e\u90e8\u548c\u865a\u90e8\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5bf9\u4e8e\u5f20\u91cf\u5f62\u72b6\u7684\u89e3\u91ca\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5047\u8bbe<code>q_per_token_split_into_pairs<\/code>\u7684\u5f62\u72b6\u662f<code>(batch_size, sequence_length, 2)<\/code>\uff0c\u5176\u4e2d<code>2<\/code>\u8868\u793a\u6210\u5bf9\u7684\u5b9e\u90e8\u548c\u865a\u90e8\u3002<\/li>\n\n\n\n<li>\u4f7f\u7528<code>torch.view_as_complex<\/code>\u540e\uff0c\u6700\u540e\u4e00\u4e2a\u7ef4\u5ea6\u4f1a\u88ab\u89c6\u4e3a\u590d\u6570\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u6b64\u65b0\u7684\u5f20\u91cf<code>q_per_token_as_complex_numbers<\/code>\u7684\u5f62\u72b6\u5c06\u53d8\u4e3a<code>(batch_size, sequence_length)<\/code>\uff0c\u6bcf\u4e2a\u5143\u7d20\u90fd\u662f\u4e00\u4e2a\u590d\u6570\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u79cd\u8f6c\u6362\u901a\u5e38\u7528\u4e8e\u4fe1\u53f7\u5904\u7406\u6216\u795e\u7ecf\u7f51\u7edc\u4e2d\u5904\u7406\u590d\u6570\u6570\u636e\uff0c\u4f7f\u5f97\u540e\u7eed\u7684\u8ba1\u7b97\u53ef\u4ee5\u76f4\u63a5\u5728\u590d\u6570\u57df\u4e2d\u8fdb\u884c\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >q_per_token_as_complex_numbers_rotated = q_per_token_as_complex_numbers * freqs_cis\nq_per_token_as_complex_numbers_rotated.shape<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \" >torch.Size([17, 64])<\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>q_per_token_as_complex_numbers * freqs_cis<\/code>\uff1a\u8fd9\u4e2a\u64cd\u4f5c\u5c06\u4e24\u4e2a\u590d\u6570\u5f20\u91cf\u76f8\u4e58\u3002\u5728\u590d\u6570\u4e58\u6cd5\u4e2d\uff0c\u4e24\u4e2a\u590d\u6570\u76f8\u4e58\u7684\u7ed3\u679c\u662f\u4e00\u4e2a\u65b0\u7684\u590d\u6570\uff0c\u5176\u6a21\u957f\u662f\u539f\u4e24\u4e2a\u590d\u6570\u6a21\u957f\u7684\u4e58\u79ef\uff0c\u5176\u89d2\u5ea6\u662f\u539f\u4e24\u4e2a\u590d\u6570\u89d2\u5ea6\u7684\u548c\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5bf9\u4e8e\u5f20\u91cf\u5f62\u72b6\u7684\u89e3\u91ca\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5047\u8bbe <code>q_per_token_as_complex_numbers<\/code> \u7684\u5f62\u72b6\u662f <code>(batch_size, sequence_length)<\/code>\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u662f\u4e00\u4e2a\u590d\u6570\u3002<\/li>\n\n\n\n<li><code>freqs_cis<\/code> \u7684\u5f62\u72b6\u5e94\u5f53\u4e0e <code>q_per_token_as_complex_numbers<\/code> \u517c\u5bb9\uff0c\u4f8b\u5982\u4e5f\u662f <code>(batch_size, sequence_length)<\/code> \u6216\u80fd\u591f\u5e7f\u64ad\u5230\u8fd9\u4e2a\u5f62\u72b6\u3002<\/li>\n\n\n\n<li>\u7ed3\u679c\u5f20\u91cf <code>q_per_token_as_complex_numbers_rotated<\/code> \u5c06\u4fdd\u6301\u4e0e <code>q_per_token_as_complex_numbers<\/code> \u76f8\u540c\u7684\u5f62\u72b6 <code>(batch_size, sequence_length)<\/code>\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u6837\u7684\u64cd\u4f5c\u5e38\u7528\u4e8e\u4fe1\u53f7\u5904\u7406\u548c\u6df1\u5ea6\u5b66\u4e60\u4e2d\u7684\u7279\u5f81\u53d8\u6362\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u9700\u8981\u8c03\u6574\u76f8\u4f4d\u6216\u9891\u7387\u7279\u6027\u7684\u5e94\u7528\u4e2d\u3002\u6bcf\u4e2a\u590d\u6570\u7684\u65cb\u8f6c\u53ef\u4ee5\u770b\u4f5c\u662f\u5728\u590d\u5e73\u9762\u4e0a\u7684\u4e00\u4e2a\u89d2\u5ea6\u53d8\u5316\uff0c\u8fd9\u5bf9\u4e8e\u67d0\u4e9b\u7c7b\u578b\u7684\u7b97\u6cd5\uff08\u6bd4\u5982\u90a3\u4e9b\u6d89\u53ca\u65f6\u95f4\u5e8f\u5217\u6216\u5468\u671f\u6027\u6570\u636e\u5904\u7406\u7684\u7b97\u6cd5\uff09\u7279\u522b\u6709\u7528\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>14. \u83b7\u5f97\u65cb\u8f6c\u5411\u91cf\u540e\u00a0<\/strong><\/h2>\n\n\n\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u518d\u6b21\u5c06\u590d\u6570\u89c6\u4e3a\u5b9e\u6570\u6765\u5c06\u67e5\u8be2\u4f5c\u4e3a\u5bf9\u8fd4\u56de<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >q_per_token_split_into_pairs_rotated = torch.view_as_real(q_per_token_as_complex_numbers_rotated)\nq_per_token_split_into_pairs_rotated.shape<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \" >torch.Size([17, 64, 2])<\/pre><\/div>\n\n\n\n<p>\u8fd9\u4e2a\u64cd\u4f5c\u7684\u5177\u4f53\u884c\u4e3a\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>torch.view_as_real(q_per_token_as_complex_numbers_rotated)<\/code>\uff1a\u6b64\u51fd\u6570\u5c06\u8f93\u5165\u7684\u590d\u6570\u5f20\u91cf\u8f6c\u6362\u4e3a\u5b9e\u6570\u5f20\u91cf\uff0c\u6bcf\u4e2a\u590d\u6570\u7684\u5b9e\u90e8\u548c\u865a\u90e8\u6210\u5bf9\u51fa\u73b0\uff0c\u5f62\u6210\u6700\u540e\u4e00\u4e2a\u7ef4\u5ea6\u7684\u4e24\u4e2a\u8fde\u7eed\u5143\u7d20\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u5173\u4e8e\u5f20\u91cf\u5f62\u72b6\u7684\u89e3\u91ca\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5047\u8bbe<code>q_per_token_as_complex_numbers_rotated<\/code>\u7684\u5f62\u72b6\u662f<code>(batch_size, sequence_length)<\/code>\uff0c\u5176\u4e2d\u6bcf\u4e2a\u5143\u7d20\u662f\u4e00\u4e2a\u590d\u6570\u3002<\/li>\n\n\n\n<li>\u8f6c\u6362\u540e\u7684\u5f20\u91cf<code>q_per_token_split_into_pairs_rotated<\/code>\u7684\u5f62\u72b6\u5c06\u53d8\u4e3a<code>(batch_size, sequence_length, 2)<\/code>\u3002\u6700\u540e\u7684<code>2<\/code>\u8868\u793a\u590d\u6570\u7684\u4e24\u4e2a\u7ec4\u6210\u90e8\u5206\uff1a\u5b9e\u90e8\u548c\u865a\u90e8\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u6837\u7684\u8f6c\u6362\u4f7f\u5f97\u590d\u6570\u6570\u636e\u7684\u5b9e\u90e8\u548c\u865a\u90e8\u53ef\u4ee5\u4f5c\u4e3a\u72ec\u7acb\u7684\u7279\u5f81\u5728\u540e\u7eed\u5904\u7406\u4e2d\u4f7f\u7528\uff0c\u9002\u7528\u4e8e\u9700\u8981\u5206\u522b\u5904\u7406\u590d\u6570\u5b9e\u90e8\u548c\u865a\u90e8\u7684\u573a\u666f\uff0c\u4f8b\u5982\u5728\u67d0\u4e9b\u673a\u5668\u5b66\u4e60\u6a21\u578b\u7684\u8f93\u5165\u5904\u7406\u4e2d\u3002<\/p>\n\n\n\n<p>\u65cb\u8f6c\u540e\u7684\u5bf9\u73b0\u5728\u5df2\u7ecf\u5408\u5e76\uff0c\u6211\u4eec\u73b0\u5728\u6709\u4e86\u4e00\u4e2a\u65b0\u7684\u67e5\u8be2\u5411\u91cf\uff08\u65cb\u8f6c\u540e\u7684\u67e5\u8be2\u5411\u91cf\uff09\uff0c\u5176\u5f62\u72b6\u4e3a [17&#215;128]\uff0c\u5176\u4e2d 17 \u662f\u6807\u8bb0\u7684\u6570\u91cf\uff0c128 \u662f\u67e5\u8be2\u5411\u91cf\u7684\u7ef4\u5ea6\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >q_per_token_rotated = q_per_token_split_into_pairs_rotated.view(q_per_token.shape)\nq_per_token_rotated.shape<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \" >torch.Size([17, 128])<\/pre><\/div>\n\n\n\n<p>\u5728\u8fd9\u91cc\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>q_per_token.shape<\/code> \u539f\u672c\u7684\u5f62\u72b6\u53ef\u80fd\u662f <code>(batch_size, sequence_length)<\/code>\uff0c\u5047\u8bbe\u4e4b\u524d\u64cd\u4f5c\u4e2d\u6bcf\u4e2atoken\u5bf9\u5e94\u7684\u590d\u6570\uff08\u5b9e\u90e8\u548c\u865a\u90e8\uff09\u88ab\u89c6\u4e3a\u72ec\u7acb\u7684\u7279\u5f81\u6216\u7ef4\u5ea6\u5904\u7406\u3002<\/li>\n\n\n\n<li><code>view(q_per_token.shape)<\/code> \u65b9\u6cd5\u7528\u4e8e\u5c06\u5f20\u91cf <code>q_per_token_split_into_pairs_rotated<\/code> \u4ece\u5305\u542b\u590d\u6570\u5b9e\u865a\u90e8\u7684\u4e09\u7ef4\u5f62\u72b6 <code>(batch_size, sequence_length, 2)<\/code> \u8f6c\u6362\u56de\u4e8c\u7ef4\u5f62\u72b6 <code>(batch_size, sequence_length)<\/code>\u3002\u8fd9\u79cd\u64cd\u4f5c\u901a\u5e38\u610f\u5473\u7740\u5728\u67d0\u79cd\u5904\u7406\uff08\u5982\u590d\u6570\u5230\u5b9e\u6570\u7684\u8f6c\u6362\uff09\u4e4b\u540e\uff0c\u6211\u4eec\u9700\u8981\u6062\u590d\u6570\u636e\u5230\u67d0\u4e2a\u7279\u5b9a\u7684\u7ef4\u5ea6\u7ed3\u6784\u4ee5\u9002\u914d\u540e\u7eed\u7684\u5904\u7406\u6d41\u7a0b\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u56e0\u6b64\uff0c\u6700\u7ec8\u7684\u5f20\u91cf <code>q_per_token_rotated<\/code> \u5c06\u4fdd\u6301\u4e0e\u539f\u59cb <code>q_per_token<\/code> \u76f8\u540c\u7684\u5f62\u72b6\uff0c\u8fd9\u4e5f\u610f\u5473\u7740\u6bcf\u4e2a token \u73b0\u5728\u5bf9\u5e94\u4e00\u4e2a\u5408\u5e76\u540e\u7684\u7279\u5f81\u5411\u91cf\uff0c\u800c\u4e0d\u518d\u662f\u5206\u5f00\u7684\u5b9e\u90e8\u548c\u865a\u90e8\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>15. \u952e\uff08\u51e0\u4e4e\u4e0e\u67e5\u8be2\u76f8\u540c\uff09<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"568\" src=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-31-1024x568.png\" alt=\"\" class=\"wp-image-3949\" srcset=\"https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-31-1024x568.png 1024w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-31-300x166.png 300w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-31-768x426.png 768w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-31-1536x852.png 1536w, https:\/\/www.aqwu.net\/wp\/wp-content\/uploads\/2024\/05\/\u56fe\u7247-31.png 1603w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u4e0d\u6253\u7b97\u901a\u8fc7\u94a5\u5319\u7684\u6570\u5b66\u8ba1\u7b97\uff0c\u4f60\u552f\u4e00\u9700\u8981\u8bb0\u4f4f\u7684\u662f\uff1a<\/p>\n\n\n\n<p>\u952e\u751f\u6210\u952e\u5411\u91cf\uff0c\u4e5f\u662f dimention 128<\/p>\n\n\n\n<p>\u952e\u7684\u6743\u91cd\u6570\u53ea\u6709\u67e5\u8be2\u6743\u91cd\u7684 1\/4\uff0c\u8fd9\u662f\u56e0\u4e3a\u952e\u7684\u6743\u91cd\u4e00\u6b21\u5728 4 \u4e2a\u5934\u4e4b\u95f4\u5171\u4eab\uff0c\u4ee5\u51cf\u5c11\u6240\u9700\u7684\u8ba1\u7b97\u6b21\u6570<\/p>\n\n\n\n<p>\u952e\u4e5f\u4f1a\u65cb\u8f6c\u4ee5\u6dfb\u52a0\u4f4d\u7f6e\u4fe1\u606f\uff0c\u5c31\u50cf\u51fa\u4e8e\u76f8\u540c\u539f\u56e0\u7684\u67e5\u8be2\u4e00\u6837<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" ># \u4ece\u6a21\u578b\u4e2d\u83b7\u53d6\u7b2c0\u5c42\u7684\u952e\uff08key\uff09\u6743\u91cd\nk_layer0 = model[\"layers.0.attention.wk.weight\"]\n\n# \u5c06\u952e\uff08key\uff09\u6743\u91cd\u77e9\u9635\u91cd\u65b0\u5f62\u72b6\u4e3a (n_kv_heads, k_layer0.shape[0] \/\/ n_kv_heads, dim)\n# \u5176\u4e2d n_kv_heads \u662f\u952e\u5411\u91cf\u7684\u5934\u6570\uff0cdim \u662f\u6bcf\u4e2a\u5934\u7684\u7ef4\u5ea6\nk_layer0 = k_layer0.view(n_kv_heads, k_layer0.shape[0] \/\/ n_kv_heads, dim)\n\nk_layer0.shape<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \" >torch.Size([8, 128, 4096])<\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>n_kv_heads<\/code> \u8868\u793a\u7528\u4e8e\u952e\u5411\u91cf\u7684\u5934\u7684\u6570\u91cf\u3002<\/li>\n\n\n\n<li><code>k_layer0.shape[0] \/\/ n_kv_heads<\/code> \u8ba1\u7b97\u6bcf\u4e2a\u5934\u9700\u8981\u5904\u7406\u7684\u952e\u5411\u91cf\u7684\u6570\u91cf\u3002<\/li>\n\n\n\n<li><code>dim<\/code> \u662f\u6bcf\u4e2a\u952e\u5411\u91cf\u7684\u7ef4\u5ea6\uff0c\u901a\u5e38\u4e0e\u6a21\u578b\u7684\u5d4c\u5165\u7ef4\u5ea6\u76f8\u5339\u914d\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8be5\u64cd\u4f5c\u540e\u7684 <code>k_layer0<\/code> \u7684\u5f62\u72b6\u5c06\u662f <code>(n_kv_heads, k_layer0.shape[0] \/\/ n_kv_heads, dim)<\/code>\u3002\u8fd9\u610f\u5473\u7740\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b2c\u4e00\u7ef4 <code>n_kv_heads<\/code> \u8868\u793a\u5934\u7684\u6570\u91cf\u3002<\/li>\n\n\n\n<li>\u7b2c\u4e8c\u7ef4 <code>k_layer0.shape[0] \/\/ n_kv_heads<\/code> \u8868\u793a\u6bcf\u4e2a\u5934\u5904\u7406\u7684\u952e\u5411\u91cf\u7684\u6570\u91cf\u3002<\/li>\n\n\n\n<li>\u7b2c\u4e09\u7ef4 <code>dim<\/code> \u8868\u793a\u6bcf\u4e2a\u952e\u5411\u91cf\u7684\u7ef4\u5ea6\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u8fd9\u79cd\u91cd\u5851\u662f\u4e3a\u4e86\u4fbf\u4e8e\u5728\u540e\u7eed\u64cd\u4f5c\u4e2d\uff0c\u6bcf\u4e2a\u6ce8\u610f\u529b\u5934\u80fd\u591f\u72ec\u7acb\u5730\u5bf9\u5e94\u5e76\u5904\u7406\u5176\u5206\u914d\u7684\u952e\u5411\u91cf\uff0c\u4ece\u800c\u5b9e\u73b0\u6709\u6548\u7684\u5e76\u884c\u5904\u7406\u548c\u66f4\u7cbe\u7ec6\u7684\u6ce8\u610f\u529b\u673a\u5236\u3002<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >k_layer0_head0 = k_layer0[0]\nk_layer0_head0.shape<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \" >torch.Size([128, 4096])<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >k_per_token = torch.matmul(token_embeddings, k_layer0_head0.T)\nk_per_token.shape<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \" >torch.Size([17, 128])<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >k_per_token_split_into_pairs = k_per_token.float().view(k_per_token.shape[0], -1, 2)\nk_per_token_split_into_pairs.shape<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \" >torch.Size([17, 64, 2])<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >k_per_token_as_complex_numbers = torch.view_as_complex(k_per_token_split_into_pairs)\nk_per_token_as_complex_numbers.shape<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \" >torch.Size([17, 64])<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >k_per_token_split_into_pairs_rotated = torch.view_as_real(k_per_token_as_complex_numbers * freqs_cis)\nk_per_token_split_into_pairs_rotated.shape<\/pre><\/div>\n\n\n\n<p>\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \" >torch.Size([17, 64, 2])<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:python decode:true \" >k_per_token_rotated = k_per_token_split_into_pairs_rotated.view(k_per_token.shape)\nk_per_token_rotated.shape<\/pre><\/div>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:sh decode:true \" >torch.Size([17, 128])<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u53c2\u8003\u8fde\u63a5\uff1ahttps:\/\/github.com\/naklecha\/llama3-from-scratch  [&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":[444,445,443,442],"tags":[434],"class_list":["post-3869","post","type-post","status-publish","format-standard","hentry","category-ai","category-ainews","category-llm","category-llms","tag-llama3"],"views":3361,"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/3869","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=3869"}],"version-history":[{"count":76,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/3869\/revisions"}],"predecessor-version":[{"id":3962,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/3869\/revisions\/3962"}],"wp:attachment":[{"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3869"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3869"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.aqwu.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3869"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}