Skip to content

fix(studio): load the MLX training dataset before the model - #16618

Merged
danielhanchen merged 1 commit into
unslothai:mainfrom
Lyxot:fix/mlx-dataset-before-model
Nov 4, 2026
Merged

danielhanchen merged 1 commit into
unslothai:mainfrom
Lyxot:fix/mlx-dataset-before-model

Conversation

@Lyxot

@Lyxot Lyxot commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator

Problem

On Apple Silicon, _run_mlx_training downloads and loads the model with FastMLXModel.from_pretrained before it touches the dataset. Any dataset error therefore only shows up after the full model download and load: a missing S3 manifest, an ambiguous audio reference, missing local files, or no dataset configured. With a large model that can take minutes, only to fail on something the dataset step could have reported immediately. The CUDA path (run_training_process) already loads the dataset before the model.

Change

  • The raw dataset fetch (Hugging Face via _load_hf_train_and_eval_datasets, local files, the S3 download, and local_eval_datasets) now runs right after the model security check, before from_pretrained. None of it reads the model or tokenizer.
  • If a stop is requested while the dataset loads, the run now ends with "Training cancelled" before the model load, the same event the S3 cancel path already sends. Without this check, a stop during the now-earlier dataset load would still start the model download.
  • Row bounding (_slice) and formatting stay after the model load: the max_steps row bound depends on whether the loaded model is a vision model, and formatting needs the tokenizer. _slice is now applied once after the fetch instead of in each loader branch.
  • The "Loading {model}..." status message moves to just before from_pretrained.

Errors raised while formatting rows still appear after the model load, since formatting needs the tokenizer. The CUDA path behaves the same way.

Validation

Two new tests in tests/test_mlx_dataset_format_failure.py call the real _run_mlx_training with MLX and unsloth_zoo.mlx replaced by stand-ins, so they also run off Apple Silicon:

  • test_mlx_dataset_error_surfaces_before_the_model_loads: an S3 download error is raised and from_pretrained is never called.
  • test_mlx_stop_during_dataset_load_skips_the_model_load: a stop set during the HF dataset load ends the run with "Training cancelled" and skips the model load.

Each test fails when the change it covers is reverted.

cd studio/backend
python -m pytest -q tests/test_mlx_dataset_format_failure.py tests/test_mlx_epoch_report.py tests/test_model_identity.py tests/test_training_finetune_targets_matrix.py tests/test_training_stop_watchdog.py
# 2400 passed

A dataset error on Apple Silicon (missing S3 manifest, ambiguous audio reference, missing local files) only surfaced after the full model download and load. Fetch the train and local eval datasets right after the model security check, matching the CUDA path's dataset-then-model order. Row bounding and formatting stay after the load since they need is_vlm and the tokenizer.
@Lyxot

Lyxot commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 4, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-04T06:02:15.438158Z eaa6fba Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: eaa6fbaa9b

ℹ️ 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 commented Nov 4, 2026

Copy link
Copy Markdown
Collaborator Author

Review and CI summary for eaa6fbaa9b:

  • Codex review: Didn't find any major issues on the first round, with no review threads opened.
  • CI: upstream jobs were stuck pending, so the same head and base ran on a staging PR in my fork ([staging] fix(studio): load the MLX training dataset before the model Lyxot/unsloth#218, now closed): 45 passed, 2 skipped. On the first run, two jobs failed for reasons unrelated to this change, and both passed when rerun:
    • Backend tests (Python 3.13, g-r): 1 of 35,375 tests failed, test_orchestrator_switch_waits.py::test_worker_crash_mid_frame_cannot_block_the_shutdown_deadline. It tests the inference orchestrator's shutdown deadline, which this PR doesn't touch, and it passes locally.
    • security suites (torch-dependent): the actions/checkout step failed before any tests ran.
  • Changes during review: none. The head is still the original commit eaa6fbaa9b.

danielhanchen added a commit that referenced this pull request Nov 4, 2026
…shards (#16634)

## Summary

`Repo tests (CPU, gloo-tp-pp)` is at its 2100 s cap. On #16618, which
only touches the Studio MLX worker, it was interrupted with the suite
almost done:

```
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! KeyboardInterrupt !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
174 passed, 7 skipped, 79 warnings, 320 subtests passed in 2088.59s (0:34:48)
##[error]Process completed with exit code 124.
```

The other recent runs of that job, across the last 20 PRs, took 30.5 to
36.2 minutes end to end, setup included, so a slightly slower runner is
enough to tip it over. When this shard was created (#15951 split, #16447
comment) tensor plus pipeline parallel were about 1397 s. Pipeline
parallel has grown a lot since then: every
`torch.distributed.pipelining` schedule (#15655), per-stage sharded
loading (#15656), no graph break on a non-last stage (#16167), and
explicit-unscale overflow agreement (#16417). Nothing is wrong with
those tests. The shard just outgrew its slot, the same way `gloo` did in
#15951 and #16447.

## Change

- `.github/workflows/studio-backend-ci.yml`: `gloo-tp-pp` becomes two
shards with the same settings (`-n 4`, the 2100 s cap, the marker
filter):
  - `gloo-tp`: `tests/test_tp_n*gloo*.py` (2 files);
  - `gloo-pp`: `tests/test_pp*gloo*.py` (8 files).
- `tests/studio/test_backend_ci_parallel_isolation.py`:
`test_the_command_scan_sees_the_parallel_run_and_the_serial_steps` pins
the number of parallel pytest runs. It asks to be told when a shard is
added, so its counts go from 12 to 13 parallel runs and from 9 to 10
repo-root shards.

The selections are the old one split in two, so every file still runs
exactly once.

## Testing

- `tests/test_ci_repo_cpu_shards.py`: 15 passed. With `test_pp*gloo*`
narrowed to `test_pp_[!n]*gloo*` as a sabotage,
`test_no_file_is_dropped_or_run_twice` fails, so a dropped file is still
caught.
- `tests/studio/test_backend_ci_parallel_isolation.py`,
`tests/test_ci_repo_cpu_shards.py` and the workflow and cache-budget
guards under `tests/studio`: 164 passed.
- The two new shards' run times on this PR's own CI are the real check.
Both should finish well under the cap.
@danielhanchen danielhanchen self-assigned this Nov 4, 2026
@danielhanchen
danielhanchen merged commit d63d056 into unslothai:main Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants