@@ -44,12 +44,18 @@ class Tape:
4444 ),
4545 ir_version=10,
4646 )
47+
48+ Attributes:
49+ graph_like: The graph to append the new nodes and initializers to. When
50+ it is None, the nodes and initializers are creating without owned by a graph.
51+ Initializers will not be added to functions because it is not supported by ONNX.
4752 """
4853
49- def __init__ (self ) -> None :
54+ def __init__ (self , graph_like : ir . Graph | ir . Function | None = None ) -> None :
5055 self ._nodes : list [ir .Node ] = []
5156 self ._initializers : list [ir .Value ] = []
5257 self ._used_opsets : UsedOpsets = set ()
58+ self .graph_like = graph_like
5359
5460 def __repr__ (self ) -> str :
5561 return f"Tape(nodes={ self ._nodes } , initializers={ self ._initializers } )"
@@ -92,7 +98,7 @@ def op(
9298 num_outputs = 1 ,
9399 overload = overload ,
94100 version = version ,
95- graph = graph ,
101+ graph = graph or self . graph_like ,
96102 name = name ,
97103 doc_string = doc_string ,
98104 metadata_props = metadata_props ,
@@ -129,7 +135,7 @@ def op_multi_output(
129135 num_outputs = num_outputs ,
130136 overload = overload ,
131137 version = version ,
132- graph = graph ,
138+ graph = graph or self . graph_like ,
133139 name = name ,
134140 doc_string = doc_string ,
135141 metadata_props = metadata_props ,
@@ -148,6 +154,8 @@ def initializer(self, tensor: ir.TensorProtocol, name: str | None = None) -> ir.
148154 name = name , shape = shape , type = ir .TensorType (tensor .dtype ), const_value = tensor
149155 )
150156 self ._initializers .append (value )
157+ if isinstance (self .graph_like , ir .Graph ):
158+ self .graph_like .register_initializer (value )
151159 return value
152160
153161
0 commit comments