Skip to content

Commit

Permalink
[OneDNN] Fix Convolution Performance when IC <=4 (PaddlePaddle#58435)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanglirong1999 authored Oct 30, 2023
1 parent 6ccca1d commit ae10e7c
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
2 changes: 1 addition & 1 deletion paddle/phi/kernels/onednn/conv_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ class ConvOneDNNHandlerT
weights_md = funcs::OneDNNMemDesc(
weights_tz, data_type, funcs::OneDNNMemoryFormat::any);
}
if (input->dims().size() == 4 && input->dims()[1] == 3) {
if (input->dims().size() == 4 && input->dims()[1] <= 4) {
chosen_memory_format = funcs::OneDNNMemoryFormat::nhwc;
}
const auto dst_md = funcs::OneDNNMemDesc(
Expand Down
89 changes: 89 additions & 0 deletions test/cpp/fluid/mkldnn/test_conv_mkldnn_nhwc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,92 @@ TEST(test_conv2d_output, int8) {

op->Run(scope, cpu_place);
}
TEST(test_conv2d_output, ic1) {
paddle::framework::Scope scope;
paddle::platform::CPUPlace cpu_place;

paddle::framework::OpDesc conv2d_op(nullptr);
conv2d_op.SetType("conv2d");
conv2d_op.SetInput("Input", {"conv2d-X"});
conv2d_op.SetInput("Filter", {"conv2d-Y"});
conv2d_op.SetOutput("Output", {"conv2d-Out"});

AddVarToScope<float>("conv2d-X", &scope, {1, 1, 224, 224});
AddVarToScope<float>("conv2d-Y", &scope, {64, 1, 7, 7});
AddVarToScope<float>("conv2d-Out", &scope, {1, 64, 218, 218});

const std::vector<int> strides({1, 1});
const std::vector<int> paddings({1, 1});
const std::vector<int> dilations({1, 1});
const int groups = 1;

conv2d_op.SetAttr("strides", strides);
conv2d_op.SetAttr("paddings", paddings);
conv2d_op.SetAttr("dilations", dilations);
conv2d_op.SetAttr("groups", groups);
conv2d_op.SetAttr("use_mkldnn", true);

auto op = paddle::framework::OpRegistry::CreateOp(conv2d_op);

op->Run(scope, cpu_place);
}

TEST(test_conv2d_output, ic2) {
paddle::framework::Scope scope;
paddle::platform::CPUPlace cpu_place;

paddle::framework::OpDesc conv2d_op(nullptr);
conv2d_op.SetType("conv2d");
conv2d_op.SetInput("Input", {"conv2d-X"});
conv2d_op.SetInput("Filter", {"conv2d-Y"});
conv2d_op.SetOutput("Output", {"conv2d-Out"});

AddVarToScope<float>("conv2d-X", &scope, {1, 2, 224, 224});
AddVarToScope<float>("conv2d-Y", &scope, {64, 2, 7, 7});
AddVarToScope<float>("conv2d-Out", &scope, {1, 64, 218, 218});

const std::vector<int> strides({1, 1});
const std::vector<int> paddings({1, 1});
const std::vector<int> dilations({1, 1});
const int groups = 1;

conv2d_op.SetAttr("strides", strides);
conv2d_op.SetAttr("paddings", paddings);
conv2d_op.SetAttr("dilations", dilations);
conv2d_op.SetAttr("groups", groups);
conv2d_op.SetAttr("use_mkldnn", true);

auto op = paddle::framework::OpRegistry::CreateOp(conv2d_op);

op->Run(scope, cpu_place);
}

TEST(test_conv2d_output, ic4) {
paddle::framework::Scope scope;
paddle::platform::CPUPlace cpu_place;

paddle::framework::OpDesc conv2d_op(nullptr);
conv2d_op.SetType("conv2d");
conv2d_op.SetInput("Input", {"conv2d-X"});
conv2d_op.SetInput("Filter", {"conv2d-Y"});
conv2d_op.SetOutput("Output", {"conv2d-Out"});

AddVarToScope<float>("conv2d-X", &scope, {1, 4, 224, 224});
AddVarToScope<float>("conv2d-Y", &scope, {64, 4, 7, 7});
AddVarToScope<float>("conv2d-Out", &scope, {1, 64, 218, 218});

const std::vector<int> strides({1, 1});
const std::vector<int> paddings({1, 1});
const std::vector<int> dilations({1, 1});
const int groups = 1;

conv2d_op.SetAttr("strides", strides);
conv2d_op.SetAttr("paddings", paddings);
conv2d_op.SetAttr("dilations", dilations);
conv2d_op.SetAttr("groups", groups);
conv2d_op.SetAttr("use_mkldnn", true);

auto op = paddle::framework::OpRegistry::CreateOp(conv2d_op);

op->Run(scope, cpu_place);
}

0 comments on commit ae10e7c

Please sign in to comment.