Home | History | Annotate | Download | only in cpu
      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 <algorithm>
     17 #include <cassert>
     18 
     19 #include "third_party/eigen3/Eigen/Core"
     20 #include "tensorflow/compiler/xla/service/cpu/runtime_matvec.h"
     21 
     22 using tensorflow::int32;
     23 using tensorflow::int64;
     24 
     25 namespace {
     26 
     27 // Does mat * x or mat^T * x.
     28 template <typename T>
     29 void MatVec(T* out_buf, T* mat_buf, T* x_buf, int64 rows, int64 cols,
     30             int32 transpose) {
     31   // Use an Eigen Matrix instead of a Tensor, as the GEMV from Matrix seems to
     32   // be faster (b/30223679).  See also: the matmul op kernel in TensorFlow,
     33   // which implements the same optimization.
     34   using Matrix = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>;
     35   using MatrixMap = Eigen::Map<Matrix>;
     36 
     37   using Vector = Eigen::Matrix<T, Eigen::Dynamic, 1>;
     38   using VectorMap = Eigen::Map<Vector>;
     39 
     40   auto x = VectorMap(x_buf, cols);
     41   auto out = VectorMap(out_buf, rows);
     42 
     43   int64 mat_rows = rows;
     44   int64 mat_cols = cols;
     45 
     46   if (transpose) {
     47     std::swap(mat_rows, mat_cols);
     48   }
     49 
     50   auto mat = MatrixMap(mat_buf, mat_rows, mat_cols);
     51 
     52   if (transpose) {
     53     out = mat.transpose() * x;
     54   } else {
     55     out = mat * x;
     56   }
     57 }
     58 
     59 // Converts matmul-style args to matvec.
     60 template <typename T>
     61 void DispatchMatVec(T* out, T* lhs, T* rhs, int64 m, int64 n, int64 k,
     62                     int32 transpose_lhs, int32 transpose_rhs) {
     63   // If the input is in the form x * A, where x is the vector, then bring A back
     64   // over to the left hand side.  We make use of the identity
     65   //
     66   //   (x * A)^T = A^T * x^T
     67   //
     68   // We do not need to take the transpose of x or of the result since taking
     69   // the transpose of a vector does not change the memory layout.
     70   const int64 cols = k;
     71 
     72   T* mat;
     73   T* vec;
     74   int64 rows;
     75   bool transpose_mat;
     76 
     77   bool is_mat_vec = (n == 1);
     78 
     79   if (is_mat_vec) {
     80     mat = lhs;
     81     vec = rhs;
     82     rows = m;
     83     transpose_mat = transpose_lhs;
     84   } else {
     85     mat = rhs;
     86     vec = lhs;
     87     rows = n;
     88     transpose_mat = !transpose_rhs;
     89   }
     90 
     91   MatVec<T>(out, mat, vec, rows, cols, transpose_mat);
     92 }
     93 
     94 }  // namespace
     95 
     96 namespace xla {
     97 
     98 void EigenMatVecF32(float* out, float* lhs, float* rhs, int64 m, int64 n,
     99                     int64 k, int32 transpose_lhs, int32 transpose_rhs) {
    100   assert((m == 1 || n == 1) && "not a matrix-vector multiply");
    101   DispatchMatVec<float>(out, lhs, rhs, m, n, k, transpose_lhs, transpose_rhs);
    102 }
    103 
    104 void EigenMatVecF64(double* out, double* lhs, double* rhs, int64 m, int64 n,
    105                     int64 k, int32 transpose_lhs, int32 transpose_rhs) {
    106   assert((m == 1 || n == 1) && "not a matrix-vector multiply");
    107   DispatchMatVec<double>(out, lhs, rhs, m, n, k, transpose_lhs, transpose_rhs);
    108 }
    109 
    110 }  // namespace xla
    111