Skip to content

Commit 3176e9f

Browse files
shoumikhinfacebook-github-bot
authored andcommitted
Add Module methodName API.
Summary: #8363 Reviewed By: mergennachin Differential Revision: D71918193
1 parent 1d93152 commit 3176e9f

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

extension/apple/ExecuTorch/Exported/ExecuTorchModule.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ __attribute__((deprecated("This API is experimental.")))
104104
*/
105105
- (BOOL)isMethodLoaded:(NSString *)methodName NS_SWIFT_NAME(isLoaded(_:));
106106

107+
/**
108+
* Retrieves the set of method names available in the loaded program.
109+
*
110+
* The method names are returned as an unordered set of strings. The program and methods
111+
* are loaded as needed.
112+
*
113+
* @param error A pointer to an NSError pointer that is set if an error occurs.
114+
* @return An unordered set of method names, or nil in case of an error.
115+
*/
116+
- (nullable NSSet<NSString *> *)methodNames:(NSError **)error;
117+
107118
+ (instancetype)new NS_UNAVAILABLE;
108119
- (instancetype)init NS_UNAVAILABLE;
109120

extension/apple/ExecuTorch/Exported/ExecuTorchModule.mm

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,21 @@ - (BOOL)isMethodLoaded:(NSString *)methodName {
7777
return _module->is_method_loaded(methodName.UTF8String);
7878
}
7979

80+
- (nullable NSSet<NSString *> *)methodNames:(NSError **)error {
81+
const auto result = _module->method_names();
82+
if (!result.ok()) {
83+
if (error) {
84+
*error = [NSError errorWithDomain:ExecuTorchErrorDomain
85+
code:(NSInteger)result.error()
86+
userInfo:nil];
87+
}
88+
return nil;
89+
}
90+
NSMutableSet<NSString *> *methods = [NSMutableSet setWithCapacity:result->size()];
91+
for (const auto &name : *result) {
92+
[methods addObject:(NSString *)@(name.c_str())];
93+
}
94+
return methods;
95+
}
96+
8097
@end

extension/apple/ExecuTorch/__tests__/ModuleTest.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,16 @@ class ModuleTest: XCTestCase {
3939
XCTAssertNoThrow(try module.load("forward"))
4040
XCTAssertTrue(module.isLoaded("forward"))
4141
}
42+
43+
func testMethodNames() {
44+
let bundle = Bundle(for: type(of: self))
45+
guard let modelPath = bundle.path(forResource: "add", ofType: "pte") else {
46+
XCTFail("Couldn't find the model file")
47+
return
48+
}
49+
let module = Module(filePath: modelPath)
50+
var methodNames: Set<String>?
51+
XCTAssertNoThrow(methodNames = try module.methodNames())
52+
XCTAssertEqual(methodNames, Set(["forward"]))
53+
}
4254
}

0 commit comments

Comments
 (0)