Home | History | Annotate | Download | only in kernels
      1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
      2 
      3 Licensed under the Apache License, Version 2.0 (the "License");
      4 you may not use this file except in compliance with the License.
      5 You may obtain a copy of the License at
      6 
      7     http://www.apache.org/licenses/LICENSE-2.0
      8 
      9 Unless required by applicable law or agreed to in writing, software
     10 distributed under the License is distributed on an "AS IS" BASIS,
     11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 See the License for the specific language governing permissions and
     13 limitations under the License.
     14 ==============================================================================*/
     15 
     16 // See docs in ../ops/linalg_ops.cc.
     17 
     18 #include "third_party/eigen3/Eigen/Core"
     19 #include "third_party/eigen3/Eigen/Eigenvalues"
     20 #include "tensorflow/core/framework/kernel_def_builder.h"
     21 #include "tensorflow/core/framework/op_kernel.h"
     22 #include "tensorflow/core/framework/tensor_shape.h"
     23 #include "tensorflow/core/kernels/linalg_ops_common.h"
     24 #include "tensorflow/core/lib/core/errors.h"
     25 #include "tensorflow/core/platform/logging.h"
     26 #include "tensorflow/core/platform/types.h"
     27 
     28 namespace tensorflow {
     29 
     30 template <class Scalar>
     31 class SelfAdjointEigOp : public LinearAlgebraOp<Scalar> {
     32  public:
     33   typedef LinearAlgebraOp<Scalar> Base;
     34 
     35   explicit SelfAdjointEigOp(OpKernelConstruction* context) : Base(context) {}
     36 
     37   using TensorShapes = typename Base::TensorShapes;
     38   using Matrix = typename Base::Matrix;
     39   using MatrixMaps = typename Base::MatrixMaps;
     40   using ConstMatrixMap = typename Base::ConstMatrixMap;
     41   using ConstMatrixMaps = typename Base::ConstMatrixMaps;
     42 
     43   TensorShapes GetOutputMatrixShapes(
     44       const TensorShapes& input_matrix_shapes) const final {
     45     int64 d = input_matrix_shapes[0].dim_size(0);
     46     return TensorShapes({TensorShape({d + 1, d})});
     47   }
     48 
     49   void ComputeMatrix(OpKernelContext* context, const ConstMatrixMaps& inputs,
     50                      MatrixMaps* outputs) final {
     51     const int64 rows = inputs[0].rows();
     52     if (rows == 0) {
     53       // If X is an empty matrix (0 rows, 0 col), X * X' == X.
     54       // Therefore, we return X.
     55       return;
     56     }
     57 
     58     Eigen::SelfAdjointEigenSolver<
     59         Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>>
     60         es(inputs[0]);
     61     OP_REQUIRES(context, es.info() == Eigen::Success,
     62                 errors::InvalidArgument("Self Adjoint Eigen decomposition was"
     63                                         "not successful. "
     64                                         "The input might not be valid."));
     65     outputs->at(0).row(0) = es.eigenvalues().transpose();
     66     outputs->at(0).bottomRows(rows) = es.eigenvectors();
     67   }
     68 };
     69 
     70 REGISTER_LINALG_OP("SelfAdjointEig", (SelfAdjointEigOp<float>), float);
     71 REGISTER_LINALG_OP("SelfAdjointEig", (SelfAdjointEigOp<double>), double);
     72 REGISTER_LINALG_OP("BatchSelfAdjointEig", (SelfAdjointEigOp<float>), float);
     73 REGISTER_LINALG_OP("BatchSelfAdjointEig", (SelfAdjointEigOp<double>), double);
     74 }  // namespace tensorflow
     75