• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef TENSORFLOW_CONTRIB_RNN_KERNELS_BLAS_GEMM_H_
17 #define TENSORFLOW_CONTRIB_RNN_KERNELS_BLAS_GEMM_H_
18 
19 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
20 #include "tensorflow/core/framework/tensor_types.h"
21 #include "tensorflow/core/kernels/eigen_activations.h"
22 #include "tensorflow/core/platform/types.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 class OpKernelContext;
30 namespace functor {
31 
32 template <typename T>
33 struct TensorCuBlasGemm {
34   void operator()(OpKernelContext* ctx, bool transa, bool transb, uint64 m,
35                   uint64 n, uint64 k, float alpha, const T* a, int lda,
36                   const T* b, int ldb, float beta, T* c, int ldc);
37 };
38 
39 template <typename T>
40 struct gemm_compute_type {
41   typedef T type;
42 };
43 
44 template <>
45 struct gemm_compute_type<Eigen::half> {
46   typedef float type;
47 };
48 
49 template <typename Device, typename T, bool USE_CUBLAS>
50 struct TensorBlasGemm;
51 
52 template <typename Device, typename T>
53 struct TensorBlasGemm<Device, T, true /* USE_CUBLAS */> {
54   static void compute(OpKernelContext* ctx, const Device& d, bool transa,
55                       bool transb, typename gemm_compute_type<T>::type alpha,
56                       typename TTypes<T>::ConstMatrix a,
57                       typename TTypes<T>::ConstMatrix b,
58                       typename gemm_compute_type<T>::type beta,
59                       typename TTypes<T>::Matrix c) {
60     int64 m = c.dimensions()[0];
61     int64 n = c.dimensions()[1];
62     int64 k = transa ? a.dimensions()[0] : a.dimensions()[1];
63 
64     TensorCuBlasGemm<T>()(ctx, transb, transa, n, m, k, alpha, b.data(),
65                           transb ? k : n, a.data(), transa ? m : k, beta,
66                           c.data(), n);
67   }
68 };
69 
70 template <typename Device, typename T>
71 struct TensorBlasGemm<Device, T, false /* USE_CUBLAS */> {
72   static void compute(OpKernelContext* ctx, const Device& d, bool transa,
73                       bool transb, typename gemm_compute_type<T>::type alpha,
74                       typename TTypes<T>::ConstMatrix a,
75                       typename TTypes<T>::ConstMatrix b,
76                       typename gemm_compute_type<T>::type beta,
77                       typename TTypes<T>::Matrix c) {
78     Eigen::array<Eigen::IndexPair<Eigen::DenseIndex>, 1> contract_pairs;
79     contract_pairs[0] =
80         Eigen::IndexPair<Eigen::DenseIndex>(transa == false, transb == true);
81     if (alpha == typename gemm_compute_type<T>::type(1.f) &&
82         beta == typename gemm_compute_type<T>::type(0.f)) {
83       c.device(d) = a.contract(b, contract_pairs);
84     } else if (alpha == typename gemm_compute_type<T>::type(1.f) &&
85                beta == typename gemm_compute_type<T>::type(1.f)) {
86       c.device(d) += a.contract(b, contract_pairs);
87     } else {
88       c.device(d) = c.constant(T(alpha)) * a.contract(b, contract_pairs) +
89                     c.constant(T(beta)) * c;
90     }
91   }
92 };
93 
94 }  // namespace functor
95 }  // namespace tensorflow
96 
97 #endif  // TENSORFLOW_CONTRIB_RNN_KERNELS_BLAS_GEMM_H_
98