<!--
{
  "documentType" : "article",
  "framework" : "Accelerate",
  "identifier" : "/documentation/Accelerate/bnns-library",
  "metadataVersion" : "0.1.0",
  "role" : "collectionGroup",
  "title" : "BNNS"
}
-->

# BNNS

Implement and run neural networks for training and inference.

## Discussion

The Accelerate framework’s BNNS library is a collection of functions that you use to construct neural networks for training and inference. BNNS provides routines optimized for high performance and low energy consumption across all Apple platforms.

The BNNSGraph API provides the means to build CPU based neural networks from the mlmodelc file that Xcode compiles from an ML package.

BNNSGraph allows the BNNS library to execute entire networks rather than individual layers. By considering the full model, BNNS can apply graph-level optimizations such as omitting unnecessary copy operations, fusing computational kernels, and avoiding computing redundant information. Furthermore, BNNS can optimize data layouts for constant data — such as convolution weights — and this allows the fastest execution on specific hardware. These optimizations can represent significant performance and energy-efficiency improvements.

## Topics

### Building graphs in Swift

[`makeContext(options:_:)`](/documentation/Accelerate/BNNSGraph/makeContext(options:_:))

Returns a new context that wraps a graph object that the given closure defines.

[`BNNSGraph.Builder`](/documentation/Accelerate/BNNSGraph/Builder)

A structure thats provides a closure you can use to define the arguments and operations of a
BNNS Graph.

[`BNNSGraph.Builder.Tensor`](/documentation/Accelerate/BNNSGraph/Builder/Tensor)

A structure that represents an abstract handle to a tensor that you use within a `BNNSGraph.makeContext` closure.

  <doc://com.apple.accelerate/documentation/Accelerate/supporting-real-time-ml-inference-on-the-cpu>

### Creating and executing graphs in Swift

[`BNNSGraph.Context`](/documentation/Accelerate/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.

### Compiling a graph object

[`bnns_graph_t`](/documentation/Accelerate/bnns_graph_t)

The compiled graph object.

[`BNNSGraphCompileFromFile(_:_:_:)`](/documentation/Accelerate/BNNSGraphCompileFromFile(_:_:_:))

Compiles a source mlmodelc file to a graph object.

### Specifying and querying compilation options

[`bnns_graph_compile_options_t`](/documentation/Accelerate/bnns_graph_compile_options_t)

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

[`BNNSGraphCompileOptionsMakeDefault()`](/documentation/Accelerate/BNNSGraphCompileOptionsMakeDefault())

Returns an allocated compilation options object with default values.

[`BNNSGraphCompileOptionsDestroy(_:)`](/documentation/Accelerate/BNNSGraphCompileOptionsDestroy(_:))

Destroys the specified compilation options object.

[`BNNSGraphCompileOptionsSetOutputPath(_:_:)`](/documentation/Accelerate/BNNSGraphCompileOptionsSetOutputPath(_:_:))

Sets the option for graph compilation to generate the graph object directly to the specified file.

[`BNNSGraphCompileOptionsGetOutputPath(_:)`](/documentation/Accelerate/BNNSGraphCompileOptionsGetOutputPath(_:))

Returns the option for the compiled graph’s output path.

[`BNNSGraphCompileOptionsSetOutputFD(_:_:)`](/documentation/Accelerate/BNNSGraphCompileOptionsSetOutputFD(_:_:))

Sets the option for graph compilation to generate the graph object directly to the specified file descriptor.

[`BNNSGraphCompileOptionsGetOutputFD(_:)`](/documentation/Accelerate/BNNSGraphCompileOptionsGetOutputFD(_:))

Returns the option for the compiled graph’s output file descriptor.

[`BNNSGraphCompileOptionsSetTargetSingleThread(_:_:)`](/documentation/Accelerate/BNNSGraphCompileOptionsSetTargetSingleThread(_:_:))

Sets the option for the compiled graph to execute on a single thread.

[`BNNSGraphCompileOptionsGetTargetSingleThread(_:)`](/documentation/Accelerate/BNNSGraphCompileOptionsGetTargetSingleThread(_:))

Returns the option for the compiled graph to execute on a single thread.

[`BNNSGraphCompileOptionsSetOptimizationPreference(_:_:)`](/documentation/Accelerate/BNNSGraphCompileOptionsSetOptimizationPreference(_:_:))

Sets the option for the compiled graph to optimize for either size or performance.

[`BNNSGraphCompileOptionsGetOptimizationPreference(_:)`](/documentation/Accelerate/BNNSGraphCompileOptionsGetOptimizationPreference(_:))

Returns the option for the compiled graph to optimize for either size or performance.

[`BNNSGraphOptimizationPreference`](/documentation/Accelerate/BNNSGraphOptimizationPreference)

Constants that describe the compilation optimization preference.

[`BNNSGraphCompileOptionsSetGenerateDebugInfo(_:_:)`](/documentation/Accelerate/BNNSGraphCompileOptionsSetGenerateDebugInfo(_:_:))

Sets the option for the compiled graph to include debugging information.

[`BNNSGraphCompileOptionsGetGenerateDebugInfo(_:)`](/documentation/Accelerate/BNNSGraphCompileOptionsGetGenerateDebugInfo(_:))

Returns the option for the compiled graph to include debugging information.

[`BNNSTargetSystemGeneric`](/documentation/Accelerate/BNNSTargetSystemGeneric)

### Specifying a graph’s compile-time message callback

[`BNNSGraphCompileOptionsSetMessageLogMask(_:_:)`](/documentation/Accelerate/BNNSGraphCompileOptionsSetMessageLogMask(_:_:))

Sets the mask for compile-time messages.

[`BNNSGraphContextSetMessageLogMask(_:_:)`](/documentation/Accelerate/BNNSGraphContextSetMessageLogMask(_:_:))

Sets mask for log messages that are logged (either via `os_log` or the user specified callback)

[`BNNSGraphMessageLevel`](/documentation/Accelerate/BNNSGraphMessageLevel)

Constants that specify the mask for compile-time messages.

[`BNNSGraphCompileOptionsSetMessageLogCallback(_:_:_:)`](/documentation/Accelerate/BNNSGraphCompileOptionsSetMessageLogCallback(_:_:_:))

Specifies a customized callback function that reports compile-time messages.

[`bnns_graph_compile_message_fn_t`](/documentation/Accelerate/bnns_graph_compile_message_fn_t)

The graph compile-message logging callback function.

[`bnns_user_message_data_t`](/documentation/Accelerate/bnns_user_message_data_t)

Additional user-defined logging argument for message-logging callbacks.

### Querying a graph’s properties

[`BNNSGraphGetArgumentIntents(_:_:_:_:)`](/documentation/Accelerate/BNNSGraphGetArgumentIntents(_:_:_:_:))

Extracts the intents of arguments for the given function argument.

[`BNNSGraphArgumentIntent`](/documentation/Accelerate/BNNSGraphArgumentIntent)

Constants that describe argument intents.

[`BNNSGraphGetArgumentCount(_:_:)`](/documentation/Accelerate/BNNSGraphGetArgumentCount(_:_:))

Returns the number of arguments for the given function argument.

[`BNNSGraphGetArgumentNames(_:_:_:_:)`](/documentation/Accelerate/BNNSGraphGetArgumentNames(_:_:_:_:))

Extracts the names of arguments for the given function argument.

[`BNNSGraphGetFunctionCount(_:)`](/documentation/Accelerate/BNNSGraphGetFunctionCount(_:))

Returns the number of callable functions in the specified graph.

[`BNNSGraphGetFunctionNames(_:_:_:)`](/documentation/Accelerate/BNNSGraphGetFunctionNames(_:_:_:))

Extracts the names of callable functions in the graph.

[`BNNSGraphGetInputCount(_:_:)`](/documentation/Accelerate/BNNSGraphGetInputCount(_:_:))

Returns the number of input arguments for the given function argument.

[`BNNSGraphGetInputNames(_:_:_:_:)`](/documentation/Accelerate/BNNSGraphGetInputNames(_:_:_:_:))

Extracts the names of input arguments for the given function argument.

[`BNNSGraphGetOutputCount(_:_:)`](/documentation/Accelerate/BNNSGraphGetOutputCount(_:_:))

Returns the number of output arguments for the given function argument.

[`BNNSGraphGetOutputNames(_:_:_:_:)`](/documentation/Accelerate/BNNSGraphGetOutputNames(_:_:_:_:))

Extracts the names of output arguments for the given function argument.

[`BNNSGraphGetArgumentPosition(_:_:_:)`](/documentation/Accelerate/BNNSGraphGetArgumentPosition(_:_:_:))

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

[`BNNSGraphGetArgumentInterleaveFactors(_:_:_:_:_:)`](/documentation/Accelerate/BNNSGraphGetArgumentInterleaveFactors(_:_:_:_:_:))

Returns the interleave factors for arguments, if present

### Creating and destroying a context

[`bnns_graph_context_t`](/documentation/Accelerate/bnns_graph_context_t)

An object that wraps a compiled graph object.

[`BNNSGraphContextMake(_:)`](/documentation/Accelerate/BNNSGraphContextMake(_:))

Returns an allocated and initialized graph context from the specified graph.

[`BNNSGraphContextMakeStreaming(_:_:_:_:)`](/documentation/Accelerate/BNNSGraphContextMakeStreaming(_:_:_:_:))

Returns an allocated and initialized graph context with streaming support from the specified graph.

[`BNNSGraphContextDestroy(_:)`](/documentation/Accelerate/BNNSGraphContextDestroy(_:))

Destroys the specified graph context.

### Specifying and querying a context’s properties

[`BNNSGraphContextSetStreamingAdvanceCount(_:_:)`](/documentation/Accelerate/BNNSGraphContextSetStreamingAdvanceCount(_:_:))

Sets the streaming advancement amount for cases with dynamically shaped inputs.

[`BNNSGraphContextSetArgumentType(_:_:)`](/documentation/Accelerate/BNNSGraphContextSetArgumentType(_:_:))

Specifies the argument type for a graph context.

[`BNNSGraphArgumentType`](/documentation/Accelerate/BNNSGraphArgumentType)

Constants that specify the argument type for a graph context.

[`BNNSGraphContextSetDynamicShapes(_:_:_:_:)`](/documentation/Accelerate/BNNSGraphContextSetDynamicShapes(_:_:_:_:))

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

[`bnns_graph_shape_t`](/documentation/Accelerate/bnns_graph_shape_t)

The specification of the shape of an argument.

[`BNNSGraphContextSetBatchSize(_:_:_:)`](/documentation/Accelerate/BNNSGraphContextSetBatchSize(_:_:_:))

Sets the batch size for a graph.

[`BNNSGraphContextEnableNanAndInfChecks(_:_:)`](/documentation/Accelerate/BNNSGraphContextEnableNanAndInfChecks(_:_:))

Specifies that the context checks intermediate tensors for NaNs and infinities.

[`BNNSGraphContextGetWorkspaceSize(_:_:)`](/documentation/Accelerate/BNNSGraphContextGetWorkspaceSize(_:_:))

Returns the minimum size, in bytes, of the workspace that graph context execution requires.

[`BNNSGraphContextSetStreamingAdvanceCount(_:_:)`](/documentation/Accelerate/BNNSGraphContextSetStreamingAdvanceCount(_:_:))

Sets the streaming advancement amount for cases with dynamically shaped inputs.

### Specifying a context’s execute-time message callback

[`BNNSGraphContextSetMessageLogCallback(_:_:_:)`](/documentation/Accelerate/BNNSGraphContextSetMessageLogCallback(_:_:_:))

Specifies a customized callback function that reports execution-time messages.

[`bnns_graph_execute_message_fn_t`](/documentation/Accelerate/bnns_graph_execute_message_fn_t)

The graph execute-message logging callback function.

[`BNNSGraphMessageLevel`](/documentation/Accelerate/BNNSGraphMessageLevel)

Constants that specify the mask for compile-time messages.

[`bnns_user_message_data_t`](/documentation/Accelerate/bnns_user_message_data_t)

Additional user-defined logging argument for message-logging callbacks.

### Specifying a context’s allocation callbacks

[`BNNSGraphContextSetWorkspaceAllocationCallback(_:_:_:_:_:)`](/documentation/Accelerate/BNNSGraphContextSetWorkspaceAllocationCallback(_:_:_:_:_:))

Sets the allocation and deallocation callbacks for internal workspace.

[`BNNSGraphContextSetOutputAllocationCallback(_:_:_:_:_:)`](/documentation/Accelerate/BNNSGraphContextSetOutputAllocationCallback(_:_:_:_:_:))

Sets the allocation and deallocation callbacks for function outputs.

[`bnns_graph_realloc_fn_t`](/documentation/Accelerate/bnns_graph_realloc_fn_t)

The workspace and output allocation function.

[`bnns_graph_free_all_fn_t`](/documentation/Accelerate/bnns_graph_free_all_fn_t)

The workspace and output deallocation function.

### Specifying and querying a tensor’s properties

[`BNNSTensor`](/documentation/Accelerate/BNNSTensor)

A structure that describes the shape, stride, data type, and, optionally, the memory location of an n-dimensional array.

[`BNNSTensorGetAllocationSize(_:)`](/documentation/Accelerate/BNNSTensorGetAllocationSize(_:))

Returns the minimum allocation size, in bytes, of the specified tensor.

[`BNNSGraphContextGetTensor(_:_:_:_:_:)`](/documentation/Accelerate/BNNSGraphContextGetTensor(_:_:_:_:_:))

Sets the properties of a tensor for the specified function argument.

[`BNNSGraphTensorFillStrides(_:_:_:_:)`](/documentation/Accelerate/BNNSGraphTensorFillStrides(_:_:_:_:))

Sets the stride of the specifed tensor for compatibility with the given model’s input or output argument based on its current shape.

### Executing a graph

[`BNNSGraphContextExecute(_:_:_:_:_:_:)`](/documentation/Accelerate/BNNSGraphContextExecute(_:_:_:_:_:_:))

Executes the specified function with the given context.

[`BNNSGraphArgumentType`](/documentation/Accelerate/BNNSGraphArgumentType)

Constants that specify the argument type for a graph context.

### Enumerations

[`BNNS`](/documentation/Accelerate/BNNS)

An enumeration that acts as a namespace for Swift overlays to BNNS.

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

An enumeration that acts as a namespace for the Swift overlays to BNNS Graph.

### Structures

[`BNNSDataType`](/documentation/Accelerate/BNNSDataType)

BNNS Data Types.

[`BNNSSparsityParameters`](/documentation/Accelerate/BNNSSparsityParameters)

[`BNNSSparsityType`](/documentation/Accelerate/BNNSSparsityType)

[`BNNSTargetSystem`](/documentation/Accelerate/BNNSTargetSystem)

[`bnns_graph_argument_t`](/documentation/Accelerate/bnns_graph_argument_t)

Describes data associated with an input or output argument

[`BNNSImageStackDescriptor`](/documentation/Accelerate/BNNSImageStackDescriptor)

[`BNNSVectorDescriptor`](/documentation/Accelerate/BNNSVectorDescriptor)

### Protocols

[`BNNSScalar`](/documentation/Accelerate/BNNSScalar)

### Macros

[`BNNS_MAX_TENSOR_DIMENSION`](/documentation/Accelerate/BNNS_MAX_TENSOR_DIMENSION)

### Deprecated symbols

  <doc://com.apple.accelerate/documentation/Accelerate/classic-bnns-api>



---

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)