MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / replace_vars

Method replace_vars

imperative/python/megengine/utils/network.py:378–399  ·  view source on GitHub ↗

r"""Replaces vars in the graph. Args: repl_dict: the map {old_var: new_var} that specifies how to replace the vars.

(self, repl_dict: Dict[VarNode, VarNode])

Source from the content-addressed store, hash-verified

376 assert not blacklist, "unused items in blacklist: {}".format(blacklist)
377
378 def replace_vars(self, repl_dict: Dict[VarNode, VarNode]):
379 r"""Replaces vars in the graph.
380
381 Args:
382 repl_dict: the map {old_var: new_var} that specifies how to replace the vars.
383 """
384 if not all([var.owner for var in repl_dict.values()]):
385 self.add_dep_oprs(*list(repl_dict.values()))
386 for var in self.all_vars:
387 if var in repl_dict:
388 repl_var = repl_dict[var]
389 if repl_var is var:
390 continue
391 for opnode in var.users:
392 # use method 'is' instead of 'in' to avoid
393 # compare VarNode use elemwise equal
394 assert any([var is _ for _ in opnode.inputs])
395 opnode.inputs = [repl_var if var is i else i for i in opnode.inputs]
396 if opnode not in repl_var.users:
397 repl_var.users.append(opnode)
398 var.users.clear()
399 self._compile()
400
401 def replace_oprs(self, repl_dict: Dict[OpNode, OpNode]):
402 r"""Replaces operators in the graph.

Callers 8

visualizeFunction · 0.45
_make_feedMethod · 0.45
test_replace_varFunction · 0.45
test_splice_networkFunction · 0.45
test_make_constFunction · 0.45
test_replace_varsFunction · 0.45
check_pygraph_dumpFunction · 0.45

Calls 6

add_dep_oprsMethod · 0.95
_compileMethod · 0.95
listFunction · 0.85
valuesMethod · 0.80
appendMethod · 0.45
clearMethod · 0.45

Tested by 6

test_replace_varFunction · 0.36
test_splice_networkFunction · 0.36
test_make_constFunction · 0.36
test_replace_varsFunction · 0.36
check_pygraph_dumpFunction · 0.36