1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#import "TensorHelper.h"
#import <Foundation/Foundation.h>
@implementation TensorHelper
/**
* Supported tensor data type
*/
NSString* const JsTensorTypeBool = @"bool";
NSString* const JsTensorTypeUnsignedByte = @"uint8";
NSString* const JsTensorTypeByte = @"int8";
NSString* const JsTensorTypeShort = @"int16";
NSString* const JsTensorTypeInt = @"int32";
NSString* const JsTensorTypeLong = @"int64";
NSString* const JsTensorTypeFloat = @"float32";
NSString* const JsTensorTypeDouble = @"float64";
NSString* const JsTensorTypeString = @"string";
/**
* It creates an input tensor from a map passed by react native js.
* 'data' is blob object and the buffer is stored in RCTBlobManager. It first resolve it and creates a tensor.
*/
+ (Ort::Value)createInputTensor:(RCTBlobManager*)blobManager
input:(NSDictionary*)input
ortAllocator:(OrtAllocator*)ortAllocator
allocations:(std::vector<Ort::MemoryAllocation>&)allocations {
// shape
NSArray* dimsArray = [input objectForKey:@"dims"];
std::vector<int64_t> dims;
dims.reserve(dimsArray.count);
for (NSNumber* dim in dimsArray) {
dims.emplace_back([dim longLongValue]);
}
// type
ONNXTensorElementDataType tensorType = [self getOnnxTensorType:[input objectForKey:@"type"]];
// data
if (tensorType == ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING) {
NSArray* values = [input objectForKey:@"data"];
auto inputTensor =
Ort::Value::CreateTensor(ortAllocator, dims.data(), dims.size(), ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING);
size_t index = 0;
for (NSString* value in values) {
inputTensor.FillStringTensorElement([value UTF8String], index++);
}
return inputTensor;
} else {
NSDictionary* data = [input objectForKey:@"data"];
NSString* blobId = [data objectForKey:@"blobId"];
long size = [[data objectForKey:@"size"] longValue];
long offset = [[data objectForKey:@"offset"] longValue];
auto buffer = [blobManager resolve:blobId offset:offset size:size];
Ort::Value inputTensor = [self createInputTensor:tensorType
dims:dims
buffer:buffer
ortAllocator:ortAllocator
allocations:allocations];
[blobManager remove:blobId];
return inputTensor;
}
}
/**
* It creates an output map from an output tensor.
* a data array is store in RCTBlobManager.
*/
+ (NSDictionary*)createOutputTensor:(RCTBlobManager*)blobManager
outputNames:(const std::vector<const char*>&)outputNames
values:(const std::vector<Ort::Value>&)values {
if (outputNames.size() != values.size()) {
NSException* exception = [NSException exceptionWithName:@"create output tensor"
reason:@"output name and tensor count mismatched"
userInfo:nil];
@throw exception;
}
NSMutableDictionary* outputTensorMap = [NSMutableDictionary dictionary];
for (size_t i = 0; i < outputNames.size(); ++i) {
const auto outputName = outputNames[i];
const Ort::Value& value = values[i];
if (!value.IsTensor()) {
NSException* exception = [NSException exceptionWithName:@"create output tensor"
reason:@"only tensor type is supported"
userInfo:nil];
@throw exception;
}
NSMutableDictionary* outputTensor = [NSMutableDictionary dictionary];
// dims
NSMutableArray* outputDims = [NSMutableArray array];
auto dims = value.GetTensorTypeAndShapeInfo().GetShape();
for (auto dim : dims) {
[outputDims addObject:[NSNumber numberWithLongLong:dim]];
}
outputTensor[@"dims"] = outputDims;
// type
outputTensor[@"type"] = [self getJsTensorType:value.GetTensorTypeAndShapeInfo().GetElementType()];
// data
if (value.GetTensorTypeAndShapeInfo().GetElementType() == ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING) {
NSMutableArray* buffer = [NSMutableArray array];
for (NSInteger i = 0; i < value.GetTensorTypeAndShapeInfo().GetElementCount(); ++i) {
size_t elementLength = value.GetStringTensorElementLength(i);
std::string element(elementLength, '\0');
value.GetStringTensorElement(elementLength, i, (void*)element.data());
[buffer addObject:[NSString stringWithUTF8String:element.data()]];
}
outputTensor[@"data"] = buffer;
} else {
NSData* data = [self createOutputTensor:value];
NSString* blobId = [blobManager store:data];
outputTensor[@"data"] = @{
@"blobId" : blobId,
@"offset" : @0,
@"size" : @(data.length),
};
}
outputTensorMap[[NSString stringWithUTF8String:outputName]] = outputTensor;
}
return outputTensorMap;
}
template <typename T>
static Ort::Value createInputTensorT(OrtAllocator* ortAllocator, const std::vector<int64_t>& dims, NSData* buffer,
std::vector<Ort::MemoryAllocation>& allocations) {
T* dataBuffer = static_cast<T*>(ortAllocator->Alloc(ortAllocator, [buffer length]));
allocations.emplace_back(ortAllocator, dataBuffer, [buffer length]);
memcpy(static_cast<void*>(dataBuffer), [buffer bytes], [buffer length]);
return Ort::Value::CreateTensor<T>(ortAllocator->Info(ortAllocator), dataBuffer, buffer.length / sizeof(T),
dims.data(), dims.size());
}
+ (Ort::Value)createInputTensor:(ONNXTensorElementDataType)tensorType
dims:(const std::vector<int64_t>&)dims
buffer:(NSData*)buffer
ortAllocator:(OrtAllocator*)ortAllocator
allocations:(std::vector<Ort::MemoryAllocation>&)allocations {
switch (tensorType) {
case ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT:
return createInputTensorT<float>(ortAllocator, dims, buffer, allocations);
case ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT8:
return createInputTensorT<uint8_t>(ortAllocator, dims, buffer, allocations);
case ONNX_TENSOR_ELEMENT_DATA_TYPE_INT8:
return createInputTensorT<int8_t>(ortAllocator, dims, buffer, allocations);
case ONNX_TENSOR_ELEMENT_DATA_TYPE_INT16:
return createInputTensorT<int16_t>(ortAllocator, dims, buffer, allocations);
case ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32:
return createInputTensorT<int32_t>(ortAllocator, dims, buffer, allocations);
case ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64:
return createInputTensorT<int64_t>(ortAllocator, dims, buffer, allocations);
case ONNX_TENSOR_ELEMENT_DATA_TYPE_BOOL:
return createInputTensorT<bool>(ortAllocator, dims, buffer, allocations);
case ONNX_TENSOR_ELEMENT_DATA_TYPE_DOUBLE:
return createInputTensorT<double_t>(ortAllocator, dims, buffer, allocations);
case ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED:
case ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT16:
case ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING:
case ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT16:
case ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT32:
case ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT64:
case ONNX_TENSOR_ELEMENT_DATA_TYPE_COMPLEX64:
case ONNX_TENSOR_ELEMENT_DATA_TYPE_COMPLEX128:
case ONNX_TENSOR_ELEMENT_DATA_TYPE_BFLOAT16:
default: {
NSException* exception = [NSException exceptionWithName:@"create input tensor"
reason:@"unsupported tensor type"
userInfo:nil];
@throw exception;
}
}
}
template <typename T>
static NSData* createOutputTensorT(const Ort::Value& tensor) {
const auto data = tensor.GetTensorData<T>();
return [NSData dataWithBytesNoCopy:(void*)data
length:tensor.GetTensorTypeAndShapeInfo().GetElementCount() * sizeof(T)
freeWhenDone:false];
}
+ (NSData*)createOutputTensor:(const Ort::Value&)tensor {
ONNXTensorElementDataType tensorType = tensor.GetTensorTypeAndShapeInfo().GetElementType();
switch (tensorType) {
case ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT:
return createOutputTensorT<float>(tensor);
case ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT8:
return createOutputTensorT<uint8_t>(tensor);
case ONNX_TENSOR_ELEMENT_DATA_TYPE_INT8:
return createOutputTensorT<int8_t>(tensor);
case ONNX_TENSOR_ELEMENT_DATA_TYPE_INT16:
return createOutputTensorT<int16_t>(tensor);
case ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32:
return createOutputTensorT<int32_t>(tensor);
case ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64:
return createOutputTensorT<int64_t>(tensor);
case ONNX_TENSOR_ELEMENT_DATA_TYPE_BOOL:
return createOutputTensorT<bool>(tensor);
case ONNX_TENSOR_ELEMENT_DATA_TYPE_DOUBLE:
return createOutputTensorT<double_t>(tensor);
case ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED:
case ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT16:
case ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING:
case ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT16:
case ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT32:
case ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT64:
case ONNX_TENSOR_ELEMENT_DATA_TYPE_COMPLEX64:
case ONNX_TENSOR_ELEMENT_DATA_TYPE_COMPLEX128:
case ONNX_TENSOR_ELEMENT_DATA_TYPE_BFLOAT16:
default: {
NSException* exception = [NSException exceptionWithName:@"create output tensor"
reason:@"unsupported tensor type"
userInfo:nil];
@throw exception;
}
}
}
NSDictionary* JsTensorTypeToOnnxTensorTypeMap;
NSDictionary* OnnxTensorTypeToJsTensorTypeMap;
+ (void)initialize {
JsTensorTypeToOnnxTensorTypeMap = @{
JsTensorTypeFloat : @(ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT),
JsTensorTypeUnsignedByte : @(ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT8),
JsTensorTypeByte : @(ONNX_TENSOR_ELEMENT_DATA_TYPE_INT8),
JsTensorTypeShort : @(ONNX_TENSOR_ELEMENT_DATA_TYPE_INT16),
JsTensorTypeInt : @(ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32),
JsTensorTypeLong : @(ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64),
JsTensorTypeString : @(ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING),
JsTensorTypeBool : @(ONNX_TENSOR_ELEMENT_DATA_TYPE_BOOL),
JsTensorTypeDouble : @(ONNX_TENSOR_ELEMENT_DATA_TYPE_DOUBLE)
};
OnnxTensorTypeToJsTensorTypeMap = @{
@(ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT) : JsTensorTypeFloat,
@(ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT8) : JsTensorTypeUnsignedByte,
@(ONNX_TENSOR_ELEMENT_DATA_TYPE_INT8) : JsTensorTypeByte,
@(ONNX_TENSOR_ELEMENT_DATA_TYPE_INT16) : JsTensorTypeShort,
@(ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32) : JsTensorTypeInt,
@(ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64) : JsTensorTypeLong,
@(ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING) : JsTensorTypeString,
@(ONNX_TENSOR_ELEMENT_DATA_TYPE_BOOL) : JsTensorTypeBool,
@(ONNX_TENSOR_ELEMENT_DATA_TYPE_DOUBLE) : JsTensorTypeDouble
};
}
+ (ONNXTensorElementDataType)getOnnxTensorType:(const NSString*)type {
if ([JsTensorTypeToOnnxTensorTypeMap objectForKey:type]) {
return (ONNXTensorElementDataType)[JsTensorTypeToOnnxTensorTypeMap[type] intValue];
} else {
return ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED;
}
}
+ (NSString*)getJsTensorType:(ONNXTensorElementDataType)type {
if ([OnnxTensorTypeToJsTensorTypeMap objectForKey:@(type)]) {
return OnnxTensorTypeToJsTensorTypeMap[@(type)];
} else {
return @"undefined";
}
}
@end
|