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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
//! This file has been automatically generated by `objc2`'s `header-translator`.
//! DO NOT EDIT
use core::ffi::*;
use core::ptr::NonNull;
use objc2::__framework_prelude::*;
use objc2_foundation::*;
use crate::*;
extern_class!(
/// A training graph created from one or more MLCGraph objects
/// plus additional layers added directly to the training graph.
///
/// See also [Apple's documentation](https://developer.apple.com/documentation/mlcompute/mlctraininggraph?language=objc)
#[unsafe(super(MLCGraph, NSObject))]
#[derive(Debug, PartialEq, Eq, Hash)]
#[cfg(feature = "MLCGraph")]
#[deprecated]
pub struct MLCTrainingGraph;
);
#[cfg(feature = "MLCGraph")]
extern_conformance!(
unsafe impl NSObjectProtocol for MLCTrainingGraph {}
);
#[cfg(feature = "MLCGraph")]
impl MLCTrainingGraph {
extern_methods!(
#[cfg(feature = "MLCOptimizer")]
/// The optimizer to be used with the training graph
#[deprecated]
#[unsafe(method(optimizer))]
#[unsafe(method_family = none)]
pub unsafe fn optimizer(&self) -> Option<Retained<MLCOptimizer>>;
/// Returns the total size in bytes of device memory used for all intermediate tensors
/// for forward, gradient passes and optimizer update for all layers in the training graph.
/// We recommend executing an iteration before checking the device memory size as
/// the buffers needed get allocated when the corresponding pass such as gradient,
/// optimizer update is executed.
///
/// Returns: A NSUInteger value
#[deprecated]
#[unsafe(method(deviceMemorySize))]
#[unsafe(method_family = none)]
pub unsafe fn deviceMemorySize(&self) -> NSUInteger;
#[cfg(all(feature = "MLCLayer", feature = "MLCOptimizer"))]
/// Create a training graph
///
/// Parameter `graphObjects`: The layers from these graph objects will be added to the training graph
///
/// Parameter `lossLayer`: The loss layer to use. The loss layer can also be added to the training graph
/// using nodeWithLayer:sources:lossLabels
///
/// Parameter `optimizer`: The optimizer to use
///
/// Returns: A new training graph object
#[deprecated]
#[unsafe(method(graphWithGraphObjects:lossLayer:optimizer:))]
#[unsafe(method_family = none)]
pub unsafe fn graphWithGraphObjects_lossLayer_optimizer(
graph_objects: &NSArray<MLCGraph>,
loss_layer: Option<&MLCLayer>,
optimizer: Option<&MLCOptimizer>,
) -> Retained<Self>;
#[cfg(feature = "MLCTensor")]
/// Add the list of inputs to the training graph
///
/// Parameter `inputs`: The inputs
///
/// Parameter `lossLabels`: The loss label inputs
///
/// Returns: A boolean indicating success or failure
#[deprecated]
#[unsafe(method(addInputs:lossLabels:))]
#[unsafe(method_family = none)]
pub unsafe fn addInputs_lossLabels(
&self,
inputs: &NSDictionary<NSString, MLCTensor>,
loss_labels: Option<&NSDictionary<NSString, MLCTensor>>,
) -> bool;
#[cfg(feature = "MLCTensor")]
/// Add the list of inputs to the training graph
///
/// Each input, loss label or label weights tensor is identified by a NSString.
/// When the training graph is executed, this NSString is used to identify which data object
/// should be as input data for each tensor whose device memory needs to be updated
/// before the graph is executed.
///
/// Parameter `inputs`: The inputs
///
/// Parameter `lossLabels`: The loss label inputs
///
/// Parameter `lossLabelWeights`: The loss label weights
///
/// Returns: A boolean indicating success or failure
#[deprecated]
#[unsafe(method(addInputs:lossLabels:lossLabelWeights:))]
#[unsafe(method_family = none)]
pub unsafe fn addInputs_lossLabels_lossLabelWeights(
&self,
inputs: &NSDictionary<NSString, MLCTensor>,
loss_labels: Option<&NSDictionary<NSString, MLCTensor>>,
loss_label_weights: Option<&NSDictionary<NSString, MLCTensor>>,
) -> bool;
#[cfg(feature = "MLCTensor")]
/// Add the list of outputs to the training graph
///
/// Parameter `outputs`: The outputs
///
/// Returns: A boolean indicating success or failure
#[deprecated]
#[unsafe(method(addOutputs:))]
#[unsafe(method_family = none)]
pub unsafe fn addOutputs(&self, outputs: &NSDictionary<NSString, MLCTensor>) -> bool;
#[cfg(feature = "MLCTensor")]
/// Add the list of tensors whose contributions are not to be taken when computing gradients during gradient pass
///
/// Parameter `tensors`: The list of tensors
///
/// Returns: A boolean indicating success or failure
#[deprecated]
#[unsafe(method(stopGradientForTensors:))]
#[unsafe(method_family = none)]
pub unsafe fn stopGradientForTensors(&self, tensors: &NSArray<MLCTensor>) -> bool;
#[cfg(all(feature = "MLCDevice", feature = "MLCTypes"))]
/// Compile the training graph for a device.
///
/// Parameter `options`: The compiler options to use when compiling the training graph
///
/// Parameter `device`: The MLCDevice object
///
/// Returns: A boolean indicating success or failure
#[deprecated]
#[unsafe(method(compileWithOptions:device:))]
#[unsafe(method_family = none)]
pub unsafe fn compileWithOptions_device(
&self,
options: MLCGraphCompilationOptions,
device: &MLCDevice,
) -> bool;
#[cfg(all(
feature = "MLCDevice",
feature = "MLCTensor",
feature = "MLCTensorData",
feature = "MLCTypes"
))]
/// Compile the training graph for a device.
///
/// Specifying the list of constant tensors when we compile the graph allows MLCompute to perform additional optimizations at compile time.
///
/// Parameter `options`: The compiler options to use when compiling the training graph
///
/// Parameter `device`: The MLCDevice object
///
/// Parameter `inputTensors`: The list of input tensors that are constants
///
/// Parameter `inputTensorsData`: The tensor data to be used with these constant input tensors
///
/// Returns: A boolean indicating success or failure
#[unsafe(method(compileWithOptions:device:inputTensors:inputTensorsData:))]
#[unsafe(method_family = none)]
pub unsafe fn compileWithOptions_device_inputTensors_inputTensorsData(
&self,
options: MLCGraphCompilationOptions,
device: &MLCDevice,
input_tensors: Option<&NSDictionary<NSString, MLCTensor>>,
input_tensors_data: Option<&NSDictionary<NSString, MLCTensorData>>,
) -> bool;
#[cfg(feature = "MLCOptimizer")]
/// Compile the optimizer to be used with a training graph.
///
/// Typically the optimizer to be used with a training graph is specifed when the training graph is created using
/// graphWithGraphObjects:lossLayer:optimizer. The optimizer will be compiled in when compileWithOptions:device
/// is called if an optimizer is specified with the training graph. In the case where the optimizer to be used is not known
/// when the graph is created or compiled, this method can be used to associate and compile a training graph with an optimizer.
///
/// Parameter `optimizer`: The MLCOptimizer object
///
/// Returns: A boolean indicating success or failure
#[deprecated]
#[unsafe(method(compileOptimizer:))]
#[unsafe(method_family = none)]
pub unsafe fn compileOptimizer(&self, optimizer: &MLCOptimizer) -> bool;
/// Link mutiple training graphs
///
/// This is used to link subsequent training graphs with first training sub-graph.
/// This method should be used when we have tensors shared by one or more layers in multiple sub-graphs
///
/// Parameter `graphs`: The list of training graphs to link
///
/// Returns: A boolean indicating success or failure
#[deprecated]
#[unsafe(method(linkWithGraphs:))]
#[unsafe(method_family = none)]
pub unsafe fn linkWithGraphs(&self, graphs: &NSArray<MLCTrainingGraph>) -> bool;
#[cfg(feature = "MLCTensor")]
/// Get the gradient tensor for an input tensor
///
/// Parameter `input`: The input tensor
///
/// Returns: The gradient tensor
#[deprecated]
#[unsafe(method(gradientTensorForInput:))]
#[unsafe(method_family = none)]
pub unsafe fn gradientTensorForInput(
&self,
input: &MLCTensor,
) -> Option<Retained<MLCTensor>>;
#[cfg(all(feature = "MLCLayer", feature = "MLCTensor"))]
/// Get the source gradient tensors for a layer in the training graph
///
/// Parameter `layer`: A layer in the training graph
///
/// Returns: A list of tensors
#[deprecated]
#[unsafe(method(sourceGradientTensorsForLayer:))]
#[unsafe(method_family = none)]
pub unsafe fn sourceGradientTensorsForLayer(
&self,
layer: &MLCLayer,
) -> Retained<NSArray<MLCTensor>>;
#[cfg(all(feature = "MLCLayer", feature = "MLCTensor"))]
/// Get the result gradient tensors for a layer in the training graph
///
/// Parameter `layer`: A layer in the training graph
///
/// Returns: A list of tensors
#[deprecated]
#[unsafe(method(resultGradientTensorsForLayer:))]
#[unsafe(method_family = none)]
pub unsafe fn resultGradientTensorsForLayer(
&self,
layer: &MLCLayer,
) -> Retained<NSArray<MLCTensor>>;
#[cfg(all(feature = "MLCLayer", feature = "MLCTensor"))]
/// Get the gradient data for a trainable parameter associated with a layer
///
/// This can be used to get the gradient data for weights or biases parameters associated with a convolution,
/// fully connected or convolution transpose layer
///
/// Parameter `parameter`: The updatable parameter associated with the layer
///
/// Parameter `layer`: A layer in the training graph. Must be one of the following:
/// - MLCConvolutionLayer
/// - MLCFullyConnectedLayer
/// - MLCBatchNormalizationLayer
/// - MLCInstanceNormalizationLayer
/// - MLCGroupNormalizationLayer
/// - MLCLayerNormalizationLayer
/// - MLCEmbeddingLayer
/// - MLCMultiheadAttentionLayer
///
/// Returns: The gradient data. Will return nil if the layer is marked as not trainable or if
/// training graph is not executed with separate calls to forward and gradient passes.
#[deprecated]
#[unsafe(method(gradientDataForParameter:layer:))]
#[unsafe(method_family = none)]
pub unsafe fn gradientDataForParameter_layer(
&self,
parameter: &MLCTensor,
layer: &MLCLayer,
) -> Option<Retained<NSData>>;
#[cfg(feature = "MLCTensor")]
/// Allocate an entry for a user specified gradient for a tensor
///
/// Parameter `tensor`: A result tensor produced by a layer in the training graph
/// that is input to some user specified code and will need to
/// provide a user gradient during the gradient pass.
///
/// Returns: A gradient tensor
#[deprecated]
#[unsafe(method(allocateUserGradientForTensor:))]
#[unsafe(method_family = none)]
pub unsafe fn allocateUserGradientForTensor(
&self,
tensor: &MLCTensor,
) -> Option<Retained<MLCTensor>>;
#[cfg(all(
feature = "MLCTensor",
feature = "MLCTensorData",
feature = "MLCTypes",
feature = "block2"
))]
/// Execute the training graph (forward, gradient and optimizer update) with given source and label data
///
/// Execute the training graph with given source and label data. If an optimizer is specified, the optimizer update is applied.
/// If MLCExecutionOptionsSynchronous is specified in 'options', this method returns after the graph has been executed.
/// Otherwise, this method returns after the graph has been queued for execution. The completion handler is called after the graph
/// has finished execution.
///
/// Parameter `inputsData`: The data objects to use for inputs
///
/// Parameter `lossLabelsData`: The data objects to use for loss labels
///
/// Parameter `lossLabelWeightsData`: The data objects to use for loss label weights
///
/// Parameter `batchSize`: The batch size to use. For a graph where batch size changes between layers this value must be 0.
///
/// Parameter `options`: The execution options
///
/// Parameter `completionHandler`: The completion handler
///
/// Returns: A boolean indicating success or failure
///
/// # Safety
///
/// `completion_handler` must be a valid pointer or null.
#[deprecated]
#[unsafe(method(executeWithInputsData:lossLabelsData:lossLabelWeightsData:batchSize:options:completionHandler:))]
#[unsafe(method_family = none)]
pub unsafe fn executeWithInputsData_lossLabelsData_lossLabelWeightsData_batchSize_options_completionHandler(
&self,
inputs_data: &NSDictionary<NSString, MLCTensorData>,
loss_labels_data: Option<&NSDictionary<NSString, MLCTensorData>>,
loss_label_weights_data: Option<&NSDictionary<NSString, MLCTensorData>>,
batch_size: NSUInteger,
options: MLCExecutionOptions,
completion_handler: MLCGraphCompletionHandler,
) -> bool;
#[cfg(all(
feature = "MLCTensor",
feature = "MLCTensorData",
feature = "MLCTypes",
feature = "block2"
))]
/// Execute the training graph (forward, gradient and optimizer update) with given source and label data
///
/// Parameter `inputsData`: The data objects to use for inputs
///
/// Parameter `lossLabelsData`: The data objects to use for loss labels
///
/// Parameter `lossLabelWeightsData`: The data objects to use for loss label weights
///
/// Parameter `outputsData`: The data objects to use for outputs
///
/// Parameter `batchSize`: The batch size to use. For a graph where batch size changes between layers this value must be 0.
///
/// Parameter `options`: The execution options
///
/// Parameter `completionHandler`: The completion handler
///
/// Returns: A boolean indicating success or failure
///
/// # Safety
///
/// `completion_handler` must be a valid pointer or null.
#[deprecated]
#[unsafe(method(executeWithInputsData:lossLabelsData:lossLabelWeightsData:outputsData:batchSize:options:completionHandler:))]
#[unsafe(method_family = none)]
pub unsafe fn executeWithInputsData_lossLabelsData_lossLabelWeightsData_outputsData_batchSize_options_completionHandler(
&self,
inputs_data: &NSDictionary<NSString, MLCTensorData>,
loss_labels_data: Option<&NSDictionary<NSString, MLCTensorData>>,
loss_label_weights_data: Option<&NSDictionary<NSString, MLCTensorData>>,
outputs_data: Option<&NSDictionary<NSString, MLCTensorData>>,
batch_size: NSUInteger,
options: MLCExecutionOptions,
completion_handler: MLCGraphCompletionHandler,
) -> bool;
#[cfg(all(feature = "MLCTensor", feature = "MLCTypes", feature = "block2"))]
/// Execute the forward pass of the training graph
///
/// Parameter `batchSize`: The batch size to use. For a graph where batch size changes between layers this value must be 0.
///
/// Parameter `options`: The execution options
///
/// Parameter `completionHandler`: The completion handler
///
/// Returns: A boolean indicating success or failure
///
/// # Safety
///
/// `completion_handler` must be a valid pointer or null.
#[deprecated]
#[unsafe(method(executeForwardWithBatchSize:options:completionHandler:))]
#[unsafe(method_family = none)]
pub unsafe fn executeForwardWithBatchSize_options_completionHandler(
&self,
batch_size: NSUInteger,
options: MLCExecutionOptions,
completion_handler: MLCGraphCompletionHandler,
) -> bool;
#[cfg(all(
feature = "MLCTensor",
feature = "MLCTensorData",
feature = "MLCTypes",
feature = "block2"
))]
/// Execute the forward pass for the training graph
///
/// Parameter `batchSize`: The batch size to use. For a graph where batch size changes between layers this value must be 0.
///
/// Parameter `options`: The execution options
///
/// Parameter `outputsData`: The data objects to use for outputs
///
/// Parameter `completionHandler`: The completion handler
///
/// Returns: A boolean indicating success or failure
///
/// # Safety
///
/// `completion_handler` must be a valid pointer or null.
#[deprecated]
#[unsafe(method(executeForwardWithBatchSize:options:outputsData:completionHandler:))]
#[unsafe(method_family = none)]
pub unsafe fn executeForwardWithBatchSize_options_outputsData_completionHandler(
&self,
batch_size: NSUInteger,
options: MLCExecutionOptions,
outputs_data: Option<&NSDictionary<NSString, MLCTensorData>>,
completion_handler: MLCGraphCompletionHandler,
) -> bool;
#[cfg(all(feature = "MLCTensor", feature = "MLCTypes", feature = "block2"))]
/// Execute the gradient pass of the training graph
///
/// Parameter `batchSize`: The batch size to use. For a graph where batch size changes between layers this value must be 0.
///
/// Parameter `options`: The execution options
///
/// Parameter `completionHandler`: The completion handler
///
/// Returns: A boolean indicating success or failure
///
/// # Safety
///
/// `completion_handler` must be a valid pointer or null.
#[deprecated]
#[unsafe(method(executeGradientWithBatchSize:options:completionHandler:))]
#[unsafe(method_family = none)]
pub unsafe fn executeGradientWithBatchSize_options_completionHandler(
&self,
batch_size: NSUInteger,
options: MLCExecutionOptions,
completion_handler: MLCGraphCompletionHandler,
) -> bool;
#[cfg(all(
feature = "MLCTensor",
feature = "MLCTensorData",
feature = "MLCTypes",
feature = "block2"
))]
/// Execute the gradient pass of the training graph
///
/// Parameter `batchSize`: The batch size to use. For a graph where batch size changes between layers this value must be 0.
///
/// Parameter `options`: The execution options
///
/// Parameter `outputsData`: The data objects to use for outputs
///
/// Parameter `completionHandler`: The completion handler
///
/// Returns: A boolean indicating success or failure
///
/// # Safety
///
/// `completion_handler` must be a valid pointer or null.
#[deprecated]
#[unsafe(method(executeGradientWithBatchSize:options:outputsData:completionHandler:))]
#[unsafe(method_family = none)]
pub unsafe fn executeGradientWithBatchSize_options_outputsData_completionHandler(
&self,
batch_size: NSUInteger,
options: MLCExecutionOptions,
outputs_data: Option<&NSDictionary<NSString, MLCTensorData>>,
completion_handler: MLCGraphCompletionHandler,
) -> bool;
#[cfg(all(feature = "MLCTensor", feature = "MLCTypes", feature = "block2"))]
/// Execute the optimizer update pass of the training graph
///
/// Parameter `options`: The execution options
///
/// Parameter `completionHandler`: The completion handler
///
/// Returns: A boolean indicating success or failure
///
/// # Safety
///
/// `completion_handler` must be a valid pointer or null.
#[deprecated]
#[unsafe(method(executeOptimizerUpdateWithOptions:completionHandler:))]
#[unsafe(method_family = none)]
pub unsafe fn executeOptimizerUpdateWithOptions_completionHandler(
&self,
options: MLCExecutionOptions,
completion_handler: MLCGraphCompletionHandler,
) -> bool;
/// Synchronize updates (weights/biases from convolution, fully connected and LSTM layers, tensor parameters)
/// from device memory to host memory.
#[deprecated]
#[unsafe(method(synchronizeUpdates))]
#[unsafe(method_family = none)]
pub unsafe fn synchronizeUpdates(&self);
#[cfg(feature = "MLCTensorParameter")]
/// Set the input tensor parameters that also will be updated by the optimizer
///
/// These represent the list of input tensors to be updated when we execute the optimizer update
/// Weights, bias or beta, gamma tensors are not included in this list. MLCompute automatically
/// adds them to the parameter list based on whether the layer is marked as updatable or not.
///
/// Parameter `parameters`: The list of input tensors to be updated by the optimizer
///
/// Returns: A boolean indicating success or failure
#[deprecated]
#[unsafe(method(setTrainingTensorParameters:))]
#[unsafe(method_family = none)]
pub unsafe fn setTrainingTensorParameters(
&self,
parameters: &NSArray<MLCTensorParameter>,
) -> bool;
#[cfg(all(
feature = "MLCTensor",
feature = "MLCTensorData",
feature = "MLCTensorOptimizerDeviceData"
))]
/// Associates the given optimizer data and device data buffers with the tensor.
/// Returns true if the data is successfully associated with the tensor and copied to the device.
///
/// The caller must guarantee the lifetime of the underlying memory of
/// `data`for the entirety of the tensor's
/// lifetime. The
/// `deviceData`buffers are allocated by MLCompute. This method must be called
/// before executeOptimizerUpdateWithOptions or executeWithInputsData is called for the training graph.
/// We recommend using this method instead of using [MLCTensor bindOptimizerData] especially if the
/// optimizer update is being called multiple times for each batch.
///
/// Parameter `data`: The optimizer data to be associated with the tensor
///
/// Parameter `deviceData`: The optimizer device data to be associated with the tensor
///
/// Parameter `tensor`: The tensor
///
/// Returns: A Boolean value indicating whether the data is successfully associated with the tensor .
#[deprecated]
#[unsafe(method(bindOptimizerData:deviceData:withTensor:))]
#[unsafe(method_family = none)]
pub unsafe fn bindOptimizerData_deviceData_withTensor(
&self,
data: &NSArray<MLCTensorData>,
device_data: Option<&NSArray<MLCTensorOptimizerDeviceData>>,
tensor: &MLCTensor,
) -> bool;
);
}
/// Methods declared on superclass `MLCGraph`.
#[cfg(feature = "MLCGraph")]
impl MLCTrainingGraph {
extern_methods!(
/// Creates a new graph.
///
/// Returns: A new graph.
#[deprecated]
#[unsafe(method(graph))]
#[unsafe(method_family = none)]
pub unsafe fn graph() -> Retained<Self>;
);
}
/// Methods declared on superclass `NSObject`.
#[cfg(feature = "MLCGraph")]
impl MLCTrainingGraph {
extern_methods!(
#[unsafe(method(init))]
#[unsafe(method_family = init)]
pub unsafe fn init(this: Allocated<Self>) -> Retained<Self>;
#[unsafe(method(new))]
#[unsafe(method_family = new)]
pub unsafe fn new() -> Retained<Self>;
);
}