Skip to content

Commit b3873ef

Browse files
authored
Return retained objects from Module constructors.
Differential Revision: D76491602 Pull Request resolved: #11633
1 parent 4a9952e commit b3873ef

File tree

2 files changed

+43
-31
lines changed

2 files changed

+43
-31
lines changed

extension/apple/ExecuTorch/Exported/ExecuTorchModule.h

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ __attribute__((deprecated("This API is experimental.")))
189189
* @param error A pointer to an NSError pointer that is set if an error occurs.
190190
* @return An unordered set of method names, or nil in case of an error.
191191
*/
192-
- (nullable NSSet<NSString *> *)methodNames:(NSError **)error;
192+
- (nullable NSSet<NSString *> *)methodNames:(NSError **)error
193+
NS_RETURNS_RETAINED;
193194

194195
/**
195196
* Retrieves full metadata for a particular method in the loaded module.
@@ -202,7 +203,8 @@ __attribute__((deprecated("This API is experimental.")))
202203
* @return An ExecuTorchMethodMetadata object on success, or nil if the method isn’t found or a load error occurred.
203204
*/
204205
- (nullable ExecuTorchMethodMetadata *)methodMetadata:(NSString *)methodName
205-
error:(NSError **)error;
206+
error:(NSError **)error
207+
NS_RETURNS_RETAINED;
206208

207209
/**
208210
* Executes a specific method with the provided input values.
@@ -217,7 +219,8 @@ __attribute__((deprecated("This API is experimental.")))
217219
- (nullable NSArray<ExecuTorchValue *> *)executeMethod:(NSString *)methodName
218220
withInputs:(NSArray<ExecuTorchValue *> *)values
219221
error:(NSError **)error
220-
NS_REFINED_FOR_SWIFT;
222+
NS_REFINED_FOR_SWIFT
223+
NS_RETURNS_RETAINED;
221224

222225
/**
223226
* Executes a specific method with the provided single input value.
@@ -232,7 +235,8 @@ __attribute__((deprecated("This API is experimental.")))
232235
- (nullable NSArray<ExecuTorchValue *> *)executeMethod:(NSString *)methodName
233236
withInput:(ExecuTorchValue *)value
234237
error:(NSError **)error
235-
NS_SWIFT_UNAVAILABLE("");
238+
NS_SWIFT_UNAVAILABLE("")
239+
NS_RETURNS_RETAINED;
236240

237241
/**
238242
* Executes a specific method with no input values.
@@ -245,7 +249,8 @@ __attribute__((deprecated("This API is experimental.")))
245249
*/
246250
- (nullable NSArray<ExecuTorchValue *> *)executeMethod:(NSString *)methodName
247251
error:(NSError **)error
248-
NS_SWIFT_NAME(execute(_:));
252+
NS_SWIFT_NAME(execute(_:))
253+
NS_RETURNS_RETAINED;
249254

250255
/**
251256
* Executes a specific method with the provided input tensors.
@@ -260,7 +265,8 @@ __attribute__((deprecated("This API is experimental.")))
260265
- (nullable NSArray<ExecuTorchValue *> *)executeMethod:(NSString *)methodName
261266
withTensors:(NSArray<ExecuTorchTensor *> *)tensors
262267
error:(NSError **)error
263-
NS_SWIFT_UNAVAILABLE("");
268+
NS_SWIFT_UNAVAILABLE("")
269+
NS_RETURNS_RETAINED;
264270

265271
/**
266272
* Executes a specific method with the provided single input tensor.
@@ -275,7 +281,8 @@ __attribute__((deprecated("This API is experimental.")))
275281
- (nullable NSArray<ExecuTorchValue *> *)executeMethod:(NSString *)methodName
276282
withTensor:(ExecuTorchTensor *)tensor
277283
error:(NSError **)error
278-
NS_SWIFT_UNAVAILABLE("");
284+
NS_SWIFT_UNAVAILABLE("")
285+
NS_RETURNS_RETAINED;
279286

280287
/**
281288
* Executes the "forward" method with the provided input values.
@@ -288,7 +295,8 @@ __attribute__((deprecated("This API is experimental.")))
288295
*/
289296
- (nullable NSArray<ExecuTorchValue *> *)forwardWithInputs:(NSArray<ExecuTorchValue *> *)values
290297
error:(NSError **)error
291-
NS_SWIFT_UNAVAILABLE("");
298+
NS_SWIFT_UNAVAILABLE("")
299+
NS_RETURNS_RETAINED;
292300

293301
/**
294302
* Executes the "forward" method with the provided single input value.
@@ -301,7 +309,8 @@ __attribute__((deprecated("This API is experimental.")))
301309
*/
302310
- (nullable NSArray<ExecuTorchValue *> *)forwardWithInput:(ExecuTorchValue *)value
303311
error:(NSError **)error
304-
NS_SWIFT_UNAVAILABLE("");
312+
NS_SWIFT_UNAVAILABLE("")
313+
NS_RETURNS_RETAINED;
305314

306315
/**
307316
* Executes the "forward" method with no inputs.
@@ -311,7 +320,8 @@ __attribute__((deprecated("This API is experimental.")))
311320
* @param error A pointer to an NSError pointer that is set if an error occurs.
312321
* @return An NSArray of ExecuTorchValue objects representing the outputs, or nil in case of an error.
313322
*/
314-
- (nullable NSArray<ExecuTorchValue *> *)forward:(NSError **)error;
323+
- (nullable NSArray<ExecuTorchValue *> *)forward:(NSError **)error
324+
NS_RETURNS_RETAINED;
315325

316326
/**
317327
* Executes the "forward" method with the provided input tensors.
@@ -324,7 +334,8 @@ __attribute__((deprecated("This API is experimental.")))
324334
*/
325335
- (nullable NSArray<ExecuTorchValue *> *)forwardWithTensors:(NSArray<ExecuTorchTensor *> *)tensors
326336
error:(NSError **)error
327-
NS_SWIFT_UNAVAILABLE("");
337+
NS_SWIFT_UNAVAILABLE("")
338+
NS_RETURNS_RETAINED;
328339

329340
/**
330341
* Executes the "forward" method with the provided single input tensor.
@@ -337,7 +348,8 @@ __attribute__((deprecated("This API is experimental.")))
337348
*/
338349
- (nullable NSArray<ExecuTorchValue *> *)forwardWithTensor:(ExecuTorchTensor *)tensor
339350
error:(NSError **)error
340-
NS_SWIFT_UNAVAILABLE("");
351+
NS_SWIFT_UNAVAILABLE("")
352+
NS_RETURNS_RETAINED;
341353

342354
+ (instancetype)new NS_UNAVAILABLE;
343355
- (instancetype)init NS_UNAVAILABLE;

extension/apple/ExecuTorch/Exported/ExecuTorchModule.mm

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ static inline EValue toEValue(ExecuTorchValue *value) {
3838
return EValue();
3939
}
4040

41-
static inline ExecuTorchValue *toExecuTorchValue(EValue value) {
41+
static inline ExecuTorchValue *toExecuTorchValue(EValue value) NS_RETURNS_RETAINED {
4242
if (value.isTensor()) {
4343
auto nativeInstance = make_tensor_ptr(value.toTensor());
4444
return [ExecuTorchValue valueWithTensor:[[ExecuTorchTensor alloc] initWithNativeInstance:&nativeInstance]];
@@ -118,20 +118,20 @@ - (nullable instancetype)initWithMethodMetadata:(const MethodMeta &)methodMeta
118118
error:(NSError **)error {
119119
self = [super init];
120120
if (self) {
121-
_name = @(methodMeta.name());
121+
_name = [[NSString alloc] initWithUTF8String:methodMeta.name()];
122122
const NSInteger inputCount = methodMeta.num_inputs();
123123
const NSInteger outputCount = methodMeta.num_outputs();
124124
const NSInteger attributeCount = methodMeta.num_attributes();
125125
const NSInteger memoryPlannedBufferCount = methodMeta.num_memory_planned_buffers();
126126
const NSInteger backendCount = methodMeta.num_backends();
127127
_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];
128+
_inputValueTags = [[NSMutableArray alloc] initWithCapacity:inputCount];
129+
_outputValueTags = [[NSMutableArray alloc] initWithCapacity:outputCount];
130+
_inputTensorMetadatas = [NSMutableDictionary new];
131+
_outputTensorMetadatas = [NSMutableDictionary new];
132+
_attributeTensorMetadatas = [[NSMutableArray alloc] initWithCapacity:attributeCount];
133+
_memoryPlannedBufferSizes = [[NSMutableArray alloc] initWithCapacity:memoryPlannedBufferCount];
134+
_backendNames = [[NSMutableArray alloc] initWithCapacity:backendCount];
135135

136136
for (NSInteger index = 0; index < inputCount; ++index) {
137137
auto result = methodMeta.input_tag(index);
@@ -206,7 +206,7 @@ - (nullable instancetype)initWithMethodMetadata:(const MethodMeta &)methodMeta
206206
}
207207
return nil;
208208
}
209-
NSString *backendName = [NSString stringWithUTF8String:result.get()];
209+
NSString *backendName = [[NSString alloc] initWithUTF8String:result.get()];
210210
[_backendNames addObject:backendName];
211211
}
212212
}
@@ -308,9 +308,9 @@ - (BOOL)isMethodLoaded:(NSString *)methodName {
308308
}
309309
return nil;
310310
}
311-
NSMutableSet<NSString *> *methods = [NSMutableSet setWithCapacity:result->size()];
311+
NSMutableSet<NSString *> *methods = [[NSMutableSet alloc] initWithCapacity:result->size()];
312312
for (const auto &name : *result) {
313-
[methods addObject:(NSString *)@(name.c_str())];
313+
[methods addObject:(NSString *)[[NSString alloc] initWithUTF8String:name.c_str()]];
314314
}
315315
return methods;
316316
}
@@ -343,7 +343,7 @@ - (nullable ExecuTorchMethodMetadata *)methodMetadata:(NSString *)methodName
343343
}
344344
return nil;
345345
}
346-
NSMutableArray<ExecuTorchValue *> *outputs = [NSMutableArray arrayWithCapacity:result->size()];
346+
NSMutableArray<ExecuTorchValue *> *outputs = [[NSMutableArray alloc] initWithCapacity:result->size()];
347347
for (const auto &value : *result) {
348348
[outputs addObject:toExecuTorchValue(value)];
349349
}
@@ -354,7 +354,7 @@ - (nullable ExecuTorchMethodMetadata *)methodMetadata:(NSString *)methodName
354354
withInput:(ExecuTorchValue *)value
355355
error:(NSError **)error {
356356
return [self executeMethod:methodName
357-
withInputs:@[value]
357+
withInputs:[[NSArray alloc] initWithObjects:value, nil]
358358
error:error];
359359
}
360360

@@ -368,7 +368,7 @@ - (nullable ExecuTorchMethodMetadata *)methodMetadata:(NSString *)methodName
368368
- (nullable NSArray<ExecuTorchValue *> *)executeMethod:(NSString *)methodName
369369
withTensors:(NSArray<ExecuTorchTensor *> *)tensors
370370
error:(NSError **)error {
371-
NSMutableArray<ExecuTorchValue *> *values = [NSMutableArray arrayWithCapacity:tensors.count];
371+
NSMutableArray<ExecuTorchValue *> *values = [[NSMutableArray alloc] initWithCapacity:tensors.count];
372372
for (ExecuTorchTensor *tensor in tensors) {
373373
[values addObject:[ExecuTorchValue valueWithTensor:tensor]];
374374
}
@@ -381,7 +381,7 @@ - (nullable ExecuTorchMethodMetadata *)methodMetadata:(NSString *)methodName
381381
withTensor:(ExecuTorchTensor *)tensor
382382
error:(NSError **)error {
383383
return [self executeMethod:methodName
384-
withInputs:@[[ExecuTorchValue valueWithTensor:tensor]]
384+
withInputs:[[NSArray alloc] initWithObjects:[ExecuTorchValue valueWithTensor:tensor], nil]
385385
error:error];
386386
}
387387

@@ -395,7 +395,7 @@ - (nullable ExecuTorchMethodMetadata *)methodMetadata:(NSString *)methodName
395395
- (nullable NSArray<ExecuTorchValue *> *)forwardWithInput:(ExecuTorchValue *)value
396396
error:(NSError **)error {
397397
return [self executeMethod:@"forward"
398-
withInputs:@[value]
398+
withInputs:[[NSArray alloc] initWithObjects:value, nil]
399399
error:error];
400400
}
401401

@@ -407,7 +407,7 @@ - (nullable ExecuTorchMethodMetadata *)methodMetadata:(NSString *)methodName
407407

408408
- (nullable NSArray<ExecuTorchValue *> *)forwardWithTensors:(NSArray<ExecuTorchTensor *> *)tensors
409409
error:(NSError **)error {
410-
NSMutableArray<ExecuTorchValue *> *values = [NSMutableArray arrayWithCapacity:tensors.count];
410+
NSMutableArray<ExecuTorchValue *> *values = [[NSMutableArray alloc] initWithCapacity:tensors.count];
411411
for (ExecuTorchTensor *tensor in tensors) {
412412
[values addObject:[ExecuTorchValue valueWithTensor:tensor]];
413413
}
@@ -419,7 +419,7 @@ - (nullable ExecuTorchMethodMetadata *)methodMetadata:(NSString *)methodName
419419
- (nullable NSArray<ExecuTorchValue *> *)forwardWithTensor:(ExecuTorchTensor *)tensor
420420
error:(NSError **)error {
421421
return [self executeMethod:@"forward"
422-
withInputs:@[[ExecuTorchValue valueWithTensor:tensor]]
422+
withInputs:[[NSArray alloc] initWithObjects:[ExecuTorchValue valueWithTensor:tensor], nil]
423423
error:error];
424424
}
425425

0 commit comments

Comments
 (0)