Dieses Tutorial bietet einen umfassenden Leitfaden zur Verwendung und Feinabstimmung des Mistral 7B-Sprachmodells für natürliche Sprachverarbeitungsaufgaben. Sie erfahren
Zugriff auf Mistral 7BDieser Abschnitt zeigt das Laden des Modells aus Kaggle und Durchführung von Inferenz. Es sind wichtige Bibliotheksaktualisierungen von entscheidender Bedeutung, um Fehler zu verhindern:
<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>
: 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>
vereinfacht: 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>
unter Verwendung von Techniken wie PEFT, 4-Bit-Quantisierung und Qlora. Das Tutorial bezieht sich auch auf einen Leitfaden zu Feinabstimmungslama 2 für einen weiteren Kontext. 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>
<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>
<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>
<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>
<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>
Das fein abgestimmte Modell wird gerettet und auf den umarmenden Gesichtszentrum gedrückt:
<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>
Modellleistung wird unter Verwendung von Gewichten und Verzerrungen bewertet. Inferenzbeispiele werden bereitgestellt.
Der Adapter wird mit dem Basismodell zusammengeführt, und das resultierende Modell wird zum Umarmungsgesicht gedrückt.
Das fusionierte Modell ist vom Umarmungsgesicht geladen und die Schlussfolgerung wird demonstriert.
Das Tutorial schließt mit einer Zusammenfassung der Funktionen von Mistral 7B und einer Zusammenfassung der Schritte, die mit dem Zugriff auf, Feinabstimmung und Bereitstellung des Modells verbunden sind. Ressourcen und FAQs sind ebenfalls enthalten. Der Schwerpunkt liegt auf der Bereitstellung eines praktischen Leitfadens für Benutzer, die mit diesem leistungsstarken Sprachmodell arbeiten können.
Das obige ist der detaillierte Inhalt vonMistral 7B Tutorial: Eine Schritt-für-Schritt-Anleitung zur Verwendung und der Feinabstimmung Mistral 7B. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!