• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2021 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 #include "tensorflow/lite/experimental/acceleration/configuration/flatbuffer_to_proto.h"
16 
17 #include "tensorflow/lite/experimental/acceleration/configuration/configuration.pb.h"
18 #include "tensorflow/lite/experimental/acceleration/configuration/configuration_generated.h"
19 #include "tensorflow/lite/minimal_logging.h"
20 
21 namespace tflite {
22 namespace {
ConvertExecutionPreference(ExecutionPreference preference)23 proto::ExecutionPreference ConvertExecutionPreference(
24     ExecutionPreference preference) {
25   switch (preference) {
26     case ExecutionPreference_ANY:
27       return proto::ExecutionPreference::ANY;
28     case ExecutionPreference_LOW_LATENCY:
29       return proto::ExecutionPreference::LOW_LATENCY;
30     case ExecutionPreference_LOW_POWER:
31       return proto::ExecutionPreference::LOW_POWER;
32     case ExecutionPreference_FORCE_CPU:
33       return proto::ExecutionPreference::FORCE_CPU;
34   }
35   TFLITE_LOG_PROD(TFLITE_LOG_ERROR,
36                   "Unexpected value for ExecutionPreference: %d", preference);
37   return proto::ExecutionPreference::ANY;
38 }
39 
ConvertDelegate(Delegate delegate)40 proto::Delegate ConvertDelegate(Delegate delegate) {
41   switch (delegate) {
42     case Delegate_NONE:
43       return proto::Delegate::NONE;
44     case Delegate_NNAPI:
45       return proto::Delegate::NNAPI;
46     case Delegate_GPU:
47       return proto::Delegate::GPU;
48     case Delegate_HEXAGON:
49       return proto::Delegate::HEXAGON;
50     case Delegate_XNNPACK:
51       return proto::Delegate::XNNPACK;
52     case Delegate_EDGETPU:
53       return proto::Delegate::EDGETPU;
54     case Delegate_EDGETPU_CORAL:
55       return proto::Delegate::EDGETPU_CORAL;
56     case Delegate_CORE_ML:
57       return proto::Delegate::CORE_ML;
58   }
59   TFLITE_LOG_PROD(TFLITE_LOG_ERROR, "Unexpected value for Delegate: %d",
60                   delegate);
61   return proto::Delegate::NONE;
62 }
63 
ConvertNNAPIExecutionPreference(NNAPIExecutionPreference preference)64 proto::NNAPIExecutionPreference ConvertNNAPIExecutionPreference(
65     NNAPIExecutionPreference preference) {
66   switch (preference) {
67     case NNAPIExecutionPreference_UNDEFINED:
68       return proto::NNAPIExecutionPreference::UNDEFINED;
69     case NNAPIExecutionPreference_NNAPI_LOW_POWER:
70       return proto::NNAPIExecutionPreference::NNAPI_LOW_POWER;
71     case NNAPIExecutionPreference_NNAPI_FAST_SINGLE_ANSWER:
72       return proto::NNAPIExecutionPreference::NNAPI_FAST_SINGLE_ANSWER;
73     case NNAPIExecutionPreference_NNAPI_SUSTAINED_SPEED:
74       return proto::NNAPIExecutionPreference::NNAPI_SUSTAINED_SPEED;
75   }
76   TFLITE_LOG_PROD(TFLITE_LOG_ERROR,
77                   "Unexpected value for NNAPIExecutionPreference: %d",
78                   preference);
79   return proto::NNAPIExecutionPreference::UNDEFINED;
80 }
81 
ConvertNNAPIExecutionPriority(NNAPIExecutionPriority priority)82 proto::NNAPIExecutionPriority ConvertNNAPIExecutionPriority(
83     NNAPIExecutionPriority priority) {
84   switch (priority) {
85     case NNAPIExecutionPriority_NNAPI_PRIORITY_UNDEFINED:
86       return proto::NNAPIExecutionPriority::NNAPI_PRIORITY_UNDEFINED;
87     case NNAPIExecutionPriority_NNAPI_PRIORITY_LOW:
88       return proto::NNAPIExecutionPriority::NNAPI_PRIORITY_LOW;
89     case NNAPIExecutionPriority_NNAPI_PRIORITY_MEDIUM:
90       return proto::NNAPIExecutionPriority::NNAPI_PRIORITY_MEDIUM;
91     case NNAPIExecutionPriority_NNAPI_PRIORITY_HIGH:
92       return proto::NNAPIExecutionPriority::NNAPI_PRIORITY_HIGH;
93   }
94   TFLITE_LOG_PROD(TFLITE_LOG_ERROR,
95                   "Unexpected value for NNAPIExecutionPriority: %d", priority);
96   return proto::NNAPIExecutionPriority::NNAPI_PRIORITY_UNDEFINED;
97 }
98 
ConvertGPUBackend(GPUBackend backend)99 proto::GPUBackend ConvertGPUBackend(GPUBackend backend) {
100   switch (backend) {
101     case GPUBackend_UNSET:
102       return proto::GPUBackend::UNSET;
103     case GPUBackend_OPENCL:
104       return proto::GPUBackend::OPENCL;
105     case GPUBackend_OPENGL:
106       return proto::GPUBackend::OPENGL;
107   }
108   TFLITE_LOG_PROD(TFLITE_LOG_ERROR, "Unexpected value for GPUBackend: %d",
109                   backend);
110   return proto::GPUBackend::UNSET;
111 }
112 
ConvertGPUInferenceUsage(GPUInferenceUsage preference)113 proto::GPUInferenceUsage ConvertGPUInferenceUsage(
114     GPUInferenceUsage preference) {
115   switch (preference) {
116     case GPUInferenceUsage_GPU_INFERENCE_PREFERENCE_FAST_SINGLE_ANSWER:
117       return proto::GPUInferenceUsage::
118           GPU_INFERENCE_PREFERENCE_FAST_SINGLE_ANSWER;
119     case GPUInferenceUsage_GPU_INFERENCE_PREFERENCE_SUSTAINED_SPEED:
120       return proto::GPUInferenceUsage::GPU_INFERENCE_PREFERENCE_SUSTAINED_SPEED;
121   }
122   TFLITE_LOG_PROD(TFLITE_LOG_ERROR,
123                   "Unexpected value for GPUInferenceUsage: %d", preference);
124   return proto::GPUInferenceUsage::GPU_INFERENCE_PREFERENCE_FAST_SINGLE_ANSWER;
125 }
126 
ConvertGPUInferencePriority(GPUInferencePriority priority)127 proto::GPUInferencePriority ConvertGPUInferencePriority(
128     GPUInferencePriority priority) {
129   switch (priority) {
130     case GPUInferencePriority_GPU_PRIORITY_AUTO:
131       return proto::GPUInferencePriority::GPU_PRIORITY_AUTO;
132     case GPUInferencePriority_GPU_PRIORITY_MAX_PRECISION:
133       return proto::GPUInferencePriority::GPU_PRIORITY_MAX_PRECISION;
134     case GPUInferencePriority_GPU_PRIORITY_MIN_LATENCY:
135       return proto::GPUInferencePriority::GPU_PRIORITY_MIN_LATENCY;
136     case GPUInferencePriority_GPU_PRIORITY_MIN_MEMORY_USAGE:
137       return proto::GPUInferencePriority::GPU_PRIORITY_MIN_MEMORY_USAGE;
138   }
139   TFLITE_LOG_PROD(TFLITE_LOG_ERROR,
140                   "Unexpected value for GPUInferencePriority: %d", priority);
141   return proto::GPUInferencePriority::GPU_PRIORITY_AUTO;
142 }
143 
ConvertEdgeTpuPowerState(EdgeTpuPowerState state)144 proto::EdgeTpuPowerState ConvertEdgeTpuPowerState(EdgeTpuPowerState state) {
145   switch (state) {
146     case EdgeTpuPowerState_UNDEFINED_POWERSTATE:
147       return proto::EdgeTpuPowerState::UNDEFINED_POWERSTATE;
148     case EdgeTpuPowerState_TPU_CORE_OFF:
149       return proto::EdgeTpuPowerState::TPU_CORE_OFF;
150     case EdgeTpuPowerState_READY:
151       return proto::EdgeTpuPowerState::READY;
152     case EdgeTpuPowerState_ACTIVE_MIN_POWER:
153       return proto::EdgeTpuPowerState::ACTIVE_MIN_POWER;
154     case EdgeTpuPowerState_ACTIVE_VERY_LOW_POWER:
155       return proto::EdgeTpuPowerState::ACTIVE_VERY_LOW_POWER;
156     case EdgeTpuPowerState_ACTIVE_LOW_POWER:
157       return proto::EdgeTpuPowerState::ACTIVE_LOW_POWER;
158     case EdgeTpuPowerState_ACTIVE:
159       return proto::EdgeTpuPowerState::ACTIVE;
160     case EdgeTpuPowerState_OVER_DRIVE:
161       return proto::EdgeTpuPowerState::OVER_DRIVE;
162   }
163   TFLITE_LOG_PROD(TFLITE_LOG_ERROR,
164                   "Unexpected value for EdgeTpuSettings::PowerState: %d",
165                   state);
166   return proto::EdgeTpuPowerState::UNDEFINED_POWERSTATE;
167 }
168 
ConvertFallbackSettings(const FallbackSettings & settings)169 proto::FallbackSettings ConvertFallbackSettings(
170     const FallbackSettings& settings) {
171   proto::FallbackSettings proto_settings;
172   proto_settings.set_allow_automatic_fallback_on_compilation_error(
173       settings.allow_automatic_fallback_on_compilation_error());
174   proto_settings.set_allow_automatic_fallback_on_execution_error(
175       settings.allow_automatic_fallback_on_execution_error());
176   return proto_settings;
177 }
178 
ConvertNNAPISettings(const NNAPISettings & settings)179 proto::NNAPISettings ConvertNNAPISettings(const NNAPISettings& settings) {
180   proto::NNAPISettings proto_settings;
181   if (settings.accelerator_name() != nullptr) {
182     proto_settings.set_accelerator_name(settings.accelerator_name()->str());
183   }
184   if (settings.cache_directory() != nullptr) {
185     proto_settings.set_cache_directory(settings.cache_directory()->str());
186   }
187   if (settings.model_token() != nullptr) {
188     proto_settings.set_model_token(settings.model_token()->str());
189   }
190   proto_settings.set_execution_preference(
191       ConvertNNAPIExecutionPreference(settings.execution_preference()));
192   proto_settings.set_no_of_nnapi_instances_to_cache(
193       settings.no_of_nnapi_instances_to_cache());
194   if (settings.fallback_settings() != nullptr) {
195     *(proto_settings.mutable_fallback_settings()) =
196         ConvertFallbackSettings(*settings.fallback_settings());
197   }
198   proto_settings.set_allow_nnapi_cpu_on_android_10_plus(
199       settings.allow_nnapi_cpu_on_android_10_plus());
200   proto_settings.set_execution_priority(
201       ConvertNNAPIExecutionPriority(settings.execution_priority()));
202   proto_settings.set_allow_dynamic_dimensions(
203       settings.allow_dynamic_dimensions());
204   proto_settings.set_allow_fp16_precision_for_fp32(
205       settings.allow_fp16_precision_for_fp32());
206   proto_settings.set_use_burst_computation(settings.use_burst_computation());
207   proto_settings.set_support_library_handle(settings.support_library_handle());
208 
209   return proto_settings;
210 }
211 
ConvertGPUSettings(const GPUSettings & settings)212 proto::GPUSettings ConvertGPUSettings(const GPUSettings& settings) {
213   proto::GPUSettings proto_settings;
214   proto_settings.set_is_precision_loss_allowed(
215       settings.is_precision_loss_allowed());
216   proto_settings.set_enable_quantized_inference(
217       settings.enable_quantized_inference());
218   proto_settings.set_force_backend(ConvertGPUBackend(settings.force_backend()));
219   proto_settings.set_inference_priority1(
220       ConvertGPUInferencePriority(settings.inference_priority1()));
221   proto_settings.set_inference_priority2(
222       ConvertGPUInferencePriority(settings.inference_priority2()));
223   proto_settings.set_inference_priority3(
224       ConvertGPUInferencePriority(settings.inference_priority3()));
225   proto_settings.set_inference_preference(
226       ConvertGPUInferenceUsage(settings.inference_preference()));
227   if (settings.cache_directory() != nullptr) {
228     proto_settings.set_cache_directory(settings.cache_directory()->str());
229   }
230   if (settings.model_token() != nullptr) {
231     proto_settings.set_model_token(settings.model_token()->str());
232   }
233   return proto_settings;
234 }
235 
ConvertHexagonSettings(const HexagonSettings & settings)236 proto::HexagonSettings ConvertHexagonSettings(const HexagonSettings& settings) {
237   proto::HexagonSettings proto_settings;
238   proto_settings.set_debug_level(settings.debug_level());
239   proto_settings.set_powersave_level(settings.powersave_level());
240   proto_settings.set_print_graph_profile(settings.print_graph_profile());
241   proto_settings.set_print_graph_debug(settings.print_graph_debug());
242   return proto_settings;
243 }
244 
ConvertXNNPackSettings(const XNNPackSettings & settings)245 proto::XNNPackSettings ConvertXNNPackSettings(const XNNPackSettings& settings) {
246   proto::XNNPackSettings proto_settings;
247   proto_settings.set_num_threads(settings.num_threads());
248   proto_settings.set_flags(::tflite::proto::XNNPackFlags(settings.flags()));
249   return proto_settings;
250 }
251 
ConvertCoreMLSettings(const CoreMLSettings & settings)252 proto::CoreMLSettings ConvertCoreMLSettings(const CoreMLSettings& settings) {
253   proto::CoreMLSettings proto_settings;
254   switch (settings.enabled_devices()) {
255     case CoreMLSettings_::EnabledDevices_DEVICES_ALL:
256       proto_settings.set_enabled_devices(proto::CoreMLSettings::DEVICES_ALL);
257       break;
258     case CoreMLSettings_::EnabledDevices_DEVICES_WITH_NEURAL_ENGINE:
259       proto_settings.set_enabled_devices(
260           proto::CoreMLSettings::DEVICES_WITH_NEURAL_ENGINE);
261       break;
262     default:
263       TFLITE_LOG_PROD(TFLITE_LOG_ERROR, "Invalid devices enum: %d",
264                       settings.enabled_devices());
265   }
266   proto_settings.set_coreml_version(settings.coreml_version());
267   proto_settings.set_max_delegated_partitions(
268       settings.max_delegated_partitions());
269   proto_settings.set_min_nodes_per_partition(
270       settings.min_nodes_per_partition());
271   return proto_settings;
272 }
273 
ConvertCPUSettings(const CPUSettings & settings)274 proto::CPUSettings ConvertCPUSettings(const CPUSettings& settings) {
275   proto::CPUSettings proto_settings;
276   proto_settings.set_num_threads(settings.num_threads());
277   return proto_settings;
278 }
279 
ConvertEdgeTpuDeviceSpec(const EdgeTpuDeviceSpec & device_spec)280 proto::EdgeTpuDeviceSpec ConvertEdgeTpuDeviceSpec(
281     const EdgeTpuDeviceSpec& device_spec) {
282   proto::EdgeTpuDeviceSpec proto_settings;
283 
284   if (device_spec.device_paths() != nullptr) {
285     for (int i = 0; i < device_spec.device_paths()->size(); ++i) {
286       auto device_path = device_spec.device_paths()->Get(i);
287       proto_settings.add_device_paths(device_path->str());
288     }
289   }
290 
291   proto_settings.set_platform_type(
292       static_cast<proto::EdgeTpuDeviceSpec::PlatformType>(
293           device_spec.platform_type()));
294   proto_settings.set_num_chips(device_spec.num_chips());
295   proto_settings.set_chip_family(device_spec.chip_family());
296 
297   return proto_settings;
298 }
299 
ConvertEdgeTpuSettings(const EdgeTpuSettings & settings)300 proto::EdgeTpuSettings ConvertEdgeTpuSettings(const EdgeTpuSettings& settings) {
301   proto::EdgeTpuSettings proto_settings;
302   proto_settings.set_inference_power_state(
303       ConvertEdgeTpuPowerState(settings.inference_power_state()));
304   proto_settings.set_inference_priority(settings.inference_priority());
305   if (settings.model_token() != nullptr) {
306     proto_settings.set_model_token(settings.model_token()->str());
307   }
308   if (settings.edgetpu_device_spec() != nullptr) {
309     *(proto_settings.mutable_edgetpu_device_spec()) =
310         ConvertEdgeTpuDeviceSpec(*settings.edgetpu_device_spec());
311   }
312   proto_settings.set_float_truncation_type(
313       static_cast<proto::EdgeTpuSettings::FloatTruncationType>(
314           settings.float_truncation_type()));
315 
316   auto inactive_powre_configs = settings.inactive_power_configs();
317   if (inactive_powre_configs != nullptr) {
318     for (int i = 0; i < inactive_powre_configs->size(); ++i) {
319       auto config = inactive_powre_configs->Get(i);
320       auto proto_config = proto_settings.add_inactive_power_configs();
321       proto_config->set_inactive_power_state(
322           ConvertEdgeTpuPowerState(config->inactive_power_state()));
323       proto_config->set_inactive_timeout_us(config->inactive_timeout_us());
324     }
325   }
326 
327   return proto_settings;
328 }
329 
ConvertCoralSettings(const CoralSettings & settings)330 proto::CoralSettings ConvertCoralSettings(const CoralSettings& settings) {
331   proto::CoralSettings proto_settings;
332   if (settings.device() != nullptr) {
333     proto_settings.set_device(settings.device()->str());
334   }
335   proto_settings.set_performance(
336       static_cast<proto::CoralSettings::Performance>(settings.performance()));
337   proto_settings.set_usb_always_dfu(settings.usb_always_dfu());
338   proto_settings.set_usb_max_bulk_in_queue_length(
339       settings.usb_max_bulk_in_queue_length());
340   return proto_settings;
341 }
342 
ConvertTfliteSettings(const TFLiteSettings & settings)343 proto::TFLiteSettings ConvertTfliteSettings(const TFLiteSettings& settings) {
344   proto::TFLiteSettings proto_settings;
345   proto_settings.set_delegate(ConvertDelegate(settings.delegate()));
346   if (settings.nnapi_settings() != nullptr) {
347     *(proto_settings.mutable_nnapi_settings()) =
348         ConvertNNAPISettings(*settings.nnapi_settings());
349   }
350   if (settings.gpu_settings() != nullptr) {
351     *(proto_settings.mutable_gpu_settings()) =
352         ConvertGPUSettings(*settings.gpu_settings());
353   }
354   if (settings.hexagon_settings() != nullptr) {
355     *(proto_settings.mutable_hexagon_settings()) =
356         ConvertHexagonSettings(*settings.hexagon_settings());
357   }
358 
359   if (settings.xnnpack_settings() != nullptr) {
360     *(proto_settings.mutable_xnnpack_settings()) =
361         ConvertXNNPackSettings(*settings.xnnpack_settings());
362   }
363 
364   if (settings.coreml_settings() != nullptr) {
365     *(proto_settings.mutable_coreml_settings()) =
366         ConvertCoreMLSettings(*settings.coreml_settings());
367   }
368 
369   if (settings.cpu_settings() != nullptr) {
370     *(proto_settings.mutable_cpu_settings()) =
371         ConvertCPUSettings(*settings.cpu_settings());
372   }
373 
374   proto_settings.set_max_delegated_partitions(
375       settings.max_delegated_partitions());
376   if (settings.edgetpu_settings() != nullptr) {
377     *(proto_settings.mutable_edgetpu_settings()) =
378         ConvertEdgeTpuSettings(*settings.edgetpu_settings());
379   }
380   if (settings.coral_settings() != nullptr) {
381     *(proto_settings.mutable_coral_settings()) =
382         ConvertCoralSettings(*settings.coral_settings());
383   }
384   if (settings.fallback_settings() != nullptr) {
385     *(proto_settings.mutable_fallback_settings()) =
386         ConvertFallbackSettings(*settings.fallback_settings());
387   }
388   proto_settings.set_disable_default_delegates(
389       settings.disable_default_delegates());
390   return proto_settings;
391 }
392 
ConvertModelFile(const ModelFile & model_file)393 proto::ModelFile ConvertModelFile(const ModelFile& model_file) {
394   proto::ModelFile proto_settings;
395   if (model_file.filename() != nullptr) {
396     proto_settings.set_filename(model_file.filename()->str());
397   }
398   proto_settings.set_fd(model_file.fd());
399   proto_settings.set_offset(model_file.offset());
400   proto_settings.set_length(model_file.length());
401   return proto_settings;
402 }
403 
ConvertBenchmarkStoragePaths(const BenchmarkStoragePaths & storage_paths)404 proto::BenchmarkStoragePaths ConvertBenchmarkStoragePaths(
405     const BenchmarkStoragePaths& storage_paths) {
406   proto::BenchmarkStoragePaths proto_settings;
407   if (storage_paths.storage_file_path() != nullptr) {
408     proto_settings.set_storage_file_path(
409         storage_paths.storage_file_path()->str());
410   }
411   if (storage_paths.data_directory_path() != nullptr) {
412     proto_settings.set_data_directory_path(
413         storage_paths.data_directory_path()->str());
414   }
415   return proto_settings;
416 }
417 
ConvertMinibenchmarkSettings(const MinibenchmarkSettings & settings)418 proto::MinibenchmarkSettings ConvertMinibenchmarkSettings(
419     const MinibenchmarkSettings& settings) {
420   proto::MinibenchmarkSettings proto_settings;
421   if (settings.settings_to_test() != nullptr &&
422       settings.settings_to_test()->size() > 0) {
423     for (int i = 0; i < settings.settings_to_test()->size(); ++i) {
424       auto tflite_setting = settings.settings_to_test()->Get(i);
425       auto proto_tflite_setting = proto_settings.add_settings_to_test();
426       *proto_tflite_setting = ConvertTfliteSettings(*tflite_setting);
427     }
428   }
429   if (settings.model_file() != nullptr) {
430     *(proto_settings.mutable_model_file()) =
431         ConvertModelFile(*settings.model_file());
432   }
433   if (settings.storage_paths() != nullptr) {
434     *(proto_settings.mutable_storage_paths()) =
435         ConvertBenchmarkStoragePaths(*settings.storage_paths());
436   }
437   return proto_settings;
438 }
439 
ConvertBenchmarkEventType(BenchmarkEventType type)440 proto::BenchmarkEventType ConvertBenchmarkEventType(BenchmarkEventType type) {
441   switch (type) {
442     case BenchmarkEventType_UNDEFINED_BENCHMARK_EVENT_TYPE:
443       return proto::BenchmarkEventType::UNDEFINED_BENCHMARK_EVENT_TYPE;
444     case BenchmarkEventType_START:
445       return proto::BenchmarkEventType::START;
446     case BenchmarkEventType_END:
447       return proto::BenchmarkEventType::END;
448     case BenchmarkEventType_ERROR:
449       return proto::BenchmarkEventType::ERROR;
450     case BenchmarkEventType_LOGGED:
451       return proto::BenchmarkEventType::LOGGED;
452     case BenchmarkEventType_RECOVERED_ERROR:
453       return proto::BenchmarkEventType::RECOVERED_ERROR;
454   }
455   TFLITE_LOG_PROD(TFLITE_LOG_ERROR,
456                   "Unexpected value for BenchmarkEventType: %d", type);
457   return proto::BenchmarkEventType::UNDEFINED_BENCHMARK_EVENT_TYPE;
458 }
459 
ConvertBenchmarkMetric(const BenchmarkMetric & metric)460 proto::BenchmarkMetric ConvertBenchmarkMetric(const BenchmarkMetric& metric) {
461   proto::BenchmarkMetric proto_metric;
462   if (metric.name() != nullptr) {
463     proto_metric.set_name(metric.name()->str());
464   }
465   auto values = metric.values();
466   if (values != nullptr) {
467     for (int i = 0; i < values->size(); ++i) {
468       proto_metric.add_values(values->Get(i));
469     }
470   }
471   return proto_metric;
472 }
473 
ConvertBenchmarkResult(const BenchmarkResult & result)474 proto::BenchmarkResult ConvertBenchmarkResult(const BenchmarkResult& result) {
475   proto::BenchmarkResult proto_result;
476   auto initialization_time_us = result.initialization_time_us();
477   if (initialization_time_us != nullptr) {
478     for (int i = 0; i < initialization_time_us->size(); ++i) {
479       proto_result.add_initialization_time_us(initialization_time_us->Get(i));
480     }
481   }
482   auto inference_time_us = result.inference_time_us();
483   if (inference_time_us != nullptr) {
484     for (int i = 0; i < inference_time_us->size(); ++i) {
485       proto_result.add_inference_time_us(inference_time_us->Get(i));
486     }
487   }
488   proto_result.set_max_memory_kb(result.max_memory_kb());
489   proto_result.set_ok(result.ok());
490   auto metrics = result.metrics();
491   if (metrics != nullptr) {
492     for (int i = 0; i < metrics->size(); ++i) {
493       *proto_result.add_metrics() = ConvertBenchmarkMetric(*metrics->Get(i));
494     }
495   }
496   return proto_result;
497 }
498 
ConvertBenchmarkStage(BenchmarkStage stage)499 proto::BenchmarkStage ConvertBenchmarkStage(BenchmarkStage stage) {
500   switch (stage) {
501     case BenchmarkStage_UNKNOWN:
502       return proto::BenchmarkStage::UNKNOWN;
503     case BenchmarkStage_INITIALIZATION:
504       return proto::BenchmarkStage::INITIALIZATION;
505     case BenchmarkStage_INFERENCE:
506       return proto::BenchmarkStage::INFERENCE;
507   }
508   TFLITE_LOG_PROD(TFLITE_LOG_ERROR, "Unexpected value for BenchmarkStage: %d",
509                   stage);
510   return proto::BenchmarkStage::UNKNOWN;
511 }
512 
ConvertBenchmarkErrorCode(const ErrorCode & code)513 proto::ErrorCode ConvertBenchmarkErrorCode(const ErrorCode& code) {
514   proto::ErrorCode proto_code;
515   proto_code.set_source(ConvertDelegate(code.source()));
516   proto_code.set_tflite_error(code.tflite_error());
517   proto_code.set_underlying_api_error(code.underlying_api_error());
518   return proto_code;
519 }
520 
ConvertBenchmarkError(const BenchmarkError & error)521 proto::BenchmarkError ConvertBenchmarkError(const BenchmarkError& error) {
522   proto::BenchmarkError proto_error;
523   proto_error.set_stage(ConvertBenchmarkStage(error.stage()));
524   proto_error.set_exit_code(error.exit_code());
525   proto_error.set_signal(error.signal());
526   auto error_codes = error.error_code();
527   if (error_codes != nullptr) {
528     for (int i = 0; i < error_codes->size(); ++i) {
529       *proto_error.add_error_code() =
530           ConvertBenchmarkErrorCode(*error_codes->Get(i));
531     }
532   }
533   proto_error.set_mini_benchmark_error_code(error.mini_benchmark_error_code());
534   return proto_error;
535 }
536 
ConvertBenchmarkEvent(const BenchmarkEvent & event)537 proto::BenchmarkEvent ConvertBenchmarkEvent(const BenchmarkEvent& event) {
538   proto::BenchmarkEvent proto_event;
539   if (event.tflite_settings() != nullptr) {
540     *proto_event.mutable_tflite_settings() =
541         ConvertTfliteSettings(*event.tflite_settings());
542   }
543   proto_event.set_event_type(ConvertBenchmarkEventType(event.event_type()));
544   if (event.result() != nullptr) {
545     *proto_event.mutable_result() = ConvertBenchmarkResult(*event.result());
546   }
547   if (event.error() != nullptr) {
548     *proto_event.mutable_error() = ConvertBenchmarkError(*event.error());
549   }
550   proto_event.set_boottime_us(event.boottime_us());
551   proto_event.set_wallclock_us(event.wallclock_us());
552   return proto_event;
553 }
554 
ConvertBestAccelerationDecision(const BestAccelerationDecision & decision)555 proto::BestAccelerationDecision ConvertBestAccelerationDecision(
556     const BestAccelerationDecision& decision) {
557   proto::BestAccelerationDecision proto_decision;
558   proto_decision.set_number_of_source_events(
559       decision.number_of_source_events());
560   if (decision.min_latency_event() != nullptr) {
561     *proto_decision.mutable_min_latency_event() =
562         ConvertBenchmarkEvent(*decision.min_latency_event());
563   }
564   proto_decision.set_min_inference_time_us(decision.min_inference_time_us());
565   return proto_decision;
566 }
567 
ConvertBenchmarkInitializationFailure(const BenchmarkInitializationFailure & init_failure)568 proto::BenchmarkInitializationFailure ConvertBenchmarkInitializationFailure(
569     const BenchmarkInitializationFailure& init_failure) {
570   proto::BenchmarkInitializationFailure proto_init_failure;
571   proto_init_failure.set_initialization_status(
572       init_failure.initialization_status());
573   return proto_init_failure;
574 }
575 
576 }  // namespace
577 
ConvertFromFlatbuffer(const ComputeSettings & settings,bool skip_mini_benchmark_settings)578 proto::ComputeSettings ConvertFromFlatbuffer(
579     const ComputeSettings& settings, bool skip_mini_benchmark_settings) {
580   proto::ComputeSettings proto_settings;
581 
582   proto_settings.set_preference(
583       ConvertExecutionPreference(settings.preference()));
584   if (settings.tflite_settings() != nullptr) {
585     *(proto_settings.mutable_tflite_settings()) =
586         ConvertTfliteSettings(*settings.tflite_settings());
587   }
588   if (settings.model_namespace_for_statistics() != nullptr) {
589     proto_settings.set_model_namespace_for_statistics(
590         settings.model_namespace_for_statistics()->str());
591   }
592   if (settings.model_identifier_for_statistics() != nullptr) {
593     proto_settings.set_model_identifier_for_statistics(
594         settings.model_identifier_for_statistics()->str());
595   }
596   if (!skip_mini_benchmark_settings &&
597       settings.settings_to_test_locally() != nullptr) {
598     *(proto_settings.mutable_settings_to_test_locally()) =
599         ConvertMinibenchmarkSettings(*settings.settings_to_test_locally());
600   }
601 
602   return proto_settings;
603 }
604 
ConvertFromFlatbuffer(const ComputeSettingsT & settings,bool skip_mini_benchmark_settings)605 proto::ComputeSettings ConvertFromFlatbuffer(
606     const ComputeSettingsT& settings, bool skip_mini_benchmark_settings) {
607   flatbuffers::FlatBufferBuilder fbb;
608   fbb.Finish(ComputeSettings::Pack(fbb, &settings));
609   auto settings_fbb =
610       flatbuffers::GetRoot<ComputeSettings>(fbb.GetBufferPointer());
611   return ConvertFromFlatbuffer(*settings_fbb, skip_mini_benchmark_settings);
612 }
613 
ConvertFromFlatbuffer(const MiniBenchmarkEvent & event)614 proto::MiniBenchmarkEvent ConvertFromFlatbuffer(
615     const MiniBenchmarkEvent& event) {
616   proto::MiniBenchmarkEvent proto_event;
617   proto_event.set_is_log_flushing_event(event.is_log_flushing_event());
618   if (event.best_acceleration_decision() != nullptr) {
619     *proto_event.mutable_best_acceleration_decision() =
620         ConvertBestAccelerationDecision(*event.best_acceleration_decision());
621   }
622   if (event.initialization_failure() != nullptr) {
623     *proto_event.mutable_initialization_failure() =
624         ConvertBenchmarkInitializationFailure(*event.initialization_failure());
625   }
626 
627   if (event.benchmark_event() != nullptr) {
628     *proto_event.mutable_benchmark_event() =
629         ConvertBenchmarkEvent(*event.benchmark_event());
630   }
631 
632   return proto_event;
633 }
634 
ConvertFromFlatbuffer(const MiniBenchmarkEventT & event)635 proto::MiniBenchmarkEvent ConvertFromFlatbuffer(
636     const MiniBenchmarkEventT& event) {
637   flatbuffers::FlatBufferBuilder fbb;
638   fbb.Finish(MiniBenchmarkEvent::Pack(fbb, &event));
639   auto event_fbb =
640       flatbuffers::GetRoot<MiniBenchmarkEvent>(fbb.GetBufferPointer());
641   return ConvertFromFlatbuffer(*event_fbb);
642 }
643 }  // namespace tflite
644