message: Separate the memory usage of initialization (i.e. including allocating tensors etc.) from the actual inference run in the benchmark tool so that we have a more clear picture of memory usage.
diff: @@ -25,6 +25,18 @@ limitations under the License.
  namespace tflite {
  namespace benchmark {
  +namespace {
  +using tflite::profiling::memory::MemoryUsage;
  +void LogMemUsageDelta(const std::string& tag, const MemoryUsage& before,
  + const MemoryUsage& after) {
  + const auto delta = after - before;
  + TFLITE_LOG(INFO) << "[" << tag << "] - Memory usage: max resident set size = "
  + << delta.max_rss_kb / 1024.0
  + << " MB, total malloc-ed size = "
  + << delta.total_allocated_bytes / 1024.0 / 1024.0 << " MB";
  +}
  +} // namespace
  +
  using tensorflow::Stat;
  BenchmarkParams BenchmarkModel::DefaultParams() {
  @@ -161,13 +173,14 @@ TfLiteStatus BenchmarkModel::Run() {
  LogParams();
  const auto start_mem_usage = profiling::memory::GetMemoryUsage();
  -
  int64_t initialization_start_us = profiling::time::NowMicros();
  TF_LITE_ENSURE_STATUS(Init());
  + const auto init_end_mem_usage = profiling::memory::GetMemoryUsage();
  int64_t initialization_end_us = profiling::time::NowMicros();
  int64_t startup_latency_us = initialization_end_us - initialization_start_us;
  TFLITE_LOG(INFO) << "Initialized session in " << startup_latency_us / 1e3
  << "ms";
  + LogMemUsageDelta("Init Phase", start_mem_usage, init_end_mem_usage);
  TF_LITE_ENSURE_STATUS(PrepareInputData());
  @@ -186,13 +199,8 @@ TfLiteStatus BenchmarkModel::Run() {
  Run(params_.Get<int32_t>("num_runs"), params_.Get<float>("min_secs"),
  params_.Get<float>("max_secs"), REGULAR, &status);
  - const auto mem_usage = profiling::memory::GetMemoryUsage() - start_mem_usage;
  -
  - TFLITE_LOG(INFO) << "Memory usage: max resident set size = "
  - << mem_usage.max_rss_kb / 1024.0 << " MB";
  - TFLITE_LOG(INFO) << "Memory usage: total malloc-ed memory = "
  - << mem_usage.total_allocated_bytes / 1024.0 / 1024.0
  - << " MB";
  + const auto after_run_mem_usage = profiling::memory::GetMemoryUsage();
  + LogMemUsageDelta("Overall", start_mem_usage, after_run_mem_usage);
  listeners_.OnBenchmarkEnd(
  {startup_latency_us, input_bytes, warmup_time_us, inference_time_us});
  