Skip to content

Commit f6bc827

Browse files
Update on "[BE] Refresh documentation for stable ABI / API"
[ghstack-poisoned]
2 parents 1c27bbb + 8ffd1fd commit f6bc827

File tree

1 file changed

+24
-21
lines changed

1 file changed

+24
-21
lines changed

docs/source/notes/libtorch_stable_abi.md

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,46 @@
22

33
## Overview
44

5-
The LibTorch Stable ABI (Application Binary Interface) provides a stable interface for extending PyTorch functionality without being tightly coupled to specific PyTorch versions. This enables the development of custom operators and extensions that remain compatible across PyTorch releases.
5+
The LibTorch Stable ABI (Application Binary Interface) provides an interface for extending PyTorch functionality without being tightly coupled to specific PyTorch versions. This enables the development of custom operators and extensions that remain compatible across PyTorch releases.
66

77
The stable ABI consists of three main components:
88

99
1. **Stable C headers** - Low-level C API implemented by libtorch (primarily `torch/csrc/inductor/aoti_torch/c/shim.h`)
10-
2. **Header-only C++ library** - Standalone utilities (`torch/headeronly/*`)
10+
2. **Header-only C++ library** - Standalone utilities implemented in only headers such that there is no dependence on libtorch (`torch/headeronly/*`)
1111
3. **Stable C++ wrappers** - High-level C++ convenience wrappers (`torch/csrc/stable/*`)
1212

1313
We discuss each of these in detail
1414

15-
### Stable C headers
16-
17-
The stable C headers used by AOTInductor form the foundation of the stable ABI. However, this is **use at your own risk**. For example, users must handle the memory lifecycle of objects returned by certain APIs.
18-
Further, the stack-based APIs discussed below which allow the user to call the PyTorch dispatcher don't provide strong guarantees on forward and backward compatibility.
19-
20-
Unless absolutely necessary, we recommend the high-level C++ API in `torch/csrc/stable`
21-
which will handle all the rough edges of the C API for the user.
22-
2315
### `torch/headeronly`
2416

2517
This is a set of inlined C++ headers are completely decoupled from libtorch. The headers consist of certain utilities that might be familiar to custom extension writers. For example, the
2618
`c10::ScalarType` enum lives here as `torch::headeronly::ScalarType`.
2719

2820
### `torch/csrc/stable`
2921

30-
This is a set of inlined C++ headers that provide wrappers around the C API that handle the footguns
31-
discussed above.
22+
This is a set of inlined C++ headers that provide wrappers around the C API that handle the rough edges
23+
discussed below.
3224

3325
It consists of
3426

3527
- torch/csrc/stable/library.h: Provides a stable version of TORCH_LIBRARY and similar macros.
36-
- torch/csrc/stable/tensor.h: Provides torch::stable::Tensor, a stable version of at::Tensor.
37-
- torch/csrc/stable/ops.h: Provides a stable interface for calling ops from `native_functions.yaml`.
28+
- torch/csrc/stable/tensor_struct.h: Provides torch::stable::Tensor, a stable version of at::Tensor.
29+
- torch/csrc/stable/ops.h: Provides a stable interface for calling ATen ops from `native_functions.yaml`.
3830
- torch/csrc/stable/accelerator.h: Provides a stable interface for device-generic objects and APIs
39-
(e.g. `getCurrentStream`).
31+
(e.g. `getCurrentStream`, `DeviceGuard`).
32+
33+
We are continuing to improve coverage in our `torch/csrc/stable` APIs. Please file an issue if you'd like to see support for particular APIs in your custom extension.
4034

41-
The interface provided by `torch/csrc/stable` should be familiar to custom extension writers but might not match perfectly in certain cases.
35+
### Stable C headers
4236

37+
The stable C headers used by AOTInductor form the foundation of the stable ABI. However, this is **use at your own risk**. For example, users must handle the memory lifecycle of objects returned by certain APIs.
38+
Further, the stack-based APIs discussed below which allow the user to call the PyTorch dispatcher don't provide strong guarantees on forward and backward compatibility.
4339

44-
## How are objects passed across ABI boundaries when interacting with the dispatcher?
40+
Unless absolutely necessary, we recommend the high-level C++ API in `torch/csrc/stable`
41+
which will handle all the rough edges of the C API for the user.
42+
43+
44+
## How are objects passed across the ABI boundary when interacting with the dispatcher?
4545

4646
When interacting with the dispatcher via the stable APIs (``STABLE_TORCH_LIBRARY`` etc.) we use a boxed convention. Arguments and returns are represented as a stack of ``StableIValue`` which correlates with a `torch::jit::stack` of IValues. We discuss the following below
4747
1. StableIValue Conversions
@@ -108,7 +108,7 @@ There are two invariants for the stack:
108108
The above is relevant in two places:
109109

110110
1. `STABLE_TORCH_LIBRARY`
111-
Unlike `TORCH_LIBRARY`, the dispatcher expects kernels registered via `STABLE_TORCH_LIBRARY` to be boxed. This means they must have the signature `(StableIValue* stack, uint64_t num_args, uint64_t num_outputs) -> void`. This will soon be abstracted away from the user, but for the time being, users can use the `from` and `to`.
111+
Unlike `TORCH_LIBRARY`, the dispatcher expects kernels registered via `STABLE_TORCH_LIBRARY` to be boxed. This means they must have the signature `(StableIValue* stack, uint64_t num_args, uint64_t num_outputs) -> void`.We plan to eventually abstract away the need for manual boxing, but, for the time being, please use `from` and `to`.
112112

113113
```cpp
114114
Tensor my_amax_vec(Tensor t) {
@@ -123,8 +123,11 @@ The above is relevant in two places:
123123
```
124124

125125
2. `aoti_torch_call_dispatcher`
126-
127-
This has the signature
126+
This API allows you to call the PyTorch dispatcher from C/C++ code. It has the following signature:
128127
```cpp
129-
aoti_torch_call_dispatcher(const char* opName, const char* overloadName, StableIValue* stack)
128+
aoti_torch_call_dispatcher(const char* opName, const char* overloadName, StableIValue* stack);
130129
```
130+
131+
`aoti_torch_call_dispatcher` will call the op overload defined by a given `opName`, `overloadName`, and a stack of
132+
StableIValues. This call will populate any return values of the op into the stack in their StableIValue form,
133+
with `ret0` at index 0, `ret1` at index 1, and so on.

0 commit comments

Comments
 (0)