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_SUPPORT_CC_COMMON_H_
17 #define TENSORFLOW_LITE_SUPPORT_CC_COMMON_H_
18 
19 #include "absl/status/status.h"
20 #include "absl/strings/string_view.h"
21 
22 namespace tflite {
23 namespace support {
24 
25 // Name (aka type URL key) of the `absl::Status` payload which contains a
26 // stringified `TfLiteSupportStatus` code (see below).
27 constexpr absl::string_view kTfLiteSupportPayload =
28     "tflite::support::TfLiteSupportStatus";
29 
30 // Error codes for TensorFlow Lite Support (TFLS) C++ APIs.
31 //
32 // Such codes capture errors encountered in the TFLS layer. They complement all
33 // the other type of errors that occur in the lower-level TF Lite codebase (see
34 // `TfLiteStatus` codes).
35 //
36 // At runtime, such codes are meant to be attached (where applicable) to a
37 // `absl::Status` in a key-value manner with `kTfLiteSupportPayload` as key and
38 // stringifed error code as value (aka payload). This logic is encapsulated in
39 // the `CreateStatusWithPayload` helper below for convenience.
40 //
41 // The returned status includes:
42 // 1. The canonical error code (INVALID_ARGUMENT)
43 // 2. The fine-grained error message ("Invalid metadata ...")
44 // 3. The specific TFLS code as a payload (kMetadataInvalidSchemaVersionError)
45 enum class TfLiteSupportStatus {
46   // Generic error codes.
47 
48   // Success.
49   kOk = 0,
50   // Unspecified error.
51   kError = 1,
52   // Invalid argument specified.
53   kInvalidArgumentError = 2,
54   // Invalid FlatBuffer file or buffer specified.
55   kInvalidFlatBufferError = 3,
56 
57   // File I/O error codes.
58 
59   // No such file.
60   kFileNotFoundError = 100,
61   // Permission issue.
62   kFilePermissionDeniedError,
63   // I/O error when reading file.
64   kFileReadError,
65   // I/O error when mmap-ing file.
66   kFileMmapError,
67 
68   // TensorFlow Lite metadata error codes.
69 
70   // Unexpected schema version (aka file_identifier) in the Metadata FlatBuffer.
71   kMetadataInvalidSchemaVersionError = 200,
72   // No such associated file within metadata, or file has not been packed.
73   kMetadataAssociatedFileNotFoundError,
74   // ZIP I/O error when unpacking an associated file.
75   kMetadataAssociatedFileZipError,
76   // Inconsistency error between the metadata and actual TF Lite model.
77   // E.g.: number of labels and output tensor values differ.
78   kMetadataInconsistencyError,
79   // Invalid process units specified.
80   // E.g.: multiple ProcessUnits with the same type for a given tensor.
81   kMetadataInvalidProcessUnitsError,
82   // Inconsistency error with the number of labels.
83   // E.g.: label files for different locales have a different number of labels.
84   kMetadataNumLabelsMismatchError,
85   // Score calibration parameters parsing error.
86   // E.g.: too many parameters provided in the corresponding associated file.
87   kMetadataMalformedScoreCalibrationError,
88   // Unexpected number of subgraphs for the current task.
89   // E.g.: image classification expects a single subgraph.
90   kMetadataInvalidNumSubgraphsError,
91   // A given tensor requires NormalizationOptions but none were found.
92   // E.g.: float input tensor requires normalization to preprocess input images.
93   kMetadataMissingNormalizationOptionsError,
94   // Invalid ContentProperties specified.
95   // E.g. expected ImageProperties, got BoundingBoxProperties.
96   kMetadataInvalidContentPropertiesError,
97   // Metadata is mandatory but was not found.
98   // E.g. current task requires TFLite Model Metadata but none was found.
99   kMetadataNotFoundError,
100   // Associated TENSOR_AXIS_LABELS or TENSOR_VALUE_LABELS file is mandatory but
101   // none was found or it was empty.
102   // E.g. current task requires labels but none were found.
103   kMetadataMissingLabelsError,
104   // The ProcessingUnit for tokenizer is not correctly configured.
105   // E.g BertTokenizer doesn't have a valid vocab file associated.
106   kMetadataInvalidTokenizerError,
107 
108   // Input tensor(s) error codes.
109 
110   // Unexpected number of input tensors for the current task.
111   // E.g. current task expects a single input tensor.
112   kInvalidNumInputTensorsError = 300,
113   // Unexpected input tensor dimensions for the current task.
114   // E.g.: only 4D input tensors supported.
115   kInvalidInputTensorDimensionsError,
116   // Unexpected input tensor type for the current task.
117   // E.g.: current task expects a uint8 pixel image as input.
118   kInvalidInputTensorTypeError,
119   // Unexpected input tensor bytes size.
120   // E.g.: size in bytes does not correspond to the expected number of pixels.
121   kInvalidInputTensorSizeError,
122   // No correct input tensor found for the model.
123   // E.g.: input tensor name is not part of the text model's input tensors.
124   kInputTensorNotFoundError,
125 
126   // Output tensor(s) error codes.
127 
128   // Unexpected output tensor dimensions for the current task.
129   // E.g.: only a batch size of 1 is supported.
130   kInvalidOutputTensorDimensionsError = 400,
131   // Unexpected input tensor type for the current task.
132   // E.g.: multi-head model with different output tensor types.
133   kInvalidOutputTensorTypeError,
134   // No correct output tensor found for the model.
135   // E.g.: output tensor name is not part of the text model's output tensors.
136   kOutputTensorNotFoundError,
137   // Unexpected number of output tensors for the current task.
138   // E.g.: current task expects a single output tensor.
139   kInvalidNumOutputTensorsError,
140 
141   // Image processing error codes.
142 
143   // Unspecified image processing failures.
144   kImageProcessingError = 500,
145   // Unexpected input or output buffer metadata.
146   // E.g.: rotate RGBA buffer to Grayscale buffer by 90 degrees.
147   kImageProcessingInvalidArgumentError,
148   // Image processing operation failures.
149   // E.g. libyuv rotation failed for an unknown reason.
150   kImageProcessingBackendError,
151 };
152 
153 // Convenience helper to create an `absl::Status` augmented with the
154 // fine-grained `tfls_code` attached as payload under the
155 // `kTfLiteSupportPayload` type URL key.
156 //
157 // This should only be used for non-ok codes since otherwise it does nothing
158 // more than returning an object identical to an OK status. See `absl::Status`
159 // for more details.
160 absl::Status CreateStatusWithPayload(
161     absl::StatusCode canonical_code, absl::string_view message,
162     tflite::support::TfLiteSupportStatus tfls_code =
163         tflite::support::TfLiteSupportStatus::kError);
164 
165 }  // namespace support
166 }  // namespace tflite
167 #endif  // TENSORFLOW_LITE_SUPPORT_CC_COMMON_H_
168