Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
54b247b1
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看板
未验证
提交
54b247b1
编写于
9月 05, 2023
作者:
G
gouzil
提交者:
GitHub
9月 05, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Fluid] move lars_momentum_xpu to phi (#56751)
* [Fluid] move lars_momentum_xpu to phi * Empty-Commit;test=kunlun;
上级
b196e076
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
113 addition
and
125 deletion
+113
-125
paddle/fluid/operators/optimizers/lars_momentum_op_xpu.cc
paddle/fluid/operators/optimizers/lars_momentum_op_xpu.cc
+0
-125
paddle/phi/kernels/xpu/lars_momentum_kernel.cc
paddle/phi/kernels/xpu/lars_momentum_kernel.cc
+113
-0
未找到文件。
paddle/fluid/operators/optimizers/lars_momentum_op_xpu.cc
已删除
100644 → 0
浏览文件 @
b196e076
/* 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/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/phi/backends/xpu/enforce_xpu.h"
namespace
paddle
{
namespace
operators
{
template
<
typename
T
,
typename
DeviceContext
>
class
LarsMomentumOpXPUKernel
:
public
framework
::
OpKernel
<
T
>
{
using
XPUType
=
typename
XPUTypeTrait
<
T
>::
Type
;
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
bool
multi_precision
=
ctx
.
Attr
<
bool
>
(
"multi_precision"
);
auto
param_out
=
ctx
.
MultiOutput
<
phi
::
DenseTensor
>
(
"ParamOut"
);
auto
velocity_out
=
ctx
.
MultiOutput
<
phi
::
DenseTensor
>
(
"VelocityOut"
);
auto
param
=
ctx
.
MultiInput
<
phi
::
DenseTensor
>
(
"Param"
);
auto
velocity
=
ctx
.
MultiInput
<
phi
::
DenseTensor
>
(
"Velocity"
);
auto
learning_rate
=
ctx
.
MultiInput
<
phi
::
DenseTensor
>
(
"LearningRate"
);
auto
grad
=
ctx
.
MultiInput
<
phi
::
DenseTensor
>
(
"Grad"
);
auto
weight_decay_arr
=
ctx
.
Attr
<
std
::
vector
<
float
>>
(
"lars_weight_decay"
);
auto
master_param
=
ctx
.
MultiInput
<
phi
::
DenseTensor
>
(
"MasterParam"
);
auto
master_param_out
=
ctx
.
MultiOutput
<
phi
::
DenseTensor
>
(
"MasterParamOut"
);
float
mu
=
static_cast
<
T
>
(
ctx
.
Attr
<
float
>
(
"mu"
));
float
lars_coeff
=
ctx
.
Attr
<
float
>
(
"lars_coeff"
);
float
epsilon
=
ctx
.
Attr
<
float
>
(
"epsilon"
);
float
rescale_grad
=
ctx
.
Attr
<
float
>
(
"rescale_grad"
);
std
::
vector
<
XPUType
*>
param_list
;
std
::
vector
<
XPUType
*>
grad_list
;
std
::
vector
<
XPUType
*>
param_out_list
;
std
::
vector
<
float
*>
velocity_list
;
std
::
vector
<
float
*>
velocity_out_list
;
std
::
vector
<
float
*>
lrs
;
std
::
vector
<
int
>
param_sizes
;
std
::
vector
<
float
*>
master_param_list
;
std
::
vector
<
float
*>
master_param_out_list
;
int
op_num
=
param
.
size
();
for
(
int
i
=
0
;
i
<
op_num
;
++
i
)
{
param_list
.
push_back
(
reinterpret_cast
<
XPUType
*>
(
const_cast
<
T
*>
((
param
[
i
]
->
data
<
T
>
()))));
grad_list
.
push_back
(
reinterpret_cast
<
XPUType
*>
(
const_cast
<
T
*>
(
grad
[
i
]
->
data
<
T
>
())));
param_out_list
.
push_back
(
reinterpret_cast
<
XPUType
*>
(
param_out
[
i
]
->
mutable_data
<
T
>
(
ctx
.
GetPlace
())));
velocity_list
.
push_back
(
const_cast
<
float
*>
(
velocity
[
i
]
->
data
<
float
>
()));
velocity_out_list
.
push_back
(
velocity_out
[
i
]
->
mutable_data
<
float
>
(
ctx
.
GetPlace
()));
lrs
.
push_back
(
const_cast
<
float
*>
(
learning_rate
[
i
]
->
data
<
float
>
()));
param_sizes
.
push_back
(
param
[
i
]
->
numel
());
PADDLE_ENFORCE_EQ
(
param_list
[
i
],
param_out_list
[
i
],
platform
::
errors
::
InvalidArgument
(
"Input(Param) and Output(ParamOut) must be the same Tensors."
));
PADDLE_ENFORCE_EQ
(
velocity_list
[
i
],
velocity_out_list
[
i
],
platform
::
errors
::
InvalidArgument
(
"Input(Velocity) and Output(VelocityOut) must be "
"the same Tensors."
));
if
(
multi_precision
)
{
master_param_list
.
push_back
(
const_cast
<
float
*>
(
master_param
[
i
]
->
data
<
float
>
()));
master_param_out_list
.
push_back
(
master_param_out
[
i
]
->
mutable_data
<
float
>
(
ctx
.
GetPlace
()));
PADDLE_ENFORCE_EQ
(
master_param_list
[
i
],
master_param_out_list
[
i
],
platform
::
errors
::
InvalidArgument
(
"Input(MasterParam) and Output(MasterParamOut) "
"must be the same Tensors."
));
}
else
{
master_param_list
.
push_back
(
nullptr
);
master_param_out_list
.
push_back
(
nullptr
);
}
}
auto
&
dev_ctx
=
ctx
.
template
device_context
<
platform
::
XPUDeviceContext
>();
int
r
=
lars_momentum
(
dev_ctx
.
x_context
(),
param_list
,
grad_list
,
velocity_list
,
lrs
,
master_param_list
,
param_out_list
,
velocity_out_list
,
master_param_out_list
,
weight_decay_arr
,
param_sizes
,
mu
,
lars_coeff
,
epsilon
,
rescale_grad
);
PADDLE_ENFORCE_XDNN_SUCCESS
(
r
,
"lars_momentum"
);
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
namespace
plat
=
paddle
::
platform
;
PD_REGISTER_STRUCT_KERNEL
(
lars_momentum
,
XPU
,
ALL_LAYOUT
,
ops
::
LarsMomentumOpXPUKernel
,
float
,
plat
::
float16
)
{}
#endif
paddle/phi/kernels/xpu/lars_momentum_kernel.cc
0 → 100644
浏览文件 @
54b247b1
// Copyright (c) 2023 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/lars_momentum_kernel.h"
#include "paddle/phi/backends/xpu/enforce_xpu.h"
#include "paddle/phi/backends/xpu/xpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
namespace
phi
{
template
<
typename
T
,
typename
Context
>
void
LarsMomentumKernel
(
const
Context
&
dev_ctx
,
const
std
::
vector
<
const
DenseTensor
*>&
param
,
const
std
::
vector
<
const
DenseTensor
*>&
velocity
,
const
std
::
vector
<
const
DenseTensor
*>&
learning_rate
,
const
std
::
vector
<
const
DenseTensor
*>&
grad
,
const
paddle
::
optional
<
std
::
vector
<
const
DenseTensor
*>>&
master_param
,
const
std
::
vector
<
float
>&
weight_decay_arr
,
float
mu
,
float
lars_coeff
,
float
epsilon
,
bool
multi_precision
,
float
rescale_grad
,
std
::
vector
<
DenseTensor
*>
param_out
,
std
::
vector
<
DenseTensor
*>
velocity_out
,
std
::
vector
<
DenseTensor
*>
master_param_out
)
{
using
XPUType
=
typename
XPUTypeTrait
<
T
>::
Type
;
std
::
vector
<
XPUType
*>
param_list
;
std
::
vector
<
XPUType
*>
grad_list
;
std
::
vector
<
XPUType
*>
param_out_list
;
std
::
vector
<
float
*>
velocity_list
;
std
::
vector
<
float
*>
velocity_out_list
;
std
::
vector
<
float
*>
lrs
;
std
::
vector
<
int
>
param_sizes
;
std
::
vector
<
float
*>
master_param_list
;
std
::
vector
<
float
*>
master_param_out_list
;
int
op_num
=
param
.
size
();
for
(
int
i
=
0
;
i
<
op_num
;
++
i
)
{
param_list
.
push_back
(
reinterpret_cast
<
XPUType
*>
(
const_cast
<
T
*>
((
param
[
i
]
->
data
<
T
>
()))));
grad_list
.
push_back
(
reinterpret_cast
<
XPUType
*>
(
const_cast
<
T
*>
(
grad
[
i
]
->
data
<
T
>
())));
param_out_list
.
push_back
(
reinterpret_cast
<
XPUType
*>
(
dev_ctx
.
template
Alloc
<
T
>(
param_out
[
i
])));
velocity_list
.
push_back
(
const_cast
<
float
*>
(
velocity
[
i
]
->
data
<
float
>
()));
velocity_out_list
.
push_back
(
dev_ctx
.
template
Alloc
<
float
>(
velocity_out
[
i
]));
lrs
.
push_back
(
const_cast
<
float
*>
(
learning_rate
[
i
]
->
data
<
float
>
()));
param_sizes
.
push_back
(
param
[
i
]
->
numel
());
PADDLE_ENFORCE_EQ
(
param_list
[
i
],
param_out_list
[
i
],
phi
::
errors
::
InvalidArgument
(
"Input(Param) and Output(ParamOut) must be the same Tensors."
));
PADDLE_ENFORCE_EQ
(
velocity_list
[
i
],
velocity_out_list
[
i
],
phi
::
errors
::
InvalidArgument
(
"Input(Velocity) and Output(VelocityOut) must be "
"the same Tensors."
));
if
(
multi_precision
)
{
master_param_list
.
push_back
(
const_cast
<
float
*>
(
master_param
.
get
()[
i
]
->
data
<
float
>
()));
master_param_out_list
.
push_back
(
dev_ctx
.
template
Alloc
<
float
>(
master_param_out
[
i
]));
PADDLE_ENFORCE_EQ
(
master_param_list
[
i
],
master_param_out_list
[
i
],
phi
::
errors
::
InvalidArgument
(
"Input(MasterParam) and Output(MasterParamOut) "
"must be the same Tensors."
));
}
else
{
master_param_list
.
push_back
(
nullptr
);
master_param_out_list
.
push_back
(
nullptr
);
}
}
int
r
=
lars_momentum
(
dev_ctx
.
x_context
(),
param_list
,
grad_list
,
velocity_list
,
lrs
,
master_param_list
,
param_out_list
,
velocity_out_list
,
master_param_out_list
,
weight_decay_arr
,
param_sizes
,
mu
,
lars_coeff
,
epsilon
,
rescale_grad
);
PADDLE_ENFORCE_XDNN_SUCCESS
(
r
,
"lars_momentum"
);
}
}
// namespace phi
PD_REGISTER_KERNEL
(
lars_momentum
,
XPU
,
ALL_LAYOUT
,
phi
::
LarsMomentumKernel
,
float
,
phi
::
dtype
::
float16
)
{}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录