func concatenate<T>(_ tensors: [BNNSGraph.Builder.Tensor<T>], axis: Int) -> BNNSGraph.Builder.Tensor<T> where T : BNNSScalar
Adds a concatenation operation to the current graph.
- Parameter tensors: An array that contains the tensors that the operation concatenates.
- Parameter axis: The axis along which the operation performs concatenation.
func argument<T>(name: String? = nil, dataType: T.Type, shape: [Int], intent: BNNSGraph.Builder.Intent = .input) -> BNNSGraph.Builder.Tensor<T> where T : BNNSScalar
Registers and returns an input or in-out tensor argument to the graph.
- Parameter name: The name of the argument.
- Parameter dataType: The argument's data type.
- Parameter shape: An array of integers or closed ranges that define the sizes of each
dimension of the argument. Pass a negative value to specify a flexible dimension.
- Parameter intent: A constant that specifies whether the argument is input, output, or in-out.
The following code shows how to specify inputs with flexible shapes. The code calls `setBatchSize`
before executing the context to specify that the two inputs and the output tensors are 8-element vectors:
```
let context = try? BNNSGraph.makeContext ({
builder in
let x = builder.argument(name: "x",
dataType: Float.self,
shape: [-1])
let y = builder.argument(name: "y",
dataType: Float.self,
shape: [-1])
let z = x + y
return [z]
})
var args = context.argumentNames().map {
name in
return context.tensor(argument: name,
fillKnownDynamicShapes: false)!
}
defer {
args.forEach {
$0.deallocate()
}
}
let n = 8
context.setBatchSize(n)
args[context.argumentPosition(argument: "x")]
.allocate(initializingFrom: [1, 2, 3, 4, 5, 6, 7, 8] as [Float])
args[context.argumentPosition(argument: "y")]
.allocate(initializingFrom: [10, 20, 30, 40, 50, 60, 70, 80] as [Float])
// Output argument
args[0].allocate(as: Float.self, count: n)
try context.executeFunction(arguments: &args)
```
On return, the output tensor contains the values `[11, 22, 33, 44, 55, 66, 77, 88]`.
func constant<T>(name: String? = nil, value: T) -> BNNSGraph.Builder.Tensor<T> where T : BNNSScalar
Registers and returns a tensor that contains a constant scalar value.
- Parameter name: The name of the tensor.
- Parameter value: The scalar value that the function copies into the compiled context.
For example, the following code defines the tensor `x` that contains the single-precision
scalar value `10.0`:
```
let context = try BNNSGraph.makeContext ({
builder in
let x = builder.constant(name: "x",
value : Float(10))
let y = builder.constant(name: "y",
values : [1, 2, 3, 4, 5, 6, 7, 8] as [Float])
let z = x + y
return [z]
})
var results = [context.allocateTensor(argument: context.argumentNames()[0],
fillKnownDynamicShapes: false)!]
defer {
results[0].deallocate()
}
try context.executeFunction(arguments: &results)
```
On return, the `results[0]` tensor contains the values `[11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0]`.
Returns a rank 2 tensor from an array of arrays.
- Precondition: Each sub-array must contain the same number of elements.
The following code shows how to use this function to multiply two 2x2 matrices:
```
let xValues = [[1, 2],
[3, 4]] as [[Float]]
let yValues = [[5, 6],
[7, 8]] as [[Float]]
let context = try BNNSGraph.makeContext {
builder in
let x = builder.constant(values: xValues)
let y = builder.constant(values: yValues)
let z = x.matmul(other: y)
return [z] // On return, `z` equals `[19.0, 22.0, 43.0, 50.0]`.
}
```