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 "tensorflow/compiler/xla/service/cpu/runtime_matmul.h"
     17 
     18 #define EIGEN_USE_THREADS
     19 
     20 #include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
     21 #include "tensorflow/compiler/xla/executable_run_options.h"
     22 #include "tensorflow/compiler/xla/service/cpu/runtime_matvec.h"
     23 #include "tensorflow/core/platform/types.h"
     24 
     25 using tensorflow::int32;
     26 using tensorflow::int64;
     27 
     28 namespace {
     29 
     30 template <typename T>
     31 void MatMul(const void* run_options_ptr, T* out, T* lhs, T* rhs, int64 m,
     32             int64 n, int64 k, int32 transpose_lhs, int32 transpose_rhs) {
     33   const xla::ExecutableRunOptions* run_options =
     34       static_cast<const xla::ExecutableRunOptions*>(run_options_ptr);
     35 
     36   int64 lhs_rows = m;
     37   int64 lhs_cols = k;
     38   if (transpose_lhs) {
     39     std::swap(lhs_rows, lhs_cols);
     40   }
     41 
     42   int64 rhs_rows = k;
     43   int64 rhs_cols = n;
     44   if (transpose_rhs) {
     45     std::swap(rhs_rows, rhs_cols);
     46   }
     47 
     48   const Eigen::TensorMap<Eigen::Tensor<const T, 2>, Eigen::Aligned> A(
     49       lhs, lhs_rows, lhs_cols);
     50   const Eigen::TensorMap<Eigen::Tensor<const T, 2>, Eigen::Aligned> B(
     51       rhs, rhs_rows, rhs_cols);
     52   Eigen::TensorMap<Eigen::Tensor<T, 2>, Eigen::Aligned> C(out, m, n);
     53 
     54   typedef typename Eigen::Tensor<T, 2>::DimensionPair DimPair;
     55   int lhs_contract_dim = transpose_lhs ? 0 : 1;
     56   int rhs_contract_dim = transpose_rhs ? 1 : 0;
     57   const Eigen::array<DimPair, 1> dims(
     58       {DimPair(lhs_contract_dim, rhs_contract_dim)});
     59 
     60   // Matrix multiply is a special case of the "contract" operation where
     61   // the contraction is performed along dimension 1 of the lhs and dimension
     62   // 0 of the rhs.
     63   C.device(*run_options->intra_op_thread_pool()) = A.contract(B, dims);
     64 }
     65 
     66 }  // namespace
     67 
     68 void __xla_cpu_runtime_EigenMatMulF32(const void* run_options_ptr, float* out,
     69                                       float* lhs, float* rhs, int64 m, int64 n,
     70                                       int64 k, int32 transpose_lhs,
     71                                       int32 transpose_rhs) {
     72   if (m == 1 || n == 1) {
     73     // Despite being single threaded, this version of matrix * vector is faster.
     74     xla::EigenMatVecF32(out, lhs, rhs, m, n, k, transpose_lhs, transpose_rhs);
     75   } else {
     76     MatMul<float>(run_options_ptr, out, lhs, rhs, m, n, k, transpose_lhs,
     77                   transpose_rhs);
     78   }
     79 }
     80 
     81 void __xla_cpu_runtime_EigenMatMulF64(const void* run_options_ptr, double* out,
     82                                       double* lhs, double* rhs, int64 m,
     83                                       int64 n, int64 k, int32 transpose_lhs,
     84                                       int32 transpose_rhs) {
     85   if (m == 1 || n == 1) {
     86     // Despite being single threaded, this version of matrix * vector is faster.
     87     xla::EigenMatVecF64(out, lhs, rhs, m, n, k, transpose_lhs, transpose_rhs);
     88   } else {
     89     MatMul<double>(run_options_ptr, out, lhs, rhs, m, n, k, transpose_lhs,
     90                    transpose_rhs);
     91   }
     92 }
     93