<!--
{
  "availability" : [
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: 15.0.0 -",
    "macOS: 12.0.0 -",
    "tvOS: 15.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "MetalPerformanceShadersGraph",
  "identifier" : "/documentation/MetalPerformanceShadersGraph/MPSGraph/topK(_: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)topKWithSourceTensor:k:name:"
  },
  "title" : "topK(_:k:name:)"
}
-->

# topK(_:k:name:)

Creates a TopK operation and returns the value and indices tensors

```
func topK(_ source: MPSGraphTensor, k: Int, name: String?) -> [MPSGraphTensor]
```

## Parameters

`source`

Tensor containing source data

`k`

The number of largest values to return

`name`

The name for the operation.

## Return Value

A valid MPSGraphTensor array of size 2

## Discussion

Finds the k largest values along the minor dimension of the input. The source must have
at least k elements along its minor dimension.
The first element of the result array corresponds to the top values, and the second element of
the result array corresponds to the indices of the top values.

---

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)