<!--
{
  "availability" : [
    "iOS: 11.2.0 -",
    "iPadOS: 11.2.0 -",
    "macCatalyst: 13.1.0 -",
    "macOS: 10.13.2 -",
    "tvOS: 11.2.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 4.2.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "CoreML",
  "identifier" : "/documentation/CoreML/MLCustomLayer/encode(commandBuffer:inputs:outputs:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Core ML"
    ],
    "preciseIdentifier" : "c:objc(pl)MLCustomLayer(im)encodeToCommandBuffer:inputs:outputs:error:"
  },
  "title" : "encode(commandBuffer:inputs:outputs:)"
}
-->

# encode(commandBuffer:inputs:outputs:)

Encodes GPU commands to evaluate the custom layer.

```
optional func encode(commandBuffer: any MTLCommandBuffer, inputs: [any MTLTexture], outputs: [any MTLTexture]) throws
```

## Parameters

`commandBuffer`

A command buffer that defines the work the layer performs on the GPU.

`inputs`

A texture array that represents the layer’s inputs.

`outputs`

A texture array that represents the layer’s outputs.

## Discussion

Implement this method to use the GPU to evaluate your layer. Fill `commandBuffer` with the GPU commands that evaluate the layer. Don’t commit the command buffer in this method; Core ML executes the command buffer after this method returns.

Improve your layer’s performance by caching the <doc://com.apple.documentation/documentation/Metal/MTLComputePipelineState> instances you create and intend to reuse in subsequent calls.

Implementing this method doesn’t guarantee that Core ML evaluates this layer on the GPU. For example, Core ML may evaluate the layer on the CPU if the system doesn’t have enough GPU’s resources to run the custom layer.

> Important:
> The GPU works with 16-bit floats, not 32-bit floats. Verify that lower precision values don’t impact your network’s behavior.

If you don’t implement this method, Core ML instead uses [`evaluate(inputs:outputs:)`](/documentation/CoreML/MLCustomLayer/evaluate(inputs:outputs:)).

For more information about using the GPU for general purpose programming, see `Compute Processing`.

---

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)