Skip to content

Add Module loadMethod API. #9685

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
18 changes: 18 additions & 0 deletions extension/apple/ExecuTorch/Exported/ExecuTorchModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,24 @@ __attribute__((deprecated("This API is experimental.")))
*/
- (BOOL)isLoaded;

/**
* Loads a specific method from the program.
*
* @param methodName A string representing the name of the method to load.
* @param error A pointer to an NSError pointer that is set if an error occurs.
* @return YES if the method was successfully loaded; otherwise, NO.
*/
- (BOOL)loadMethod:(NSString *)methodName
error:(NSError **)error NS_SWIFT_NAME(load(_:));

/**
* Checks if a specific method is loaded.
*
* @param methodName A string representing the method name.
* @return YES if the method is loaded; otherwise, NO.
*/
- (BOOL)isMethodLoaded:(NSString *)methodName NS_SWIFT_NAME(isLoaded(_:));

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

Expand Down
18 changes: 18 additions & 0 deletions extension/apple/ExecuTorch/Exported/ExecuTorchModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,22 @@ - (BOOL)isLoaded {
return _module->is_loaded();
}

- (BOOL)loadMethod:(NSString *)methodName
error:(NSError **)error {
const auto errorCode = _module->load_method(methodName.UTF8String);
if (errorCode != Error::Ok) {
if (error) {
*error = [NSError errorWithDomain:ExecuTorchErrorDomain
code:(NSInteger)errorCode
userInfo:nil];
}
return NO;
}
return YES;
}

- (BOOL)isMethodLoaded:(NSString *)methodName {
return _module->is_method_loaded(methodName.UTF8String);
}

@end
11 changes: 11 additions & 0 deletions extension/apple/ExecuTorch/__tests__/ModuleTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,15 @@ class ModuleTest: XCTestCase {
XCTAssertNoThrow(try module.load())
XCTAssertTrue(module.isLoaded())
}

func testLoadMethod() {
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)
XCTAssertNoThrow(try module.load("forward"))
XCTAssertTrue(module.isLoaded("forward"))
}
}
Loading