Skip to content

Commit b379b19

Browse files
authored
Merge pull request #41 from pytorch/gh/larryliu0820/1/base
[PyTorch] Add RFC for lightweight dispatch
2 parents 0ef2c3f + 2f8e3ca commit b379b19

File tree

1 file changed

+339
-0
lines changed

1 file changed

+339
-0
lines changed

RFC-0020-Lightweight-Dispatch.md

+339
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,339 @@
1+
# Context
2+
Currently we rely on `TORCH_LIBRARY` [API](https://pytorch.org/tutorials/advanced/dispatcher.html) to register operators into dispatcher. As PyTorch aims to support a wider set of devices and use cases, the issue of op registration/dispatching static initialization time and runtime overhead as well as build-time complexity has risen to the fore, we see the need for a lighter version of our operator dispatching mechanism.
3+
4+
We thought about possible solutions:
5+
* One option is to keep using the torch-library C++ API to register/dispatch ops, but this will necessitate careful cost-cutting for these use cases with more and more intrusive “#ifdef” customizations in the core framework.
6+
* The other option (which is what we propose here) is to utilize the function schema yaml file to "declare" ops and then use the codegen framework to generate lightweight code for runtime execution.
7+
8+
The essential point of this proposal is that the function schema DSL (which we use to declare the standard ATen ops) combined with the codegen framework (which we use to generate the registrations, dispatch stubs and other “glue” code for ATen ops) is the bare minimum set of reusable tools for building custom extensions that are compatible with the PyTorch ecosystem.
9+
10+
# Motivation
11+
* **Performance**
12+
* For recent use cases of mobile interpreter, we need to satisfy more and more strict initialization latency requirements, where analysis shows op registration contributes to a large portion of it.
13+
* With existing meta-programming based unboxing logic shared between mobile and server, it’s relatively inflexible to introduce optimizations.
14+
* Also with static dispatch, we don’t have to register all of the ops into the JIT op registry, which saves runtime memory usage and further reduces static initialization time.
15+
* It is possible to avoid dispatching at runtime.
16+
* **Modularity and binary size**
17+
* Currently the mobile runtime consists of both JIT op registry and c10 dispatcher. This project will make it possible to not depend on the c10 dispatcher (opt-in), delivering a cleaner runtime library.
18+
* This project creates an opportunity to reduce binary size by getting rid of the dispatcher and enables further size optimization on unboxing wrappers.
19+
* **Ability to incorporate custom implementation of ATen ops**
20+
* For some of the mobile use cases, we need to support custom implementations of ATen ops. With an extra op registration path such as codegen unboxing it is easier to hookup ops with custom native functions.
21+
22+
# Overview
23+
![codegen drawio](https://user-images.githubusercontent.com/8188269/154173938-baad9ee6-0e3c-40bb-a9d6-649137e3f3f9.png)
24+
25+
26+
Currently the mobile interpreter registers all ATen ops into the dispatcher and some other ops into the JIT op registry. At model inference time, the interpreter will look for the operator name in the JIT op registry first, if not found then it will look into the dispatcher. This proposal **adds a build flavor that moves these ATen ops from dispatcher to JIT op registry** so that it’s easier to optimize (e.g. avoid schema parsing) and reduce dependencies.
27+
28+
The interpreter is looking for a boxed function but our native implementation is unboxed. We need “glue code” to hook up these two. This proposal **extends the capabilities of codegen to generate the unboxing wrappers for operators**, as well as the code to register them into the JIT op registry. The interpreter will call generated unboxing wrappers, inside these wrappers we pop out values from the stack, and delegate to the unboxed API.
29+
30+
To avoid hitting the dispatcher from the unboxed API, we will choose static dispatch so that we hit native functions from the unboxed API directly. To make sure we have feature parity as the default build, this proposal **adds support for multiple backends in static dispatch**.
31+
32+
In addition to that, this proposal also supports features critical to mobile use cases, such as **tracing based selective build** and **runtime modularization** work.
33+
34+
# Step by step walkthrough
35+
36+
How will our new codegen unboxing wrapper fit into the picture of op registration and dispatching? For these use cases, we only need per-op codegen unboxing (red box on the left) as well as static dispatch. This way we can avoid all dependencies on c10::Dispatcher.
37+
38+
We are going to break the project down into three parts, for **step 1 we are going to implement the codegen logic** and generate code based on [native_functions.yaml](https://fburl.com/code/2wkgwyoq), then we are going to verify the flow that we are able to find jit op in the registry and eventually call codegen unboxing wrapper (the red flow on the left). **Step 2 will focus on how to make sure we have feature parity** with the original op registration and dispatch system, with tasks like supporting multiple backends in static dispatch, supporting custom ops as well as custom kernels for ATen ops. For **step 3 we are going to integrate with some target hardware platforms** to validate latency and binary size improvements. These are the problems we need to address in step 3 including: avoiding schema parsing at library init time, supporting tracing based selective build. The goal of step 3 is to make sure per-op codegen unboxing works for our target hardware platforms and is ready to ship to production use cases.
39+
40+
41+
### Step 1
42+
43+
Bring back the unboxing kernel codegen using the new codegen framework. And make the registration no-op when we turn on the static root-op dispatch for lightweight dispatch use cases. All tasks in step 1 are based on the server version of PyTorch interpreter.
44+
45+
46+
#### Codegen core logic
47+
48+
These tasks will generate C++ code that pops IValues out from a stack and casts them to their corresponding C++ types. This core logic should be shared across two types of codegens so that it can be covered by all the existing tests on server side.
49+
50+
51+
52+
* **JIT type -> C++ type**. This is necessary for some of the optional C++ types, e.g., we need to map `int` to `int64_t` for the last argument in the example.
53+
* This is already done in [types.py](https://github.com/pytorch/pytorch/blob/master/tools/codegen/api/types.py), and we need to integrate it into our new codegen.
54+
* **JIT type -> IValue to basic type conversion C++ code.** E.g., the first argument of this operator: `Tensor(a) self` needs to be translated to: `(std::move(peek(stack, 0, 4))).toTensor()`
55+
* IValue provides APIs to directly convert an IValue to these basic types. See [ivalue_inl.h](https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/core/ivalue_inl.h#L1453-L1493)
56+
* Here’s a [list](#bookmark=id.deyvpbsb5yel) of all the JIT types appearing in native_functions.yaml, most of them can be converted using IValue’s API.
57+
* Add a binding function between a JIT type to a piece of C++ code that converts IValue to a specific C++ type.
58+
* **JIT type -> IValue to ArrayRef type conversion C++ code. **IValue doesn’t provide explicit APIs for these ArrayRef types, but they are widely used in native_functions.yaml.
59+
* We can use the meta programming logic ([make_boxed_from_unboxed_functor.h](https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/core/boxing/impl/make_boxed_from_unboxed_functor.h#L354)) as reference, convert the ivalue to vector then to ArrayRef.
60+
* **JIT type -> IValue to TensorOptions type conversion C++ code.**
61+
* Handle TensorOptions (that is not 1-1 mapping across two types of arguments), we can refer to [python.py](https://github.com/pytorch/pytorch/blob/master/tools/codegen/api/python.py#L999-L1068), maybe follow the logic over there.
62+
* **JIT schema -> unboxed function**. With all the arguments being translated, generate the C++ code to call the correct unboxed function and return the result (push it back to stack).
63+
* Figure out how to map schema to unboxed C++ function. Reference [python.py](https://github.com/pytorch/pytorch/blob/master/tools/codegen/api/python.py#L955)
64+
* Deal with method and function separately, also handle the `out` cases.
65+
66+
67+
#### Codegen source file details
68+
69+
With the logic from the previous section, we should be able to wrap the code into a function pointer and register it into [torch::jit::OperatorRegistry](https://github.com/pytorch/pytorch/blob/master/torch/csrc/jit/runtime/operator.cpp#L19).
70+
71+
72+
73+
* Wrap generated C++ code in [OperatorGenerator](https://github.com/pytorch/pytorch/blob/master/torch/csrc/jit/runtime/operator.h#L221) so that it gets registered into the registry. Generate code for all functions in [native_functions.yaml](https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/native_functions.yaml). Code snippet as an example:
74+
```cpp
75+
RegisterCodegenUnboxedKernels.cpp
76+
===================
77+
RegisterOperators reg({
78+
OperatorGenerator(
79+
TORCH_SELECTIVE_SCHEMA("aten::get_device(Tensor self) -> int"),
80+
[](Stack & stack) {
81+
RECORD_FUNCTION("get_device", std::vector<c10::IValue>());
82+
at::unboxing::get_device(stack);
83+
},
84+
aliasAnalysisFromSchema()
85+
),
86+
...
87+
})
88+
UnboxingFunctions.h
89+
===================
90+
namespace at {
91+
namespace unboxing {
92+
93+
TORCH_API at::Tensor get_device(Stack & stack);
94+
95+
} // namespace unboxing
96+
} // namespace at
97+
98+
UnboxingFunctions.cpp
99+
=====================
100+
namespace at {
101+
namespace unboxing {
102+
103+
TORCH_API at::Tensor get_device(Stack & stack) {
104+
auto result = at::get_device(
105+
(std::move(peek(stack, 0, 1))).toTensor()
106+
);
107+
drop(stack, 1);
108+
pack(stack, std::move(result));
109+
}
110+
111+
} // namespace unboxing
112+
} // namespace at
113+
```
114+
115+
116+
117+
118+
* Generate separate header/cpp for codegen unboxing wrapper. We should put the codegen unboxing body into a separate function with dedicated namespace so that it is on par with the other codegen (Functions.h).
119+
* Compile generated code with current runtime and make sure the calls to ATen ops are getting dispatched to our codegen’d unboxing wrapper.
120+
* The easiest way to test is to generate a wrapper that prints out/throws an exception. Then we can execute a scripted module to trigger the dispatch.
121+
#### Server & OSS integration
122+
123+
**Bringing codegen unboxing to server build is out of the scope of this project.** We evaluated the option of replacing JIT op registration hook (`register_c10_ops.cpp`) with codegen unboxing wrappers, but realized that effort needs proper design and a lot of effort and only brings in small value:
124+
125+
126+
127+
* Having two op registration mechanisms brings more confusion.
128+
* For the scenario of adding a new operator (not to `native_functions.yaml`), we need to provide clear guidance to add it to the JIT op registry as well, otherwise JIT execution will break.
129+
* We can add tests on the mobile build for the sake of coverage.
130+
131+
For OSS mobile integration, we will need to have a new build flavor to switch between c10 dispatcher vs jit op registry. This new flavor will include codegen source files (`UnboxingFunctions.h, UnboxingFunctions.cpp, RegisterCodegenUnboxedKernels.cpp`) instead of existing dispatcher related source files: `Operators.cpp`, `RegisterSchema.cpp `etc, similar to the internal build configuration. Again, this will be delivered as a build flavor for user to opt in, the dispatcher will be used by default.
132+
133+
134+
135+
### Step 2
136+
137+
With step 1 we already have a working codegen unboxing + static dispatch system working but it only works for the `CPU` backend. Nowadays most models being deployed on edge devices are quantized models so we will need to support both `CPU` and `QuantizedCPU` backend. In addition to that, a lot of our models feature custom ops, however we can’t register custom ops through the old dispatcher (`TORCH_LIBRARY`) APIs any more. Here I’m proposing a solution that exposes the `native_function.yaml` syntax to the internal developers targeting this runtime mode: allow them to use the yaml file format to declare their custom ops and/or custom kernels.
138+
139+
140+
141+
142+
#### Support multiple backends in static dispatch
143+
144+
**NOTE: this may be optional if we enabled backend tracing for ops.** For the vast majority of models, we will only have 1 backend per operator, meaning that if we can pass the backend info into codegen, we don’t have to do dispatch based on dispatch key.
145+
146+
In the scenario that a model contains both floating point ops and quantized ops, our codegen should be able to statically dispatch to the correct backend. The following diagram shows what will be generated and included in the build and demonstrates the dependency relationship.
147+
148+
Let’s take `acosh` as an example:
149+
```yaml
150+
native_functions.yaml
151+
=====================
152+
- func: acosh(Tensor self) -> Tensor
153+
variants: function, method
154+
structured_delegate: acosh.out
155+
156+
- func: acosh.out(Tensor self, *, Tensor(a!) out) -> Tensor(a!)
157+
structured: True
158+
structured_inherits: TensorIteratorBase
159+
dispatch:
160+
CPU, CUDA: acosh_out
161+
```
162+
163+
And if we pass the backends we want to codegen for both `CPU` and `QuantizedCPU` backends, our `Functions.h` will be something like this (borrowing from Jiakai’s [PR](https://github.com/pytorch/pytorch/pull/51554/commits/0ba3d4cc42187f69f17e0c382f0ab51e071a4a44)):
164+
165+
```cpp
166+
Functions.h
167+
===========
168+
// aten::acosh(Tensor self) -> Tensor
169+
TORCH_API inline at::Tensor acosh(const at::Tensor & self) {
170+
DispatchKeySet _dk_set = c10::detail::multi_dispatch_key_set(tensor);
171+
DispatchKey _dk = _dk_set.highestPriorityBackendTypeId();
172+
switch (_dk) {
173+
case DispatchKey::CPU:
174+
return at::cpu::acosh(self);
175+
case DispatchKey::QuantizedCPU:
176+
default:
177+
TORCH_CHECK(false, "Unsupported static dispatch", _dk);
178+
}
179+
}
180+
```
181+
Also we will generate these files:
182+
183+
184+
185+
* `CPUFunctions_inl.h` (does not contain `acosh` declaration)
186+
* `CPUFunctions.h`
187+
* `RegisterCPU.cpp` (without `TORCH_LIBRARY` calls)
188+
* `QuantizedFunctions_inl.h` (contains acosh declaration)
189+
* `QuantizedFunctions.h`
190+
* `RegisterQuantizedCPU.cpp` (without `TORCH_LIBRARY` calls, contains `acosh` definition)
191+
192+
193+
194+
### Step 3
195+
196+
With step 2 finished we should have feature parity as the existing op registration & dispatch system. Now we need to consider the problems specific to edge devices. How do we support custom kernels for different edge devices? How do we make sure the performance is improved as expected? How do we make the binary size as small as possible? This step is aiming to tackle these problems and the end goal is to ship this codegen unboxing + static dispatch approach.
197+
198+
199+
#### Bring codegen to target platform
200+
201+
202+
203+
* Consider adding ops to our new `custom_ops.yaml` created in step 2 (maybe also rename), let the codegen read from the new yaml. The benefit of doing this is that we can easily support ATen ops with custom kernels (not to be confused with custom ops) and we only have a single source of truth.
204+
* There are two options, either we figure out all the dependencies for all the ops required, or we leverage tracing based selective build.
205+
* Bring everything to our target hardware platform to make sure it builds and runs.
206+
* Disable current Dispatcher. Avoid linking any `TORCH_LIBRARY` API calls in the build, we can only profile the performance this way.
207+
* With codegen unboxing + static dispatch, we hope that we can reach a much smaller percentage of cycle count for the op registration step.
208+
209+
210+
211+
#### Avoid schema parsing at runtime
212+
213+
As mentioned in step 1, we are registering an operator into the registry along with a schema string. We realized at the library initialization time we need to spend a lot of resources on schema parsing, according to the profiling results based on our prototype. We also noticed that the required information to instantiate a schema object are all available at codegen time, we can pass these data to the registry directly so that we can save time at runtime. For example:
214+
215+
216+
```
217+
CodegenUnboxing.cpp
218+
===================
219+
RegisterOperators reg({
220+
OperatorGenerator(
221+
"aten::get_device", // name
222+
"", // overload_name
223+
arguments, // a vector of arguments
224+
returns, // a vector of returns
225+
[](Stack & stack) {
226+
RECORD_FUNCTION("get_device", std::vector<c10::IValue>());
227+
at::unboxing::get_device(stack);
228+
},
229+
aliasAnalysisFromSchema()
230+
),
231+
...
232+
})
233+
```
234+
235+
236+
This way we can directly instantiate `FunctionSchema` objects without parsing at runtime. Of course we need to change APIs in `operator.h` to make this happen.
237+
238+
Q: Can we completely get rid of `FunctionSchema` and only register name/overload_name?
239+
240+
A: No, because we should have feature parity to the current system and backward compatibility for mobile models is a feature we need to support for the lightweight dispatch system. Currently we rely on the number of arguments to let the new runtime be able to run the old model.
241+
242+
243+
#### Support tracing based selective build
244+
245+
* In [gen.py](https://github.com/pytorch/pytorch/blob/master/tools/codegen/gen.py) the files we generate will go through the selector similar to what we are doing to `RegisterSchema.cpp` right now.
246+
* We need to make sure the binary size is on-par with or even better than existing tracing based selective build.
247+
248+
## Risks
249+
250+
There are 3 risks:
251+
252+
253+
254+
1. Performance gain of using JIT op registry is insignificant or even worse than dispatcher.
255+
1. De-risked: from the prototype running on a target platform it is proved to save latency on initial load.
256+
2. Binary size regression. Need to make sure selective build works.
257+
3. Mobile use case requires features only available on dispatcher.
258+
1. E.g., boxed fallback mechanism for [conj](https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/native/MathBitsFallback.h#L78) operator.
259+
260+
## Testing & Tooling Plan
261+
262+
Expand existing tests on the mobile interpreter to cover codegen logic. Let the cpp test target depending on codegen unboxing library, test if a module forward result from JIT execution equals to the mobile interpreter execution. Since JIT execution goes through metaprogramming unboxing and mobile interpreter execution goes through codegen unboxing, we can make sure the correctness of codegen unboxing. Example:
263+
264+
```cpp
265+
TEST(LiteInterpreterTest, UpsampleNearest2d) {
266+
Module m("m");
267+
m.define(R"(
268+
def forward(self, input: Tensor, scale:float):
269+
return torch.upsample_nearest2d(input, [1, 1], float(scale), float(scale))
270+
)");
271+
272+
std::vector<IValue> inputs;
273+
inputs.emplace_back(torch::rand({1, 3, 128, 128}));
274+
inputs.emplace_back(at::Scalar(2.0));
275+
auto ref = m.forward(inputs);
276+
277+
std::stringstream ss;
278+
m._save_for_mobile(ss);
279+
mobile::Module bc = _load_for_mobile(ss);
280+
IValue res;
281+
res = bc.forward(inputs);
282+
283+
auto resd = res.toTensor();
284+
auto refd = ref.toTensor();
285+
ASSERT_TRUE(resd.equal(refd));
286+
}
287+
```
288+
## Appendix
289+
List of IValue types we need to support in unboxing:
290+
291+
'Device',
292+
'Device?',
293+
'Dimname',
294+
'Dimname[1]',
295+
'Dimname[]',
296+
'Dimname[]?',
297+
'Generator?',
298+
'Layout?',
299+
'MemoryFormat',
300+
'MemoryFormat?',
301+
'Scalar',
302+
'Scalar?',
303+
'ScalarType',
304+
'ScalarType?',
305+
'Scalar[]',
306+
'Storage',
307+
'Stream',
308+
'Tensor',
309+
'Tensor(a!)',
310+
'Tensor(a!)[]',
311+
'Tensor(a)',
312+
'Tensor(b!)',
313+
'Tensor(c!)',
314+
'Tensor(d!)',
315+
'Tensor?',
316+
'Tensor?[]',
317+
'Tensor[]',
318+
'bool',
319+
'bool?',
320+
'bool[2]',
321+
'bool[3]',
322+
'bool[4]',
323+
'float',
324+
'float?',
325+
'float[]?',
326+
'int',
327+
'int?',
328+
'int[1]',
329+
'int[1]?',
330+
'int[2]',
331+
'int[2]?',
332+
'int[3]',
333+
'int[4]',
334+
'int[5]',
335+
'int[6]',
336+
'int[]',
337+
'int[]?',
338+
'str',
339+
'str?'

0 commit comments

Comments
 (0)