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