Nutnell commited on
Commit
cb59384
·
verified ·
1 Parent(s): 94b7c40

Update fine_tune.py

Browse files
Files changed (1) hide show
  1. fine_tune.py +94 -94
fine_tune.py CHANGED
@@ -1,95 +1,95 @@
1
- # fine_tuning/fine_tune.py
2
-
3
- import os
4
- import torch
5
- from datasets import load_dataset
6
- from transformers import (
7
- AutoModelForCausalLM,
8
- AutoTokenizer,
9
- TrainingArguments,
10
- )
11
- from peft import LoraConfig
12
- from trl import SFTTrainer
13
-
14
- # --- 1. Configuration ---
15
- # Using the efficient, pre-quantized Llama 3 model
16
- base_model_name = "unsloth/llama-3-8b-Instruct-bnb-4bit"
17
- output_dir = "fine_tuning/results/llama-3-8b-instruct-direct-ed"
18
- dataset_path = "fine_tuning/dataset.jsonl"
19
-
20
-
21
- # --- 2. Load the Dataset ---
22
- print("Loading dataset...")
23
- dataset = load_dataset("json", data_files=dataset_path, split="train")
24
- print("Dataset loaded successfully.")
25
-
26
-
27
- # --- 3. Load the Base Model & Tokenizer ---
28
- print(f"Loading base model: {base_model_name}...")
29
- # This model is already optimized to load in 4-bit, no extra config needed
30
- model = AutoModelForCausalLM.from_pretrained(
31
- base_model_name,
32
- device_map="auto",
33
- trust_remote_code=True,
34
- )
35
- model.config.use_cache = False
36
- print("Base model loaded successfully.")
37
-
38
- tokenizer = AutoTokenizer.from_pretrained(base_model_name, trust_remote_code=True)
39
- if tokenizer.pad_token is None:
40
- tokenizer.pad_token = tokenizer.eos_token
41
- tokenizer.padding_side = "right"
42
- print("Tokenizer loaded and configured.")
43
-
44
-
45
- # --- 4. Configure PEFT (LoRA) ---
46
- peft_config = LoraConfig(
47
- lora_alpha=16,
48
- lora_dropout=0.1,
49
- r=16,
50
- bias="none",
51
- task_type="CAUSAL_LM",
52
- # Target modules specific to Llama 3 models
53
- target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
54
- )
55
- print("LoRA configured.")
56
-
57
-
58
- # --- 5. Define Training Arguments ---
59
- training_arguments = TrainingArguments(
60
- output_dir=output_dir,
61
- num_train_epochs=1,
62
- per_device_train_batch_size=2,
63
- gradient_accumulation_steps=2,
64
- optim="paged_adamw_32bit",
65
- logging_steps=10,
66
- learning_rate=2e-4,
67
- weight_decay=0.01,
68
- fp16=True, # Use mixed precision
69
- max_grad_norm=0.3,
70
- max_steps=-1,
71
- warmup_ratio=0.03,
72
- group_by_length=True,
73
- lr_scheduler_type="linear",
74
- )
75
- print("Training arguments set.")
76
-
77
-
78
- # --- 6. Initialize and Start Training ---
79
- trainer = SFTTrainer(
80
- model=model,
81
- train_dataset=dataset,
82
- peft_config=peft_config,
83
- dataset_text_field="text",
84
- max_seq_length=2048,
85
- tokenizer=tokenizer,
86
- args=training_arguments,
87
- )
88
- print("Trainer initialized. Starting the fine-tuning process...")
89
- trainer.train()
90
- print("Training complete.")
91
-
92
-
93
- # --- 7. Save the Final Model ---
94
- trainer.model.save_pretrained(output_dir)
95
  print(f"Fine-tuned model adapter saved to {output_dir}")
 
1
+
2
+ # fine_tuning/fine_tune.py
3
+
4
+ import os
5
+ import torch
6
+ from datasets import load_dataset
7
+ from transformers import (
8
+ AutoModelForCausalLM,
9
+ AutoTokenizer,
10
+ TrainingArguments,
11
+ BitsAndBytesConfig
12
+ )
13
+ from peft import LoraConfig
14
+ from trl import SFTTrainer
15
+
16
+
17
+ base_model_name = "unsloth/llama-3-8b-Instruct-bnb-4bit"
18
+ output_dir = "fine_tuning/results/llama-3-8b-instruct-direct-ed"
19
+
20
+ dataset_path = "dataset.jsonl"
21
+
22
+
23
+ # Load the Dataset
24
+ print("Loading dataset...")
25
+ dataset = load_dataset("json", data_files=dataset_path, split="train")
26
+ print("Dataset loaded successfully.")
27
+
28
+
29
+ # Load the Base Model & Tokenizer
30
+ print(f"Loading base model: {base_model_name}...")
31
+ model = AutoModelForCausalLM.from_pretrained(
32
+ base_model_name,
33
+ device_map="auto",
34
+ trust_remote_code=True,
35
+ )
36
+ model.config.use_cache = False
37
+ print("Base model loaded successfully.")
38
+
39
+ tokenizer = AutoTokenizer.from_pretrained(base_model_name, trust_remote_code=True)
40
+ if tokenizer.pad_token is None:
41
+ tokenizer.pad_token = tokenizer.eos_token
42
+ tokenizer.padding_side = "right"
43
+ print("Tokenizer loaded and configured.")
44
+
45
+
46
+ # Configure PEFT (LoRA)
47
+ peft_config = LoraConfig(
48
+ lora_alpha=16,
49
+ lora_dropout=0.1,
50
+ r=16,
51
+ bias="none",
52
+ task_type="CAUSAL_LM",
53
+ target_modules=["q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj"],
54
+ )
55
+ print("LoRA configured.")
56
+
57
+
58
+ # Define Training Arguments
59
+ training_arguments = TrainingArguments(
60
+ output_dir=output_dir,
61
+ num_train_epochs=1,
62
+ per_device_train_batch_size=2,
63
+ gradient_accumulation_steps=2,
64
+ optim="paged_adamw_32bit",
65
+ logging_steps=10,
66
+ learning_rate=2e-4,
67
+ weight_decay=0.01,
68
+ fp16=True,
69
+ max_grad_norm=0.3,
70
+ max_steps=-1,
71
+ warmup_ratio=0.03,
72
+ group_by_length=True,
73
+ lr_scheduler_type="linear",
74
+ )
75
+ print("Training arguments set.")
76
+
77
+
78
+ # Initialize and Start Training
79
+ trainer = SFTTrainer(
80
+ model=model,
81
+ train_dataset=dataset,
82
+ peft_config=peft_config,
83
+ dataset_text_field="text",
84
+ max_seq_length=2048,
85
+ tokenizer=tokenizer,
86
+ args=training_arguments,
87
+ )
88
+ print("Trainer initialized. Starting the fine-tuning process...")
89
+ trainer.train()
90
+ print("Training complete.")
91
+
92
+
93
+ # Save the Final Model
94
+ trainer.model.save_pretrained(output_dir)
95
  print(f"Fine-tuned model adapter saved to {output_dir}")