• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 }  // namespace flex
134 }  // namespace tflite
135