Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
3ebd5af8
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
3ebd5af8
编写于
4月 04, 2023
作者:
C
cyberslack_lee
提交者:
GitHub
4月 04, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
support auto generate for bce_loss (#52231)
* bce_loss * fix error * fix * fix * fix * reslove confilict
上级
cf361716
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
29 addition
and
205 deletion
+29
-205
paddle/fluid/operators/bce_loss_op.cc
paddle/fluid/operators/bce_loss_op.cc
+0
-158
paddle/phi/api/yaml/backward.yaml
paddle/phi/api/yaml/backward.yaml
+11
-0
paddle/phi/api/yaml/legacy_backward.yaml
paddle/phi/api/yaml/legacy_backward.yaml
+0
-11
paddle/phi/api/yaml/legacy_ops.yaml
paddle/phi/api/yaml/legacy_ops.yaml
+0
-9
paddle/phi/api/yaml/op_compat.yaml
paddle/phi/api/yaml/op_compat.yaml
+7
-0
paddle/phi/api/yaml/ops.yaml
paddle/phi/api/yaml/ops.yaml
+11
-0
paddle/phi/ops/compat/bce_loss_sig.cc
paddle/phi/ops/compat/bce_loss_sig.cc
+0
-27
未找到文件。
paddle/fluid/operators/bce_loss_op.cc
已删除
100644 → 0
浏览文件 @
cf361716
/* Copyright (c) 2020 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
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
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/phi/infermeta/binary.h"
namespace
paddle
{
namespace
operators
{
class
BCELossOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
phi
::
KernelKey
GetExpectedKernelType
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
return
phi
::
KernelKey
(
OperatorWithKernel
::
IndicateVarDataType
(
ctx
,
"X"
),
ctx
.
device_context
().
GetPlace
());
}
};
class
BCELossGradOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
OP_INOUT_CHECK
(
ctx
->
HasInput
(
"X"
),
"Input"
,
"X"
,
"BCELossGrad"
);
OP_INOUT_CHECK
(
ctx
->
HasInput
(
"Label"
),
"Input"
,
"Label"
,
"BCELossGrad"
);
OP_INOUT_CHECK
(
ctx
->
HasInput
(
framework
::
GradVarName
(
"Out"
)),
"Input"
,
framework
::
GradVarName
(
"Out"
),
"BCELossGrad"
);
OP_INOUT_CHECK
(
ctx
->
HasOutput
(
framework
::
GradVarName
(
"X"
)),
"Output"
,
framework
::
GradVarName
(
"X"
),
"BCELossGrad"
);
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
auto
labels_dims
=
ctx
->
GetInputDim
(
"Label"
);
auto
dout_dims
=
ctx
->
GetInputDim
(
framework
::
GradVarName
(
"Out"
));
bool
check
=
true
;
if
((
!
ctx
->
IsRuntime
())
&&
(
phi
::
product
(
x_dims
)
<=
0
||
phi
::
product
(
labels_dims
)
<=
0
))
{
check
=
false
;
}
if
(
check
)
{
PADDLE_ENFORCE_EQ
(
x_dims
,
labels_dims
,
platform
::
errors
::
InvalidArgument
(
"Input(X) and Input(Label) shall have the same "
"shape. But received: the shape of Input(X) is "
"[%s], the shape of Input(Label) is [%s]."
,
x_dims
,
labels_dims
));
PADDLE_ENFORCE_EQ
(
x_dims
,
dout_dims
,
platform
::
errors
::
InvalidArgument
(
"Input(X) and Input(Out@Grad) shall have the same "
"shape. But received: the shape of Input(X) is "
"[%s], the shape of Input(Out@Grad) is [%s]."
,
x_dims
,
dout_dims
));
}
ctx
->
SetOutputDim
(
framework
::
GradVarName
(
"X"
),
x_dims
);
ctx
->
ShareLoD
(
"X"
,
framework
::
GradVarName
(
"X"
));
}
protected:
phi
::
KernelKey
GetExpectedKernelType
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
return
phi
::
KernelKey
(
OperatorWithKernel
::
IndicateVarDataType
(
ctx
,
"X"
),
ctx
.
device_context
().
GetPlace
());
}
};
class
BCELossOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
void
Make
()
override
{
AddInput
(
"X"
,
"(Tensor, default Tensor<float>), the input is a tensor of logits"
"computed by the previous operator, which is always the result of"
"a sigmoid operator. Input must between in 0 and 1."
);
AddInput
(
"Label"
,
"(Tensor, default Tensor<float>), have same shape with input"
"label should between in 0 and 1."
);
AddOutput
(
"Out"
,
"(Tensor, default Tensor<float>), have same shape with"
"input"
);
AddComment
(
R"DOC(
BinaryCrossEntropy operator.
This measures the element-wise probability error in classification tasks
in which each class is independent.
The logitstic loss is given as follows:
$$loss = -Label * \log(X) - (1 - Label) * \log(1 - X)$$
)DOC"
);
}
};
template
<
typename
T
>
class
BCELossGradOpMaker
:
public
framework
::
SingleGradOpMaker
<
T
>
{
public:
using
framework
::
SingleGradOpMaker
<
T
>::
SingleGradOpMaker
;
protected:
void
Apply
(
GradOpPtr
<
T
>
op
)
const
override
{
op
->
SetType
(
"bce_loss_grad"
);
op
->
SetInput
(
"X"
,
this
->
Input
(
"X"
));
op
->
SetInput
(
"Label"
,
this
->
Input
(
"Label"
));
op
->
SetInput
(
framework
::
GradVarName
(
"Out"
),
this
->
OutputGrad
(
"Out"
));
op
->
SetOutput
(
framework
::
GradVarName
(
"X"
),
this
->
InputGrad
(
"X"
));
}
};
DECLARE_INPLACE_OP_INFERER
(
BCELossInplaceInferer
,
{
"X"
,
"Out"
});
DECLARE_INPLACE_OP_INFERER
(
BCELossGradInplaceInferer
,
{
framework
::
GradVarName
(
"Out"
),
framework
::
GradVarName
(
"X"
)});
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
DECLARE_INFER_SHAPE_FUNCTOR
(
bce_loss
,
BCELossInferShapeFunctor
,
PD_INFER_META
(
phi
::
BCELossInferMeta
));
REGISTER_OPERATOR
(
bce_loss
,
ops
::
BCELossOp
,
ops
::
BCELossOpMaker
,
ops
::
BCELossGradOpMaker
<
paddle
::
framework
::
OpDesc
>
,
ops
::
BCELossGradOpMaker
<
paddle
::
imperative
::
OpBase
>
,
ops
::
BCELossInplaceInferer
,
BCELossInferShapeFunctor
);
REGISTER_OPERATOR
(
bce_loss_grad
,
ops
::
BCELossGradOp
,
ops
::
BCELossGradInplaceInferer
);
paddle/phi/api/yaml/backward.yaml
浏览文件 @
3ebd5af8
...
...
@@ -121,6 +121,17 @@
func
:
atanh_grad
inplace
:
(out_grad -> x_grad)
-
backward_op
:
bce_loss_grad
forward
:
bce_loss (Tensor input, Tensor label) -> Tensor(out)
args
:
(Tensor input, Tensor label, Tensor out_grad)
output
:
Tensor(input_grad)
infer_meta
:
func
:
UnchangedInferMeta
param
:
[
input
]
kernel
:
func
:
bce_loss_grad
inplace
:
(out_grad -> input_grad)
-
backward_op
:
bicubic_interp_grad
forward
:
bicubic_interp (Tensor x, Tensor out_size, Tensor[] size_tensor, Tensor scale_tensor, str data_layout="NCHW", int out_d=0, int out_h=0, int out_w=0, float[] scale={}, str interp_method="bilinear", bool align_corners=true, int align_mode=1) -> Tensor(output)
args
:
(Tensor x, Tensor out_size, Tensor[] size_tensor, Tensor scale_tensor, Tensor output_grad, str data_layout, int out_d, int out_h, int out_w, float[] scale, str interp_method, bool align_corners, int align_mode)
...
...
paddle/phi/api/yaml/legacy_backward.yaml
浏览文件 @
3ebd5af8
...
...
@@ -134,17 +134,6 @@
composite
:
batch_norm_grad(x, scale, bias, mean_out, variance_out, saved_mean, saved_variance, reserve_space, out_grad, momentum, epsilon, data_layout, is_test, use_global_stats, trainable_statistics)
backward
:
batch_norm_double_grad
-
backward_op
:
bce_loss_grad
forward
:
bce_loss (Tensor input, Tensor label) -> Tensor(out)
args
:
(Tensor input, Tensor label, Tensor out_grad)
output
:
Tensor(input_grad)
infer_meta
:
func
:
UnchangedInferMeta
param
:
[
input
]
kernel
:
func
:
bce_loss_grad
inplace
:
(out_grad -> input_grad)
-
backward_op
:
bilinear_tensor_product_grad
forward
:
bilinear_tensor_product (Tensor x, Tensor y, Tensor weight, Tensor bias) -> Tensor(out)
args
:
(Tensor x, Tensor y, Tensor weight, Tensor out_grad)
...
...
paddle/phi/api/yaml/legacy_ops.yaml
浏览文件 @
3ebd5af8
...
...
@@ -214,15 +214,6 @@
view
:
(mean -> mean_out), (variance -> variance_out)
backward
:
batch_norm_grad
-
op
:
bce_loss
args
:
(Tensor input, Tensor label)
output
:
Tensor
infer_meta
:
func
:
BCELossInferMeta
kernel
:
func
:
bce_loss
backward
:
bce_loss_grad
-
op
:
bilinear_tensor_product
args
:
(Tensor x, Tensor y, Tensor weight, Tensor bias)
output
:
Tensor
...
...
paddle/phi/api/yaml/op_compat.yaml
浏览文件 @
3ebd5af8
...
...
@@ -198,6 +198,13 @@
extra
:
attrs
:
[
bool use_mkldnn = false
,
bool fuse_with_relu = false
]
-
op
:
bce_loss
backward
:
bce_loss_grad
inputs
:
{
input
:
X
,
label
:
Label
}
outputs
:
out
:
Out
-
op
:
bernoulli
inputs
:
x
:
X
...
...
paddle/phi/api/yaml/ops.yaml
浏览文件 @
3ebd5af8
...
...
@@ -143,6 +143,17 @@
data_type
:
x
optional
:
ins_tag_weight
-
op
:
bce_loss
args
:
(Tensor input, Tensor label)
output
:
Tensor
infer_meta
:
func
:
BCELossInferMeta
kernel
:
func
:
bce_loss
data_type
:
input
inplace
:
(input -> out)
backward
:
bce_loss_grad
-
op
:
bernoulli
args
:
(Tensor x)
output
:
Tensor(out)
...
...
paddle/phi/ops/compat/bce_loss_sig.cc
已删除
100644 → 0
浏览文件 @
cf361716
// Copyright (c) 2022 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "paddle/phi/core/compat/op_utils.h"
namespace
phi
{
KernelSignature
BCELossGradOpArgumentMapping
(
const
ArgumentMappingContext
&
ctx
)
{
return
KernelSignature
(
"bce_loss_grad"
,
{
"X"
,
"Label"
,
"Out@GRAD"
},
{},
{
"X@GRAD"
});
}
}
// namespace phi
PD_REGISTER_ARG_MAPPING_FN
(
bce_loss_grad
,
phi
::
BCELossGradOpArgumentMapping
);
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录