| /* |
| * Copyright (C) 2021 MediaTek Inc., this file is modified on 02/26/2021 |
| * by MediaTek Inc. based on MIT License . |
| * Permission is hereby granted, free of charge, to any person obtaining a copy |
| * of this software and associated documentation files (the ""Software""), to |
| * deal in the Software without restriction, including without limitation the |
| * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| * sell copies of the Software, and to permit persons to whom the Software is |
| * furnished to do so, subject to the following conditions: |
| * |
| * The above copyright notice and this permission notice shall be included in |
| * all copies or substantial portions of the Software. |
| * |
| * THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| * SOFTWARE. |
| */ |
| |
| #ifndef DELEGATE_MTK_NEURON_NEURON_DELEGATE_UTILS_H_ |
| #define DELEGATE_MTK_NEURON_NEURON_DELEGATE_UTILS_H_ |
| |
| #include <dlfcn.h> |
| #include <fcntl.h> |
| |
| #include <string> |
| |
| #include "delegate/mtk_neuron/neuron_types.h" |
| #include "tensorflow/lite/c/c_api_opaque.h" |
| #include "tensorflow/lite/c/common.h" |
| #include "tensorflow/lite/context_util.h" |
| #ifdef __ANDROID__ |
| #include <sys/system_properties.h> |
| #endif |
| |
| namespace tflite { |
| namespace neuron { |
| |
| #define RETURN_TFLITE_ERROR_IF_NEURON_ERROR(context, code, call_desc) \ |
| do { \ |
| const auto _code = (code); \ |
| const auto _call_desc = (call_desc); \ |
| if (_code != NEURON_NO_ERROR) { \ |
| const auto error_desc = NeuronErrorDescription(_code); \ |
| TfLiteOpaqueContextReportError( \ |
| context, "Neuron returned error %s at line %d while %s.\n", \ |
| error_desc.c_str(), __LINE__, _call_desc); \ |
| return kTfLiteError; \ |
| } \ |
| } while (0) |
| |
| #define RETURN_TFLITE_ERROR_IF_ION_ERROR(code) \ |
| do { \ |
| const auto _code = (code); \ |
| if (_code != 0) { \ |
| return kTfLiteError; \ |
| } \ |
| } while (0) |
| |
| #define TF_LITE_OPAQUE_ENSURE_STATUS(a) \ |
| do { \ |
| const TfLiteStatus s = (a); \ |
| if (s != kTfLiteOk) { \ |
| return s; \ |
| } \ |
| } while (0) |
| |
| inline std::string GetPropertyValue(const std::string& property) { |
| #ifdef __ANDROID__ |
| char value[PROP_VALUE_MAX]; |
| if (0 == __system_property_get(property.c_str(), value)) { |
| return std::string(); |
| } |
| return std::string(value); |
| #else // !__ANDROID__ |
| return std::string(); |
| #endif // __ANDROID__ |
| } |
| |
| inline std::string NeuronErrorDescription(int error_code) { |
| switch (error_code) { |
| case NEURON_NO_ERROR: |
| return "NEURON_NO_ERROR"; |
| case NEURON_OUT_OF_MEMORY: |
| return "NEURON_OUT_OF_MEMORY"; |
| case NEURON_INCOMPLETE: |
| return "NEURON_INCOMPLETE"; |
| case NEURON_UNEXPECTED_NULL: |
| return "NEURON_UNEXPECTED_NULL"; |
| case NEURON_BAD_DATA: |
| return "NEURON_BAD_DATA"; |
| case NEURON_OP_FAILED: |
| return "NEURON_OP_FAILED"; |
| case NEURON_BAD_STATE: |
| return "NEURON_BAD_STATE"; |
| case NEURON_UNMAPPABLE: |
| return "NEURON_UNMAPPABLE"; |
| case NEURON_OUTPUT_INSUFFICIENT_SIZE: |
| return "NEURON_OUTPUT_INSUFFICIENT_SIZE"; |
| case NEURON_UNAVAILABLE_DEVICE: |
| return "NEURON_UNAVAILABLE_DEVICE"; |
| default: |
| return "Unknown Neuron error code: " + std::to_string(error_code); |
| } |
| } |
| |
| inline bool HasZeroes(TfLiteIntArrayView array) { |
| for (auto value : array) { |
| if (value == 0) { |
| return true; |
| } |
| } |
| return false; |
| } |
| |
| inline bool IsFloat(TfLiteType type) { |
| switch (type) { |
| case kTfLiteFloat32: |
| return true; |
| default: |
| return false; |
| } |
| } |
| |
| inline bool IsFloatOrUInt8(TfLiteType type) { |
| switch (type) { |
| case kTfLiteFloat32: |
| case kTfLiteUInt8: |
| return true; |
| default: |
| return false; |
| } |
| } |
| |
| inline bool IsQuantized(TfLiteType type) { |
| switch (type) { |
| case kTfLiteUInt8: |
| case kTfLiteInt8: |
| return true; |
| default: |
| // kTfLiteInt16 isn't supported as quantized type yet. |
| return false; |
| } |
| } |
| |
| inline bool IsInt64(TfLiteType type) { return type == kTfLiteInt64; } |
| |
| inline bool IsConstantTensor(const TfLiteOpaqueTensor* tensor) { |
| return TfLiteOpaqueTensorGetAllocationType(tensor) == kTfLiteMmapRo; |
| } |
| |
| inline int64_t NumElements(const TfLiteOpaqueTensor* tensor) { |
| int64_t count = 1; |
| for (int i = 0; i < TfLiteOpaqueTensorNumDims(tensor); ++i) { |
| count *= TfLiteOpaqueTensorDim(tensor, i); |
| } |
| return count; |
| } |
| |
| } // namespace neuron |
| } // namespace tflite |
| |
| #endif // DELEGATE_MTK_NEURON_NEURON_DELEGATE_UTILS_H_ |