<!--
{
  "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/MLState",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Core ML"
    ],
    "preciseIdentifier" : "c:objc(cs)MLState"
  },
  "title" : "MLState"
}
-->

# MLState

Handle to the state buffers.

```
class MLState
```

## Overview

A stateful model maintains a state from one prediction to another by storing the information in the state buffers. To use such a model, the client must request the model to create state buffers and get `MLState` object, which is the handle to those buffers. Then, at the prediction time, pass the `MLState` object in one of the stateful prediction functions.

```swift
// Load a stateful model
let modelAsset = try MLModelAsset(url: modelURL)
let model = try await MLModel.load(asset: modelAsset, configuration: MLModelConfiguration())

// Request a state
let state = model.newState()

// Run predictions
for _ in 0 ..< 42 {
  _ = try await model.prediction(from: inputFeatures, using: state)
}

// Access the state buffer.
state.withMultiArray(for: "accumulator") { stateMultiArray in
  ...
}
```

The object is a handle to the state buffers. The client shall not read or write the buffers while a prediction is in-flight.

Each stateful prediction that uses the same `MLState` must be serialized. Otherwise, if two such predictions run concurrently, the behavior is undefined.

## Topics

### Getting a state buffer

[`getMultiArrayForStateNamed:handler:`](/documentation/CoreML/MLState/getMultiArrayForStateNamed:handler:)

Gets a mutable view into a state buffer.

[`withMultiArray(for:_:)`](/documentation/CoreML/MLState/withMultiArray(for:_:))

[`withMultiArray(_:)`](/documentation/CoreML/MLState/withMultiArray(_:))



---

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)