Crate tenferro_capi

Crate tenferro_capi 

Source
Expand description

C-API (FFI) for tenferro.

Exposes tensor lifecycle, einsum, SVD (including AD rules), and DLPack interop to host languages such as Julia, Python (JAX, PyTorch), and C/C++.

§Design principles

  • Opaque pointers: TfeTensorF64 is an opaque handle wrapping Tensor<f64>. Host languages never see Rust internals.
  • Status codes: Every function takes a *mut tfe_status_t as its last argument. Rust panics are caught with catch_unwind and converted to TFE_INTERNAL_ERROR.
  • Stateless AD rules: Only rrule (VJP) and frule (JVP) are exposed. The AD tape / TrackedTensor / DualTensor are Rust-internal and not exposed via FFI. Host languages manage their own AD tapes (ChainRules.jl, PyTorch autograd, JAX custom_vjp).
  • f64 only in this POC phase. All functions carry the _f64 suffix.
  • DLPack interop: Zero-copy tensor exchange with Julia, Python, and other frameworks via DLManagedTensorVersioned. Supports CPU and GPU memory. Use tfe_tensor_f64_to_dlpack (export) and tfe_tensor_f64_from_dlpack (import).
  • Copy semantics for convenience functions: tfe_tensor_f64_from_data copies the caller’s data into a Rust-owned buffer. For zero-copy, use DLPack.

§Memory ownership

AllocationFreed by
Tensor from _from_data / _zeros / _clonetfe_tensor_f64_release
Tensor from _from_dlpacktfe_tensor_f64_release (calls DLPack deleter)
Output tensor (via **_out)tfe_tensor_f64_release
Gradient tensor (rrule output)tfe_tensor_f64_release
grads_out array (einsum rrule)Caller provides buffer
Input data pointerCaller (data is copied)
DLManagedTensorVersioned from _to_dlpackConsumer calls deleter

§Example (C pseudocode)

tfe_status_t status;
size_t shape[] = {3, 4};
double data[12] = { /* ... */ };

tfe_tensor_f64 *a = tfe_tensor_f64_from_data(data, 12, shape, 2, &status);
assert(status == TFE_SUCCESS);

const tfe_tensor_f64 *ops[] = {a, a};
tfe_tensor_f64 *c = tfe_einsum_f64("ij,jk->ik", ops, 2, &status);

tfe_tensor_f64_release(c);
tfe_tensor_f64_release(a);

Structs§

DLDataType
DLPack data type descriptor.
DLDevice
DLPack device descriptor.
DLManagedTensorVersioned
DLPack managed tensor with version and ownership (DLPack v1.0+).
DLPackVersion
DLPack version information.
DLTensor
DLPack tensor descriptor (unmanaged).
TfeTensorF64
Opaque handle wrapping a Tensor<f64>.

Constants§

DLPACK_FLAG_BITMASK_IS_COPIED
Data was copied (not zero-copy).
DLPACK_FLAG_BITMASK_READ_ONLY
Data is read-only (consumer must not write).
KDLCOMPLEX
Complex type code.
KDLCPU
CPU device.
KDLCUDA
NVIDIA CUDA GPU device memory.
KDLCUDA_HOST
Pinned CUDA CPU memory (cudaMallocHost).
KDLCUDA_MANAGED
CUDA managed/unified memory (cudaMallocManaged).
KDLFLOAT
Floating-point type code.
KDLINT
Integer type code.
KDLROCM
AMD ROCm GPU device memory.
KDLROCM_HOST
Pinned ROCm CPU memory (hipMallocHost).
TFE_INTERNAL_ERROR
Internal error (Rust panic or unexpected failure).
TFE_INVALID_ARGUMENT
Invalid argument (null pointer, bad subscript string, etc.).
TFE_SHAPE_MISMATCH
Tensor shape mismatch for the requested operation.
TFE_SUCCESS
Operation completed successfully.

Functions§

tfe_einsum_f64
Execute einsum using string notation.
tfe_einsum_frule_f64
Forward-mode rule (JVP) for einsum.
tfe_einsum_rrule_f64
Reverse-mode rule (VJP) for einsum.
tfe_svd_f64
Compute the SVD of a tensor.
tfe_svd_frule_f64
Forward-mode rule (JVP) for SVD.
tfe_svd_rrule_f64
Reverse-mode rule (VJP) for SVD.
tfe_tensor_f64_clone
Deep-copy a tensor.
tfe_tensor_f64_data
Return a pointer to the tensor’s raw data buffer.
tfe_tensor_f64_from_data
Create a tensor from caller-provided data (copy semantics).
tfe_tensor_f64_from_dlpack
Import a DLPack managed tensor as a tenferro tensor (zero-copy).
tfe_tensor_f64_len
Return the total number of elements in the tensor.
tfe_tensor_f64_ndim
Return the number of dimensions (rank) of the tensor.
tfe_tensor_f64_release
Release (free) a tensor.
tfe_tensor_f64_shape
Write the shape of the tensor into the caller-provided buffer.
tfe_tensor_f64_to_dlpack
Export a tensor as a DLPack managed tensor (zero-copy).
tfe_tensor_f64_zeros
Create a tensor filled with zeros.

Type Aliases§

tfe_status_t
Status code type returned by all C-API functions.