Repository navigation
Conversation
| @@ -20,12 +20,43 @@ | |||
|
|
|||
|
|
|||
| class GptOssStateDictAdapter(MoEStateDictAdapter): | |||
There was a problem hiding this comment.
Maybe you haven't seen the issue -- how does this solution compare with option 2 at #4996 (comment)
cc @Zhuoxi2000
|
@claude review this PR |
|
Claude finished @tianyu-l's task in 2m 2s —— View job Review of PR #5064
Summary: The fix is correct and the layout table above the class constants is very helpful. I traced every path that reaches the adapter:
If the config flag doesn't match the checkpoint, the load should fail loudly rather than load bad weights. With I have one compatibility concern and a few minor points. 1. Old TorchTitan HF saves stop loading (please call this out)Before this PR,
Those files were never valid HF checkpoints, so breaking them is reasonable. Per the "protect battle-tested code paths" rule, though, please state the break in the PR description. Something like this would do: "GPT-OSS HF checkpoints saved by TorchTitan before this change use the 2. Expert weights are now saved as non-contiguous views (worth checking on a real save)
3. Minor points
4. On @tianyu-l's earlier question (option 2 in #4996)I couldn't open the issue comment from this environment, so I can't compare against option 2 directly. On its own terms, this PR's approach of choosing the name map from Verdict: The change looks good. My main requests are to document the break in (1) and run the real-save check in (2). I didn't run the tests or pre-commit, because this environment doesn't allow Python or |
Summary: An HF load calls `to_hf` on the destination state dict to get the keys to read. Some quantized checkpoints store a tensor under a different name than the unquantized checkpoint does: compressed-tensors MXFP4 (Kimi K3) stores `X.weight_packed` and `X.weight_scale` where the bf16 checkpoint has `X.weight`. The adapter has no way to tell which one is being read. This adds keyword-only `to_hf(state_dict, *, to_quantized_layout=False)` and `from_hf(hf_state_dict, *, is_quantized_layout=False)` to the state dict adapter protocol, and the DCP load passes its `from_quantized` to both. `to_quantized_layout` makes `to_hf` return the quantized checkpoint's key names; only the names change, and no values are quantized. The storage reader then fills those entries with dequantized tensors, so the dict `from_hf` receives is in the quantized checkpoint's layout without being quantized, and `is_quantized_layout` tells `from_hf` to read the names `to_hf` chose instead of inferring the format from the keys. These are per-call arguments rather than adapter state because the same adapter also converts state dicts for HF saves, RL weight sync, and the conversion scripts, which always want unquantized names. Every in-tree adapter accepts both arguments: - DeepSeek V4 and Kimi K2.7 wrap DeepSeek V3 and forward them. - DeepSeek V3 and GPT-OSS, which already load quantized checkpoints, ignore them for now. - Adapters that cannot load a quantized checkpoint (Flux, Llama 3, Qwen3, Qwen3.5, Muse Glimmer, Kimi K3 and the HF transformers backend) raise `ValueError`. Before, the base storage reader only logged a warning and read the checkpoint as unquantized. The `torch_checkpointing` HF load path is left alone for now. Test Plan: ``` pytest -q tests/unit_tests/cpu/components/checkpointer/test_checkpoint.py tests/unit_tests/cpu/models/test_state_dict_adapter.py tests/unit_tests/cpu/components/checkpointer/test_torch_checkpointing.py SKIP=lychee-link-checker pre-commit run --files <changed files> ``` 128 passed, and pre-commit passes. The new `test_quantized_hf_load_requests_quantized_names` checks that a quantized DCP load calls `to_hf(..., to_quantized_layout=True)` and `from_hf(..., is_quantized_layout=True)`. The new `test_adapters_without_quantized_loads_reject_quantized_layout` checks that the Flux, Llama 3 and Qwen3 adapters raise for both, and fails if the Flux check is removed. This change was written with the help of an AI assistant. --- [//]: # (BEGIN SAPLING FOOTER) Stack created with [Sapling](https://sapling-scm.com). Best reviewed with [ReviewStack](https://reviewstack.dev/pytorch/torchtitan/pull/5063). * #5337 * #5065 * #5064 * __->__ #5063
8cc5437 to
58e1425
Compare
Summary: With E experts, D = hidden dim, and F = expert hidden dim, TorchTitan stores the gate/up projections (D -> F) as `w13.weight [E, 2, F, D]` and the down projection (F -> D) as `w2.weight [E, D, F]`: output size first, like nn.Linear. `GptOssStateDictAdapter.to_hf` always emitted the MXFP4 names `gate_up_proj_blocks` and `down_proj_blocks` in that layout, which is what the quantized reader dequantizes to. That is only right for a quantized load. Unquantized GPT-OSS checkpoints (and HF transformers, which computes x @ W) name these `gate_up_proj [E, D, 2F]` and `down_proj [E, F, D]`, input size first; `unsloth/gpt-oss-20b-BF16` has `gate_up_proj [32, 2880, 5760]`. So a bf16 GPT-OSS checkpoint could not be loaded, and `last_save_in_hf` wrote tensors under MXFP4 names with the wrong layout. `to_hf` now uses the unquantized names and layout by default, and the `_blocks` names with the TorchTitan layout when `to_quantized_layout=True`. `from_hf` with `is_quantized_layout=True` reads only those names, so both directions share one name map (`from_hf_map` or `from_hf_map_quantized`). The biases have the same names in both formats. The released checkpoint's `model.safetensors.index.json` names the MXFP4 tensors, so `fqn_to_index_mapping` maps `X_blocks` to `X` and drops `X_scales`. HF saves keep the released file sharding. Test Plan: ``` pytest -q tests/unit_tests/cpu/models/test_state_dict_adapter.py tests/unit_tests/cpu/components/checkpointer/test_checkpoint.py SKIP=lychee-link-checker pre-commit run --files tests/unit_tests/cpu/models/test_state_dict_adapter.py torchtitan/models/gpt_oss/state_dict_adapter.py ``` 87 passed, and pre-commit passes. The GPT-OSS round trip now runs with the quantized layout off and on. It checks that the quantized expert weights are the transpose of the unquantized ones, and that `from_hf` with a mismatched flag leaves the expert weights out. A new test covers the index remapping. Expert names and shapes were checked against the `unsloth/gpt-oss-20b-BF16` safetensors headers. This change was written with the help of an AI assistant.
Summary:
With E experts, D = hidden dim, and F = expert hidden dim, TorchTitan stores
the gate/up projections (D -> F) as
w13.weight [E, 2, F, D]and the downprojection (F -> D) as
w2.weight [E, D, F]: output size first, likenn.Linear.
GptOssStateDictAdapter.to_hfalways emitted the MXFP4 namesgate_up_proj_blocksanddown_proj_blocksin that layout, which is what thequantized reader dequantizes to. That is only right for a quantized load.
Unquantized GPT-OSS checkpoints (and HF transformers, which computes x @ W)
name these
gate_up_proj [E, D, 2F]anddown_proj [E, F, D], input sizefirst;
unsloth/gpt-oss-20b-BF16hasgate_up_proj [32, 2880, 5760]. So abf16 GPT-OSS checkpoint could not be loaded, and
last_save_in_hfwrotetensors under MXFP4 names with the wrong layout.
to_hfnow uses the unquantized names and layout by default, and the_blocksnames with the TorchTitan layout whento_quantized_layout=True.from_hfwithis_quantized_layout=Truereads only those names, so bothdirections share one name map (
from_hf_maporfrom_hf_map_quantized).The biases have the same names in both formats.
The released checkpoint's
model.safetensors.index.jsonnames the MXFP4tensors, so
fqn_to_index_mappingmapsX_blockstoXand dropsX_scales. HF saves keep the released file sharding.Test Plan:
87 passed, and pre-commit passes. The GPT-OSS round trip now runs with
the quantized layout off and on. It checks that the quantized expert weights
are the transpose of the unquantized ones, and that
from_hfwith amismatched flag leaves the expert weights out.
A new test covers the index remapping. Expert names and shapes were checked
against the
unsloth/gpt-oss-20b-BF16safetensors headers.This change was written with the help of an AI assistant.
Stack created with Sapling. Best reviewed with ReviewStack.