Skip to content

Commit 3737346

Browse files
committed
fix unused
1 parent 6532733 commit 3737346

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

onnx_array_api/graph_api/graph_builder.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,14 @@
33
import numpy as np
44
import onnx.helper as oh
55
import onnx.numpy_helper as onh
6-
from onnx import AttributeProto, FunctionProto, ModelProto, NodeProto, TensorProto
6+
from onnx import (
7+
AttributeProto,
8+
FunctionProto,
9+
GraphProto,
10+
ModelProto,
11+
NodeProto,
12+
TensorProto,
13+
)
714
from onnx.reference import ReferenceEvaluator
815

916
T = "TENSOR"
@@ -655,6 +662,22 @@ def optimize(self, check_order: bool = False):
655662
if check_order:
656663
self.check_order()
657664

665+
def hidden_inputs_graph(self, graph: GraphProto) -> Set[str]:
666+
hidden = set()
667+
memo = set(i.name for i in graph.initializer)
668+
memo |= set(i.name for i in graph.sparse_initializer)
669+
for node in graph.node:
670+
for i in node.input:
671+
if i not in memo:
672+
hidden.add(i)
673+
for att in node.attribute:
674+
if att.type == AttributeProto.GRAPH and att.g:
675+
hid = self.hidden_inputs_graph(att.g)
676+
less = set(h for h in hid if h not in memo)
677+
hidden |= less
678+
memo |= set(node.output)
679+
return hidden
680+
658681
def remove_unused(self):
659682
"""
660683
Simple function to remove unused nodes.
@@ -671,6 +694,11 @@ def remove_unused(self):
671694
for i in node.input:
672695
marked[o].add(i)
673696
used = True
697+
for att in node.attribute:
698+
if att.type == AttributeProto.GRAPH and att.g:
699+
hidden_inputs = self.hidden_inputs_graph(att.g)
700+
for i in hidden_inputs:
701+
marked[i] = set()
674702
if used:
675703
for i in node.input:
676704
marked[i] = set()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy