1 /* Copyright 2018 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 #include "tensorflow/lite/delegates/flex/util.h"
16
17 namespace tflite {
18 namespace flex {
19
ConvertStatus(TfLiteContext * context,const tensorflow::Status & status)20 TfLiteStatus ConvertStatus(TfLiteContext* context,
21 const tensorflow::Status& status) {
22 if (!status.ok()) {
23 context->ReportError(context, "%s", status.error_message().c_str());
24 return kTfLiteError;
25 }
26 return kTfLiteOk;
27 }
28
CopyShapeAndType(TfLiteContext * context,const tensorflow::Tensor & src,TfLiteTensor * tensor)29 TfLiteStatus CopyShapeAndType(TfLiteContext* context,
30 const tensorflow::Tensor& src,
31 TfLiteTensor* tensor) {
32 tensor->type = GetTensorFlowLiteType(static_cast<TF_DataType>(src.dtype()));
33 if (tensor->type == kTfLiteNoType) {
34 context->ReportError(context,
35 "TF Lite does not support TensorFlow data type: %s",
36 DataTypeString(src.dtype()).c_str());
37 return kTfLiteError;
38 }
39
40 int num_dims = src.dims();
41 TfLiteIntArray* shape = TfLiteIntArrayCreate(num_dims);
42 for (int j = 0; j < num_dims; ++j) {
43 // We need to cast from TensorFlow's int64 to TF Lite's int32. Let's
44 // make sure there's no overflow.
45 if (src.dim_size(j) >= std::numeric_limits<int>::max()) {
46 context->ReportError(context,
47 "Dimension value in TensorFlow shape is larger than "
48 "supported by TF Lite");
49 TfLiteIntArrayFree(shape);
50 return kTfLiteError;
51 }
52 shape->data[j] = static_cast<int>(src.dim_size(j));
53 }
54 return context->ResizeTensor(context, tensor, shape);
55 }
56
GetTensorFlowDataType(TfLiteType type)57 TF_DataType GetTensorFlowDataType(TfLiteType type) {
58 switch (type) {
59 case kTfLiteNoType:
60 return TF_FLOAT;
61 case kTfLiteFloat32:
62 return TF_FLOAT;
63 case kTfLiteFloat16:
64 return TF_HALF;
65 case kTfLiteFloat64:
66 return TF_DOUBLE;
67 case kTfLiteInt16:
68 return TF_INT16;
69 case kTfLiteInt32:
70 return TF_INT32;
71 case kTfLiteUInt32:
72 return TF_UINT32;
73 case kTfLiteUInt8:
74 return TF_UINT8;
75 case kTfLiteInt8:
76 return TF_INT8;
77 case kTfLiteInt64:
78 return TF_INT64;
79 case kTfLiteUInt64:
80 return TF_UINT64;
81 case kTfLiteComplex64:
82 return TF_COMPLEX64;
83 case kTfLiteComplex128:
84 return TF_COMPLEX128;
85 case kTfLiteString:
86 return TF_STRING;
87 case kTfLiteBool:
88 return TF_BOOL;
89 case kTfLiteResource:
90 return TF_RESOURCE;
91 case kTfLiteVariant:
92 return TF_VARIANT;
93 }
94 }
95
GetTensorFlowLiteType(TF_DataType type)96 TfLiteType GetTensorFlowLiteType(TF_DataType type) {
97 switch (type) {
98 case TF_FLOAT:
99 return kTfLiteFloat32;
100 case TF_HALF:
101 return kTfLiteFloat16;
102 case TF_DOUBLE:
103 return kTfLiteFloat64;
104 case TF_INT16:
105 return kTfLiteInt16;
106 case TF_INT32:
107 return kTfLiteInt32;
108 case TF_UINT8:
109 return kTfLiteUInt8;
110 case TF_INT8:
111 return kTfLiteInt8;
112 case TF_INT64:
113 return kTfLiteInt64;
114 case TF_UINT64:
115 return kTfLiteUInt64;
116 case TF_COMPLEX64:
117 return kTfLiteComplex64;
118 case TF_COMPLEX128:
119 return kTfLiteComplex128;
120 case TF_STRING:
121 return kTfLiteString;
122 case TF_BOOL:
123 return kTfLiteBool;
124 case TF_RESOURCE:
125 return kTfLiteResource;
126 case TF_VARIANT:
127 return kTfLiteVariant;
128 default:
129 return kTfLiteNoType;
130 }
131 }
132
133 // Returns the TF data type name to be stored in the FunctionDef.
TfLiteTypeToTfTypeName(TfLiteType type)134 const char* TfLiteTypeToTfTypeName(TfLiteType type) {
135 switch (type) {
136 case kTfLiteNoType:
137 return "invalid";
138 case kTfLiteFloat32:
139 return "float";
140 case kTfLiteInt16:
141 return "int16";
142 case kTfLiteInt32:
143 return "int32";
144 case kTfLiteUInt32:
145 return "uint32";
146 case kTfLiteUInt8:
147 return "uint8";
148 case kTfLiteInt8:
149 return "int8";
150 case kTfLiteInt64:
151 return "int64";
152 case kTfLiteUInt64:
153 return "uint64";
154 case kTfLiteBool:
155 return "bool";
156 case kTfLiteComplex64:
157 return "complex64";
158 case kTfLiteComplex128:
159 return "complex128";
160 case kTfLiteString:
161 return "string";
162 case kTfLiteFloat16:
163 return "float16";
164 case kTfLiteFloat64:
165 return "float64";
166 case kTfLiteResource:
167 return "resource";
168 case kTfLiteVariant:
169 return "variant";
170 }
171 return "invalid";
172 }
173
174 } // namespace flex
175 } // namespace tflite
176