Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
7ca28bb6
P
Paddle
项目概览
Crayon鑫
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
7ca28bb6
编写于
9月 01, 2021
作者:
Q
QingshuChen
提交者:
GitHub
9月 01, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
support KL label smooth (#35177)
* support KL label smooth * update UT for KL label_smooth
上级
5baccfdd
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
132 addition
and
9 deletion
+132
-9
cmake/external/xpu.cmake
cmake/external/xpu.cmake
+1
-1
paddle/fluid/framework/operator.cc
paddle/fluid/framework/operator.cc
+4
-4
paddle/fluid/imperative/prepared_operator.cc
paddle/fluid/imperative/prepared_operator.cc
+4
-4
paddle/fluid/operators/label_smooth_op_xpu.cc
paddle/fluid/operators/label_smooth_op_xpu.cc
+57
-0
paddle/fluid/platform/xpu/xpu2_op_list.h
paddle/fluid/platform/xpu/xpu2_op_list.h
+2
-0
python/paddle/fluid/tests/unittests/xpu/test_label_smooth_op_xpu.py
...dle/fluid/tests/unittests/xpu/test_label_smooth_op_xpu.py
+64
-0
未找到文件。
cmake/external/xpu.cmake
浏览文件 @
7ca28bb6
...
...
@@ -35,7 +35,7 @@ ELSE ()
ENDIF
()
SET
(
XPU_BASE_URL_WITHOUT_DATE
"https://baidu-kunlun-product.cdn.bcebos.com/KL-SDK/klsdk-dev"
)
SET
(
XPU_BASE_URL
"
${
XPU_BASE_URL_WITHOUT_DATE
}
/202108
18
"
)
SET
(
XPU_BASE_URL
"
${
XPU_BASE_URL_WITHOUT_DATE
}
/202108
26
"
)
SET
(
XPU_XRE_URL
"
${
XPU_BASE_URL
}
/
${
XPU_XRE_DIR_NAME
}
.tar.gz"
CACHE STRING
""
FORCE
)
SET
(
XPU_XDNN_URL
"
${
XPU_BASE_URL
}
/
${
XPU_XDNN_DIR_NAME
}
.tar.gz"
CACHE STRING
""
FORCE
)
SET
(
XPU_XCCL_URL
"
${
XPU_BASE_URL_WITHOUT_DATE
}
/20210623/
${
XPU_XCCL_DIR_NAME
}
.tar.gz"
CACHE STRING
""
FORCE
)
...
...
paddle/fluid/framework/operator.cc
浏览文件 @
7ca28bb6
...
...
@@ -1254,10 +1254,10 @@ void OperatorWithKernel::ChooseKernel(const RuntimeContext& ctx,
}
#endif
#ifdef PADDLE_WITH_XPU
if
(
(
kernel_iter
==
kernels
.
end
(
)
&&
is_xpu_place
(
expected_kernel_key
.
place_
)
&&
!
paddle
::
platform
::
is_xpu_support_op
(
type_
,
expected_kernel_key
)
)
||
paddle
::
platform
::
is_in_xpu_black_list
(
type_
))
{
if
(
is_xpu_place
(
expected_kernel_key
.
place_
)
&&
(
kernel_iter
==
kernels
.
end
()
||
!
paddle
::
platform
::
is_xpu_support_op
(
type_
,
expected_kernel_key
)
||
paddle
::
platform
::
is_in_xpu_black_list
(
type_
)
))
{
VLOG
(
3
)
<<
"missing XPU kernel: "
<<
type_
<<
", expected_kernel_key:"
<<
expected_kernel_key
<<
", fallbacking to CPU one!"
;
...
...
paddle/fluid/imperative/prepared_operator.cc
浏览文件 @
7ca28bb6
...
...
@@ -131,10 +131,10 @@ PreparedOp PrepareImpl(const NameVarMap<VarType>& ins,
auto
&
kernels
=
kernels_iter
->
second
;
auto
kernel_iter
=
kernels
.
find
(
expected_kernel_key
);
#ifdef PADDLE_WITH_XPU
if
(
(
kernel_iter
==
kernels
.
end
(
)
&&
is_xpu_place
(
expected_kernel_key
.
place_
)
&&
!
paddle
::
platform
::
is_xpu_support_op
(
op
.
Type
(),
expected_kernel_key
)
)
||
paddle
::
platform
::
is_in_xpu_black_list
(
op
.
Type
(
)))
{
if
(
is_xpu_place
(
expected_kernel_key
.
place_
)
&&
(
kernel_iter
==
kernels
.
end
()
||
!
paddle
::
platform
::
is_xpu_support_op
(
op
.
Type
(),
expected_kernel_key
)
||
paddle
::
platform
::
is_in_xpu_black_list
(
op
.
Type
()
)))
{
VLOG
(
3
)
<<
"missing XPU kernel: "
<<
op
.
Type
()
<<
", expected_kernel_key:"
<<
expected_kernel_key
<<
", fallbacking to CPU one!"
;
...
...
paddle/fluid/operators/label_smooth_op_xpu.cc
0 → 100644
浏览文件 @
7ca28bb6
/* Copyright (c) 2016 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. */
#ifdef PADDLE_WITH_XPU
#include "paddle/fluid/operators/label_smooth_op.h"
#include "paddle/fluid/framework/op_registry.h"
namespace
paddle
{
namespace
operators
{
template
<
typename
DeviceContext
,
typename
T
>
class
LabelSmoothXPUKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
{
auto
*
out_t
=
ctx
.
Output
<
framework
::
LoDTensor
>
(
"Out"
);
auto
*
in_t
=
ctx
.
Input
<
framework
::
LoDTensor
>
(
"X"
);
auto
*
dist_t
=
ctx
.
Input
<
framework
::
Tensor
>
(
"PriorDist"
);
auto
label_dim
=
in_t
->
dims
()[
in_t
->
dims
().
size
()
-
1
];
auto
ptr
=
out_t
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
auto
epsilon
=
ctx
.
Attr
<
float
>
(
"epsilon"
);
auto
&
dev_ctx
=
ctx
.
template
device_context
<
DeviceContext
>();
if
(
dist_t
)
{
PADDLE_THROW
(
platform
::
errors
::
External
(
"XPU doesn't support dist label smooth"
));
}
else
{
int
r
=
xpu
::
label_smooth
<
T
>
(
dev_ctx
.
x_context
(),
in_t
->
data
<
T
>
(),
ptr
,
in_t
->
numel
(),
epsilon
,
label_dim
);
PADDLE_ENFORCE_EQ
(
r
,
XPU_SUCCESS
,
platform
::
errors
::
External
(
"XPU API(label_smooth) return wrong "
"value[%d %s]"
,
r
,
XPUAPIErrorMsg
[
r
]));
}
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_XPU_KERNEL
(
label_smooth
,
ops
::
LabelSmoothXPUKernel
<
paddle
::
platform
::
XPUDeviceContext
,
float
>
);
#endif
paddle/fluid/platform/xpu/xpu2_op_list.h
浏览文件 @
7ca28bb6
...
...
@@ -29,6 +29,8 @@ using XPUOpMap = std::unordered_map<std::string, XPUKernelSet>;
XPUOpMap
&
get_kl2_ops
()
{
// KL1支持的op,通过op_name, data_type, place来索引
static
XPUOpMap
s_xpu2_kernels
{
{
"label_smooth"
,
XPUKernelSet
({
pOpKernelType
(
vartype
::
FP32
,
XPUPlace
())})},
{
"mul"
,
XPUKernelSet
({
pOpKernelType
(
vartype
::
FP32
,
XPUPlace
()),
pOpKernelType
(
vartype
::
FP16
,
XPUPlace
())})},
{
"elementwise_sub"
,
...
...
python/paddle/fluid/tests/unittests/xpu/test_label_smooth_op_xpu.py
0 → 100644
浏览文件 @
7ca28bb6
# 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.
from
__future__
import
print_function
import
unittest
import
paddle
import
numpy
as
np
import
sys
sys
.
path
.
append
(
".."
)
from
op_test_xpu
import
XPUOpTest
paddle
.
enable_static
()
class
TestLabelSmoothOp
(
XPUOpTest
):
def
config
(
self
):
self
.
op_type
=
"label_smooth"
self
.
epsilon
=
0.1
self
.
use_xpu
=
True
batch_size
,
self
.
label_dim
=
10
,
12
self
.
label
=
np
.
zeros
((
batch_size
,
self
.
label_dim
)).
astype
(
"float32"
)
nonzero_index
=
np
.
random
.
randint
(
self
.
label_dim
,
size
=
(
batch_size
))
self
.
label
[
np
.
arange
(
batch_size
),
nonzero_index
]
=
1
def
setUp
(
self
):
self
.
config
()
smoothed_label
=
(
1
-
self
.
epsilon
)
*
self
.
label
+
self
.
epsilon
/
self
.
label_dim
self
.
inputs
=
{
'X'
:
self
.
label
}
self
.
attrs
=
{
'epsilon'
:
self
.
epsilon
}
self
.
outputs
=
{
'Out'
:
smoothed_label
}
def
test_check_output
(
self
):
if
not
paddle
.
is_compiled_with_xpu
():
return
self
.
check_output_with_place
(
paddle
.
XPUPlace
(
0
),
atol
=
1e-6
)
def
test_check_grad
(
self
):
return
class
TestLabelSmoothOp3D
(
TestLabelSmoothOp
):
def
setUp
(
self
):
super
(
TestLabelSmoothOp3D
,
self
).
setUp
()
self
.
inputs
[
'X'
]
=
self
.
inputs
[
'X'
].
reshape
(
[
2
,
-
1
,
self
.
inputs
[
'X'
].
shape
[
-
1
]])
self
.
outputs
[
'Out'
]
=
self
.
outputs
[
'Out'
].
reshape
(
self
.
inputs
[
'X'
]
.
shape
)
if
__name__
==
'__main__'
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录