Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
52a0a677
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看板
未验证
提交
52a0a677
编写于
9月 05, 2023
作者:
G
gouzil
提交者:
GitHub
9月 05, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Fluid] move lars_momentum_op InferShape to phi (#56749)
* move to phi * fix * fix type
上级
99ae88f1
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
110 addition
and
112 deletion
+110
-112
paddle/fluid/operators/optimizers/lars_momentum_op.cc
paddle/fluid/operators/optimizers/lars_momentum_op.cc
+8
-112
paddle/phi/infermeta/multiary.cc
paddle/phi/infermeta/multiary.cc
+86
-0
paddle/phi/infermeta/multiary.h
paddle/phi/infermeta/multiary.h
+16
-0
未找到文件。
paddle/fluid/operators/optimizers/lars_momentum_op.cc
浏览文件 @
52a0a677
...
...
@@ -13,7 +13,9 @@ See the License for the specific language governing permissions and
limitations under the License. */
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/phi/infermeta/multiary.h"
namespace
paddle
{
namespace
operators
{
...
...
@@ -22,117 +24,6 @@ class LarsMomentumOp : public framework::OperatorWithKernel {
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
protected:
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
OP_INOUT_CHECK
(
ctx
->
HasInputs
(
"Param"
),
"Input"
,
"Param"
,
"LarsMomentum"
);
OP_INOUT_CHECK
(
ctx
->
HasInputs
(
"Grad"
),
"Input"
,
"Grad"
,
"LarsMomentum"
);
OP_INOUT_CHECK
(
ctx
->
HasInputs
(
"Velocity"
),
"Input"
,
"Velocity"
,
"LarsMomentum"
);
OP_INOUT_CHECK
(
ctx
->
HasInputs
(
"LearningRate"
),
"Input"
,
"LearningRate"
,
"LarsMomentum"
);
OP_INOUT_CHECK
(
ctx
->
HasOutputs
(
"ParamOut"
),
"Output"
,
"ParamOut"
,
"LarsMomentum"
);
OP_INOUT_CHECK
(
ctx
->
HasOutputs
(
"VelocityOut"
),
"Output"
,
"VelocityOut"
,
"LarsMomentum"
);
PADDLE_ENFORCE_EQ
(
ctx
->
GetInputsVarType
(
"Param"
).
front
(),
framework
::
proto
::
VarType
::
LOD_TENSOR
,
platform
::
errors
::
InvalidArgument
(
"The input var's type should be phi::DenseTensor, "
"but the received is %s"
,
ctx
->
GetInputsVarType
(
"Param"
).
front
()));
auto
lr_dims
=
ctx
->
GetInputsDim
(
"LearningRate"
);
auto
grad_dim
=
ctx
->
GetInputsDim
(
"Grad"
);
auto
param_dim
=
ctx
->
GetInputsDim
(
"Param"
);
auto
velocity_dim
=
ctx
->
GetInputsDim
(
"Velocity"
);
auto
lars_weight_decays
=
ctx
->
Attrs
().
Get
<
std
::
vector
<
float
>>
(
"lars_weight_decay"
);
auto
multi_precision
=
ctx
->
Attrs
().
Get
<
bool
>
(
"multi_precision"
);
PADDLE_ENFORCE_EQ
(
param_dim
.
size
(),
grad_dim
.
size
(),
platform
::
errors
::
InvalidArgument
(
"Input(Param) and Input(Grad) of LarsMomentumOp should have "
"same quantity. But number of Param is [%d] and Grad is [%d]."
,
param_dim
.
size
(),
grad_dim
.
size
()));
PADDLE_ENFORCE_EQ
(
param_dim
.
size
(),
velocity_dim
.
size
(),
platform
::
errors
::
InvalidArgument
(
"Input(Param) and Input(Velocity) of LarsMomentumOp should "
"have same quantity. But number of Param is [%d] and Velocity "
"is [%d]."
,
param_dim
.
size
(),
velocity_dim
.
size
()));
PADDLE_ENFORCE_EQ
(
lars_weight_decays
.
size
(),
grad_dim
.
size
(),
platform
::
errors
::
InvalidArgument
(
"Attr(Lars_weight_decay) and "
"Input(Grad) of LarsMomentumOp should have same quantity. "
"But number of Lars_weight_decay is [%d] and Grad is [%d]."
,
lars_weight_decays
.
size
(),
grad_dim
.
size
()));
if
(
multi_precision
)
{
OP_INOUT_CHECK
(
ctx
->
HasInputs
(
"MasterParam"
),
"Input"
,
"MasterParam"
,
"LarsMomentumMultiPrecision"
);
OP_INOUT_CHECK
(
ctx
->
HasOutputs
(
"MasterParamOut"
),
"Output"
,
"MasterParamOut"
,
"LarsMomentumMultiPrecision"
);
}
for
(
auto
&
lr_dim
:
lr_dims
)
{
PADDLE_ENFORCE_EQ
(
phi
::
product
(
lr_dim
),
1
,
platform
::
errors
::
InvalidArgument
(
"Learning_rate should be a scalar. But Received "
"LearningRate's dim [%s]"
,
phi
::
product
(
lr_dim
)));
}
for
(
size_t
i
=
0
;
i
<
param_dim
.
size
();
++
i
)
{
PADDLE_ENFORCE_EQ
(
ctx
->
GetInputsVarType
(
"Grad"
)[
i
],
framework
::
proto
::
VarType
::
LOD_TENSOR
,
platform
::
errors
::
InvalidArgument
(
"The Var(%s)'s type should be phi::DenseTensor, "
"but the received is %s"
,
ctx
->
Inputs
(
"Grad"
)[
i
].
front
(),
ctx
->
GetInputsVarType
(
"Grad"
)[
i
]));
PADDLE_ENFORCE_EQ
(
param_dim
[
i
],
grad_dim
[
i
],
platform
::
errors
::
InvalidArgument
(
"Input(Param) and Input(Grad) input of LarsMomentumOp shall "
"have same dimension. But Param`s dim is [%s] and Grad's dim "
"is [%s]."
,
param_dim
[
i
],
grad_dim
[
i
]));
PADDLE_ENFORCE_EQ
(
param_dim
[
i
],
velocity_dim
[
i
],
platform
::
errors
::
InvalidArgument
(
"Input(Param) and Input(Velocity) of LarsMomentumOp shall have "
"same dimension. But Param dim [%s] differs with Velocity dim "
"[%s]."
,
param_dim
[
i
],
velocity_dim
[
i
]));
}
ctx
->
SetOutputsDim
(
"ParamOut"
,
param_dim
);
ctx
->
SetOutputsDim
(
"VelocityOut"
,
param_dim
);
if
(
ctx
->
HasOutputs
(
"MasterParamOut"
))
{
ctx
->
SetOutputsDim
(
"MasterParamOut"
,
param_dim
);
}
}
protected:
phi
::
KernelKey
GetExpectedKernelType
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
...
...
@@ -226,6 +117,10 @@ class LarsMomentumOpVarTypeInference : public framework::VarTypeInference {
}
// namespace operators
}
// namespace paddle
DECLARE_INFER_SHAPE_FUNCTOR
(
lars_momentum
,
LarsMomentumInferShapeFunctor
,
PD_INFER_META
(
phi
::
LarsMomentumInferMeta
));
namespace
ops
=
paddle
::
operators
;
REGISTER_OPERATOR
(
lars_momentum
,
...
...
@@ -233,4 +128,5 @@ REGISTER_OPERATOR(
ops
::
LarsMomentumOpMaker
,
paddle
::
framework
::
EmptyGradOpMaker
<
paddle
::
framework
::
OpDesc
>
,
paddle
::
framework
::
EmptyGradOpMaker
<
paddle
::
imperative
::
OpBase
>
,
ops
::
LarsMomentumOpVarTypeInference
);
ops
::
LarsMomentumOpVarTypeInference
,
LarsMomentumInferShapeFunctor
);
paddle/phi/infermeta/multiary.cc
浏览文件 @
52a0a677
...
...
@@ -2577,6 +2577,92 @@ void LambInferMeta(const MetaTensor& param,
}
}
void
LarsMomentumInferMeta
(
const
std
::
vector
<
const
MetaTensor
*>&
param
,
const
std
::
vector
<
const
MetaTensor
*>&
velocity
,
const
std
::
vector
<
const
MetaTensor
*>&
learning_rate
,
const
std
::
vector
<
const
MetaTensor
*>&
grad
,
const
paddle
::
optional
<
std
::
vector
<
const
MetaTensor
*>>&
master_param
,
const
std
::
vector
<
float
>&
lars_weight_decay
,
float
mu
,
float
lars_coeff
,
float
epsilon
,
bool
multi_precision
,
float
rescale_grad
,
std
::
vector
<
MetaTensor
*>
param_out
,
std
::
vector
<
MetaTensor
*>
velocity_out
,
std
::
vector
<
MetaTensor
*>
master_param_out
)
{
std
::
vector
<
DDim
>
lr_dims
=
GetMetaTensorsDim
(
learning_rate
);
std
::
vector
<
DDim
>
grad_dim
=
GetMetaTensorsDim
(
grad
);
std
::
vector
<
DDim
>
param_dim
=
GetMetaTensorsDim
(
param
);
std
::
vector
<
DDim
>
velocity_dim
=
GetMetaTensorsDim
(
velocity
);
PADDLE_ENFORCE_EQ
(
param_dim
.
size
(),
grad_dim
.
size
(),
phi
::
errors
::
InvalidArgument
(
"Input(Param) and Input(Grad) of LarsMomentumOp should have "
"same quantity. But number of Param is [%d] and Grad is [%d]."
,
param_dim
.
size
(),
grad_dim
.
size
()));
PADDLE_ENFORCE_EQ
(
param_dim
.
size
(),
velocity_dim
.
size
(),
phi
::
errors
::
InvalidArgument
(
"Input(Param) and Input(Velocity) of LarsMomentumOp should "
"have same quantity. But number of Param is [%d] and Velocity "
"is [%d]."
,
param_dim
.
size
(),
velocity_dim
.
size
()));
PADDLE_ENFORCE_EQ
(
lars_weight_decay
.
size
(),
grad_dim
.
size
(),
phi
::
errors
::
InvalidArgument
(
"Attr(Lars_weight_decay) and "
"Input(Grad) of LarsMomentumOp should have same quantity. "
"But number of Lars_weight_decay is [%d] and Grad is [%d]."
,
lars_weight_decay
.
size
(),
grad_dim
.
size
()));
for
(
auto
&
lr_dim
:
lr_dims
)
{
PADDLE_ENFORCE_EQ
(
phi
::
product
(
lr_dim
),
1
,
phi
::
errors
::
InvalidArgument
(
"Learning_rate should be a scalar. But Received "
"LearningRate's dim [%s]"
,
phi
::
product
(
lr_dim
)));
}
for
(
size_t
i
=
0
;
i
<
param_dim
.
size
();
++
i
)
{
PADDLE_ENFORCE_EQ
(
param_dim
[
i
],
grad_dim
[
i
],
phi
::
errors
::
InvalidArgument
(
"Input(Param) and Input(Grad) input of LarsMomentumOp shall "
"have same dimension. But Param`s dim is [%s] and Grad's dim "
"is [%s]."
,
param_dim
[
i
],
grad_dim
[
i
]));
PADDLE_ENFORCE_EQ
(
param_dim
[
i
],
velocity_dim
[
i
],
phi
::
errors
::
InvalidArgument
(
"Input(Param) and Input(Velocity) of LarsMomentumOp shall have "
"same dimension. But Param dim [%s] differs with Velocity dim "
"[%s]."
,
param_dim
[
i
],
velocity_dim
[
i
]));
}
for
(
size_t
i
=
0
;
i
<
param_out
.
size
();
i
++
)
{
param_out
[
i
]
->
set_dims
(
param_dim
[
i
]);
velocity_out
[
i
]
->
set_dims
(
param_dim
[
i
]);
if
(
master_param
!=
nullptr
)
{
master_param_out
[
i
]
->
set_dims
(
param_dim
[
i
]);
}
}
}
void
LLMInt8LinearInferMeta
(
const
MetaTensor
&
x
,
const
MetaTensor
&
weight
,
const
MetaTensor
&
bias
,
...
...
paddle/phi/infermeta/multiary.h
浏览文件 @
52a0a677
...
...
@@ -459,6 +459,22 @@ void LambInferMeta(const MetaTensor& param,
MetaTensor
*
beta2_pow_out
,
MetaTensor
*
master_param_outs
);
void
LarsMomentumInferMeta
(
const
std
::
vector
<
const
MetaTensor
*>&
param
,
const
std
::
vector
<
const
MetaTensor
*>&
velocity
,
const
std
::
vector
<
const
MetaTensor
*>&
learning_rate
,
const
std
::
vector
<
const
MetaTensor
*>&
grad
,
const
paddle
::
optional
<
std
::
vector
<
const
MetaTensor
*>>&
master_param
,
const
std
::
vector
<
float
>&
lars_weight_decay
,
float
mu
,
float
lars_coeff
,
float
epsilon
,
bool
multi_precision
,
float
rescale_grad
,
std
::
vector
<
MetaTensor
*>
param_out
,
std
::
vector
<
MetaTensor
*>
velocity_out
,
std
::
vector
<
MetaTensor
*>
master_param_out
);
void
LLMInt8LinearInferMeta
(
const
MetaTensor
&
x
,
const
MetaTensor
&
weight
,
const
MetaTensor
&
bias
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录