Erases a ``Node`` from the ``Graph``. Throws an exception if there are still users of that node in the ``Graph``. Args: to_erase (Node): The ``Node`` to erase from the ``Graph``.
(self, to_erase : Node)
| 923 | |
| 924 | @compatibility(is_backward_compatible=True) |
| 925 | def erase_node(self, to_erase : Node) -> None: |
| 926 | """ |
| 927 | Erases a ``Node`` from the ``Graph``. Throws an exception if |
| 928 | there are still users of that node in the ``Graph``. |
| 929 | |
| 930 | Args: |
| 931 | |
| 932 | to_erase (Node): The ``Node`` to erase from the ``Graph``. |
| 933 | """ |
| 934 | if len(to_erase.users) > 0: |
| 935 | raise RuntimeError(f'Tried to erase Node {to_erase} but it still had {len(to_erase.users)} ' |
| 936 | f'users in the graph: {to_erase.users}!') |
| 937 | if to_erase._erased: |
| 938 | warnings.warn(f"erase_node({to_erase}) on an already erased node") |
| 939 | return |
| 940 | |
| 941 | to_erase._remove_from_list() |
| 942 | to_erase._erased = True # iterators may retain handles to erased nodes |
| 943 | self._len -= 1 |
| 944 | |
| 945 | # Null out this Node's argument nodes so that the Nodes referred to |
| 946 | # can update their ``users`` accordingly |
| 947 | new_args = map_arg(to_erase.args, lambda n: None) |
| 948 | assert isinstance(new_args, tuple) |
| 949 | to_erase.args = new_args |
| 950 | new_kwargs = map_arg(to_erase.kwargs, lambda n: None) |
| 951 | assert isinstance(new_kwargs, dict) |
| 952 | to_erase.kwargs = new_kwargs |
| 953 | |
| 954 | @compatibility(is_backward_compatible=True) |
| 955 | def inserting_before(self, n: Optional[Node] = None): |