algorythmtechnologies commited on
Commit
c31dbe8
·
verified ·
1 Parent(s): b48b735

Delete finetune_qlora.py

Browse files
Files changed (1) hide show
  1. finetune_qlora.py +0 -158
finetune_qlora.py DELETED
@@ -1,158 +0,0 @@
1
- import os
2
- import torch
3
- from datasets import load_dataset
4
- from transformers import (
5
- AutoModelForCausalLM,
6
- AutoTokenizer,
7
- BitsAndBytesConfig,
8
- TrainingArguments,
9
- )
10
- from peft import LoraConfig, get_peft_model, PeftModel
11
- from trl import SFTTrainer
12
-
13
- # 1. Model and Tokenizer Configuration
14
- model_name = "./qwen2.5-coder-3b-instruct" # Path to the local model directory
15
- dataset_name = "corrected_syntax_dataset.jsonl" # Path to your dataset
16
- new_model = "qwen2.5-coder-3b-instruct-syntax-finetuned" # Name for the fine-tuned model
17
-
18
- # 2. Q-LoRA Configuration
19
- lora_r = 64
20
- lora_alpha = 16
21
- lora_dropout = 0.1
22
-
23
- # 3. BitsAndBytes Configuration for 4-bit Quantization
24
- use_4bit = True
25
- bnb_4bit_compute_dtype = "float16"
26
- bnb_4bit_quant_type = "nf4"
27
- use_nested_quant = False
28
-
29
- # 4. Training Arguments
30
- output_dir = "./results"
31
- num_train_epochs = 1
32
- fp16 = False
33
- bf16 = True # Use bf16 for better performance on modern GPUs
34
- per_device_train_batch_size = 4
35
- per_device_eval_batch_size = 4
36
- gradient_accumulation_steps = 1
37
- gradient_checkpointing = True
38
- max_grad_norm = 0.3
39
- learning_rate = 2e-4
40
- weight_decay = 0.001
41
- optim = "paged_adamw_32bit"
42
- lr_scheduler_type = "cosine"
43
- max_steps = -1
44
- warmup_ratio = 0.03
45
- group_by_length = True
46
- save_steps = 25
47
- logging_steps = 5
48
-
49
- # 5. SFTTrainer Configuration
50
- max_seq_length = None
51
- packing = False
52
- device_map = {"": 0}
53
-
54
- # --- Script Execution ---
55
-
56
- # Load dataset
57
- # The dataset is in JSONL format, with each line having a "messages" field.
58
- dataset = load_dataset('json', data_files=dataset_name, split="train")
59
-
60
- # Load tokenizer and model with Q-LoRA configuration
61
- compute_dtype = getattr(torch, bnb_4bit_compute_dtype)
62
-
63
- bnb_config = BitsAndBytesConfig(
64
- load_in_4bit=use_4bit,
65
- bnb_4bit_quant_type=bnb_4bit_quant_type,
66
- bnb_4bit_compute_dtype=compute_dtype,
67
- bnb_4bit_use_double_quant=use_nested_quant,
68
- )
69
-
70
- # Load base model
71
- model = AutoModelForCausalLM.from_pretrained(
72
- model_name,
73
- quantization_config=bnb_config,
74
- device_map=device_map
75
- )
76
- model.config.use_cache = False
77
- model.config.pretraining_tp = 1
78
-
79
- # Load tokenizer
80
- tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
81
- tokenizer.pad_token = tokenizer.eos_token
82
- tokenizer.padding_side = "right"
83
-
84
- # PEFT Configuration
85
- peft_config = LoraConfig(
86
- lora_alpha=lora_alpha,
87
- lora_dropout=lora_dropout,
88
- r=lora_r,
89
- bias="none",
90
- task_type="CAUSAL_LM",
91
- )
92
-
93
- # Set training parameters
94
- training_arguments = TrainingArguments(
95
- output_dir=output_dir,
96
- num_train_epochs=num_train_epochs,
97
- per_device_train_batch_size=per_device_train_batch_size,
98
- gradient_accumulation_steps=gradient_accumulation_steps,
99
- optim=optim,
100
- save_steps=save_steps,
101
- logging_steps=logging_steps,
102
- learning_rate=learning_rate,
103
- weight_decay=weight_decay,
104
- fp16=fp16,
105
- bf16=bf16,
106
- max_grad_norm=max_grad_norm,
107
- max_steps=max_steps,
108
- warmup_ratio=warmup_ratio,
109
- group_by_length=group_by_length,
110
- lr_scheduler_type=lr_scheduler_type,
111
- report_to="tensorboard"
112
- )
113
-
114
- # Initialize the SFTTrainer
115
- trainer = SFTTrainer(
116
- model=model,
117
- train_dataset=dataset,
118
- peft_config=peft_config,
119
- dataset_text_field="messages", # The field in your dataset that contains the text
120
- max_seq_length=max_seq_length,
121
- tokenizer=tokenizer,
122
- args=training_arguments,
123
- packing=packing,
124
- )
125
-
126
- # Train the model
127
- print("Starting model training...")
128
- trainer.train()
129
- print("Training complete.")
130
-
131
- # Save the fine-tuned model
132
- print(f"Saving fine-tuned model to {new_model}...")
133
- trainer.model.save_pretrained(new_model)
134
- print("Model saved.")
135
-
136
- # Merge and save the final model (optional)
137
- # Reload model in FP16 and merge it with LoRA weights
138
- base_model = AutoModelForCausalLM.from_pretrained(
139
- model_name,
140
- low_cpu_mem_usage=True,
141
- return_dict=True,
142
- torch_dtype=torch.float16,
143
- device_map=device_map,
144
- )
145
- model = PeftModel.from_pretrained(base_model, new_model)
146
- model = model.merge_and_unload()
147
-
148
- # Reload tokenizer to save it
149
- tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
150
- tokenizer.pad_token = tokenizer.eos_token
151
- tokenizer.padding_side = "right"
152
-
153
- # Save the merged model
154
- merged_model_dir = os.path.join(output_dir, "final_merged_model")
155
- model.save_pretrained(merged_model_dir, safe_serialization=True)
156
- tokenizer.save_pretrained(merged_model_dir)
157
-
158
- print(f"Final merged model saved to {merged_model_dir}")