Home | History | Annotate | Download | only in kernels
      1 /* Copyright 2017 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 SelfAdjointEigV2Op : public LinearAlgebraOp<Scalar> {
     32  public:
     33   typedef LinearAlgebraOp<Scalar> Base;
     34 
     35   explicit SelfAdjointEigV2Op(OpKernelConstruction* context) : Base(context) {
     36     OP_REQUIRES_OK(context, context->GetAttr("compute_v", &compute_v_));
     37   }
     38 
     39   using TensorShapes = typename Base::TensorShapes;
     40   using Matrix = typename Base::Matrix;
     41   using MatrixMaps = typename Base::MatrixMaps;
     42   using ConstMatrixMap = typename Base::ConstMatrixMap;
     43   using ConstMatrixMaps = typename Base::ConstMatrixMaps;
     44 
     45   TensorShapes GetOutputMatrixShapes(
     46       const TensorShapes& input_matrix_shapes) const final {
     47     int64 n = input_matrix_shapes[0].dim_size(0);
     48     if (compute_v_) {
     49       return TensorShapes({TensorShape({n}), TensorShape({n, n})});
     50     } else {
     51       return TensorShapes({TensorShape({n})});
     52     }
     53   }
     54 
     55   void ComputeMatrix(OpKernelContext* context, const ConstMatrixMaps& inputs,
     56                      MatrixMaps* outputs) final {
     57     const int64 rows = inputs[0].rows();
     58     if (rows == 0) {
     59       // If X is an empty matrix (0 rows, 0 col), X * X' == X.
     60       // Therefore, we return X.
     61       return;
     62     }
     63 
     64     Eigen::SelfAdjointEigenSolver<Matrix> eig(
     65         inputs[0],
     66         compute_v_ ? Eigen::ComputeEigenvectors : Eigen::EigenvaluesOnly);
     67     // TODO(rmlarsen): Output more detailed error info on failure.
     68     OP_REQUIRES(
     69         context, eig.info() == Eigen::Success,
     70         errors::InvalidArgument("Self-adjoint eigen decomposition was not "
     71                                 "successful. The input might not be valid."));
     72 
     73     outputs->at(0) = eig.eigenvalues().template cast<Scalar>();
     74     if (compute_v_) {
     75       outputs->at(1) = eig.eigenvectors();
     76     }
     77   }
     78 
     79  private:
     80   bool compute_v_;
     81 };
     82 
     83 }  // namespace tensorflow
     84