Skip to content

Support quantized tensor subclasses across Python and C++ - #3393

Merged
ptrendx merged 4 commits into
NVIDIA:mainfrom
dingqingy-nv:codex/te-preserve-quantized-subclass-detach
Aug 20, 2026
Merged

Support quantized tensor subclasses across Python and C++#3393
ptrendx merged 4 commits into
NVIDIA:mainfrom
dingqingy-nv:codex/te-preserve-quantized-subclass-detach

Conversation

@dingqingy-nv

@dingqingy-nvdingqingy-nv commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Description

Transformer Engine quantized tensors can be subclassed, but two boundaries assumed an exact concrete wrapper class:

  1. Concrete detach() implementations reconstructed the base TE class, dropping the tensor's runtime subclass. PyTorch then rejected the result when rewrapping it as a torch.nn.Parameter, because Parameter requires detach() to preserve the exact runtime type.
  2. The PyTorch C++ bindings used exact Py_TYPE comparisons for quantized tensor outputs. As a result, operations such as MXFP8Quantizer.update_quantized(src, dst) rejected a valid MXFP8Tensor subclass even though Python isinstance(dst, MXFP8Tensor) was true.

Megatron Core GTP exposes both cases because it represents native quantized parameters with dynamic subclasses such as GTP_MXFP8Tensor. The newer module application path in #3153 made the latent detach mismatch visible. The downstream integration is NVIDIA/Megatron-LM#6546.

Type of change

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality not to work as expected)
  • Infra/Build change
  • Code refactoring

Changes

  • Define runtime-type preservation as part of the QuantizedTensor.detach() contract.
  • Construct detached tensors through the runtime class for Float8Tensor, Float8BlockwiseQTensor, MXFP8Tensor, NVFP4Tensor, IdentityTensor, and HybridQuantizedTensor.
  • Use CPython subtype checks for the Float8, blockwise Float8, MXFP8, and NVFP4 tensor/storage wrapper families in the C++ bindings. Quantizer objects retain their existing exact-type checks.
  • Add tests that verify exact subclass preservation, aliased quantized storage, detached autograd state, successful torch.nn.Parameter construction, and update_quantized into dynamic subclasses.

Validation

Built and tested on GB300 from TE commit bf64b4e8b2985ce7ff394b7f3cb240e764b24a3a:

  • 10/10 quantized subclass regression cases passed, covering detach and C++ update_quantized across Float8, blockwise Float8, MXFP8, NVFP4, Identity, and Hybrid wrappers.
  • 3/3 existing module _apply attribute-preservation tests passed.
  • 135/135 MXFP8 2D tests passed.
  • Transformer Engine Python and C++ lint passed.
  • Clean Megatron Core focused suite passed 10/10 with the downstream detach/update workarounds absent.
  • Two-node real-data 1D and 2D MXFP8 proxy runs each completed 10 training iterations plus validation with GTP and native MXFP8 parameter gather.
  • The same two-node 1D and 2D runs also passed with NVTE_CUTEDSL_FUSED_GROUPED_MLP=1 after installing the official final cuDNN-frontend 1.27.0 package:

The cuDNN-frontend package replacement addresses a separate pre-release package mismatch around optional prob_tensor; it is not part of this PR.

Checklist

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Signed-off-by: Dingqing Yang <dingqingy@nvidia.com>
Signed-off-by: Dingqing Yang <dingqingy@nvidia.com>
@github-actionsgithub-actionsBot added the community-contribution PRs from external contributor outside the core maintainers, representing community-driven work. label Aug 18, 2026
Signed-off-by: Dingqing Yang <dingqingy@nvidia.com>
@dingqingy-nvdingqingy-nv changed the title Preserve quantized tensor subclasses on detachSupport quantized tensor subclasses across Python and C++Aug 18, 2026
@dingqingy-nv
dingqingy-nv marked this pull request as ready for review August 18, 2026 04:18
@greptile-apps

greptile-appsBot commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

The PR extends quantized-tensor subclass support across Python detach operations and C++ type detection.

  • Centralizes subclass-preserving detach behavior in QuantizedTensor.
  • Preserves runtime subclasses for Hybrid and Identity wrappers.
  • Accepts derived quantized tensor and storage wrappers at C++ boundaries.
  • Adds regression coverage for detach, parameter construction, storage aliasing, and in-place quantization updates.

Confidence Score: 5/5

The PR appears safe to merge.

No blocking failure remains.

Important Files Changed

FilenameOverview
transformer_engine/pytorch/quantized_tensor.pyCentralizes detach reconstruction through the tensor's runtime class.
transformer_engine/pytorch/csrc/pybind.hReplaces exact wrapper-type comparisons with subtype-aware CPython checks.
transformer_engine/pytorch/tensor/hybrid_tensor.pyReconstructs detached Hybrid tensors using their runtime subclass.
transformer_engine/pytorch/tensor/identity_tensor.pyPreserves the runtime Identity tensor class when wrapping aliased data.
tests/pytorch/test_quantized_tensor.pyAdds coverage for subclass-preserving detach and C++ in-place updates across four quantization formats.

Sequence Diagram

sequenceDiagram
participant Caller
participant Tensor as QuantizedTensor subclass
participant Dispatch as PyTorch dispatch
participant Binding as C++ binding
Caller->>Dispatch: detach(tensor)
Dispatch->>Tensor: tensor.detach()
Tensor->>Tensor: type(self).make_like(self)
Tensor-->>Caller: detached runtime subclass
Caller->>Binding: update_quantized(src, subclass)
Binding->>Binding: PyObject_TypeCheck
Binding-->>Caller: update accepted in place
Loading

Reviews (2): Last reviewed commit: "Centralize quantized tensor detach" | Re-trigger Greptile

@zhongbozhuzhongbozhu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

@ptrendxptrendx self-assigned this Aug 18, 2026
Comment threadtransformer_engine/pytorch/tensor/float8_tensor.py Outdated
Comment threadtransformer_engine/pytorch/csrc/pybind.h
Comment threadtransformer_engine/pytorch/quantized_tensor.py Outdated
Signed-off-by: Dingqing Yang <dingqingy@nvidia.com>
@ptrendx

Copy link
Copy Markdown
Member

/te-ci pytorch

@ptrendx
ptrendx merged commit d61c3fd into NVIDIA:mainAug 20, 2026
21 of 26 checks passed
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contributionPRs from external contributor outside the core maintainers, representing community-driven work.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

@dingqingy-nv@ptrendx@zhongbozhu