• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2020 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 #ifndef TENSORFLOW_LITE_MICRO_BENCHMARKS_MICRO_BENCHMARK_H_
17 #define TENSORFLOW_LITE_MICRO_BENCHMARKS_MICRO_BENCHMARK_H_
18 
19 #include <climits>
20 
21 #include "tensorflow/lite/micro/micro_error_reporter.h"
22 #include "tensorflow/lite/micro/micro_interpreter.h"
23 #include "tensorflow/lite/micro/micro_op_resolver.h"
24 #include "tensorflow/lite/micro/micro_profiler.h"
25 #include "tensorflow/lite/micro/micro_time.h"
26 
27 namespace tflite {
28 
29 template <typename inputT>
30 class MicroBenchmarkRunner {
31  public:
32   // The lifetimes of model, op_resolver, tensor_arena, profiler must exceed
33   // that of the created MicroBenchmarkRunner object.
MicroBenchmarkRunner(const uint8_t * model,const tflite::MicroOpResolver * op_resolver,uint8_t * tensor_arena,int tensor_arena_size,MicroProfiler * profiler)34   MicroBenchmarkRunner(const uint8_t* model,
35                        const tflite::MicroOpResolver* op_resolver,
36                        uint8_t* tensor_arena, int tensor_arena_size,
37                        MicroProfiler* profiler)
38       : interpreter_(GetModel(model), *op_resolver, tensor_arena,
39                      tensor_arena_size, GetMicroErrorReporter(), profiler) {
40     interpreter_.AllocateTensors();
41   }
42 
RunSingleIteration()43   void RunSingleIteration() {
44     // Run the model on this input and make sure it succeeds.
45     TfLiteStatus invoke_status = interpreter_.Invoke();
46     if (invoke_status == kTfLiteError) {
47       MicroPrintf("Invoke failed.");
48     }
49   }
50 
SetRandomInput(const int random_seed)51   void SetRandomInput(const int random_seed) {
52     // The pseudo-random number generator is initialized to a constant seed
53     std::srand(random_seed);
54     TfLiteTensor* input = interpreter_.input(0);
55 
56     // Pre-populate input tensor with random values.
57     int input_length = input->bytes / sizeof(inputT);
58     inputT* input_values = tflite::GetTensorData<inputT>(input);
59     for (int i = 0; i < input_length; i++) {
60       // Pre-populate input tensor with a random value based on a constant seed.
61       input_values[i] = static_cast<inputT>(
62           std::rand() % (std::numeric_limits<inputT>::max() -
63                          std::numeric_limits<inputT>::min() + 1));
64     }
65   }
66 
SetInput(const inputT * custom_input)67   void SetInput(const inputT* custom_input) {
68     TfLiteTensor* input = interpreter_.input(0);
69     inputT* input_buffer = tflite::GetTensorData<inputT>(input);
70     int input_length = input->bytes / sizeof(inputT);
71     for (int i = 0; i < input_length; i++) {
72       input_buffer[i] = custom_input[i];
73     }
74   }
75 
76  private:
77   tflite::MicroInterpreter interpreter_;
78 };
79 
80 }  // namespace tflite
81 
82 #endif  // TENSORFLOW_LITE_MICRO_BENCHMARKS_MICRO_BENCHMARK_H_
83