aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/compiler/xla/service/cpu/tests/cpu_eigen_dot_operation_test.cc
blob: 18ee25ba9158c28baaf01492c290638b9673f1ec (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/* Copyright 2018 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.
==============================================================================*/

// Tests that we call into Eigen for dot operations as needed.

#include <algorithm>
#include <cctype>
#include <string>

#include "absl/strings/str_cat.h"
#include "tensorflow/compiler/xla/service/cpu/cpu_compiler.h"
#include "tensorflow/compiler/xla/service/cpu/tests/cpu_codegen_test.h"
#include "tensorflow/compiler/xla/service/hlo_computation.h"
#include "tensorflow/compiler/xla/tests/test_utils.h"
#include "tensorflow/core/platform/test.h"

namespace xla {
namespace cpu {
namespace {

struct DotTestSpec {
  PrimitiveType primitive_type;
  string filecheck_lines;
};

string DotTestSpecToString(const ::testing::TestParamInfo<DotTestSpec>& info) {
  return PrimitiveType_Name(info.param.primitive_type);
}

class CpuEigenDotOperationTest
    : public CpuCodegenTest,
      public ::testing::WithParamInterface<DotTestSpec> {
 protected:
  void CompileAndCheck(std::unique_ptr<HloComputation> entry_computation,
                       const string& filecheck_lines) {
    CpuAotCompilationOptions options{
        /*triple=*/"x86_64", /*cpu_name=*/"", /*features=*/"",
        /*entry_point_name=*/"entry",
        /*relocation_model=*/CpuAotCompilationOptions::RelocationModel::Static};

    auto hlo_module = CreateNewModule();
    hlo_module->AddEntryComputation(std::move(entry_computation));

    CompileAheadOfTimeAndVerifyIr(std::move(hlo_module), options,
                                  filecheck_lines,
                                  /*match_optimized_ir=*/true);
  }
};

TEST_P(CpuEigenDotOperationTest, SimpleDotOp) {
  HloComputation::Builder builder(TestName());
  DotTestSpec spec = GetParam();

  auto param_shape = ShapeUtil::MakeShape(spec.primitive_type, {128, 128});

  HloInstruction* lhs = builder.AddInstruction(
      HloInstruction::CreateParameter(0, param_shape, "input"));
  HloInstruction* rhs = builder.AddInstruction(
      HloInstruction::CreateParameter(1, param_shape, "input"));

  builder.AddInstruction(CreateCanonicalDot(param_shape, lhs, rhs));
  CompileAndCheck(builder.Build(), spec.filecheck_lines);
}

TEST_P(CpuEigenDotOperationTest, DotTransposeOp) {
  HloComputation::Builder builder(TestName());
  DotTestSpec spec = GetParam();

  auto param_shape = ShapeUtil::MakeShape(spec.primitive_type, {128, 128});

  HloInstruction* lhs = builder.AddInstruction(
      HloInstruction::CreateParameter(0, param_shape, "input"));
  HloInstruction* rhs = builder.AddInstruction(
      HloInstruction::CreateParameter(1, param_shape, "input"));
  HloInstruction* lhs_transposed = builder.AddInstruction(
      HloInstruction::CreateTranspose(param_shape, lhs, {1, 0}));

  builder.AddInstruction(CreateCanonicalDot(param_shape, lhs_transposed, rhs));
  CompileAndCheck(builder.Build(), spec.filecheck_lines);
}

std::vector<DotTestSpec> GetDotTestCases() {
  std::vector<DotTestSpec> result;
  result.push_back(
      {F16, R"(CHECK: call void @__xla_cpu_runtime_EigenMatMulF16)"});
  result.push_back(
      {F32, R"(CHECK: call void @__xla_cpu_runtime_EigenMatMulF32)"});
  result.push_back(
      {F64, R"(CHECK: call void @__xla_cpu_runtime_EigenMatMulF64)"});
  return result;
}

INSTANTIATE_TEST_CASE_P(CpuEigenDotOperationTestInstantiation,
                        CpuEigenDotOperationTest,
                        ::testing::ValuesIn(GetDotTestCases()),
                        DotTestSpecToString);

}  // namespace
}  // namespace cpu
}  // namespace xla