diff --git a/paddle/fluid/operators/eigvalsh_op.cc b/paddle/fluid/operators/eigvalsh_op.cc deleted file mode 100644 index 27c70f1e9b9a9..0000000000000 --- a/paddle/fluid/operators/eigvalsh_op.cc +++ /dev/null @@ -1,113 +0,0 @@ -/* Copyright (c) 2021 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/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" - -namespace paddle { -namespace operators { - -class EigvalshOp : public framework::OperatorWithKernel { - public: - using framework::OperatorWithKernel::OperatorWithKernel; -}; - -class EigvalshOpMaker : public framework::OpProtoAndCheckerMaker { - public: - void Make() override { - AddInput("X", - "(Tensor), Hermitian or real symmetric matrices." - "Its shape should be [*, N, N] where * is zero or" - "more batch dimensions. The data type is float32 ," - "float64, complex64, complex128."); - AddOutput("Eigenvalues", - "(Tensor), The eigenvalues in ascending order." - "The data type is float32 or float64."); - AddOutput( - "Eigenvectors", - "(Tensor), The column is the normalized eigenvector " - "corresponding to the eigenvalue. The data type is the same as ``X``." - "Eigenvectors are required to calculate gradient when backward."); - AddAttr( - "UPLO", - "(string, default 'L'), 'L' represents the lower triangular matrix," - "'U' represents the upper triangular matrix.") - .SetDefault("L"); - AddAttr("is_test", - "(bool, default false) Set to true for inference only, false " - "for training.") - .SetDefault(false); - AddComment(R"DOC( -Eigvalsh Operator. - -Computes the eigenvalues of a complex Hermitian - (conjugate symmetric) or a real symmetric matrix. - -)DOC"); - } -}; - -class EigvalshGradOp : public framework::OperatorWithKernel { - public: - using framework::OperatorWithKernel::OperatorWithKernel; - - protected: - phi::KernelKey GetExpectedKernelType( - const framework::ExecutionContext& ctx) const override { - return phi::KernelKey( - OperatorWithKernel::IndicateVarDataType(ctx, "Eigenvectors"), - ctx.device_context().GetPlace()); - } -}; - -template -class EigvalshGradOpMaker : public framework::SingleGradOpMaker { - public: - using framework::SingleGradOpMaker::SingleGradOpMaker; - - protected: - void Apply(GradOpPtr op) const override { - op->SetType(this->ForwardOpType() + "_grad"); - op->SetInput("Eigenvectors", this->Output("Eigenvectors")); - op->SetInput(framework::GradVarName("Eigenvalues"), - this->OutputGrad("Eigenvalues")); - op->SetAttrMap(this->Attrs()); - op->SetOutput(framework::GradVarName("X"), this->InputGrad("X")); - } -}; - -} // namespace operators -} // namespace paddle - -namespace ops = paddle::operators; - -DECLARE_INFER_SHAPE_FUNCTOR(eigvalsh, - EigvalshInferShapeFunctor, - PD_INFER_META(phi::EigvalshInferMeta)); -DECLARE_INFER_SHAPE_FUNCTOR(eigvalsh_grad, - EigvalshGradInferShapeFunctor, - PD_INFER_META(phi::EigvalshGradInferMeta)); - -REGISTER_OPERATOR(eigvalsh, - ops::EigvalshOp, - ops::EigvalshOpMaker, - ops::EigvalshGradOpMaker, - ops::EigvalshGradOpMaker, - EigvalshInferShapeFunctor); -REGISTER_OPERATOR(eigvalsh_grad, - ops::EigvalshGradOp, - EigvalshGradInferShapeFunctor); diff --git a/paddle/phi/api/yaml/backward.yaml b/paddle/phi/api/yaml/backward.yaml index b824491536955..b5a88ec3e0ae8 100644 --- a/paddle/phi/api/yaml/backward.yaml +++ b/paddle/phi/api/yaml/backward.yaml @@ -445,6 +445,16 @@ func : eigh_grad data_type : out_v +- backward_op : eigvalsh_grad + forward : eigvalsh (Tensor x, str uplo = "L", bool is_test = false) -> Tensor(eigenvalues), Tensor(eigenvectors) + args : (Tensor eigenvectors, Tensor eigenvalues_grad, str uplo, bool is_test) + output : Tensor(x_grad) + infer_meta : + func : EigvalshGradInferMeta + kernel : + func : eigvalsh_grad + data_type : eigenvectors + - backward_op : elu_double_grad forward : elu_grad (Tensor x, Tensor out, Tensor grad_out, float alpha)-> Tensor(grad_x) args : (Tensor x, Tensor grad_out, Tensor grad_x_grad, float alpha) diff --git a/paddle/phi/api/yaml/legacy_backward.yaml b/paddle/phi/api/yaml/legacy_backward.yaml index d60f2b70597de..4990ffdebefd6 100755 --- a/paddle/phi/api/yaml/legacy_backward.yaml +++ b/paddle/phi/api/yaml/legacy_backward.yaml @@ -352,18 +352,6 @@ kernel : func : dropout_grad -- backward_op : eigvalsh_grad - forward : eigvalsh (Tensor x, str uplo, bool is_test) -> Tensor(eigenvalues), Tensor(eigenvectors) - args : (Tensor eigenvectors, Tensor eigenvalues_grad, str uplo, bool is_test) - output : Tensor(x_grad) - infer_meta : - func : EigvalshGradInferMeta - kernel : - func : eigvalsh_grad - data_type : eigenvectors - data_transform : - skip_transform : eigenvalues_grad - - backward_op : einsum_grad forward : einsum (Tensor[] x, str equation) -> Tensor(out), Tensor[](inner_cache), Tensor[](x_shape) args : (Tensor[] x_shape, Tensor[] inner_cache, Tensor out_grad, str equation) diff --git a/paddle/phi/api/yaml/legacy_ops.yaml b/paddle/phi/api/yaml/legacy_ops.yaml index 770062f2817f8..bb8a6b2004372 100755 --- a/paddle/phi/api/yaml/legacy_ops.yaml +++ b/paddle/phi/api/yaml/legacy_ops.yaml @@ -436,15 +436,6 @@ data_type: DataType::FLOAT32 optional : hypslength, refslength -- op : eigvalsh - args : (Tensor x, str uplo, bool is_test) - output : Tensor(eigenvalues), Tensor(eigenvectors) - infer_meta : - func : EigvalshInferMeta - kernel : - func : eigvalsh - backward : eigvalsh_grad - - op : einsum args : (Tensor[] x, str equation) output : Tensor, Tensor[]{x.size()}, Tensor[]{x.size()} diff --git a/paddle/phi/api/yaml/op_compat.yaml b/paddle/phi/api/yaml/op_compat.yaml index 6dab0c0f4c361..43a30fea48570 100644 --- a/paddle/phi/api/yaml/op_compat.yaml +++ b/paddle/phi/api/yaml/op_compat.yaml @@ -606,6 +606,15 @@ outputs : out : Out +- op : eigvalsh + backward : eigvalsh_grad + inputs : + {x : X} + outputs : + {eigenvalues : Eigenvalues, eigenvectors : Eigenvectors} + attrs : + uplo : UPLO + - op : elementwise_pow backward : elementwise_pow_grad extra : diff --git a/paddle/phi/api/yaml/ops.yaml b/paddle/phi/api/yaml/ops.yaml index 604089585210f..bf5414f56de59 100644 --- a/paddle/phi/api/yaml/ops.yaml +++ b/paddle/phi/api/yaml/ops.yaml @@ -474,6 +474,16 @@ kernel : func : eigvals +- op : eigvalsh + args : (Tensor x, str uplo = "L", bool is_test = false) + output : Tensor(eigenvalues), Tensor(eigenvectors) + infer_meta : + func : EigvalshInferMeta + kernel : + func : eigvalsh + data_type : x + backward : eigvalsh_grad + - op : elu args : (Tensor x, float alpha = 1.0f) output : Tensor(out) diff --git a/paddle/phi/kernels/cpu/eigvalsh_grad_kernel.cc b/paddle/phi/kernels/cpu/eigvalsh_grad_kernel.cc index b7b5927740e09..2489cbc825b22 100644 --- a/paddle/phi/kernels/cpu/eigvalsh_grad_kernel.cc +++ b/paddle/phi/kernels/cpu/eigvalsh_grad_kernel.cc @@ -26,4 +26,6 @@ PD_REGISTER_KERNEL(eigvalsh_grad, float, double, phi::dtype::complex, - phi::dtype::complex) {} + phi::dtype::complex) { + kernel->InputAt(1).SetDataType(phi::dtype::ToReal(kernel_key.dtype())); +} diff --git a/paddle/phi/kernels/gpu/eigvalsh_grad_kernel.cu b/paddle/phi/kernels/gpu/eigvalsh_grad_kernel.cu index de26617d80f1b..bf62c2736e87c 100644 --- a/paddle/phi/kernels/gpu/eigvalsh_grad_kernel.cu +++ b/paddle/phi/kernels/gpu/eigvalsh_grad_kernel.cu @@ -26,4 +26,6 @@ PD_REGISTER_KERNEL(eigvalsh_grad, float, double, phi::dtype::complex, - phi::dtype::complex) {} + phi::dtype::complex) { + kernel->InputAt(1).SetDataType(phi::dtype::ToReal(kernel_key.dtype())); +} diff --git a/paddle/phi/kernels/gpu/eigvalsh_kernel.cu b/paddle/phi/kernels/gpu/eigvalsh_kernel.cu index 383f036c98cf9..a075dad6cddb3 100644 --- a/paddle/phi/kernels/gpu/eigvalsh_kernel.cu +++ b/paddle/phi/kernels/gpu/eigvalsh_kernel.cu @@ -26,4 +26,6 @@ PD_REGISTER_KERNEL(eigvalsh, // cuda_only float, double, phi::dtype::complex, - phi::dtype::complex) {} + phi::dtype::complex) { + kernel->InputAt(1).SetDataType(phi::dtype::ToReal(kernel_key.dtype())); +} diff --git a/paddle/phi/ops/compat/eigvalsh_sig.cc b/paddle/phi/ops/compat/eigvalsh_sig.cc deleted file mode 100644 index b0635403355f7..0000000000000 --- a/paddle/phi/ops/compat/eigvalsh_sig.cc +++ /dev/null @@ -1,34 +0,0 @@ -/* 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 EigvalshOpArgumentMapping(const ArgumentMappingContext& ctx) { - return KernelSignature( - "eigvalsh", {"X"}, {"UPLO", "is_test"}, {"Eigenvalues", "Eigenvectors"}); -} - -KernelSignature EigvalshGradOpArgumentMapping( - const ArgumentMappingContext& ctx) { - return KernelSignature("eigvalsh_grad", - {"Eigenvectors", "Eigenvalues@GRAD"}, - {"UPLO", "is_test"}, - {"X@GRAD"}); -} -} // namespace phi - -PD_REGISTER_ARG_MAPPING_FN(eigvalsh, phi::EigvalshOpArgumentMapping); -PD_REGISTER_ARG_MAPPING_FN(eigvalsh_grad, phi::EigvalshGradOpArgumentMapping);