aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/compiler/jit/node_matchers.cc
blob: d8ace628e6b76e011ecddd4d526efc4db9c9237e (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/* 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.
==============================================================================*/

#include "tensorflow/compiler/jit/node_matchers.h"

#include <utility>
#include "absl/algorithm/container.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_join.h"
#include "absl/strings/str_split.h"
#include "tensorflow/core/framework/tensor.pb.h"

namespace tensorflow {
namespace testing {
namespace matchers {
namespace {

using impl::NodeMatcherProperties;

string IndentAllButFirstLine(absl::string_view text) {
  std::vector<std::string> lines = absl::StrSplit(text, '\n');
  for (int i = 1; i < lines.size(); i++) {
    lines[i].insert(0, "  ");
  }
  return absl::StrJoin(lines, "\n");
}

template <typename T>
bool CompareTensor(const Tensor& actual, const Tensor& expected,
                   ::testing::MatchResultListener* listener) {
  if (actual.NumElements() != expected.NumElements()) {
    if (listener->IsInterested()) {
      *listener << "\nwas looking for tensor with " << expected.NumElements()
                << " elements, found tensor with " << actual.NumElements()
                << " elements";
      return false;
    }
  }

  for (int64 i = 0, e = actual.NumElements(); i < e; i++) {
    if (actual.flat<T>()(i) != expected.flat<T>()(i)) {
      *listener << "\nmismatch in constant tensor at index " << i
                << " expected = " << expected.flat<T>()(i)
                << " actual = " << actual.flat<T>()(i);
      return false;
    }
  }

  return true;
}

bool MatchAndExplainTensor(const Tensor& tensor, const Tensor& expected_tensor,
                           ::testing::MatchResultListener* listener) {
  if (tensor.dtype() != expected_tensor.dtype()) {
    if (listener->IsInterested()) {
      *listener << "\nexpected tensor of type "
                << DataType_Name(expected_tensor.dtype())
                << " but found one of type " << DataType_Name(tensor.dtype());
      return false;
    }
  }

  switch (tensor.dtype()) {
    case DT_FLOAT:
      return CompareTensor<float>(tensor, expected_tensor, listener);
    case DT_DOUBLE:
      return CompareTensor<double>(tensor, expected_tensor, listener);
    case DT_INT8:
      return CompareTensor<int8>(tensor, expected_tensor, listener);
    case DT_INT16:
      return CompareTensor<int16>(tensor, expected_tensor, listener);
    case DT_INT32:
      return CompareTensor<int32>(tensor, expected_tensor, listener);
    case DT_INT64:
      return CompareTensor<int64>(tensor, expected_tensor, listener);
    case DT_UINT8:
      return CompareTensor<uint8>(tensor, expected_tensor, listener);
    case DT_UINT16:
      return CompareTensor<uint16>(tensor, expected_tensor, listener);
    case DT_UINT32:
      return CompareTensor<uint32>(tensor, expected_tensor, listener);
    case DT_UINT64:
      return CompareTensor<uint64>(tensor, expected_tensor, listener);
    default:
      LOG(FATAL) << "Unsupported dtype "  // Crash ok: testonly.
                 << DataType_Name(tensor.dtype());
  }
}

using Input = std::pair<const Node*, int>;

struct NodeMatcher : public ::testing::MatcherInterface<const Node*> {
  bool MatchAndExplain(
      const Node* node,
      ::testing::MatchResultListener* listener) const override {
    if (op && node->type_string() != *op) {
      if (listener->IsInterested()) {
        *listener << "\nexpected op " << *op << " but found "
                  << node->type_string();
      }
      return false;
    }

    if (assigned_device && node->assigned_device_name() != *assigned_device) {
      if (listener->IsInterested()) {
        *listener << "\nexpected assigned_device " << *assigned_device
                  << " but found \"" << node->assigned_device_name() << "\"";
      }
      return false;
    }

    if (name && node->name() != *name) {
      if (listener->IsInterested()) {
        *listener << "\nexpected name " << *name << " but found "
                  << node->name();
      }
      return false;
    }

    if (constant_value) {
      const TensorProto* proto = nullptr;
      if (!GetNodeAttr(node->def(), "value", &proto).ok()) {
        if (listener->IsInterested()) {
          *listener << "\ncould not find \"value\" attribute in node";
        }
        return false;
      }

      Tensor tensor(proto->dtype());
      if (!tensor.FromProto(*proto)) {
        if (listener->IsInterested()) {
          *listener << "\ncould not convert TensorProto in \"value\" attribute "
                       "to Tensor";
        }
        return false;
      }

      if (!MatchAndExplainTensor(/*tensor=*/tensor,
                                 /*expected_tensor=*/*constant_value,
                                 listener)) {
        return false;
      }
    }

    if (input_matchers) {
      if (input_matchers->size() != node->num_inputs()) {
        if (listener->IsInterested()) {
          *listener << "\nexpected " << input_matchers->size()
                    << " inputs but node has " << node->num_inputs();
        }
        return false;
      }

      for (int input_idx = 0, e = input_matchers->size(); input_idx < e;
           input_idx++) {
        if (!MatchAndExplainInput(node, input_idx, listener)) {
          return false;
        }
      }
    }

    std::vector<const Node*> control_deps;
    for (const Edge* e : node->in_edges()) {
      if (e->IsControlEdge()) {
        control_deps.push_back(e->src());
      }
    }

    ::testing::StringMatchResultListener inner_listener;
    if (control_dep_set &&
        !control_dep_set->MatchAndExplain(control_deps, &inner_listener)) {
      if (listener->IsInterested()) {
        string explanation = inner_listener.str();
        if (!explanation.empty()) {
          explanation = absl::StrCat(", ", explanation, ",");
        }
        *listener << "ctrl_deps" << explanation << " does not match expected: ";
        control_dep_set->DescribeTo(listener->stream());
      }
      return false;
    }
    return true;
  }

  void DescribeTo(::std::ostream* os) const override {
    std::vector<string> predicates;

    if (name) {
      predicates.push_back(absl::StrCat("name: ", *name));
    }

    if (op) {
      predicates.push_back(absl::StrCat("op: ", *op));
    }

    if (assigned_device) {
      predicates.push_back(absl::StrCat("assigned device: ", *assigned_device));
    }

    bool printed_something = !predicates.empty();

    *os << absl::StrJoin(predicates, ", ");

    if (constant_value) {
      printed_something = true;
      *os << "constant value: " << constant_value->DebugString();
    }

    if (input_matchers) {
      if (!input_matchers->empty()) {
        printed_something = true;
        *os << " with " << (input_matchers->size() == 1 ? "only " : "")
            << "input" << (input_matchers->size() == 1 ? "" : "s") << " ";
      }

      if (input_matchers->size() == 1) {
        ::std::stringstream ss;
        input_matchers->front().DescribeTo(&ss);
        printed_something = true;
        *os << "matching " << ss.str();
      } else {
        int edge_idx = 0;
        for (const ::testing::Matcher<Input>& matcher : (*input_matchers)) {
          *os << "\n  [" << edge_idx << "] matching (";
          ::std::stringstream ss;
          matcher.DescribeTo(&ss);
          printed_something = true;
          *os << IndentAllButFirstLine(ss.str());
          *os << ")";
          edge_idx++;
        }
      }
    }

    if (control_dep_set) {
      printed_something = true;
      *os << " and control deps ";
      control_dep_set->DescribeTo(os);
    }

    if (!printed_something) {
      *os << "is any node";
    }
  }

  bool MatchAndExplainInput(const Node* node, int input_idx,
                            ::testing::MatchResultListener* listener) const {
    const Edge* edge;
    if (!node->input_edge(input_idx, &edge).ok()) {
      if (listener->IsInterested()) {
        *listener << "\ncould not find incoming edge for input " << input_idx;
      }
      return false;
    }

    ::testing::StringMatchResultListener inner_listener;
    Input input = {edge->src(), edge->src_output()};
    if ((*input_matchers)[input_idx].MatchAndExplain(input, &inner_listener)) {
      return true;
    }

    if (listener->IsInterested()) {
      *listener << "\ninput " << input_idx << " does not match expected:\n";
      (*input_matchers)[input_idx].DescribeTo(listener->stream());
      string explanation = inner_listener.str();
      if (!explanation.empty()) {
        *listener << ", " << explanation;
      }
    }
    return false;
  }

  absl::optional<string> op;
  absl::optional<string> name;
  absl::optional<string> assigned_device;
  absl::optional<Tensor> constant_value;
  absl::optional<std::vector<::testing::Matcher<Input>>> input_matchers;
  absl::optional<::testing::Matcher<absl::Span<const Node* const>>>
      control_dep_set;
};

// Matches a dst and dst_output on an input edge.  Today we only use this with
// dst_output=0 but we will eventually need to support multi-output operations.
class InputMatcher : public ::testing::MatcherInterface<Input> {
 public:
  InputMatcher(::testing::Matcher<const Node*> src_matcher, int src_output)
      : src_matcher_(std::move(src_matcher)), src_output_(src_output) {}

  bool MatchAndExplain(
      Input input, ::testing::MatchResultListener* listener) const override {
    ::testing::StringMatchResultListener inner_listener;
    if (!src_matcher_.MatchAndExplain(input.first, &inner_listener)) {
      if (listener->IsInterested()) {
        *listener << "\nsource does not match expected ";
        src_matcher_.DescribeTo(listener->stream());
        string explanation = inner_listener.str();
        if (!explanation.empty()) {
          *listener << "\n\t" << explanation;
        }
      }
      return false;
    }
    if (input.second != src_output_) {
      if (listener->IsInterested()) {
        *listener << "\nexpected output slot to be " << src_output_
                  << " but found " << input.second;
      }
      return false;
    }

    return true;
  }

  void DescribeTo(::std::ostream* os) const override {
    if (src_output_) {
      *os << "output slot: " << src_output_ << ", source: (";
    }

    src_matcher_.DescribeTo(os);

    if (src_output_) {
      *os << ")";
    }
  }

 private:
  ::testing::Matcher<const Node*> src_matcher_;
  int src_output_;
};

std::vector<::testing::Matcher<Input>> NodeMatchersToInputMatchers(
    absl::Span<const ::testing::Matcher<const Node*>> node_matchers) {
  std::vector<::testing::Matcher<Input>> result;
  absl::c_transform(node_matchers, std::back_inserter(result),
                    [](::testing::Matcher<const Node*> n) {
                      return ::testing::MakeMatcher(new InputMatcher(n, 0));
                    });
  return result;
}
}  // namespace

::testing::Matcher<const Node*> impl::NodeWith(
    absl::Span<const NodeMatcherProperties> props) {
  NodeMatcher* matcher = new NodeMatcher();
  for (const NodeMatcherProperties& prop : props) {
    if (prop.name()) {
      DCHECK(!matcher->name);
      matcher->name = prop.name();
    }

    if (prop.op()) {
      DCHECK(!matcher->op);
      matcher->op = prop.op();
    }

    if (prop.constant_value()) {
      DCHECK(!matcher->constant_value);
      matcher->constant_value = prop.constant_value();
    }

    if (prop.assigned_device()) {
      DCHECK(!matcher->assigned_device);
      matcher->assigned_device = prop.assigned_device();
    }

    if (prop.input_nodes()) {
      DCHECK(!matcher->input_matchers);
      matcher->input_matchers =
          NodeMatchersToInputMatchers(*prop.input_nodes());
    }

    if (prop.control_deps()) {
      DCHECK(!matcher->control_dep_set);
      matcher->control_dep_set =
          ::testing::UnorderedElementsAreArray(*prop.control_deps());
    }
  }

  return ::testing::MakeMatcher(matcher);
}

impl::NodeMatcherProperties Name(string name) {
  impl::NodeMatcherProperties props;
  props.set_name(std::move(name));
  return props;
}

// Matches a node with op `op`.
impl::NodeMatcherProperties Op(string op) {
  impl::NodeMatcherProperties props;
  props.set_op(std::move(op));
  return props;
}

// Matches a node with assigned device `assigned_device`.
impl::NodeMatcherProperties AssignedDevice(string assigned_device) {
  impl::NodeMatcherProperties props;
  props.set_assigned_device(std::move(assigned_device));
  return props;
}

impl::NodeMatcherProperties impl::Inputs(
    absl::Span<const ::testing::Matcher<const Node*>> inputs) {
  std::vector<::testing::Matcher<const Node*>> inputs_vector;
  absl::c_copy(inputs, std::back_inserter(inputs_vector));

  impl::NodeMatcherProperties props;
  props.set_input_nodes(std::move(inputs_vector));
  return props;
}

impl::NodeMatcherProperties impl::CtrlDeps(
    absl::Span<const ::testing::Matcher<const Node*>> control_deps) {
  std::vector<::testing::Matcher<const Node*>> control_deps_vector;
  absl::c_copy(control_deps, std::back_inserter(control_deps_vector));

  impl::NodeMatcherProperties props;
  props.set_control_deps(std::move(control_deps_vector));
  return props;
}

NodeMatcherProperties ConstantValue(
    const ::tensorflow::Input::Initializer& val) {
  TF_CHECK_OK(val.status);
  NodeMatcherProperties props;
  props.set_constant_value(val.tensor);
  return props;
}

::testing::Matcher<const Node*> Const(
    const ::tensorflow::Input::Initializer& val) {
  return NodeWith(ConstantValue(val));
}
}  // namespace matchers

Node* FindNodeByName(Graph* g, absl::string_view name) {
  for (Node* n : g->nodes()) {
    if (n->name() == name) {
      return n;
    }
  }

  return nullptr;
}
}  // namespace testing
}  // namespace tensorflow