1/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#ifndef TENSORFLOW_CORE_UTIL_EQUAL_GRAPH_DEF_H_
17#define TENSORFLOW_CORE_UTIL_EQUAL_GRAPH_DEF_H_
18
19#include "tensorflow/core/framework/graph_def_util.h"
20#include "tensorflow/core/platform/protobuf.h"
21#include "tensorflow/core/platform/types.h"
22
23namespace tensorflow {
24
25class GraphDef;
26class NodeDef;
27
28struct EqualGraphDefOptions {
29 // Should internal attributes (attribute names that start with '_') be
30 // ignored?
31 bool ignore_internal_attrs = true;
32};
33
34// Determines if actual and expected are equal, ignoring versions and ordering
35// of nodes, attrs, and control inputs. If the GraphDefs are different and
36// diff != nullptr, *diff is set to an explanation of the difference. Note that
37// we use node names to match up nodes between the graphs, and so the naming of
38// nodes must be consistent.
39bool EqualGraphDef(const GraphDef& actual, const GraphDef& expected,
40 string* diff, const EqualGraphDefOptions& options = {});
41
42// Returns a hash of `gdef` that is consistent with EqualGraphDef. In other
43// words, if two graph defs compare equal according to EqualGraphDef,
44// GraphDefHash will return the same value for both of them when called
45// with the same `options` that was used in the call to EqualGraphDef.
46// Similarly to protobuf deterministic serialization, hash value is
47// guaranteed to be stable only for a given binary. In particular, one should
48// probably not persist the returned value.
49uint64 GraphDefHash(const GraphDef& gdef,
50 const EqualGraphDefOptions& options = {});
51
52// Determines if actual and expected are equal, ignoring: ordering of
53// attrs, internal attributes (if set in `options`), and control inputs.
54//
55// If the NodeDefs are different and
56// diff != nullptr, *diff is set to an explanation of the difference.
57bool EqualNodeDef(const NodeDef& actual, const NodeDef& expected, string* diff,
58 const EqualGraphDefOptions& options = {});
59
60// Returns a hash of `ndef` that is consistent with EqualNodeDef. In other
61// words, if two node defs compare equal according to EqualNodeDef, NodeDefHash
62// will return the same value for both of them when called with the same
63// `options` that was used in the call to EqualNodeDef.
64// Similarly to protobuf deterministic serialization, hash value is
65// guaranteed to be stable only for a given binary. In particular, one should
66// probably not persist the returned value.
67uint64 NodeDefHash(const NodeDef& ndef,
68 const EqualGraphDefOptions& options = {});
69
70// Determines if actual and expected are equal, ignoring ordering. If they're
71// different and diff != nullptr, *diff is set to an explanation of the
72// difference.
73bool EqualRepeatedNodeDef(const protobuf::RepeatedPtrField<NodeDef>& actual,
74 const protobuf::RepeatedPtrField<NodeDef>& expected,
75 string* diff,
76 const EqualGraphDefOptions& options = {});
77
78// Returns a hash of `ndefs` that is consistent with EqualRepeatedNodeDef.
79// In other words, if two ndefs compare equal according to
80// EqualRepeatedNodeDef, RepeatedNodeDefHash will return the same value for
81// both of them when called with the same `options` that was used in
82// the call to EqualRepeatedNodeDef.
83// Similarly to protobuf deterministic serialization, hash value is
84// guaranteed to be stable only for a given binary. In particular, one should
85// probably not persist the returned value.
86uint64 RepeatedNodeDefHash(const protobuf::RepeatedPtrField<NodeDef>& ndefs,
87 const EqualGraphDefOptions& options = {});
88
89#define TF_EXPECT_GRAPH_EQ(expected, actual) \
90 do { \
91 string diff; \
92 EXPECT_TRUE(EqualGraphDef(actual, expected, &diff)) \
93 << diff << "\nExpected:\n" \
94 << SummarizeGraphDef(expected) << "\nActual:\n" \
95 << SummarizeGraphDef(actual); \
96 } while (false)
97
98} // namespace tensorflow
99
100#endif // TENSORFLOW_CORE_UTIL_EQUAL_GRAPH_DEF_H_
101