-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path10_qwen35_vision_finetuning.py
More file actions
215 lines (180 loc) · 6.95 KB
/
10_qwen35_vision_finetuning.py
File metadata and controls
215 lines (180 loc) · 6.95 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
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
"""
Qwen3.5 Vision Fine-Tuning with MLX-Tune
This example mirrors Unsloth's Qwen3.5 Vision notebook:
https://colab.research.google.com/github/unslothai/notebooks/blob/main/nb/Qwen3_5_(0_8B)_Vision.ipynb
Instead of CUDA/Triton, this runs natively on Apple Silicon using MLX.
Just change the import line to switch between Unsloth and MLX-Tune!
Usage:
python examples/10_qwen35_vision_finetuning.py
"""
# ===========================================================================
# MLX-Tune imports (Unsloth equivalent)
# ===========================================================================
# Unsloth (CUDA):
# from unsloth import FastVisionModel
# from unsloth.trainer import UnslothVisionDataCollator
# from trl import SFTTrainer, SFTConfig
#
# MLX-Tune (Apple Silicon) - SAME API:
from mlx_tune import FastVisionModel, UnslothVisionDataCollator, VLMSFTTrainer
from mlx_tune.vlm import VLMSFTConfig
# ===========================================================================
# Step 1: Load the model
# ===========================================================================
print("=" * 70)
print("Step 1: Loading Qwen3.5-0.8B Vision Model")
print("=" * 70)
model, processor = FastVisionModel.from_pretrained(
"mlx-community/Qwen3.5-0.8B-bf16",
load_in_4bit=False, # Use False for 16bit LoRA (better quality)
use_gradient_checkpointing="unsloth", # For long context
)
# ===========================================================================
# Step 2: Add LoRA adapters
# ===========================================================================
print("\n" + "=" * 70)
print("Step 2: Adding LoRA Adapters")
print("=" * 70)
model = FastVisionModel.get_peft_model(
model,
finetune_vision_layers=True, # Fine-tune vision layers
finetune_language_layers=True, # Fine-tune language layers
finetune_attention_modules=True, # Fine-tune attention
finetune_mlp_modules=True, # Fine-tune MLP
r=16, # LoRA rank
lora_alpha=16, # Recommended: alpha == r
lora_dropout=0,
bias="none",
random_state=3407,
use_rslora=False,
loftq_config=None,
)
# ===========================================================================
# Step 3: Prepare the dataset
# ===========================================================================
print("\n" + "=" * 70)
print("Step 3: Preparing Dataset")
print("=" * 70)
from datasets import load_dataset
dataset = load_dataset("unsloth/LaTeX_OCR", split="train")
print(f"Dataset loaded: {len(dataset)} samples")
print(f"Columns: {dataset.column_names}")
print(f"Sample text: {dataset[2]['text'][:100]}...")
# Format dataset for vision fine-tuning
# All vision tasks should use this format:
# [
# {"role": "user", "content": [{"type": "text", "text": Q}, {"type": "image", "image": img}]},
# {"role": "assistant", "content": [{"type": "text", "text": A}]},
# ]
instruction = "Write the LaTeX representation for this image."
def convert_to_conversation(sample):
conversation = [
{
"role": "user",
"content": [
{"type": "text", "text": instruction},
{"type": "image", "image": sample["image"]},
],
},
{
"role": "assistant",
"content": [{"type": "text", "text": sample["text"]}],
},
]
return {"messages": conversation}
converted_dataset = [convert_to_conversation(sample) for sample in dataset]
print(f"Converted {len(converted_dataset)} samples to conversation format")
# ===========================================================================
# Step 4: Test inference BEFORE training
# ===========================================================================
print("\n" + "=" * 70)
print("Step 4: Pre-Training Inference Test")
print("=" * 70)
FastVisionModel.for_inference(model)
image = dataset[2]["image"]
prompt = "Write the LaTeX representation for this image."
print(f"Prompt: {prompt}")
print("Generating response (before training)...")
try:
response = model.generate(
prompt=prompt,
image=image,
max_tokens=128,
temperature=1.5,
min_p=0.1,
)
print(f"Response: {response}")
except Exception as e:
print(f"Pre-training inference error (expected for some models): {e}")
# ===========================================================================
# Step 5: Train the model
# ===========================================================================
print("\n" + "=" * 70)
print("Step 5: Training")
print("=" * 70)
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, # batch_size=1 recommended for VLM (images vary in size)
gradient_accumulation_steps=4,
warmup_steps=5,
max_steps=30, # Increase to 500+ for real training
learning_rate=2e-4, # Use 5e-5 for longer runs
logging_steps=1,
optim="adam",
weight_decay=0.001,
lr_scheduler_type="linear",
seed=3407,
output_dir="outputs",
report_to="none",
remove_unused_columns=False,
dataset_text_field="",
dataset_kwargs={"skip_prepare_dataset": True},
max_length=2048,
),
)
trainer_stats = trainer.train()
print(f"\nTraining metrics: {trainer_stats.metrics}")
# ===========================================================================
# Step 6: Test inference AFTER training
# ===========================================================================
print("\n" + "=" * 70)
print("Step 6: Post-Training Inference Test")
print("NOTE: With only 30 steps, output quality may degrade (not enough")
print("training to learn the task, but enough to disrupt existing weights).")
print("For real results, train for 500+ steps with lr=5e-5.")
print("=" * 70)
FastVisionModel.for_inference(model)
image = dataset[2]["image"]
prompt = "Write the LaTeX representation for this image."
print(f"Prompt: {prompt}")
print("Generating response (after training)...")
try:
response = model.generate(
prompt=prompt,
image=image,
max_tokens=128,
temperature=1.5,
min_p=0.1,
)
print(f"Response: {response}")
except Exception as e:
print(f"Post-training inference error: {e}")
# ===========================================================================
# Step 7: Save the model
# ===========================================================================
print("\n" + "=" * 70)
print("Step 7: Saving Model")
print("=" * 70)
# Save LoRA adapters only (recommended for sharing)
model.save_pretrained("qwen_lora")
print("LoRA adapters saved to qwen_lora/")
# Uncomment to save merged model (larger but self-contained)
# model.save_pretrained_merged("qwen_merged", processor)
print("\n" + "=" * 70)
print("Done! Vision fine-tuning complete.")
print("=" * 70)