You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/source/notes/libtorch_stable_abi.md
+24-21Lines changed: 24 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,46 +2,46 @@
2
2
3
3
## Overview
4
4
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.
6
6
7
7
The stable ABI consists of three main components:
8
8
9
9
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/*`)
11
11
3.**Stable C++ wrappers** - High-level C++ convenience wrappers (`torch/csrc/stable/*`)
12
12
13
13
We discuss each of these in detail
14
14
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
-
23
15
### `torch/headeronly`
24
16
25
17
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
26
18
`c10::ScalarType` enum lives here as `torch::headeronly::ScalarType`.
27
19
28
20
### `torch/csrc/stable`
29
21
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.
32
24
33
25
It consists of
34
26
35
27
- 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`.
38
30
- 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.
40
34
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
42
36
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.
43
39
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?
45
45
46
46
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
47
47
1. StableIValue Conversions
@@ -108,7 +108,7 @@ There are two invariants for the stack:
108
108
The above is relevant in two places:
109
109
110
110
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`.
112
112
113
113
```cpp
114
114
Tensor my_amax_vec(Tensor t) {
@@ -123,8 +123,11 @@ The above is relevant in two places:
123
123
```
124
124
125
125
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:
0 commit comments