Skip to content

Commit 70ab5ab

Browse files
authored
Introduce set input/output API to Module. (#13363)
Summary: . Differential Revision: D80149417
1 parent 14b0088 commit 70ab5ab

File tree

4 files changed

+423
-0
lines changed

4 files changed

+423
-0
lines changed

extension/apple/ExecuTorch/Exported/ExecuTorch+Module.swift

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,123 @@ public extension Module {
176176
try execute("forward")
177177
}
178178
}
179+
180+
@available(*, deprecated, message: "This API is experimental.")
181+
public extension Module {
182+
/// Sets a single input value for a method at the specified index.
183+
///
184+
/// - Parameters:
185+
/// - value: The input as a `ValueConvertible`.
186+
/// - method: The method name.
187+
/// - index: Zero-based input index.
188+
/// - Throws: If setting the input fails.
189+
func setInput(_ value: ValueConvertible, for method: String, at index: Int) throws {
190+
try __setInput(value.asValue(), forMethod: method, at: index)
191+
}
192+
193+
/// Sets a single input value for a method at index 0.
194+
///
195+
/// - Parameters:
196+
/// - value: The input as a `ValueConvertible`.
197+
/// - method: The method name.
198+
/// - Throws: If setting the input fails.
199+
func setInput(_ value: ValueConvertible, for method: String) throws {
200+
try setInput(value, for: method, at: 0)
201+
}
202+
203+
/// Sets a single input value for the "forward" method at the specified index.
204+
///
205+
/// - Parameters:
206+
/// - value: The input as a `ValueConvertible`.
207+
/// - index: Zero-based input index.
208+
/// - Throws: If setting the input fails.
209+
func setInput(_ value: ValueConvertible, at index: Int) throws {
210+
try setInput(value, for: "forward", at: index)
211+
}
212+
213+
/// Sets the first input value (index 0) for the "forward" method.
214+
///
215+
/// - Parameter value: The input as a `ValueConvertible`.
216+
/// - Throws: If setting the input fails.
217+
func setInput(_ value: ValueConvertible) throws {
218+
try setInput(value, for: "forward", at: 0)
219+
}
220+
221+
/// Sets all input values for a method.
222+
///
223+
/// - Parameters:
224+
/// - values: The inputs as an array of `ValueConvertible`.
225+
/// - method: The method name.
226+
/// - Throws: If setting the inputs fails.
227+
func setInputs(_ values: [ValueConvertible], for method: String) throws {
228+
try __setInputs(values.map { $0.asValue() }, forMethod: method)
229+
}
230+
231+
/// Sets all input values for the "forward" method.
232+
///
233+
/// - Parameter values: The inputs as an array of `ValueConvertible`.
234+
/// - Throws: If setting the inputs fails.
235+
func setInputs(_ values: [ValueConvertible]) throws {
236+
try setInputs(values, for: "forward")
237+
}
238+
239+
/// Sets all input values for a method using variadic arguments.
240+
///
241+
/// - Parameters:
242+
/// - values: The inputs as a variadic list of `ValueConvertible`.
243+
/// - method: The method name.
244+
/// - Throws: If setting the inputs fails.
245+
func setInputs(_ values: ValueConvertible..., for method: String) throws {
246+
try setInputs(values, for: method)
247+
}
248+
249+
/// Sets all input values for the "forward" method using variadic arguments.
250+
///
251+
/// - Parameter values: The inputs as a variadic list of `ValueConvertible`.
252+
/// - Throws: If setting the inputs fails.
253+
func setInputs(_ values: ValueConvertible...) throws {
254+
try setInputs(values, for: "forward")
255+
}
256+
257+
/// Sets the output location for a method at the specified index.
258+
///
259+
/// Only tensor outputs are supported. The provided value must wrap a tensor
260+
/// with compatible shape and data type for the method’s output slot.
261+
///
262+
/// - Parameters:
263+
/// - value: The output buffer as a `ValueConvertible` (tensor).
264+
/// - method: The method name.
265+
/// - index: Zero-based output index.
266+
/// - Throws: If setting the output fails.
267+
func setOutput(_ value: ValueConvertible, for method: String, at index: Int) throws {
268+
try __setOutput(value.asValue(), forMethod: method, at: index)
269+
}
270+
271+
/// Sets the output location for a method at index 0.
272+
///
273+
/// - Parameters:
274+
/// - value: The output buffer as a `ValueConvertible` (tensor).
275+
/// - method: The method name.
276+
/// - Throws: If setting the output fails.
277+
func setOutput(_ value: ValueConvertible, for method: String) throws {
278+
try setOutput(value, for: method, at: 0)
279+
}
280+
281+
/// Sets the output location for the "forward" method at the specified index.
282+
///
283+
/// - Parameters:
284+
/// - value: The output buffer as a `ValueConvertible` (tensor).
285+
/// - index: Zero-based output index.
286+
/// - Throws: If setting the output fails.
287+
func setOutput(_ value: ValueConvertible, at index: Int) throws {
288+
try setOutput(value, for: "forward", at: index)
289+
}
290+
291+
/// Sets the first output location (index 0) for the "forward" method.
292+
///
293+
/// - Parameter value: The output buffer as a `ValueConvertible` (tensor).
294+
/// - Throws: If setting the output fails.
295+
func setOutput(_ value: ValueConvertible) throws {
296+
try setOutput(value, for: "forward", at: 0)
297+
}
298+
}

extension/apple/ExecuTorch/Exported/ExecuTorchModule.h

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,145 @@ __attribute__((deprecated("This API is experimental.")))
358358
NS_SWIFT_UNAVAILABLE("")
359359
NS_RETURNS_RETAINED;
360360

361+
/**
362+
* Sets a single input value for the "forward" method at index 0.
363+
*
364+
* @param value The input value.
365+
* @param error On failure, set to an NSError describing the issue.
366+
* @return YES on success; NO otherwise.
367+
*/
368+
- (BOOL)setInput:(ExecuTorchValue *)value
369+
error:(NSError **)error NS_SWIFT_UNAVAILABLE("");
370+
371+
/**
372+
* Sets a single input value for the "forward" method at the specified index.
373+
*
374+
* @param value The input value.
375+
* @param index Zero-based input index.
376+
* @param error On failure, set to an NSError describing the issue.
377+
* @return YES on success; NO otherwise.
378+
*/
379+
- (BOOL)setInput:(ExecuTorchValue *)value
380+
atIndex:(NSInteger)index
381+
error:(NSError **)error NS_SWIFT_UNAVAILABLE("");
382+
383+
/**
384+
* Sets a single input value for the specified method at index 0.
385+
*
386+
* @param value The input value.
387+
* @param methodName The method name.
388+
* @param error On failure, set to an NSError describing the issue.
389+
* @return YES on success; NO otherwise.
390+
*/
391+
- (BOOL)setInput:(ExecuTorchValue *)value
392+
forMethod:(NSString *)methodName
393+
error:(NSError **)error NS_SWIFT_UNAVAILABLE("");
394+
395+
/**
396+
* Sets a single input value for the specified method at the given index.
397+
*
398+
* The module retains the provided value to keep its backing storage alive
399+
* until the value is overwritten or the module is deallocated.
400+
*
401+
* @param value The input value.
402+
* @param methodName The method name.
403+
* @param index Zero-based input index.
404+
* @param error On failure, set to an NSError describing the issue.
405+
* @return YES on success; NO otherwise.
406+
*/
407+
- (BOOL)setInput:(ExecuTorchValue *)value
408+
forMethod:(NSString *)methodName
409+
atIndex:(NSInteger)index
410+
error:(NSError **)error NS_REFINED_FOR_SWIFT;
411+
412+
/**
413+
* Sets all input values for the "forward" method.
414+
*
415+
* The number and types of values must match the method’s declared inputs.
416+
*
417+
* @param values The input values, one per declared input.
418+
* @param error On failure, set to an NSError describing the issue.
419+
* @return YES on success; NO otherwise.
420+
*/
421+
- (BOOL)setInputs:(NSArray<ExecuTorchValue *> *)values
422+
error:(NSError **)error NS_SWIFT_UNAVAILABLE("");
423+
424+
/**
425+
* Sets all input values for the specified method.
426+
*
427+
* The module retains the provided values to keep their backing storage alive
428+
* until the values are overwritten or the module is deallocated.
429+
*
430+
* @param values The input values, one per declared input.
431+
* @param methodName The method name.
432+
* @param error On failure, set to an NSError describing the issue.
433+
* @return YES on success; NO otherwise.
434+
*/
435+
- (BOOL)setInputs:(NSArray<ExecuTorchValue *> *)values
436+
forMethod:(NSString *)methodName
437+
error:(NSError **)error NS_REFINED_FOR_SWIFT;
438+
439+
/**
440+
* Sets the output buffer for the "forward" method at index 0.
441+
*
442+
* Only tensor outputs are supported. The provided value must wrap a tensor
443+
* compatible with the method’s output slot.
444+
*
445+
* @param value The output buffer (must wrap a tensor).
446+
* @param error On failure, set to an NSError describing the issue.
447+
* @return YES on success; NO otherwise.
448+
*/
449+
- (BOOL)setOutput:(ExecuTorchValue *)value
450+
error:(NSError **)error NS_SWIFT_UNAVAILABLE("");
451+
452+
/**
453+
* Sets the output buffer for the "forward" method at the specified index.
454+
*
455+
* Only tensor outputs are supported. The provided value must wrap a tensor
456+
* compatible with the method’s output slot.
457+
*
458+
* @param value The output buffer (must wrap a tensor).
459+
* @param index Zero-based output index.
460+
* @param error On failure, set to an NSError describing the issue.
461+
* @return YES on success; NO otherwise.
462+
*/
463+
- (BOOL)setOutput:(ExecuTorchValue *)value
464+
atIndex:(NSInteger)index
465+
error:(NSError **)error NS_SWIFT_UNAVAILABLE("");
466+
467+
/**
468+
* Sets the output buffer for the specified method at index 0.
469+
*
470+
* Only tensor outputs are supported. The provided value must wrap a tensor
471+
* compatible with the method’s output slot.
472+
*
473+
* @param value The output buffer (must wrap a tensor).
474+
* @param methodName The method name.
475+
* @param error On failure, set to an NSError describing the issue.
476+
* @return YES on success; NO otherwise.
477+
*/
478+
- (BOOL)setOutput:(ExecuTorchValue *)value
479+
forMethod:(NSString *)methodName
480+
error:(NSError **)error NS_SWIFT_UNAVAILABLE("");
481+
482+
/**
483+
* Sets the output buffer for the specified method at the given index.
484+
*
485+
* The module retains the provided value to keep its backing storage alive
486+
* until the value is overwritten or the module is deallocated.
487+
* Only tensor outputs are supported.
488+
*
489+
* @param value The output buffer (must wrap a tensor).
490+
* @param methodName The method name.
491+
* @param index Zero-based output index.
492+
* @param error On failure, set to an NSError describing the issue.
493+
* @return YES on success; NO otherwise.
494+
*/
495+
- (BOOL)setOutput:(ExecuTorchValue *)value
496+
forMethod:(NSString *)methodName
497+
atIndex:(NSInteger)index
498+
error:(NSError **)error NS_REFINED_FOR_SWIFT;
499+
361500
+ (instancetype)new NS_UNAVAILABLE;
362501
- (instancetype)init NS_UNAVAILABLE;
363502

0 commit comments

Comments
 (0)