Home | History | Annotate | Download | only in benchmark
      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 
     29 TEST(BenchmarkModelTest, InitializeAndRun) {
     30   const string dir = testing::TmpDir();
     31   const string filename_pb = io::JoinPath(dir, "graphdef.pb");
     32 
     33   // Create a simple graph and write it to filename_pb.
     34   const int input_width = 400;
     35   const int input_height = 10;
     36   benchmark_model::InputLayerInfo input;
     37   input.shape = TensorShape({input_width, input_height});
     38   input.data_type = DT_FLOAT;
     39   const TensorShape constant_shape({input_height, input_width});
     40 
     41   Tensor constant_tensor(DT_FLOAT, constant_shape);
     42   test::FillFn<float>(&constant_tensor, [](int) -> float { return 3.0; });
     43 
     44   auto root = Scope::NewRootScope().ExitOnError();
     45   auto placeholder =
     46       ops::Placeholder(root, DT_FLOAT, ops::Placeholder::Shape(input.shape));
     47   input.name = placeholder.node()->name();
     48   auto m = ops::MatMul(root, placeholder, constant_tensor);
     49   const string output_name = m.node()->name();
     50 
     51   GraphDef graph_def;
     52   TF_ASSERT_OK(root.ToGraphDef(&graph_def));
     53   string graph_def_serialized;
     54   graph_def.SerializeToString(&graph_def_serialized);
     55   TF_ASSERT_OK(
     56       WriteStringToFile(Env::Default(), filename_pb, graph_def_serialized));
     57 
     58   std::unique_ptr<Session> session;
     59   std::unique_ptr<GraphDef> loaded_graph_def;
     60   TF_ASSERT_OK(benchmark_model::InitializeSession(1, filename_pb, &session,
     61                                                   &loaded_graph_def));
     62   std::unique_ptr<StatSummarizer> stats;
     63   stats.reset(new tensorflow::StatSummarizer(*(loaded_graph_def.get())));
     64   int64 time;
     65   int64 num_runs = 0;
     66   TF_ASSERT_OK(benchmark_model::TimeMultipleRuns(
     67       0.0, 10, 0.0, {input}, {output_name}, session.get(), stats.get(), &time,
     68       &num_runs));
     69   ASSERT_EQ(num_runs, 10);
     70 }
     71 
     72 }  // namespace
     73 }  // namespace tensorflow
     74