Skip to content

Studio: thread hf_token through load_and_format_dataset - #8924

Merged
oobabooga merged 6 commits into
unslothai:mainfrom
KrishPatel1905:fix/dataset-token-forwarding
Sep 3, 2026
Merged

oobabooga merged 6 commits into
unslothai:mainfrom
KrishPatel1905:fix/dataset-token-forwarding

Conversation

@KrishPatel1905

Copy link
Copy Markdown
Contributor

Problem

UnslothTrainer.load_and_format_dataset built its load_dataset and get_dataset_split_names kwargs with only path/name/revision, so a gated or private dataset was read under whatever HF_TOKEN the environment carried rather than the request's token. On a host whose env token differs from the request token, the metadata probes and the eventual load ran under the wrong identity.

The GPU worker (core/training/worker.py) and the CLI (unsloth_cli) both already resolve a token; it just was not forwarded.

Solution

  • Add an hf_token parameter to load_and_format_dataset and thread it into load_kwargs, eval_load_kwargs, and probe_kwargs. All eight remote load_dataset call sites and the split probe reuse those dicts, so one change covers the streaming and eager train/eval paths.
  • worker.py and unsloth_cli/commands/train.py pass their resolved token.
  • When hf_token is unset the kwargs are unchanged, so the existing HF_TOKEN environment fallback is untouched.

Tests

New tests/test_training_token_forwarding.py:

  • streaming path forwards the token to load_dataset (train + eval) and to get_dataset_split_names
  • eager path forwards the token
  • an absent token stays absent (env fallback preserved)

On main the two forwarding tests fail with "TypeError: load_and_format_dataset() got an unexpected keyword argument 'hf_token'"; with the fix all three pass.

Local verification: 3/3 new tests pass; test_training_preflight.py, test_audio_type_inconclusive.py, test_audio_decode_load_order.py, test_third_party_progress_bars.py pass in isolation. A 6-test cross-file ordering failure in test_audio_type_inconclusive.py reproduces identically on clean main on a host where the repo's own unsloth package is importable, so it is pre-existing and unrelated.

pre-commit (ruff + kwarg-spacing) passes on all changed files.

load_and_format_dataset built its load_dataset and get_dataset_split_names kwargs with only path/name/revision, so a gated or private dataset read under whatever HF_TOKEN the environment carried rather than the request's token. The GPU worker and CLI already resolve a token; forward it.

- add hf_token parameter to UnslothTrainer.load_and_format_dataset and thread it into load_kwargs / eval_load_kwargs / probe_kwargs (all eight remote load_dataset call sites plus the split probe reuse these dicts)
- worker.py and unsloth_cli/commands/train.py pass their resolved token
- tests: test_training_token_forwarding.py pins the forwarding on streaming and eager paths, and that an absent token stays absent

No behaviour change when hf_token is unset (kwargs unchanged).

@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: 8f5f8953ec

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

dataset_source = cfg.data.dataset or "",
format_type = cfg.data.format_type,
local_datasets = cfg.data.local_dataset,
hf_token = hf_token,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Keep the MLX trainer adapter call compatible

When unsloth train selects the MLX backend on Apple Silicon, _create_cli_trainer() returns _MLXTrainerAdapter, whose load_and_format_dataset() signature does not accept hf_token. Because this keyword is now passed unconditionally—even when its value is None—every MLX CLI training invocation raises TypeError here before the dataset is queued; either extend the adapter's compatible signature or avoid passing this argument to it.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Correct, and it was a hard TypeError: at 8f5f895 the adapter signature ended at require_exact_resume_resources with no **kwargs, while unsloth_cli/commands/train.py:142 passes hf_token unconditionally, so _create_cli_trainer returning create_mlx_trainer_adapter() on an MLX host would raise before the dataset was queued. Fixed in 98bbdf6: _MLXTrainerAdapter.load_and_format_dataset now accepts hf_token, and the MLX worker keeps reading the token from _model_config (training.py:727, merged into the worker config at :901 and read at :1626), so nothing is forwarded twice. Covered by test_mlx_adapter_accepts_dataset_hf_token in studio/backend/tests/test_training_cached_start.py.

dataset_local_path: Optional[str] = None,
dataset_revision: Optional[str] = None,
require_exact_resume_resources: bool = False,
hf_token: Optional[str] = None,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Forward the token through automatic eval detection

When evaluation is enabled for a non-streaming private/gated Hub dataset without an explicit eval_split, the new token is not passed to _auto_detect_eval_split_from_hf() at line 2976. That helper consequently calls both get_dataset_split_names() and the candidate load_dataset() without the request token; if ambient credentials differ or are absent, detection fails and silently falls back to holding out training rows instead of using the dataset's validation split, changing experiment results despite the authenticated training split loading successfully.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Confirmed: at 8f5f895 _auto_detect_eval_split_from_hf took revision but no token, so the eval-enabled non-streaming path with no explicit eval_split probed and loaded unauthenticated and fell back to holding out training rows. Fixed in ab8d816: the helper takes token and threads it into both get_dataset_split_names (trainer.py:3190) and the candidate load_dataset (:3212), with the call site passing token = hf_token (:2913). Pinned by test_auto_detect_eval_forwards_explicit_token.

The auto-detect path (eval enabled, non-streaming, no explicit eval_split) called _auto_detect_eval_split_from_hf, which built its own get_dataset_split_names and candidate load_dataset kwargs without the request token. Add a token parameter and thread it through both calls; load_and_format_dataset passes its hf_token.

Adds test_auto_detect_eval_forwards_explicit_token pinning the probe and candidate load carry the token.
@KrishPatel1905

Copy link
Copy Markdown
Contributor Author

Thanks for the review - confirmed and fixed in ab8d816.

The auto-detect path (eval enabled, non-streaming, no explicit eval_split) called _auto_detect_eval_split_from_hf, which built its own get_dataset_split_names and candidate load_dataset kwargs without the request token. It now takes a token parameter, threaded from load_and_format_dataset into both calls.

Coverage: new test_auto_detect_eval_forwards_explicit_token pins the probe and the candidate load carry the token (4/4 in the file pass). test_training_preflight.py (53) and the audio/load-order files (30) pass alongside it; pre-commit (ruff + kwarg spacing) is clean.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 3, 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-09-03T19:35:04.254411Z 4172a0a New commits
ℹ️ 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.

@oobabooga

Copy link
Copy Markdown
Member

Both review items check out against the code and are fixed on head, so I marked them agreed and replied on each thread with the commit that closed it.

I pushed f492c6e on top of your branch. It is comments and docstrings only, no code change: the test module docstring and the paragraph added to the load_and_format_dataset docstring were carrying bug narration that belongs in the PR description, and three comments restated the assertion directly under them. The huggingface_hub.get_token() citation, the __spec__ = None note and the MIN_EVAL_ROWS note are kept.

I also ran the repository's full CI on a mirror of this branch rather than waiting on the fork queue: 18 workflows pass, with the Kaggle T4 GPU job skipped as it always is without a GPU runner. That includes Backend CI, which is the run that matters here because it exercises the new test file in the studio.txt-only environment where the unsloth/trl stubbing is load bearing.

oobabooga added a commit to oobabooga/unsloth that referenced this pull request Sep 3, 2026
oobabooga added a commit to oobabooga/unsloth that referenced this pull request Sep 3, 2026
@oobabooga

Copy link
Copy Markdown
Member

Thanks. Training was reading Hugging Face datasets under whatever credential the host machine carries, not the token supplied with the request. That breaks private and gated datasets. Both review items were genuine and are fixed on head: the MLX adapter mirrors the trainer signature again, and the automatic eval-split detection now receives the token too.

I confirmed the token reaches the Hub request, not just the kwargs dict. I then tested it against a real gated dataset (meta-llama/Llama-3.1-8B-Instruct-evals) from a host with no HF credentials. Without a token the eval-split probe is refused by the gate and silently falls back to holding out training rows, the same on main and on this branch. With the request token it succeeds. When no token is supplied, the arguments passed to datasets are identical to main across 48 configurations covering the streaming, eager, auto-detect, local-file, cached-resume and slice paths. The worker config schema and the API surface are unchanged, so existing installs behave as before.

_MLXTrainerAdapter has to mirror the trainer signature exactly, because UnslothTrainer() returns the adapter on Apple Silicon. The first commit here added hf_token to the trainer only, which is the crash the first review round caught. On a real Apple Silicon machine that intermediate state raises TypeError: load_and_format_dataset() got an unexpected keyword argument 'hf_token', and head does not. Main is unaffected either way, since nothing on main passes the argument.

CI is green on the branch and the new tests pass on Linux, macOS arm64 and Windows. I did not exercise the S3 dataset path or run an actual MLX fine-tune, neither of which this change touches.

@oobabooga
oobabooga merged commit 789b25a into unslothai:main Sep 3, 2026
1 check passed
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.

3 participants