aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/compiler/xla/service/cpu/runtime_matmul.cc
blob: 677080a8623224cdd65e35b3116ae57b7b3b3ca2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/compiler/xla/service/cpu/runtime_matmul.h"

#define EIGEN_USE_THREADS

#include "third_party/eigen3/unsupported/Eigen/CXX11/Tensor"
#include "tensorflow/compiler/xla/executable_run_options.h"
#include "tensorflow/core/platform/types.h"

using tensorflow::int32;
using tensorflow::int64;

namespace {

template <typename T>
void MatMul(const void* run_options_ptr, T* out, T* lhs, T* rhs, int64 m,
            int64 n, int64 k, int32 transpose_lhs, int32 transpose_rhs) {
  const xla::ExecutableRunOptions* run_options =
      static_cast<const xla::ExecutableRunOptions*>(run_options_ptr);

  int64 lhs_rows = m;
  int64 lhs_cols = k;
  if (transpose_lhs) {
    std::swap(lhs_rows, lhs_cols);
  }

  int64 rhs_rows = k;
  int64 rhs_cols = n;
  if (transpose_rhs) {
    std::swap(rhs_rows, rhs_cols);
  }

  const Eigen::TensorMap<Eigen::Tensor<const T, 2>, Eigen::Aligned> A(
      lhs, lhs_rows, lhs_cols);
  const Eigen::TensorMap<Eigen::Tensor<const T, 2>, Eigen::Aligned> B(
      rhs, rhs_rows, rhs_cols);
  Eigen::TensorMap<Eigen::Tensor<T, 2>, Eigen::Aligned> C(out, m, n);

  typedef typename Eigen::Tensor<T, 2>::DimensionPair DimPair;
  int lhs_contract_dim = transpose_lhs ? 0 : 1;
  int rhs_contract_dim = transpose_rhs ? 1 : 0;
  const Eigen::array<DimPair, 1> dims(
      DimPair(lhs_contract_dim, rhs_contract_dim));

  // Matrix multiply is a special case of the "contract" operation where
  // the contraction is performed along dimension 1 of the lhs and dimension
  // 0 of the rhs.
  C.device(*run_options->intra_op_thread_pool()) = A.contract(B, dims);
}

}  // namespace

void __xla_cpu_runtime_EigenMatMulF32(const void* run_options_ptr, float* out,
                                      float* lhs, float* rhs, int64 m, int64 n,
                                      int64 k, int32 transpose_lhs,
                                      int32 transpose_rhs) {
  MatMul<float>(run_options_ptr, out, lhs, rhs, m, n, k, transpose_lhs,
                transpose_rhs);
}

void __xla_cpu_runtime_EigenMatMulF64(const void* run_options_ptr, double* out,
                                      double* lhs, double* rhs, int64 m,
                                      int64 n, int64 k, int32 transpose_lhs,
                                      int32 transpose_rhs) {
  MatMul<double>(run_options_ptr, out, lhs, rhs, m, n, k, transpose_lhs,
                 transpose_rhs);
}