Skip to content

[PyTorch] Fix device conversion for grouped parameters - #4031

Open
vthumbe1503 wants to merge 1 commit into
NVIDIA:mainfrom
vthumbe1503:fix/grouped-tensor-device-conversion
Open

vthumbe1503 wants to merge 1 commit into
NVIDIA:mainfrom
vthumbe1503:fix/grouped-tensor-device-conversion

Conversation

@vthumbe1503

Copy link
Copy Markdown
Collaborator

Description

Fix device conversion of GroupedTensor parameters 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 TE GroupedLinear weights are grouped tensor wrappers rather than ordinary tensors. This also applies to standalone TE modules constructed with single_grouped_weight=True or single_grouped_bias=True.

For example:

import torch
import transformer_engine.pytorch as te

module = te.GroupedLinear(
    2, 32, 64,
    device="cpu",
    params_dtype=torch.float32,
    single_grouped_weight=True,
    single_grouped_bias=True,
)
module.to(device="cuda", dtype=torch.bfloat16)

assert module.weight.device == module.weight.rowwise_data.device
assert module.weight.dtype == module.weight.rowwise_data.dtype

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

  • Bug fix (non-breaking change which fixes an issue)

Changes

  • Handle device conversion by moving the grouped data, scales, and tensor-valued metadata, then reconstructing through the GroupedTensor constructor. Cast unquantized data when the requested dtype changes, while preserving quantized storage and metadata dtypes.
  • Add a .data setter 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.
  • Include grouped parameters in the existing module _apply attribute-preservation logic, retaining caller-owned attributes and correctly rebinding attached methods.
  • Keep regression coverage to three cases: CPU-to-CUDA-to-CPU conversion with parameter swapping disabled/enabled, and dense .data assignment. 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

  • Current patch: Python syntax checks, Black formatting check, and git diff --check pass.
  • An earlier implementation was GPU-tested on H100, including the grouped tensor suite and CPU-initialized grouped-linear forward/backward correctness. The subsequent constructor/property cleanup and reduced tests have not been rerun on GPU, and the current rebase onto main has not been GPU-validated. Those earlier results are not validation of this exact revision.
  • GPU regression tests, full L0 validation, and human review remain pending; opening as a draft.

Checklist

  • I have read and followed the contributing guidelines.
  • I have added focused regression tests.
  • New and existing unit tests pass with this exact revision.
  • Full L0 checks pass.
  • Human review is complete.

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]>
@vthumbe1503
vthumbe1503 marked this pull request as ready for review December 5, 2026 02:09
@vthumbe1503
vthumbe1503 requested a review from ksivaman as a code owner December 5, 2026 02:09
@greptile-apps

greptile-apps Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 4/5RetriggerView in Greptile

[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

  1. P1 Conversion fails after splitting ▶

Summary

Adds device and dtype conversion for grouped tensors and keeps caller-owned parameter attributes across module conversion.

  • Grouped parameters and their backing buffers move together during module conversion.
  • Compatible dense assignments update a grouped parameter’s backing storage.

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()),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 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

This branch has not been deployed

No deployments
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