1 /* Copyright 2019 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_TOOLS_BENCHMARK_EXPERIMENTAL_C_BENCHMARK_C_API_H_ 17 #define TENSORFLOW_LITE_TOOLS_BENCHMARK_EXPERIMENTAL_C_BENCHMARK_C_API_H_ 18 19 #include "tensorflow/lite/c/c_api_types.h" 20 21 // ----------------------------------------------------------------------------- 22 // Experimental C APIs for the benchmark tool, mainly intended to be used for 23 // building a standalone TensorFlow Lite benchmark framework for iOS. This 24 // header only has a minimal dependency to the C API types, which can be 25 // included in the framework itself. 26 // ----------------------------------------------------------------------------- 27 28 #ifdef __cplusplus 29 extern "C" { 30 #endif // __cplusplus 31 32 typedef enum { 33 TfLiteBenchmarkWarmup, 34 TfLiteBenchmarkRegular, 35 } TfLiteBenchmarkRunType; 36 37 // ----------------------------------------------------------------------------- 38 // C APIs corresponding to tensorflow::Stat<int64_t> type. 39 // ----------------------------------------------------------------------------- 40 typedef struct TfLiteBenchmarkInt64Stat { 41 bool empty; 42 int64_t first; 43 int64_t newest; 44 int64_t max; 45 int64_t min; 46 int64_t count; 47 int64_t sum; 48 double squared_sum; 49 bool all_same; 50 double avg; 51 int64_t std_deviation; 52 } TfLiteBenchmarkInt64Stat; 53 54 // ----------------------------------------------------------------------------- 55 // C APIs corresponding to tflite::benchmark::BenchmarkResults type. 56 // ----------------------------------------------------------------------------- 57 typedef struct TfLiteBenchmarkResults TfLiteBenchmarkResults; 58 59 extern TfLiteBenchmarkInt64Stat 60 TfLiteBenchmarkResultsGetInferenceTimeMicroseconds( 61 const TfLiteBenchmarkResults* results); 62 63 extern TfLiteBenchmarkInt64Stat TfLiteBenchmarkResultsGetWarmupTimeMicroseconds( 64 const TfLiteBenchmarkResults* results); 65 66 extern int64_t TfLiteBenchmarkResultsGetStartupLatencyMicroseconds( 67 const TfLiteBenchmarkResults* results); 68 69 extern uint64_t TfLiteBenchmarkResultsGetInputBytes( 70 const TfLiteBenchmarkResults* results); 71 72 extern double TfLiteBenchmarkResultsGetThroughputMbPerSecond( 73 const TfLiteBenchmarkResults* results); 74 75 // ----------------------------------------------------------------------------- 76 // C APIs corresponding to tflite::benchmark::BenchmarkListener type. 77 // ----------------------------------------------------------------------------- 78 typedef struct TfLiteBenchmarkListener TfLiteBenchmarkListener; 79 80 extern TfLiteBenchmarkListener* TfLiteBenchmarkListenerCreate(); 81 82 extern void TfLiteBenchmarkListenerDelete(TfLiteBenchmarkListener* listener); 83 84 // Sets the listener callbacks. Only non-null callback functions will be called 85 // when the following events occur. The user_data pointer provided by the caller 86 // will also be forwarded as a parameter of each callback function. 87 // 88 // - on_benchmark_start: Called before the (outer) inference loop begins. Note 89 // that this is called *after* the interpreter has been initialized, but 90 // *before* any warmup runs have been executed. 91 // - on_single_run_start: Called before a single (inner) inference call starts. 92 // - on_single_run_end: Called before a single (inner) inference call ends. 93 // - on_benchmark_end: Called after the (outer) inference loop ends. 94 // 95 // In case of `on_benchmark_end` callback, the passed in `results` pointer is 96 // only valid during the callback function execution, and will be destroyed 97 // afterwards. 98 extern void TfLiteBenchmarkListenerSetCallbacks( 99 TfLiteBenchmarkListener* listener, void* user_data, 100 void (*on_benchmark_start_fn)(void* user_data), 101 void (*on_single_run_start_fn)(void* user_data, 102 TfLiteBenchmarkRunType runType), 103 void (*on_single_run_end_fn)(void* user_data), 104 void (*on_benchmark_end_fn)(void* user_data, 105 TfLiteBenchmarkResults* results)); 106 107 // ----------------------------------------------------------------------------- 108 // C APIs corresponding to tflite::benchmark::BenchmarkTfLiteModel type. 109 // ----------------------------------------------------------------------------- 110 typedef struct TfLiteBenchmarkTfLiteModel TfLiteBenchmarkTfLiteModel; 111 112 // TODO(b/144321502): Support BenchmarkParams. 113 extern TfLiteBenchmarkTfLiteModel* TfLiteBenchmarkTfLiteModelCreate(); 114 115 extern void TfLiteBenchmarkTfLiteModelDelete( 116 TfLiteBenchmarkTfLiteModel* benchmark_model); 117 118 extern TfLiteStatus TfLiteBenchmarkTfLiteModelInit( 119 TfLiteBenchmarkTfLiteModel* benchmark_model); 120 121 extern TfLiteStatus TfLiteBenchmarkTfLiteModelRun( 122 TfLiteBenchmarkTfLiteModel* benchmark_model); 123 124 extern TfLiteStatus TfLiteBenchmarkTfLiteModelRunWithArgs( 125 TfLiteBenchmarkTfLiteModel* benchmark_model, int argc, char** argv); 126 127 extern void TfLiteBenchmarkTfLiteModelAddListener( 128 TfLiteBenchmarkTfLiteModel* benchmark_model, 129 const TfLiteBenchmarkListener* listener); 130 131 #ifdef __cplusplus 132 } // extern "C" 133 #endif // __cplusplus 134 135 #endif // TENSORFLOW_LITE_TOOLS_BENCHMARK_EXPERIMENTAL_C_BENCHMARK_C_API_H_ 136