Skip to content

Commit 0c31df1

Browse files
leofangemcastilloclaude
committed
Update module docstring to document both THPVariable layouts
Co-Authored-By: Emilio Castillo <ecastillo@nvidia.com> Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 00b8ec9 commit 0c31df1

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

cuda_core/cuda/core/_tensor_bridge.pyx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,18 @@ The ``pyobj_to_aten_handle`` trick exploits the internal layout of
1212
1313
struct THPVariable {
1414
PyObject_HEAD
15-
MaybeOwned<at::Tensor> cdata; // <-- this IS the AtenTensorHandle
15+
at::Tensor cdata; // <-- &cdata is usable as AtenTensorHandle
16+
...
1617
};
1718
18-
Offsetting past ``PyObject_HEAD`` gives us the ``at::Tensor`` pointer
19-
without any Python attribute access or method calls (~10 ns per tensor).
19+
In PyTorch 2.3–2.9 ``cdata`` was ``c10::MaybeOwned<at::Tensor>``;
20+
from 2.10 onward it is ``at::Tensor``. In both cases ``&cdata``
21+
(offset ``sizeof(PyObject)`` from the start of the object) is accepted
22+
by the AOTI stable C ABI functions as an ``AtenTensorHandle``.
23+
24+
Offsetting past ``PyObject_HEAD`` gives us the handle
25+
without any Python attribute access or method calls (~14 ns for all
26+
7 metadata queries).
2027
2128
Credit: Emilio Castillo (ecastillo@nvidia.com) – original tensor-bridge POC.
2229

0 commit comments

Comments
 (0)