-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path03_finetuning.py
More file actions
66 lines (58 loc) · 1.54 KB
/
Copy path03_finetuning.py
File metadata and controls
66 lines (58 loc) · 1.54 KB
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
"""
QuantLLM v2.2 -- Fine-tuning with LoRA
Fine-tune a model on a small custom dataset, test the result,
then export to GGUF.
"""
from quantllm import turbo
# Sample training data (instruction format)
training_data = [
{
"text": (
"### Instruction:\n"
"What is Python?\n\n"
"### Response:\n"
"Python is a high-level, interpreted programming language."
)
},
{
"text": (
"### Instruction:\n"
"Explain AI.\n\n"
"### Response:\n"
"AI is artificial intelligence -- machines that can learn and reason."
)
},
{
"text": (
"### Instruction:\n"
"What is machine learning?\n\n"
"### Response:\n"
"ML is a subset of AI where systems learn patterns from data."
)
},
]
# Load model
print("Loading TinyLlama...")
model = turbo("TinyLlama/TinyLlama-1.1B-Chat-v1.0")
# Fine-tune
print("\nFine-tuning (1 epoch, 3 samples)...")
result = model.finetune(
data=training_data,
epochs=1,
batch_size=2,
learning_rate=2e-4,
lora_r=8,
lora_alpha=16,
output_dir="./finetuned_model",
)
print(f" train_loss: {result['train_loss']:.4f}")
print(f" output_dir: {result['output_dir']}")
# Test
print("\nTesting fine-tuned model...")
response = model.generate("What is Python?", max_new_tokens=50)
print(f" {response}")
# Export
print("\nExporting to GGUF...")
path = model.export("gguf", "finetuned-q4.gguf")
print(f" {path}")
print("\nFine-tuning complete.")