Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
ea4b2c5e
P
Paddle
项目概览
PaddlePaddle
/
Paddle
接近 2 年 前同步成功
通知
2323
Star
20933
Fork
5424
代码
文件
提交
分支
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看板
未验证
提交
ea4b2c5e
编写于
7月 22, 2022
作者:
F
freeliuzc
提交者:
GitHub
7月 22, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[phi] move inverse op from fluid to phi (#44471)
* move inverse from fluid to phi with unitest bug * fix bug, add eager op yaml
上级
8ccbb863
变更
20
隐藏空白更改
内联
并排
Showing
20 changed file
with
363 addition
and
168 deletion
+363
-168
paddle/fluid/operators/inverse_op.cc
paddle/fluid/operators/inverse_op.cc
+22
-64
paddle/fluid/operators/inverse_op.cu.cc
paddle/fluid/operators/inverse_op.cu.cc
+0
-26
paddle/fluid/operators/inverse_op.h
paddle/fluid/operators/inverse_op.h
+0
-73
paddle/phi/api/yaml/legacy_api.yaml
paddle/phi/api/yaml/legacy_api.yaml
+9
-0
paddle/phi/api/yaml/legacy_backward.yaml
paddle/phi/api/yaml/legacy_backward.yaml
+9
-0
paddle/phi/infermeta/backward.cc
paddle/phi/infermeta/backward.cc
+8
-0
paddle/phi/infermeta/backward.h
paddle/phi/infermeta/backward.h
+4
-0
paddle/phi/infermeta/unary.cc
paddle/phi/infermeta/unary.cc
+37
-0
paddle/phi/infermeta/unary.h
paddle/phi/infermeta/unary.h
+2
-0
paddle/phi/kernels/cpu/inverse_grad_kernel.cc
paddle/phi/kernels/cpu/inverse_grad_kernel.cc
+20
-0
paddle/phi/kernels/cpu/inverse_kernel.cc
paddle/phi/kernels/cpu/inverse_kernel.cc
+20
-0
paddle/phi/kernels/gpu/inverse_grad_kernel.cu
paddle/phi/kernels/gpu/inverse_grad_kernel.cu
+22
-0
paddle/phi/kernels/gpu/inverse_kernel.cu
paddle/phi/kernels/gpu/inverse_kernel.cu
+22
-0
paddle/phi/kernels/impl/inverse_grad_kernel_impl.h
paddle/phi/kernels/impl/inverse_grad_kernel_impl.h
+52
-0
paddle/phi/kernels/impl/inverse_kernel_impl.h
paddle/phi/kernels/impl/inverse_kernel_impl.h
+36
-0
paddle/phi/kernels/inverse_grad_kernel.h
paddle/phi/kernels/inverse_grad_kernel.h
+27
-0
paddle/phi/kernels/inverse_kernel.h
paddle/phi/kernels/inverse_kernel.h
+28
-0
paddle/phi/ops/compat/inverse_sig.cc
paddle/phi/ops/compat/inverse_sig.cc
+26
-0
python/paddle/fluid/tests/unittests/test_inverse_op.py
python/paddle/fluid/tests/unittests/test_inverse_op.py
+16
-4
python/paddle/tensor/math.py
python/paddle/tensor/math.py
+3
-1
未找到文件。
paddle/fluid/operators/inverse_op.cc
浏览文件 @
ea4b2c5e
...
...
@@ -12,57 +12,23 @@ 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/inverse_op.h"
#include <string>
#include <unordered_map>
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/backward.h"
#include "paddle/phi/infermeta/unary.h"
#include "paddle/phi/kernels/funcs/blas/blas.h"
#include "paddle/phi/kernels/funcs/matrix_inverse.h"
namespace
paddle
{
namespace
operators
{
class
InverseOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
OP_INOUT_CHECK
(
ctx
->
HasInput
(
"Input"
),
"Input"
,
"Input"
,
"Inverse"
);
OP_INOUT_CHECK
(
ctx
->
HasOutput
(
"Output"
),
"Output"
,
"Output"
,
"Inverse"
);
auto
input_dims
=
ctx
->
GetInputDim
(
"Input"
);
int64_t
input_rank
=
input_dims
.
size
();
PADDLE_ENFORCE_GE
(
input_rank
,
2
,
platform
::
errors
::
InvalidArgument
(
"The dimension of Input(Input) is expected to be no less than 2. "
"But received: Input(Input)'s dimension = %d, shape = [%s]."
,
input_rank
,
input_dims
));
for
(
int64_t
i
=
0
;
i
<
input_rank
;
++
i
)
{
PADDLE_ENFORCE_EQ
(
(
input_dims
[
i
]
==
-
1
)
||
(
input_dims
[
i
]
>
0
),
true
,
platform
::
errors
::
InvalidArgument
(
"Each dimension of input tensor is expected to be -1 or a "
"positive number, but received %d. Input's shape is [%s]."
,
input_dims
[
i
],
input_dims
));
}
if
(
input_dims
[
input_rank
-
2
]
>
0
&&
input_dims
[
input_rank
-
1
]
>
0
)
{
PADDLE_ENFORCE_EQ
(
input_dims
[
input_rank
-
2
],
input_dims
[
input_rank
-
1
],
platform
::
errors
::
InvalidArgument
(
"The last two dimensions are expected to be equal. "
"But received: %d and %d; "
"Input(Input)'s shape = [%s]."
,
input_dims
[
input_rank
-
2
],
input_dims
[
input_rank
-
1
],
input_dims
));
}
ctx
->
SetOutputDim
(
"Output"
,
input_dims
);
ctx
->
ShareLoD
(
"Input"
,
/*->*/
"Output"
);
}
};
class
InverseOpInferVarType
:
public
framework
::
PassInDtypeAndVarTypeToOutput
{
...
...
@@ -78,19 +44,6 @@ class InverseOpInferVarType : public framework::PassInDtypeAndVarTypeToOutput {
class
InverseGradOp
:
public
framework
::
OperatorWithKernel
{
public:
using
framework
::
OperatorWithKernel
::
OperatorWithKernel
;
void
InferShape
(
framework
::
InferShapeContext
*
ctx
)
const
override
{
auto
input_grad
=
framework
::
GradVarName
(
"Input"
);
auto
output_grad
=
framework
::
GradVarName
(
"Output"
);
OP_INOUT_CHECK
(
ctx
->
HasInput
(
"Output"
),
"Input"
,
"Output"
,
"InverseGrad"
);
OP_INOUT_CHECK
(
ctx
->
HasInput
(
output_grad
),
"Input"
,
output_grad
,
"InverseGrad"
);
if
(
ctx
->
HasOutput
(
input_grad
))
{
ctx
->
SetOutputDim
(
input_grad
,
ctx
->
GetInputDim
(
output_grad
));
}
}
};
class
InverseOpMaker
:
public
framework
::
OpProtoAndCheckerMaker
{
...
...
@@ -128,18 +81,23 @@ class InverseGradOpMaker : public framework::SingleGradOpMaker<T> {
}
// namespace paddle
namespace
ops
=
paddle
::
operators
;
DECLARE_INFER_SHAPE_FUNCTOR
(
inverse
,
InverseInferShapeFunctor
,
PD_INFER_META
(
phi
::
InverseInferMeta
));
DECLARE_INFER_SHAPE_FUNCTOR
(
inverse_grad
,
InverseGradInferShapeFunctor
,
PD_INFER_META
(
phi
::
InverseGradInferMeta
));
REGISTER_OPERATOR
(
inverse
,
ops
::
InverseOp
,
ops
::
InverseOpMaker
,
ops
::
InverseOpInferVarType
,
ops
::
InverseGradOpMaker
<
paddle
::
framework
::
OpDesc
>
,
ops
::
InverseGradOpMaker
<
paddle
::
imperative
::
OpBase
>
);
REGISTER_OPERATOR
(
inverse_grad
,
ops
::
InverseGradOp
);
ops
::
InverseGradOpMaker
<
paddle
::
imperative
::
OpBase
>
,
InverseInferShapeFunctor
);
REGISTER_OP_CPU_KERNEL
(
inverse
,
ops
::
InverseKernel
<
phi
::
CPUContext
,
float
>
,
ops
::
InverseKernel
<
phi
::
CPUContext
,
double
>
);
REGISTER_OP_CPU_KERNEL
(
inverse_grad
,
ops
::
InverseGradKernel
<
phi
::
CPUContext
,
float
>
,
ops
::
InverseGradKernel
<
phi
::
CPUContext
,
double
>
);
REGISTER_OPERATOR
(
inverse_grad
,
ops
::
InverseGradOp
,
InverseGradInferShapeFunctor
);
paddle/fluid/operators/inverse_op.cu.cc
已删除
100644 → 0
浏览文件 @
8ccbb863
/* 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.
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/fluid/operators/inverse_op.h"
namespace
ops
=
paddle
::
operators
;
REGISTER_OP_CUDA_KERNEL
(
inverse
,
ops
::
InverseKernel
<
paddle
::
platform
::
CUDADeviceContext
,
float
>
,
ops
::
InverseKernel
<
paddle
::
platform
::
CUDADeviceContext
,
double
>
);
REGISTER_OP_CUDA_KERNEL
(
inverse_grad
,
ops
::
InverseGradKernel
<
paddle
::
platform
::
CUDADeviceContext
,
float
>
,
ops
::
InverseGradKernel
<
paddle
::
platform
::
CUDADeviceContext
,
double
>
);
paddle/fluid/operators/inverse_op.h
已删除
100644 → 0
浏览文件 @
8ccbb863
/* 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.
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/op_registry.h"
#include "paddle/phi/kernels/funcs/blas/blas.h"
#include "paddle/phi/kernels/funcs/matrix_inverse.h"
namespace
paddle
{
namespace
operators
{
template
<
typename
DeviceContext
,
typename
T
>
class
InverseKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
input
=
context
.
Input
<
framework
::
Tensor
>
(
"Input"
);
auto
*
output
=
context
.
Output
<
framework
::
Tensor
>
(
"Output"
);
output
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
&
dev_ctx
=
context
.
template
device_context
<
DeviceContext
>();
phi
::
funcs
::
MatrixInverseFunctor
<
DeviceContext
,
T
>
mat_inv
;
mat_inv
(
dev_ctx
,
*
input
,
output
);
}
};
template
<
typename
DeviceContext
,
typename
T
>
class
InverseGradKernel
:
public
framework
::
OpKernel
<
T
>
{
public:
void
Compute
(
const
framework
::
ExecutionContext
&
context
)
const
override
{
auto
*
a_inv
=
context
.
Input
<
framework
::
Tensor
>
(
"Output"
);
auto
*
a_inv_grad
=
context
.
Input
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"Output"
));
auto
*
a_grad
=
context
.
Output
<
framework
::
Tensor
>
(
framework
::
GradVarName
(
"Input"
));
if
(
a_grad
)
{
a_grad
->
mutable_data
<
T
>
(
context
.
GetPlace
());
auto
blas
=
phi
::
funcs
::
GetBlas
<
DeviceContext
,
T
>
(
context
);
auto
&
dev_ctx
=
context
.
template
device_context
<
DeviceContext
>();
framework
::
Tensor
tmp_out
=
context
.
AllocateTmpTensor
<
T
,
DeviceContext
>
(
a_inv
->
dims
(),
dev_ctx
);
auto
mat_dim_a0
=
phi
::
funcs
::
CreateMatrixDescriptor
(
a_inv_grad
->
dims
(),
0
,
false
);
auto
mat_dim_b0
=
phi
::
funcs
::
CreateMatrixDescriptor
(
a_inv
->
dims
(),
0
,
true
);
blas
.
MatMul
(
*
a_inv_grad
,
mat_dim_a0
,
*
a_inv
,
mat_dim_b0
,
T
(
1
),
&
tmp_out
,
T
(
0
));
auto
mat_dim_a1
=
phi
::
funcs
::
CreateMatrixDescriptor
(
a_inv
->
dims
(),
0
,
true
);
auto
mat_dim_b1
=
phi
::
funcs
::
CreateMatrixDescriptor
(
tmp_out
.
dims
(),
0
,
false
);
blas
.
MatMul
(
*
a_inv
,
mat_dim_a1
,
tmp_out
,
mat_dim_b1
,
T
(
-
1
),
a_grad
,
T
(
0
));
}
}
};
}
// namespace operators
}
// namespace paddle
paddle/phi/api/yaml/legacy_api.yaml
浏览文件 @
ea4b2c5e
...
...
@@ -1042,6 +1042,15 @@
intermediate
:
saved_mean, saved_variance
backward
:
instance_norm_grad
-
api
:
inverse
args
:
(Tensor x)
output
:
Tensor(out)
infer_meta
:
func
:
InverseInferMeta
kernel
:
func
:
inverse
backward
:
inverse_grad
# is_empty
-
api
:
is_empty
args
:
(Tensor x)
...
...
paddle/phi/api/yaml/legacy_backward.yaml
浏览文件 @
ea4b2c5e
...
...
@@ -967,6 +967,15 @@
optional
:
scale
backward
:
instance_norm_double_grad
-
backward_api
:
inverse_grad
forward
:
inverse(Tensor x) -> Tensor(out)
args
:
(Tensor out, Tensor out_grad)
output
:
Tensor(x_grad)
infer_meta
:
func
:
InverseGradInferMeta
kernel
:
func
:
inverse_grad
-
backward_api
:
kldiv_loss_grad
forward
:
kldiv_loss(Tensor x, Tensor label, str reduction) -> Tensor(out)
args
:
(Tensor x, Tensor label, Tensor out_grad, str reduction)
...
...
paddle/phi/infermeta/backward.cc
浏览文件 @
ea4b2c5e
...
...
@@ -403,6 +403,14 @@ void InstanceNormDoubleGradInferMeta(const MetaTensor& x,
}
}
void
InverseGradInferMeta
(
const
MetaTensor
&
out
,
const
MetaTensor
&
dout
,
MetaTensor
*
dx
)
{
if
(
dx
)
{
dx
->
set_dims
(
dout
.
dims
());
}
}
void
KernelWithXShapeInferMeta
(
const
MetaTensor
&
xshape
,
MetaTensor
*
dx
)
{
auto
xshape_dims
=
xshape
.
dims
();
auto
x_dims
=
phi
::
slice_ddim
(
xshape_dims
,
1
,
xshape_dims
.
size
());
...
...
paddle/phi/infermeta/backward.h
浏览文件 @
ea4b2c5e
...
...
@@ -183,6 +183,10 @@ void InstanceNormDoubleGradInferMeta(const MetaTensor& x,
MetaTensor
*
dscale
,
MetaTensor
*
ddy
);
void
InverseGradInferMeta
(
const
MetaTensor
&
out
,
const
MetaTensor
&
dout
,
MetaTensor
*
dx
);
void
KernelWithXShapeInferMeta
(
const
MetaTensor
&
xshape
,
MetaTensor
*
dx
);
void
MaxPoolWithIndexGradInferMeta
(
const
MetaTensor
&
x
,
...
...
paddle/phi/infermeta/unary.cc
浏览文件 @
ea4b2c5e
...
...
@@ -1025,6 +1025,43 @@ void InferMetaFromVecValue(const MetaTensor& x,
}
}
void
InverseInferMeta
(
const
MetaTensor
&
x
,
MetaTensor
*
out
)
{
auto
input_dims
=
x
.
dims
();
int64_t
input_rank
=
input_dims
.
size
();
PADDLE_ENFORCE_GE
(
input_rank
,
2
,
errors
::
InvalidArgument
(
"The dimension of Input(Input) is expected to be no less than 2. "
"But received: Input(Input)'s dimension = %d, shape = [%s]."
,
input_rank
,
input_dims
));
for
(
int64_t
i
=
0
;
i
<
input_rank
;
++
i
)
{
PADDLE_ENFORCE_EQ
(
(
input_dims
[
i
]
==
-
1
)
||
(
input_dims
[
i
]
>
0
),
true
,
errors
::
InvalidArgument
(
"Each dimension of input tensor is expected to be -1 or a "
"positive number, but received %d. Input's shape is [%s]."
,
input_dims
[
i
],
input_dims
));
}
if
(
input_dims
[
input_rank
-
2
]
>
0
&&
input_dims
[
input_rank
-
1
]
>
0
)
{
PADDLE_ENFORCE_EQ
(
input_dims
[
input_rank
-
2
],
input_dims
[
input_rank
-
1
],
errors
::
InvalidArgument
(
"The last two dimensions are expected to be equal. "
"But received: %d and %d; "
"Input(Input)'s shape = [%s]."
,
input_dims
[
input_rank
-
2
],
input_dims
[
input_rank
-
1
],
input_dims
));
}
out
->
set_dims
(
input_dims
);
out
->
share_lod
(
x
);
}
void
IsEmptyInferMeta
(
const
MetaTensor
&
x
,
MetaTensor
*
out
)
{
out
->
set_dims
(
phi
::
make_ddim
({
1
}));
out
->
set_dtype
(
DataType
::
BOOL
);
...
...
paddle/phi/infermeta/unary.h
浏览文件 @
ea4b2c5e
...
...
@@ -146,6 +146,8 @@ void InferMetaFromVecValue(const MetaTensor& x,
const
std
::
vector
<
int64_t
>&
shape
,
MetaTensor
*
out
);
void
InverseInferMeta
(
const
MetaTensor
&
x
,
MetaTensor
*
out
);
void
IsEmptyInferMeta
(
const
MetaTensor
&
x
,
MetaTensor
*
out
);
void
IsfiniteInferMeta
(
const
MetaTensor
&
input
,
MetaTensor
*
out
);
...
...
paddle/phi/kernels/cpu/inverse_grad_kernel.cc
0 → 100644
浏览文件 @
ea4b2c5e
// 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/impl/inverse_grad_kernel_impl.h"
#include "paddle/phi/core/kernel_registry.h"
PD_REGISTER_KERNEL
(
inverse_grad
,
CPU
,
ALL_LAYOUT
,
phi
::
InverseGradKernel
,
float
,
double
)
{}
paddle/phi/kernels/cpu/inverse_kernel.cc
0 → 100644
浏览文件 @
ea4b2c5e
// 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/impl/inverse_kernel_impl.h"
#include "paddle/phi/core/kernel_registry.h"
PD_REGISTER_KERNEL
(
inverse
,
CPU
,
ALL_LAYOUT
,
phi
::
InverseKernel
,
float
,
double
)
{}
paddle/phi/kernels/gpu/inverse_grad_kernel.cu
0 → 100644
浏览文件 @
ea4b2c5e
// 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/inverse_grad_kernel.h"
#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/inverse_grad_kernel_impl.h"
PD_REGISTER_KERNEL
(
inverse_grad
,
GPU
,
ALL_LAYOUT
,
phi
::
InverseGradKernel
,
float
,
double
)
{}
paddle/phi/kernels/gpu/inverse_kernel.cu
0 → 100644
浏览文件 @
ea4b2c5e
// 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/inverse_kernel.h"
#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/inverse_kernel_impl.h"
PD_REGISTER_KERNEL
(
inverse
,
GPU
,
ALL_LAYOUT
,
phi
::
InverseKernel
,
float
,
double
)
{}
paddle/phi/kernels/impl/inverse_grad_kernel_impl.h
0 → 100644
浏览文件 @
ea4b2c5e
// 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/inverse_grad_kernel.h"
#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/kernels/funcs/blas/blas.h"
#include "paddle/phi/kernels/funcs/matrix_inverse.h"
namespace
phi
{
template
<
typename
T
,
typename
Context
>
void
InverseGradKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
out
,
const
DenseTensor
&
out_grad
,
DenseTensor
*
in_grad
)
{
if
(
in_grad
)
{
dev_ctx
.
template
Alloc
<
T
>(
in_grad
);
auto
blas
=
phi
::
funcs
::
GetBlas
<
Context
,
T
>
(
dev_ctx
);
DenseTensor
tmp_out
;
tmp_out
.
Resize
(
out
.
dims
());
dev_ctx
.
template
Alloc
<
T
>(
&
tmp_out
);
auto
mat_dim_a0
=
phi
::
funcs
::
CreateMatrixDescriptor
(
out_grad
.
dims
(),
0
,
false
);
auto
mat_dim_b0
=
phi
::
funcs
::
CreateMatrixDescriptor
(
out
.
dims
(),
0
,
true
);
blas
.
MatMul
(
out_grad
,
mat_dim_a0
,
out
,
mat_dim_b0
,
T
(
1
),
&
tmp_out
,
T
(
0
));
auto
mat_dim_a1
=
phi
::
funcs
::
CreateMatrixDescriptor
(
out
.
dims
(),
0
,
true
);
auto
mat_dim_b1
=
phi
::
funcs
::
CreateMatrixDescriptor
(
tmp_out
.
dims
(),
0
,
false
);
blas
.
MatMul
(
out
,
mat_dim_a1
,
tmp_out
,
mat_dim_b1
,
T
(
-
1
),
in_grad
,
T
(
0
));
}
}
}
// namespace phi
paddle/phi/kernels/impl/inverse_kernel_impl.h
0 → 100644
浏览文件 @
ea4b2c5e
// 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/inverse_kernel.h"
#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/kernels/funcs/matrix_inverse.h"
namespace
phi
{
template
<
typename
T
,
typename
Context
>
void
InverseKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
x
,
DenseTensor
*
out
)
{
dev_ctx
.
template
Alloc
<
T
>(
out
);
phi
::
funcs
::
MatrixInverseFunctor
<
Context
,
T
>
mat_inv
;
mat_inv
(
dev_ctx
,
x
,
out
);
}
}
// namespace phi
paddle/phi/kernels/inverse_grad_kernel.h
0 → 100644
浏览文件 @
ea4b2c5e
// 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/core/dense_tensor.h"
namespace
phi
{
template
<
typename
T
,
typename
Context
>
void
InverseGradKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
out
,
const
DenseTensor
&
out_grad
,
DenseTensor
*
in_grad
);
}
// namespace phi
paddle/phi/kernels/inverse_kernel.h
0 → 100644
浏览文件 @
ea4b2c5e
// 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/backends/cpu/cpu_context.h"
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/kernels/funcs/matrix_inverse.h"
namespace
phi
{
template
<
typename
T
,
typename
Context
>
void
InverseKernel
(
const
Context
&
dev_ctx
,
const
DenseTensor
&
x
,
DenseTensor
*
out
);
}
// namespace phi
paddle/phi/ops/compat/inverse_sig.cc
0 → 100644
浏览文件 @
ea4b2c5e
// 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
InverseGradOpArgumentMapping
(
const
ArgumentMappingContext
&
ctx
)
{
return
KernelSignature
(
"inverse_grad"
,
{
"Output"
,
"Output@GRAD"
},
{},
{
"Input@GRAD"
});
}
}
// namespace phi
PD_REGISTER_ARG_MAPPING_FN
(
inverse_grad
,
phi
::
InverseGradOpArgumentMapping
);
python/paddle/fluid/tests/unittests/test_inverse_op.py
浏览文件 @
ea4b2c5e
...
...
@@ -25,6 +25,7 @@ class TestInverseOp(OpTest):
def
config
(
self
):
self
.
matrix_shape
=
[
10
,
10
]
self
.
dtype
=
"float64"
self
.
python_api
=
paddle
.
tensor
.
math
.
inverse
def
setUp
(
self
):
self
.
op_type
=
"inverse"
...
...
@@ -38,10 +39,10 @@ class TestInverseOp(OpTest):
self
.
outputs
=
{
'Output'
:
inverse
}
def
test_check_output
(
self
):
self
.
check_output
()
self
.
check_output
(
check_eager
=
True
)
def
test_grad
(
self
):
self
.
check_grad
([
'Input'
],
'Output'
)
self
.
check_grad
([
'Input'
],
'Output'
,
check_eager
=
True
)
class
TestInverseOpBatched
(
TestInverseOp
):
...
...
@@ -49,6 +50,7 @@ class TestInverseOpBatched(TestInverseOp):
def
config
(
self
):
self
.
matrix_shape
=
[
8
,
4
,
4
]
self
.
dtype
=
"float64"
self
.
python_api
=
paddle
.
tensor
.
math
.
inverse
class
TestInverseOpLarge
(
TestInverseOp
):
...
...
@@ -56,9 +58,13 @@ class TestInverseOpLarge(TestInverseOp):
def
config
(
self
):
self
.
matrix_shape
=
[
32
,
32
]
self
.
dtype
=
"float64"
self
.
python_api
=
paddle
.
tensor
.
math
.
inverse
def
test_grad
(
self
):
self
.
check_grad
([
'Input'
],
'Output'
,
max_relative_error
=
1e-6
)
self
.
check_grad
([
'Input'
],
'Output'
,
max_relative_error
=
1e-6
,
check_eager
=
True
)
class
TestInverseOpFP32
(
TestInverseOp
):
...
...
@@ -66,9 +72,13 @@ class TestInverseOpFP32(TestInverseOp):
def
config
(
self
):
self
.
matrix_shape
=
[
10
,
10
]
self
.
dtype
=
"float32"
self
.
python_api
=
paddle
.
tensor
.
math
.
inverse
def
test_grad
(
self
):
self
.
check_grad
([
'Input'
],
'Output'
,
max_relative_error
=
1e-2
)
self
.
check_grad
([
'Input'
],
'Output'
,
max_relative_error
=
1e-2
,
check_eager
=
True
)
class
TestInverseOpBatchedFP32
(
TestInverseOpFP32
):
...
...
@@ -76,6 +86,7 @@ class TestInverseOpBatchedFP32(TestInverseOpFP32):
def
config
(
self
):
self
.
matrix_shape
=
[
8
,
4
,
4
]
self
.
dtype
=
"float32"
self
.
python_api
=
paddle
.
tensor
.
math
.
inverse
class
TestInverseOpLargeFP32
(
TestInverseOpFP32
):
...
...
@@ -83,6 +94,7 @@ class TestInverseOpLargeFP32(TestInverseOpFP32):
def
config
(
self
):
self
.
matrix_shape
=
[
32
,
32
]
self
.
dtype
=
"float32"
self
.
python_api
=
paddle
.
tensor
.
math
.
inverse
class
TestInverseAPI
(
unittest
.
TestCase
):
...
...
python/paddle/tensor/math.py
浏览文件 @
ea4b2c5e
...
...
@@ -1932,7 +1932,9 @@ def inverse(x, name=None):
print(inv) # [[0.5, 0], [0, 0.5]]
"""
if
paddle
.
in_dynamic_mode
():
if
in_dygraph_mode
():
return
_C_ops
.
final_state_inverse
(
x
)
elif
paddle
.
in_dynamic_mode
():
return
_C_ops
.
inverse
(
x
)
def
_check_input
(
x
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录