Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove oneDNN-specific attributes from matmul #49444

Merged
merged 103 commits into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from 97 commits
Commits
Show all changes
103 commits
Select commit Hold shift + click to select a range
8cf612a
replace matmul with matmul_v2 in fuse passes
Silv3S Dec 14, 2022
1a877be
Remove fusion logic from matmul
Silv3S Dec 14, 2022
71da01e
Merge branch 'develop' into clean_matmuls
Silv3S Dec 15, 2022
e47c071
removing fusion methods
Silv3S Dec 15, 2022
2d863cd
add proper name
Silv3S Dec 15, 2022
02fbcb9
adjust namespaces
Silv3S Dec 15, 2022
7df1a54
clean attrs in python tests
Silv3S Dec 21, 2022
c7b4e05
delete checkpoint and restore matmul version
Silv3S Dec 21, 2022
c98f126
remove unused code
Silv3S Dec 22, 2022
53f6ae5
Merge branch 'PaddlePaddle:develop' into remove_extra_matmul_attrs
Silv3S Dec 23, 2022
a301add
matmul and reshape/transpose fuses migrated
Silv3S Dec 23, 2022
3cff939
split MatmulOneDNN headers
Silv3S Dec 27, 2022
cc26096
fuse activation and eltwise_add
Silv3S Dec 27, 2022
a8c3774
add fuse_activation
Silv3S Dec 27, 2022
edb6615
matmul_transpose_reshape/reshape_transpose_matmul
Silv3S Dec 28, 2022
2bf6d5a
matmul + elementwise_add (fused)
Silv3S Dec 28, 2022
b0bf97e
activation temporary modifciation
Silv3S Dec 28, 2022
1efa5c0
restore matmul(v1) version 0
Silv3S Dec 29, 2022
c345fe3
Merge branch 'develop' into matmul_v1_version
Silv3S Jan 3, 2023
773f3c1
Merge branch 'develop' into remove_extra_matmul_attrs
Silv3S Jan 3, 2023
aee35ec
Merge branch 'PaddlePaddle:develop' into remove_extra_matmul_attrs
Silv3S Jan 3, 2023
cac16fb
merge newest develop
Silv3S Jan 4, 2023
1cfa75b
revert depedency from other PR
Silv3S Jan 4, 2023
b2e9baa
Merge branch 'remove_extra_matmul_attrs' of https://github.com/Silv3S…
Silv3S Jan 4, 2023
5eb838c
remove depedency from other PR
Silv3S Jan 4, 2023
dc3cf8e
revert pbtxt
Silv3S Jan 4, 2023
5833035
remove placeholders from matmul_v2
Silv3S Jan 4, 2023
5d71310
add description in OPMaker
Silv3S Jan 5, 2023
8195aa4
remove matmul_v2_op.h and all depedencies
Silv3S Jan 5, 2023
9e1c32b
remove dims changing in base op
Silv3S Jan 5, 2023
c7df785
add possibility to fuse already fused_matmul
Silv3S Jan 5, 2023
946bf04
Merge branch 'PaddlePaddle:develop' into remove_extra_matmul_attrs
Silv3S Jan 5, 2023
415202e
Merge branch 'PaddlePaddle:develop' into remove_extra_matmul_attrs
Silv3S Jan 8, 2023
52d43ca
Merge branch 'PaddlePaddle:develop' into remove_extra_matmul_attrs
Silv3S Jan 9, 2023
8687536
restart broken CI
Silv3S Jan 9, 2023
4bd883f
Empty-Commit
Silv3S Jan 9, 2023
35ad305
Merge branch 'PaddlePaddle:develop' into remove_extra_matmul_attrs
Silv3S Jan 10, 2023
efaacbf
revert matmul_utils.h
Silv3S Jan 10, 2023
60b2eec
codestyle
Silv3S Jan 10, 2023
3d266dd
adjust imports
Silv3S Jan 10, 2023
39edccd
add pbtxt file
Silv3S Jan 10, 2023
25659f4
Merge branch 'PaddlePaddle:develop' into remove_extra_matmul_attrs
Silv3S Jan 11, 2023
0dc52e0
100% matmul unit tests coverage
Silv3S Jan 11, 2023
a00c10e
trigger CI with minimal changes to develop
Silv3S Jan 11, 2023
274f399
adjust changes to develop
Silv3S Jan 11, 2023
bd57ba6
add fused_matmul op
Silv3S Jan 12, 2023
a825670
inherit base ops
Silv3S Jan 12, 2023
dbbeacc
add "v2"
Silv3S Jan 12, 2023
2276699
move OPMaker
Silv3S Jan 12, 2023
7f741b9
Gradually add fused_matmul files
Silv3S Jan 12, 2023
2fa89a8
second batch of fused_matmul changes
Silv3S Jan 12, 2023
bef0d26
split infershapes of matmul_v2 and fused_matmul
Silv3S Jan 12, 2023
f69b13d
Merge branch 'remove_extra_matmul_attrs' into matmul_v1_version
Silv3S Jan 16, 2023
e2fbf17
Merge branch 'develop' into matmul_v1_version
Silv3S Jan 16, 2023
1593cdb
merge code from other PR
Silv3S Jan 16, 2023
d61c27b
2023
Silv3S Jan 17, 2023
862671a
Merge branch 'develop' into remove_extra_matmul_attrs
Silv3S Jan 17, 2023
f4b945a
inherit fused_matmul from matmul_v2
Silv3S Jan 17, 2023
b8dc210
Update paddle/phi/backends/onednn/onednn_reuse.h
Silv3S Jan 17, 2023
1ce346e
Update paddle/phi/kernels/fusion/onednn/fused_matmul_kernel.cc
Silv3S Jan 17, 2023
51e704a
resolve conflicts
Silv3S Jan 18, 2023
5cbb217
Merge branch 'develop' into remove_extra_matmul_attrs
Silv3S Jan 18, 2023
f0e2abe
codestyle
Silv3S Jan 18, 2023
961be5f
Merge branch 'develop' into matmul_v1_version
Silv3S Jan 19, 2023
416e133
Merge branch 'remove_extra_matmul_attrs' into matmul_v1_version
Silv3S Jan 19, 2023
dafb13c
simplify isgemmlinear
Silv3S Jan 19, 2023
1799c0c
2023
Silv3S Jan 24, 2023
74f8eea
remove import
Silv3S Jan 24, 2023
ee92808
Merge branch 'develop' into matmul_v1_version
Silv3S Jan 31, 2023
258f0f5
Merge branch 'develop' into matmul_v1_version
Silv3S Feb 3, 2023
0d1e3c3
reuse methods
Silv3S Feb 6, 2023
988d9ca
Merge branch 'PaddlePaddle:develop' into matmul_v1_version
Silv3S Feb 6, 2023
7821c03
matmul_v2_mkldnn cleanup
Silv3S Feb 7, 2023
36780c3
simplify ExecuteMatMulV1Grad
Silv3S Feb 7, 2023
eb3e66b
matmul refactored
Silv3S Feb 17, 2023
42daa32
fc
Silv3S Feb 17, 2023
76cec53
SetOutMemDescWithLogicalLayoutFusesSupport
Silv3S Feb 17, 2023
10914c4
matmul_v2
Silv3S Feb 17, 2023
044e771
alpha support
Silv3S Feb 17, 2023
b860852
group repetetive funcs
Silv3S Feb 21, 2023
a28c27d
matmul utils
Silv3S Feb 21, 2023
cfecb2f
execute matmul methods
Silv3S Feb 21, 2023
45b0d9a
Merge branch 'PaddlePaddle:develop' into onednn_ops_cleanup
Silv3S Feb 21, 2023
ce093a6
Merge branch 'PaddlePaddle:develop' into onednn_ops_cleanup
Silv3S Feb 21, 2023
b8c0d19
restore registered kernel names
Silv3S Feb 22, 2023
100e555
Merge branch 'PaddlePaddle:develop' into onednn_ops_cleanup
Silv3S Feb 22, 2023
cb7c9da
split header and impl files
Silv3S Feb 23, 2023
1712c85
remove double negatives
Silv3S Feb 23, 2023
9bade5e
Merge branch 'develop' into matmul_v1_version
Silv3S Feb 23, 2023
6091f5f
Merge branch 'onednn_ops_cleanup' into matmul_v1_version
Silv3S Feb 23, 2023
8e0228b
reduce numer of modified files
Silv3S Feb 24, 2023
612cc42
Merge branch 'develop' into matmul_v1_version
Silv3S Mar 6, 2023
c41a809
adjust ExecuteMatmul
Silv3S Mar 6, 2023
47dbfd9
add scales for ut
Silv3S Mar 6, 2023
7e97b3e
dates
Silv3S Mar 6, 2023
ab1e8e0
limit number of modified files
Silv3S Mar 14, 2023
ba40854
fluid imports
Silv3S Mar 14, 2023
a46b4ae
Merge branch 'develop' into matmul_v1_version
Silv3S Mar 27, 2023
207ed4a
remove alpha
Silv3S Mar 27, 2023
ef0fbaa
Merge branch 'develop' into matmul_v1_version
Silv3S Mar 28, 2023
9bdfbd4
Merge branch 'develop' into matmul_v1_version
Silv3S Apr 3, 2023
b7226d9
Merge branch 'PaddlePaddle:develop' into matmul_v1_version
Silv3S Apr 4, 2023
7fabb00
codestyle
Silv3S Apr 4, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 6 additions & 26 deletions paddle/fluid/framework/ir/fuse_gemm_epilogue_pass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -509,22 +503,8 @@ ir::Graph *FuseGemmEpiloguePass::FuseLinearActBwd(

bool FuseGemmEpiloguePass::IsGemmFromLinear_(
const std::vector<int64_t> &x_shape,
const std::vector<int64_t> &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<int> tmp_vec =
PADDLE_GET_CONST(std::vector<int>, matmul_v2_op->GetAttr(attr_name));
if (tmp_vec.size() > 0) return false;
}
}
return true;
const std::vector<int64_t> &w_shape) const {
return (w_shape.size() == 2 && x_shape.size() >= 2);
}

} // namespace ir
Expand Down
3 changes: 1 addition & 2 deletions paddle/fluid/framework/ir/fuse_gemm_epilogue_pass.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ class FuseGemmEpiloguePass : public FusePassBase {

private:
bool IsGemmFromLinear_(const std::vector<int64_t> &x_shape,
const std::vector<int64_t> &w_shape,
OpDesc *matmul_v2_op) const;
const std::vector<int64_t> &w_shape) const;
const std::string GetReserveSpaceCacheKey(const std::string var_name,
int block_id) const {
return std::to_string(block_id) + var_name;
Expand Down
24 changes: 0 additions & 24 deletions paddle/fluid/operators/compat/matmul.pbtxt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
14 changes: 1 addition & 13 deletions paddle/fluid/operators/feed_forward_test.cu
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -167,12 +167,6 @@ void GetLinearOpGrad(const std::vector<T> &x_vec,
dout_ptr, dout_vec.data(), size_z * sizeof(T), cudaMemcpyHostToDevice);

bool use_mkldnn = false;
std::vector<int> fused_reshape_X = {};
std::vector<int> fused_reshape_Y = {};
std::vector<int> fused_reshape_Out = {};
std::vector<int> fused_transpose_X = {};
std::vector<int> fused_transpose_Y = {};
std::vector<int> 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;
Expand All @@ -182,12 +176,6 @@ void GetLinearOpGrad(const std::vector<T> &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});
Expand Down
73 changes: 13 additions & 60 deletions paddle/fluid/operators/matmul_op.cc
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -302,15 +299,15 @@ class MatMulGradKernel : public framework::OpKernel<T> {
bool transpose_y = context.Attr<bool>("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()) {
dx->Resize(x.dims());
}
}

framework::DDim dy_dims;
phi::DDim dy_dims;
if (dy) {
dy_dims = dy->dims();
if (dy_dims != y.dims()) {
Expand Down Expand Up @@ -345,23 +342,15 @@ class MatMulGradKernel : public framework::OpKernel<T> {
}
};

framework::DDim GetDimForInput(const framework::InferShapeContext &ctx,
std::string input_name) {
auto shape = ctx.Attrs().Get<std::vector<int>>("fused_reshape_" + input_name);
auto axis =
ctx.Attrs().Get<std::vector<int>>("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;
}

Expand Down Expand Up @@ -447,23 +436,23 @@ class MatMulDoubleGradKernel : public framework::OpKernel<T> {

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()) {
dx->Resize(x.dims());
}
}

framework::DDim dy_dims;
phi::DDim dy_dims;
if (dy) {
dy_dims = dy->dims();
if (dy_dims != y.dims()) {
dy->Resize(y.dims());
}
}

framework::DDim ddout_dims;
phi::DDim ddout_dims;
if (ddout) {
ddout_dims = ddout->dims();
if (ddout_dims != dout.dims()) {
Expand Down Expand Up @@ -615,7 +604,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.",
Expand All @@ -631,7 +620,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.",
Expand All @@ -645,7 +634,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].",
Expand Down Expand Up @@ -679,16 +668,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<std::vector<int>>("fused_reshape_Out");
auto axis = context->Attrs().Get<std::vector<int>>("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");
}
Expand Down Expand Up @@ -747,34 +728,6 @@ class MatMulOpMaker : public framework::OpProtoAndCheckerMaker {
"(bool, default false) Indicates if MKL-DNN kernel will be used")
.SetDefault(false)
.AsExtra();
AddAttr<std::vector<int>>("fused_reshape_X",
R"DOC(Shape of fused reshape of `X` input.)DOC")
.SetDefault({})
.AsExtra();
AddAttr<std::vector<int>>("fused_reshape_Y",
R"DOC(Shape of fused reshape of `Y` input.)DOC")
.SetDefault({})
.AsExtra();
AddAttr<std::vector<int>>("fused_transpose_X",
R"DOC(Axis of fused transpose of `X` input.)DOC")
.SetDefault({})
.AsExtra();
AddAttr<std::vector<int>>("fused_transpose_Y",
R"DOC(Axis of fused transpose of `Y` input.)DOC")
.SetDefault({})
.AsExtra();
AddAttr<std::vector<int>>(
"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<std::vector<int>>(
"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<bool>(
"use_quantizer",
"(bool, default false) "
Expand Down
Loading