Skip to content

gpt-oss: read config.transformers_weights before the standard filenames when probing the expert layout - #15386

Merged
danielhanchen merged 3 commits into
mainfrom
dh/s7-rc-gptossweights
Oct 26, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
dh/s7-rc-gptossweights

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

gpt_oss_checkpoint_has_fused_experts decides between the fused and the split (pre-quantized) bitsandbytes expert layout by reading tensor names from the checkpoint. _checkpoint_tensor_dtypes only looked at the standard filenames (model.safetensors, its index, pytorch_model.bin, its index), but Transformers loads config.transformers_weights first whenever it is set, and never falls back to the standard names in that case:

  • transformers 4.57.6: PreTrainedModel.from_pretrained reads getattr(config, "transformers_weights", None) and _get_resolved_checkpoint_files uses it ahead of every standard name for local directories and Hub repos (modeling_utils.py lines 918-921 and 1010-1012, call site at 4869).
  • transformers main (5.x): same behaviour in _get_resolved_checkpoint_files (modeling_utils.py lines 556-590 and 636-638, call site at 4259), plus a path traversal check added in Reject path traversal in the transformers_weights config field huggingface/transformers#46890.

So a repo that has a split model.safetensors next to a fused chosen.safetensors with "transformers_weights": "chosen.safetensors" was probed as split, which routes the load onto the split-expert classes and skips the fused-layout float16 refusal in loader.py. The reverse (fused standard file, split explicit file) was probed as fused.

Change

  • _checkpoint_tensor_dtypes takes transformers_weights. When it names a *.safetensors file or a *.safetensors.index.json index, only that file is read (no variant, no use_safetensors, no .bin fallback, no standard-name Hub metadata shortcut), matching Transformers.
  • A name that escapes the model directory returns "unknown", since newer Transformers rejects it.
  • A name that is not a safetensors file or index keeps the existing order (Transformers raises on it at load anyway).
  • gpt_oss_checkpoint_has_fused_experts forwards config.transformers_weights. Configs without the field behave exactly as before.

Reachability

None of the public gpt-oss configs I checked set the field (openai/gpt-oss-20b, openai/gpt-oss-120b, unsloth/gpt-oss-20b-unsloth-bnb-4bit, unsloth/gpt-oss-20b-bnb-4bit, unsloth/gpt-oss-20b-BF16), and PretrainedConfig.save_pretrained deletes it before writing. Only a hand-edited config can carry it, so this is a low-severity consistency fix.

Tests

New tests/test_gpt_oss_transformers_weights_layout.py (CPU, tiny generated safetensors):

Test base (origin/main) this branch
test_explicit_file_decides_the_layout[split_standard_fused_chosen] FAIL PASS
test_explicit_file_decides_the_layout[fused_standard_split_chosen] FAIL PASS
test_explicit_index_decides_the_layout FAIL PASS
test_explicit_file_ignores_variant_and_use_safetensors FAIL PASS
test_invalid_or_escaping_explicit_names_never_read_other_files FAIL PASS
test_hub_probe_reads_the_explicit_file FAIL PASS

The existing gpt-oss layout suites (test_gpt_oss_bnb_checkpoint_layout.py, test_gpt_oss_bf16_load_in_4bit.py, test_gpt_oss_fused_4bit_checkpoint_roundtrip.py) still pass. I also checked the probe against Transformers' own _get_resolved_checkpoint_files (5.17.0) on both layouts: the probe now agrees with the file Transformers actually loads.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 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-10-26T14:37:54.760057Z 9ce376d 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.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 9ce376d38a

ℹ️ 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".

danielhanchen added a commit that referenced this pull request Oct 26, 2026
…ative Trainer runs back on the default (#15397)

## Problem

#15381 set `adam_epsilon = 1e-4` in the shared `_trainer_args` of
`tests/test_tp_native_gloo.py`. That fixed the three UNSLOTH_TP_SIZE
Trainer tests in `test_tp_env_gloo.py`, but on the CI runners it broke
the two native Trainer tests that use the same helper. Comparing the
`gloo` shard before and after (both on current-main bases):

| CI run | native
`test_trainer_trains_a_tensor_parallel_model_like_one_process[1-2]` /
`[2-2]` | `test_env_tp2_*` (3 tests) |
|---|---|---|
| #15284, base 975e864 (before #15381) | pass | fail |
| #15386, base 0be4c5d (after #15381) | fail on `lora_A` state (`rtol =
1e-4, atol = 1e-6`) | pass |

On this host both groups pass at both settings. The runners' CPUs round
differently from this machine, and I could not emulate theirs here:
`ATEN_CPU_CAPABILITY=avx2` aborts the spawned ranks with `buffer
overflow detected`. So the CI table above is the evidence for which eps
each group needs.

The two groups differ in their adapters. The env tests use
`init_lora_weights = False`, where every factor gets a real gradient
from step 1; that is where the default eps amplifies reduction-order
noise, as measured in #15381. The native tests use the default `B = 0`
start, where `lora_A`'s gradients are tiny for the first steps. There,
eps 1e-4 sits inside their range and moves `lora_A` differently on the
two arms.

## Fix

- `_trainer_args` goes back to AdamW's default eps, so the native
Trainer tests run as they did when they passed on CI.
- `test_tp_env_gloo.py` passes `adam_epsilon = 1e-4` only at its own
training call, with the measurement and the reason in a comment.

Tolerances are unchanged.

## Verification

- `tests/test_tp_env_gloo.py` and `tests/test_tp_native_gloo.py`: 52
passed on this host.
- This PR's own `Repo tests (CPU, gloo)` shard is the check on runner
hardware. Both groups should pass there.

## Not covered here

`test_tp2_x_fsdp2_dp2_matches_one_process[phi3-True-None]` also fails in
both CI runs above, by 6.9e-5 to 7.7e-5 relative on grad norms
(tolerance 1e-5), and passes on this host. It only became visible after
#15355: before that, the `phi3` cases errored on the removed tiny model.
It uses its own hand-written loop (eps 1e-6), not `_trainer_args`, so
this change does not touch it.
danielhanchen added a commit that referenced this pull request Oct 26, 2026
… shards (#15400)

## Problem


`tests/test_tp_native_gloo.py::test_tp2_x_fsdp2_dp2_matches_one_process[phi3-True-None]`
fails in `Repo tests (CPU, gloo)` on every recent run, on the per-step
grad norms (tolerance 1e-5):

| CI run | step 1 | step 2 | step 3 |
|---|---|---|---|
| #15284 (job 121590971980) | 1.5e-6 | 3.6e-5 | 7.7e-5 |
| #15386 (job 121637289351) | 1.5e-6 | 3.6e-5 | 6.9e-5 |

It only became visible after #15355. Before that, every `phi3` case
errored on the removed `trl-internal-testing/tiny-Phi3ForCausalLM`. On
this host the same test passes inside 1e-5.

## Product or test

The test. In the same run, the loss check (rel 1e-5) and the step-1
per-gradient check (`rtol = 1e-4, atol = 1e-6` on every LoRA gradient)
both pass. Only the multi-step norms drift, and the gap grows each step
from a 1.5e-6 start. That is reduction-order noise compounded by AdamW,
which phi3's fused qkv and gate_up shards make larger than llama's. The
runners' CPUs round differently from this host.

A real tensor-parallel error does not hide behind the norms. With the
backward all-reduce of the column-parallel input removed in
`tp_native.py` (`return grad, None`), the phi3 case fails on the step-1
per-gradient check, before the norms are compared.

## Fix

`_TP_FSDP2_NORM_REL = {"phi3": 2e-4}` sets the norm tolerance for phi3
only, with the CI measurements in a comment. Every other arch keeps
1e-5. The loss, per-gradient, mismatch and placement checks are
unchanged for all arches.

## Verification

- `test_tp2_x_fsdp2_dp2_matches_one_process`: all 5 parametrizations
pass.
- Sabotage: with the all-reduce removed, the phi3 case still fails.
- ruff format and check are clean.
[skip ci]
@danielhanchen
danielhanchen merged commit 029a596 into main Oct 26, 2026
@danielhanchen
danielhanchen deleted the dh/s7-rc-gptossweights branch October 26, 2026 23:53
danielhanchen added a commit that referenced this pull request Oct 30, 2026
…el dir (#16061)

Follow-up to #15386.

## Problem

#15386 checks `config.transformers_weights` for containment against a
synthetic `abspath("model_dir")`. An absolute name that points inside
the real local checkpoint directory (transformers 4.57.6 joins it
verbatim, 5.x accepts it after checking against the model dir plus
subfolder) was treated as escaping the directory. The layout probe then
returned unknown and a fused bnb 4-bit gpt-oss checkpoint was classified
as split, which loads the experts randomly. Only a hand-edited config
writes such a path.

## Change

Check local paths against the real directory plus subfolder, as
transformers does. A path outside the directory is still refused.

## Tests

`tests/test_gpt_oss_transformers_weights_layout.py`, CPU, torch 2.13,
transformers 5.17:

- main: 2 failed, 6 passed. Failing:
`test_absolute_explicit_name_inside_the_model_dir_decides_the_layout[None]`,
`[sub]`.
- this branch: 8 passed.

## Not covered

No end to end gpt-oss load; only the layout classification is checked.
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.

1 participant