<!--
{
  "availability" : [
    "iOS: 18.0.0 -",
    "iPadOS: 18.0.0 -",
    "macCatalyst: -",
    "macOS: 15.0.0 -",
    "tvOS: 18.0.0 -",
    "visionOS: -",
    "watchOS: 11.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Accelerate",
  "identifier" : "/documentation/Accelerate/BNNSGraph/Context",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Accelerate"
    ],
    "preciseIdentifier" : "s:10Accelerate9BNNSGraphO7ContextC"
  },
  "title" : "BNNSGraph.Context"
}
-->

# BNNSGraph.Context

A wrapper around a compiled graph object that adds a required modifiable context to support dynamically sized models and set execute-time options.

```
class Context
```

## Overview

A [`BNNSGraph.Context`](/documentation/Accelerate/BNNSGraph/Context) instance provides a wrapper around the C API [`bnns_graph_t`](/documentation/Accelerate/bnns_graph_t) and [`bnns_graph_context_t`](/documentation/Accelerate/bnns_graph_context_t) types. Because this class manages its own memory, you don’t need to call [`BNNSGraphContextDestroy(_:)`](/documentation/Accelerate/BNNSGraphContextDestroy(_:)) to deallocate its resources.

## Topics

### Creating a graph context

[`struct CompileOptions`](/documentation/Accelerate/BNNSGraph/CompileOptions)

The compilation options that BNNS uses when compiling a source mlmodelc file to a graph object.

### Specifying and querying a graph context’s properties

[`func setDynamicShapes([BNNSGraph.Shape], forFunction: String?) async throws -> [BNNSGraph.Shape]`](/documentation/Accelerate/BNNSGraph/Context/setDynamicShapes(_:forFunction:))

Specifies the dynamic shapes for a graph and, if possible, infers the output shapes.

[`struct Shape`](/documentation/Accelerate/BNNSGraph/Shape)

The specification of the shape of an argument.

[`func argumentCount(forFunction: String?) -> Int`](/documentation/Accelerate/BNNSGraph/Context/argumentCount(forFunction:))

Returns the number of arguments for the given function argument.

[`func argumentNames(forFunction: String?) -> [String]`](/documentation/Accelerate/BNNSGraph/Context/argumentNames(forFunction:))

Returns the names of arguments for the given function argument.

[`func argumentPosition(forFunction: String?, argument: String) -> Int`](/documentation/Accelerate/BNNSGraph/Context/argumentPosition(forFunction:argument:))

Returns the index into the arguments array for the given function argument.

[`var functionCount: Int`](/documentation/Accelerate/BNNSGraph/Context/functionCount)

The number of input arguments for the given function argument.

[`var functionNames: [String]`](/documentation/Accelerate/BNNSGraph/Context/functionNames)

Returns the names of callable functions in the graph.

[`var checkForNaNsAndInfinities: Bool`](/documentation/Accelerate/BNNSGraph/Context/checkForNaNsAndInfinities)

A Boolean value that specifies that the context checks intermediate tensors for NaNs and infinities.

### Specifying a tensor’s properties

[`func tensor(forFunction: String?, argument: String, fillKnownDynamicShapes: Bool) -> BNNSTensor?`](/documentation/Accelerate/BNNSGraph/Context/tensor(forFunction:argument:fillKnownDynamicShapes:))

Returns an unallocated tensor for a given function argument.

### Executing a graph

[`func executeFunction(String?, arguments: inout [BNNSTensor]) async throws`](/documentation/Accelerate/BNNSGraph/Context/executeFunction(_:arguments:)-8bhcn)

Executes the specified function using an array of input and output tensors.

[`func executeFunction<T>(String?, arguments: [T]) throws`](/documentation/Accelerate/BNNSGraph/Context/executeFunction(_:arguments:)-95snr)

Executes the specified function using an array of input and output pointers.

[`protocol PointerArgument`](/documentation/Accelerate/BNNSGraph/PointerArgument)

A type that BNNS Graph accepts as an input-output argument.

### Handling errors

[`enum Error`](/documentation/Accelerate/BNNSGraph/Error)

Error codes that a graph context throws.

### Initializers

[`init(compileFromPath: String, functionName: String?, options: BNNSGraph.CompileOptions) async throws`](/documentation/Accelerate/BNNSGraph/Context/init(compileFromPath:functionName:options:)-3nn5g)

Returns a new context that wraps a graph object which represents the compiled `.mlmodelc` file.

[`init(compileFromPath: String, functionName: String?, options: BNNSGraph.CompileOptions) throws`](/documentation/Accelerate/BNNSGraph/Context/init(compileFromPath:functionName:options:)-6ghot)

Synchronously returns a new context that wraps a graph object which represents the compiled `.mlmodelc` file.

### Instance Properties

[`var streamingAdvanceCount: Int`](/documentation/Accelerate/BNNSGraph/Context/streamingAdvanceCount)

Sets streaming advancement amount for cases with dynamically shaped inputs.

### Instance Methods

[`func allocateTensor(forFunction: String?, argument: String, fillKnownDynamicShapes: Bool) -> BNNSTensor?`](/documentation/Accelerate/BNNSGraph/Context/allocateTensor(forFunction:argument:fillKnownDynamicShapes:))

Returns an allocated tensor for a given function argument.

[`func executeFunction(String?, arguments: inout [BNNSTensor]) throws`](/documentation/Accelerate/BNNSGraph/Context/executeFunction(_:arguments:)-52eh3)

Synchronously executes the specified function with the provided context.

[`func setBatchSize(Int, forFunction: String?) async`](/documentation/Accelerate/BNNSGraph/Context/setBatchSize(_:forFunction:)-4872x)

Sets the batch size for a graph.

[`func setBatchSize(Int, forFunction: String?)`](/documentation/Accelerate/BNNSGraph/Context/setBatchSize(_:forFunction:)-8eqzm)

Synchronously sets the batch size for a graph.

---

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)