Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
c_api_internal.c
00001 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved. 00002 00003 Licensed under the Apache License, Version 2.0 (the "License"); 00004 you may not use this file except in compliance with the License. 00005 You may obtain a copy of the License at 00006 00007 http://www.apache.org/licenses/LICENSE-2.0 00008 00009 Unless required by applicable law or agreed to in writing, software 00010 distributed under the License is distributed on an "AS IS" BASIS, 00011 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00012 See the License for the specific language governing permissions and 00013 limitations under the License. 00014 ==============================================================================*/ 00015 00016 #include "tensorflow/lite/c/c_api_internal.h" 00017 #ifndef TF_LITE_STATIC_MEMORY 00018 #include <stdlib.h> 00019 #include <string.h> 00020 #endif // TF_LITE_STATIC_MEMORY 00021 00022 int TfLiteIntArrayGetSizeInBytes(int size) { 00023 static TfLiteIntArray dummy; 00024 return sizeof(dummy) + sizeof(dummy.data[0]) * size; 00025 } 00026 00027 int TfLiteIntArrayEqual(const TfLiteIntArray* a, const TfLiteIntArray* b) { 00028 if (a == b) return 1; 00029 if (a == NULL || b == NULL) return 0; 00030 return TfLiteIntArrayEqualsArray(a, b->size, b->data); 00031 } 00032 00033 int TfLiteIntArrayEqualsArray(const TfLiteIntArray* a, int b_size, 00034 const int b_data[]) { 00035 if (a == NULL) return (b_size == 0); 00036 if (a->size != b_size) return 0; 00037 int i = 0; 00038 for (; i < a->size; i++) 00039 if (a->data[i] != b_data[i]) return 0; 00040 return 1; 00041 } 00042 00043 #ifndef TF_LITE_STATIC_MEMORY 00044 00045 TfLiteIntArray* TfLiteIntArrayCreate(int size) { 00046 TfLiteIntArray* ret = 00047 (TfLiteIntArray*)malloc(TfLiteIntArrayGetSizeInBytes(size)); 00048 ret->size = size; 00049 return ret; 00050 } 00051 00052 TfLiteIntArray* TfLiteIntArrayCopy(const TfLiteIntArray* src) { 00053 if (!src) return NULL; 00054 TfLiteIntArray* ret = TfLiteIntArrayCreate(src->size); 00055 if (ret) { 00056 memcpy(ret->data, src->data, src->size * sizeof(int)); 00057 } 00058 return ret; 00059 } 00060 00061 void TfLiteIntArrayFree(TfLiteIntArray* a) { free(a); } 00062 00063 #endif // TF_LITE_STATIC_MEMORY 00064 00065 int TfLiteFloatArrayGetSizeInBytes(int size) { 00066 static TfLiteFloatArray dummy; 00067 return sizeof(dummy) + sizeof(dummy.data[0]) * size; 00068 } 00069 00070 #ifndef TF_LITE_STATIC_MEMORY 00071 00072 TfLiteFloatArray* TfLiteFloatArrayCreate(int size) { 00073 TfLiteFloatArray* ret = 00074 (TfLiteFloatArray*)malloc(TfLiteFloatArrayGetSizeInBytes(size)); 00075 ret->size = size; 00076 return ret; 00077 } 00078 00079 void TfLiteFloatArrayFree(TfLiteFloatArray* a) { free(a); } 00080 00081 void TfLiteTensorDataFree(TfLiteTensor* t) { 00082 if (t->allocation_type == kTfLiteDynamic) { 00083 free(t->data.raw); 00084 } 00085 t->data.raw = NULL; 00086 } 00087 00088 void TfLiteQuantizationFree(TfLiteQuantization* quantization) { 00089 if (quantization->type == kTfLiteAffineQuantization) { 00090 TfLiteAffineQuantization* q_params = 00091 (TfLiteAffineQuantization*)(quantization->params); 00092 if (q_params->scale) { 00093 TfLiteFloatArrayFree(q_params->scale); 00094 q_params->scale = NULL; 00095 } 00096 if (q_params->zero_point) { 00097 TfLiteIntArrayFree(q_params->zero_point); 00098 q_params->zero_point = NULL; 00099 } 00100 free(q_params); 00101 } 00102 quantization->params = NULL; 00103 quantization->type = kTfLiteNoQuantization; 00104 } 00105 00106 void TfLiteTensorFree(TfLiteTensor* t) { 00107 TfLiteTensorDataFree(t); 00108 if (t->dims) TfLiteIntArrayFree(t->dims); 00109 t->dims = NULL; 00110 00111 TfLiteQuantizationFree(&t->quantization); 00112 } 00113 00114 void TfLiteTensorReset(TfLiteType type, const char* name, TfLiteIntArray* dims, 00115 TfLiteQuantizationParams quantization, char* buffer, 00116 size_t size, TfLiteAllocationType allocation_type, 00117 const void* allocation, bool is_variable, 00118 TfLiteTensor* tensor) { 00119 TfLiteTensorFree(tensor); 00120 tensor->type = type; 00121 tensor->name = name; 00122 tensor->dims = dims; 00123 tensor->params = quantization; 00124 tensor->data.raw = buffer; 00125 tensor->bytes = size; 00126 tensor->allocation_type = allocation_type; 00127 tensor->allocation = allocation; 00128 tensor->is_variable = is_variable; 00129 00130 tensor->quantization.type = kTfLiteNoQuantization; 00131 tensor->quantization.params = NULL; 00132 } 00133 00134 void TfLiteTensorRealloc(size_t num_bytes, TfLiteTensor* tensor) { 00135 if (tensor->allocation_type != kTfLiteDynamic) { 00136 return; 00137 } 00138 if (!tensor->data.raw) { 00139 tensor->data.raw = malloc(num_bytes); 00140 } else if (num_bytes > tensor->bytes) { 00141 tensor->data.raw = realloc(tensor->data.raw, num_bytes); 00142 } 00143 tensor->bytes = num_bytes; 00144 } 00145 #endif // TF_LITE_STATIC_MEMORY 00146 00147 const char* TfLiteTypeGetName(TfLiteType type) { 00148 switch (type) { 00149 case kTfLiteNoType: 00150 return "NOTYPE"; 00151 case kTfLiteFloat32: 00152 return "FLOAT32"; 00153 case kTfLiteInt16: 00154 return "INT16"; 00155 case kTfLiteInt32: 00156 return "INT32"; 00157 case kTfLiteUInt8: 00158 return "UINT8"; 00159 case kTfLiteInt8: 00160 return "INT8"; 00161 case kTfLiteInt64: 00162 return "INT64"; 00163 case kTfLiteBool: 00164 return "BOOL"; 00165 case kTfLiteComplex64: 00166 return "COMPLEX64"; 00167 case kTfLiteString: 00168 return "STRING"; 00169 case kTfLiteFloat16: 00170 return "FLOAT16"; 00171 } 00172 return "Unknown type"; 00173 } 00174 00175 TfLiteDelegate TfLiteDelegateCreate() { 00176 TfLiteDelegate d = { 00177 .data_ = NULL, 00178 .Prepare = NULL, 00179 .CopyFromBufferHandle = NULL, 00180 .CopyToBufferHandle = NULL, 00181 .FreeBufferHandle = NULL, 00182 .flags = kTfLiteDelegateFlagsNone, 00183 }; 00184 return d; 00185 }
Generated on Wed Jul 13 2022 16:03:35 by
1.7.2