aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/compiler/xla/service/hlo_matchers.cc
blob: 5269cad94d35be3dd1c009588bbe422ff1533364 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* 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/hlo_matchers.h"

#include "absl/strings/str_join.h"
#include "tensorflow/compiler/xla/service/hlo_instruction.h"
#include "tensorflow/compiler/xla/test.h"

namespace xla {
namespace testing {

bool HloMatcher::MatchAndExplain(
    const HloInstruction* instruction,
    ::testing::MatchResultListener* listener) const {
  // These cases are self-explanatory from the printed value.
  if (!instruction || instruction->opcode() != opcode_) {
    return false;
  }
  // Special case: no operand matchers means don't verify.
  if (operands_.empty()) {
    return true;
  }
  const auto& operands = instruction->operands();
  if (operands.size() != operands_.size()) {
    *listener << "has too "
              << (operands.size() > operands_.size() ? "many" : "few")
              << " operands (got " << operands.size() << ", want "
              << operands_.size() << ")";
    return false;
  }
  for (int index = 0; index < operands.size(); index++) {
    ::testing::StringMatchResultListener inner_listener;
    if (!operands_[index].MatchAndExplain(operands[index], &inner_listener)) {
      if (listener->IsInterested()) {
        *listener << "\noperand " << index << ":\n\t"
                  << operands[index]->ToString()
                  << "\ndoesn't match expected:\n\t";
        operands_[index].DescribeTo(listener->stream());
        string explanation = inner_listener.str();
        if (!explanation.empty()) {
          *listener << ", " << explanation;
        }
      }
      return false;
    }
  }
  return true;
}

void HloMatcher::DescribeTo(::std::ostream* os) const {
  *os << opcode_;
  if (!operands_.empty()) {
    *os << "(";
    for (int i = 0; i < operands_.size(); i++) {
      if (i > 0) {
        *os << ", ";
      }
      operands_[i].DescribeTo(os);
    }
    *os << ")";
  }
}

bool HloParameterMatcher::MatchAndExplain(
    const HloInstruction* instruction,
    ::testing::MatchResultListener* listener) const {
  if (!HloMatcher::MatchAndExplain(instruction, listener)) {
    return false;
  }
  if (instruction->parameter_number() != parameter_number_) {
    *listener << "has wrong parameter number (got "
              << instruction->parameter_number() << ", want "
              << parameter_number_ << ")";
    return false;
  }
  return true;
}

bool HloGetTupleElementMatcher::MatchAndExplain(
    const HloInstruction* instruction,
    ::testing::MatchResultListener* listener) const {
  if (!HloMatcher::MatchAndExplain(instruction, listener)) {
    return false;
  }
  if (instruction->tuple_index() != tuple_index_) {
    *listener << "has wrong tuple index (got " << instruction->tuple_index()
              << ", want " << tuple_index_ << ")";
    return false;
  }
  return true;
}

void HloCustomCallMatcher::DescribeTo(std::ostream* os) const {
  HloMatcher::DescribeTo(os);
  *os << " with call target that ";
  call_target_matcher_.DescribeTo(os);
}

bool HloCustomCallMatcher::MatchAndExplain(
    const HloInstruction* instruction,
    ::testing::MatchResultListener* listener) const {
  if (!HloMatcher::MatchAndExplain(instruction, listener)) {
    return false;
  }
  ::testing::StringMatchResultListener sub_listener;
  bool result = ExplainMatchResult(
      call_target_matcher_, instruction->custom_call_target(), &sub_listener);
  if (sub_listener.str().empty()) {
    sub_listener << " that ";

    std::stringstream desc_stream;
    if (result) {
      call_target_matcher_.DescribeTo(&desc_stream);
    } else {
      call_target_matcher_.DescribeNegationTo(&desc_stream);
    }
    sub_listener << desc_stream.str();
  }
  *listener << "custom-call with call target" << sub_listener.str();
  return result;
}

bool HloShapeMatcher::MatchAndExplain(
    const HloInstruction* instruction,
    ::testing::MatchResultListener* listener) const {
  if (ShapeUtil::Compatible(instruction->shape(), shape_)) {
    return true;
  }
  *listener << instruction->ToString() << " has incorrect shape (expected: "
            << ShapeUtil::HumanString(shape_) << ")";
  return false;
}

void HloShapeMatcher::DescribeTo(std::ostream* os) const {
  *os << ShapeUtil::HumanString(shape_);
}

bool HloShapeAndLayoutMatcher::MatchAndExplain(
    const HloInstruction* instruction,
    ::testing::MatchResultListener* listener) const {
  if (ShapeUtil::Equal(instruction->shape(), shape_)) {
    return true;
  }
  *listener << instruction->ToString() << " has incorrect shape (expected: "
            << ShapeUtil::HumanStringWithLayout(shape_) << ")";
  return false;
}

void HloShapeAndLayoutMatcher::DescribeTo(std::ostream* os) const {
  *os << ShapeUtil::HumanStringWithLayout(shape_);
}

bool HloShardingMatcher::MatchAndExplain(
    const HloInstruction* instruction,
    ::testing::MatchResultListener* listener) const {
  if (!sharding_.has_value()) {
    if (!instruction->has_sharding()) {
      return true;
    }
    *listener << instruction->ToString() << " expected to have no sharding.";
    return false;
  }
  if (instruction->has_sharding()) {
    if (instruction->sharding() == sharding_.value()) {
      return true;
    }
    *listener << instruction->ToString()
              << " has incorrect sharding (expected: " << sharding_->ToString()
              << ")";
    return false;
  } else {
    *listener << instruction->ToString()
              << " has no sharding (expected: " << sharding_->ToString() << ")";
    return false;
  }
}

void HloShardingMatcher::DescribeTo(std::ostream* os) const {
  if (sharding_.has_value()) {
    *os << sharding_->ToString();
  } else {
    *os << "<no-sharding>";
  }
}

bool HloDotWithContractingDimsMatcher::MatchAndExplain(
    const HloInstruction* instruction,
    ::testing::MatchResultListener* listener) const {
  if (!HloMatcher::MatchAndExplain(instruction, listener)) {
    return false;
  }

  const DotDimensionNumbers& dim_nums = instruction->dot_dimension_numbers();
  if (dim_nums.lhs_contracting_dimensions_size() != 1 ||
      dim_nums.lhs_contracting_dimensions(0) != lhs_contracting_dim_) {
    *listener << instruction->ToString()
              << " has wrong lhs_contracting_dimensions (got {"
              << absl::StrJoin(dim_nums.lhs_contracting_dimensions(), ",")
              << "} want {" << lhs_contracting_dim_ << "})";
    return false;
  }

  if (dim_nums.rhs_contracting_dimensions_size() != 1 ||
      dim_nums.rhs_contracting_dimensions(0) != rhs_contracting_dim_) {
    *listener << instruction->ToString()
              << " has wrong rhs_contracting_dimensions (got {"
              << absl::StrJoin(dim_nums.rhs_contracting_dimensions(), ",")
              << "} want {" << rhs_contracting_dim_ << "})";
    return false;
  }

  return true;
}

void HloDotWithContractingDimsMatcher::DescribeTo(std::ostream* os) const {
  HloMatcher::DescribeTo(os);
  *os << " with lhs_contracting_dims={" << lhs_contracting_dim_
      << "} and rhs_contracting_dims={" << rhs_contracting_dim_ << "}";
}

}  // namespace testing

void PrintTo(const HloInstruction* inst, ::std::ostream* os) {
  *os << (inst ? inst->ToString() : "nullptr");
}

void PrintTo(HloInstruction* inst, ::std::ostream* os) {
  PrintTo(const_cast<const HloInstruction*>(inst), os);
}

}  // namespace xla