<!--
{
  "availability" : [
    "iOS: 11.0.0 -",
    "iPadOS: 11.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.13.0 -",
    "tvOS: 11.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "MetalPerformanceShaders",
  "identifier" : "/documentation/MetalPerformanceShaders/MPSState",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Metal Performance Shaders"
    ],
    "preciseIdentifier" : "c:objc(cs)MPSState"
  },
  "title" : "MPSState"
}
-->

# MPSState

An opaque data container for large storage in MPS CNN filters.

```
class MPSState
```

## Overview

Some MPS CNN kernels produce additional information beyond an [`MPSImage`](/documentation/MetalPerformanceShaders/MPSImage). These may be pooling indices where the result came from, convolution weights, or other information not contained in the usual [`MPSImage`](/documentation/MetalPerformanceShaders/MPSImage) result from a [`MPSCNNKernel`](/documentation/MetalPerformanceShaders/MPSCNNKernel). An [`MPSState`](/documentation/MetalPerformanceShaders/MPSState) object typically contains one or more expensive <doc://com.apple.documentation/documentation/Metal/MTLResource> objects such as textures or buffers to store this information. It provides a base class with interfaces for managing this storage. Child classes may add additional functionality specific to their contents.

Some [`MPSState`](/documentation/MetalPerformanceShaders/MPSState) objects are temporary. Temporary state objects, for example, [`MPSTemporaryImage`](/documentation/MetalPerformanceShaders/MPSTemporaryImage) and [`MPSTemporaryMatrix`](/documentation/MetalPerformanceShaders/MPSTemporaryMatrix), are for very short lived storage, perhaps just a few lines of code within the scope of a single <doc://com.apple.documentation/documentation/Metal/MTLCommandBuffer>. They are very efficient for storage, as several temporary objects can share the same memory over the course of a command buffer. This can improve both memory usage and time spent in the kernel wiring down memory and such. You may find that some large CNN tasks can not be computed without them, as nontemporary storage would simply take up too much memory.

In exchange, the lifetime of the underlying storage in temporary [`MPSState`](/documentation/MetalPerformanceShaders/MPSState) objects needs to be carefully managed. ARC often waits until the end of scope to release objects. Temporary storage often needs to be released sooner than that. Consequently the lifetime of the data in the underlying Metal resources is managed by a [`readCount`](/documentation/MetalPerformanceShaders/MPSState/readCount) property. Each time a [`MPSCNNKernel`](/documentation/MetalPerformanceShaders/MPSCNNKernel) reads a temporary [`MPSState`](/documentation/MetalPerformanceShaders/MPSState) object the [`readCount`](/documentation/MetalPerformanceShaders/MPSState/readCount) is automatically decremented. When it reaches 0, the underlying storage is recycled for use by other MPS temporary objects, and the data is becomes undefined. If you need to consume the data multiple times, you should set the [`readCount`](/documentation/MetalPerformanceShaders/MPSState/readCount) to a larger number to prevent the data from becoming undefined. You may set the [`readCount`](/documentation/MetalPerformanceShaders/MPSState/readCount) to 0 yourself to return the storage to MPS, if for any reason, you realize that the [`MPSState`](/documentation/MetalPerformanceShaders/MPSState) object will no longer be used.

The contents of a temporary [`MPSState`](/documentation/MetalPerformanceShaders/MPSState) object are only valid from creation to the time the [`readCount`](/documentation/MetalPerformanceShaders/MPSState/readCount) reaches 0. The data is only valid for the <doc://com.apple.documentation/documentation/Metal/MTLCommandBuffer> on which it was created. Nontemporary [`MPSState`](/documentation/MetalPerformanceShaders/MPSState) objects are valid on any <doc://com.apple.documentation/documentation/Metal/MTLCommandBuffer> on the same device until they are released.

## Topics

### Instance Properties

[`isTemporary`](/documentation/MetalPerformanceShaders/MPSState/isTemporary)

[`label`](/documentation/MetalPerformanceShaders/MPSState/label)

[`readCount`](/documentation/MetalPerformanceShaders/MPSState/readCount)

[`resource`](/documentation/MetalPerformanceShaders/MPSState/resource)

[`resourceCount`](/documentation/MetalPerformanceShaders/MPSState/resourceCount)

## Relationships

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

### Conforms To

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

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

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

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

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

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

### Inherited By

[`MPSCNNNormalizationGammaAndBetaState`](/documentation/MetalPerformanceShaders/MPSCNNNormalizationGammaAndBetaState)

[`MPSRNNMatrixTrainingState`](/documentation/MetalPerformanceShaders/MPSRNNMatrixTrainingState)

[`MPSRNNRecurrentImageState`](/documentation/MetalPerformanceShaders/MPSRNNRecurrentImageState)

[`MPSRNNRecurrentMatrixState`](/documentation/MetalPerformanceShaders/MPSRNNRecurrentMatrixState)

[`MPSNNBinaryGradientState`](/documentation/MetalPerformanceShaders/MPSNNBinaryGradientState)

[`MPSCNNLossLabels`](/documentation/MetalPerformanceShaders/MPSCNNLossLabels)

[`MPSNDArrayGradientState`](/documentation/MetalPerformanceShaders/MPSNDArrayGradientState)

[`MPSCNNNormalizationMeanAndVarianceState`](/documentation/MetalPerformanceShaders/MPSCNNNormalizationMeanAndVarianceState)

[`MPSNNGradientState`](/documentation/MetalPerformanceShaders/MPSNNGradientState)

[`MPSNNMultiaryGradientState`](/documentation/MetalPerformanceShaders/MPSNNMultiaryGradientState)

[`MPSCNNConvolutionWeightsAndBiasesState`](/documentation/MetalPerformanceShaders/MPSCNNConvolutionWeightsAndBiasesState)

---

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)