One Logo Pool Ball
Accelerate · IOS API REFERENCE

BNNSGraph.Context

class · iOS 18.0+ · SDK 27.1

class Context

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

Members (18)

method · iOS 26.0+

setBatchSize

TypeScript
setBatchSizeForFunction(batchSize: number, function_?: (string) | null): void
Swift declaration
func setBatchSize(_ batchSize: Int, forFunction function: String? = nil)

Synchronously sets the batch size for a graph. This is a special case of `setDynamicShapes(_:forFunction:)` where the only dynamic sizes that occur are the first index of their tensor (that is, the batch dimension) and are all equal. This allows just passing a single `batchSize` constant. This function has no effect if you pass a `batchSize` value that's less than zero. - Parameter batchSize: The batch size. - Parameter function: The specific function to set shapes for. You may set this to `nil` if there is only one function. - SeeAlso: `BNNSGraphContextSetBatchSize`

method · iOS 18.0+

setBatchSize

TypeScript
setBatchSizeForFunction5550(batchSize: number, function_?: (string) | null): Promise<void>
Swift declaration
func setBatchSize(_ batchSize: Int, forFunction function: String? = nil) async

Sets the batch size for a graph. This is a special case of `setDynamicShapes(_:forFunction:)` where the only dynamic sizes that occur are the first index of their tensor (that is, the batch dimension) and are all equal. This allows just passing a single `batchSize` constant. This function has no effect if you pass a `batchSize` value that's less than zero. - Parameter batchSize: The batch size. - Parameter function: The specific function to set shapes for. You may set this to `nil` if there is only one function. - SeeAlso: `BNNSGraphContextSetBatchSize`

method · iOS 18.0+

argumentCount

TypeScript
argumentCount(function_?: (string) | null): number
Swift declaration
func argumentCount(forFunction function: String? = nil) -> Int

The number of arguments for a graph function. - Parameter function: The specific function to query. You may set this to `nil` if there is only one function. - SeeAlso: `BNNSGraphGetArgumentCount`

method · iOS 18.0+

argumentNames

TypeScript
argumentNames(function_?: (string) | null): Array<string>
Swift declaration
func argumentNames(forFunction function: String? = nil) -> [String]

The argument names of outputs and inputs to a graph function. - Parameter function: The specific function to query. You may set this to `nil` if there is only one function. - SeeAlso: `BNNSGraphGetArgumentNames`

get · iOS 18.0+

functionCount

TypeScript
get functionCount(): number
Swift declaration
var functionCount: Int { get }

The number of callable functions in this graph. - Parameter function: The specific function to query. You may set this to `nil` if there is only one function. - SeeAlso: `BNNSGraphGetFunctionCount`

get · iOS 18.0+

functionNames

TypeScript
get functionNames(): Array<string>
Swift declaration
var functionNames: [String] { get }

The names of callable functions in the graph. - SeeAlso: `BNNSGraphGetFunctionNames`

method · iOS 26.0+

allocateTensor

TypeScript
allocateTensor(function_: (string) | null, argument: string, fillKnownDynamicShapes: boolean): (BNNSTensor) | null
Swift declaration
func allocateTensor(forFunction function: String? = nil, argument: String, fillKnownDynamicShapes: Bool) -> BNNSTensor?

Returns an allocated tensor for a given function argument. - Parameter function: The specific function to create the tensor for. You may set this to `nil` if there is only one function. - Parameter argument: The argument to create the tensor for. - Parameter fillKnownDynamicShapes: if `true`, the function replaces any dynamic shapes with shapes that will be used on next execution of `Context`, either drawn from default shapes in the `.mlmodelc` function or supplied by a preceding call to `setDynamicShapes(_:forFunction:)` or `setBatchSize(_:forFunction:)`. Otherwise the function represents dynamically-sized dimensions with a value of `-1` in either shape or stride as appropriate. - Note: This function supports `Float`, `Float16`, `Bool`, and `Int32` datatypes only and returns `nil` for other types. - SeeAlso: `BNNSGraphContextGetTensor`

init · iOS 26.0+

init

TypeScript
static initCompileFromPathFunctionNameOptions(path: string, functionName?: (string) | null, options?: BNNSGraph_CompileOptions): BNNSGraph_Context
Swift declaration
init(compileFromPath path: String, functionName: String? = nil, options: BNNSGraph.CompileOptions = CompileOptions()) throws

Synchronously returns a new context that wraps a graph object which represents the compiled `.mlmodelc` file. - Parameter path: The path to the `.mlmodelc` program file. - Parameter functionName: The name of a specific function to be compiled. Pass `nil` to specify that the function compiles all functions. - Parameter options: The compilation options. - SeeAlso: `BNNSGraphCompileFromFile` - SeeAlso: `BNNSGraphContextMake`

init · iOS 18.0+

init

TypeScript
static initCompileFromPathFunctionNameOptions86465(path: string, functionName?: (string) | null, options?: BNNSGraph_CompileOptions): Promise<BNNSGraph_Context>
Swift declaration
init(compileFromPath path: String, functionName: String? = nil, options: BNNSGraph.CompileOptions = CompileOptions()) async throws

Returns a new context that wraps a graph object which represents the compiled `.mlmodelc` file. - Parameter path: The path to the `.mlmodelc` program file. - Parameter functionName: The name of a specific function to be compiled. Pass `nil` to specify that the function compiles all functions. - Parameter options: The compilation options. - SeeAlso: `BNNSGraphCompileFromFile` - SeeAlso: `BNNSGraphContextMake`

method · iOS 26.0+

executeFunction

TypeScript
executeFunctionArguments(function_: (string) | null, arguments_: Array<BNNSTensor>): { $ret: null; $out: { "arguments": Array<BNNSTensor> } }
Swift declaration
func executeFunction(_ function: String? = nil, arguments: inout [BNNSTensor]) throws

Synchronously executes the specified function with the provided context. If the underlying model contains dynamic shaped inputs or outputs, these must be set prior to calling this routine through a call to either `setDynamicShapes(_:forFunction:)` or `setBatchSize(_:forFunction:)`. Don't modify the shapes again until this routine has returned. - Parameter function: The specific function to execute. You may set this to `nil` if there is only one function. - Parameter arguments: The output and input arguments. The same context must only be used by a single thread at a time. - SeeAlso: `BNNSGraphContextExecute`

Omit the output parameters from the call. The result contains $ret and $out, with arguments inside $out.

method · iOS 18.0+

executeFunction

TypeScript
executeFunctionArguments38708(function_: (string) | null, arguments_: Array<BNNSTensor>): Promise<{ $ret: null; $out: { "arguments": Array<BNNSTensor> } }>
Swift declaration
func executeFunction(_ function: String? = nil, arguments: inout [BNNSTensor]) async throws

Executes the specified function with the provided context. If the underlying model contains dynamic shaped inputs or outputs, these must be set prior to calling this routine through a call to either `setDynamicShapes(_:forFunction:)` or `setBatchSize(_:forFunction:)`. Don't modify the shapes again until this routine has returned. - Parameter function: The specific function to execute. You may set this to `nil` if there is only one function. - Parameter arguments: The output and input arguments. Note that the arguments may not be in the same order as the original `mlpackage` or `mlmodelc` file. Use the `argumentPosition(forFunction:argument:)` to get the correct position in the `arguments` array for a given argument. The same context must only be executed by a single thread at a time. - SeeAlso: `BNNSGraphContextExecute`

Omit the output parameters from the call. The result contains $ret and $out, with arguments inside $out.

method · iOS 18.0+

argumentPosition

TypeScript
argumentPosition(function_: (string) | null, argument: string): number
Swift declaration
func argumentPosition(forFunction function: String? = nil, argument: String) -> Int

Returns the position in the `arguments` array for a call to `executeFunction(_:arguments:)` for the given argument name. - Parameter function: The specific function to query. You may set this to `nil` if there is only one function. - Parameter argument: The input or output argument name that the function queries. - SeeAlso: `BNNSGraphContextGetArgumentPosition`

method · iOS 18.0+

setDynamicShapes

TypeScript
setDynamicShapes(shapes: Array<BNNSGraph_Shape>, function_?: (string) | null): Promise<Array<BNNSGraph_Shape>>
Swift declaration
func setDynamicShapes(_ shapes: [BNNSGraph.Shape], forFunction function: String? = nil) async throws -> [BNNSGraph.Shape]

Specifies the dynamic shapes for a graph and infers (if possible) output shapes. - Parameter shapes: The array of shapes for outputs and inputs, in the same order you pass to `executeFunction(_:arguments:)`. - Parameter function: The specific function to set shapes for. You may set this to `nil` if there is only one function. On entry, the function reads the input shapes from this array, unless indicated otherwise by setting `shapes[idx].rank = 0`, in which case the constant or default value from the `.mlmodelc` source is used. If you specify a non-zero value that does not match a constant size in the `.mlmodelc` source, the function generates an error and the output shapes are not read. On exit, the function returns output shapes that have `shapes[idx].rank != 0` to an upper bound on the expected output shape. If the function can't deduce the output shape as it depends on input data values, the value of `shapes[idx].size[d]` is set to zero for that dimension.

get · iOS 26.0+

streamingAdvanceCount

TypeScript
get streamingAdvanceCount(): number
Swift declaration
var streamingAdvanceCount: Int { get set }

Sets streaming advancement amount for cases with dynamically shaped inputs. - Parameters: - context: The graph context. - advance_count: An integer value that specifies the number of elements that the function advances the internal state pointer. For models compiled with the `BNNSOption` attribute `StateMode=Streaming` enabled, where `slice_update` operations use an update parameter of dynamic shape, BNNS can't unambigiously determine the streaming advancement size. In this case, call this function before calling ``BNNSGraphContextExecute`` to set the advancement size for each frame. This function advances the internal state pointer by `advance_count` elements in the streaming dimension before returning from ``BNNSGraphContextExecute``. > Note: The BNNS streaming APIs do not support models that require different advancement amounts for different states.

set · iOS 26.0+

streamingAdvanceCount

TypeScript
set streamingAdvanceCount(value: number)
Swift declaration
var streamingAdvanceCount: Int { get set }

Sets streaming advancement amount for cases with dynamically shaped inputs. - Parameters: - context: The graph context. - advance_count: An integer value that specifies the number of elements that the function advances the internal state pointer. For models compiled with the `BNNSOption` attribute `StateMode=Streaming` enabled, where `slice_update` operations use an update parameter of dynamic shape, BNNS can't unambigiously determine the streaming advancement size. In this case, call this function before calling ``BNNSGraphContextExecute`` to set the advancement size for each frame. This function advances the internal state pointer by `advance_count` elements in the streaming dimension before returning from ``BNNSGraphContextExecute``. > Note: The BNNS streaming APIs do not support models that require different advancement amounts for different states.

get · iOS 18.0+

checkForNaNsAndInfinities

TypeScript
get checkForNaNsAndInfinities(): boolean
Swift declaration
var checkForNaNsAndInfinities: Bool { get set }

Enables debug mode, checks intermediate tensors for nans and infinities. > - Not for use in production code. - SeeAlso: `BNNSGraphContextEnableNanAndInfChecks`

set · iOS 18.0+

checkForNaNsAndInfinities

TypeScript
set checkForNaNsAndInfinities(value: boolean)
Swift declaration
var checkForNaNsAndInfinities: Bool { get set }

Enables debug mode, checks intermediate tensors for nans and infinities. > - Not for use in production code. - SeeAlso: `BNNSGraphContextEnableNanAndInfChecks`

method · iOS 18.0+

tensor

TypeScript
tensor(function_: (string) | null, argument: string, fillKnownDynamicShapes: boolean): (BNNSTensor) | null
Swift declaration
func tensor(forFunction function: String? = nil, argument: String, fillKnownDynamicShapes: Bool) -> BNNSTensor?

Returns an unallocated tensor for a given function argument. - Parameter function: The specific function to create the tensor for. You may set this to `nil` if there is only one function. - Parameter argument: The argument to create the tensor for. - Parameter fillKnownDynamicShapes: if `true`, the function replaces any dynamic shapes with shapes that will be used on next execution of `Context`, either drawn from default shapes in the `.mlmodelc` function or supplied by a preceding call to `setDynamicShapes(_:forFunction:)` or `setBatchSize(_:forFunction:)`. Otherwise the function represents dynamically-sized dimensions with a value of `-1` in either shape or stride as appropriate. - SeeAlso: `BNNSGraphContextGetTensor`

Skipped declarations

1 declarations were excluded from these bindings. Each reason comes from the generator or the iOS SDK compiler.

BNNSGraph.Context.executeFunction(_:arguments:)

This declaration needs Swift generic type arguments. (no canonical witness for T)

All Accelerate types →