---
title: "allocateUserGradient(for:)"
framework: ML Compute
role: symbol
role_heading: Instance Method
platforms: [iOS 14.0+, iPadOS 14.0+, Mac Catalyst 14.0+, macOS 11.0+, tvOS 14.0+]
path: "mlcompute/mlctraininggraph/allocateusergradient(for:)"
---

# allocateUserGradient(for:)

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

## Declaration

```swift
func allocateUserGradient(for tensor: MLCTensor) -> MLCTensor?
```

## Parameters

- `tensor`: A result tensor.

## Return Value

Return Value A gradient tensor.

## Discussion

Discussion Third-party numeric libraries can perform additional computations on the result tensor produced by a layer in the training graph. This external computation may produce gradients that you can backpropagate during gradient execution by using this method to allocate an entry for the gradient.

## See Also

### Preparing Training Graphs

- [addInputs(_:lossLabels:)](mlcompute/mlctraininggraph/addinputs(_:losslabels:).md)
- [addInputs(_:lossLabels:lossLabelWeights:)](mlcompute/mlctraininggraph/addinputs(_:losslabels:losslabelweights:).md)
- [addOutputs(_:)](mlcompute/mlctraininggraph/addoutputs(_:).md)
- [stopGradient(for:)](mlcompute/mlctraininggraph/stopgradient(for:).md)
- [compileOptimizer(_:)](mlcompute/mlctraininggraph/compileoptimizer(_:).md)
- [compile(options:device:)](mlcompute/mlctraininggraph/compile(options:device:).md)
- [compile(options:device:inputTensors:inputTensorsData:)](mlcompute/mlctraininggraph/compile(options:device:inputtensors:inputtensorsdata:).md)
- [link(with:)](mlcompute/mlctraininggraph/link(with:).md)
- [MLCGraphCompilationOptions](mlcompute/mlcgraphcompilationoptions.md)
