Skip to content

Add Python Torch conversions - #11

Open
yuanknv wants to merge 8 commits into
nvcyc/cuda_buffer_pyfrom
feature/torch-conversions-py
Open

Add Python Torch conversions#11
yuanknv wants to merge 8 commits into
nvcyc/cuda_buffer_pyfrom
feature/torch-conversions-py

Conversation

@yuanknv

@yuanknv yuanknv commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Description

This pull request adds typed Python conversions between PyTorch tensors and tensor_msgs/ExperimentalTensor messages.

The new torch_conversions_py package exposes the torch_conversions Python module with the following APIs:

  • allocate_tensor_msg(): Allocates CPU-backed or CUDA-backed message storage.
  • from_output_tensor_msg(): Returns a writable Torch view over message-owned storage.
  • from_input_tensor_msg(): Returns an independent tensor by default or a zero-copy read-only view with clone=False.
  • to_tensor_msg(): Copies an existing Torch tensor into message-owned storage.
  • set_stream(): Provides CUDA stream context management.

For CUDA-backed messages, a private pybind extension exposes the rosidl::Buffer allocation through DLPack. The DLPack context retains the buffer and its CUDA read or write handle until PyTorch releases the tensor, preserving asynchronous stream and event synchronization without copying the message storage.

This pull request is stacked on #6, which provides cuda_buffer_py.

Is this user-facing behavior change?

No. This pull request adds a new optional Python API without changing existing behavior.

This adds a new Python API for constructing ExperimentalTensor messages and accessing their CPU or CUDA storage as PyTorch tensors.

Did you use Generative AI?

Yes. OpenAI GPT-5.6 Sol via Cursor was used to assist with implementation and test.

Additional Information

This work extends the C++ Torch conversions introduced in #1 with an equivalent Python-facing API.

@yuanknv
yuanknv force-pushed the feature/torch-conversions-py branch from fdd989f to 4197dee Compare August 19, 2026 00:01
@yuanknv
yuanknv force-pushed the feature/torch-conversions-py branch from 4197dee to 778a301 Compare August 19, 2026 18:15
@yuanknv yuanknv assigned yuanknv and unassigned nvcyc Aug 20, 2026
Comment on lines +15 to +19
<depend>rosidl_buffer</depend>

<exec_depend>pytorch_vendor</exec_depend>
<exec_depend>rosidl_buffer_py</exec_depend>
<exec_depend>tensor_msgs</exec_depend>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to add cuda_buffer and cuda_buffer_py as depndencies?

Suggested change
<depend>rosidl_buffer</depend>
<exec_depend>pytorch_vendor</exec_depend>
<exec_depend>rosidl_buffer_py</exec_depend>
<exec_depend>tensor_msgs</exec_depend>
<depend>cuda_buffer</depend>
<depend>rosidl_buffer</depend>
<exec_depend>cuda_buffer_py</exec_depend>
<exec_depend>pytorch_vendor</exec_depend>
<exec_depend>rosidl_buffer_py</exec_depend>
<exec_depend>tensor_msgs</exec_depend>

Expose typed CPU and CUDA tensor-message conversions, preserve DLPack-backed buffer ownership, and validate zero-copy inter-process rclpy transport.
Add an independent Python Torch vendor and keep CUDA integration optional so CPU and CUDA platforms use the same conversion API.
Keep package documentation centralized and avoid duplicating platform behavior details.
Rename the Python package to pytorch_vendor and align both vendor selectors with official CUDA 13.0 and 13.2 artifacts.
Separate CPU and CUDA conversion behavior behind a private registry so future hardware support does not change the public API.
Make the private extension's scope explicit so future hardware adapters can provide distinct native bridges.
Use a versioned C ABI so CPU users are not forced to install CUDA buffer runtime dependencies.
Keep the core CPU-capable package platform-independent and discover optional conversion adapters through Python entry points.
@yuanknv
yuanknv force-pushed the feature/torch-conversions-py branch from 4dddf00 to f5c5175 Compare August 27, 2026 23:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants