• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2017 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 #include "tensorflow/lite/c/c_api_internal.h"
17 #ifndef TF_LITE_STATIC_MEMORY
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #endif  // TF_LITE_STATIC_MEMORY
22 
TfLiteIntArrayGetSizeInBytes(int size)23 int TfLiteIntArrayGetSizeInBytes(int size) {
24   static TfLiteIntArray dummy;
25   return sizeof(dummy) + sizeof(dummy.data[0]) * size;
26 }
27 
TfLiteIntArrayEqual(TfLiteIntArray * a,TfLiteIntArray * b)28 int TfLiteIntArrayEqual(TfLiteIntArray* a, TfLiteIntArray* b) {
29   if (a == b) return 1;
30   if (a == NULL || b == NULL) return 0;
31   return TfLiteIntArrayEqualsArray(a, b->size, b->data);
32 }
33 
TfLiteIntArrayEqualsArray(TfLiteIntArray * a,int b_size,int b_data[])34 int TfLiteIntArrayEqualsArray(TfLiteIntArray* a, int b_size, int b_data[]) {
35   if (a == NULL) return (b_size == 0);
36   if (a->size != b_size) return 0;
37   int i = 0;
38   for (; i < a->size; i++)
39     if (a->data[i] != b_data[i]) return 0;
40   return 1;
41 }
42 
43 #ifndef TF_LITE_STATIC_MEMORY
44 
TfLiteIntArrayCreate(int size)45 TfLiteIntArray* TfLiteIntArrayCreate(int size) {
46   TfLiteIntArray* ret =
47       (TfLiteIntArray*)malloc(TfLiteIntArrayGetSizeInBytes(size));
48   ret->size = size;
49   return ret;
50 }
51 
TfLiteIntArrayPrint(const char * s,TfLiteIntArray * a)52 void TfLiteIntArrayPrint(const char* s, TfLiteIntArray* a) {
53   printf("%s: length=%d [", s, a->size);
54   if (a->size) printf("%d", a->data[0]);
55   int i = 1;
56   for (; i < a->size; i++) {
57     printf(" %d", a->data[i]);
58   }
59   printf("]\n");
60 }
61 
TfLiteIntArrayCopy(const TfLiteIntArray * src)62 TfLiteIntArray* TfLiteIntArrayCopy(const TfLiteIntArray* src) {
63   if (!src) return NULL;
64   TfLiteIntArray* ret = TfLiteIntArrayCreate(src->size);
65   if (ret) {
66     memcpy(ret->data, src->data, src->size * sizeof(int));
67   }
68   return ret;
69 }
70 
TfLiteIntArrayFree(TfLiteIntArray * a)71 void TfLiteIntArrayFree(TfLiteIntArray* a) { free(a); }
72 
TfLiteFloatArrayGetSizeInBytes(int size)73 int TfLiteFloatArrayGetSizeInBytes(int size) {
74   static TfLiteFloatArray dummy;
75   return sizeof(dummy) + sizeof(dummy.data[0]) * size;
76 }
77 
TfLiteFloatArrayCreate(int size)78 TfLiteFloatArray* TfLiteFloatArrayCreate(int size) {
79   TfLiteFloatArray* ret =
80       (TfLiteFloatArray*)malloc(TfLiteFloatArrayGetSizeInBytes(size));
81   ret->size = size;
82   return ret;
83 }
84 
TfLiteFloatArrayFree(TfLiteFloatArray * a)85 void TfLiteFloatArrayFree(TfLiteFloatArray* a) { free(a); }
86 
TfLiteTensorDataFree(TfLiteTensor * t)87 void TfLiteTensorDataFree(TfLiteTensor* t) {
88   if (t->allocation_type == kTfLiteDynamic && t->data.raw) {
89     free(t->data.raw);
90   }
91   t->data.raw = NULL;
92 }
93 
TfLiteQuantizationFree(TfLiteQuantization * quantization)94 void TfLiteQuantizationFree(TfLiteQuantization* quantization) {
95   if (quantization->type == kTfLiteAffineQuantization) {
96     TfLiteAffineQuantization* q_params =
97         (TfLiteAffineQuantization*)(quantization->params);
98     if (q_params->scale) {
99       TfLiteFloatArrayFree(q_params->scale);
100       q_params->scale = NULL;
101     }
102     if (q_params->zero_point) {
103       TfLiteIntArrayFree(q_params->zero_point);
104       q_params->zero_point = NULL;
105     }
106     free(q_params);
107   }
108   quantization->params = NULL;
109   quantization->type = kTfLiteNoQuantization;
110 }
111 
TfLiteTensorFree(TfLiteTensor * t)112 void TfLiteTensorFree(TfLiteTensor* t) {
113   TfLiteTensorDataFree(t);
114   if (t->dims) TfLiteIntArrayFree(t->dims);
115   t->dims = NULL;
116 
117   TfLiteQuantizationFree(&t->quantization);
118 }
119 
TfLiteTensorReset(TfLiteType type,const char * name,TfLiteIntArray * dims,TfLiteQuantizationParams quantization,char * buffer,size_t size,TfLiteAllocationType allocation_type,const void * allocation,bool is_variable,TfLiteTensor * tensor)120 void TfLiteTensorReset(TfLiteType type, const char* name, TfLiteIntArray* dims,
121                        TfLiteQuantizationParams quantization, char* buffer,
122                        size_t size, TfLiteAllocationType allocation_type,
123                        const void* allocation, bool is_variable,
124                        TfLiteTensor* tensor) {
125   TfLiteTensorFree(tensor);
126   tensor->type = type;
127   tensor->name = name;
128   tensor->dims = dims;
129   tensor->params = quantization;
130   tensor->data.raw = buffer;
131   tensor->bytes = size;
132   tensor->allocation_type = allocation_type;
133   tensor->allocation = allocation;
134   tensor->is_variable = is_variable;
135 
136   tensor->quantization.type = kTfLiteNoQuantization;
137   tensor->quantization.params = NULL;
138 }
139 
TfLiteTensorRealloc(size_t num_bytes,TfLiteTensor * tensor)140 void TfLiteTensorRealloc(size_t num_bytes, TfLiteTensor* tensor) {
141   if (tensor->allocation_type != kTfLiteDynamic) {
142     return;
143   }
144   if (!tensor->data.raw) {
145     tensor->data.raw = malloc(num_bytes);
146   } else if (num_bytes > tensor->bytes) {
147     tensor->data.raw = realloc(tensor->data.raw, num_bytes);
148   }
149   tensor->bytes = num_bytes;
150 }
151 #endif  // TF_LITE_STATIC_MEMORY
152 
TfLiteTypeGetName(TfLiteType type)153 const char* TfLiteTypeGetName(TfLiteType type) {
154   switch (type) {
155     case kTfLiteNoType:
156       return "NOTYPE";
157     case kTfLiteFloat32:
158       return "FLOAT32";
159     case kTfLiteInt16:
160       return "INT16";
161     case kTfLiteInt32:
162       return "INT32";
163     case kTfLiteUInt8:
164       return "UINT8";
165     case kTfLiteInt8:
166       return "INT8";
167     case kTfLiteInt64:
168       return "INT64";
169     case kTfLiteBool:
170       return "BOOL";
171     case kTfLiteComplex64:
172       return "COMPLEX64";
173     case kTfLiteString:
174       return "STRING";
175   }
176   return "Unknown type";
177 }
178 
TfLiteDelegateCreate()179 TfLiteDelegate TfLiteDelegateCreate() {
180   TfLiteDelegate d = {
181       .data_ = NULL,
182       .Prepare = NULL,
183       .CopyFromBufferHandle = NULL,
184       .CopyToBufferHandle = NULL,
185       .FreeBufferHandle = NULL,
186       .flags = kTfLiteDelegateFlagsNone,
187   };
188   return d;
189 }
190