Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
564dcd52
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看板
未验证
提交
564dcd52
编写于
3月 21, 2022
作者:
F
From00
提交者:
GitHub
3月 21, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Move frobenius_norm OP to phi (#40707)
上级
df3ae18a
变更
11
隐藏空白更改
内联
并排
Showing
11 changed file
with
311 addition
and
23 deletion
+311
-23
paddle/fluid/operators/reduce_ops/frobenius_norm_op.cc
paddle/fluid/operators/reduce_ops/frobenius_norm_op.cc
+10
-17
paddle/phi/kernels/cpu/frobenius_norm_grad_kernel.cc
paddle/phi/kernels/cpu/frobenius_norm_grad_kernel.cc
+25
-0
paddle/phi/kernels/cpu/frobenius_norm_kernel.cc
paddle/phi/kernels/cpu/frobenius_norm_kernel.cc
+21
-0
paddle/phi/kernels/frobenius_norm_grad_kernel.h
paddle/phi/kernels/frobenius_norm_grad_kernel.h
+33
-0
paddle/phi/kernels/frobenius_norm_kernel.h
paddle/phi/kernels/frobenius_norm_kernel.h
+30
-0
paddle/phi/kernels/funcs/reduce_functor.h
paddle/phi/kernels/funcs/reduce_functor.h
+34
-6
paddle/phi/kernels/gpu/frobenius_norm_grad_kernel.cu
paddle/phi/kernels/gpu/frobenius_norm_grad_kernel.cu
+25
-0
paddle/phi/kernels/gpu/frobenius_norm_kernel.cu
paddle/phi/kernels/gpu/frobenius_norm_kernel.cu
+21
-0
paddle/phi/kernels/impl/frobenius_norm_grad_kernel_impl.h
paddle/phi/kernels/impl/frobenius_norm_grad_kernel_impl.h
+39
-0
paddle/phi/kernels/impl/frobenius_norm_kernel_impl.h
paddle/phi/kernels/impl/frobenius_norm_kernel_impl.h
+35
-0
paddle/phi/ops/compat/frobenius_norm_sig.cc
paddle/phi/ops/compat/frobenius_norm_sig.cc
+38
-0
未找到文件。
paddle/fluid/operators/reduce_ops/frobenius_norm_op.cc
浏览文件 @
564dcd52
...
...
@@ -12,9 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "paddle/fluid/operators/reduce_ops/frobenius_norm_op.h"
#include <string>
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/reduce_ops/reduce_op.h"
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/unary.h"
namespace
paddle
{
namespace
framework
{
...
...
@@ -56,22 +59,12 @@ class FrobeniusNormOpMaker : public ops::ReduceOpMaker {
virtual
std
::
string
GetOpType
()
const
{
return
"Reduce frobenius_norm"
;
}
};
DECLARE_INFER_SHAPE_FUNCTOR
(
frobenius_norm
,
FrobeniusNormInferShapeFunctor
,
PD_INFER_META
(
phi
::
ReduceInferMetaBase
));
REGISTER_OPERATOR
(
frobenius_norm
,
ops
::
ReduceOp
,
FrobeniusNormOpMaker
,
ops
::
FrobeniusNormOpGradMaker
<
paddle
::
framework
::
OpDesc
>
,
ops
::
FrobeniusNormOpGradMaker
<
paddle
::
imperative
::
OpBase
>
);
ops
::
FrobeniusNormOpGradMaker
<
paddle
::
imperative
::
OpBase
>
,
FrobeniusNormInferShapeFunctor
);
REGISTER_OPERATOR
(
frobenius_norm_grad
,
ops
::
ReduceGradOp
);
REGISTER_OP_CPU_KERNEL
(
frobenius_norm
,
ops
::
ReduceKernel
<
paddle
::
platform
::
CPUDeviceContext
,
float
,
ops
::
FrobeniusNormFunctor
>
,
ops
::
ReduceKernel
<
paddle
::
platform
::
CPUDeviceContext
,
double
,
ops
::
FrobeniusNormFunctor
>
);
template
<
typename
T
>
using
CPUFrobeniusNormGradKernel
=
ops
::
FrobeniusNormGradKernel
<
paddle
::
platform
::
CPUDeviceContext
,
T
,
ops
::
FrobeniusNormGradFunctor
>
;
REGISTER_OP_CPU_KERNEL
(
frobenius_norm_grad
,
CPUFrobeniusNormGradKernel
<
float
>
,
CPUFrobeniusNormGradKernel
<
double
>
);
paddle/phi/kernels/cpu/frobenius_norm_grad_kernel.cc
0 → 100644
浏览文件 @
564dcd52
// 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/kernels/frobenius_norm_grad_kernel.h"
#include "paddle/phi/kernels/impl/frobenius_norm_grad_kernel_impl.h"
#include "paddle/phi/core/kernel_registry.h"
PD_REGISTER_KERNEL
(
frobenius_norm_grad
,
CPU
,
ALL_LAYOUT
,
phi
::
FrobeniusNormGradKernel
,
float
,
double
)
{}
paddle/phi/kernels/cpu/frobenius_norm_kernel.cc
0 → 100644
浏览文件 @
564dcd52
// 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/kernels/frobenius_norm_kernel.h"
#include "paddle/phi/kernels/impl/frobenius_norm_kernel_impl.h"
#include "paddle/phi/core/kernel_registry.h"
PD_REGISTER_KERNEL
(
frobenius_norm
,
CPU
,
ALL_LAYOUT
,
phi
::
FrobeniusNormKernel
,
float
,
double
)
{}
paddle/phi/kernels/frobenius_norm_grad_kernel.h
0 → 100644
浏览文件 @
564dcd52
// 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.
#pragma once
#include <vector>
#include "paddle/phi/core/dense_tensor.h"
namespace
phi
{
template
<
typename
T
,
typename
Context
>
void
FrobeniusNormGradKernel
(
const
Context
&
ctx
,
const
DenseTensor
&
x
,
const
DenseTensor
&
out
,
const
DenseTensor
&
dout
,
const
std
::
vector
<
int64_t
>&
axis
,
bool
keep_dim
,
bool
reduce_all
,
DataType
in_dtype
,
DataType
out_dtype
,
DenseTensor
*
dx
);
}
// namespace phi
paddle/
fluid/operators/reduce_ops/frobenius_norm_op
.h
→
paddle/
phi/kernels/frobenius_norm_kernel
.h
浏览文件 @
564dcd52
// Copyright (c) 202
0
PaddlePaddle Authors. All Rights Reserved.
// Copyright (c) 202
2
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.
...
...
@@ -15,40 +15,16 @@
#pragma once
#include <vector>
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/fluid/operators/reduce_ops/reduce_op.h"
namespace
phi
{
namespace
paddle
{
namespace
operators
{
template
<
typename
T
,
typename
Context
>
void
FrobeniusNormKernel
(
const
Context
&
ctx
,
const
DenseTensor
&
x
,
const
std
::
vector
<
int64_t
>&
axis
,
bool
keep_dim
,
bool
reduce_all
,
DenseTensor
*
out
);
// \partial \| X \|_F = \frac{X}{ \| X \|_F }
template
<
typename
DeviceContext
,
typename
T
,
typename
Functor
>
class
FrobeniusNormGradKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
// default use Eigen broadcast
ReduceGradKernel
<
DeviceContext
,
T
,
Functor
,
false
>
kernel
;
kernel
.
Compute
(
context
);
}
};
struct
FrobeniusNormFunctor
{
template
<
typename
DeviceContext
,
typename
X
,
typename
Y
,
typename
Dim
>
void
operator
()(
const
DeviceContext
&
place
,
X
*
x
,
Y
*
y
,
const
Dim
&
dim
)
{
y
->
device
(
place
)
=
((
x
->
square
()).
sum
(
dim
)).
sqrt
();
}
};
struct
FrobeniusNormGradFunctor
{
template
<
typename
DeviceContext
,
typename
X
,
typename
Y
,
typename
DX
,
typename
DY
,
typename
Dim
>
void
operator
()(
const
DeviceContext
&
place
,
X
*
x
,
Y
*
y
,
DX
*
dx
,
DY
*
dy
,
const
Dim
&
dim
,
int
size
)
{
dx
->
device
(
place
)
=
y
->
broadcast
(
dim
);
dx
->
device
(
place
)
=
*
dx
+
dx
->
constant
(
1e-12
f
);
dx
->
device
(
place
)
=
(
*
x
/
*
dx
)
*
(
dy
->
broadcast
(
dim
));
}
};
}
// namespace operators
}
// namespace paddle
}
// namespace phi
paddle/phi/kernels/funcs/reduce_functor.h
浏览文件 @
564dcd52
...
...
@@ -17,11 +17,39 @@
namespace
phi
{
namespace
funcs
{
////////
Su
m Functor ///////
struct
Su
mFunctor
{
////////
Frobenius Nor
m Functor ///////
struct
FrobeniusNor
mFunctor
{
template
<
typename
DeviceContext
,
typename
X
,
typename
Y
,
typename
Dim
>
void
operator
()(
const
DeviceContext
&
place
,
X
*
x
,
Y
*
y
,
const
Dim
&
dim
)
{
y
->
device
(
place
)
=
x
->
sum
(
dim
);
y
->
device
(
place
)
=
((
x
->
square
()).
sum
(
dim
)).
sqrt
();
}
};
struct
FrobeniusNormGradFunctor
{
template
<
typename
DeviceContext
,
typename
X
,
typename
Y
,
typename
DX
,
typename
DY
,
typename
Dim
>
void
operator
()(
const
DeviceContext
&
place
,
X
*
x
,
Y
*
y
,
DX
*
dx
,
DY
*
dy
,
const
Dim
&
dim
,
int
size
)
{
dx
->
device
(
place
)
=
y
->
broadcast
(
dim
);
dx
->
device
(
place
)
=
*
dx
+
dx
->
constant
(
1e-12
f
);
dx
->
device
(
place
)
=
(
*
x
/
*
dx
)
*
(
dy
->
broadcast
(
dim
));
}
};
//////// Max Functor ///////
struct
MaxFunctor
{
template
<
typename
DeviceContext
,
typename
X
,
typename
Y
,
typename
Dim
>
void
operator
()(
const
DeviceContext
&
place
,
X
*
x
,
Y
*
y
,
const
Dim
&
dim
)
{
y
->
device
(
place
)
=
x
->
maximum
(
dim
);
}
};
...
...
@@ -41,11 +69,11 @@ struct ProdFunctor {
}
};
////////
Max
Functor ///////
struct
Max
Functor
{
////////
Sum
Functor ///////
struct
Sum
Functor
{
template
<
typename
DeviceContext
,
typename
X
,
typename
Y
,
typename
Dim
>
void
operator
()(
const
DeviceContext
&
place
,
X
*
x
,
Y
*
y
,
const
Dim
&
dim
)
{
y
->
device
(
place
)
=
x
->
maxim
um
(
dim
);
y
->
device
(
place
)
=
x
->
s
um
(
dim
);
}
};
...
...
paddle/phi/kernels/gpu/frobenius_norm_grad_kernel.cu
0 → 100644
浏览文件 @
564dcd52
// 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/kernels/frobenius_norm_grad_kernel.h"
#include "paddle/phi/kernels/impl/frobenius_norm_grad_kernel_impl.h"
#include "paddle/phi/core/kernel_registry.h"
PD_REGISTER_KERNEL
(
frobenius_norm_grad
,
GPU
,
ALL_LAYOUT
,
phi
::
FrobeniusNormGradKernel
,
float
,
double
)
{}
paddle/phi/kernels/gpu/frobenius_norm_kernel.cu
0 → 100644
浏览文件 @
564dcd52
// 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/kernels/frobenius_norm_kernel.h"
#include "paddle/phi/kernels/impl/frobenius_norm_kernel_impl.h"
#include "paddle/phi/core/kernel_registry.h"
PD_REGISTER_KERNEL
(
frobenius_norm
,
GPU
,
ALL_LAYOUT
,
phi
::
FrobeniusNormKernel
,
float
,
double
)
{}
paddle/phi/kernels/impl/frobenius_norm_grad_kernel_impl.h
0 → 100644
浏览文件 @
564dcd52
// 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.
#pragma once
#include "paddle/phi/kernels/frobenius_norm_grad_kernel.h"
#include "paddle/phi/kernels/funcs/reduce_functor.h"
#include "paddle/phi/kernels/impl/reduce_grad.h"
namespace
phi
{
template
<
typename
T
,
typename
Context
>
void
FrobeniusNormGradKernel
(
const
Context
&
ctx
,
const
DenseTensor
&
x
,
const
DenseTensor
&
out
,
const
DenseTensor
&
dout
,
const
std
::
vector
<
int64_t
>&
axis
,
bool
keep_dim
,
bool
reduce_all
,
DataType
in_dtype
,
DataType
out_dtype
,
DenseTensor
*
dx
)
{
ReduceGradKernel
<
Context
,
T
,
funcs
::
FrobeniusNormGradFunctor
>
(
ctx
,
x
,
dout
,
out
,
axis
,
keep_dim
,
reduce_all
,
in_dtype
,
out_dtype
,
dx
);
}
}
// namespace phi
paddle/
fluid/operators/reduce_ops/frobenius_norm_op.cu
→
paddle/
phi/kernels/impl/frobenius_norm_kernel_impl.h
浏览文件 @
564dcd52
// Copyright (c) 202
0
PaddlePaddle Authors. All Rights Reserved.
// Copyright (c) 202
2
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.
...
...
@@ -12,21 +12,24 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "paddle/fluid/operators/reduce_ops/frobenius_norm_op.h"
#include "paddle/fluid/operators/reduce_ops/reduce_op.cu.h"
#pragma once
template
<
typename
T
>
using
CUDAFrobeniusNormKernel
=
ops
::
ReduceKernel
<
paddle
::
platform
::
CUDADeviceContext
,
T
,
ops
::
FrobeniusNormFunctor
>
;
#include "paddle/phi/kernels/frobenius_norm_kernel.h"
REGISTER_OP_CUDA_KERNEL
(
frobenius_norm
,
CUDAFrobeniusNormKernel
<
float
>
,
CUDAFrobeniusNormKernel
<
double
>
);
#include "paddle/phi/kernels/cpu/reduce.h"
#include "paddle/phi/kernels/funcs/reduce_functor.h"
template
<
typename
T
>
using
CUDAFrobeniusNormGradKernel
=
ops
::
ReduceGradKernel
<
paddle
::
platform
::
CUDADeviceContext
,
T
,
ops
::
FrobeniusNormGradFunctor
>
;
namespace
phi
{
REGISTER_OP_CUDA_KERNEL
(
frobenius_norm_grad
,
CUDAFrobeniusNormGradKernel
<
float
>
,
CUDAFrobeniusNormGradKernel
<
double
>
);
template
<
typename
T
,
typename
Context
>
void
FrobeniusNormKernel
(
const
Context
&
ctx
,
const
DenseTensor
&
x
,
const
std
::
vector
<
int64_t
>&
axis
,
bool
keep_dim
,
bool
reduce_all
,
DenseTensor
*
out
)
{
Reduce
<
Context
,
T
,
funcs
::
FrobeniusNormFunctor
>
(
ctx
,
x
,
reduce_all
,
axis
,
keep_dim
,
x
.
dtype
(),
out
);
}
}
// namespace phi
paddle/phi/ops/compat/frobenius_norm_sig.cc
0 → 100644
浏览文件 @
564dcd52
/* 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
FrobeniusNormOpArgumentMapping
(
const
ArgumentMappingContext
&
ctx
)
{
return
KernelSignature
(
"frobenius_norm"
,
{
"X"
},
{
"dim"
,
"keep_dim"
,
"reduce_all"
},
{
"Out"
});
}
KernelSignature
FrobeniusNormGradOpArgumentMapping
(
const
ArgumentMappingContext
&
ctx
)
{
return
KernelSignature
(
"frobenius_norm_grad"
,
{
"X"
,
"Out"
,
GradVarName
(
"Out"
)},
{
"dim"
,
"keep_dim"
,
"reduce_all"
,
"in_dtype"
,
"out_dtype"
},
{
GradVarName
(
"X"
)});
}
}
// namespace phi
PD_REGISTER_ARG_MAPPING_FN
(
frobenius_norm
,
phi
::
FrobeniusNormOpArgumentMapping
);
PD_REGISTER_ARG_MAPPING_FN
(
frobenius_norm_grad
,
phi
::
FrobeniusNormGradOpArgumentMapping
);
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录