Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
609077e9
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
609077e9
编写于
3月 25, 2022
作者:
C
Chen Weihang
提交者:
GitHub
3月 25, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
move mul op infershape (#40917)
上级
4ab8255a
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
93 addition
and
87 deletion
+93
-87
paddle/fluid/operators/mul_op.cc
paddle/fluid/operators/mul_op.cc
+12
-87
paddle/phi/infermeta/binary.cc
paddle/phi/infermeta/binary.cc
+75
-0
paddle/phi/infermeta/binary.h
paddle/phi/infermeta/binary.h
+6
-0
未找到文件。
paddle/fluid/operators/mul_op.cc
浏览文件 @
609077e9
...
...
@@ -21,6 +21,10 @@ limitations under the License. */
#include "paddle/fluid/platform/mkldnn_helper.h"
#endif
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/backward.h"
#include "paddle/phi/infermeta/binary.h"
namespace
paddle
{
namespace
operators
{
...
...
@@ -34,72 +38,6 @@ class MulOp : public framework::OperatorWithKernel {
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
OP_INOUT_CHECK
(
ctx
->
HasInput
(
"X"
),
"Input"
,
"X"
,
"Mul"
);
OP_INOUT_CHECK
(
ctx
->
HasInput
(
"Y"
),
"Input"
,
"Y"
,
"Mul"
);
OP_INOUT_CHECK
(
ctx
->
HasOutput
(
"Out"
),
"Output"
,
"Out"
,
"Mul"
);
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
auto
y_dims
=
ctx
->
GetInputDim
(
"Y"
);
int
x_num_col_dims
=
ctx
->
Attrs
().
Get
<
int
>
(
"x_num_col_dims"
);
int
y_num_col_dims
=
ctx
->
Attrs
().
Get
<
int
>
(
"y_num_col_dims"
);
VLOG
(
3
)
<<
"mul operator x.shape="
<<
x_dims
<<
" y.shape="
<<
y_dims
<<
" x_num_col_dims="
<<
x_num_col_dims
<<
" y_num_col_dims="
<<
y_num_col_dims
;
PADDLE_ENFORCE_NE
(
phi
::
product
(
y_dims
),
0
,
platform
::
errors
::
PreconditionNotMet
(
"The Input variable Y(%s) has not "
"been initialized. You may need to confirm "
"if you put exe.run(startup_program) "
"after optimizer.minimize function."
,
ctx
->
Inputs
(
"Y"
).
front
()));
PADDLE_ENFORCE_GT
(
x_dims
.
size
(),
x_num_col_dims
,
platform
::
errors
::
InvalidArgument
(
"The input tensor X's dimensions of MulOp "
"should be larger than x_num_col_dims. But received X's "
"dimensions = %d, X's shape = [%s], x_num_col_dims = %d."
,
x_dims
.
size
(),
x_dims
,
x_num_col_dims
));
PADDLE_ENFORCE_GT
(
y_dims
.
size
(),
y_num_col_dims
,
platform
::
errors
::
InvalidArgument
(
"The input tensor Y's dimensions of MulOp "
"should be larger than y_num_col_dims. But received Y's "
"dimensions = %d, Y's shape = [%s], y_num_col_dims = %d."
,
y_dims
.
size
(),
y_dims
,
y_num_col_dims
));
auto
x_mat_dims
=
phi
::
flatten_to_2d
(
x_dims
,
x_num_col_dims
);
auto
y_mat_dims
=
phi
::
flatten_to_2d
(
y_dims
,
y_num_col_dims
);
PADDLE_ENFORCE_EQ
(
x_mat_dims
[
1
],
y_mat_dims
[
0
],
platform
::
errors
::
InvalidArgument
(
"After flatten the input tensor X and Y to 2-D dimensions matrix "
"X1 and Y1, the matrix X1's width must be equal with matrix Y1's "
"height. But received X's shape = [%s], X1's shape = [%s], X1's "
"width = %s; Y's shape = [%s], Y1's shape = [%s], Y1's height = "
"%s."
,
x_dims
,
x_mat_dims
,
x_mat_dims
[
1
],
y_dims
,
y_mat_dims
,
y_mat_dims
[
0
]));
std
::
vector
<
int64_t
>
output_dims
;
output_dims
.
reserve
(
static_cast
<
size_t
>
(
x_num_col_dims
+
y_dims
.
size
()
-
y_num_col_dims
));
for
(
int
i
=
0
;
i
<
x_num_col_dims
;
++
i
)
{
output_dims
.
push_back
(
x_dims
[
i
]);
}
for
(
int
i
=
y_num_col_dims
;
i
<
y_dims
.
size
();
++
i
)
{
output_dims
.
push_back
(
y_dims
[
i
]);
}
ctx
->
SetOutputDim
(
"Out"
,
phi
::
make_ddim
(
output_dims
));
ctx
->
ShareLoD
(
"X"
,
/*->*/
"Out"
);
}
framework
::
OpKernelType
GetExpectedKernelType
(
const
framework
::
ExecutionContext
&
ctx
)
const
{
framework
::
LibraryType
library
=
framework
::
LibraryType
::
kPlain
;
...
...
@@ -225,25 +163,6 @@ class MulGradOp : public framework::OperatorWithKernel {
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
OP_INOUT_CHECK
(
ctx
->
HasInput
(
"X"
),
"Input"
,
"X"
,
"mul"
);
OP_INOUT_CHECK
(
ctx
->
HasInput
(
"Y"
),
"Input"
,
"Y"
,
"mul"
);
OP_INOUT_CHECK
(
ctx
->
HasInput
(
framework
::
GradVarName
(
"Out"
)),
"Input"
,
"Out@GRAD"
,
"mul"
);
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
auto
y_dims
=
ctx
->
GetInputDim
(
"Y"
);
auto
x_grad_name
=
framework
::
GradVarName
(
"X"
);
auto
y_grad_name
=
framework
::
GradVarName
(
"Y"
);
if
(
ctx
->
HasOutput
(
x_grad_name
))
{
ctx
->
SetOutputDim
(
x_grad_name
,
x_dims
);
}
if
(
ctx
->
HasOutput
(
y_grad_name
))
{
ctx
->
SetOutputDim
(
y_grad_name
,
y_dims
);
}
}
framework
::
OpKernelType
GetExpectedKernelType
(
const
framework
::
ExecutionContext
&
ctx
)
const
{
framework
::
LibraryType
library
=
framework
::
LibraryType
::
kPlain
;
...
...
@@ -348,12 +267,18 @@ class MulDoubleGradMaker : public framework::SingleGradOpMaker<T> {
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
DECLARE_INFER_SHAPE_FUNCTOR
(
mul
,
MulInferShapeFunctor
,
PD_INFER_META
(
phi
::
MatmulWithFlattenInferMeta
));
REGISTER_OPERATOR
(
mul
,
ops
::
MulOp
,
ops
::
MulOpMaker
,
ops
::
MulOpInferVarType
,
ops
::
MulOpGradMaker
<
paddle
::
framework
::
OpDesc
>
,
ops
::
MulOpGradMaker
<
paddle
::
imperative
::
OpBase
>
);
ops
::
MulOpGradMaker
<
paddle
::
imperative
::
OpBase
>
,
MulInferShapeFunctor
);
DECLARE_INFER_SHAPE_FUNCTOR
(
mul_grad
,
MulGradInferShapeFunctor
,
PD_INFER_META
(
phi
::
GeneralBinaryGradInferMeta
));
REGISTER_OPERATOR
(
mul_grad
,
ops
::
MulGradOp
,
ops
::
MulDoubleGradMaker
<
paddle
::
framework
::
OpDesc
>
,
ops
::
MulDoubleGradMaker
<
paddle
::
imperative
::
OpBase
>
);
ops
::
MulDoubleGradMaker
<
paddle
::
imperative
::
OpBase
>
,
MulGradInferShapeFunctor
);
REGISTER_OPERATOR
(
mul_grad_grad
,
ops
::
MulDoubleGradOp
);
paddle/phi/infermeta/binary.cc
浏览文件 @
609077e9
...
...
@@ -1267,6 +1267,81 @@ void MatmulInferMeta(const MetaTensor& x,
out
->
set_layout
(
x
.
layout
());
}
void
MatmulWithFlattenInferMeta
(
const
MetaTensor
&
x
,
const
MetaTensor
&
y
,
int
x_num_col_dims
,
int
y_num_col_dims
,
MetaTensor
*
out
)
{
auto
x_dims
=
x
.
dims
();
auto
y_dims
=
y
.
dims
();
VLOG
(
3
)
<<
"mul operator x.shape="
<<
x_dims
<<
" y.shape="
<<
y_dims
<<
" x_num_col_dims="
<<
x_num_col_dims
<<
" y_num_col_dims="
<<
y_num_col_dims
;
PADDLE_ENFORCE_NE
(
phi
::
product
(
y_dims
),
0
,
phi
::
errors
::
PreconditionNotMet
(
"The Input variable Y has not "
"been initialized. You may need to confirm "
"if you put exe.run(startup_program) "
"after optimizer.minimize function."
));
PADDLE_ENFORCE_GT
(
x_dims
.
size
(),
x_num_col_dims
,
phi
::
errors
::
InvalidArgument
(
"The input tensor X's dimensions of MulOp "
"should be larger than x_num_col_dims. But received X's "
"dimensions = %d, X's shape = [%s], x_num_col_dims = %d."
,
x_dims
.
size
(),
x_dims
,
x_num_col_dims
));
PADDLE_ENFORCE_GT
(
y_dims
.
size
(),
y_num_col_dims
,
phi
::
errors
::
InvalidArgument
(
"The input tensor Y's dimensions of MulOp "
"should be larger than y_num_col_dims. But received Y's "
"dimensions = %d, Y's shape = [%s], y_num_col_dims = %d."
,
y_dims
.
size
(),
y_dims
,
y_num_col_dims
));
auto
x_mat_dims
=
phi
::
flatten_to_2d
(
x_dims
,
x_num_col_dims
);
auto
y_mat_dims
=
phi
::
flatten_to_2d
(
y_dims
,
y_num_col_dims
);
PADDLE_ENFORCE_EQ
(
x_mat_dims
[
1
],
y_mat_dims
[
0
],
phi
::
errors
::
InvalidArgument
(
"After flatten the input tensor X and Y to 2-D dimensions matrix "
"X1 and Y1, the matrix X1's width must be equal with matrix Y1's "
"height. But received X's shape = [%s], X1's shape = [%s], X1's "
"width = %s; Y's shape = [%s], Y1's shape = [%s], Y1's height = "
"%s."
,
x_dims
,
x_mat_dims
,
x_mat_dims
[
1
],
y_dims
,
y_mat_dims
,
y_mat_dims
[
0
]));
std
::
vector
<
int64_t
>
output_dims
;
output_dims
.
reserve
(
static_cast
<
size_t
>
(
x_num_col_dims
+
y_dims
.
size
()
-
y_num_col_dims
));
for
(
int
i
=
0
;
i
<
x_num_col_dims
;
++
i
)
{
output_dims
.
push_back
(
x_dims
[
i
]);
}
for
(
int
i
=
y_num_col_dims
;
i
<
y_dims
.
size
();
++
i
)
{
output_dims
.
push_back
(
y_dims
[
i
]);
}
out
->
set_dims
(
phi
::
make_ddim
(
output_dims
));
out
->
set_dtype
(
x
.
dtype
());
out
->
share_lod
(
x
);
}
void
MvInferMeta
(
const
MetaTensor
&
x
,
const
MetaTensor
&
vec
,
MetaTensor
*
out
)
{
auto
dim_x
=
x
.
dims
();
auto
dim_vec
=
vec
.
dims
();
...
...
paddle/phi/infermeta/binary.h
浏览文件 @
609077e9
...
...
@@ -186,6 +186,12 @@ void MatmulInferMeta(const MetaTensor& x,
bool
trans_y
,
MetaTensor
*
out
);
void
MatmulWithFlattenInferMeta
(
const
MetaTensor
&
x
,
const
MetaTensor
&
y
,
int
x_num_col_dims
,
int
y_num_col_dims
,
MetaTensor
*
out
);
void
MvInferMeta
(
const
MetaTensor
&
x
,
const
MetaTensor
&
vec
,
MetaTensor
*
out
);
void
PReluInferMeta
(
const
MetaTensor
&
x
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录