EyeAI
Loading...
Searching...
No Matches
TfLiteUtils.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <memory>
6#include <optional>
7#include <span>
8#include <string_view>
9#if EYE_AI_CORE_USE_PREBUILT_TFLITE
10#include <tflite/c/c_api.h>
11#include <tflite/delegates/gpu/delegate.h>
12#else
13#include <tensorflow/lite/c/c_api.h>
14#include <tensorflow/lite/delegates/gpu/delegate.h>
15#endif
16
17class ProfilingFrame;
18
19std::string_view format_tflite_type(TfLiteType type);
20
22std::optional<size_t> get_tflite_type_size(TfLiteType type);
23
24std::string_view format_tflite_status(TfLiteStatus status);
25
28[[nodiscard]] std::optional<TfLiteAffineQuantization>
29get_tensor_quantization(const TfLiteTensor* tensor);
30
31[[nodiscard]] std::span<const int> get_tensor_shape(const TfLiteTensor* tensor);
32
40[[nodiscard]] std::
41 unique_ptr<TfLiteDelegate, decltype(&TfLiteGpuDelegateV2Delete)>
43 std::string_view gpu_delegate_serialization_dir,
44 std::string_view model_token,
45 ProfilingFrame& profiling_frame
46 );
47
53
55[[nodiscard]] std::unique_ptr<TfLiteDelegate, void (*)(TfLiteDelegate*)>
56
57
59 std::string_view delegate_serialization_dir,
60 std::string_view model_token,
61 NpuConfiguration config,
62 std::string_view skel_library_dir
63);
64
67 public:
68 enum Type : uint8_t { Input, Output } type;
69
70 TensorType() = delete;
72
73 [[nodiscard]] std::string_view to_string() const;
74 bool operator==(const TensorType& other) const = default;
75};
76
77struct [[nodiscard]] TfLiteNonFloatTensorTypeError {
80
81 [[nodiscard]] std::string to_string() const;
82 bool operator==(const TfLiteNonFloatTensorTypeError& other) const = default;
83};
84
85struct [[nodiscard]] TfLiteTensorsNotCreatedError {
87
88 [[nodiscard]] std::string to_string() const;
89 bool operator==(const TfLiteTensorsNotCreatedError& other) const = default;
90};
91
96
97 [[nodiscard]] std::string to_string() const;
98 bool
99 operator==(const TfLiteTensorElementCountMismatch& other) const = default;
100};
101
102struct [[nodiscard]] TfLiteCopyFromInputTensorError {
103 TfLiteStatus status;
104
105 [[nodiscard]] std::string to_string() const;
106 bool
107 operator==(const TfLiteCopyFromInputTensorError& other) const = default;
108};
109
111 TfLiteLoadNonQuantizedInputError,
116);
118 TfLiteType quantized_type;
119
120 [[nodiscard]] std::string to_string() const;
122 default;
123};
124struct [[nodiscard]] QuantizationElementsMismatch {
127
128 [[nodiscard]] std::string to_string() const;
129 bool operator==(const QuantizationElementsMismatch& other) const = default;
130};
131struct [[nodiscard]] AsymmetricQuantizationError {
132 [[nodiscard]] static std::string to_string();
133 bool operator==(const AsymmetricQuantizationError& other) const = default;
134};
135struct [[nodiscard]] InvalidQuantizedType {
136 TfLiteType quantized_type;
137
138 [[nodiscard]] std::string to_string() const;
139 bool operator==(const InvalidQuantizedType& other) const = default;
140};
142 QuantizeFloatError,
146);
148 TfLiteLoadQuantizedInputError,
152 QuantizeFloatError
153);
155 TfLiteLoadInputError,
156 TfLiteLoadNonQuantizedInputError,
157 TfLiteLoadQuantizedInputError
158);
159
161[[nodiscard]] std::optional<TfLiteLoadInputError> load_input_tensor_with_floats(
162 TfLiteTensor* input_tensor,
163 std::span<const float> values,
164 ProfilingFrame& profiling_frame
165);
166
167struct [[nodiscard]] TfLiteCopyToOutputTensorError {
168 TfLiteStatus status;
169
170 [[nodiscard]] std::string to_string() const;
171 bool operator==(const TfLiteCopyToOutputTensorError& other) const = default;
172};
173
175 TfLiteReadNonQuantizedOutputError,
179);
181 DequantizeFloatError,
185);
187 TfLiteReadQuantizedOutputError,
190 DequantizeFloatError
191);
193 TfLiteReadOutputError,
194 TfLiteReadNonQuantizedOutputError,
195 TfLiteReadQuantizedOutputError
196);
197
199[[nodiscard]] std::optional<TfLiteReadOutputError>
201 const TfLiteTensor* output_tensor,
202 std::span<float> output,
203 ProfilingFrame& profiling_frame
204);
205
206struct [[nodiscard]] TfLiteCreateInterpreterError {
207 [[nodiscard]] static std::string to_string();
208 bool operator==(const TfLiteCreateInterpreterError& other) const = default;
209};
210
211struct [[nodiscard]] TfLiteAllocateTensorsError {
212 TfLiteStatus status;
213
214 [[nodiscard]] std::string to_string() const;
215 bool operator==(const TfLiteAllocateTensorsError& other) const = default;
216};
217
219 TfLiteCreateRuntimeError,
222);
223
224struct [[nodiscard]] TfLiteInvokeInterpreterError {
225 TfLiteStatus status;
226
227 [[nodiscard]] std::string to_string() const;
228 bool operator==(const TfLiteInvokeInterpreterError& other) const = default;
229};
230
231struct [[nodiscard]] InvalidInputFormatForModel {
234
235 [[nodiscard]] std::string to_string() const;
236 bool operator==(const InvalidInputFormatForModel& other) const = default;
237};
238
239struct [[nodiscard]] InvalidOutputFormatForModel {
242
243 [[nodiscard]] std::string to_string() const;
244 bool operator==(const InvalidOutputFormatForModel& other) const = default;
245};
246
248 TfLiteRunInferenceError,
249 TfLiteLoadInputError,
251 TfLiteReadOutputError,
254);
#define COMBINED_ERROR(name,...)
Definition Errors.hpp:88
FloatTensorFormat
Definition TensorBuffer.hpp:73
std::optional< size_t > get_tflite_type_size(TfLiteType type)
Definition TfLiteUtils.cpp:548
std::optional< TfLiteAffineQuantization > get_tensor_quantization(const TfLiteTensor *tensor)
Definition TfLiteUtils.cpp:25
std::unique_ptr< TfLiteDelegate, void(*)(TfLiteDelegate *)> create_qnn_npu_delegate(std::string_view delegate_serialization_dir, std::string_view model_token, NpuConfiguration config, std::string_view skel_library_dir)
Definition TfLiteUtils.cpp:69
std::optional< TfLiteReadOutputError > read_floats_from_output_tensor(const TfLiteTensor *output_tensor, std::span< float > output, ProfilingFrame &profiling_frame)
reads floats array from output tensor, supports quantization
Definition TfLiteUtils.cpp:297
std::unique_ptr< TfLiteDelegate, decltype(&TfLiteGpuDelegateV2Delete)> create_gpu_delegate(std::string_view gpu_delegate_serialization_dir, std::string_view model_token, ProfilingFrame &profiling_frame)
Definition TfLiteUtils.cpp:42
std::string_view format_tflite_type(TfLiteType type)
Definition TfLiteUtils.cpp:474
std::span< const int > get_tensor_shape(const TfLiteTensor *tensor)
Definition TfLiteUtils.cpp:34
std::optional< TfLiteLoadInputError > load_input_tensor_with_floats(TfLiteTensor *input_tensor, std::span< const float > values, ProfilingFrame &profiling_frame)
loads input tensor with floats array, supports quantization
Definition TfLiteUtils.cpp:203
NpuConfiguration
Definition TfLiteUtils.hpp:48
@ rel2abs
Definition TfLiteUtils.hpp:50
@ MiDaS
Definition TfLiteUtils.hpp:49
@ Yolo
Definition TfLiteUtils.hpp:51
std::string_view format_tflite_status(TfLiteStatus status)
Definition TfLiteUtils.cpp:521
collection of profile records from different threads (lock-free thread-safe)
Definition Profiling.hpp:52
either a input or a output tensor
Definition TfLiteUtils.hpp:66
Type
Definition TfLiteUtils.hpp:68
@ Input
Definition TfLiteUtils.hpp:68
@ Output
Definition TfLiteUtils.hpp:68
TensorType()=delete
enum TensorType::Type type
bool operator==(const TensorType &other) const =default
std::string_view to_string() const
Definition TfLiteUtils.cpp:441
TensorType(Type type)
Definition TfLiteUtils.hpp:71
Definition TfLiteUtils.hpp:131
bool operator==(const AsymmetricQuantizationError &other) const =default
static std::string to_string()
Definition TfLiteUtils.cpp:430
Definition TfLiteUtils.hpp:117
TfLiteType quantized_type
Definition TfLiteUtils.hpp:118
bool operator==(const InvalidFloat32QuantizationTypeError &other) const =default
std::string to_string() const
Definition TfLiteUtils.cpp:415
Definition TfLiteUtils.hpp:231
FloatTensorFormat provided
Definition TfLiteUtils.hpp:232
std::string to_string() const
Definition TfLiteUtils.cpp:458
bool operator==(const InvalidInputFormatForModel &other) const =default
FloatTensorFormat expected
Definition TfLiteUtils.hpp:233
Definition TfLiteUtils.hpp:239
bool operator==(const InvalidOutputFormatForModel &other) const =default
std::string to_string() const
Definition TfLiteUtils.cpp:466
FloatTensorFormat expected
Definition TfLiteUtils.hpp:241
FloatTensorFormat provided
Definition TfLiteUtils.hpp:240
Definition TfLiteUtils.hpp:135
TfLiteType quantized_type
Definition TfLiteUtils.hpp:136
std::string to_string() const
Definition TfLiteUtils.cpp:434
bool operator==(const InvalidQuantizedType &other) const =default
Definition TfLiteUtils.hpp:124
size_t input_elements
Definition TfLiteUtils.hpp:125
std::string to_string() const
Definition TfLiteUtils.cpp:422
bool operator==(const QuantizationElementsMismatch &other) const =default
size_t quantized_out_elements
Definition TfLiteUtils.hpp:126
Definition TfLiteUtils.hpp:211
std::string to_string() const
Definition TfLiteRuntime.cpp:243
bool operator==(const TfLiteAllocateTensorsError &other) const =default
TfLiteStatus status
Definition TfLiteUtils.hpp:212
Definition TfLiteUtils.hpp:102
TfLiteStatus status
Definition TfLiteUtils.hpp:103
bool operator==(const TfLiteCopyFromInputTensorError &other) const =default
std::string to_string() const
Definition TfLiteUtils.cpp:408
Definition TfLiteUtils.hpp:167
std::string to_string() const
Definition TfLiteUtils.cpp:452
TfLiteStatus status
Definition TfLiteUtils.hpp:168
bool operator==(const TfLiteCopyToOutputTensorError &other) const =default
Definition TfLiteUtils.hpp:206
static std::string to_string()
Definition TfLiteRuntime.cpp:238
bool operator==(const TfLiteCreateInterpreterError &other) const =default
Definition TfLiteUtils.hpp:224
TfLiteStatus status
Definition TfLiteUtils.hpp:225
bool operator==(const TfLiteInvokeInterpreterError &other) const =default
std::string to_string() const
Definition TfLiteRuntime.cpp:249
Definition TfLiteUtils.hpp:77
std::string to_string() const
Definition TfLiteUtils.cpp:389
bool operator==(const TfLiteNonFloatTensorTypeError &other) const =default
TfLiteType tensor_element_type
Definition TfLiteUtils.hpp:79
TensorType tensor_type
Definition TfLiteUtils.hpp:78
Definition TfLiteUtils.hpp:92
size_t expected_elements
Definition TfLiteUtils.hpp:95
bool operator==(const TfLiteTensorElementCountMismatch &other) const =default
TensorType tensor_type
Definition TfLiteUtils.hpp:93
std::string to_string() const
Definition TfLiteUtils.cpp:400
size_t provided_elements
Definition TfLiteUtils.hpp:94
Definition TfLiteUtils.hpp:85
TensorType tensor_type
Definition TfLiteUtils.hpp:86
std::string to_string() const
Definition TfLiteUtils.cpp:396
bool operator==(const TfLiteTensorsNotCreatedError &other) const =default