-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy path39_gemma4_text_to_sql.py
More file actions
182 lines (148 loc) · 6.18 KB
/
Copy path39_gemma4_text_to_sql.py
File metadata and controls
182 lines (148 loc) · 6.18 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
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
"""
Example 39: Gemma 4 Text-to-SQL Fine-Tuning
Text-only fine-tuning through the VLM path (no images needed).
Uses the same dataset as Google's official Gemma 4 fine-tuning guide:
https://ai.google.dev/gemma/docs/core/huggingface_text_finetune_qlora
Dataset: philschmid/gretel-synthetic-text-to-sql
Task: Convert natural language questions to SQL queries
NOTE: Gemma 4 models are all VLMs — use FastVisionModel even for text tasks.
Usage:
python examples/39_gemma4_text_to_sql.py
"""
from mlx_tune import FastVisionModel, UnslothVisionDataCollator, VLMSFTTrainer
from mlx_tune.vlm import VLMSFTConfig
def main():
print("=" * 70)
print("GEMMA 4 TEXT-TO-SQL: Google's Official Fine-Tuning Example")
print("=" * 70)
# ========================================================================
# Step 1: Load Gemma 4 model
# ========================================================================
print("\n[Step 1] Loading Gemma 4 E4B model...")
model, processor = FastVisionModel.from_pretrained(
"mlx-community/gemma-4-e4b-it-4bit",
load_in_4bit=True,
)
# ========================================================================
# Step 2: Add LoRA adapters (text layers only)
# ========================================================================
print("\n[Step 2] Adding LoRA adapters (language layers only)...")
model = FastVisionModel.get_peft_model(
model,
finetune_vision_layers=False, # No vision for text-only task
finetune_language_layers=True,
finetune_attention_modules=True,
finetune_mlp_modules=True,
r=16,
lora_alpha=16,
lora_dropout=0,
bias="none",
random_state=3407,
)
# ========================================================================
# Step 3: Prepare Text-to-SQL dataset
# ========================================================================
print("\n[Step 3] Loading Text-to-SQL dataset...")
from datasets import load_dataset
dataset = load_dataset("philschmid/gretel-synthetic-text-to-sql", split="train")
print(f"Dataset: {len(dataset)} samples")
print(f"Columns: {dataset.column_names}")
# Take a subset for demo
dataset = dataset.select(range(min(500, len(dataset))))
print(f"Using {len(dataset)} samples for demo")
def format_text_to_sql(sample):
"""Format as chat conversation for text-only VLM fine-tuning."""
# Build the user prompt with schema context
user_msg = f"Given the following SQL schema:\n{sample['sql_context']}\n\n"
user_msg += f"Write a SQL query for: {sample['sql_prompt']}"
return {
"messages": [
{
"role": "user",
"content": [{"type": "text", "text": user_msg}],
},
{
"role": "assistant",
"content": [{"type": "text", "text": sample["sql"]}],
},
]
}
converted_dataset = [format_text_to_sql(s) for s in dataset]
print(f"Converted {len(converted_dataset)} samples")
print(f"\nSample prompt:\n{converted_dataset[0]['messages'][0]['content'][0]['text'][:200]}...")
print(f"\nSample SQL:\n{converted_dataset[0]['messages'][1]['content'][0]['text'][:200]}...")
# ========================================================================
# Step 4: Pre-training inference
# ========================================================================
print("\n[Step 4] Pre-training inference test...")
FastVisionModel.for_inference(model)
test_prompt = (
"Given the following SQL schema:\n"
"CREATE TABLE employees (id INT, name VARCHAR, department VARCHAR, salary DECIMAL);\n\n"
"Write a SQL query for: Find the average salary by department"
)
try:
response = model.generate(
prompt=test_prompt,
max_tokens=128,
temperature=0.3,
)
print(f"Response: {response}")
except Exception as e:
print(f"Pre-training inference error: {e}")
# ========================================================================
# Step 5: Train
# ========================================================================
print("\n[Step 5] Training...")
FastVisionModel.for_training(model)
trainer = VLMSFTTrainer(
model=model,
tokenizer=processor,
data_collator=UnslothVisionDataCollator(model, processor),
train_dataset=converted_dataset,
args=VLMSFTConfig(
per_device_train_batch_size=1,
gradient_accumulation_steps=4,
warmup_steps=5,
max_steps=30,
learning_rate=2e-4,
logging_steps=1,
optim="adam",
weight_decay=0.001,
lr_scheduler_type="linear",
seed=3407,
output_dir="outputs_gemma4_sql",
report_to="none",
remove_unused_columns=False,
dataset_text_field="",
dataset_kwargs={"skip_prepare_dataset": True},
max_length=512,
),
)
trainer_stats = trainer.train()
print(f"\nTraining metrics: {trainer_stats.metrics}")
# ========================================================================
# Step 6: Post-training inference
# ========================================================================
print("\n[Step 6] Post-training inference test...")
FastVisionModel.for_inference(model)
try:
response = model.generate(
prompt=test_prompt,
max_tokens=128,
temperature=0.3,
)
print(f"Response: {response}")
except Exception as e:
print(f"Post-training inference error: {e}")
# ========================================================================
# Step 7: Save
# ========================================================================
print("\n[Step 7] Saving LoRA adapters...")
model.save_pretrained("gemma4_sql_lora")
print("Saved to gemma4_sql_lora/")
print("\n" + "=" * 70)
print("Done! Gemma 4 Text-to-SQL fine-tuning complete.")
print("=" * 70)
if __name__ == "__main__":
main()