xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/tools/evaluation/stages/inference_profiler_stage.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 #ifndef TENSORFLOW_LITE_TOOLS_EVALUATION_STAGES_INFERENCE_PROFILER_STAGE_H_
16 #define TENSORFLOW_LITE_TOOLS_EVALUATION_STAGES_INFERENCE_PROFILER_STAGE_H_
17 
18 #include <stdint.h>
19 
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include "tensorflow/core/util/stats_calculator.h"
25 #include "tensorflow/lite/tools/evaluation/evaluation_delegate_provider.h"
26 #include "tensorflow/lite/tools/evaluation/evaluation_stage.h"
27 #include "tensorflow/lite/tools/evaluation/proto/evaluation_config.pb.h"
28 #include "tensorflow/lite/tools/evaluation/stages/tflite_inference_stage.h"
29 
30 namespace tflite {
31 namespace evaluation {
32 
33 // An EvaluationStage to profile a custom TFLite inference config by comparing
34 // performance in two settings:
35 // 1. User-defined TfliteInferenceParams (The 'test' setting)
36 // 2. Default TfliteInferenceParams (The 'reference' setting)
37 // The latter essentially implies single-threaded CPU execution.
38 class InferenceProfilerStage : public EvaluationStage {
39  public:
InferenceProfilerStage(const EvaluationStageConfig & config)40   explicit InferenceProfilerStage(const EvaluationStageConfig& config)
41       : EvaluationStage(config) {}
42 
Init()43   TfLiteStatus Init() override { return Init(nullptr); }
44   TfLiteStatus Init(const DelegateProviders* delegate_providers);
45 
46   // New Gaussian random data is used as input for each Run.
47   TfLiteStatus Run() override;
48 
49   EvaluationStageMetrics LatestMetrics() override;
50 
51  private:
52   std::unique_ptr<TfliteInferenceStage> reference_stage_;
53   std::unique_ptr<TfliteInferenceStage> test_stage_;
54 
55   const TfLiteModelInfo* model_info_;
56   std::vector<int64_t> input_num_elements_;
57   std::vector<int64_t> output_num_elements_;
58 
59   // One Stat for each model output.
60   std::vector<tensorflow::Stat<float>> error_stats_;
61 
62   // One of the following 3 will be populated based on model_input_type_, and
63   // used as the input for the underlying model.
64   std::vector<std::vector<float>> float_tensors_;
65   std::vector<std::vector<int8_t>> int8_tensors_;
66   std::vector<std::vector<uint8_t>> uint8_tensors_;
67 };
68 
69 }  // namespace evaluation
70 }  // namespace tflite
71 
72 #endif  // TENSORFLOW_LITE_TOOLS_EVALUATION_STAGES_INFERENCE_PROFILER_STAGE_H_
73