|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#import <Foundation/Foundation.h> |
| 10 | + |
| 11 | +#import "ExecuTorchBackendOption.h" |
| 12 | + |
| 13 | +NS_ASSUME_NONNULL_BEGIN |
| 14 | + |
| 15 | +/** |
| 16 | + * An immutable, opaque container for per-delegate load-time configuration, |
| 17 | + * built from a dictionary mapping backend identifiers to arrays of |
| 18 | + * `ExecuTorchBackendOption` objects. |
| 19 | + * |
| 20 | + * # Lifetime |
| 21 | + * |
| 22 | + * Once a `BackendOptionsMap` is passed to a `Module` load call, the `Module` |
| 23 | + * **retains** it for as long as the underlying program references it. The |
| 24 | + * caller does not need to manage this lifetime manually — ARC handles it. |
| 25 | + * |
| 26 | + * # Reuse |
| 27 | + * |
| 28 | + * The same `BackendOptionsMap` instance can be reused across multiple `Module`s |
| 29 | + * and across multiple load calls. Build it once, pass it many times. |
| 30 | + * |
| 31 | + * # Validation |
| 32 | + * |
| 33 | + * Validation (option-key length, string-value length, integer 32-bit range) |
| 34 | + * happens at construction time. If the input dictionary contains an invalid |
| 35 | + * entry, the initializer returns `nil` and populates the out-error. |
| 36 | + * |
| 37 | + * @note The current C++ runtime stores integer option values as 32-bit `int`. |
| 38 | + * Passing an integer outside `[INT32_MIN, INT32_MAX]` will cause the |
| 39 | + * initializer to fail with `Error::InvalidArgument`. |
| 40 | + */ |
| 41 | +NS_SWIFT_NAME(BackendOptionsMap) |
| 42 | +__attribute__((objc_subclassing_restricted)) |
| 43 | +@interface ExecuTorchBackendOptionsMap : NSObject |
| 44 | + |
| 45 | +/** |
| 46 | + * Creates a backend options map from a dictionary of per-backend options. |
| 47 | + * |
| 48 | + * @param options A dictionary mapping backend identifiers (e.g. "CoreMLBackend") |
| 49 | + * to arrays of `ExecuTorchBackendOption` objects configuring that backend. |
| 50 | + * @param error On failure, populated with an `NSError` describing the validation |
| 51 | + * problem (e.g. invalid integer range). |
| 52 | + * @return A new instance, or `nil` if validation fails. |
| 53 | + */ |
| 54 | +- (nullable instancetype)initWithOptions:(NSDictionary<NSString *, NSArray<ExecuTorchBackendOption *> *> *)options |
| 55 | + error:(NSError **)error |
| 56 | + NS_DESIGNATED_INITIALIZER NS_SWIFT_NAME(init(options:)); |
| 57 | + |
| 58 | +/** |
| 59 | + * Convenience class factory mirroring `-initWithOptions:error:`. |
| 60 | + */ |
| 61 | ++ (nullable instancetype)mapWithOptions:(NSDictionary<NSString *, NSArray<ExecuTorchBackendOption *> *> *)options |
| 62 | + error:(NSError **)error |
| 63 | + NS_RETURNS_RETAINED; |
| 64 | + |
| 65 | +/** |
| 66 | + * The options the receiver was constructed with, exposed as a deep-immutable |
| 67 | + * snapshot dictionary captured at construction time. Useful for debugging and |
| 68 | + * round-tripping. |
| 69 | + */ |
| 70 | +@property (nonatomic, readonly) NSDictionary<NSString *, NSArray<ExecuTorchBackendOption *> *> *options; |
| 71 | + |
| 72 | ++ (instancetype)new NS_UNAVAILABLE; |
| 73 | +- (instancetype)init NS_UNAVAILABLE; |
| 74 | + |
| 75 | +@end |
| 76 | + |
| 77 | +NS_ASSUME_NONNULL_END |
0 commit comments