From e94b26daf5b68feadb89583bd9ac031bf49cda02 Mon Sep 17 00:00:00 2001 From: Adam <38704900+grygielski@users.noreply.github.com> Date: Tue, 3 Sep 2019 10:44:07 +0200 Subject: [PATCH] using MKLDNNMemoryFormat = mkldnn::memory::format changes (#19568) * using MKLDNNMemoryFormat = mkldnn::memory::format changes test=develop * PADDLE_ENFORCE update test=develop --- .../fluid/framework/data_layout_transform.cc | 10 +- .../fluid/framework/data_layout_transform.h | 17 +- .../mkldnn/elementwise_add_mkldnn_op.cc | 32 ++-- .../mkldnn/elementwise_mul_mkldnn_op.cc | 21 +-- .../operators/mkldnn/activation_mkldnn_op.cc | 25 +-- .../operators/mkldnn/batch_norm_mkldnn_op.cc | 37 +++-- .../operators/mkldnn/concat_mkldnn_op.cc | 15 +- .../fluid/operators/mkldnn/conv_mkldnn_op.cc | 150 +++++++++++------- .../mkldnn/conv_transpose_mkldnn_op.cc | 50 +++--- .../operators/mkldnn/dequantize_mkldnn_op.cc | 4 +- paddle/fluid/operators/mkldnn/fc_mkldnn_op.cc | 22 +-- .../fluid/operators/mkldnn/mul_mkldnn_op.cc | 40 ++--- .../fluid/operators/mkldnn/pool_mkldnn_op.cc | 42 ++--- .../operators/mkldnn/requantize_mkldnn_op.cc | 4 +- .../operators/mkldnn/softmax_mkldnn_op.cc | 18 +-- .../fluid/operators/mkldnn/sum_mkldnn_op.cc | 30 ++-- .../operators/mkldnn/transpose_mkldnn_op.cc | 2 +- paddle/fluid/platform/mkldnn_helper.h | 65 ++++---- paddle/fluid/platform/mkldnn_reuse.h | 39 +++-- 19 files changed, 341 insertions(+), 282 deletions(-) diff --git a/paddle/fluid/framework/data_layout_transform.cc b/paddle/fluid/framework/data_layout_transform.cc index 393b7e9887a..16f9241f55a 100644 --- a/paddle/fluid/framework/data_layout_transform.cc +++ b/paddle/fluid/framework/data_layout_transform.cc @@ -18,7 +18,6 @@ #include "paddle/fluid/operators/math/math_function.h" #ifdef PADDLE_WITH_MKLDNN -#include "paddle/fluid/platform/mkldnn_helper.h" #include "paddle/fluid/platform/mkldnn_reuse.h" #endif @@ -135,9 +134,10 @@ void innerTransDataLayoutFromMKLDNN(DataLayout in_layout, DataLayout out_layout, const Tensor& in, Tensor* out, platform::Place place) { #ifdef PADDLE_WITH_MKLDNN - PADDLE_ENFORCE(in.format() != memory::format::format_undef && - in.format() != memory::format::any, - "Input tensor should have specified memory format"); + PADDLE_ENFORCE_NE(in.format(), MKLDNNMemoryFormat::format_undef, + "Input tensor should have specified memory format"); + PADDLE_ENFORCE_NE(in.format(), MKLDNNMemoryFormat::any, + "Input tensor should have specified memory format"); // Set default as NCHW in case not specified out_layout = @@ -183,7 +183,7 @@ void innerTransDataLayoutFromMKLDNN(DataLayout in_layout, DataLayout out_layout, } out->set_layout(out_layout); // reset format since the out tensor will be feed to non-MKLDNN OPkernel - out->set_format(memory::format::format_undef); + out->set_format(MKLDNNMemoryFormat::format_undef); #endif } diff --git a/paddle/fluid/framework/data_layout_transform.h b/paddle/fluid/framework/data_layout_transform.h index e34e3dc3afc..d67ea1e5308 100644 --- a/paddle/fluid/framework/data_layout_transform.h +++ b/paddle/fluid/framework/data_layout_transform.h @@ -21,30 +21,33 @@ #include "paddle/fluid/framework/tensor.h" #include "paddle/fluid/framework/variable.h" +#ifdef PADDLE_WITH_MKLDNN +#include "paddle/fluid/platform/mkldnn_helper.h" +#endif + namespace paddle { namespace framework { #ifdef PADDLE_WITH_MKLDNN -using MKLDNNFormat = mkldnn::memory::format; using MKLDNNDataType = mkldnn::memory::data_type; -inline MKLDNNFormat ToMKLDNNFormat(const DataLayout& layout) { +inline MKLDNNMemoryFormat ToMKLDNNFormat(const DataLayout& layout) { switch (layout) { case DataLayout::kNHWC: - return MKLDNNFormat::nhwc; + return MKLDNNMemoryFormat::nhwc; case DataLayout::kNCHW: - return MKLDNNFormat::nchw; + return MKLDNNMemoryFormat::nchw; default: PADDLE_THROW("Fail to convert layout %s to MKLDNN format", DataLayoutToString(layout)); } } -inline DataLayout ToPaddleLayout(const MKLDNNFormat& format) { +inline DataLayout ToPaddleLayout(const MKLDNNMemoryFormat& format) { switch (format) { - case MKLDNNFormat::nhwc: + case MKLDNNMemoryFormat::nhwc: return DataLayout::kNHWC; - case MKLDNNFormat::nchw: + case MKLDNNMemoryFormat::nchw: return DataLayout::kNCHW; default: PADDLE_THROW("Fail to convert MKLDNN format to paddle layout"); diff --git a/paddle/fluid/operators/elementwise/mkldnn/elementwise_add_mkldnn_op.cc b/paddle/fluid/operators/elementwise/mkldnn/elementwise_add_mkldnn_op.cc index 49cfe0a0ab0..505294bad77 100644 --- a/paddle/fluid/operators/elementwise/mkldnn/elementwise_add_mkldnn_op.cc +++ b/paddle/fluid/operators/elementwise/mkldnn/elementwise_add_mkldnn_op.cc @@ -55,20 +55,20 @@ class EltwiseAddMKLDNNKernel : public framework::OpKernel { // broadcast operations need to performed. if (x_dims != y_dims_untrimed) { Tensor _x; - mkldnn::memory::format format; + MKLDNNMemoryFormat format; std::vector src_x_tz = framework::vectorize2int(x_dims); if ((src_x_tz.size() == 3 && - x->format() != (format = memory::format::ncw)) || + x->format() != (format = MKLDNNMemoryFormat::ncw)) || (src_x_tz.size() == 4 && - x->format() != (format = memory::format::nchw)) || + x->format() != (format = MKLDNNMemoryFormat::nchw)) || (src_x_tz.size() == 5 && - x->format() != (format = memory::format::ncdhw))) { + x->format() != (format = MKLDNNMemoryFormat::ncdhw))) { _x.Resize(x_dims); mkldnn::memory::data_type in_type = platform::MKLDNNGetDataType(); auto out_format = platform::MKLDNNFormatForSize( - x_dims.size(), mkldnn::memory::format::nchw); + x_dims.size(), MKLDNNMemoryFormat::nchw); const std::string key = platform::ReorderMKLDNNHandler::GetHash( src_x_tz, x->format(), out_format, std::to_string(in_type)); @@ -119,12 +119,15 @@ class EltwiseAddMKLDNNKernel : public framework::OpKernel { z->set_layout(DataLayout::kMKLDNN); z->set_format(format); } else { - PADDLE_ENFORCE(x->layout() == DataLayout::kMKLDNN && - x->format() != memory::format::format_undef, - "Wrong layout/format set for X tensor"); - PADDLE_ENFORCE(y->layout() == DataLayout::kMKLDNN && - y->format() != memory::format::format_undef, - "Wrong layout/format set for Y tensor"); + PADDLE_ENFORCE_EQ(x->layout(), DataLayout::kMKLDNN, + "Wrong layout set for X tensor"); + PADDLE_ENFORCE_NE(x->format(), MKLDNNMemoryFormat::format_undef, + "Wrong format set for X tensor"); + + PADDLE_ENFORCE_EQ(y->layout(), DataLayout::kMKLDNN, + "Wrong layout set for Y tensor"); + PADDLE_ENFORCE_NE(y->format(), MKLDNNMemoryFormat::format_undef, + "Wrong format set for Y tensor"); std::vector src_x_tz = framework::vectorize2int(x_dims); std::vector src_y_tz = framework::vectorize2int(y_dims_untrimed); @@ -148,7 +151,7 @@ class EltwiseAddMKLDNNKernel : public framework::OpKernel { paddle::platform::to_void_cast(y_data)); auto dst_md = memory::desc({dst_tz}, platform::MKLDNNGetDataType(), - memory::format::any); + MKLDNNMemoryFormat::any); auto sum_pd = handler.AcquireSumPrimitiveDescriptor( {src_x_memory, src_y_memory}, scales, dst_md); @@ -164,8 +167,9 @@ class EltwiseAddMKLDNNKernel : public framework::OpKernel { stream(stream::kind::eager).submit(pipeline).wait(); z->set_layout(DataLayout::kMKLDNN); - z->set_format( - (memory::format)dst_memory->get_primitive_desc().desc().data.format); + z->set_format((MKLDNNMemoryFormat)dst_memory->get_primitive_desc() + .desc() + .data.format); } } }; diff --git a/paddle/fluid/operators/elementwise/mkldnn/elementwise_mul_mkldnn_op.cc b/paddle/fluid/operators/elementwise/mkldnn/elementwise_mul_mkldnn_op.cc index f2f4d3fee05..43aa1fcfb1f 100644 --- a/paddle/fluid/operators/elementwise/mkldnn/elementwise_mul_mkldnn_op.cc +++ b/paddle/fluid/operators/elementwise/mkldnn/elementwise_mul_mkldnn_op.cc @@ -37,7 +37,7 @@ static void UpdateDataFormat(const framework::ExecutionContext& ctx, if (ctx.op().HasAttr(attribute)) { auto format_as_string = ctx.Attr(attribute); auto format = StringToMKLDNNFormat(&format_as_string); - if (format != memory::format::any) { + if (format != MKLDNNMemoryFormat::any) { tensor->set_format(format); } } @@ -51,7 +51,8 @@ static void ReorderInput(framework::Tensor* tensor, auto dims = paddle::framework::vectorize2int(tensor->dims()); framework::Tensor out_tensor; out_tensor.Resize(tensor->dims()); - out_tensor.set_format(isFourDim ? memory::format::nchw : memory::format::nc); + out_tensor.set_format(isFourDim ? MKLDNNMemoryFormat::nchw + : MKLDNNMemoryFormat::nc); out_tensor.set_layout(tensor->layout()); mkldnn::memory input_memory = { {{dims, platform::MKLDNNGetDataType(), tensor->format()}, engine}, @@ -86,8 +87,8 @@ class ElementwiseMulMKLDNNKernel : public framework::OpKernel { const bool is_avx512_enabled = platform::MayIUse(platform::avx512f); const bool are_dims_divisable = !(x_int_dims[1] % 16); - const bool is_x_format_correct = x->format() == memory::format::nChw16c; - const bool is_y_format_correct = y->format() == memory::format::nc; + const bool is_x_format_correct = x->format() == MKLDNNMemoryFormat::nChw16c; + const bool is_y_format_correct = y->format() == MKLDNNMemoryFormat::nc; if (is_x_format_correct && is_y_format_correct && are_dims_divisable && is_avx512_enabled) { int pre, n, post; @@ -133,12 +134,12 @@ class ElementwiseMulMKLDNNKernel : public framework::OpKernel { } else { // Fallback to naive version: const bool are_inputs_in_same_format = x->format() == y->format(); - const bool is_x_nchw = x->format() == memory::format::nchw; - const bool is_x_nc = x->format() == memory::format::nc; - const bool is_x_x = x->format() == memory::format::x; - const bool is_y_nchw = y->format() == memory::format::nchw; - const bool is_y_nc = y->format() == memory::format::nc; - const bool is_y_x = y->format() == memory::format::x; + const bool is_x_nchw = x->format() == MKLDNNMemoryFormat::nchw; + const bool is_x_nc = x->format() == MKLDNNMemoryFormat::nc; + const bool is_x_x = x->format() == MKLDNNMemoryFormat::x; + const bool is_y_nchw = y->format() == MKLDNNMemoryFormat::nchw; + const bool is_y_nc = y->format() == MKLDNNMemoryFormat::nc; + const bool is_y_x = y->format() == MKLDNNMemoryFormat::x; if (!are_inputs_in_same_format) { using platform::MKLDNNDeviceContext; auto& dev_ctx = ctx.template device_context(); diff --git a/paddle/fluid/operators/mkldnn/activation_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/activation_mkldnn_op.cc index 52de20258a9..0714cb86ffd 100644 --- a/paddle/fluid/operators/mkldnn/activation_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/activation_mkldnn_op.cc @@ -47,9 +47,10 @@ class MKLDNNActivationKernel public: void Compute(const framework::ExecutionContext &ctx) const override { const auto *x = ctx.Input("X"); - PADDLE_ENFORCE(x->layout() == DataLayout::kMKLDNN && - x->format() != memory::format::format_undef, - "Wrong layout/format set for Input x tensor"); + PADDLE_ENFORCE_EQ(x->layout(), DataLayout::kMKLDNN, + "Wrong layout set for X tensor"); + PADDLE_ENFORCE_NE(x->format(), MKLDNNMemoryFormat::format_undef, + "Wrong format set for X tensor"); Functor functor; functor(ctx); @@ -62,12 +63,13 @@ class MKLDNNActivationGradKernel public: void Compute(const framework::ExecutionContext &ctx) const override { const auto *diff_y = ctx.Input(framework::GradVarName("Out")); - PADDLE_ENFORCE(diff_y->layout() == DataLayout::kMKLDNN && - diff_y->format() != memory::format::format_undef, - "Wrong layout/format set for Input OutGrad tensor"); + PADDLE_ENFORCE_EQ(diff_y->layout(), DataLayout::kMKLDNN, + "Wrong layout set for Input OutGrad tensor"); + PADDLE_ENFORCE_NE(diff_y->format(), MKLDNNMemoryFormat::format_undef, + "Wrong format set for Input OutGrad tensor"); - PADDLE_ENFORCE( - !ctx.Attr("is_test"), + PADDLE_ENFORCE_EQ( + ctx.Attr("is_test"), false, "is_test attribute should be set to False in training phase."); Functor functor; @@ -97,8 +99,7 @@ void eltwise_forward(const framework::ExecutionContext &ctx, std::vector src_tz = framework::vectorize2int(x->dims()); - auto src_format = - src_tz.size() == 2 ? mkldnn::memory::format::nc : x->format(); + auto src_format = src_tz.size() == 2 ? MKLDNNMemoryFormat::nc : x->format(); bool is_test = ctx.Attr("is_test"); @@ -152,10 +153,10 @@ void eltwise_grad(const framework::ExecutionContext &ctx, // diff_dst and src dims should be the same auto src_format = - diff_dst_tz.size() == 2 ? mkldnn::memory::format::nc : x->format(); + diff_dst_tz.size() == 2 ? MKLDNNMemoryFormat::nc : x->format(); auto diff_y_format = - diff_dst_tz.size() == 2 ? mkldnn::memory::format::nc : diff_y->format(); + diff_dst_tz.size() == 2 ? MKLDNNMemoryFormat::nc : diff_y->format(); auto diff_dst_md = platform::MKLDNNMemDesc( diff_dst_tz, platform::MKLDNNGetDataType(), diff_y_format); diff --git a/paddle/fluid/operators/mkldnn/batch_norm_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/batch_norm_mkldnn_op.cc index ffe908d6675..92f3a2c8216 100644 --- a/paddle/fluid/operators/mkldnn/batch_norm_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/batch_norm_mkldnn_op.cc @@ -121,7 +121,8 @@ class BatchNormMKLDNNHandler : public platform::MKLDNNHandler { } static std::string GetHash(const memory::dims &input_dims, float epsilon, - unsigned flag, bool is_test, memory::format format, + unsigned flag, bool is_test, + MKLDNNMemoryFormat format, const std::string &suffix = "") { auto dims2str = [](const memory::dims &operand_dims) { std::string dstr = ""; @@ -191,9 +192,10 @@ class BatchNormMKLDNNOpKernel : public paddle::framework::OpKernel { const auto *scale = ctx.Input("Scale"); const auto *shift = ctx.Input("Bias"); - PADDLE_ENFORCE(x->layout() == DataLayout::kMKLDNN && - x->format() != memory::format::format_undef, - "Wrong layout/format set for Input x tensor"); + PADDLE_ENFORCE_EQ(x->layout(), DataLayout::kMKLDNN, + "Wrong layout set for X tensor"); + PADDLE_ENFORCE_NE(x->format(), MKLDNNMemoryFormat::format_undef, + "Wrong format set for X tensor"); const T *x_data = x->data(); const T *mean_data = mean->data(); @@ -230,7 +232,7 @@ class BatchNormMKLDNNOpKernel : public paddle::framework::OpKernel { if (fuse_with_relu) flags |= mkldnn::fuse_bn_relu; // create mkldnn memory from input x tensor - mkldnn::memory::format input_format = + MKLDNNMemoryFormat input_format = platform::MKLDNNFormatForSize(src_tz.size(), x->format()); // keys for backward pass @@ -331,9 +333,10 @@ class BatchNormMKLDNNGradOpKernel : public paddle::framework::OpKernel { auto *diff_scale = ctx.Output(framework::GradVarName("Scale")); auto *diff_shift = ctx.Output(framework::GradVarName("Bias")); - PADDLE_ENFORCE(diff_y->layout() == DataLayout::kMKLDNN && - diff_y->format() != memory::format::format_undef, - "Wrong layout/format set for Input diff_y tensor"); + PADDLE_ENFORCE_EQ(diff_y->layout(), DataLayout::kMKLDNN, + "Wrong layout set for Input diff_y tensor"); + PADDLE_ENFORCE_NE(diff_y->format(), MKLDNNMemoryFormat::format_undef, + "Wrong format set for Input diff_y tensor"); const T *x_data = x->data(); const T *diff_y_data = diff_y->data(); @@ -357,10 +360,10 @@ class BatchNormMKLDNNGradOpKernel : public paddle::framework::OpKernel { using bn_bwd_types = bn_type_traits; - mkldnn::memory::format dst_format = + MKLDNNMemoryFormat dst_format = platform::MKLDNNFormatForSize(src_tz.size(), diff_y->format()); - mkldnn::memory::format input_format = + MKLDNNMemoryFormat input_format = platform::MKLDNNFormatForSize(src_tz.size(), x->format()); unsigned flags = mkldnn::use_scale_shift; @@ -481,9 +484,10 @@ class BatchNormMKLDNNGradOpKernel : public paddle::framework::OpKernel { // set layout/format of output tensors diff_x->set_layout(DataLayout::kMKLDNN); - diff_x->set_format((memory::format)diff_src_memory->get_primitive_desc() - .desc() - .data.format); + diff_x->set_format( + (MKLDNNMemoryFormat)diff_src_memory->get_primitive_desc() + .desc() + .data.format); } else { // primitives already exist UpdateMemoryData(dev_ctx, key_batch_norm_src_mem_p, to_void_cast(x_data)); @@ -509,9 +513,10 @@ class BatchNormMKLDNNGradOpKernel : public paddle::framework::OpKernel { // set layout/format of output tensors diff_x->set_layout(DataLayout::kMKLDNN); - diff_x->set_format((memory::format)diff_src_memory->get_primitive_desc() - .desc() - .data.format); + diff_x->set_format( + (MKLDNNMemoryFormat)diff_src_memory->get_primitive_desc() + .desc() + .data.format); } // execute optional reorder and batch_norm backward primitive diff --git a/paddle/fluid/operators/mkldnn/concat_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/concat_mkldnn_op.cc index 8c3b4e7ab41..03555dbadcb 100644 --- a/paddle/fluid/operators/mkldnn/concat_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/concat_mkldnn_op.cc @@ -30,11 +30,10 @@ using platform::to_void_cast; static void EnforceLayouts(const std::vector inputs) { for (auto* input : inputs) { - const bool is_layout_correct = input->layout() == DataLayout::kMKLDNN; - const bool is_format_defined = - input->format() != memory::format::format_undef; - PADDLE_ENFORCE(is_layout_correct && is_format_defined, - "Wrong layout/format set for Input tensor"); + PADDLE_ENFORCE_EQ(input->layout(), DataLayout::kMKLDNN, + "Wrong layout set for Input tensor"); + PADDLE_ENFORCE_NE(input->format(), MKLDNNMemoryFormat::format_undef, + "Wrong format set for Input tensor"); } } @@ -48,9 +47,9 @@ static memory::primitive_desc CreateMemPrimDesc(const Tensor& input, return mem_prim_desc; } -static mkldnn::memory::format GetDstMemFormat( +static MKLDNNMemoryFormat GetDstMemFormat( const concat::primitive_desc& concat_pd) { - return (memory::format)concat_pd.dst_primitive_desc().desc().data.format; + return (MKLDNNMemoryFormat)concat_pd.dst_primitive_desc().desc().data.format; } static platform::CPUPlace GetCpuPlace( @@ -126,7 +125,7 @@ class ConcatPrimitiveFactory { memory::desc CreateDstMemDescriptor(Tensor* output, const memory::data_type& dt) { auto dst_dims = paddle::framework::vectorize2int(output->dims()); - return memory::desc(dst_dims, dt, memory::format::any); + return memory::desc(dst_dims, dt, MKLDNNMemoryFormat::any); } mkldnn::memory CreateDstMemory(const concat::primitive_desc& concat_pd, diff --git a/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc index e84bcc58cee..9c3ffe45a5a 100644 --- a/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc @@ -60,12 +60,12 @@ inline void GetWeightsTz(std::vector& weights_tz, int groups, // NOLINT } } -inline mkldnn::memory::format GetWeightsFormat(mkldnn::memory::format format, - int groups, bool is_conv3d) { +inline MKLDNNMemoryFormat GetWeightsFormat(MKLDNNMemoryFormat format, + int groups, bool is_conv3d) { if (is_conv3d) { - return (groups == 1) ? format : mkldnn::memory::format::goidhw; + return (groups == 1) ? format : MKLDNNMemoryFormat::goidhw; } else { - return (groups == 1) ? format : mkldnn::memory::format::goihw; + return (groups == 1) ? format : MKLDNNMemoryFormat::goihw; } } @@ -129,22 +129,38 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel { auto* bias = ctx.HasInput("Bias") ? ctx.Input("Bias") : nullptr; auto* output = ctx.Output("Output"); - PADDLE_ENFORCE(input->layout() == DataLayout::kMKLDNN && - input->format() != memory::format::format_undef, - "Wrong layout/format set for Input tensor"); - PADDLE_ENFORCE(filter->layout() == DataLayout::kMKLDNN && - filter->format() != memory::format::format_undef, - "Wrong layout/format set for Filter tensor"); - PADDLE_ENFORCE(input->dims().size() == 4 || input->dims().size() == 5, - "Input must be with 4 or 5 dimensions, i.e. NCHW or NCDHW"); - PADDLE_ENFORCE(filter->dims().size() == 4 || filter->dims().size() == 5, - "Filter must be with 4 or 5 dimensions, i.e. OIHW or OIDHW"); + PADDLE_ENFORCE_EQ(input->layout(), DataLayout::kMKLDNN, + "Wrong layout set for Input tensor"); + PADDLE_ENFORCE_NE(input->format(), MKLDNNMemoryFormat::format_undef, + "Wrong format set for Input tensor"); + + PADDLE_ENFORCE_EQ(filter->layout(), DataLayout::kMKLDNN, + "Wrong layout set for Filter tensor"); + PADDLE_ENFORCE_NE(filter->format(), MKLDNNMemoryFormat::format_undef, + "Wrong format set for Filter tensor"); + + PADDLE_ENFORCE_GE( + input->dims().size(), 4, + "Input must be with 4 or 5 dimensions, i.e. NCHW or NCDHW"); + PADDLE_ENFORCE_LE( + input->dims().size(), 5, + "Input must be with 4 or 5 dimensions, i.e. NCHW or NCDHW"); + + PADDLE_ENFORCE_GE( + filter->dims().size(), 4, + "Filter must be with 4 or 5 dimensions, i.e. OIHW or OIDHW"); + PADDLE_ENFORCE_LE( + filter->dims().size(), 5, + "Filter must be with 4 or 5 dimensions, i.e. OIHW or OIDHW"); + if (bias) { - PADDLE_ENFORCE(bias->layout() == DataLayout::kMKLDNN && - bias->format() != memory::format::format_undef, - "Wrong layout/format set for Bias tensor"); - PADDLE_ENFORCE(bias->dims().size() == 1, - "Bias must only have 1 dimension, i.e. X"); + PADDLE_ENFORCE_EQ(bias->layout(), DataLayout::kMKLDNN, + "Wrong layout set for Bias tensor"); + PADDLE_ENFORCE_NE(bias->format(), MKLDNNMemoryFormat::format_undef, + "Wrong format set for Bias tensor"); + + PADDLE_ENFORCE_EQ(bias->dims().size(), 1, + "Bias must only have 1 dimension, i.e. X"); } std::vector strides = ctx.Attr>("strides"); @@ -182,7 +198,7 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel { std::vector pipeline; auto src_format = input->format(); - mkldnn::memory::format weights_format = + MKLDNNMemoryFormat weights_format = GetWeightsFormat(filter->format(), g, is_conv3d); auto user_src_md = platform::MKLDNNMemDesc( @@ -198,9 +214,9 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel { auto chosen_memory_format = platform::data_format_to_memory_format(data_format); - weights_format = mkldnn::memory::format::any; + weights_format = MKLDNNMemoryFormat::any; // Check the format for user's special output - if (chosen_memory_format != mkldnn::memory::format::any) { + if (chosen_memory_format != MKLDNNMemoryFormat::any) { if (is_conv3d) { chosen_memory_format = platform::MKLDNNFormatForSize(src_tz.size(), chosen_memory_format); @@ -224,7 +240,7 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel { if (bias) { bias_tz = paddle::framework::vectorize2int(bias->dims()); auto bias_md = platform::MKLDNNMemDesc( - bias_tz, platform::MKLDNNGetDataType(), memory::format::x); + bias_tz, platform::MKLDNNGetDataType(), MKLDNNMemoryFormat::x); conv_pd = handler.AcquireConvolutionPrimitiveDescriptor( src_md, weights_md, bias_md, dst_md, strides, paddings, mkldnn_engine, fuse_activation, fuse_alpha, fuse_beta, fuse_residual_conn, @@ -295,7 +311,7 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel { if (bias) { const T* bias_data = bias->data(); auto user_bias_md = platform::MKLDNNMemDesc( - {bias_tz}, platform::MKLDNNGetDataType(), memory::format::x); + {bias_tz}, platform::MKLDNNGetDataType(), MKLDNNMemoryFormat::x); user_bias_memory_p = handler.AcquireBiasMemory(user_bias_md, to_void_cast(bias_data)); @@ -328,22 +344,38 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel { auto* bias = ctx.HasInput("Bias") ? ctx.Input("Bias") : nullptr; auto* output = ctx.Output("Output"); - PADDLE_ENFORCE(input->layout() == DataLayout::kMKLDNN && - input->format() != memory::format::format_undef, - "Wrong layout/format set for Input tensor"); - PADDLE_ENFORCE(filter->layout() == DataLayout::kMKLDNN && - filter->format() != memory::format::format_undef, - "Wrong layout/format set for Filter tensor"); - PADDLE_ENFORCE(input->dims().size() == 4 || input->dims().size() == 5, - "Input must be with 4 or 5 dimensions, i.e. NCHW or NCDHW"); - PADDLE_ENFORCE(filter->dims().size() == 4 || filter->dims().size() == 5, - "Filter must be with 4 or 5 dimensions, i.e. OIHW or OIDHW"); + PADDLE_ENFORCE_EQ(input->layout(), DataLayout::kMKLDNN, + "Wrong layout set for Input tensor"); + PADDLE_ENFORCE_NE(input->format(), MKLDNNMemoryFormat::format_undef, + "Wrong format set for Input tensor"); + + PADDLE_ENFORCE_EQ(filter->layout(), DataLayout::kMKLDNN, + "Wrong layout set for Filter tensor"); + PADDLE_ENFORCE_NE(filter->format(), MKLDNNMemoryFormat::format_undef, + "Wrong format set for Filter tensor"); + + PADDLE_ENFORCE_GE( + input->dims().size(), 4, + "Input must be with 4 or 5 dimensions, i.e. NCHW or NCDHW"); + PADDLE_ENFORCE_LE( + input->dims().size(), 5, + "Input must be with 4 or 5 dimensions, i.e. NCHW or NCDHW"); + + PADDLE_ENFORCE_GE( + filter->dims().size(), 4, + "Filter must be with 4 or 5 dimensions, i.e. OIHW or OIDHW"); + PADDLE_ENFORCE_LE( + filter->dims().size(), 5, + "Filter must be with 4 or 5 dimensions, i.e. OIHW or OIDHW"); + if (bias) { - PADDLE_ENFORCE(bias->layout() == DataLayout::kMKLDNN && - bias->format() != memory::format::format_undef, - "Wrong layout/format set for Bias tensor"); - PADDLE_ENFORCE(bias->dims().size() == 1, - "Bias must only have 1 dimension, i.e. X"); + PADDLE_ENFORCE_EQ(bias->layout(), DataLayout::kMKLDNN, + "Wrong layout set for Bias tensor"); + PADDLE_ENFORCE_NE(bias->format(), MKLDNNMemoryFormat::format_undef, + "Wrong format set for Bias tensor"); + + PADDLE_ENFORCE_EQ(bias->dims().size(), 1, + "Bias must only have 1 dimension, i.e. X"); } std::vector strides = ctx.Attr>("strides"); @@ -456,8 +488,7 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel { platform::MKLDNNMemDesc({src_tz}, src_dt, input->format()); auto user_weights_md = platform::MKLDNNMemDesc( {weights_tz}, platform::MKLDNNGetDataType(), - ((g) == 1) ? mkldnn::memory::format::oihw - : mkldnn::memory::format::goihw); + ((g) == 1) ? MKLDNNMemoryFormat::oihw : MKLDNNMemoryFormat::goihw); /* create memory descriptor for convolution without specified format * ('any') which lets a primitive (convolution in this case) choose @@ -485,7 +516,7 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel { if (bias) { bias_tz = paddle::framework::vectorize2int(bias->dims()); auto bias_md = platform::MKLDNNMemDesc(bias_tz, memory::data_type::s32, - mkldnn::memory::format::x); + MKLDNNMemoryFormat::x); conv_pd = handler->AcquireConvolutionPrimitiveDescriptor( src_md, weights_md, bias_md, dst_md, strides, paddings, mkldnn_engine, fuse_activation, fuse_alpha, fuse_beta, @@ -545,7 +576,7 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel { if (bias) { const K* bias_data = bias->data(); auto user_bias_md = platform::MKLDNNMemDesc( - {bias_tz}, platform::MKLDNNGetDataType(), memory::format::x); + {bias_tz}, platform::MKLDNNGetDataType(), MKLDNNMemoryFormat::x); auto user_bias_memory_p = handler->AcquireBiasMemory( user_bias_md, to_void_cast(bias_data)); std::shared_ptr bias_memory_p; @@ -641,18 +672,23 @@ class ConvMKLDNNGradOpKernel : public paddle::framework::OpKernel { Tensor* input_grad = ctx.Output(framework::GradVarName("Input")); Tensor* filter_grad = ctx.Output(framework::GradVarName("Filter")); - PADDLE_ENFORCE(input->layout() == DataLayout::kMKLDNN && - input->format() != memory::format::format_undef, - "Wrong layout/format set for Input tensor"); - PADDLE_ENFORCE(filter->layout() == DataLayout::kMKLDNN && - filter->format() != memory::format::format_undef, - "Wrong layout/format set for Filter tensor"); - PADDLE_ENFORCE(output_grad->layout() == DataLayout::kMKLDNN && - output_grad->format() != memory::format::format_undef, - "Wrong layout/format set for output_grad tensor"); + PADDLE_ENFORCE_EQ(input->layout(), DataLayout::kMKLDNN, + "Wrong layout set for Input tensor"); + PADDLE_ENFORCE_NE(input->format(), MKLDNNMemoryFormat::format_undef, + "Wrong format set for Input tensor"); - PADDLE_ENFORCE( - !ctx.Attr("is_test"), + PADDLE_ENFORCE_EQ(filter->layout(), DataLayout::kMKLDNN, + "Wrong layout set for Filter tensor"); + PADDLE_ENFORCE_NE(filter->format(), MKLDNNMemoryFormat::format_undef, + "Wrong format set for Filter tensor"); + + PADDLE_ENFORCE_EQ(output_grad->layout(), DataLayout::kMKLDNN, + "Wrong layout set for output_grad tensor"); + PADDLE_ENFORCE_NE(output_grad->format(), MKLDNNMemoryFormat::format_undef, + "Wrong format set for output_grad tensor"); + + PADDLE_ENFORCE_EQ( + ctx.Attr("is_test"), false, "is_test attribute should be set to False in training phase."); if (!input_grad && !filter_grad) return; @@ -677,7 +713,7 @@ class ConvMKLDNNGradOpKernel : public paddle::framework::OpKernel { std::vector dst_tz = paddle::framework::vectorize2int(output_grad->dims()); auto src_format = input->format(); - mkldnn::memory::format weights_format = + MKLDNNMemoryFormat weights_format = GetWeightsFormat(filter->format(), g, is_conv3d); // Get an unique name from "argument" name of "input" and "Filter" variable @@ -706,9 +742,9 @@ class ConvMKLDNNGradOpKernel : public paddle::framework::OpKernel { auto chosen_memory_format = platform::data_format_to_memory_format(data_format); - weights_format = mkldnn::memory::format::any; + weights_format = MKLDNNMemoryFormat::any; // Check the format for user's special output - if (chosen_memory_format != mkldnn::memory::format::any) { + if (chosen_memory_format != MKLDNNMemoryFormat::any) { if (is_conv3d) { chosen_memory_format = platform::MKLDNNFormatForSize(src_tz.size(), chosen_memory_format); diff --git a/paddle/fluid/operators/mkldnn/conv_transpose_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/conv_transpose_mkldnn_op.cc index 79361d45f34..e7758ba19b7 100644 --- a/paddle/fluid/operators/mkldnn/conv_transpose_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/conv_transpose_mkldnn_op.cc @@ -45,23 +45,29 @@ class ConvTransposeMKLDNNOpKernel : public paddle::framework::OpKernel { auto* bias = ctx.HasInput("Bias") ? ctx.Input("Bias") : nullptr; auto* output = ctx.Output("Output"); - PADDLE_ENFORCE(input->layout() == DataLayout::kMKLDNN && - input->format() != mkldnn::memory::format::format_undef, - "Wrong layout/format set for Input tensor"); - PADDLE_ENFORCE(filter->layout() == DataLayout::kMKLDNN && - filter->format() != mkldnn::memory::format::format_undef, - "Wrong layout/format set for Filter tensor"); - PADDLE_ENFORCE(input->dims().size() == 4, - "Input must be with 4 dimensions, i.e. NCHW"); - PADDLE_ENFORCE(filter->dims().size() == 4, - "Filter must be with 4 dimensions, i.e. OIHW"); + PADDLE_ENFORCE_EQ(input->layout(), DataLayout::kMKLDNN, + "Wrong layout set for Input tensor"); + PADDLE_ENFORCE_NE(input->format(), MKLDNNMemoryFormat::format_undef, + "Wrong format set for Input tensor"); + + PADDLE_ENFORCE_EQ(filter->layout(), DataLayout::kMKLDNN, + "Wrong layout set for Filter tensor"); + PADDLE_ENFORCE_NE(filter->format(), MKLDNNMemoryFormat::format_undef, + "Wrong format set for Filter tensor"); + + PADDLE_ENFORCE_EQ(input->dims().size(), 4, + "Input must be with 4 dimensions, i.e. NCHW"); + PADDLE_ENFORCE_EQ(filter->dims().size(), 4, + "Filter must be with 4 dimensions, i.e. OIHW"); if (bias) { - PADDLE_ENFORCE(bias->layout() == DataLayout::kMKLDNN && - bias->format() != mkldnn::memory::format::format_undef, - "Wrong layout/format set for Bias tensor"); - PADDLE_ENFORCE(bias->dims().size() == 1, - "Bias must only have 1 dimension, i.e. X"); + PADDLE_ENFORCE_EQ(bias->layout(), DataLayout::kMKLDNN, + "Wrong layout set for Bias tensor"); + PADDLE_ENFORCE_NE(bias->format(), MKLDNNMemoryFormat::format_undef, + "Wrong format set for Bias tensor"); + + PADDLE_ENFORCE_EQ(bias->dims().size(), 1, + "Bias must only have 1 dimension, i.e. X"); } std::vector strides = ctx.Attr>("strides"); @@ -129,10 +135,9 @@ class ConvTransposeMKLDNNOpKernel : public paddle::framework::OpKernel { auto user_src_md = platform::MKLDNNMemDesc( {src_tz}, platform::MKLDNNGetDataType(), input->format()); - auto user_weights_md = - platform::MKLDNNMemDesc({weights_tz}, platform::MKLDNNGetDataType(), - (g == 1) ? mkldnn::memory::format::oihw - : mkldnn::memory::format::goihw); + auto user_weights_md = platform::MKLDNNMemDesc( + {weights_tz}, platform::MKLDNNGetDataType(), + (g == 1) ? MKLDNNMemoryFormat::oihw : MKLDNNMemoryFormat::goihw); /* create memory descriptor for convolution without specified format * ('any') which lets a primitive (convolution in this case) choose @@ -163,7 +168,7 @@ class ConvTransposeMKLDNNOpKernel : public paddle::framework::OpKernel { if (bias) { bias_tz = paddle::framework::vectorize2int(bias->dims()); auto bias_md = platform::MKLDNNMemDesc( - bias_tz, platform::MKLDNNGetDataType(), mkldnn::memory::format::x); + bias_tz, platform::MKLDNNGetDataType(), MKLDNNMemoryFormat::x); conv_transpose_pd = handler.AcquireConvolutionPrimitiveDescriptor( src_md, weights_md, bias_md, dst_md, strides, paddings, mkldnn_engine, fuse_activation, fuse_alpha, fuse_beta, false, fwd_prop_kind); @@ -198,9 +203,8 @@ class ConvTransposeMKLDNNOpKernel : public paddle::framework::OpKernel { std::shared_ptr conv_p; if (bias) { const T* bias_data = bias->data(); - auto user_bias_md = - platform::MKLDNNMemDesc({bias_tz}, platform::MKLDNNGetDataType(), - mkldnn::memory::format::x); + auto user_bias_md = platform::MKLDNNMemDesc( + {bias_tz}, platform::MKLDNNGetDataType(), MKLDNNMemoryFormat::x); auto user_bias_memory_p = handler.AcquireBiasMemory( user_bias_md, platform::to_void_cast(bias_data)); diff --git a/paddle/fluid/operators/mkldnn/dequantize_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/dequantize_mkldnn_op.cc index accc9a9d71f..336bd564462 100644 --- a/paddle/fluid/operators/mkldnn/dequantize_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/dequantize_mkldnn_op.cc @@ -63,7 +63,7 @@ class DeQuantOpKernel : public framework::OpKernel { std::vector dst_tz = paddle::framework::vectorize2int(output->dims()); mkldnn::memory::data_type src_dt = paddle::framework::ToMKLDNNDataType(input->type()); - mkldnn::memory::format src_fmt = input->format(); + MKLDNNMemoryFormat src_fmt = input->format(); std::string key = CreateKey(ctx, src_dt, src_tz, reorder_scale[0]); const std::string key_prim = key + "@reorder_p"; const std::string key_src_mem = key + "@src_mem"; @@ -87,7 +87,7 @@ class DeQuantOpKernel : public framework::OpKernel { std::shared_ptr(new primitive::at(*src_memory)); auto dst_md = platform::MKLDNNMemDesc({dst_tz}, memory::data_type::f32, - memory::format::nchw); + MKLDNNMemoryFormat::nchw); auto dst_pd = mkldnn::memory::primitive_desc(dst_md, engine); dst_memory = std::make_shared( dst_pd, to_void_cast(output_data)); diff --git a/paddle/fluid/operators/mkldnn/fc_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/fc_mkldnn_op.cc index b525eaac3ef..8f720f3268e 100644 --- a/paddle/fluid/operators/mkldnn/fc_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/fc_mkldnn_op.cc @@ -59,7 +59,7 @@ class FCPrimitiveFactory { weights_ = CreateFourDimWeightsMemory(input, weights); } - auto dst_desc = CreateMemDescriptor(output, memory::format::any); + auto dst_desc = CreateMemDescriptor(output, MKLDNNMemoryFormat::any); fc_ = CreateFcPrimitive(*input_, *weights_, dst_desc, bias, output, ctx); return *fc_; @@ -70,14 +70,14 @@ class FCPrimitiveFactory { const Tensor* in) { input_->set_data_handle(const_cast(in->data())); output_->set_data_handle(out->mutable_data(ctx.GetPlace())); - if (out->format() == memory::format::format_undef) { + if (out->format() == MKLDNNMemoryFormat::format_undef) { auto output_format = output_->get_primitive_desc().desc().data.format; - out->set_format((memory::format)output_format); + out->set_format((MKLDNNMemoryFormat)output_format); } } - memory::format MatchWeightFormat(memory::format fmt) { - using format = memory::format; + MKLDNNMemoryFormat MatchWeightFormat(MKLDNNMemoryFormat fmt) { + using format = MKLDNNMemoryFormat; switch (fmt) { case format::nChw16c: return format::oIhw16i; @@ -102,13 +102,13 @@ class FCPrimitiveFactory { } static mkldnn::memory::desc CreateMemDescriptor(const std::vector& dims, - memory::format format) { + MKLDNNMemoryFormat format) { return platform::MKLDNNMemDesc(dims, platform::MKLDNNGetDataType(), format); } static mkldnn::memory::desc CreateMemDescriptor(const Tensor* tensor, - memory::format format) { + MKLDNNMemoryFormat format) { auto dims = framework::vectorize2int(tensor->dims()); return CreateMemDescriptor(dims, format); } @@ -126,8 +126,8 @@ class FCPrimitiveFactory { mkldnn::memory TransposeWeights(const Tensor* weights) { auto dims = framework::vectorize2int(weights->dims()); std::swap(dims[0], dims[1]); // Correct output dimensions - auto src_desc = CreateMemDescriptor(dims, memory::format::io); - auto dst_desc = CreateMemDescriptor(dims, memory::format::oi); + auto src_desc = CreateMemDescriptor(dims, MKLDNNMemoryFormat::io); + auto dst_desc = CreateMemDescriptor(dims, MKLDNNMemoryFormat::oi); return Reorder(src_desc, dst_desc, weights->data()); } @@ -187,7 +187,7 @@ class FCPrimitiveFactory { auto dims = {weight_dims[1], input_dims[1], input_dims[2], input_dims[3]}; auto dst_format = MatchWeightFormat(input->format()); - auto src_desc = CreateMemDescriptor(dims, memory::format::oihw); + auto src_desc = CreateMemDescriptor(dims, MKLDNNMemoryFormat::oihw); auto dst_desc = CreateMemDescriptor(dims, dst_format); return Reorder(src_desc, dst_desc, weights_->get_data_handle()); @@ -199,7 +199,7 @@ class FCPrimitiveFactory { auto dst_prim_desc = fc_prim_desc.dst_primitive_desc(); auto buffer_size = dst_prim_desc.get_size(); T* output_data = output->mutable_data(ctx.GetPlace(), buffer_size); - output->set_format((memory::format)dst_prim_desc.desc().data.format); + output->set_format((MKLDNNMemoryFormat)dst_prim_desc.desc().data.format); return memory(dst_prim_desc, to_void_cast(output_data)); } diff --git a/paddle/fluid/operators/mkldnn/mul_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/mul_mkldnn_op.cc index 6d870b43acf..f20c66be0fe 100644 --- a/paddle/fluid/operators/mkldnn/mul_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/mul_mkldnn_op.cc @@ -62,10 +62,10 @@ class MulPrimitiveFactory { return *mul_; } - auto src_desc = CreateMemDescriptor(&x_matrix, memory::format::nc); + auto src_desc = CreateMemDescriptor(&x_matrix, MKLDNNMemoryFormat::nc); x_input_ = CreateMemory(src_desc, &x_matrix); y_input_ = TransposeInputY(&y_matrix); - auto dst_desc = CreateMemDescriptor(output, memory::format::any); + auto dst_desc = CreateMemDescriptor(output, MKLDNNMemoryFormat::any); mul_ = CreateMulPrimitive(*x_input_, *y_input_, dst_desc, output, ctx); return *mul_; @@ -77,14 +77,14 @@ class MulPrimitiveFactory { const ExecutionContext &ctx) { Tensor x_tmp; Tensor data_matrix; - memory::format src_fmt = data->format(); - memory::format dst_fmt; + MKLDNNMemoryFormat src_fmt = data->format(); + MKLDNNMemoryFormat dst_fmt; auto src_mdesc = CreateMemDescriptor(data, src_fmt); if ((data->dims().size() == 4 && - src_fmt != (dst_fmt = memory::format::nchw)) || + src_fmt != (dst_fmt = MKLDNNMemoryFormat::nchw)) || (data->dims().size() == 5 && - src_fmt != (dst_fmt = memory::format::ncdhw))) { + src_fmt != (dst_fmt = MKLDNNMemoryFormat::ncdhw))) { auto dst_mdesc = CreateMemDescriptor(data, dst_fmt); x_tmp.mutable_data(ctx.GetPlace(), data->memory_size()); @@ -92,7 +92,7 @@ class MulPrimitiveFactory { to_void_cast(x_tmp.data())); x_tmp.Resize(data->dims()); - x_tmp.set_format((memory::format)dst_mdesc.data.format); + x_tmp.set_format((MKLDNNMemoryFormat)dst_mdesc.data.format); data_matrix = framework::ReshapeToMatrix(x_tmp, num_col_dims); } else { data_matrix = framework::ReshapeToMatrix(*data, num_col_dims); @@ -106,15 +106,15 @@ class MulPrimitiveFactory { x_input_->set_data_handle(to_void_cast(in->data())); output_->set_data_handle(out->mutable_data(ctx.GetPlace())); - if (out->format() == memory::format::format_undef) { + if (out->format() == MKLDNNMemoryFormat::format_undef) { auto output_format = output_->get_primitive_desc().desc().data.format; - out->set_format((memory::format)output_format); + out->set_format((MKLDNNMemoryFormat)output_format); } } template memory::desc CreateMemDescriptor( - const Tensor *tensor, memory::format format, + const Tensor *tensor, MKLDNNMemoryFormat format, memory::data_type type = platform::MKLDNNGetDataType()) { auto dims = framework::vectorize2int(tensor->dims()); return platform::MKLDNNMemDesc(dims, type, format); @@ -122,7 +122,7 @@ class MulPrimitiveFactory { template memory::desc CreateMemDescriptor( - const std::vector &dims, memory::format format, + const std::vector &dims, MKLDNNMemoryFormat format, memory::data_type type = platform::MKLDNNGetDataType()) { return platform::MKLDNNMemDesc(dims, type, format); } @@ -139,7 +139,7 @@ class MulPrimitiveFactory { auto buffer_size = dst_prim_desc.get_size(); OT *output_data = output->mutable_data(ctx.GetPlace(), buffer_size); - output->set_format((memory::format)dst_prim_desc.desc().data.format); + output->set_format((MKLDNNMemoryFormat)dst_prim_desc.desc().data.format); return memory(dst_prim_desc, to_void_cast(output_data)); } @@ -158,8 +158,8 @@ class MulPrimitiveFactory { memory TransposeInputY(const Tensor *input_y) { auto dims = framework::vectorize2int(input_y->dims()); std::swap(dims[0], dims[1]); // Correct output dimensions - auto src_desc = CreateMemDescriptor(dims, memory::format::io); - auto dst_desc = CreateMemDescriptor(dims, memory::format::oi); + auto src_desc = CreateMemDescriptor(dims, MKLDNNMemoryFormat::io); + auto dst_desc = CreateMemDescriptor(dims, MKLDNNMemoryFormat::oi); return Reorder(src_desc, dst_desc, to_void_cast(input_y->data())); } @@ -230,15 +230,15 @@ class QuantMulPrimitiveFactory : public MulPrimitiveFactory { return *(this->mul_); } - auto src_desc = - this->template CreateMemDescriptor(&x_matrix, memory::format::nc); + auto src_desc = this->template CreateMemDescriptor( + &x_matrix, MKLDNNMemoryFormat::nc); this->x_input_ = this->template CreateMemory(src_desc, &x_matrix); const auto trans_y = this->TransposeInputY(&y_matrix); this->y_input_ = QuantInputY(trans_y, scale_y); auto dst_desc = - this->template CreateMemDescriptor(output, memory::format::any); + this->template CreateMemDescriptor(output, MKLDNNMemoryFormat::any); this->mul_ = CreateMulPrimitive(*(this->x_input_), *(this->y_input_), dst_desc, output, ctx); @@ -270,9 +270,9 @@ class QuantMulPrimitiveFactory : public MulPrimitiveFactory { auto y_dims = std::vector(dims, dims + ndims); auto user_y_desc = - this->template CreateMemDescriptor(y_dims, memory::format::oi); - auto y_desc = - this->template CreateMemDescriptor(y_dims, memory::format::oi); + this->template CreateMemDescriptor(y_dims, MKLDNNMemoryFormat::oi); + auto y_desc = this->template CreateMemDescriptor( + y_dims, MKLDNNMemoryFormat::oi); return ReorderWithScale(user_y_desc, y_desc, input_y.get_data_handle(), scale_y); diff --git a/paddle/fluid/operators/mkldnn/pool_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/pool_mkldnn_op.cc index 52554800a30..6fbbe8fee22 100644 --- a/paddle/fluid/operators/mkldnn/pool_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/pool_mkldnn_op.cc @@ -42,9 +42,10 @@ class PoolMKLDNNOpKernel : public paddle::framework::OpKernel { const Tensor* input = ctx.Input("X"); Tensor* output = ctx.Output("Out"); - PADDLE_ENFORCE(input->layout() == DataLayout::kMKLDNN && - input->format() != memory::format::format_undef, - "Wrong layout/format set for Input tensor"); + PADDLE_ENFORCE_EQ(input->layout(), DataLayout::kMKLDNN, + "Wrong layout set for Input tensor"); + PADDLE_ENFORCE_NE(input->format(), MKLDNNMemoryFormat::format_undef, + "Wrong format set for Input tensor"); std::string pooling_type = ctx.Attr("pooling_type"); std::vector ksize = ctx.Attr>("ksize"); @@ -72,7 +73,7 @@ class PoolMKLDNNOpKernel : public paddle::framework::OpKernel { std::vector dst_tz = paddle::framework::vectorize2int(output->dims()); auto input_format = input->format(); - memory::format output_format{memory::format::format_undef}; + MKLDNNMemoryFormat output_format{MKLDNNMemoryFormat::format_undef}; mkldnn::memory::data_type dt = paddle::framework::ToMKLDNNDataType(input->type()); @@ -95,8 +96,7 @@ class PoolMKLDNNOpKernel : public paddle::framework::OpKernel { * ('any') which lets a primitive (pooling in this case) choose * the memory format preferred for best performance */ - auto dst_md = - platform::MKLDNNMemDesc(dst_tz, dt, mkldnn::memory::format::any); + auto dst_md = platform::MKLDNNMemDesc(dst_tz, dt, MKLDNNMemoryFormat::any); auto pooling_pd = handler.AcquirePoolingPrimitiveDescriptor( src_tz, dst_tz, src_md, dst_md, ksize, strides, paddings, @@ -112,7 +112,7 @@ class PoolMKLDNNOpKernel : public paddle::framework::OpKernel { stream(stream::kind::eager).submit(pipeline).wait(); output_format = - (memory::format)dst_memory->get_primitive_desc().desc().data.format; + (MKLDNNMemoryFormat)dst_memory->get_primitive_desc().desc().data.format; output->set_layout(DataLayout::kMKLDNN); output->set_format(output_format); @@ -130,15 +130,18 @@ class PoolMKLDNNGradOpKernel : public paddle::framework::OpKernel { const Tensor* out_grad = ctx.Input(framework::GradVarName("Out")); Tensor* in_x_grad = ctx.Output(framework::GradVarName("X")); - PADDLE_ENFORCE(in_x->layout() == DataLayout::kMKLDNN && - in_x->format() != memory::format::format_undef, - "Wrong layout/format set for Input X tensor"); - PADDLE_ENFORCE(out_grad->layout() == DataLayout::kMKLDNN && - out_grad->format() != memory::format::format_undef, - "Wrong layout/format set for Input output_grad tensor"); + PADDLE_ENFORCE_EQ(in_x->layout(), DataLayout::kMKLDNN, + "Wrong layout set for Input tensor"); + PADDLE_ENFORCE_NE(in_x->format(), MKLDNNMemoryFormat::format_undef, + "Wrong format set for Input tensor"); - PADDLE_ENFORCE( - !ctx.Attr("is_test"), + PADDLE_ENFORCE_EQ(out_grad->layout(), DataLayout::kMKLDNN, + "Wrong layout set for Input output_grad tensor"); + PADDLE_ENFORCE_NE(out_grad->format(), MKLDNNMemoryFormat::format_undef, + "Wrong format set for Input output_grad tensor"); + + PADDLE_ENFORCE_EQ( + ctx.Attr("is_test"), false, "is_test attribute should be set to False in training phase."); std::string pooling_type = ctx.Attr("pooling_type"); @@ -161,7 +164,7 @@ class PoolMKLDNNGradOpKernel : public paddle::framework::OpKernel { const T* out_grad_data = out_grad->data(); T* in_x_grad_data = in_x_grad->mutable_data(ctx.GetPlace()); - memory::format in_x_grad_format{memory::format::format_undef}; + MKLDNNMemoryFormat in_x_grad_format{MKLDNNMemoryFormat::format_undef}; std::vector diff_src_tz = paddle::framework::vectorize2int(in_x_grad->dims()); @@ -186,9 +189,8 @@ class PoolMKLDNNGradOpKernel : public paddle::framework::OpKernel { auto diff_dst_memory = handler.AcquireDiffDstMemory( diff_dst_md, to_void_cast(out_grad_data)); - auto diff_src_md = - platform::MKLDNNMemDesc(diff_src_tz, platform::MKLDNNGetDataType(), - mkldnn::memory::format::any); + auto diff_src_md = platform::MKLDNNMemDesc( + diff_src_tz, platform::MKLDNNGetDataType(), MKLDNNMemoryFormat::any); auto bwd_pd = handler.AcquirePoolingBackwardPrimitiveDescriptor( diff_dst_md, diff_src_md, ksize, strides, paddings); @@ -202,7 +204,7 @@ class PoolMKLDNNGradOpKernel : public paddle::framework::OpKernel { pipeline.push_back(*pool_bwd_p); mkldnn::stream(mkldnn::stream::kind::eager).submit(pipeline).wait(); - in_x_grad_format = (memory::format)diff_src_memory->get_primitive_desc() + in_x_grad_format = (MKLDNNMemoryFormat)diff_src_memory->get_primitive_desc() .desc() .data.format; in_x_grad->set_layout(DataLayout::kMKLDNN); diff --git a/paddle/fluid/operators/mkldnn/requantize_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/requantize_mkldnn_op.cc index 9dfbafe53be..ece0c3250c9 100644 --- a/paddle/fluid/operators/mkldnn/requantize_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/requantize_mkldnn_op.cc @@ -48,8 +48,8 @@ class ReQuantOpKernel : public framework::OpKernel { mkldnn::memory::data_type src_dt = paddle::framework::ToMKLDNNDataType(input->type()); mkldnn::memory::data_type dst_dt = src_dt; - mkldnn::memory::format src_fmt = memory::format::nhwc; - mkldnn::memory::format dst_fmt = memory::format::nhwc; + MKLDNNMemoryFormat src_fmt = MKLDNNMemoryFormat::nhwc; + MKLDNNMemoryFormat dst_fmt = MKLDNNMemoryFormat::nhwc; const T* input_data = input->data(); T* output_data = output->mutable_data(ctx.GetPlace()); diff --git a/paddle/fluid/operators/mkldnn/softmax_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/softmax_mkldnn_op.cc index 66be0cd2e32..51669f30532 100644 --- a/paddle/fluid/operators/mkldnn/softmax_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/softmax_mkldnn_op.cc @@ -36,7 +36,7 @@ template class SoftmaxMKLDNNHandler : public platform::MKLDNNHandler { public: SoftmaxMKLDNNHandler(const std::vector& dims, - const mkldnn::memory::format fmt, + const MKLDNNMemoryFormat fmt, const platform::MKLDNNDeviceContext& dev_ctx, mkldnn::engine engine, const std::string& base_key) : platform::MKLDNNHandler(dev_ctx, engine, base_key), @@ -44,8 +44,8 @@ class SoftmaxMKLDNNHandler : public platform::MKLDNNHandler { fmt_(fmt) {} SoftmaxMKLDNNHandler(const std::vector& dims, - const mkldnn::memory::format fmt, - const mkldnn::memory::format diff_fmt, + const MKLDNNMemoryFormat fmt, + const MKLDNNMemoryFormat diff_fmt, const platform::MKLDNNDeviceContext& dev_ctx, mkldnn::engine engine, const std::string& base_key) : platform::MKLDNNHandler(dev_ctx, engine, base_key), @@ -165,8 +165,8 @@ class SoftmaxMKLDNNHandler : public platform::MKLDNNHandler { private: std::vector dims_; - mkldnn::memory::format fmt_; - mkldnn::memory::format diff_fmt_; + MKLDNNMemoryFormat fmt_; + MKLDNNMemoryFormat diff_fmt_; std::shared_ptr fwd_pd_; }; @@ -207,8 +207,8 @@ class SoftmaxMKLDNNKernel : public paddle::framework::OpKernel { const std::string key = platform::MKLDNNHandler::GetHash(softmax_tz, ctx.op().Output("Out")); - SoftmaxMKLDNNHandler handler(softmax_tz, mkldnn::memory::format::nc, - dev_ctx, mkldnn_engine, key); + SoftmaxMKLDNNHandler handler(softmax_tz, MKLDNNMemoryFormat::nc, dev_ctx, + mkldnn_engine, key); // Currently only NC data format is supported auto softmax_src_memory_p = @@ -288,8 +288,8 @@ class SoftmaxMKLDNNGradKernel : public paddle::framework::OpKernel { // TODO(jczaja): Add layouts support when there is a need to do so // Two dimensional softmax does support NC format // Normalization is made after innermost dimension eg. C out of NC - SoftmaxMKLDNNHandler handler(softmax_tz, mkldnn::memory::format::nc, - mkldnn::memory::format::nc, dev_ctx, + SoftmaxMKLDNNHandler handler(softmax_tz, MKLDNNMemoryFormat::nc, + MKLDNNMemoryFormat::nc, dev_ctx, mkldnn_engine, key); auto dst_memory_p = handler.AcquireDstMemory(to_void_cast(dst_data)); diff --git a/paddle/fluid/operators/mkldnn/sum_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/sum_mkldnn_op.cc index 0649c059ecc..fb1477284cf 100644 --- a/paddle/fluid/operators/mkldnn/sum_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/sum_mkldnn_op.cc @@ -65,27 +65,29 @@ class SumMKLDNNOpKernel : public paddle::framework::OpKernel { std::vector dst_tz = framework::vectorize2int(output->dims()); auto src_tz = dst_tz; - memory::format output_format{memory::format::format_undef}; + MKLDNNMemoryFormat output_format{MKLDNNMemoryFormat::format_undef}; std::vector scales; std::vector srcs_mpd; std::vector srcs_mem; - PADDLE_ENFORCE(in_vars[0]->IsType(), - "Input[0] must be LoDTensors"); + PADDLE_ENFORCE_EQ(in_vars[0]->IsType(), true, + "Input[0] must be LoDTensors"); auto& input0 = in_vars[0]->Get(); - PADDLE_ENFORCE(input0.layout() == DataLayout::kMKLDNN && - input0.format() != memory::format::format_undef, - "Wrong layout/format for inputs[0]"); + PADDLE_ENFORCE_EQ(input0.layout(), DataLayout::kMKLDNN, + "Wrong layout set for inputs[0] tensor"); + PADDLE_ENFORCE_NE(input0.format(), MKLDNNMemoryFormat::format_undef, + "Wrong format set for inputs[0] tensor"); - memory::format input_format = input0.format(); + MKLDNNMemoryFormat input_format = input0.format(); for (int i = 0; i < N; i++) { - PADDLE_ENFORCE(in_vars[i]->IsType(), - "all inputs must be all LoDTensors"); + PADDLE_ENFORCE_EQ(in_vars[i]->IsType(), true, + "all inputs must be all LoDTensors"); auto& input = in_vars[i]->Get(); - PADDLE_ENFORCE(input.layout() == DataLayout::kMKLDNN && - input.format() != memory::format::format_undef, - "Wrong layout/format for inputs"); + PADDLE_ENFORCE_EQ(input.layout(), DataLayout::kMKLDNN, + "Wrong layout set for inputs"); + PADDLE_ENFORCE_NE(input.format(), MKLDNNMemoryFormat::format_undef, + "Wrong format set for inputs"); if (input.numel() == 0) { continue; @@ -103,7 +105,7 @@ class SumMKLDNNOpKernel : public paddle::framework::OpKernel { } auto dst_md = - memory::desc(dst_tz, memory::data_type::f32, memory::format::any); + memory::desc(dst_tz, memory::data_type::f32, MKLDNNMemoryFormat::any); auto sum_pd = sum::primitive_desc(dst_md, scales, srcs_mpd); @@ -119,7 +121,7 @@ class SumMKLDNNOpKernel : public paddle::framework::OpKernel { } auto sum_prim = mkldnn::sum(sum_pd, inputs, *dst_mem); - output_format = (memory::format)platform::GetMKLDNNFormat(sum_pd); + output_format = (MKLDNNMemoryFormat)platform::GetMKLDNNFormat(sum_pd); primitive reorder_prim; std::shared_ptr target_mem; diff --git a/paddle/fluid/operators/mkldnn/transpose_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/transpose_mkldnn_op.cc index 88b96dcde8a..1bcf0a3ce61 100644 --- a/paddle/fluid/operators/mkldnn/transpose_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/transpose_mkldnn_op.cc @@ -64,7 +64,7 @@ class TransposeMKLDNNOpKernel : public paddle::framework::OpKernel { mkldnn::stream(mkldnn::stream::kind::eager).submit(pipeline).wait(); output->set_layout(DataLayout::kNCHW); - output->set_format(mkldnn::memory::format::format_undef); + output->set_format(MKLDNNMemoryFormat::format_undef); } }; diff --git a/paddle/fluid/platform/mkldnn_helper.h b/paddle/fluid/platform/mkldnn_helper.h index 4f8c4b7cc8f..f127ef01d7b 100644 --- a/paddle/fluid/platform/mkldnn_helper.h +++ b/paddle/fluid/platform/mkldnn_helper.h @@ -21,6 +21,9 @@ limitations under the License. */ #include "paddle/fluid/framework/operator.h" #include "paddle/fluid/platform/place.h" namespace paddle { +#ifdef PADDLE_WITH_MKLDNN +using MKLDNNMemoryFormat = mkldnn::memory::format; +#endif namespace platform { using MKLDNNStream = mkldnn::stream; @@ -69,7 +72,7 @@ tf_pd MKLDNNBwdPrimitiveDesc(const Engine& e, const Primitive& p, inline mkldnn::memory::desc MKLDNNMemDesc(const std::vector& dims, mkldnn::memory::data_type data_type, - mkldnn::memory::format format) { + MKLDNNMemoryFormat format) { mkldnn::memory::dims tz = dims; return mkldnn::memory::desc({tz}, data_type, format); } @@ -108,71 +111,71 @@ inline void Reorder(const mkldnn::memory& src, const mkldnn::memory& dst) { mkldnn::stream(mkldnn::stream::kind::eager).submit(pipeline).wait(); } -inline mkldnn::memory::format GetMKLDNNFormat(const mkldnn::memory memory) { - return static_cast( +inline MKLDNNMemoryFormat GetMKLDNNFormat(const mkldnn::memory memory) { + return static_cast( memory.get_primitive_desc().desc().data.format); } -inline mkldnn::memory::format GetMKLDNNFormat( +inline MKLDNNMemoryFormat GetMKLDNNFormat( const mkldnn::sum::primitive_desc& memory) { - return static_cast( + return static_cast( memory.dst_primitive_desc().desc().data.format); } -inline mkldnn::memory::format MKLDNNFormatForSize( - size_t dims_size, mkldnn::memory::format data_format) { +inline MKLDNNMemoryFormat MKLDNNFormatForSize(size_t dims_size, + MKLDNNMemoryFormat data_format) { if (dims_size == 1) { - return mkldnn::memory::format::x; + return MKLDNNMemoryFormat::x; } else if (dims_size == 2) { - return mkldnn::memory::format::nc; + return MKLDNNMemoryFormat::nc; } else if (dims_size == 3) { - if (data_format == mkldnn::memory::format::nchw) { - return mkldnn::memory::format::ncw; - } else if (data_format == mkldnn::memory::format::nhwc) { - return mkldnn::memory::format::nwc; + if (data_format == MKLDNNMemoryFormat::nchw) { + return MKLDNNMemoryFormat::ncw; + } else if (data_format == MKLDNNMemoryFormat::nhwc) { + return MKLDNNMemoryFormat::nwc; } } else if (dims_size == 4) { - if (data_format == mkldnn::memory::format::goihw) { - return mkldnn::memory::format::oihw; + if (data_format == MKLDNNMemoryFormat::goihw) { + return MKLDNNMemoryFormat::oihw; } } else if (dims_size == 5) { - if (data_format == mkldnn::memory::format::goidhw) { - return mkldnn::memory::format::oidhw; + if (data_format == MKLDNNMemoryFormat::goidhw) { + return MKLDNNMemoryFormat::oidhw; } - if (data_format == mkldnn::memory::format::nchw) { - return mkldnn::memory::format::ncdhw; - } else if (data_format == mkldnn::memory::format::nhwc) { - return mkldnn::memory::format::ndhwc; + if (data_format == MKLDNNMemoryFormat::nchw) { + return MKLDNNMemoryFormat::ncdhw; + } else if (data_format == MKLDNNMemoryFormat::nhwc) { + return MKLDNNMemoryFormat::ndhwc; } } return data_format; } -inline mkldnn::memory::format data_format_to_memory_format( +inline MKLDNNMemoryFormat data_format_to_memory_format( const std::string& data_format) { switch (framework::StringToDataLayout(data_format)) { case framework::DataLayout::kNHWC: - return mkldnn::memory::format::nhwc; + return MKLDNNMemoryFormat::nhwc; case framework::DataLayout::kNCHW: - return mkldnn::memory::format::nchw; + return MKLDNNMemoryFormat::nchw; default: - return mkldnn::memory::format::any; + return MKLDNNMemoryFormat::any; } } -inline mkldnn::memory::format StringToMKLDNNFormat(std::string* format) { +inline MKLDNNMemoryFormat StringToMKLDNNFormat(std::string* format) { std::transform(format->begin(), format->end(), format->begin(), ::tolower); if (!format->compare("nchw")) { - return mkldnn::memory::format::nchw; + return MKLDNNMemoryFormat::nchw; } else if (!format->compare("nchw16c")) { - return mkldnn::memory::format::nChw16c; + return MKLDNNMemoryFormat::nChw16c; } else if (!format->compare("nchw8c")) { - return mkldnn::memory::format::nChw8c; + return MKLDNNMemoryFormat::nChw8c; } else if (!format->compare("nhwc")) { - return mkldnn::memory::format::nhwc; + return MKLDNNMemoryFormat::nhwc; } else { - return mkldnn::memory::format::any; + return MKLDNNMemoryFormat::any; } } diff --git a/paddle/fluid/platform/mkldnn_reuse.h b/paddle/fluid/platform/mkldnn_reuse.h index 8285e61a069..633be7f3268 100644 --- a/paddle/fluid/platform/mkldnn_reuse.h +++ b/paddle/fluid/platform/mkldnn_reuse.h @@ -121,7 +121,7 @@ class MKLDNNHandler { std::shared_ptr AcquireMemory( const std::vector& dims, const mkldnn::memory::data_type dtype, - const mkldnn::memory::format& fmt, void* ptr, const std::string& suffix) { + const MKLDNNMemoryFormat& fmt, void* ptr, const std::string& suffix) { /*Generate key*/ auto local_key = key_ + suffix; auto mem_p = @@ -236,7 +236,7 @@ class MKLDNNHandler { const mkldnn::memory::dims& weights_dims, const std::vector& strides, const std::vector& paddings, const std::vector& dilations, const int& groups, const mkldnn::memory::data_type& srcdt, - const mkldnn::memory::format& format, const std::string& fuse_activation, + const MKLDNNMemoryFormat& format, const std::string& fuse_activation, const bool& residual, const std::string& suffix) { AppendKeyDims(key, input_dims); @@ -454,9 +454,8 @@ class ActivationMKLDNNHandler : public MKLDNNHandler { static std::string GetHash(const memory::dims& input_dims, const mkldnn::algorithm algorithm, - const mkldnn::memory::format fmt, - const float alpha, const float beta, - const std::string& suffix) { + const MKLDNNMemoryFormat fmt, const float alpha, + const float beta, const std::string& suffix) { std::string key; key.reserve(platform::MKLDNNHandler::MaxKeyLength); platform::MKLDNNHandler::AppendKeyDims(&key, input_dims); @@ -606,7 +605,7 @@ class LRNMKLDNNHandler : public MKLDNNHandler { static std::string GetHash(const memory::dims& input_dims, const int n, const float alpha, const float beta, const float k, - const memory::format& fmt, + const MKLDNNMemoryFormat& fmt, const std::string& suffix) { std::string key; key.reserve(platform::MKLDNNHandler::MaxKeyLength); @@ -691,7 +690,7 @@ class PoolingMKLDNNHandler : public MKLDNNHandler { pooling_type_ == "max" ? fwd_pd_->workspace_primitive_desc() : mkldnn::memory::primitive_desc( - {{}, dt_, mkldnn::memory::format::nchw}, engine_); + {{}, dt_, MKLDNNMemoryFormat::nchw}, engine_); // Pooling PD has to be passed to Grad op that // may be executed by diffrent thread, hence // for that one we use key that does not contain TID @@ -801,7 +800,7 @@ class PoolingMKLDNNHandler : public MKLDNNHandler { const memory::dims& input_dims, const std::string& pooling_type, const std::vector& ksize, const std::vector& strides, const std::vector& paddings, const memory::data_type& dt, - const memory::format& fmt, const std::string& suffix) { + const MKLDNNMemoryFormat& fmt, const std::string& suffix) { std::string key; key.reserve(platform::MKLDNNHandler::MaxKeyLength); platform::MKLDNNHandler::AppendKeyDims(&key, input_dims); @@ -855,7 +854,7 @@ class TransposeMKLDNNHandler : public MKLDNNHandler { logical_axis_(dims.size(), 0) {} std::shared_ptr AcquireSrcMemory( - const mkldnn::memory::format& fmt, void* ptr) { + const MKLDNNMemoryFormat& fmt, void* ptr) { auto local_key = key_ + "@user_src_mem_p"; auto mem_p = std::static_pointer_cast(dev_ctx_.GetBlob(local_key)); @@ -865,7 +864,7 @@ class TransposeMKLDNNHandler : public MKLDNNHandler { for (size_t i = 0; i < logical_axis_.size(); ++i) { logical_axis_[i] = i; } - auto src_md = fmt != mkldnn::memory::format::nchw + auto src_md = fmt != MKLDNNMemoryFormat::nchw ? platform::MKLDNNMemDesc( dims_, platform::MKLDNNGetDataType(), fmt) : Axis2MemoryDesc(dims_, logical_axis_); @@ -967,12 +966,12 @@ class ReorderMKLDNNHandler : public MKLDNNHandler { dtype_(dtype) {} std::shared_ptr AcquireSrcMemory( - const mkldnn::memory::format& fmt, void* ptr) { + const MKLDNNMemoryFormat& fmt, void* ptr) { return this->AcquireMemory(dims_, dtype_, fmt, ptr, "@user_src_mem_p"); } std::shared_ptr AcquireDstMemory( - framework::Tensor* output, const mkldnn::memory::format& fmt, + framework::Tensor* output, const MKLDNNMemoryFormat& fmt, platform::Place place) { auto local_key = key_ + "@user_dst_mem_p"; auto mem_p = @@ -1007,8 +1006,8 @@ class ReorderMKLDNNHandler : public MKLDNNHandler { } static std::string GetHash(std::vector& shape, // NOLINT - mkldnn::memory::format in_fmt, - mkldnn::memory::format out_fmt, + MKLDNNMemoryFormat in_fmt, + MKLDNNMemoryFormat out_fmt, const std::string& suffix) { return dims2str(shape) + std::to_string(in_fmt) + "->" + std::to_string(out_fmt) + "#" + suffix; @@ -1071,8 +1070,8 @@ class ConvMKLDNNTemplateHandler : public MKLDNNHandler { return conv_pd_->dst_primitive_desc().get_size(); } - mkldnn::memory::format GetDstFormat() const { - return static_cast( + MKLDNNMemoryFormat GetDstFormat() const { + return static_cast( conv_pd_->dst_primitive_desc().desc().data.format); } @@ -1435,10 +1434,10 @@ static void SetDstMemoryQuantized( std::shared_ptr& dst_memory) { // NOLINT T* output_data = output->mutable_data(ctx.GetPlace()); const size_t dst_dims = dst_tz.size(); - memory::format dst_fmt; - PADDLE_ENFORCE(dst_dims <= 5, - "Dst memory for quantization can not have dims > 5"); - dst_fmt = platform::MKLDNNFormatForSize(dst_dims, memory::format::nhwc); + MKLDNNMemoryFormat dst_fmt; + PADDLE_ENFORCE_LE(dst_dims, 5, + "Dst memory for quantization can not have dims > 5"); + dst_fmt = platform::MKLDNNFormatForSize(dst_dims, MKLDNNMemoryFormat::nhwc); auto dst_md = platform::MKLDNNMemDesc( {dst_tz}, paddle::framework::ToMKLDNNDataType( -- GitLab