Skip to content

Add Module execute API. #9687

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
15 changes: 15 additions & 0 deletions extension/apple/ExecuTorch/Exported/ExecuTorchModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,21 @@ __attribute__((deprecated("This API is experimental.")))
*/
- (nullable NSSet<NSString *> *)methodNames:(NSError **)error;

/**
* Executes a specific method with the provided input values.
*
* The method is loaded on demand if not already loaded.
*
* @param methodName A string representing 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 *> *)executeMethod:(NSString *)methodName
withInputs:(NSArray<ExecuTorchValue *> *)values
error:(NSError **)error
NS_SWIFT_NAME(execute(_:_:));

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

Expand Down
45 changes: 45 additions & 0 deletions extension/apple/ExecuTorch/Exported/ExecuTorchModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,27 @@
using namespace executorch::extension;
using namespace executorch::runtime;

static inline EValue toEValue(ExecuTorchValue *value) {
if (value.isTensor) {
auto *nativeTensorPtr = value.tensorValue.nativeInstance;
ET_CHECK(nativeTensorPtr);
auto nativeTensor = *reinterpret_cast<TensorPtr *>(nativeTensorPtr);
ET_CHECK(nativeTensor);
return *nativeTensor;
}
ET_CHECK_MSG(false, "Unsupported ExecuTorchValue type");
return EValue();
}

static inline ExecuTorchValue *toExecuTorchValue(EValue value) {
if (value.isTensor()) {
auto nativeInstance = make_tensor_ptr(value.toTensor());
return [ExecuTorchValue valueWithTensor:[[ExecuTorchTensor alloc] initWithNativeInstance:&nativeInstance]];
}
ET_CHECK_MSG(false, "Unsupported EValue type");
return [ExecuTorchValue new];
}

@implementation ExecuTorchModule {
std::unique_ptr<Module> _module;
}
Expand Down Expand Up @@ -94,4 +115,28 @@ - (BOOL)isMethodLoaded:(NSString *)methodName {
return methods;
}

- (nullable NSArray<ExecuTorchValue *> *)executeMethod:(NSString *)methodName
withInputs:(NSArray<ExecuTorchValue *> *)values
error:(NSError **)error {
std::vector<EValue> inputs;
inputs.reserve(values.count);
for (ExecuTorchValue *value in values) {
inputs.push_back(toEValue(value));
}
const auto result = _module->execute(methodName.UTF8String, inputs);
if (!result.ok()) {
if (error) {
*error = [NSError errorWithDomain:ExecuTorchErrorDomain
code:(NSInteger)result.error()
userInfo:nil];
}
return nil;
}
NSMutableArray<ExecuTorchValue *> *outputs = [NSMutableArray arrayWithCapacity:result->size()];
for (const auto &value : *result) {
[outputs addObject:toExecuTorchValue(value)];
}
return outputs;
}

@end
21 changes: 21 additions & 0 deletions extension/apple/ExecuTorch/__tests__/ModuleTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,25 @@ class ModuleTest: XCTestCase {
XCTAssertNoThrow(methodNames = try module.methodNames())
XCTAssertEqual(methodNames, Set(["forward"]))
}

func testExecute() {
let bundle = Bundle(for: type(of: self))
guard let modelPath = bundle.path(forResource: "add", ofType: "pte") else {
XCTFail("Couldn't find the model file")
return
}
let module = Module(filePath: modelPath)
var inputData: [Float] = [1.0]
let inputTensor = inputData.withUnsafeMutableBytes {
Tensor(bytesNoCopy: $0.baseAddress!, shape:[1], dataType: .float)
}
let inputs = [Value(inputTensor), Value(inputTensor)]
var outputs: [Value]?
XCTAssertNoThrow(outputs = try module.execute("forward", inputs))
var outputData: [Float] = [2.0]
let outputTensor = outputData.withUnsafeMutableBytes {
Tensor(bytesNoCopy: $0.baseAddress!, shape:[1], dataType: .float, shapeDynamism: .static)
}
XCTAssertEqual(outputs?[0].tensor, outputTensor)
}
}
Loading