Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(core): forward TensorWrapper tensor methods under torch.compile (#5397)

NiteeshGowda722b10b0424cf6
diff --git a/changelog.d/5397.fixed.md b/changelog.d/5397.fixed.md
new file mode 100644
index 000000000..3a3fa093a
--- /dev/null
+++ b/changelog.d/5397.fixed.md
@@ -0,0 +1 @@
+A tensor method called through a `TensorWrapper` such as `Vector3`, for example `Vector3(t.clone()).sum()`, compiles with `torch.compile(..., fullgraph=True)`. Dynamo used to fail on it when the wrapper held an intermediate tensor, and on torch 2.5.1 for any wrapper.
diff --git a/kornia/core/tensor_wrapper.py b/kornia/core/tensor_wrapper.py
index ec73a4d2c..0b4d61f37 100644
--- a/kornia/core/tensor_wrapper.py
+++ b/kornia/core/tensor_wrapper.py
@@ -183,6 +183,11 @@ class TensorWrapper:
# Get value from underlying tensor
val = getattr(self._data, name)
+ # A tensor method is returned as is, so its result is not wrapped. Deciding this from the class keeps
+ # ``_wrap`` from asking for the bound method's type, which Dynamo does not know when the tensor is an
+ # intermediate, nor on torch 2.5.1 for any tensor.
+ if callable(getattr(Tensor, name, None)):
+ return val
# Wrap the result if it's a tensor
return _wrap(val, type(self))
diff --git a/tests/core/test_tensor_wrapper.py b/tests/core/test_tensor_wrapper.py
index 12b1d21dc..f33cf2282 100644
--- a/tests/core/test_tensor_wrapper.py
+++ b/tests/core/test_tensor_wrapper.py
@@ -399,15 +399,31 @@ class TestTensorWrapperProtocol(BaseTester):
(lambda t: TensorWrapper(t).unwrap(), True),
(lambda t: (TensorWrapper(t) + 1).unwrap(), True),
(_build_and_update, True),
+ (lambda t: TensorWrapper(t).sum(), True),
+ # A forwarded method on a wrapper built from an intermediate tensor (#5241).
+ (lambda t: TensorWrapper(t.clone()).sum(), True),
+ (lambda t: TensorWrapper(t * 2).mean(dim=-1), True),
+ (lambda t: TensorWrapper(t + 1).reshape(-1), True),
# torch 2.5.1's Dynamo breaks the graph here, cleanly: it does not send unary ``-`` or ``~``, a
# comparison, ``@`` or a binary or in-place operator with a tensor operand to a user class, and it cannot
- # trace a torch function on a wrapper, ``__getitem__`` or a forwarded method.
+ # trace a torch function on a wrapper or ``__getitem__``.
(lambda t: torch.add(TensorWrapper(t), 1).unwrap(), False),
(lambda t: TensorWrapper(t)[0].unwrap(), False),
- (lambda t: TensorWrapper(t).sum(), False),
(lambda t: (TensorWrapper(t) @ TensorWrapper(t.T)).unwrap(), False),
],
- ids=["data", "unwrap", "add", "inplace_and_reflected", "torch_add", "getitem", "method", "matmul"],
+ ids=[
+ "data",
+ "unwrap",
+ "add",
+ "inplace_and_reflected",
+ "method",
+ "sum_intermediate",
+ "mean_intermediate",
+ "reshape_intermediate",
+ "torch_add",
+ "getitem",
+ "matmul",
+ ],
)
def test_eager_backend_traces_a_function_that_builds_a_wrapper(self, device, dtype, fn, fullgraph):
# The eager backend keeps this in the ordinary jobs; test_dynamo covers the optimizer backends.