From 33d0564c8ee6a7d6af129deacdb7505fc8d7ce3f Mon Sep 17 00:00:00 2001 From: Bin Li Date: Thu, 12 Apr 2018 15:46:25 +0800 Subject: [PATCH] Log feature map size and arguments during benchmark - Print feature map of each layer. - If op is convolution or pooling layer, print: stride, padding, dilation and kernel size, too. - For padding, if padding type is given, print 'VALID/SAME/FULL', else if padding values are given, print padding values. --- mace/benchmark/BUILD | 2 + mace/benchmark/stat_summarizer.cc | 95 +++++++++++++++++++++++++++---- mace/benchmark/stat_summarizer.h | 5 +- mace/core/net.cc | 26 ++++++++- mace/public/mace_types.h | 10 ++++ 5 files changed, 124 insertions(+), 14 deletions(-) diff --git a/mace/benchmark/BUILD b/mace/benchmark/BUILD index 9334e747..50ed42ee 100644 --- a/mace/benchmark/BUILD +++ b/mace/benchmark/BUILD @@ -16,6 +16,8 @@ cc_library( hdrs = ["stat_summarizer.h"], linkstatic = 1, deps = [ + "//mace/core", + "//mace/kernels", "//mace/public", "//mace/utils", ], diff --git a/mace/benchmark/stat_summarizer.cc b/mace/benchmark/stat_summarizer.cc index 10a89d80..87636839 100644 --- a/mace/benchmark/stat_summarizer.cc +++ b/mace/benchmark/stat_summarizer.cc @@ -11,7 +11,8 @@ #include "mace/public/mace.h" #include "mace/utils/logging.h" - +#include "mace/core/types.h" +#include "mace/kernels/conv_pool_2d_util.h" namespace mace { namespace benchmark { @@ -55,6 +56,8 @@ void StatSummarizer::ProcessMetadata(const RunMetadata &run_metadata) { if (result.second) { detail->name = name; detail->type = op_type; + detail->output_shape = ops.output_shape; + detail->args = ops.args; detail->run_order = node_num; @@ -83,7 +86,7 @@ std::string StatSummarizer::ShortSummary() const { } std::ostream &InitField(std::ostream &stream, int width) { - stream << "\t" << std::right << std::setw(width) << std::fixed + stream << std::right << std::setw(width) << std::fixed << std::setprecision(3); return stream; } @@ -98,12 +101,72 @@ std::string StatSummarizer::HeaderString(const std::string &title) const { InitField(stream, 9) << "[start]"; InitField(stream, 9) << "[first]"; InitField(stream, 9) << "[avg ms]"; - InitField(stream, 8) << "[%]"; - InitField(stream, 8) << "[cdf%]"; + InitField(stream, 9) << "[%]"; + InitField(stream, 9) << "[cdf%]"; InitField(stream, 10) << "[mem KB]"; - InitField(stream, 9) << "[times called]"; - stream << "\t" - << "[Name]"; + InitField(stream, 10) << "[Name]"; + InitField(stream, 8) << "[stride]"; + InitField(stream, 10) << "[padding]"; + InitField(stream, 10) << "[dilation]"; + InitField(stream, 15) << "[kernel]"; + stream << std::right << std::setw(45) << "[output shape]"; + + return stream.str(); +} + +std::string PaddingTypeToString(int padding_type) { + std::stringstream stream; + Padding type = static_cast(padding_type); + switch (type) { + case VALID: stream << "VALID"; break; + case SAME: stream << "SAME"; break; + case FULL: stream << "FULL"; break; + default: stream << padding_type; break; + } + + return stream.str(); +} + +std::string ShapeToString(const std::vector &output_shape) { + if (output_shape.empty()) { + return ""; + } + + std::stringstream stream; + stream << "["; + for (int i = 0; i < output_shape.size(); ++i) { + const std::vector &dims = output_shape[i].dims(); + for (int j = 0; j < dims.size(); ++j) { + stream << dims[j]; + if (j != dims.size() - 1) { + stream << ","; + } + } + if (i != output_shape.size() - 1) { + stream << ":"; + } + } + stream << "]"; + + return stream.str(); +} + +template +std::string VectorToString(const std::vector &vec) { + if (vec.empty()) { + return ""; + } + + std::stringstream stream; + stream << "["; + for (int i = 0; i < vec.size(); ++i) { + stream << vec[i]; + if (i != vec.size() - 1) { + stream << ","; + } + } + stream << "]"; + return stream.str(); } @@ -115,18 +178,25 @@ std::string StatSummarizer::ColumnString(const StatSummarizer::Detail &detail, const double avg_time_ms = detail.rel_end_us.avg() / 1000.0; const double percentage = detail.rel_end_us.sum() * 100.0 / stat.sum(); const double cdf_percentage = (cumulative_stat_on_node * 100.0f) / stat.sum(); - const int64_t times_called = detail.times_called / num_runs(); std::stringstream stream; InitField(stream, 24) << detail.type; InitField(stream, 9) << start_ms; InitField(stream, 9) << first_time_ms; InitField(stream, 9) << avg_time_ms; - InitField(stream, 7) << percentage << "%"; - InitField(stream, 7) << cdf_percentage << "%"; + InitField(stream, 8) << percentage << "%"; + InitField(stream, 8) << cdf_percentage << "%"; InitField(stream, 10) << detail.mem_used.newest() / 1000.0; - InitField(stream, 9) << times_called; - stream << "\t" << detail.name; + InitField(stream, 10) << detail.name; + InitField(stream, 8) << VectorToString(detail.args.strides); + if (detail.args.padding_type != -1) { + InitField(stream, 10) << PaddingTypeToString(detail.args.padding_type); + } else { + InitField(stream, 10) << VectorToString(detail.args.paddings); + } + InitField(stream, 10) << VectorToString(detail.args.dilations); + InitField(stream, 15) << VectorToString(detail.args.kernels); + stream << std::right << std::setw(45) << ShapeToString(detail.output_shape); return stream.str(); } @@ -138,6 +208,7 @@ void StatSummarizer::OrderNodesByMetric( for (const auto &det : details_) { const Detail *detail = &(det.second); + std::stringstream stream; stream << std::setw(20) << std::right << std::setprecision(10) << std::fixed; diff --git a/mace/benchmark/stat_summarizer.h b/mace/benchmark/stat_summarizer.h index 8d2b99ad..96d597da 100644 --- a/mace/benchmark/stat_summarizer.h +++ b/mace/benchmark/stat_summarizer.h @@ -14,6 +14,8 @@ #include #include +#include "mace/public/mace_types.h" + namespace mace { class RunMetadata; @@ -175,6 +177,8 @@ class StatSummarizer { struct Detail { std::string name; std::string type; + std::vector output_shape; + ConvPoolArgs args; int64_t run_order; Stat start_us; Stat rel_end_us; @@ -189,7 +193,6 @@ class StatSummarizer { std::string ColumnString(const Detail &detail, const int64_t cumulative_stat_on_node, const Stat &stat) const; - Stat run_total_us_; Stat memory_; diff --git a/mace/core/net.cc b/mace/core/net.cc index 02efc1a4..7563ed5c 100644 --- a/mace/core/net.cc +++ b/mace/core/net.cc @@ -67,8 +67,32 @@ bool SerialNet::Run(RunMetadata *run_metadata) { } if (run_metadata != nullptr) { + std::vector strides; + int padding_type = -1; + std::vector paddings; + std::vector dilations; + std::vector kernels; + std::string type = op->debug_def().type(); + + if (type.compare("Conv2D") == 0 || + type.compare("FusedConv2D") == 0 || + type.compare("DepthwiseConv2d") == 0 || + type.compare("Pooling") == 0) { + strides = op->GetRepeatedArgument("strides"); + padding_type = op->GetSingleArgument("padding", -1); + paddings = op->GetRepeatedArgument("padding_values"); + dilations = op->GetRepeatedArgument("dilations"); + if (type.compare("Pooling") == 0) { + kernels = op->GetRepeatedArgument("kernels"); + } else { + kernels = op->Input(1)->shape(); + } + } + OperatorStats op_stats = {op->debug_def().name(), op->debug_def().type(), - call_stats}; + op->debug_def().output_shape(), + {strides, padding_type, paddings, dilations, + kernels}, call_stats}; run_metadata->op_stats.emplace_back(op_stats); } diff --git a/mace/public/mace_types.h b/mace/public/mace_types.h index 14182583..daa3441d 100644 --- a/mace/public/mace_types.h +++ b/mace/public/mace_types.h @@ -326,9 +326,19 @@ struct CallStats { int64_t end_micros; }; +struct ConvPoolArgs { + std::vector strides; + int padding_type; + std::vector paddings; + std::vector dilations; + std::vector kernels; +}; + struct OperatorStats { std::string operator_name; std::string type; + std::vector output_shape; + ConvPoolArgs args; CallStats stats; }; -- GitLab