Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:LaurentMazare Tch rs Tensor Fallible Generated

From Leeroopedia


Knowledge Sources
Domains Tensor_Operations, Code_Generation, FFI
Last Updated 2026-02-08 00:00 GMT

Overview

Auto-generated fallible (Result-returning) methods on Tensor that perform direct FFI calls to the underlying libtorch C library in the tch-rs library.

Description

The Tensor_Fallible_Generated module is an automatically generated file containing approximately 2,638 public methods on the Tensor type, each returning Result<Tensor, TchError>. These methods form the lowest Rust-level interface to PyTorch operations, calling through FFI to the C bindings provided by the torch-sys crate. Each method allocates a C tensor pointer array, invokes the corresponding atg_* function via the unsafe_torch_err! macro, and wraps the resulting C pointer in a Rust Tensor value.

This file is generated from PyTorch's operation declarations and should never be edited by hand. The header of the file states: THIS FILE IS AUTOMATICALLY GENERATED, DO NOT EDIT BY HAND!

The panicking wrappers in tensor_generated.rs delegate directly to these fallible methods.

Usage

Use the f_* methods when you need explicit error handling rather than panicking on failure. Each method name is prefixed with f_ followed by the operation name.

Code Reference

Source Location

Generation Pattern

Every method follows this FFI call pattern:

impl Tensor {
    pub fn f_method_name(&self, args...) -> Result<Tensor, TchError> {
        let mut c_tensors = [std::ptr::null_mut(); 1];
        unsafe_torch_err!(atg_method_name(
            c_tensors.as_mut_ptr(),
            self.c_tensor,
            // ... additional arguments
        ));
        Ok(Tensor { c_tensor: c_tensors[0] })
    }
}

For methods returning multiple tensors, the c_tensors array is sized accordingly and multiple Tensor values are constructed from the returned pointers.

Helper Functions

The file defines two helper functions for converting tensor lists to C pointer arrays:

fn ptr_list_opt<T: Borrow<Tensor>>(l: &[Option<T>]) -> Vec<*mut C_tensor> {
    l.iter().map(|x| x.as_ref().map_or(std::ptr::null_mut(), |x| x.borrow().c_tensor)).collect()
}

fn ptr_list<T: Borrow<Tensor>>(l: &[T]) -> Vec<*mut C_tensor> {
    l.iter().map(|x| x.borrow().c_tensor).collect()
}

Import

use tch::Tensor; // All f_* methods are inherent to Tensor

I/O Contract

Inputs

Name Type Required Description
self &Tensor or &mut Tensor Yes The tensor to operate on
args Varies per method Varies Operation-specific arguments (other tensors, scalars, dimensions, etc.)

Outputs

Name Type Description
result Result<Tensor, TchError> Ok(Tensor) on success, Err(TchError) on failure

Representative Methods

The following are illustrative examples from the 2,638 generated methods:

impl Tensor {
    // Element-wise addition via FFI
    pub fn f_add(&self, other: &Tensor) -> Result<Tensor, TchError> {
        let mut c_tensors = [std::ptr::null_mut(); 1];
        unsafe_torch_err!(atg_add(c_tensors.as_mut_ptr(), self.c_tensor, other.c_tensor));
        Ok(Tensor { c_tensor: c_tensors[0] })
    }

    // ReLU activation via FFI
    pub fn f_relu(&self) -> Result<Tensor, TchError> {
        let mut c_tensors = [std::ptr::null_mut(); 1];
        unsafe_torch_err!(atg_relu(c_tensors.as_mut_ptr(), self.c_tensor));
        Ok(Tensor { c_tensor: c_tensors[0] })
    }

    // In-place ReLU via FFI
    pub fn f_relu_(&mut self) -> Result<Tensor, TchError> {
        let mut c_tensors = [std::ptr::null_mut(); 1];
        unsafe_torch_err!(atg_relu_(c_tensors.as_mut_ptr(), self.c_tensor));
        Ok(Tensor { c_tensor: c_tensors[0] })
    }

    // Scalar addition via FFI
    pub fn f_add_scalar<S: Into<Scalar>>(&self, other: S) -> Result<Tensor, TchError> {
        let mut c_tensors = [std::ptr::null_mut(); 1];
        unsafe_torch_err!(atg_add_scalar(
            c_tensors.as_mut_ptr(),
            self.c_tensor,
            other.into().c_scalar
        ));
        Ok(Tensor { c_tensor: c_tensors[0] })
    }

    // Matrix multiplication via FFI
    pub fn f_matmul(&self, other: &Tensor) -> Result<Tensor, TchError> {
        let mut c_tensors = [std::ptr::null_mut(); 1];
        unsafe_torch_err!(atg_matmul(c_tensors.as_mut_ptr(), self.c_tensor, other.c_tensor));
        Ok(Tensor { c_tensor: c_tensors[0] })
    }

    // Softmax via FFI
    pub fn f_softmax(&self, dim: i64, dtype: impl Into<Option<Kind>>) -> Result<Tensor, TchError> {
        let mut c_tensors = [std::ptr::null_mut(); 1];
        unsafe_torch_err!(atg_softmax(
            c_tensors.as_mut_ptr(),
            self.c_tensor,
            dim,
            dtype.into().map_or(-1, |v| v.c_int())
        ));
        Ok(Tensor { c_tensor: c_tensors[0] })
    }
}

FFI Call Mechanism

The unsafe_torch_err! macro is the central safety boundary between Rust and the C libtorch library. It:

  1. Calls the atg_* function from the torch-sys crate within an unsafe block
  2. Checks for errors set by the C library
  3. Converts any C-side errors into TchError values

The atg_* functions are C bindings generated by the torch-sys build process, mapping 1:1 to PyTorch's C++ ATen operations.

Method Categories

The fallible methods mirror the same categories as tensor_generated.rs:

Category Examples
Arithmetic f_add, f_sub, f_mul, f_div, f_remainder
Linear Algebra f_matmul, f_mm, f_bmm, f_dot, f_addmm
Activation Functions f_relu, f_sigmoid, f_tanh, f_gelu, f_softmax
Reduction f_sum, f_mean, f_max, f_min, f_argmax
Shape Manipulation f_view, f_reshape, f_transpose, f_permute, f_squeeze
Convolution f_conv1d, f_conv2d, f_conv3d, f_conv_transpose2d
Creation (associated) f_zeros, f_ones, f_randn, f_rand, f_arange
In-place (trailing _) f_add_, f_mul_, f_zero_, f_fill_, f_copy_

Usage Examples

use tch::{Kind, TchError, Tensor};

// Fallible arithmetic with error handling
fn safe_matmul(a: &Tensor, b: &Tensor) -> Result<Tensor, TchError> {
    let c = a.f_matmul(b)?;
    let activated = c.f_relu()?;
    activated.f_softmax(1, Kind::Float)
}

// Pattern: fallible operations in a chain
let x = Tensor::randn([2, 3], (Kind::Float, tch::Device::Cpu));
let w = Tensor::randn([3, 5], (Kind::Float, tch::Device::Cpu));
match x.f_matmul(&w) {
    Ok(result) => println!("Success: {:?}", result.size()),
    Err(e) => eprintln!("Operation failed: {}", e),
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment