BNNSGraphContextSetDynamicShapes(_:_:_:_:)
Specifies the dynamic shapes for a graph and, if possible, infers, the output shapes.
Declaration
func BNNSGraphContextSetDynamicShapes(_ context: bnns_graph_context_t, _ function: UnsafePointer<CChar>?, _ shapes_count: Int, _ shapes: UnsafeMutablePointer<bnns_graph_shape_t>) -> Int32Parameters
- context:
The graph context.
- function:
The function. Specify as
nilif the graph only contains one function. - shapes_count:
The number of elements in the
shapesarray. - shapes:
An array of input and output shapes in the same order as you pass to Bnnsgraphcontextexecute(_:_:_:_:_:_:).
On input, this function reads input shapes with a nonzero rank, and uses the constant or default value from the source model for input shapes with a zero rank. The function generates an error for shapes with a nonzero value that doesn’t match the source model.
On output, the function sets output shapes with a nonzero rank to the upper bounds of the expected output shape. If the function can’t deduce the output shape because it depends on the input data values, the value of
shapes[idx].size[d]is zero.
Return Value
- 0 on success if all tensor shapes were exactly determined. That is, the workspace size is exact.
Discussion
1on success if one or more tensor shapes are merely bounds, but no tensor is unbounded. That is, the workspace size is bounded.2on success if one or more tensor shapes are unbounded and BNNS will allocate workspace memory during execution.A negative value indicates an error.
Discussion
Don’t call this function while existing calls to BNNSGraphContextExecute(_:_:_:_:_:_:) are running.
For example, the following code sets the dynamic shapes for an example graph context, specifies the output shape, and updates the context’s required workspace size.
var inputShape: [UInt64] = [1024, 1, 1]
let rank = inputShape.count
var outputShape = [UInt64](repeating: 0, count: rank)
let result = outputShape.withUnsafeMutableBufferPointer { output in
inputShape.withUnsafeMutableBufferPointer { input in
var shapes = [
bnns_graph_shape_t(rank: rank, shape: output.baseAddress!),
bnns_graph_shape_t(rank:rank, shape: input.baseAddress!)
]
return BNNSGraphContextSetDynamicShapes(context, nil,
shapes.count, &shapes)
}
}
// Prints "[1024, 1, 1]".
print(outputShape)On return, the output shape contains the correct size for the input shape, and a subsequent call to BNNSGraphContextGetWorkspaceSize(_:_:) returns the correct workspace size.