mlx-optiq
Fine-tuning · Step 2

LoRA SFT on Apple Silicon

Supervised fine-tuning teaches the model your format and your answers. It is the first training step, and for many tasks the only one.

train.shbash
$ optiq lora train mlx-community/Qwen3.5-4B-OptiQ-4bit \
    --data ./my_data \
    -o ./my_adapter

# See how rank was spread across the layers
$ optiq lora info ./my_adapter

The model argument is an OptiQ quant, from Hugging Face or a local folder. The base stays frozen; only the adapter trains, and it is saved in PEFT layout (adapter_config.json and adapters.safetensors) next to a small file recording the rank each layer got.

A frozen quantized base with a small trained LoRA adapter beside each block; blocks kept at higher precision get twice the adapter rank.quantized base, frozenLoRA adapter, trainedblock 1rank rblock 2, kept at higher precisionrank 2xblock 3rank rblock 4rank rblock 5, kept at higher precisionrank 2xblock 6rank rby_bits: layers OptiQ kept at more bits get more adapter rank
OptiQ already measured which layers are sensitive when it quantized the base. The same signal decides adapter rank.

What the defaults do

  • Every block, all seven projections. --num-layers -1 and the full target-module set, the same coverage Unsloth recommends (PEFT's own default for Qwen is only the query and value projections).
  • Rank 8, scaled by sensitivity. With --rank-scaling by_bits, layers OptiQ kept at more bits get twice the base rank. constant gives every layer the same rank; by_kl follows the raw sensitivity score.
  • Loss on the answer only. --mask-prompt is on, so the prompt tokens do not count. With multi-turn data, --all-turns auto trains every assistant turn, not just the last.
  • Epochs, not steps. Without --iters, SFT runs three epochs over your data at a learning rate of 2e-4. --num-epochs changes the count; --iters sets an absolute number of steps; --lr overrides the rate.
  • Sequences up to 512 tokens. Raise --max-seq-length if your examples are longer.

For more capacity, --preset sets rank and alpha together: small (r=8, α=16), default (r=8, α=8), medium (r=16, α=16), large, xl and xxl up to r=128.

bigger.shbash
$ optiq lora train mlx-community/Qwen3.5-4B-OptiQ-4bit \
    --data ./my_data -o ./my_adapter \
    --preset medium --num-epochs 2 --max-seq-length 2048

When memory runs short

Sequence length is what fills memory. In order of how much they help:

  • --max-seq-length: shorter examples, or split long ones.
  • --grad-checkpoint: recompute activations instead of keeping them.
  • --num-layers 16: adapt only the last 16 blocks.
  • --batch-size 1 with --grad-accumulation-steps to keep the effective batch.

At long context the full logit tensor is what runs out first. OptiQ switches to a fused loss on its own once that tensor would pass about 512 MB, so it depends on vocabulary size as well as sequence length (--fused-ce forces it, same gradients). That is how Qwen3.5-4B trains at 8k context on a 24 GB M4. Measured on Qwen3.5-4B-OptiQ-4bit, M3 Max 36 GB, batch 1, all layers, with --grad-checkpoint:

ContextStep timePeak memory
2,0482.8 s7.7 GB
4,0966.7 s11.4 GB
8,19245 s19.9 GB

The same run in the Lab

In optiq lab, Fine-tune walks through five steps: base model, dataset, hyperparameters, train, and save. It trains on any OptiQ quant, MLX-native with no PyTorch, and uses the same by_bits rank scaling. The hyperparameter page carries the main fields with the same defaults as the CLI, the train step draws the loss as it goes, and the last step can bundle the adapter with its base and push it to Hugging Face with the token saved in Settings.

The Lab's Fine-tune wizard

Every flag, the rank-scaling benchmark and the full ceiling map are in the LoRA reference.