Home | History | Annotate | Download | only in kernels
      1 /* Copyright 2016 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 // This header file is used by the individual svd_*op*.cc files for registering
     19 // individual kernels. A separate file is used for each instantiated kernel to
     20 // improve compilation times.
     21 #include <algorithm>
     22 
     23 #include "third_party/eigen3/Eigen/SVD"
     24 #include "tensorflow/core/framework/kernel_def_builder.h"
     25 #include "tensorflow/core/framework/op_kernel.h"
     26 #include "tensorflow/core/framework/tensor_shape.h"
     27 #include "tensorflow/core/kernels/linalg_ops_common.h"
     28 #include "tensorflow/core/lib/core/errors.h"
     29 #include "tensorflow/core/platform/logging.h"
     30 #include "tensorflow/core/platform/macros.h"
     31 #include "tensorflow/core/platform/types.h"
     32 
     33 namespace tensorflow {
     34 
     35 template <class Scalar>
     36 class SvdOp : public LinearAlgebraOp<Scalar> {
     37  public:
     38   typedef LinearAlgebraOp<Scalar> Base;
     39 
     40   explicit SvdOp(OpKernelConstruction* context) : Base(context) {
     41     OP_REQUIRES_OK(context, context->GetAttr("compute_uv", &compute_uv_));
     42     OP_REQUIRES_OK(context, context->GetAttr("full_matrices", &full_matrices_));
     43   }
     44 
     45   using TensorShapes = typename Base::TensorShapes;
     46 
     47   void ValidateInputMatrixShapes(
     48       OpKernelContext* context,
     49       const TensorShapes& input_matrix_shapes) const final {
     50     Base::ValidateSingleMatrix(context, input_matrix_shapes);
     51   }
     52 
     53   TensorShapes GetOutputMatrixShapes(
     54       const TensorShapes& input_matrix_shapes) const final {
     55     int64 m = input_matrix_shapes[0].dim_size(0);
     56     int64 n = input_matrix_shapes[0].dim_size(1);
     57     int64 min_size = std::min(m, n);
     58     if (compute_uv_) {
     59       return TensorShapes({TensorShape({min_size}),
     60                            TensorShape({m, full_matrices_ ? m : min_size}),
     61                            TensorShape({n, full_matrices_ ? n : min_size})});
     62     } else {
     63       return TensorShapes({TensorShape({min_size})});
     64     }
     65   }
     66 
     67   // TODO(rmlarsen): This should depend on compute_uv. See b/30409375.
     68   int64 GetCostPerUnit(const TensorShapes& input_matrix_shapes) const final {
     69     double m = static_cast<double>(input_matrix_shapes[0].dim_size(0));
     70     double n = static_cast<double>(input_matrix_shapes[0].dim_size(1));
     71     double cost = 12 * std::max(m, n) * std::min(m, n) * std::min(m, n);
     72     return cost >= static_cast<double>(kint64max) ? kint64max
     73                                                   : static_cast<int64>(cost);
     74   }
     75 
     76   using Matrix = typename Base::Matrix;
     77   using MatrixMaps = typename Base::MatrixMaps;
     78   using ConstMatrixMap = typename Base::ConstMatrixMap;
     79   using ConstMatrixMaps = typename Base::ConstMatrixMaps;
     80 
     81   void ComputeMatrix(OpKernelContext* context, const ConstMatrixMaps& inputs,
     82                      MatrixMaps* outputs) final {
     83     int options = 0;  // Don't compute singular vectors;
     84     if (compute_uv_) {
     85       options = full_matrices_ ? Eigen::ComputeFullU | Eigen::ComputeFullV
     86                                : Eigen::ComputeThinU | Eigen::ComputeThinV;
     87     }
     88     Eigen::BDCSVD<Matrix> svd(inputs[0], options);
     89     outputs->at(0) = svd.singularValues().template cast<Scalar>();
     90     if (compute_uv_) {
     91       outputs->at(1) = svd.matrixU();
     92       outputs->at(2) = svd.matrixV();
     93     }
     94   }
     95 
     96  private:
     97   bool compute_uv_;
     98   bool full_matrices_;
     99 
    100   TF_DISALLOW_COPY_AND_ASSIGN(SvdOp);
    101 };
    102 
    103 }  // namespace tensorflow
    104