1// Copyright 2022 Google Inc. 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#import "TFLCommonUtil.h" 16 17#import "TFLQuantizationParameters+Internal.h" 18#import "tensorflow/lite/objc/apis/TFLTensor.h" 19 20#include "tensorflow/lite/c/c_api.h" 21 22NS_ASSUME_NONNULL_BEGIN 23 24TFLTensorDataType TFLTensorDataTypeFromCTensor(const TfLiteTensor *cTensor) { 25 TfLiteType cTensorType = TfLiteTensorType(cTensor); 26 switch (cTensorType) { 27 case kTfLiteFloat32: 28 return TFLTensorDataTypeFloat32; 29 case kTfLiteFloat16: 30 return TFLTensorDataTypeFloat16; 31 case kTfLiteFloat64: 32 return TFLTensorDataTypeFloat64; 33 case kTfLiteInt32: 34 return TFLTensorDataTypeInt32; 35 case kTfLiteUInt8: 36 return TFLTensorDataTypeUInt8; 37 case kTfLiteInt8: 38 return TFLTensorDataTypeInt8; 39 case kTfLiteInt64: 40 return TFLTensorDataTypeInt64; 41 case kTfLiteBool: 42 return TFLTensorDataTypeBool; 43 case kTfLiteInt16: 44 return TFLTensorDataTypeInt16; 45 case kTfLiteNoType: 46 case kTfLiteString: 47 case kTfLiteComplex64: 48 case kTfLiteComplex128: 49 case kTfLiteUInt16: 50 case kTfLiteUInt32: 51 case kTfLiteUInt64: 52 case kTfLiteResource: 53 case kTfLiteVariant: 54 // kTfLiteString, kTfLiteUInt64, kTfLiteComplex64, kTfLiteComplex128, 55 // kTfLiteResource and kTfLiteVariant are not supported in TensorFlow Lite 56 // Objc API. 57 return TFLTensorDataTypeNoType; 58 } 59} 60 61NSString *__nullable TFLTensorNameFromCTensor(const TfLiteTensor *cTensor) { 62 const char *cName = TfLiteTensorName(cTensor); 63 if (cName == nullptr) return nil; 64 return [NSString stringWithUTF8String:cName]; 65} 66 67TFLQuantizationParameters *__nullable 68TFLQuantizationParamsFromCTensor(const TfLiteTensor *cTensor) { 69 TfLiteQuantizationParams cParams = TfLiteTensorQuantizationParams(cTensor); 70 TFLQuantizationParameters *quantizationParams; 71 72 // TODO(b/119735362): Update this check once the TfLiteQuantizationParams struct has a mode. 73 if (cParams.scale != 0.0) { 74 quantizationParams = [[TFLQuantizationParameters alloc] initWithScale:cParams.scale 75 zeroPoint:cParams.zero_point]; 76 } 77 return quantizationParams; 78} 79 80NS_ASSUME_NONNULL_END 81