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])
| 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. |