Repository navigation
[PyTorch] Fix device conversion for grouped parameters - #4031
vthumbe1503 wants to merge 1 commit into
Conversation
Move grouped backing buffers along with the tensor wrapper during module conversion. Synchronize grouped storage on data assignment and preserve caller-owned parameter attributes through module parameter swaps. Signed-off-by: Varun Thumbe <[email protected]>
Confidence Score: 4/5
[Medium impact] The changes affect conversion and data assignment for grouped PyTorch parameters. Fix reconstruction of native-created uniform tensors before merging; conversion can raise after their members are split. Findings
SummaryAdds device and dtype conversion for grouped tensors and keeps caller-owned parameter attributes across module conversion.
Reviews (1) · Last reviewed commit: "Fix device conversion for grouped parame..." · Reviewed by Greptile |
| nvfp4_use_4over6=storage.nvfp4_use_4over6, | ||
| nvfp4_e4m3_max=storage.nvfp4_e4m3_max, | ||
| scale_inv_dtype=storage._scale_inv_dtype, | ||
| stride=tuple(src.stride()), |
There was a problem hiding this comment.
Conversion fails after splitting
A native-created uniform GroupedTensor starts with a 2D wrapper and no tensor_shapes. Calling split_into_quantized_tensors() fills those member shapes. Conversion then passes them into the constructor, which chooses a 3D wrapper, while this line retains the original 2D stride. PyTorch rejects the different shape and stride lengths, so .cpu() or dtype conversion raises after splitting. Preserve the source wrapper shape when rebuilding it.
Knowledge Base Used: PyTorch tensor and operator abstractions
Description
Fix device conversion of
GroupedTensorparameters so the tensor wrapper and its backing buffers stay on the same device.Use case
Megatron-LM can initialize expert weights on CPU before moving the model to CUDA. With
moe_single_grouped_weight=True, the TEGroupedLinearweights are grouped tensor wrappers rather than ordinary tensors. This also applies to standalone TE modules constructed withsingle_grouped_weight=Trueorsingle_grouped_bias=True.For example:
Without handling grouped storage explicitly, module conversion can update the wrapper to CUDA while leaving its backing data on CPU. With PyTorch's parameter-swap conversion enabled, the fallback can instead lose the grouped tensor representation or caller-owned parameter attributes. This is a grouped-parameter conversion issue, not a MegaMoE- or NCCL-EP-specific issue.
Type of change
Changes
GroupedTensorconstructor. Cast unquantized data when the requested dtype changes, while preserving quantized storage and metadata dtypes..datasetter that synchronizes grouped backing storage when PyTorch assigns a converted parameter. Support compatible contiguous dense assignments to unquantized, uniform grouped tensors and invalidate cached member tensors._applyattribute-preservation logic, retaining caller-owned attributes and correctly rebinding attached methods..dataassignment. The conversion cases cover grouped weights and biases, dtype conversion, parameter identity, cached members, and caller attributes without adding a separate GEMM numerical test.Validation
git diff --checkpass.Checklist