{"id":444,"date":"2024-11-07T15:05:29","date_gmt":"2024-11-07T07:05:29","guid":{"rendered":"https:\/\/my.di.cloudns.asia\/?p=444"},"modified":"2024-11-07T15:07:59","modified_gmt":"2024-11-07T07:07:59","slug":"llama-3-2-%e5%be%ae%e8%b0%83%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/my.di.cloudns.asia\/index.php\/2024\/11\/07\/444.html","title":{"rendered":"Llama 3.2 \u5fae\u8c03\u6307\u5357"},"content":{"rendered":"<blockquote>\n<p>\u672c\u6587\u7531 <a href=\"http:\/\/ksria.com\/simpread\/\">\u7b80\u60a6 SimpRead<\/a> \u8f6c\u7801\uff0c \u539f\u6587\u5730\u5740 <a href=\"https:\/\/blog.csdn.net\/shebao3333\/article\/details\/142711758\">blog.csdn.net<\/a> \u82f1\u6587\u539f\u6587\u5730\u5740 <a href=\"https:\/\/huggingface.co\/blog\/ImranzamanML\/fine-tuning-1b-llama-32-a-comprehensive-article\">huggingface.co<\/a><\/p>\n<\/blockquote>\n<p>\u8ba9\u6211\u4eec\u901a\u8fc7\u5fae\u8c03 Llama 3.2 \u6765\u627e\u5230\u4e00\u4e9b\u7cbe\u795e\u4e0a\u7684\u5e73\u9759\u3002<\/p>\n<p>\u6211\u4eec\u9700\u8981\u5b89\u88c5 unsloth\uff0c\u4ee5\u66f4\u5c0f\u7684\u5c3a\u5bf8\u5b9e\u73b0 2 \u500d\u7684\u5feb\u901f\u8bad\u7ec3<\/p>\n<pre><code>!pip install unsloth\n\n!pip uninstall unsloth -y &amp;&amp; pip install --upgrade --no-cache-dir &quot;unsloth[colab-new] @ git+https:\/\/github.com\/unslothai\/unsloth.git&quot;<\/code><\/pre>\n<blockquote>\n<p>\u6211\u4eec\u5c06\u4f7f\u7528 Unsloth\uff0c\u56e0\u4e3a\u5b83\u663e\u8457\u63d0\u9ad8\u4e86\u5fae\u8c03\u5927\u578b\u8bed\u8a00\u6a21\u578b (LLM) \u7684\u6548\u7387\uff0c\u7279\u522b\u662f LLaMA \u548c Mistral\u3002\u4f7f\u7528 Unsloth\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u9ad8\u7ea7\u91cf\u5316\u6280\u672f\uff08\u4f8b\u5982 4 \u4f4d\u548c 16 \u4f4d\u91cf\u5316\uff09\u6765\u51cf\u5c11\u5185\u5b58\u5e76\u52a0\u5feb\u8bad\u7ec3\u548c\u63a8\u7406\u901f\u5ea6\u3002\u8fd9\u610f\u5473\u7740\u6211\u4eec\u751a\u81f3\u53ef\u4ee5\u5728\u8d44\u6e90\u6709\u9650\u7684\u786c\u4ef6\u4e0a\u90e8\u7f72\u5f3a\u5927\u7684\u6a21\u578b\uff0c\u800c\u4e0d\u4f1a\u5f71\u54cd\u6027\u80fd\u3002  <\/p>\n<p>\u6b64\u5916\uff0cUnsloth \u5e7f\u6cdb\u7684\u517c\u5bb9\u6027\u548c\u5b9a\u5236\u9009\u9879\u5141\u8bb8\u6267\u884c\u91cf\u5316\u8fc7\u7a0b\u4ee5\u6ee1\u8db3\u4ea7\u54c1\u7684\u7279\u5b9a\u9700\u6c42\u3002\u8fd9\u79cd\u7075\u6d3b\u6027\u52a0\u4e0a\u5176\u5c06 VRAM \u4f7f\u7528\u91cf\u51cf\u5c11\u9ad8\u8fbe 60% \u7684\u80fd\u529b\uff0c\u4f7f Unsloth \u6210\u4e3a AI \u5de5\u5177\u5305\u4e2d\u5fc5\u4e0d\u53ef\u5c11\u7684\u5de5\u5177\u3002\u5b83\u4e0d\u4ec5\u4ec5\u662f\u4f18\u5316\u6a21\u578b\uff0c\u800c\u662f\u8ba9\u5c16\u7aef AI \u66f4\u6613\u4e8e\u8bbf\u95ee\uff0c\u66f4\u9ad8\u6548\u5730\u5e94\u7528\u4e8e\u73b0\u5b9e\u4e16\u754c\u3002<\/p>\n<\/blockquote>\n<p>\u5bf9\u4e8e\u5fae\u8c03\uff0c\u6211\u4f7f\u7528\u4e86\u4ee5\u4e0b\u8bbe\u7f6e\uff1a<\/p>\n<ul>\n<li><a href=\"https:\/\/pytorch.org\/\" title=\"Torch 2.1.1 - CUDA 12.1\u00a0\">Torch 2.1.1 &#8211; CUDA 12.1<\/a> \u53ef\u5b9e\u73b0\u9ad8\u6548\u8ba1\u7b97\u3002<\/li>\n<li><a href=\"https:\/\/github.com\/unslothai\/unsloth\" title=\"Unsloth\u00a0\">Unsloth<\/a> \u53ef\u5b9e\u73b0\u5927\u578b\u8bed\u8a00\u6a21\u578b (LLM) \u7684 2 \u500d\u66f4\u5feb\u7684\u8bad\u7ec3\u901f\u5ea6\u3002<\/li>\n<li><a href=\"https:\/\/www.nvidia.com\/content\/dam\/en-zz\/Solutions\/Data-Center\/h100\/PB-11773-001_v01.pdf\" title=\"H100 NVL GPU\u00a0\">H100 NVL GPU<\/a> \u53ef\u6ee1\u8db3\u5bc6\u96c6\u5904\u7406\u8981\u6c42\uff0c\u4f46\u4f60\u53ef\u4ee5\u4f7f\u7528\u529f\u7387\u8f83\u4f4e\u7684 GPU\uff0c\u5373 Kaggle GPU\u3002<\/li>\n<\/ul>\n<p>\u4e3a\u4ec0\u4e48\u662f LLaMA 3.2\uff1f<\/p>\n<p>\u5b83\u662f\u5f00\u6e90\u4e14\u53ef\u8bbf\u95ee\u7684\uff0c\u5e76\u63d0\u4f9b\u4e86\u6839\u636e\u7279\u5b9a\u9700\u6c42\u8fdb\u884c\u81ea\u5b9a\u4e49\u548c\u5fae\u8c03\u7684<a href=\"https:\/\/so.csdn.net\/so\/search?q=%E7%81%B5%E6%B4%BB%E6%80%A7&spm=1001.2101.3001.7020\">\u7075\u6d3b\u6027<\/a>\u3002\u7531\u4e8e Meta \u7684\u6a21\u578b\u6743\u91cd\u662f\u5f00\u6e90\u7684\uff0c\u56e0\u6b64\u53ef\u4ee5\u975e\u5e38\u8f7b\u677e\u5730\u5bf9\u4efb\u4f55\u95ee\u9898\u8fdb\u884c\u5fae\u8c03\uff0c\u6211\u4eec\u5c06\u5728 Hugging Face \u7684<a href=\"https:\/\/huggingface.co\/datasets\/Amod\/mental_health_counseling_conversations\" title=\"\u5fc3\u7406\u5065\u5eb7\u6570\u636e\u96c6\">\u5fc3\u7406\u5065\u5eb7\u6570\u636e\u96c6<\/a>\u4e0a\u5bf9\u5176\u8fdb\u884c\u5fae\u8c03<\/p>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/my.di.cloudns.asia\/wp-content\/uploads\/2024\/11\/post-444-672c66bbe3b53.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/my.di.cloudns.asia\/wp-content\/uploads\/2024\/11\/post-444-672c66bbe3b53.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<blockquote>\n<p>\u00a0<strong>NSDT \u5de5\u5177\u63a8\u8350<\/strong>\uff1a\u00a0<a href=\"https:\/\/tools.nsdt.cloud\/DreamTexture?s=7878\" title=\"Three.js AI\u7eb9\u7406\u5f00\u53d1\u5305\">Three.js AI \u7eb9\u7406\u5f00\u53d1\u5305<\/a>\u00a0&#8211;\u00a0<a href=\"https:\/\/tools.nsdt.cloud\/UnrealSynth?s=7878\" title=\"YOLO\u5408\u6210\u6570\u636e\u751f\u6210\u5668\">YOLO \u5408\u6210\u6570\u636e\u751f\u6210\u5668<\/a>\u00a0&#8211;\u00a0<a href=\"https:\/\/gltf.nsdt.cloud\/?s=7878\" title=\"GLTF\/GLB\u5728\u7ebf\u7f16\u8f91\">GLTF\/GLB \u5728\u7ebf\u7f16\u8f91<\/a>\u00a0&#8211;\u00a0<a href=\"https:\/\/3dconvert.nsdt.cloud\/?s=7878\" title=\"3D\u6a21\u578b\u683c\u5f0f\u5728\u7ebf\u8f6c\u6362\">3D \u6a21\u578b\u683c\u5f0f\u5728\u7ebf\u8f6c\u6362<\/a>\u00a0&#8211;\u00a0<a href=\"https:\/\/studio.nsdt.cloud\/?s=7878\" title=\"\u53ef\u7f16\u7a0b3D\u573a\u666f\u7f16\u8f91\u5668\">\u53ef\u7f16\u7a0b 3D \u573a\u666f\u7f16\u8f91\u5668<\/a>\u00a0&#8211;\u00a0<a href=\"https:\/\/3dconvert.nsdt.cloud\/conv\/plugin?s=7878\" title=\"REVIT\u5bfc\u51fa3D\u6a21\u578b\u63d2\u4ef6\">REVIT \u5bfc\u51fa 3D \u6a21\u578b\u63d2\u4ef6<\/a>\u00a0&#8211;\u00a0<a href=\"https:\/\/tools.nsdt.cloud\/3DSEE?s=7878\" title=\"3D\u6a21\u578b\u8bed\u4e49\u641c\u7d22\u5f15\u64ce\">3D \u6a21\u578b\u8bed\u4e49\u641c\u7d22\u5f15\u64ce<\/a>\u00a0&#8211;\u00a0<a href=\"http:\/\/netron.bimant.com\/?s=7878\" title=\"AI\u6a21\u578b\u5728\u7ebf\u67e5\u770b\">AI \u6a21\u578b\u5728\u7ebf\u67e5\u770b<\/a>\u00a0&#8211;\u00a0<a href=\"https:\/\/tools.nsdt.cloud\/three-pivot?s=7878\" title=\"Three.js\u865a\u62df\u8f74\u5fc3\u5f00\u53d1\u5305\">Three.js \u865a\u62df\u8f74\u5fc3\u5f00\u53d1\u5305<\/a>\u00a0&#8211;\u00a0<a href=\"https:\/\/simplify.nsdt.cloud\/?s=7878\" title=\"3D\u6a21\u578b\u5728\u7ebf\u51cf\u9762\">3D \u6a21\u578b\u5728\u7ebf\u51cf\u9762<\/a>\u00a0&#8211;\u00a0<a href=\"https:\/\/cut.nsdt.cloud\/?s=7878\" title=\"STL\u6a21\u578b\u5728\u7ebf\u5207\u5272\">STL \u6a21\u578b\u5728\u7ebf\u5207\u5272<\/a><\/p>\n<\/blockquote>\n<h3>1\u3001Python \u5e93<\/h3>\n<p>\u6570\u636e\u5904\u7406\u548c\u53ef\u89c6\u5316<\/p>\n<pre><code>import os\nimport numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nplt.style.use(&#039;ggplot&#039;)<\/code><\/pre>\n<p>LLM \u6a21\u578b\u8bad\u7ec3\uff1a<\/p>\n<pre><code>import torch\nfrom trl import SFTTrainer\nfrom transformers import TrainingArguments, TextStreamer\nfrom unsloth.chat_templates import get_chat_template\nfrom unsloth import FastLanguageModel\nfrom datasets import Dataset\nfrom unsloth import is_bfloat16_supported\n\n# Saving model\nfrom transformers import AutoTokenizer, AutoModelForSequenceClassification\n\n# Warnings\nimport warnings\nwarnings.filterwarnings(&quot;ignore&quot;)\n\n%matplotlib inline<\/code><\/pre>\n<h3>2\u3001\u8c03\u7528\u6570\u636e\u96c6<\/h3>\n<pre><code>data = pd.read_json(&quot;hf:\/\/datasets\/Amod\/mental_health_counseling_conversations\/combined_dataset.json&quot;, lines=True)\n<\/code><\/pre>\n<h3>3\u3001\u63a2\u7d22\u6027\u6570\u636e\u5206\u6790<\/h3>\n<p>\u8ba9\u6211\u4eec\u68c0\u67e5\u4e00\u4e0b\u6bcf\u4e2a\u4e0a\u4e0b\u6587\u4e2d\u7684\u5355\u8bcd\u957f\u5ea6\uff1a<\/p>\n<pre><code>data[&#039;Context_length&#039;] = data[&#039;Context&#039;].apply(len)\nplt.figure(figsize=(10, 3))\nsns.histplot(data[&#039;Context_length&#039;], bins=50, kde=True)\nplt.title(&#039;Distribution of Context Lengths&#039;)\nplt.xlabel(&#039;Length of Context&#039;)\nplt.ylabel(&#039;Frequency&#039;)\nplt.show()<\/code><\/pre>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/my.di.cloudns.asia\/wp-content\/uploads\/2024\/11\/post-444-672c66bd4725c.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/my.di.cloudns.asia\/wp-content\/uploads\/2024\/11\/post-444-672c66bd4725c.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<p>\u6ce8\u610f\uff1a\u5982\u4e0a\u6240\u793a\uff0c\u5355\u8bcd\u6570\u6700\u5c11\u4e3a 1500 \u4e2a\uff0c\u800c\u4e14\u5b58\u5728\u663e\u8457\u5dee\u5f02\uff0c\u56e0\u6b64\u6211\u4eec\u53ea\u4f7f\u7528 1500 \u4e2a\u6216\u66f4\u5c11\u5355\u8bcd\u7684\u6570\u636e\u3002<\/p>\n<pre><code>filtered_data = data[data[&#039;Context_length&#039;] &lt;= 1500]\n\nln_Context = filtered_data[&#039;Context&#039;].apply(len)\nplt.figure(figsize=(10, 3))\nsns.histplot(ln_Context, bins=50, kde=True)\nplt.title(&#039;Distribution of Context Lengths&#039;)\nplt.xlabel(&#039;Length of Context&#039;)\nplt.ylabel(&#039;Frequency&#039;)\nplt.show()<\/code><\/pre>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/my.di.cloudns.asia\/wp-content\/uploads\/2024\/11\/post-444-672c66be95f19.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/my.di.cloudns.asia\/wp-content\/uploads\/2024\/11\/post-444-672c66be95f19.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<p>\u6ce8\u610f\uff1a\u73b0\u5728\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e9b\u6570\u636e\u3002<\/p>\n<p>\u73b0\u5728\u8ba9\u6211\u4eec\u68c0\u67e5\u4e00\u4e0b\u6bcf\u4e2a\u56de\u590d\u7684\u5355\u8bcd\u957f\u5ea6\uff1a<\/p>\n<pre><code>ln_Response = filtered_data[&#039;Response&#039;].apply(len)\nplt.figure(figsize=(10, 3))\nsns.histplot(ln_Response, bins=50, kde=True, color=&#039;teal&#039;)\nplt.title(&#039;Distribution of Response Lengths&#039;)\nplt.xlabel(&#039;Length of Response&#039;)\nplt.ylabel(&#039;Frequency&#039;)\nplt.show()<\/code><\/pre>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/my.di.cloudns.asia\/wp-content\/uploads\/2024\/11\/post-444-672c66bfd34d4.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/my.di.cloudns.asia\/wp-content\/uploads\/2024\/11\/post-444-672c66bfd34d4.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<p>\u6ce8\u610f\uff1a\u8fd9\u4e5f\u662f 4000 \u5b57\u957f\u5ea6\u7684\u56de\u5e94\u4e4b\u540e\uff0c\u51fa\u73b0\u4e86\u660e\u663e\u7684\u4e0b\u964d\u3002<\/p>\n<pre><code>filtered_data = filtered_data[ln_Response &lt;= 4000]\n\nln_Response = filtered_data[&#039;Response&#039;].apply(len)\nplt.figure(figsize=(10, 3))\nsns.histplot(ln_Response, bins=50, kde=True, color=&#039;teal&#039;)\nplt.title(&#039;Distribution of Response Lengths&#039;)\nplt.xlabel(&#039;Length of Response&#039;)\nplt.ylabel(&#039;Frequency&#039;)\nplt.show()<\/code><\/pre>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/my.di.cloudns.asia\/wp-content\/uploads\/2024\/11\/post-444-672c66c12403f.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/my.di.cloudns.asia\/wp-content\/uploads\/2024\/11\/post-444-672c66c12403f.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<p>\u6ce8\u610f\uff1a\u4e0d\u9700\u8981\u8fdb\u884c\u8fd9\u6837\u7684\u6570\u636e\u51c6\u5907\u6765\u5904\u7406 LLM \u6a21\u578b\u7684\u6587\u672c\u957f\u5ea6\uff0c\u4f46\u4e3a\u4e86\u4fdd\u6301\u5b57\u6570\u7684\u4e00\u81f4\u6027\uff0c\u6211\u4ec5\u4ee5 4000 \u4e2a\u5b57\u4ee5\u4e0b\u7684\u5b57\u4e3a\u4f8b\uff0c\u4ee5\u4fbf\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u4efb\u4f55\u6570\u636e\u9884\u5904\u7406\u3002<\/p>\n<h3>4\u3001\u6a21\u578b\u8bad\u7ec3<\/h3>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/my.di.cloudns.asia\/wp-content\/uploads\/2024\/11\/post-444-672c66c2b5b20.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  decoding=\"async\" data-original=\"https:\/\/my.di.cloudns.asia\/wp-content\/uploads\/2024\/11\/post-444-672c66c2b5b20.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<p>\u8ba9\u6211\u4eec\u6df1\u5165\u7814\u7a76 Llama 3.2 \u6a21\u578b\u5e76\u5728\u6211\u4eec\u7684\u6570\u636e\u4e0a\u8fdb\u884c\u8bad\u7ec3\u3002<\/p>\n<h4>4.1 \u52a0\u8f7d\u6a21\u578b<\/h4>\n<p>\u6211\u4eec\u5c06\u4f7f\u7528\u53ea\u6709 10 \u4ebf\u4e2a\u53c2\u6570\u7684 Llama 3.2\uff0c\u4f46\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 30 \u4ebf\u3001110 \u4ebf\u6216 900 \u4ebf\u4e2a\u7248\u672c\u3002<\/p>\n<p>\u4e5f\u53ef\u4ee5\u6839\u636e\u4f60\u7684\u8981\u6c42\u9075\u5faa\u4ee5\u4e0b\u5173\u952e\u65b9\u9762\uff1a<\/p>\n<ul>\n<li>\u6700\u5927\u5e8f\u5217\u957f\u5ea6<\/li>\n<\/ul>\n<p>\u6211\u4eec\u4f7f\u7528\u4e86\u00a0<code>max_seq_length<\/code>\u00a05020\uff0c\u8fd9\u662f\u6a21\u578b\u4e2d\u53ef\u4ee5\u4f7f\u7528\u7684\u6700\u5927\u6807\u8bb0\u6570\uff0c\u53ef\u4ee5\u5728\u5355\u4e2a\u8f93\u5165\u5e8f\u5217\u4e2d\u5904\u7406\u3002\u8fd9\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u957f\u6587\u672c\u7684\u4efb\u52a1\u81f3\u5173\u91cd\u8981\uff0c\u53ef\u786e\u4fdd\u6a21\u578b\u5728\u6bcf\u6b21\u4f20\u9012\u4e2d\u90fd\u80fd\u6355\u83b7\u66f4\u591a\u4e0a\u4e0b\u6587\u3002\u53ef\u4ee5\u6839\u636e\u8981\u6c42\u4f7f\u7528\u5b83\u3002<\/p>\n<ul>\n<li>\u52a0\u8f7d Llama 3.2 \u6a21\u578b<\/li>\n<\/ul>\n<p>\u4f7f\u7528\u00a0<code>FastLanguageModel.from_pretrained<\/code>\u00a0\u548c\u7279\u5b9a\u7684\u9884\u8bad\u7ec3\u6a21\u578b\u00a0<code>unsloth\/Llama-3.2-1B-bnb-4bitt<\/code>\u00a0\u52a0\u8f7d\u6a21\u578b\u548c\u6807\u8bb0\u5668\u3002\u8fd9\u9488\u5bf9 4 \u4f4d\u7cbe\u5ea6\u8fdb\u884c\u4e86\u4f18\u5316\uff0c\u53ef\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\u91cf\u5e76\u63d0\u9ad8\u8bad\u7ec3\u901f\u5ea6\uff0c\u800c\u4e0d\u4f1a\u663e\u7740\u5f71\u54cd\u6027\u80fd\u3002\u00a0<code>load_in_4bit=True<\/code>\u00a0\u53c2\u6570\u53ef\u5b9e\u73b0\u8fd9\u79cd\u9ad8\u6548\u7684 4 \u4f4d\u91cf\u5316\uff0c\u4f7f\u5176\u66f4\u9002\u5408\u5728\u6027\u80fd\u8f83\u5f31\u7684\u786c\u4ef6\u4e0a\u8fdb\u884c\u5fae\u8c03\u3002<\/p>\n<ul>\n<li>\u5e94\u7528 PEFT\uff08\u53c2\u6570\u9ad8\u6548\u5fae\u8c03\uff09<\/li>\n<\/ul>\n<p>\u7136\u540e\u6211\u4eec\u4f7f\u7528\u00a0<code>get_peft_model<\/code>\u00a0\u914d\u7f6e\u6a21\u578b\uff0c\u5b83\u5e94\u7528\u4e86 LoRA\uff08\u4f4e\u79e9\u81ea\u9002\u5e94\uff09\u6280\u672f\u3002\u8fd9\u79cd\u65b9\u6cd5\u4fa7\u91cd\u4e8e\u4ec5\u5fae\u8c03\u6a21\u578b\u7684\u7279\u5b9a\u5c42\u6216\u90e8\u5206\uff0c\u800c\u4e0d\u662f\u6574\u4e2a\u7f51\u7edc\uff0c\u4ece\u800c\u5927\u5927\u51cf\u5c11\u4e86\u6240\u9700\u7684\u8ba1\u7b97\u8d44\u6e90\u3002<\/p>\n<p>\u53c2\u6570<code>r=16<\/code>\u00a0\u548c\u00a0<code>lora_alpha=16<\/code>\u00a0\u7b49\u53ef\u8c03\u6574\u8fd9\u4e9b\u81ea\u9002\u5e94\u7684\u590d\u6742\u6027\u548c\u7f29\u653e\u6bd4\u4f8b\u3002\u4f7f\u7528\u00a0<code>target_modules<\/code>\u00a0\u6307\u5b9a\u5e94\u8c03\u6574\u6a21\u578b\u7684\u54ea\u4e9b\u5c42\uff0c\u5176\u4e2d\u5305\u62ec\u6d89\u53ca\u6ce8\u610f\u673a\u5236\u7684\u5173\u952e\u7ec4\u4ef6\uff0c\u5982\u00a0<code>q_proj<\/code>\u3001\u00a0<code>k_proj<\/code>\u00a0\u548c\u00a0<code>v_proj<\/code>\u3002<\/p>\n<p><code>use_rslora=True<\/code>\u00a0\u53ef\u6fc0\u6d3b\u00a0<code>Rank-Stabilized LoRA<\/code>\uff0c\u4ece\u800c\u63d0\u9ad8\u5fae\u8c03\u8fc7\u7a0b\u7684\u7a33\u5b9a\u6027\u3002\u00a0<code>use_gradient_checkpointing=&quot;unsloth&quot;<\/code>\u00a0\u786e\u4fdd\u901a\u8fc7\u9009\u62e9\u6027\u5730\u4ec5\u5b58\u50a8\u5fc5\u8981\u7684\u8ba1\u7b97\u6765\u4f18\u5316\u8bad\u7ec3\u671f\u95f4\u7684\u5185\u5b58\u4f7f\u7528\uff0c\u4ece\u800c\u8fdb\u4e00\u6b65\u63d0\u9ad8\u6a21\u578b\u7684\u6548\u7387\u3002<\/p>\n<ul>\n<li>\u9a8c\u8bc1\u53ef\u8bad\u7ec3\u53c2\u6570<\/li>\n<\/ul>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u4f7f\u7528\u00a0<code>model.print_trainable_parameters()<\/code>\u00a0\u6253\u5370\u51fa\u5c06\u5728\u5fae\u8c03\u671f\u95f4\u66f4\u65b0\u7684\u53c2\u6570\u6570\u91cf\uff0c\u4ece\u800c\u9a8c\u8bc1\u662f\u5426\u53ea\u8bad\u7ec3\u4e86\u6a21\u578b\u7684\u9884\u671f\u90e8\u5206\u3002<\/p>\n<p>\u8fd9\u79cd\u6280\u672f\u7ec4\u5408\u4e0d\u4ec5\u4f7f\u5fae\u8c03\u8fc7\u7a0b\u66f4\u52a0\u9ad8\u6548\uff0c\u800c\u4e14\u66f4\u6613\u4e8e\u8bbf\u95ee\uff0c\u5373\u4f7f\u5728\u8ba1\u7b97\u8d44\u6e90\u6709\u9650\u7684\u60c5\u51b5\u4e0b\uff0c\u4f60\u4e5f\u53ef\u4ee5\u90e8\u7f72\u6b64\u6a21\u578b\u3002<\/p>\n<p>\u5c06 tokenz \u7684\u6700\u5927\u957f\u5ea6\u8bbe\u7f6e\u4e3a 5020 \u8db3\u4ee5\u4f5c\u4e3a\u4f4e\u79e9\u81ea\u9002\u5e94 (LoRA) \u8fdb\u884c\u8bad\u7ec3\uff0c\u4f46\u60a8\u53ef\u4ee5\u6839\u636e\u4f60\u7684\u6570\u636e\u548c\u8981\u6c42\u4f7f\u7528\u3002<\/p>\n<pre><code>max_seq_length = 5020\nmodel, tokenizer = FastLanguageModel.from_pretrained(\n    model_,\n    max_seq_length=max_seq_length,\n    load_in_4bit=True,\n    dtype=None,\n)\n\nmodel = FastLanguageModel.get_peft_model(\n    model,\n    r=16,\n    lora_alpha=16,\n    lora_dropout=0,\n    target_modules=[&quot;q_proj&quot;, &quot;k_proj&quot;, &quot;v_proj&quot;, &quot;up_proj&quot;, &quot;down_proj&quot;, &quot;o_proj&quot;, &quot;gate_proj&quot;],\n    use_rslora=True,\n    use_gradient_checkpointing=&quot;unsloth&quot;,\n    random_state = 32,\n    loftq_config = None,\n)\nprint(model.print_trainable_parameters())<\/code><\/pre>\n<h4>4.2 \u4e3a\u6a21\u578b\u63d0\u8981\u51c6\u5907\u6570\u636e<\/h4>\n<p>\u73b0\u5728\u662f\u65f6\u5019\u8bbe\u8ba1\u7528\u4e8e\u5fc3\u7406\u5065\u5eb7\u5206\u6790\u7684\u683c\u5f0f\u63d0\u793a\u4e86\u3002\u6b64\u529f\u80fd\u4ece\u5fc3\u7406\u5b66\u89d2\u5ea6\u5206\u6790\u8f93\u5165\u6587\u672c\uff0c\u8bc6\u522b\u60c5\u7eea\u56f0\u6270\u3001\u5e94\u5bf9\u673a\u5236\u6216\u6574\u4f53\u5fc3\u7406\u5065\u5eb7\u7684\u6307\u6807\u3002\u5b83\u8fd8\u5f3a\u8c03\u6f5c\u5728\u7684\u62c5\u5fe7\u6216\u79ef\u6781\u65b9\u9762\uff0c\u4e3a\u6bcf\u4e2a\u89c2\u5bdf\u7ed3\u679c\u63d0\u4f9b\u7b80\u8981\u89e3\u91ca\u3002\u6211\u4eec\u5c06\u51c6\u5907\u8fd9\u4e9b\u6570\u636e\u4ee5\u4f9b\u6a21\u578b\u8fdb\u4e00\u6b65\u5904\u7406\uff0c\u786e\u4fdd\u6bcf\u4e2a\u8f93\u5165\u8f93\u51fa\u5bf9\u90fd\u5177\u6709\u6e05\u6670\u7684\u683c\u5f0f\uff0c\u4ee5\u4fbf\u8fdb\u884c\u6709\u6548\u5206\u6790\u3002<\/p>\n<p>\u8981\u8bb0\u4f4f\u7684\u8981\u70b9\uff1a<\/p>\n<ul>\n<li>\u6570\u636e\u63d0\u793a\u7ed3\u6784<\/li>\n<\/ul>\n<p><code>data_prompt<\/code>\u00a0\u662f\u4e00\u4e2a\u683c\u5f0f\u5316\u7684\u5b57\u7b26\u4e32\u6a21\u677f\uff0c\u65e8\u5728\u6307\u5bfc\u6a21\u578b\u5206\u6790\u63d0\u4f9b\u7684\u6587\u672c\u3002\u5b83\u5305\u62ec\u8f93\u5165\u6587\u672c\uff08\u4e0a\u4e0b\u6587\uff09\u548c\u6a21\u578b\u54cd\u5e94\u7684\u5360\u4f4d\u7b26\u3002\u8be5\u6a21\u677f\u4e13\u95e8\u63d0\u793a\u6a21\u578b\u8bc6\u522b\u5fc3\u7406\u5065\u5eb7\u6307\u6807\uff0c\u4f7f\u6a21\u578b\u66f4\u5bb9\u6613\u5fae\u8c03\u5fc3\u7406\u5065\u5eb7\u76f8\u5173\u4efb\u52a1\u3002<\/p>\n<ul>\n<li>\u5e8f\u5217\u7ed3\u675f\u6807\u8bb0<\/li>\n<\/ul>\n<p>\u4ece\u6807\u8bb0\u5668\u4e2d\u68c0\u7d22\u00a0<code>EOS_TOKEN<\/code>\u00a0\u4ee5\u8868\u793a\u6bcf\u4e2a\u6587\u672c\u5e8f\u5217\u7684\u7ed3\u675f\u3002\u6b64\u6807\u8bb0\u5bf9\u4e8e\u6a21\u578b\u8bc6\u522b\u63d0\u793a\u4f55\u65f6\u7ed3\u675f\u81f3\u5173\u91cd\u8981\uff0c\u6709\u52a9\u4e8e\u5728\u8bad\u7ec3\u6216\u63a8\u7406\u671f\u95f4\u7ef4\u62a4\u6570\u636e\u7684\u7ed3\u6784\u3002<\/p>\n<ul>\n<li>\u683c\u5f0f\u5316\u51fd\u6570<\/li>\n<\/ul>\n<p><code>formatting_prompt<\/code>\u00a0\u7528\u4e8e\u83b7\u53d6\u4e00\u6279\u793a\u4f8b\u5e76\u6839\u636e\u00a0<code>data_prompt<\/code>\u00a0\u5bf9\u5176\u8fdb\u884c\u683c\u5f0f\u5316\u3002\u5b83\u904d\u5386\u8f93\u5165\u548c\u8f93\u51fa\u5bf9\uff0c\u5c06\u5b83\u4eec\u63d2\u5165\u6a21\u677f\u5e76\u5728\u672b\u5c3e\u9644\u52a0 EOS \u6807\u8bb0\u3002\u7136\u540e\uff0c\u8be5\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u683c\u5f0f\u5316\u6587\u672c\u7684\u5b57\u5178\uff0c\u53ef\u7528\u4e8e\u6a21\u578b\u8bad\u7ec3\u6216\u8bc4\u4f30\u3002<\/p>\n<ul>\n<li>\u51fd\u6570\u8f93\u51fa<\/li>\n<\/ul>\n<p>\u8be5\u51fd\u6570\u8f93\u51fa\u4e00\u4e2a\u5b57\u5178\uff0c\u5176\u4e2d\u952e\u4e3a \u201c\u6587\u672c\u201d\uff0c\u503c\u662f\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7684\u5217\u8868\u3002\u6bcf\u4e2a\u5b57\u7b26\u4e32\u4ee3\u8868\u6a21\u578b\u7684\u5b8c\u6574\u51c6\u5907\u63d0\u793a\uff0c\u7ed3\u5408\u4e86\u4e0a\u4e0b\u6587\u3001\u54cd\u5e94\u548c\u7ed3\u6784\u5316\u63d0\u793a\u6a21\u677f\u3002<\/p>\n<pre><code>data_prompt = &quot;&quot;&quot;Analyze the provided text from a mental health perspective. Identify any indicators of emotional distress, coping mechanisms, or psychological well-being. Highlight any potential concerns or positive aspects related to mental health, and provide a brief explanation for each observation.\n### Input:\n{}\n### Response:\n{}&quot;&quot;&quot;\n\nEOS_TOKEN = tokenizer.eos_token\ndef formatting_prompt(examples):\n    inputs       = examples[&quot;Context&quot;]\n    outputs      = examples[&quot;Response&quot;]\n    texts = []\n    for input_, output in zip(inputs, outputs):\n        text = data_prompt.format(input_, output) + EOS_TOKEN\n        texts.append(text)\n    return { &quot;text&quot; : texts, }<\/code><\/pre>\n<h4>4.3 \u683c\u5f0f\u5316\u6570\u636e\u4ee5\u8fdb\u884c\u8bad\u7ec3<\/h4>\n<pre><code>training_data = Dataset.from_pandas(filtered_data)\ntraining_data = training_data.map(formatting_prompt, batched=True)<\/code><\/pre>\n<h4>4.4 \u4f7f\u7528\u81ea\u5b9a\u4e49\u53c2\u6570\u548c\u6570\u636e\u8fdb\u884c\u6a21\u578b\u8bad\u7ec3<\/h4>\n<p>\u4f7f\u7528\u00a0<code>sudo apt-get update<\/code>\u00a0\u5237\u65b0\u53ef\u7528\u8f6f\u4ef6\u5305\u5217\u8868\uff0c\u4f7f\u7528\u00a0<code>sudo apt-get install build-essential<\/code>\u00a0\u5b89\u88c5\u5fc5\u5907\u5de5\u5177\u3002\u5982\u679c\u51fa\u73b0\u4efb\u4f55\u9519\u8bef\uff0c\u8bf7\u5728 shell \u4e0a\u8fd0\u884c\u6b64\u547d\u4ee4\u3002<\/p>\n<pre><code>#sudo apt-get update\n#sudo apt-get install build-essential<\/code><\/pre>\n<h4>4.5 \u8bad\u7ec3\u8bbe\u7f6e\u5f00\u59cb\u5fae\u8c03\uff01<\/h4>\n<p>\u6211\u4eec\u5c06\u4f7f\u7528\u6a21\u578b\u548c\u6807\u8bb0\u5668\u4ee5\u53ca\u8bad\u7ec3\u6570\u636e\u96c6\u521d\u59cb\u5316\u00a0<code>SFTTrainer<\/code>\u3002\u00a0<code>dataset_text_field<\/code>\u00a0\u53c2\u6570\u6307\u5b9a\u6570\u636e\u96c6\u4e2d\u5305\u542b\u6211\u4eec\u4e0a\u9762\u51c6\u5907\u7684\u7528\u4e8e\u8bad\u7ec3\u7684\u6587\u672c\u7684\u5b57\u6bb5\u3002\u8bad\u7ec3\u5668\u8d1f\u8d23\u7ba1\u7406\u5fae\u8c03\u8fc7\u7a0b\uff0c\u5305\u62ec\u6570\u636e\u5904\u7406\u548c\u6a21\u578b\u66f4\u65b0\u3002<\/p>\n<p>\u8bad\u7ec3\u53c2\u6570\u5982\u4e0b\uff1a<\/p>\n<p><code>TrainingArguments<\/code>\u00a0\u7c7b\u7528\u4e8e\u5b9a\u4e49\u8bad\u7ec3\u8fc7\u7a0b\u7684\u5173\u952e\u8d85\u53c2\u6570\u3002\u8fd9\u4e9b\u5305\u62ec\uff1a<\/p>\n<ul>\n<li><code>learning_rate=3e-4<\/code>\uff1a\u8bbe\u7f6e\u4f18\u5316\u5668\u7684\u5b66\u4e60\u7387\u3002<\/li>\n<li><code>per_device_train_batch_size=32<\/code>\uff1a\u5b9a\u4e49\u6bcf\u4e2a\u8bbe\u5907\u7684\u6279\u6b21\u5927\u5c0f\uff0c\u4f18\u5316 GPU \u4f7f\u7528\u7387\u3002<\/li>\n<li><code>num_train_epochs=20<\/code>\uff1a\u6307\u5b9a\u8bad\u7ec3\u5468\u671f\u6570\u3002<\/li>\n<li><code>fp16=not is_bfloat16_supported()<\/code>\u00a0\u548c\u00a0<code>bf16=is_bfloat16_supported()<\/code>\uff1a\u542f\u7528\u6df7\u5408\u7cbe\u5ea6\u8bad\u7ec3\u4ee5\u51cf\u5c11\u5185\u5b58\u4f7f\u7528\u91cf\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u786c\u4ef6\u652f\u6301\u3002<\/li>\n<li><code>optim=&quot;adamw_8bit&quot;<\/code>\uff1a\u4f7f\u7528 8 \u4f4d AdamW \u4f18\u5316\u5668\u6765\u9ad8\u6548\u4f7f\u7528\u5185\u5b58\u3002<\/li>\n<li><code>weight_decay=0.01<\/code>\uff1a\u5e94\u7528\u6743\u91cd\u8870\u51cf\u4ee5\u9632\u6b62\u8fc7\u5ea6\u62df\u5408\u3002<\/li>\n<li><code>output_dir=&quot;output&quot;<\/code>\uff1a\u6307\u5b9a\u5c06\u4fdd\u5b58\u8bad\u7ec3\u6a21\u578b\u548c\u65e5\u5fd7\u7684\u76ee\u5f55\u3002<\/li>\n<\/ul>\n<p>\u6700\u540e\uff0c\u6211\u4eec\u8c03\u7528\u00a0<code>trainer.train()<\/code>\u00a0\u65b9\u6cd5\u6765\u542f\u52a8\u8bad\u7ec3\u8fc7\u7a0b\u3002\u5b83\u4f7f\u7528\u6211\u4eec\u5b9a\u4e49\u7684\u53c2\u6570\u6765\u5fae\u8c03\u6a21\u578b\uff0c\u8c03\u6574\u6743\u91cd\u5e76\u4ece\u63d0\u4f9b\u7684\u6570\u636e\u96c6\u4e2d\u5b66\u4e60\u3002\u8bad\u7ec3\u5668\u8fd8\u5904\u7406\u6570\u636e\u6253\u5305\u548c\u68af\u5ea6\u7d2f\u79ef\uff0c\u4f18\u5316\u8bad\u7ec3\u7ba1\u9053\u4ee5\u83b7\u5f97\u66f4\u597d\u7684\u6027\u80fd\u3002<\/p>\n<p>\u6709\u65f6 pytorch \u4f1a\u4fdd\u7559\u5185\u5b58\u5e76\u4e14\u4e0d\u4f1a\u91ca\u653e\u56de\u6765\u3002\u8bbe\u7f6e\u6b64\u73af\u5883\u53d8\u91cf\u53ef\u4ee5\u5e2e\u52a9\u907f\u514d\u5185\u5b58\u788e\u7247\u3002\u4f60\u53ef\u4ee5\u5728\u8fd0\u884c\u6a21\u578b\u4e4b\u524d\u5728\u73af\u5883\u6216\u811a\u672c\u4e2d\u8bbe\u7f6e\u5b83<\/p>\n<pre><code>export PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True\n<\/code><\/pre>\n<p>\u5982\u679c GPU \u4e2d\u4e0d\u518d\u9700\u8981\u53d8\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528 del \u5220\u9664\u5b83\u4eec\uff0c\u7136\u540e\u8c03\u7528<\/p>\n<pre><code>torch.cuda.empty_cache()\n<\/code><\/pre>\n<pre><code>trainer=SFTTrainer(\n    model=model,\n    tokenizer=tokenizer,\n    train_dataset=training_data,\n    dataset_text_field=&quot;text&quot;,\n    max_seq_length=max_seq_length,\n    dataset_num_proc=2,\n    packing=True,\n    args=TrainingArguments(\n        learning_rate=3e-4,\n        lr_scheduler_type=&quot;linear&quot;,\n        per_device_train_batch_size=16,\n        gradient_accumulation_steps=8,\n        num_train_epochs=40,\n        fp16=not is_bfloat16_supported(),\n        bf16=is_bfloat16_supported(),\n        logging_steps=1,\n        optim=&quot;adamw_8bit&quot;,\n        weight_decay=0.01,\n        warmup_steps=10,\n        output_dir=&quot;output&quot;,\n        seed=0,\n    ),\n)\n\ntrainer.train()<\/code><\/pre>\n<h4>4.6 \u63a8\u7406<\/h4>\n<pre><code>text=&quot;I&#039;m going through some things with my feelings and myself. I barely sleep and I do nothing but think about how I&#039;m worthless and how I shouldn&#039;t be here. I&#039;ve never tried or contemplated suicide. I&#039;ve always wanted to fix my issues, but I never get around to it. How can I change my feeling of being worthless to everyone?&quot;\n<\/code><\/pre>\n<p>\u6ce8\u610f\uff1a\u8ba9\u6211\u4eec\u4f7f\u7528\u5fae\u8c03\u6a21\u578b\u8fdb\u884c\u63a8\u7406\uff0c\u4ee5\u4fbf\u6839\u636e\u4e0e\u5fc3\u7406\u5065\u5eb7\u76f8\u5173\u7684\u63d0\u793a\u751f\u6210\u53cd\u5e94\uff01<\/p>\n<p>\u4ee5\u4e0b\u662f\u9700\u8981\u6ce8\u610f\u7684\u4e00\u4e9b\u8981\u70b9\uff1a<\/p>\n<p><code>model = FastLanguageModel.for_inference(model)<\/code>\u00a0\u4e13\u95e8\u4e3a\u63a8\u7406\u914d\u7f6e\u6a21\u578b\uff0c\u4f18\u5316\u5176\u751f\u6210\u54cd\u5e94\u7684\u6027\u80fd\u3002<\/p>\n<p>\u4f7f\u7528\u00a0<code>tokenizer<\/code>\u00a0\u5bf9\u8f93\u5165\u6587\u672c\u8fdb\u884c\u6807\u8bb0\uff0c\u5b83\u5c06\u6587\u672c\u8f6c\u6362\u4e3a\u6a21\u578b\u53ef\u4ee5\u5904\u7406\u7684\u683c\u5f0f\u3002\u6211\u4eec\u4f7f\u7528\u00a0<code>data_prompt<\/code>\u00a0\u6765\u683c\u5f0f\u5316\u8f93\u5165\u6587\u672c\uff0c\u800c\u5c06\u54cd\u5e94\u5360\u4f4d\u7b26\u7559\u7a7a\u4ee5\u4ece\u6a21\u578b\u83b7\u53d6\u54cd\u5e94\u3002\u00a0<code>return_tensors = &quot;pt&quot;<\/code>\u00a0\u53c2\u6570\u6307\u5b9a\u8f93\u51fa\u5e94\u4e3a PyTorch \u5f20\u91cf\uff0c\u7136\u540e\u4f7f\u7528\u00a0<code>.to(&quot;cuda&quot;)<\/code>\u00a0\u5c06\u5176\u79fb\u52a8\u5230 GPU \u4ee5\u52a0\u5feb\u5904\u7406\u901f\u5ea6\u3002<\/p>\n<p><code>model.generate<\/code>\u00a0\u65b9\u6cd5\u6839\u636e\u6807\u8bb0\u5316\u7684\u8f93\u5165\u751f\u6210\u54cd\u5e94\u3002\u53c2\u6570\u00a0<code>max_new_tokens = 5020<\/code>\u00a0\u548c\u00a0<code>use_cache = True<\/code>\u00a0\u786e\u4fdd\u6a21\u578b\u53ef\u4ee5\u901a\u8fc7\u5229\u7528\u6765\u81ea\u5148\u524d\u5c42\u7684\u7f13\u5b58\u8ba1\u7b97\u6765\u6709\u6548\u5730\u751f\u6210\u957f\u800c\u8fde\u8d2f\u7684\u54cd\u5e94\u3002<\/p>\n<pre><code>model = FastLanguageModel.for_inference(model)\ninputs = tokenizer(\n[\n    data_prompt.format(\n        #instructions\n        text,\n        #answer\n        &quot;&quot;,\n    )\n], return_tensors = &quot;pt&quot;).to(&quot;cuda&quot;)\n\noutputs = model.generate(**inputs, max_new_tokens = 5020, use_cache = True)\nanswer=tokenizer.batch_decode(outputs)\nanswer = answer[0].split(&quot;### Response:&quot;)[-1]\nprint(&quot;Answer of the question is:&quot;, answer)<\/code><\/pre>\n<p>\u95ee\u9898\u7684\u7b54\u6848\u5982\u4e0b\uff1a<\/p>\n<pre><code>I&#039;m sorry to hear that you are feeling so overwhelmed. It sounds like you are trying to figure out what is going on with you. I would suggest that you see a therapist who specializes in working with people who are struggling with depression. Depression is a common issue that people struggle with. It is important to address the issue of depression in order to improve your quality of life. Depression can lead to other issues such as anxiety, hopelessness, and loss of pleasure in activities. Depression can also lead to thoughts of suicide. If you are thinking of suicide, please call 911 or go to the nearest hospital emergency department. If you are not thinking of suicide, but you are feeling overwhelmed, please call 800-273-8255. This number is free and confidential and you can talk to someone about anything. You can also go to www.suicidepreventionlifeline.org to find a local suicide prevention hotline.&lt;|end_of_text|&gt;\n<\/code><\/pre>\n<p>\u6ce8\u610f\uff1a\u4ee5\u4e0b\u662f\u6211\u4eec\u5982\u4f55\u5b89\u5168\u5730\u5c06\u7ecf\u8fc7\u5fae\u8c03\u7684\u6a21\u578b\u53ca\u5176\u6807\u8bb0\u5668\u63a8\u9001\u5230 Hugging Face Hub\uff0c\u4ee5\u4fbf\u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u4f7f\u7528\uff1a\u00a0<a href=\"https:\/\/huggingface.co\/ImranzamanML\/1B_finetuned_llama3.2\" title=\"ImranzamanML\/1B_finetuned_llama3.2\u00a0\">ImranzamanML\/1B_finetuned_llama3.2<\/a> \u3002<\/p>\n<pre><code>os.environ[&quot;HF_TOKEN&quot;] = &quot;hugging face token key, you can create from your HF account.&quot;\nmodel.push_to_hub(&quot;ImranzamanML\/1B_finetuned_llama3.2&quot;, use_auth_token=os.getenv(&quot;HF_TOKEN&quot;))\ntokenizer.push_to_hub(&quot;ImranzamanML\/1B_finetuned_llama3.2&quot;, use_auth_token=os.getenv(&quot;HF_TOKEN&quot;))<\/code><\/pre>\n<p>\u6ce8\u610f\uff1a\u6211\u4eec\u8fd8\u53ef\u4ee5\u5728\u673a\u5668\u672c\u5730\u4fdd\u5b58\u5fae\u8c03\u540e\u7684\u6a21\u578b\u53ca\u5176\u6807\u8bb0\u5668\u3002<\/p>\n<pre><code>model.save_pretrained(&quot;model\/1B_finetuned_llama3.2&quot;)\ntokenizer.save_pretrained(&quot;model\/1B_finetuned_llama3.2&quot;)<\/code><\/pre>\n<p>\u4e0b\u9762\u7684\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u52a0\u8f7d\u5df2\u4fdd\u5b58\u7684\u6a21\u578b\u5e76\u4f7f\u7528\u5b83\uff01<\/p>\n<pre><code>model, tokenizer = FastLanguageModel.from_pretrained(\nmodel_name = &quot;model\/1B_finetuned_llama3.2&quot;,\nmax_seq_length = 5020,\ndtype = None,\nload_in_4bit = True)<\/code><\/pre>\n<p>\u539f\u6587\u94fe\u63a5\uff1a<a href=\"http:\/\/www.bimant.com\/blog\/llama-3-2-fine-tuning-guide\/\" title=\"Llama 3.2 \u5fae\u8c03\u6307\u5357 - BimAnt\">Llama 3.2 \u5fae\u8c03\u6307\u5357 &#8211; BimAnt<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u7531 \u7b80\u60a6 SimpRead \u8f6c\u7801\uff0c \u539f\u6587\u5730\u5740 blog.csdn.net \u82f1\u6587\u539f\u6587\u5730\u5740 huggingfa [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-444","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/my.di.cloudns.asia\/index.php\/wp-json\/wp\/v2\/posts\/444","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/my.di.cloudns.asia\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/my.di.cloudns.asia\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/my.di.cloudns.asia\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/my.di.cloudns.asia\/index.php\/wp-json\/wp\/v2\/comments?post=444"}],"version-history":[{"count":2,"href":"https:\/\/my.di.cloudns.asia\/index.php\/wp-json\/wp\/v2\/posts\/444\/revisions"}],"predecessor-version":[{"id":446,"href":"https:\/\/my.di.cloudns.asia\/index.php\/wp-json\/wp\/v2\/posts\/444\/revisions\/446"}],"wp:attachment":[{"href":"https:\/\/my.di.cloudns.asia\/index.php\/wp-json\/wp\/v2\/media?parent=444"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/my.di.cloudns.asia\/index.php\/wp-json\/wp\/v2\/categories?post=444"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/my.di.cloudns.asia\/index.php\/wp-json\/wp\/v2\/tags?post=444"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}