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_MATMUL_OP_H_
     17 #define TENSORFLOW_KERNELS_MATMUL_OP_H_
     18 
     19 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
     20 #include "tensorflow/core/framework/tensor.h"
     21 #include "tensorflow/core/framework/tensor_types.h"
     22 #include "tensorflow/core/lib/hash/hash.h"
     23 
     24 namespace tensorflow {
     25 namespace functor {
     26 
     27 // Helpers to define tensor<T> needed by MatMul op.
     28 template <typename T>
     29 struct MatMulTypes {
     30   typedef Eigen::TensorMap<Eigen::Tensor<T, 2, Eigen::RowMajor>, Eigen::Aligned>
     31       out_type;
     32   typedef Eigen::TensorMap<Eigen::Tensor<const T, 2, Eigen::RowMajor>,
     33                            Eigen::Aligned>
     34       in_type;
     35 };
     36 
     37 template <typename Device, typename In0, typename In1, typename Out,
     38           typename DimPair>
     39 void MatMul(const Device& d, Out out, In0 in0, In1 in1,
     40             const DimPair& dim_pair) {
     41   out.device(d) = in0.contract(in1, dim_pair);
     42 }
     43 
     44 template <typename Device, typename T>
     45 struct MatMulFunctor {
     46   // Computes on device "d": out = in0 * in1, where * is matrix
     47   // multiplication.
     48   void operator()(
     49       const Device& d, typename MatMulTypes<T>::out_type out,
     50       typename MatMulTypes<T>::in_type in0,
     51       typename MatMulTypes<T>::in_type in1,
     52       const Eigen::array<Eigen::IndexPair<Eigen::DenseIndex>, 1>& dim_pair);
     53 };
     54 
     55 }  // end namespace functor
     56 
     57 #if GOOGLE_CUDA
     58 // Encapsulate all the shape information that is used in matmul operations.
     59 class MatmulParameters {
     60  public:
     61   MatmulParameters(bool transa, bool transb, uint64 m, uint64 n, uint64 k,
     62                    DataType dtype, int device_id)
     63       : transa_(transa),
     64         transb_(transb),
     65         m_(m),
     66         n_(n),
     67         k_(k),
     68         dtype_(dtype),
     69         device_id_(device_id) {
     70     hash_code_ = transa;
     71     hash_code_ = Hash64Combine(hash_code_, transb);
     72     hash_code_ = Hash64Combine(hash_code_, m);
     73     hash_code_ = Hash64Combine(hash_code_, n);
     74     hash_code_ = Hash64Combine(hash_code_, k);
     75     hash_code_ = Hash64Combine(hash_code_, dtype);
     76     hash_code_ = Hash64Combine(hash_code_, device_id);
     77   }
     78   bool operator==(const MatmulParameters& other) const {
     79     return this->get_data_as_tuple() == other.get_data_as_tuple();
     80   }
     81 
     82   bool operator!=(const MatmulParameters& other) const {
     83     return !(*this == other);
     84   }
     85   uint64 hash() const { return hash_code_; }
     86 
     87   string ToString() const {
     88     // clang-format off
     89     return strings::StrCat(
     90         transa_, ", ", transb_, ", ",
     91         m_, ", ", n_, ", ", k_,
     92         dtype_, ", ", device_id_);
     93     // clang-format on
     94   }
     95 
     96  private:
     97   typedef std::tuple<bool, bool, int64, int64, int64, DataType, int>
     98       ParameterDataType;
     99 
    100   ParameterDataType get_data_as_tuple() const {
    101     return std::make_tuple(transa_, transb_, m_, n_, k_, dtype_, device_id_);
    102   }
    103 
    104   bool transa_;
    105   bool transb_;
    106   uint64 m_;
    107   uint64 n_;
    108   uint64 k_;
    109   DataType dtype_;
    110   int device_id_;
    111   uint64 hash_code_;
    112 };
    113 
    114 typedef Eigen::GpuDevice GPUDevice;
    115 
    116 #endif  // GOOGLE_CUDA
    117 
    118 }  // end namespace tensorflow
    119 
    120 #endif  // TENSORFLOW_KERNELS_MATMUL_OP_H_
    121