• 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 kTfLiteInt16:
64       return TF_INT16;
65     case kTfLiteInt32:
66       return TF_INT32;
67     case kTfLiteUInt8:
68       return TF_UINT8;
69     case kTfLiteInt8:
70       return TF_INT8;
71     case kTfLiteInt64:
72       return TF_INT64;
73     case kTfLiteComplex64:
74       return TF_COMPLEX64;
75     case kTfLiteString:
76       return TF_STRING;
77     case kTfLiteBool:
78       return TF_BOOL;
79   }
80 }
81 
GetTensorFlowLiteType(TF_DataType type)82 TfLiteType GetTensorFlowLiteType(TF_DataType type) {
83   switch (type) {
84     case TF_FLOAT:
85       return kTfLiteFloat32;
86     case TF_INT16:
87       return kTfLiteInt16;
88     case TF_INT32:
89       return kTfLiteInt32;
90     case TF_UINT8:
91       return kTfLiteUInt8;
92     case TF_INT8:
93       return kTfLiteInt8;
94     case TF_INT64:
95       return kTfLiteInt64;
96     case TF_COMPLEX64:
97       return kTfLiteComplex64;
98     case TF_STRING:
99       return kTfLiteString;
100     case TF_BOOL:
101       return kTfLiteBool;
102     default:
103       return kTfLiteNoType;
104   }
105 }
106 
107 }  // namespace flex
108 }  // namespace tflite
109