>本教程提供了使用和微調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中文網其他相關文章!