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_SPARSE_TENSOR_DENSE_MATMUL_OP_H_
     17 #define TENSORFLOW_KERNELS_SPARSE_TENSOR_DENSE_MATMUL_OP_H_
     18 
     19 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
     20 #include "tensorflow/core/framework/tensor_types.h"
     21 #include "tensorflow/core/framework/types.h"
     22 #include "tensorflow/core/lib/core/errors.h"
     23 
     24 namespace tensorflow {
     25 
     26 namespace functor {
     27 
     28 template <typename Device, typename T, typename Tindices, bool ADJ_A,
     29           bool ADJ_B>
     30 struct SparseTensorDenseMatMulFunctor {
     31   static EIGEN_ALWAYS_INLINE Status Compute(
     32       const Device& d, typename TTypes<T>::Matrix out,
     33       typename TTypes<Tindices>::ConstMatrix a_indices,
     34       typename TTypes<T>::ConstVec a_values, typename TTypes<T>::ConstMatrix b);
     35 };
     36 
     37 template <typename MATRIX, bool ADJ>
     38 class MaybeAdjoint;
     39 
     40 template <typename MATRIX>
     41 class MaybeAdjoint<MATRIX, false> {
     42  public:
     43   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE MaybeAdjoint(MATRIX m) : m_(m) {}
     44   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename MATRIX::Scalar operator()(
     45       const typename MATRIX::Index i, const typename MATRIX::Index j) const {
     46     return m_(i, j);
     47   }
     48 
     49  private:
     50   const MATRIX m_;
     51 };
     52 
     53 template <typename T>
     54 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE T MaybeConj(T v) {
     55   return v;
     56 }
     57 
     58 template <typename MATRIX>
     59 class MaybeAdjoint<MATRIX, true> {
     60  public:
     61   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE MaybeAdjoint(MATRIX m) : m_(m) {}
     62   EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE typename MATRIX::Scalar operator()(
     63       const typename MATRIX::Index i, const typename MATRIX::Index j) const {
     64     return Eigen::numext::conj(m_(j, i));
     65   }
     66 
     67  private:
     68   const MATRIX m_;
     69 };
     70 
     71 }  // end namespace functor
     72 }  // end namespace tensorflow
     73 
     74 #endif  // TENSORFLOW_KERNELS_SPARSE_TENSOR_DENSE_MATMUL_OP_H_
     75