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.

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.

Problem

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

Some operators, like CRF, are complicated and inefficient to be implemented on GPU. The CRF operator will only have a CPU kernel.

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.

Some layout and place are particular. One example is that MKLDNN uses nChw8 and there is no other library uses nChw8c.

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.

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

Solution: data transform

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.

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.

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.

We transform the input data from actual to expect if the actual kernel type is not as same as expect kernel type.