Home | History | Annotate | Download | only in util
      1 /* Copyright 2017 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/util/stat_summarizer.h"
     17 
     18 #include "tensorflow/core/framework/graph.pb.h"
     19 #include "tensorflow/core/framework/step_stats.pb.h"
     20 #include "tensorflow/core/lib/core/status.h"
     21 #include "tensorflow/core/lib/core/status_test_util.h"
     22 #include "tensorflow/core/platform/protobuf.h"
     23 #include "tensorflow/core/platform/test.h"
     24 #include "tensorflow/core/public/session.h"
     25 #include "tensorflow/core/public/session_options.h"
     26 
     27 namespace tensorflow {
     28 namespace {
     29 
     30 TEST(StatSummarizerTest, ExtractsOpTypes) {
     31   // GraphDef for a single constant name 'myconstant'
     32   const std::string graph_def_str(R"EOF(
     33 node {
     34   name: "myconstant"
     35   op: "Const"
     36   attr {
     37     key: "dtype"
     38     value {
     39       type: DT_FLOAT
     40     }
     41   }
     42   attr {
     43     key: "value"
     44     value {
     45       tensor {
     46         dtype: DT_FLOAT
     47         tensor_shape {
     48         }
     49         float_val: 1.0
     50       }
     51     }
     52   }
     53 }
     54 versions {
     55   producer: 21
     56 }
     57   )EOF");
     58   GraphDef graph_def;
     59   ASSERT_TRUE(protobuf::TextFormat::ParseFromString(graph_def_str, &graph_def));
     60 
     61   std::unique_ptr<Session> session(NewSession(SessionOptions()));
     62   ASSERT_TRUE(session != nullptr);
     63   TF_ASSERT_OK(session->Create(graph_def));
     64 
     65   RunOptions run_options;
     66   run_options.set_trace_level(RunOptions::FULL_TRACE);
     67 
     68   RunMetadata run_metadata;
     69   std::vector<Tensor> outputs;
     70   TF_ASSERT_OK(session->Run(run_options, {}, {"myconstant:0"}, {}, &outputs,
     71                             &run_metadata));
     72 
     73   StatSummarizerOptions opts;
     74   StatSummarizer stats(graph_def);
     75   stats.ProcessStepStats(run_metadata.step_stats());
     76 
     77   const std::string output = stats.GetOutputString();
     78   const std::string by_node_type = stats.GetStatsByNodeType();
     79 
     80   // output should contain both the node type and node name.
     81   ASSERT_TRUE(output.find("Const") != std::string::npos) << output;
     82   ASSERT_TRUE(output.find("myconstant") != std::string::npos) << output;
     83   // stats by node type should include the type.
     84   ASSERT_TRUE(by_node_type.find("Const") != std::string::npos) << by_node_type;
     85 }
     86 
     87 }  // namespace
     88 }  // namespace tensorflow
     89