Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
d35b5b58
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看板
未验证
提交
d35b5b58
编写于
3月 11, 2022
作者:
X
xiongkun
提交者:
GitHub
3月 11, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[phi] [infershape] transfer nll_loss infer shape into phi (#40375)
* transfer nll_loss infershape into phi
上级
9ebe7276
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
95 addition
and
72 deletion
+95
-72
paddle/fluid/operators/nll_loss_op.cc
paddle/fluid/operators/nll_loss_op.cc
+6
-72
paddle/phi/infermeta/ternary.cc
paddle/phi/infermeta/ternary.cc
+80
-0
paddle/phi/infermeta/ternary.h
paddle/phi/infermeta/ternary.h
+9
-0
未找到文件。
paddle/fluid/operators/nll_loss_op.cc
浏览文件 @
d35b5b58
...
...
@@ -14,7 +14,9 @@ limitations under the License. */
#include <memory>
#include <string>
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/phi/infermeta/ternary.h"
namespace
paddle
{
namespace
operators
{
...
...
@@ -23,77 +25,6 @@ class NLLLossOp : public framework::OperatorWithKernel {
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
OP_INOUT_CHECK
(
ctx
->
HasInput
(
"X"
),
"Input"
,
"X"
,
"NLLLoss"
);
OP_INOUT_CHECK
(
ctx
->
HasInput
(
"Label"
),
"Input"
,
"Label"
,
"NLLLoss"
);
OP_INOUT_CHECK
(
ctx
->
HasOutput
(
"Out"
),
"Output"
,
"Out"
,
"NLLLoss"
);
OP_INOUT_CHECK
(
ctx
->
HasOutput
(
"Total_weight"
),
"Output"
,
"Total_weight"
,
"NLLLoss"
);
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
auto
label_dims
=
ctx
->
GetInputDim
(
"Label"
);
auto
reduction
=
ctx
->
Attrs
().
Get
<
std
::
string
>
(
"reduction"
);
PADDLE_ENFORCE_EQ
(
x_dims
.
size
()
==
2
||
x_dims
.
size
()
==
4
,
true
,
platform
::
errors
::
InvalidArgument
(
"The tensor rank of Input(X) must be 2 or 4."
));
bool
contain_unknown_dim
=
phi
::
contain_unknown_dim
(
x_dims
)
||
phi
::
contain_unknown_dim
(
label_dims
);
bool
check
=
ctx
->
IsRuntime
()
||
!
contain_unknown_dim
;
if
(
check
)
{
PADDLE_ENFORCE_EQ
(
x_dims
[
0
],
label_dims
[
0
],
platform
::
errors
::
InvalidArgument
(
"ShapeError: Expected input batch_size to match label batch_size,"
"But received: the Input(x) batch_size is [%s], the Input(label) "
" batch_size is [%s]."
,
x_dims
[
0
],
label_dims
[
0
]));
if
(
ctx
->
HasInput
(
"Weight"
))
{
auto
w_dims
=
ctx
->
GetInputDim
(
"Weight"
);
PADDLE_ENFORCE_EQ
(
w_dims
.
size
(),
1
,
platform
::
errors
::
InvalidArgument
(
"Input(Weight) should be a 1D tensor."
));
PADDLE_ENFORCE_EQ
(
x_dims
[
1
],
w_dims
[
0
],
platform
::
errors
::
InvalidArgument
(
"Expected input tensor Weight's size should equal "
"to the first dimension of the input tensor X. But received "
"Weight's "
"size is %d, the first dimension of input X is %d"
,
w_dims
[
0
],
x_dims
[
1
]));
}
}
if
(
x_dims
.
size
()
==
2
)
{
if
(
reduction
==
"none"
)
{
ctx
->
SetOutputDim
(
"Out"
,
{
x_dims
[
0
]});
}
else
{
ctx
->
SetOutputDim
(
"Out"
,
{
1
});
}
}
else
if
(
x_dims
.
size
()
==
4
)
{
PADDLE_ENFORCE_EQ
(
label_dims
.
size
(),
3
,
platform
::
errors
::
InvalidArgument
(
"Expected Input(Lable) dimensions=3, received %d."
,
label_dims
.
size
()));
auto
input0
=
x_dims
[
0
];
auto
input2
=
x_dims
[
2
];
auto
input3
=
x_dims
[
3
];
auto
label0
=
label_dims
[
0
];
auto
label1
=
label_dims
[
1
];
auto
label2
=
label_dims
[
2
];
PADDLE_ENFORCE_EQ
(
input0
==
label0
&&
input2
==
label1
&&
input3
==
label2
,
true
,
platform
::
errors
::
InvalidArgument
(
"Input(X) tensor shape should "
"match to Input(Label) tensor "
"shape."
));
if
(
reduction
==
"none"
)
{
ctx
->
SetOutputDim
(
"Out"
,
{
x_dims
[
0
],
x_dims
[
2
],
x_dims
[
3
]});
}
else
{
ctx
->
SetOutputDim
(
"Out"
,
{
1
});
}
}
ctx
->
SetOutputDim
(
"Total_weight"
,
{
1
});
}
protected:
framework
::
OpKernelType
GetExpectedKernelType
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
...
...
@@ -259,8 +190,11 @@ class NLLLossGradMaker : public framework::SingleGradOpMaker<T> {
}
// namespace operators
}
// namespace paddle
DECLARE_INFER_SHAPE_FUNCTOR
(
nll_loss
,
NllLossRawInferShapeFunctor
,
PD_INFER_META
(
phi
::
NllLossRawInferMeta
));
namespace
ops
=
paddle
::
operators
;
REGISTER_OPERATOR
(
nll_loss
,
ops
::
NLLLossOp
,
ops
::
NLLLossOpMaker
,
ops
::
NLLLossGradMaker
<
paddle
::
framework
::
OpDesc
>
,
ops
::
NLLLossGradMaker
<
paddle
::
imperative
::
OpBase
>
);
ops
::
NLLLossGradMaker
<
paddle
::
imperative
::
OpBase
>
,
NllLossRawInferShapeFunctor
);
REGISTER_OPERATOR
(
nll_loss_grad
,
ops
::
NLLLossGradOp
);
paddle/phi/infermeta/ternary.cc
浏览文件 @
d35b5b58
...
...
@@ -89,6 +89,86 @@ void AddmmInferMeta(const MetaTensor& input,
out
->
set_dtype
(
input
.
dtype
());
}
void
NllLossRawInferMeta
(
const
MetaTensor
&
input
,
const
MetaTensor
&
label
,
paddle
::
optional
<
const
MetaTensor
&>
weight
,
int64_t
ignore_index
,
const
std
::
string
&
reduction
,
MetaTensor
*
out
,
MetaTensor
*
total_weight
,
MetaConfig
config
)
{
auto
x_dims
=
input
.
dims
();
auto
label_dims
=
label
.
dims
();
PADDLE_ENFORCE_EQ
(
x_dims
.
size
()
==
2
||
x_dims
.
size
()
==
4
,
true
,
phi
::
errors
::
InvalidArgument
(
"The tensor rank of Input(X) must be 2 or 4."
));
bool
contain_unknown_dim
=
phi
::
contain_unknown_dim
(
x_dims
)
||
phi
::
contain_unknown_dim
(
label_dims
);
bool
check
=
config
.
is_runtime
||
!
contain_unknown_dim
;
if
(
check
)
{
PADDLE_ENFORCE_EQ
(
x_dims
[
0
],
label_dims
[
0
],
phi
::
errors
::
InvalidArgument
(
"ShapeError: Expected input batch_size to match label batch_size,"
"But received: the Input(x) batch_size is [%s], the Input(label) "
" batch_size is [%s]."
,
x_dims
[
0
],
label_dims
[
0
]));
if
(
weight
.
get_ptr
()
!=
nullptr
)
{
auto
w_dims
=
weight
->
dims
();
PADDLE_ENFORCE_EQ
(
w_dims
.
size
(),
1
,
phi
::
errors
::
InvalidArgument
(
"Input(Weight) should be a 1D tensor."
));
PADDLE_ENFORCE_EQ
(
x_dims
[
1
],
w_dims
[
0
],
phi
::
errors
::
InvalidArgument
(
"Expected input tensor Weight's size should equal "
"to the first dimension of the input tensor X. But received "
"Weight's "
"size is %d, the first dimension of input X is %d"
,
w_dims
[
0
],
x_dims
[
1
]));
}
}
if
(
x_dims
.
size
()
==
2
)
{
if
(
reduction
==
"none"
)
{
out
->
set_dims
({
x_dims
[
0
]});
}
else
{
out
->
set_dims
({
1
});
}
}
else
if
(
x_dims
.
size
()
==
4
)
{
PADDLE_ENFORCE_EQ
(
label_dims
.
size
(),
3
,
phi
::
errors
::
InvalidArgument
(
"Expected Input(Lable) dimensions=3, received %d."
,
label_dims
.
size
()));
auto
input0
=
x_dims
[
0
];
auto
input2
=
x_dims
[
2
];
auto
input3
=
x_dims
[
3
];
auto
label0
=
label_dims
[
0
];
auto
label1
=
label_dims
[
1
];
auto
label2
=
label_dims
[
2
];
PADDLE_ENFORCE_EQ
(
input0
==
label0
&&
input2
==
label1
&&
input3
==
label2
,
true
,
phi
::
errors
::
InvalidArgument
(
"Input(X) tensor shape should "
"match to Input(Label) tensor "
"shape."
));
if
(
reduction
==
"none"
)
{
out
->
set_dims
({
x_dims
[
0
],
x_dims
[
2
],
x_dims
[
3
]});
}
else
{
out
->
set_dims
({
1
});
}
}
total_weight
->
set_dims
({
1
});
out
->
set_dtype
(
input
.
dtype
());
total_weight
->
set_dtype
(
input
.
dtype
());
}
void
ScatterInferMeta
(
const
MetaTensor
&
x
,
const
MetaTensor
&
index
,
const
MetaTensor
&
updates
,
...
...
paddle/phi/infermeta/ternary.h
浏览文件 @
d35b5b58
...
...
@@ -56,6 +56,15 @@ void ScatterInferMeta(const MetaTensor& x,
bool
overwrite
,
MetaTensor
*
out
);
void
NllLossRawInferMeta
(
const
MetaTensor
&
input
,
const
MetaTensor
&
label
,
paddle
::
optional
<
const
MetaTensor
&>
weight
,
int64_t
ignore_index
,
const
std
::
string
&
reduction
,
MetaTensor
*
out
,
MetaTensor
*
total_weight
,
MetaConfig
config
=
MetaConfig
());
void
ScatterNdAddInferMeta
(
const
MetaTensor
&
x
,
const
MetaTensor
&
index
,
const
MetaTensor
&
updates
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录