def _replace_node_function(
        self,
        node: _torch.fx.Node,
        target: _Any,
        fun_args: _Tuple[_Any],
        fun_kwargs: _Dict[str, _Any],
    ):
        """
        Method which takes in an input node and replacement target function, and properly
        replaces the input node in the graph with a new node created for the
        replacement function. Preserves the node name. Intended to be a subgraph
        replacement with no global effect on the rest of the graph.
        """

        with self.graph_module.graph.inserting_after(node):
            node_name = node.name
            new_node = self.graph_module.graph.call_function(
                target, args=fun_args, kwargs=fun_kwargs
            )
            node.replace_all_uses_with(new_node)
            self.graph_module.graph.erase_node(node)
            new_node.name = node_name

        Method which takes in an input node and replacement target function, and properly
        replaces the input node in the graph with a new node created for the
        replacement function. Preserves the node name. Intended to be a subgraph
        replacement with no global effect on the rest of the graph.
        
coremltools/optimize/torch/_utils/graph_utils.py