Implementation:LaurentMazare Tch rs Torch Api Generated Cpp
| 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:
- The function name is prefixed with
atg_(auto-generated tensor). - It takes a
tensor *out__pointer as its first argument, which is used to return one or more output tensors. - The function body is wrapped in the
PROTECTmacro (defined in torch_api.h) for exception-to-error-code translation. - Inside the PROTECT block, it calls the corresponding
torch::orself->method, then stores the result viaout__[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 variantatg_add_tensor_- tensor + tensor variant (trailing underscore indicates tensor overload)atg_add_- in-place variantatg_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
- Repository: LaurentMazare_Tch_rs
- File: torch-sys/libtch/torch_api_generated.cpp
- Lines: 1-19043
- Generation: THIS FILE IS AUTOMATICALLY GENERATED, DO NOT EDIT BY HAND
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
- Principle:LaurentMazare_Tch_rs_Generated_FFI_Bindings
- Implementation:LaurentMazare_Tch_rs_Torch_Api_Generated_H - Header file declaring these functions
- Implementation:LaurentMazare_Tch_rs_C_Generated_Rs - Rust FFI bindings matching these C++ functions
- Implementation:LaurentMazare_Tch_rs_Torch_Api_Cpp - Hand-written companion C++ implementation
- Implementation:LaurentMazare_Tch_rs_Torch_Api_H - Base header defining types and PROTECT macro