Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
5f0a8adc
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2298
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看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
5f0a8adc
编写于
11月 07, 2022
作者:
Q
QingshuChen
提交者:
GitHub
11月 07, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
support kldiv_loss/kldiv_loss_grad for kunlun (#47638)
*test=kunlun
上级
87753ee8
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
250 addition
and
0 deletion
+250
-0
paddle/fluid/platform/device/xpu/xpu2_op_list.h
paddle/fluid/platform/device/xpu/xpu2_op_list.h
+3
-0
paddle/phi/kernels/xpu/kldiv_loss_grad_kernel.cc
paddle/phi/kernels/xpu/kldiv_loss_grad_kernel.cc
+51
-0
paddle/phi/kernels/xpu/kldiv_loss_kernel.cc
paddle/phi/kernels/xpu/kldiv_loss_kernel.cc
+49
-0
python/paddle/fluid/tests/unittests/xpu/test_kldiv_loss_op_xpu.py
...addle/fluid/tests/unittests/xpu/test_kldiv_loss_op_xpu.py
+147
-0
未找到文件。
paddle/fluid/platform/device/xpu/xpu2_op_list.h
浏览文件 @
5f0a8adc
...
...
@@ -306,6 +306,9 @@ XPUOpMap& get_kl2_ops() {
{
"huber_loss_grad"
,
XPUKernelSet
({
pOpKernelType
(
vartype
::
FP32
,
XPUPlace
())})},
{
"huber_loss"
,
XPUKernelSet
({
pOpKernelType
(
vartype
::
FP32
,
XPUPlace
())})},
{
"kldiv_loss"
,
XPUKernelSet
({
pOpKernelType
(
vartype
::
FP32
,
XPUPlace
())})},
{
"kldiv_loss_grad"
,
XPUKernelSet
({
pOpKernelType
(
vartype
::
FP32
,
XPUPlace
())})},
{
"iou_similarity"
,
XPUKernelSet
({
pOpKernelType
(
vartype
::
FP32
,
XPUPlace
())})},
{
"index_select"
,
...
...
paddle/phi/kernels/xpu/kldiv_loss_grad_kernel.cc
0 → 100644
浏览文件 @
5f0a8adc
/* 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/backends/xpu/enforce_xpu.h"
#include "paddle/phi/core/enforce.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/softmax_kernel.h"
namespace
phi
{
template
<
typename
T
,
typename
Context
>
void
KLDivLossGradKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
x
,
const
DenseTensor
&
label
,
const
DenseTensor
&
d_out
,
const
std
::
string
&
reduction
,
DenseTensor
*
d_x
)
{
using
XPUType
=
typename
XPUTypeTrait
<
T
>::
Type
;
dev_ctx
.
template
Alloc
<
T
>(
d_x
);
if
(
d_x
->
numel
()
==
0
)
{
return
;
}
int
r
=
XPU_SUCCESS
;
r
=
xpu
::
kldiv_loss_grad
(
dev_ctx
.
x_context
(),
reinterpret_cast
<
const
XPUType
*>
(
label
.
data
<
T
>
()),
reinterpret_cast
<
const
XPUType
*>
(
d_out
.
data
<
T
>
()),
reinterpret_cast
<
XPUType
*>
(
d_x
->
data
<
T
>
()),
d_x
->
numel
());
PADDLE_ENFORCE_XDNN_SUCCESS
(
r
,
"kldiv_loss_grad"
);
if
(
"none"
!=
reduction
)
{
PADDLE_THROW
(
phi
::
errors
::
Unavailable
(
"Not supported reduction [%s] in kldiv_loss_grad"
,
reduction
));
}
}
}
// namespace phi
PD_REGISTER_KERNEL
(
kldiv_loss_grad
,
XPU
,
ALL_LAYOUT
,
phi
::
KLDivLossGradKernel
,
float
)
{}
paddle/phi/kernels/xpu/kldiv_loss_kernel.cc
0 → 100644
浏览文件 @
5f0a8adc
/* 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/backends/xpu/enforce_xpu.h"
#include "paddle/phi/core/enforce.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/softmax_kernel.h"
namespace
phi
{
template
<
typename
T
,
typename
Context
>
void
KLDivLossKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
x
,
const
DenseTensor
&
label
,
const
std
::
string
&
reduction
,
DenseTensor
*
out
)
{
using
XPUType
=
typename
XPUTypeTrait
<
T
>::
Type
;
dev_ctx
.
template
Alloc
<
T
>(
out
);
if
(
out
->
numel
()
==
0
)
{
return
;
}
int
r
=
XPU_SUCCESS
;
r
=
xpu
::
kldiv_loss
(
dev_ctx
.
x_context
(),
reinterpret_cast
<
const
XPUType
*>
(
x
.
data
<
T
>
()),
reinterpret_cast
<
const
XPUType
*>
(
label
.
data
<
T
>
()),
reinterpret_cast
<
XPUType
*>
(
out
->
data
<
T
>
()),
out
->
numel
());
PADDLE_ENFORCE_XDNN_SUCCESS
(
r
,
"kldiv_loss"
);
if
(
"none"
!=
reduction
)
{
PADDLE_THROW
(
phi
::
errors
::
Unavailable
(
"Not supported reduction [%s] in kldiv_loss"
,
reduction
));
}
}
}
// namespace phi
PD_REGISTER_KERNEL
(
kldiv_loss
,
XPU
,
ALL_LAYOUT
,
phi
::
KLDivLossKernel
,
float
)
{}
python/paddle/fluid/tests/unittests/xpu/test_kldiv_loss_op_xpu.py
0 → 100644
浏览文件 @
5f0a8adc
# Copyright (c) 2018 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.
import
sys
sys
.
path
.
append
(
".."
)
import
paddle
import
unittest
import
numpy
as
np
from
paddle.nn.functional
import
kl_div
from
op_test_xpu
import
XPUOpTest
from
xpu.get_test_cover_info
import
(
create_test_class
,
get_xpu_op_support_types
,
XPUOpTestWrapper
,
)
paddle
.
enable_static
()
def
kldiv_loss
(
x
,
target
,
reduction
):
output
=
target
*
(
np
.
log
(
target
)
-
x
)
loss
=
np
.
where
(
target
>=
0
,
output
,
np
.
zeros_like
(
x
))
if
reduction
==
"batchmean"
:
if
len
(
x
.
shape
)
>
0
:
return
loss
.
sum
()
/
x
.
shape
[
0
]
else
:
return
loss
.
sum
()
if
reduction
==
"mean"
:
return
loss
.
mean
()
if
reduction
==
"sum"
:
return
loss
.
sum
()
return
loss
class
XPUTestKLDivLossOp
(
XPUOpTestWrapper
):
def
__init__
(
self
):
self
.
op_name
=
'kldiv_loss'
self
.
use_dynamic_create_class
=
False
class
TestKLDivLossOp
(
XPUOpTest
):
def
setUp
(
self
):
self
.
initTestCase
()
self
.
op_type
=
'kldiv_loss'
self
.
dtype
=
np
.
float32
self
.
__class__
.
use_xpu
=
True
self
.
python_api
=
kl_div
x
=
np
.
random
.
uniform
(
-
10
,
10
,
self
.
x_shape
).
astype
(
'float32'
)
target
=
np
.
random
.
uniform
(
-
10
,
10
,
self
.
x_shape
).
astype
(
'float32'
)
self
.
attrs
=
{
"reduction"
:
self
.
reduction
}
self
.
inputs
=
{
'X'
:
x
,
'Target'
:
target
,
}
loss
=
kldiv_loss
(
x
,
target
,
self
.
reduction
)
self
.
outputs
=
{
'Loss'
:
loss
.
astype
(
'float32'
)}
def
test_check_output
(
self
):
self
.
check_output
(
check_eager
=
True
)
def
test_check_grad
(
self
):
self
.
check_grad_with_place
(
paddle
.
XPUPlace
(
0
),
[
'X'
],
'Loss'
,
no_grad_set
=
set
([
"Target"
]),
check_eager
=
True
,
)
def
initTestCase
(
self
):
self
.
x_shape
=
(
4
,
5
,
5
)
self
.
reduction
=
'none'
class
TestKLDivLossOp2
(
TestKLDivLossOp
):
def
initTestCase
(
self
):
self
.
x_shape
=
(
3
,
2
,
7
,
7
)
self
.
reduction
=
'none'
class
TestKLDivLossOp3
(
TestKLDivLossOp
):
def
initTestCase
(
self
):
self
.
x_shape
=
(
2
,
3
,
5
,
7
,
9
)
self
.
reduction
=
'none'
class
TestKLDivLossOp4
(
TestKLDivLossOp
):
def
initTestCase
(
self
):
self
.
x_shape
=
(
5
,
20
)
self
.
reduction
=
'none'
class
TestKLDivLossDygraph
(
unittest
.
TestCase
):
def
run_kl_loss
(
self
,
reduction
,
shape
=
(
5
,
20
)):
x
=
np
.
random
.
uniform
(
-
10
,
10
,
shape
).
astype
(
'float32'
)
target
=
np
.
random
.
uniform
(
-
10
,
10
,
shape
).
astype
(
'float32'
)
gt_loss
=
kldiv_loss
(
x
,
target
,
reduction
)
with
paddle
.
fluid
.
dygraph
.
guard
():
kldiv_criterion
=
paddle
.
nn
.
KLDivLoss
(
reduction
)
pred_loss
=
kldiv_criterion
(
paddle
.
to_tensor
(
x
),
paddle
.
to_tensor
(
target
)
)
np
.
testing
.
assert_allclose
(
pred_loss
.
numpy
(),
gt_loss
,
rtol
=
1e-05
)
def
test_kl_loss_none
(
self
):
self
.
run_kl_loss
(
'none'
)
def
test_kl_loss_static_api
(
self
):
input
=
paddle
.
fluid
.
data
(
name
=
'input'
,
shape
=
[
5
,
20
])
label
=
paddle
.
fluid
.
data
(
name
=
'label'
,
shape
=
[
5
,
20
])
paddle
.
nn
.
functional
.
kl_div
(
input
,
label
)
class
TestKLDivLossTypePromotion
(
unittest
.
TestCase
):
def
test_kl_div_promotion
(
self
):
with
paddle
.
fluid
.
dygraph
.
guard
():
x1
=
paddle
.
rand
([
5
,
20
],
dtype
=
'float32'
)
target1
=
paddle
.
rand
([
5
,
20
],
dtype
=
'float32'
)
kldiv_criterion
=
paddle
.
nn
.
KLDivLoss
()
pred_loss1
=
kldiv_criterion
(
x1
,
target1
)
x2
=
paddle
.
rand
([
5
,
20
],
dtype
=
'float32'
)
target2
=
paddle
.
rand
([
5
,
20
],
dtype
=
'float32'
)
pred_loss2
=
paddle
.
nn
.
functional
.
kl_div
(
x2
,
target2
)
support_types
=
get_xpu_op_support_types
(
'kldiv_loss'
)
for
stype
in
support_types
:
create_test_class
(
globals
(),
XPUTestKLDivLossOp
,
stype
)
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录