Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Streamline] Extend set of invariants for MoveScalarLinearPastInvariants #30

Merged
merged 3 commits into from
Jan 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions src/finn/transformation/streamline/reorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,19 @@ class MoveScalarLinearPastInvariants(Transformation):
GlobalAveragePool
"""

# Op-types of currently supported invariants
SUPPORTED_INVARIANTS = {
"GlobalAveragePool",
"Identity",
"Reshape",
"Transpose",
"Flatten",
"Expand",
"Slice",
"Squeeze",
"Unsqueeze",
}

def apply(self, model):
graph = model.graph
node_ind = 0
Expand All @@ -618,13 +631,7 @@ def apply(self, model):
# Extract mode and scales and input shape
mode = get_by_name(n.attribute, "mode").s.decode("ascii")
is_nearest_neighbor_resample = mode == "nearest"
if (
n.op_type == "GlobalAveragePool"
or n.op_type == "Reshape"
or n.op_type == "Transpose"
or n.op_type == "Flatten"
or is_nearest_neighbor_resample
):
if n.op_type in self.SUPPORTED_INVARIANTS or is_nearest_neighbor_resample:
in0 = n.input[0]
if in0 is None:
continue
Expand All @@ -633,7 +640,17 @@ def apply(self, model):
if prod0 is None:
continue

if prod0.op_type in ["Mul", "Add", "Div"]:
if prod0.op_type in ["Mul", "Div", "Add", "Sub"]:
# Cannot handle fork-nodes, try MoveLinearPastFork first
if model.is_fork_node(prod0):
warnings.warn(
f"{self.__class__.__name__}:"
f" Skipping near match: {prod0.name} is a fork-node,"
f" try MoveLinearPastFork first"
)
# Skip transforming this node as moving this would lead
# to messed up or detached graph
continue
# check if second input of producer is an initializer
init0 = model.get_initializer(prod0.input[1])
# if either initializer is None, skip
Expand Down