Verified Solution

[pytorch/pytorch] Dynamo does not know how to trace the builtin `.TensorBase._make_wrapper_subclass.`

Sponsored Content
### ROOT CAUSE The issue arises because `torch.compile` and TorchDynamo fail to trace the built-in `_make_wrapper_subclass` method, which is used to create custom tensor subclasses. This method is not handled by the dispatch mechanism, leading to tracing failures during compilation. ### CODE FIX To resolve this, override the `__torch_dispatch__` method to explicitly handle `_make_wrapper_subclass` by redirecting it through the dispatch mechanism. Here's the fix: ```python import torch class LoggingTensor(torch.Tensor): @staticmethod def __new__(cls, data): return torch.Tensor._make_wrapper_subclass( cls, data.shape, dtype=data.dtype, device=data.device ) def __repr__(self): return f"LoggingTensor({self.elem})" @classmethod def __torch_dispatch__(cls, func, types, args=(), kwargs=None): import torch._dynamo as dynamo # Handle tensor creation via _make_wrapper_subclass if func is torch.Tensor._make_wrapper_subclass: args = (cls,) + args[1:] # Adjust args to match _make_wrapper_subclass signature return dynamo.ops.dispatch(*args, **kwargs) # Original dispatch logic for other operations kwargs = kwargs or {} unwrap = lambda t: t.elem if isinstance(t, LoggingTensor) else t wrap = lambda t: LoggingTensor(t) if isinstance(t, torch.Tensor) else t out = func( *torch.utils._pytree.tree_map(unwrap, args), **torch.utils._pytree.tree_map(unwrap, kwargs), ) return torch.utils._pytree.tree_map(wrap, out) ``` This fix ensures that `_make_wrapper_subclass` is properly traced by intercepting it within the dispatch mechanism, allowing `torch.compile` to handle the tensor creation correctly.
Deploy on DigitalOcean ($200 Credit)

Related Fixes

[tensorflow/tensorflow] Numerical discrepancy in tf.linalg.matmul vs PyTorch torch.matmul for 3D float32 tensors
[gitlab-org/gitlab] Conan v2 package registry returns incorrect responses `200 OK` with a phantom revision
[gitlab-org/gitlab] discussions store: updateNote uses deep merge where splice used full replacement