makeContext(options:_:)
Returns a new context that wraps a graph object that the given closure defines.
Declaration
static func makeContext(options: BNNSGraph.CompileOptions = CompileOptions(), _ block: (inout BNNSGraph.Builder) -> [any BNNSGraph.TensorDescriptor]) throws -> BNNSGraph.ContextParameters
- options:
The compilation options.
- block:
A closure with a
BNNSGraph.Builderparameter that points to the BNNS Graph builder.
Discussion
Use this function to specify the operations that define a BNNS graph. For example, the following code creates a graph that performs element-wise multiplication of two eight-element vectors:
let context = try BNNSGraph.makeContext ({
builder in
let x = builder.argument(name: "x",
dataType: Float.self,
shape: [8])
let y = builder.argument(name: "y",
dataType: Float.self,
shape: [8])
let z = x * y
return [z]
})The following code defines the arguments and executes the graph:
var args = context.argumentNames().map {
name in
return context.tensor(argument: name,
fillKnownDynamicShapes: false)!
}
defer {
args.forEach {
$0.deallocate()
}
}
args[context.argumentPosition(argument: "x")]
.allocate(initializingFrom: [1, 2, 3, 4, 5, 6, 7, 8] as [Float])
args[context.argumentPosition(argument: "y")]
.allocate(initializingFrom: [8, 7, 6, 5, 4, 3, 2, 1] as [Float])
// Output argument
args[0].allocate(as: Float.self, count: 8)
try context.executeFunction(arguments: &args)On return, args[0] contains the values [8.0, 14.0, 18.0, 20.0, 20.0, 18.0, 14.0, 8.0].