Signet Forge 0.1.0
C++20 Parquet library with AI-native extensions
DEMO
Loading...
Searching...
No Matches
signet::forge::NumpyBridge Class Reference

Exports and imports Signet tensors via DLPack, enabling zero-copy interoperability with PyTorch, NumPy, JAX, and other DLPack-aware frameworks. More...

#include <numpy_bridge.hpp>

Static Public Member Functions

static expected< DLManagedTensor * > export_tensor (const TensorView &tensor)
 Export a TensorView as a DLManagedTensor (zero-copy, non-owning).
 
static expected< DLManagedTensor * > export_owned_tensor (OwnedTensor &&tensor)
 Export an OwnedTensor as a DLManagedTensor (zero-copy ownership transfer).
 
static expected< TensorViewimport_tensor (const DLManagedTensor *managed)
 Import a DLManagedTensor as a TensorView (zero-copy).
 
static expected< OwnedTensorimport_tensor_copy (const DLManagedTensor *managed)
 Import a DLManagedTensor as an OwnedTensor (deep copy).
 

Detailed Description

Exports and imports Signet tensors via DLPack, enabling zero-copy interoperability with PyTorch, NumPy, JAX, and other DLPack-aware frameworks.

Three export modes:

Two import modes:

// Export (Python side via pybind11):
// Wrap in PyCapsule named "dltensor", then:
// torch.from_dlpack(capsule) or numpy.from_dlpack(capsule)
// Import (C++ side):
auto view = *NumpyBridge::import_tensor(managed);
static expected< DLManagedTensor * > export_tensor(const TensorView &tensor)
Export a TensorView as a DLManagedTensor (zero-copy, non-owning).
static expected< TensorView > import_tensor(const DLManagedTensor *managed)
Import a DLManagedTensor as a TensorView (zero-copy).
DLPack managed tensor – the exchange object for from_dlpack().
Note
All methods are static – NumpyBridge is a stateless utility class.
Only CPU and CUDAHost devices are supported for import.
See also
BufferInfo, to_buffer_info

Definition at line 329 of file numpy_bridge.hpp.

Member Function Documentation

◆ export_owned_tensor()

static expected< DLManagedTensor * > signet::forge::NumpyBridge::export_owned_tensor ( OwnedTensor &&  tensor)
inlinestatic

Export an OwnedTensor as a DLManagedTensor (zero-copy ownership transfer).

The OwnedTensor is moved into the DLManagedTensor's manager context. When the consumer calls the deleter, the OwnedTensor (and its data) is freed. This is a true zero-copy ownership transfer – no data is copied and no source lifetime constraints apply.

Parameters
tensorAn OwnedTensor to transfer (moved from; left in a valid but unspecified state after the call).
Returns
A heap-allocated DLManagedTensor that owns both the struct and the tensor data, or INTERNAL_ERROR for invalid tensors, UNSUPPORTED_TYPE for non-contiguous tensors.
See also
export_tensor, import_tensor_copy

Definition at line 391 of file numpy_bridge.hpp.

◆ export_tensor()

static expected< DLManagedTensor * > signet::forge::NumpyBridge::export_tensor ( const TensorView tensor)
inlinestatic

Export a TensorView as a DLManagedTensor (zero-copy, non-owning).

The TensorView's data is NOT copied. The returned DLManagedTensor's DLTensor.data points directly into the TensorView's memory.

IMPORTANT: The TensorView (and its underlying data) must outlive the DLManagedTensor. The consumer calls deleter() when done, which frees only the DLManagedTensor and its helper arrays, NOT the data.

Parameters
tensorA valid, contiguous TensorView.
Returns
A heap-allocated DLManagedTensor (caller takes ownership of the struct, not the data), or INTERNAL_ERROR for invalid tensors, UNSUPPORTED_TYPE for non-contiguous tensors.
See also
export_owned_tensor, import_tensor

Definition at line 345 of file numpy_bridge.hpp.

◆ import_tensor()

static expected< TensorView > signet::forge::NumpyBridge::import_tensor ( const DLManagedTensor managed)
inlinestatic

Import a DLManagedTensor as a TensorView (zero-copy).

The returned TensorView wraps the DLTensor's data directly. The DLManagedTensor (and thus the underlying data) must remain valid for the lifetime of the TensorView. The caller is still responsible for calling the DLManagedTensor's deleter when done.

Only CPU and CUDAHost tensors are supported. Non-CPU devices are rejected. Strided (non-contiguous) tensors are rejected unless the strides match C-contiguous layout exactly.

Parameters
managedA valid DLManagedTensor (must not be null).
Returns
A TensorView wrapping the DLPack data, or INTERNAL_ERROR for null/invalid inputs, UNSUPPORTED_TYPE for non-CPU devices, non-contiguous strides, or unsupported DLPack data types.
See also
import_tensor_copy, export_tensor

Definition at line 443 of file numpy_bridge.hpp.

◆ import_tensor_copy()

static expected< OwnedTensor > signet::forge::NumpyBridge::import_tensor_copy ( const DLManagedTensor managed)
inlinestatic

Import a DLManagedTensor as an OwnedTensor (deep copy).

Copies the data from the DLPack tensor into a new heap allocation. After this call returns, the DLManagedTensor can be released independently – the OwnedTensor owns its data.

Only CPU and CUDAHost tensors are supported. Strided (non-contiguous) tensors ARE supported – data is gathered into a contiguous C-order layout via element-by-element copy.

For contiguous tensors, a fast memcpy path is used. For strided tensors, a multi-index iteration walks the source layout.

Parameters
managedA valid DLManagedTensor (must not be null).
Returns
An OwnedTensor with a contiguous deep copy of the data, or INTERNAL_ERROR for null/invalid inputs, UNSUPPORTED_TYPE for non-CPU devices or unsupported dtypes.
See also
import_tensor, export_owned_tensor

Definition at line 554 of file numpy_bridge.hpp.


The documentation for this class was generated from the following file: