<!--
{
  "availability" : [
    "iOS: 17.0.0 -",
    "iPadOS: 17.0.0 -",
    "macCatalyst: 17.0.0 -",
    "macOS: 14.0.0 -",
    "tvOS: 17.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "MetalPerformanceShadersGraph",
  "identifier" : "/documentation/MetalPerformanceShadersGraph/MPSGraph/topKGradient(_:source:axis:k:name:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Metal Performance Shaders Graph",
      "MetalPerformanceShadersGraph"
    ],
    "preciseIdentifier" : "c:objc(cs)MPSGraph(im)topKWithGradientTensor:source:axis:k:name:"
  },
  "title" : "topKGradient(_:source:axis:k:name:)"
}
-->

# topKGradient(_:source:axis:k:name:)

Creates a TopKGradient operation and returns the result tensor.

```
func topKGradient(_ gradient: MPSGraphTensor, source: MPSGraphTensor, axis: Int, k: Int, name: String?) -> MPSGraphTensor
```

## Parameters

`gradient`

Tensor containing the incoming gradient.

`source`

Tensor containing source data.

`axis`

The dimension along which to compute the TopK values..

`k`

The number of largest values to return.

`name`

The name for the operation.

## Return Value

A valid MPSGraphTensor object.

## Discussion

Finds the K largest values along the minor dimension of the input. The input must have
at least K elements along its minor dimension.

---

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)