Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
2b98993b
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看板
未验证
提交
2b98993b
编写于
3月 22, 2023
作者:
R
RedContritio
提交者:
GitHub
3月 22, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
support auto generate for p_norm (#51590)
* supoort auto generate p_norm * fix bug in backward
上级
ec877d1f
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
34 addition
and
184 deletion
+34
-184
paddle/fluid/operators/p_norm_op.cc
paddle/fluid/operators/p_norm_op.cc
+0
-139
paddle/phi/api/yaml/backward.yaml
paddle/phi/api/yaml/backward.yaml
+10
-0
paddle/phi/api/yaml/legacy_backward.yaml
paddle/phi/api/yaml/legacy_backward.yaml
+0
-10
paddle/phi/api/yaml/legacy_ops.yaml
paddle/phi/api/yaml/legacy_ops.yaml
+0
-9
paddle/phi/api/yaml/op_compat.yaml
paddle/phi/api/yaml/op_compat.yaml
+7
-0
paddle/phi/api/yaml/op_version.yaml
paddle/phi/api/yaml/op_version.yaml
+8
-0
paddle/phi/api/yaml/ops.yaml
paddle/phi/api/yaml/ops.yaml
+9
-0
paddle/phi/ops/compat/p_norm_sig.cc
paddle/phi/ops/compat/p_norm_sig.cc
+0
-26
未找到文件。
paddle/fluid/operators/p_norm_op.cc
已删除
100644 → 0
浏览文件 @
ec877d1f
/* Copyright (c) 2020 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.
Indicesou 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 <memory>
#include <string>
#include <vector>
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/op_version_registry.h"
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/backward.h"
#include "paddle/phi/infermeta/unary.h"
namespace
paddle
{
namespace
operators
{
class
PnormOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
public:
void
Make
()
override
{
AddInput
(
"X"
,
"(Tensor) A tensor of rank >= axis."
);
AddAttr
<
float
>
(
"porder"
,
"(float, default 2) The porder is the p order vector norm "
"to calculate. Available for porder=0, inf, -inf and any "
"real number."
)
.
SetDefault
(
2.0
f
);
AddAttr
<
int
>
(
"axis"
,
"The axis on which to apply norm operation. If axis < 0, "
"the dimension to pnorm is rank(X) + axis. -1 is "
"the last dimension."
)
.
SetDefault
(
-
1
);
AddAttr
<
float
>
(
"epsilon"
,
"(float, default 1e-12) The epsilon value is used "
"to avoid division by zero."
)
.
SetDefault
(
1.0e-12
f
);
AddAttr
<
bool
>
(
"keepdim"
,
"(bool, default false) Whether to keep the dimensions as the input."
)
.
SetDefault
(
false
);
AddAttr
<
bool
>
(
"asvector"
,
"(bool, default false) as vector norm when axis is None and "
"input is matrix, "
)
.
SetDefault
(
false
);
AddOutput
(
"Out"
,
"(Tensor) Output result tensor of p-norm"
);
AddComment
(
R"DOC(
Pnorm Operator.
Given a tensor X, compute Lp-norm of X.
When p = 0, defining $0^0 = 0$, the zero-norm of X is simply the number of non-zero elements of X.
$$
||X||_{0} = \lim_{p \rightarrow 0} \sum_i |x_i|^p
$$
When p = inf, the inf-norm of X is the maximum element of X.
$$
||X||_\infty = \max_i |x_i|
$$
When p = -inf, the negative-inf-norm of X is the minimum element of X.
$$
||X||_{-\infty} = \min_i |x_i|
$$
Otherwise, the p-norm of X follows the formula,
$$
||X||_{p} = (\sum_i |x_i|^p)^{1/p}
$$
where, $\sum_i $ is calculated along the `axis` dimension.
)DOC"
);
}
};
class
PnormOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
};
class
PnormOpGrad
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
};
template
<
typename
T
>
class
PnormOpGradOpMaker
:
public
framework
::
SingleGradOpMaker
<
T
>
{
public:
using
framework
::
SingleGradOpMaker
<
T
>::
SingleGradOpMaker
;
protected:
void
Apply
(
GradOpPtr
<
T
>
op
)
const
override
{
op
->
SetType
(
"p_norm_grad"
);
op
->
SetAttrMap
(
this
->
Attrs
());
op
->
SetInput
(
"X"
,
this
->
Input
(
"X"
));
op
->
SetInput
(
"Out"
,
this
->
Output
(
"Out"
));
op
->
SetInput
(
framework
::
GradVarName
(
"Out"
),
this
->
OutputGrad
(
"Out"
));
op
->
SetOutput
(
framework
::
GradVarName
(
"X"
),
this
->
InputGrad
(
"X"
));
}
};
}
// namespace operators
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
using
CPU
=
phi
::
CPUContext
;
DECLARE_INFER_SHAPE_FUNCTOR
(
p_norm
,
PNormInferShapeFunctor
,
PD_INFER_META
(
phi
::
PNormInferMeta
));
DECLARE_INFER_SHAPE_FUNCTOR
(
p_norm_grad
,
PNormGradInferShapeFunctor
,
PD_INFER_META
(
phi
::
GeneralUnaryGradInferMeta
));
REGISTER_OPERATOR
(
p_norm
,
ops
::
PnormOp
,
ops
::
PnormOpMaker
,
ops
::
PnormOpGradOpMaker
<
paddle
::
framework
::
OpDesc
>
,
ops
::
PnormOpGradOpMaker
<
paddle
::
imperative
::
OpBase
>
,
PNormInferShapeFunctor
);
REGISTER_OPERATOR
(
p_norm_grad
,
ops
::
PnormOpGrad
,
PNormGradInferShapeFunctor
);
REGISTER_OP_VERSION
(
p_norm
).
AddCheckpoint
(
R"ROC(
Upgrade p_norm, add 1 attribute [asvector].
)ROC"
,
paddle
::
framework
::
compatible
::
OpVersionDesc
().
NewAttr
(
"asvector"
,
"Compute as vector when axis is None and input is matrix"
,
false
));
paddle/phi/api/yaml/backward.yaml
浏览文件 @
2b98993b
...
...
@@ -1028,6 +1028,16 @@
func
:
overlap_add_grad
data_type
:
x
-
backward_op
:
p_norm_grad
forward
:
p_norm(Tensor x, float porder=2, int axis=-1, float epsilon=1.0e-12f, bool keepdim=false, bool asvector=false) -> Tensor(out)
args
:
(Tensor x, Tensor out, Tensor out_grad, float porder, int axis, float epsilon, bool keepdim, bool asvector)
output
:
Tensor(x_grad)
infer_meta
:
func
:
GeneralUnaryGradInferMeta
param
:
[
x
]
kernel
:
func
:
p_norm_grad
-
backward_op
:
pixel_shuffle_grad
forward
:
pixel_shuffle (Tensor x, int upscale_factor=1, str data_format="NCHW") -> Tensor(out)
args
:
(Tensor out_grad, int upscale_factor, str data_format)
...
...
paddle/phi/api/yaml/legacy_backward.yaml
浏览文件 @
2b98993b
...
...
@@ -863,16 +863,6 @@
kernel
:
func
:
norm_grad
-
backward_op
:
p_norm_grad
forward
:
p_norm(Tensor x, float porder, int axis, float epsilon, bool keepdim, bool asvector=false) -> Tensor(out)
args
:
(Tensor x, Tensor out, Tensor out_grad, float porder, int axis, float epsilon, bool keepdim, bool asvector)
output
:
Tensor(x_grad)
infer_meta
:
func
:
UnchangedInferMeta
param
:
[
x
]
kernel
:
func
:
p_norm_grad
-
backward_op
:
pad3d_double_grad
forward
:
pad3d_grad(Tensor x, Tensor grad_out, IntArray paddings, str mode, float pad_value, str data_format) -> Tensor(grad_x)
args
:
(Tensor grad_x_grad, IntArray paddings, str mode, float pad_value, str data_format)
...
...
paddle/phi/api/yaml/legacy_ops.yaml
浏览文件 @
2b98993b
...
...
@@ -1235,15 +1235,6 @@
output
:
Tensor(out)
invoke
:
full_like(x, 1, dtype, place)
-
op
:
p_norm
args
:
(Tensor x, float porder, int axis, float epsilon, bool keepdim, bool asvector=false)
output
:
Tensor(out)
infer_meta
:
func
:
PNormInferMeta
kernel
:
func
:
p_norm
backward
:
p_norm_grad
-
op
:
pad
args
:
(Tensor x, int[] paddings, Scalar pad_value)
output
:
Tensor
...
...
paddle/phi/api/yaml/op_compat.yaml
浏览文件 @
2b98993b
...
...
@@ -1290,6 +1290,13 @@
outputs
:
out
:
Out
-
op
:
p_norm
backward
:
p_norm_grad
inputs
:
x
:
X
outputs
:
out
:
Out
-
op
:
pad2d
backward
:
pad2d_grad
extra
:
...
...
paddle/phi/api/yaml/op_version.yaml
浏览文件 @
2b98993b
...
...
@@ -102,6 +102,14 @@
comment
:
In order to force fill output variable to gpu memory.
default
:
"
false"
-
op
:
p_norm
version
:
-
checkpoint
:
Upgrade p_norm, add 1 attribute [asvector].
action
:
-
add_attr
:
asvector
comment
:
Compute as vector when axis is None and input is matrix.
default
:
"
false"
-
op
:
pixel_shuffle
version
:
-
checkpoint
:
Compatible upgrade of pixel_shuffle, add a new attribute [data_format]
...
...
paddle/phi/api/yaml/ops.yaml
浏览文件 @
2b98993b
...
...
@@ -1078,6 +1078,15 @@
data_type
:
x
backward
:
overlap_add_grad
-
op
:
p_norm
args
:
(Tensor x, float porder=2, int axis=-1, float epsilon=1.0e-12f, bool keepdim=false, bool asvector=false)
output
:
Tensor(out)
infer_meta
:
func
:
PNormInferMeta
kernel
:
func
:
p_norm
backward
:
p_norm_grad
-
op
:
pixel_shuffle
args
:
(Tensor x, int upscale_factor=1, str data_format="NCHW")
output
:
Tensor
...
...
paddle/phi/ops/compat/p_norm_sig.cc
已删除
100644 → 0
浏览文件 @
ec877d1f
// Copyright (c) 2022 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/core/compat/op_utils.h"
namespace
phi
{
KernelSignature
PNormGradOpArgumentMapping
(
const
ArgumentMappingContext
&
ctx
)
{
return
KernelSignature
(
"p_norm_grad"
,
{
"X"
,
"Out"
,
"Out@GRAD"
},
{
"porder"
,
"axis"
,
"epsilon"
,
"keepdim"
,
"asvector"
},
{
"X@GRAD"
});
}
}
// namespace phi
PD_REGISTER_ARG_MAPPING_FN
(
p_norm_grad
,
phi
::
PNormGradOpArgumentMapping
);
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录