import{s as Js,n as ms,o as is}from"../chunks/scheduler.65852ee5.js";import{S as cs,i as Us,g as c,s as a,r as p,A as ys,h as U,f as s,c as n,j as Ms,u as M,x as y,k as os,y as rs,a as e,v as o,d as J,t as m,w as i}from"../chunks/index.aa74147d.js";import{D as Ts,C as r}from"../chunks/DocNotebookDropdown.1a7e6a42.js";import{H as w,E as ws}from"../chunks/getInferenceSnippets.a1cbc7e2.js";function ds(Wt){let T,Il,Cl,_l,d,Zl,u,gl,b,Ft='<em>作者：<a href="https://github.com/MrzEsma" rel="nofollow">Mohammadreza Esmaeiliyan</a></em>',Xl,j,kt='在这个 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> 等）上的用户生成内容的非结构化标题和描述中创建结构化输出。',Bl,h,Nt='你可以在 <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,f,$t="让我们开始吧！",Gl,C,Ql,R,Et='<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>。',Wl,I,Fl,_,kl,Z,Nl,g,Yt="LoRA（低秩适配，Low-Rank Adaptation）通过构建并将低秩矩阵添加到每个模型层中来存储权重变化。这种方法仅打开这些层进行微调，而不改变原始模型权重，也不需要长时间训练。生成的权重非常轻量，可以多次产生，从而允许在将 LLM 加载到 RAM 中后微调多个任务。",$l,X,At='你可以在 <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>。',El,B,Yl,V,zt="QLoRA（量化低秩适配，Quantized Low-Rank Adaptation）是一种高效的微调方法，通过使用 4 位量化，使大型语言模型能够在更小的 GPU 上运行。该方法在减少内存使用的同时，保持了 16 位微调的全部性能，使得在单个 48GB GPU 上也可以微调最多 650 亿参数的模型。QLoRA 结合了 4 位 NormalFloat 数据类型、双重量化和分页优化器，有效管理内存。它允许对具有低秩适配器的模型进行微调，显著提高了 AI 模型开发的可访问性。",Al,G,vt='你可以在 <a href="https://huggingface.co/blog/4bit-transformers-bitsandbytes" rel="nofollow">Hugging Face</a> 上了解更多关于 QLoRA 的信息。',zl,Q,vl,W,Hl,F,Sl,k,xl,N,Ht="<code>LoraConfig</code> 对象用于在使用 Peft 库时配置 LoRA（低秩适配）设置。它可以帮助减少需要微调的参数数量，从而加速训练并减少内存使用。以下是各个参数的详细说明：",ql,$,St="<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>",Ll,E,xt="通过调整这些参数，<code>LoraConfig</code> 有助于在微调过程中高效地应用 LoRA 方法，从而优化计算资源和训练效率。",Dl,Y,Ol,A,qt="这个代码块配置了使用 <strong>BitsAndBytes (bnb)</strong> 库的设置，该库提供了高效的内存管理和压缩技术，专为 PyTorch 模型设计。具体来说，它定义了如何加载和量化模型权重为 4 位精度，这对于减少内存使用并可能加速推理非常有用。",Kl,z,Lt="<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>",Pl,v,lt,H,Dt="QLoRA 使用了两种不同的数据类型：一种用于存储基础模型权重（在这里是 4 位 NormalFloat），另一种用于计算操作（16 位）。在前向和反向传播过程中，QLoRA 会将权重从存储格式解量化为计算格式。然而，它仅为 LoRA 参数计算梯度，这些参数使用的是 16 位 bfloat 数据类型。这样的做法确保了权重只有在必要时才会解压，从而在训练和推理过程中保持较低的内存使用量。",tt,S,st,x,et,q,at,L,Ot="关于聊天模板，我们简要说明一下，为了理解用户和模型在模型训练过程中对话的结构，创建了一系列预留短语来区分用户的消息和模型的回复。这确保了模型能够准确理解每条消息的来源，并保持对话结构的连贯性。通常，遵循聊天模板有助于提高任务的准确性。然而，当微调数据集与模型之间存在分布偏移时，使用特定的聊天模板可能更加有助于提升效果。",nt,D,Kt='欲了解更多信息，请访问 <a href="https://huggingface.co/blog/chat-templates" rel="nofollow">Hugging Face 博客关于聊天模板</a>。',pt,O,Mt,K,ot,P,Jt,ll,Pt="<code>SFTTrainer</code> 随后被实例化，用于处理模型的监督微调（SFT）。这个训练器专门针对 SFT 进行设计，并包括了一些额外的参数，例如 <code>formatting_func</code> 和 <code>packing</code>，这些参数通常在标准的训练器类中是找不到的。",mt,tl,ls=`<strong><code>formatting_func</code></strong>：一个自定义函数，用于格式化训练样本，将指令和回复模板组合在一起。
<strong><code>packing</code></strong>：禁用将多个样本打包成一个序列，这是标准 <code>Trainer</code> 类中没有的参数。`,it,sl,ct,el,Ut,al,yt,nl,rt,pl,Tt,Ml,wt,ol,dt,Jl,ut,ml,bt,il,ts="在这里，我们将适配器与基础模型合并，并将合并后的模型推送到模型库中。你也可以只推送适配器到模型库，而避免推送沉重的基础模型文件，方法如下：",jt,cl,ht,Ul,ss="然后，你可以按照以下方式加载模型：",ft,yl,Ct,rl,es="通过这种方法，你能够高效地加载适配器模型，而不需要加载完整的基础模型，从而减少内存消耗并提高推理速度。",Rt,Tl,It,wl,as='<code>vllm</code> 库是目前用于大型语言模型（LLM）推理的最快引擎之一。有关可用选项的比较概览，你可以参考这篇博客：<a href="https://medium.com/@gsuresh957/7-frameworks-for-serving-llms-5044b533ee88" rel="nofollow">7 Frameworks for Serving LLMs</a>。',_t,dl,ns="在这个示例中，我们对该任务使用了我们微调模型的第一个版本进行推理。",Zt,ul,gt,bl,Xt,jl,Bt,hl,ps='在这篇博客中，你可以阅读关于微调大型语言模型（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,fl,Gt,Rl,Qt;return d=new Ts({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"}]}}),u=new w({props:{title:"微调大型语言模型以生成波斯语产品目录的 JSON 格式",local:"微调大型语言模型以生成波斯语产品目录的-json-格式",headingTag:"h1"}}),C=new r({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}}),I=new w({props:{title:"设置超参数",local:"设置超参数",headingTag:"h2"}}),_=new r({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}}),Z=new r({props:{code:"JTIzJTIwTG9SQSUyMHBhcmFtZXRlcnMlMEFsb3JhX3IlMjAlM0QlMjA2NCUwQWxvcmFfYWxwaGElMjAlM0QlMjBsb3JhX3IlMjAqJTIwMiUwQWxvcmFfZHJvcG91dCUyMCUzRCUyMDAuMSUwQXRhcmdldF9tb2R1bGVzJTIwJTNEJTIwJTVCJTIycV9wcm9qJTIyJTJDJTIwJTIydl9wcm9qJTIyJTJDJTIwJTIya19wcm9qJTIyJTVE",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">&quot;k_proj&quot;</span>]`,wrap:!1}}),B=new r({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}}),Q=new r({props:{code:"JTIzJTIwVHJhaW5pbmdBcmd1bWVudHMlMjBwYXJhbWV0ZXJzJTBBbnVtX3RyYWluX2Vwb2NocyUyMCUzRCUyMDElMEFmcDE2JTIwJTNEJTIwRmFsc2UlMEFiZjE2JTIwJTNEJTIwRmFsc2UlMEFwZXJfZGV2aWNlX3RyYWluX2JhdGNoX3NpemUlMjAlM0QlMjA0JTBBZ3JhZGllbnRfYWNjdW11bGF0aW9uX3N0ZXBzJTIwJTNEJTIwMSUwQWdyYWRpZW50X2NoZWNrcG9pbnRpbmclMjAlM0QlMjBUcnVlJTBBbGVhcm5pbmdfcmF0ZSUyMCUzRCUyMDAuMDAwMTUlMEF3ZWlnaHRfZGVjYXklMjAlM0QlMjAwLjAxJTBBb3B0aW0lMjAlM0QlMjAlMjJwYWdlZF9hZGFtd18zMmJpdCUyMiUwQWxyX3NjaGVkdWxlcl90eXBlJTIwJTNEJTIwJTIyY29zaW5lJTIyJTBBbWF4X3N0ZXBzJTIwJTNEJTIwLTElMEF3YXJtdXBfcmF0aW8lMjAlM0QlMjAwLjAzJTBBZ3JvdXBfYnlfbGVuZ3RoJTIwJTNEJTIwVHJ1ZSUwQXNhdmVfc3RlcHMlMjAlM0QlMjAwJTBBbG9nZ2luZ19zdGVwcyUyMCUzRCUyMDI1JTBBJTBBJTIzJTIwU0ZUJTIwcGFyYW1ldGVycyUwQW1heF9zZXFfbGVuZ3RoJTIwJTNEJTIwTm9uZSUwQXBhY2tpbmclMjAlM0QlMjBGYWxzZSUwQWRldmljZV9tYXAlMjAlM0QlMjAlN0IlMjIlMjIlM0ElMjAwJTdEJTBBJTBBJTIzJTIwRGF0YXNldCUyMHBhcmFtZXRlcnMlMEF1c2Vfc3BlY2lhbF90ZW1wbGF0ZSUyMCUzRCUyMFRydWUlMEFyZXNwb25zZV90ZW1wbGF0ZSUyMCUzRCUyMCUyMiUyMCUyMyUyMyUyMyUyMEFuc3dlciUzQSUyMiUwQWluc3RydWN0aW9uX3Byb21wdF90ZW1wbGF0ZSUyMCUzRCUyMCclMjIlMjMlMjMlMjMlMjBIdW1hbiUzQSUyMiclMEF1c2VfbGxhbWFfbGlrZV9tb2RlbCUyMCUzRCUyMFRydWU=",highlighted:`<span class="hljs-comment"># TrainingArguments 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">&quot; ### Answer:&quot;</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}}),W=new w({props:{title:"模型训练",local:"模型训练",headingTag:"h2"}}),F=new r({props:{code:"JTIzJTIwTG9hZCUyMGRhdGFzZXQlMjAoeW91JTIwY2FuJTIwcHJvY2VzcyUyMGl0JTIwaGVyZSklMEFkYXRhc2V0JTIwJTNEJTIwbG9hZF9kYXRhc2V0KGRhdGFzZXRfbmFtZSUyQyUyMHNwbGl0JTNEJTIydHJhaW4lMjIpJTBBcGVyY2VudF9vZl90cmFpbl9kYXRhc2V0JTIwJTNEJTIwMC45NSUwQW90aGVyX2NvbHVtbnMlMjAlM0QlMjAlNUJpJTIwZm9yJTIwaSUyMGluJTIwZGF0YXNldC5jb2x1bW5fbmFtZXMlMjBpZiUyMGklMjBub3QlMjBpbiUyMCU1QiUyMmluc3RydWN0aW9uJTIyJTJDJTIwJTIyb3V0cHV0JTIyJTVEJTVEJTBBZGF0YXNldCUyMCUzRCUyMGRhdGFzZXQucmVtb3ZlX2NvbHVtbnMob3RoZXJfY29sdW1ucyklMEFzcGxpdF9kYXRhc2V0JTIwJTNEJTIwZGF0YXNldC50cmFpbl90ZXN0X3NwbGl0KCUwQSUyMCUyMCUyMCUyMHRyYWluX3NpemUlM0RpbnQoZGF0YXNldC5udW1fcm93cyUyMColMjBwZXJjZW50X29mX3RyYWluX2RhdGFzZXQpJTJDJTIwc2VlZCUzRDE5JTJDJTIwc2h1ZmZsZSUzREZhbHNlJTBBKSUwQXRyYWluX2RhdGFzZXQlMjAlM0QlMjBzcGxpdF9kYXRhc2V0JTVCJTIydHJhaW4lMjIlNUQlMEFldmFsX2RhdGFzZXQlMjAlM0QlMjBzcGxpdF9kYXRhc2V0JTVCJTIydGVzdCUyMiU1RCUwQXByaW50KGYlMjJTaXplJTIwb2YlMjB0aGUlMjB0cmFpbiUyMHNldCUzQSUyMCU3Qmxlbih0cmFpbl9kYXRhc2V0KSU3RC4lMjBTaXplJTIwb2YlMjB0aGUlMjB2YWxpZGF0aW9uJTIwc2V0JTNBJTIwJTdCbGVuKGV2YWxfZGF0YXNldCklN0QlMjIp",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">&quot;instruction&quot;</span>, <span class="hljs-string">&quot;output&quot;</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}}),k=new r({props:{code:"JTIzJTIwTG9hZCUyMExvUkElMjBjb25maWd1cmF0aW9uJTBBcGVmdF9jb25maWclMjAlM0QlMjBMb3JhQ29uZmlnKCUwQSUyMCUyMCUyMCUyMHIlM0Rsb3JhX3IlMkMlMEElMjAlMjAlMjAlMjBsb3JhX2FscGhhJTNEbG9yYV9hbHBoYSUyQyUwQSUyMCUyMCUyMCUyMGxvcmFfZHJvcG91dCUzRGxvcmFfZHJvcG91dCUyQyUwQSUyMCUyMCUyMCUyMGJpYXMlM0QlMjJub25lJTIyJTJDJTBBJTIwJTIwJTIwJTIwdGFza190eXBlJTNEJTIyQ0FVU0FMX0xNJTIyJTJDJTBBJTIwJTIwJTIwJTIwdGFyZ2V0X21vZHVsZXMlM0R0YXJnZXRfbW9kdWxlcyUyQyUwQSk=",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 r({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}}),v=new w({props:{title:"为什么我们有两种数据类型（quant_type 和 compute_type）？",local:"为什么我们有两种数据类型quanttype-和-computetype",headingTag:"h3"}}),S=new r({props:{code:"JTIzJTIwTG9hZCUyMGJhc2UlMjBtb2RlbCUwQW1vZGVsJTIwJTNEJTIwQXV0b01vZGVsRm9yQ2F1c2FsTE0uZnJvbV9wcmV0cmFpbmVkKG1vZGVsX25hbWUlMkMlMjBxdWFudGl6YXRpb25fY29uZmlnJTNEYm5iX2NvbmZpZyUyQyUyMGRldmljZV9tYXAlM0RkZXZpY2VfbWFwKSUwQW1vZGVsLmNvbmZpZy51c2VfY2FjaGUlMjAlM0QlMjBGYWxzZQ==",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}}),x=new r({props:{code:"JTIzJTIwU2V0JTIwdHJhaW5pbmclMjBwYXJhbWV0ZXJzJTBBdHJhaW5pbmdfYXJndW1lbnRzJTIwJTNEJTIwVHJhaW5pbmdBcmd1bWVudHMoJTBBJTIwJTIwJTIwJTIwb3V0cHV0X2RpciUzRG5ld19tb2RlbCUyQyUwQSUyMCUyMCUyMCUyMG51bV90cmFpbl9lcG9jaHMlM0RudW1fdHJhaW5fZXBvY2hzJTJDJTBBJTIwJTIwJTIwJTIwcGVyX2RldmljZV90cmFpbl9iYXRjaF9zaXplJTNEcGVyX2RldmljZV90cmFpbl9iYXRjaF9zaXplJTJDJTBBJTIwJTIwJTIwJTIwZ3JhZGllbnRfYWNjdW11bGF0aW9uX3N0ZXBzJTNEZ3JhZGllbnRfYWNjdW11bGF0aW9uX3N0ZXBzJTJDJTBBJTIwJTIwJTIwJTIwb3B0aW0lM0RvcHRpbSUyQyUwQSUyMCUyMCUyMCUyMHNhdmVfc3RlcHMlM0RzYXZlX3N0ZXBzJTJDJTBBJTIwJTIwJTIwJTIwbG9nZ2luZ19zdGVwcyUzRGxvZ2dpbmdfc3RlcHMlMkMlMEElMjAlMjAlMjAlMjBsZWFybmluZ19yYXRlJTNEbGVhcm5pbmdfcmF0ZSUyQyUwQSUyMCUyMCUyMCUyMHdlaWdodF9kZWNheSUzRHdlaWdodF9kZWNheSUyQyUwQSUyMCUyMCUyMCUyMGZwMTYlM0RmcDE2JTJDJTBBJTIwJTIwJTIwJTIwYmYxNiUzRGJmMTYlMkMlMEElMjAlMjAlMjAlMjBtYXhfc3RlcHMlM0RtYXhfc3RlcHMlMkMlMEElMjAlMjAlMjAlMjB3YXJtdXBfcmF0aW8lM0R3YXJtdXBfcmF0aW8lMkMlMEElMjAlMjAlMjAlMjBncmFkaWVudF9jaGVja3BvaW50aW5nJTNEZ3JhZGllbnRfY2hlY2twb2ludGluZyUyQyUwQSUyMCUyMCUyMCUyMGdyb3VwX2J5X2xlbmd0aCUzRGdyb3VwX2J5X2xlbmd0aCUyQyUwQSUyMCUyMCUyMCUyMGxyX3NjaGVkdWxlcl90eXBlJTNEbHJfc2NoZWR1bGVyX3R5cGUlMkMlMEEp",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 r({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 r({props:{code:"ZGVmJTIwc3BlY2lhbF9mb3JtYXR0aW5nX3Byb21wdHMoZXhhbXBsZSklM0ElMEElMjAlMjAlMjAlMjBvdXRwdXRfdGV4dHMlMjAlM0QlMjAlNUIlNUQlMEElMjAlMjAlMjAlMjBmb3IlMjBpJTIwaW4lMjByYW5nZShsZW4oZXhhbXBsZSU1QiUyMmluc3RydWN0aW9uJTIyJTVEKSklM0ElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjB0ZXh0JTIwJTNEJTIwZiUyMiU3Qmluc3RydWN0aW9uX3Byb21wdF90ZW1wbGF0ZSU3RCU3QmV4YW1wbGUlNUInaW5zdHJ1Y3Rpb24nJTVEJTVCaSU1RCU3RCU1Q24lN0JyZXNwb25zZV90ZW1wbGF0ZSU3RCUyMCU3QmV4YW1wbGUlNUInb3V0cHV0JyU1RCU1QmklNUQlN0QlMjIlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBvdXRwdXRfdGV4dHMuYXBwZW5kKHRleHQpJTBBJTIwJTIwJTIwJTIwcmV0dXJuJTIwb3V0cHV0X3RleHRzJTBBJTBBJTBBZGVmJTIwbm9ybWFsX2Zvcm1hdHRpbmdfcHJvbXB0cyhleGFtcGxlKSUzQSUwQSUyMCUyMCUyMCUyMG91dHB1dF90ZXh0cyUyMCUzRCUyMCU1QiU1RCUwQSUyMCUyMCUyMCUyMGZvciUyMGklMjBpbiUyMHJhbmdlKGxlbihleGFtcGxlJTVCJTIyaW5zdHJ1Y3Rpb24lMjIlNUQpKSUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMGNoYXRfdGVtcCUyMCUzRCUyMCU1QiUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCU3QiUyMnJvbGUlMjIlM0ElMjAlMjJzeXN0ZW0lMjIlMkMlMjAlMjJjb250ZW50JTIyJTNBJTIwZXhhbXBsZSU1QiUyMmluc3RydWN0aW9uJTIyJTVEJTVCaSU1RCU3RCUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMCU3QiUyMnJvbGUlMjIlM0ElMjAlMjJhc3Npc3RhbnQlMjIlMkMlMjAlMjJjb250ZW50JTIyJTNBJTIwZXhhbXBsZSU1QiUyMm91dHB1dCUyMiU1RCU1QmklNUQlN0QlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlNUQlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjB0ZXh0JTIwJTNEJTIwdG9rZW5pemVyLmFwcGx5X2NoYXRfdGVtcGxhdGUoY2hhdF90ZW1wJTJDJTIwdG9rZW5pemUlM0RGYWxzZSklMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBvdXRwdXRfdGV4dHMuYXBwZW5kKHRleHQpJTBBJTIwJTIwJTIwJTIwcmV0dXJuJTIwb3V0cHV0X3RleHRz",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">&quot;instruction&quot;</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">&quot;instruction&quot;</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">&quot;instruction&quot;</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">&quot;output&quot;</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}}),K=new r({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}}),P=new r({props:{code:"dHJhaW5lciUyMCUzRCUyMFNGVFRyYWluZXIoJTBBJTIwJTIwJTIwJTIwbW9kZWwlM0Rtb2RlbCUyQyUwQSUyMCUyMCUyMCUyMHRyYWluX2RhdGFzZXQlM0R0cmFpbl9kYXRhc2V0JTJDJTBBJTIwJTIwJTIwJTIwZXZhbF9kYXRhc2V0JTNEZXZhbF9kYXRhc2V0JTJDJTBBJTIwJTIwJTIwJTIwcGVmdF9jb25maWclM0RwZWZ0X2NvbmZpZyUyQyUwQSUyMCUyMCUyMCUyMGZvcm1hdHRpbmdfZnVuYyUzRGZvcm1hdHRpbmdfZnVuYyUyQyUwQSUyMCUyMCUyMCUyMGRhdGFfY29sbGF0b3IlM0Rjb2xsYXRvciUyQyUwQSUyMCUyMCUyMCUyMG1heF9zZXFfbGVuZ3RoJTNEbWF4X3NlcV9sZW5ndGglMkMlMEElMjAlMjAlMjAlMjBwcm9jZXNzaW5nX2NsYXNzJTNEdG9rZW5pemVyJTJDJTBBJTIwJTIwJTIwJTIwYXJncyUzRHRyYWluaW5nX2FyZ3VtZW50cyUyQyUwQSUyMCUyMCUyMCUyMHBhY2tpbmclM0RwYWNraW5nJTJDJTBBKQ==",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}}),sl=new r({props:{code:"JTIzJTIwVHJhaW4lMjBtb2RlbCUwQXRyYWluZXIudHJhaW4oKSUwQSUwQSUyMyUyMFNhdmUlMjBmaW5lJTIwdHVuZWQlMjBMb3JhJTIwQWRhcHRvciUwQXRyYWluZXIubW9kZWwuc2F2ZV9wcmV0cmFpbmVkKG5ld19tb2RlbCk=",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}}),el=new w({props:{title:"推理",local:"推理",headingTag:"h2"}}),al=new r({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}}),nl=new r({props:{code:"ZGVmJTIwZ2VuZXJhdGUobW9kZWwlMkMlMjBwcm9tcHQlM0ElMjBzdHIlMkMlMjBrd2FyZ3MpJTNBJTBBJTIwJTIwJTIwJTIwdG9rZW5pemVkX3Byb21wdCUyMCUzRCUyMHRva2VuaXplcihwcm9tcHQlMkMlMjByZXR1cm5fdGVuc29ycyUzRCUyMnB0JTIyKS50byhtb2RlbC5kZXZpY2UpJTBBJTBBJTIwJTIwJTIwJTIwcHJvbXB0X2xlbmd0aCUyMCUzRCUyMGxlbih0b2tlbml6ZWRfcHJvbXB0LmdldCglMjJpbnB1dF9pZHMlMjIpJTVCMCU1RCklMEElMEElMjAlMjAlMjAlMjB3aXRoJTIwdG9yY2guY3VkYS5hbXAuYXV0b2Nhc3QoKSUzQSUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMG91dHB1dF90b2tlbnMlMjAlM0QlMjBtb2RlbC5nZW5lcmF0ZSgqKnRva2VuaXplZF9wcm9tcHQlMkMlMjAqKmt3YXJncyklMjBpZiUyMGt3YXJncyUyMGVsc2UlMjBtb2RlbC5nZW5lcmF0ZSgqKnRva2VuaXplZF9wcm9tcHQpJTBBJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwb3V0cHV0JTIwJTNEJTIwdG9rZW5pemVyLmRlY29kZShvdXRwdXRfdG9rZW5zJTVCMCU1RCU1QnByb21wdF9sZW5ndGglM0ElNUQlMkMlMjBza2lwX3NwZWNpYWxfdG9rZW5zJTNEVHJ1ZSklMEElMEElMjAlMjAlMjAlMjByZXR1cm4lMjBvdXRwdXQ=",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">&quot;pt&quot;</span>).to(model.device)

    prompt_length = <span class="hljs-built_in">len</span>(tokenized_prompt.get(<span class="hljs-string">&quot;input_ids&quot;</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}}),pl=new r({props:{code:"YmFzZV9tb2RlbCUyMCUzRCUyMEF1dG9Nb2RlbEZvckNhdXNhbExNLmZyb21fcHJldHJhaW5lZChuZXdfbW9kZWwlMkMlMjByZXR1cm5fZGljdCUzRFRydWUlMkMlMjBkZXZpY2VfbWFwJTNEJTIyYXV0byUyMiUyQyUyMHRva2VuJTNEJTIyJTIyKSUwQXRva2VuaXplciUyMCUzRCUyMEF1dG9Ub2tlbml6ZXIuZnJvbV9wcmV0cmFpbmVkKG5ld19tb2RlbCUyQyUyMG1heF9sZW5ndGglM0RtYXhfc2VxX2xlbmd0aCklMEFtb2RlbCUyMCUzRCUyMFBlZnRNb2RlbC5mcm9tX3ByZXRyYWluZWQoYmFzZV9tb2RlbCUyQyUyMG5ld19tb2RlbCklMEFkZWwlMjBiYXNlX21vZGVs",highlighted:`base_model = AutoModelForCausalLM.from_pretrained(new_model, return_dict=<span class="hljs-literal">True</span>, device_map=<span class="hljs-string">&quot;auto&quot;</span>, token=<span class="hljs-string">&quot;&quot;</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}}),Ml=new r({props:{code:"c2FtcGxlJTIwJTNEJTIwZXZhbF9kYXRhc2V0JTVCMCU1RCUwQWlmJTIwdXNlX3NwZWNpYWxfdGVtcGxhdGUlM0ElMEElMjAlMjAlMjAlMjBwcm9tcHQlMjAlM0QlMjBmJTIyJTdCaW5zdHJ1Y3Rpb25fcHJvbXB0X3RlbXBsYXRlJTdEJTdCc2FtcGxlJTVCJ2luc3RydWN0aW9uJyU1RCU3RCU1Q24lN0JyZXNwb25zZV90ZW1wbGF0ZSU3RCUyMiUwQWVsc2UlM0ElMEElMjAlMjAlMjAlMjBjaGF0X3RlbXAlMjAlM0QlMjAlNUIlN0IlMjJyb2xlJTIyJTNBJTIwJTIyc3lzdGVtJTIyJTJDJTIwJTIyY29udGVudCUyMiUzQSUyMHNhbXBsZSU1QiUyMmluc3RydWN0aW9uJTIyJTVEJTdEJTVEJTBBJTIwJTIwJTIwJTIwcHJvbXB0JTIwJTNEJTIwdG9rZW5pemVyLmFwcGx5X2NoYXRfdGVtcGxhdGUoY2hhdF90ZW1wJTJDJTIwdG9rZW5pemUlM0RGYWxzZSUyQyUyMGFkZF9nZW5lcmF0aW9uX3Byb21wdCUzRFRydWUp",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">&quot;instruction&quot;</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}}),ol=new r({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}}),Jl=new w({props:{title:"合并基础模型",local:"合并基础模型",headingTag:"h2"}}),ml=new r({props:{code:"Y2xlYXJfaGFyZHdhcmVzKCklMEFtZXJnZWRfbW9kZWwlMjAlM0QlMjBtb2RlbC5tZXJnZV9hbmRfdW5sb2FkKCklMEFjbGVhcl9oYXJkd2FyZXMoKSUwQWRlbCUyMG1vZGVsJTBBYWRhcHRlcl9tb2RlbF9uYW1lJTIwJTNEJTIwJTIyeW91cl9oZl9hY2NvdW50JTJGeW91cl9kZXNpcmVkX25hbWUlMjIlMEFtZXJnZWRfbW9kZWwucHVzaF90b19odWIoYWRhcHRlcl9tb2RlbF9uYW1lKQ==",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">&quot;your_hf_account/your_desired_name&quot;</span>
merged_model.push_to_hub(adapter_model_name)`,wrap:!1}}),cl=new r({props:{code:"bW9kZWwucHVzaF90b19odWIoYWRhcHRlcl9tb2RlbF9uYW1lKQ==",highlighted:"model.push_to_hub(adapter_model_name)",wrap:!1}}),yl=new r({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}}),Tl=new w({props:{title:"用 Vllm 快速推理",local:"用-vllm-快速推理",headingTag:"h2"}}),ul=new r({props:{code:"ZnJvbSUyMHZsbG0lMjBpbXBvcnQlMjBMTE0lMkMlMjBTYW1wbGluZ1BhcmFtcyUwQSUwQXByb21wdCUyMCUzRCUyMCUyMiUyMiUyMiUyMyUyMyUyMyUyMFF1ZXN0aW9uJTNBJTIwaGVyZSUyMGlzJTIwYSUyMHByb2R1Y3QlMjB0aXRsZSUyMGZyb20lMjBhJTIwSXJhbmlhbiUyMG1hcmtldHBsYWNlLiUyMCUyMCU1Q24lMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBnaXZlJTIwbWUlMjB0aGUlMjBQcm9kdWN0JTIwRW50aXR5JTIwYW5kJTIwQXR0cmlidXRlcyUyMG9mJTIwdGhpcyUyMHByb2R1Y3QlMjBpbiUyMFBlcnNpYW4lMjBsYW5ndWFnZS4lNUNuJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwZ2l2ZSUyMHRoZSUyMG91dHB1dCUyMGluJTIwdGhpcyUyMGpzb24lMjBmb3JtYXQlM0ElMjAlN0InYXR0cmlidXRlcyclM0ElMjAlN0InYXR0cmlidXRlX25hbWUnJTIwJTNBJTIwJTNDYXR0cmlidXRlJTIwdmFsdWUlM0UlMkMlMjAuLi4lN0QlMkMlMjAncHJvZHVjdF9lbnRpdHknJTNBJTIwJyUzQ3Byb2R1Y3QlMjBlbnRpdHklM0UnJTdELiU1Q24lMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjBEb24ndCUyMG1ha2UlMjBhc3N1bXB0aW9ucyUyMGFib3V0JTIwd2hhdCUyMHZhbHVlcyUyMHRvJTIwcGx1ZyUyMGludG8lMjBqc29uLiUyMEp1c3QlMjBnaXZlJTIwSnNvbiUyMG5vdCUyMGElMjBzaW5nbGUlMjB3b3JkJTIwbW9yZS4lNUNuJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTIwJTVDbnByb2R1Y3QlMjB0aXRsZSUzQSUyMiUyMiUyMiUwQXVzZXJfcHJvbXB0X3RlbXBsYXRlJTIwJTNEJTIwJTIyJTIzJTIzJTIzJTIwUXVlc3Rpb24lM0ElMjAlMjIlMEFyZXNwb25zZV90ZW1wbGF0ZSUyMCUzRCUyMCUyMiUyMCUyMyUyMyUyMyUyMEFuc3dlciUzQSUyMiUwQSUwQWxsbSUyMCUzRCUyMExMTShtb2RlbCUzRCUyMkJhU2FsYW0lMkZMbGFtYTItN2ItZW50aXR5LWF0dHItdjElMjIlMkMlMjBncHVfbWVtb3J5X3V0aWxpemF0aW9uJTNEMC45JTJDJTIwdHJ1c3RfcmVtb3RlX2NvZGUlM0RUcnVlKSUwQSUwQXByb2R1Y3QlMjAlM0QlMjAlMjIlRDklODUlRDglQTclRDklODYlRDglQUElRDklODglMjAlRDglQTclRDglQjMlRDklQkUlRDglQjElRDglQUElMjAlRDklQkUlRDglQTclRDklODYlREIlOEMlRDglQjAlMjAlRDklODIlRDglQUYlMjAlRDglQUMlRDklODQlRDklODglREIlOEMlMjAlREElQTklRDglQTclRDglQjElMjAlRDglQUQlRDglQUYlRDklODglRDglQUYlRDglQTclMjA4NSUyMCVEOCVCMyVEOCVBNyVEOSU4NiVEOCVBQSVEQiU4QyUyMCVEOSU4NSVEOCVBQSVEOCVCMSUyMCVEOSU4MiVEOCVBRiUyMCVEOSVCRSVEOCVCNCVEOCVBQSUyMCVEQSVBOSVEOCVBNyVEOCVCMSUyMCVEOCVBRCVEOCVBRiVEOSU4OCVEOCVBRiVEOCVBNyUyMDg4JTIwJUQ4JUIzJUQ4JUE3JUQ5JTg2JUQ4JUFBJURCJThDJTIwJUQ5JTg1JUQ4JUFBJUQ4JUIxJTIyJTBBc2FtcGxpbmdfcGFyYW1zJTIwJTNEJTIwU2FtcGxpbmdQYXJhbXModGVtcGVyYXR1cmUlM0QwLjAlMkMlMjBtYXhfdG9rZW5zJTNENzUpJTBBcHJvbXB0JTIwJTNEJTIwZiUyMiU3QnVzZXJfcHJvbXB0X3RlbXBsYXRlJTdEJTIwJTdCcHJvbXB0JTdEJTdCcHJvZHVjdCU3RCU1Q24lMjAlN0JyZXNwb25zZV90ZW1wbGF0ZSU3RCUyMiUwQW91dHB1dHMlMjAlM0QlMjBsbG0uZ2VuZXJhdGUocHJvbXB0JTJDJTIwc2FtcGxpbmdfcGFyYW1zKSUwQSUwQXByaW50KG91dHB1dHMlNUIwJTVELm91dHB1dHMlNUIwJTVELnRleHQp",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">&quot;### Question: &quot;</span>
response_template = <span class="hljs-string">&quot; ### Answer:&quot;</span>

llm = LLM(model=<span class="hljs-string">&quot;BaSalam/Llama2-7b-entity-attr-v1&quot;</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">&quot;مانتو اسپرت پانیذ قد جلوی کار حدودا 85 سانتی متر قد پشت کار حدودا 88 سانتی متر&quot;</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&quot;<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>&quot;</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}}),bl=new w({props:{title:"样例输出",local:"样例输出",headingTag:"h3"}}),jl=new r({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}}),fl=new ws({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"),Il=a(),Cl=c("p"),_l=a(),p(d.$$.fragment),Zl=a(),p(u.$$.fragment),gl=a(),b=c("p"),b.innerHTML=Ft,Xl=a(),j=c("p"),j.innerHTML=kt,Bl=a(),h=c("p"),h.innerHTML=Nt,Vl=a(),f=c("p"),f.textContent=$t,Gl=a(),p(C.$$.fragment),Ql=a(),R=c("p"),R.innerHTML=Et,Wl=a(),p(I.$$.fragment),Fl=a(),p(_.$$.fragment),kl=a(),p(Z.$$.fragment),Nl=a(),g=c("p"),g.textContent=Yt,$l=a(),X=c("p"),X.innerHTML=At,El=a(),p(B.$$.fragment),Yl=a(),V=c("p"),V.textContent=zt,Al=a(),G=c("p"),G.innerHTML=vt,zl=a(),p(Q.$$.fragment),vl=a(),p(W.$$.fragment),Hl=a(),p(F.$$.fragment),Sl=a(),p(k.$$.fragment),xl=a(),N=c("p"),N.innerHTML=Ht,ql=a(),$=c("ul"),$.innerHTML=St,Ll=a(),E=c("p"),E.innerHTML=xt,Dl=a(),p(Y.$$.fragment),Ol=a(),A=c("p"),A.innerHTML=qt,Kl=a(),z=c("ul"),z.innerHTML=Lt,Pl=a(),p(v.$$.fragment),lt=a(),H=c("p"),H.textContent=Dt,tt=a(),p(S.$$.fragment),st=a(),p(x.$$.fragment),et=a(),p(q.$$.fragment),at=a(),L=c("p"),L.textContent=Ot,nt=a(),D=c("p"),D.innerHTML=Kt,pt=a(),p(O.$$.fragment),Mt=a(),p(K.$$.fragment),ot=a(),p(P.$$.fragment),Jt=a(),ll=c("p"),ll.innerHTML=Pt,mt=a(),tl=c("p"),tl.innerHTML=ls,it=a(),p(sl.$$.fragment),ct=a(),p(el.$$.fragment),Ut=a(),p(al.$$.fragment),yt=a(),p(nl.$$.fragment),rt=a(),p(pl.$$.fragment),Tt=a(),p(Ml.$$.fragment),wt=a(),p(ol.$$.fragment),dt=a(),p(Jl.$$.fragment),ut=a(),p(ml.$$.fragment),bt=a(),il=c("p"),il.textContent=ts,jt=a(),p(cl.$$.fragment),ht=a(),Ul=c("p"),Ul.textContent=ss,ft=a(),p(yl.$$.fragment),Ct=a(),rl=c("p"),rl.textContent=es,Rt=a(),p(Tl.$$.fragment),It=a(),wl=c("p"),wl.innerHTML=as,_t=a(),dl=c("p"),dl.textContent=ns,Zt=a(),p(ul.$$.fragment),gt=a(),p(bl.$$.fragment),Xt=a(),p(jl.$$.fragment),Bt=a(),hl=c("p"),hl.innerHTML=ps,Vt=a(),p(fl.$$.fragment),Gt=a(),Rl=c("p"),this.h()},l(l){const t=ys("svelte-u9bgzb",document.head);T=U(t,"META",{name:!0,content:!0}),t.forEach(s),Il=n(l),Cl=U(l,"P",{}),Ms(Cl).forEach(s),_l=n(l),M(d.$$.fragment,l),Zl=n(l),M(u.$$.fragment,l),gl=n(l),b=U(l,"P",{"data-svelte-h":!0}),y(b)!=="svelte-8a0kwr"&&(b.innerHTML=Ft),Xl=n(l),j=U(l,"P",{"data-svelte-h":!0}),y(j)!=="svelte-ytjka3"&&(j.innerHTML=kt),Bl=n(l),h=U(l,"P",{"data-svelte-h":!0}),y(h)!=="svelte-18zof4y"&&(h.innerHTML=Nt),Vl=n(l),f=U(l,"P",{"data-svelte-h":!0}),y(f)!=="svelte-izpy37"&&(f.textContent=$t),Gl=n(l),M(C.$$.fragment,l),Ql=n(l),R=U(l,"P",{"data-svelte-h":!0}),y(R)!=="svelte-v8byij"&&(R.innerHTML=Et),Wl=n(l),M(I.$$.fragment,l),Fl=n(l),M(_.$$.fragment,l),kl=n(l),M(Z.$$.fragment,l),Nl=n(l),g=U(l,"P",{"data-svelte-h":!0}),y(g)!=="svelte-17g0396"&&(g.textContent=Yt),$l=n(l),X=U(l,"P",{"data-svelte-h":!0}),y(X)!=="svelte-lbe80f"&&(X.innerHTML=At),El=n(l),M(B.$$.fragment,l),Yl=n(l),V=U(l,"P",{"data-svelte-h":!0}),y(V)!=="svelte-1rjxcef"&&(V.textContent=zt),Al=n(l),G=U(l,"P",{"data-svelte-h":!0}),y(G)!=="svelte-1c1a47q"&&(G.innerHTML=vt),zl=n(l),M(Q.$$.fragment,l),vl=n(l),M(W.$$.fragment,l),Hl=n(l),M(F.$$.fragment,l),Sl=n(l),M(k.$$.fragment,l),xl=n(l),N=U(l,"P",{"data-svelte-h":!0}),y(N)!=="svelte-18wq4yr"&&(N.innerHTML=Ht),ql=n(l),$=U(l,"UL",{"data-svelte-h":!0}),y($)!=="svelte-df25sr"&&($.innerHTML=St),Ll=n(l),E=U(l,"P",{"data-svelte-h":!0}),y(E)!=="svelte-2de196"&&(E.innerHTML=xt),Dl=n(l),M(Y.$$.fragment,l),Ol=n(l),A=U(l,"P",{"data-svelte-h":!0}),y(A)!=="svelte-1cw7yvz"&&(A.innerHTML=qt),Kl=n(l),z=U(l,"UL",{"data-svelte-h":!0}),y(z)!=="svelte-p1afkq"&&(z.innerHTML=Lt),Pl=n(l),M(v.$$.fragment,l),lt=n(l),H=U(l,"P",{"data-svelte-h":!0}),y(H)!=="svelte-103x3r6"&&(H.textContent=Dt),tt=n(l),M(S.$$.fragment,l),st=n(l),M(x.$$.fragment,l),et=n(l),M(q.$$.fragment,l),at=n(l),L=U(l,"P",{"data-svelte-h":!0}),y(L)!=="svelte-vxdgqe"&&(L.textContent=Ot),nt=n(l),D=U(l,"P",{"data-svelte-h":!0}),y(D)!=="svelte-1ncr8o1"&&(D.innerHTML=Kt),pt=n(l),M(O.$$.fragment,l),Mt=n(l),M(K.$$.fragment,l),ot=n(l),M(P.$$.fragment,l),Jt=n(l),ll=U(l,"P",{"data-svelte-h":!0}),y(ll)!=="svelte-y0ejnv"&&(ll.innerHTML=Pt),mt=n(l),tl=U(l,"P",{"data-svelte-h":!0}),y(tl)!=="svelte-n6jo8f"&&(tl.innerHTML=ls),it=n(l),M(sl.$$.fragment,l),ct=n(l),M(el.$$.fragment,l),Ut=n(l),M(al.$$.fragment,l),yt=n(l),M(nl.$$.fragment,l),rt=n(l),M(pl.$$.fragment,l),Tt=n(l),M(Ml.$$.fragment,l),wt=n(l),M(ol.$$.fragment,l),dt=n(l),M(Jl.$$.fragment,l),ut=n(l),M(ml.$$.fragment,l),bt=n(l),il=U(l,"P",{"data-svelte-h":!0}),y(il)!=="svelte-ro7ka9"&&(il.textContent=ts),jt=n(l),M(cl.$$.fragment,l),ht=n(l),Ul=U(l,"P",{"data-svelte-h":!0}),y(Ul)!=="svelte-xuk7qr"&&(Ul.textContent=ss),ft=n(l),M(yl.$$.fragment,l),Ct=n(l),rl=U(l,"P",{"data-svelte-h":!0}),y(rl)!=="svelte-1x8pg25"&&(rl.textContent=es),Rt=n(l),M(Tl.$$.fragment,l),It=n(l),wl=U(l,"P",{"data-svelte-h":!0}),y(wl)!=="svelte-efl6cb"&&(wl.innerHTML=as),_t=n(l),dl=U(l,"P",{"data-svelte-h":!0}),y(dl)!=="svelte-19bm9ra"&&(dl.textContent=ns),Zt=n(l),M(ul.$$.fragment,l),gt=n(l),M(bl.$$.fragment,l),Xt=n(l),M(jl.$$.fragment,l),Bt=n(l),hl=U(l,"P",{"data-svelte-h":!0}),y(hl)!=="svelte-1nce2nx"&&(hl.innerHTML=ps),Vt=n(l),M(fl.$$.fragment,l),Gt=n(l),Rl=U(l,"P",{}),Ms(Rl).forEach(s),this.h()},h(){os(T,"name","hf:doc:metadata"),os(T,"content",us)},m(l,t){rs(document.head,T),e(l,Il,t),e(l,Cl,t),e(l,_l,t),o(d,l,t),e(l,Zl,t),o(u,l,t),e(l,gl,t),e(l,b,t),e(l,Xl,t),e(l,j,t),e(l,Bl,t),e(l,h,t),e(l,Vl,t),e(l,f,t),e(l,Gl,t),o(C,l,t),e(l,Ql,t),e(l,R,t),e(l,Wl,t),o(I,l,t),e(l,Fl,t),o(_,l,t),e(l,kl,t),o(Z,l,t),e(l,Nl,t),e(l,g,t),e(l,$l,t),e(l,X,t),e(l,El,t),o(B,l,t),e(l,Yl,t),e(l,V,t),e(l,Al,t),e(l,G,t),e(l,zl,t),o(Q,l,t),e(l,vl,t),o(W,l,t),e(l,Hl,t),o(F,l,t),e(l,Sl,t),o(k,l,t),e(l,xl,t),e(l,N,t),e(l,ql,t),e(l,$,t),e(l,Ll,t),e(l,E,t),e(l,Dl,t),o(Y,l,t),e(l,Ol,t),e(l,A,t),e(l,Kl,t),e(l,z,t),e(l,Pl,t),o(v,l,t),e(l,lt,t),e(l,H,t),e(l,tt,t),o(S,l,t),e(l,st,t),o(x,l,t),e(l,et,t),o(q,l,t),e(l,at,t),e(l,L,t),e(l,nt,t),e(l,D,t),e(l,pt,t),o(O,l,t),e(l,Mt,t),o(K,l,t),e(l,ot,t),o(P,l,t),e(l,Jt,t),e(l,ll,t),e(l,mt,t),e(l,tl,t),e(l,it,t),o(sl,l,t),e(l,ct,t),o(el,l,t),e(l,Ut,t),o(al,l,t),e(l,yt,t),o(nl,l,t),e(l,rt,t),o(pl,l,t),e(l,Tt,t),o(Ml,l,t),e(l,wt,t),o(ol,l,t),e(l,dt,t),o(Jl,l,t),e(l,ut,t),o(ml,l,t),e(l,bt,t),e(l,il,t),e(l,jt,t),o(cl,l,t),e(l,ht,t),e(l,Ul,t),e(l,ft,t),o(yl,l,t),e(l,Ct,t),e(l,rl,t),e(l,Rt,t),o(Tl,l,t),e(l,It,t),e(l,wl,t),e(l,_t,t),e(l,dl,t),e(l,Zt,t),o(ul,l,t),e(l,gt,t),o(bl,l,t),e(l,Xt,t),o(jl,l,t),e(l,Bt,t),e(l,hl,t),e(l,Vt,t),o(fl,l,t),e(l,Gt,t),e(l,Rl,t),Qt=!0},p:ms,i(l){Qt||(J(d.$$.fragment,l),J(u.$$.fragment,l),J(C.$$.fragment,l),J(I.$$.fragment,l),J(_.$$.fragment,l),J(Z.$$.fragment,l),J(B.$$.fragment,l),J(Q.$$.fragment,l),J(W.$$.fragment,l),J(F.$$.fragment,l),J(k.$$.fragment,l),J(Y.$$.fragment,l),J(v.$$.fragment,l),J(S.$$.fragment,l),J(x.$$.fragment,l),J(q.$$.fragment,l),J(O.$$.fragment,l),J(K.$$.fragment,l),J(P.$$.fragment,l),J(sl.$$.fragment,l),J(el.$$.fragment,l),J(al.$$.fragment,l),J(nl.$$.fragment,l),J(pl.$$.fragment,l),J(Ml.$$.fragment,l),J(ol.$$.fragment,l),J(Jl.$$.fragment,l),J(ml.$$.fragment,l),J(cl.$$.fragment,l),J(yl.$$.fragment,l),J(Tl.$$.fragment,l),J(ul.$$.fragment,l),J(bl.$$.fragment,l),J(jl.$$.fragment,l),J(fl.$$.fragment,l),Qt=!0)},o(l){m(d.$$.fragment,l),m(u.$$.fragment,l),m(C.$$.fragment,l),m(I.$$.fragment,l),m(_.$$.fragment,l),m(Z.$$.fragment,l),m(B.$$.fragment,l),m(Q.$$.fragment,l),m(W.$$.fragment,l),m(F.$$.fragment,l),m(k.$$.fragment,l),m(Y.$$.fragment,l),m(v.$$.fragment,l),m(S.$$.fragment,l),m(x.$$.fragment,l),m(q.$$.fragment,l),m(O.$$.fragment,l),m(K.$$.fragment,l),m(P.$$.fragment,l),m(sl.$$.fragment,l),m(el.$$.fragment,l),m(al.$$.fragment,l),m(nl.$$.fragment,l),m(pl.$$.fragment,l),m(Ml.$$.fragment,l),m(ol.$$.fragment,l),m(Jl.$$.fragment,l),m(ml.$$.fragment,l),m(cl.$$.fragment,l),m(yl.$$.fragment,l),m(Tl.$$.fragment,l),m(ul.$$.fragment,l),m(bl.$$.fragment,l),m(jl.$$.fragment,l),m(fl.$$.fragment,l),Qt=!1},d(l){l&&(s(Il),s(Cl),s(_l),s(Zl),s(gl),s(b),s(Xl),s(j),s(Bl),s(h),s(Vl),s(f),s(Gl),s(Ql),s(R),s(Wl),s(Fl),s(kl),s(Nl),s(g),s($l),s(X),s(El),s(Yl),s(V),s(Al),s(G),s(zl),s(vl),s(Hl),s(Sl),s(xl),s(N),s(ql),s($),s(Ll),s(E),s(Dl),s(Ol),s(A),s(Kl),s(z),s(Pl),s(lt),s(H),s(tt),s(st),s(et),s(at),s(L),s(nt),s(D),s(pt),s(Mt),s(ot),s(Jt),s(ll),s(mt),s(tl),s(it),s(ct),s(Ut),s(yt),s(rt),s(Tt),s(wt),s(dt),s(ut),s(bt),s(il),s(jt),s(ht),s(Ul),s(ft),s(Ct),s(rl),s(Rt),s(It),s(wl),s(_t),s(dl),s(Zt),s(gt),s(Xt),s(Bt),s(hl),s(Vt),s(Gt),s(Rl)),s(T),i(d,l),i(u,l),i(C,l),i(I,l),i(_,l),i(Z,l),i(B,l),i(Q,l),i(W,l),i(F,l),i(k,l),i(Y,l),i(v,l),i(S,l),i(x,l),i(q,l),i(O,l),i(K,l),i(P,l),i(sl,l),i(el,l),i(al,l),i(nl,l),i(pl,l),i(Ml,l),i(ol,l),i(Jl,l),i(ml,l),i(cl,l),i(yl,l),i(Tl,l),i(ul,l),i(bl,l),i(jl,l),i(fl,l)}}}const us='{"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 bs(Wt){return is(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Rs extends cs{constructor(T){super(),Us(this,T,bs,ds,Js,{})}}export{Rs as component};
