diff --git a/paddle/fluid/framework/ir/fuse_gemm_epilogue_pass.cc b/paddle/fluid/framework/ir/fuse_gemm_epilogue_pass.cc index e708a3bbdf80f3a59cd7d16a0a153af9be308d54..b211ec779922c6782031c8886838f7c74771eeca 100644 --- a/paddle/fluid/framework/ir/fuse_gemm_epilogue_pass.cc +++ b/paddle/fluid/framework/ir/fuse_gemm_epilogue_pass.cc @@ -81,8 +81,7 @@ ir::Graph *FuseGemmEpiloguePass::FuseLinearFwd(ir::Graph *graph, // currently. The conditions below are used to verify wether matmul_v2 // is created by paddle.nn.Linear auto matmul_op_desc = matmul_op->Op(); - if (!IsGemmFromLinear_(matmul_x_shape, matmul_w_shape, matmul_op_desc)) - return; + if (!IsGemmFromLinear_(matmul_x_shape, matmul_w_shape)) return; bool trans_x, trans_y; GetTransposeAttrsFromOp(*matmul_op_desc, &trans_x, &trans_y); @@ -165,8 +164,7 @@ ir::Graph *FuseGemmEpiloguePass::FuseLinearActFwd( // currently. The conditions below are used to verify wether matmul_v2 // is created by paddle.nn.Linear auto matmul_op_desc = matmul_op->Op(); - if (!IsGemmFromLinear_(matmul_x_shape, matmul_w_shape, matmul_op_desc)) - return; + if (!IsGemmFromLinear_(matmul_x_shape, matmul_w_shape)) return; auto activation = act_op->Op()->Type(); @@ -291,9 +289,7 @@ ir::Graph *FuseGemmEpiloguePass::FuseLinearBwd(ir::Graph *graph, // currently. The conditions below are used to verify wether matmul_v2 // is created by paddle.nn.Linear auto matmul_grad_op_desc = matmul_grad_op->Op(); - if (!IsGemmFromLinear_( - matmul_grad_x_shape, matmul_grad_w_shape, matmul_grad_op_desc)) - return; + if (!IsGemmFromLinear_(matmul_grad_x_shape, matmul_grad_w_shape)) return; bool trans_x, trans_y; GetTransposeAttrsFromOp(*matmul_grad_op_desc, &trans_x, &trans_y); @@ -430,9 +426,7 @@ ir::Graph *FuseGemmEpiloguePass::FuseLinearActBwd( // currently. The conditions below are used to verify wether matmul_v2 // is created by paddle.nn.Linear auto matmul_grad_op_desc = matmul_grad_op->Op(); - if (!IsGemmFromLinear_( - matmul_grad_x_shape, matmul_grad_w_shape, matmul_grad_op_desc)) - return; + if (!IsGemmFromLinear_(matmul_grad_x_shape, matmul_grad_w_shape)) return; auto activation_grad = act_grad_op->Op()->Type(); @@ -509,22 +503,8 @@ ir::Graph *FuseGemmEpiloguePass::FuseLinearActBwd( bool FuseGemmEpiloguePass::IsGemmFromLinear_( const std::vector &x_shape, - const std::vector &w_shape, - OpDesc *matmul_v2_op) const { - if (w_shape.size() != 2 || x_shape.size() < 2) return false; - for (auto attr_name : {"fused_reshape_Out", - "fused_reshape_X", - "fused_reshape_Y", - "fused_transpose_Out", - "fused_transpose_X", - "fused_transpose_Y"}) { - if (matmul_v2_op->HasAttr(attr_name)) { - std::vector tmp_vec = - PADDLE_GET_CONST(std::vector, matmul_v2_op->GetAttr(attr_name)); - if (tmp_vec.size() > 0) return false; - } - } - return true; + const std::vector &w_shape) const { + return (w_shape.size() == 2 && x_shape.size() >= 2); } } // namespace ir diff --git a/paddle/fluid/framework/ir/fuse_gemm_epilogue_pass.h b/paddle/fluid/framework/ir/fuse_gemm_epilogue_pass.h index 31b2634af8213ea769bebae1dacdac0b2faee7cf..6ed57621061d0c3e759aeeffb0e017259da7dcdf 100644 --- a/paddle/fluid/framework/ir/fuse_gemm_epilogue_pass.h +++ b/paddle/fluid/framework/ir/fuse_gemm_epilogue_pass.h @@ -90,8 +90,7 @@ class FuseGemmEpiloguePass : public FusePassBase { private: bool IsGemmFromLinear_(const std::vector &x_shape, - const std::vector &w_shape, - OpDesc *matmul_v2_op) const; + const std::vector &w_shape) const; const std::string GetReserveSpaceCacheKey(const std::string var_name, int block_id) const { return std::to_string(block_id) + var_name; diff --git a/paddle/fluid/operators/compat/matmul.pbtxt b/paddle/fluid/operators/compat/matmul.pbtxt index 8f29d93660608928a21dbb96e16b7a579fa3aa63..b1d786baab41d97a26403f2b2fd300adaa0c6b3c 100644 --- a/paddle/fluid/operators/compat/matmul.pbtxt +++ b/paddle/fluid/operators/compat/matmul.pbtxt @@ -75,28 +75,4 @@ extra { name: "force_fp32_output" type: BOOLEAN } - attrs { - name: "fused_reshape_Out" - type: INTS - } - attrs { - name: "fused_reshape_X" - type: INTS - } - attrs { - name: "fused_reshape_Y" - type: INTS - } - attrs { - name: "fused_transpose_Out" - type: INTS - } - attrs { - name: "fused_transpose_X" - type: INTS - } - attrs { - name: "fused_transpose_Y" - type: INTS - } } diff --git a/paddle/fluid/operators/feed_forward_test.cu b/paddle/fluid/operators/feed_forward_test.cu index 6c4bc4d562cd788c93975d605a4f3495340baaf1..08d91d1bb2af3eddae372c8d353534191247de88 100644 --- a/paddle/fluid/operators/feed_forward_test.cu +++ b/paddle/fluid/operators/feed_forward_test.cu @@ -1,4 +1,4 @@ -/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. +/* Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -167,12 +167,6 @@ void GetLinearOpGrad(const std::vector &x_vec, dout_ptr, dout_vec.data(), size_z * sizeof(T), cudaMemcpyHostToDevice); bool use_mkldnn = false; - std::vector fused_reshape_X = {}; - std::vector fused_reshape_Y = {}; - std::vector fused_reshape_Out = {}; - std::vector fused_transpose_X = {}; - std::vector fused_transpose_Y = {}; - std::vector fused_transpose_Out = {}; bool use_quantizer = false, force_fp32_output = false; std::string mkldnn_data_type = "float32"; float Scale_x = 1.0, Scale_y = 1.0, Scale_out = 1.0; @@ -182,12 +176,6 @@ void GetLinearOpGrad(const std::vector &x_vec, attrs.insert({"transpose_Y", transpose_b}); attrs.insert({"alpha", alpha}); attrs.insert({"use_mkldnn", use_mkldnn}); - attrs.insert({"fused_reshape_X", fused_reshape_X}); - attrs.insert({"fused_reshape_Y", fused_reshape_Y}); - attrs.insert({"fused_reshape_Out", fused_reshape_Out}); - attrs.insert({"fused_transpose_X", fused_transpose_X}); - attrs.insert({"fused_transpose_Y", fused_transpose_Y}); - attrs.insert({"fused_transpose_Out", fused_transpose_Out}); attrs.insert({"use_quantizer", use_quantizer}); attrs.insert({"mkldnn_data_type", mkldnn_data_type}); attrs.insert({"Scale_x", Scale_x}); diff --git a/paddle/fluid/operators/matmul_op.cc b/paddle/fluid/operators/matmul_op.cc index 3036cdc5615ade227cc0d59e26b6d5d1be2defa9..73c2577caa81c72528442386b0e4b7cc2da8135c 100644 --- a/paddle/fluid/operators/matmul_op.cc +++ b/paddle/fluid/operators/matmul_op.cc @@ -1,4 +1,4 @@ -/* Copyright (c) 2017 PaddlePaddle Authors. All Rights Reserved. +/* Copyright (c) 2023 PaddlePaddle Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at @@ -16,9 +16,6 @@ limitations under the License. */ #include "paddle/fluid/framework/op_registry.h" #include "paddle/fluid/framework/op_version_registry.h" #include "paddle/phi/kernels/funcs/blas/blas.h" -#ifdef PADDLE_WITH_MKLDNN -#include "paddle/fluid/platform/mkldnn_helper.h" -#endif namespace paddle { namespace operators { @@ -303,7 +300,7 @@ class MatMulGradKernel : public framework::OpKernel { bool transpose_y = context.Attr("transpose_Y"); ReshapeXYOutIntoMatrixSequence(&x, &y, &dout, transpose_x, transpose_y); - framework::DDim dx_dims; + phi::DDim dx_dims; if (dx) { dx_dims = dx->dims(); if (dx_dims != x.dims()) { @@ -311,7 +308,7 @@ class MatMulGradKernel : public framework::OpKernel { } } - framework::DDim dy_dims; + phi::DDim dy_dims; if (dy) { dy_dims = dy->dims(); if (dy_dims != y.dims()) { @@ -346,23 +343,15 @@ class MatMulGradKernel : public framework::OpKernel { } }; -framework::DDim GetDimForInput(const framework::InferShapeContext &ctx, - std::string input_name) { - auto shape = ctx.Attrs().Get>("fused_reshape_" + input_name); - auto axis = - ctx.Attrs().Get>("fused_transpose_" + input_name); +phi::DDim GetDimForInput(const framework::InferShapeContext &ctx, + std::string input_name) { auto dim = ctx.GetInputDim(input_name); - PADDLE_ENFORCE_GT(dim.size(), 0, - platform::errors::InvalidArgument( + phi::errors::InvalidArgument( "The Input(%s) has not been initialized properly. The " "shape of Input(%s) = [%s].", dim)); - - if (!shape.empty() && !axis.empty()) { - dim = dim.reshape(shape).transpose(axis); - } return dim; } @@ -449,7 +438,7 @@ class MatMulDoubleGradKernel : public framework::OpKernel { ReshapeXYOutIntoMatrixSequence(&x, &y, &dout, transpose_x, transpose_y); - framework::DDim dx_dims; + phi::DDim dx_dims; if (dx) { dx_dims = dx->dims(); if (dx_dims != x.dims()) { @@ -457,7 +446,7 @@ class MatMulDoubleGradKernel : public framework::OpKernel { } } - framework::DDim dy_dims; + phi::DDim dy_dims; if (dy) { dy_dims = dy->dims(); if (dy_dims != y.dims()) { @@ -465,7 +454,7 @@ class MatMulDoubleGradKernel : public framework::OpKernel { } } - framework::DDim ddout_dims; + phi::DDim ddout_dims; if (ddout) { ddout_dims = ddout->dims(); if (ddout_dims != dout.dims()) { @@ -617,7 +606,7 @@ class MatMulOp : public framework::OperatorWithKernel { mat_dim_x.batch_size_ == mat_dim_y.batch_size_ || mat_dim_x.batch_size_ == 0 || mat_dim_y.batch_size_ == 0, true, - platform::errors::InvalidArgument( + phi::errors::InvalidArgument( "The batch size of the two matrices should be equal, or " "at least one is zero.\n" "But received X's shape: %s, Y's shape: %s.", @@ -633,7 +622,7 @@ class MatMulOp : public framework::OperatorWithKernel { PADDLE_ENFORCE_LE( head_number, mat_dim_x.width_, - platform::errors::InvalidArgument( + phi::errors::InvalidArgument( "Unsatisfied mkl acceleration library requirements: " "The number of heads " "(%d) must be equal to X's width. But received X's shape: %s.", @@ -647,7 +636,7 @@ class MatMulOp : public framework::OperatorWithKernel { #else PADDLE_ENFORCE_EQ(mat_dim_x.width_, mat_dim_y.height_, - platform::errors::InvalidArgument( + phi::errors::InvalidArgument( "Input X's width should be equal to the Y's height, " "but received X's shape: [%s], " "Y's shape: [%s].", @@ -681,16 +670,8 @@ class MatMulOp : public framework::OperatorWithKernel { dim_out = {1}; } - framework::DDim ddim_out = phi::make_ddim(dim_out); - -#ifdef PADDLE_WITH_MKLDNN - auto shape = context->Attrs().Get>("fused_reshape_Out"); - auto axis = context->Attrs().Get>("fused_transpose_Out"); + phi::DDim ddim_out = phi::make_ddim(dim_out); - if (!shape.empty() && !axis.empty()) { - ddim_out = ddim_out.transpose(axis).reshape(shape); - } -#endif context->SetOutputDim("Out", ddim_out); context->ShareLoD("X", "Out"); } @@ -749,34 +730,6 @@ class MatMulOpMaker : public framework::OpProtoAndCheckerMaker { "(bool, default false) Indicates if MKL-DNN kernel will be used") .SetDefault(false) .AsExtra(); - AddAttr>("fused_reshape_X", - R"DOC(Shape of fused reshape of `X` input.)DOC") - .SetDefault({}) - .AsExtra(); - AddAttr>("fused_reshape_Y", - R"DOC(Shape of fused reshape of `Y` input.)DOC") - .SetDefault({}) - .AsExtra(); - AddAttr>("fused_transpose_X", - R"DOC(Axis of fused transpose of `X` input.)DOC") - .SetDefault({}) - .AsExtra(); - AddAttr>("fused_transpose_Y", - R"DOC(Axis of fused transpose of `Y` input.)DOC") - .SetDefault({}) - .AsExtra(); - AddAttr>( - "fused_reshape_Out", - R"DOC(When MKLDNN MatMul_transpose_reshape fuse activated, " - "it's a shape attribute of fused reshape for `Out` output.)DOC") - .SetDefault({}) - .AsExtra(); - AddAttr>( - "fused_transpose_Out", - R"DOC(When MKLDNN MatMul_transpose_reshape fuse activated, " - "it's a axis attribute of fused transpose for `Out` output.)DOC") - .SetDefault({}) - .AsExtra(); AddAttr( "use_quantizer", "(bool, default false) " diff --git a/paddle/fluid/operators/mkldnn/matmul_mkldnn_op.cc b/paddle/fluid/operators/mkldnn/matmul_mkldnn_op.cc index 3d27e90ccb5e37025872cbb1576fd1272de1d93d..04338fc866954ec1b7355d643cc835a4f47d439c 100644 --- a/paddle/fluid/operators/mkldnn/matmul_mkldnn_op.cc +++ b/paddle/fluid/operators/mkldnn/matmul_mkldnn_op.cc @@ -71,16 +71,6 @@ phi::DenseTensor FoldFirstAndLastDims(const OneDNNContext &dev_ctx, return output; } -phi::DDim GetDimForInput(const ExecutionContext &ctx, std::string input_name) { - auto shape = ctx.Attr>("fused_reshape_" + input_name); - auto axis = ctx.Attr>("fused_transpose_" + input_name); - auto input_dims = ctx.Input(input_name)->dims(); - if (!shape.empty() && !axis.empty()) { - return input_dims.reshape(shape).transpose(axis); - } - return input_dims; -} - template class MatMulV1OneDNNHandler : public phi::funcs::OneDNNHandlerNoCachingT { @@ -91,10 +81,7 @@ class MatMulV1OneDNNHandler const std::vector &x_org_dims, bool trans_x, const std::vector &y_org_dims, - bool trans_y, - bool is_output_fused, - const std::vector &x_strides_override, - const std::vector &y_strides_override) + bool trans_y) : phi::funcs::OneDNNHandlerNoCachingT(engine, cpu_place) { // M X K * K X N @@ -121,47 +108,27 @@ class MatMulV1OneDNNHandler y_strides.reserve(x_dims.size()); out_strides.reserve(x_dims.size()); - if (x_strides_override.empty()) { - if (trans_x) { - x_strides.insert(x_strides.end(), {M * K, 1, M}); - } else { - x_strides.insert(x_strides.end(), {M * K, K, 1}); - } + if (trans_x) { + x_strides.insert(x_strides.end(), {M * K, 1, M}); } else { - x_strides = x_strides_override; + x_strides.insert(x_strides.end(), {M * K, K, 1}); } - - if (y_strides_override.empty()) { - if (trans_y) { - y_strides.insert(y_strides.end(), {N * K, 1, K}); - } else { - y_strides.insert(y_strides.end(), {N * K, N, 1}); - } + if (trans_y) { + y_strides.insert(y_strides.end(), {N * K, 1, K}); } else { - y_strides = y_strides_override; + y_strides.insert(y_strides.end(), {N * K, N, 1}); } - out_strides.insert(out_strides.end(), {M * N, N, 1}); out_ddims.insert(out_ddims.end(), {std::max(x_dims[MB_idx], y_dims[MB_idx]), M, N}); for (int i = x_dims.size() - 4; i >= 0; --i) { out_ddims[i] = std::max(x_dims[i], y_dims[i]); - if (x_strides_override.empty()) { - x_strides[i] = x_dims[i + 1] * x_strides[i + 1]; - } - if (y_strides_override.empty()) { - y_strides[i] = y_dims[i + 1] * y_strides[i + 1]; - } + x_strides[i] = x_dims[i + 1] * x_strides[i + 1]; + y_strides[i] = y_dims[i + 1] * y_strides[i + 1]; out_strides[i] = out_ddims[i + 1] * out_strides[i + 1]; } - // TODO(jczaja): Why not for int8?? - if (!phi::funcs::is_int8() && is_output_fused) { - std::vector transpose_axis = {0, 2, 1, 3}; - out_strides = phi::funcs::FakeTransposeStrides(out_ddims, transpose_axis); - } - auto x_md = memory::desc(x_dims, phi::funcs::OneDNNGetDataType(), x_strides); auto y_md = @@ -191,34 +158,10 @@ class MatMulV1OneDNNHandler dnnl::primitive_attr CreateMatmulAttrs(const ExecutionContext &ctx) { dnnl::primitive_attr matmul_attrs; - dnnl::post_ops post_operations; - float scale_out = ComputeOutputScale(ctx); if (scale_out != 1.0f) { matmul_attrs.set_output_scales(0, {scale_out}); } - - if (ctx.HasInput("ResidualData")) { - auto *residual_data = ctx.Input("ResidualData"); - auto residual_data_tz = phi::vectorize(residual_data->dims()); - auto residual_data_md = memory::desc(residual_data_tz, - phi::funcs::OneDNNGetDataType(), - dnnl::memory::format_tag::any); - post_operations.append_binary(dnnl::algorithm::binary_add, - residual_data_md); - if (ctx.HasAttr("Scale_in_eltwise")) { - float sum_scale = scale_out / ctx.Attr("Scale_in_eltwise"); - post_operations.append_sum(sum_scale); - } - } - - if (ctx.HasAttr("fused_output_scale")) { - float scale_alpha = ctx.Attr("fused_output_scale"); - post_operations.append_eltwise( - 1.0, dnnl::algorithm::eltwise_linear, scale_alpha, 0.0f); - } - - matmul_attrs.set_post_ops(post_operations); return matmul_attrs; } @@ -272,10 +215,10 @@ class MatMulOneDNNHandler memory::dims out_dims = {out_bs, M, N}; memory::dims x_strides = - !trans_x ? memory::dims{M * K, K, 1} : memory::dims{M * K, 1, M}; + trans_x ? memory::dims{M * K, 1, M} : memory::dims{M * K, K, 1}; memory::dims y_strides = - !trans_y ? memory::dims{N * K, N, 1} : memory::dims{N * K, 1, K}; + trans_y ? memory::dims{N * K, 1, K} : memory::dims{N * K, N, 1}; memory::dims out_strides = memory::dims{M * N, N, 1}; auto x_md = memory::desc(x_dims, OneDNNGetDataType(), x_strides); @@ -364,12 +307,6 @@ void ReshapeXYOutToMatrixSequence(phi::DenseTensor *x, ReshapeTensorToMatrixSequence(y, mat_dim_y); } -bool IsOutputFused(const ExecutionContext &ctx) { - auto &fused_reshape_Out = ctx.Attr>("fused_reshape_Out"); - auto &fused_transpose_Out = ctx.Attr>("fused_transpose_Out"); - return !fused_reshape_Out.empty() && !fused_transpose_Out.empty(); -} - template void ExecuteMatMulV1(const ExecutionContext &ctx, const dnnl::engine onednn_engine, @@ -380,29 +317,8 @@ void ExecuteMatMulV1(const ExecutionContext &ctx, const std::vector &y_dims, bool trans_y, phi::DenseTensor *out) { - std::vector x_strides_override = phi::funcs::GetInputStrides( - "X", - x->dims(), - trans_x, - ctx.Attr>("fused_reshape_X"), - ctx.Attr>("fused_transpose_X")); - std::vector y_strides_override = phi::funcs::GetInputStrides( - "Y", - y->dims(), - trans_y, - ctx.Attr>("fused_reshape_Y"), - ctx.Attr>("fused_transpose_Y")); - - MatMulV1OneDNNHandler handler(ctx, - onednn_engine, - ctx.GetPlace(), - x_dims, - trans_x, - y_dims, - trans_y, - IsOutputFused(ctx), - x_strides_override, - y_strides_override); + MatMulV1OneDNNHandler handler( + ctx, onednn_engine, ctx.GetPlace(), x_dims, trans_x, y_dims, trans_y); const auto src_memory_p = handler.AcquireSrcMemory(x); const auto weights_memory_p = handler.AcquireWeightsMemory(y); const auto dst_memory_p = handler.AcquireDstMemory(out); @@ -414,27 +330,12 @@ void ExecuteMatMulV1(const ExecutionContext &ctx, {DNNL_ARG_WEIGHTS, *weights_memory_p}, {DNNL_ARG_DST, *dst_memory_p}}; - if (ctx.HasInput("ResidualData")) { - auto *residual_data = ctx.Input("ResidualData"); - const auto residual_data_memory_p = handler.AcquireSrcMemory(residual_data); - matmul_args.insert({DNNL_ARG_ATTR_MULTIPLE_POST_OP(0) | DNNL_ARG_SRC_1, - *residual_data_memory_p}); - } - auto &astream = OneDNNContext::tls().get_stream(); matmul_p->execute(astream, matmul_args); astream.wait(); - // TODO(jczaja): Explain why int8 format of dst is ABCD and do not need - // permute - if (IsOutputFused(ctx) && !phi::funcs::is_int8()) { - auto axis = ctx.Attr>("fused_transpose_Out"); - auto permuted_md = dst_memory_p->get_desc().permute_axes(axis); - out->set_mem_desc(permuted_md.reshape(vectorize(out->dims()))); - } else { - out->set_mem_desc( - dst_memory_p->get_desc().reshape(vectorize(out->dims()))); - } + out->set_mem_desc( + dst_memory_p->get_desc().reshape(vectorize(out->dims()))); } template @@ -462,13 +363,11 @@ class MatMulMKLDNNKernel : public paddle::framework::OpKernel { auto *x = ctx.Input("X"); auto *y = ctx.Input("Y"); auto *out = ctx.Output("Out"); - bool trans_x = ctx.HasAttr("trans_x") ? ctx.Attr("trans_x") - : ctx.Attr("transpose_X"); - bool trans_y = ctx.HasAttr("trans_y") ? ctx.Attr("trans_y") - : ctx.Attr("transpose_Y"); + bool trans_x = ctx.Attr("transpose_X"); + bool trans_y = ctx.Attr("transpose_Y"); - auto x_dims = vectorize(GetDimForInput(ctx, "X")); - auto y_dims = vectorize(GetDimForInput(ctx, "Y")); + auto x_dims = vectorize(x->dims()); + auto y_dims = vectorize(y->dims()); int ndims = std::max(x_dims.size(), y_dims.size()); ndims = std::max(ndims, 3); @@ -539,7 +438,7 @@ class MatMulMKLDNNKernel : public paddle::framework::OpKernel { } } - if (!IsOutputFused(ctx) && x_dims.size() > 2 && y_dims.size() > 2) { + if (x_dims.size() > 2 && y_dims.size() > 2) { auto out_dims = vectorize(out->dims()); for (size_t i = 0; i < (*x_bd_dims).size() - 2; ++i) { PADDLE_ENFORCE_EQ( @@ -587,12 +486,8 @@ class MatMulGradMKLDNNKernel : public paddle::framework::OpKernel { auto *dy = ctx.Output(paddle::framework::GradVarName("Y")); - bool transpose_x = ctx.HasAttr("transpose_X") - ? ctx.Attr("transpose_X") - : ctx.Attr("trans_x"); - bool transpose_y = ctx.HasAttr("transpose_Y") - ? ctx.Attr("transpose_Y") - : ctx.Attr("trans_y"); + bool transpose_x = ctx.Attr("transpose_X"); + bool transpose_y = ctx.Attr("transpose_Y"); ReshapeXYOutToMatrixSequence(&x, &y, &dout, transpose_x, transpose_y); @@ -696,14 +591,14 @@ class MatMulGradMKLDNNKernel : public paddle::framework::OpKernel { out->dims().size() == 2; phi::DenseTensor x_combined, y_combined; - if (!need_combine) { - x_combined = *x; - y_combined = *y; - } else { + if (need_combine) { x_combined = is_fold_init_dims_x ? FoldOuterDims(*x) : FoldFirstAndLastDims(dev_ctx, x); y_combined = is_fold_init_dims_y ? FoldOuterDims(*y) : FoldFirstAndLastDims(dev_ctx, y); + } else { + x_combined = *x; + y_combined = *y; } MatMulOneDNNHandler handler(engine, diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index b6c4a3206fbf81ba415789055c639666e101311f..0c064bdd6fe949bfcd17cdffcd54dd022e13f194 100644 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -1283,9 +1283,7 @@ outputs : out : Out extra : - attrs : [bool use_mkldnn = false, 'int[] fused_reshape_Out = {}', 'int[] fused_transpose_Out = {}', - str mkldnn_data_type = "float32", 'int[] fused_reshape_X = {}', 'int[] fused_reshape_Y = {}', - 'int[] fused_transpose_X = {}', 'int[] fused_transpose_Y = {}'] + attrs : [bool use_mkldnn = false, str mkldnn_data_type = "float32"] - op : matmul_with_flatten (mul) backward : matmul_with_flatten_grad (mul_grad) diff --git a/paddle/phi/backends/onednn/matmul_utils.h b/paddle/phi/backends/onednn/matmul_utils.h index c55a95d78d1741f9e9dde549efa5baa7232c40da..70ed7e29c9637ce29829ae589d72168ef036c8ad 100644 --- a/paddle/phi/backends/onednn/matmul_utils.h +++ b/paddle/phi/backends/onednn/matmul_utils.h @@ -93,23 +93,7 @@ class MatmulOneDNNHandler : public OneDNNHandlerNoCachingT { auto y_md = memory::desc(y_dims, OneDNNGetDataType(), y_strides); auto out_md = memory::desc(out_ddims, OneDNNGetDataType(), out_strides); - const auto matmul_attrs = CreateMatmulAttrs(dev_ctx); - - this->AcquireForwardPrimitiveDescriptor(matmul_attrs, x_md, y_md, out_md); - } - - dnnl::primitive_attr CreateMatmulAttrs(const OneDNNContext& dev_ctx) { - dnnl::primitive_attr matmul_attrs; - dnnl::post_ops post_operations; - - float scale_out = dev_ctx.HasDnnAttr("alpha") - ? PADDLE_GET_CONST(float, dev_ctx.GetDnnAttr("alpha")) - : 1.0f; - if (scale_out != 1.0f) { - matmul_attrs.set_output_scales(0, {scale_out}); - } - matmul_attrs.set_post_ops(post_operations); - return matmul_attrs; + this->AcquireForwardPrimitiveDescriptor(x_md, y_md, out_md); } std::shared_ptr AcquireWeightsMemory(const DenseTensor* input) { @@ -175,27 +159,6 @@ inline void ExecuteMatmul(const OneDNNContext& dev_ctx, bool trans_x, bool trans_y, DenseTensor* out) { - auto shape_x = dev_ctx.HasDnnAttr("fused_reshape_X") - ? PADDLE_GET_CONST(std::vector, - dev_ctx.GetDnnAttr("fused_reshape_X")) - : std::vector(); - auto axis_x = dev_ctx.HasDnnAttr("fused_transpose_X") - ? PADDLE_GET_CONST(std::vector, - dev_ctx.GetDnnAttr("fused_transpose_X")) - : std::vector(); - auto shape_y = dev_ctx.HasDnnAttr("fused_reshape_Y") - ? PADDLE_GET_CONST(std::vector, - dev_ctx.GetDnnAttr("fused_reshape_Y")) - : std::vector(); - auto axis_y = dev_ctx.HasDnnAttr("fused_transpose_Y") - ? PADDLE_GET_CONST(std::vector, - dev_ctx.GetDnnAttr("fused_transpose_Y")) - : std::vector(); - - auto x_strides_override = - GetInputStrides("X", x.dims(), trans_x, shape_x, shape_x); - auto y_strides_override = - GetInputStrides("Y", y.dims(), trans_y, shape_y, axis_y); MatmulOneDNNHandler handler( dev_ctx, x_dims, y_dims, trans_x, trans_y); diff --git a/paddle/phi/kernels/onednn/matmul_kernel.cc b/paddle/phi/kernels/onednn/matmul_kernel.cc index ff574e71b49d412e7bcd4481bcf6a69b48bcac4d..520dc04b0f15624a070a2dd8f02e42abedd949cd 100644 --- a/paddle/phi/kernels/onednn/matmul_kernel.cc +++ b/paddle/phi/kernels/onednn/matmul_kernel.cc @@ -28,25 +28,6 @@ using phi::ReshapeToMatrix; namespace phi { -DDim GetDimsForInput(const OneDNNContext &dev_ctx, - DDim input_dims, - std::string input_name) { - auto shape = - dev_ctx.HasDnnAttr("fused_reshape_" + input_name) - ? PADDLE_GET_CONST(std::vector, - dev_ctx.GetDnnAttr("fused_reshape_" + input_name)) - : std::vector(); - auto axis = dev_ctx.HasDnnAttr("fused_transpose_" + input_name) - ? PADDLE_GET_CONST( - std::vector, - dev_ctx.GetDnnAttr("fused_transpose_" + input_name)) - : std::vector(); - if (!shape.empty() && !axis.empty()) { - return input_dims.reshape(shape).transpose(axis); - } - return input_dims; -} - void CalculateMatrixDims(const std::vector &x_dims, const std::vector &y_dims, std::vector *x_bd_dims, @@ -120,9 +101,8 @@ void MatmulKernel(const Context &dev_ctx, ? PADDLE_GET_CONST(bool, dev_ctx.GetDnnAttr("force_fp32_output")) : false; - auto x_dims = vectorize(GetDimsForInput(dev_ctx, x.dims(), "X")); - auto y_dims = vectorize(GetDimsForInput(dev_ctx, y.dims(), "Y")); - + auto x_dims = vectorize(x.dims()); + auto y_dims = vectorize(y.dims()); int ndims = std::max(x_dims.size(), y_dims.size()); ndims = std::max(ndims, 3); diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_flatten2_matmul_fuse_pass.py b/python/paddle/fluid/tests/unittests/ir/inference/test_flatten2_matmul_fuse_pass.py index 60763d63f790f4c2626df64beda5fef71fbf8d2b..e2833725aa9602e0a8d25e9c44c4a8344b52d644 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_flatten2_matmul_fuse_pass.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_flatten2_matmul_fuse_pass.py @@ -93,12 +93,6 @@ class TestFlatten2MatmulFusePass(PassAutoScanTest): alpha=alpha, transpose_X=transpose_X, transpose_Y=transpose_Y, - fused_reshape_X=[], - fused_reshape_Y=[], - fused_transpose_X=[], - fused_transpose_Y=[], - fused_reshape_Out=[], - fused_transpose_Out=[], ) add_op = OpConfig( diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_map_matmul_to_mul_pass.py b/python/paddle/fluid/tests/unittests/ir/inference/test_map_matmul_to_mul_pass.py index 559ab7acb700a4043e3e0ce14787063c8f8e8657..3e49e11c256fa68325c2440bf7dff38b4d364150 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_map_matmul_to_mul_pass.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_map_matmul_to_mul_pass.py @@ -96,12 +96,6 @@ class TestMapMatmulToMulPass(PassAutoScanTest): alpha=alpha, transpose_X=transpose_X, transpose_Y=transpose_Y, - fused_reshape_X=[], - fused_reshape_Y=[], - fused_transpose_X=[], - fused_transpose_Y=[], - fused_reshape_Out=[], - fused_transpose_Out=[], ) ops = [ diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_matmul_scale_fuse_pass.py b/python/paddle/fluid/tests/unittests/ir/inference/test_matmul_scale_fuse_pass.py index 64f08b8b9ed23851952290e72fe6613b31accebf..55b59ef4d5ca69f777dd24950fbecce76f58dbc4 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_matmul_scale_fuse_pass.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_matmul_scale_fuse_pass.py @@ -84,12 +84,6 @@ class TestMatmulScaleFusePass(PassAutoScanTest): transpose_X=transpose_X, transpose_Y=transpose_Y, alpha=alpha, - fused_reshape_X=[], - fused_reshape_Y=[], - fused_transpose_X=[], - fused_transpose_Y=[], - fused_reshape_Out=[], - fused_transpose_Out=[], head_number=1, ) is_scale_tensor = draw(st.booleans()) diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_mkldnn_scale_matmul_fuse_pass.py b/python/paddle/fluid/tests/unittests/ir/inference/test_mkldnn_scale_matmul_fuse_pass.py index 5c1a11625611ba67b82c3d462dcc87d1d0998708..01aebb474c9874bd75be905f17c3dc7ff4d73579 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_mkldnn_scale_matmul_fuse_pass.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_mkldnn_scale_matmul_fuse_pass.py @@ -113,12 +113,6 @@ class TestScaleMatmulMkldnnFusePass(PassAutoScanTest): 'transpose_X': attrs[1]['transpose_X'], 'transpose_Y': attrs[1]['transpose_Y'], 'alpha': attrs[1]['alpha'], - 'fused_reshape_X': [], - 'fused_reshape_Y': [], - 'fused_transpose_X': [], - 'fused_transpose_Y': [], - 'fused_reshape_Out': [], - 'fused_transpose_Out': [], }, }, ] diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_multihead_matmul_fuse_pass_v3.py b/python/paddle/fluid/tests/unittests/ir/inference/test_multihead_matmul_fuse_pass_v3.py index 359cf1398e6d9e20a6581215c1c85fba4f45d6a5..81e2e313c93c1d361793cd9bf971dc977df2d613 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_multihead_matmul_fuse_pass_v3.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_multihead_matmul_fuse_pass_v3.py @@ -118,12 +118,6 @@ class TestMultiheadMatmulFusePass(PassAutoScanTest): alpha=0.125, transpose_X=False, transpose_Y=False, - fused_reshape_Out=[], - fused_reshape_X=[], - fused_reshape_Y=[], - fused_transpose_Out=[], - fused_transpose_X=[], - fused_transpose_Y=[], ) ele_3 = OpConfig( "elementwise_add", @@ -151,12 +145,6 @@ class TestMultiheadMatmulFusePass(PassAutoScanTest): alpha=1.0, transpose_X=False, transpose_Y=False, - fused_reshape_Out=[], - fused_reshape_X=[], - fused_reshape_Y=[], - fused_transpose_Out=[], - fused_transpose_X=[], - fused_transpose_Y=[], ) transpose_3 = OpConfig( "transpose2", diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_multihead_matmul_roformer_fuse_pass.py b/python/paddle/fluid/tests/unittests/ir/inference/test_multihead_matmul_roformer_fuse_pass.py index 12be6672421275e86be7f9eb2a513f451f8394c4..a39d00479850772a6bfe800425a911a183b76128 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_multihead_matmul_roformer_fuse_pass.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_multihead_matmul_roformer_fuse_pass.py @@ -263,12 +263,6 @@ class TestMultiheadMatmulRoformerFusePass(PassAutoScanTest): alpha=1.0, transpose_X=False, transpose_Y=True, - fused_reshape_Out=[], - fused_reshape_X=[], - fused_reshape_Y=[], - fused_transpose_Out=[], - fused_transpose_X=[], - fused_transpose_Y=[], ) ele_3 = OpConfig( "elementwise_add", @@ -316,12 +310,6 @@ class TestMultiheadMatmulRoformerFusePass(PassAutoScanTest): alpha=1.0, transpose_X=False, transpose_Y=False, - fused_reshape_Out=[], - fused_reshape_X=[], - fused_reshape_Y=[], - fused_transpose_Out=[], - fused_transpose_X=[], - fused_transpose_Y=[], ) ops = [ mul_0, diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_onednn_matmul_transpose_reshape_fuse_pass.py b/python/paddle/fluid/tests/unittests/ir/inference/test_onednn_matmul_transpose_reshape_fuse_pass.py index d7ad3f64162b30298081d3a1e8b98e3ed7d01546..3afc4b0ac1cf6526797cc997b6f1f46dbae8ac4c 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_onednn_matmul_transpose_reshape_fuse_pass.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_onednn_matmul_transpose_reshape_fuse_pass.py @@ -67,15 +67,9 @@ class TestOneDNNMatmulTransposeReshapeFusePass(PassAutoScanTest): inputs={'X': ['input_data1'], 'Y': ['input_data2']}, outputs={'Out': ['matmul_output']}, attrs={ - 'transpose_X': transpose_X, - 'transpose_Y': transpose_Y, - 'alpha': alpha, - 'fused_reshape_X': [], - 'fused_reshape_Y': [], - 'fused_transpose_X': [], - 'fused_transpose_Y': [], - 'fused_reshape_Out': [], - 'fused_transpose_Out': [], + "transpose_X": transpose_X, + "transpose_Y": transpose_Y, + "alpha": alpha, }, ) diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_onednn_reshape_transpose_matmul_fuse_pass.py b/python/paddle/fluid/tests/unittests/ir/inference/test_onednn_reshape_transpose_matmul_fuse_pass.py index 4b11fa4826826d99ad5d7407e96f3f4c98119b65..b47ed04701df4b01273a35b1dbb79d8518c8c4c6 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_onednn_reshape_transpose_matmul_fuse_pass.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_onednn_reshape_transpose_matmul_fuse_pass.py @@ -124,12 +124,6 @@ class TestOneDNNReshapeTransposeMatmulFusePass(PassAutoScanTest): 'transpose_X': attrs[2]['transpose_X'], 'transpose_Y': attrs[2]['transpose_Y'], 'alpha': attrs[2]['alpha'], - 'fused_reshape_X': [], - 'fused_reshape_Y': [], - 'fused_transpose_X': [], - 'fused_transpose_Y': [], - 'fused_reshape_Out': [], - 'fused_transpose_Out': [], }, }, ] diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_reshape2_matmul_fuse_pass.py b/python/paddle/fluid/tests/unittests/ir/inference/test_reshape2_matmul_fuse_pass.py index 31c117154ddbce8edfe1a39b4d4cd036f21614b5..09dee9420df7f5f473ef4a30ad00f8b39accea9b 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_reshape2_matmul_fuse_pass.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_reshape2_matmul_fuse_pass.py @@ -102,12 +102,6 @@ class TestReshape2MatmulFusePass(PassAutoScanTest): alpha=alpha, transpose_X=transpose_X, transpose_Y=transpose_Y, - fused_reshape_X=[], - fused_reshape_Y=[], - fused_transpose_X=[], - fused_transpose_Y=[], - fused_reshape_Out=[], - fused_transpose_Out=[], ) add_op = OpConfig( diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_squared_mat_sub_fuse_pass.py b/python/paddle/fluid/tests/unittests/ir/inference/test_squared_mat_sub_fuse_pass.py index 6414a9eb20552b5e39dd80fd57cb45f6d9087166..023d7a8198007c4f8f70f8b136bfb43ce7fc1155 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_squared_mat_sub_fuse_pass.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_squared_mat_sub_fuse_pass.py @@ -56,12 +56,6 @@ class TestSquaredMatSubFusePass(PassAutoScanTest): "transpose_X": transpose_X, "transpose_Y": transpose_Y, "alpha": alpha1, - "fused_reshape_X": [], - "fused_reshape_Y": [], - "fused_transpose_X": [], - "fused_transpose_Y": [], - "fused_reshape_Out": [], - "fused_transpose_Out": [], }, ) @@ -94,12 +88,6 @@ class TestSquaredMatSubFusePass(PassAutoScanTest): "transpose_X": transpose_X, "transpose_Y": transpose_Y, "alpha": alpha2, - "fused_reshape_X": [], - "fused_reshape_Y": [], - "fused_transpose_X": [], - "fused_transpose_Y": [], - "fused_reshape_Out": [], - "fused_transpose_Out": [], }, ) diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_squeeze2_matmul_fuse_pass.py b/python/paddle/fluid/tests/unittests/ir/inference/test_squeeze2_matmul_fuse_pass.py index 6a1c017a42599a64fa0dbf58df9cfa3300bb49b0..d554b86fb7ef19e3d857deedea00f1518b223052 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_squeeze2_matmul_fuse_pass.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_squeeze2_matmul_fuse_pass.py @@ -104,12 +104,6 @@ class TestSqueeze2MatmulFusePass(PassAutoScanTest): alpha=alpha, transpose_X=transpose_X, transpose_Y=transpose_Y, - fused_reshape_X=[], - fused_reshape_Y=[], - fused_transpose_X=[], - fused_transpose_Y=[], - fused_reshape_Out=[], - fused_transpose_Out=[], ) add_op = OpConfig( diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_matmul.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_matmul.py index 456cae6164cdfac306c9f234b8f51feeccede242..516542827299c95c05b5edc7266bc9af17c00770 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_matmul.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_matmul.py @@ -52,12 +52,6 @@ class TrtConvertMatmulTest_static(TrtLayerAutoScanTest): "transpose_X": trans_x, "transpose_Y": trans_y, "alpha": alpha, - "fused_reshape_X": [], - "fused_reshape_Y": [], - "fused_transpose_X": [], - "fused_transpose_Y": [], - "fused_reshape_Out": [], - "fused_transpose_Out": [], } ] ops_config = [ @@ -143,12 +137,6 @@ class TrtConvertMatmulTest_dynamic(TrtLayerAutoScanTest): "transpose_X": trans_x, "transpose_Y": trans_y, "alpha": alpha, - "fused_reshape_X": [], - "fused_reshape_Y": [], - "fused_transpose_X": [], - "fused_transpose_Y": [], - "fused_reshape_Out": [], - "fused_transpose_Out": [], } ] ops_config = [ diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_multihead_matmul.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_multihead_matmul.py index cca7fcddcdabbf7e7959964a39a666180b91c517..1e788ac4b941d9fe3482b538c284d0ebfa6f66dd 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_multihead_matmul.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_multihead_matmul.py @@ -72,12 +72,6 @@ class TrtConvertMultiHeadMatmulTest(TrtLayerAutoScanTest): "alpha": 1.0, "transpose_X": False, "transpose_Y": True, - "fused_reshape_X": [], - "fused_reshape_Y": [], - "fused_transpose_X": [], - "fused_transpose_Y": [], - "fused_reshape_Out": [], - "fused_transpose_Out": [], }, {"axis": axis}, {"axis": -1, "is_test": True}, @@ -92,12 +86,6 @@ class TrtConvertMultiHeadMatmulTest(TrtLayerAutoScanTest): "alpha": 1.0, "transpose_X": False, "transpose_Y": False, - "fused_reshape_X": [], - "fused_reshape_Y": [], - "fused_transpose_X": [], - "fused_transpose_Y": [], - "fused_reshape_Out": [], - "fused_transpose_Out": [], }, {"axis": [0, 2, 1, 3]}, {"shape": [0, 0, 768]}, @@ -512,12 +500,6 @@ class TrtConvertMultiHeadMatmulTestInt8(TrtConvertMultiHeadMatmulTest): "alpha": 1.0, "transpose_X": False, "transpose_Y": True, - "fused_reshape_X": [], - "fused_reshape_Y": [], - "fused_transpose_X": [], - "fused_transpose_Y": [], - "fused_reshape_Out": [], - "fused_transpose_Out": [], }, {"axis": axis}, {"axis": -1, "is_test": True}, @@ -532,12 +514,6 @@ class TrtConvertMultiHeadMatmulTestInt8(TrtConvertMultiHeadMatmulTest): "alpha": 1.0, "transpose_X": False, "transpose_Y": False, - "fused_reshape_X": [], - "fused_reshape_Y": [], - "fused_transpose_X": [], - "fused_transpose_Y": [], - "fused_reshape_Out": [], - "fused_transpose_Out": [], }, {"axis": [0, 2, 1, 3]}, {"shape": [0, 0, 768]}, @@ -1142,12 +1118,6 @@ class TrtConvertMultiHeadMatmulTest_biasqk_seqseq(TrtLayerAutoScanTest): "alpha": 1.0, "transpose_X": False, "transpose_Y": True, - "fused_reshape_X": [], - "fused_reshape_Y": [], - "fused_transpose_X": [], - "fused_transpose_Y": [], - "fused_reshape_Out": [], - "fused_transpose_Out": [], }, {"axis": axis}, {"axis": -1, "is_test": True}, @@ -1162,12 +1132,6 @@ class TrtConvertMultiHeadMatmulTest_biasqk_seqseq(TrtLayerAutoScanTest): "alpha": 1.0, "transpose_X": False, "transpose_Y": False, - "fused_reshape_X": [], - "fused_reshape_Y": [], - "fused_transpose_X": [], - "fused_transpose_Y": [], - "fused_reshape_Out": [], - "fused_transpose_Out": [], }, {"axis": [0, 2, 1, 3]}, {"shape": [0, 0, 768]}, diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_multihead_matmul_roformer.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_multihead_matmul_roformer.py index f6665ecd23af20122a6b97505a32e4592d947b1d..2de959a6ee71690e8ffe73654040f881cad74106 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_multihead_matmul_roformer.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_convert_multihead_matmul_roformer.py @@ -98,12 +98,6 @@ class TrtConvertMultiHeadMatmulRoformerTest(TrtLayerAutoScanTest): "alpha": 1.0, "transpose_X": False, "transpose_Y": True, - "fused_reshape_X": [], - "fused_reshape_Y": [], - "fused_transpose_X": [], - "fused_transpose_Y": [], - "fused_reshape_Out": [], - "fused_transpose_Out": [], }, {"axis": axis}, {"axis": -1, "is_test": True}, @@ -118,12 +112,6 @@ class TrtConvertMultiHeadMatmulRoformerTest(TrtLayerAutoScanTest): "alpha": 1.0, "transpose_X": False, "transpose_Y": False, - "fused_reshape_X": [], - "fused_reshape_Y": [], - "fused_transpose_X": [], - "fused_transpose_Y": [], - "fused_reshape_Out": [], - "fused_transpose_Out": [], }, {"axis": [0, 2, 1, 3]}, {"shape": [0, 0, 768]}, diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_flatten2_matmul_fuse_pass.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_flatten2_matmul_fuse_pass.py index bacf239a2ad06fa289b333090d86c048e6d5ea88..6d62c6b8ab343c38ad97871062055f6b5e488ef6 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_flatten2_matmul_fuse_pass.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_flatten2_matmul_fuse_pass.py @@ -111,12 +111,6 @@ class TestFlatten2MatmulFusePass(PassAutoScanTest): alpha=alpha, transpose_X=transpose_X, transpose_Y=transpose_Y, - fused_reshape_X=[], - fused_reshape_Y=[], - fused_transpose_X=[], - fused_transpose_Y=[], - fused_reshape_Out=[], - fused_transpose_Out=[], ) add_op = OpConfig( diff --git a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_squeeze2_matmul_fuse_pass.py b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_squeeze2_matmul_fuse_pass.py index a8627041ef501c1a128bb56f4d64c752178c7311..1c7026df19c783494e0ae09bb81c015d32331dbe 100644 --- a/python/paddle/fluid/tests/unittests/ir/inference/test_trt_squeeze2_matmul_fuse_pass.py +++ b/python/paddle/fluid/tests/unittests/ir/inference/test_trt_squeeze2_matmul_fuse_pass.py @@ -116,12 +116,6 @@ class TestSqueeze2MatmulFusePass(PassAutoScanTest): alpha=alpha, transpose_X=transpose_X, transpose_Y=transpose_Y, - fused_reshape_X=[], - fused_reshape_Y=[], - fused_transpose_X=[], - fused_transpose_Y=[], - fused_reshape_Out=[], - fused_transpose_Out=[], ) add_op = OpConfig( diff --git a/test/mkldnn/test_matmul_mkldnn_op.py b/test/mkldnn/test_matmul_mkldnn_op.py index 07c4a5bf7ded23a6d83553c8da4d34a5de43fc0c..0078722b61aae11a9ee2ebbc8e14e62336577f72 100644 --- a/test/mkldnn/test_matmul_mkldnn_op.py +++ b/test/mkldnn/test_matmul_mkldnn_op.py @@ -17,10 +17,7 @@ import unittest import numpy as np -from paddle.fluid.tests.unittests.eager_op_test import ( - OpTest, - skip_check_grad_ci, -) +from paddle.fluid.tests.unittests.eager_op_test import OpTest class TestDnnlMatMulOp(OpTest): @@ -257,321 +254,6 @@ class TestDnnlMatMulOpInt8ForceFP32BasicScales(TestDnnlMatMulOp): self.attrs = {'force_fp32_output': True} -@skip_check_grad_ci(reason="DNNL's MatMul doesn't implement grad kernel.") -class TestReshapeTransposeMatMulOp(OpTest): - def init_data_type(self): - self.data_type_ = 'float32' - - def generate_data(self): - self.x = ( - np.random.random([2, 128, 768]) - .astype("float32") - .reshape([2, 128, 12, 64]) - .transpose([0, 2, 1, 3]) - ) - self.y = ( - np.random.random([2, 128, 768]) - .astype("float32") - .reshape([2, 128, 12, 64]) - .transpose([0, 2, 1, 3]) - ) - self.out = np.matmul(self.x, self.y.transpose([0, 1, 3, 2])) - self.fused_reshape_X = [] - self.fused_transpose_X = [] - self.fused_reshape_Y = [] - self.fused_transpose_Y = [] - - def set_op_type_and_transpose_y_name(self): - self.op_type = "matmul" - self.transpose_y_name = "transpose_Y" - - def setUp(self): - self.set_op_type_and_transpose_y_name() - self._cpu_only = True - self.use_mkldnn = True - self.transpose_y = True - self.init_data_type() - self.generate_data() - - self.inputs = {'X': self.x, 'Y': self.y} - self.attrs = { - 'use_mkldnn': self.use_mkldnn, - self.transpose_y_name: self.transpose_y, - } - if len(self.fused_transpose_X) > 0: - self.attrs['fused_transpose_X'] = self.fused_transpose_X - if len(self.fused_transpose_Y) > 0: - self.attrs['fused_transpose_Y'] = self.fused_transpose_Y - if len(self.fused_reshape_X) > 0: - self.attrs['fused_reshape_X'] = self.fused_reshape_X - if len(self.fused_reshape_Y) > 0: - self.attrs['fused_reshape_Y'] = self.fused_reshape_Y - - self.outputs = {'Out': self.out} - - def test_check_output(self): - self.check_output() - - -class TestReshapeTransposeMatMulOp4DXFloat(TestReshapeTransposeMatMulOp): - def generate_data(self): - self.x = np.random.random([2, 128, 768]).astype("float32") - self.y = ( - np.random.random([2, 128, 768]) - .astype("float32") - .reshape([2, 128, 12, 64]) - .transpose([0, 2, 1, 3]) - ) - self.fused_transpose_X = [0, 2, 1, 3] - self.fused_reshape_X = [0, 0, 12, 64] - self.fused_transpose_Y = [] - self.fused_reshape_Y = [] - self.out = np.matmul( - self.x.reshape([2, 128, 12, 64]).transpose([0, 2, 1, 3]), - self.y.transpose([0, 1, 3, 2]), - ) - - -class TestReshapeTransposeMatMulOp4DXInt8(TestReshapeTransposeMatMulOp4DXFloat): - def init_data_type(self): - self.data_type_ = 'int8' - - -class TestReshapeTransposeMatMulOp4DYFloat(TestReshapeTransposeMatMulOp): - def generate_data(self): - self.x = ( - np.random.random([2, 128, 768]) - .astype("float32") - .reshape([2, 128, 12, 64]) - .transpose([0, 2, 1, 3]) - ) - self.y = np.random.random([2, 128, 768]).astype("float32") - self.fused_transpose_X = [] - self.fused_reshape_X = [] - self.fused_transpose_Y = [0, 2, 1, 3] - self.fused_reshape_Y = [0, 0, 12, 64] - self.out = np.matmul( - self.x, self.y.reshape([2, 128, 12, 64]).transpose([0, 2, 3, 1]) - ) - - -class TestReshapeTransposeMatMulOp4DYInt8(TestReshapeTransposeMatMulOp4DYFloat): - def init_data_type(self): - self.data_type_ = 'int8' - - -class TestReshapeTransposeMatMulOp4DXYFloat(TestReshapeTransposeMatMulOp): - def generate_data(self): - self.x = np.random.random([2, 128, 768]).astype("float32") - self.y = np.random.random([2, 128, 768]).astype("float32") - self.fused_transpose_X = [0, 2, 1, 3] - self.fused_reshape_X = [0, 0, 12, 64] - self.fused_transpose_Y = [0, 2, 1, 3] - self.fused_reshape_Y = [0, 0, 12, 64] - self.out = np.matmul( - self.x.reshape([2, 128, 12, 64]).transpose([0, 2, 1, 3]), - self.y.reshape([2, 128, 12, 64]).transpose([0, 2, 3, 1]), - ) - - -class TestReshapeTransposeMatMulOp4DXYInt8( - TestReshapeTransposeMatMulOp4DXYFloat -): - def init_data_type(self): - self.data_type_ = 'int8' - - -class TestReshapeTransposeMatMulOp2DXFloat(TestReshapeTransposeMatMulOp): - def generate_data(self): - self.x = np.random.random([2, 5, 10]).astype("float32") - self.y = ( - np.random.random([2, 5, 10]) - .astype("float32") - .reshape([10, 10]) - .transpose([1, 0]) - ) - self.fused_transpose_X = [1, 0] - self.fused_reshape_X = [10, 10] - self.fused_transpose_Y = [] - self.fused_reshape_Y = [] - self.out = np.matmul( - self.x.reshape([10, 10]).transpose([1, 0]), self.y.transpose([1, 0]) - ) - - -class TestReshapeTransposeMatMulOp2DXInt8(TestReshapeTransposeMatMulOp2DXFloat): - def init_data_type(self): - self.data_type_ = 'int8' - - -class TestReshapeTransposeMatMulOp2DYFloat(TestReshapeTransposeMatMulOp): - def generate_data(self): - self.x = ( - np.random.random([2, 5, 10]) - .astype("float32") - .reshape([10, 10]) - .transpose([1, 0]) - ) - self.y = np.random.random([2, 5, 10]).astype("float32") - self.fused_transpose_X = [] - self.fused_reshape_X = [] - self.fused_transpose_Y = [1, 0] - self.fused_reshape_Y = [10, 10] - self.out = np.matmul(self.x, self.y.reshape([10, 10])) - - -class TestReshapeTransposeMatMulOp2DYInt8(TestReshapeTransposeMatMulOp2DYFloat): - def init_data_type(self): - self.data_type_ = 'int8' - - -class TestReshapeTransposeMatMulOp3DXFloat(TestReshapeTransposeMatMulOp): - def generate_data(self): - self.x = np.random.random([2, 2, 5, 5]).astype("float32") - self.y = ( - np.random.random([2, 2, 5, 5]) - .astype("float32") - .reshape([2, 10, 5]) - .transpose([0, 2, 1]) - ) - self.fused_transpose_X = [0, 2, 1] - self.fused_reshape_X = [2, 10, 5] - self.fused_transpose_Y = [] - self.fused_reshape_Y = [] - self.out = np.matmul( - self.x.reshape([2, 10, 5]).transpose(0, 2, 1), - self.y.transpose(0, 2, 1), - ) - - -class TestReshapeTransposeMatMulOp3DXInt8(TestReshapeTransposeMatMulOp3DXFloat): - def init_data_type(self): - self.data_type_ = 'int8' - - -class TestReshapeTransposeMatMulOp3DYFloat(TestReshapeTransposeMatMulOp): - def generate_data(self): - self.x = ( - np.random.random([2, 2, 5, 5]) - .astype(self.data_type_) - .reshape([2, 10, 5]) - .transpose([0, 2, 1]) - ) - self.y = np.random.random([2, 2, 5, 5]).astype(self.data_type_) - self.fused_transpose_X = [] - self.fused_reshape_X = [] - self.fused_transpose_Y = [0, 2, 1] - self.fused_reshape_Y = [2, 10, 5] - self.out = np.matmul(self.x, self.y.reshape([2, 10, 5])) - - -class TestReshapeTransposeMatMulOp3DYInt8(TestReshapeTransposeMatMulOp3DYFloat): - def init_data_type(self): - self.data_type_ = 'int8' - - -@skip_check_grad_ci(reason="Tests inference only optimization.") -class TestMatMulOpTransposeReshapeEmptyFloat(OpTest): - def init_data_type(self): - self.data_type_ = np.float32 - - def generate_data(self): - self.bs = 1 - self.x = np.random.random([self.bs, 128, 128]).astype(self.data_type_) - self.y = np.random.random([self.bs, 128, 64]).astype(self.data_type_) - - def init_params_and_out(self): - self.transpose_out = [] - self.reshape_out = [] - self.out = np.matmul(self.x, self.y) - - def set_op_type(self): - self.op_type = "matmul" - - def setUp(self): - self.set_op_type() - self._cpu_only = True - self.use_mkldnn = True - self.init_data_type() - self.generate_data() - self.init_params_and_out() - - self.inputs = {'X': self.x, 'Y': self.y} - self.attrs = {'use_mkldnn': self.use_mkldnn} - - if len(self.reshape_out) > 0: - self.attrs['fused_reshape_Out'] = self.reshape_out - if len(self.transpose_out) > 0: - self.attrs['fused_transpose_Out'] = self.transpose_out - - self.inputs = {'X': self.x, 'Y': self.y} - self.outputs = {'Out': self.out} - - def test_check_output(self): - self.check_output() - - def check_raise_error(self, msg): - try: - self.check_output() - except Exception as e: - if msg in str(e): - raise AttributeError - else: - print(e) - - -class TestMatMulOpTransposeReshapeIntEmptyInt( - TestMatMulOpTransposeReshapeEmptyFloat -): - def init_data_type(self): - self.data_type_ = np.int8 - - -class TestMatMulOpTransposeReshapeBasicFloat( - TestMatMulOpTransposeReshapeEmptyFloat -): - def generate_data(self): - self.bs = 8 - self.x = np.random.random([self.bs, 12, 128, 128]).astype( - self.data_type_ - ) - self.y = np.random.random([self.bs, 12, 128, 64]).astype( - self.data_type_ - ) - - def init_params_and_out(self): - self.transpose_out = [0, 2, 1, 3] - self.reshape_out = [0, 0, self.x.shape[1] * self.y.shape[-1]] - self.out = ( - np.matmul(self.x, self.y) - .transpose([0, 2, 1, 3]) - .reshape([self.bs, -1, self.x.shape[1] * self.y.shape[-1]]) - ) - - -class TestMatMulOpTransposeReshapeBasicInt( - TestMatMulOpTransposeReshapeBasicFloat -): - def init_data_type(self): - self.data_type_ = np.int8 - - -class TestMatMulOpTransposeReshapeOtherDimFloat( - TestMatMulOpTransposeReshapeBasicFloat -): - def generate_data(self): - self.bs = 11 - self.x = np.random.random([self.bs, 12, 14, 18]).astype(self.data_type_) - self.y = np.random.random([self.bs, 12, 18, 13]).astype(self.data_type_) - - -class TestMatMulOpTransposeReshapeOtherDimInt( - TestMatMulOpTransposeReshapeOtherDimFloat -): - def init_data_type(self): - self.data_type_ = np.int8 - - if __name__ == "__main__": from paddle import enable_static diff --git a/test/mkldnn/test_matmul_v2_mkldnn_op.py b/test/mkldnn/test_matmul_v2_mkldnn_op.py index 97910f596ddd4f47672c45a9a403d9c8bb8e0b7e..958a8cca21e511d8ecdbacdc2958268bd89ee20f 100644 --- a/test/mkldnn/test_matmul_v2_mkldnn_op.py +++ b/test/mkldnn/test_matmul_v2_mkldnn_op.py @@ -15,19 +15,6 @@ import unittest import numpy as np -from test_matmul_mkldnn_op import ( - TestMatMulOpTransposeReshapeBasicFloat, - TestMatMulOpTransposeReshapeEmptyFloat, - TestMatMulOpTransposeReshapeOtherDimFloat, - TestReshapeTransposeMatMulOp, - TestReshapeTransposeMatMulOp2DXFloat, - TestReshapeTransposeMatMulOp2DYFloat, - TestReshapeTransposeMatMulOp3DXFloat, - TestReshapeTransposeMatMulOp3DYFloat, - TestReshapeTransposeMatMulOp4DXFloat, - TestReshapeTransposeMatMulOp4DXYFloat, - TestReshapeTransposeMatMulOp4DYFloat, -) import paddle from paddle.fluid import core @@ -472,89 +459,6 @@ create_bf16_test_class(TestMatMulV2MatrixXMatrix6Dx2DOneDNNOp) create_bf16_test_class(TestMatMulV2MatrixXMatrix2Dx5DOneDNNOp) -class TestMatMulV2OpTransposeReshapeEmptyFloat( - TestMatMulOpTransposeReshapeEmptyFloat -): - def set_op_type(self): - self.op_type = "matmul_v2" - - -class TestMatMulV2OpTransposeReshapeBasicFloat( - TestMatMulOpTransposeReshapeBasicFloat -): - def set_op_type(self): - self.op_type = "matmul_v2" - - -class TestMatMulV2OpTransposeReshapeOtherDimFloat( - TestMatMulOpTransposeReshapeOtherDimFloat -): - def set_op_type(self): - self.op_type = "matmul_v2" - - -class TestMatMulV2OpReshapeTranspose(TestReshapeTransposeMatMulOp): - def set_op_type_and_transpose_y_name(self): - self.op_type = "matmul_v2" - self.transpose_y_name = "trans_y" - - -class TestMatMulV2OpReshapeTranspose4DXFloat( - TestReshapeTransposeMatMulOp4DXFloat -): - def set_op_type_and_transpose_y_name(self): - self.op_type = "matmul_v2" - self.transpose_y_name = "trans_y" - - -class TestMatMulV2OpReshapeTranspose4DYFloat( - TestReshapeTransposeMatMulOp4DYFloat -): - def set_op_type_and_transpose_y_name(self): - self.op_type = "matmul_v2" - self.transpose_y_name = "trans_y" - - -class TestMatMulV2OpReshapeTranspose4DXYFloat( - TestReshapeTransposeMatMulOp4DXYFloat -): - def set_op_type_and_transpose_y_name(self): - self.op_type = "matmul_v2" - self.transpose_y_name = "trans_y" - - -class TestMatMulV2OpReshapeTranspose2DXFloat( - TestReshapeTransposeMatMulOp2DXFloat -): - def set_op_type_and_transpose_y_name(self): - self.op_type = "matmul_v2" - self.transpose_y_name = "trans_y" - - -class TestMatMulV2OpReshapeTranspose2DYFloat( - TestReshapeTransposeMatMulOp2DYFloat -): - def set_op_type_and_transpose_y_name(self): - self.op_type = "matmul_v2" - self.transpose_y_name = "trans_y" - - -class TestMatMulV2OpReshapeTranspose3DXFloat( - TestReshapeTransposeMatMulOp3DXFloat -): - def set_op_type_and_transpose_y_name(self): - self.op_type = "matmul_v2" - self.transpose_y_name = "trans_y" - - -class TestMatMulV2OpReshapeTranspose3DYFloat( - TestReshapeTransposeMatMulOp3DYFloat -): - def set_op_type_and_transpose_y_name(self): - self.op_type = "matmul_v2" - self.transpose_y_name = "trans_y" - - if __name__ == "__main__": paddle.enable_static() unittest.main()