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 #ifndef TENSORFLOW_KERNELS_MATRIX_DIAG_OP_H_
     17 #define TENSORFLOW_KERNELS_MATRIX_DIAG_OP_H_
     18 
     19 // Generator definition for MatrixDiagOp, must be compilable by nvcc.
     20 
     21 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
     22 #include "tensorflow/core/framework/tensor_types.h"
     23 #include "tensorflow/core/platform/types.h"
     24 
     25 namespace tensorflow {
     26 
     27 namespace generator {
     28 
     29 template <typename T>
     30 class MatrixDiagPartGenerator {
     31  public:
     32   EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
     33   MatrixDiagPartGenerator(typename TTypes<T, 3>::ConstTensor input)
     34       : input_(input) {}
     35 
     36   EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T
     37   operator()(const Eigen::array<Eigen::DenseIndex, 2>& coords) const {
     38     Eigen::array<Eigen::DenseIndex, 3> diag_from_coords(
     39         {coords[0], coords[1], coords[1]});
     40     return input_(diag_from_coords);
     41   }
     42 
     43  private:
     44   typename TTypes<T, 3>::ConstTensor input_;
     45 };
     46 
     47 template <typename T>
     48 class MatrixDiagGenerator {
     49  public:
     50   EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE
     51   MatrixDiagGenerator(typename TTypes<T, 2>::ConstTensor input)
     52       : input_(input) {}
     53 
     54   EIGEN_DEVICE_FUNC EIGEN_ALWAYS_INLINE T
     55   operator()(const Eigen::array<Eigen::DenseIndex, 3>& coords) const {
     56     if (coords[2] != coords[1]) return T();
     57 
     58     Eigen::array<Eigen::DenseIndex, 2> diag_coords({coords[0], coords[1]});
     59     return input_(diag_coords);
     60   }
     61 
     62  private:
     63   typename TTypes<T, 2>::ConstTensor input_;
     64 };
     65 
     66 }  // namespace generator
     67 
     68 namespace functor {
     69 
     70 template <typename Device, typename T>
     71 struct MatrixDiagPart {
     72   EIGEN_ALWAYS_INLINE static void Compute(
     73       const Device& d, typename TTypes<T, 3>::ConstTensor input,
     74       typename TTypes<T, 2>::Tensor output) {
     75     generator::MatrixDiagPartGenerator<T> generator(input);
     76     output.device(d) = output.generate(generator);
     77   }
     78 };
     79 
     80 template <typename Device, typename T>
     81 struct MatrixDiag {
     82   EIGEN_ALWAYS_INLINE static void Compute(
     83       const Device& d, typename TTypes<T, 2>::ConstTensor input,
     84       typename TTypes<T, 3>::Tensor output) {
     85     generator::MatrixDiagGenerator<T> generator(input);
     86     output.device(d) = output.generate(generator);
     87   }
     88 };
     89 
     90 }  // namespace functor
     91 
     92 }  // namespace tensorflow
     93 
     94 #endif  // TENSORFLOW_KERNELS_MATRIX_DIAG_OP_H_
     95