Skip to content
Snippets Groups Projects
Commit aa206a66 authored by Yaman Umuroglu's avatar Yaman Umuroglu
Browse files

[Transform] add apply_repeated, add some docs on adding transforms

parent 16cc40a2
No related branches found
No related tags found
No related merge requests found
"""
Guide to writing FINN transformations
-------------------------------------
* Your transformation should take in an ONNX model, and return a tuple with
(transformed_model: ModelProto, model_was_changed: Bool)
* The original model should not be modified, use e.g. copy.deepcopy() if you
want to work on a copy of the graph for modifications.
* 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 and return only the transformed model instead of a tuple.
"""
......@@ -13,6 +13,20 @@ def give_unique_names(model):
return new_model
def apply_repeated(model, transform):
"""Applies given transform repeatedly until no more changes can be made.
Transform must return (transformed_model, model_was_changed)."""
transformed_model = model
model_was_changed = True
while model_was_changed:
(transformed_model, model_was_changed) = transform(transformed_model)
return transformed_model
# TODO consider making a wrapper for ONNX model and make the below functions
# members - they aren't really proper transformations
def get_tensor_shape(model, tensor_name):
"""Returns the shape of tensor with given name, if it has ValueInfoProto."""
graph = model.graph
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment