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/tools/benchmark/benchmark_model.h"
17
18 #include "tensorflow/cc/ops/standard_ops.h"
19 #include "tensorflow/core/framework/tensor_testutil.h"
20 #include "tensorflow/core/graph/graph_def_builder.h"
21 #include "tensorflow/core/lib/core/status_test_util.h"
22 #include "tensorflow/core/lib/io/path.h"
23 #include "tensorflow/core/platform/test.h"
24 #include "tensorflow/core/platform/test_benchmark.h"
25
26 namespace tensorflow {
27 namespace {
28
CreateTestGraph(const::tensorflow::Scope & root,benchmark_model::InputLayerInfo * input,string * output_name,GraphDef * graph_def)29 void CreateTestGraph(const ::tensorflow::Scope& root,
30 benchmark_model::InputLayerInfo* input,
31 string* output_name, GraphDef* graph_def) {
32 // Create a simple graph and write it to filename_pb.
33 const int input_width = 400;
34 const int input_height = 10;
35 input->shape = TensorShape({input_width, input_height});
36 input->data_type = DT_FLOAT;
37 const TensorShape constant_shape({input_height, input_width});
38
39 Tensor constant_tensor(DT_FLOAT, constant_shape);
40 test::FillFn<float>(&constant_tensor, [](int) -> float { return 3.0; });
41
42 auto placeholder =
43 ops::Placeholder(root, DT_FLOAT, ops::Placeholder::Shape(input->shape));
44 input->name = placeholder.node()->name();
45 auto m = ops::MatMul(root, placeholder, constant_tensor);
46 *output_name = m.node()->name();
47 TF_ASSERT_OK(root.ToGraphDef(graph_def));
48 }
49
TEST(BenchmarkModelTest,InitializeAndRun)50 TEST(BenchmarkModelTest, InitializeAndRun) {
51 const string dir = testing::TmpDir();
52 const string filename_pb = io::JoinPath(dir, "graphdef.pb");
53 auto root = Scope::NewRootScope().ExitOnError();
54
55 benchmark_model::InputLayerInfo input;
56 string output_name;
57 GraphDef graph_def;
58 CreateTestGraph(root, &input, &output_name, &graph_def);
59 string graph_def_serialized;
60 graph_def.SerializeToString(&graph_def_serialized);
61 TF_ASSERT_OK(
62 WriteStringToFile(Env::Default(), filename_pb, graph_def_serialized));
63
64 std::unique_ptr<Session> session;
65 std::unique_ptr<GraphDef> loaded_graph_def;
66 TF_ASSERT_OK(benchmark_model::InitializeSession(1, filename_pb, &session,
67 &loaded_graph_def));
68 std::unique_ptr<StatSummarizer> stats;
69 stats.reset(new tensorflow::StatSummarizer(*(loaded_graph_def.get())));
70 int64 time;
71 int64 num_runs = 0;
72 TF_ASSERT_OK(benchmark_model::TimeMultipleRuns(
73 0.0, 10, 0.0, {input}, {output_name}, {}, session.get(), stats.get(),
74 &time, &num_runs));
75 ASSERT_EQ(num_runs, 10);
76 }
77
TEST(BenchmarkModeTest,TextProto)78 TEST(BenchmarkModeTest, TextProto) {
79 const string dir = testing::TmpDir();
80 const string filename_txt = io::JoinPath(dir, "graphdef.pb.txt");
81 auto root = Scope::NewRootScope().ExitOnError();
82
83 benchmark_model::InputLayerInfo input;
84 string output_name;
85 GraphDef graph_def;
86 CreateTestGraph(root, &input, &output_name, &graph_def);
87 TF_ASSERT_OK(WriteTextProto(Env::Default(), filename_txt, graph_def));
88
89 std::unique_ptr<Session> session;
90 std::unique_ptr<GraphDef> loaded_graph_def;
91 TF_ASSERT_OK(benchmark_model::InitializeSession(1, filename_txt, &session,
92 &loaded_graph_def));
93 std::unique_ptr<StatSummarizer> stats;
94 stats.reset(new tensorflow::StatSummarizer(*(loaded_graph_def.get())));
95 int64 time;
96 int64 num_runs = 0;
97 TF_ASSERT_OK(benchmark_model::TimeMultipleRuns(
98 0.0, 10, 0.0, {input}, {output_name}, {}, session.get(), stats.get(),
99 &time, &num_runs));
100 ASSERT_EQ(num_runs, 10);
101 }
102
103 } // namespace
104 } // namespace tensorflow
105