Skip to content

[PyTorch] Use torch's register_custom_class API for opaque quantizers when available - #3495

Merged
pggPL merged 1 commit into
NVIDIA:mainfrom
mmarcinkiewicz:michalm/opaque-object-new-api
Sep 8, 2026
Merged

[PyTorch] Use torch's register_custom_class API for opaque quantizers when available#3495
pggPL merged 1 commit into
NVIDIA:mainfrom
mmarcinkiewicz:michalm/opaque-object-new-api

Conversation

@mmarcinkiewicz

@mmarcinkiewicz mmarcinkiewicz commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Description

Switch quantizer_opaque.py, custom_op.py and the torch.compile test to the new names. Both import sites already sit behind try/except that records the torch.compile custom-op path as disabled, so a PyTorch that predates the rename (torch < 2.14) keeps importing and running eagerly with the compile path off, the same as a PyTorch without the opaque-object API at all.

Fixes # (issue)

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 to not work as expected)
  • Infra/Build change
  • Code refactoring

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

@github-actions github-actions Bot added the community-contribution PRs from external contributor outside the core maintainers, representing community-driven work. label Sep 8, 2026
@greptile-apps

greptile-apps Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR updates Transformer Engine’s PyTorch Dynamo integration to use the current opaque-object API without invoking deprecated wrappers.

  • Registers quantizers and OpaqueValueBundle through register_custom_class with typ="constant".
  • Replaces opaque-value checks with is_opaque_constant_type.
  • Updates the torch.compile test fixture to use the renamed API directly.
  • The previous compatibility-testing finding is no longer outstanding because the compatibility helper and its separate legacy branch were removed, and the remaining implementation targets the repository’s supported PyTorch range.

Confidence Score: 5/5

The PR appears safe to merge because the renamed opaque-object APIs are available throughout the repository’s supported PyTorch range and no outstanding correctness issue remains.

The current code consistently uses register_custom_class, is_opaque_constant_type, and typ="constant" across production and test paths. The previous finding concerned untested branches in a compatibility helper that has since been removed; the repository’s minimum supported PyTorch version does not require that legacy branch.

Important Files Changed

Filename Overview
transformer_engine/pytorch/dynamo/quantizer_opaque.py Replaces compatibility-helper registration with the current opaque constant-class API supported by the repository’s minimum PyTorch version.
transformer_engine/pytorch/dynamo/custom_op.py Migrates OpaqueValueBundle registration and type checks to the renamed PyTorch APIs.
tests/pytorch/test_torch_compile.py Updates the test-only opaque class registration to use register_custom_class and constant semantics.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart LR
    Import[Transformer Engine import] --> Register[register_custom_class]
    Register --> Quantizers[Opaque quantizer classes]
    Register --> Bundle[OpaqueValueBundle]
    Quantizers --> Compile[torch.compile specialization]
    Bundle --> Compile
Loading

Reviews (4): Last reviewed commit: "[PyTorch] Use torch's register_custom_cl..." | Re-trigger Greptile

Comment on lines +29 to +31
if hasattr(oo, "register_custom_class") and hasattr(oo, "is_opaque_constant_type"):
return oo.register_custom_class, oo.is_opaque_constant_type, "constant"
return oo.register_opaque_type, oo.is_opaque_value_type, "value"

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.

P2 Compatibility branches lack tests

This helper introduces separate paths for the new and legacy PyTorch APIs, but the updated compile test only runs the path exposed by the installed PyTorch version and does not check for deprecation warnings. Add isolated tests that force both API generations and verify the matching function and typ value; otherwise the warning regression or legacy fallback could break without CI detecting it.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@pggPL pggPL self-assigned this Sep 8, 2026
@mmarcinkiewicz
mmarcinkiewicz force-pushed the michalm/opaque-object-new-api branch from f1e2312 to dee7226 Compare September 8, 2026 10:23
from ..utils import record_compile_disabled


def opaque_object_api() -> Tuple[Any, Any, str]:

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.

You can remove it and just import new API directly. It already is behind try/except. One would be affected only when one tries to use torch.compile with pytorch with older API, but this would not work due to other issues in Pytorch. Also, first version with torch.compile support will be 2.20, so we can safely remove this logic and test and just change imports.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, thanks. Dropped the helper and its tests and switched the imports directly; the existing try/except handles older torch as before.

PyTorch renamed the opaque-object entry points: register_opaque_type ->
register_custom_class, is_opaque_value_type -> is_opaque_constant_type,
typ="value" -> typ="constant". The old names remain as thin wrappers that
log a deprecation warning on every call, so each TE import now emits 21
warning lines (5 quantizer classes registered, 11 type checks) -- once per
process, including every torch.compile worker. A 16-rank training job logs
them from ~320 processes.

Switch quantizer_opaque.py, custom_op.py and the torch.compile test to the
new names. Both import sites already sit behind try/except that records the
torch.compile custom-op path as disabled, so a PyTorch that predates the
rename (torch < 2.14) keeps importing and running eagerly with the compile
path off -- the same behaviour as a PyTorch without the opaque-object API
at all.

Signed-off-by: Michal Marcinkiewicz <michalm@nvidia.com>
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@mmarcinkiewicz
mmarcinkiewicz force-pushed the michalm/opaque-object-new-api branch from cc06308 to 296e419 Compare September 8, 2026 14:18
@pggPL

pggPL commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator

/te-ci pytorch

@pggPL pggPL 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

@pggPL
pggPL merged commit b97f5ab into NVIDIA:main Sep 8, 2026
23 of 28 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants