Skip to content

Commit

Permalink
[Deprecated] MoveLinearPastEltwiseAdd() removed from the codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal Danilowicz committed Sep 20, 2024
1 parent 39a2efe commit 51a9199
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 273 deletions.
81 changes: 0 additions & 81 deletions src/finn/transformation/streamline/reorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,87 +517,6 @@ def apply(self, model):
return (model, graph_modified)


class MoveLinearPastEltwiseAdd(Transformation):
"""
DEPRECATED, use MoveAddPastJoinAdd() and MoveMulPastJoinAdd()
Move linear operations (mul, add) past elementwise add operations where possible.
Specifically,matches and transforms the following patterns:
(x*C) + (y*C) -> (x + y) * C
(x+A) + (y+B) -> (x + y) + (A + B)
where x and y are dynamic inputs, A, B, C are constant tensors (in general).
"""

def move_node(self, graph, n, prod0, prod1, node_ind):
# found! move one of the muls to output, remove the other one
lin0_in0 = prod0.input[0]
lin1_in0 = prod1.input[0]
in0 = n.input[0]
out = n.output[0]
# TODO: check shapes don't change through scalar mul or add
# connect the eltwise add inputs to mul inputs
n.input[0] = lin0_in0
n.input[1] = lin1_in0
# connect mul0 output to eltwise add output
prod0.output[0] = out
# connect the input of mul0 and output of eltwise add together
n.output[0] = in0
prod0.input[0] = in0
# move prod0 node past eltwise add node, and remove prod1
graph.node.remove(prod1)
graph.node.remove(prod0)
graph.node.insert(node_ind - 2, prod0)

def apply(self, model):
graph = model.graph
node_ind = 0
graph_modified = False
nodes = [n for n in graph.node]
for n in nodes:
node_ind += 1
if n.op_type == "Add":
# check for tensors on both inputs (eltwise add)
# scalar add has an initializer on one input
in0 = n.input[0]
in1 = n.input[1]
if in0 is None or in1 is None:
continue
A = model.get_initializer(in0)
B = model.get_initializer(in1)
if A is not None or B is not None:
continue
# check for mul with same initializer on both inputs
prod0 = model.find_producer(in0)
prod1 = model.find_producer(in1)
# Also check case when both branches are empty and come
# from the same node: (prod0 == prod1)
# Other transform should handle that
if prod0 is None or prod1 is None or (prod0 == prod1):
continue
if len(prod0.input) < 2 or len(prod1.input) < 2:
continue
init0 = model.get_initializer(prod0.input[1])
init1 = model.get_initializer(prod1.input[1])
# if either initializer is None, skip
if init0 is None or init1 is None:
continue
if prod0.op_type == "Mul" and prod1.op_type == "Mul":
if np.array_equal(init0, init1):
self.move_node(graph, n, prod0, prod1, node_ind)
node_ind -= 1
graph_modified = True
elif prod0.op_type == "Add" and prod1.op_type == "Add":
init = init0 + init1
# update initializer of prod0, which we'll move
model.set_initializer(prod0.input[1], init)
self.move_node(graph, n, prod0, prod1, node_ind)
node_ind -= 1
graph_modified = True
else:
continue
model = model.transform(InferShapes())
return (model, graph_modified)


class MoveScalarLinearPastInvariants(Transformation):
"""Move scalar linear operations (mul, add) past functions which are invariant
to them. Specifically, matches and transforms the following patterns:
Expand Down
192 changes: 0 additions & 192 deletions tests/transformation/streamline/test_linear_past_eltwise.py

This file was deleted.

0 comments on commit 51a9199

Please sign in to comment.