MCPcopy Create free account
hub / github.com/pytorch/pytorch / replace_all_uses_with

Method replace_all_uses_with

torch/fx/node.py:527–576  ·  view source on GitHub ↗

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
                              )

Source from the content-addressed store, hash-verified

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):

Callers 15

_dedup_collectivesFunction · 0.80
_lower_subgraph_nodesFunction · 0.80
_convert_outputFunction · 0.80
_rebuild_graphFunction · 0.80
_replace_patternFunction · 0.80
insert_subgmFunction · 0.80
merge_matmulFunction · 0.80
fuseFunction · 0.80
optimize_for_inferenceFunction · 0.80
_inline_moduleFunction · 0.80

Calls 6

listFunction · 0.85
isinstanceFunction · 0.85
__update_args_kwargsMethod · 0.80
map_argFunction · 0.70
itemsMethod · 0.45
appendMethod · 0.45

Tested by 6

test_remove_usesMethod · 0.64
test_replace_usesMethod · 0.64
convertMethod · 0.64
transformMethod · 0.64