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 Torch Api Generated Cpp

From Leeroopedia


Knowledge Sources
Domains FFI, Code Generation, Deep Learning
Last Updated 2026-02-08 00:00 GMT

Overview

Auto-generated C++ file containing wrapper functions for all PyTorch tensor operations, providing C-linkage entry points that bridge Rust FFI calls to the PyTorch C++ API.

Description

torch_api_generated.cpp is a machine-generated file comprising 19,043 lines of C++ code. This file must not be edited by hand. It is produced by the code generation tooling in the tch-rs repository (the gen directory) which parses the PyTorch C++ API and emits a C wrapper function for each tensor operation.

Every function in this file follows an identical pattern:

  1. The function name is prefixed with atg_ (auto-generated tensor).
  2. It takes a tensor *out__ pointer as its first argument, which is used to return one or more output tensors.
  3. The function body is wrapped in the PROTECT macro (defined in torch_api.h) for exception-to-error-code translation.
  4. Inside the PROTECT block, it calls the corresponding torch:: or self-> method, then stores the result via out__[0] = new torch::Tensor(outputs__).

For operations that return multiple tensors (e.g., _aminmax), multiple output slots are populated: out__[0], out__[1], etc.

The naming convention distinguishes variants:

  • atg_add - tensor + scalar variant
  • atg_add_tensor_ - tensor + tensor variant (trailing underscore indicates tensor overload)
  • atg_add_ - in-place variant
  • atg_add_out - pre-allocated output variant

Usage

This file is compiled as part of the torch-sys crate build and linked into the final binary. Rust code calls these functions through the declarations in c_generated.rs. Developers should never modify this file directly; instead, they should update the code generator if new operations or signature changes are needed.

Code Reference

Source Location

Signature

// Canonical pattern for a generated wrapper function:
void atg___and__(tensor *out__, tensor self, scalar other) {
  PROTECT(
    auto outputs__ = torch::__and__(*self, *other);
    out__[0] = new torch::Tensor(outputs__);
  )
}

// Tensor-tensor overload variant:
void atg___and__tensor_(tensor *out__, tensor self, tensor other) {
  PROTECT(
    auto outputs__ = torch::__and__(*self, *other);
    out__[0] = new torch::Tensor(outputs__);
  )
}

// In-place variant:
void atg___iand__(tensor *out__, tensor self, scalar other) {
  PROTECT(
    auto outputs__ = self->__iand__(*other);
    out__[0] = new torch::Tensor(outputs__);
  )
}

// Multi-output function example:
void atg__aminmax(tensor *out__, tensor self) {
  PROTECT(
    auto outputs__ = torch::_aminmax(*self);
    out__[0] = new torch::Tensor(std::get<0>(outputs__));
    out__[1] = new torch::Tensor(std::get<1>(outputs__));
  )
}

Import

// THIS FILE IS AUTOMATICALLY GENERATED, DO NOT EDIT BY HAND!
#include "torch_api_generated.h"

I/O Contract

Parameter Type Description
out__ tensor* (pointer to array of torch::Tensor*) Caller-allocated output array; function populates out__[0], out__[1], etc. with heap-allocated tensors
self tensor (torch::Tensor*) The input tensor on which the operation is performed
other scalar or tensor Second operand (scalar or tensor variant)
Dimension args int64_t* + int length Array-based dimension parameters (e.g., output_size_data, output_size_len)
Boolean flags int Integer booleans (0/1) for flags like keepdim, non_blocking
Output Behavior Description
Success Output tensor(s) written to out__ array slots; torch_last_err remains nullptr
Error torch_last_err set to a strdup'd error message via the PROTECT macro; output slots may be uninitialized

Usage Examples

// Calling a generated function from C (or via Rust FFI):
tensor out[1];
tensor self_tensor = at_new_tensor();  // some existing tensor
scalar s = ats_float(2.0);

// Perform element-wise AND with a scalar
atg___and__(out, self_tensor, s);
// out[0] now contains the result tensor

// Error check
char *err = get_and_reset_last_err();
if (err != nullptr) {
    // handle error
    free(err);
}

Related Pages

Page Connections

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