• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2020 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/core/graph/costmodel.h"
17 
18 #include <memory>
19 #include <string>
20 
21 #include "tensorflow/cc/framework/scope.h"
22 #include "tensorflow/core/common_runtime/costmodel_manager.h"
23 #include "tensorflow/core/common_runtime/graph_constructor.h"
24 #include "tensorflow/core/common_runtime/step_stats_collector.h"
25 #include "tensorflow/core/framework/cost_graph.pb.h"
26 #include "tensorflow/core/framework/step_stats.pb.h"
27 #include "tensorflow/core/graph/graph.h"
28 #include "tensorflow/core/lib/core/status_test_util.h"
29 #include "tensorflow/core/platform/protobuf.h"
30 #include "tensorflow/core/platform/test.h"
31 #include "tensorflow/core/util/dump_graph.h"
32 
33 namespace tensorflow {
34 namespace {
35 
InitGraph(const string & s,Graph * graph)36 static void InitGraph(const string& s, Graph* graph) {
37   GraphDef graph_def;
38 
39   auto parser = protobuf::TextFormat::Parser();
40   CHECK(parser.MergeFromString(s, &graph_def)) << s;
41   GraphConstructorOptions opts;
42   TF_CHECK_OK(ConvertGraphDefToGraph(opts, graph_def, graph));
43 }
44 
GenerateStepStats(Graph * graph,StepStats * step_stats,const string & device_name)45 static void GenerateStepStats(Graph* graph, StepStats* step_stats,
46                               const string& device_name) {
47   // Fill RunMetadata's step_stats and partition_graphs fields.
48   DeviceStepStats* device_stepstats = step_stats->add_dev_stats();
49   device_stepstats->set_device(device_name);
50   for (const auto& node_def : graph->nodes()) {
51     NodeExecStats* node_stats = device_stepstats->add_node_stats();
52     node_stats->set_node_name(node_def->name());
53   }
54 }
55 
56 REGISTER_OP("Input").Output("o: float").SetIsStateful();
57 
TEST(CostModelTest,GlobalId)58 TEST(CostModelTest, GlobalId) {
59   Scope scope = Scope::NewRootScope().ExitOnError();
60   std::unique_ptr<Graph> graph1 =
61       std::unique_ptr<Graph>(new Graph(OpRegistry::Global()));
62   std::unique_ptr<Graph> graph2 =
63       std::unique_ptr<Graph>(new Graph(OpRegistry::Global()));
64   InitGraph(
65       "node { name: 'A1' op: 'Input'}"
66       "node { name: 'B1' op: 'Input'}"
67       "node { name: 'C1' op: 'Mul' attr { key: 'T' value { type: DT_FLOAT } }"
68       " input: ['A1', 'B1'] }"
69       "node { name: 'D1' op: 'Mul' attr { key: 'T' value { type: DT_FLOAT } }"
70       " input: ['A1', 'B1'] }",
71       graph1.get());
72   InitGraph(
73       "node { name: 'A2' op: 'Input'}"
74       "node { name: 'B2' op: 'Input'}"
75       "node { name: 'C2' op: 'Mul' attr { key: 'T' value { type: DT_FLOAT } }"
76       " input: ['A2', 'B2'] }"
77       "node { name: 'D2' op: 'Mul' attr { key: 'T' value { type: DT_FLOAT } }"
78       " input: ['A2', 'B2'] }",
79       graph2.get());
80   StepStats step_stats;
81   GenerateStepStats(graph1.get(), &step_stats, "DummyDevice1");
82   GenerateStepStats(graph2.get(), &step_stats, "DummyDevice2");
83   StepStatsCollector collector(&step_stats);
84   std::unordered_map<string, const Graph*> device_map;
85   device_map["DummyDevice1"] = graph1.get();
86   device_map["DummyDevice2"] = graph2.get();
87   CostModelManager cost_model_manager;
88   collector.BuildCostModel(&cost_model_manager, device_map);
89   CostGraphDef cost_graph_def;
90   TF_ASSERT_OK(
91       cost_model_manager.AddToCostGraphDef(graph1.get(), &cost_graph_def));
92   TF_ASSERT_OK(
93       cost_model_manager.AddToCostGraphDef(graph2.get(), &cost_graph_def));
94   ASSERT_EQ(cost_graph_def.node_size(), 12);
95   absl::flat_hash_map<int32, const CostGraphDef::Node> ids;
96   for (auto node : cost_graph_def.node()) {
97     int32_t index = node.id();
98     auto result = ids.insert({index, node});
99     EXPECT_TRUE(result.second);
100   }
101 }
102 
103 }  // namespace
104 }  // namespace tensorflow
105