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