Editing
Fine-tuning Large Language Models
(section)
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== <span style="color: #FFFFFF;">Applying</span> == '''LoRA fine-tuning with HuggingFace + PEFT:''' <syntaxhighlight lang="python"> from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments from peft import LoraConfig, get_peft_model, TaskType from trl import SFTTrainer import datasets # Load base model (quantized for efficiency) model = AutoModelForCausalLM.from_pretrained( "meta-llama/Llama-2-7b-hf", load_in_4bit=True, # QLoRA: quantize to 4-bit device_map="auto" ) # LoRA configuration lora_config = LoraConfig( r=16, # Rank lora_alpha=32, # Scaling factor target_modules=["q_proj", "v_proj"], # Which layers to adapt lora_dropout=0.05, bias="none", task_type=TaskType.CAUSAL_LM ) model = get_peft_model(model, lora_config) model.print_trainable_parameters() # trainable params: 4,194,304 || all params: 6,742,609,920 || trainable%: 0.06% # Training setup training_args = TrainingArguments( output_dir="./finetuned_model", num_train_epochs=3, per_device_train_batch_size=4, gradient_accumulation_steps=4, learning_rate=2e-4, fp16=True, save_steps=100, logging_steps=25, ) # Dataset: each sample has "text" field with full formatted prompt+response dataset = datasets.load_dataset("json", data_files="train.jsonl")["train"] trainer = SFTTrainer( model=model, args=training_args, train_dataset=dataset, dataset_text_field="text", max_seq_length=2048, ) trainer.train() </syntaxhighlight> ; Data format for instruction tuning (Llama chat template) : '''System''' β Defines the model's role and constraints : '''User turn''' β The instruction or question : '''Assistant turn''' β The desired response (what the model learns to produce) : '''Special tokens''' β [INST], [/INST], <<SYS>> etc. must exactly match the model's chat template </div> <div style="background-color: #8B4500; color: #FFFFFF; padding: 20px; border-radius: 8px; margin-bottom: 15px;">
Summary:
Please note that all contributions to BloomWiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
BloomWiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information