Skip to content

Route supported notebooks through UnslothTrainer - #294

Closed
Lyxot wants to merge 5 commits into
unslothai:mainfrom
Lyxot:mlx/trainer-api
Closed

Lyxot wants to merge 5 commits into
unslothai:mainfrom
Lyxot:mlx/trainer-api

Conversation

@Lyxot

@Lyxot Lyxot commented Jun 19, 2026 •

Copy link
Copy Markdown

Summary

Companion PR for the main unsloth MLX trainer API PR: unslothai/unsloth#6462

This migrates supported SFT notebooks, plus explicitly selected near-term notebook surfaces, to the backend-aware public Unsloth trainer API. Users should not need to edit trainer imports by hand for MLX: notebooks import UnslothTrainer / UnslothTrainingArguments from unsloth, and the runtime routes to CUDA/ROCm/XPU or MLX as appropriate when the task surface is supported.

Companion PRs

Notebook Scope

This PR updates SFT-style notebook trainer surfaces that are supported by the MLX route now, plus selected notebook surfaces that should not need another public API migration when backend-owned support lands. It intentionally excludes task families that are not in the MLX notebook target set.

The mechanical notebook migration is intentionally narrow:

  • Replace notebook from trl import SFTTrainer, SFTConfig with from unsloth import UnslothTrainer, UnslothTrainingArguments.
  • Replace notebook SFTTrainer(...) calls with UnslothTrainer(...).
  • Replace notebook SFTConfig(...) calls with UnslothTrainingArguments(...).
  • Replace CUDA-only memory-stat cells with get_gpu_memory_stats() from unsloth.
  • Replace CUDA-only cleanup cells with clear_gpu_memory() from unsloth where the notebook had a cleanup helper.
  • Remove stale import torch lines when they were only used by the replaced CUDA memory-stat cells.
  • Update nearby markdown so trainer descriptions and links refer to the public Unsloth trainer/fine-tuning API instead of TRL SFTTrainer docs.
  • Normalize final training-stat cells so they work with both CUDA TrainOutput.metrics and the MLX trainer's metrics dict.

The broadened notebook set now includes:

  • Existing supported text SFT notebooks already covered by the trainer API migration.
  • 10 Synthetic-named text SFT notebooks across Colab, AMD, Kaggle, and original templates.
  • 44 verified Vision/VL notebooks that use UnslothVisionDataCollator and map to MLX training-qualified VLM families.
  • 18 text-only DataCollatorForSeq2Seq SFT notebooks, including Llama 3.2, Llama 3.3, Phi-4, Qwen2.5 Coder, and Synthetic Data Hackathon variants.
  • 4 packing=True TinyLlama SFT notebooks, so packing recipes are already routed through the public Unsloth trainer API.

What this PR avoids:

  • It does not change update_all_notebooks.py; the migration was a one-time generated replacement and the replacement script is not tracked.
  • It does not rewrite unsupported task notebooks into MLX-compatible notebooks.
  • It does not add UnslothTrainingArguments descriptions to notebooks that did not already have config-related explanatory text.
  • It does not replace unrelated torch usage such as inference tensors, generation snippets, or examples that still need PyTorch on CUDA/ROCm.
  • It keeps unsupported non-text custom-collator task notebooks on their existing trainer path until the corresponding MLX task support exists.

Documentation Link Updates

Some notebooks had markdown text changed from SFTTrainer to UnslothTrainer. Those descriptions no longer point to TRL SFTTrainer docs.

  • Migrated trainer descriptions now point at the Unsloth fine-tuning guide instead of TRL SFT docs.
  • The earlier accidental continued-pretraining docs target is not used.
  • Remaining TRL SFT links are completion-masking / response-only-training notes where the link still describes the underlying TRL masking concept, not the public trainer class imported by the notebook.

Notebooks Touched

The diff updates 258 notebook files across:

  • nb/
  • kaggle/
  • original_template/

Representative migrated notebook families include supported Gemma/Gemma2/Gemma3/Gemma4 text and chat notebooks, Llama, Mistral, Qwen, Phi, AMD/Kaggle variants, Synthetic text notebooks, verified Vision/VL notebooks, packing=True TinyLlama variants, and text-only DataCollatorForSeq2Seq conversational/hackathon variants.

Verified Vision/VL families included in this PR:

  • Gemma 3 / Gemma 3N / Gemma 4 Vision
  • Llama 3.2 Vision
  • Ministral 3 VL
  • Pixtral
  • Qwen2-VL / Qwen2.5-VL / Qwen3-VL

Unsupported or uncertain task/model families remain unchanged, including Audio/OCR/TTS task notebooks, GRPO/RL notebooks, QAT/deployment notebooks, custom trainer subclass notebooks, stale alternate Falcon templates, ERNIE VL, Paddle OCR Vision, Qwen3.5 Vision variants, and LFM2.5-VL.

LFM2.5-VL is intentionally excluded because the current MLX compile qualification reports lfm2_vl as fallback-only / not training-qualified, with compile blockers around Python data extraction and ragged control flow.

Non-Goals / Deferred Work

  • Generic custom data_collator execution on MLX remains deferred in the runtime PRs. The 44 Vision/VL notebooks are included because UnslothVisionDataCollator is a supported backend-aware compatibility path that routes processor/image batches into MLXTrainer.
  • Text-only DataCollatorForSeq2Seq notebooks are included in the public API surface so they do not need another notebook migration when the backend support lands.
  • packing=True runtime behavior on MLX remains backend-owned. This PR only routes packing notebooks through the public Unsloth trainer API.
  • Unsupported MLX task families remain excluded rather than being partially converted.
  • The notebooks PR depends on the main unsloth PR and zoo PR for runtime behavior; by itself it only changes notebook source.

Real Gemma3 Notebook Parity Run

I ran a real supported Gemma3 SFT notebook path from nb/Gemma3_(270M).ipynb on three variants. The runner follows the notebook cells for model load, LoRA setup, Gemma3 chat template, Thytu/ChessInstruct formatting, and train_on_responses_only; the only training change is max_steps=100 -> 10 so the PR can record a short curve. No model weights over 5GB were downloaded.

Run configuration:

  • Model: unsloth/gemma-3-270m-it
  • Dataset: Thytu/ChessInstruct, train[:10000]
  • Filtering after response masking: 665 all-masked rows removed on both CUDA and MLX, leaving 9,335 usable examples
  • Sequence length: 2048
  • LoRA: r=128, lora_alpha=128, target modules q_proj/k_proj/v_proj/o_proj/gate_proj/up_proj/down_proj
  • Batch: per_device_train_batch_size=4, gradient_accumulation_steps=1, effective batch size 4
  • Scheduler: warmup_steps=5, learning_rate=5e-5, lr_scheduler_type="linear"
  • Optimizer argument: optim="adamw_8bit"
  • Seed: 3407
  • CUDA hardware: RTX 4070 SUPER, Torch 2.11.0+cu128, Transformers 5.5.0, TRL 0.24.0
  • MLX hardware: Apple Silicon / MLX, mlx-lm 0.31.3, Transformers 5.5.0, TRL 0.24.0

Variant commits:

  • CUDA main reference: unslothai/unsloth@45f060899e56d8a8d218529733075814148f6b19 + unslothai/unsloth-zoo@28063c2a28b148b32390476dc86d2940395416af, using notebook-main trl.SFTTrainer / SFTConfig
  • CUDA branch: Lyxot/unsloth@60385efb06d236202a8adc2540a0210935f94511 + Lyxot/unsloth-zoo@6685266e567511fd2e582ab677834dc0345a62b7, using branch UnslothTrainer / UnslothTrainingArguments
  • MLX branch retest: Lyxot/unsloth@1040ccccf0905df2ae4f237fb6953bfad1715827 + Lyxot/unsloth-zoo@6685266e567511fd2e582ab677834dc0345a62b7, using branch UnslothTrainer / UnslothTrainingArguments

Results:

Step CUDA main loss CUDA main grad CUDA branch loss CUDA branch grad MLX branch loss MLX branch grad
1 4.1118 97.9610 4.1118 97.9610 4.0985 91.4525
2 3.9207 95.0271 3.9207 95.0271 3.9070 91.9407
3 3.3180 78.2600 3.3180 78.2600 3.3060 77.0045
4 1.9192 53.1489 1.9192 53.1489 1.8891 52.4635
5 1.2203 23.5901 1.2203 23.5901 1.1846 25.1157
6 0.8083 9.4253 0.8083 9.4253 0.8041 9.2907
7 0.6018 7.3403 0.6018 7.3403 0.5780 6.5590
8 0.6660 7.1101 0.6660 7.1101 0.6714 6.1685
9 0.5907 6.9775 0.5907 6.9775 0.5859 6.7463
10 0.8521 8.8184 0.8521 8.8184 0.8565 8.8934

Curve summary:

  • CUDA branch vs CUDA main: max absolute loss delta 0.0; max absolute grad-norm delta 0.0.
  • MLX branch vs CUDA main: same curve shape with max absolute loss delta 0.0357; mean absolute loss delta 0.0147; final loss 0.8565 vs CUDA 0.8521; final grad norm 8.8934 vs CUDA 8.8184.
  • MLX retest trained tokens: 481, matching the previous 30-step parity run's first 10 steps after removing the extra EOS token.
  • Average train loss: CUDA main 1.8009, CUDA branch 1.8009, MLX branch 1.7881.

Notebook Audit / Validation

Notebook-specific audits performed for this branch:

  • Audited all 258 modified notebooks as valid JSON.
  • Checked modified notebooks for remaining SFTTrainer / SFTConfig imports in the migration scope.
  • Checked modified notebooks for stale CUDA memory-helper usage after replacing memory-stat cells.
  • Checked modified notebooks for stale import torch introduced only by the old memory-stat cells, including kaggle/Gemma4_(31B)-Text.ipynb.
  • Checked modified markdown so UnslothTrainer descriptions do not still link to TRL SFTTrainer docs or the continued-pretraining docs.
  • Checked the Synthetic set: 10 Synthetic-named text notebooks remain in the migration scope.
  • Checked the broadened text-collator set: 18 modified DataCollatorForSeq2Seq notebooks, with Orpheus TTS collator notebooks intentionally left unchanged.
  • Checked the packing set: 4 modified packing=True TinyLlama notebooks.
  • Checked the Vision/VL set: 44 modified notebooks using UnslothVisionDataCollator.
  • Checked MLX compile qualification for included VLM architecture families: gemma3, gemma3n, gemma4, mllama, mistral3, pixtral, qwen2_vl, qwen2_5_vl, and qwen3_vl are training-qualified.
  • Checked that lfm2_vl is not training-qualified and that LFM2.5-VL notebooks remain unchanged.
  • Checked that unsupported Audio/OCR/TTS/GRPO/QAT/deployment/custom-subclass notebooks are not touched by the latest expansion.
  • Checked that modified notebooks no longer read trainer_stats.metrics[...] directly where the final stats cell needs backend-compatible metrics access.
  • Verified representative Hugging Face public config metadata for included VLM model repos.
  • Ran the real Gemma3_(270M).ipynb path above against CUDA main, CUDA branch, and MLX branch.

The CUDA branch result is exactly identical to CUDA main for 10 logged training steps, which is the key regression check for existing NVIDIA behavior after replacing notebook imports with UnslothTrainer.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates multiple Jupyter Notebooks to migrate from Hugging Face TRL's SFTTrainer and SFTConfig to Unsloth's native UnslothTrainer and UnslothTrainingArguments. Additionally, it replaces manual PyTorch CUDA memory tracking and cache clearing with Unsloth's utility functions get_gpu_memory_stats and clear_gpu_memory, and removes redundant import torch statements. There are no review comments, so I have no feedback to provide.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

@Lyxot
Lyxot force-pushed the mlx/trainer-api branch from 8b6e0b5 to 60b3dec Compare June 19, 2026 07:12
@Lyxot
Lyxot marked this pull request as ready for review June 19, 2026 07:59
@Lyxot
Lyxot requested a review from danielhanchen as a code owner June 19, 2026 07:59
Copilot AI review requested due to automatic review settings June 19, 2026 07:59
@Lyxot

Lyxot commented Jun 19, 2026

Copy link
Copy Markdown
Author

@codex review

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

Migrates supported SFT-style notebooks to use the backend-aware public Unsloth trainer API (UnslothTrainer / UnslothTrainingArguments) so notebooks run without manual trainer import changes across CUDA/ROCm/XPU and MLX.

Changes:

  • Replace TRL SFTTrainer / SFTConfig usage with UnslothTrainer / UnslothTrainingArguments across supported notebooks.
  • Replace CUDA-only memory stat cells with get_gpu_memory_stats() and CUDA-only cleanup calls with clear_gpu_memory().
  • Remove torch imports where they were only used for the replaced CUDA memory helpers.

Reviewed changes

Copilot reviewed 258 out of 258 changed files in this pull request and generated no comments.

Show a summary per file
File Description
original_template/gpt_oss_(20B)_500K_Context_Fine_tuning.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
original_template/gpt-oss-(20B)-Fine-tuning.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
original_template/gpt-oss-(120B)_A100-Fine-tuning.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
original_template/Qwen3_(4B)-Thinking.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
original_template/Qwen3_(4B)-Instruct.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
original_template/Qwen3_(14B).ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
original_template/Qwen3_(14B)-Reasoning-Conversational.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
original_template/Qwen2_(7B)-Alpaca.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
original_template/Meta_Synthetic_Data_Llama3_2_(3B).ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
original_template/Magistral_(24B)-Reasoning-Conversational.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
original_template/Llama3_(8B)-Alpaca.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
original_template/Llama3.2_(1B)-RAFT.ipynb Switch trainer class to UnslothTrainer; replace CUDA memory stats with get_gpu_memory_stats()
original_template/Granite4.0_350M.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
original_template/Granite4.0.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
original_template/Gemma4_(12B)_Vision.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
original_template/Gemma4_(12B)_Text.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
original_template/Gemma3_(4B).ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
original_template/Gemma3_(27B)_A100-Conversational.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
original_template/Gemma3_(270M).ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
original_template/Gemma3N_(4B)-Conversational.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
original_template/Gemma2_(9B)-Alpaca.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
original_template/Gemma2_(2B)-Alpaca.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
original_template/Falcon_H1_(0.5B)-Alpaca.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
original_template/CodeGemma_(7B)-Conversational.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/gpt_oss_(20B)_500K_Context_Fine_tuning.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/gpt-oss-(20B)-Fine-tuning.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/gpt-oss-(120B)_A100-Fine-tuning.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/TinyQwen3_MoE.ipynb Switch trainer/config imports to Unsloth API; replace CUDA cache clear with clear_gpu_memory()
nb/Qwen3_VL_(8B)-Vision.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Qwen3_MoE.ipynb Switch trainer/config imports to Unsloth API; replace CUDA cache clear with clear_gpu_memory()
nb/Qwen3_(4B)-Thinking.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Qwen3_(4B)-Instruct.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Qwen3_(14B).ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Qwen3_(14B)-Reasoning-Conversational.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Meta_Synthetic_Data_Llama3_2_(3B).ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Magistral_(24B)-Reasoning-Conversational.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Llama3_(8B)-Alpaca.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Llama3.2_(1B)-RAFT.ipynb Switch trainer class to UnslothTrainer; replace CUDA memory stats with get_gpu_memory_stats()
nb/Kaggle-gpt_oss_(20B)_500K_Context_Fine_tuning.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Kaggle-gpt-oss-(20B)-Fine-tuning.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Kaggle-gpt-oss-(120B)_A100-Fine-tuning.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Kaggle-Qwen3_(4B)-Thinking.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Kaggle-Qwen3_(4B)-Instruct.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Kaggle-Qwen3_(14B).ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Kaggle-Magistral_(24B)-Reasoning-Conversational.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Kaggle-Llama3_(8B)-Alpaca.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Kaggle-Llama3.2_(1B)-RAFT.ipynb Switch trainer class to UnslothTrainer; replace CUDA memory stats with get_gpu_memory_stats()
nb/Kaggle-Granite4.0_350M.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Kaggle-Granite4.0.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Kaggle-Gemma4_(12B)_Vision.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Kaggle-Gemma4_(12B)_Text.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Kaggle-Gemma3_(4B).ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Kaggle-Gemma3_(27B)_A100-Conversational.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Kaggle-Gemma3_(270M).ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Kaggle-Gemma3N_(4B)-Conversational.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Kaggle-Gemma2_(9B)-Alpaca.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Kaggle-Gemma2_(2B)-Alpaca.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Kaggle-CodeGemma_(7B)-Conversational.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Granite4.0_350M.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Granite4.0.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Gemma4_(E4B)-Vision.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Gemma4_(E4B)-Text.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Gemma4_(E2B)-Vision.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Gemma4_(E2B)-Text.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Gemma4_(31B)-Vision.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Gemma4_(31B)-Text.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Gemma4_(26B_A4B)-Vision.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Gemma4_(26B_A4B)-Text.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Gemma4_(12B)_Vision.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Gemma4_(12B)_Text.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Gemma3_(4B).ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Gemma3_(27B)_A100-Conversational.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Gemma3_(270M).ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Gemma3N_(4B)-Conversational.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Gemma2_(9B)-Alpaca.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Gemma2_(2B)-Alpaca.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/GLM_Flash_A100(80GB).ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/FunctionGemma_(270M).ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/Falcon_H1_(0.5B)-Alpaca.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/CodeGemma_(7B)-Conversational.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/AMD-gpt_oss_(20B)_500K_Context_Fine_tuning.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/AMD-gpt-oss-(20B)-Fine-tuning.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/AMD-gpt-oss-(120B)_A100-Fine-tuning.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/AMD-TinyQwen3_MoE.ipynb Switch trainer/config imports to Unsloth API; replace CUDA cache clear with clear_gpu_memory()
nb/AMD-Qwen3_MoE.ipynb Switch trainer/config imports to Unsloth API; replace CUDA cache clear with clear_gpu_memory()
nb/AMD-Qwen3_(4B)-Thinking.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/AMD-Qwen3_(4B)-Instruct.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/AMD-Qwen3_(14B).ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/AMD-Magistral_(24B)-Reasoning-Conversational.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/AMD-Llama3_(8B)-Alpaca.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/AMD-Llama3.2_(1B)-RAFT.ipynb Switch trainer class to UnslothTrainer; replace CUDA memory stats with get_gpu_memory_stats()
nb/AMD-Granite4.0_350M.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/AMD-Granite4.0.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/AMD-Gemma4_(E4B)-Vision.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/AMD-Gemma4_(E4B)-Text.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/AMD-Gemma4_(E2B)-Vision.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/AMD-Gemma4_(E2B)-Text.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/AMD-Gemma4_(31B)-Vision.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/AMD-Gemma4_(31B)-Text.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/AMD-Gemma4_(26B_A4B)-Vision.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/AMD-Gemma4_(26B_A4B)-Text.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/AMD-Gemma4_(12B)_Vision.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/AMD-Gemma4_(12B)_Text.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/AMD-Gemma3_(4B).ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/AMD-Gemma3_(27B)_A100-Conversational.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/AMD-Gemma3_(270M).ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/AMD-Gemma3N_(4B)-Conversational.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/AMD-Gemma2_(9B)-Alpaca.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/AMD-Gemma2_(2B)-Alpaca.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/AMD-GLM_Flash_A100(80GB).ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/AMD-FunctionGemma_(270M).ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/AMD-Falcon_H1_(0.5B)-Alpaca.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
nb/AMD-CodeGemma_(7B)-Conversational.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
kaggle/Gemma4_(E4B)-Text.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
kaggle/Gemma4_(31B)-Text.ipynb Switch trainer/config imports to Unsloth API; replace CUDA memory stats with get_gpu_memory_stats()
Comments suppressed due to low confidence (3)

original_template/gpt-oss-(20B)-Fine-tuning.ipynb:1

  • Using positional indexing into get_gpu_memory_stats() is brittle and harder to read. Consider unpacking the returned tuple instead (e.g., assign used_memory via unpacking) so the notebook remains clear and resilient if the helper's return order ever changes.
    nb/gpt_oss_(20B)_500K_Context_Fine_tuning.ipynb:1
  • Same issue as other notebooks: avoid get_gpu_memory_stats()[1] and unpack the tuple to extract used_memory. This improves readability and reduces the chance of silently breaking if get_gpu_memory_stats() changes.
    nb/Qwen3_MoE.ipynb:1
  • The cleanup order is suboptimal: calling gc.collect() before clearing GPU caches is typically more effective because it frees Python-side references first. Consider reordering to import gc -> gc.collect() -> clear_gpu_memory() so clear_gpu_memory() can release memory from objects actually reclaimed by GC.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 60b3decb0d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread nb/Gemma3_(4B)-Vision.ipynb Resolved
Comment thread nb/Llama3.1_(8B)-Alpaca.ipynb Resolved
@Lyxot
Lyxot force-pushed the mlx/trainer-api branch from ecc5f5e to f6f7ccd Compare June 19, 2026 08:36
@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.

@danielhanchen

Copy link
Copy Markdown
Member

Reviewed and tested the migration on CUDA, and checked generator consistency.

Verified on GPU

  • Migrated text notebook (Llama3.2_(1B_and_3B)-Conversational) runs end to end: from unsloth import UnslothTrainer, UnslothTrainingArguments, get_chat_template, standardize_sharegpt, train_on_responses_only, get_gpu_memory_stats(), and the trainer_stats_metrics cell all work; loss decreases.
  • Migrated vision notebook (Qwen2_VL_(7B)-Vision, run with a small VL model) runs end to end with UnslothVisionDataCollator + UnslothTrainer.
  • The trainer-class swap is behaviorally identical to the old trl route on GPU (verified on unsloth#6462: bit-identical on gemma-3-270m, within run-to-run nondeterminism on Llama-1B).

One thing to fix before merge: python_scripts/ is stale

The PR migrates nb/ (200 files) and original_template/ (56 files) but does not regenerate python_scripts/. As a result 267 of 433 exported scripts still do from trl import SFTTrainer and only 10 use UnslothTrainer, so anyone running the exported scripts hits the old path.

Running python update_all_notebooks.py fixes this: it brings python_scripts/ to 200 UnslothTrainer / 72 SFTTrainer (the 72 being the intentionally-excluded GRPO/audio/etc families), and it does NOT revert the nb/ migration (update_unsloth_config early-returns on UnslothTrainingArguments).

Smaller notes

  • Re-running the generator also produces ~38 cosmetic nb/ diffs (cell ids / execution_count / source-array chunking; the rendered code is identical) plus a small README diff, i.e. the committed tree is not generator-clean. Regenerating and committing would normalize this.
  • The ~70 SFT notebooks still on from trl import SFTTrainer are the audio / TTS / OCR / GRPO / QAT / deployment / MoE / ERNIE-VL / Qwen3.5-VL / Falcon families listed as out of scope, so that looks intentional.

The public MLX trainer API these notebooks call was also validated on a real macos-14 runner: https://github.com/danielhanchen/unsloth-staging-2/actions/runs/27955271488

Net: the migration is correct and safe on CUDA; please regenerate python_scripts/ (and ideally the notebooks) via update_all_notebooks.py so the derived artifacts match. Companion PRs unsloth#6462 and unsloth-zoo#790 reviewed as well.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7202a148e9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Lyxot Lyxot closed this Jul 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants