Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
59e5c49f
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看板
未验证
提交
59e5c49f
编写于
3月 16, 2022
作者:
C
Chen Weihang
提交者:
GitHub
3月 16, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
move gather infershape (#40594)
上级
ec6b8fbd
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
68 addition
and
59 deletion
+68
-59
paddle/fluid/operators/gather_op.cc
paddle/fluid/operators/gather_op.cc
+13
-59
paddle/phi/infermeta/binary.cc
paddle/phi/infermeta/binary.cc
+49
-0
paddle/phi/infermeta/binary.h
paddle/phi/infermeta/binary.h
+6
-0
未找到文件。
paddle/fluid/operators/gather_op.cc
浏览文件 @
59e5c49f
...
...
@@ -15,9 +15,14 @@ limitations under the License. */
#include <memory>
#include <string>
#include <vector>
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/op_version_registry.h"
#include "paddle/phi/core/ddim.h"
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/backward.h"
#include "paddle/phi/infermeta/binary.h"
namespace
paddle
{
namespace
operators
{
...
...
@@ -26,58 +31,6 @@ class GatherOp : public framework::OperatorWithKernel {
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
PADDLE_ENFORCE_EQ
(
ctx
->
HasInput
(
"X"
),
true
,
platform
::
errors
::
InvalidArgument
(
"Input(X) of GatherOp should not be null."
));
PADDLE_ENFORCE_EQ
(
ctx
->
HasInput
(
"Index"
),
true
,
platform
::
errors
::
InvalidArgument
(
"Input(Index) of GatherOp should not be null."
));
PADDLE_ENFORCE_EQ
(
ctx
->
HasOutput
(
"Out"
),
true
,
platform
::
errors
::
InvalidArgument
(
"Output(Out) of GatherOp should not be null."
));
auto
index_dims
=
ctx
->
GetInputDim
(
"Index"
);
if
(
index_dims
.
size
()
==
2
)
{
PADDLE_ENFORCE_EQ
(
index_dims
[
1
],
1
,
platform
::
errors
::
InvalidArgument
(
"The last dim of index should be 1 when it is 2D, but we get %d"
,
index_dims
[
1
]));
}
else
{
PADDLE_ENFORCE_EQ
(
index_dims
.
size
(),
1
,
platform
::
errors
::
InvalidArgument
(
"The index should be 1D, when it is not 2D, but we get %d"
,
index_dims
.
size
()));
}
auto
axis
=
ctx
->
Attrs
().
Get
<
int
>
(
"axis"
);
auto
input_dim
=
ctx
->
GetInputDim
(
"X"
);
if
(
ctx
->
HasInput
(
"Axis"
)
||
axis
==
0
)
{
// if HasInput("Axis"), we can not obtain correct shape of output
int
batch_size
=
index_dims
[
0
];
framework
::
DDim
output_dims
(
input_dim
);
output_dims
[
0
]
=
batch_size
;
ctx
->
SetOutputDim
(
"Out"
,
output_dims
);
ctx
->
ShareLoD
(
"X"
,
/*->*/
"Out"
);
}
else
{
int
index_size
=
index_dims
[
0
];
std
::
vector
<
int
>
out_dim_vec
;
for
(
int
i
=
0
;
i
<
axis
;
i
++
)
{
out_dim_vec
.
push_back
(
input_dim
[
i
]);
}
out_dim_vec
.
push_back
(
index_size
);
for
(
int
i
=
axis
+
1
;
i
<
input_dim
.
size
();
i
++
)
{
out_dim_vec
.
push_back
(
input_dim
[
i
]);
}
auto
output_dims
=
phi
::
make_ddim
(
out_dim_vec
);
ctx
->
SetOutputDim
(
"Out"
,
output_dims
);
ctx
->
ShareLoD
(
"X"
,
/*->*/
"Out"
);
}
}
protected:
framework
::
OpKernelType
GetExpectedKernelType
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
...
...
@@ -100,11 +53,6 @@ class GatherGradOp : public framework::OperatorWithKernel {
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
ctx
->
SetOutputDim
(
framework
::
GradVarName
(
"X"
),
ctx
->
GetInputDim
(
"X"
));
ctx
->
ShareLoD
(
"X"
,
/*-->*/
framework
::
GradVarName
(
"X"
));
}
protected:
framework
::
OpKernelType
GetExpectedKernelType
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
...
...
@@ -193,11 +141,17 @@ DECLARE_NO_NEED_BUFFER_VARS_INFERER(GatherGradNoNeedBufferVarInferer, "X");
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
DECLARE_INFER_SHAPE_FUNCTOR
(
gather
,
GatherInferShapeFunctor
,
PD_INFER_META
(
phi
::
GatherInferMeta
));
REGISTER_OPERATOR
(
gather
,
ops
::
GatherOp
,
ops
::
GatherOpMaker
,
ops
::
GatherGradOpMaker
<
paddle
::
framework
::
OpDesc
>
,
ops
::
GatherGradOpMaker
<
paddle
::
imperative
::
OpBase
>
);
ops
::
GatherGradOpMaker
<
paddle
::
imperative
::
OpBase
>
,
GatherInferShapeFunctor
);
DECLARE_INFER_SHAPE_FUNCTOR
(
gather_grad
,
GatherGradInferShapeFunctor
,
PD_INFER_META
(
phi
::
GeneralUnaryGradInferMeta
));
REGISTER_OPERATOR
(
gather_grad
,
ops
::
GatherGradOp
,
ops
::
GatherGradNoNeedBufferVarInferer
);
ops
::
GatherGradNoNeedBufferVarInferer
,
GatherGradInferShapeFunctor
);
REGISTER_OP_VERSION
(
gather
)
.
AddCheckpoint
(
R"ROC(upgrad gather, add a new input [Axis])ROC"
,
...
...
paddle/phi/infermeta/binary.cc
浏览文件 @
59e5c49f
...
...
@@ -431,6 +431,55 @@ void ElementwiseRawInferMeta(const MetaTensor& x,
out
->
share_lod
(
x
);
}
void
GatherInferMeta
(
const
MetaTensor
&
x
,
const
MetaTensor
&
index
,
const
Scalar
&
axis
,
MetaTensor
*
out
)
{
auto
index_dims
=
index
.
dims
();
if
(
index_dims
.
size
()
==
2
)
{
PADDLE_ENFORCE_EQ
(
index_dims
[
1
],
1
,
phi
::
errors
::
InvalidArgument
(
"The last dim of index should be 1 when it is 2D, but we get %d"
,
index_dims
[
1
]));
}
else
{
PADDLE_ENFORCE_EQ
(
index_dims
.
size
(),
1
,
phi
::
errors
::
InvalidArgument
(
"The index should be 1D, when it is not 2D, but we get %d"
,
index_dims
.
size
()));
}
auto
input_dim
=
x
.
dims
();
auto
axis_v
=
axis
.
to
<
int
>
();
if
(
axis
.
FromTensor
()
||
axis_v
==
0
)
{
// if axis.FromTensor(), we can not obtain correct shape of output
int
batch_size
=
index_dims
[
0
];
phi
::
DDim
output_dims
(
input_dim
);
output_dims
[
0
]
=
batch_size
;
out
->
set_dims
(
output_dims
);
out
->
set_dtype
(
x
.
dtype
());
out
->
share_lod
(
x
);
}
else
{
int
index_size
=
index_dims
[
0
];
std
::
vector
<
int
>
out_dim_vec
;
for
(
int
i
=
0
;
i
<
axis_v
;
i
++
)
{
out_dim_vec
.
push_back
(
input_dim
[
i
]);
}
out_dim_vec
.
push_back
(
index_size
);
for
(
int
i
=
axis_v
+
1
;
i
<
input_dim
.
size
();
i
++
)
{
out_dim_vec
.
push_back
(
input_dim
[
i
]);
}
auto
output_dims
=
phi
::
make_ddim
(
out_dim_vec
);
out
->
set_dims
(
output_dims
);
out
->
set_dtype
(
x
.
dtype
());
out
->
share_lod
(
x
);
}
}
void
GatherNdInferMeta
(
const
MetaTensor
&
x
,
const
MetaTensor
&
index
,
MetaTensor
*
out
)
{
...
...
paddle/phi/infermeta/binary.h
浏览文件 @
59e5c49f
...
...
@@ -14,6 +14,7 @@ limitations under the License. */
#pragma once
#include "paddle/phi/common/scalar.h"
#include "paddle/phi/core/meta_tensor.h"
namespace
phi
{
...
...
@@ -81,6 +82,11 @@ void ElementwiseRawInferMeta(const MetaTensor& x_meta,
int
axis
,
MetaTensor
*
out
);
void
GatherInferMeta
(
const
MetaTensor
&
x
,
const
MetaTensor
&
index
,
const
Scalar
&
axis
,
MetaTensor
*
out
);
void
GatherNdInferMeta
(
const
MetaTensor
&
x
,
const
MetaTensor
&
index
,
MetaTensor
*
out
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录