<!--
{
  "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

[`static func makeContext(options: BNNSGraph.CompileOptions, (inout BNNSGraph.Builder) -> [any BNNSGraph.TensorDescriptor]) throws -> BNNSGraph.Context`](/documentation/Accelerate/BNNSGraph/makeContext(options:_:))

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

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

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

[`struct 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

[`class 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

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

The compiled graph object.

[`func BNNSGraphCompileFromFile(UnsafePointer<CChar>, UnsafePointer<CChar>?, bnns_graph_compile_options_t) -> bnns_graph_t`](/documentation/Accelerate/BNNSGraphCompileFromFile(_:_:_:))

Compiles a source mlmodelc file to a graph object.

### Specifying and querying compilation options

[`struct 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.

[`func BNNSGraphCompileOptionsMakeDefault() -> bnns_graph_compile_options_t`](/documentation/Accelerate/BNNSGraphCompileOptionsMakeDefault())

Returns an allocated compilation options object with default values.

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

Destroys the specified compilation options object.

[`func BNNSGraphCompileOptionsSetOutputPath(bnns_graph_compile_options_t, UnsafePointer<CChar>?)`](/documentation/Accelerate/BNNSGraphCompileOptionsSetOutputPath(_:_:))

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

[`func BNNSGraphCompileOptionsGetOutputPath(bnns_graph_compile_options_t) -> UnsafePointer<CChar>?`](/documentation/Accelerate/BNNSGraphCompileOptionsGetOutputPath(_:))

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

[`func BNNSGraphCompileOptionsSetOutputFD(bnns_graph_compile_options_t, Int32)`](/documentation/Accelerate/BNNSGraphCompileOptionsSetOutputFD(_:_:))

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

[`func BNNSGraphCompileOptionsGetOutputFD(bnns_graph_compile_options_t) -> Int32`](/documentation/Accelerate/BNNSGraphCompileOptionsGetOutputFD(_:))

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

[`func BNNSGraphCompileOptionsSetTargetSingleThread(bnns_graph_compile_options_t, Bool)`](/documentation/Accelerate/BNNSGraphCompileOptionsSetTargetSingleThread(_:_:))

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

[`func BNNSGraphCompileOptionsGetTargetSingleThread(bnns_graph_compile_options_t) -> Bool`](/documentation/Accelerate/BNNSGraphCompileOptionsGetTargetSingleThread(_:))

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

[`func BNNSGraphCompileOptionsSetOptimizationPreference(bnns_graph_compile_options_t, BNNSGraphOptimizationPreference)`](/documentation/Accelerate/BNNSGraphCompileOptionsSetOptimizationPreference(_:_:))

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

[`func BNNSGraphCompileOptionsGetOptimizationPreference(bnns_graph_compile_options_t) -> BNNSGraphOptimizationPreference`](/documentation/Accelerate/BNNSGraphCompileOptionsGetOptimizationPreference(_:))

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

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

Constants that describe the compilation optimization preference.

[`func BNNSGraphCompileOptionsSetGenerateDebugInfo(bnns_graph_compile_options_t, Bool)`](/documentation/Accelerate/BNNSGraphCompileOptionsSetGenerateDebugInfo(_:_:))

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

[`func BNNSGraphCompileOptionsGetGenerateDebugInfo(bnns_graph_compile_options_t) -> Bool`](/documentation/Accelerate/BNNSGraphCompileOptionsGetGenerateDebugInfo(_:))

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

[`var BNNSTargetSystemGeneric: BNNSTargetSystem`](/documentation/Accelerate/BNNSTargetSystemGeneric)

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

[`func BNNSGraphCompileOptionsSetMessageLogMask(bnns_graph_compile_options_t, UInt32)`](/documentation/Accelerate/BNNSGraphCompileOptionsSetMessageLogMask(_:_:))

Sets the mask for compile-time messages.

[`func BNNSGraphContextSetMessageLogMask(bnns_graph_context_t, UInt32) -> Int32`](/documentation/Accelerate/BNNSGraphContextSetMessageLogMask(_:_:))

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

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

Constants that specify the mask for compile-time messages.

[`func BNNSGraphCompileOptionsSetMessageLogCallback(bnns_graph_compile_options_t, bnns_graph_compile_message_fn_t, UnsafeMutablePointer<bnns_user_message_data_t>?)`](/documentation/Accelerate/BNNSGraphCompileOptionsSetMessageLogCallback(_:_:_:))

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

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

The graph compile-message logging callback function.

[`struct 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

[`func BNNSGraphGetArgumentIntents(bnns_graph_t, UnsafePointer<CChar>?, Int, UnsafeMutablePointer<BNNSGraphArgumentIntent>) -> Int32`](/documentation/Accelerate/BNNSGraphGetArgumentIntents(_:_:_:_:))

Extracts the intents of arguments for the given function argument.

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

Constants that describe argument intents.

[`func BNNSGraphGetArgumentCount(bnns_graph_t, UnsafePointer<CChar>?) -> Int`](/documentation/Accelerate/BNNSGraphGetArgumentCount(_:_:))

Returns the number of arguments for the given function argument.

[`func BNNSGraphGetArgumentNames(bnns_graph_t, UnsafePointer<CChar>?, Int, UnsafeMutablePointer<UnsafePointer<CChar>?>) -> Int32`](/documentation/Accelerate/BNNSGraphGetArgumentNames(_:_:_:_:))

Extracts the names of arguments for the given function argument.

[`func BNNSGraphGetFunctionCount(bnns_graph_t) -> Int`](/documentation/Accelerate/BNNSGraphGetFunctionCount(_:))

Returns the number of callable functions in the specified graph.

[`func BNNSGraphGetFunctionNames(bnns_graph_t, Int, UnsafeMutablePointer<UnsafePointer<CChar>?>) -> Int32`](/documentation/Accelerate/BNNSGraphGetFunctionNames(_:_:_:))

Extracts the names of callable functions in the graph.

[`func BNNSGraphGetInputCount(bnns_graph_t, UnsafePointer<CChar>?) -> Int`](/documentation/Accelerate/BNNSGraphGetInputCount(_:_:))

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

[`func BNNSGraphGetInputNames(bnns_graph_t, UnsafePointer<CChar>?, Int, UnsafeMutablePointer<UnsafePointer<CChar>?>) -> Int32`](/documentation/Accelerate/BNNSGraphGetInputNames(_:_:_:_:))

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

[`func BNNSGraphGetOutputCount(bnns_graph_t, UnsafePointer<CChar>?) -> Int`](/documentation/Accelerate/BNNSGraphGetOutputCount(_:_:))

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

[`func BNNSGraphGetOutputNames(bnns_graph_t, UnsafePointer<CChar>?, Int, UnsafeMutablePointer<UnsafePointer<CChar>?>) -> Int32`](/documentation/Accelerate/BNNSGraphGetOutputNames(_:_:_:_:))

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

[`func BNNSGraphGetArgumentPosition(bnns_graph_t, UnsafePointer<CChar>?, UnsafePointer<CChar>) -> Int`](/documentation/Accelerate/BNNSGraphGetArgumentPosition(_:_:_:))

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

[`func BNNSGraphGetArgumentInterleaveFactors(bnns_graph_t, UnsafePointer<CChar>?, Int, UnsafeMutablePointer<UnsafePointer<UInt16>?>, UnsafeMutablePointer<Int>) -> Int32`](/documentation/Accelerate/BNNSGraphGetArgumentInterleaveFactors(_:_:_:_:_:))

Returns the interleave factors for arguments, if present

### Creating and destroying a context

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

An object that wraps a compiled graph object.

[`func BNNSGraphContextMake(bnns_graph_t) -> bnns_graph_context_t`](/documentation/Accelerate/BNNSGraphContextMake(_:))

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

[`func BNNSGraphContextMakeStreaming(bnns_graph_t, UnsafePointer<CChar>?, Int, UnsafePointer<BNNSTensor>?) -> bnns_graph_context_t`](/documentation/Accelerate/BNNSGraphContextMakeStreaming(_:_:_:_:))

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

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

Destroys the specified graph context.

### Specifying and querying a context’s properties

[`func BNNSGraphContextSetStreamingAdvanceCount(bnns_graph_context_t, Int) -> Int32`](/documentation/Accelerate/BNNSGraphContextSetStreamingAdvanceCount(_:_:))

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

[`func BNNSGraphContextSetArgumentType(bnns_graph_context_t, BNNSGraphArgumentType) -> Int32`](/documentation/Accelerate/BNNSGraphContextSetArgumentType(_:_:))

Specifies the argument type for a graph context.

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

Constants that specify the argument type for a graph context.

[`func BNNSGraphContextSetDynamicShapes(bnns_graph_context_t, UnsafePointer<CChar>?, Int, UnsafeMutablePointer<bnns_graph_shape_t>) -> Int32`](/documentation/Accelerate/BNNSGraphContextSetDynamicShapes(_:_:_:_:))

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

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

The specification of the shape of an argument.

[`func BNNSGraphContextSetBatchSize(bnns_graph_context_t, UnsafePointer<CChar>?, UInt64) -> Int32`](/documentation/Accelerate/BNNSGraphContextSetBatchSize(_:_:_:))

Sets the batch size for a graph.

[`func BNNSGraphContextEnableNanAndInfChecks(bnns_graph_context_t, Bool)`](/documentation/Accelerate/BNNSGraphContextEnableNanAndInfChecks(_:_:))

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

[`func BNNSGraphContextGetWorkspaceSize(bnns_graph_context_t, UnsafePointer<CChar>?) -> Int`](/documentation/Accelerate/BNNSGraphContextGetWorkspaceSize(_:_:))

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

[`func BNNSGraphContextSetStreamingAdvanceCount(bnns_graph_context_t, Int) -> Int32`](/documentation/Accelerate/BNNSGraphContextSetStreamingAdvanceCount(_:_:))

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

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

[`func BNNSGraphContextSetMessageLogCallback(bnns_graph_context_t, bnns_graph_execute_message_fn_t, UnsafeMutablePointer<bnns_user_message_data_t>?) -> Int32`](/documentation/Accelerate/BNNSGraphContextSetMessageLogCallback(_:_:_:))

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

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

The graph execute-message logging callback function.

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

Constants that specify the mask for compile-time messages.

[`struct 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

[`func BNNSGraphContextSetWorkspaceAllocationCallback(bnns_graph_context_t, bnns_graph_realloc_fn_t?, bnns_graph_free_all_fn_t?, Int, UnsafeMutableRawPointer?) -> Int32`](/documentation/Accelerate/BNNSGraphContextSetWorkspaceAllocationCallback(_:_:_:_:_:))

Sets the allocation and deallocation callbacks for internal workspace.

[`func BNNSGraphContextSetOutputAllocationCallback(bnns_graph_context_t, bnns_graph_realloc_fn_t?, bnns_graph_free_all_fn_t?, Int, UnsafeMutableRawPointer?) -> Int32`](/documentation/Accelerate/BNNSGraphContextSetOutputAllocationCallback(_:_:_:_:_:))

Sets the allocation and deallocation callbacks for function outputs.

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

The workspace and output allocation function.

[`typealias 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

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

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

[`func BNNSTensorGetAllocationSize(UnsafePointer<BNNSTensor>) -> Int`](/documentation/Accelerate/BNNSTensorGetAllocationSize(_:))

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

[`func BNNSGraphContextGetTensor(bnns_graph_context_t, UnsafePointer<CChar>?, UnsafePointer<CChar>, Bool, UnsafeMutablePointer<BNNSTensor>) -> Int32`](/documentation/Accelerate/BNNSGraphContextGetTensor(_:_:_:_:_:))

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

[`func BNNSGraphTensorFillStrides(bnns_graph_t, UnsafePointer<CChar>?, UnsafePointer<CChar>, UnsafeMutablePointer<BNNSTensor>) -> Int32`](/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

[`func BNNSGraphContextExecute(bnns_graph_context_t, UnsafePointer<CChar>?, Int, UnsafeMutablePointer<bnns_graph_argument_t>, Int, UnsafeMutablePointer<CChar>?) -> Int32`](/documentation/Accelerate/BNNSGraphContextExecute(_:_:_:_:_:_:))

Executes the specified function with the given context.

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

Constants that specify the argument type for a graph context.

### Enumerations

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

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

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

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

### Structures

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

BNNS Data Types.

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

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

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

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

Describes data associated with an input or output argument

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

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

### Protocols

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

### Macros

[`var BNNS_MAX_TENSOR_DIMENSION: Int32`](/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)