[PyTorch] Make quantized-tensor __repr__ safe#3146
Conversation
Greptile SummaryThis PR adds a defensive fallback to
Confidence Score: 5/5Safe to merge — all changes are confined to repr methods with no impact on quantization logic, data movement, or training correctness. Every change is a defensive wrapper around existing repr logic. The try/except blocks cannot affect tensor data, gradients, or computation graphs, and the new safe_quantized_repr helper is a pure string-building utility. The only gap is that str(error) inside the helper is itself unguarded, but this is a cosmetic reliability edge case on the fallback path. transformer_engine/pytorch/tensor/_quantization_helpers.py — the new safe_quantized_repr helper; specifically the unguarded str(error) interpolation on line 121. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["__repr__ called"] --> B["try: dequantize() + format string"]
B -->|success| C["Return full repr\n(fp8_dtype, scale_inv, data=...)"]
B -->|"Exception as exc"| D["safe_quantized_repr(obj, cls_name, extras, error=exc)"]
D --> E["getattr(obj, '_fp8_dtype', None)"]
E --> F["append extras if provided\n(e.g. is_2D_scaled for blockwise)"]
F --> G["try: append shape=tuple(obj.shape)"]
G --> H["try: append dtype=obj.dtype"]
H --> I["append data=<unmaterialized: ErrorType: msg>"]
I --> J["Return fallback repr string"]
style C fill:#90EE90
style J fill:#FFD700
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
flowchart TD
A["__repr__ called"] --> B["try: dequantize() + format string"]
B -->|success| C["Return full repr\n(fp8_dtype, scale_inv, data=...)"]
B -->|"Exception as exc"| D["safe_quantized_repr(obj, cls_name, extras, error=exc)"]
D --> E["getattr(obj, '_fp8_dtype', None)"]
E --> F["append extras if provided\n(e.g. is_2D_scaled for blockwise)"]
F --> G["try: append shape=tuple(obj.shape)"]
G --> H["try: append dtype=obj.dtype"]
H --> I["append data=<unmaterialized: ErrorType: msg>"]
I --> J["Return fallback repr string"]
style C fill:#90EE90
style J fill:#FFD700
Reviews (6): Last reviewed commit: "Make quantized __repr__ fallback univers..." | Re-trigger Greptile |
381a964 to
222e55c
Compare
Under torch.compile, TE quantized-tensor __repr__ methods are invoked on
FakeTensors during AOT autograd's structured logging. The repr bodies call
self._scale_inv.item() and/or self.dequantize() (which dispatches to the raw
C++ op tex.dequantize), both of which access a FakeTensor's data pointer and
raise:
RuntimeError: Cannot access data pointer of Tensor (e.g. FakeTensor,
FunctionalTensor) ...
This was the sole cause of six fp8 failures in tests/pytorch/test_torch_compile.py.
Fix: add one shared helper, safe_quantized_repr, in tensor/_quantization_helpers.py
(a safe leaf module importing only torch) that builds a metadata-only repr
string. Each data-touching __repr__ now wraps its existing body in a try/except
and falls back to the helper when the data cannot be materialized. The eager
(non-fake) repr output is unchanged; only a fallback path is added.
Wrapped reprs: Float8Tensor, Float8BlockwiseQTensor, MXFP8Tensor, NVFP4Tensor
and their *Storage counterparts.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
222e55c to
33e938c
Compare
for more information, see https://pre-commit.ci
1c0a996 to
ebeba92
Compare
…logic Remove the FakeTensor-specific heuristic (_is_fake_data_access_error) and the warning path from safe_quantized_repr. The fallback is now a plain metadata-only repr triggered by any exception while materializing data, with each attribute access individually guarded so __repr__ never raises. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Signed-off-by: Pawel Gadzinski <pgadzinski@nvidia.com>
ebeba92 to
b30b6ee
Compare
|
/te-ci pytorch |
Description
I've encountered multiple situations when there was some error in the code inside repr.
It's very annoying, so I created this PR to fallback to some safe repr, when dequantize is not possible.
Also in some torch.compile logic in pytorch repr is called for Float8Tensor with fake tensors inside, which obviously fails on
.deqauntize().Type of change
Changes
safe_quantized_repr(obj, cls_name)intransformer_engine/pytorch/tensor/_quantization_helpers.py(a safe leaf module importing onlytorch) that builds a metadata-only repr (fp8_dtype,shape,dtype,data=<unmaterialized>), defensively guarding each attribute access.__repr__body intry/except Exceptionand fall back tosafe_quantized_reprwhen data cannot be materialized. Affected classes:Float8Tensor,Float8BlockwiseQTensor,MXFP8Tensor,NVFP4Tensor, and their*Storagecounterparts (Float8TensorStorage,Float8BlockwiseQTensorStorage,MXFP8TensorStorage,NVFP4TensorStorage).NVFP4Tensoreager string is intentionally preserved in the try branch and not "fixed".Checklist:
AI assistance: this PR was prepared with the help of Claude (Anthropic) under my review.