Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
0d920178
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
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看板
未验证
提交
0d920178
编写于
8月 08, 2023
作者:
W
Wang Xin
提交者:
GitHub
8月 08, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
move `decayed_adagrad_op` to phi (#55995)
* move decayed_adagrad_op to phi * fix bug
上级
3c03ade8
变更
10
显示空白变更内容
内联
并排
Showing
10 changed file
with
213 addition
and
80 deletion
+213
-80
paddle/fluid/operators/optimizers/decayed_adagrad_op.cc
paddle/fluid/operators/optimizers/decayed_adagrad_op.cc
+11
-5
paddle/fluid/operators/optimizers/decayed_adagrad_op.h
paddle/fluid/operators/optimizers/decayed_adagrad_op.h
+0
-73
paddle/phi/infermeta/multiary.cc
paddle/phi/infermeta/multiary.cc
+39
-0
paddle/phi/infermeta/multiary.h
paddle/phi/infermeta/multiary.h
+9
-0
paddle/phi/kernels/cpu/decayed_adagrad_kernel.cc
paddle/phi/kernels/cpu/decayed_adagrad_kernel.cc
+21
-0
paddle/phi/kernels/decayed_adagrad_kernel.h
paddle/phi/kernels/decayed_adagrad_kernel.h
+31
-0
paddle/phi/kernels/gpu/decayed_adagrad_kernel.cu
paddle/phi/kernels/gpu/decayed_adagrad_kernel.cu
+21
-0
paddle/phi/kernels/impl/decayed_adagrad_kernel_impl.h
paddle/phi/kernels/impl/decayed_adagrad_kernel_impl.h
+49
-0
paddle/phi/ops/compat/decayed_adagrad_sig.cc
paddle/phi/ops/compat/decayed_adagrad_sig.cc
+30
-0
test/legacy_test/test_decayed_adagrad_op.py
test/legacy_test/test_decayed_adagrad_op.py
+2
-2
未找到文件。
paddle/fluid/operators/optimizers/decayed_adagrad_op.cc
浏览文件 @
0d920178
...
...
@@ -12,7 +12,10 @@ 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/fluid/operators/optimizers/decayed_adagrad_op.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
{
...
...
@@ -127,9 +130,12 @@ stability to avoid the division by zero error.
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
DECLARE_INFER_SHAPE_FUNCTOR
(
decayed_adagrad
,
DecayedAdagradShapeFunctor
,
PD_INFER_META
(
phi
::
DecayedAdagradInferMeta
));
REGISTER_OP_WITHOUT_GRADIENT
(
decayed_adagrad
,
ops
::
DecayedAdagradOp
,
ops
::
DecayedAdagradOpMaker
);
PD_REGISTER_STRUCT_KERNEL
(
decayed_adagrad
,
CPU
,
ALL_LAYOUT
,
ops
::
DecayedAdagradOpKernel
,
float
)
{}
ops
::
DecayedAdagradOpMaker
,
DecayedAdagradShapeFunctor
);
paddle/fluid/operators/optimizers/decayed_adagrad_op.h
已删除
100644 → 0
浏览文件 @
3c03ade8
/* 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. */
#pragma once
#include "paddle/fluid/framework/eigen.h"
#include "paddle/fluid/framework/op_registry.h"
namespace
paddle
{
namespace
operators
{
template
<
typename
T
,
typename
DeviceContext
>
class
DecayedAdagradOpKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
ctx
)
const
override
{
const
auto
*
param_var
=
ctx
.
InputVar
(
"Param"
);
PADDLE_ENFORCE_EQ
(
param_var
->
IsType
<
phi
::
DenseTensor
>
(),
true
,
platform
::
errors
::
InvalidArgument
(
"The Var(%s)'s type should be phi::DenseTensor, "
"but the received is %s"
,
ctx
.
InputNames
(
"Param"
).
front
(),
framework
::
ToTypeName
(
param_var
->
Type
())));
const
auto
*
grad_var
=
ctx
.
InputVar
(
"Grad"
);
PADDLE_ENFORCE_EQ
(
grad_var
->
IsType
<
phi
::
DenseTensor
>
(),
true
,
platform
::
errors
::
InvalidArgument
(
"The Var(%s)'s type should be phi::DenseTensor, "
"but the received is %s"
,
ctx
.
InputNames
(
"Grad"
).
front
(),
framework
::
ToTypeName
(
grad_var
->
Type
())));
auto
param_out_tensor
=
ctx
.
Output
<
phi
::
DenseTensor
>
(
"ParamOut"
);
auto
moment_out_tensor
=
ctx
.
Output
<
phi
::
DenseTensor
>
(
"MomentOut"
);
param_out_tensor
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
moment_out_tensor
->
mutable_data
<
T
>
(
ctx
.
GetPlace
());
float
decay
=
ctx
.
Attr
<
float
>
(
"decay"
);
float
epsilon
=
ctx
.
Attr
<
float
>
(
"epsilon"
);
auto
param
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
ctx
.
Input
<
phi
::
DenseTensor
>
(
"Param"
));
auto
grad
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
ctx
.
Input
<
phi
::
DenseTensor
>
(
"Grad"
));
auto
moment
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
ctx
.
Input
<
phi
::
DenseTensor
>
(
"Moment"
));
auto
lr
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
ctx
.
Input
<
phi
::
DenseTensor
>
(
"LearningRate"
));
auto
param_out
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
param_out_tensor
);
auto
moment_out
=
framework
::
EigenVector
<
T
>::
Flatten
(
*
moment_out_tensor
);
auto
&
place
=
*
ctx
.
template
device_context
<
DeviceContext
>().
eigen_device
();
moment_out
.
device
(
place
)
=
decay
*
moment
+
(
1
-
decay
)
*
grad
*
grad
;
Eigen
::
DSizes
<
int
,
1
>
m_dsize
(
moment_out_tensor
->
numel
());
param_out
.
device
(
place
)
=
param
-
lr
.
broadcast
(
m_dsize
)
*
grad
/
(
moment_out
.
sqrt
()
+
epsilon
);
}
};
}
// namespace operators
}
// namespace paddle
paddle/phi/infermeta/multiary.cc
浏览文件 @
0d920178
...
...
@@ -1057,6 +1057,45 @@ void CudnnLSTMInferMeta(
state_out
->
set_dtype
(
phi
::
DataType
::
UINT8
);
}
void
DecayedAdagradInferMeta
(
const
MetaTensor
&
param
,
const
MetaTensor
&
grad
,
const
MetaTensor
&
moment
,
const
MetaTensor
&
learning_rate
,
float
decay
,
float
epsilon
,
MetaTensor
*
param_out
,
MetaTensor
*
moment_out
)
{
auto
lr_dims
=
learning_rate
.
dims
();
PADDLE_ENFORCE_NE
(
phi
::
product
(
lr_dims
),
0
,
phi
::
errors
::
InvalidArgument
(
"Maybe the Input variable LearningRate has not "
"been initialized. You may need to confirm "
"if you put exe.run(startup_program) "
"after optimizer.minimize function."
));
PADDLE_ENFORCE_EQ
(
phi
::
product
(
lr_dims
),
1
,
phi
::
errors
::
InvalidArgument
(
"LearningRate should have one element"
));
auto
param_dims
=
param
.
dims
();
PADDLE_ENFORCE_EQ
(
param_dims
,
grad
.
dims
(),
phi
::
errors
::
InvalidArgument
(
"Param and Grad input of DecayedAdagradOp should have "
"the same dimension."
));
PADDLE_ENFORCE_EQ
(
param_dims
,
moment
.
dims
(),
phi
::
errors
::
InvalidArgument
(
"Param and Moment input of DecayedAdagradOp should have "
"the same dimension."
));
param_out
->
set_dims
(
param_dims
);
param_out
->
set_dtype
(
param
.
dtype
());
moment_out
->
set_dims
(
param_dims
);
moment_out
->
set_dtype
(
param
.
dtype
());
}
inline
int
ConvOutputSize
(
int
input_size
,
int
filter_size
,
int
dilation
,
int
padding
,
int
stride
)
{
const
int
dkernel
=
dilation
*
(
filter_size
-
1
)
+
1
;
...
...
paddle/phi/infermeta/multiary.h
浏览文件 @
0d920178
...
...
@@ -258,6 +258,15 @@ void CudnnLSTMInferMeta(
MetaTensor
*
reserve
,
MetaTensor
*
state_out
);
void
DecayedAdagradInferMeta
(
const
MetaTensor
&
param
,
const
MetaTensor
&
grad
,
const
MetaTensor
&
moment
,
const
MetaTensor
&
learning_rate
,
float
decay
,
float
epsilon
,
MetaTensor
*
param_out
,
MetaTensor
*
moment_out
);
void
DeformableConvInferMeta
(
const
MetaTensor
&
x
,
const
MetaTensor
&
offset
,
const
MetaTensor
&
filter
,
...
...
paddle/phi/kernels/cpu/decayed_adagrad_kernel.cc
0 → 100644
浏览文件 @
0d920178
// 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/decayed_adagrad_kernel.h"
#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/decayed_adagrad_kernel_impl.h"
PD_REGISTER_KERNEL
(
decayed_adagrad
,
CPU
,
ALL_LAYOUT
,
phi
::
DecayedAdagradDenseKernel
,
float
)
{}
paddle/phi/kernels/decayed_adagrad_kernel.h
0 → 100644
浏览文件 @
0d920178
// 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.
#pragma once
#include "paddle/phi/core/dense_tensor.h"
namespace
phi
{
template
<
typename
T
,
typename
Context
>
void
DecayedAdagradDenseKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
param
,
const
DenseTensor
&
grad
,
const
DenseTensor
&
moment
,
const
DenseTensor
&
learning_rate
,
float
decay
,
float
epsilon
,
DenseTensor
*
param_out
,
DenseTensor
*
moment_out
);
}
// namespace phi
paddle/phi/kernels/gpu/decayed_adagrad_kernel.cu
0 → 100644
浏览文件 @
0d920178
// 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/decayed_adagrad_kernel.h"
#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/decayed_adagrad_kernel_impl.h"
PD_REGISTER_KERNEL
(
decayed_adagrad
,
GPU
,
ALL_LAYOUT
,
phi
::
DecayedAdagradDenseKernel
,
float
)
{}
paddle/phi/kernels/impl/decayed_adagrad_kernel_impl.h
0 → 100644
浏览文件 @
0d920178
// 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.
#pragma once
#include "paddle/phi/kernels/decayed_adagrad_kernel.h"
#include "paddle/phi/kernels/funcs/eigen/common.h"
namespace
phi
{
template
<
typename
T
,
typename
Context
>
void
DecayedAdagradDenseKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
param_t
,
const
DenseTensor
&
grad_t
,
const
DenseTensor
&
moment_t
,
const
DenseTensor
&
learning_rate
,
float
decay
,
float
epsilon
,
DenseTensor
*
param_out_t
,
DenseTensor
*
moment_out_t
)
{
dev_ctx
.
template
Alloc
<
T
>(
param_out_t
);
dev_ctx
.
template
Alloc
<
T
>(
moment_out_t
);
auto
param
=
EigenVector
<
T
>::
Flatten
(
param_t
);
auto
grad
=
EigenVector
<
T
>::
Flatten
(
grad_t
);
auto
moment
=
EigenVector
<
T
>::
Flatten
(
moment_t
);
auto
lr
=
EigenVector
<
T
>::
Flatten
(
learning_rate
);
auto
param_out
=
EigenVector
<
T
>::
Flatten
(
*
param_out_t
);
auto
moment_out
=
EigenVector
<
T
>::
Flatten
(
*
moment_out_t
);
auto
&
place
=
*
dev_ctx
.
eigen_device
();
moment_out
.
device
(
place
)
=
decay
*
moment
+
(
1
-
decay
)
*
grad
*
grad
;
Eigen
::
DSizes
<
int
,
1
>
m_dsize
(
moment_out_t
->
numel
());
param_out
.
device
(
place
)
=
param
-
lr
.
broadcast
(
m_dsize
)
*
grad
/
(
moment_out
.
sqrt
()
+
epsilon
);
}
}
// namespace phi
paddle/
fluid/operators/optimizers/decayed_adagrad_op.cu
→
paddle/
phi/ops/compat/decayed_adagrad_sig.cc
浏览文件 @
0d920178
/* Copyright (c) 20
16
PaddlePaddle Authors. All Rights Reserved.
/* Copyright (c) 20
23
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.
...
...
@@ -11,9 +11,20 @@ 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/fluid/operators/optimizers/decayed_adagrad_op.h"
namespace
ops
=
paddle
::
operators
;
#include "paddle/phi/core/compat/op_utils.h"
PD_REGISTER_STRUCT_KERNEL
(
decayed_adagrad
,
GPU
,
ALL_LAYOUT
,
ops
::
DecayedAdagradOpKernel
,
float
)
{}
namespace
phi
{
KernelSignature
DecayedAdagradOpArgumentMapping
(
const
ArgumentMappingContext
&
ctx
UNUSED
)
{
return
KernelSignature
(
"decayed_adagrad"
,
{
"Param"
,
"Grad"
,
"Moment"
,
"LearningRate"
},
{
"decay"
,
"epsilon"
},
{
"ParamOut"
,
"MomentOut"
});
}
}
// namespace phi
PD_REGISTER_ARG_MAPPING_FN
(
decayed_adagrad
,
phi
::
DecayedAdagradOpArgumentMapping
);
test/legacy_test/test_decayed_adagrad_op.py
浏览文件 @
0d920178
...
...
@@ -46,7 +46,7 @@ class TestDecayedAdagradOp1(OpTest):
self
.
outputs
=
{
'ParamOut'
:
param_out
,
'MomentOut'
:
moment_out
}
def
test_check_output
(
self
):
self
.
check_output
()
self
.
check_output
(
check_dygraph
=
False
)
class
TestDecayedAdagradOp2
(
OpTest
):
...
...
@@ -77,7 +77,7 @@ class TestDecayedAdagradOp2(OpTest):
self
.
outputs
=
{
'ParamOut'
:
param_out
,
'MomentOut'
:
moment_out
}
def
test_check_output
(
self
):
self
.
check_output
()
self
.
check_output
(
check_dygraph
=
False
)
if
__name__
==
"__main__"
:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录