From b8423b0c30b4f63b849189816299f395586dee3b Mon Sep 17 00:00:00 2001 From: zeroRains Date: Wed, 29 Nov 2023 14:57:47 +0000 Subject: [PATCH 1/6] try to fix the bug in fc_mkldnn --- paddle/phi/api/yaml/fused_ops.yaml | 2 +- paddle/phi/api/yaml/op_compat.yaml | 2 +- paddle/phi/infermeta/fusion.cc | 29 +--------- paddle/phi/infermeta/fusion.h | 7 --- paddle/phi/kernels/fusion/onednn/fc_kernel.cc | 53 ++++++++++++++++--- paddle/phi/kernels/impl/fc_kernel_impl.h | 7 --- 6 files changed, 48 insertions(+), 52 deletions(-) diff --git a/paddle/phi/api/yaml/fused_ops.yaml b/paddle/phi/api/yaml/fused_ops.yaml index b9b895c4183e0..45229ae641dbd 100644 --- a/paddle/phi/api/yaml/fused_ops.yaml +++ b/paddle/phi/api/yaml/fused_ops.yaml @@ -110,7 +110,7 @@ data_type : x - op : fc - args : (Tensor input, Tensor w, Tensor bias, int in_num_col_dims = 1, str activation_type = "", bool use_mkldnn = false, bool padding_weights = false, bool use_quantizer = false, str mkldnn_data_type = "float32", float scale_in = 1.0f, float[] scale_weights = {1.0f}, float scale_out = 1.0f, bool force_fp32_output = false) + args : (Tensor input, Tensor w, Tensor bias, int in_num_col_dims = 1, str activation_type = "", bool padding_weights = false) output : Tensor(out) infer_meta : func : FCInferMeta diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index c0a1b73dd07f0..82c3bc309055a 100755 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -1045,7 +1045,7 @@ scale_weights : Scale_weights scale_out : Scale_out extra : - [bool @ALL_KERNELS_MUST_COMPUTE_RUNTIME_SHAPE@ = true] + attrs : [bool use_mkldnn = false, bool use_quantizer = false, str mkldnn_data_type = "float32", float scale_in = 1.0f, "float[] scale_weights = {1.0f}", float scale_out = 1.0f, bool force_fp32_output = false] - op : feed outputs: {out: Out} diff --git a/paddle/phi/infermeta/fusion.cc b/paddle/phi/infermeta/fusion.cc index 7b8fcab75838d..3f8e1536e3ec6 100644 --- a/paddle/phi/infermeta/fusion.cc +++ b/paddle/phi/infermeta/fusion.cc @@ -2921,14 +2921,7 @@ void FCInferMeta(const MetaTensor& input, const MetaTensor& bias, const int in_num_col_dims, const std::string& activation_type, - const bool use_mkldnn, const bool padding_weights, - const bool use_quantizer, - const std::string& mkldnn_data_type, - const float scale_in, - const std::vector& sclae_weights, - const float scale_out, - const bool force_fp32_output, MetaTensor* out) { PADDLE_ENFORCE_GE( in_num_col_dims, @@ -2937,15 +2930,7 @@ void FCInferMeta(const MetaTensor& input, "The in_num_col_dims is expected to equal or greater than 1. " "But received the in_num_col_dims is %d. ", in_num_col_dims)); - std::string mkldnn_data_type_list[] = {"float32", "int8", "bfloat16"}; - PADDLE_ENFORCE_EQ( - std::find(std::begin(mkldnn_data_type_list), - std::end(mkldnn_data_type_list), - mkldnn_data_type) != std::end(mkldnn_data_type_list), - true, - phi::errors::InvalidArgument("The mkldnn_data_type shoule be [float32, " - "int8, bfloat16], but found %s.", - mkldnn_data_type.c_str())); + auto w_dims = w.dims(); PADDLE_ENFORCE_EQ( w_dims.size(), @@ -3018,18 +3003,6 @@ void FCInferMeta(const MetaTensor& input, activation_type.c_str())); } - if (use_mkldnn) { - PADDLE_ENFORCE_EQ( - in_dims.size() >= 2 && in_dims.size() <= 4, - true, - phi::errors::Unimplemented( - "The Input of fc is expected to be a 2-D, 3-D or 4-D tensor when " - "use_mkldnn is set. But received the number of Input's " - "dimensions is %d, Input's shape is %s.", - in_dims.size(), - in_dims)); - } - std::vector output_dims; phi::funcs::FCOutputSize( in_dims, w_dims, output_dims, in_num_col_dims, padding_weights); diff --git a/paddle/phi/infermeta/fusion.h b/paddle/phi/infermeta/fusion.h index 8eb928fd2382e..9c2e1c29a0000 100644 --- a/paddle/phi/infermeta/fusion.h +++ b/paddle/phi/infermeta/fusion.h @@ -705,13 +705,6 @@ void FCInferMeta(const MetaTensor& input, const MetaTensor& bias, const int in_num_col_dims, const std::string& activation_type, - const bool use_mkldnn, const bool padding_weights, - const bool use_quantizer, - const std::string& mkldnn_data_type, - const float scale_in, - const std::vector& sclae_weights, - const float scale_out, - const bool force_fp32_output, MetaTensor* out); } // namespace phi diff --git a/paddle/phi/kernels/fusion/onednn/fc_kernel.cc b/paddle/phi/kernels/fusion/onednn/fc_kernel.cc index 368a1a616eab8..bbee8d3553956 100644 --- a/paddle/phi/kernels/fusion/onednn/fc_kernel.cc +++ b/paddle/phi/kernels/fusion/onednn/fc_kernel.cc @@ -567,17 +567,53 @@ void FCKernel(const Context& dev_ctx, const paddle::optional& bias, const int in_num_col_dims, const std::string& activation_type, - const bool use_mkldnn, const bool padding_weights, - const bool use_quantizer, - const std::string& mkldnn_data_type, - const float scale_in, - const std::vector& scale_weights, - const float scale_out, - const bool force_fp32_output, DenseTensor* out) { + std::cout << "step1__________________________________" << std::endl; + const bool use_mkldnn = + PADDLE_GET_CONST(bool, dev_ctx.GetDnnAttr("use_mkldnn")); + std::cout << "step2__________________________________" << std::endl; + const bool use_quantizer = + PADDLE_GET_CONST(bool, dev_ctx.GetDnnAttr("use_quantizer")); + std::cout << "step3__________________________________" << std::endl; + const std::string mkldnn_data_type = + PADDLE_GET_CONST(std::string, dev_ctx.GetDnnAttr("mkldnn_data_type")); + std::cout << "step4__________________________________" << std::endl; + const float scale_in = + PADDLE_GET_CONST(float, dev_ctx.GetDnnAttr("scale_in")); + std::cout << "step5__________________________________" << std::endl; + const std::vector scale_weights = + PADDLE_GET_CONST(std::vector, dev_ctx.GetDnnAttr("scale_weights")); + std::cout << "step6__________________________________" << std::endl; + const float scale_out = + PADDLE_GET_CONST(float, dev_ctx.GetDnnAttr("scale_out")); + std::cout << "step7__________________________________" << std::endl; + const bool force_fp32_output = + PADDLE_GET_CONST(bool, dev_ctx.GetDnnAttr("force_fp32_output")); + std::cout << "step8__________________________________" << std::endl; + std::string mkldnn_data_type_list[] = {"float32", "int8", "bfloat16"}; + PADDLE_ENFORCE_EQ( + std::find(std::begin(mkldnn_data_type_list), + std::end(mkldnn_data_type_list), + mkldnn_data_type) != std::end(mkldnn_data_type_list), + true, + phi::errors::InvalidArgument("The mkldnn_data_type shoule be [float32, " + "int8, bfloat16], but found %s.", + mkldnn_data_type.c_str())); + auto in_dims = input.dims(); + if (use_mkldnn) { + PADDLE_ENFORCE_EQ( + in_dims.size() >= 2 && in_dims.size() <= 4, + true, + phi::errors::Unimplemented( + "The Input of fc is expected to be a 2-D, 3-D or 4-D tensor when " + "use_mkldnn is set. But received the number of Input's " + "dimensions is %d, Input's shape is %s.", + in_dims.size(), + in_dims)); + } + std::cout << "step9__________________________________" << std::endl; bool fuse_relu = activation_type == "relu"; - IF_CHANGE_FC_TW_TYPENAME((std::is_same::value), ([&] { if (force_fp32_output) { // NOLINT RunKernel(dev_ctx, @@ -647,6 +683,7 @@ void FCKernel(const Context& dev_ctx, out); } })); + std::cout << "step10__________________________________" << std::endl; } } // namespace fusion diff --git a/paddle/phi/kernels/impl/fc_kernel_impl.h b/paddle/phi/kernels/impl/fc_kernel_impl.h index 061f1baad3108..d5fd48b6119ac 100644 --- a/paddle/phi/kernels/impl/fc_kernel_impl.h +++ b/paddle/phi/kernels/impl/fc_kernel_impl.h @@ -30,14 +30,7 @@ void FCKernel(const Context& dev_ctx, const paddle::optional& bias, const int in_num_col_dims, const std::string& activation_type, - const bool use_mkldnn, const bool padding_weights, - const bool use_quantizer, - const std::string& mkldnn_data_type, - const float scale_in, - const std::vector& scale_weights, - const float scale_out, - const bool force_fp32_output, DenseTensor* out) { bool with_relu = (activation_type == "relu") ? true : false; From 1ec7a711c9bc98ed4c5ceb685fb58ed4958cd679 Mon Sep 17 00:00:00 2001 From: zeroRains Date: Fri, 1 Dec 2023 01:39:25 +0000 Subject: [PATCH 2/6] fix the missing attr bug --- paddle/phi/kernels/fusion/onednn/fc_kernel.cc | 41 +++++++++++-------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/paddle/phi/kernels/fusion/onednn/fc_kernel.cc b/paddle/phi/kernels/fusion/onednn/fc_kernel.cc index bbee8d3553956..d6c65e27665aa 100644 --- a/paddle/phi/kernels/fusion/onednn/fc_kernel.cc +++ b/paddle/phi/kernels/fusion/onednn/fc_kernel.cc @@ -569,28 +569,37 @@ void FCKernel(const Context& dev_ctx, const std::string& activation_type, const bool padding_weights, DenseTensor* out) { - std::cout << "step1__________________________________" << std::endl; const bool use_mkldnn = - PADDLE_GET_CONST(bool, dev_ctx.GetDnnAttr("use_mkldnn")); - std::cout << "step2__________________________________" << std::endl; + dev_ctx.HasDnnAttr("use_mkldnn") + ? PADDLE_GET_CONST(bool, dev_ctx.GetDnnAttr("use_mkldnn")) + : false; const bool use_quantizer = - PADDLE_GET_CONST(bool, dev_ctx.GetDnnAttr("use_quantizer")); - std::cout << "step3__________________________________" << std::endl; + dev_ctx.HasDnnAttr("use_quantizer") + ? PADDLE_GET_CONST(bool, dev_ctx.GetDnnAttr("use_quantizer")) + : false; const std::string mkldnn_data_type = - PADDLE_GET_CONST(std::string, dev_ctx.GetDnnAttr("mkldnn_data_type")); - std::cout << "step4__________________________________" << std::endl; + dev_ctx.HasDnnAttr("mkldnn_data_type") + ? PADDLE_GET_CONST(std::string, + dev_ctx.GetDnnAttr("mkldnn_data_type")) + : "float32"; const float scale_in = - PADDLE_GET_CONST(float, dev_ctx.GetDnnAttr("scale_in")); - std::cout << "step5__________________________________" << std::endl; + dev_ctx.HasDnnAttr("scale_in") + ? PADDLE_GET_CONST(float, dev_ctx.GetDnnAttr("scale_in")) + : 1.0f; + std::vector tmp_scale_weights = {1.0f}; const std::vector scale_weights = - PADDLE_GET_CONST(std::vector, dev_ctx.GetDnnAttr("scale_weights")); - std::cout << "step6__________________________________" << std::endl; + dev_ctx.HasDnnAttr("scale_weights") + ? PADDLE_GET_CONST(std::vector, + dev_ctx.GetDnnAttr("scale_weights")) + : tmp_scale_weights; const float scale_out = - PADDLE_GET_CONST(float, dev_ctx.GetDnnAttr("scale_out")); - std::cout << "step7__________________________________" << std::endl; + dev_ctx.HasDnnAttr("scale_out") + ? PADDLE_GET_CONST(float, dev_ctx.GetDnnAttr("scale_out")) + : 1.0f; const bool force_fp32_output = - PADDLE_GET_CONST(bool, dev_ctx.GetDnnAttr("force_fp32_output")); - std::cout << "step8__________________________________" << std::endl; + dev_ctx.HasDnnAttr("force_fp32_output") + ? PADDLE_GET_CONST(bool, dev_ctx.GetDnnAttr("force_fp32_output")) + : false; std::string mkldnn_data_type_list[] = {"float32", "int8", "bfloat16"}; PADDLE_ENFORCE_EQ( std::find(std::begin(mkldnn_data_type_list), @@ -612,7 +621,6 @@ void FCKernel(const Context& dev_ctx, in_dims.size(), in_dims)); } - std::cout << "step9__________________________________" << std::endl; bool fuse_relu = activation_type == "relu"; IF_CHANGE_FC_TW_TYPENAME((std::is_same::value), ([&] { if (force_fp32_output) { // NOLINT @@ -683,7 +691,6 @@ void FCKernel(const Context& dev_ctx, out); } })); - std::cout << "step10__________________________________" << std::endl; } } // namespace fusion From 94919e72417179b54adef1c87cdc5c552cf26fc4 Mon Sep 17 00:00:00 2001 From: zeroRains Date: Tue, 26 Dec 2023 07:28:00 +0000 Subject: [PATCH 3/6] fix the parameters bug --- paddle/fluid/operators/ops_extra_info.h | 1 - paddle/phi/api/yaml/op_compat.yaml | 6 +----- paddle/phi/kernels/fusion/onednn/fc_kernel.cc | 12 ++++++------ 3 files changed, 7 insertions(+), 12 deletions(-) diff --git a/paddle/fluid/operators/ops_extra_info.h b/paddle/fluid/operators/ops_extra_info.h index e58c3f5a80aa6..624154177d9b0 100644 --- a/paddle/fluid/operators/ops_extra_info.h +++ b/paddle/fluid/operators/ops_extra_info.h @@ -100,7 +100,6 @@ const std::unordered_map {"mkldnn_data_type", ExtraAttrProperty::ONEDNN}, {"scale_x", ExtraAttrProperty::ONEDNN}, {"scale_y", ExtraAttrProperty::ONEDNN}, - {"scale_out", ExtraAttrProperty::ONEDNN}, {"Scale_in", ExtraAttrProperty::ONEDNN}, {"Scale_in_eltwise", ExtraAttrProperty::ONEDNN}, {"Scale_x", ExtraAttrProperty::ONEDNN}, diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index f4335f604c39e..cfe9c5360f5f3 100755 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -1043,12 +1043,8 @@ bias : Bias outputs : out : Out - attrs : - scale_in : Scale_in - scale_weights : Scale_weights - scale_out : Scale_out extra : - attrs : [bool use_mkldnn = false, bool use_quantizer = false, str mkldnn_data_type = "float32", float scale_in = 1.0f, "float[] scale_weights = {1.0f}", float scale_out = 1.0f, bool force_fp32_output = false] + attrs : [bool use_mkldnn = false, bool use_quantizer = false, str mkldnn_data_type = "float32", float Scale_in = 1.0f, "float[] Scale_weights = {1.0f}", float Scale_out = 1.0f, bool force_fp32_output = false] - op : feed outputs: {out: Out} diff --git a/paddle/phi/kernels/fusion/onednn/fc_kernel.cc b/paddle/phi/kernels/fusion/onednn/fc_kernel.cc index 303f5d36c16eb..0d39677276ead 100644 --- a/paddle/phi/kernels/fusion/onednn/fc_kernel.cc +++ b/paddle/phi/kernels/fusion/onednn/fc_kernel.cc @@ -583,18 +583,18 @@ void FCKernel(const Context& dev_ctx, dev_ctx.GetDnnAttr("mkldnn_data_type")) : "float32"; const float scale_in = - dev_ctx.HasDnnAttr("scale_in") - ? PADDLE_GET_CONST(float, dev_ctx.GetDnnAttr("scale_in")) + dev_ctx.HasDnnAttr("Scale_in") + ? PADDLE_GET_CONST(float, dev_ctx.GetDnnAttr("Scale_in")) : 1.0f; std::vector tmp_scale_weights = {1.0f}; const std::vector scale_weights = - dev_ctx.HasDnnAttr("scale_weights") + dev_ctx.HasDnnAttr("Scale_weights") ? PADDLE_GET_CONST(std::vector, - dev_ctx.GetDnnAttr("scale_weights")) + dev_ctx.GetDnnAttr("Scale_weights")) : tmp_scale_weights; const float scale_out = - dev_ctx.HasDnnAttr("scale_out") - ? PADDLE_GET_CONST(float, dev_ctx.GetDnnAttr("scale_out")) + dev_ctx.HasDnnAttr("Scale_out") + ? PADDLE_GET_CONST(float, dev_ctx.GetDnnAttr("Scale_out")) : 1.0f; const bool force_fp32_output = dev_ctx.HasDnnAttr("force_fp32_output") From d5d828c23264d594cedd4c45c222ee7235d28997 Mon Sep 17 00:00:00 2001 From: zeroRains Date: Tue, 26 Dec 2023 07:42:41 +0000 Subject: [PATCH 4/6] remove the paramars in pir --- .../fc_elementwise_layernorm_fuse_pass.cc | 14 --- .../pir/transforms/fusion/fc_fuse_pass.cc | 49 ++------ .../fusion/fc_with_special_op_fuse_pass.cc | 105 +++++------------- 3 files changed, 36 insertions(+), 132 deletions(-) diff --git a/paddle/fluid/pir/transforms/fusion/fc_elementwise_layernorm_fuse_pass.cc b/paddle/fluid/pir/transforms/fusion/fc_elementwise_layernorm_fuse_pass.cc index c3bef294a8db9..fdb4621fb350b 100644 --- a/paddle/fluid/pir/transforms/fusion/fc_elementwise_layernorm_fuse_pass.cc +++ b/paddle/fluid/pir/transforms/fusion/fc_elementwise_layernorm_fuse_pass.cc @@ -31,14 +31,7 @@ class FcElementwiseLayerNormFusePattern { {"in_num_col_dims", pat.Attr("in_num_col_dims")}, {"activation_type", pat.Attr("activation_type")}, - {"use_mkldnn", pat.Attr("use_mkldnn")}, {"padding_weights", pat.Attr("padding_weights")}, - {"use_quantizer", pat.Attr("use_quantizer")}, - {"mkldnn_data_type", pat.Attr("mkldnn_data_type")}, - {"scale_in", pat.Attr("scale_in")}, - {"scale_weights", pat.Attr("scale_weights")}, - {"scale_out", pat.Attr("scale_out")}, - {"force_fp32_output", pat.Attr("force_fp32_output")}, }); const auto &add = pat.Op(paddle::dialect::AddOp::name()); const auto &layernorm = @@ -104,14 +97,7 @@ class FcElementwiseLayerNormFuse2Pattern { {"in_num_col_dims", pat.Attr("in_num_col_dims")}, {"activation_type", pat.Attr("activation_type")}, - {"use_mkldnn", pat.Attr("use_mkldnn")}, {"padding_weights", pat.Attr("padding_weights")}, - {"use_quantizer", pat.Attr("use_quantizer")}, - {"mkldnn_data_type", pat.Attr("mkldnn_data_type")}, - {"scale_in", pat.Attr("scale_in")}, - {"scale_weights", pat.Attr("scale_weights")}, - {"scale_out", pat.Attr("scale_out")}, - {"force_fp32_output", pat.Attr("force_fp32_output")}, }); const auto &add = pat.Op(paddle::dialect::AddOp::name()); const auto &layernorm = diff --git a/paddle/fluid/pir/transforms/fusion/fc_fuse_pass.cc b/paddle/fluid/pir/transforms/fusion/fc_fuse_pass.cc index 269ffd8633da8..2a320b75d6cc3 100644 --- a/paddle/fluid/pir/transforms/fusion/fc_fuse_pass.cc +++ b/paddle/fluid/pir/transforms/fusion/fc_fuse_pass.cc @@ -65,32 +65,15 @@ class MatmulAddPattern : public pir::drr::DrrPatternBase { const auto &false_attr = res.Attr( [](const pir::drr::MatchContext &match_ctx) -> bool { return false; }); - const auto &fc = res.Op( - paddle::dialect::FcOp::name(), - {{ - {"in_num_col_dims", in_num_col_dims_attr}, - {"activation_type", - res.Attr([](const pir::drr::MatchContext &match_ctx) - -> std::string { return ""; })}, - {"use_mkldnn", false_attr}, - {"padding_weights", false_attr}, - {"use_quantizer", false_attr}, - {"mkldnn_data_type", - res.Attr([](const pir::drr::MatchContext &match_ctx) - -> std::string { return "float32"; })}, - {"scale_in", - res.Attr([](const pir::drr::MatchContext &match_ctx) -> float { - return 1.0f; - })}, - {"scale_weights", - res.Attr([](const pir::drr::MatchContext &match_ctx) - -> std::vector { return {1.0f}; })}, - {"scale_out", - res.Attr([](const pir::drr::MatchContext &match_ctx) -> float { - return 1.0f; - })}, - {"force_fp32_output", false_attr}, - }}); + const auto &fc = + res.Op(paddle::dialect::FcOp::name(), + {{ + {"in_num_col_dims", in_num_col_dims_attr}, + {"activation_type", + res.Attr([](const pir::drr::MatchContext &match_ctx) + -> std::string { return ""; })}, + {"padding_weights", false_attr}, + }}); fc({&res.Tensor("x"), &res.Tensor("w"), &res.Tensor("y")}, {&res.Tensor("add_out")}); } @@ -105,14 +88,7 @@ class FcWithReluPattern : public pir::drr::DrrPatternBase { {{ {"in_num_col_dims", pat.Attr("in_num_col_dims")}, {"activation_type", pat.Attr("activation_type")}, - {"use_mkldnn", pat.Attr("use_mkldnn")}, {"padding_weights", pat.Attr("padding_weights")}, - {"use_quantizer", pat.Attr("use_quantizer")}, - {"mkldnn_data_type", pat.Attr("mkldnn_data_type")}, - {"scale_in", pat.Attr("scale_in")}, - {"scale_weights", pat.Attr("scale_weights")}, - {"scale_out", pat.Attr("scale_out")}, - {"force_fp32_output", pat.Attr("force_fp32_output")}, }}); fc({&pat.Tensor("x"), &pat.Tensor("w"), &pat.Tensor("y")}, {&pat.Tensor("fc_out")}); @@ -133,14 +109,7 @@ class FcWithReluPattern : public pir::drr::DrrPatternBase { {"activation_type", res.Attr([](const pir::drr::MatchContext &match_ctx) -> std::string { return "relu"; })}, - {"use_mkldnn", pat.Attr("use_mkldnn")}, {"padding_weights", pat.Attr("padding_weights")}, - {"use_quantizer", pat.Attr("use_quantizer")}, - {"mkldnn_data_type", pat.Attr("mkldnn_data_type")}, - {"scale_in", pat.Attr("scale_in")}, - {"scale_weights", pat.Attr("scale_weights")}, - {"scale_out", pat.Attr("scale_out")}, - {"force_fp32_output", pat.Attr("force_fp32_output")}, }}); fc_with_relu({&res.Tensor("x"), &res.Tensor("w"), &res.Tensor("y")}, {&res.Tensor("relu_out")}); diff --git a/paddle/fluid/pir/transforms/fusion/fc_with_special_op_fuse_pass.cc b/paddle/fluid/pir/transforms/fusion/fc_with_special_op_fuse_pass.cc index 59994c5e5d924..6bb2b3a6d512d 100644 --- a/paddle/fluid/pir/transforms/fusion/fc_with_special_op_fuse_pass.cc +++ b/paddle/fluid/pir/transforms/fusion/fc_with_special_op_fuse_pass.cc @@ -94,32 +94,15 @@ class SqueezeFcFusePattern const auto &false_attr = res.Attr( [](const pir::drr::MatchContext &match_ctx) -> bool { return false; }); - const auto &fc = res.Op( - paddle::dialect::FcOp::name(), - {{ - {"in_num_col_dims", in_num_col_dims_attr}, - {"activation_type", - res.Attr([](const pir::drr::MatchContext &match_ctx) - -> std::string { return ""; })}, - {"use_mkldnn", false_attr}, - {"padding_weights", false_attr}, - {"use_quantizer", false_attr}, - {"mkldnn_data_type", - res.Attr([](const pir::drr::MatchContext &match_ctx) - -> std::string { return "float32"; })}, - {"scale_in", - res.Attr([](const pir::drr::MatchContext &match_ctx) -> float { - return 1.0f; - })}, - {"scale_weights", - res.Attr([](const pir::drr::MatchContext &match_ctx) - -> std::vector { return {1.0f}; })}, - {"scale_out", - res.Attr([](const pir::drr::MatchContext &match_ctx) -> float { - return 1.0f; - })}, - {"force_fp32_output", false_attr}, - }}); + const auto &fc = + res.Op(paddle::dialect::FcOp::name(), + {{ + {"in_num_col_dims", in_num_col_dims_attr}, + {"activation_type", + res.Attr([](const pir::drr::MatchContext &match_ctx) + -> std::string { return ""; })}, + {"padding_weights", false_attr}, + }}); fc({&res.Tensor("x"), &res.Tensor("w"), &res.Tensor("bias")}, {&res.Tensor("add_out")}); } @@ -248,32 +231,15 @@ class ReshapeFcFusePattern const auto &false_attr = res.Attr( [](const pir::drr::MatchContext &match_ctx) -> bool { return false; }); - const auto &fc = res.Op( - paddle::dialect::FcOp::name(), - {{ - {"in_num_col_dims", in_num_col_dims_attr}, - {"activation_type", - res.Attr([](const pir::drr::MatchContext &match_ctx) - -> std::string { return ""; })}, - {"use_mkldnn", false_attr}, - {"padding_weights", false_attr}, - {"use_quantizer", false_attr}, - {"mkldnn_data_type", - res.Attr([](const pir::drr::MatchContext &match_ctx) - -> std::string { return "float32"; })}, - {"scale_in", - res.Attr([](const pir::drr::MatchContext &match_ctx) -> float { - return 1.0f; - })}, - {"scale_weights", - res.Attr([](const pir::drr::MatchContext &match_ctx) - -> std::vector { return {1.0f}; })}, - {"scale_out", - res.Attr([](const pir::drr::MatchContext &match_ctx) -> float { - return 1.0f; - })}, - {"force_fp32_output", false_attr}, - }}); + const auto &fc = + res.Op(paddle::dialect::FcOp::name(), + {{ + {"in_num_col_dims", in_num_col_dims_attr}, + {"activation_type", + res.Attr([](const pir::drr::MatchContext &match_ctx) + -> std::string { return ""; })}, + {"padding_weights", false_attr}, + }}); fc({&res.Tensor("x"), &res.Tensor("w"), &res.Tensor("bias")}, {&res.Tensor("add_out")}); } @@ -336,32 +302,15 @@ class FlattenFcFusePattern const auto &false_attr = res.Attr( [](const pir::drr::MatchContext &match_ctx) -> bool { return false; }); - const auto &fc = res.Op( - paddle::dialect::FcOp::name(), - {{ - {"in_num_col_dims", in_num_col_dims_attr}, - {"activation_type", - res.Attr([](const pir::drr::MatchContext &match_ctx) - -> std::string { return ""; })}, - {"use_mkldnn", false_attr}, - {"padding_weights", false_attr}, - {"use_quantizer", false_attr}, - {"mkldnn_data_type", - res.Attr([](const pir::drr::MatchContext &match_ctx) - -> std::string { return "float32"; })}, - {"scale_in", - res.Attr([](const pir::drr::MatchContext &match_ctx) -> float { - return 1.0f; - })}, - {"scale_weights", - res.Attr([](const pir::drr::MatchContext &match_ctx) - -> std::vector { return {1.0f}; })}, - {"scale_out", - res.Attr([](const pir::drr::MatchContext &match_ctx) -> float { - return 1.0f; - })}, - {"force_fp32_output", false_attr}, - }}); + const auto &fc = + res.Op(paddle::dialect::FcOp::name(), + {{ + {"in_num_col_dims", in_num_col_dims_attr}, + {"activation_type", + res.Attr([](const pir::drr::MatchContext &match_ctx) + -> std::string { return ""; })}, + {"padding_weights", false_attr}, + }}); fc({&res.Tensor("x"), &res.Tensor("w"), &res.Tensor("bias")}, {&res.Tensor("add_out")}); } From 7e59f387cafc3e885544f58d3df3a830ba4328a7 Mon Sep 17 00:00:00 2001 From: zeroRains Date: Tue, 26 Dec 2023 08:03:01 +0000 Subject: [PATCH 5/6] roback and add attr --- paddle/fluid/framework/operator.h | 2 +- paddle/fluid/operators/compat/fc.pbtxt | 2 +- paddle/phi/api/yaml/op_compat.yaml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/paddle/fluid/framework/operator.h b/paddle/fluid/framework/operator.h index 7f47ef640c19c..31c6bb4ef8806 100644 --- a/paddle/fluid/framework/operator.h +++ b/paddle/fluid/framework/operator.h @@ -103,7 +103,7 @@ constexpr char kEnableCacheRuntimeContext[] = "@ENABLE_CACHE_RUNTIME_CONTEXT@"; /// TODO(luotao): Note that this temporal attribute would be deleted after all /// ops contain it. constexpr char kAllKernelsMustComputeRuntimeShape[] = - "@ALL_KERNELS_MUST_COMPUTE_RUNTIME_SHAPE@"; + "ALL_KERNELS_MUST_COMPUTE_RUNTIME_SHAPE"; // define some kernel priority /* Define multiple kernel type fallback order*/ diff --git a/paddle/fluid/operators/compat/fc.pbtxt b/paddle/fluid/operators/compat/fc.pbtxt index b7b9fe7acda73..babd80260d771 100644 --- a/paddle/fluid/operators/compat/fc.pbtxt +++ b/paddle/fluid/operators/compat/fc.pbtxt @@ -27,7 +27,7 @@ extra { type: BOOLEAN } attrs { - name: "@ALL_KERNELS_MUST_COMPUTE_RUNTIME_SHAPE@" + name: "ALL_KERNELS_MUST_COMPUTE_RUNTIME_SHAPE" type: BOOLEAN } attrs { diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index cfe9c5360f5f3..3ef6e4c7d5921 100755 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -1044,7 +1044,7 @@ outputs : out : Out extra : - attrs : [bool use_mkldnn = false, bool use_quantizer = false, str mkldnn_data_type = "float32", float Scale_in = 1.0f, "float[] Scale_weights = {1.0f}", float Scale_out = 1.0f, bool force_fp32_output = false] + attrs : [bool ALL_KERNELS_MUST_COMPUTE_RUNTIME_SHAPE = true, bool use_mkldnn = false, bool use_quantizer = false, str mkldnn_data_type = "float32", float Scale_in = 1.0f, "float[] Scale_weights = {1.0f}", float Scale_out = 1.0f, bool force_fp32_output = false] - op : feed outputs: {out: Out} From f2a52dbe4761170cf259c698d1fcf9e63c06422d Mon Sep 17 00:00:00 2001 From: zeroRains Date: Tue, 26 Dec 2023 08:05:13 +0000 Subject: [PATCH 6/6] add the scale_out --- paddle/fluid/operators/ops_extra_info.h | 1 + 1 file changed, 1 insertion(+) diff --git a/paddle/fluid/operators/ops_extra_info.h b/paddle/fluid/operators/ops_extra_info.h index 624154177d9b0..e58c3f5a80aa6 100644 --- a/paddle/fluid/operators/ops_extra_info.h +++ b/paddle/fluid/operators/ops_extra_info.h @@ -100,6 +100,7 @@ const std::unordered_map {"mkldnn_data_type", ExtraAttrProperty::ONEDNN}, {"scale_x", ExtraAttrProperty::ONEDNN}, {"scale_y", ExtraAttrProperty::ONEDNN}, + {"scale_out", ExtraAttrProperty::ONEDNN}, {"Scale_in", ExtraAttrProperty::ONEDNN}, {"Scale_in_eltwise", ExtraAttrProperty::ONEDNN}, {"Scale_x", ExtraAttrProperty::ONEDNN},