What should do to add MKLDNN kernel
Base class that need to implement:
-
MKLDNNLoDTensorderived from LoDTensor. in formal PaddlePaddle. Like MKLDNNMatrix.h for v2 -
MKLDNNKernelBasederived from KernelBase. Like MKLDNNLayer.h in formal PaddlePaddle for v2 -
Tensor transform functions used to tranform between LodTensor and MKLDNNKernel
Steps to add new kernel
Take pool_cudnn_op as example:
- add
pool_cudnn_op.cu.cc - implement
PoolCUDNNOpKernel - register kernel with library type
CUDNN
REGISTER_OP_KERNEL(pool2d, CUDNN, ::paddle::platform::CUDAPlace,
ops::PoolCUDNNOpKernel<float>,
ops::PoolCUDNNOpKernel<double>);
If we want to add pool_mkldnn kernel, the step should be:
- add new file
pool_mkldnn_op.cc - implement PoolMKLDNNOpKernel
- register PoolMKLDNNOpKernel with library
MKLDNN
REGISTER_OP_KERNEL(pool2d, MKLDNN, ::paddle::platform::MKLDNNPlace,
ops::PoolMKLDNNOpKernel<float>,
ops::PoolMKLDNNOpKernel<double>);
Can we move the instruction to some wiki page, link it here and close the issue?
Your suggestion sounds nice. The related documentation is new_op_kernel_en.md. Can you move the instruction to it?
@pzelazko-intel can you validate the instruction above and if it is actual, insert it into the file pointed by @luotao1 ?
@jacquesqiao For MKLDNN kernels we actually derive from framework::OpKernel directly. Please take a look at #8879.
Does class representing MKLDNN matrix have to derive from LoDTensor? Couldn't it derive from Tensor?
Does class representing MKLDNN matrix have to derive from LoDTensor? Couldn't it derive from Tensor?
It should derive from LoDTensor. The reason is that (sorry, we don't have a documentation about it now, we will add it):
All the input and output of ops are LoDTensor in fact. Ops should deliver the LoD information one by one, if one of them is not LoDTensor, the delivery is broken. For examples, A->B->C, if B doesn't use LoDTensor, C would not get the LoD information. (https://github.com/PaddlePaddle/Paddle/issues/3717)
The related code is https://github.com/PaddlePaddle/Paddle/blob/ee88855ddfe5511e448b55dac5347aac33edfdf3/paddle/fluid/framework/operator.cc#L245-L307