Implementation:LaurentMazare Tch rs C Generated Rs
| Knowledge Sources | |
|---|---|
| Domains | FFI, Code Generation, Rust |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Auto-generated Rust FFI bindings file containing extern "C" declarations for all generated PyTorch tensor operations, providing the Rust-side counterpart to the C header torch_api_generated.h.
Description
c_generated.rs is a machine-generated Rust source file comprising 16,003 lines of extern "C" function declarations. This file must not be edited by hand. It is produced by the same code generator that creates the C++ implementation and header files, ensuring perfect alignment between the Rust declarations and the C++ symbols.
The file begins with:
- A comment marking it as automatically generated
#[allow(clippy::all)]to suppress all Clippy lints on generated codeuse crate::{C_scalar, C_tensor};to import the opaque FFI types from lib.rsuse libc::c_int;for C integer type compatibility
Every function declaration follows the same pattern as the C header, but expressed in Rust FFI syntax:
pub fn atg_<operation_name>(
out__: *mut *mut C_tensor,
self_: *mut C_tensor,
other_: *mut C_scalar, // or *mut C_tensor for tensor variant
);
Key characteristics of the generated bindings:
- All pointers are raw:
*mut *mut C_tensorfor output arrays,*mut C_tensorfor input tensors - Rust keyword avoidance: Parameters that would collide with Rust keywords use trailing underscores (e.g.,
self_,out_) - C type mapping:
c_intfor C integers,f64/i64for numeric types,*mut C_scalarfor scalars - Array parameters: Represented as
*const i64pointer plusc_intlength
The generated functions cover the complete set of PyTorch tensor operations including:
- Arithmetic:
atg_add,atg_sub,atg_mul,atg_div - Linear algebra:
atg_mm,atg_bmm,atg_matmul,atg_svd - Neural network layers:
atg_conv2d,atg_linear,atg_batch_norm - Activations:
atg_relu,atg_sigmoid,atg_tanh,atg_gelu - Reductions:
atg_sum,atg_mean,atg_max,atg_min - Shape operations:
atg_reshape,atg_view,atg_permute,atg_cat
Usage
This module is declared in lib.rs via pub mod c_generated; and re-exported as part of the torch-sys crate. The higher-level tch crate calls these unsafe FFI functions through safe Rust wrappers that handle error checking (via get_and_reset_last_err) and memory management (dropping allocated tensors). Developers should never modify this file; changes must come through the code generator.
Code Reference
Source Location
- Repository: LaurentMazare_Tch_rs
- File: torch-sys/src/c_generated.rs
- Lines: 1-16003
- Generation: THIS FILE IS AUTOMATICALLY GENERATED, DO NOT EDIT BY HAND
Signature
/* THIS FILE IS AUTOMATICALLY GENERATED, DO NOT EDIT BY HAND! */
#[allow(clippy::all)]
use crate::{C_scalar, C_tensor};
use libc::c_int;
extern "C" {
pub fn atg___and__(
out__: *mut *mut C_tensor,
self_: *mut C_tensor,
other_: *mut C_scalar,
);
pub fn atg___and__tensor_(
out__: *mut *mut C_tensor,
self_: *mut C_tensor,
other_: *mut C_tensor,
);
pub fn atg___iand__(
out__: *mut *mut C_tensor,
self_: *mut C_tensor,
other_: *mut C_scalar,
);
// ... ~2600+ more declarations
}
Import
// In torch-sys/src/lib.rs:
pub mod c_generated;
// In downstream tch crate code:
use torch_sys::c_generated::*;
I/O Contract
| Rust FFI Type | C Equivalent | Description |
|---|---|---|
*mut *mut C_tensor |
tensor * |
Output array of tensor pointers; caller allocates, callee populates |
*mut C_tensor |
tensor |
Input tensor pointer (opaque) |
*mut C_scalar |
scalar |
Input scalar pointer (opaque) |
c_int |
int |
C integer for flags, enum values, array lengths |
i64 |
int64_t |
64-bit integer for dimensions, indices |
f64 |
double |
64-bit float for learning rates, epsilon, etc. |
*const i64 + c_int |
int64_t *data, int len |
Array parameter passed as pointer + length |
| Safety Consideration | Description |
|---|---|
| All functions are unsafe | Must be called within an unsafe block in Rust
|
| Null check required | Caller must check get_and_reset_last_err() after each call
|
| Memory ownership | Returned tensor pointers must eventually be freed via at_free
|
| Output array sizing | Caller must allocate sufficient output slots (check operation's output count) |
Usage Examples
use torch_sys::{C_tensor, C_scalar, c_generated};
use std::ptr;
unsafe {
// Allocate output slot
let mut output: *mut C_tensor = ptr::null_mut();
// Call generated FFI function
c_generated::atg___and__(&mut output, self_tensor, scalar_value);
// Check for errors
let err = torch_sys::get_and_reset_last_err();
if !err.is_null() {
let err_str = std::ffi::CStr::from_ptr(err).to_string_lossy();
libc::free(err as *mut libc::c_void);
panic!("FFI error: {}", err_str);
}
// output now contains a valid tensor pointer
// Must eventually call at_free(output) to release memory
}
Related Pages
- Principle:LaurentMazare_Tch_rs_Generated_FFI_Bindings
- Implementation:LaurentMazare_Tch_rs_Torch_Api_Generated_H - C header these bindings correspond to
- Implementation:LaurentMazare_Tch_rs_Torch_Api_Generated_Cpp - C++ implementations of these functions
- Implementation:LaurentMazare_Tch_rs_Torch_Sys_Lib - Parent module that declares this as
pub mod c_generated