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 #ifndef TENSORFLOW_COMPILER_XLA_SERVICE_CPU_RUNTIME_MATVEC_H_
17 #define TENSORFLOW_COMPILER_XLA_SERVICE_CPU_RUNTIME_MATVEC_H_
18
19 #include "third_party/eigen3/Eigen/Core"
20
21 #include "tensorflow/core/platform/types.h"
22
23 namespace xla {
24
25 namespace detail {
26
27 using tensorflow::int32;
28 using tensorflow::int64;
29
30 // Does mat * x or mat^T * x.
31 template <typename T>
MatVec(T * out_buf,T * mat_buf,T * x_buf,int64 rows,int64 cols,int32 transpose)32 void MatVec(T* out_buf, T* mat_buf, T* x_buf, int64 rows, int64 cols,
33 int32 transpose) {
34 // Use an Eigen Matrix instead of a Tensor, as the GEMV from Matrix seems to
35 // be faster (b/30223679). See also: the matmul op kernel in TensorFlow,
36 // which implements the same optimization.
37 using Matrix = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>;
38 using MatrixMap = Eigen::Map<Matrix>;
39
40 using Vector = Eigen::Matrix<T, Eigen::Dynamic, 1>;
41 using VectorMap = Eigen::Map<Vector>;
42
43 auto x = VectorMap(x_buf, cols);
44 auto out = VectorMap(out_buf, rows);
45
46 int64 mat_rows = rows;
47 int64 mat_cols = cols;
48
49 if (transpose) {
50 std::swap(mat_rows, mat_cols);
51 }
52
53 auto mat = MatrixMap(mat_buf, mat_rows, mat_cols);
54
55 if (transpose) {
56 out = mat.transpose() * x;
57 } else {
58 out = mat * x;
59 }
60 }
61
62 // Converts matmul-style args to matvec.
63 template <typename T>
DispatchMatVec(T * out,T * lhs,T * rhs,int64 m,int64 n,int64 k,int32 transpose_lhs,int32 transpose_rhs)64 void DispatchMatVec(T* out, T* lhs, T* rhs, int64 m, int64 n, int64 k,
65 int32 transpose_lhs, int32 transpose_rhs) {
66 // If the input is in the form x * A, where x is the vector, then bring A back
67 // over to the left hand side. We make use of the identity
68 //
69 // (x * A)^T = A^T * x^T
70 //
71 // We do not need to take the transpose of x or of the result since taking
72 // the transpose of a vector does not change the memory layout.
73 const int64 cols = k;
74
75 T* mat;
76 T* vec;
77 int64 rows;
78 bool transpose_mat;
79
80 bool is_mat_vec = (n == 1);
81
82 if (is_mat_vec) {
83 mat = lhs;
84 vec = rhs;
85 rows = m;
86 transpose_mat = transpose_lhs;
87 } else {
88 mat = rhs;
89 vec = lhs;
90 rows = n;
91 transpose_mat = !transpose_rhs;
92 }
93
94 MatVec<T>(out, mat, vec, rows, cols, transpose_mat);
95 }
96
97 } // namespace detail
98
99 // Performs a matrix-vector multiplication using Eigen. 'lhs' and 'rhs' are
100 // pointers to buffers containing input matrices in column-major order. 'out' is
101 // a pointer to a buffer sufficiently large to hold the result of the
102 // operation. Following standard nomenclature: lhs is m x k, rhs is k x n, and
103 // out is m x n.
104 //
105 // This requires that m = 1 or n = 1.
106 //
107 // TODO(b/64684907): Compare runtime performance of these functions with dot
108 // simplification.
109 template <typename T>
EigenMatVec(T * out,T * lhs,T * rhs,tensorflow::int64 m,tensorflow::int64 n,tensorflow::int64 k,tensorflow::int32 transpose_lhs,tensorflow::int32 transpose_rhs)110 void EigenMatVec(T* out, T* lhs, T* rhs, tensorflow::int64 m,
111 tensorflow::int64 n, tensorflow::int64 k,
112 tensorflow::int32 transpose_lhs,
113 tensorflow::int32 transpose_rhs) {
114 assert((m == 1 || n == 1) && "not a matrix-vector multiply");
115 detail::DispatchMatVec<T>(out, lhs, rhs, m, n, k, transpose_lhs,
116 transpose_rhs);
117 }
118
119 } // namespace xla
120
121 #endif // TENSORFLOW_COMPILER_XLA_SERVICE_CPU_RUNTIME_MATVEC_H_
122