-
Notifications
You must be signed in to change notification settings - Fork 971
Expand file tree
/
Copy pathExecuTorch+Module.swift
More file actions
327 lines (293 loc) · 12.7 KB
/
ExecuTorch+Module.swift
File metadata and controls
327 lines (293 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/*
* 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.
*/
@_exported import ExecuTorch
public extension TensorMetadata {
/// The size of each dimension.
var shape: [Int] { __shape.map(\.intValue) }
/// The layout order of each dimension.
var dimensionOrder: [Int] { __dimensionOrder.map(\.intValue) }
}
public extension MethodMetadata {
/// The declared input tags.
var inputValueTags: [ValueTag] {
__inputValueTags.map { ValueTag(rawValue: $0.uint32Value)! }
}
/// The declared output tags.
var outputValueTags: [ValueTag] {
__outputValueTags.map { ValueTag(rawValue: $0.uint32Value)! }
}
/// A dictionary mapping each input index to its `TensorMetadata`.
var inputTensorMetadata: [Int: TensorMetadata] {
Dictionary(uniqueKeysWithValues:
__inputTensorMetadata.map { (key, value) in (key.intValue, value) }
)
}
/// A dictionary mapping each output index to its `TensorMetadata`.
var outputTensorMetadata: [Int: TensorMetadata] {
Dictionary(uniqueKeysWithValues:
__outputTensorMetadata.map { (key, value) in (key.intValue, value) }
)
}
/// The sizes of all memory-planned buffers as a native Swift array of `Int`.
var memoryPlannedBufferSizes: [Int] {
__memoryPlannedBufferSizes.map(\.intValue)
}
}
public extension Module {
/// Loads the module's program with per-delegate backend options.
///
/// The receiver retains `options` for as long as the underlying program
/// references it (lifetime tracked via ARC).
///
/// - Parameters:
/// - options: A `BackendOptionsMap` built once from a dict of
/// per-backend `BackendOption`s, e.g.
/// `try BackendOptionsMap(["CoreMLBackend": [BackendOption("compute_unit", "cpu_and_gpu")]])`.
/// - verification: The verification level to apply when loading the program.
/// - Throws: An error if loading fails.
func load(
_ options: BackendOptionsMap,
verification: ModuleVerification = .minimal
) throws {
try __load(withOptions: options, verification: verification)
}
/// Loads a specific method from the program with per-delegate backend options.
///
/// - Parameters:
/// - method: The name of the method to load.
/// - options: A `BackendOptionsMap` built once from a dict of
/// per-backend `BackendOption`s.
/// - Throws: An error if loading fails.
func load(
_ method: String,
options: BackendOptionsMap
) throws {
try __loadMethod(method, options: options)
}
}
public extension Module {
/// Executes a specific method with the provided input values.
/// The method is loaded on demand if not already loaded.
///
/// - Parameters:
/// - method: The name of the method to execute.
/// - inputs: An array of `ValueConvertible` types representing the inputs.
/// - Returns: An array of `Value` objects representing the outputs.
/// - Throws: An error if method execution fails.
func execute(_ method: String, _ inputs: [ValueConvertible]) throws -> [Value] {
try __executeMethod(method, withInputs: inputs.map { $0.asValue() } )
}
/// Executes a specific method with variadic inputs.
///
/// - Parameters:
/// - method: The name of the method to execute.
/// - inputs: A variadic list of `ValueConvertible` inputs.
/// - Returns: An array of `Value` objects representing the outputs.
/// - Throws: An error if loading or execution fails.
func execute(_ method: String, _ inputs: ValueConvertible...) throws -> [Value] {
try execute(method, inputs)
}
/// Executes the "forward" method with the provided input values.
/// The method is loaded on demand if not already loaded.
///
/// - Parameter inputs: An array of `ValueConvertible` types representing the inputs.
/// - Returns: An array of `Value` objects representing the outputs.
/// - Throws: An error if method execution fails.
func forward(_ inputs: [ValueConvertible]) throws -> [Value] {
try __executeMethod("forward", withInputs: inputs.map { $0.asValue() })
}
/// Executes the "forward" method with variadic inputs.
///
/// - Parameter inputs: A variadic list of `ValueConvertible` inputs.
/// - Returns: An array of `Value` objects representing the outputs.
/// - Throws: An error if loading or execution fails.
func forward(_ inputs: ValueConvertible...) throws -> [Value] {
try forward(inputs)
}
}
public extension Module {
/// Executes a specific method and decodes the outputs into `Output` generic type.
///
/// - Parameters:
/// - method: The name of the method to execute.
/// - inputs: An array of `ValueConvertible` inputs.
/// - Returns: An instance of `Output` decoded from the returned `[Value]`, or `nil` on mismatch.
/// - Throws: An error if loading, execution or result conversion fails.
func execute<Output: ValueSequenceConstructible>(_ method: String, _ inputs: [ValueConvertible]) throws -> Output {
try Output(__executeMethod(method, withInputs: inputs.map { $0.asValue() }))
}
/// Executes a specific method with variadic inputs and decodes into `Output` generic type.
///
/// - Parameters:
/// - method: The name of the method to execute.
/// - inputs: A variadic list of `ValueConvertible` inputs.
/// - Returns: An instance of `Output` decoded from the returned `[Value]`, or `nil` on mismatch.
/// - Throws: An error if loading, execution or result conversion fails.
func execute<Output: ValueSequenceConstructible>(_ method: String, _ inputs: ValueConvertible...) throws -> Output {
try execute(method, inputs)
}
/// Executes a specific method with a single input and decodes into `Output` generic type.
///
/// - Parameters:
/// - method: The name of the method to execute.
/// - input: A single `ValueConvertible` input.
/// - Returns: An instance of `Output` decoded from the returned `[Value]`, or `nil` on mismatch.
/// - Throws: An error if loading, execution or result conversion fails.
func execute<Output: ValueSequenceConstructible>(_ method: String, _ input: ValueConvertible) throws -> Output {
try execute(method, [input])
}
/// Executes a specific method with no inputs and decodes into `Output` generic type.
///
/// - Parameter method: The name of the method to execute.
/// - Returns: An instance of `Output` decoded from the returned `[Value]`, or `nil` on mismatch.
/// - Throws: An error if loading, execution or result conversion fails.
func execute<Output: ValueSequenceConstructible>(_ method: String) throws -> Output {
try execute(method, [])
}
/// Executes the "forward" method and decodes into `Output` generic type.
///
/// - Parameters:
/// - inputs: An array of `ValueConvertible` inputs to pass to "forward".
/// - Returns: An instance of `Output` decoded from the returned `[Value]`, or `nil` on mismatch.
/// - Throws: An error if loading, execution or result conversion fails.
func forward<Output: ValueSequenceConstructible>(_ inputs: [ValueConvertible]) throws -> Output {
try execute("forward", inputs)
}
/// Executes the "forward" method with variadic inputs and decodes into `Output` generic type.
///
/// - Parameters:
/// - inputs: A variadic list of `ValueConvertible` inputs.
/// - Returns: An instance of `Output` decoded from the returned `[Value]`, or `nil` on mismatch.
/// - Throws: An error if loading, execution or result conversion fails.
func forward<Output: ValueSequenceConstructible>(_ inputs: ValueConvertible...) throws -> Output {
try forward(inputs)
}
/// Executes the "forward" method with a single input and decodes into `Output` generic type.
///
/// - Parameters:
/// - input: A single `ValueConvertible` to pass to "forward".
/// - Returns: An instance of `Output` decoded from the returned `[Value]`, or `nil` on mismatch.
/// - Throws: An error if loading, execution or result conversion fails.
func forward<Output: ValueSequenceConstructible>(_ input: ValueConvertible) throws -> Output {
try forward([input])
}
/// Executes the "forward" method with no inputs and decodes into `Output` generic type.
///
/// - Returns: An instance of `Output` decoded from the returned `[Value]`, or `nil` on mismatch.
/// - Throws: An error if loading, execution or result conversion fails.
func forward<Output: ValueSequenceConstructible>() throws -> Output {
try execute("forward")
}
}
public extension Module {
/// Sets a single input value for a method at the specified index.
///
/// - Parameters:
/// - value: The input as a `ValueConvertible`.
/// - method: The method name.
/// - index: Zero-based input index.
/// - Throws: If setting the input fails.
func setInput(_ value: ValueConvertible, for method: String, at index: Int) throws {
try __setInput(value.asValue(), forMethod: method, at: index)
}
/// Sets a single input value for a method at index 0.
///
/// - Parameters:
/// - value: The input as a `ValueConvertible`.
/// - method: The method name.
/// - Throws: If setting the input fails.
func setInput(_ value: ValueConvertible, for method: String) throws {
try setInput(value, for: method, at: 0)
}
/// Sets a single input value for the "forward" method at the specified index.
///
/// - Parameters:
/// - value: The input as a `ValueConvertible`.
/// - index: Zero-based input index.
/// - Throws: If setting the input fails.
func setInput(_ value: ValueConvertible, at index: Int) throws {
try setInput(value, for: "forward", at: index)
}
/// Sets the first input value (index 0) for the "forward" method.
///
/// - Parameter value: The input as a `ValueConvertible`.
/// - Throws: If setting the input fails.
func setInput(_ value: ValueConvertible) throws {
try setInput(value, for: "forward", at: 0)
}
/// Sets all input values for a method.
///
/// - Parameters:
/// - values: The inputs as an array of `ValueConvertible`.
/// - method: The method name.
/// - Throws: If setting the inputs fails.
func setInputs(_ values: [ValueConvertible], for method: String) throws {
try __setInputs(values.map { $0.asValue() }, forMethod: method)
}
/// Sets all input values for the "forward" method.
///
/// - Parameter values: The inputs as an array of `ValueConvertible`.
/// - Throws: If setting the inputs fails.
func setInputs(_ values: [ValueConvertible]) throws {
try setInputs(values, for: "forward")
}
/// Sets all input values for a method using variadic arguments.
///
/// - Parameters:
/// - values: The inputs as a variadic list of `ValueConvertible`.
/// - method: The method name.
/// - Throws: If setting the inputs fails.
func setInputs(_ values: ValueConvertible..., for method: String) throws {
try setInputs(values, for: method)
}
/// Sets all input values for the "forward" method using variadic arguments.
///
/// - Parameter values: The inputs as a variadic list of `ValueConvertible`.
/// - Throws: If setting the inputs fails.
func setInputs(_ values: ValueConvertible...) throws {
try setInputs(values, for: "forward")
}
/// Sets the output location for a method at the specified index.
///
/// Only tensor outputs are supported. The provided value must wrap a tensor
/// with compatible shape and data type for the method’s output slot.
///
/// - Parameters:
/// - value: The output buffer as a `ValueConvertible` (tensor).
/// - method: The method name.
/// - index: Zero-based output index.
/// - Throws: If setting the output fails.
func setOutput(_ value: ValueConvertible, for method: String, at index: Int) throws {
try __setOutput(value.asValue(), forMethod: method, at: index)
}
/// Sets the output location for a method at index 0.
///
/// - Parameters:
/// - value: The output buffer as a `ValueConvertible` (tensor).
/// - method: The method name.
/// - Throws: If setting the output fails.
func setOutput(_ value: ValueConvertible, for method: String) throws {
try setOutput(value, for: method, at: 0)
}
/// Sets the output location for the "forward" method at the specified index.
///
/// - Parameters:
/// - value: The output buffer as a `ValueConvertible` (tensor).
/// - index: Zero-based output index.
/// - Throws: If setting the output fails.
func setOutput(_ value: ValueConvertible, at index: Int) throws {
try setOutput(value, for: "forward", at: index)
}
/// Sets the first output location (index 0) for the "forward" method.
///
/// - Parameter value: The output buffer as a `ValueConvertible` (tensor).
/// - Throws: If setting the output fails.
func setOutput(_ value: ValueConvertible) throws {
try setOutput(value, for: "forward", at: 0)
}
}