Transformation

Guide to writing FINN transformations

  • Your transformation must inherit the Transformation abstract base class.

  • Your transformation’s apply function should take in a ModelWrapper, and return a tuple with (transformed_model: ModelWrapper, model_was_changed: Bool)

  • The transformations are meant to be applied using the .transform function in ModelWrapper. This makes a deep copy of the input model by default, so you don’t have to.

  • model_was_changed indicates whether your transformation made any changes to the model. If you know your transformation needs to be called only once and repeated calls have no further effect, you can return False even if the model was changed.

  • You MUST return model_was_changed=False at some point when your transformation is called multiple times, otherwise apply_repeated() will loop infinitely.

  • If you cannot guarantee that the transformation will reach a fixed point, you must declare this, return model_was_changed = False and let the user manually re-apply the transform.

class finn.transformation.Transformation

Bases: abc.ABC

Transformation class all transformations are based on. Contains only abstract method apply() every transformation has to fill.

abstract apply(model)

Transformation Passes

finn.transformation.batchnorm_to_affine

class finn.transformation.batchnorm_to_affine.BatchNormToAffine

Bases: finn.transformation.Transformation

Replaces any test-time BatchNorm layers with Mul-Add layers.

apply(model)

finn.transformation.bipolar_to_xnor

class finn.transformation.bipolar_to_xnor.ConvertBipolarMatMulToXnorPopcount

Bases: finn.transformation.Transformation

Convert MatMul nodes with all-bipolar inputs to XnorPopcountMatMul and associated result correction.

apply(model)

finn.transformation.fold_constants

class finn.transformation.fold_constants.FoldConstants

Bases: finn.transformation.Transformation

Replace the output of a node with const-only inputs with a precomputed result.

apply(model)

finn.transformation.general

class finn.transformation.general.ConvertSubToAdd

Bases: finn.transformation.Transformation

Convert sub nodes to add nodes of appropriate sign.

apply(model)
class finn.transformation.general.GiveRandomTensorNames

Bases: finn.transformation.Transformation

Give random tensor names to all tensors.

apply(model)
class finn.transformation.general.GiveReadableTensorNames

Bases: finn.transformation.Transformation

Give more human-readable names to all internal tensors. It’s recommended to apply give_unique_node_names prior to this transform.

apply(model)
class finn.transformation.general.GiveUniqueNodeNames

Bases: finn.transformation.Transformation

Give unique names to each node in the graph using enumeration.

apply(model)

finn.transformation.infer_datatypes

class finn.transformation.infer_datatypes.InferDataTypes

Bases: finn.transformation.Transformation

Infer FINN DataType info for all intermediate/output tensors based on inputs and node type.

apply(model)

finn.transformation.infer_shapes

class finn.transformation.infer_shapes.InferShapes

Bases: finn.transformation.Transformation

Ensure every tensor in the model has a specified shape (ValueInfo).

apply(model)