Paddle icon indicating copy to clipboard operation
Paddle copied to clipboard

What should do to add MKLDNN kernel

Open jacquesqiao opened this issue 8 years ago • 5 comments

Base class that need to implement:

  • MKLDNNLoDTensor derived from LoDTensor. in formal PaddlePaddle. Like MKLDNNMatrix.h for v2

  • MKLDNNKernelBase derived 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:

  1. add pool_cudnn_op.cu.cc
  2. implement PoolCUDNNOpKernel
  3. 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:

  1. add new file pool_mkldnn_op.cc
  2. implement PoolMKLDNNOpKernel
  3. register PoolMKLDNNOpKernel with library MKLDNN
REGISTER_OP_KERNEL(pool2d, MKLDNN, ::paddle::platform::MKLDNNPlace,
                   ops::PoolMKLDNNOpKernel<float>,
                   ops::PoolMKLDNNOpKernel<double>);

jacquesqiao avatar Feb 09 '18 05:02 jacquesqiao

Can we move the instruction to some wiki page, link it here and close the issue?

mrysztow avatar Mar 06 '18 15:03 mrysztow

Your suggestion sounds nice. The related documentation is new_op_kernel_en.md. Can you move the instruction to it?

luotao1 avatar Mar 07 '18 08:03 luotao1

@pzelazko-intel can you validate the instruction above and if it is actual, insert it into the file pointed by @luotao1 ?

mrysztow avatar Mar 08 '18 14:03 mrysztow

@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?

pzelazko-intel avatar Mar 08 '18 14:03 pzelazko-intel

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

luotao1 avatar Mar 12 '18 04:03 luotao1