1 /* Copyright 2017 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 #include "tensorflow/compiler/xla/service/cpu/runtime_single_threaded_matmul.h"
17
18 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
19 #include "tensorflow/compiler/xla/service/cpu/runtime_matvec.h"
20 #include "tensorflow/core/platform/dynamic_annotations.h"
21 #include "tensorflow/core/platform/types.h"
22
23 #if defined(TENSORFLOW_USE_CUSTOM_CONTRACTION_KERNEL)
24 #include "tensorflow/core/kernels/eigen_contraction_kernel.h"
25 #endif
26
27 using tensorflow::int32;
28 using tensorflow::int64;
29
30 namespace {
31
Is16BytesAligned(void * ptr)32 bool Is16BytesAligned(void* ptr) {
33 return reinterpret_cast<uintptr_t>(ptr) % 16 == 0;
34 }
35
36 template <typename T, Eigen::AlignmentType Alignment>
MatMul(const void * run_options_ptr,T * out,T * lhs,T * rhs,int64 m,int64 n,int64 k,int32 transpose_lhs,int32 transpose_rhs)37 void MatMul(const void* run_options_ptr, T* out, T* lhs, T* rhs, int64 m,
38 int64 n, int64 k, int32 transpose_lhs, int32 transpose_rhs) {
39 int64 lhs_rows = m;
40 int64 lhs_cols = k;
41 if (transpose_lhs) {
42 std::swap(lhs_rows, lhs_cols);
43 }
44
45 int64 rhs_rows = k;
46 int64 rhs_cols = n;
47 if (transpose_rhs) {
48 std::swap(rhs_rows, rhs_cols);
49 }
50
51 const Eigen::TensorMap<Eigen::Tensor<const T, 2>, Alignment> A(lhs, lhs_rows,
52 lhs_cols);
53 const Eigen::TensorMap<Eigen::Tensor<const T, 2>, Alignment> B(rhs, rhs_rows,
54 rhs_cols);
55 Eigen::TensorMap<Eigen::Tensor<T, 2>, Alignment> C(out, m, n);
56
57 typedef typename Eigen::Tensor<T, 2>::DimensionPair DimPair;
58 int lhs_contract_dim = transpose_lhs ? 0 : 1;
59 int rhs_contract_dim = transpose_rhs ? 1 : 0;
60 const Eigen::array<DimPair, 1> dims(
61 {DimPair(lhs_contract_dim, rhs_contract_dim)});
62
63 // Matrix multiply is a special case of the "contract" operation where
64 // the contraction is performed along dimension 1 of the lhs and dimension
65 // 0 of the rhs.
66 C = A.contract(B, dims);
67 }
68
69 template <typename T>
SingleThreadedMatMulDispatch(const void * run_options_ptr,T * out,T * lhs,T * rhs,int64 m,int64 n,int64 k,int32 transpose_lhs,int32 transpose_rhs)70 void SingleThreadedMatMulDispatch(const void* run_options_ptr, T* out, T* lhs,
71 T* rhs, int64 m, int64 n, int64 k,
72 int32 transpose_lhs, int32 transpose_rhs) {
73 bool all_buffers_16b_aligned =
74 Is16BytesAligned(out) && Is16BytesAligned(lhs) && Is16BytesAligned(rhs);
75
76 if (!all_buffers_16b_aligned) {
77 MatMul<T, Eigen::Unaligned>(run_options_ptr, out, lhs, rhs, m, n, k,
78 transpose_lhs, transpose_rhs);
79 }
80
81 if (m == 1 || n == 1) {
82 xla::EigenMatVec<T>(out, lhs, rhs, m, n, k, transpose_lhs, transpose_rhs);
83 } else {
84 MatMul<T, Eigen::Aligned16>(run_options_ptr, out, lhs, rhs, m, n, k,
85 transpose_lhs, transpose_rhs);
86 }
87 }
88
89 } // namespace
90
91 TF_ATTRIBUTE_NO_SANITIZE_MEMORY void
__xla_cpu_runtime_EigenSingleThreadedMatMulF16(const void * run_options_ptr,Eigen::half * out,Eigen::half * lhs,Eigen::half * rhs,int64 m,int64 n,int64 k,int32 transpose_lhs,int32 transpose_rhs)92 __xla_cpu_runtime_EigenSingleThreadedMatMulF16(
93 const void* run_options_ptr, Eigen::half* out, Eigen::half* lhs,
94 Eigen::half* rhs, int64 m, int64 n, int64 k, int32 transpose_lhs,
95 int32 transpose_rhs) {
96 SingleThreadedMatMulDispatch<Eigen::half>(run_options_ptr, out, lhs, rhs, m,
97 n, k, transpose_lhs, transpose_rhs);
98 }
99
100 TF_ATTRIBUTE_NO_SANITIZE_MEMORY void
__xla_cpu_runtime_EigenSingleThreadedMatMulF32(const void * run_options_ptr,float * out,float * lhs,float * rhs,int64 m,int64 n,int64 k,int32 transpose_lhs,int32 transpose_rhs)101 __xla_cpu_runtime_EigenSingleThreadedMatMulF32(const void* run_options_ptr,
102 float* out, float* lhs,
103 float* rhs, int64 m, int64 n,
104 int64 k, int32 transpose_lhs,
105 int32 transpose_rhs) {
106 SingleThreadedMatMulDispatch<float>(run_options_ptr, out, lhs, rhs, m, n, k,
107 transpose_lhs, transpose_rhs);
108 }
109
110 TF_ATTRIBUTE_NO_SANITIZE_MEMORY void
__xla_cpu_runtime_EigenSingleThreadedMatMulF64(const void * run_options_ptr,double * out,double * lhs,double * rhs,int64 m,int64 n,int64 k,int32 transpose_lhs,int32 transpose_rhs)111 __xla_cpu_runtime_EigenSingleThreadedMatMulF64(const void* run_options_ptr,
112 double* out, double* lhs,
113 double* rhs, int64 m, int64 n,
114 int64 k, int32 transpose_lhs,
115 int32 transpose_rhs) {
116 SingleThreadedMatMulDispatch<double>(run_options_ptr, out, lhs, rhs, m, n, k,
117 transpose_lhs, transpose_rhs);
118 }
119