kernel_hint_design.md 2.5 KB
Newer Older
1 2 3
# Kernel Hint Design

## Problem
W
weixing02 已提交
4
In PaddlePaddle's [Design](https://github.com/PaddlePaddle/Paddle/blob/develop/doc/fluid/design/execution/switch.md), one Operator may have multiple kernels. Users may have some personal preference to choose a certain type of kernel for an operator, such as `force_cpu` to choose a CPU kernel, `use_cudnn` to choose a CUDNN kernel, we need to provide a way for users to do this.
Q
qiaolongfei 已提交
5

Q
qiaolongfei 已提交
6
In the current design, we use KernelType to describe one kernel.
Q
qiaolongfei 已提交
7 8 9 10 11 12 13 14

```cpp
struct KernelType {
  Place place_;
  DataType data_type_;
  LayoutType layout_;
};
```
Q
qiaolongfei 已提交
15
 `place_` `data_type_` and `layout_` can be got from the input tensors of the operator, `GetActualKernelType(inputs)` use inputs to infer the proper kernel key that fit the incoming data, but users can not directly configure it.
Q
qiaolongfei 已提交
16

W
weixing02 已提交
17
The [design](https://github.com/PaddlePaddle/Paddle/blob/develop/doc/fluid/design/execution/switch.md) also provides a virtual method `GetExpectedKernelType` that user can overload and use to choose the KernelType they want to use.
Q
qiaolongfei 已提交
18

Q
qiaolongfei 已提交
19
So we should send the information user defined in proto to `GetExpectedKernelType` for choosing a kernel.
Q
qiaolongfei 已提交
20 21 22 23

The problem is, how should we define and send the information for `GetExpectedKernelType` to use?

## Solution
Q
qiaolongfei 已提交
24

Q
qiaolongfei 已提交
25 26
### Potential choice
1. Do nothing, let the user add the information they want to operator‘s attribute and get them inside `GetExpectedKernelType`, this can work properly. But there is a little problem that users may define many kinds of hints for the same purpose, such as `force_cpu`, `use_cpu`, `cpu_kernel` to choose CPU kernel, and `use_cudnn`, `force_cudnn`, `cudnn_kernel` to choose CUDNN kernel.
Q
qiaolongfei 已提交
27

Q
qiaolongfei 已提交
28
2. Pre-define all the needed option and use a single attr key such as `kernel_hint` for the user, this is not so flexible if the user wants to define some more kind of hint.
Q
qiaolongfei 已提交
29

Q
qiaolongfei 已提交
30 31 32 33
### Final choice
To provide enough flexibility while avoiding confusion definition, we can define some global constants for these attribute names, such as `force_cpu`, `use_cudnn`, `use_mkldnn` for a user to choose.

In C++
Q
qiaolongfei 已提交
34 35 36 37 38 39

```cpp
const std::string kForceCPU = "force_cpu";
const std::string kUseCUDNN = "use_cudnn";
const std::string kUseMKLDNN = "use_mkldnn";

Q
qiaolongfei 已提交
40
KernelType GetExpectedKernelType() {
Q
qiaolongfei 已提交
41 42 43 44 45
  if (Attr<bool>(kForceCPU)) {
    return KernelType(CPUPlace, ...)
  } else {
    ...
  }
Q
qiaolongfei 已提交
46 47 48 49 50 51
}
```

In Python code

```python
Q
qiaolongfei 已提交
52 53 54
FORCE_CPU = core.kForceCPU()

def xx_layer(..., force_cpu=false):
Q
qiaolongfei 已提交
55 56 57 58
  layer_helper = LayerHelper(...)
  layer_helper.append_op(
    type="xx",
    attr={FORCE_CPU: force_cpu})
Q
qiaolongfei 已提交
59
```