Skip to content

Commit 7e4cbfe

Browse files
jacquesqiaowangkuiyi
authored andcommitted
update switch kernel documentation (#7597)
* update switch_kernel.md * update the demo code * update
1 parent d77e6a6 commit 7e4cbfe

File tree

1 file changed

+67
-34
lines changed

1 file changed

+67
-34
lines changed

doc/design/switch_kernel.md

Lines changed: 67 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
## Background
2-
Every operator has many kernels because there are multiple data types, places, data layout that Fluid supports. We use the `KernelType` to describe kernel types that operators can hold.
2+
Every operator has many kernels because there are multiple data types, places, data layout, library type that Fluid supports. We use the `OpKernelType ` to describe kernel types that operators can hold.
33

4-
The `KernelType` is as follows.
4+
The `OpKernelType ` is as follows:
55

6-
```
7-
struct KernelType {
6+
```cpp
7+
struct OpKernelType {
88
Place place_;
99
DataType data_type_;
10-
LayoutType layout_;
10+
DataLayout data_layout_;
11+
LibraryType library_type_;
1112
};
1213
```
1314
14-
The `place_` is a descriptor of the device and the computational library, e.g., `MKLDNNPlace`, `CUDAPlace`.
15+
- The `place_` is a descriptor of the device, e.g., CPUPlace, CUDAPlace.
1516
16-
The `data_type_` is the data type that this kernel performs on, e.g., `FP32`, `INT64`. Note that one kernel may have inputs with different data types. However, it will be a major `data_type`. For example, the `cross_entropy` takes `int64` as it label, and `double`/`float` as its input logit and output cost. The major `data_type` of `cross_entropy` is `float`/`double`.
17+
- The `data_type_` is the data type that this kernel performs on, e.g., `FP32`, `INT64`. Note that one kernel may have inputs with different data types. However, it will be a major `data_type`. For example, the `cross_entropy` takes `int64` as it label, and `double`/`float` as its input logit and output cost. The major `data_type` of `cross_entropy` is `float` or `double`.
1718
18-
The `layout` is useful for some computational library. One example is that MKLDNN uses many kinds of layout, such as `nChw8c`. Each kind of layout will invoke the different kernel.
19+
- The `data_layout_ ` is useful for some computational library. One example is that MKLDNN uses many kinds of layout, such as `nChw8c`. Each kind of layout will invoke the different kernel.
20+
21+
- The `library_type_` describes the computational library, e.g., `MKLDNN`, `CUDNN`.
1922
2023
## Problem
2124
@@ -25,42 +28,72 @@ We register a kernel for every operator and every kernel type ideally. However,
2528
2. Some operators will take too many memory. It is better to force them into CPU. However, the rest of operators in this neural network will be performed on GPU, i.e., model parallel problem.
2629
3. Some layout and place are particular. One example is that MKLDNN uses `nChw8` and there is no other library uses `nChw8c`.
2730
28-
Problems under these situations are similar. We can formalise this problem as follow.
31+
Take one situation to give a detailed explanation, if we have two Operators: OP1 and OP2, OP1 has one output `op1_to_op2`, and `op1_to_op2` is the input of OP2.
32+
33+
If OP1 and OP2 run on the same place(for example CPUPlace), then `op1_2_op2` can be used directly by OP2.
34+
35+
```
36+
OP1(CPUPlace)
37+
|
38+
op1_2_op2
39+
|
40+
OP2(CPUPlace)
41+
```
42+
43+
If OP1 and OP2 run one different place, then OP2 cannot `use op1_2_op2` directly.
44+
45+
Problems under these situations are similar. We can formalize this problem as follow.
2946
3047
We register kernels with types $KT = \{kt_1, kt_2, kt_3, ...\}$ for one operator. The inputs of this operator should be run on kernel type $kt_{?}$, which the $kt_{?} \notin KT$. How to cast the input of this operator from $kt_{?}$ to any of kernel type in $KT$.
3148
32-
## Solution
49+
## Solution: data transform
3350
34-
It is clearly that transforming inputs of an operator toadapt another kernel type is not related to the particular operator. So we should register these transformation methods as global methods.
51+
It is clear that transforming inputs of an operator to adapt another kernel type is not related to the particular operator. So we should register these transformation methods as global methods.
3552
36-
We can infer a kernel type from the inputs of an operators. We let this kernel type as `actual kernel type`, which means this kernel type is the actually kernel type that operator should be performed.
53+
We can infer kernel type for each input of an operator. We let this kernel type as `actual kernel type for var`, which means this kernel type is the kernel type that can process this input variable.
3754
3855
We can get a kernel type by 1) The configuration of operator description. (Users may want to force use `MKL` for `conv` operator). 2) The place of the current executor. (Executor is running on GPU). This kernel type is what we expect the operator will be performed on. We let this kernel type as `expect kernel type`.
3956
40-
We transform the input data from `actual` to `expect` if the expect kernel type is not as same as actual kernel type.
57+
We transform the input data from `actual` to `expect` if the actual kernel type is not as same as expect kernel type.
4158
42-
The algorithm is described as follow
59+
The algorithm is described as following
4360
4461
```cpp
45-
using DataTransformationFN = std::function<void(const Tensor& in, Tensor* out)>;
46-
using KernelTypePair = std::pair<KernelType, KernelType>;
47-
48-
map<KernelTypePair, DataTransformationFN> g_data_transformation_;
49-
50-
void OpWithKernel::Run() {
51-
vec<Tensor> inputs = ...
52-
auto actual_kernel_type = GetActualKernelType(inputs);
53-
54-
// The expected kernel type is related to actual kernel type.
55-
// For the most operators, the expected kernel type is as same as
56-
// actual kernel type.
57-
//
58-
// So we pass `actual_kernel_type` as a parameter of
59-
// GetExpectedKernelType
60-
auto expect_kernel_type = GetExpectedKernelType(actual_kernel_type);
61-
62-
auto trans = g_data_transformation_[{actual_kernel_type, expect_kernel_type}];
63-
64-
kernel.run(trans(inputs));
62+
void OperatorWithKernel::Run(
63+
const Scope& scope,
64+
const platform::Place& place) const {
65+
ExecutionContext ctx(...);
66+
auto expected_kernel_key = this->GetExpectedKernelType(ctx);
67+
68+
Scope& new_scope = scope.NewScope();
69+
70+
for (auto& var_name : this->Inputs()) {
71+
auto* tensor_in = GetTensor(var_name);
72+
auto kernel_type_for_var = this->GetKernelTypeForVar(...);
73+
if (kernel_type_for_var.place_ != expected_kernel_key.place_) {
74+
auto* trans_var = new_scope.Var(var_name);
75+
auto* out = DataTransform(expected_kernel_key,
76+
kernel_type_for_var,
77+
*tensor_in);
78+
CopyVariableWithTensor(...);
79+
}
80+
}
81+
82+
auto kernel = kernels.find(expected_kernel_key);
83+
kernel->Compute(ExecutionContext(...));
6584
}
6685
```
86+
87+
then the actual process for the multi-device above will be:
88+
89+
```
90+
OP1(CPUPlace)
91+
|
92+
op1_2_op2(on CPU)
93+
|
94+
[transform](from CPU to GPU)
95+
|
96+
op1_2_op2(on GPU)
97+
|
98+
OP2(CUDAPlace)
99+
```

0 commit comments

Comments
 (0)