MCPcopy Create free account
hub / github.com/Oneflow-Inc/oneflow / iter_nodes

Method iter_nodes

python/oneflow/framework/args_tree.py:146–185  ·  view source on GitHub ↗

r""" return a generator of the args tree nodes in the DFS manner. The node returned can be of type NamedArg or non-NamedArg depending on whether gen_name is set. If gen_name is set, the node will be NamedArg.

(self)

Source from the content-addressed store, hash-verified

144 return self._gen_name
145
146 def iter_nodes(self):
147 r"""
148 return a generator of the args tree nodes in the DFS manner.
149 The node returned can be of type NamedArg or non-NamedArg depending on whether gen_name is set.
150 If gen_name is set, the node will be NamedArg.
151 """
152
153 if self._gen_name:
154 args_to_iter = self._named_io_args
155 else:
156 args_to_iter = self._io_args
157
158 # NOTE(lixiang): Generator expression and iterator are used.
159 # This avoids generating the full list in memory and only processes the nodes that need to be processed,
160 # reducing time and space consumption.
161 stack = [iter([args_to_iter])]
162 while len(stack) > 0:
163 try:
164 curr = next(stack[-1])
165 if _is_raw_type(curr, NamedArg):
166 curr_value = curr.value()
167 else:
168 curr_value = curr
169
170 if _is_raw_type(curr_value, list) or _is_raw_type(curr_value, tuple):
171 children = curr_value
172 elif _is_raw_type(curr_value, dict) or _is_raw_type(
173 curr_value, OrderedDict
174 ):
175 children = curr_value.values()
176 else:
177 children = None
178
179 if children:
180 stack.append(iter(children))
181
182 yield curr
183
184 except StopIteration:
185 stack.pop()
186
187 def iter_named_nodes(self):
188 assert self._gen_name, "Only use this if gen_name is set!"

Callers 9

iter_named_nodesMethod · 0.95
check_input_globalFunction · 0.95
__all_globalMethod · 0.95
__all_localMethod · 0.95
fnMethod · 0.95
__flatten_ioMethod · 0.95
gen_keyMethod · 0.95

Calls 5

_is_raw_typeFunction · 0.85
valueMethod · 0.45
valuesMethod · 0.45
appendMethod · 0.45
popMethod · 0.45

Tested by 4

__all_globalMethod · 0.76
__all_localMethod · 0.76
fnMethod · 0.76