Repository navigation
gpt-oss: read config.transformers_weights before the standard filenames when probing the expert layout - #15386
Merged
Merged
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Member
Author
|
@codex review |
|
Codex Review: Didn't find any major issues. Can't wait for the next one! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
gpt_oss_checkpoint_has_fused_expertsdecides between the fused and the split (pre-quantized) bitsandbytes expert layout by reading tensor names from the checkpoint._checkpoint_tensor_dtypesonly looked at the standard filenames (model.safetensors, its index,pytorch_model.bin, its index), but Transformers loadsconfig.transformers_weightsfirst whenever it is set, and never falls back to the standard names in that case:PreTrainedModel.from_pretrainedreadsgetattr(config, "transformers_weights", None)and_get_resolved_checkpoint_filesuses it ahead of every standard name for local directories and Hub repos (modeling_utils.pylines 918-921 and 1010-1012, call site at 4869)._get_resolved_checkpoint_files(modeling_utils.pylines 556-590 and 636-638, call site at 4259), plus a path traversal check added in Reject path traversal in thetransformers_weightsconfig field huggingface/transformers#46890.So a repo that has a split
model.safetensorsnext to a fusedchosen.safetensorswith"transformers_weights": "chosen.safetensors"was probed as split, which routes the load onto the split-expert classes and skips the fused-layout float16 refusal inloader.py. The reverse (fused standard file, split explicit file) was probed as fused.Change
_checkpoint_tensor_dtypestakestransformers_weights. When it names a*.safetensorsfile or a*.safetensors.index.jsonindex, only that file is read (no variant, nouse_safetensors, no.binfallback, no standard-name Hub metadata shortcut), matching Transformers.gpt_oss_checkpoint_has_fused_expertsforwardsconfig.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), andPretrainedConfig.save_pretraineddeletes 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_explicit_file_decides_the_layout[split_standard_fused_chosen]test_explicit_file_decides_the_layout[fused_standard_split_chosen]test_explicit_index_decides_the_layouttest_explicit_file_ignores_variant_and_use_safetensorstest_invalid_or_escaping_explicit_names_never_read_other_filestest_hub_probe_reads_the_explicit_fileThe 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.