>本教程提供了使用和微调Mistral 7b语言模型的自然语言处理任务的综合指南。 您将学会利用Kaggle进行模型访问,执行推理,应用量化技术,微调模型,合并适配器并部署到拥抱面枢纽。
>访问Mistral 7b>本节演示了从kaggle和执行推理加载模型。 基本图书馆更新对于防止错误至关重要:
使用BITSANDBYTES使用NF4配置进行4位量化量化量增强加载速度并降低内存使用情况:
<code>!pip install -q -U transformers !pip install -q -U accelerate !pip install -q -U bitsandbytes</code>
>将Mistral 7b模型添加到您的Kaggle笔记本中涉及以下步骤:
<code>from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig, pipeline import torch bnb_config = BitsAndBytesConfig( load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_use_double_quant=True, )</code>>单击右图中的“添加模型”。
>搜索“ Mistral 7b”,选择“ 7b-v0.1-hf”,然后添加。
库:
使用
>
transformers
<code>model_name = "/kaggle/input/mistral/pytorch/7b-v0.1-hf/1" tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForCausalLM.from_pretrained( model_name, load_in_4bit=True, quantization_config=bnb_config, torch_dtype=torch.bfloat16, device_map="auto", trust_remote_code=True, )</code>
pipeline
MISTRAL 7B微调
<code>pipe = pipeline( "text-generation", model=model, tokenizer = tokenizer, torch_dtype=torch.bfloat16, device_map="auto" )</code>本节通过使用PEFT,4位量化和Qlora之类的技术来指导您通过
>设置
<code>prompt = "As a data scientist, can you explain the concept of regularization in machine learning?" sequences = pipe( prompt, do_sample=True, max_new_tokens=100, temperature=0.7, top_k=50, top_p=0.95, num_return_sequences=1, ) print(sequences[0]['generated_text'])</code>安装了必要的库:
使用Kaggle Secrets安全管理API密钥:guanaco-llama2-1k
数据加载
<code>%%capture %pip install -U bitsandbytes %pip install -U transformers %pip install -U peft %pip install -U accelerate %pip install -U trl</code>
加载数据集并显示样本:
><code>from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig,HfArgumentParser,TrainingArguments,pipeline, logging from peft import LoraConfig, PeftModel, prepare_model_for_kbit_training, get_peft_model import os,torch, wandb from datasets import load_dataset from trl import SFTTrainer</code>
<code>from kaggle_secrets import UserSecretsClient user_secrets = UserSecretsClient() secret_hf = user_secrets.get_secret("HUGGINGFACE_TOKEN") secret_wandb = user_secrets.get_secret("wandb")</code>
>加载Mistral 7b
<code>!huggingface-cli login --token $secret_hf wandb.login(key = secret_wandb) run = wandb.init( project='Fine tuning mistral 7B', job_type="training", anonymous="allow" )</code>该模型装有4位精度:
<code>base_model = "/kaggle/input/mistral/pytorch/7b-v0.1-hf/1" dataset_name = "mlabonne/guanaco-llama2-1k" new_model = "mistral_7b_guanaco"</code>加载令牌
>
<code>dataset = load_dataset(dataset_name, split="train") dataset["text"][100]</code>添加适配器
添加了一个lora适配器以进行有效的微调:
>培训论点是定义的:
<code>bnb_config = BitsAndBytesConfig( load_in_4bit= True, bnb_4bit_quant_type= "nf4", bnb_4bit_compute_dtype= torch.bfloat16, bnb_4bit_use_double_quant= False, ) model = AutoModelForCausalLM.from_pretrained( base_model, load_in_4bit=True, quantization_config=bnb_config, torch_dtype=torch.bfloat16, device_map="auto", trust_remote_code=True, ) model.config.use_cache = False model.config.pretraining_tp = 1 model.gradient_checkpointing_enable()</code>
SFTTRAINER已配置并启动培训: 微调模型被保存并推到拥抱的脸上枢纽: 合并适配器 访问微型模型
<code>!pip install -q -U transformers
!pip install -q -U accelerate
!pip install -q -U bitsandbytes</code>
保存并推动模型
<code>from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig, pipeline
import torch
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_use_double_quant=True,
)</code>
模型评估
使用权重和偏见评估模型性能。提供了推理示例。
>适配器与基本模型合并,并将结果模型推向拥抱的脸。
合并的模型是通过拥抱的脸和推理加载的。
结论
以上是Mistral 7B教程:使用和微调Mistral 7b的分步指南的详细内容。更多信息请关注PHP中文网其他相关文章!