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 Generated

From Leeroopedia


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

Overview

Auto-generated panicking wrapper methods on Tensor that delegate to the fallible (f_*) counterparts in the tch-rs library.

Description

The Tensor_Generated module is an automatically generated file containing approximately 2,638 public methods on the Tensor type. Each method is a thin, panicking wrapper that calls the corresponding fallible method (prefixed with f_) and unwraps the Result. This provides a convenient, concise API for cases where the caller expects operations to succeed and prefers a panic on failure rather than explicit error handling.

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!

Usage

These methods are available directly on any Tensor value. They provide the primary ergonomic API surface for the tch-rs library, covering the full breadth of PyTorch tensor operations.

Code Reference

Source Location

Generation Pattern

Every method in this file follows the same delegation pattern:

impl Tensor {
    pub fn method_name(&self, args...) -> Tensor {
        self.f_method_name(args...).unwrap()
    }
}

For in-place methods (suffixed with _), the receiver is &mut self:

impl Tensor {
    pub fn method_name_(&mut self, args...) -> Tensor {
        self.f_method_name_(args...).unwrap()
    }
}

Import

use tch::Tensor; // All generated 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 Tensor The result tensor (panics via .unwrap() on error)

Representative Methods

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

impl Tensor {
    // Element-wise addition
    pub fn add(&self, other: &Tensor) -> Tensor {
        self.f_add(other).unwrap()
    }

    // Matrix multiplication
    pub fn matmul(&self, other: &Tensor) -> Tensor {
        self.f_matmul(other).unwrap()
    }

    // ReLU activation
    pub fn relu(&self) -> Tensor {
        self.f_relu().unwrap()
    }

    // Softmax along a dimension
    pub fn softmax(&self, dim: i64, dtype: impl Into<Option<Kind>>) -> Tensor {
        self.f_softmax(dim, dtype).unwrap()
    }

    // In-place addition
    pub fn add_(&mut self, other: &Tensor) -> Tensor {
        self.f_add_(other).unwrap()
    }

    // Scalar addition
    pub fn add_scalar<S: Into<Scalar>>(&self, other: S) -> Tensor {
        self.f_add_scalar(other).unwrap()
    }
}

Method Categories

The generated methods span the following categories of PyTorch operations:

Category Examples
Arithmetic add, sub, mul, div, remainder, fmod
Linear Algebra matmul, mm, bmm, dot, mv, addmm
Activation Functions relu, sigmoid, tanh, gelu, softmax, log_softmax
Reduction sum, mean, max, min, prod, argmax, argmin
Shape Manipulation view, reshape, transpose, permute, squeeze, unsqueeze, flatten
Convolution conv1d, conv2d, conv3d, conv_transpose2d
Pooling max_pool2d, avg_pool2d, adaptive_avg_pool2d
Normalization batch_norm, layer_norm, group_norm
Element-wise Math abs, exp, log, sqrt, pow, sin, cos, clamp
Comparison eq_tensor, gt, lt, ge, le, ne_tensor
Creation (associated) zeros, ones, randn, rand, full, arange, linspace
Indexing index_select, gather, scatter, masked_select, where_self
In-place (trailing _) add_, mul_, zero_, fill_, copy_

Usage Examples

use tch::{Kind, Tensor};

// Basic arithmetic
let a = Tensor::randn([3, 4], (Kind::Float, tch::Device::Cpu));
let b = Tensor::randn([3, 4], (Kind::Float, tch::Device::Cpu));
let c = a.add(&b);

// Matrix multiplication
let x = Tensor::randn([2, 3], (Kind::Float, tch::Device::Cpu));
let w = Tensor::randn([3, 5], (Kind::Float, tch::Device::Cpu));
let y = x.matmul(&w);  // Shape: [2, 5]

// Chained operations (panics on error)
let output = x.matmul(&w).relu().softmax(1, Kind::Float);

Related Pages

Page Connections

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