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

# MPSRNNMatrixInferenceLayer

A recurrent neural network layer for inference on Metal Performance Shaders matrices.

```
class MPSRNNMatrixInferenceLayer
```

## Overview

The [`MPSRNNMatrixInferenceLayer`](/documentation/MetalPerformanceShaders/MPSRNNMatrixInferenceLayer) specifies a recurrent neural network layer for inference on [`MPSMatrix`](/documentation/MetalPerformanceShaders/MPSMatrix) objects. Two types of recurrent layers are supported:

- [`MPSRNNImageInferenceLayer`](/documentation/MetalPerformanceShaders/MPSRNNImageInferenceLayer)—Operates with convolutions on images.
- [`MPSRNNMatrixInferenceLayer`](/documentation/MetalPerformanceShaders/MPSRNNMatrixInferenceLayer)—Operates on matrices.

You can use [`MPSRNNImageInferenceLayer`](/documentation/MetalPerformanceShaders/MPSRNNImageInferenceLayer) to implement the latter by using 1 x 1 images, but due to image size restrictions and performance, [`MPSRNNMatrixInferenceLayer`](/documentation/MetalPerformanceShaders/MPSRNNMatrixInferenceLayer) is the better choice for linear recurrent layers.

[`MPSRNNMatrixInferenceLayer`](/documentation/MetalPerformanceShaders/MPSRNNMatrixInferenceLayer) is initialized using either of the following:

- A single [`MPSRNNDescriptor`](/documentation/MetalPerformanceShaders/MPSRNNDescriptor) instance, which further specifies the recurrent network layer.
- An array of [`MPSRNNDescriptor`](/documentation/MetalPerformanceShaders/MPSRNNDescriptor) instances, which specifies a stack of recurrent layers that can operate in parallel a subset of the inputs in a sequence of inputs and recurrent outputs.

Stacks with bidirectionally traversing encode functions don’t support starting from a previous set of recurrent states. However, you can achieve this effect by defining two separate unidirectional stacks of layers, running the same input sequence on them separately (one forward and one backward), and ultimately combining the two result sequences.

## Topics

### Initializers

[`-  initWithCoder:device:`](/documentation/MetalPerformanceShaders/MPSRNNMatrixInferenceLayer/init(coder:device:))

[`-  initWithDevice:rnnDescriptor:`](/documentation/MetalPerformanceShaders/MPSRNNMatrixInferenceLayer/init(device:rnnDescriptor:))

[`-  initWithDevice:rnnDescriptors:`](/documentation/MetalPerformanceShaders/MPSRNNMatrixInferenceLayer/init(device:rnnDescriptors:))

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

A description of a recursive neural network block or layer.

### Instance Properties

[`bidirectionalCombineMode`](/documentation/MetalPerformanceShaders/MPSRNNMatrixInferenceLayer/bidirectionalCombineMode)

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

Modes that define how two images or matrices are combined.

[`inputFeatureChannels`](/documentation/MetalPerformanceShaders/MPSRNNMatrixInferenceLayer/inputFeatureChannels)

[`numberOfLayers`](/documentation/MetalPerformanceShaders/MPSRNNMatrixInferenceLayer/numberOfLayers)

[`outputFeatureChannels`](/documentation/MetalPerformanceShaders/MPSRNNMatrixInferenceLayer/outputFeatureChannels)

[`recurrentOutputIsTemporary`](/documentation/MetalPerformanceShaders/MPSRNNMatrixInferenceLayer/recurrentOutputIsTemporary)

[`storeAllIntermediateStates`](/documentation/MetalPerformanceShaders/MPSRNNMatrixInferenceLayer/storeAllIntermediateStates)

### Instance Methods

[`-  copyWithZone:device:`](/documentation/MetalPerformanceShaders/MPSRNNMatrixInferenceLayer/copy(with:device:))

[`-  encodeBidirectionalSequenceToCommandBuffer:sourceSequence:destinationForwardMatrices:destinationBackwardMatrices:`](/documentation/MetalPerformanceShaders/MPSRNNMatrixInferenceLayer/encodeBidirectionalSequence(commandBuffer:sourceSequence:destinationForwardMatrices:destinationBackwardMatrices:))

[`-  encodeSequenceToCommandBuffer:sourceMatrices:destinationMatrices:recurrentInputState:recurrentOutputStates:`](/documentation/MetalPerformanceShaders/MPSRNNMatrixInferenceLayer/encodeSequence(commandBuffer:sourceMatrices:destinationMatrices:recurrentInputState:recurrentOutputStates:))

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

A class holds all the data that’s passed from one sequence iteration of the matrix-based recurrent neural network layer to the next.

[`-  encodeSequenceToCommandBuffer:sourceMatrices:sourceOffsets:destinationMatrices:destinationOffsets:recurrentInputState:recurrentOutputStates:`](/documentation/MetalPerformanceShaders/MPSRNNMatrixInferenceLayer/encodeSequence(commandBuffer:sourceMatrices:sourceOffsets:destinationMatrices:destinationOffsets:recurrentInputState:recurrentOutputStates:))

## Relationships

### Conforms To

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

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

[`NSCopying`](/documentation/Foundation/NSCopying)

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

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

[`NSCoding`](/documentation/Foundation/NSCoding)

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

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

[`NSSecureCoding`](/documentation/Foundation/NSSecureCoding)

### Inherits From

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

---

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)