diff --git a/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc index 20f5ffa4d56383a661f339b5d3aef488b9944414..b1b69af145eb40024d4b6cb094924cf8b1b0eaaa 100644 --- a/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/conv_mkldnn_op.cc @@ -338,8 +338,6 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel { const auto& mkldnn_engine = dev_ctx.GetEngine(); auto* input = ctx.Input("Input"); - auto* filter = ctx.Input("Filter"); - auto* bias = ctx.HasInput("Bias") ? ctx.Input("Bias") : nullptr; auto* output = ctx.Output("Output"); PADDLE_ENFORCE_EQ(input->layout(), DataLayout::kMKLDNN, @@ -347,11 +345,6 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel { 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"); @@ -359,57 +352,14 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel { 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_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"); - std::vector paddings = ctx.Attr>("paddings"); - std::vector dilations = ctx.Attr>("dilations"); - int groups = ctx.Attr("groups"); std::string fuse_activation = ctx.Attr("fuse_activation"); - float fuse_alpha = ctx.Attr("fuse_alpha"); - float fuse_beta = ctx.Attr("fuse_beta"); bool fuse_residual_conn = ctx.Attr("fuse_residual_connection"); - bool force_fp32_output = ctx.Attr("force_fp32_output"); bool unsigned_output = (fuse_activation == "relu" || fuse_activation == "relu6"); - PADDLE_ENFORCE(!fuse_residual_conn || !force_fp32_output, - "residual fusion does not support force output with fp32"); - - bool is_conv3d = strides.size() == 3U; - PADDLE_ENFORCE( - is_conv3d - ? dilations.size() == 3 && dilations[0] == 1 && dilations[1] == 1 && - dilations[2] == 1 - : dilations.size() == 2 && dilations[0] == 1 && dilations[1] == 1, - "dilation in convolution is not implemented yet"); - - PADDLE_ENFORCE_NE(is_conv3d, true, - "int8 does not support conv3d currently"); - const T* input_data = input->data(); auto src_tz = paddle::framework::vectorize(input->dims()); - auto weights_tz = paddle::framework::vectorize(filter->dims()); - int g = std::max(groups, 1); - - GetWeightsTz(weights_tz, g, is_conv3d); - auto dst_tz = paddle::framework::vectorize(output->dims()); mkldnn::memory::data_type src_dt = paddle::framework::ToMKLDNNDataType(input->type()); @@ -448,6 +398,63 @@ class ConvMKLDNNOpKernel : public paddle::framework::OpKernel { dev_ctx.GetBlob(prim_key)); if (conv_p == nullptr || !is_test) { + float fuse_alpha = ctx.Attr("fuse_alpha"); + float fuse_beta = ctx.Attr("fuse_beta"); + bool force_fp32_output = ctx.Attr("force_fp32_output"); + + auto* filter = ctx.Input("Filter"); + + 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( + 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"); + + PADDLE_ENFORCE_EQ( + !fuse_residual_conn || !force_fp32_output, true, + "residual fusion does not support force output with fp32"); + + auto* bias = ctx.HasInput("Bias") ? ctx.Input("Bias") : nullptr; + + if (bias) { + 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 paddings = ctx.Attr>("paddings"); + std::vector dilations = ctx.Attr>("dilations"); + std::vector strides = ctx.Attr>("strides"); + + bool is_conv3d = strides.size() == 3U; + + PADDLE_ENFORCE_NE(is_conv3d, true, + "int8 does not support conv3d currently"); + + int groups = ctx.Attr("groups"); + auto weights_tz = paddle::framework::vectorize(filter->dims()); + int g = std::max(groups, 1); + + GetWeightsTz(weights_tz, g, is_conv3d); + auto dst_tz = paddle::framework::vectorize(output->dims()); + + PADDLE_ENFORCE_EQ( + is_conv3d + ? dilations.size() == 3 && dilations[0] == 1 && + dilations[1] == 1 && dilations[2] == 1 + : dilations.size() == 2 && dilations[0] == 1 && dilations[1] == 1, + true, "dilation in convolution is not implemented yet"); + const K* filter_data = filter->data(); auto scale_in_data = ctx.Attr("Scale_in"); auto scale_in_eltwise_data = ctx.Attr("Scale_in_eltwise"); diff --git a/paddle/fluid/platform/mkldnn_helper.h b/paddle/fluid/platform/mkldnn_helper.h index 1ff568cef3401724463ab129c81a522bf1aebc22..f2acd2a82e39bef3b9ee4692d25bc008e2209ab0 100644 --- a/paddle/fluid/platform/mkldnn_helper.h +++ b/paddle/fluid/platform/mkldnn_helper.h @@ -205,7 +205,7 @@ inline void AppendKey(std::string* key, const std::vector& dims) { template inline std::string CreateKey(ArgTypes&&... args) { std::string key; - key.reserve(256); + key.reserve(64); using expand_type = int[]; expand_type{0, (AppendKey(&key, std::forward(args)), 0)...}; return key;