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

Handle ExtractConvBias properly and remove unused bias Quant nodes #122

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 13 additions & 7 deletions src/qonnx/transformation/extract_conv_bias.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,10 @@


class ExtractBiasFromConv(Transformation):
"""
Extracts the (optional) Bias from a Conv(Transpose) node and inserts it behind the
Conv(Transpose) node as an Add node.
"""

def apply(self, model):
graph = model.graph
node_ind = 0
nodes_to_remove = []
for n in graph.node:
node_ind += 1
if n.op_type in ["Conv", "ConvTranspose"]:
Expand All @@ -49,9 +45,16 @@ def apply(self, model):
# Extract bias
bias = model.get_initializer(n.input[2])
if bias is None:
warnings.warn(f"Could not extract bias from node {n}")
continue
producer = model.find_producer(n.input[2])
bias = model.get_initializer(producer.input[0])
if bias is None:
warnings.warn(f"Could not extract bias from node")
continue

if producer is not None:
# Mark the producer node for removal
nodes_to_remove.append(producer)

# Insert bias as Add node behind the Conv node
out_shape = model.get_tensor_shape(n.output[0])
# Reshape bias tensor
Expand Down Expand Up @@ -80,6 +83,9 @@ def apply(self, model):
# Repoint Conv output and remove bias tensor
n.output[0] = act_add_tensor.name
n.input.remove(n.input[2])

for node_to_remove in nodes_to_remove:
graph.node.remove(node_to_remove)

return model, True

Expand Down