kernel_selection.md 4.3 KB
Newer Older
W
weixing 已提交
1
# Background
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.
Y
Yu Yang 已提交
3

4
The `OpKernelType ` is as follows:
Y
Yu Yang 已提交
5

6 7
```cpp
struct OpKernelType {
Y
Yu Yang 已提交
8 9
  Place place_;
  DataType data_type_;
10 11
  DataLayout data_layout_;
  LibraryType library_type_;
Y
Yu Yang 已提交
12 13 14
};
```

15
- The `place_` is a descriptor of the device, e.g., CPUPlace, CUDAPlace.
Y
Yu Yang 已提交
16

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`.
Y
Yu Yang 已提交
18

19 20 21
- 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.

- The `library_type_` describes the computational library, e.g., `MKLDNN`, `CUDNN`.
Y
Yu Yang 已提交
22 23 24 25 26 27 28 29 30

## Problem

We register a kernel for every operator and every kernel type ideally. However, it is impracticable for the following situations.

1. Some operators, like CRF, are complicated and inefficient to be implemented on GPU. The CRF operator will only have a CPU kernel.
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.
3. Some layout and place are particular. One example is that MKLDNN uses `nChw8` and there is no other library uses `nChw8c`.

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
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.

If OP1 and OP2 run on the same place(for example CPUPlace), then `op1_2_op2` can be used directly by OP2.

```
OP1(CPUPlace)
     |
 op1_2_op2
     |
OP2(CPUPlace)
```

If OP1 and OP2 run one different place, then OP2 cannot `use op1_2_op2` directly.

Problems under these situations are similar. We can formalize this problem as follow.
Y
Yu Yang 已提交
46 47 48

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$.

49
## Solution: data transform
Y
Yu Yang 已提交
50

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.
Y
Yu Yang 已提交
52

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.
Y
Yu Yang 已提交
54 55 56

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`.

57
We transform the input data from `actual` to `expect` if the actual kernel type is not as same as expect kernel type.
Y
Yu Yang 已提交
58

59
The algorithm is described as following
Y
Yu Yang 已提交
60 61

```cpp
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
void OperatorWithKernel::Run(
        const Scope& scope,
        const platform::Place& place) const {
  ExecutionContext ctx(...);
  auto expected_kernel_key = this->GetExpectedKernelType(ctx);

  Scope& new_scope = scope.NewScope();

  for (auto& var_name : this->Inputs()) {
    auto* tensor_in = GetTensor(var_name);
    auto kernel_type_for_var = this->GetKernelTypeForVar(...);
    if (kernel_type_for_var.place_ != expected_kernel_key.place_) {
      auto* trans_var = new_scope.Var(var_name);
      auto* out = DataTransform(expected_kernel_key,
                                kernel_type_for_var,
                                *tensor_in);
      CopyVariableWithTensor(...);
    }
  }

  auto kernel = kernels.find(expected_kernel_key);
  kernel->Compute(ExecutionContext(...));
Y
Yu Yang 已提交
84 85
}
```
86 87 88 89 90 91 92 93 94 95 96 97 98 99

then the actual process for the multi-device above will be:

```
OP1(CPUPlace)
     |
op1_2_op2(on CPU)
     |
[transform](from CPU to GPU)
     |
op1_2_op2(on GPU)
     |
OP2(CUDAPlace)
```