Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
27e252d9
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看板
未验证
提交
27e252d9
编写于
11月 21, 2022
作者:
T
taixiurong
提交者:
GitHub
11月 21, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add adamw suppor xpu, test=kunlun (#48114)
上级
394a7179
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
51 addition
and
44 deletion
+51
-44
paddle/fluid/platform/device/xpu/xpu2_op_list.h
paddle/fluid/platform/device/xpu/xpu2_op_list.h
+3
-1
paddle/phi/kernels/xpu/adamw_kernel.cc
paddle/phi/kernels/xpu/adamw_kernel.cc
+39
-35
python/paddle/fluid/tests/unittests/xpu/test_adamw_op_xpu.py
python/paddle/fluid/tests/unittests/xpu/test_adamw_op_xpu.py
+9
-8
未找到文件。
paddle/fluid/platform/device/xpu/xpu2_op_list.h
浏览文件 @
27e252d9
...
...
@@ -34,7 +34,9 @@ XPUOpMap& get_kl2_ops() {
XPUKernelSet
({
pOpKernelType
(
vartype
::
FP32
,
XPUPlace
()),
pOpKernelType
(
vartype
::
FP16
,
XPUPlace
())})},
{
"adadelta"
,
XPUKernelSet
({
pOpKernelType
(
vartype
::
FP32
,
XPUPlace
())})},
{
"adamw"
,
XPUKernelSet
({
pOpKernelType
(
vartype
::
FP32
,
XPUPlace
())})},
{
"adamw"
,
XPUKernelSet
({
pOpKernelType
(
vartype
::
FP32
,
XPUPlace
()),
pOpKernelType
(
vartype
::
FP16
,
XPUPlace
())})},
{
"adam"
,
XPUKernelSet
({
pOpKernelType
(
vartype
::
FP32
,
XPUPlace
()),
pOpKernelType
(
vartype
::
FP16
,
XPUPlace
())})},
...
...
paddle/phi/kernels/xpu/adamw_kernel.cc
浏览文件 @
27e252d9
...
...
@@ -52,6 +52,7 @@ void AdamwDenseKernel(const Context& dev_ctx,
DenseTensor
*
beta1_pow_out
,
DenseTensor
*
beta2_pow_out
,
DenseTensor
*
master_param_outs
)
{
using
XPUType
=
typename
XPUTypeTrait
<
T
>::
Type
;
bool
skip_update_
=
false
;
if
(
skip_update
.
is_initialized
())
{
PADDLE_ENFORCE_EQ
(
...
...
@@ -89,40 +90,42 @@ void AdamwDenseKernel(const Context& dev_ctx,
beta2_pow_ptr
=
xpu_beta2_pow
.
template
data
<
float
>();
}
if
(
with_decay
)
{
int
r
=
xpu
::
adamw
(
dev_ctx
.
x_context
(),
grad
.
template
data
<
T
>(),
moment1
.
template
data
<
float
>(),
moment2
.
template
data
<
float
>(),
param
.
template
data
<
T
>(),
beta1_pow_ptr
,
beta2_pow_ptr
,
learning_rate
.
template
data
<
float
>(),
dev_ctx
.
template
Alloc
<
float
>(
moment1_out
),
dev_ctx
.
template
Alloc
<
float
>(
moment2_out
),
dev_ctx
.
template
Alloc
<
T
>(
param_out
),
beta1_
,
beta2_
,
epsilon_
,
coeff
,
param
.
numel
());
int
r
=
xpu
::
adamw
(
dev_ctx
.
x_context
(),
reinterpret_cast
<
const
XPUType
*>
(
grad
.
template
data
<
T
>()),
moment1
.
template
data
<
float
>(),
moment2
.
template
data
<
float
>(),
reinterpret_cast
<
const
XPUType
*>
(
param
.
template
data
<
T
>()),
beta1_pow_ptr
,
beta2_pow_ptr
,
learning_rate
.
template
data
<
float
>(),
dev_ctx
.
template
Alloc
<
float
>(
moment1_out
),
dev_ctx
.
template
Alloc
<
float
>(
moment2_out
),
reinterpret_cast
<
XPUType
*>
(
dev_ctx
.
template
Alloc
<
T
>(
param_out
)),
beta1_
,
beta2_
,
epsilon_
,
coeff
,
param
.
numel
());
PADDLE_ENFORCE_XDNN_SUCCESS
(
r
,
"adamw"
);
}
else
{
int
r
=
xpu
::
adam
(
dev_ctx
.
x_context
(),
grad
.
template
data
<
T
>(),
moment1
.
template
data
<
float
>(),
moment2
.
template
data
<
float
>(),
param
.
template
data
<
T
>(),
beta1_pow_ptr
,
beta2_pow_ptr
,
learning_rate
.
template
data
<
float
>(),
dev_ctx
.
template
Alloc
<
float
>(
moment1_out
),
dev_ctx
.
template
Alloc
<
float
>(
moment2_out
),
dev_ctx
.
template
Alloc
<
T
>(
param_out
),
beta1_
,
beta2_
,
epsilon_
,
param
.
numel
());
PADDLE_ENFORCE_XDNN_SUCCESS
(
r
,
"adamw"
);
int
r
=
xpu
::
adam
(
dev_ctx
.
x_context
(),
reinterpret_cast
<
const
XPUType
*>
(
grad
.
template
data
<
T
>()),
moment1
.
template
data
<
float
>(),
moment2
.
template
data
<
float
>(),
reinterpret_cast
<
const
XPUType
*>
(
param
.
template
data
<
T
>()),
beta1_pow_ptr
,
beta2_pow_ptr
,
learning_rate
.
template
data
<
float
>(),
dev_ctx
.
template
Alloc
<
float
>(
moment1_out
),
dev_ctx
.
template
Alloc
<
float
>(
moment2_out
),
reinterpret_cast
<
XPUType
*>
(
dev_ctx
.
template
Alloc
<
T
>(
param_out
)),
beta1_
,
beta2_
,
epsilon_
,
param
.
numel
());
PADDLE_ENFORCE_XDNN_SUCCESS
(
r
,
"adam"
);
}
if
(
!
use_global_beta_pow
)
{
...
...
@@ -145,7 +148,7 @@ void AdamwDenseKernel(const Context& dev_ctx,
false
,
beta1_
,
0.0
f
);
PADDLE_ENFORCE_XDNN_SUCCESS
(
r
,
"
adamw
"
);
PADDLE_ENFORCE_XDNN_SUCCESS
(
r
,
"
scale
"
);
r
=
xpu
::
scale
(
dev_ctx
.
x_context
(),
beta2_pow_ptr
,
beta2_pow_out_p
,
...
...
@@ -153,14 +156,15 @@ void AdamwDenseKernel(const Context& dev_ctx,
false
,
beta2_
,
0.0
f
);
PADDLE_ENFORCE_XDNN_SUCCESS
(
r
,
"
adamw
"
);
PADDLE_ENFORCE_XDNN_SUCCESS
(
r
,
"
scale
"
);
}
}
}
}
// namespace phi
PD_REGISTER_KERNEL
(
adamw
,
XPU
,
ALL_LAYOUT
,
phi
::
AdamwDenseKernel
,
float
)
{
PD_REGISTER_KERNEL
(
adamw
,
XPU
,
ALL_LAYOUT
,
phi
::
AdamwDenseKernel
,
float
,
phi
::
dtype
::
float16
)
{
// Skip beta1_pow, beta2_pow, skip_update data transform
kernel
->
InputAt
(
5
).
SetBackend
(
phi
::
Backend
::
ALL_BACKEND
);
kernel
->
InputAt
(
6
).
SetBackend
(
phi
::
Backend
::
ALL_BACKEND
);
...
...
python/paddle/fluid/tests/unittests/xpu/test_adamw_op_xpu.py
浏览文件 @
27e252d9
...
...
@@ -90,9 +90,9 @@ class XPUTestAdamwOp1(XPUOpTestWrapper):
self
.
dtype
=
self
.
in_type_str
param
=
np
.
random
.
uniform
(
-
1
,
1
,
self
.
shape
).
astype
(
self
.
dtype
)
grad
=
np
.
random
.
uniform
(
-
1
,
1
,
self
.
shape
).
astype
(
self
.
dtype
)
moment1
=
np
.
random
.
uniform
(
-
1
,
1
,
self
.
shape
).
astype
(
self
.
dtype
)
moment1
=
np
.
random
.
uniform
(
-
1
,
1
,
self
.
shape
).
astype
(
"float32"
)
# The second moment is positive
moment2
=
np
.
random
.
random
(
self
.
shape
).
astype
(
self
.
dtype
)
moment2
=
np
.
random
.
random
(
self
.
shape
).
astype
(
"float32"
)
learning_rate
=
0.004
beta1
=
0.78
...
...
@@ -106,9 +106,9 @@ class XPUTestAdamwOp1(XPUOpTestWrapper):
'Grad'
:
grad
,
'Moment1'
:
moment1
,
'Moment2'
:
moment2
,
'LearningRate'
:
np
.
array
([
learning_rate
]).
astype
(
self
.
dtype
),
'Beta1Pow'
:
np
.
array
([
beta1_pow
]).
astype
(
self
.
dtype
),
'Beta2Pow'
:
np
.
array
([
beta2_pow
]).
astype
(
self
.
dtype
),
'LearningRate'
:
np
.
array
([
learning_rate
]).
astype
(
"float32"
),
'Beta1Pow'
:
np
.
array
([
beta1_pow
]).
astype
(
"float32"
),
'Beta2Pow'
:
np
.
array
([
beta2_pow
]).
astype
(
"float32"
),
}
self
.
attrs
=
{
...
...
@@ -127,8 +127,8 @@ class XPUTestAdamwOp1(XPUOpTestWrapper):
'Moment1Out'
:
moment1_out
,
'Moment2Out'
:
moment2_out
,
'ParamOut'
:
param_out
,
'Beta1PowOut'
:
np
.
array
([
beta1_pow
]).
astype
(
self
.
dtype
)
*
beta1
,
'Beta2PowOut'
:
np
.
array
([
beta2_pow
]).
astype
(
self
.
dtype
)
*
beta2
,
'Beta1PowOut'
:
np
.
array
([
beta1_pow
]).
astype
(
"float32"
)
*
beta1
,
'Beta2PowOut'
:
np
.
array
([
beta2_pow
]).
astype
(
"float32"
)
*
beta2
,
}
def
init_shape
(
self
):
...
...
@@ -305,7 +305,8 @@ class XPUTestAdamwOp2(XPUOpTestWrapper):
support_types
=
get_xpu_op_support_types
(
'adamw'
)
for
stype
in
support_types
:
create_test_class
(
globals
(),
XPUTestAdamwOp1
,
stype
)
create_test_class
(
globals
(),
XPUTestAdamwOp2
,
stype
)
if
stype
==
"float32"
:
create_test_class
(
globals
(),
XPUTestAdamwOp2
,
stype
)
if
__name__
==
"__main__"
:
paddle
.
enable_static
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录