Skip to content

Add Module forward API with overloads. #9689

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions extension/apple/ExecuTorch/Exported/ExecuTorchModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,55 @@ __attribute__((deprecated("This API is experimental.")))
error:(NSError **)error
NS_SWIFT_NAME(execute(_:_:));

/**
* Executes the "forward" method with the provided input values.
*
* This is a convenience method that calls the executeMethod with "forward" as the method name.
*
* @param values An NSArray of ExecuTorchValue objects representing the inputs.
* @param error A pointer to an NSError pointer that is set if an error occurs.
* @return An NSArray of ExecuTorchValue objects representing the outputs, or nil in case of an error.
*/
- (nullable NSArray<ExecuTorchValue *> *)forwardWithInputs:(NSArray<ExecuTorchValue *> *)values
error:(NSError **)error
NS_SWIFT_NAME(forward(_:));

/**
* Executes the "forward" method with the provided single input value.
*
* This is a convenience method that calls the executeMethod with "forward" as the method name.
*
* @param value An ExecuTorchValue object representing the input.
* @param error A pointer to an NSError pointer that is set if an error occurs.
* @return An NSArray of ExecuTorchValue objects representing the outputs, or nil in case of an error.
*/
- (nullable NSArray<ExecuTorchValue *> *)forwardWithInput:(ExecuTorchValue *)value
error:(NSError **)error
NS_SWIFT_NAME(forward(_:));

/**
* Executes the "forward" method with no inputs.
*
* This is a convenience method that calls the executeMethod with "forward" as the method name.
*
* @param error A pointer to an NSError pointer that is set if an error occurs.
* @return An NSArray of ExecuTorchValue objects representing the outputs, or nil in case of an error.
*/
- (nullable NSArray<ExecuTorchValue *> *)forward:(NSError **)error;

/**
* Executes the "forward" method with no inputs.
*
* This is a convenience method that calls the executeMethod with "forward" as the method name.
*
* @param tensors An NSArray of ExecuTorchTensor objects representing the inputs.
* @param error A pointer to an NSError pointer that is set if an error occurs.
* @return An NSArray of ExecuTorchValue objects representing the outputs, or nil in case of an error.
*/
- (nullable NSArray<ExecuTorchValue *> *)forwardWithTensors:(NSArray<ExecuTorchTensor *> *)tensors
error:(NSError **)error
NS_SWIFT_NAME(forward(_:));

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

Expand Down
31 changes: 31 additions & 0 deletions extension/apple/ExecuTorch/Exported/ExecuTorchModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,35 @@ - (BOOL)isMethodLoaded:(NSString *)methodName {
error:error];
}

- (nullable NSArray<ExecuTorchValue *> *)forwardWithInputs:(NSArray<ExecuTorchValue *> *)values
error:(NSError **)error {
return [self executeMethod:@"forward"
withInputs:values
error:error];
}

- (nullable NSArray<ExecuTorchValue *> *)forwardWithInput:(ExecuTorchValue *)value
error:(NSError **)error {
return [self executeMethod:@"forward"
withInputs:@[value]
error:error];
}

- (nullable NSArray<ExecuTorchValue *> *)forward:(NSError **)error {
return [self executeMethod:@"forward"
withInputs:@[]
error:error];
}

- (nullable NSArray<ExecuTorchValue *> *)forwardWithTensors:(NSArray<ExecuTorchTensor *> *)tensors
error:(NSError **)error {
NSMutableArray<ExecuTorchValue *> *values = [NSMutableArray arrayWithCapacity:tensors.count];
for (ExecuTorchTensor *tensor in tensors) {
[values addObject:[ExecuTorchValue valueWithTensor:tensor]];
}
return [self executeMethod:@"forward"
withInputs:values
error:error];
}

@end
2 changes: 1 addition & 1 deletion extension/apple/ExecuTorch/__tests__/ModuleTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class ModuleTest: XCTestCase {
}
let inputs = [inputTensor, inputTensor]
var outputs: [Value]?
XCTAssertNoThrow(outputs = try module.execute("forward", inputs))
XCTAssertNoThrow(outputs = try module.forward(inputs))
var outputData: [Float] = [2.0]
let outputTensor = outputData.withUnsafeMutableBytes {
Tensor(bytesNoCopy: $0.baseAddress!, shape:[1], dataType: .float, shapeDynamism: .static)
Expand Down
Loading