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/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_utils.h" 27 #include "tensorflow/core/profiler/tfprof_log.pb.h" 28 #include "tensorflow/core/profiler/tfprof_options.h" 29 #include "tensorflow/core/profiler/tfprof_output.pb.h" 30 #include "tensorflow/core/protobuf/config.pb.h" 31 32 namespace tensorflow { 33 namespace tfprof { 34 35 string CheckAndRemoveDoc(const string& doc) { 36 auto pos = doc.find("Profile:"); 37 CHECK(pos != doc.npos); 38 return doc.substr(pos + 9); 39 } 40 41 class TFProfShowTest : public ::testing::Test { 42 protected: 43 TFProfShowTest() { 44 string graph_path = 45 io::JoinPath(testing::TensorFlowSrcRoot(), 46 "core/profiler/internal/testdata/graph.pbtxt"); 47 std::unique_ptr<tensorflow::GraphDef> graph_pb(new tensorflow::GraphDef()); 48 TF_CHECK_OK( 49 ReadProtoFile(Env::Default(), graph_path, graph_pb.get(), false)); 50 51 std::unique_ptr<tensorflow::RunMetadata> run_meta_pb( 52 new tensorflow::RunMetadata()); 53 string run_meta_path = 54 io::JoinPath(testing::TensorFlowSrcRoot(), 55 "core/profiler/internal/testdata/run_meta"); 56 TF_CHECK_OK( 57 ReadProtoFile(Env::Default(), run_meta_path, run_meta_pb.get(), true)); 58 59 std::unique_ptr<OpLogProto> op_log_pb(new OpLogProto()); 60 string op_log_path = 61 io::JoinPath(testing::TensorFlowSrcRoot(), 62 "core/profiler/internal/testdata/tfprof_log"); 63 TF_CHECK_OK(ReadBinaryProto(Env::Default(), op_log_path, op_log_pb.get())); 64 65 string ckpt_path = io::JoinPath(testing::TensorFlowSrcRoot(), 66 "core/profiler/internal/testdata/ckpt"); 67 TF_Status* status = TF_NewStatus(); 68 std::unique_ptr<checkpoint::CheckpointReader> ckpt_reader( 69 new checkpoint::CheckpointReader(ckpt_path, status)); 70 CHECK(TF_GetCode(status) == TF_OK); 71 TF_DeleteStatus(status); 72 73 tf_stats_.reset(new TFStats(std::move(graph_pb), std::move(run_meta_pb), 74 std::move(op_log_pb), std::move(ckpt_reader))); 75 tf_stats_->BuildAllViews(); 76 } 77 78 string TestToFromProto(const string& cmd, const Options& opts, 79 bool show_multi_node = false) { 80 string profile_file = io::JoinPath(testing::TmpDir(), "profile"); 81 tf_stats_->WriteProfile(profile_file); 82 TFStats new_stats(profile_file, nullptr); 83 new_stats.BuildAllViews(); 84 if (show_multi_node) { 85 new_stats.ShowMultiGraphNode(cmd, opts); 86 } else { 87 new_stats.ShowGraphNode(cmd, opts); 88 } 89 string dump_str; 90 TF_CHECK_OK(ReadFileToString(Env::Default(), 91 opts.output_options.at("outfile"), &dump_str)); 92 return dump_str; 93 } 94 95 std::unique_ptr<TFStats> tf_stats_; 96 }; 97 98 TEST_F(TFProfShowTest, DumpScopeMode) { 99 string dump_file = io::JoinPath(testing::TmpDir(), "dump"); 100 Options opts( 101 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, "name", 102 {"VariableV2"}, // accout_type_regexes 103 {".*"}, {""}, {".*"}, {""}, false, 104 {"params", "bytes", "peak_bytes", "residual_bytes", "output_bytes", 105 "micros", "accelerator_micros", "cpu_micros", "float_ops"}, 106 "file", {{"outfile", dump_file}}); 107 tf_stats_->ShowGraphNode("scope", opts); 108 109 string dump_str; 110 TF_CHECK_OK(ReadFileToString(Env::Default(), dump_file, &dump_str)); 111 EXPECT_EQ( 112 "node name | # parameters | # float_ops | requested bytes | peak bytes | " 113 "residual bytes | output bytes | total execution time | accelerator " 114 "execution time | cpu execution time\n_TFProfRoot (--/451 params, --/0 " 115 "flops, --/2.56KB, --/2.56KB, --/2.56KB, --/2.56KB, --/13us, --/0us, " 116 "--/13us)\n DW (3x3x3x6, 162/162 params, 0/0 flops, 1.28KB/1.28KB, " 117 "1.28KB/1.28KB, 1.28KB/1.28KB, 1.28KB/1.28KB, 2us/2us, 0us/0us, " 118 "2us/2us)\n DW2 (2x2x6x12, 288/288 params, 0/0 flops, 1.28KB/1.28KB, " 119 "1.28KB/1.28KB, 1.28KB/1.28KB, 1.28KB/1.28KB, 11us/11us, 0us/0us, " 120 "11us/11us)\n ScalarW (1, 1/1 params, 0/0 flops, 0B/0B, 0B/0B, 0B/0B, " 121 "0B/0B, 0us/0us, 0us/0us, 0us/0us)\n", 122 CheckAndRemoveDoc(dump_str)); 123 124 EXPECT_EQ(dump_str, TestToFromProto("scope", opts)); 125 } 126 127 TEST_F(TFProfShowTest, DumpAcceleratorAndCPUMicros) { 128 string dump_file = io::JoinPath(testing::TmpDir(), "dump"); 129 Options opts(5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, "cpu_micros", 130 {".*"}, // accout_type_regexes 131 {".*"}, {""}, {".*"}, {""}, false, 132 {"accelerator_micros", "cpu_micros"}, "file", 133 {{"outfile", dump_file}}); 134 tf_stats_->ShowGraphNode("scope", opts); 135 136 string dump_str; 137 TF_CHECK_OK(ReadFileToString(Env::Default(), dump_file, &dump_str)); 138 EXPECT_EQ( 139 "node name | accelerator execution time | cpu execution " 140 "time\n_TFProfRoot (--/404us, --/4.54ms)\n Conv2D (226us/226us, " 141 "4.07ms/4.07ms)\n Conv2D_1 (178us/178us, 419us/419us)\n " 142 "_retval_Conv2D_1_0_0 (0us/0us, 41us/41us)\n DW2 (0us/0us, 11us/11us)\n " 143 " DW2/Assign (0us/0us, 0us/0us)\n DW2/Initializer (0us/0us, " 144 "0us/0us)\n DW2/Initializer/random_normal (0us/0us, 0us/0us)\n " 145 " DW2/Initializer/random_normal/RandomStandardNormal (0us/0us, " 146 "0us/0us)\n DW2/Initializer/random_normal/mean (0us/0us, " 147 "0us/0us)\n DW2/Initializer/random_normal/mul (0us/0us, " 148 "0us/0us)\n DW2/Initializer/random_normal/shape (0us/0us, " 149 "0us/0us)\n DW2/Initializer/random_normal/stddev (0us/0us, " 150 "0us/0us)\n DW2/read (0us/0us, 0us/0us)\n DW (0us/0us, 2us/2us)\n " 151 "DW/Assign (0us/0us, 0us/0us)\n DW/Initializer (0us/0us, 0us/0us)\n " 152 " DW/Initializer/random_normal (0us/0us, 0us/0us)\n " 153 "DW/Initializer/random_normal/RandomStandardNormal (0us/0us, 0us/0us)\n " 154 " DW/Initializer/random_normal/mean (0us/0us, 0us/0us)\n " 155 "DW/Initializer/random_normal/mul (0us/0us, 0us/0us)\n " 156 "DW/Initializer/random_normal/shape (0us/0us, 0us/0us)\n " 157 "DW/Initializer/random_normal/stddev (0us/0us, 0us/0us)\n DW/read " 158 "(0us/0us, 0us/0us)\n zeros (0us/0us, 2us/2us)\n ScalarW (0us/0us, " 159 "0us/0us)\n ScalarW/Assign (0us/0us, 0us/0us)\n " 160 "ScalarW/Initializer (0us/0us, 0us/0us)\n " 161 "ScalarW/Initializer/random_normal (0us/0us, 0us/0us)\n " 162 "ScalarW/Initializer/random_normal/RandomStandardNormal (0us/0us, " 163 "0us/0us)\n ScalarW/Initializer/random_normal/mean (0us/0us, " 164 "0us/0us)\n ScalarW/Initializer/random_normal/mul (0us/0us, " 165 "0us/0us)\n ScalarW/Initializer/random_normal/shape (0us/0us, " 166 "0us/0us)\n ScalarW/Initializer/random_normal/stddev (0us/0us, " 167 "0us/0us)\n ScalarW/read (0us/0us, 0us/0us)\n init (0us/0us, " 168 "0us/0us)\n", 169 CheckAndRemoveDoc(dump_str)); 170 171 EXPECT_EQ(dump_str, TestToFromProto("scope", opts)); 172 } 173 174 TEST_F(TFProfShowTest, DumpOpMode) { 175 string dump_file = io::JoinPath(testing::TmpDir(), "dump"); 176 Options opts( 177 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -1, "params", 178 {".*"}, // accout_type_regexes 179 {".*"}, {""}, {".*"}, {""}, false, 180 {"params", "bytes", "micros", "float_ops", "occurrence", "input_shapes"}, 181 "file", {{"outfile", dump_file}}); 182 tf_stats_->ShowMultiGraphNode("op", opts); 183 184 string dump_str; 185 TF_CHECK_OK(ReadFileToString(Env::Default(), dump_file, &dump_str)); 186 EXPECT_EQ( 187 "nodename|requestedbytes|totalexecutiontime|acceleratorexecutiontime|" 188 "cpuexecutiontime|#parameters|#float_ops|opoccurrence(run|defined)|" 189 "inputshapes\nVariableV22.56KB(100.00%,8.40%),13us(100.00%,0.26%),0us(" 190 "100.00%,0.00%),13us(100.00%,0.29%),451params(100.00%,100.00%),0float_" 191 "ops(100.00%,0.00%),2|3\n\ninput_type:\t(run*2|defined*3)\texec_time:" 192 "13us\n\nAdd0B(0.00%,0.00%),0us(99.74%,0.00%),0us(100.00%,0.00%),0us(99." 193 "71%,0.00%),0params(0.00%,0.00%),0float_ops(100.00%,0.00%),0|3\n\ninput_" 194 "type:0:1,\t1:1\t(run*0|defined*1)\texec_time:0us\ninput_type:0:2x2x6x12," 195 "\t1:1\t(run*0|defined*1)\texec_time:0us\ninput_type:0:3x3x3x6,\t1:1\t(" 196 "run*0|defined*1)\texec_time:0us\n\nAssign0B(0.00%,0.00%),0us(99.74%,0." 197 "00%),0us(100.00%,0.00%),0us(99.71%,0.00%),0params(0.00%,0.00%),0float_" 198 "ops(100.00%,0.00%),0|3\n\ninput_type:0:1,\t1:1\t(run*0|defined*1)\texec_" 199 "time:0us\ninput_type:0:2x2x6x12,\t1:2x2x6x12\t(run*0|defined*1)\texec_" 200 "time:0us\ninput_type:0:3x3x3x6,\t1:3x3x3x6\t(run*0|defined*1)\texec_" 201 "time:0us\n\nConst0B(0.00%,0.00%),2us(99.74%,0.04%),0us(100.00%,0.00%)," 202 "2us(99.71%,0.04%),0params(0.00%,0.00%),0float_ops(100.00%,0.00%),1|" 203 "10\n\ninput_type:\t(run*1|defined*10)\texec_time:2us\n\nConv2D27.90KB(" 204 "91.60%,91.60%),4.89ms(99.70%,98.87%),404us(100.00%,100.00%),4.49ms(99." 205 "67%,98.77%),0params(0.00%,0.00%),10.44kfloat_ops(100.00%,100.00%),2|" 206 "2\n\ninput_type:0:2x3x3x6,\t1:2x2x6x12\t(run*1|defined*1)\texec_time:" 207 "597us\ninput_type:0:2x6x6x3,\t1:3x3x3x6\t(run*1|defined*1)\texec_time:4." 208 "29ms\n\nIdentity0B(0.00%,0.00%),0us(0.83%,0.00%),0us(0.00%,0.00%),0us(0." 209 "90%,0.00%),0params(0.00%,0.00%),0float_ops(0.00%,0.00%),0|3\n\ninput_" 210 "type:0:1\t(run*0|defined*1)\texec_time:0us\ninput_type:0:2x2x6x12\t(run*" 211 "0|defined*1)\texec_time:0us\ninput_type:0:3x3x3x6\t(run*0|defined*1)" 212 "\texec_time:0us\n\n", 213 StringReplace(CheckAndRemoveDoc(dump_str), " ", "")); 214 215 EXPECT_EQ(dump_str, TestToFromProto("op", opts, true)); 216 } 217 } // namespace tfprof 218 } // namespace tensorflow 219