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:
TfeTensorF64is an opaque handle wrappingTensor<f64>. Host languages never see Rust internals. - Status codes: Every function takes a
*mut tfe_status_tas its last argument. Rust panics are caught withcatch_unwindand converted toTFE_INTERNAL_ERROR. - Stateless AD rules: Only
rrule(VJP) andfrule(JVP) are exposed. The AD tape /TrackedTensor/DualTensorare 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
_f64suffix. - DLPack interop: Zero-copy tensor exchange with Julia, Python, and
other frameworks via
DLManagedTensorVersioned. Supports CPU and GPU memory. Usetfe_tensor_f64_to_dlpack(export) andtfe_tensor_f64_from_dlpack(import). - Copy semantics for convenience functions:
tfe_tensor_f64_from_datacopies the caller’s data into a Rust-owned buffer. For zero-copy, use DLPack.
§Memory ownership
| Allocation | Freed by |
|---|---|
Tensor from _from_data / _zeros / _clone | tfe_tensor_f64_release |
Tensor from _from_dlpack | tfe_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 pointer | Caller (data is copied) |
DLManagedTensorVersioned from _to_dlpack | Consumer 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§
- DLData
Type - DLPack data type descriptor.
- DLDevice
- DLPack device descriptor.
- DLManaged
Tensor Versioned - DLPack managed tensor with version and ownership (DLPack v1.0+).
- DLPack
Version - DLPack version information.
- DLTensor
- DLPack tensor descriptor (unmanaged).
- TfeTensor
F64 - 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.