common.cc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. #include "tensorflow/lite/c/common.h"
  13. #include "tensorflow/lite/c/c_api_types.h"
  14. #ifdef TF_LITE_TENSORFLOW_PROFILER
  15. #include <string>
  16. #include "tensorflow/lite/core/macros.h"
  17. #include "tensorflow/lite/tensorflow_profiler_logger.h"
  18. #endif
  19. #ifndef TF_LITE_STATIC_MEMORY
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #endif // TF_LITE_STATIC_MEMORY
  23. #ifdef TF_LITE_TENSORFLOW_PROFILER
  24. namespace tflite {
  25. // Use weak symbols here (even though they are guarded by macros) to avoid
  26. // build breakage when building a benchmark requires TFLite runs. The main
  27. // benchmark library should have tensor_profiler_logger dependency.
  28. TFLITE_ATTRIBUTE_WEAK void OnTfLiteTensorAlloc(TfLiteTensor* tensor,
  29. size_t num_bytes);
  30. TFLITE_ATTRIBUTE_WEAK void OnTfLiteTensorDealloc(TfLiteTensor* tensor);
  31. } // namespace tflite
  32. #endif // TF_LITE_TENSORFLOW_PROFILER
  33. extern "C" {
  34. size_t TfLiteIntArrayGetSizeInBytes(int size) {
  35. static TfLiteIntArray dummy;
  36. size_t computed_size = sizeof(dummy) + sizeof(dummy.data[0]) * size;
  37. #if defined(_MSC_VER)
  38. // Context for why this is needed is in http://b/189926408#comment21
  39. computed_size -= sizeof(dummy.data[0]);
  40. #endif
  41. return computed_size;
  42. }
  43. int TfLiteIntArrayEqual(const TfLiteIntArray* a, const TfLiteIntArray* b) {
  44. if (a == b) return 1;
  45. if (a == nullptr || b == nullptr) return 0;
  46. return TfLiteIntArrayEqualsArray(a, b->size, b->data);
  47. }
  48. int TfLiteIntArrayEqualsArray(const TfLiteIntArray* a, int b_size,
  49. const int b_data[]) {
  50. if (a == nullptr) return (b_size == 0);
  51. if (a->size != b_size) return 0;
  52. int i = 0;
  53. for (; i < a->size; i++)
  54. if (a->data[i] != b_data[i]) return 0;
  55. return 1;
  56. }
  57. #ifndef TF_LITE_STATIC_MEMORY
  58. TfLiteIntArray* TfLiteIntArrayCreate(int size) {
  59. size_t alloc_size = TfLiteIntArrayGetSizeInBytes(size);
  60. if (alloc_size <= 0) return nullptr;
  61. TfLiteIntArray* ret = (TfLiteIntArray*)malloc(alloc_size);
  62. if (!ret) return ret;
  63. ret->size = size;
  64. return ret;
  65. }
  66. TfLiteIntArray* TfLiteIntArrayCopy(const TfLiteIntArray* src) {
  67. if (!src) return nullptr;
  68. TfLiteIntArray* ret = TfLiteIntArrayCreate(src->size);
  69. if (ret) {
  70. memcpy(ret->data, src->data, src->size * sizeof(int));
  71. }
  72. return ret;
  73. }
  74. void TfLiteIntArrayFree(TfLiteIntArray* a) { free(a); }
  75. #endif // TF_LITE_STATIC_MEMORY
  76. int TfLiteFloatArrayGetSizeInBytes(int size) {
  77. static TfLiteFloatArray dummy;
  78. int computed_size = sizeof(dummy) + sizeof(dummy.data[0]) * size;
  79. #if defined(_MSC_VER)
  80. // Context for why this is needed is in http://b/189926408#comment21
  81. computed_size -= sizeof(dummy.data[0]);
  82. #endif
  83. return computed_size;
  84. }
  85. #ifndef TF_LITE_STATIC_MEMORY
  86. TfLiteFloatArray* TfLiteFloatArrayCreate(int size) {
  87. TfLiteFloatArray* ret =
  88. (TfLiteFloatArray*)malloc(TfLiteFloatArrayGetSizeInBytes(size));
  89. ret->size = size;
  90. return ret;
  91. }
  92. void TfLiteFloatArrayFree(TfLiteFloatArray* a) { free(a); }
  93. void TfLiteTensorDataFree(TfLiteTensor* t) {
  94. if (t->allocation_type == kTfLiteDynamic ||
  95. t->allocation_type == kTfLitePersistentRo) {
  96. if (t->data.raw) {
  97. #ifdef TF_LITE_TENSORFLOW_PROFILER
  98. tflite::OnTfLiteTensorDealloc(t);
  99. #endif
  100. free(t->data.raw);
  101. }
  102. }
  103. t->data.raw = nullptr;
  104. }
  105. void TfLiteQuantizationFree(TfLiteQuantization* quantization) {
  106. if (quantization->type == kTfLiteAffineQuantization) {
  107. TfLiteAffineQuantization* q_params =
  108. (TfLiteAffineQuantization*)(quantization->params);
  109. if (q_params->scale) {
  110. TfLiteFloatArrayFree(q_params->scale);
  111. q_params->scale = nullptr;
  112. }
  113. if (q_params->zero_point) {
  114. TfLiteIntArrayFree(q_params->zero_point);
  115. q_params->zero_point = nullptr;
  116. }
  117. free(q_params);
  118. }
  119. quantization->params = nullptr;
  120. quantization->type = kTfLiteNoQuantization;
  121. }
  122. void TfLiteSparsityFree(TfLiteSparsity* sparsity) {
  123. if (sparsity == nullptr) {
  124. return;
  125. }
  126. if (sparsity->traversal_order) {
  127. TfLiteIntArrayFree(sparsity->traversal_order);
  128. sparsity->traversal_order = nullptr;
  129. }
  130. if (sparsity->block_map) {
  131. TfLiteIntArrayFree(sparsity->block_map);
  132. sparsity->block_map = nullptr;
  133. }
  134. if (sparsity->dim_metadata) {
  135. int i = 0;
  136. for (; i < sparsity->dim_metadata_size; i++) {
  137. TfLiteDimensionMetadata metadata = sparsity->dim_metadata[i];
  138. if (metadata.format == kTfLiteDimSparseCSR) {
  139. TfLiteIntArrayFree(metadata.array_segments);
  140. metadata.array_segments = nullptr;
  141. TfLiteIntArrayFree(metadata.array_indices);
  142. metadata.array_indices = nullptr;
  143. }
  144. }
  145. free(sparsity->dim_metadata);
  146. sparsity->dim_metadata = nullptr;
  147. }
  148. free(sparsity);
  149. }
  150. void TfLiteTensorFree(TfLiteTensor* t) {
  151. TfLiteTensorDataFree(t);
  152. if (t->dims) TfLiteIntArrayFree(t->dims);
  153. t->dims = nullptr;
  154. if (t->dims_signature) {
  155. TfLiteIntArrayFree((TfLiteIntArray*)t->dims_signature);
  156. }
  157. t->dims_signature = nullptr;
  158. TfLiteQuantizationFree(&t->quantization);
  159. TfLiteSparsityFree(t->sparsity);
  160. t->sparsity = nullptr;
  161. }
  162. void TfLiteTensorReset(TfLiteType type, const char* name, TfLiteIntArray* dims,
  163. TfLiteQuantizationParams quantization, char* buffer,
  164. size_t size, TfLiteAllocationType allocation_type,
  165. const void* allocation, bool is_variable,
  166. TfLiteTensor* tensor) {
  167. TfLiteTensorFree(tensor);
  168. tensor->type = type;
  169. tensor->name = name;
  170. tensor->dims = dims;
  171. tensor->params = quantization;
  172. tensor->data.raw = buffer;
  173. tensor->bytes = size;
  174. tensor->allocation_type = allocation_type;
  175. tensor->allocation = allocation;
  176. tensor->is_variable = is_variable;
  177. tensor->quantization.type = kTfLiteNoQuantization;
  178. tensor->quantization.params = nullptr;
  179. }
  180. TfLiteStatus TfLiteTensorCopy(const TfLiteTensor* src, TfLiteTensor* dst) {
  181. if (!src || !dst) return kTfLiteOk;
  182. if (src->bytes != dst->bytes) return kTfLiteError;
  183. if (src == dst) return kTfLiteOk;
  184. dst->type = src->type;
  185. if (dst->dims) TfLiteIntArrayFree(dst->dims);
  186. dst->dims = TfLiteIntArrayCopy(src->dims);
  187. memcpy(dst->data.raw, src->data.raw, src->bytes);
  188. dst->buffer_handle = src->buffer_handle;
  189. dst->data_is_stale = src->data_is_stale;
  190. dst->delegate = src->delegate;
  191. return kTfLiteOk;
  192. }
  193. void TfLiteTensorRealloc(size_t num_bytes, TfLiteTensor* tensor) {
  194. if (tensor->allocation_type != kTfLiteDynamic &&
  195. tensor->allocation_type != kTfLitePersistentRo) {
  196. return;
  197. }
  198. // TODO(b/145340303): Tensor data should be aligned.
  199. if (!tensor->data.raw) {
  200. tensor->data.raw = (char*)malloc(num_bytes);
  201. #ifdef TF_LITE_TENSORFLOW_PROFILER
  202. tflite::OnTfLiteTensorAlloc(tensor, num_bytes);
  203. #endif
  204. } else if (num_bytes > tensor->bytes) {
  205. #ifdef TF_LITE_TENSORFLOW_PROFILER
  206. tflite::OnTfLiteTensorDealloc(tensor);
  207. #endif
  208. tensor->data.raw = (char*)realloc(tensor->data.raw, num_bytes);
  209. #ifdef TF_LITE_TENSORFLOW_PROFILER
  210. tflite::OnTfLiteTensorAlloc(tensor, num_bytes);
  211. #endif
  212. }
  213. tensor->bytes = num_bytes;
  214. }
  215. #endif // TF_LITE_STATIC_MEMORY
  216. const char* TfLiteTypeGetName(TfLiteType type) {
  217. switch (type) {
  218. case kTfLiteNoType:
  219. return "NOTYPE";
  220. case kTfLiteFloat32:
  221. return "FLOAT32";
  222. case kTfLiteUInt16:
  223. return "UINT16";
  224. case kTfLiteInt16:
  225. return "INT16";
  226. case kTfLiteInt32:
  227. return "INT32";
  228. case kTfLiteUInt32:
  229. return "UINT32";
  230. case kTfLiteUInt8:
  231. return "UINT8";
  232. case kTfLiteInt8:
  233. return "INT8";
  234. case kTfLiteInt64:
  235. return "INT64";
  236. case kTfLiteUInt64:
  237. return "UINT64";
  238. case kTfLiteBool:
  239. return "BOOL";
  240. case kTfLiteComplex64:
  241. return "COMPLEX64";
  242. case kTfLiteComplex128:
  243. return "COMPLEX128";
  244. case kTfLiteString:
  245. return "STRING";
  246. case kTfLiteFloat16:
  247. return "FLOAT16";
  248. case kTfLiteFloat64:
  249. return "FLOAT64";
  250. case kTfLiteResource:
  251. return "RESOURCE";
  252. case kTfLiteVariant:
  253. return "VARIANT";
  254. }
  255. return "Unknown type";
  256. }
  257. TfLiteDelegate TfLiteDelegateCreate() { return TfLiteDelegate{}; }
  258. } // extern "C"