1 /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15
16 // Tests for the backward const analysis.
17
18 #include "tensorflow/compiler/tf2xla/functionalize_cond.h"
19
20 #include "absl/container/flat_hash_set.h"
21 #include "absl/strings/string_view.h"
22 #include "tensorflow/cc/framework/ops.h"
23 #include "tensorflow/cc/framework/scope.h"
24 #include "tensorflow/cc/ops/array_ops.h"
25 #include "tensorflow/cc/ops/const_op.h"
26 #include "tensorflow/cc/ops/control_flow_ops.h"
27 #include "tensorflow/cc/ops/function_ops.h"
28 #include "tensorflow/cc/ops/standard_ops.h"
29 #include "tensorflow/core/framework/types.pb.h"
30 #include "tensorflow/core/graph/testlib.h"
31 #include "tensorflow/core/lib/core/status_test_util.h"
32 #include "tensorflow/core/platform/test.h"
33
34 namespace tensorflow {
35 namespace functionalize_cond {
36
37 class FunctionalizeCondTest : public ::testing::Test {
38 protected:
FunctionalizeCondTest()39 FunctionalizeCondTest() {
40 graph_.reset(new Graph(OpRegistry::Global()));
41 flib_def_.reset(
42 new FunctionLibraryDefinition(OpRegistry::Global(), fdef_lib_));
43 fc_.reset(new functionalize_cond::FunctionalizeCond(
44 graph_.get(), flib_def_.get(), NodeFilter{}));
45 }
46
GetUniqueId(const StateMap::StateMap::CondState & state)47 StateMap::CondId GetUniqueId(const StateMap::StateMap::CondState& state) {
48 return fc_->state_map_.GetCondId(state);
49 }
50
GetString(const StateMap::StateMap::CondId id)51 string GetString(const StateMap::StateMap::CondId id) {
52 return fc_->state_map_.CondStateToString(id);
53 }
54
JoinCondStatesNonMerge(StateMap::CondId src,StateMap::CondId dst)55 StatusOr<StateMap::CondId> JoinCondStatesNonMerge(StateMap::CondId src,
56 StateMap::CondId dst) {
57 return fc_->JoinCondStatesNonMerge(src, dst);
58 }
59
JoinCondStatesMerge(Node * n,StateMap::CondId src,StateMap::CondId dst)60 StatusOr<StateMap::CondId> JoinCondStatesMerge(Node* n, StateMap::CondId src,
61 StateMap::CondId dst) {
62 return fc_->JoinCondStatesMerge(n, src, dst);
63 }
64
65 FunctionDefLibrary fdef_lib_;
66 std::unique_ptr<functionalize_cond::FunctionalizeCond> fc_;
67 std::unique_ptr<FunctionLibraryDefinition> flib_def_;
68 std::unique_ptr<Graph> graph_;
69 };
70
71 namespace {
72
TEST_F(FunctionalizeCondTest,JoinCondStates)73 TEST_F(FunctionalizeCondTest, JoinCondStates) {
74 Tensor pred_tensor(DT_BOOL, TensorShape());
75 pred_tensor.flat<bool>().setZero();
76 Node* pred = test::graph::Constant(graph_.get(), pred_tensor, "pred");
77 Tensor val_tensor(DT_INT32, TensorShape());
78 val_tensor.flat<int>().setZero();
79 Node* val = test::graph::Constant(graph_.get(), val_tensor, "val");
80 Node* m = test::graph::Merge(graph_.get(), val, val);
81
82 StateMap::CondId then_branch;
83 {
84 StateMap::CondState ss;
85 ss.insert(std::make_pair(OutputTensor(pred, 0), BranchType::kThenBranch));
86 then_branch = GetUniqueId(ss);
87 }
88 StateMap::CondId else_branch;
89 {
90 StateMap::CondState ss;
91 ss.insert(std::make_pair(OutputTensor(pred, 0), BranchType::kElseBranch));
92 else_branch = GetUniqueId(ss);
93 }
94
95 // An non-merge op with inputs from then and else branch.
96 Status status = JoinCondStatesNonMerge(then_branch, else_branch).status();
97 EXPECT_TRUE(errors::IsInvalidArgument(status));
98
99 // Merge between then and else branch.
100 auto joined_or = JoinCondStatesMerge(m, then_branch, else_branch);
101 TF_EXPECT_OK(joined_or.status());
102 StateMap::CondId joined = joined_or.ValueOrDie();
103
104 // Merge between then branch and both branch.
105 auto t = JoinCondStatesNonMerge(then_branch, joined);
106 // Note: this is OK in terms of constraint predication, but
107 TF_EXPECT_OK(t.status());
108 }
109
TEST_F(FunctionalizeCondTest,JoinCondStatesMergeWithInputNotInCondContext)110 TEST_F(FunctionalizeCondTest, JoinCondStatesMergeWithInputNotInCondContext) {
111 Tensor val_tensor(DT_INT32, TensorShape());
112 val_tensor.flat<int>().setZero();
113 Node* val = test::graph::Constant(graph_.get(), val_tensor, "val");
114 Node* m = test::graph::Merge(graph_.get(), val, val);
115
116 StateMap::CondState cond_state;
117 auto joined_or = JoinCondStatesMerge(m, /*src=*/nullptr, &cond_state);
118 EXPECT_FALSE(joined_or.ok());
119 }
120
TEST(FunctionalizeCond,DuplicateConstNodes)121 TEST(FunctionalizeCond, DuplicateConstNodes) {
122 Scope root = Scope::NewRootScope().ExitOnError();
123 auto const_op = ops::Const(root.WithOpName("const"), 1);
124 auto arg_0_op = ops::_Arg(root.WithOpName("arg_0"), DT_BOOL, 0);
125 auto arg_1_op = ops::_Arg(root.WithOpName("arg_1"), DT_INT32, 1);
126 auto switch_op = ops::Switch(root.WithOpName("switch"), arg_1_op, arg_0_op);
127 auto identity_n_false_op =
128 ops::IdentityN(root.WithOpName("identity_n_0"),
129 {switch_op.output_false, const_op, const_op});
130 auto identity_n_true_op =
131 ops::IdentityN(root.WithOpName("identity_n_1"),
132 {switch_op.output_true, const_op, const_op});
133 auto merge_op = ops::Merge(
134 root.WithOpName("merge"),
135 {identity_n_false_op.output.front(), identity_n_true_op.output.front()});
136 GraphDef graph_def;
137 TF_ASSERT_OK(root.ToGraphDef(&graph_def));
138
139 Graph graph(OpRegistry::Global());
140 GraphConstructorOptions options;
141 TF_EXPECT_OK(ConvertGraphDefToGraph(options, graph_def, &graph));
142
143 FunctionDefLibrary fdef_lib;
144 FunctionLibraryDefinition flib_def(OpRegistry::Global(), fdef_lib);
145
146 auto status = tensorflow::FunctionalizeCond(&graph, &flib_def);
147 TF_ASSERT_OK(status);
148
149 FunctionDefLibrary flib_def_proto = flib_def.ToProto();
150 for (const auto& fdef : flib_def_proto.function()) {
151 absl::flat_hash_set<absl::string_view> node_names;
152 for (const auto& node : fdef.node_def()) {
153 EXPECT_TRUE(node_names.insert(node.name()).second)
154 << node.op() << " with duplicate node name '" << node.name()
155 << "' found.";
156 }
157 }
158 }
159
160 } // namespace
161 } // namespace functionalize_cond
162 } // namespace tensorflow
163