Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MLU]transpose convbpf output to HWCN for better performance #44552

Merged
merged 1 commit into from
Jul 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions paddle/fluid/operators/conv_op_mlu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,8 @@ class MLUDepthwiseConvGradOpKernel : public framework::OpKernel<T> {
Tensor output_grad_tensor(output_grad->type());
const std::vector<int> perm_to_nhwc = {0, 2, 3, 1};
const std::vector<int> perm_to_nchw = {0, 3, 1, 2};
const std::vector<int> perm_hwcm_to_mchw = {3, 2, 0, 1};
const std::vector<int> perm_mchw_to_hwcm = {2, 3, 1, 0};
if (channel_last) {
input_tensor.ShareDataWith(*input);
output_grad_tensor.ShareDataWith(*output_grad);
Expand All @@ -462,10 +464,12 @@ class MLUDepthwiseConvGradOpKernel : public framework::OpKernel<T> {

auto filter_grad_dims = filter_grad->dims();
Tensor temp_filter_grad(filter_grad->type());
temp_filter_grad.mutable_data<T>({filter_grad_dims[0],
filter_grad_dims[2],
filter_grad_dims[3],
filter_grad_dims[1]},
// Details about setting diff_w hwcn for better performance, see the CNNL
// documentation.
temp_filter_grad.mutable_data<T>({filter_grad_dims[perm_mchw_to_hwcm[0]],
filter_grad_dims[perm_mchw_to_hwcm[1]],
filter_grad_dims[perm_mchw_to_hwcm[2]],
filter_grad_dims[perm_mchw_to_hwcm[3]]},
ctx.GetPlace());

cnnlDataType_t tensor_dtype = ToCnnlDataType<T>();
Expand All @@ -474,7 +478,7 @@ class MLUDepthwiseConvGradOpKernel : public framework::OpKernel<T> {
MLUCnnlTensorDesc out_grad_desc(
output_grad_tensor, data_layout, tensor_dtype);
MLUCnnlTensorDesc temp_filter_grad_desc(
temp_filter_grad, data_layout, tensor_dtype);
temp_filter_grad, CNNL_LAYOUT_HWCN, tensor_dtype);

MLUCnnlConvolutionDesc conv_desc(in_dims_size,
paddings.data(),
Expand All @@ -492,9 +496,9 @@ class MLUDepthwiseConvGradOpKernel : public framework::OpKernel<T> {
temp_filter_grad_desc.get(),
GetBasePtr(&temp_filter_grad));

// transpose filter_grad from MHWC to MCHW
// transpose filter_grad from HWCM to MCHW
TransposeFromMLUTensor<T>(ctx,
perm_to_nchw,
perm_hwcm_to_mchw,
&temp_filter_grad,
filter_grad,
false /*need_reshape_or_alloc*/);
Expand Down