<!--
{
  "availability" : [
    "iOS: 27.0.0 -",
    "iPadOS: 27.0.0 -",
    "tvOS: 27.0.0 -",
    "visionOS: 27.0.0 -",
    "watchOS: 27.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreAI",
  "identifier" : "/documentation/CoreAI/InferenceFunction/AsyncValue",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Core AI",
      "CoreAI"
    ],
    "preciseIdentifier" : "s:13CoreAIRuntime17InferenceFunctionV10AsyncValueC"
  },
  "title" : "InferenceFunction.AsyncValue"
}
-->

# InferenceFunction.AsyncValue

A future which will provide an inference value once any pending write is complete.

```
final class AsyncValue
```

## Overview

An `AsyncValue` contains an underlying `InferenceValue` however that value may be
actively in-use by some previously dispatched async work, and thus accessing the underlying
value below an `AsyncValue` requires an `await` to wait for any previous compute writing it to be
complete.

An `AsyncValue` is immutable once any previous compute has completed.

Async values can be used in async pipelines of inference to dispatch multiple inference functions
in sequence without waiting for each to complete before dispatching the next. This can improve performance
by parallelizing phases of the inferences which are not data dependent:

```swift
 // Pipeline encoding of a text embedding function followed by decoder
 var textTokens: NDArray = ...
 let embeddingOutputs = try textEmbeddingFunction.encode(inputs: ["tokens": .init(textTokens)])
 let embeddings: InferenceFunction.AsyncValue = embeddingsOutputs["embeddings"]

 let decoderOutputs = try decodingFunction.encode(inputs: ["embeddings": embeddings])
 let logits = decoderOutputs["logits"]!
 // Await the compute of logits to be complete
 let logitsNDArray = try await logits.ndArray
```

## Topics

### Creating an async value

[`init(CVReadOnlyPixelBuffer)`](/documentation/CoreAI/InferenceFunction/AsyncValue/init(_:)-5qtut)

Initialize the `AsyncValue` holding the provided pixel buffer.

[`init(consuming InferenceFunction.AsyncMutableValue)`](/documentation/CoreAI/InferenceFunction/AsyncValue/init(_:)-90hbj)

Initialize an async value from an existing mutable async value.

[`init(consuming NDArray)`](/documentation/CoreAI/InferenceFunction/AsyncValue/init(_:)-9wk3)

Initialize the `AsyncValue` holding the provided ndArray.

[`init(unsafeBuffer: consuming any MTLBuffer, byteOffset: Int, scalarType: NDArray.ScalarType, shape: [Int], strides: [Int], interleaveLayout: NDArray.InterleaveLayout?)`](/documentation/CoreAI/InferenceFunction/AsyncValue/init(unsafeBuffer:byteOffset:scalarType:shape:strides:interleaveLayout:))

Creates an async ndArray value that directly references the provided metal buffer,
interpreted as the specified scalar type and shape.

### Accessing an async value

[`var kind: InferenceValue.Kind`](/documentation/CoreAI/InferenceFunction/AsyncValue/kind)

The kind of inference value held by this async value.

[`var ndArray: NDArray?`](/documentation/CoreAI/InferenceFunction/AsyncValue/ndArray)

Waits for any pending write access on the underlying ndArray to complete, then returns it.

[`var pixelBuffer: CVReadOnlyPixelBuffer?`](/documentation/CoreAI/InferenceFunction/AsyncValue/pixelBuffer)

Waits for any pending write access on the underlying pixel buffer to complete, then returns it.

## Relationships

### Conforms To

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`Sendable`](/documentation/Swift/Sendable)

---

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)