File size: 8,505 Bytes
393ef36
3830f5a
8211b00
393ef36
 
3830f5a
393ef36
 
 
 
 
 
 
8211b00
393ef36
 
8211b00
393ef36
 
 
 
 
 
 
 
 
 
 
 
 
8211b00
393ef36
 
 
 
 
 
8211b00
393ef36
8211b00
 
3830f5a
 
 
 
 
393ef36
 
 
 
8211b00
393ef36
 
 
 
 
 
 
 
 
 
8211b00
 
 
 
 
 
393ef36
 
 
 
 
 
8211b00
 
 
393ef36
 
8211b00
393ef36
 
 
 
 
8211b00
 
393ef36
 
 
 
 
 
 
8211b00
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
393ef36
8211b00
 
 
 
 
393ef36
 
 
8211b00
393ef36
 
 
 
8211b00
 
393ef36
 
8211b00
 
393ef36
8211b00
 
 
 
 
393ef36
8211b00
 
393ef36
8211b00
 
393ef36
8211b00
393ef36
 
 
3830f5a
 
 
 
 
 
8211b00
3830f5a
 
 
393ef36
8211b00
 
 
393ef36
3830f5a
393ef36
3830f5a
393ef36
 
 
 
 
 
 
 
 
 
 
8211b00
393ef36
 
 
8211b00
 
393ef36
8211b00
 
 
 
393ef36
 
8211b00
393ef36
8211b00
 
393ef36
 
8211b00
 
 
393ef36
 
 
 
8211b00
393ef36
8211b00
 
 
 
 
 
 
 
 
 
 
 
 
 
393ef36
 
 
 
 
 
 
8211b00
393ef36
 
3830f5a
393ef36
 
 
 
 
8211b00
393ef36
8211b00
 
 
 
393ef36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# ==============================================
# Stephen Model Fine-Tuning Script (LoRA + PEFT)
# Clean + Debug-Enhanced for Grad & Deprecation Warnings
# ==============================================

!pip install -q "transformers>=4.44.0" "datasets" "peft>=0.12.0" accelerate bitsandbytes sentencepiece huggingface_hub

import os
from datetime import datetime
from huggingface_hub import login, whoami
from datasets import load_dataset
from transformers import (
    AutoTokenizer, AutoModelForCausalLM, TrainingArguments,
    Trainer, DataCollatorForLanguageModeling, BitsAndBytesConfig
)
from peft import LoraConfig, get_peft_model
from peft import prepare_model_for_kbit_training
import torch

# ==============================================
# Logging helper
# ==============================================
def log(msg):
    print(f"[{datetime.now().strftime('%H:%M:%S')}] {msg}")

# ==============================================
# 1. Hugging Face Login
# ==============================================
HF_TOKEN = os.getenv("HF_TOKEN")
if not HF_TOKEN:
    raise ValueError("❌ HF_TOKEN environment variable not set.")

log("Logging into Hugging Face...")
login(token=HF_TOKEN, add_to_git_credential=True)
log(f"Logged in as: {whoami()['name']} ✅")

# ==============================================
# 2. Load Dataset
# ==============================================
dataset_name = "dgtalbug/stephen-dataset"  # CHANGE THIS
data_file = "stephen.jsonl"                # CHANGE THIS

log(f"Loading dataset: {dataset_name}/{data_file} ...")
dataset = load_dataset(dataset_name, data_files=data_file, split="train")
log(f"Dataset loaded — {len(dataset)} rows")
log(f"First example: {dataset[0]}")

# ==============================================
# 3. Load Base Model & Tokenizer
# ==============================================
base_model = "dgtalbug/stable-code-instruct-3b"  # CHANGE THIS
log(f"Loading base model: {base_model}...")

tokenizer = AutoTokenizer.from_pretrained(
    base_model,
    token=HF_TOKEN,
    use_fast=True
)
if tokenizer.pad_token is None:
    tokenizer.pad_token = tokenizer.eos_token

# ✅ Quantization config
bnb_config = BitsAndBytesConfig(
    load_in_8bit=True,
    llm_int8_threshold=6.0
)

try:
    model = AutoModelForCausalLM.from_pretrained(
        base_model,
        token=HF_TOKEN,
        device_map="auto",
        torch_dtype=torch.float16,
        trust_remote_code=True,
        return_dict=True,
        quantization_config=bnb_config
    )
except Exception as e:
    log(f"⚠️ Quantized load failed: {e} — falling back to fp16.")
    model = AutoModelForCausalLM.from_pretrained(
        base_model,
        token=HF_TOKEN,
        device_map="auto",
        torch_dtype=torch.float16,
        trust_remote_code=True,
        return_dict=True
    )

log("Base model loaded ✅")

# ==============================================
# 4. LoRA Config
# ==============================================
# log("Configuring LoRA...")
# lora_config = LoraConfig(
#     r=16,
#     lora_alpha=32,
#     target_modules=["q_proj", "v_proj", "k_proj", "o_proj"],
#     lora_dropout=0.05,
#     bias="none",
#     task_type="CAUSAL_LM"
# )
# model = get_peft_model(model, lora_config)

# # ✅ Ensure LoRA params require grad
# for name, param in model.named_parameters():
#     if "lora" in name:
#         param.requires_grad = True
#     else:
#         param.requires_grad = False

# # ✅ Sanity check: see how many params are trainable
# model.print_trainable_parameters()

# log("LoRA config applied ✅")
log("Configuring LoRA...")

# First, prepare for 8-bit training (important for bitsandbytes)
model = prepare_model_for_kbit_training(model)

# LoRA config
lora_config = LoraConfig(
    r=16,
    lora_alpha=32,
    target_modules=["q_proj", "v_proj", "k_proj", "o_proj"],  # adjust if needed
    lora_dropout=0.05,
    bias="none",
    task_type="CAUSAL_LM"
)

# Apply LoRA
model = get_peft_model(model, lora_config)

# Double-check trainable params
trainable_params = []
for name, param in model.named_parameters():
    if param.requires_grad:
        trainable_params.append(name)

if not trainable_params:
    raise RuntimeError("❌ No parameters set to require gradients! LoRA not applied correctly.")

log(f"✅ Found {len(trainable_params)} trainable parameters.")
log(f"First 20 trainable params: {trainable_params[:20]}")

# Print PEFT/LoRA summary
model.print_trainable_parameters()
# ==============================================
# 5. Tokenize Dataset
# ==============================================
log("Tokenizing dataset...")

first_row = dataset[0]
if "text" in first_row:
    text_key = "text"
elif "prompt" in first_row:
    text_key = "prompt"
else:
    text_key = list(first_row.keys())[0]

log(f"Using text key: '{text_key}'")

def tokenize_fn(example):
    tokenized = tokenizer(example[text_key], truncation=True, padding="max_length", max_length=512)
    tokenized["labels"] = tokenized["input_ids"].copy()  # ✅ Ensure labels exist for grad
    return tokenized

tokenized_dataset = dataset.map(tokenize_fn, batched=True, remove_columns=dataset.column_names)
log("Tokenization complete ✅")
log(f"Tokenized sample: {tokenized_dataset[0]}")

# ==============================================
# 6. Data Collator
# ==============================================
data_collator = DataCollatorForLanguageModeling(tokenizer, mlm=False)

# ==============================================
# 7. Training Arguments
# ==============================================
output_dir = "./stephen-lora"
log("Preparing training arguments...")

training_args = TrainingArguments(
    output_dir=output_dir,
    overwrite_output_dir=True,
    per_device_train_batch_size=8,
    gradient_accumulation_steps=2,
    gradient_checkpointing=True,
    warmup_steps=50,
    num_train_epochs=3,
    max_steps=-1,
    learning_rate=1e-4,
    lr_scheduler_type="cosine",
    fp16=True,
    optim="adamw_torch",
    logging_dir="./logs",
    logging_steps=20,
    save_strategy="epoch",
    save_total_limit=2,
    push_to_hub=True,
    hub_strategy="end",
    ddp_find_unused_parameters=False,
    label_names=["labels"]
)
log("Training arguments ready ✅")

# ==============================================
# 8. Debugging Helper Hooks
# ==============================================
def debug_batch(batch):
    log(f"🔍 Debug batch keys: {list(batch.keys())}")
    log(f"🔍 First input_ids: {batch['input_ids'][0][:10]}")
    log(f"🔍 First labels: {batch['labels'][0][:10]}")
    log(f"🔍 labels.requires_grad? {torch.tensor(batch['labels']).requires_grad}")

# ==============================================
# 9. Custom Trainer (safe + debug)
# ==============================================
class SafeTrainer(Trainer):
    def compute_loss(self, model, inputs, return_outputs=False, **kwargs):
        # Debug batch content once
        if self.state.global_step == 0:
            debug_batch(inputs)
        if "labels" not in inputs:
            inputs["labels"] = inputs["input_ids"].clone()
        outputs = model(**inputs)
        loss = outputs.get("loss") if isinstance(outputs, dict) else outputs[0]
        return (loss, outputs) if return_outputs else loss

log("Initializing Trainer...")
trainer = SafeTrainer(
    model=model,
    args=training_args,
    train_dataset=tokenized_dataset,
    data_collator=data_collator
)
log("Trainer initialized ✅")

# ==============================================
# 10. Train & Push
# ==============================================
trainable_params = [n for n, p in model.named_parameters() if p.requires_grad]
log(f"Trainable params count: {len(trainable_params)}")
log(f"First 20 trainable params: {trainable_params[:20]}")

last_ckpt = None
if os.path.isdir(output_dir):
    checkpoints = [d for d in os.listdir(output_dir) if d.startswith("checkpoint-")]
    if checkpoints:
        last_ckpt = os.path.join(output_dir, sorted(checkpoints)[-1])

if last_ckpt and os.path.isdir(last_ckpt):
    log(f"Resuming from checkpoint: {last_ckpt}")
    trainer.train(resume_from_checkpoint=last_ckpt)
else:
    log("No checkpoint found — starting fresh training.")
    trainer.train()

log("Training completed ✅")

try:
    log("Pushing fine-tuned model to Hugging Face Hub...")
    trainer.push_to_hub(repo_id="dgtalbug/stephen", token=HF_TOKEN)
    log(f"Model pushed to: https://huggingface.co/dgtalbug/stephen ✅")
except Exception as e:
    log(f"⚠️ Push to hub failed: {e}")