Tutorial ini menyediakan panduan yang komprehensif untuk menggunakan dan menyempurnakan model bahasa Mistral 7B untuk tugas pemprosesan bahasa semula jadi. Anda akan belajar untuk memanfaatkan Kaggle untuk akses model, melakukan kesimpulan, memohon teknik kuantisasi, menyempurnakan model, menggabungkan penyesuai, dan digunakan ke hab muka yang memeluk.
Mistral 7b boleh diakses melalui pelbagai platform termasuk muka memeluk, puncak AI, meniru, sagemaker jumpstart, dan baseten. Tutorial ini memberi tumpuan kepada menggunakan ciri "Model" Kaggle untuk akses yang diperkemas, menghapuskan keperluan untuk muat turun manual.
Bahagian ini menunjukkan memuatkan model dari kaggle dan melakukan kesimpulan. Kemas kini perpustakaan penting adalah penting untuk mengelakkan kesilapan:
<code>!pip install -q -U transformers !pip install -q -U accelerate !pip install -q -U bitsandbytes</code>
kuantisasi 4-bit dengan konfigurasi NF4 menggunakan Bitsandbytes meningkatkan kelajuan pemuatan dan mengurangkan penggunaan memori:
<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>
Menambah model Mistral 7B ke Notebook Kaggle anda melibatkan langkah -langkah ini:
: 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
<code>pipe = pipeline( "text-generation", model=model, tokenizer = tokenizer, torch_dtype=torch.bfloat16, device_map="auto" )</code>
<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>
dataset, menggunakan teknik seperti PEFT, kuantisasi 4-bit, dan qlora. Tutorial juga merujuk panduan mengenai Llama 2 penalaan untuk konteks selanjutnya. 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>
<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>
<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>
Memuatkan Mistral 7b
Memuatkan tokenizer
<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>
Menambah penyesuai
<code>tokenizer = AutoTokenizer.from_pretrained(base_model, trust_remote_code=True) tokenizer.padding_side = 'right' tokenizer.pad_token = tokenizer.eos_token tokenizer.add_eos_token = True tokenizer.add_bos_token, tokenizer.add_eos_token</code>
HyperParameters
<code>model = prepare_model_for_kbit_training(model) peft_config = LoraConfig( lora_alpha=16, lora_dropout=0.1, r=64, bias="none", task_type="CAUSAL_LM", target_modules=["q_proj", "k_proj", "v_proj", "o_proj","gate_proj"] ) model = get_peft_model(model, peft_config)</code>
latihan sft
<code>training_arguments = TrainingArguments( output_, num_train_epochs=1, per_device_train_batch_size=4, gradient_accumulation_steps=1, optim="paged_adamw_32bit", save_steps=25, logging_steps=25, learning_rate=2e-4, weight_decay=0.001, fp16=False, bf16=False, max_grad_norm=0.3, max_steps=-1, warmup_ratio=0.03, group_by_length=True, lr_scheduler_type="constant", report_to="wandb" )</code>
<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>
menggabungkan penyesuai
mengakses model yang disesuaikan dengan baik
Kesimpulan
Atas ialah kandungan terperinci Mistral 7b Tutorial: Panduan Langkah demi Langkah untuk Menggunakan dan Menyempurnakan Mistral 7b. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!