Implementation:LaurentMazare Tch rs Torch Api Generated H
| Knowledge Sources | |
|---|---|
| Domains | FFI, Code Generation, Deep Learning |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Auto-generated C header file declaring all generated tensor operation wrapper functions, providing the extern "C" declarations that both the C++ implementation and the Rust FFI bindings reference.
Description
torch_api_generated.h is a machine-generated header file comprising 2,643 lines of C function declarations. This file must not be edited by hand. It is produced alongside torch_api_generated.cpp by the tch-rs code generator.
The file structure is:
- A comment marking it as automatically generated
- An
#include "torch_api.h"to pull in the base type definitions (tensor,scalar,optimizer,module,ivalue) - An
extern "C"block containing all function declarations
Each declaration follows the pattern:
void atg_<operation_name>(tensor *, ...parameters...);
The first parameter is always a tensor * pointer used as an output array. Subsequent parameters vary based on the PyTorch operation signature. Common parameter types include:
tensor self- input tensorscalar other- scalar operandtensor other- tensor operandint64_t *data, int len- array-based dimension/size parametersint- boolean flags and enum valuesdouble- floating point parameters
The header covers the full range of PyTorch operations including arithmetic, linear algebra, convolutions, pooling, normalization, loss functions, activation functions, and more.
Usage
This header is included by torch_api_generated.cpp to ensure the implementations match the declarations. It transitively provides the opaque type definitions from torch_api.h. Developers should never modify this file; changes must come through the code generator.
Code Reference
Source Location
- Repository: LaurentMazare_Tch_rs
- File: torch-sys/libtch/torch_api_generated.h
- Lines: 1-2643
- Generation: THIS FILE IS AUTOMATICALLY GENERATED, DO NOT EDIT BY HAND
Signature
// THIS FILE IS AUTOMATICALLY GENERATED, DO NOT EDIT BY HAND!
#include "torch_api.h"
extern "C" {
void atg___and__(tensor *, tensor self, scalar other);
void atg___and__tensor_(tensor *, tensor self, tensor other);
void atg___iand__(tensor *, tensor self, scalar other);
void atg___iand__tensor_(tensor *, tensor self, tensor other);
void atg___ilshift__(tensor *, tensor self, scalar other);
void atg___ilshift__tensor_(tensor *, tensor self, tensor other);
void atg___ior__(tensor *, tensor self, scalar other);
void atg___ior__tensor_(tensor *, tensor self, tensor other);
// ... ~2600+ more declarations
void atg__adaptive_avg_pool2d(tensor *, tensor self,
int64_t *output_size_data, int output_size_len);
void atg__adaptive_avg_pool2d_backward(tensor *, tensor grad_output,
tensor self);
void atg__batch_norm_with_update(tensor *, tensor input, tensor weight,
tensor bias, tensor running_mean, tensor running_var,
double momentum, double eps);
}
Import
#include "torch_api.h"
I/O Contract
| Declaration Pattern | Description |
|---|---|
void atg_<op>(tensor *, ...) |
Standard operation: first param is output array, rest are inputs |
void atg_<op>_(tensor *, tensor self, ...) |
In-place variant: modifies self, still writes to output array |
void atg_<op>_out(tensor *, tensor out, ...) |
Pre-allocated output variant: writes result into provided out tensor
|
void atg_<op>_tensor_(tensor *, tensor self, tensor other) |
Tensor-tensor overload (trailing underscore distinguishes from scalar variant) |
| Type Mapping | C Type | PyTorch Type |
|---|---|---|
| Tensor | tensor (void* or torch::Tensor*) |
at::Tensor
|
| Scalar | scalar (void* or torch::Scalar*) |
at::Scalar
|
| Int array | int64_t *data, int len |
at::IntArrayRef
|
| Boolean | int (0 or 1) |
bool
|
| Dtype | int |
torch::ScalarType enum value
|
| Device | int |
Device index (-1=CPU, -2=MPS, -3=Vulkan, >=0=CUDA) |
Usage Examples
// These declarations enable Rust FFI binding:
// In Rust (c_generated.rs), the corresponding declaration is:
// pub fn atg___and__(out__: *mut *mut C_tensor,
// self_: *mut C_tensor,
// other_: *mut C_scalar);
// The header provides the contract that both the C++ implementation
// (torch_api_generated.cpp) and the Rust bindings (c_generated.rs)
// must satisfy.
Related Pages
- Principle:LaurentMazare_Tch_rs_Generated_FFI_Bindings
- Implementation:LaurentMazare_Tch_rs_Torch_Api_Generated_Cpp - C++ implementations of these declarations
- Implementation:LaurentMazare_Tch_rs_C_Generated_Rs - Rust FFI bindings matching these declarations
- Implementation:LaurentMazare_Tch_rs_Torch_Api_H - Base header defining types used in these declarations