-
Notifications
You must be signed in to change notification settings - Fork 759
[PyTorch][torch.compile] Make quantizers opaque value objects #3152
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pggPL
wants to merge
14
commits into
NVIDIA:main
Choose a base branch
from
pggPL:make_qunatizers_opaque
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+452
−1
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
f3401df
[PyTorch] Make tensorless quantizers opaque value objects for torch.c…
pggPL c4ad54c
[PyTorch] Drop quantizer value registry; reconstruct via __fx_repr__ …
pggPL a06324b
[PyTorch] Split dynamo.py into a dynamo/ package
pggPL ea5b396
[PyTorch] Raise in quantizer __fx_repr__ when a process group is stored
pggPL aa65e34
[PyTorch] Cover NVFP4 in quantizer value-object test
pggPL e1b1db6
Reject a value quantizer that carries an amax reduction group in __eq…
pggPL 8c33d0e
Recognize value-opaque quantizers via a class flag
pggPL 945f62d
Address review: narrow opaque-type except, add fullgraph test, fix nv…
pggPL e3c8f43
Restore NVFP4 rht_matrix on value-key rebuild; assert quantize round-…
pggPL 3f68621
Enforce process-group rejection in _value_key, not __fx_repr__; add test
pggPL 32d1768
Strengthen fullgraph test: quantize/dequantize via a custom op, not p…
pggPL 28bde9e
Clarify comments: rht_matrix_random_sign_mask_t derivation; why the o…
pggPL 2c3c5df
Reword opaque-flag comment: self-contained, no Linear reference
pggPL 826f271
Cover is_opaque_value_type with the import-safety guard too
pggPL File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| # Copyright (c) 2022-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # | ||
| # See LICENSE for license information. | ||
|
|
||
| """torch.compile glue for Transformer Engine.""" | ||
|
|
||
| from .quantizer_opaque import register_value_opaque_quantizer, is_value_opaque_quantizer | ||
|
|
||
| __all__ = [ | ||
| "register_value_opaque_quantizer", | ||
| "is_value_opaque_quantizer", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| # Copyright (c) 2022-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # | ||
| # See LICENSE for license information. | ||
|
|
||
| """Value-opaque quantizers for torch.compile.""" | ||
|
|
||
| from __future__ import annotations | ||
| from typing import Any, Dict, Tuple | ||
|
|
||
| from ..constants import DType | ||
|
|
||
|
|
||
| # Registration marks the class with this attribute rather than recording it in a | ||
| # module-level set. It looks odd but is a deliberate workaround: the check must | ||
| # stay traceable when it runs inside a torch.compile graph -- Dynamo can bake a | ||
| # ``getattr`` on the opaque quantizer into a constant, but cannot evaluate | ||
| # ``type(q) in some_set`` (no equality/hash rules for the opaque class object), | ||
| # which would graph-break under ``fullgraph=True``. | ||
| _VALUE_OPAQUE_FLAG = "_te_compile_value_opaque" | ||
|
|
||
|
|
||
| def is_value_opaque_quantizer(quantizer: Any) -> bool: | ||
| """Whether *quantizer*'s class is registered as a torch.compile value-opaque | ||
| type.""" | ||
| return getattr(quantizer, _VALUE_OPAQUE_FLAG, False) | ||
|
|
||
|
|
||
| def _rebuild_quantizer(cls: type, items: Tuple[Tuple[str, Any], ...]) -> Any: | ||
| """Rebuild a tensorless quantizer of type *cls* from its value items. | ||
|
|
||
| Referenced by the ``__fx_repr__`` emitted for value-opaque quantizers; the | ||
| generated FX code calls this to materialize the quantizer constant. | ||
| """ | ||
| # Bypass ``__init__`` and restore the value attributes directly: the value | ||
| # items already capture every value-defining field (including derived ones), | ||
| # and the constructors have heterogeneous signatures / side effects. | ||
| obj = cls.__new__(cls) | ||
| field_names = set() | ||
| for name, value in items: | ||
| if name == "dtype": | ||
| value = DType.cast(value) | ||
| object.__setattr__(obj, name, value) | ||
| field_names.add(name) | ||
| # The deprecated amax-reduction group is not a value field; initialize it to | ||
| # None so attribute access keeps working on the rebuilt quantizer. | ||
| if "with_amax_reduction" in field_names and not hasattr(obj, "amax_reduction_group"): | ||
| object.__setattr__(obj, "amax_reduction_group", None) | ||
| # Restore non-value derived state that ``__init__`` would normally build but | ||
| # that cannot live in the value key (e.g. NVFP4's ``rht_matrix`` tensor). | ||
| finalize = getattr(obj, "_rebuild_derived_state", None) | ||
| if finalize is not None: | ||
| finalize() | ||
| return obj | ||
|
|
||
|
|
||
| def _quantizer_fx_repr(self: Any) -> Tuple[str, Dict[str, Any]]: | ||
| """``__fx_repr__`` for value-opaque quantizers (attached at registration). | ||
|
|
||
| Returns an evaluable expression that rebuilds the quantizer via | ||
| :func:`_rebuild_quantizer`, capturing both the helper and the quantizer | ||
| class itself in the FX globals so codegen can resolve them with no global | ||
| registry and no qualname collisions. | ||
|
|
||
| Raises ``TypeError`` (via :meth:`Quantizer._value_key`) if the quantizer | ||
| stores a process group (e.g. a non-``None`` deprecated | ||
| ``amax_reduction_group``): live distributed state must never be baked into | ||
| the graph as a constant. Pass the reduction group per quantize call instead | ||
| of storing it on the quantizer. | ||
| """ | ||
| cls = type(self) | ||
| items = self._value_key()[1] | ||
| return ( | ||
| f"_rebuild_quantizer({cls.__name__}, {items!r})", | ||
| {"_rebuild_quantizer": _rebuild_quantizer, cls.__name__: cls}, | ||
| ) | ||
|
|
||
|
|
||
| def register_value_opaque_quantizer(cls: type) -> None: | ||
| """Register a tensorless quantizer class as a torch.compile value opaque type. | ||
|
|
||
| Attaches ``__fx_repr__`` and registers the class with | ||
| ``torch._library.opaque_object``. Safe to call on any PyTorch build: on | ||
| versions without the opaque-object API it only attaches ``__fx_repr__`` | ||
| (harmless), so Transformer Engine keeps importing and running in eager mode. | ||
|
|
||
| The quantizer class must already provide value ``__eq__`` / ``__hash__`` and | ||
| a non-``None`` ``_value_fields`` (see | ||
| :class:`transformer_engine.pytorch.quantized_tensor.Quantizer`). | ||
| """ | ||
| # Stamp the class so it can be recognized as value-opaque in dynamo-traced | ||
| # code (used to fall back to eager for unregistered quantizers). | ||
| setattr(cls, _VALUE_OPAQUE_FLAG, True) | ||
|
|
||
| # ``register_opaque_type`` requires ``__fx_repr__`` to already exist on the | ||
| # class, so attach it before registering. | ||
| if "__fx_repr__" not in cls.__dict__: | ||
| cls.__fx_repr__ = _quantizer_fx_repr | ||
|
|
||
| try: | ||
| from torch._library.opaque_object import ( # pylint: disable=import-outside-toplevel | ||
| register_opaque_type, | ||
| is_opaque_value_type, | ||
| ) | ||
| except (ImportError, AttributeError): | ||
| # Older PyTorch without the opaque-object API: eager value semantics | ||
| # still work; torch.compile specialization on the quantizer does not. | ||
| return | ||
|
|
||
| try: | ||
| if not is_opaque_value_type(cls): | ||
| register_opaque_type(cls, typ="value") | ||
| except (RuntimeError, TypeError): | ||
| # Keep TE importable: neither the opaque-type query nor the registration | ||
| # must crash the import, e.g. on PyTorch versions with only partial / | ||
| # experimental opaque-object support. | ||
| pass | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.