1 /* Copyright 2016 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 #include "tensorflow/cc/framework/scope.h"
17
18 #include "tensorflow/cc/ops/array_ops.h"
19 #include "tensorflow/core/platform/test.h"
20
21 namespace tensorflow {
22
TEST(ScopeTest,BasicNames)23 TEST(ScopeTest, BasicNames) {
24 Scope root = Scope::NewRootScope();
25 EXPECT_EQ(root.GetUniqueNameForOp("add"), "add");
26 EXPECT_EQ(root.GetUniqueNameForOp("add"), "add_1");
27 EXPECT_EQ(root.GetUniqueNameForOp("add"), "add_2");
28 EXPECT_EQ(root.GetUniqueNameForOp("mul"), "mul");
29 }
30
TEST(ScopeTest,OpAndScopeNameCollision)31 TEST(ScopeTest, OpAndScopeNameCollision) {
32 Scope root = Scope::NewRootScope();
33 EXPECT_EQ(root.GetUniqueNameForOp("foo"), "foo");
34 EXPECT_EQ(root.GetUniqueNameForOp("foo"), "foo_1");
35 EXPECT_EQ(root.GetUniqueNameForOp("foo_1"), "foo_1_1");
36 EXPECT_EQ(root.GetUniqueNameForOp("foo_2"), "foo_2");
37 EXPECT_EQ(root.GetUniqueNameForOp("foo"), "foo_3");
38 EXPECT_EQ(root.GetUniqueNameForOp("foo_2"), "foo_2_1");
39 }
40
TEST(ScopeTest,HierarchicalNames)41 TEST(ScopeTest, HierarchicalNames) {
42 Scope root = Scope::NewRootScope();
43 Scope child = root.NewSubScope("child");
44 EXPECT_EQ(child.GetUniqueNameForOp("add"), "child/add");
45 EXPECT_EQ(child.GetUniqueNameForOp("add"), "child/add_1");
46 EXPECT_EQ(child.GetUniqueNameForOp("mul"), "child/mul");
47
48 Scope child_1 = root.NewSubScope("child");
49 EXPECT_EQ(child_1.GetUniqueNameForOp("add"), "child_1/add");
50 EXPECT_EQ(child_1.GetUniqueNameForOp("add"), "child_1/add_1");
51 EXPECT_EQ(child_1.GetUniqueNameForOp("mul"), "child_1/mul");
52
53 Scope c_c = root.NewSubScope("c").NewSubScope("c");
54 EXPECT_EQ(c_c.GetUniqueNameForOp("add"), "c/c/add");
55
56 Scope c_1 = root.NewSubScope("c");
57 Scope c_1_c = c_1.NewSubScope("c");
58 EXPECT_EQ(c_1_c.GetUniqueNameForOp("add"), "c_1/c/add");
59
60 Scope c_1_c_1 = c_1.NewSubScope("c");
61 EXPECT_EQ(c_1_c_1.GetUniqueNameForOp("add"), "c_1/c_1/add");
62
63 EXPECT_EQ(root.NewSubScope("").NewSubScope("").GetUniqueNameForOp("d"), "d");
64 EXPECT_EQ(root.NewSubScope("").GetUniqueNameForOp("d"), "d_1");
65 EXPECT_EQ(root.GetUniqueNameForOp("d"), "d_2");
66 }
67
TEST(ScopeTest,ScopeAndOpNames)68 TEST(ScopeTest, ScopeAndOpNames) {
69 Scope root = Scope::NewRootScope();
70 Scope child = root.NewSubScope("child");
71
72 EXPECT_EQ(child.GetUniqueNameForOp("add"), "child/add");
73 EXPECT_EQ(root.GetUniqueNameForOp("child"), "child_1");
74
75 EXPECT_EQ(root.NewSubScope("child").GetUniqueNameForOp("p"), "child_2/p");
76 }
77
78 namespace {
79
LastOp(const Scope & scope)80 string LastOp(const Scope& scope) { return scope.GetUniqueNameForOp("Last"); }
81
AnotherCompositeOp(const Scope & scope)82 std::vector<string> AnotherCompositeOp(const Scope& scope) {
83 auto cop_scopes = scope.GetCompositeOpScopes("another_cop");
84 const string c1 = cop_scopes.child.GetUniqueNameForOp("c1");
85 const string c2 = cop_scopes.child.GetUniqueNameForOp("mul");
86 return {c1, c2, LastOp(cop_scopes.last)};
87 }
88
LinearOp(const Scope & scope)89 std::vector<string> LinearOp(const Scope& scope) {
90 auto cop_scopes = scope.GetCompositeOpScopes("linear");
91 Scope linear = cop_scopes.child;
92 const string mul_op_name = linear.GetUniqueNameForOp("mul");
93 const string bias_add_op_name = linear.GetUniqueNameForOp("bias_add");
94 auto cop_names = AnotherCompositeOp(cop_scopes.last);
95 return {mul_op_name, bias_add_op_name, cop_names[0], cop_names[1],
96 cop_names[2]};
97 }
98
99 } // namespace
100
TEST(ScopeTest,CompositeOp)101 TEST(ScopeTest, CompositeOp) {
102 Scope root = Scope::NewRootScope();
103 const auto names1 = LinearOp(root);
104
105 EXPECT_EQ(names1[0], "linear/mul");
106 EXPECT_EQ(names1[1], "linear/bias_add");
107 EXPECT_EQ(names1[2], "linear/c1");
108 EXPECT_EQ(names1[3], "linear/mul_1");
109 EXPECT_EQ(names1[4], "linear");
110
111 EXPECT_EQ(root.GetUniqueNameForOp("linear"), "linear_1");
112
113 const auto names2 = LinearOp(root);
114
115 EXPECT_EQ(names2[0], "linear_2/mul");
116 EXPECT_EQ(names2[1], "linear_2/bias_add");
117 EXPECT_EQ(names2[2], "linear_2/c1");
118 EXPECT_EQ(names2[3], "linear_2/mul_1");
119 EXPECT_EQ(names2[4], "linear_2");
120
121 const auto names3 = LinearOp(root.WithOpName("c"));
122
123 EXPECT_EQ(names3[0], "c/mul");
124 EXPECT_EQ(names3[1], "c/bias_add");
125 EXPECT_EQ(names3[2], "c/c1");
126 EXPECT_EQ(names3[3], "c/mul_1");
127 EXPECT_EQ(names3[4], "c");
128 }
129
TEST(ScopeTest,SingleUseScope)130 TEST(ScopeTest, SingleUseScope) {
131 Scope root = Scope::NewRootScope();
132 auto cop_scopes = root.GetCompositeOpScopes("cop");
133 // cop_scopes.last is a single use scope
134 EXPECT_EQ(cop_scopes.last.GetUniqueNameForOp("foo"), "cop");
135 cop_scopes.last.GetUniqueNameForOp("foo");
136 // Error status should be set on cop_scopes.last
137 EXPECT_FALSE(cop_scopes.last.ok());
138 }
139
TEST(ScopeTest,ControlDeps)140 TEST(ScopeTest, ControlDeps) {
141 Scope root = Scope::NewRootScope();
142 auto c1 = Operation();
143 auto c2 = Operation();
144 Scope c = root.WithControlDependencies({c1, c2});
145 EXPECT_EQ(c.control_deps().size(), 2);
146 Scope c_c = c.WithControlDependencies({Operation()});
147 EXPECT_EQ(c_c.control_deps().size(), 3);
148 }
149
TEST(ScopeTest,CreateOutput)150 TEST(ScopeTest, CreateOutput) {
151 Scope root = Scope::NewRootScope();
152 Output a = ops::Placeholder(root.WithOpName("a"), DT_FLOAT);
153 Output add;
154 ASSERT_TRUE(
155 CreateOutputWithScope("Add", {a, a}, root.WithOpName("add"), &add).ok());
156 EXPECT_EQ(add.node()->name(), "add");
157 EXPECT_EQ(add.node()->type_string(), "Add");
158 }
159
160 } // namespace tensorflow
161