Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
6fc5d88a
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看板
未验证
提交
6fc5d88a
编写于
2月 24, 2022
作者:
L
Linjie Chen
提交者:
GitHub
2月 24, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[phi] move bce_loss to phi (#39868)
* move bce_loss to phi * refine PADDLE_ENFORCE * revert PADDLE_ENFORCE * fix ci
上级
eb4ad509
变更
13
隐藏空白更改
内联
并排
Showing
13 changed file
with
364 addition
and
239 deletion
+364
-239
paddle/fluid/operators/bce_loss_op.cc
paddle/fluid/operators/bce_loss_op.cc
+8
-44
paddle/fluid/operators/bce_loss_op.cu
paddle/fluid/operators/bce_loss_op.cu
+0
-109
paddle/fluid/operators/bce_loss_op.h
paddle/fluid/operators/bce_loss_op.h
+0
-85
paddle/fluid/operators/bce_loss_op_npu.cc
paddle/fluid/operators/bce_loss_op_npu.cc
+1
-1
paddle/phi/infermeta/binary.cc
paddle/phi/infermeta/binary.cc
+38
-0
paddle/phi/infermeta/binary.h
paddle/phi/infermeta/binary.h
+4
-0
paddle/phi/kernels/bce_loss_grad_kernel.h
paddle/phi/kernels/bce_loss_grad_kernel.h
+28
-0
paddle/phi/kernels/bce_loss_kernel.h
paddle/phi/kernels/bce_loss_kernel.h
+27
-0
paddle/phi/kernels/cpu/bce_loss_grad_kernel.cc
paddle/phi/kernels/cpu/bce_loss_grad_kernel.cc
+47
-0
paddle/phi/kernels/cpu/bce_loss_kernel.cc
paddle/phi/kernels/cpu/bce_loss_kernel.cc
+59
-0
paddle/phi/kernels/gpu/bce_loss_grad_kernel.cu
paddle/phi/kernels/gpu/bce_loss_grad_kernel.cu
+59
-0
paddle/phi/kernels/gpu/bce_loss_kernel.cu
paddle/phi/kernels/gpu/bce_loss_kernel.cu
+64
-0
paddle/phi/ops/compat/bce_loss_sig.cc
paddle/phi/ops/compat/bce_loss_sig.cc
+29
-0
未找到文件。
paddle/fluid/operators/bce_loss_op.cc
浏览文件 @
6fc5d88a
...
...
@@ -12,11 +12,14 @@ 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/fluid/operators/bce_loss_op.h"
#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
{
...
...
@@ -26,41 +29,6 @@ class BCELossOp : public framework::OperatorWithKernel {
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
OP_INOUT_CHECK
(
ctx
->
HasInput
(
"X"
),
"Input"
,
"X"
,
"BCELoss"
);
OP_INOUT_CHECK
(
ctx
->
HasInput
(
"Label"
),
"Input"
,
"Label"
,
"BCELoss"
);
OP_INOUT_CHECK
(
ctx
->
HasOutput
(
"Out"
),
"Output"
,
"Out"
,
"BCELoss"
);
auto
x_dims
=
ctx
->
GetInputDim
(
"X"
);
auto
labels_dims
=
ctx
->
GetInputDim
(
"Label"
);
int
rank
=
x_dims
.
size
();
PADDLE_ENFORCE_EQ
(
rank
,
labels_dims
.
size
(),
platform
::
errors
::
InvalidArgument
(
"Input(X) and Input(Label) shall have the same rank."
"But received: the rank of Input(X) is [%d], "
"the rank of Input(Label) is [%d]."
,
rank
,
labels_dims
.
size
()));
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
));
}
ctx
->
ShareDim
(
"X"
,
"Out"
);
ctx
->
ShareLoD
(
"X"
,
"Out"
);
}
protected:
framework
::
OpKernelType
GetExpectedKernelType
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
...
...
@@ -170,16 +138,12 @@ DECLARE_INPLACE_OP_INFERER(BCELossGradInplaceInferer,
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
DELCARE_INFER_SHAPE_FUNCTOR
(
bce_loss
,
BCELossInferShapeFunctor
,
PT_INFER_META
(
phi
::
BCELossInferMeta
));
REGISTER_OPERATOR
(
bce_loss
,
ops
::
BCELossOp
,
ops
::
BCELossOpMaker
,
ops
::
BCELossGradOpMaker
<
paddle
::
framework
::
OpDesc
>
,
ops
::
BCELossGradOpMaker
<
paddle
::
imperative
::
OpBase
>
,
ops
::
BCELossInplaceInferer
);
ops
::
BCELossInplaceInferer
,
BCELossInferShapeFunctor
);
REGISTER_OPERATOR
(
bce_loss_grad
,
ops
::
BCELossGradOp
,
ops
::
BCELossGradInplaceInferer
);
REGISTER_OP_CPU_KERNEL
(
bce_loss
,
ops
::
BCELossOpKernel
<
paddle
::
platform
::
CPUDeviceContext
,
float
>
,
ops
::
BCELossOpKernel
<
paddle
::
platform
::
CPUDeviceContext
,
double
>
);
REGISTER_OP_CPU_KERNEL
(
bce_loss_grad
,
ops
::
BCELossGradOpKernel
<
paddle
::
platform
::
CPUDeviceContext
,
float
>
,
ops
::
BCELossGradOpKernel
<
paddle
::
platform
::
CPUDeviceContext
,
double
>
);
paddle/fluid/operators/bce_loss_op.cu
已删除
100644 → 0
浏览文件 @
eb4ad509
/* 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 <algorithm>
#include "paddle/fluid/operators/bce_loss_op.h"
#include "paddle/fluid/operators/elementwise/elementwise_op_impl.cu.h"
#include "paddle/fluid/operators/math.h"
#include "paddle/fluid/platform/device/gpu/gpu_launch_config.h"
#include "paddle/fluid/platform/device/gpu/gpu_primitives.h"
#include "paddle/phi/core/hostdevice.h"
namespace
paddle
{
namespace
operators
{
template
<
typename
T
>
struct
BCELossFunctor
{
T
one
;
T
neg_100
;
HOSTDEVICE
inline
BCELossFunctor
()
{
one
=
static_cast
<
T
>
(
1.0
f
);
neg_100
=
static_cast
<
T
>
(
-
100.
);
}
HOSTDEVICE
inline
T
operator
()(
const
T
x
,
const
T
label
)
const
{
PADDLE_ENFORCE
(
(
x
>=
static_cast
<
T
>
(
0
))
&&
(
x
<=
one
),
"Input is expected to be within the interval [0, 1], but recieved %f."
,
x
);
T
term1
=
max
(
real_log
(
x
),
neg_100
);
T
term2
=
max
(
real_log
(
one
-
x
),
neg_100
);
return
(((
label
-
one
)
*
term2
)
-
(
label
*
term1
));
}
};
template
<
typename
T
>
struct
BCELossGradFunctor
{
T
one
;
T
eps
;
HOSTDEVICE
inline
BCELossGradFunctor
()
{
one
=
static_cast
<
T
>
(
1.0
f
);
eps
=
static_cast
<
T
>
(
1e-12
);
}
HOSTDEVICE
inline
T
operator
()(
const
T
x
,
const
T
label
,
const
T
dout
)
const
{
T
term1
=
max
((
one
-
x
)
*
x
,
eps
);
return
(
dout
*
(
x
-
label
)
/
term1
);
}
};
using
Tensor
=
framework
::
Tensor
;
template
<
typename
DeviceContext
,
typename
T
>
class
BCELossCUDAKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
*
x
=
ctx
.
Input
<
Tensor
>
(
"X"
);
auto
*
labels
=
ctx
.
Input
<
Tensor
>
(
"Label"
);
auto
*
out
=
ctx
.
Output
<
Tensor
>
(
"Out"
);
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
std
::
vector
<
const
framework
::
Tensor
*>
ins
=
{
x
,
labels
};
std
::
vector
<
framework
::
Tensor
*>
outs
=
{
out
};
auto
&
dev_ctx
=
ctx
.
template
device_context
<
platform
::
CUDADeviceContext
>();
auto
functor
=
BCELossFunctor
<
T
>
();
paddle
::
operators
::
LaunchSameDimsElementwiseCudaKernel
<
T
>
(
dev_ctx
,
ins
,
&
outs
,
functor
);
}
};
template
<
typename
DeviceContext
,
typename
T
>
class
BCELossGradCUDAKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
*
x
=
ctx
.
Input
<
Tensor
>
(
"X"
);
auto
*
labels
=
ctx
.
Input
<
Tensor
>
(
"Label"
);
auto
*
dout
=
ctx
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
dx
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
dx
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
std
::
vector
<
const
framework
::
Tensor
*>
ins
=
{
x
,
labels
,
dout
};
std
::
vector
<
framework
::
Tensor
*>
outs
=
{
dx
};
auto
&
dev_ctx
=
ctx
.
template
device_context
<
platform
::
CUDADeviceContext
>();
auto
functor
=
BCELossGradFunctor
<
T
>
();
paddle
::
operators
::
LaunchSameDimsElementwiseCudaKernel
<
T
>
(
dev_ctx
,
ins
,
&
outs
,
functor
);
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_CUDA_KERNEL
(
bce_loss
,
ops
::
BCELossCUDAKernel
<
paddle
::
platform
::
CUDADeviceContext
,
float
>
,
ops
::
BCELossCUDAKernel
<
paddle
::
platform
::
CUDADeviceContext
,
double
>
);
REGISTER_OP_CUDA_KERNEL
(
bce_loss_grad
,
ops
::
BCELossGradCUDAKernel
<
paddle
::
platform
::
CUDADeviceContext
,
float
>
,
ops
::
BCELossGradCUDAKernel
<
paddle
::
platform
::
CUDADeviceContext
,
double
>
);
paddle/fluid/operators/bce_loss_op.h
已删除
100644 → 0
浏览文件 @
eb4ad509
/* 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. */
#pragma once
#include <algorithm> // for max
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/math.h"
namespace
paddle
{
namespace
operators
{
using
Tensor
=
framework
::
Tensor
;
template
<
typename
DeviceContext
,
typename
T
>
class
BCELossOpKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
*
x
=
ctx
.
Input
<
Tensor
>
(
"X"
);
auto
*
labels
=
ctx
.
Input
<
Tensor
>
(
"Label"
);
auto
*
out
=
ctx
.
Output
<
Tensor
>
(
"Out"
);
auto
x_data
=
x
->
data
<
T
>
();
auto
label_data
=
labels
->
data
<
T
>
();
auto
out_data
=
out
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
auto
x_numel
=
x
->
numel
();
// out = -(label * ln(x) + (1 - label) * ln(1 - x)) = (label - 1) * ln(1 -
// x) - label * ln(x)
for
(
int64_t
i
=
0
;
i
<
x_numel
;
++
i
)
{
PADDLE_ENFORCE_GE
(
x_data
[
i
],
static_cast
<
T
>
(
0
),
platform
::
errors
::
InvalidArgument
(
"Illegal input, input must be greater than or equal to 0"
));
PADDLE_ENFORCE_LE
(
x_data
[
i
],
static_cast
<
T
>
(
1
),
platform
::
errors
::
InvalidArgument
(
"Illegal input, input must be less than or equal to 1"
));
out_data
[
i
]
=
(
label_data
[
i
]
-
static_cast
<
T
>
(
1
))
*
std
::
max
(
real_log
(
static_cast
<
T
>
(
1
)
-
x_data
[
i
]),
(
T
)(
-
100
))
-
label_data
[
i
]
*
std
::
max
(
real_log
(
x_data
[
i
]),
(
T
)(
-
100
));
}
}
};
template
<
typename
DeviceContext
,
typename
T
>
class
BCELossGradOpKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
auto
*
x
=
ctx
.
Input
<
Tensor
>
(
"X"
);
auto
*
labels
=
ctx
.
Input
<
Tensor
>
(
"Label"
);
auto
*
dout
=
ctx
.
Input
<
Tensor
>
(
framework
::
GradVarName
(
"Out"
));
auto
*
dx
=
ctx
.
Output
<
Tensor
>
(
framework
::
GradVarName
(
"X"
));
auto
dx_data
=
dx
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
auto
dout_data
=
dout
->
data
<
T
>
();
auto
x_data
=
x
->
data
<
T
>
();
auto
label_data
=
labels
->
data
<
T
>
();
int
x_numel
=
x
->
numel
();
// dx = dout * ((x - label)/(x - x^2))
for
(
int
i
=
0
;
i
<
x_numel
;
++
i
)
{
dx_data
[
i
]
=
dout_data
[
i
]
*
((
x_data
[
i
]
-
label_data
[
i
])
/
std
::
max
((
static_cast
<
T
>
(
1
)
-
x_data
[
i
])
*
x_data
[
i
],
static_cast
<
T
>
(
1e-12
)));
}
}
};
}
// namespace operators
}
// namespace paddle
paddle/fluid/operators/bce_loss_op_npu.cc
浏览文件 @
6fc5d88a
...
...
@@ -12,7 +12,7 @@ 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/fluid/
operators/bce_loss_op
.h"
#include "paddle/fluid/
framework/op_registry
.h"
#include "paddle/fluid/platform/device/npu/npu_op_runner.h"
namespace
paddle
{
...
...
paddle/phi/infermeta/binary.cc
浏览文件 @
6fc5d88a
...
...
@@ -230,4 +230,42 @@ void Atan2InferMeta(const MetaTensor& x, const MetaTensor& y, MetaTensor* out) {
out
->
set_dims
(
in_dims
);
}
void
BCELossInferMeta
(
const
MetaTensor
&
input
,
const
MetaTensor
&
label
,
MetaTensor
*
out
,
MetaConfig
config
)
{
auto
input_dims
=
input
.
dims
();
auto
label_dims
=
label
.
dims
();
int
rank
=
input_dims
.
size
();
PADDLE_ENFORCE_EQ
(
rank
,
label_dims
.
size
(),
phi
::
errors
::
InvalidArgument
(
"Input(X) and Input(Label) shall have the same rank."
"But received: the rank of Input(X) is [%d], "
"the rank of Input(Label) is [%d]."
,
rank
,
label_dims
.
size
()));
bool
check
=
true
;
if
((
!
config
.
is_runtime
)
&&
(
phi
::
product
(
input_dims
)
<=
0
||
phi
::
product
(
label_dims
)
<=
0
))
{
check
=
false
;
}
if
(
check
)
{
PADDLE_ENFORCE_EQ
(
input_dims
,
label_dims
,
phi
::
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]."
,
input_dims
,
label_dims
));
}
out
->
set_dims
(
input_dims
);
out
->
share_lod
(
input
);
}
}
// namespace phi
paddle/phi/infermeta/binary.h
浏览文件 @
6fc5d88a
...
...
@@ -54,4 +54,8 @@ void HuberLossInferMeta(const MetaTensor& input_meta,
MetaConfig
config
=
MetaConfig
());
void
Atan2InferMeta
(
const
MetaTensor
&
x
,
const
MetaTensor
&
y
,
MetaTensor
*
out
);
void
BCELossInferMeta
(
const
MetaTensor
&
input
,
const
MetaTensor
&
label
,
MetaTensor
*
out
,
MetaConfig
config
=
MetaConfig
());
}
// namespace phi
paddle/phi/kernels/bce_loss_grad_kernel.h
0 → 100644
浏览文件 @
6fc5d88a
// 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.
#pragma once
#include "paddle/phi/core/dense_tensor.h"
namespace
phi
{
template
<
typename
T
,
typename
Context
>
void
BCELossGradKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
input
,
const
DenseTensor
&
label
,
const
DenseTensor
&
out_grad
,
DenseTensor
*
input_grad
);
}
// namespace phi
paddle/phi/kernels/bce_loss_kernel.h
0 → 100644
浏览文件 @
6fc5d88a
// 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.
#pragma once
#include "paddle/phi/core/dense_tensor.h"
namespace
phi
{
template
<
typename
T
,
typename
Context
>
void
BCELossKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
input
,
const
DenseTensor
&
label
,
DenseTensor
*
out
);
}
// namespace phi
paddle/phi/kernels/cpu/bce_loss_grad_kernel.cc
0 → 100644
浏览文件 @
6fc5d88a
// 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/kernels/bce_loss_grad_kernel.h"
#include <algorithm> // for max
#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
namespace
phi
{
template
<
typename
T
,
typename
Context
>
void
BCELossGradKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
input
,
const
DenseTensor
&
label
,
const
DenseTensor
&
out_grad
,
DenseTensor
*
input_grad
)
{
auto
dx_data
=
dev_ctx
.
template
Alloc
<
T
>(
input_grad
);
auto
dout_data
=
out_grad
.
data
<
T
>
();
auto
x_data
=
input
.
data
<
T
>
();
auto
label_data
=
label
.
data
<
T
>
();
int
x_numel
=
input
.
numel
();
// dx = dout * ((x - label)/(x - x^2))
for
(
int
i
=
0
;
i
<
x_numel
;
++
i
)
{
dx_data
[
i
]
=
dout_data
[
i
]
*
((
x_data
[
i
]
-
label_data
[
i
])
/
std
::
max
((
static_cast
<
T
>
(
1
)
-
x_data
[
i
])
*
x_data
[
i
],
static_cast
<
T
>
(
1e-12
)));
}
}
}
// namespace phi
PD_REGISTER_KERNEL
(
bce_loss_grad
,
CPU
,
ALL_LAYOUT
,
phi
::
BCELossGradKernel
,
float
,
double
)
{}
paddle/phi/kernels/cpu/bce_loss_kernel.cc
0 → 100644
浏览文件 @
6fc5d88a
// 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/kernels/bce_loss_kernel.h"
#include <algorithm> // for max
#include "paddle/fluid/operators/math.h"
#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
namespace
phi
{
template
<
typename
T
,
typename
Context
>
void
BCELossKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
input
,
const
DenseTensor
&
label
,
DenseTensor
*
out
)
{
auto
x_data
=
input
.
data
<
T
>
();
auto
label_data
=
label
.
data
<
T
>
();
auto
out_data
=
dev_ctx
.
template
Alloc
<
T
>(
out
);
auto
x_numel
=
input
.
numel
();
// out = -(label * ln(x) + (1 - label) * ln(1 - x)) = (label - 1) * ln(1 -
// x) - label * ln(x)
for
(
int64_t
i
=
0
;
i
<
x_numel
;
++
i
)
{
PADDLE_ENFORCE_GE
(
x_data
[
i
],
static_cast
<
T
>
(
0
),
phi
::
errors
::
InvalidArgument
(
"Illegal input, input must be greater than or equal to 0"
));
PADDLE_ENFORCE_LE
(
x_data
[
i
],
static_cast
<
T
>
(
1
),
phi
::
errors
::
InvalidArgument
(
"Illegal input, input must be less than or equal to 1"
));
out_data
[
i
]
=
(
label_data
[
i
]
-
static_cast
<
T
>
(
1
))
*
std
::
max
(
paddle
::
operators
::
real_log
(
static_cast
<
T
>
(
1
)
-
x_data
[
i
]),
(
T
)(
-
100
))
-
label_data
[
i
]
*
std
::
max
(
paddle
::
operators
::
real_log
(
x_data
[
i
]),
(
T
)(
-
100
));
}
}
}
// namespace phi
PD_REGISTER_KERNEL
(
bce_loss
,
CPU
,
ALL_LAYOUT
,
phi
::
BCELossKernel
,
float
,
double
)
{}
paddle/phi/kernels/gpu/bce_loss_grad_kernel.cu
0 → 100644
浏览文件 @
6fc5d88a
// 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/kernels/bce_loss_grad_kernel.h"
#include <algorithm>
#include <vector>
#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/hostdevice.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/funcs/elementwise_base.h"
namespace
phi
{
template
<
typename
T
>
struct
BCELossGradFunctor
{
T
one
;
T
eps
;
HOSTDEVICE
inline
BCELossGradFunctor
()
{
one
=
static_cast
<
T
>
(
1.0
f
);
eps
=
static_cast
<
T
>
(
1e-12
);
}
HOSTDEVICE
inline
T
operator
()(
const
T
x
,
const
T
label
,
const
T
dout
)
const
{
T
term1
=
max
((
one
-
x
)
*
x
,
eps
);
return
(
dout
*
(
x
-
label
)
/
term1
);
}
};
template
<
typename
T
,
typename
Context
>
void
BCELossGradKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
input
,
const
DenseTensor
&
label
,
const
DenseTensor
&
out_grad
,
DenseTensor
*
input_grad
)
{
dev_ctx
.
template
Alloc
<
T
>(
input_grad
);
std
::
vector
<
const
DenseTensor
*>
ins
=
{
&
input
,
&
label
,
&
out_grad
};
std
::
vector
<
DenseTensor
*>
outs
=
{
input_grad
};
auto
functor
=
BCELossGradFunctor
<
T
>
();
phi
::
funcs
::
ElementwiseKernel
<
T
>
(
dev_ctx
,
ins
,
&
outs
,
functor
);
}
}
// namespace phi
PD_REGISTER_KERNEL
(
bce_loss_grad
,
GPU
,
ALL_LAYOUT
,
phi
::
BCELossGradKernel
,
float
,
double
)
{}
paddle/phi/kernels/gpu/bce_loss_kernel.cu
0 → 100644
浏览文件 @
6fc5d88a
// 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/kernels/bce_loss_kernel.h"
#include <algorithm>
#include <vector>
#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/hostdevice.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/funcs/elementwise_base.h"
#include "paddle/phi/kernels/primitive/functor_primitives.h"
namespace
phi
{
template
<
typename
T
>
struct
BCELossFunctor
{
T
one
;
T
neg_100
;
HOSTDEVICE
inline
BCELossFunctor
()
{
one
=
static_cast
<
T
>
(
1.0
f
);
neg_100
=
static_cast
<
T
>
(
-
100.
);
}
HOSTDEVICE
inline
T
operator
()(
const
T
x
,
const
T
label
)
const
{
PADDLE_ENFORCE
(
(
x
>=
static_cast
<
T
>
(
0
))
&&
(
x
<=
one
),
"Input is expected to be within the interval [0, 1], but recieved %f."
,
x
);
T
term1
=
max
(
phi
::
kps
::
details
::
Log
(
x
),
neg_100
);
T
term2
=
max
(
phi
::
kps
::
details
::
Log
(
one
-
x
),
neg_100
);
return
(((
label
-
one
)
*
term2
)
-
(
label
*
term1
));
}
};
template
<
typename
T
,
typename
Context
>
void
BCELossKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
input
,
const
DenseTensor
&
label
,
DenseTensor
*
out
)
{
dev_ctx
.
template
Alloc
<
T
>(
out
);
std
::
vector
<
const
DenseTensor
*>
ins
=
{
&
input
,
&
label
};
std
::
vector
<
DenseTensor
*>
outs
=
{
out
};
auto
functor
=
BCELossFunctor
<
T
>
();
phi
::
funcs
::
ElementwiseKernel
<
T
>
(
dev_ctx
,
ins
,
&
outs
,
functor
);
}
}
// namespace phi
PD_REGISTER_KERNEL
(
bce_loss
,
GPU
,
ALL_LAYOUT
,
phi
::
BCELossKernel
,
float
,
double
)
{}
paddle/phi/ops/compat/bce_loss_sig.cc
0 → 100644
浏览文件 @
6fc5d88a
// 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"
,
GradVarName
(
"Out"
)},
{},
{
GradVarName
(
"X"
)});
}
}
// 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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录