Repository navigation
Conversation
PR Status: Approved 🚀Please fix all CI failures and trigger merge by commenting "@pytorchbot merge". ✅ AUTOMATED REVIEW - OUTDATED (earlier commit) - Ready for human review:
Findings (1): Reviewed commit: 🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/201291
Note: Links to docs will display an error until the docs builds have been completed. ✅ You can merge normally! (9 Unrelated Failures)As of commit b18e698 with merge base e5e5dde ( UNSTABLE - The following jobs are marked as unstable, possibly due to flakiness on trunk:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
@ivy-zhou has exported this pull request. If you are a Meta employee, you can view the originating Diff in D124826603. |
9e76956 to
8e76f80
Compare
…01291) Summary: Kimi K3 releases its routed experts in the compressed-tensors MXFP4 format: `X.weight_packed` is `[out, in/2]` uint8 with two E2M1 values per byte, and `X.weight_scale` is `[out, in/32]` uint8 E8M0 scales, one per 32 values. `QuantizedHuggingFaceStorageReader` handles FP8 (`weight_scale_inv`) and GPT-OSS MXFP4 (`_blocks`/`_scales`), but not this format. The encoding is the same as GPT-OSS's, and the bytes of a `[out, in/2]` tensor are laid out like one GPT-OSS expert, `[rows, groups, 16]`. So the reader reuses `_dequantize_tensor_mxfp4` with an expert dim of 1. Slice reads fetch whole 32-value groups and trim the result. Names follow the GPT-OSS convention: the reader reports `X.weight_packed` under its own name with the dequantized shape `[out, in]`, and a caller asks for `X.weight_packed` to get bf16 values back. TorchTitan's `to_hf(..., quantized=True)` requests these names. The alternative was to report the tensor as `X.weight`, so callers could use unquantized names, but that would make this the only format the reader renames. `read_metadata` also replaces the tensor's chunk list, because the load planner builds read items from `chunks`, not `size`. NVFP4 uses the same tensor names with one scale per 16 values, so a scale whose shape doesn't match MXFP4 raises a `ValueError` instead of decoding garbage. The DCPv2 resharder is not changed here. The fbcode and xplat copies are identical. This change was written with the help of an AI assistant. Test Plan: ``` cd fbcode buck2 test //caffe2/test/distributed/checkpoint:test_hf_safetensor_e2e //caffe2/test/distributed/checkpoint:test_quantized_hf_storage arc lint fbcode/caffe2/torch/distributed/checkpoint/quantized_hf_storage.py fbcode/caffe2/test/distributed/checkpoint/test_hf_safetensor_e2e.py xplat/caffe2/torch/distributed/checkpoint/quantized_hf_storage.py xplat/caffe2/test/distributed/checkpoint/test_hf_safetensor_e2e.py ``` Pass 10, Fail 2, Skip 4. The new `test_compressed_tensors_mxfp4_loading` passes. It dequantizes a synthetic `weight_packed`/`weight_scale` pair, including a scale stored in a different file and a slice read at rows 1:3, cols 40:90, and checks that an NVFP4-shaped scale raises. The two failures, `TestDTensorReshardPlacementChange.test_2d_to_2d_reshard_placement_change` and `TestDTensorReshardMeshChange.test_2d_to_1d_reshard_mesh_change`, use the plain `HuggingFaceStorageReader`/`HuggingFaceStorageWriter`, and fail the same way on master `b5c36964ae40` without this change (Pass 9, Fail 2, Skip 4). Lint reports no issues. Differential Revision: D124826603
…01291) Summary: Pull Request resolved: pytorch#201291 Kimi K3 releases its routed experts in the compressed-tensors MXFP4 format: `X.weight_packed` is `[out, in/2]` uint8 with two E2M1 values per byte, and `X.weight_scale` is `[out, in/32]` uint8 E8M0 scales, one per 32 values. `QuantizedHuggingFaceStorageReader` handles FP8 (`weight_scale_inv`) and GPT-OSS MXFP4 (`_blocks`/`_scales`), but not this format. The encoding is the same as GPT-OSS's, and the bytes of a `[out, in/2]` tensor are laid out like one GPT-OSS expert, `[rows, groups, 16]`. So the reader reuses `_dequantize_tensor_mxfp4` with an expert dim of 1. Slice reads fetch whole 32-value groups and trim the result. Names follow the GPT-OSS convention: the reader reports `X.weight_packed` under its own name with the dequantized shape `[out, in]`, and a caller asks for `X.weight_packed` to get bf16 values back. TorchTitan's `to_hf(..., quantized=True)` requests these names. The alternative was to report the tensor as `X.weight`, so callers could use unquantized names, but that would make this the only format the reader renames. `read_metadata` also replaces the tensor's chunk list, because the load planner builds read items from `chunks`, not `size`. NVFP4 uses the same tensor names with one scale per 16 values, so a scale whose shape doesn't match MXFP4 raises a `ValueError` instead of decoding garbage. The DCPv2 resharder is not changed here. The fbcode and xplat copies are identical. This change was written with the help of an AI assistant. Test Plan: ``` cd fbcode buck2 test //caffe2/test/distributed/checkpoint:test_hf_safetensor_e2e //caffe2/test/distributed/checkpoint:test_quantized_hf_storage arc lint fbcode/caffe2/torch/distributed/checkpoint/quantized_hf_storage.py fbcode/caffe2/test/distributed/checkpoint/test_hf_safetensor_e2e.py xplat/caffe2/torch/distributed/checkpoint/quantized_hf_storage.py xplat/caffe2/test/distributed/checkpoint/test_hf_safetensor_e2e.py ``` Pass 10, Fail 2, Skip 4. The new `test_compressed_tensors_mxfp4_loading` passes. It dequantizes a synthetic `weight_packed`/`weight_scale` pair, including a scale stored in a different file and a slice read at rows 1:3, cols 40:90, and checks that an NVFP4-shaped scale raises. The two failures, `TestDTensorReshardPlacementChange.test_2d_to_2d_reshard_placement_change` and `TestDTensorReshardMeshChange.test_2d_to_1d_reshard_mesh_change`, use the plain `HuggingFaceStorageReader`/`HuggingFaceStorageWriter`, and fail the same way on master `b5c36964ae40` without this change (Pass 9, Fail 2, Skip 4). Lint reports no issues. Differential Revision: D124826603
8e76f80 to
2237624
Compare
2237624 to
6f87d19
Compare
…01291) Summary: Kimi K3 releases its routed experts in the compressed-tensors MXFP4 format: `X.weight_packed` is `[out, in/2]` uint8 with two E2M1 values per byte, and `X.weight_scale` is `[out, in/32]` uint8 E8M0 scales, one per 32 values. `QuantizedHuggingFaceStorageReader` handles FP8 (`weight_scale_inv`) and GPT-OSS MXFP4 (`_blocks`/`_scales`), but not this format. The encoding is the same as GPT-OSS's, and the bytes of a `[out, in/2]` tensor are laid out like one GPT-OSS expert, `[rows, groups, 16]`. So the reader reuses `_dequantize_tensor_mxfp4` with an expert dim of 1. Slice reads fetch whole 32-value groups and trim the result. Names follow the GPT-OSS convention: the reader reports `X.weight_packed` under its own name with the dequantized shape `[out, in]`, and a caller asks for `X.weight_packed` to get bf16 values back. TorchTitan's `to_hf(..., quantized=True)` requests these names. The alternative was to report the tensor as `X.weight`, so callers could use unquantized names, but that would make this the only format the reader renames. `read_metadata` also replaces the tensor's chunk list, because the load planner builds read items from `chunks`, not `size`, and reports the dequantized dtype, because empty-state-dict loads allocate their destination from the metadata. Other compressed-tensors formats use the same `weight_packed`/`weight_scale` names: NVFP4 (`float8_e4m3fn` scales, one per 16 values) and int4 `pack-quantized` (int32 `[out, in/8]` with a float scale, which at `group_size=128` matches the MXFP4 scale shape). So a pair is dequantized only when `weight_packed` is 2D and a whole number of 16-byte groups wide, both tensors are uint8, and the scale is one per 32 values; any other pair (including 3D int4 MoE weights) loads raw, as it did before this change. The DCPv2 resharder is not changed here. The fbcode and xplat copies are identical. This change was written with the help of an AI assistant. Test Plan: ``` cd fbcode buck2 test //caffe2/test/distributed/checkpoint:test_hf_safetensor_e2e //caffe2/test/distributed/checkpoint:test_quantized_hf_storage arc lint fbcode/caffe2/torch/distributed/checkpoint/quantized_hf_storage.py fbcode/caffe2/test/distributed/checkpoint/test_hf_safetensor_e2e.py xplat/caffe2/torch/distributed/checkpoint/quantized_hf_storage.py xplat/caffe2/test/distributed/checkpoint/test_hf_safetensor_e2e.py ``` Pass 10, Fail 0, Skip 6. The new `test_compressed_tensors_mxfp4_loading` passes. It dequantizes a synthetic `weight_packed`/`weight_scale` pair through `dist_cp.load`, including a scale stored in a different file and a slice read at rows 1:3, cols 40:90. It loads the checkpoint into an empty state dict with `_load_state_dict_from_keys` and checks the MXFP4 tensor comes back as float32 with the expected values; without the dtype fix it comes back as `torch.uint8`. (It loads the whole checkpoint: `_load_state_dict_from_keys` with explicit keys reads `metadata.planner_data`, which HF readers never set, so key filtering fails for any HF reader.) It then `dist_cp.load`s five non-MXFP4 pairs into tensors of their stored shape and dtype and checks the bytes round-trip: NVFP4 (`float8_e4m3fn` scale), a uint8 scale that is not one per 32 values, a 40-byte width that is not a whole number of 16-byte groups, int4 `pack-quantized` (int32 `[out, in/8]`, bf16 scale per 128 values), and 3D int4 MoE weights. Before the rank/alignment check, the 40-byte case was misclassified as MXFP4 (`Size mismatch between saved torch.Size([4, 80]) and current: torch.Size([4, 40])`), and a 3D `weight_packed` aborted `read_metadata` on the 2D unpack. The six skipped tests are the multi-GPU DTensor and consolidation tests, which skip on this ASAN build because it disables CUDA. On an earlier version with CUDA available, two of them, `TestDTensorReshardPlacementChange.test_2d_to_2d_reshard_placement_change` and `TestDTensorReshardMeshChange.test_2d_to_1d_reshard_mesh_change`, use the plain `HuggingFaceStorageReader`/`HuggingFaceStorageWriter` and failed the same way on master `b5c36964ae40` without this change. `arc lint -e extra` reports no issues, and pyrefly reports no errors in `quantized_hf_storage.py`. Differential Revision: D124826603
…01291) Summary: Kimi K3 releases its routed experts in the compressed-tensors MXFP4 format: `X.weight_packed` is `[out, in/2]` uint8 with two E2M1 values per byte, and `X.weight_scale` is `[out, in/32]` uint8 E8M0 scales, one per 32 values. `QuantizedHuggingFaceStorageReader` handles FP8 (`weight_scale_inv`) and GPT-OSS MXFP4 (`_blocks`/`_scales`), but not this format. The encoding is the same as GPT-OSS's, and the bytes of a `[out, in/2]` tensor are laid out like one GPT-OSS expert, `[rows, groups, 16]`. So the reader reuses `_dequantize_tensor_mxfp4` with an expert dim of 1. Slice reads fetch whole 32-value groups and trim the result. Names follow the GPT-OSS convention: the reader reports `X.weight_packed` under its own name with the dequantized shape `[out, in]`, and a caller asks for `X.weight_packed` to get bf16 values back. TorchTitan's `to_hf(..., quantized=True)` requests these names. The alternative was to report the tensor as `X.weight`, so callers could use unquantized names, but that would make this the only format the reader renames. `read_metadata` also replaces the tensor's chunk list, because the load planner builds read items from `chunks`, not `size`, and reports the dequantized dtype, because empty-state-dict loads allocate their destination from the metadata. Other compressed-tensors formats use the same `weight_packed`/`weight_scale` names: NVFP4 (`float8_e4m3fn` scales, one per 16 values) and int4 `pack-quantized` (int32 `[out, in/8]` with a float scale, which at `group_size=128` matches the MXFP4 scale shape). So a pair is dequantized only when `weight_packed` is 2D and a whole number of 16-byte groups wide, both tensors are uint8, and the scale is one per 32 values; any other pair (including 3D int4 MoE weights) loads raw, as it did before this change. The DCPv2 resharder is not changed here. The fbcode and xplat copies are identical. This change was written with the help of an AI assistant. Test Plan: ``` cd fbcode buck2 test //caffe2/test/distributed/checkpoint:test_hf_safetensor_e2e //caffe2/test/distributed/checkpoint:test_quantized_hf_storage arc lint fbcode/caffe2/torch/distributed/checkpoint/quantized_hf_storage.py fbcode/caffe2/test/distributed/checkpoint/test_hf_safetensor_e2e.py xplat/caffe2/torch/distributed/checkpoint/quantized_hf_storage.py xplat/caffe2/test/distributed/checkpoint/test_hf_safetensor_e2e.py ``` Pass 10, Fail 0, Skip 6. The new `test_compressed_tensors_mxfp4_loading` passes. It dequantizes a synthetic `weight_packed`/`weight_scale` pair through `dist_cp.load`, including a scale stored in a different file and a slice read at rows 1:3, cols 40:90. It loads the checkpoint into an empty state dict with `_load_state_dict_from_keys` and checks the MXFP4 tensor comes back as float32 with the expected values; without the dtype fix it comes back as `torch.uint8`. (It loads the whole checkpoint: `_load_state_dict_from_keys` with explicit keys reads `metadata.planner_data`, which HF readers never set, so key filtering fails for any HF reader.) It then `dist_cp.load`s five non-MXFP4 pairs into tensors of their stored shape and dtype and checks the bytes round-trip: NVFP4 (`float8_e4m3fn` scale), a uint8 scale that is not one per 32 values, a 40-byte width that is not a whole number of 16-byte groups, int4 `pack-quantized` (int32 `[out, in/8]`, bf16 scale per 128 values), and 3D int4 MoE weights. Before the rank/alignment check, the 40-byte case was misclassified as MXFP4 (`Size mismatch between saved torch.Size([4, 80]) and current: torch.Size([4, 40])`), and a 3D `weight_packed` aborted `read_metadata` on the 2D unpack. The six skipped tests are the multi-GPU DTensor and consolidation tests, which skip on this ASAN build because it disables CUDA. On an earlier version with CUDA available, two of them, `TestDTensorReshardPlacementChange.test_2d_to_2d_reshard_placement_change` and `TestDTensorReshardMeshChange.test_2d_to_1d_reshard_mesh_change`, use the plain `HuggingFaceStorageReader`/`HuggingFaceStorageWriter` and failed the same way on master `b5c36964ae40` without this change. `arc lint -e extra` reports no issues, and pyrefly reports no errors in `quantized_hf_storage.py`. Differential Revision: D124826603
6f87d19 to
e4ebd7c
Compare
e4ebd7c to
e043d06
Compare
…01291) Summary: Kimi K3 releases its routed experts in the compressed-tensors MXFP4 format: `X.weight_packed` is `[out, in/2]` uint8 with two E2M1 values per byte, and `X.weight_scale` is `[out, in/32]` uint8 E8M0 scales, one per 32 values. `QuantizedHuggingFaceStorageReader` handles FP8 (`weight_scale_inv`) and GPT-OSS MXFP4 (`_blocks`/`_scales`), but not this format. The encoding is the same as GPT-OSS's, and the bytes of a `[out, in/2]` tensor are laid out like one GPT-OSS expert, `[rows, groups, 16]`. So the reader reuses `_dequantize_tensor_mxfp4` with an expert dim of 1. Slice reads fetch whole 32-value groups and trim the result. Names follow the GPT-OSS convention: the reader reports `X.weight_packed` under its own name with the dequantized shape `[out, in]`, and a caller asks for `X.weight_packed` to get bf16 values back. TorchTitan's `to_hf(..., quantized=True)` requests these names. The alternative was to report the tensor as `X.weight`, so callers could use unquantized names, but that would make this the only format the reader renames. `read_metadata` also replaces the tensor's chunk list, because the load planner builds read items from `chunks`, not `size`, and reports the dequantized dtype, because empty-state-dict loads allocate their destination from the metadata. Other compressed-tensors formats use the same `weight_packed`/`weight_scale` names: NVFP4 (`float8_e4m3fn` scales, one per 16 values) and int4 `pack-quantized` (int32 `[out, in/8]` with a float scale, which at `group_size=128` matches the MXFP4 scale shape). So a pair is dequantized only when `weight_packed` is 2D and a whole number of 16-byte groups wide, both tensors are uint8, and the scale is one per 32 values; any other pair (including 3D int4 MoE weights) loads raw, as it did before this change. The DCPv2 resharder is not changed here. The fbcode and xplat copies are identical. This change was written with the help of an AI assistant. Test Plan: ``` cd fbcode buck2 test //caffe2/test/distributed/checkpoint:test_hf_safetensor_e2e //caffe2/test/distributed/checkpoint:test_quantized_hf_storage arc lint fbcode/caffe2/torch/distributed/checkpoint/quantized_hf_storage.py fbcode/caffe2/test/distributed/checkpoint/test_hf_safetensor_e2e.py xplat/caffe2/torch/distributed/checkpoint/quantized_hf_storage.py xplat/caffe2/test/distributed/checkpoint/test_hf_safetensor_e2e.py ``` Pass 11, Fail 0, Skip 6. The new `test_compressed_tensors_mxfp4_loading` passes. It dequantizes a synthetic `weight_packed`/`weight_scale` pair through `dist_cp.load`, including a scale stored in a different file and a slice read at rows 1:3, cols 40:90, and loads it again with `target_dtype=torch.bfloat16`, checking exact bf16 values. The new `TestCompressedTensorsMXFP4ShardedLoad.test_shard_starts_mid_group` (CPU/gloo, 4 ranks, so it runs here) loads a 96-column tensor into a `Shard(1)` DTensor, so each rank's 24 columns start partway into a 32-value group and the planner's overlap computation runs on the rewritten chunk; without the chunk rewrite it fails with 163 / 384 elements mismatched. It loads the checkpoint into an empty state dict with `_load_state_dict_from_keys` and checks the MXFP4 tensor comes back as float32 with the expected values; without the dtype fix it comes back as `torch.uint8`. (It loads the whole checkpoint: `_load_state_dict_from_keys` with explicit keys reads `metadata.planner_data`, which HF readers never set, so key filtering fails for any HF reader.) It then `dist_cp.load`s five non-MXFP4 pairs into tensors of their stored shape and dtype and checks the bytes round-trip: NVFP4 (`float8_e4m3fn` scale), a uint8 scale that is not one per 32 values, a 40-byte width that is not a whole number of 16-byte groups, int4 `pack-quantized` (int32 `[out, in/8]`, bf16 scale per 128 values), and 3D int4 MoE weights. Before the rank/alignment check, the 40-byte case was misclassified as MXFP4 (`Size mismatch between saved torch.Size([4, 80]) and current: torch.Size([4, 40])`), and a 3D `weight_packed` aborted `read_metadata` on the 2D unpack. The six skipped tests are the multi-GPU DTensor and consolidation tests, which skip on this ASAN build because it disables CUDA. On an earlier version with CUDA available, two of them, `TestDTensorReshardPlacementChange.test_2d_to_2d_reshard_placement_change` and `TestDTensorReshardMeshChange.test_2d_to_1d_reshard_mesh_change`, use the plain `HuggingFaceStorageReader`/`HuggingFaceStorageWriter` and failed the same way on master `b5c36964ae40` without this change. `arc lint -e extra` reports no issues, and pyrefly reports no errors in `quantized_hf_storage.py`. Differential Revision: D124826603
…01291) Summary: Pull Request resolved: pytorch#201291 Kimi K3 releases its routed experts in the compressed-tensors MXFP4 format: `X.weight_packed` is `[out, in/2]` uint8 with two E2M1 values per byte, and `X.weight_scale` is `[out, in/32]` uint8 E8M0 scales, one per 32 values. `QuantizedHuggingFaceStorageReader` handles FP8 (`weight_scale_inv`) and GPT-OSS MXFP4 (`_blocks`/`_scales`), but not this format. The encoding is the same as GPT-OSS's, and the bytes of a `[out, in/2]` tensor are laid out like one GPT-OSS expert, `[rows, groups, 16]`. So the reader reuses `_dequantize_tensor_mxfp4` with an expert dim of 1. Slice reads fetch whole 32-value groups and trim the result. Names follow the GPT-OSS convention: the reader reports `X.weight_packed` under its own name with the dequantized shape `[out, in]`, and a caller asks for `X.weight_packed` to get bf16 values back. TorchTitan's `to_hf(..., quantized=True)` requests these names. The alternative was to report the tensor as `X.weight`, so callers could use unquantized names, but that would make this the only format the reader renames. `read_metadata` also replaces the tensor's chunk list, because the load planner builds read items from `chunks`, not `size`, and reports the dequantized dtype, because empty-state-dict loads allocate their destination from the metadata. Other compressed-tensors formats use the same `weight_packed`/`weight_scale` names: NVFP4 (`float8_e4m3fn` scales, one per 16 values) and int4 `pack-quantized` (int32 `[out, in/8]` with a float scale, which at `group_size=128` matches the MXFP4 scale shape). So a pair is dequantized only when `weight_packed` is 2D and a whole number of 16-byte groups wide, both tensors are uint8, and the scale is one per 32 values; any other pair (including 3D int4 MoE weights) loads raw, as it did before this change. The DCPv2 resharder is not changed here. The fbcode and xplat copies are identical. This change was written with the help of an AI assistant. Test Plan: ``` cd fbcode buck2 test //caffe2/test/distributed/checkpoint:test_hf_safetensor_e2e //caffe2/test/distributed/checkpoint:test_quantized_hf_storage arc lint fbcode/caffe2/torch/distributed/checkpoint/quantized_hf_storage.py fbcode/caffe2/test/distributed/checkpoint/test_hf_safetensor_e2e.py xplat/caffe2/torch/distributed/checkpoint/quantized_hf_storage.py xplat/caffe2/test/distributed/checkpoint/test_hf_safetensor_e2e.py ``` Pass 11, Fail 0, Skip 6. The new `test_compressed_tensors_mxfp4_loading` passes. It dequantizes a synthetic `weight_packed`/`weight_scale` pair through `dist_cp.load`, including a scale stored in a different file and a slice read at rows 1:3, cols 40:90, and loads it again with `target_dtype=torch.bfloat16`, checking exact bf16 values. The new `TestCompressedTensorsMXFP4ShardedLoad.test_shard_starts_mid_group` (CPU/gloo, 4 ranks, so it runs here) loads a 96-column tensor into a `Shard(1)` DTensor, so each rank's 24 columns start partway into a 32-value group and the planner's overlap computation runs on the rewritten chunk; without the chunk rewrite it fails with 163 / 384 elements mismatched. It loads the checkpoint into an empty state dict with `_load_state_dict_from_keys` and checks the MXFP4 tensor comes back as float32 with the expected values; without the dtype fix it comes back as `torch.uint8`. (It loads the whole checkpoint: `_load_state_dict_from_keys` with explicit keys reads `metadata.planner_data`, which HF readers never set, so key filtering fails for any HF reader.) It then `dist_cp.load`s five non-MXFP4 pairs into tensors of their stored shape and dtype and checks the bytes round-trip: NVFP4 (`float8_e4m3fn` scale), a uint8 scale that is not one per 32 values, a 40-byte width that is not a whole number of 16-byte groups, int4 `pack-quantized` (int32 `[out, in/8]`, bf16 scale per 128 values), and 3D int4 MoE weights. Before the rank/alignment check, the 40-byte case was misclassified as MXFP4 (`Size mismatch between saved torch.Size([4, 80]) and current: torch.Size([4, 40])`), and a 3D `weight_packed` aborted `read_metadata` on the 2D unpack. The six skipped tests are the multi-GPU DTensor and consolidation tests, which skip on this ASAN build because it disables CUDA. On an earlier version with CUDA available, two of them, `TestDTensorReshardPlacementChange.test_2d_to_2d_reshard_placement_change` and `TestDTensorReshardMeshChange.test_2d_to_1d_reshard_mesh_change`, use the plain `HuggingFaceStorageReader`/`HuggingFaceStorageWriter` and failed the same way on master `b5c36964ae40` without this change. `arc lint -e extra` reports no issues, and pyrefly reports no errors in `quantized_hf_storage.py`. Differential Revision: D124826603
e043d06 to
b25e9d0
Compare
b25e9d0 to
e4b63b8
Compare
…01291) Summary: Kimi K3 releases its routed experts in the compressed-tensors MXFP4 format: `X.weight_packed` is `[out, in/2]` uint8 with two E2M1 values per byte, and `X.weight_scale` is `[out, in/32]` uint8 E8M0 scales, one per 32 values. `QuantizedHuggingFaceStorageReader` handles FP8 (`weight_scale_inv`) and GPT-OSS MXFP4 (`_blocks`/`_scales`), but not this format. The encoding is the same as GPT-OSS's, and the bytes of a `[out, in/2]` tensor are laid out like one GPT-OSS expert, `[rows, groups, 16]`. So the reader reuses `_dequantize_tensor_mxfp4` with an expert dim of 1. Slice reads fetch whole 32-value groups and trim the result. Names follow the GPT-OSS convention: the reader reports `X.weight_packed` under its own name with the dequantized shape `[out, in]`, and a caller asks for `X.weight_packed` to get bf16 values back. TorchTitan's `to_hf(..., quantized=True)` requests these names. The alternative was to report the tensor as `X.weight`, so callers could use unquantized names, but that would make this the only format the reader renames. `read_metadata` also replaces the tensor's chunk list, because the load planner builds read items from `chunks`, not `size`, and reports the dequantized dtype, because empty-state-dict loads allocate their destination from the metadata. Other compressed-tensors formats use the same `weight_packed`/`weight_scale` names: NVFP4 (`float8_e4m3fn` scales, one per 16 values) and int4 `pack-quantized` (int32 `[out, in/8]` with a float scale, which at `group_size=128` matches the MXFP4 scale shape). So a pair is dequantized only when `weight_packed` is 2D and a whole number of 16-byte groups wide, both tensors are uint8, and the scale is one per 32 values; any other pair (including 3D int4 MoE weights) loads raw, as it did before this change. The DCPv2 resharder is not changed here. The fbcode and xplat copies are identical. This change was written with the help of an AI assistant. Test Plan: ``` cd fbcode buck2 test //caffe2/test/distributed/checkpoint:test_hf_safetensor_e2e //caffe2/test/distributed/checkpoint:test_quantized_hf_storage arc lint fbcode/caffe2/torch/distributed/checkpoint/quantized_hf_storage.py fbcode/caffe2/test/distributed/checkpoint/test_hf_safetensor_e2e.py fbcode/caffe2/test/distributed/checkpoint/test_quantized_hf_storage.py xplat/caffe2/torch/distributed/checkpoint/quantized_hf_storage.py xplat/caffe2/test/distributed/checkpoint/test_hf_safetensor_e2e.py xplat/caffe2/test/distributed/checkpoint/test_quantized_hf_storage.py ``` Pass 17, Fail 0, Skip 6. - `test_compressed_tensors_mxfp4_loading` dequantizes a synthetic `weight_packed`/`weight_scale` pair through `dist_cp.load`, with the scale stored in a different file, and loads it again with `target_dtype=torch.bfloat16`, checking exact bf16 values. It then loads the checkpoint into an empty state dict with `_load_state_dict_from_keys` and checks the MXFP4 tensor comes back as float32 with the expected values; without the dtype fix it comes back as `torch.uint8`. (It loads the whole checkpoint: `_load_state_dict_from_keys` with explicit keys reads `metadata.planner_data`, which HF readers never set, so key filtering fails for any HF reader.) - `test_compressed_tensors_non_mxfp4_loads_raw` is parametrized over five non-MXFP4 pairs, each in its own checkpoint, and checks the bytes round-trip through `dist_cp.load` into tensors of their stored shape and dtype: NVFP4 (`float8_e4m3fn` scale), a uint8 scale that is not one per 32 values, a 40-byte width that is not a whole number of 16-byte groups, int4 `pack-quantized` (int32 `[out, in/8]`, bf16 scale per 128 values), and 3D int4 MoE weights. Before the rank/alignment check, the 40-byte case was misclassified as MXFP4 (`Size mismatch between saved torch.Size([4, 80]) and current: torch.Size([4, 40])`), and a 3D `weight_packed` aborted `read_metadata` on the 2D unpack. - `TestQuantizedHfStorage.test_compressed_tensors_mxfp4_slice_read` builds a read item for rows 1:3, cols 40:90 with the planner's `create_read_items_for_chunk_list` from the reader's own metadata and checks `_process_read_request` commits exactly that slice. - `TestCompressedTensorsMXFP4ShardedLoad.test_shard_starts_mid_group` (CPU/gloo, so it runs here) loads a 160-column tensor into a `Shard(1)` DTensor. Every shard boundary falls mid-group for 2, 3 or 4 ranks, so the planner's overlap computation runs on the rewritten chunk; it skips with fewer than 2 ranks. With the earlier 96-column version, removing the chunk rewrite made it fail with 163 / 384 elements mismatched. The six skipped tests are the multi-GPU DTensor and consolidation tests, which skip on this ASAN build because it disables CUDA. On an earlier version with CUDA available, two of them, `TestDTensorReshardPlacementChange.test_2d_to_2d_reshard_placement_change` and `TestDTensorReshardMeshChange.test_2d_to_1d_reshard_mesh_change`, use the plain `HuggingFaceStorageReader`/`HuggingFaceStorageWriter` and failed the same way on master `b5c36964ae40` without this change. `arc lint` reports no issues. The GitHub PYFMT lint (isort 6.0.1, usort 1.1.3, ruff 0.14.4, run through `tools/linter/adapters/pyfmt_linter.py`) reports no changes to the lines this diff adds. pyrefly reports no new errors in `quantized_hf_storage.py` or the two test files. The fbcode and xplat copies are identical. Differential Revision: D124826603
…01291) Summary: Kimi K3 releases its routed experts in the compressed-tensors MXFP4 format: `X.weight_packed` is `[out, in/2]` uint8 with two E2M1 values per byte, and `X.weight_scale` is `[out, in/32]` uint8 E8M0 scales, one per 32 values. `QuantizedHuggingFaceStorageReader` handles FP8 (`weight_scale_inv`) and GPT-OSS MXFP4 (`_blocks`/`_scales`), but not this format. The encoding is the same as GPT-OSS's, and the bytes of a `[out, in/2]` tensor are laid out like one GPT-OSS expert, `[rows, groups, 16]`. So the reader reuses `_dequantize_tensor_mxfp4` with an expert dim of 1. Slice reads fetch whole 32-value groups and trim the result. Names follow the GPT-OSS convention: the reader reports `X.weight_packed` under its own name with the dequantized shape `[out, in]`, and a caller asks for `X.weight_packed` to get bf16 values back. TorchTitan's `to_hf(..., quantized=True)` requests these names. The alternative was to report the tensor as `X.weight`, so callers could use unquantized names, but that would make this the only format the reader renames. `read_metadata` also replaces the tensor's chunk list, because the load planner builds read items from `chunks`, not `size`, and reports the dequantized dtype, because empty-state-dict loads allocate their destination from the metadata. Other compressed-tensors formats use the same `weight_packed`/`weight_scale` names: NVFP4 (`float8_e4m3fn` scales, one per 16 values) and int4 `pack-quantized` (int32 `[out, in/8]` with a float scale, which at `group_size=128` matches the MXFP4 scale shape). So a pair is dequantized only when `weight_packed` is 2D and a whole number of 16-byte groups wide, both tensors are uint8, and the scale is one per 32 values; any other pair (including 3D int4 MoE weights) loads raw, as it did before this change. The DCPv2 resharder is not changed here. The fbcode and xplat copies are identical. This change was written with the help of an AI assistant. Test Plan: ``` cd fbcode buck2 test //caffe2/test/distributed/checkpoint:test_hf_safetensor_e2e //caffe2/test/distributed/checkpoint:test_quantized_hf_storage arc lint fbcode/caffe2/torch/distributed/checkpoint/quantized_hf_storage.py fbcode/caffe2/torch/testing/_internal/distributed/checkpoint_utils.py xplat/caffe2/torch/testing/_internal/distributed/checkpoint_utils.py fbcode/caffe2/test/distributed/checkpoint/test_hf_safetensor_e2e.py fbcode/caffe2/test/distributed/checkpoint/test_quantized_hf_storage.py xplat/caffe2/torch/distributed/checkpoint/quantized_hf_storage.py xplat/caffe2/test/distributed/checkpoint/test_hf_safetensor_e2e.py xplat/caffe2/test/distributed/checkpoint/test_quantized_hf_storage.py ``` Pass 17, Fail 0, Skip 6. - `test_compressed_tensors_mxfp4_loading` dequantizes a synthetic `weight_packed`/`weight_scale` pair through `dist_cp.load`, with the scale stored in a different file, and loads it again with `target_dtype=torch.bfloat16`, checking exact bf16 values. It then loads the checkpoint into an empty state dict with `_load_state_dict_from_keys` and checks the MXFP4 tensor comes back as float32 with the expected values; without the dtype fix it comes back as `torch.uint8`. (It loads the whole checkpoint: `_load_state_dict_from_keys` with explicit keys reads `metadata.planner_data`, which HF readers never set, so key filtering fails for any HF reader.) - `test_compressed_tensors_non_mxfp4_loads_raw` is parametrized over five non-MXFP4 pairs, each in its own checkpoint, and checks the bytes round-trip through `dist_cp.load` into tensors of their stored shape and dtype: NVFP4 (`float8_e4m3fn` scale), a uint8 scale that is not one per 32 values, a 40-byte width that is not a whole number of 16-byte groups, int4 `pack-quantized` (int32 `[out, in/8]`, bf16 scale per 128 values), and 3D int4 MoE weights. Before the rank/alignment check, the 40-byte case was misclassified as MXFP4 (`Size mismatch between saved torch.Size([4, 80]) and current: torch.Size([4, 40])`), and a 3D `weight_packed` aborted `read_metadata` on the 2D unpack. - `TestQuantizedHfStorage.test_compressed_tensors_mxfp4_slice_read` builds a read item for rows 1:3, cols 40:90 with the planner's `create_read_items_for_chunk_list` from the reader's own metadata and checks `_process_read_request` commits exactly that slice. - `TestCompressedTensorsMXFP4ShardedLoad.test_shard_starts_mid_group` (CPU/gloo, so it runs here) loads a 160-column tensor into a `Shard(1)` DTensor. Every shard boundary falls mid-group for 2, 3 or 4 ranks, so the planner's overlap computation runs on the rewritten chunk; it skips with fewer than 2 ranks. With the earlier 96-column version, removing the chunk rewrite made it fail with 163 / 384 elements mismatched. The six skipped tests are the multi-GPU DTensor and consolidation tests, which skip on this ASAN build because it disables CUDA. On an earlier version with CUDA available, two of them, `TestDTensorReshardPlacementChange.test_2d_to_2d_reshard_placement_change` and `TestDTensorReshardMeshChange.test_2d_to_1d_reshard_mesh_change`, use the plain `HuggingFaceStorageReader`/`HuggingFaceStorageWriter` and failed the same way on master `b5c36964ae40` without this change. `arc lint` reports no issues. The GitHub PYFMT lint (isort 6.0.1, usort 1.1.3, ruff 0.14.4, run through `tools/linter/adapters/pyfmt_linter.py`) reports no changes to the lines this diff adds. pyrefly reports no new errors in `quantized_hf_storage.py`, the two test files, or `checkpoint_utils.py`, which now holds the MXFP4 reference encoder and checkpoint writer the tests share. The fbcode and xplat copies are identical. Differential Revision: D124826603
e4b63b8 to
b73d922
Compare
Withdrawn: the automated review of 1eb9eb8 found nothing blocking.
…01291) Summary: Pull Request resolved: pytorch#201291 Kimi K3 releases its routed experts in the compressed-tensors MXFP4 format: `X.weight_packed` is `[out, in/2]` uint8 with two E2M1 values per byte, and `X.weight_scale` is `[out, in/32]` uint8 E8M0 scales, one per 32 values. `QuantizedHuggingFaceStorageReader` handles FP8 (`weight_scale_inv`) and GPT-OSS MXFP4 (`_blocks`/`_scales`), but not this format. The encoding is the same as GPT-OSS's, and the bytes of a `[out, in/2]` tensor are laid out like one GPT-OSS expert, `[rows, groups, 16]`. So the reader reuses `_dequantize_tensor_mxfp4` with an expert dim of 1. Slice reads fetch whole 32-value groups and trim the result. Names follow the GPT-OSS convention: the reader reports `X.weight_packed` under its own name with the dequantized shape `[out, in]`, and a caller asks for `X.weight_packed` to get bf16 values back. TorchTitan's `to_hf(..., quantized=True)` requests these names. The alternative was to report the tensor as `X.weight`, so callers could use unquantized names, but that would make this the only format the reader renames. `read_metadata` also replaces the tensor's chunk list, because the load planner builds read items from `chunks`, not `size`, and reports the dequantized dtype, because empty-state-dict loads allocate their destination from the metadata. Other compressed-tensors formats use the same `weight_packed`/`weight_scale` names: NVFP4 (`float8_e4m3fn` scales, one per 16 values) and int4 `pack-quantized` (int32 `[out, in/8]` with a float scale, which at `group_size=128` matches the MXFP4 scale shape). So a pair is dequantized only when `weight_packed` is 2D and a whole number of 16-byte groups wide, both tensors are uint8, and the scale is one per 32 values; any other pair (including 3D int4 MoE weights) loads raw, as it did before this change. The DCPv2 resharder is not changed here. The fbcode and xplat copies are identical. This change was written with the help of an AI assistant. Test Plan: ``` cd fbcode buck2 test //caffe2/test/distributed/checkpoint:test_hf_safetensor_e2e //caffe2/test/distributed/checkpoint:test_quantized_hf_storage arc lint fbcode/caffe2/torch/distributed/checkpoint/quantized_hf_storage.py fbcode/caffe2/torch/testing/_internal/distributed/checkpoint_utils.py xplat/caffe2/torch/testing/_internal/distributed/checkpoint_utils.py fbcode/caffe2/test/distributed/checkpoint/test_hf_safetensor_e2e.py fbcode/caffe2/test/distributed/checkpoint/test_quantized_hf_storage.py xplat/caffe2/torch/distributed/checkpoint/quantized_hf_storage.py xplat/caffe2/test/distributed/checkpoint/test_hf_safetensor_e2e.py xplat/caffe2/test/distributed/checkpoint/test_quantized_hf_storage.py ``` Pass 19, Fail 0, Skip 6. - `test_compressed_tensors_mxfp4_loading` dequantizes a synthetic `weight_packed`/`weight_scale` pair through `dist_cp.load`, with the scale stored in a different file, and loads it again with `target_dtype=torch.bfloat16`, checking exact bf16 values. It then loads the checkpoint into an empty state dict with `_load_state_dict_from_keys`, once with the default `target_dtype` and once with `torch.bfloat16`, and checks the MXFP4 tensor comes back in that dtype with exactly the expected values; without the dtype fix it comes back as `torch.uint8`. (It loads the whole checkpoint: `_load_state_dict_from_keys` with explicit keys reads `metadata.planner_data`, which HF readers never set, so key filtering fails for any HF reader.) - `test_compressed_tensors_non_mxfp4_loads_raw` is parametrized over seven non-MXFP4 pairs, each in its own checkpoint, and checks the bytes round-trip through `dist_cp.load` into tensors of their stored shape and dtype: NVFP4 (`float8_e4m3fn` scale), MXFP4 shapes with an int8 `weight_packed`, MXFP4 shapes with a `float8_e4m3fn` scale, a uint8 scale that is not one per 32 values, a 40-byte width that is not a whole number of 16-byte groups, int4 `pack-quantized` (int32 `[out, in/8]`, bf16 scale per 128 values), and 3D int4 MoE weights. Before the rank/alignment check, the 40-byte case was misclassified as MXFP4 (`Size mismatch between saved torch.Size([4, 80]) and current: torch.Size([4, 40])`), and a 3D `weight_packed` aborted `read_metadata` on the 2D unpack. - `TestQuantizedHfStorage.test_compressed_tensors_mxfp4_slice_read` builds a read item for rows 1:3, cols 40:90 with the planner's `create_read_items_for_chunk_list` from the reader's own metadata and checks `_process_read_request` commits exactly that slice. - `TestCompressedTensorsMXFP4ShardedLoad.test_shard_starts_mid_group` (CPU/gloo, so it runs here) loads a 160-column tensor into a `Shard(1)` DTensor. Every shard boundary falls mid-group for 2, 3 or 4 ranks (`DTensorTestBase` uses 4, or the GPU count on 2- or 3-GPU hosts), so the planner's overlap computation runs on the rewritten chunk. With the earlier 96-column version, removing the chunk rewrite made it fail with 163 / 384 elements mismatched. To check the new cases catch regressions, I temporarily removed both uint8 checks from `is_mxfp4` and hardcoded `torch.float32` in place of `self.target_dtype`: `test_compressed_tensors_mxfp4_loading` and the `packed_not_uint8`, `scale_not_uint8` and `int4` cases failed. The six skipped tests are the multi-GPU DTensor and consolidation tests, which skip on this ASAN build because it disables CUDA. On an earlier version with CUDA available, two of them, `TestDTensorReshardPlacementChange.test_2d_to_2d_reshard_placement_change` and `TestDTensorReshardMeshChange.test_2d_to_1d_reshard_mesh_change`, use the plain `HuggingFaceStorageReader`/`HuggingFaceStorageWriter` and failed the same way on master `b5c36964ae40` without this change. `arc lint` reports no issues. The GitHub PYFMT lint (isort 6.0.1, usort 1.1.3, ruff 0.14.4, run through `tools/linter/adapters/pyfmt_linter.py`) reports no changes to the lines this diff adds. pyrefly reports no new errors in `quantized_hf_storage.py`, the two test files, or `checkpoint_utils.py`, which now holds the MXFP4 reference encoder and checkpoint writer the tests share. The fbcode and xplat copies are identical. Differential Revision: D124826603
1eb9eb8 to
bdc4c37
Compare
There was a problem hiding this comment.
Automated review of bdc4c37: nothing blocking; ready for human review.
This PR teaches QuantizedHuggingFaceStorageReader to dequantize compressed-tensors MXFP4 X.weight_packed / X.weight_scale pairs (Kimi K3 format) by rewriting the tensor's size, dtype and chunks in read_metadata and reusing _dequantize_tensor_mxfp4 with an expert dim of 1. Recommendation: Needs Discussion. The implementation and tests look correct, and the only blocker is an undocumented change in behavior that needs a release-note entry and a check of downstream callers that load these checkpoints raw today.
⚪ 1 non-blocking finding
Existing checkpoints now load differently, with no release note (also :114-124). Before: the mapping never picked up .weight_scale, so a compressed-tensors MXFP4 checkpoint loaded raw through this reader. read_metadata reported X.weight_packed as uint8 \[out, in/2\], and _load_state_dict_from_keys returned those bytes. After: the same checkpoint reports X.weight_packed as target_dtype \[out, in\]. Who breaks: a caller that pre-allocates uint8 \[out, in/2\] destinations (for example, to dequantize the weights itself) now fails with the size-mismatch AssertionError. A caller that
This review is AI-generated and advisory. Each new automated review replaces the previous one.
|
@claude review this PR |
|
Claude finished @fegin's task in 1m 12s —— View job The PR is close to approvable. There are no blocking findings, and one low-confidence question about the scale dtype is worth a quick check before landing. PR Review: #201291 (AI-generated, Claude Code)SummaryThe PR teaches I checked the decoding against the compressed-tensors packing: low nibble first, the same E2M1 lookup table, and E8M0 scales with bias 127. I also checked the detection gate ( Code Quality
RecommendationApprove. Nothing blocks. Confirm the on-disk scale dtype of the target checkpoints (first bullet) before landing. The BC change, where existing |
…01291) Summary: Kimi K3 releases its routed experts in the compressed-tensors MXFP4 format: `X.weight_packed` is `[out, in/2]` uint8 with two E2M1 values per byte, and `X.weight_scale` is `[out, in/32]` uint8 E8M0 scales, one per 32 values. `QuantizedHuggingFaceStorageReader` handles FP8 (`weight_scale_inv`) and GPT-OSS MXFP4 (`_blocks`/`_scales`), but not this format. The encoding is the same as GPT-OSS's, and the bytes of a `[out, in/2]` tensor are laid out like one GPT-OSS expert, `[rows, groups, 16]`. So the reader reuses `_dequantize_tensor_mxfp4` with an expert dim of 1. Slice reads fetch whole 32-value groups and trim the result. Names follow the GPT-OSS convention: the reader reports `X.weight_packed` under its own name with the dequantized shape `[out, in]`, and a caller asks for `X.weight_packed` to get bf16 values back. TorchTitan's `to_hf(..., quantized=True)` requests these names. The alternative was to report the tensor as `X.weight`, so callers could use unquantized names, but that would make this the only format the reader renames. `read_metadata` also replaces the tensor's chunk list, because the load planner builds read items from `chunks`, not `size`, and reports the dequantized dtype, because empty-state-dict loads allocate their destination from the metadata. Other compressed-tensors formats use the same `weight_packed`/`weight_scale` names: NVFP4 (`float8_e4m3fn` scales, one per 16 values) and int4 `pack-quantized` (int32 `[out, in/8]` with a float scale, which at `group_size=128` matches the MXFP4 scale shape). So a pair is dequantized only when `weight_packed` is 2D and a whole number of 16-byte groups wide, both tensors are uint8, and the scale is one per 32 values; any other pair (including 3D int4 MoE weights) loads raw, as it did before this change. The DCPv2 resharder is not changed here. The fbcode and xplat copies are identical. This change was written with the help of an AI assistant. Test Plan: ``` cd fbcode buck2 test //caffe2/test/distributed/checkpoint:test_hf_safetensor_e2e //caffe2/test/distributed/checkpoint:test_quantized_hf_storage arc lint fbcode/caffe2/torch/distributed/checkpoint/quantized_hf_storage.py fbcode/caffe2/torch/testing/_internal/distributed/checkpoint_utils.py xplat/caffe2/torch/testing/_internal/distributed/checkpoint_utils.py fbcode/caffe2/test/distributed/checkpoint/test_hf_safetensor_e2e.py fbcode/caffe2/test/distributed/checkpoint/test_quantized_hf_storage.py xplat/caffe2/torch/distributed/checkpoint/quantized_hf_storage.py xplat/caffe2/test/distributed/checkpoint/test_hf_safetensor_e2e.py xplat/caffe2/test/distributed/checkpoint/test_quantized_hf_storage.py ``` Pass 19, Fail 0, Skip 6. - `test_compressed_tensors_mxfp4_loading` dequantizes a synthetic `weight_packed`/`weight_scale` pair through `dist_cp.load`, with the scale stored in a different file, and loads it again with `target_dtype=torch.bfloat16`, checking exact bf16 values. It then loads the checkpoint into an empty state dict with `_load_state_dict_from_keys`, once with the default `target_dtype` and once with `torch.bfloat16`, and checks the MXFP4 tensor comes back in that dtype with exactly the expected values; without the dtype fix it comes back as `torch.uint8`. (It loads the whole checkpoint: `_load_state_dict_from_keys` with explicit keys reads `metadata.planner_data`, which HF readers never set, so key filtering fails for any HF reader.) - `test_compressed_tensors_non_mxfp4_loads_raw` is parametrized over seven non-MXFP4 pairs, each in its own checkpoint, and checks the bytes round-trip through `dist_cp.load` into tensors of their stored shape and dtype: NVFP4 (`float8_e4m3fn` scale), MXFP4 shapes with an int8 `weight_packed`, MXFP4 shapes with a `float8_e4m3fn` scale, a uint8 scale that is not one per 32 values, a 40-byte width that is not a whole number of 16-byte groups, int4 `pack-quantized` (int32 `[out, in/8]`, bf16 scale per 128 values), and 3D int4 MoE weights. Before the rank/alignment check, the 40-byte case was misclassified as MXFP4 (`Size mismatch between saved torch.Size([4, 80]) and current: torch.Size([4, 40])`), and a 3D `weight_packed` aborted `read_metadata` on the 2D unpack. - `TestQuantizedHfStorage.test_compressed_tensors_mxfp4_slice_read` builds a read item for rows 1:3, cols 40:90 with the planner's `create_read_items_for_chunk_list` from the reader's own metadata and checks `_process_read_request` commits exactly that slice. - `TestCompressedTensorsMXFP4ShardedLoad.test_shard_starts_mid_group` (CPU/gloo, so it runs here) loads a 160-column tensor into a `Shard(1)` DTensor. Every shard boundary falls mid-group for 2, 3 or 4 ranks (`DTensorTestBase` uses 4, or the GPU count on 2- or 3-GPU hosts), so the planner's overlap computation runs on the rewritten chunk. With the earlier 96-column version, removing the chunk rewrite made it fail with 163 / 384 elements mismatched. To check the new cases catch regressions, I temporarily removed both uint8 checks from `is_mxfp4` and hardcoded `torch.float32` in place of `self.target_dtype`: `test_compressed_tensors_mxfp4_loading` and the `packed_not_uint8`, `scale_not_uint8` and `int4` cases failed. The six skipped tests are the multi-GPU DTensor and consolidation tests, which skip on this ASAN build because it disables CUDA. On an earlier version with CUDA available, two of them, `TestDTensorReshardPlacementChange.test_2d_to_2d_reshard_placement_change` and `TestDTensorReshardMeshChange.test_2d_to_1d_reshard_mesh_change`, use the plain `HuggingFaceStorageReader`/`HuggingFaceStorageWriter` and failed the same way on master `b5c36964ae40` without this change. `arc lint` reports no issues. The GitHub PYFMT lint (isort 6.0.1, usort 1.1.3, ruff 0.14.4, run through `tools/linter/adapters/pyfmt_linter.py`) reports no changes to the lines this diff adds. pyrefly reports no new errors in `quantized_hf_storage.py`, the two test files, or `checkpoint_utils.py`, which now holds the MXFP4 reference encoder and checkpoint writer the tests share. The fbcode and xplat copies are identical. Reviewed By: fegin, shuhuayu Differential Revision: D124826603
bdc4c37 to
54ad35b
Compare
…01291) Summary: Pull Request resolved: pytorch#201291 Kimi K3 releases its routed experts in the compressed-tensors MXFP4 format: `X.weight_packed` is `[out, in/2]` uint8 with two E2M1 values per byte, and `X.weight_scale` is `[out, in/32]` uint8 E8M0 scales, one per 32 values. `QuantizedHuggingFaceStorageReader` handles FP8 (`weight_scale_inv`) and GPT-OSS MXFP4 (`_blocks`/`_scales`), but not this format. The encoding is the same as GPT-OSS's, and the bytes of a `[out, in/2]` tensor are laid out like one GPT-OSS expert, `[rows, groups, 16]`. So the reader reuses `_dequantize_tensor_mxfp4` with an expert dim of 1. Slice reads fetch whole 32-value groups and trim the result. Names follow the GPT-OSS convention: the reader reports `X.weight_packed` under its own name with the dequantized shape `[out, in]`, and a caller asks for `X.weight_packed` to get bf16 values back. TorchTitan's `to_hf(..., quantized=True)` requests these names. The alternative was to report the tensor as `X.weight`, so callers could use unquantized names, but that would make this the only format the reader renames. `read_metadata` also replaces the tensor's chunk list, because the load planner builds read items from `chunks`, not `size`, and reports the dequantized dtype, because empty-state-dict loads allocate their destination from the metadata. Other compressed-tensors formats use the same `weight_packed`/`weight_scale` names: NVFP4 (`float8_e4m3fn` scales, one per 16 values) and int4 `pack-quantized` (int32 `[out, in/8]` with a float scale, which at `group_size=128` matches the MXFP4 scale shape). So a pair is dequantized only when `weight_packed` is 2D and a whole number of 16-byte groups wide, both tensors are uint8, and the scale is one per 32 values; any other pair (including 3D int4 MoE weights) loads raw, as it did before this change. The DCPv2 resharder is not changed here. The fbcode and xplat copies are identical. This change was written with the help of an AI assistant. Test Plan: ``` cd fbcode buck2 test //caffe2/test/distributed/checkpoint:test_hf_safetensor_e2e //caffe2/test/distributed/checkpoint:test_quantized_hf_storage arc lint fbcode/caffe2/torch/distributed/checkpoint/quantized_hf_storage.py fbcode/caffe2/torch/testing/_internal/distributed/checkpoint_utils.py xplat/caffe2/torch/testing/_internal/distributed/checkpoint_utils.py fbcode/caffe2/test/distributed/checkpoint/test_hf_safetensor_e2e.py fbcode/caffe2/test/distributed/checkpoint/test_quantized_hf_storage.py xplat/caffe2/torch/distributed/checkpoint/quantized_hf_storage.py xplat/caffe2/test/distributed/checkpoint/test_hf_safetensor_e2e.py xplat/caffe2/test/distributed/checkpoint/test_quantized_hf_storage.py ``` Pass 19, Fail 0, Skip 6. - `test_compressed_tensors_mxfp4_loading` dequantizes a synthetic `weight_packed`/`weight_scale` pair through `dist_cp.load`, with the scale stored in a different file, and loads it again with `target_dtype=torch.bfloat16`, checking exact bf16 values. It then loads the checkpoint into an empty state dict with `_load_state_dict_from_keys`, once with the default `target_dtype` and once with `torch.bfloat16`, and checks the MXFP4 tensor comes back in that dtype with exactly the expected values; without the dtype fix it comes back as `torch.uint8`. (It loads the whole checkpoint: `_load_state_dict_from_keys` with explicit keys reads `metadata.planner_data`, which HF readers never set, so key filtering fails for any HF reader.) - `test_compressed_tensors_non_mxfp4_loads_raw` is parametrized over seven non-MXFP4 pairs, each in its own checkpoint, and checks the bytes round-trip through `dist_cp.load` into tensors of their stored shape and dtype: NVFP4 (`float8_e4m3fn` scale), MXFP4 shapes with an int8 `weight_packed`, MXFP4 shapes with a `float8_e4m3fn` scale, a uint8 scale that is not one per 32 values, a 40-byte width that is not a whole number of 16-byte groups, int4 `pack-quantized` (int32 `[out, in/8]`, bf16 scale per 128 values), and 3D int4 MoE weights. Before the rank/alignment check, the 40-byte case was misclassified as MXFP4 (`Size mismatch between saved torch.Size([4, 80]) and current: torch.Size([4, 40])`), and a 3D `weight_packed` aborted `read_metadata` on the 2D unpack. - `TestQuantizedHfStorage.test_compressed_tensors_mxfp4_slice_read` builds a read item for rows 1:3, cols 40:90 with the planner's `create_read_items_for_chunk_list` from the reader's own metadata and checks `_process_read_request` commits exactly that slice. - `TestCompressedTensorsMXFP4ShardedLoad.test_shard_starts_mid_group` (CPU/gloo, so it runs here) loads a 160-column tensor into a `Shard(1)` DTensor. Every shard boundary falls mid-group for 2, 3 or 4 ranks (`DTensorTestBase` uses 4, or the GPU count on 2- or 3-GPU hosts), so the planner's overlap computation runs on the rewritten chunk. With the earlier 96-column version, removing the chunk rewrite made it fail with 163 / 384 elements mismatched. To check the new cases catch regressions, I temporarily removed both uint8 checks from `is_mxfp4` and hardcoded `torch.float32` in place of `self.target_dtype`: `test_compressed_tensors_mxfp4_loading` and the `packed_not_uint8`, `scale_not_uint8` and `int4` cases failed. The six skipped tests are the multi-GPU DTensor and consolidation tests, which skip on this ASAN build because it disables CUDA. On an earlier version with CUDA available, two of them, `TestDTensorReshardPlacementChange.test_2d_to_2d_reshard_placement_change` and `TestDTensorReshardMeshChange.test_2d_to_1d_reshard_mesh_change`, use the plain `HuggingFaceStorageReader`/`HuggingFaceStorageWriter` and failed the same way on master `b5c36964ae40` without this change. `arc lint` reports no issues. The GitHub PYFMT lint (isort 6.0.1, usort 1.1.3, ruff 0.14.4, run through `tools/linter/adapters/pyfmt_linter.py`) reports no changes to the lines this diff adds. pyrefly reports no new errors in `quantized_hf_storage.py`, the two test files, or `checkpoint_utils.py`, which now holds the MXFP4 reference encoder and checkpoint writer the tests share. The fbcode and xplat copies are identical. Reviewed By: fegin, shuhuayu Differential Revision: D124826603
54ad35b to
b18e698
Compare
|
@pytorchbot merge (Initiating merge automatically since Phabricator Diff has merged) |
Merge startedYour change will be merged once all checks pass (ETA 0-4 Hours). Learn more about merging in the wiki. Questions? Feedback? Please reach out to the PyTorch DevX Team |
Mark the single-rank save/load tests as GENERIC and the multi-rank
consolidation and resharding tests as ACCELERATOR, and instantiate the latter
with instantiate_device_type_tests(except_for=("cpu",), allow_xpu=True) so any
registered accelerator collects them.
The resharding classes keep DTensorContinuousTestBase from pytorch#197677; its process
pool is spawned lazily to support device-type instantiation, as in
test_fsdp_dtensor_state_dict.py. Test bodies keep using self.device_type; the
`device` argument is only accepted to satisfy device-type instantiation.
TestCompressedTensorsMXFP4ShardedLoad (added in pytorch#201291) is left unchanged.
This replaces the earlier version of this PR, which targeted the file before
pytorch#197677.
Test Plan:
```bash
spin quicklint test/distributed/checkpoint/test_hf_safetensor_e2e.py
```
No runtime available locally: the file depends on DCP helpers newer than the
available NPU nightly, and there is no local CUDA build. Relying on CI.
Prepared with AI assistance and reviewed by the contributor.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_018dErhVMhZwJQXMUAkGfe9X
Summary:
Kimi K3 releases its routed experts in the compressed-tensors MXFP4 format:
X.weight_packedis[out, in/2]uint8 with two E2M1 values per byte, andX.weight_scaleis[out, in/32]uint8 E8M0 scales, one per 32 values.QuantizedHuggingFaceStorageReaderhandles FP8 (weight_scale_inv) and GPT-OSS MXFP4 (_blocks/_scales), but not this format.The encoding is the same as GPT-OSS's, and the bytes of a
[out, in/2]tensor are laid out like one GPT-OSS expert,[rows, groups, 16]. So the reader reuses_dequantize_tensor_mxfp4with an expert dim of 1. Slice reads fetch whole 32-value groups and trim the result.Names follow the GPT-OSS convention: the reader reports
X.weight_packedunder its own name with the dequantized shape[out, in], and a caller asks forX.weight_packedto get bf16 values back. TorchTitan'sto_hf(..., quantized=True)requests these names. The alternative was to report the tensor asX.weight, so callers could use unquantized names, but that would make this the only format the reader renames.read_metadataalso replaces the tensor's chunk list, because the load planner builds read items fromchunks, notsize, and reports the dequantized dtype, because empty-state-dict loads allocate their destination from the metadata.Other compressed-tensors formats use the same
weight_packed/weight_scalenames: NVFP4 (float8_e4m3fnscales, one per 16 values) and int4pack-quantized(int32[out, in/8]with a float scale, which atgroup_size=128matches the MXFP4 scale shape). So a pair is dequantized only whenweight_packedis 2D and a whole number of 16-byte groups wide, both tensors are uint8, and the scale is one per 32 values; any other pair (including 3D int4 MoE weights) loads raw, as it did before this change.The DCPv2 resharder is not changed here.
The fbcode and xplat copies are identical. This change was written with the help of an AI assistant.
Test Plan:
Pass 19, Fail 0, Skip 6.
test_compressed_tensors_mxfp4_loadingdequantizes a syntheticweight_packed/weight_scalepair throughdist_cp.load, with the scale stored in a different file, and loads it again withtarget_dtype=torch.bfloat16, checking exact bf16 values. It then loads the checkpoint into an empty state dict with_load_state_dict_from_keys, once with the defaulttarget_dtypeand once withtorch.bfloat16, and checks the MXFP4 tensor comes back in that dtype with exactly the expected values; without the dtype fix it comes back astorch.uint8. (It loads the whole checkpoint:_load_state_dict_from_keyswith explicit keys readsmetadata.planner_data, which HF readers never set, so key filtering fails for any HF reader.)test_compressed_tensors_non_mxfp4_loads_rawis parametrized over seven non-MXFP4 pairs, each in its own checkpoint, and checks the bytes round-trip throughdist_cp.loadinto tensors of their stored shape and dtype: NVFP4 (float8_e4m3fnscale), MXFP4 shapes with an int8weight_packed, MXFP4 shapes with afloat8_e4m3fnscale, a uint8 scale that is not one per 32 values, a 40-byte width that is not a whole number of 16-byte groups, int4pack-quantized(int32[out, in/8], bf16 scale per 128 values), and 3D int4 MoE weights. Before the rank/alignment check, the 40-byte case was misclassified as MXFP4 (Size mismatch between saved torch.Size([4, 80]) and current: torch.Size([4, 40])), and a 3Dweight_packedabortedread_metadataon the 2D unpack.TestQuantizedHfStorage.test_compressed_tensors_mxfp4_slice_readbuilds a read item for rows 1:3, cols 40:90 with the planner'screate_read_items_for_chunk_listfrom the reader's own metadata and checks_process_read_requestcommits exactly that slice.TestCompressedTensorsMXFP4ShardedLoad.test_shard_starts_mid_group(CPU/gloo, so it runs here) loads a 160-column tensor into aShard(1)DTensor. Every shard boundary falls mid-group for 2, 3 or 4 ranks (DTensorTestBaseuses 4, or the GPU count on 2- or 3-GPU hosts), so the planner's overlap computation runs on the rewritten chunk. With the earlier 96-column version, removing the chunk rewrite made it fail with 163 / 384 elements mismatched.To check the new cases catch regressions, I temporarily removed both uint8 checks from
is_mxfp4and hardcodedtorch.float32in place ofself.target_dtype:test_compressed_tensors_mxfp4_loadingand thepacked_not_uint8,scale_not_uint8andint4cases failed.The six skipped tests are the multi-GPU DTensor and consolidation tests, which skip on this ASAN build because it disables CUDA. On an earlier version with CUDA available, two of them,
TestDTensorReshardPlacementChange.test_2d_to_2d_reshard_placement_changeandTestDTensorReshardMeshChange.test_2d_to_1d_reshard_mesh_change, use the plainHuggingFaceStorageReader/HuggingFaceStorageWriterand failed the same way on masterb5c36964ae40without this change.arc lintreports no issues. The GitHub PYFMT lint (isort 6.0.1, usort 1.1.3, ruff 0.14.4, run throughtools/linter/adapters/pyfmt_linter.py) reports no changes to the lines this diff adds. pyrefly reports no new errors inquantized_hf_storage.py, the two test files, orcheckpoint_utils.py, which now holds the MXFP4 reference encoder and checkpoint writer the tests share. The fbcode and xplat copies are identical.Reviewed By: fegin, shuhuayu
Differential Revision: D124826603