Replace all uses of ``self`` in the Graph with the Node ``replace_with``. Args: replace_with (Node): The node to replace all uses of ``self`` with. delete_user_cb (Callable): Callback that is called to determine whether a given user of the sel
(self,
replace_with : 'Node',
delete_user_cb: Callable[['Node'], bool] = lambda user: True,
*,
propagate_meta=False
)
| 525 | |
| 526 | @compatibility(is_backward_compatible=True) |
| 527 | def replace_all_uses_with(self, |
| 528 | replace_with : 'Node', |
| 529 | delete_user_cb: Callable[['Node'], bool] = lambda user: True, |
| 530 | *, |
| 531 | propagate_meta=False |
| 532 | ) -> List['Node']: |
| 533 | """ |
| 534 | Replace all uses of ``self`` in the Graph with the Node ``replace_with``. |
| 535 | |
| 536 | Args: |
| 537 | |
| 538 | replace_with (Node): The node to replace all uses of ``self`` with. |
| 539 | delete_user_cb (Callable): Callback that is called to determine |
| 540 | whether a given user of the self node should be removed. |
| 541 | propagate_meta (bool): Whether or not to copy all properties |
| 542 | on the .meta field of the original node onto the replacement node. |
| 543 | For safety, this is only valid to do if the replacement node |
| 544 | doesn't already have an existing .meta field. |
| 545 | |
| 546 | Returns: |
| 547 | |
| 548 | The list of Nodes on which this change was made. |
| 549 | """ |
| 550 | if propagate_meta: |
| 551 | assert len(replace_with.meta) == 0, \ |
| 552 | 'Called node.replace_all_uses_with(replace_with, propagate_meta=True), ' \ |
| 553 | 'but replace_with already has .meta keys' |
| 554 | for k, v in self.meta.items(): |
| 555 | replace_with.meta[k] = v |
| 556 | to_process = list(self.users) |
| 557 | skipped = [] |
| 558 | for use_node in to_process: |
| 559 | if not delete_user_cb(use_node): |
| 560 | skipped.append(use_node) |
| 561 | continue |
| 562 | |
| 563 | def maybe_replace_node(n : Node) -> Node: |
| 564 | if n == self: |
| 565 | return replace_with |
| 566 | else: |
| 567 | return n |
| 568 | |
| 569 | new_args = map_arg(use_node.args, maybe_replace_node) |
| 570 | new_kwargs = map_arg(use_node.kwargs, maybe_replace_node) |
| 571 | assert isinstance(new_args, tuple) |
| 572 | assert isinstance(new_kwargs, dict) |
| 573 | use_node.__update_args_kwargs(new_args, new_kwargs) |
| 574 | |
| 575 | assert len(self.users) - len(skipped) == 0 |
| 576 | return [n for n in to_process if n not in skipped] |
| 577 | |
| 578 | @compatibility(is_backward_compatible=False) |
| 579 | def is_impure(self): |