|
9 | 9 | #import "ExecuTorchModule.h"
|
10 | 10 |
|
11 | 11 | #import "ExecuTorchError.h"
|
| 12 | +#import "ExecuTorchUtils.h" |
12 | 13 |
|
13 | 14 | #import <executorch/extension/module/module.h>
|
14 | 15 | #import <executorch/extension/tensor/tensor.h>
|
@@ -62,6 +63,186 @@ static inline EValue toEValue(ExecuTorchValue *value) {
|
62 | 63 | return [ExecuTorchValue new];
|
63 | 64 | }
|
64 | 65 |
|
| 66 | +@interface ExecuTorchTensorMetadata () |
| 67 | + |
| 68 | +- (instancetype)initWithTensorMetadata:(const TensorInfo &)tensorInfo |
| 69 | + NS_DESIGNATED_INITIALIZER; |
| 70 | + |
| 71 | +@end |
| 72 | + |
| 73 | +@implementation ExecuTorchTensorMetadata { |
| 74 | + NSArray<NSNumber *> *_shape; |
| 75 | + NSArray<NSNumber *> *_dimensionOrder; |
| 76 | + ExecuTorchDataType _dataType; |
| 77 | + BOOL _isMemoryPlanned; |
| 78 | + NSString *_name; |
| 79 | +} |
| 80 | + |
| 81 | +- (instancetype)initWithTensorMetadata:(const TensorInfo &)tensorInfo { |
| 82 | + self = [super init]; |
| 83 | + if (self) { |
| 84 | + _shape = utils::toNSArray(tensorInfo.sizes()); |
| 85 | + _dimensionOrder = utils::toNSArray(tensorInfo.dim_order()); |
| 86 | + _dataType = (ExecuTorchDataType)tensorInfo.scalar_type(); |
| 87 | + _isMemoryPlanned = tensorInfo.is_memory_planned(); |
| 88 | + _name = [[NSString alloc] initWithBytes:tensorInfo.name().data() |
| 89 | + length:tensorInfo.name().size() |
| 90 | + encoding:NSUTF8StringEncoding]; |
| 91 | + } |
| 92 | + return self; |
| 93 | +} |
| 94 | + |
| 95 | +@end |
| 96 | + |
| 97 | +@interface ExecuTorchMethodMetadata () |
| 98 | + |
| 99 | +- (nullable instancetype)initWithMethodMetadata:(const MethodMeta &)methodMeta |
| 100 | + error:(NSError **)error |
| 101 | + NS_DESIGNATED_INITIALIZER; |
| 102 | + |
| 103 | +@end |
| 104 | + |
| 105 | +@implementation ExecuTorchMethodMetadata { |
| 106 | + NSString *_name; |
| 107 | + NSMutableArray<NSNumber *> *_inputValueTags; |
| 108 | + NSMutableArray<NSNumber *> *_outputValueTags; |
| 109 | + NSMutableDictionary<NSNumber *, ExecuTorchTensorMetadata *> *_inputTensorMetadatas; |
| 110 | + NSMutableDictionary<NSNumber *, ExecuTorchTensorMetadata *> *_outputTensorMetadatas; |
| 111 | + NSMutableArray<ExecuTorchTensorMetadata *> *_attributeTensorMetadatas; |
| 112 | + NSMutableArray<NSNumber *> *_memoryPlannedBufferSizes; |
| 113 | + NSMutableArray<NSString *> *_backendNames; |
| 114 | + NSInteger _instructionCount; |
| 115 | +} |
| 116 | + |
| 117 | +- (nullable instancetype)initWithMethodMetadata:(const MethodMeta &)methodMeta |
| 118 | + error:(NSError **)error { |
| 119 | + self = [super init]; |
| 120 | + if (self) { |
| 121 | + _name = @(methodMeta.name()); |
| 122 | + const NSInteger inputCount = methodMeta.num_inputs(); |
| 123 | + const NSInteger outputCount = methodMeta.num_outputs(); |
| 124 | + const NSInteger attributeCount = methodMeta.num_attributes(); |
| 125 | + const NSInteger memoryPlannedBufferCount = methodMeta.num_memory_planned_buffers(); |
| 126 | + const NSInteger backendCount = methodMeta.num_backends(); |
| 127 | + _instructionCount = methodMeta.num_instructions(); |
| 128 | + _inputValueTags = [NSMutableArray arrayWithCapacity:inputCount]; |
| 129 | + _outputValueTags = [NSMutableArray arrayWithCapacity:outputCount]; |
| 130 | + _inputTensorMetadatas = [NSMutableDictionary dictionary]; |
| 131 | + _outputTensorMetadatas = [NSMutableDictionary dictionary]; |
| 132 | + _attributeTensorMetadatas = [NSMutableArray arrayWithCapacity:attributeCount]; |
| 133 | + _memoryPlannedBufferSizes = [NSMutableArray arrayWithCapacity:memoryPlannedBufferCount]; |
| 134 | + _backendNames = [NSMutableArray arrayWithCapacity:backendCount]; |
| 135 | + |
| 136 | + for (NSInteger index = 0; index < inputCount; ++index) { |
| 137 | + auto result = methodMeta.input_tag(index); |
| 138 | + if (!result.ok()) { |
| 139 | + if (error) { |
| 140 | + *error = ExecuTorchErrorWithCode((ExecuTorchErrorCode)result.error()); |
| 141 | + } |
| 142 | + return nil; |
| 143 | + } |
| 144 | + const auto inputValueTag = (ExecuTorchValueTag)result.get(); |
| 145 | + [_inputValueTags addObject:@(inputValueTag)]; |
| 146 | + |
| 147 | + if (inputValueTag == ExecuTorchValueTagTensor) { |
| 148 | + auto tensorMetadataResult = methodMeta.input_tensor_meta(index); |
| 149 | + if (!tensorMetadataResult.ok()) { |
| 150 | + if (error) { |
| 151 | + *error = ExecuTorchErrorWithCode((ExecuTorchErrorCode)tensorMetadataResult.error()); |
| 152 | + } |
| 153 | + return nil; |
| 154 | + } |
| 155 | + _inputTensorMetadatas[@(index)] = [[ExecuTorchTensorMetadata alloc] initWithTensorMetadata:tensorMetadataResult.get()]; |
| 156 | + } |
| 157 | + } |
| 158 | + for (NSInteger index = 0; index < outputCount; ++index) { |
| 159 | + auto result = methodMeta.output_tag(index); |
| 160 | + if (!result.ok()) { |
| 161 | + if (error) { |
| 162 | + *error = ExecuTorchErrorWithCode((ExecuTorchErrorCode)result.error()); |
| 163 | + } |
| 164 | + return nil; |
| 165 | + } |
| 166 | + const auto outputValueTag = (ExecuTorchValueTag)result.get(); |
| 167 | + [_outputValueTags addObject:@(outputValueTag)]; |
| 168 | + |
| 169 | + if (outputValueTag == ExecuTorchValueTagTensor) { |
| 170 | + auto tensorMetadataResult = methodMeta.output_tensor_meta(index); |
| 171 | + if (!tensorMetadataResult.ok()) { |
| 172 | + if (error) { |
| 173 | + *error = ExecuTorchErrorWithCode((ExecuTorchErrorCode)tensorMetadataResult.error()); |
| 174 | + } |
| 175 | + return nil; |
| 176 | + } |
| 177 | + _outputTensorMetadatas[@(index)] = [[ExecuTorchTensorMetadata alloc] initWithTensorMetadata:tensorMetadataResult.get()]; |
| 178 | + } |
| 179 | + } |
| 180 | + for (NSInteger index = 0; index < attributeCount; ++index) { |
| 181 | + auto result = methodMeta.attribute_tensor_meta(index); |
| 182 | + if (!result.ok()) { |
| 183 | + if (error) { |
| 184 | + *error = ExecuTorchErrorWithCode((ExecuTorchErrorCode)result.error()); |
| 185 | + } |
| 186 | + return nil; |
| 187 | + } |
| 188 | + [_attributeTensorMetadatas addObject:[[ExecuTorchTensorMetadata alloc] initWithTensorMetadata:result.get()]]; |
| 189 | + } |
| 190 | + for (NSInteger index = 0; index < memoryPlannedBufferCount; ++index) { |
| 191 | + auto result = methodMeta.memory_planned_buffer_size(index); |
| 192 | + if (!result.ok()) { |
| 193 | + if (error) { |
| 194 | + *error = ExecuTorchErrorWithCode((ExecuTorchErrorCode)result.error()); |
| 195 | + } |
| 196 | + return nil; |
| 197 | + } |
| 198 | + const auto memoryPlannedBufferSize = result.get(); |
| 199 | + [_memoryPlannedBufferSizes addObject:@(memoryPlannedBufferSize)]; |
| 200 | + } |
| 201 | + for (NSInteger index = 0; index < backendCount; ++index) { |
| 202 | + auto result = methodMeta.get_backend_name(index); |
| 203 | + if (!result.ok()) { |
| 204 | + if (error) { |
| 205 | + *error = ExecuTorchErrorWithCode((ExecuTorchErrorCode)result.error()); |
| 206 | + } |
| 207 | + return nil; |
| 208 | + } |
| 209 | + NSString *backendName = [NSString stringWithUTF8String:result.get()]; |
| 210 | + [_backendNames addObject:backendName]; |
| 211 | + } |
| 212 | + } |
| 213 | + return self; |
| 214 | +} |
| 215 | + |
| 216 | +- (NSArray<NSNumber *> *)inputValueTags { |
| 217 | + return _inputValueTags; |
| 218 | +} |
| 219 | + |
| 220 | +- (NSArray<NSNumber *> *)outputValueTags { |
| 221 | + return _outputValueTags; |
| 222 | +} |
| 223 | + |
| 224 | +- (NSDictionary<NSNumber *,ExecuTorchTensorMetadata *> *)inputTensorMetadatas { |
| 225 | + return _inputTensorMetadatas; |
| 226 | +} |
| 227 | + |
| 228 | +- (NSDictionary<NSNumber *,ExecuTorchTensorMetadata *> *)outputTensorMetadatas { |
| 229 | + return _outputTensorMetadatas; |
| 230 | +} |
| 231 | + |
| 232 | +- (NSArray<ExecuTorchTensorMetadata *> *)attributeTensorMetadatas { |
| 233 | + return _attributeTensorMetadatas; |
| 234 | +} |
| 235 | + |
| 236 | +- (NSArray<NSNumber *> *)memoryPlannedBufferSizes { |
| 237 | + return _memoryPlannedBufferSizes; |
| 238 | +} |
| 239 | + |
| 240 | +- (NSArray<NSString *> *)backendNames { |
| 241 | + return _backendNames; |
| 242 | +} |
| 243 | + |
| 244 | +@end |
| 245 | + |
65 | 246 | @implementation ExecuTorchModule {
|
66 | 247 | std::unique_ptr<Module> _module;
|
67 | 248 | }
|
@@ -134,6 +315,19 @@ - (BOOL)isMethodLoaded:(NSString *)methodName {
|
134 | 315 | return methods;
|
135 | 316 | }
|
136 | 317 |
|
| 318 | +- (nullable ExecuTorchMethodMetadata *)methodMetadata:(NSString *)methodName |
| 319 | + error:(NSError **)error { |
| 320 | + const auto result = _module->method_meta(methodName.UTF8String); |
| 321 | + if (!result.ok()) { |
| 322 | + if (error) { |
| 323 | + *error = ExecuTorchErrorWithCode((ExecuTorchErrorCode)result.error()); |
| 324 | + } |
| 325 | + return nil; |
| 326 | + } |
| 327 | + return [[ExecuTorchMethodMetadata alloc] initWithMethodMetadata:result.get() |
| 328 | + error:error]; |
| 329 | +} |
| 330 | + |
137 | 331 | - (nullable NSArray<ExecuTorchValue *> *)executeMethod:(NSString *)methodName
|
138 | 332 | withInputs:(NSArray<ExecuTorchValue *> *)values
|
139 | 333 | error:(NSError **)error {
|
|
0 commit comments