<!--
{
  "availability" : [
    "iOS: 18.0.0 -",
    "iPadOS: 18.0.0 -",
    "macCatalyst: 18.0.0 -",
    "macOS: 15.0.0 -",
    "tvOS: 18.0.0 -",
    "visionOS: 2.0.0 -",
    "watchOS: 11.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreML",
  "identifier" : "/documentation/CoreML/withMLTensorComputePolicy(_:_:)-8stx9",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "Core ML"
    ],
    "preciseIdentifier" : "s:6CoreML25withMLTensorComputePolicyyxAA09MLComputeF0V_xyYaKXEtYaKlF"
  },
  "title" : "withMLTensorComputePolicy(_:_:)"
}
-->

# withMLTensorComputePolicy(_:_:)

Calls the given closure within a task-local context using the specified compute policy to influence what compute device tensor
operations are executed on.

```
func withMLTensorComputePolicy<R>(_ computePolicy: MLComputePolicy, _ body: () async throws -> R) async rethrows -> R
```

## Parameters

`computePolicy`

A compute policy that will be set before the closure gets called and restored after the closure returns.

`body`

A nullary closure. If the closure has a return value, that value is also used as the return value of the
`withMLTensorComputePolicy(_:_:)` function.

## Return Value

The return value, if any, of the `body` closure.

---

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)