-
Notifications
You must be signed in to change notification settings - Fork 971
Add BackendOption loading to swift bindings (#18855) #18855
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
Open
metascroy
wants to merge
6
commits into
pytorch:main
Choose a base branch
from
metascroy:export-D100710833
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,241
−8
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
extension/apple/ExecuTorch/Exported/ExecuTorchBackendOption.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| #import <Foundation/Foundation.h> | ||
|
|
||
| NS_ASSUME_NONNULL_BEGIN | ||
|
|
||
| /** | ||
| * Enum to define the type of a backend option value. | ||
| */ | ||
| typedef NS_ENUM(NSInteger, ExecuTorchBackendOptionType) { | ||
| ExecuTorchBackendOptionTypeBoolean, | ||
| ExecuTorchBackendOptionTypeInteger, | ||
| ExecuTorchBackendOptionTypeString, | ||
| } NS_SWIFT_NAME(BackendOptionType); | ||
|
|
||
| /** | ||
| * Represents a single key-value configuration option for a backend. | ||
| * | ||
| * Backend options are used to pass per-delegate configuration (e.g., compute | ||
| * unit, thread count, cache directory) when loading a module. Each option has | ||
| * a string key and a typed value (boolean, integer, or string). | ||
| */ | ||
| NS_SWIFT_NAME(BackendOption) | ||
| __attribute__((objc_subclassing_restricted)) | ||
| @interface ExecuTorchBackendOption : NSObject | ||
|
|
||
| /** The option key name (e.g. "compute_unit", "num_threads"). */ | ||
| @property (nonatomic, readonly) NSString *key; | ||
|
|
||
| /** The type of the option value. */ | ||
| @property (nonatomic, readonly) ExecuTorchBackendOptionType type; | ||
|
|
||
| /** The boolean value. Only valid when type is Boolean. */ | ||
| @property (nonatomic, readonly) BOOL boolValue; | ||
|
|
||
| /** The integer value. Only valid when type is Integer. */ | ||
| @property (nonatomic, readonly) NSInteger intValue; | ||
|
|
||
| /** The string value. Only valid when type is String. */ | ||
| @property (nullable, nonatomic, readonly) NSString *stringValue; | ||
|
|
||
| /** | ||
| * Creates a backend option with a boolean value. | ||
| * | ||
| * @param key The option key. | ||
| * @param value The boolean value. | ||
| * @return A new ExecuTorchBackendOption instance. | ||
| */ | ||
| + (instancetype)optionWithKey:(NSString *)key | ||
| booleanValue:(BOOL)value | ||
| NS_SWIFT_NAME(init(_:_:)) | ||
| NS_RETURNS_RETAINED; | ||
|
|
||
| /** | ||
| * Creates a backend option with an integer value. | ||
| * | ||
| * @param key The option key. | ||
| * @param value The integer value. | ||
| * @return A new ExecuTorchBackendOption instance. | ||
| */ | ||
| + (instancetype)optionWithKey:(NSString *)key | ||
| integerValue:(NSInteger)value | ||
| NS_SWIFT_NAME(init(_:_:)) | ||
| NS_RETURNS_RETAINED; | ||
|
|
||
| /** | ||
| * Creates a backend option with a string value. | ||
| * | ||
| * @param key The option key. | ||
| * @param value The string value. | ||
| * @return A new ExecuTorchBackendOption instance. | ||
| */ | ||
| + (instancetype)optionWithKey:(NSString *)key | ||
| stringValue:(NSString *)value | ||
| NS_SWIFT_NAME(init(_:_:)) | ||
| NS_RETURNS_RETAINED; | ||
|
|
||
| + (instancetype)new NS_UNAVAILABLE; | ||
| - (instancetype)init NS_UNAVAILABLE; | ||
|
|
||
| @end | ||
|
|
||
| NS_ASSUME_NONNULL_END |
137 changes: 137 additions & 0 deletions
137
extension/apple/ExecuTorch/Exported/ExecuTorchBackendOption.mm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| /* | ||
| * Copyright (c) Meta Platforms, Inc. and affiliates. | ||
| * All rights reserved. | ||
| * | ||
| * This source code is licensed under the BSD-style license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|
|
||
| #import "ExecuTorchBackendOption.h" | ||
|
|
||
| @implementation ExecuTorchBackendOption { | ||
| NSString *_key; | ||
| ExecuTorchBackendOptionType _type; | ||
| BOOL _boolValue; | ||
| NSInteger _intValue; | ||
| NSString *_stringValue; | ||
| } | ||
|
|
||
| - (instancetype)initWithKey:(NSString *)key | ||
| booleanValue:(BOOL)value { | ||
| self = [super init]; | ||
| if (self) { | ||
| _key = [key copy]; | ||
| _type = ExecuTorchBackendOptionTypeBoolean; | ||
| _boolValue = value; | ||
| } | ||
| return self; | ||
| } | ||
|
|
||
| - (instancetype)initWithKey:(NSString *)key | ||
| integerValue:(NSInteger)value { | ||
| self = [super init]; | ||
| if (self) { | ||
| _key = [key copy]; | ||
| _type = ExecuTorchBackendOptionTypeInteger; | ||
| _intValue = value; | ||
| } | ||
| return self; | ||
| } | ||
|
|
||
| - (instancetype)initWithKey:(NSString *)key | ||
| stringValue:(NSString *)value { | ||
| self = [super init]; | ||
| if (self) { | ||
| _key = [key copy]; | ||
| _type = ExecuTorchBackendOptionTypeString; | ||
| _stringValue = [value copy]; | ||
| } | ||
| return self; | ||
| } | ||
|
|
||
| + (instancetype)optionWithKey:(NSString *)key | ||
| booleanValue:(BOOL)value { | ||
| return [[self alloc] initWithKey:key booleanValue:value]; | ||
| } | ||
|
|
||
| + (instancetype)optionWithKey:(NSString *)key | ||
| integerValue:(NSInteger)value { | ||
| return [[self alloc] initWithKey:key integerValue:value]; | ||
| } | ||
|
|
||
| + (instancetype)optionWithKey:(NSString *)key | ||
| stringValue:(NSString *)value { | ||
| return [[self alloc] initWithKey:key stringValue:value]; | ||
| } | ||
|
|
||
| #pragma mark - NSObject | ||
|
|
||
| - (NSString *)description { | ||
| switch (_type) { | ||
| case ExecuTorchBackendOptionTypeBoolean: | ||
| return [NSString stringWithFormat:@"<%@ %@=%@ (bool)>", | ||
| NSStringFromClass([self class]), _key, _boolValue ? @"true" : @"false"]; | ||
| case ExecuTorchBackendOptionTypeInteger: | ||
| return [NSString stringWithFormat:@"<%@ %@=%ld (int)>", | ||
| NSStringFromClass([self class]), _key, (long)_intValue]; | ||
| case ExecuTorchBackendOptionTypeString: | ||
| return [NSString stringWithFormat:@"<%@ %@=%@ (string)>", | ||
| NSStringFromClass([self class]), _key, | ||
| _stringValue ? [NSString stringWithFormat:@"\"%@\"", _stringValue] : @"(null)"]; | ||
| } | ||
| return [super description]; | ||
| } | ||
|
|
||
| - (NSString *)debugDescription { | ||
| return [self description]; | ||
| } | ||
|
|
||
| - (BOOL)isEqual:(id)object { | ||
| if (self == object) { | ||
| return YES; | ||
| } | ||
| if (![object isKindOfClass:[ExecuTorchBackendOption class]]) { | ||
| return NO; | ||
| } | ||
| ExecuTorchBackendOption *other = (ExecuTorchBackendOption *)object; | ||
| if (_type != other.type || ![_key isEqualToString:other.key]) { | ||
| return NO; | ||
| } | ||
| switch (_type) { | ||
| case ExecuTorchBackendOptionTypeBoolean: | ||
| return _boolValue == other.boolValue; | ||
| case ExecuTorchBackendOptionTypeInteger: | ||
| return _intValue == other.intValue; | ||
| case ExecuTorchBackendOptionTypeString: { | ||
| // Both are non-null when type is String (init enforces it), but be | ||
| // defensive in case of subclass/manual misuse. | ||
| NSString *otherString = other.stringValue; | ||
| if (_stringValue == otherString) { | ||
| return YES; | ||
| } | ||
| if (_stringValue == nil || otherString == nil) { | ||
| return NO; | ||
| } | ||
| return [_stringValue isEqualToString:otherString]; | ||
| } | ||
| } | ||
| return NO; | ||
| } | ||
|
|
||
| - (NSUInteger)hash { | ||
| NSUInteger h = _key.hash ^ (NSUInteger)_type; | ||
| switch (_type) { | ||
| case ExecuTorchBackendOptionTypeBoolean: | ||
| h ^= (NSUInteger)(_boolValue ? 1 : 0); | ||
| break; | ||
| case ExecuTorchBackendOptionTypeInteger: | ||
| h ^= (NSUInteger)_intValue; | ||
| break; | ||
| case ExecuTorchBackendOptionTypeString: | ||
| h ^= _stringValue.hash; | ||
| break; | ||
| } | ||
| return h; | ||
| } | ||
|
|
||
| @end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Module.load(_ options: BackendOptionsMap, verification:)here, plus the existingloadMethod:error:(bridged to Swift asload(_:)atExecuTorchModule.h:233), produce an autocomplete collision:module.load(someMap)andmodule.load("forward")both compile cleanly with no first-positional label hint.Suggest renaming the first label:
load(options:)andload(_ method:, options:)(i.e. add the label tooptions:here and on the sibling at line 76). One keystroke at the call site, eliminates a real ambiguity for anyone using autocomplete.