aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/grappler/optimizers/loop_optimizer_test.cc
blob: 3d54aa7a7922bfcb474c9efb7207e1548e204ddb (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
/* 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/core/grappler/optimizers/loop_optimizer.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/core/framework/node_def.pb.h"
#include "tensorflow/core/grappler/grappler_item.h"
#include "tensorflow/core/grappler/inputs/trivial_test_graph_input_yielder.h"
#include "tensorflow/core/grappler/utils.h"
#include "tensorflow/core/lib/core/status_test_util.h"
#include "tensorflow/core/platform/test.h"

namespace tensorflow {
namespace grappler {
namespace {

class LoopOptimizerTest : public ::testing::Test {};

void VerifyGraphsEqual(const GraphDef& original_graph,
                       const GraphDef& optimized_graph, const string& func) {
  EXPECT_EQ(original_graph.node_size(), optimized_graph.node_size()) << func;
  for (int i = 0; i < original_graph.node_size(); ++i) {
    const NodeDef& original = original_graph.node(i);
    const NodeDef& optimized = optimized_graph.node(i);
    EXPECT_EQ(original.name(), optimized.name()) << func;
    EXPECT_EQ(original.op(), optimized.op()) << func;
    EXPECT_EQ(original.input_size(), optimized.input_size()) << func;
    for (int j = 0; j < original.input_size(); ++j) {
      EXPECT_EQ(original.input(j), optimized.input(j)) << func;
    }
  }
}

TEST_F(LoopOptimizerTest, NoOp) {
  // This trivial graph is so basic there's nothing to optimize.
  TrivialTestGraphInputYielder fake_input(4, 1, 10, false, {"CPU:0"});
  GrapplerItem item;
  CHECK(fake_input.NextItem(&item));

  LoopOptimizer optimizer;
  GraphDef output;
  Status status = optimizer.Optimize(nullptr, item, &output);
  TF_EXPECT_OK(status);

  VerifyGraphsEqual(item.graph, output, __FUNCTION__);
}

namespace {
NodeDef* AddNode(const string& name, const string& op,
                 const std::vector<string>& inputs,
                 const std::vector<std::pair<string, AttrValue>>& attributes,
                 GraphDef* graph) {
  NodeDef* node = graph->add_node();
  node->set_name(name);
  node->set_op(op);
  for (const string& input : inputs) {
    node->add_input(input);
  }
  for (auto attr : attributes) {
    (*node->mutable_attr())[attr.first] = attr.second;
  }
  return node;
}
}  // namespace

TEST_F(LoopOptimizerTest, RemovePush_NoOp) {
  GrapplerItem item;
  AttrValue frame_name;
  frame_name.set_s("foo");
  AttrValue type;
  type.set_type(DT_RESOURCE);
  GraphDef& graph = item.graph;
  AddNode("c", "Const", {}, {}, &graph);
  // Stack with corresponding push/pop.
  AddNode("stack1", "StackV2", {}, {}, &graph);
  AddNode("push1", "StackPushV2", {"stack1", "c"}, {}, &graph);
  AddNode("pop1", "StackPopV2", {"stack1"}, {}, &graph);
  AddNode("id1", "Identity", {"pop1"}, {}, &graph);
  // Stack with corresponding push/pop behind Enter.
  AddNode("stack2", "StackV2", {}, {}, &graph);
  AddNode("push_enter", "Enter", {"stack2"},
          {{"T", type}, {"frame_name", frame_name}}, &graph);
  AddNode("push2", "StackPushV2", {"push_enter", "c"}, {}, &graph);
  AddNode("pop_enter", "Enter", {"stack2"},
          {{"T", type}, {"frame_name", frame_name}}, &graph);
  AddNode("pop2", "StackPopV2", {"pop_enter"}, {}, &graph);
  AddNode("id2", "Identity", {"pop2"}, {}, &graph);
  // Stack with unexpected op type in fanout of Stack.
  AddNode("stack3", "StackV2", {}, {}, &graph);
  AddNode("push3", "StackPushV2", {"stack3", "c"}, {}, &graph);
  AddNode("stop", "StopGradient", {"stack3"}, {}, &graph);
  LoopOptimizer optimizer;
  GraphDef output;
  Status status = optimizer.Optimize(nullptr, item, &output);
  TF_EXPECT_OK(status);
  VerifyGraphsEqual(item.graph, output, __FUNCTION__);
}

TEST_F(LoopOptimizerTest, RemovePushWithoutMatchingPop) {
  GrapplerItem item;
  GraphDef& graph = item.graph;
  AttrValue frame_name;
  frame_name.set_s("foo");
  AttrValue type;
  type.set_type(DT_RESOURCE);
  AddNode("c", "Const", {}, {}, &graph);
  // Push without Pop.
  AddNode("stack1", "StackV2", {}, {}, &graph);
  AddNode("push1", "StackPushV2", {"stack1", "c"}, {}, &graph);
  // Push without Pop behind Enter.
  AddNode("stack2", "StackV2", {}, {}, &graph);
  AddNode("push_enter", "Enter", {"stack2"},
          {{"T", type}, {"frame_name", frame_name}}, &graph);
  AddNode("push2", "StackPushV2", {"push_enter", "c"}, {}, &graph);
  // Pop without consumer.
  AddNode("stack3", "StackV2", {}, {}, &graph);
  AddNode("push3", "StackPushV2", {"stack3", "c"}, {}, &graph);
  AddNode("pop3", "StackPopV2", {"stack3"}, {}, &graph);

  LoopOptimizer optimizer;
  GraphDef output;
  Status status = optimizer.Optimize(nullptr, item, &output);
  TF_EXPECT_OK(status);
  EXPECT_EQ(9, output.node_size());
  for (int i = 0; i < output.node_size(); ++i) {
    const NodeDef& node = output.node(i);
    if (node.name() == "push1") {
      EXPECT_EQ("Identity", node.op());
      EXPECT_EQ(2, node.input_size());
      EXPECT_EQ("c", node.input(0));
      EXPECT_EQ("^stack1", node.input(1));
    } else if (node.name() == "push2") {
      EXPECT_EQ("Identity", node.op());
      EXPECT_EQ(2, node.input_size());
      EXPECT_EQ("c", node.input(0));
      EXPECT_EQ("^push_enter", node.input(1));
    } else if (node.name() == "push3") {
      EXPECT_EQ("Identity", node.op());
      EXPECT_EQ(2, node.input_size());
      EXPECT_EQ("c", node.input(0));
      EXPECT_EQ("^stack3", node.input(1));
    } else {
      const NodeDef& orig_node = item.graph.node(i);
      EXPECT_EQ(orig_node.ShortDebugString(), node.ShortDebugString());
    }
  }
}

}  // namespace
}  // namespace grappler
}  // namespace tensorflow