import{s as is,n as cs,o as rs}from"../chunks/scheduler.1f027d5a.js";import{S as Us,i as ys,e as c,s as a,c as p,h as Ts,a as r,d as s,b as n,f as ms,g as M,j as U,k as os,l as ds,m as e,n as J,t as m,o,p as i}from"../chunks/index.6117a65d.js";import{C as ws}from"../chunks/CopyLLMTxtMenu.610b106b.js";import{D as bs,C as y}from"../chunks/DocNotebookDropdown.4ae0d470.js";import{H as d,E as us}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.bd18ae98.js";function js(kt){let T,Zl,Cl,Il,w,gl,b,Xl,u,Bl,j,$t='<em>作者：<a href="https://github.com/MrzEsma" rel="nofollow">Mohammadreza Esmaeiliyan</a></em>',Ql,h,Nt='在这个 Notebook 中，我们尝试对大型语言模型进行微调，且没有添加额外复杂性。该模型已针对客户级别的 GPU 进行优化，用于生成波斯语产品目录并以 JSON 格式生成结构化输出。它特别适用于从伊朗平台（如 <a href="https://basalam.com" rel="nofollow">Basalam</a>、<a href="https://divar.ir/" rel="nofollow">Divar</a>、<a href="https://www.digikala.com/" rel="nofollow">Digikala</a> 等）上的用户生成内容的非结构化标题和描述中创建结构化输出。',Gl,f,Et='你可以在 <a href="https://huggingface.co/BaSalam/Llama2-7b-entity-attr-v1" rel="nofollow">我们的 Hugging Face 账户</a> 查看微调后的 LLM。此外，我们还使用了最快的开源推理引擎之一 —— <a href="https://github.com/vllm-project/vllm" rel="nofollow">Vllm</a> 来进行推理。',Vl,_,vt="让我们开始吧！",Wl,C,Fl,R,Yt='<code>peft</code> 库，或称为参数高效微调（Parameter Efficient Fine-Tuning），旨在更高效地对大型语言模型（LLMs）进行微调。如果像传统神经网络那样打开并微调网络的上层，它将需要大量的计算和显著的 VRAM（显存）。随着近期论文中提出的方法，<code>peft</code> 库已被实现，用于高效地对 LLM 进行微调。你可以在这里了解更多关于 <code>peft</code> 的信息：<a href="https://huggingface.co/blog/peft" rel="nofollow">Hugging Face PEFT</a>。',kl,Z,$l,I,Nl,g,El,X,At="LoRA（低秩适配，Low-Rank Adaptation）通过构建并将低秩矩阵添加到每个模型层中来存储权重变化。这种方法仅打开这些层进行微调，而不改变原始模型权重，也不需要长时间训练。生成的权重非常轻量，可以多次产生，从而允许在将 LLM 加载到 RAM 中后微调多个任务。",vl,B,xt='你可以在 <a href="https://lightning.ai/pages/community/tutorial/lora-llm/" rel="nofollow">Lightning AI</a> 了解更多关于 LoRA 的信息。对于其他高效的训练方法，请参阅 <a href="https://huggingface.co/docs/transformers/perf_train_gpu_one" rel="nofollow">Hugging Face 性能训练文档</a> 和 <a href="https://huggingface.co/docs/trl/main/en/sft_trainer#enhance-models-performances-using-neftune" rel="nofollow">SFT Trainer 增强</a>。',Yl,Q,Al,G,Ht="QLoRA（量化低秩适配，Quantized Low-Rank Adaptation）是一种高效的微调方法，通过使用 4 位量化，使大型语言模型能够在更小的 GPU 上运行。该方法在减少内存使用的同时，保持了 16 位微调的全部性能，使得在单个 48GB GPU 上也可以微调最多 650 亿参数的模型。QLoRA 结合了 4 位 NormalFloat 数据类型、双重量化和分页优化器，有效管理内存。它允许对具有低秩适配器的模型进行微调，显著提高了 AI 模型开发的可访问性。",xl,V,zt='你可以在 <a href="https://huggingface.co/blog/4bit-transformers-bitsandbytes" rel="nofollow">Hugging Face</a> 上了解更多关于 QLoRA 的信息。',Hl,W,zl,F,Sl,k,Ll,$,ql,N,St="<code>LoraConfig</code> 对象用于在使用 Peft 库时配置 LoRA（低秩适配）设置。它可以帮助减少需要微调的参数数量，从而加速训练并减少内存使用。以下是各个参数的详细说明：",Dl,E,Lt="<li><strong><code>r</code></strong>：LoRA 中低秩矩阵的秩。该参数控制低秩适配的维度，直接影响模型适应能力和计算成本。</li> <li><strong><code>lora_alpha</code></strong>：该参数控制低秩适配矩阵的缩放因子。较高的 alpha 值可以提高模型学习新任务的能力。</li> <li><strong><code>lora_dropout</code></strong>：LoRA 的 dropout 比率。该参数有助于在微调过程中防止过拟合。在此案例中，设为 0.1。</li> <li><strong><code>bias</code></strong>：指定是否向低秩矩阵添加偏置项。在此案例中，设为 <code>&quot;none&quot;</code>，表示不添加偏置项。</li> <li><strong><code>task_type</code></strong>：定义模型微调的任务类型。在这里，<code>&quot;CAUSAL_LM&quot;</code> 表示任务是因果语言建模任务，即预测序列中的下一个单词。</li> <li><strong><code>target_modules</code></strong>：指定模型中将应用 LoRA 的模块。在此案例中，设为 <code>[&quot;q_proj&quot;, &quot;v_proj&quot;, &#39;k_proj&#39;]</code>，即模型注意力机制中的查询（query）、值（value）和键（key）投影层。</li>",Kl,v,qt="通过调整这些参数，<code>LoraConfig</code> 有助于在微调过程中高效地应用 LoRA 方法，从而优化计算资源和训练效率。",Ol,Y,Pl,A,Dt="这个代码块配置了使用 <strong>BitsAndBytes (bnb)</strong> 库的设置，该库提供了高效的内存管理和压缩技术，专为 PyTorch 模型设计。具体来说，它定义了如何加载和量化模型权重为 4 位精度，这对于减少内存使用并可能加速推理非常有用。",lt,x,Kt="<li><strong><code>load_in_4bit</code></strong>：一个布尔值，决定是否以 4 位精度加载模型。</li> <li><strong><code>bnb_4bit_quant_type</code></strong>：指定使用哪种类型的 4 位量化。在这里，设置为 4 位 NormalFloat (NF4) 量化类型，这是 QLoRA 中引入的一种新数据类型。该类型对于正态分布权重在信息理论上最优，提供了一种高效的方式来对模型进行量化以进行微调。</li> <li><strong><code>bnb_4bit_compute_dtype</code></strong>：设置用于涉及量化模型计算的数据类型。在 QLoRA 中，设置为 <code>&quot;float16&quot;</code>，这是混合精度训练中常用的类型，旨在平衡性能和精度。</li> <li><strong><code>bnb_4bit_use_double_quant</code></strong>：一个布尔值，指示是否使用双重量化。设置为 <code>False</code> 表示只使用单次量化，通常速度更快，但可能精度稍低。</li>",tt,H,st,z,Ot="QLoRA 使用了两种不同的数据类型：一种用于存储基础模型权重（在这里是 4 位 NormalFloat），另一种用于计算操作（16 位）。在前向和反向传播过程中，QLoRA 会将权重从存储格式解量化为计算格式。然而，它仅为 LoRA 参数计算梯度，这些参数使用的是 16 位 bfloat 数据类型。这样的做法确保了权重只有在必要时才会解压，从而在训练和推理过程中保持较低的内存使用量。",et,S,at,L,nt,q,pt,D,Pt="关于聊天模板，我们简要说明一下，为了理解用户和模型在模型训练过程中对话的结构，创建了一系列预留短语来区分用户的消息和模型的回复。这确保了模型能够准确理解每条消息的来源，并保持对话结构的连贯性。通常，遵循聊天模板有助于提高任务的准确性。然而，当微调数据集与模型之间存在分布偏移时，使用特定的聊天模板可能更加有助于提升效果。",Mt,K,ls='欲了解更多信息，请访问 <a href="https://huggingface.co/blog/chat-templates" rel="nofollow">Hugging Face 博客关于聊天模板</a>。',Jt,O,mt,P,ot,ll,it,tl,ts="<code>SFTTrainer</code> 随后被实例化，用于处理模型的监督微调（SFT）。这个训练器专门针对 SFT 进行设计，并包括了一些额外的参数，例如 <code>formatting_func</code> 和 <code>packing</code>，这些参数通常在标准的训练器类中是找不到的。",ct,sl,ss=`<strong><code>formatting_func</code></strong>：一个自定义函数，用于格式化训练样本，将指令和回复模板组合在一起。
<strong><code>packing</code></strong>：禁用将多个样本打包成一个序列，这是标准 <code>Trainer</code> 类中没有的参数。`,rt,el,Ut,al,yt,nl,Tt,pl,dt,Ml,wt,Jl,bt,ml,ut,ol,jt,il,ht,cl,es="在这里，我们将适配器与基础模型合并，并将合并后的模型推送到模型库中。你也可以只推送适配器到模型库，而避免推送沉重的基础模型文件，方法如下：",ft,rl,_t,Ul,as="然后，你可以按照以下方式加载模型：",Ct,yl,Rt,Tl,ns="通过这种方法，你能够高效地加载适配器模型，而不需要加载完整的基础模型，从而减少内存消耗并提高推理速度。",Zt,dl,It,wl,ps='<code>vllm</code> 库是目前用于大型语言模型（LLM）推理的最快引擎之一。有关可用选项的比较概览，你可以参考这篇博客：<a href="https://medium.com/@gsuresh957/7-frameworks-for-serving-llms-5044b533ee88" rel="nofollow">7 Frameworks for Serving LLMs</a>。',gt,bl,Ms="在这个示例中，我们对该任务使用了我们微调模型的第一个版本进行推理。",Xt,ul,Bt,jl,Qt,hl,Gt,fl,Js='在这篇博客中，你可以阅读关于微调大型语言模型（LLMs）的最佳实践：<a href="https://magazine.sebastianraschka.com/p/practical-tips-for-finetuning-llms?r=1h0eu9&amp;utm_campaign=post&amp;utm_medium=web" rel="nofollow">Sebastian Raschka 的杂志</a>。',Vt,_l,Wt,Rl,Ft;return w=new bs({props:{classNames:"absolute z-10 right-0 top-0",options:[{label:"Google Colab",value:"https://colab.research.google.com/github/huggingface/cookbook/blob/main/notebooks/zh-CN/fine_tuning_llm_to_generate_persian_product_catalogs_in_json_format.ipynb"}]}}),b=new ws({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),u=new d({props:{title:"微调大型语言模型以生成波斯语产品目录的 JSON 格式",local:"微调大型语言模型以生成波斯语产品目录的-json-格式",headingTag:"h1"}}),C=new y({props:{code:"aW1wb3J0JTIwdG9yY2glMEFmcm9tJTIwZGF0YXNldHMlMjBpbXBvcnQlMjBsb2FkX2RhdGFzZXQlMEFmcm9tJTIwdHJhbnNmb3JtZXJzJTIwaW1wb3J0JTIwKCUwQSUyMCUyMCUyMCUyMEF1dG9Nb2RlbEZvckNhdXNhbExNJTJDJTBBJTIwJTIwJTIwJTIwQXV0b1Rva2VuaXplciUyQyUwQSUyMCUyMCUyMCUyMEJpdHNBbmRCeXRlc0NvbmZpZyUyQyUwQSUyMCUyMCUyMCUyMFRyYWluaW5nQXJndW1lbnRzJTJDJTBBKSUwQWZyb20lMjBwZWZ0JTIwaW1wb3J0JTIwTG9yYUNvbmZpZyUyQyUyMFBlZnRNb2RlbCUwQWZyb20lMjB0cmwlMjBpbXBvcnQlMjBTRlRUcmFpbmVyJTJDJTIwRGF0YUNvbGxhdG9yRm9yQ29tcGxldGlvbk9ubHlMTQ==",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> (
    AutoModelForCausalLM,
    AutoTokenizer,
    BitsAndBytesConfig,
    TrainingArguments,
)
<span class="hljs-keyword">from</span> peft <span class="hljs-keyword">import</span> LoraConfig, PeftModel
<span class="hljs-keyword">from</span> trl <span class="hljs-keyword">import</span> SFTTrainer, DataCollatorForCompletionOnlyLM`,wrap:!1}}),Z=new d({props:{title:"设置超参数",local:"设置超参数",headingTag:"h2"}}),I=new y({props:{code:"JTIzJTIwR2VuZXJhbCUyMHBhcmFtZXRlcnMlMEFtb2RlbF9uYW1lJTIwJTNEJTIwJTIyTm91c1Jlc2VhcmNoJTJGTGxhbWEtMi03Yi1jaGF0LWhmJTIyJTIwJTIwJTIzJTIwVGhlJTIwbW9kZWwlMjB0aGF0JTIweW91JTIwd2FudCUyMHRvJTIwdHJhaW4lMjBmcm9tJTIwdGhlJTIwSHVnZ2luZyUyMEZhY2UlMjBodWIlMEFkYXRhc2V0X25hbWUlMjAlM0QlMjAlMjJCYVNhbGFtJTJGZW50aXR5LWF0dHJpYnV0ZS1kYXRhc2V0LUdQVC0zLjUtZ2VuZXJhdGVkLXYxJTIyJTIwJTIwJTIzJTIwVGhlJTIwaW5zdHJ1Y3Rpb24lMjBkYXRhc2V0JTIwdG8lMjB1c2UlMEFuZXdfbW9kZWwlMjAlM0QlMjAlMjJsbGFtYS1wZXJzaWFuLWNhdGFsb2ctZ2VuZXJhdG9yJTIyJTIwJTIwJTIzJTIwVGhlJTIwbmFtZSUyMGZvciUyMGZpbmUtdHVuZWQlMjBMb1JBJTIwQWRhcHRvcg==",highlighted:`<span class="hljs-comment"># General parameters</span>
model_name = <span class="hljs-string">&quot;NousResearch/Llama-2-7b-chat-hf&quot;</span>  <span class="hljs-comment"># The model that you want to train from the Hugging Face hub</span>
dataset_name = <span class="hljs-string">&quot;BaSalam/entity-attribute-dataset-GPT-3.5-generated-v1&quot;</span>  <span class="hljs-comment"># The instruction dataset to use</span>
new_model = <span class="hljs-string">&quot;llama-persian-catalog-generator&quot;</span>  <span class="hljs-comment"># The name for fine-tuned LoRA Adaptor</span>`,wrap:!1}}),g=new y({props:{code:"JTIzJTIwTG9SQSUyMHBhcmFtZXRlcnMlMEFsb3JhX3IlMjAlM0QlMjA2NCUwQWxvcmFfYWxwaGElMjAlM0QlMjBsb3JhX3IlMjAqJTIwMiUwQWxvcmFfZHJvcG91dCUyMCUzRCUyMDAuMSUwQXRhcmdldF9tb2R1bGVzJTIwJTNEJTIwJTVCJTIycV9wcm9qJTIyJTJDJTIwJTIydl9wcm9qJTIyJTJDJTIwJ2tfcHJvaiclNUQ=",highlighted:`<span class="hljs-comment"># LoRA parameters</span>
lora_r = <span class="hljs-number">64</span>
lora_alpha = lora_r * <span class="hljs-number">2</span>
lora_dropout = <span class="hljs-number">0.1</span>
target_modules = [<span class="hljs-string">&quot;q_proj&quot;</span>, <span class="hljs-string">&quot;v_proj&quot;</span>, <span class="hljs-string">&#x27;k_proj&#x27;</span>]`,wrap:!1}}),Q=new y({props:{code:"JTIzJTIwUUxvUkElMjBwYXJhbWV0ZXJzJTBBbG9hZF9pbl80Yml0JTIwJTNEJTIwVHJ1ZSUwQWJuYl80Yml0X2NvbXB1dGVfZHR5cGUlMjAlM0QlMjAlMjJmbG9hdDE2JTIyJTBBYm5iXzRiaXRfcXVhbnRfdHlwZSUyMCUzRCUyMCUyMm5mNCUyMiUwQWJuYl80Yml0X3VzZV9kb3VibGVfcXVhbnQlMjAlM0QlMjBGYWxzZQ==",highlighted:`<span class="hljs-comment"># QLoRA parameters</span>
load_in_4bit = <span class="hljs-literal">True</span>
bnb_4bit_compute_dtype = <span class="hljs-string">&quot;float16&quot;</span>
bnb_4bit_quant_type = <span class="hljs-string">&quot;nf4&quot;</span>
bnb_4bit_use_double_quant = <span class="hljs-literal">False</span>`,wrap:!1}}),W=new y({props:{code:"JTIzJTIwVHJhaW5pbmdBcmd1bWVudHMlMjBwYXJhbWV0ZXJzJTBBbnVtX3RyYWluX2Vwb2NocyUyMCUzRCUyMDElMEFmcDE2JTIwJTNEJTIwRmFsc2UlMEFiZjE2JTIw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Arguments parameters</span>
num_train_epochs = <span class="hljs-number">1</span>
fp16 = <span class="hljs-literal">False</span>
bf16 = <span class="hljs-literal">False</span>
per_device_train_batch_size = <span class="hljs-number">4</span>
gradient_accumulation_steps = <span class="hljs-number">1</span>
gradient_checkpointing = <span class="hljs-literal">True</span>
learning_rate = <span class="hljs-number">0.00015</span>
weight_decay = <span class="hljs-number">0.01</span>
optim = <span class="hljs-string">&quot;paged_adamw_32bit&quot;</span>
lr_scheduler_type = <span class="hljs-string">&quot;cosine&quot;</span>
max_steps = -<span class="hljs-number">1</span>
warmup_ratio = <span class="hljs-number">0.03</span>
group_by_length = <span class="hljs-literal">True</span>
save_steps = <span class="hljs-number">0</span>
logging_steps = <span class="hljs-number">25</span>

<span class="hljs-comment"># SFT parameters</span>
max_seq_length = <span class="hljs-literal">None</span>
packing = <span class="hljs-literal">False</span>
device_map = {<span class="hljs-string">&quot;&quot;</span>: <span class="hljs-number">0</span>}

<span class="hljs-comment"># Dataset parameters</span>
use_special_template = <span class="hljs-literal">True</span>
response_template = <span class="hljs-string">&#x27; ### Answer:&#x27;</span>
instruction_prompt_template = <span class="hljs-string">&#x27;&quot;### Human:&quot;&#x27;</span>
use_llama_like_model = <span class="hljs-literal">True</span>`,wrap:!1}}),F=new d({props:{title:"模型训练",local:"模型训练",headingTag:"h2"}}),k=new y({props:{code:"JTIzJTIwTG9hZCUyMGRhdGFzZXQlMjAoeW91JTIwY2FuJTIwcHJvY2VzcyUyMGl0JTIwaGVyZSklMEFkYXRhc2V0JTIwJTNEJTIwbG9hZF9kYXRhc2V0KGRhdGFzZXRfbmFtZSUyQyUyMHNwbGl0JTNEJTIydHJhaW4lMjIpJTBBcGVyY2VudF9vZl90cmFpbl9kYXRhc2V0JTIwJTNEJTIwMC45NSUwQW90aGVyX2NvbHVtbnMlMjAlM0QlMjAlNUJpJTIwZm9yJTIwaSUyMGluJTIwZGF0YXNldC5jb2x1bW5fbmFtZXMlMjBpZiUyMGklMjBub3QlMjBpbiUyMCU1QidpbnN0cnVjdGlvbiclMkMlMjAnb3V0cHV0JyU1RCU1RCUwQWRhdGFzZXQlMjAlM0QlMjBkYXRhc2V0LnJlbW92ZV9jb2x1bW5zKG90aGVyX2NvbHVtbnMpJTBBc3BsaXRfZGF0YXNldCUyMCUzRCUyMGRhdGFzZXQudHJhaW5fdGVzdF9zcGxpdCh0cmFpbl9zaXplJTNEaW50KGRhdGFzZXQubnVtX3Jvd3MlMjAqJTIwcGVyY2VudF9vZl90cmFpbl9kYXRhc2V0KSUyQyUyMHNlZWQlM0QxOSUyQyUyMHNodWZmbGUlM0RGYWxzZSklMEF0cmFpbl9kYXRhc2V0JTIwJTNEJTIwc3BsaXRfZGF0YXNldCU1QiUyMnRyYWluJTIyJTVEJTBBZXZhbF9kYXRhc2V0JTIwJTNEJTIwc3BsaXRfZGF0YXNldCU1QiUyMnRlc3QlMjIlNUQlMEFwcmludChmJTIyU2l6ZSUyMG9mJTIwdGhlJTIwdHJhaW4lMjBzZXQlM0ElMjAlN0JsZW4odHJhaW5fZGF0YXNldCklN0QuJTIwU2l6ZSUyMG9mJTIwdGhlJTIwdmFsaWRhdGlvbiUyMHNldCUzQSUyMCU3QmxlbihldmFsX2RhdGFzZXQpJTdEJTIyKQ==",highlighted:`<span class="hljs-comment"># Load dataset (you can process it here)</span>
dataset = load_dataset(dataset_name, split=<span class="hljs-string">&quot;train&quot;</span>)
percent_of_train_dataset = <span class="hljs-number">0.95</span>
other_columns = [i <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> dataset.column_names <span class="hljs-keyword">if</span> i <span class="hljs-keyword">not</span> <span class="hljs-keyword">in</span> [<span class="hljs-string">&#x27;instruction&#x27;</span>, <span class="hljs-string">&#x27;output&#x27;</span>]]
dataset = dataset.remove_columns(other_columns)
split_dataset = dataset.train_test_split(train_size=<span class="hljs-built_in">int</span>(dataset.num_rows * percent_of_train_dataset), seed=<span class="hljs-number">19</span>, shuffle=<span class="hljs-literal">False</span>)
train_dataset = split_dataset[<span class="hljs-string">&quot;train&quot;</span>]
eval_dataset = split_dataset[<span class="hljs-string">&quot;test&quot;</span>]
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Size of the train set: <span class="hljs-subst">{<span class="hljs-built_in">len</span>(train_dataset)}</span>. Size of the validation set: <span class="hljs-subst">{<span class="hljs-built_in">len</span>(eval_dataset)}</span>&quot;</span>)`,wrap:!1}}),$=new y({props:{code:"JTIzJTIwTG9hZCUyMExvUkElMjBjb25maWd1cmF0aW9uJTBBcGVmdF9jb25maWclMjAlM0QlMjBMb3JhQ29uZmlnKCUwQSUyMCUyMCUyMCUyMHIlM0Rsb3JhX3IlMkMlMEElMjAlMjAlMjAlMjBsb3JhX2FscGhhJTNEbG9yYV9hbHBoYSUyQyUwQSUyMCUyMCUyMCUyMGxvcmFfZHJvcG91dCUzRGxvcmFfZHJvcG91dCUyQyUwQSUyMCUyMCUyMCUyMGJpYXMlM0QlMjJub25lJTIyJTJDJTBBJTIwJTIwJTIwJTIwdGFza190eXBlJTNEJTIyQ0FVU0FMX0xNJTIyJTJDJTBBJTIwJTIwJTIwJTIwdGFyZ2V0X21vZHVsZXMlM0R0YXJnZXRfbW9kdWxlcyUwQSk=",highlighted:`<span class="hljs-comment"># Load LoRA configuration</span>
peft_config = LoraConfig(
    r=lora_r,
    lora_alpha=lora_alpha,
    lora_dropout=lora_dropout,
    bias=<span class="hljs-string">&quot;none&quot;</span>,
    task_type=<span class="hljs-string">&quot;CAUSAL_LM&quot;</span>,
    target_modules=target_modules
)`,wrap:!1}}),Y=new y({props:{code:"JTIzJTIwTG9hZCUyMFFMb1JBJTIwY29uZmlndXJhdGlvbiUwQWNvbXB1dGVfZHR5cGUlMjAlM0QlMjBnZXRhdHRyKHRvcmNoJTJDJTIwYm5iXzRiaXRfY29tcHV0ZV9kdHlwZSklMEElMEFibmJfY29uZmlnJTIwJTNEJTIwQml0c0FuZEJ5dGVzQ29uZmlnKCUwQSUyMCUyMCUyMCUyMGxvYWRfaW5fNGJpdCUzRGxvYWRfaW5fNGJpdCUyQyUwQSUyMCUyMCUyMCUyMGJuYl80Yml0X3F1YW50X3R5cGUlM0RibmJfNGJpdF9xdWFudF90eXBlJTJDJTBBJTIwJTIwJTIwJTIwYm5iXzRiaXRfY29tcHV0ZV9kdHlwZSUzRGNvbXB1dGVfZHR5cGUlMkMlMEElMjAlMjAlMjAlMjBibmJfNGJpdF91c2VfZG91YmxlX3F1YW50JTNEYm5iXzRiaXRfdXNlX2RvdWJsZV9xdWFudCUyQyUwQSk=",highlighted:`<span class="hljs-comment"># Load QLoRA configuration</span>
compute_dtype = <span class="hljs-built_in">getattr</span>(torch, bnb_4bit_compute_dtype)

bnb_config = BitsAndBytesConfig(
    load_in_4bit=load_in_4bit,
    bnb_4bit_quant_type=bnb_4bit_quant_type,
    bnb_4bit_compute_dtype=compute_dtype,
    bnb_4bit_use_double_quant=bnb_4bit_use_double_quant,
)`,wrap:!1}}),H=new d({props:{title:"为什么我们有两种数据类型（quant_type 和 compute_type）？",local:"为什么我们有两种数据类型quanttype-和-computetype",headingTag:"h3"}}),S=new y({props:{code:"JTIzJTIwTG9hZCUyMGJhc2UlMjBtb2RlbCUwQW1vZGVsJTIwJTNEJTIwQXV0b01vZGVsRm9yQ2F1c2FsTE0uZnJvbV9wcmV0cmFpbmVkKCUwQSUyMCUyMCUyMCUyMG1vZGVsX25hbWUlMkMlMEElMjAlMjAlMjAlMjBxdWFudGl6YXRpb25fY29uZmlnJTNEYm5iX2NvbmZpZyUyQyUwQSUyMCUyMCUyMCUyMGRldmljZV9tYXAlM0RkZXZpY2VfbWFwJTBBKSUwQW1vZGVsLmNvbmZpZy51c2VfY2FjaGUlMjAlM0QlMjBGYWxzZQ==",highlighted:`<span class="hljs-comment"># Load base model</span>
model = AutoModelForCausalLM.from_pretrained(
    model_name,
    quantization_config=bnb_config,
    device_map=device_map
)
model.config.use_cache = <span class="hljs-literal">False</span>`,wrap:!1}}),L=new y({props:{code:"JTIzJTIwU2V0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CUyMCUyMCUyMGxyX3NjaGVkdWxlcl90eXBlJTNEbHJfc2NoZWR1bGVyX3R5cGUlMEEp",highlighted:`<span class="hljs-comment"># Set training parameters</span>
training_arguments = TrainingArguments(
    output_dir=new_model,
    num_train_epochs=num_train_epochs,
    per_device_train_batch_size=per_device_train_batch_size,
    gradient_accumulation_steps=gradient_accumulation_steps,
    optim=optim,
    save_steps=save_steps,
    logging_steps=logging_steps,
    learning_rate=learning_rate,
    weight_decay=weight_decay,
    fp16=fp16,
    bf16=bf16,
    max_steps=max_steps,
    warmup_ratio=warmup_ratio,
    gradient_checkpointing=gradient_checkpointing,
    group_by_length=group_by_length,
    lr_scheduler_type=lr_scheduler_type
)`,wrap:!1}}),q=new y({props:{code:"JTIzJTIwTG9hZCUyMHRva2VuaXplciUwQXRva2VuaXplciUyMCUzRCUyMEF1dG9Ub2tlbml6ZXIuZnJvbV9wcmV0cmFpbmVkKG1vZGVsX25hbWUlMkMlMjB0cnVzdF9yZW1vdGVfY29kZSUzRFRydWUpJTBBdG9rZW5pemVyLnBhZF90b2tlbiUyMCUzRCUyMHRva2VuaXplci5lb3NfdG9rZW4lMEF0b2tlbml6ZXIucGFkZGluZ19zaWRlJTIwJTNEJTIwJTIycmlnaHQlMjIlMjAlMjAlMjMlMjBGaXglMjB3ZWlyZCUyMG92ZXJmbG93JTIwaXNzdWUlMjB3aXRoJTIwZnAxNiUyMHRyYWluaW5nJTBBaWYlMjBub3QlMjB0b2tlbml6ZXIuY2hhdF90ZW1wbGF0ZSUzQSUwQSUyMCUyMCUyMCUyMHRva2VuaXplci5jaGF0X3RlbXBsYXRlJTIwJTNEJTIwJTIyJTdCJTI1JTIwZm9yJTIwbWVzc2FnZSUyMGluJTIwbWVzc2FnZXMlMjAlMjUlN0QlN0IlN0InJTNDJTdDaW1fc3RhcnQlN0MlM0UnJTIwJTJCJTIwbWVzc2FnZSU1Qidyb2xlJyU1RCUyMCUyQiUyMCclNUNuJyUyMCUyQiUyMG1lc3NhZ2UlNUInY29udGVudCclNUQlMjAlMkIlMjAnJTNDJTdDaW1fZW5kJTdDJTNFJyUyMCUyQiUyMCclNUNuJyU3RCU3RCU3QiUyNSUyMGVuZGZvciUyMCUyNSU3RCUyMg==",highlighted:`<span class="hljs-comment"># Load tokenizer</span>
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=<span class="hljs-literal">True</span>)
tokenizer.pad_token = tokenizer.eos_token
tokenizer.padding_side = <span class="hljs-string">&quot;right&quot;</span>  <span class="hljs-comment"># Fix weird overflow issue with fp16 training</span>
<span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> tokenizer.chat_template:
    tokenizer.chat_template = <span class="hljs-string">&quot;{% for message in messages %}{{&#x27;&lt;|im_start|&gt;&#x27; + message[&#x27;role&#x27;] + &#x27;\\n&#x27; + message[&#x27;content&#x27;] + &#x27;&lt;|im_end|&gt;&#x27; + &#x27;\\n&#x27;}}{% endfor %}&quot;</span>`,wrap:!1}}),O=new y({props:{code:"ZGVmJTIwc3BlY2lhbF9mb3JtYXR0aW5nX3Byb21wdHMoZXhhbXBsZSklM0ElMEElMjAlMjAlMjAlMjBvdXRwdXRfdGV4dHMlMjAlM0QlMjAlNUIlNUQlMEElMjAlMjAlMjAlMjBmb3IlMjBpJTIwaW4lMjByYW5nZShsZW4oZXhhbXBsZSU1QidpbnN0cnVjdGlvbiclNUQpKSUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHRleHQlMjAlM0QlMjBmJTIyJTdCaW5zdHJ1Y3Rpb25fcHJvbXB0X3RlbXBsYXRlJTdEJTdCZXhhbXBsZSU1QidpbnN0cnVjdGlvbiclNUQlNUJpJTVEJTdEJTVDbiU3QnJlc3BvbnNlX3RlbXBsYXRlJTdEJTIwJTdCZXhhbXBsZSU1QidvdXRwdXQnJTVEJTVCaSU1RCU3RCUyMiUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG91dHB1dF90ZXh0cy5hcHBlbmQodGV4dCklMEElMjAlMjAlMjAlMjByZXR1cm4lMjBvdXRwdXRfdGV4dHMlMEElMEElMEFkZWYlMjBub3JtYWxfZm9ybWF0dGluZ19wcm9tcHRzKGV4YW1wbGUpJTNBJTBBJTIwJTIwJTIwJTIwb3V0cHV0X3RleHRzJTIwJTNEJTIwJTVCJTVEJTBBJTIwJTIwJTIwJTIwZm9yJTIwaSUyMGluJTIwcmFuZ2UobGVuKGV4YW1wbGUlNUInaW5zdHJ1Y3Rpb24nJTVEKSklM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBjaGF0X3RlbXAlMjAlM0QlMjAlNUIlN0IlMjJyb2xlJTIyJTNBJTIwJTIyc3lzdGVtJTIyJTJDJTIwJTIyY29udGVudCUyMiUzQSUyMGV4YW1wbGUlNUInaW5zdHJ1Y3Rpb24nJTVEJTVCaSU1RCU3RCUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCU3QiUyMnJvbGUlMjIlM0ElMjAlMjJhc3Npc3RhbnQlMjIlMkMlMjAlMjJjb250ZW50JTIyJTNBJTIwZXhhbXBsZSU1QidvdXRwdXQnJTVEJTVCaSU1RCU3RCU1RCUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHRleHQlMjAlM0QlMjB0b2tlbml6ZXIuYXBwbHlfY2hhdF90ZW1wbGF0ZShjaGF0X3RlbXAlMkMlMjB0b2tlbml6ZSUzREZhbHNlKSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG91dHB1dF90ZXh0cy5hcHBlbmQodGV4dCklMEElMjAlMjAlMjAlMjByZXR1cm4lMjBvdXRwdXRfdGV4dHM=",highlighted:`<span class="hljs-keyword">def</span> <span class="hljs-title function_">special_formatting_prompts</span>(<span class="hljs-params">example</span>):
    output_texts = []
    <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-built_in">len</span>(example[<span class="hljs-string">&#x27;instruction&#x27;</span>])):
        text = <span class="hljs-string">f&quot;<span class="hljs-subst">{instruction_prompt_template}</span><span class="hljs-subst">{example[<span class="hljs-string">&#x27;instruction&#x27;</span>][i]}</span>\\n<span class="hljs-subst">{response_template}</span> <span class="hljs-subst">{example[<span class="hljs-string">&#x27;output&#x27;</span>][i]}</span>&quot;</span>
        output_texts.append(text)
    <span class="hljs-keyword">return</span> output_texts


<span class="hljs-keyword">def</span> <span class="hljs-title function_">normal_formatting_prompts</span>(<span class="hljs-params">example</span>):
    output_texts = []
    <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-built_in">len</span>(example[<span class="hljs-string">&#x27;instruction&#x27;</span>])):
        chat_temp = [{<span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;system&quot;</span>, <span class="hljs-string">&quot;content&quot;</span>: example[<span class="hljs-string">&#x27;instruction&#x27;</span>][i]},
                     {<span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;assistant&quot;</span>, <span class="hljs-string">&quot;content&quot;</span>: example[<span class="hljs-string">&#x27;output&#x27;</span>][i]}]
        text = tokenizer.apply_chat_template(chat_temp, tokenize=<span class="hljs-literal">False</span>)
        output_texts.append(text)
    <span class="hljs-keyword">return</span> output_texts`,wrap:!1}}),P=new y({props:{code:"aWYlMjB1c2Vfc3BlY2lhbF90ZW1wbGF0ZSUzQSUwQSUyMCUyMCUyMCUyMGZvcm1hdHRpbmdfZnVuYyUyMCUzRCUyMHNwZWNpYWxfZm9ybWF0dGluZ19wcm9tcHRzJTBBJTIwJTIwJTIwJTIwaWYlMjB1c2VfbGxhbWFfbGlrZV9tb2RlbCUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHJlc3BvbnNlX3RlbXBsYXRlX2lkcyUyMCUzRCUyMHRva2VuaXplci5lbmNvZGUocmVzcG9uc2VfdGVtcGxhdGUlMkMlMjBhZGRfc3BlY2lhbF90b2tlbnMlM0RGYWxzZSklNUIyJTNBJTVEJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwY29sbGF0b3IlMjAlM0QlMjBEYXRhQ29sbGF0b3JGb3JDb21wbGV0aW9uT25seUxNKHJlc3BvbnNlX3RlbXBsYXRlJTNEcmVzcG9uc2VfdGVtcGxhdGVfaWRzJTJDJTIwdG9rZW5pemVyJTNEdG9rZW5pemVyKSUwQSUyMCUyMCUyMCUyMGVsc2UlM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBjb2xsYXRvciUyMCUzRCUyMERhdGFDb2xsYXRvckZvckNvbXBsZXRpb25Pbmx5TE0ocmVzcG9uc2VfdGVtcGxhdGUlM0RyZXNwb25zZV90ZW1wbGF0ZSUyQyUyMHRva2VuaXplciUzRHRva2VuaXplciklMEFlbHNlJTNBJTBBJTIwJTIwJTIwJTIwZm9ybWF0dGluZ19mdW5jJTIwJTNEJTIwbm9ybWFsX2Zvcm1hdHRpbmdfcHJvbXB0cw==",highlighted:`<span class="hljs-keyword">if</span> use_special_template:
    formatting_func = special_formatting_prompts
    <span class="hljs-keyword">if</span> use_llama_like_model:
        response_template_ids = tokenizer.encode(response_template, add_special_tokens=<span class="hljs-literal">False</span>)[<span class="hljs-number">2</span>:]
        collator = DataCollatorForCompletionOnlyLM(response_template=response_template_ids, tokenizer=tokenizer)
    <span class="hljs-keyword">else</span>:
        collator = DataCollatorForCompletionOnlyLM(response_template=response_template, tokenizer=tokenizer)
<span class="hljs-keyword">else</span>:
    formatting_func = normal_formatting_prompts`,wrap:!1}}),ll=new y({props:{code:"dHJhaW5lciUyMCUzRCUyMFNGVFRyYWluZXIoJTBBJTIwJTIwJTIwJTIwbW9kZWwlM0Rtb2RlbCUyQyUwQSUyMCUyMCUyMCUyMHRyYWluX2RhdGFzZXQlM0R0cmFpbl9kYXRhc2V0JTJDJTBBJTIwJTIwJTIwJTIwZXZhbF9kYXRhc2V0JTNEZXZhbF9kYXRhc2V0JTJDJTBBJTIwJTIwJTIwJTIwcGVmdF9jb25maWclM0RwZWZ0X2NvbmZpZyUyQyUwQSUyMCUyMCUyMCUyMGZvcm1hdHRpbmdfZnVuYyUzRGZvcm1hdHRpbmdfZnVuYyUyQyUwQSUyMCUyMCUyMCUyMGRhdGFfY29sbGF0b3IlM0Rjb2xsYXRvciUyQyUwQSUyMCUyMCUyMCUyMG1heF9zZXFfbGVuZ3RoJTNEbWF4X3NlcV9sZW5ndGglMkMlMEElMjAlMjAlMjAlMjBwcm9jZXNzaW5nX2NsYXNzJTNEdG9rZW5pemVyJTJDJTBBJTIwJTIwJTIwJTIwYXJncyUzRHRyYWluaW5nX2FyZ3VtZW50cyUyQyUwQSUyMCUyMCUyMCUyMHBhY2tpbmclM0RwYWNraW5nJTBBKQ==",highlighted:`trainer = SFTTrainer(
    model=model,
    train_dataset=train_dataset,
    eval_dataset=eval_dataset,
    peft_config=peft_config,
    formatting_func=formatting_func,
    data_collator=collator,
    max_seq_length=max_seq_length,
    processing_class=tokenizer,
    args=training_arguments,
    packing=packing
)`,wrap:!1}}),el=new y({props:{code:"JTIzJTIwVHJhaW4lMjBtb2RlbCUwQXRyYWluZXIudHJhaW4oKSUwQSUwQSUyMyUyMFNhdmUlMjBmaW5lJTIwdHVuZWQlMjBMb3JhJTIwQWRhcHRvciUyMCUwQXRyYWluZXIubW9kZWwuc2F2ZV9wcmV0cmFpbmVkKG5ld19tb2RlbCk=",highlighted:`<span class="hljs-comment"># Train model</span>
trainer.train()

<span class="hljs-comment"># Save fine tuned Lora Adaptor </span>
trainer.model.save_pretrained(new_model)`,wrap:!1}}),al=new d({props:{title:"推理",local:"推理",headingTag:"h2"}}),nl=new y({props:{code:"aW1wb3J0JTIwdG9yY2glMEFpbXBvcnQlMjBnYyUwQSUwQSUwQWRlZiUyMGNsZWFyX2hhcmR3YXJlcygpJTNBJTBBJTIwJTIwJTIwJTIwdG9yY2guY2xlYXJfYXV0b2Nhc3RfY2FjaGUoKSUwQSUyMCUyMCUyMCUyMHRvcmNoLmN1ZGEuaXBjX2NvbGxlY3QoKSUwQSUyMCUyMCUyMCUyMHRvcmNoLmN1ZGEuZW1wdHlfY2FjaGUoKSUwQSUyMCUyMCUyMCUyMGdjLmNvbGxlY3QoKSUwQSUwQSUwQWNsZWFyX2hhcmR3YXJlcygpJTBBY2xlYXJfaGFyZHdhcmVzKCk=",highlighted:`<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">import</span> gc


<span class="hljs-keyword">def</span> <span class="hljs-title function_">clear_hardwares</span>():
    torch.clear_autocast_cache()
    torch.cuda.ipc_collect()
    torch.cuda.empty_cache()
    gc.collect()


clear_hardwares()
clear_hardwares()`,wrap:!1}}),pl=new y({props:{code:"ZGVmJTIwZ2VuZXJhdGUobW9kZWwlMkMlMjBwcm9tcHQlM0ElMjBzdHIlMkMlMjBrd2FyZ3MpJTNBJTBBJTIwJTIwJTIwJTIwdG9rZW5pemVkX3Byb21wdCUyMCUzRCUyMHRva2VuaXplcihwcm9tcHQlMkMlMjByZXR1cm5fdGVuc29ycyUzRCdwdCcpLnRvKG1vZGVsLmRldmljZSklMEElMEElMjAlMjAlMjAlMjBwcm9tcHRfbGVuZ3RoJTIwJTNEJTIwbGVuKHRva2VuaXplZF9wcm9tcHQuZ2V0KCdpbnB1dF9pZHMnKSU1QjAlNUQpJTBBJTBBJTIwJTIwJTIwJTIwd2l0aCUyMHRvcmNoLmN1ZGEuYW1wLmF1dG9jYXN0KCklM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBvdXRwdXRfdG9rZW5zJTIwJTNEJTIwbW9kZWwuZ2VuZXJhdGUoKip0b2tlbml6ZWRfcHJvbXB0JTJDJTIwKiprd2FyZ3MpJTIwaWYlMjBrd2FyZ3MlMjBlbHNlJTIwbW9kZWwuZ2VuZXJhdGUoKip0b2tlbml6ZWRfcHJvbXB0KSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG91dHB1dCUyMCUzRCUyMHRva2VuaXplci5kZWNvZGUob3V0cHV0X3Rva2VucyU1QjAlNUQlNUJwcm9tcHRfbGVuZ3RoJTNBJTVEJTJDJTIwc2tpcF9zcGVjaWFsX3Rva2VucyUzRFRydWUpJTBBJTBBJTIwJTIwJTIwJTIwcmV0dXJuJTIwb3V0cHV0",highlighted:`<span class="hljs-keyword">def</span> <span class="hljs-title function_">generate</span>(<span class="hljs-params">model, prompt: <span class="hljs-built_in">str</span>, kwargs</span>):
    tokenized_prompt = tokenizer(prompt, return_tensors=<span class="hljs-string">&#x27;pt&#x27;</span>).to(model.device)

    prompt_length = <span class="hljs-built_in">len</span>(tokenized_prompt.get(<span class="hljs-string">&#x27;input_ids&#x27;</span>)[<span class="hljs-number">0</span>])

    <span class="hljs-keyword">with</span> torch.cuda.amp.autocast():
        output_tokens = model.generate(**tokenized_prompt, **kwargs) <span class="hljs-keyword">if</span> kwargs <span class="hljs-keyword">else</span> model.generate(**tokenized_prompt)
        output = tokenizer.decode(output_tokens[<span class="hljs-number">0</span>][prompt_length:], skip_special_tokens=<span class="hljs-literal">True</span>)

    <span class="hljs-keyword">return</span> output`,wrap:!1}}),Ml=new y({props:{code:"YmFzZV9tb2RlbCUyMCUzRCUyMEF1dG9Nb2RlbEZvckNhdXNhbExNLmZyb21fcHJldHJhaW5lZChuZXdfbW9kZWwlMkMlMjByZXR1cm5fZGljdCUzRFRydWUlMkMlMjBkZXZpY2VfbWFwJTNEJ2F1dG8nJTJDJTIwdG9rZW4lM0QnJyklMEF0b2tlbml6ZXIlMjAlM0QlMjBBdXRvVG9rZW5pemVyLmZyb21fcHJldHJhaW5lZChuZXdfbW9kZWwlMkMlMjBtYXhfbGVuZ3RoJTNEbWF4X3NlcV9sZW5ndGgpJTBBbW9kZWwlMjAlM0QlMjBQZWZ0TW9kZWwuZnJvbV9wcmV0cmFpbmVkKGJhc2VfbW9kZWwlMkMlMjBuZXdfbW9kZWwpJTBBZGVsJTIwYmFzZV9tb2RlbA==",highlighted:`base_model = AutoModelForCausalLM.from_pretrained(new_model, return_dict=<span class="hljs-literal">True</span>, device_map=<span class="hljs-string">&#x27;auto&#x27;</span>, token=<span class="hljs-string">&#x27;&#x27;</span>)
tokenizer = AutoTokenizer.from_pretrained(new_model, max_length=max_seq_length)
model = PeftModel.from_pretrained(base_model, new_model)
<span class="hljs-keyword">del</span> base_model`,wrap:!1}}),Jl=new y({props:{code:"c2FtcGxlJTIwJTNEJTIwZXZhbF9kYXRhc2V0JTVCMCU1RCUwQWlmJTIwdXNlX3NwZWNpYWxfdGVtcGxhdGUlM0ElMEElMjAlMjAlMjAlMjBwcm9tcHQlMjAlM0QlMjBmJTIyJTdCaW5zdHJ1Y3Rpb25fcHJvbXB0X3RlbXBsYXRlJTdEJTdCc2FtcGxlJTVCJ2luc3RydWN0aW9uJyU1RCU3RCU1Q24lN0JyZXNwb25zZV90ZW1wbGF0ZSU3RCUyMiUwQWVsc2UlM0ElMEElMjAlMjAlMjAlMjBjaGF0X3RlbXAlMjAlM0QlMjAlNUIlN0IlMjJyb2xlJTIyJTNBJTIwJTIyc3lzdGVtJTIyJTJDJTIwJTIyY29udGVudCUyMiUzQSUyMHNhbXBsZSU1QidpbnN0cnVjdGlvbiclNUQlN0QlNUQlMEElMjAlMjAlMjAlMjBwcm9tcHQlMjAlM0QlMjB0b2tlbml6ZXIuYXBwbHlfY2hhdF90ZW1wbGF0ZShjaGF0X3RlbXAlMkMlMjB0b2tlbml6ZSUzREZhbHNlJTJDJTIwYWRkX2dlbmVyYXRpb25fcHJvbXB0JTNEVHJ1ZSk=",highlighted:`sample = eval_dataset[<span class="hljs-number">0</span>]
<span class="hljs-keyword">if</span> use_special_template:
    prompt = <span class="hljs-string">f&quot;<span class="hljs-subst">{instruction_prompt_template}</span><span class="hljs-subst">{sample[<span class="hljs-string">&#x27;instruction&#x27;</span>]}</span>\\n<span class="hljs-subst">{response_template}</span>&quot;</span>
<span class="hljs-keyword">else</span>:
    chat_temp = [{<span class="hljs-string">&quot;role&quot;</span>: <span class="hljs-string">&quot;system&quot;</span>, <span class="hljs-string">&quot;content&quot;</span>: sample[<span class="hljs-string">&#x27;instruction&#x27;</span>]}]
    prompt = tokenizer.apply_chat_template(chat_temp, tokenize=<span class="hljs-literal">False</span>, add_generation_prompt=<span class="hljs-literal">True</span>)`,wrap:!1}}),ml=new y({props:{code:"Z2VuX2t3YXJncyUyMCUzRCUyMCU3QiUyMm1heF9uZXdfdG9rZW5zJTIyJTNBJTIwMTAyNCU3RCUwQWdlbmVyYXRlZF90ZXh0cyUyMCUzRCUyMGdlbmVyYXRlKG1vZGVsJTNEbW9kZWwlMkMlMjBwcm9tcHQlM0Rwcm9tcHQlMkMlMjBrd2FyZ3MlM0RnZW5fa3dhcmdzKSUwQXByaW50KGdlbmVyYXRlZF90ZXh0cyk=",highlighted:`gen_kwargs = {<span class="hljs-string">&quot;max_new_tokens&quot;</span>: <span class="hljs-number">1024</span>}
generated_texts = generate(model=model, prompt=prompt, kwargs=gen_kwargs)
<span class="hljs-built_in">print</span>(generated_texts)`,wrap:!1}}),ol=new d({props:{title:"合并基础模型",local:"合并基础模型",headingTag:"h2"}}),il=new y({props:{code:"Y2xlYXJfaGFyZHdhcmVzKCklMEFtZXJnZWRfbW9kZWwlMjAlM0QlMjBtb2RlbC5tZXJnZV9hbmRfdW5sb2FkKCklMEFjbGVhcl9oYXJkd2FyZXMoKSUwQWRlbCUyMG1vZGVsJTBBYWRhcHRlcl9tb2RlbF9uYW1lJTIwJTNEJTIwJ3lvdXJfaGZfYWNjb3VudCUyRnlvdXJfZGVzaXJlZF9uYW1lJyUwQW1lcmdlZF9tb2RlbC5wdXNoX3RvX2h1YihhZGFwdGVyX21vZGVsX25hbWUp",highlighted:`clear_hardwares()
merged_model = model.merge_and_unload()
clear_hardwares()
<span class="hljs-keyword">del</span> model
adapter_model_name = <span class="hljs-string">&#x27;your_hf_account/your_desired_name&#x27;</span>
merged_model.push_to_hub(adapter_model_name)`,wrap:!1}}),rl=new y({props:{code:"bW9kZWwucHVzaF90b19odWIoYWRhcHRlcl9tb2RlbF9uYW1lKQ==",highlighted:"model.push_to_hub(adapter_model_name)",wrap:!1}}),yl=new y({props:{code:"Y29uZmlnJTIwJTNEJTIwUGVmdENvbmZpZy5mcm9tX3ByZXRyYWluZWQoYWRhcHRlcl9tb2RlbF9uYW1lKSUwQW1vZGVsJTIwJTNEJTIwQXV0b01vZGVsRm9yQ2F1c2FsTE0uZnJvbV9wcmV0cmFpbmVkKGNvbmZpZy5iYXNlX21vZGVsX25hbWVfb3JfcGF0aCUyQyUyMHJldHVybl9kaWN0JTNEVHJ1ZSUyQyUyMGxvYWRfaW5fOGJpdCUzRFRydWUlMkMlMjBkZXZpY2VfbWFwJTNEJ2F1dG8nKSUwQXRva2VuaXplciUyMCUzRCUyMEF1dG9Ub2tlbml6ZXIuZnJvbV9wcmV0cmFpbmVkKGNvbmZpZy5iYXNlX21vZGVsX25hbWVfb3JfcGF0aCklMEElMEElMjMlMjAlRTUlOEElQTAlRTglQkQlQkQlMjBMb3JhJTIwJUU2JUE4JUExJUU1JTlFJThCJTBBbW9kZWwlMjAlM0QlMjBQZWZ0TW9kZWwuZnJvbV9wcmV0cmFpbmVkKG1vZGVsJTJDJTIwYWRhcHRlcl9tb2RlbF9uYW1lKQ==",highlighted:`config = PeftConfig.from_pretrained(adapter_model_name)
model = AutoModelForCausalLM.from_pretrained(config.base_model_name_or_path, return_dict=<span class="hljs-literal">True</span>, load_in_8bit=<span class="hljs-literal">True</span>, device_map=<span class="hljs-string">&#x27;auto&#x27;</span>)
tokenizer = AutoTokenizer.from_pretrained(config.base_model_name_or_path)

<span class="hljs-comment"># 加载 Lora 模型</span>
model = PeftModel.from_pretrained(model, adapter_model_name)`,wrap:!1}}),dl=new d({props:{title:"用 Vllm 快速推理",local:"用-vllm-快速推理",headingTag:"h2"}}),ul=new y({props:{code:"ZnJvbSUyMHZsbG0lMjBpbXBvcnQlMjBMTE0lMkMlMj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1wbGF0ZSU3RCclMEFvdXRwdXRzJTIwJTNEJTIwbGxtLmdlbmVyYXRlKHByb21wdCUyQyUyMHNhbXBsaW5nX3BhcmFtcyklMEElMEFwcmludChvdXRwdXRzJTVCMCU1RC5vdXRwdXRzJTVCMCU1RC50ZXh0KQ==",highlighted:`<span class="hljs-keyword">from</span> vllm <span class="hljs-keyword">import</span> LLM, SamplingParams

prompt = <span class="hljs-string">&quot;&quot;&quot;### Question: here is a product title from a Iranian marketplace.  \\n         give me the Product Entity and Attributes of this product in Persian language.\\n         give the output in this json format: {&#x27;attributes&#x27;: {&#x27;attribute_name&#x27; : &lt;attribute value&gt;, ...}, &#x27;product_entity&#x27;: &#x27;&lt;product entity&gt;&#x27;}.\\n         Don&#x27;t make assumptions about what values to plug into json. Just give Json not a single word more.\\n         \\nproduct title:&quot;&quot;&quot;</span>
user_prompt_template = <span class="hljs-string">&#x27;### Question: &#x27;</span>
response_template = <span class="hljs-string">&#x27; ### Answer:&#x27;</span>

llm = LLM(model=<span class="hljs-string">&#x27;BaSalam/Llama2-7b-entity-attr-v1&#x27;</span>, gpu_memory_utilization=<span class="hljs-number">0.9</span>, trust_remote_code=<span class="hljs-literal">True</span>)

product = <span class="hljs-string">&#x27;مانتو اسپرت پانیذ قد جلوی کار حدودا 85 سانتی متر قد پشت کار حدودا 88 سانتی متر&#x27;</span>
sampling_params = SamplingParams(temperature=<span class="hljs-number">0.0</span>, max_tokens=<span class="hljs-number">75</span>)
prompt = <span class="hljs-string">f&#x27;<span class="hljs-subst">{user_prompt_template}</span> <span class="hljs-subst">{prompt}</span><span class="hljs-subst">{product}</span>\\n <span class="hljs-subst">{response_template}</span>&#x27;</span>
outputs = llm.generate(prompt, sampling_params)

<span class="hljs-built_in">print</span>(outputs[<span class="hljs-number">0</span>].outputs[<span class="hljs-number">0</span>].text)`,wrap:!1}}),jl=new d({props:{title:"样例输出",local:"样例输出",headingTag:"h3"}}),hl=new y({props:{code:"JTdCJTBBJTIwJTIwJTIwJTIwJTIyYXR0cmlidXRlcyUyMiUzQSUyMCU3QiUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMiVEOSU4MiVEOCVBRiUyMCVEOCVBQyVEOSU4NCVEOSU4OCVEQiU4QyUyMCVEQSVBOSVEOCVBNyVEOCVCMSUyMiUzQSUyMCUyMjg1JTIwJUQ4JUIzJUQ4JUE3JUQ5JTg2JUQ4JUFBJURCJThDJTIwJUQ5JTg1JUQ4JUFBJUQ4JUIxJTIyJTJDJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIyJUQ5JTgyJUQ4JUFGJTIwJUQ5JUJFJUQ4JUI0JUQ4JUFBJTIwJURBJUE5JUQ4JUE3JUQ4JUIxJTIyJTNBJTIwJTIyODglMjAlRDglQjMlRDglQTclRDklODYlRDglQUElREIlOEMlMjAlRDklODUlRDglQUElRDglQjElMjIlMEElMjAlMjAlMjAlMjAlN0QlMkMlMEElMjAlMjAlMjAlMjAlMjJwcm9kdWN0X2VudGl0eSUyMiUzQSUyMCUyMiVEOSU4NSVEOCVBNyVEOSU4NiVEOCVBQSVEOSU4OCUyMCVEOCVBNyVEOCVCMyVEOSVCRSVEOCVCMSVEOCVBQSUyMiUwQSU3RA==",highlighted:`<span class="hljs-punctuation">{</span>
    <span class="hljs-attr">&quot;attributes&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
        <span class="hljs-attr">&quot;قد جلوی کار&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;85 سانتی متر&quot;</span><span class="hljs-punctuation">,</span>
        <span class="hljs-attr">&quot;قد پشت کار&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;88 سانتی متر&quot;</span>
    <span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
    <span class="hljs-attr">&quot;product_entity&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;مانتو اسپرت&quot;</span>
<span class="hljs-punctuation">}</span>`,wrap:!1}}),_l=new us({props:{source:"https://github.com/huggingface/cookbook/blob/main/notebooks/zh-CN/fine_tuning_llm_to_generate_persian_product_catalogs_in_json_format.md"}}),{c(){T=c("meta"),Zl=a(),Cl=c("p"),Il=a(),p(w.$$.fragment),gl=a(),p(b.$$.fragment),Xl=a(),p(u.$$.fragment),Bl=a(),j=c("p"),j.innerHTML=$t,Ql=a(),h=c("p"),h.innerHTML=Nt,Gl=a(),f=c("p"),f.innerHTML=Et,Vl=a(),_=c("p"),_.textContent=vt,Wl=a(),p(C.$$.fragment),Fl=a(),R=c("p"),R.innerHTML=Yt,kl=a(),p(Z.$$.fragment),$l=a(),p(I.$$.fragment),Nl=a(),p(g.$$.fragment),El=a(),X=c("p"),X.textContent=At,vl=a(),B=c("p"),B.innerHTML=xt,Yl=a(),p(Q.$$.fragment),Al=a(),G=c("p"),G.textContent=Ht,xl=a(),V=c("p"),V.innerHTML=zt,Hl=a(),p(W.$$.fragment),zl=a(),p(F.$$.fragment),Sl=a(),p(k.$$.fragment),Ll=a(),p($.$$.fragment),ql=a(),N=c("p"),N.innerHTML=St,Dl=a(),E=c("ul"),E.innerHTML=Lt,Kl=a(),v=c("p"),v.innerHTML=qt,Ol=a(),p(Y.$$.fragment),Pl=a(),A=c("p"),A.innerHTML=Dt,lt=a(),x=c("ul"),x.innerHTML=Kt,tt=a(),p(H.$$.fragment),st=a(),z=c("p"),z.textContent=Ot,et=a(),p(S.$$.fragment),at=a(),p(L.$$.fragment),nt=a(),p(q.$$.fragment),pt=a(),D=c("p"),D.textContent=Pt,Mt=a(),K=c("p"),K.innerHTML=ls,Jt=a(),p(O.$$.fragment),mt=a(),p(P.$$.fragment),ot=a(),p(ll.$$.fragment),it=a(),tl=c("p"),tl.innerHTML=ts,ct=a(),sl=c("p"),sl.innerHTML=ss,rt=a(),p(el.$$.fragment),Ut=a(),p(al.$$.fragment),yt=a(),p(nl.$$.fragment),Tt=a(),p(pl.$$.fragment),dt=a(),p(Ml.$$.fragment),wt=a(),p(Jl.$$.fragment),bt=a(),p(ml.$$.fragment),ut=a(),p(ol.$$.fragment),jt=a(),p(il.$$.fragment),ht=a(),cl=c("p"),cl.textContent=es,ft=a(),p(rl.$$.fragment),_t=a(),Ul=c("p"),Ul.textContent=as,Ct=a(),p(yl.$$.fragment),Rt=a(),Tl=c("p"),Tl.textContent=ns,Zt=a(),p(dl.$$.fragment),It=a(),wl=c("p"),wl.innerHTML=ps,gt=a(),bl=c("p"),bl.textContent=Ms,Xt=a(),p(ul.$$.fragment),Bt=a(),p(jl.$$.fragment),Qt=a(),p(hl.$$.fragment),Gt=a(),fl=c("p"),fl.innerHTML=Js,Vt=a(),p(_l.$$.fragment),Wt=a(),Rl=c("p"),this.h()},l(l){const t=Ts("svelte-u9bgzb",document.head);T=r(t,"META",{name:!0,content:!0}),t.forEach(s),Zl=n(l),Cl=r(l,"P",{}),ms(Cl).forEach(s),Il=n(l),M(w.$$.fragment,l),gl=n(l),M(b.$$.fragment,l),Xl=n(l),M(u.$$.fragment,l),Bl=n(l),j=r(l,"P",{"data-svelte-h":!0}),U(j)!=="svelte-8a0kwr"&&(j.innerHTML=$t),Ql=n(l),h=r(l,"P",{"data-svelte-h":!0}),U(h)!=="svelte-ytjka3"&&(h.innerHTML=Nt),Gl=n(l),f=r(l,"P",{"data-svelte-h":!0}),U(f)!=="svelte-18zof4y"&&(f.innerHTML=Et),Vl=n(l),_=r(l,"P",{"data-svelte-h":!0}),U(_)!=="svelte-izpy37"&&(_.textContent=vt),Wl=n(l),M(C.$$.fragment,l),Fl=n(l),R=r(l,"P",{"data-svelte-h":!0}),U(R)!=="svelte-v8byij"&&(R.innerHTML=Yt),kl=n(l),M(Z.$$.fragment,l),$l=n(l),M(I.$$.fragment,l),Nl=n(l),M(g.$$.fragment,l),El=n(l),X=r(l,"P",{"data-svelte-h":!0}),U(X)!=="svelte-17g0396"&&(X.textContent=At),vl=n(l),B=r(l,"P",{"data-svelte-h":!0}),U(B)!=="svelte-lbe80f"&&(B.innerHTML=xt),Yl=n(l),M(Q.$$.fragment,l),Al=n(l),G=r(l,"P",{"data-svelte-h":!0}),U(G)!=="svelte-1rjxcef"&&(G.textContent=Ht),xl=n(l),V=r(l,"P",{"data-svelte-h":!0}),U(V)!=="svelte-1c1a47q"&&(V.innerHTML=zt),Hl=n(l),M(W.$$.fragment,l),zl=n(l),M(F.$$.fragment,l),Sl=n(l),M(k.$$.fragment,l),Ll=n(l),M($.$$.fragment,l),ql=n(l),N=r(l,"P",{"data-svelte-h":!0}),U(N)!=="svelte-18wq4yr"&&(N.innerHTML=St),Dl=n(l),E=r(l,"UL",{"data-svelte-h":!0}),U(E)!=="svelte-df25sr"&&(E.innerHTML=Lt),Kl=n(l),v=r(l,"P",{"data-svelte-h":!0}),U(v)!=="svelte-2de196"&&(v.innerHTML=qt),Ol=n(l),M(Y.$$.fragment,l),Pl=n(l),A=r(l,"P",{"data-svelte-h":!0}),U(A)!=="svelte-1cw7yvz"&&(A.innerHTML=Dt),lt=n(l),x=r(l,"UL",{"data-svelte-h":!0}),U(x)!=="svelte-p1afkq"&&(x.innerHTML=Kt),tt=n(l),M(H.$$.fragment,l),st=n(l),z=r(l,"P",{"data-svelte-h":!0}),U(z)!=="svelte-103x3r6"&&(z.textContent=Ot),et=n(l),M(S.$$.fragment,l),at=n(l),M(L.$$.fragment,l),nt=n(l),M(q.$$.fragment,l),pt=n(l),D=r(l,"P",{"data-svelte-h":!0}),U(D)!=="svelte-vxdgqe"&&(D.textContent=Pt),Mt=n(l),K=r(l,"P",{"data-svelte-h":!0}),U(K)!=="svelte-1ncr8o1"&&(K.innerHTML=ls),Jt=n(l),M(O.$$.fragment,l),mt=n(l),M(P.$$.fragment,l),ot=n(l),M(ll.$$.fragment,l),it=n(l),tl=r(l,"P",{"data-svelte-h":!0}),U(tl)!=="svelte-y0ejnv"&&(tl.innerHTML=ts),ct=n(l),sl=r(l,"P",{"data-svelte-h":!0}),U(sl)!=="svelte-n6jo8f"&&(sl.innerHTML=ss),rt=n(l),M(el.$$.fragment,l),Ut=n(l),M(al.$$.fragment,l),yt=n(l),M(nl.$$.fragment,l),Tt=n(l),M(pl.$$.fragment,l),dt=n(l),M(Ml.$$.fragment,l),wt=n(l),M(Jl.$$.fragment,l),bt=n(l),M(ml.$$.fragment,l),ut=n(l),M(ol.$$.fragment,l),jt=n(l),M(il.$$.fragment,l),ht=n(l),cl=r(l,"P",{"data-svelte-h":!0}),U(cl)!=="svelte-ro7ka9"&&(cl.textContent=es),ft=n(l),M(rl.$$.fragment,l),_t=n(l),Ul=r(l,"P",{"data-svelte-h":!0}),U(Ul)!=="svelte-xuk7qr"&&(Ul.textContent=as),Ct=n(l),M(yl.$$.fragment,l),Rt=n(l),Tl=r(l,"P",{"data-svelte-h":!0}),U(Tl)!=="svelte-1x8pg25"&&(Tl.textContent=ns),Zt=n(l),M(dl.$$.fragment,l),It=n(l),wl=r(l,"P",{"data-svelte-h":!0}),U(wl)!=="svelte-efl6cb"&&(wl.innerHTML=ps),gt=n(l),bl=r(l,"P",{"data-svelte-h":!0}),U(bl)!=="svelte-19bm9ra"&&(bl.textContent=Ms),Xt=n(l),M(ul.$$.fragment,l),Bt=n(l),M(jl.$$.fragment,l),Qt=n(l),M(hl.$$.fragment,l),Gt=n(l),fl=r(l,"P",{"data-svelte-h":!0}),U(fl)!=="svelte-1nce2nx"&&(fl.innerHTML=Js),Vt=n(l),M(_l.$$.fragment,l),Wt=n(l),Rl=r(l,"P",{}),ms(Rl).forEach(s),this.h()},h(){os(T,"name","hf:doc:metadata"),os(T,"content",hs)},m(l,t){ds(document.head,T),e(l,Zl,t),e(l,Cl,t),e(l,Il,t),J(w,l,t),e(l,gl,t),J(b,l,t),e(l,Xl,t),J(u,l,t),e(l,Bl,t),e(l,j,t),e(l,Ql,t),e(l,h,t),e(l,Gl,t),e(l,f,t),e(l,Vl,t),e(l,_,t),e(l,Wl,t),J(C,l,t),e(l,Fl,t),e(l,R,t),e(l,kl,t),J(Z,l,t),e(l,$l,t),J(I,l,t),e(l,Nl,t),J(g,l,t),e(l,El,t),e(l,X,t),e(l,vl,t),e(l,B,t),e(l,Yl,t),J(Q,l,t),e(l,Al,t),e(l,G,t),e(l,xl,t),e(l,V,t),e(l,Hl,t),J(W,l,t),e(l,zl,t),J(F,l,t),e(l,Sl,t),J(k,l,t),e(l,Ll,t),J($,l,t),e(l,ql,t),e(l,N,t),e(l,Dl,t),e(l,E,t),e(l,Kl,t),e(l,v,t),e(l,Ol,t),J(Y,l,t),e(l,Pl,t),e(l,A,t),e(l,lt,t),e(l,x,t),e(l,tt,t),J(H,l,t),e(l,st,t),e(l,z,t),e(l,et,t),J(S,l,t),e(l,at,t),J(L,l,t),e(l,nt,t),J(q,l,t),e(l,pt,t),e(l,D,t),e(l,Mt,t),e(l,K,t),e(l,Jt,t),J(O,l,t),e(l,mt,t),J(P,l,t),e(l,ot,t),J(ll,l,t),e(l,it,t),e(l,tl,t),e(l,ct,t),e(l,sl,t),e(l,rt,t),J(el,l,t),e(l,Ut,t),J(al,l,t),e(l,yt,t),J(nl,l,t),e(l,Tt,t),J(pl,l,t),e(l,dt,t),J(Ml,l,t),e(l,wt,t),J(Jl,l,t),e(l,bt,t),J(ml,l,t),e(l,ut,t),J(ol,l,t),e(l,jt,t),J(il,l,t),e(l,ht,t),e(l,cl,t),e(l,ft,t),J(rl,l,t),e(l,_t,t),e(l,Ul,t),e(l,Ct,t),J(yl,l,t),e(l,Rt,t),e(l,Tl,t),e(l,Zt,t),J(dl,l,t),e(l,It,t),e(l,wl,t),e(l,gt,t),e(l,bl,t),e(l,Xt,t),J(ul,l,t),e(l,Bt,t),J(jl,l,t),e(l,Qt,t),J(hl,l,t),e(l,Gt,t),e(l,fl,t),e(l,Vt,t),J(_l,l,t),e(l,Wt,t),e(l,Rl,t),Ft=!0},p:cs,i(l){Ft||(m(w.$$.fragment,l),m(b.$$.fragment,l),m(u.$$.fragment,l),m(C.$$.fragment,l),m(Z.$$.fragment,l),m(I.$$.fragment,l),m(g.$$.fragment,l),m(Q.$$.fragment,l),m(W.$$.fragment,l),m(F.$$.fragment,l),m(k.$$.fragment,l),m($.$$.fragment,l),m(Y.$$.fragment,l),m(H.$$.fragment,l),m(S.$$.fragment,l),m(L.$$.fragment,l),m(q.$$.fragment,l),m(O.$$.fragment,l),m(P.$$.fragment,l),m(ll.$$.fragment,l),m(el.$$.fragment,l),m(al.$$.fragment,l),m(nl.$$.fragment,l),m(pl.$$.fragment,l),m(Ml.$$.fragment,l),m(Jl.$$.fragment,l),m(ml.$$.fragment,l),m(ol.$$.fragment,l),m(il.$$.fragment,l),m(rl.$$.fragment,l),m(yl.$$.fragment,l),m(dl.$$.fragment,l),m(ul.$$.fragment,l),m(jl.$$.fragment,l),m(hl.$$.fragment,l),m(_l.$$.fragment,l),Ft=!0)},o(l){o(w.$$.fragment,l),o(b.$$.fragment,l),o(u.$$.fragment,l),o(C.$$.fragment,l),o(Z.$$.fragment,l),o(I.$$.fragment,l),o(g.$$.fragment,l),o(Q.$$.fragment,l),o(W.$$.fragment,l),o(F.$$.fragment,l),o(k.$$.fragment,l),o($.$$.fragment,l),o(Y.$$.fragment,l),o(H.$$.fragment,l),o(S.$$.fragment,l),o(L.$$.fragment,l),o(q.$$.fragment,l),o(O.$$.fragment,l),o(P.$$.fragment,l),o(ll.$$.fragment,l),o(el.$$.fragment,l),o(al.$$.fragment,l),o(nl.$$.fragment,l),o(pl.$$.fragment,l),o(Ml.$$.fragment,l),o(Jl.$$.fragment,l),o(ml.$$.fragment,l),o(ol.$$.fragment,l),o(il.$$.fragment,l),o(rl.$$.fragment,l),o(yl.$$.fragment,l),o(dl.$$.fragment,l),o(ul.$$.fragment,l),o(jl.$$.fragment,l),o(hl.$$.fragment,l),o(_l.$$.fragment,l),Ft=!1},d(l){l&&(s(Zl),s(Cl),s(Il),s(gl),s(Xl),s(Bl),s(j),s(Ql),s(h),s(Gl),s(f),s(Vl),s(_),s(Wl),s(Fl),s(R),s(kl),s($l),s(Nl),s(El),s(X),s(vl),s(B),s(Yl),s(Al),s(G),s(xl),s(V),s(Hl),s(zl),s(Sl),s(Ll),s(ql),s(N),s(Dl),s(E),s(Kl),s(v),s(Ol),s(Pl),s(A),s(lt),s(x),s(tt),s(st),s(z),s(et),s(at),s(nt),s(pt),s(D),s(Mt),s(K),s(Jt),s(mt),s(ot),s(it),s(tl),s(ct),s(sl),s(rt),s(Ut),s(yt),s(Tt),s(dt),s(wt),s(bt),s(ut),s(jt),s(ht),s(cl),s(ft),s(_t),s(Ul),s(Ct),s(Rt),s(Tl),s(Zt),s(It),s(wl),s(gt),s(bl),s(Xt),s(Bt),s(Qt),s(Gt),s(fl),s(Vt),s(Wt),s(Rl)),s(T),i(w,l),i(b,l),i(u,l),i(C,l),i(Z,l),i(I,l),i(g,l),i(Q,l),i(W,l),i(F,l),i(k,l),i($,l),i(Y,l),i(H,l),i(S,l),i(L,l),i(q,l),i(O,l),i(P,l),i(ll,l),i(el,l),i(al,l),i(nl,l),i(pl,l),i(Ml,l),i(Jl,l),i(ml,l),i(ol,l),i(il,l),i(rl,l),i(yl,l),i(dl,l),i(ul,l),i(jl,l),i(hl,l),i(_l,l)}}}const hs='{"title":"微调大型语言模型以生成波斯语产品目录的 JSON 格式","local":"微调大型语言模型以生成波斯语产品目录的-json-格式","sections":[{"title":"设置超参数","local":"设置超参数","sections":[],"depth":2},{"title":"模型训练","local":"模型训练","sections":[{"title":"为什么我们有两种数据类型（quant_type 和 compute_type）？","local":"为什么我们有两种数据类型quanttype-和-computetype","sections":[],"depth":3}],"depth":2},{"title":"推理","local":"推理","sections":[],"depth":2},{"title":"合并基础模型","local":"合并基础模型","sections":[],"depth":2},{"title":"用 Vllm 快速推理","local":"用-vllm-快速推理","sections":[{"title":"样例输出","local":"样例输出","sections":[],"depth":3}],"depth":2}],"depth":1}';function fs(kt){return rs(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class gs extends Us{constructor(T){super(),ys(this,T,fs,js,is,{})}}export{gs as component};
