haisonle001 commited on
Commit
69ad59f
·
verified ·
1 Parent(s): 4e7b45a

Upload train.py

Browse files
Files changed (1) hide show
  1. train.py +109 -0
train.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from unsloth import FastLanguageModel
2
+ import torch
3
+ max_seq_length = 2048 # Choose any! We auto support RoPE Scaling internally!
4
+ dtype = None # None for auto detection. Float16 for Tesla T4, V100, Bfloat16 for Ampere+
5
+ load_in_4bit = True # Use 4bit quantization to reduce memory usage. Can be False.
6
+
7
+ model, tokenizer = FastLanguageModel.from_pretrained(
8
+ model_name = "unsloth/Meta-Llama-3.1-8B-Instruct-bnb-4bit",
9
+ max_seq_length = max_seq_length,
10
+ dtype = dtype,
11
+ load_in_4bit = load_in_4bit,
12
+ )
13
+ model = FastLanguageModel.get_peft_model(
14
+ model,
15
+ r = 16, # Choose any number > 0 ! Suggested 8, 16, 32, 64, 128
16
+ target_modules = ["q_proj", "k_proj", "v_proj", "o_proj",
17
+ "gate_proj", "up_proj", "down_proj",],
18
+ lora_alpha = 16,
19
+ lora_dropout = 0, # Supports any, but = 0 is optimized
20
+ bias = "none", # Supports any, but = "none" is optimized
21
+ # [NEW] "unsloth" uses 30% less VRAM, fits 2x larger batch sizes!
22
+ use_gradient_checkpointing = "unsloth", # True or "unsloth" for very long context
23
+ random_state = 3407,
24
+ use_rslora = False, # We support rank stabilized LoRA
25
+ loftq_config = None, # And LoftQ
26
+ )
27
+
28
+ prompt_context = """Bạn là một tư vấn viên hữu ích về luật.
29
+ ### Instruction and Input:
30
+ Dựa vào ngữ cảnh/tài liệu sau:
31
+ {}
32
+ Hãy trả lời câu hỏi: {}
33
+
34
+ ### Câu trả lời:
35
+ {}
36
+ """
37
+
38
+ prompt = """Bạn là một tư vấn viên hữu ích về luật.
39
+ ### Instruction and Input:
40
+ Hãy trả lời câu hỏi: {}
41
+ {}
42
+
43
+ ### Câu trả lời:
44
+ {}"""
45
+
46
+ EOS_TOKEN = tokenizer.eos_token # Must add EOS_TOKEN
47
+ def formatting_prompts_func(examples):
48
+ instructions = examples["context"]
49
+ inputs = examples["input"]
50
+ outputs = examples["output"]
51
+ texts = []
52
+ for instruction, input, output in zip(instructions, inputs, outputs):
53
+ # Must add EOS_TOKEN, otherwise your generation will go on forever!
54
+ if instructions:
55
+ text = prompt_context.format(instruction, input, output) + EOS_TOKEN
56
+ else:
57
+ text = prompt.format(input, output) + EOS_TOKEN
58
+ texts.append(text)
59
+ return { "text" : texts, }
60
+
61
+
62
+ from datasets import load_dataset
63
+ dataset = load_dataset("json", data_files="/root/unsloth/train_data.jsonl", split="train")
64
+ dataset = dataset.map(formatting_prompts_func, batched = True,)
65
+
66
+
67
+ from trl import SFTTrainer
68
+ from transformers import TrainingArguments, DataCollatorForSeq2Seq
69
+ from unsloth import is_bfloat16_supported
70
+
71
+ from trl import SFTTrainer
72
+ from transformers import TrainingArguments
73
+ from unsloth import is_bfloat16_supported
74
+
75
+ trainer = SFTTrainer(
76
+ model=model,
77
+ tokenizer=tokenizer,
78
+ train_dataset=dataset,
79
+ dataset_text_field="text",
80
+ max_seq_length=max_seq_length,
81
+ dataset_num_proc=16,
82
+ packing=False, # Can make training 5x faster for short sequences.
83
+ args=TrainingArguments(
84
+ per_device_train_batch_size=16,
85
+ gradient_accumulation_steps=4,
86
+ num_train_epochs=1, # Set this for 1 full training run.
87
+ learning_rate=2e-4,
88
+ fp16=not is_bfloat16_supported(),
89
+ bf16=is_bfloat16_supported(),
90
+ logging_steps=1,
91
+ optim="adamw_8bit",
92
+ weight_decay=0.01,
93
+ lr_scheduler_type="linear",
94
+ seed=3407,
95
+ output_dir="outputs",
96
+ report_to="none", # Use this for WandB, etc.
97
+ # Save every 1000 steps
98
+ save_steps=500,
99
+ save_total_limit=3, # Keep only the last 3 checkpoints
100
+ ),
101
+ )
102
+
103
+ trainer_stats = trainer.train()
104
+
105
+ model.save_pretrained("lora_model") # Local saving
106
+ tokenizer.save_pretrained("lora_model")
107
+
108
+ model.save_pretrained_merged("model", tokenizer, save_method = "merged_16bit",)
109
+ model.save_pretrained_merged("model", tokenizer, save_method = "lora",)