<!--
{
  "availability" : [
    "iOS: 14.0.0 - 17.4.0",
    "iPadOS: 14.0.0 - 17.4.0",
    "macCatalyst: 14.0.0 - 17.4.0",
    "macOS: 11.0.0 - 14.3.0",
    "tvOS: 14.0.0 - 17.4.0"
  ],
  "documentType" : "symbol",
  "framework" : "MLCompute",
  "identifier" : "/documentation/MLCompute/MLCTrainingGraph",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "ML Compute"
    ],
    "preciseIdentifier" : "c:objc(cs)MLCTrainingGraph"
  },
  "title" : "MLCTrainingGraph"
}
-->

# MLCTrainingGraph

A training graph that you create from one or more graph objects plus additional layers you add directly to the training graph.

```
class MLCTrainingGraph
```

## Overview

The framework provides a family of graph-execution methods to execute a full training iteration, and methods to execute the forward pass, the gradient pass, and optimizer update, individually.

Use one of the `execute` methods to execute a full training iteration to accelerate an ML model represented as a single training graph.

Use one of the `executeForward`, `executeGradient`, or `executeOptimizerUpdatemethods` to accelerate an ML library that separates the forward pass, gradient pass, and optimizer update as separate phases.

## Topics

### Creating Training Graphs

[`init(graphObjects:lossLayer:optimizer:)`](/documentation/MLCompute/MLCTrainingGraph/init(graphObjects:lossLayer:optimizer:))

Creates a training graph with the layers from the graph objects, loss layer, and optimizer you specify.

[Optimizers](/documentation/MLCompute/optimizers)

Create an optimizer to use with the training graph.

[`MLCTensorParameter`](/documentation/MLCompute/MLCTensorParameter)

A tensor parameter object.

### Preparing Training Graphs

[`addInputs(_:lossLabels:)`](/documentation/MLCompute/MLCTrainingGraph/addInputs(_:lossLabels:))

Adds the inputs and loss label inputs that you specify to the training graph.

[`addInputs(_:lossLabels:lossLabelWeights:)`](/documentation/MLCompute/MLCTrainingGraph/addInputs(_:lossLabels:lossLabelWeights:))

Adds the inputs, loss labels, and loss label weights that you specify to the training graph.

[`addOutputs(_:)`](/documentation/MLCompute/MLCTrainingGraph/addOutputs(_:))

Adds the outputs to the training graph you specify.

[`stopGradient(for:)`](/documentation/MLCompute/MLCTrainingGraph/stopGradient(for:))

Adds the tensors that you specify, to indicate which contributions the graph excludes when computing gradients during gradient pass.

[`compileOptimizer(_:)`](/documentation/MLCompute/MLCTrainingGraph/compileOptimizer(_:))

Compiles the optimizer to use with a training graph you specify.

[`compile(options:device:)`](/documentation/MLCompute/MLCTrainingGraph/compile(options:device:))

Compiles the training graph for the options and device you specify.

[`compile(options:device:inputTensors:inputTensorsData:)`](/documentation/MLCompute/MLCTrainingGraph/compile(options:device:inputTensors:inputTensorsData:))

Compiles the training graph for the options, device, and input tensors you specify.

[`link(with:)`](/documentation/MLCompute/MLCTrainingGraph/link(with:))

Links the training graphs you specify.

[`allocateUserGradient(for:)`](/documentation/MLCompute/MLCTrainingGraph/allocateUserGradient(for:))

Allocates an entry for a gradient for the result tensor you specify.

[`MLCGraphCompilationOptions`](/documentation/MLCompute/MLCGraphCompilationOptions)

A bitmask that specifies the options you use when compiling a graph.

### Executing Training Iterations

[`execute(inputsData:lossLabelsData:lossLabelWeightsData:batchSize:options:completionHandler:)`](/documentation/MLCompute/MLCTrainingGraph/execute(inputsData:lossLabelsData:lossLabelWeightsData:batchSize:options:completionHandler:))

Executes the training graph with the input data, batch size, execution options, and completion handler you specify.

[`execute(inputsData:lossLabelsData:lossLabelWeightsData:outputsData:batchSize:options:completionHandler:)`](/documentation/MLCompute/MLCTrainingGraph/execute(inputsData:lossLabelsData:lossLabelWeightsData:outputsData:batchSize:options:completionHandler:))

Executes the training graph with the input data, output data, batch size, execution options, and completion handler that you specify.

[`synchronizeUpdates()`](/documentation/MLCompute/MLCTrainingGraph/synchronizeUpdates())

Synchronizes updates from device memory.

[`setTrainingTensorParameters(_:)`](/documentation/MLCompute/MLCTrainingGraph/setTrainingTensorParameters(_:))

Sets the input tensor parameters, which the optimizer then updates.

[`MLCExecutionOptions`](/documentation/MLCompute/MLCExecutionOptions)

A bitmask that specifies the options you use when executing a graph.

[`MLCGraphCompletionHandler`](/documentation/MLCompute/MLCGraphCompletionHandler)

A callback completion handler you execute when a graph finishes execution.

### Executing Forward, Gradient, and Optimizer Updates

[`executeForward(batchSize:options:completionHandler:)`](/documentation/MLCompute/MLCTrainingGraph/executeForward(batchSize:options:completionHandler:))

Executes the forward pass of the training graph with the batch size, execution options, and completion handler you specify.

[`executeForward(batchSize:options:outputsData:completionHandler:)`](/documentation/MLCompute/MLCTrainingGraph/executeForward(batchSize:options:outputsData:completionHandler:))

Executes the forward pass of the training graph with the batch size, execution options, output data, and completion handler you specify.

[`executeForward(batchSize:options:outputsData:)`](/documentation/MLCompute/MLCTrainingGraph/executeForward(batchSize:options:outputsData:))

Executes the forward pass of the training graph with the batch size, execution options, and output data you specify.

[`executeGradient(batchSize:options:completionHandler:)`](/documentation/MLCompute/MLCTrainingGraph/executeGradient(batchSize:options:completionHandler:))

Executes the gradient pass of the training graph with the batch size, execution options, and completion handler you specify.

[`executeGradient(batchSize:options:outputsData:completionHandler:)`](/documentation/MLCompute/MLCTrainingGraph/executeGradient(batchSize:options:outputsData:completionHandler:))

Executes the gradient pass of the training graph with the batch size, execution options, output data, and completion handler you specify.

[`executeGradient(batchSize:options:outputsData:)`](/documentation/MLCompute/MLCTrainingGraph/executeGradient(batchSize:options:outputsData:))

Executes the gradient pass of the training graph with the batch size, execution options, and output data you specify.

[`executeOptimizerUpdate(options:completionHandler:)`](/documentation/MLCompute/MLCTrainingGraph/executeOptimizerUpdate(options:completionHandler:))

Executes the optimizer update pass of the training graph with the execution options and completion handler you specify.

[`executeOptimizerUpdate(options:)`](/documentation/MLCompute/MLCTrainingGraph/executeOptimizerUpdate(options:))

Executes the optimizer update pass of the training graph with the execution options you specify.

[`synchronizeUpdates()`](/documentation/MLCompute/MLCTrainingGraph/synchronizeUpdates())

Synchronizes updates from device memory.

[`setTrainingTensorParameters(_:)`](/documentation/MLCompute/MLCTrainingGraph/setTrainingTensorParameters(_:))

Sets the input tensor parameters, which the optimizer then updates.

[`MLCExecutionOptions`](/documentation/MLCompute/MLCExecutionOptions)

A bitmask that specifies the options you use when executing a graph.

[`MLCGraphCompletionHandler`](/documentation/MLCompute/MLCGraphCompletionHandler)

A callback completion handler you execute when a graph finishes execution.

### Inspecting Training Graphs

[`bindOptimizerData(_:deviceData:with:)`](/documentation/MLCompute/MLCTrainingGraph/bindOptimizerData(_:deviceData:with:))

Associates the optimizer and device data you specify along with the tensor.

[`optimizer`](/documentation/MLCompute/MLCTrainingGraph/optimizer)

The optimizer to use with the training graph.

[`deviceMemorySize`](/documentation/MLCompute/MLCTrainingGraph/deviceMemorySize)

The device memory size in bytes for all intermediate tensors for forward, gradient passes, and optimizer updates for all layers in the training graph.

[`gradientTensor(forInput:)`](/documentation/MLCompute/MLCTrainingGraph/gradientTensor(forInput:))

Gets the gradient tensor for the input tensor you specify.

[`sourceGradientTensors(for:)`](/documentation/MLCompute/MLCTrainingGraph/sourceGradientTensors(for:))

Gets the source gradient tensors for the layer in the training graph you specify.

[`resultGradientTensors(for:)`](/documentation/MLCompute/MLCTrainingGraph/resultGradientTensors(for:))

Gets the result gradient tensors for the layer in the training graph you specify.

[`gradientData(forParameter:layer:)`](/documentation/MLCompute/MLCTrainingGraph/gradientData(forParameter:layer:))

Gets the gradient data for the trainable parameter and associated layer you specify.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)