Skip to content
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
37 changes: 37 additions & 0 deletions extension/apple/ExecuTorch/Exported/ExecuTorchModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ typedef NS_ENUM(NSInteger, ExecuTorchModuleLoadMode) {
ExecuTorchModuleLoadModeMmapUseMlockIgnoreErrors,
} NS_SWIFT_NAME(ModuleLoadMode);

/**
* Enum to define the verification level used when loading a module.
* Values can be a subset, but must numerically match exactly those defined in
* runtime/executor/program.h
*/
typedef NS_ENUM(uint8_t, ExecuTorchVerification) {
ExecuTorchVerificationMinimal,
ExecuTorchVerificationInternalConsistency,
} NS_SWIFT_NAME(ModuleVerification);

/**
* Represents a module that encapsulates an ExecuTorch program.
* This class is a facade for loading programs and executing methods within them.
Expand Down Expand Up @@ -49,6 +59,33 @@ __attribute__((deprecated("This API is experimental.")))
*/
- (instancetype)initWithFilePath:(NSString *)filePath;

/**
* Loads the module’s program using the specified verification level.
*
* @param verification The verification level to apply when loading the program.
* @param error A pointer to an NSError pointer that will be set if an error occurs.
* @return YES if the program was successfully loaded; otherwise, NO.
*/
- (BOOL)loadWithVerification:(ExecuTorchVerification)verification
error:(NSError **)error;

/**
* Loads the module’s program using minimal verification.
*
* This is a convenience overload that defaults the verification level to Minimal.
*
* @param error A pointer to an NSError pointer that will be set if an error occurs.
* @return YES if the program was successfully loaded; otherwise, NO.
*/
- (BOOL)load:(NSError **)error;

/**
* Checks if the module is loaded.
*
* @return YES if the module's program is loaded; otherwise, NO.
*/
- (BOOL)isLoaded;

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

Expand Down
24 changes: 24 additions & 0 deletions extension/apple/ExecuTorch/Exported/ExecuTorchModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#import <executorch/extension/tensor/tensor.h>

using namespace executorch::extension;
using namespace executorch::runtime;

@implementation ExecuTorchModule {
std::unique_ptr<Module> _module;
Expand All @@ -35,4 +36,27 @@ - (instancetype)initWithFilePath:(NSString *)filePath {
return [self initWithFilePath:filePath loadMode:ExecuTorchModuleLoadModeFile];
}

- (BOOL)loadWithVerification:(ExecuTorchVerification)verification
error:(NSError **)error {
const auto errorCode = _module->load(static_cast<Program::Verification>(verification));
if (errorCode != Error::Ok) {
if (error) {
*error = [NSError errorWithDomain:ExecuTorchErrorDomain
code:(NSInteger)errorCode
userInfo:nil];
}
return NO;
}
return YES;
}

- (BOOL)load:(NSError **)error {
return [self loadWithVerification:ExecuTorchVerificationMinimal
error:error];
}

- (BOOL)isLoaded {
return _module->is_loaded();
}

@end
7 changes: 5 additions & 2 deletions extension/apple/ExecuTorch/__tests__/ModuleTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@ class ModuleTest: XCTestCase {
return Bundle(for: type(of: self))
#endif
}
func test() throws {

func testLoad() {
guard let modelPath = resourceBundle.path(forResource: "add", ofType: "pte") else {
XCTFail("Couldn't find the model file")
return
}
let module = Module(filePath: modelPath)
XCTAssertNoThrow(try module.load())
XCTAssertTrue(module.isLoaded())
}
}
Loading