Skip to content

Commit 442eac6

Browse files
committed
Added mapping between absl status code and TfliteSupportErrorCode when payload can't be converted to a valid TfLiteSupportStatus
1 parent e7c43c5 commit 442eac6

File tree

2 files changed

+73
-23
lines changed

2 files changed

+73
-23
lines changed

tensorflow_lite_support/c/common.h

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,22 @@ extern "C" {
2424

2525
// Error codes for TensorFlow Lite Task Library C APIs.
2626
//
27-
// One to one mapping with `TfLiteSupportStatus` code starting from kError = 1.
28-
// Omits `kOk` since `TfLiteErrorCode` is only to be used in the event of an
29-
// error and does not account for success unlike `TfLiteSupportStatus`. In case
30-
// of success, TensorFlow Lite Task Library C APIs return the appropriate return
31-
// value and a null error. One to one mapping makes it easier to convert between
32-
// `TfLiteSupportStatus` and `TfLiteSupportErrorCode` without long switch statements.
33-
// kErrorCodeFirst and kErrorCodeLast are also provided for safety checks during
34-
// conversion. In case of modifications in error codes, ensure that
35-
// kErrorCodeFirst is set to the least enum value and kErrorCodeLast is set to
36-
// the greatest enum value.
27+
// Holds one to one mapping with `TfLiteSupportStatus` code starting from kError
28+
// = 1. Omits `kOk` since `TfLiteErrorCode` is only to be used in the event of
29+
// an error and does not account for success unlike `TfLiteSupportStatus`. In
30+
// case of success, TensorFlow Lite Task Library C APIs return the appropriate
31+
// return value and a null error. One to one mapping makes it easier to convert
32+
// between `TfLiteSupportStatus` and `TfLiteSupportErrorCode` without long
33+
// switch statements.
34+
//
35+
// Also holds error codes mapping to absl::Status::code() starting from
36+
// kNotFound = 900 in cases where the absl::Status payload can't
37+
// be mapped to a `TfLiteSupportStatus` code. kErrorCodeFirst and kErrorCodeLast
38+
// are also provided for safety checks during conversion between
39+
// `TfLiteSupportStatus` and `TfLiteSupportErrorCode`. In case of modifications
40+
// in error codes, ensure that kErrorCodeFirst and kErrorCodeLast is
41+
// respectively, set to the least and greatest enum value amongst the error
42+
// codes mapping to TfLiteSupportStatus.
3743
enum TfLiteSupportErrorCode {
3844
// Unspecified error.
3945
kError = 1,
@@ -144,17 +150,43 @@ enum TfLiteSupportErrorCode {
144150
kImageProcessingBackendError,
145151

146152
// Convenience error codes for condition checks during type casting.
147-
148-
// Ensure it holds the least enum value.
153+
//
154+
// Codes mapping to absl status codes should not be considered for these
155+
// ranges.
156+
// They must be used exclsively for checking if error codes fall in valid
157+
// ranges when converting between TfLiteSupportStatus and
158+
// TfLiteSupportErrorCodee.
159+
160+
// Ensure it holds the least enum value amongst error codes mapping to
161+
// TfLiteSupportStatus.
149162
kErrorCodeFirst = kError,
150-
// Ensure it holds the greatest enum value.
163+
// Ensure it holds the greatest enum value amongst error codes mapping to
164+
// TfLiteSupportStatus.
151165
kErrorCodeLast = kImageProcessingBackendError,
152166

167+
// Absl Status Codes Mapping
168+
//
169+
// Codes starting from 900 will be used to map absl::Status created by TfLite
170+
// and are used as is by TfLite Support C++ layer. Such absl status objects
171+
// don't have a TfLiteSupportStatus in the payload that can be mapped to other
172+
// error codes in this struct. You must use the absl::Status::code() and map
173+
// them to the following error codes in such cases.
174+
// For more info on respective absl status codes, please see:
175+
// https://github.com/abseil/abseil-cpp/blob/master/absl/status/status.h#L91
176+
177+
// kNotFound indicates some requested entity (such as a file or directory)
178+
// was not found.
179+
kNotFound = 900,
180+
// kInternal indicates an internal error has occurred
181+
// and some invariants expected by the underlying system have not been
182+
// satisfied. This error code is reserved for serious errors.
183+
kInternal,
184+
153185
};
154186

155-
// A `TfLiteSupportError` encapsulates an error code and a descriptive message to
156-
// return in the event of an error being encountered in any TensorFlow Lite Task
157-
// Library C API.
187+
// A `TfLiteSupportError` encapsulates an error code and a descriptive message
188+
// to return in the event of an error being encountered in any TensorFlow Lite
189+
// Task Library C API.
158190
typedef struct TfLiteSupportError {
159191
// Holds the error code.
160192
enum TfLiteSupportErrorCode code;

tensorflow_lite_support/c/common_utils.cc

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717

1818
#include <string>
1919

20+
#include "external/com_google_absl/absl/status/status.h"
2021
#include "external/com_google_absl/absl/strings/cord.h"
2122
#include "tensorflow_lite_support/cc/common.h"
2223

@@ -40,7 +41,7 @@ void CreateTfLiteSupportErrorWithStatus(const absl::Status& status,
4041
int generic_error_code = static_cast<int>(kError);
4142
int error_code;
4243
try {
43-
// Try converting payload to integer if not payload is not empty. Otherwise
44+
// Try converting payload to integer if payload is not empty. Otherwise
4445
// convert a string signifying generic error code kError to integer.
4546
error_code = std::stoi(static_cast<absl::optional<std::string>>(
4647
status.GetPayload(kTfLiteSupportPayload))
@@ -51,13 +52,30 @@ void CreateTfLiteSupportErrorWithStatus(const absl::Status& status,
5152
error_code = generic_error_code;
5253
}
5354

54-
// If generated error code is outside the range of enum values possible, set
55-
// the error code to 1(kError). Assumes that enums will have an underlying
56-
// integer value and kErrorCodeLast and kErrorCodeFirst will always hold the
57-
// bounds of this range.
55+
// If error_code is outside the range of enum values possible or is kError, we
56+
// try to map the absl::Status::code() to assign appropriate
57+
// TfLiteSupportErrorCode or kError in default cases. Note: The mapping to
58+
// absl::Status::code() is done to generate a more specific error code than
59+
// kError in cases when the payload can't be mapped to TfLiteSupportStatus.
60+
// This can happen when absl::Status returned by TfLite are in turn returned
61+
// without moodification by TfLite Support Methods.
5862
if (error_code > static_cast<int>(kErrorCodeLast) ||
59-
error_code < static_cast<int>(kErrorCodeFirst))
60-
error_code = generic_error_code;
63+
error_code <= static_cast<int>(kErrorCodeFirst)) {
64+
switch (status.code()) {
65+
case absl::StatusCode::kInternal:
66+
error_code = kInternal;
67+
break;
68+
case absl::StatusCode::kInvalidArgument:
69+
error_code = kInvalidArgumentError;
70+
break;
71+
case absl::StatusCode::kNotFound:
72+
error_code = kNotFound;
73+
break;
74+
default:
75+
error_code = kError;
76+
break;
77+
}
78+
}
6179

6280
*error = new TfLiteSupportError;
6381
// TfLiteErrorCode has a one to one mapping with TfLiteSupportStatus starting

0 commit comments

Comments
 (0)