/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/lite/c/common.h"
#include "tensorflow/lite/micro/all_ops_resolver.h"
#include "tensorflow/lite/micro/benchmarks/micro_benchmark.h"
#include "tensorflow/lite/micro/examples/person_detection/model_settings.h"
#include "tensorflow/lite/micro/examples/person_detection/no_person_image_data.h"
#include "tensorflow/lite/micro/examples/person_detection/person_detect_model_data.h"
#include "tensorflow/lite/micro/examples/person_detection/person_image_data.h"
#include "tensorflow/lite/micro/micro_error_reporter.h"
#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/micro/micro_utils.h"
#include "tensorflow/lite/schema/schema_generated.h"
#include "tensorflow/lite/version.h"

/*
 * Person Detection benchmark.  Evaluates runtime performance of the visual
 * wakewords person detection model.  This is the same model found in
 * exmaples/person_detection.
 */

namespace {

using PersonDetectionOpResolver = tflite::AllOpsResolver;
using PersonDetectionBenchmarkRunner = MicroBenchmarkRunner<uint8_t>;

constexpr int kRandomSeed = 42;

// Create an area of memory to use for input, output, and intermediate arrays.
// Align arena to 16 bytes to avoid alignment warnings on certain platforms.
constexpr int kTensorArenaSize = 95 * 1024;
alignas(16) uint8_t tensor_arena[kTensorArenaSize];

uint8_t op_resolver_buffer[sizeof(PersonDetectionOpResolver)];
uint8_t benchmark_runner_buffer[sizeof(PersonDetectionBenchmarkRunner)];
PersonDetectionBenchmarkRunner* benchmark_runner = nullptr;

// Initialize benchmark runner instance explicitly to avoid global init order
// issues on Sparkfun. Use new since static variables within a method
// are automatically surrounded by locking, which breaks bluepill and stm32f4.
void CreateBenchmarkRunner() {
  // We allocate PersonDetectionOpResolver from a global buffer because the
  // object's lifetime must exceed that of the PersonDetectionBenchmarkRunner
  // object.
  benchmark_runner = new (benchmark_runner_buffer)
      PersonDetectionBenchmarkRunner(g_person_detect_model_data,
                                     new (op_resolver_buffer)
                                         PersonDetectionOpResolver(),
                                     tensor_arena, kTensorArenaSize);
}

void PersonDetectionTenIterationsWithRandomInput() {
  benchmark_runner->SetRandomInput(kRandomSeed);
  for (int i = 0; i < 10; i++) {
    benchmark_runner->RunSingleIteration();
  }
}

void PersonDetectionTenIerationsWithPerson() {
  // TODO(b/152644476): Add a way to run more than a single deterministic input.
  benchmark_runner->SetInput(g_person_data);
  for (int i = 0; i < 10; i++) {
    benchmark_runner->RunSingleIteration();
  }
}

void PersonDetectionTenIerationsWithoutPerson() {
  // TODO(b/152644476): Add a way to run more than a single deterministic input.
  benchmark_runner->SetInput(g_no_person_data);
  for (int i = 0; i < 10; i++) {
    benchmark_runner->RunSingleIteration();
  }
}

}  // namespace

TF_LITE_MICRO_BENCHMARKS_BEGIN

TF_LITE_MICRO_BENCHMARK(CreateBenchmarkRunner());
TF_LITE_MICRO_BENCHMARK(PersonDetectionTenIterationsWithRandomInput());
TF_LITE_MICRO_BENCHMARK(PersonDetectionTenIerationsWithPerson());
TF_LITE_MICRO_BENCHMARK(PersonDetectionTenIerationsWithoutPerson());

TF_LITE_MICRO_BENCHMARKS_END
