amogaddy commited on
Commit
c522400
Β·
verified Β·
1 Parent(s): 8344211

Upload finetune.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. finetune.py +214 -0
finetune.py ADDED
@@ -0,0 +1,214 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ finetune.py
3
+ ===========
4
+ Fine-tuning con LoRA/QLoRA di un LLM su dataset.jsonl.
5
+ Usa TRL SFTTrainer β€” funziona su GPU consumer (4GB+) o su Google Colab gratuito.
6
+
7
+ Modello base consigliato (leggero, multilingue, italiano):
8
+ Qwen/Qwen2.5-0.5B-Instruct ← 0.5B param, ottimo su CPU/GPU piccola
9
+ Qwen/Qwen2.5-1.5B-Instruct ← 1.5B param, qualitΓ  migliore
10
+ google/gemma-2-2b-it ← 2B param, ottimo in italiano
11
+
12
+ Uso:
13
+ # Training completo
14
+ python finetune.py --dataset dataset.jsonl --hf-repo TUO_USERNAME/generai-model
15
+
16
+ # Solo dry-run (verifica che funzioni)
17
+ python finetune.py --dataset dataset.jsonl --dry-run
18
+ """
19
+
20
+ import argparse
21
+ import json
22
+ import os
23
+ import sys
24
+ from pathlib import Path
25
+
26
+ # ── Dipendenze opzionali β€” errore chiaro se mancano ───────────────────────────
27
+ def _check_deps():
28
+ missing = []
29
+ for pkg in ["transformers", "trl", "peft", "datasets", "torch"]:
30
+ try:
31
+ __import__(pkg)
32
+ except ImportError:
33
+ missing.append(pkg)
34
+ if missing:
35
+ print("❌ Dipendenze mancanti per il training:")
36
+ print(f" pip install {' '.join(missing)}")
37
+ print(" (oppure: pip install -r requirements-train.txt)")
38
+ sys.exit(1)
39
+
40
+ _check_deps()
41
+
42
+ import torch
43
+ from datasets import Dataset
44
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
45
+ from peft import LoraConfig, get_peft_model, TaskType
46
+ from trl import SFTTrainer, SFTConfig
47
+
48
+
49
+ DEFAULT_MODEL = "Qwen/Qwen2.5-0.5B-Instruct"
50
+ OUTPUT_DIR = "./generai-finetuned"
51
+
52
+
53
+ def load_dataset_jsonl(path: str) -> Dataset:
54
+ rows = []
55
+ with open(path, encoding="utf-8") as f:
56
+ for line in f:
57
+ line = line.strip()
58
+ if line:
59
+ rows.append(json.loads(line))
60
+ if not rows:
61
+ print(f"❌ Dataset vuoto: {path}")
62
+ sys.exit(1)
63
+ print(f"[dataset] {len(rows)} esempi caricati da {path}")
64
+ return Dataset.from_list(rows)
65
+
66
+
67
+ def apply_chat_template(examples, tokenizer):
68
+ """Applica il chat template del modello ai messaggi."""
69
+ texts = []
70
+ for msgs in examples["messages"]:
71
+ text = tokenizer.apply_chat_template(
72
+ msgs,
73
+ tokenize=False,
74
+ add_generation_prompt=False,
75
+ )
76
+ texts.append(text)
77
+ return {"text": texts}
78
+
79
+
80
+ def build_model_and_tokenizer(model_name: str, use_4bit: bool):
81
+ print(f"[model] Caricamento: {model_name}")
82
+
83
+ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
84
+ if tokenizer.pad_token is None:
85
+ tokenizer.pad_token = tokenizer.eos_token
86
+
87
+ bnb_config = None
88
+ if use_4bit:
89
+ try:
90
+ import bitsandbytes # noqa
91
+ bnb_config = BitsAndBytesConfig(
92
+ load_in_4bit=True,
93
+ bnb_4bit_quant_type="nf4",
94
+ bnb_4bit_use_double_quant=True,
95
+ bnb_4bit_compute_dtype=torch.bfloat16,
96
+ )
97
+ print("[model] Quantizzazione 4-bit attivata (QLoRA)")
98
+ except ImportError:
99
+ print("[model] bitsandbytes non disponibile β€” uso fp16 standard")
100
+ use_4bit = False
101
+
102
+ model = AutoModelForCausalLM.from_pretrained(
103
+ model_name,
104
+ quantization_config=bnb_config,
105
+ torch_dtype=torch.bfloat16 if not use_4bit else None,
106
+ device_map="auto",
107
+ trust_remote_code=True,
108
+ )
109
+ return model, tokenizer
110
+
111
+
112
+ def build_lora_config() -> LoraConfig:
113
+ return LoraConfig(
114
+ task_type=TaskType.CAUSAL_LM,
115
+ r=16, # rank β€” piΓΉ alto = piΓΉ parametri addestrati
116
+ lora_alpha=32,
117
+ lora_dropout=0.05,
118
+ bias="none",
119
+ target_modules="all-linear",
120
+ )
121
+
122
+
123
+ def main():
124
+ parser = argparse.ArgumentParser()
125
+ parser.add_argument("--dataset", required=True, help="Path al file dataset.jsonl")
126
+ parser.add_argument("--model", default=DEFAULT_MODEL, help=f"Modello base HuggingFace (default: {DEFAULT_MODEL})")
127
+ parser.add_argument("--hf-repo", default="", help="Username/nome-repo su HuggingFace per upload automatico")
128
+ parser.add_argument("--epochs", type=int, default=3, help="Numero di epoche (default: 3)")
129
+ parser.add_argument("--batch", type=int, default=2, help="Batch size (default: 2)")
130
+ parser.add_argument("--lr", type=float, default=2e-4, help="Learning rate (default: 2e-4)")
131
+ parser.add_argument("--max-len", type=int, default=512, help="Lunghezza massima token (default: 512)")
132
+ parser.add_argument("--4bit", dest="use_4bit", action="store_true", help="Usa QLoRA 4-bit (serve bitsandbytes)")
133
+ parser.add_argument("--dry-run", action="store_true", help="Verifica configurazione senza addestrare")
134
+ args = parser.parse_args()
135
+
136
+ if not Path(args.dataset).exists():
137
+ print(f"❌ File non trovato: {args.dataset}")
138
+ print(" Esegui prima: python export_dataset.py")
139
+ sys.exit(1)
140
+
141
+ # ── Carica dataset ─────────────────────────────────────────────────────────
142
+ raw_ds = load_dataset_jsonl(args.dataset)
143
+
144
+ # ── Carica modello ─────────────────────────────────────────────────────────
145
+ model, tokenizer = build_model_and_tokenizer(args.model, args.use_4bit)
146
+
147
+ # ── Applica chat template ──────────────────────────────────────────────────
148
+ ds = raw_ds.map(
149
+ lambda x: apply_chat_template(x, tokenizer),
150
+ batched=True,
151
+ remove_columns=raw_ds.column_names,
152
+ )
153
+ print(f"[dataset] Esempio tokenizzato:\n{ds[0]['text'][:300]}...\n")
154
+
155
+ if args.dry_run:
156
+ print("βœ… Dry-run completato. Configurazione corretta.")
157
+ print(f" Modello : {args.model}")
158
+ print(f" Dataset : {len(ds)} esempi")
159
+ print(f" Output : {OUTPUT_DIR}")
160
+ return
161
+
162
+ # ── LoRA ───────────────────────────────────────────────────────────────────
163
+ lora_cfg = build_lora_config()
164
+ model = get_peft_model(model, lora_cfg)
165
+ model.print_trainable_parameters()
166
+
167
+ # ── Training config ────────────────────────────────────────────────────────
168
+ sft_cfg = SFTConfig(
169
+ output_dir=OUTPUT_DIR,
170
+ num_train_epochs=args.epochs,
171
+ per_device_train_batch_size=args.batch,
172
+ gradient_accumulation_steps=4,
173
+ learning_rate=args.lr,
174
+ lr_scheduler_type="cosine",
175
+ warmup_ratio=0.1,
176
+ logging_steps=10,
177
+ save_steps=50,
178
+ save_total_limit=2,
179
+ bf16=torch.cuda.is_available(),
180
+ fp16=False,
181
+ max_seq_length=args.max_len,
182
+ dataset_text_field="text",
183
+ report_to="none",
184
+ push_to_hub=bool(args.hf_repo),
185
+ hub_model_id=args.hf_repo or None,
186
+ )
187
+
188
+ trainer = SFTTrainer(
189
+ model=model,
190
+ train_dataset=ds,
191
+ args=sft_cfg,
192
+ tokenizer=tokenizer,
193
+ )
194
+
195
+ print("\nπŸš€ Avvio training...\n")
196
+ trainer.train()
197
+
198
+ # ── Salva ──────────────────────────────────────────────────────────────────
199
+ trainer.save_model(OUTPUT_DIR)
200
+ tokenizer.save_pretrained(OUTPUT_DIR)
201
+ print(f"\nβœ… Modello salvato in: {OUTPUT_DIR}")
202
+
203
+ if args.hf_repo:
204
+ print(f"⬆️ Upload su HuggingFace: https://huggingface.co/{args.hf_repo}")
205
+ trainer.push_to_hub()
206
+ else:
207
+ print("\nPer caricare su HuggingFace:")
208
+ print(f" huggingface-cli upload TUO_USERNAME/generai-model {OUTPUT_DIR}")
209
+ print("\nPer usarlo in GenerAI:")
210
+ print(f" Imposta HF_MODEL={args.hf_repo or 'TUO_USERNAME/generai-model'} in .env")
211
+
212
+
213
+ if __name__ == "__main__":
214
+ main()