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 / TrackedValue / DualValue are Rust-internal and not exposed via FFI.
  • f64 only in this POC phase. All functions carry the _f64 suffix.
  • DLPack interop: Zero-copy tensor exchange via DLManagedTensorVersioned.
  • Copy semantics for convenience functions: tfe_tensor_f64_from_data copies the caller’s data into a Rust-owned buffer.

§Header generation

cbindgen --config cbindgen.toml --crate tenferro-capi --output tenferro.h

§Examples

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);
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.
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.
DLPACK_FLAG_BITMASK_READ_ONLY
Data is read-only.
KDLCOMPLEX
Complex type code.
KDLCPU
CPU device.
KDLCUDA
NVIDIA CUDA GPU device memory.
KDLCUDA_HOST
Pinned CUDA CPU memory.
KDLCUDA_MANAGED
CUDA managed/unified memory.
KDLFLOAT
Floating-point type code.
KDLINT
Integer type code.
KDLROCM
AMD ROCm GPU device memory.
KDLROCM_HOST
Pinned ROCm CPU memory.
TFE_BUFFER_TOO_SMALL
Output buffer is too small for the requested data.
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_last_error_message
Retrieve the last error message (UTF-8, null-terminated).
tfe_svd_f64
Compute the SVD of a tensor after matricizing by left and right.
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.