Transformation - Streamline

Transformation (Streamline)

class finn.transformation.streamline.Streamline

Bases: Transformation

Apply the streamlining transform, see arXiv:1709.04060.

apply(model)

finn.transformation.streamline.absorb

class finn.transformation.streamline.absorb.Absorb1BitMulIntoConv

Bases: Transformation

Absorb bipolar or binary multiplications into the preceding convolution.

apply(model)
class finn.transformation.streamline.absorb.Absorb1BitMulIntoMatMul

Bases: Transformation

Absorb bipolar or binary multiplications into the preceding matrix multiply.

apply(model)
class finn.transformation.streamline.absorb.AbsorbAddIntoMultiThreshold

Bases: Transformation

Absorb preceding Add ops into MultiThreshold by updating the threshold values. Only scalar/1D add vectors can be absorbed.

apply(model)
class finn.transformation.streamline.absorb.AbsorbConsecutiveTransposes

Bases: Transformation

Remove (Transpose -> Transpose) patterns when the input and output of the pattern have the same layout.

apply(model)
are_opposite_permutations(perms1, perms2)
class finn.transformation.streamline.absorb.AbsorbMulIntoMultiThreshold

Bases: Transformation

Absorb preceding Mul ops into MultiThreshold by updating the threshold values. Only positive scalar/1D mul vectors can be absorbed.

apply(model)
class finn.transformation.streamline.absorb.AbsorbScalarBiasIntoMultiThreshold(max_bitwidth_increase=0)

Bases: Transformation

Absorb a scalar bias following a MultiThreshold (as a downstream Add) back into the MultiThreshold’s out_bias and re-evaluate the output datatype.

Unlike the historical sign-bias-only version, this absorbs any scalar bias irrespective of signedness. Because the hardware carries out_bias as an additive term, absorbing a (large positive) bias can widen the output datatype. To keep this from inflating the downstream datapath, absorption is only performed when the resulting output bitwidth does not grow by more than max_bitwidth_increase bits (default 0, i.e. only absorb when the output width stays the same or shrinks). Set a larger value to explicitly allow trading a wider output type for one fewer elementwise Add node.

apply(model: ModelWrapper)
class finn.transformation.streamline.absorb.AbsorbScalarMulAddIntoTopK

Bases: Transformation

Remove mul/add node prior to topk node if the op is scalar. Note that the TopK output probabilities will change, but the indices won’t.

apply(model)
class finn.transformation.streamline.absorb.AbsorbSignBiasIntoMultiThreshold(*args, **kwargs)

Bases: AbsorbScalarBiasIntoMultiThreshold

Deprecated alias for AbsorbScalarBiasIntoMultiThreshold.

The transformation was renamed because it no longer only absorbs sign bias but any scalar bias. This alias is kept for backwards compatibility and will be removed in a future release.

class finn.transformation.streamline.absorb.AbsorbTransposeIntoFlatten

Bases: Transformation

Absorb transpose node into succeeding flatten node, if H=W=1 and the first dimension stays the same. Can also be applied if flatten is implemented implicitly by a reshape node with shape [1, -1] and the first input dimension is 1

apply(model)
class finn.transformation.streamline.absorb.AbsorbTransposeIntoMultiThreshold

Bases: Transformation

For (NCHWTranspose -> MultiThreshold) move Transpose past MultiThreshold and set its data_layout mode to NHWC.

apply(model)
class finn.transformation.streamline.absorb.AbsorbTransposeIntoResize

Bases: Transformation

For (NCHWTranspose -> Resize) move Transpose past Resize and change the Resize node’s attributes accordingly.

apply(model)
class finn.transformation.streamline.absorb.FactorOutMulSignMagnitude

Bases: Transformation

Split multiply-by-constant nodes into two multiply-by-constant nodes, where the first node is a bipolar vector (of signs) and the second is a vector of magnitudes.

apply(model)

finn.transformation.streamline.collapse_repeated

class finn.transformation.streamline.collapse_repeated.CollapseRepeatedAdd

Bases: CollapseRepeatedOp

Collapse repeated adder node into a single operation.

class finn.transformation.streamline.collapse_repeated.CollapseRepeatedMul

Bases: CollapseRepeatedOp

Collapse repeated multiplier node into a single operation.

class finn.transformation.streamline.collapse_repeated.CollapseRepeatedOp(op_name, make_collapsed_param_fxn)

Bases: Transformation

Collapse repeated consecutive operations with constant parameters into a single operation. make_collapsed_param_fxn must take two tensors and return a tensor which gives the equivalent result using a single op.

apply(model)

finn.transformation.streamline.reorder

class finn.transformation.streamline.reorder.MakeMaxPoolNHWC

Bases: Transformation

Convert (MaxPool, NHWCTranspose) into (NHWCTranspose, MaxPoolNHWC) and (NCHWTranspose, MaxPool) into (MaxPoolNHWC, NCHWTranspose).

apply(model)
class finn.transformation.streamline.reorder.MakeScaleResizeNHWC

Bases: Transformation

Converts the inputs and outputs for all scales Resize and Upsample nodes from NCHW to NHWC.

apply(model)
class finn.transformation.streamline.reorder.MoveAddPastConv

Bases: Transformation

Move scalar and channelwise add operations past conv operations. We want to have adds next to each other such that they can be collapsed into a single add.

apply(model)
class finn.transformation.streamline.reorder.MoveAddPastFork

Bases: MoveOpPastFork

class finn.transformation.streamline.reorder.MoveAddPastJoinAdd

Bases: MoveIdenticalOpPastJoinOp

are_producers_identical(model, producers)

Checks only op_types Should be overwritten for additional checks

move_node(model, n, producers)

We use the base move_node method to move the first producer past the join node (and delete the rest)

class finn.transformation.streamline.reorder.MoveAddPastJoinConcat

Bases: MoveAffinePastJoinConcat

class finn.transformation.streamline.reorder.MoveAddPastMul

Bases: Transformation

Move add operations past multiply operations on linear segments of the graph. The aim is to have them next to each other such that they can be collapsed into a single add.

apply(model)
class finn.transformation.streamline.reorder.MoveAffinePastJoinConcat(linear_ops=['Mul', 'Add'])

Bases: MoveIdenticalOpPastJoinOp

Applies to scalar linear or channelwise affine ops with the same parameter value

are_producers_channelwise_ops(channel_dim, model, producers)
are_producers_identical_scalar_ops(model, producers)
move_node(model, n, producers)

Should be overwritten for some operations

Returns:

bool: whether moving the node was successful

class finn.transformation.streamline.reorder.MoveFlattenPastAffine

Bases: Transformation

Moves a node that implements a (1, -1) reshape past a MatMul, Mul or Add node.

apply(model)
class finn.transformation.streamline.reorder.MoveFlattenPastTopK

Bases: Transformation

Move flatten node past a succeeding topk node, if the “axis” attribute in topk is set to -1 and the data layout before the flatten is NHWC with H=W=1

apply(model)
class finn.transformation.streamline.reorder.MoveIdenticalOpPastJoinOp(identical_op_list, join_node_list)

Bases: Transformation

Move multiple identical operations on different branches past the common join node. It assumes the shape to be preserved by the join op in the default move_node() method

apply(model)
are_producers_identical(model, producers)

Checks only op_types Should be overwritten for additional checks

move_node(model, n, producers)

Should be overwritten for some operations

Returns:

bool: whether moving the node was successful

class finn.transformation.streamline.reorder.MoveLinearPastFork

Bases: MoveOpPastFork

class finn.transformation.streamline.reorder.MoveMaxPoolPastMultiThreshold

Bases: Transformation

Move MaxPool nodes past MultiThreshold nodes on linear segments of the graph.

apply(model)
class finn.transformation.streamline.reorder.MoveMulPastDWConv

Bases: Transformation

Move channelwise mul operations past depthwise conv operations. We want to have muls next to each other such that they can be collapsed into a single mul.

apply(model)
class finn.transformation.streamline.reorder.MoveMulPastFork

Bases: MoveOpPastFork

class finn.transformation.streamline.reorder.MoveMulPastJoinAdd

Bases: MoveIdenticalOpPastJoinOp

are_producers_identical(model, producers)

Checks only op_types Should be overwritten for additional checks

class finn.transformation.streamline.reorder.MoveMulPastJoinConcat

Bases: MoveAffinePastJoinConcat

class finn.transformation.streamline.reorder.MoveMulPastJoinMul

Bases: MoveIdenticalOpPastJoinOp

Move constant Mul operations past a join Mul node, folding the two constants into a single one: (x*A) * (y*B) -> (x*y) * (A*B). Unlike MoveMulPastJoinAdd, the two constants need not be equal since they are combined into their product.

are_producers_identical(model, producers)

Checks only op_types Should be overwritten for additional checks

move_node(model, n, producers)

Fold the producer constants into their product on the surviving producer, then use the base move_node to move it past the join node (and delete the rest).

class finn.transformation.streamline.reorder.MoveMulPastMaxPool

Bases: Transformation

Move non-negative scalar or channelwise mul operations past max pool operations. We want to have muls next to each other such that they can be collapsed into a single mul.

apply(model)
class finn.transformation.streamline.reorder.MoveOpPastFork(op_name_list)

Bases: Transformation

Move node operations past graph forks. Used when a node before a fork can be merged with nodes in the branches

apply(model)
class finn.transformation.streamline.reorder.MoveScalarAddPastMatMul

Bases: Transformation

Move scalar add operations past matmul operations. We want to have adds next to each other such that they can be collapsed into a single add.

apply(model)
class finn.transformation.streamline.reorder.MoveScalarLinearPastInvariants

Bases: Transformation

Move scalar linear operations (mul, add) past functions which are invariant to them. Specifically, matches and transforms the following patterns: f(x*C) -> f(x) * C f(x+C) -> f(x) + C where x is a dynamic input, C is a constant tensor. Known f which obey this property are: Reshape, Flatten, Transpose, GlobalAveragePool

SUPPORTED_INVARIANTS = {'Flatten', 'GlobalAveragePool', 'Reshape', 'Slice', 'Squeeze', 'Transpose', 'Unsqueeze'}
apply(model)
class finn.transformation.streamline.reorder.MoveScalarLinearPastSplit

Bases: Transformation

Move scalar Mul and Add nodes past channel split operation.

apply(model)
class finn.transformation.streamline.reorder.MoveScalarMulPastConv

Bases: Transformation

Move scalar mul operations past conv operations. We want to have muls next to each other such that they can be collapsed into a single mul.

apply(model)
class finn.transformation.streamline.reorder.MoveScalarMulPastConvTranspose

Bases: Transformation

Move scalar mul operations past ConvTranspose operations. We want to have muls next to each other such that they can be collapsed into a single mul.

apply(model)
class finn.transformation.streamline.reorder.MoveScalarMulPastMatMul

Bases: Transformation

Move scalar mul operations past matmul operations. We want to have muls next to each other such that they can be collapsed into a single mul.

apply(model)
class finn.transformation.streamline.reorder.MoveTransposePastFork

Bases: MoveOpPastFork

class finn.transformation.streamline.reorder.MoveTransposePastJoinAdd

Bases: MoveIdenticalOpPastJoinOp

are_producers_identical(model, producers)

Checks only op_types Should be overwritten for additional checks

class finn.transformation.streamline.reorder.MoveTransposePastJoinConcat

Bases: MoveIdenticalOpPastJoinOp

are_producers_identical(model, producers)

Checks only op_types Should be overwritten for additional checks

move_node(model, n, producers)

Should be overwritten for some operations

Returns:

bool: whether moving the node was successful

class finn.transformation.streamline.reorder.MoveTransposePastScalarMul

Bases: Transformation

Moves a Transpose node past a scalar Mul node

apply(model)
class finn.transformation.streamline.reorder.MoveTransposePastSplit

Bases: Transformation

apply(model)
finn.transformation.streamline.reorder.permute_shape(shape, perm)

finn.transformation.streamline.round_thresholds

class finn.transformation.streamline.round_thresholds.RoundAndClipThresholds

Bases: Transformation

For MultiThreshold, Thresholding, MVAU, and VVAU nodes operating on integer inp/accumulators, round up (ceil) threshold values to the nearest integer and clip to valid range. Type-casts thresholds (back) to the float32 container type (this is separate from the quantization annotation). Runs InferDataTypes() afterward to propagate any changes to the quantization data types.

apply(model: ModelWrapper)

finn.transformation.streamline.sign_to_thres

class finn.transformation.streamline.sign_to_thres.ConvertSignToThres

Bases: Transformation

Convert Sign node instances to MultiThreshold with threshold at 0.

apply(model)