tfprof_show_test.cc revision 9e9ffa33d154b6c332bd475d6da2030746460fed
1/* Copyright 2016 The TensorFlow Authors All Rights Reserved. 2 3Licensed under the Apache License, Version 2.0 (the "License"); 4you may not use this file except in compliance with the License. 5You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9Unless required by applicable law or agreed to in writing, software 10distributed under the License is distributed on an "AS IS" BASIS, 11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12See the License for the specific language governing permissions and 13limitations under the License. 14==============================================================================*/ 15 16#include "tensorflow/core/profiler/internal/tfprof_stats.h" 17 18#include <utility> 19 20#include "tensorflow/c/checkpoint_reader.h" 21#include "tensorflow/core/framework/graph.pb.h" 22#include "tensorflow/core/lib/io/path.h" 23#include "tensorflow/core/platform/env.h" 24#include "tensorflow/core/platform/test.h" 25#include "tensorflow/core/profiler/internal/tfprof_constants.h" 26#include "tensorflow/core/profiler/internal/tfprof_options.h" 27#include "tensorflow/core/profiler/internal/tfprof_utils.h" 28#include "tensorflow/core/profiler/tfprof_log.pb.h" 29#include "tensorflow/core/profiler/tfprof_output.pb.h" 30#include "tensorflow/core/protobuf/config.pb.h" 31 32namespace tensorflow { 33namespace tfprof { 34class TFProfShowTest : public ::testing::Test { 35 protected: 36 TFProfShowTest() { 37 string graph_path = 38 io::JoinPath(testing::TensorFlowSrcRoot(), 39 "core/profiler/internal/testdata/graph.pbtxt"); 40 std::unique_ptr<tensorflow::GraphDef> graph_pb(new tensorflow::GraphDef()); 41 TF_CHECK_OK( 42 ReadProtoFile(Env::Default(), graph_path, graph_pb.get(), false)); 43 44 std::unique_ptr<tensorflow::RunMetadata> run_meta_pb( 45 new tensorflow::RunMetadata()); 46 string run_meta_path = 47 io::JoinPath(testing::TensorFlowSrcRoot(), 48 "core/profiler/internal/testdata/run_meta"); 49 TF_CHECK_OK( 50 ReadProtoFile(Env::Default(), run_meta_path, run_meta_pb.get(), true)); 51 52 std::unique_ptr<OpLogProto> op_log_pb(new OpLogProto()); 53 string op_log_path = 54 io::JoinPath(testing::TensorFlowSrcRoot(), 55 "core/profiler/internal/testdata/tfprof_log"); 56 TF_CHECK_OK(ReadBinaryProto(Env::Default(), op_log_path, op_log_pb.get())); 57 58 string ckpt_path = io::JoinPath(testing::TensorFlowSrcRoot(), 59 "core/profiler/internal/testdata/ckpt"); 60 TF_Status* status = TF_NewStatus(); 61 std::unique_ptr<checkpoint::CheckpointReader> ckpt_reader( 62 new checkpoint::CheckpointReader(ckpt_path, status)); 63 CHECK(TF_GetCode(status) == TF_OK); 64 TF_DeleteStatus(status); 65 66 tf_stats_.reset(new TFStats(std::move(graph_pb), std::move(run_meta_pb), 67 std::move(op_log_pb), std::move(ckpt_reader))); 68 tf_stats_->BuildAllViews(); 69 } 70 71 string TestToFromProto(const string& cmd, const Options& opts, 72 bool show_multi_node = false) { 73 string profile_file = io::JoinPath(testing::TmpDir(), "profile"); 74 tf_stats_->WriteProfile(profile_file); 75 TFStats new_stats(profile_file, nullptr); 76 new_stats.BuildAllViews(); 77 if (show_multi_node) { 78 new_stats.ShowMultiGraphNode(cmd, opts); 79 } else { 80 new_stats.ShowGraphNode(cmd, opts); 81 } 82 string dump_str; 83 TF_CHECK_OK(ReadFileToString(Env::Default(), 84 opts.output_options.at("outfile"), &dump_str)); 85 return dump_str; 86 } 87 88 std::unique_ptr<TFStats> tf_stats_; 89}; 90 91TEST_F(TFProfShowTest, DumpScopeMode) { 92 string dump_file = io::JoinPath(testing::TmpDir(), "dump"); 93 Options opts( 94 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, "name", 95 {"VariableV2"}, // accout_type_regexes 96 {".*"}, {""}, {".*"}, {""}, false, 97 {"params", "bytes", "peak_bytes", "residual_bytes", "output_bytes", 98 "micros", "accelerator_micros", "cpu_micros", "float_ops"}, 99 "file", {{"outfile", dump_file}}); 100 tf_stats_->ShowGraphNode("scope", opts); 101 102 string dump_str; 103 TF_CHECK_OK(ReadFileToString(Env::Default(), dump_file, &dump_str)); 104 EXPECT_EQ( 105 "node name | # parameters | # float_ops | requested bytes | peak bytes | " 106 "residual bytes | output bytes | total execution time | accelerator " 107 "execution time | cpu execution time\n_TFProfRoot (--/451 params, --/0 " 108 "flops, --/0B, --/0B, --/0B, --/2.56KB, --/13us, --/0us, --/13us)\n DW " 109 "(3x3x3x6, 162/162 params, 0/0 flops, 0B/0B, 0B/0B, 0B/0B, " 110 "1.28KB/1.28KB, 2us/2us, 0us/0us, 2us/2us)\n DW2 (2x2x6x12, 288/288 " 111 "params, 0/0 flops, 0B/0B, 0B/0B, 0B/0B, 1.28KB/1.28KB, 11us/11us, " 112 "0us/0us, 11us/11us)\n ScalarW (1, 1/1 params, 0/0 flops, 0B/0B, 0B/0B, " 113 "0B/0B, 0B/0B, 0us/0us, 0us/0us, 0us/0us)\n", 114 dump_str); 115 116 EXPECT_EQ(dump_str, TestToFromProto("scope", opts)); 117} 118 119TEST_F(TFProfShowTest, DumpAcceleratorAndCPUMicros) { 120 string dump_file = io::JoinPath(testing::TmpDir(), "dump"); 121 Options opts(5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, "cpu_micros", 122 {".*"}, // accout_type_regexes 123 {".*"}, {""}, {".*"}, {""}, false, 124 {"accelerator_micros", "cpu_micros"}, "file", 125 {{"outfile", dump_file}}); 126 tf_stats_->ShowGraphNode("scope", opts); 127 128 string dump_str; 129 TF_CHECK_OK(ReadFileToString(Env::Default(), dump_file, &dump_str)); 130 EXPECT_EQ( 131 "node name | accelerator execution time | cpu execution " 132 "time\n_TFProfRoot (--/404us, --/4.54ms)\n Conv2D (226us/226us, " 133 "4.07ms/4.07ms)\n Conv2D_1 (178us/178us, 419us/419us)\n " 134 "_retval_Conv2D_1_0_0 (0us/0us, 41us/41us)\n DW2 (0us/0us, 11us/11us)\n " 135 " DW2/Assign (0us/0us, 0us/0us)\n DW2/Initializer (0us/0us, " 136 "0us/0us)\n DW2/Initializer/random_normal (0us/0us, 0us/0us)\n " 137 " DW2/Initializer/random_normal/RandomStandardNormal (0us/0us, " 138 "0us/0us)\n DW2/Initializer/random_normal/mean (0us/0us, " 139 "0us/0us)\n DW2/Initializer/random_normal/mul (0us/0us, " 140 "0us/0us)\n DW2/Initializer/random_normal/shape (0us/0us, " 141 "0us/0us)\n DW2/Initializer/random_normal/stddev (0us/0us, " 142 "0us/0us)\n DW2/read (0us/0us, 0us/0us)\n DW (0us/0us, 2us/2us)\n " 143 "DW/Assign (0us/0us, 0us/0us)\n DW/Initializer (0us/0us, 0us/0us)\n " 144 " DW/Initializer/random_normal (0us/0us, 0us/0us)\n " 145 "DW/Initializer/random_normal/RandomStandardNormal (0us/0us, 0us/0us)\n " 146 " DW/Initializer/random_normal/mean (0us/0us, 0us/0us)\n " 147 "DW/Initializer/random_normal/mul (0us/0us, 0us/0us)\n " 148 "DW/Initializer/random_normal/shape (0us/0us, 0us/0us)\n " 149 "DW/Initializer/random_normal/stddev (0us/0us, 0us/0us)\n DW/read " 150 "(0us/0us, 0us/0us)\n zeros (0us/0us, 2us/2us)\n ScalarW (0us/0us, " 151 "0us/0us)\n ScalarW/Assign (0us/0us, 0us/0us)\n " 152 "ScalarW/Initializer (0us/0us, 0us/0us)\n " 153 "ScalarW/Initializer/random_normal (0us/0us, 0us/0us)\n " 154 "ScalarW/Initializer/random_normal/RandomStandardNormal (0us/0us, " 155 "0us/0us)\n ScalarW/Initializer/random_normal/mean (0us/0us, " 156 "0us/0us)\n ScalarW/Initializer/random_normal/mul (0us/0us, " 157 "0us/0us)\n ScalarW/Initializer/random_normal/shape (0us/0us, " 158 "0us/0us)\n ScalarW/Initializer/random_normal/stddev (0us/0us, " 159 "0us/0us)\n ScalarW/read (0us/0us, 0us/0us)\n init (0us/0us, " 160 "0us/0us)\n", 161 dump_str); 162 163 EXPECT_EQ(dump_str, TestToFromProto("scope", opts)); 164} 165 166TEST_F(TFProfShowTest, DumpOpMode) { 167 string dump_file = io::JoinPath(testing::TmpDir(), "dump"); 168 Options opts( 169 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, "params", 170 {".*"}, // accout_type_regexes 171 {".*"}, {""}, {".*"}, {""}, false, 172 {"params", "bytes", "micros", "float_ops", "occurrence", "input_shapes"}, 173 "file", {{"outfile", dump_file}}); 174 tf_stats_->ShowMultiGraphNode("op", opts); 175 176 string dump_str; 177 TF_CHECK_OK(ReadFileToString(Env::Default(), dump_file, &dump_str)); 178 EXPECT_EQ( 179 "nodename|requestedbytes|totalexecutiontime|acceleratorexecutiontime|" 180 "cpuexecutiontime|#parameters|#float_ops|opoccurrence(run|defined)|" 181 "inputshapes\nVariableV20B(0.00%,0.00%),13us(100.00%,0.26%),0us(100.00%," 182 "0.00%),13us(100.00%,0.29%),451params(100.00%,100.00%),0float_ops(100.00%" 183 ",0.00%),2|3\n\ninput_type:\t(run*2|defined*3)\texec_time:13us\n\nAdd0B(" 184 "0.00%,0.00%),0us(99.74%,0.00%),0us(100.00%,0.00%),0us(99.71%,0.00%)," 185 "0params(0.00%,0.00%),0float_ops(100.00%,0.00%),0|3\n\ninput_type:0:1," 186 "\t1:1\t(run*0|defined*1)\texec_time:0us\ninput_type:0:2x2x6x12,\t1:1\t(" 187 "run*0|defined*1)\texec_time:0us\ninput_type:0:3x3x3x6,\t1:1\t(run*0|" 188 "defined*1)\texec_time:0us\n\nAssign0B(0.00%,0.00%),0us(99.74%,0.00%)," 189 "0us(100.00%,0.00%),0us(99.71%,0.00%),0params(0.00%,0.00%),0float_ops(" 190 "100.00%,0.00%),0|3\n\ninput_type:0:1,\t1:1\t(run*0|defined*1)\texec_" 191 "time:0us\ninput_type:0:2x2x6x12,\t1:2x2x6x12\t(run*0|defined*1)\texec_" 192 "time:0us\ninput_type:0:3x3x3x6,\t1:3x3x3x6\t(run*0|defined*1)\texec_" 193 "time:0us\n\nConst0B(0.00%,0.00%),2us(99.74%,0.04%),0us(100.00%,0.00%)," 194 "2us(99.71%,0.04%),0params(0.00%,0.00%),0float_ops(100.00%,0.00%),1|" 195 "10\n\ninput_type:\t(run*1|defined*10)\texec_time:2us\n\nConv2D14.59KB(" 196 "100.00%,100.00%),4.89ms(99.70%,98.87%),404us(100.00%,100.00%),4.49ms(99." 197 "67%,98.77%),0params(0.00%,0.00%),10.44kfloat_ops(100.00%,100.00%),2|" 198 "2\n\ninput_type:0:2x3x3x6,\t1:2x2x6x12\t(run*1|defined*1)\texec_time:" 199 "597us\ninput_type:0:2x6x6x3,\t1:3x3x3x6\t(run*1|defined*1)\texec_time:4." 200 "29ms\n\nIdentity0B(0.00%,0.00%),0us(0.83%,0.00%),0us(0.00%,0.00%),0us(0." 201 "90%,0.00%),0params(0.00%,0.00%),0float_ops(0.00%,0.00%),0|3\n\ninput_" 202 "type:0:1\t(run*0|defined*1)\texec_time:0us\ninput_type:0:2x2x6x12\t(run*" 203 "0|defined*1)\texec_time:0us\ninput_type:0:3x3x3x6\t(run*0|defined*1)" 204 "\texec_time:0us\n\n", 205 StringReplace(dump_str, " ", "")); 206 207 EXPECT_EQ(dump_str, TestToFromProto("op", opts, true)); 208} 209} // namespace tfprof 210} // namespace tensorflow 211