TrainingExtensions/torch/src/python/aimet_torch/meta/connectedgraph.py
def _parse_trace_graph(self,
                           trace: Union[torch.jit.TopLevelTracedModule, torch.jit.TracedModule],
                           model: torch.nn.Module,
                           ir_nodes_list: List[IrNode],
                           output_map: Dict[torch._C.TensorType, torch._C.TensorType],
                           higher_level_inputs: Union[List, None] = None,
                           inputs_map: Union[Dict, None] = None) -> List[torch._C.TensorType]:
        """
        Implements a depth-first graph extraction to obtain connectivity information in the form of an IrNodes list.
        Depth-first extraction is realized using recursion.

        :param trace: Pytorch JIT trace for model or a submodule
        :param model: Pytorch model to create connected graph from
        :param ir_nodes_list: List of IrNodes created from traversing the trace graph
        :param output_map: Dictionary mapping high recursion level outputs to lower level equivalent outputs
        :param higher_level_inputs: Corresponding inputs from a higher graph level
        :param inputs_map: Dictionary mapping low recursion level inputs to higher level equivalent inputs
        :return: the outputs of the traced module
        """
        if is_leaf_module(model):
            return self._parse_single_module_model(model, trace.graph, ir_nodes_list)
        if inputs_map is None:
            inputs_map = {}
        curr_inputs = [inp for inp in trace.graph.inputs()]
        if higher_level_inputs is not None:
            _update_inputs_map(curr_inputs, higher_level_inputs, inputs_map)

        # A map of sub-graph models and node name that requires recursive parsing
        node_name_to_subgraph_model = {}
        # modules that are being referenced within the sub-graph
        node_name_to_module = {curr_inputs[0].debugName(): model}
        for node in trace.graph.nodes():
            outputs = [output for output in node.outputs()]

            # retrieving a module reference
            if 'GetAttr' in node.kind():
                # For GetAttr lines, the output name will be referring to the module, and not the module's output(s)
                assert len(outputs) == 1
                getattr_node_info = ConnectedGraph._get_getattr_node_info(node)
                if getattr_node_info.node_input == curr_inputs[0].debugName():
                    getattr_node_info.node_input = None

                subgraph_model = ConnectedGraph._get_module_instance(node, node_name_to_module)
                if isinstance(subgraph_model, torch.Tensor):
                    continue
                if getattr_node_info.node_alias not in node_name_to_module:
                    node_name_to_module[getattr_node_info.node_alias] = subgraph_model
                else:
                    raise ValueError("duplicate model for {0} -> {1} and {2}".format(
                        getattr_node_info.node_alias, node_name_to_module[getattr_node_info.node_alias],
                        subgraph_model))
                if not is_leaf_module(subgraph_model):
                    node_name_to_subgraph_model[getattr_node_info.node_alias] = (subgraph_model, getattr_node_info)

            # invoking forward method
            elif 'CallMethod' in node.kind():
                self.parse_callmethod_node(node, trace, node_name_to_module, node_name_to_subgraph_model,
                                           ir_nodes_list, inputs_map, output_map)

            # functional operations e.g. cat, size etc
            else:
                ir_nodes_list.append(_create_functional_ir_node(node, inputs_map))

        # return output connections
        return [output for output in trace.graph.return_node().inputs()]

        Implements a depth-first graph extraction to obtain connectivity information in the form of an IrNodes list.
        Depth-first extraction is realized using recursion.

        :param trace: Pytorch JIT trace for model or a submodule
        :param model: Pytorch model to create connected graph from
        :param ir_nodes_list: List of IrNodes created from traversing the trace graph
        :param output_map: Dictionary mapping high recursion level outputs to lower level equivalent outputs
        :param higher_level_inputs: Corresponding inputs from a higher graph level
        :param inputs_map: Dictionary mapping low recursion level inputs to higher level equivalent inputs
        :return: the outputs of the traced module
        
Implements a depth-first graph extraction to obtain connectivity information in the form of an IrNodes list.
Depth-first extraction is realized using recursion.
Implements a depth - first graph extraction to obtain connectivity information in the form of an IrNodes list . Depth - first extraction is realized using recursion .
def _parse_trace_graph(self,
                           trace: Union[torch.jit.TopLevelTracedModule, torch.jit.TracedModule],
                           model: torch.nn.Module,
                           ir_nodes_list: List[IrNode],
                           output_map: Dict[torch._C.TensorType, torch._C.TensorType],
                           higher_level_inputs: Union[List, None] = None,
                           inputs_map: Union[Dict, None] = None) -> List[torch._C.TensorType]:
        if is_leaf_module(model):
            return self._parse_single_module_model(model, trace.graph, ir_nodes_list)
        if inputs_map is None:
            inputs_map = {}
        curr_inputs = [inp for inp in trace.graph.inputs()]
        if higher_level_inputs is not None:
            _update_inputs_map(curr_inputs, higher_level_inputs, inputs_map)
        node_name_to_subgraph_model = {}
        node_name_to_module = {curr_inputs[0].debugName(): model}
        for node in trace.graph.nodes():
            outputs = [output for output in node.outputs()]
            if 'GetAttr' in node.kind():
                assert len(outputs) == 1
                getattr_node_info = ConnectedGraph._get_getattr_node_info(node)
                if getattr_node_info.node_input == curr_inputs[0].debugName():
                    getattr_node_info.node_input = None
                subgraph_model = ConnectedGraph._get_module_instance(node, node_name_to_module)
                if isinstance(subgraph_model, torch.Tensor):
                    continue
                if getattr_node_info.node_alias not in node_name_to_module:
                    node_name_to_module[getattr_node_info.node_alias] = subgraph_model
                else:
                    raise ValueError("duplicate model for {0} -> {1} and {2}".format(
                        getattr_node_info.node_alias, node_name_to_module[getattr_node_info.node_alias],
                        subgraph_model))
                if not is_leaf_module(subgraph_model):
                    node_name_to_subgraph_model[getattr_node_info.node_alias] = (subgraph_model, getattr_node_info)
            elif 'CallMethod' in node.kind():
                self.parse_callmethod_node(node, trace, node_name_to_module, node_name_to_subgraph_model,
                                           ir_nodes_list, inputs_map, output_map)
            else:
                ir_nodes_list.append(_create_functional_ir_node(node, inputs_map))
        return [output for output in trace.graph.return_node().inputs()]
def _parse_trace_graph ( self , trace : Union [ torch . jit . TopLevelTracedModule , torch . jit . TracedModule ] , model : torch . nn . Module , ir_nodes_list : List [ IrNode ] , output_map : Dict [ torch . _C . TensorType , torch . _C . TensorType ] , higher_level_inputs : Union [ List , None ] = None , inputs_map : Union [ Dict , None ] = None ) -> List [ torch . _C . TensorType ] : if is_leaf_module ( model ) : return self . _parse_single_module_model ( model , trace . graph , ir_nodes_list ) if inputs_map is None : inputs_map = { } curr_inputs = [ inp for inp in trace . graph . inputs ( ) ] if higher_level_inputs is not None : _update_inputs_map ( curr_inputs , higher_level_inputs , inputs_map ) node_name_to_subgraph_model = { } node_name_to_module = { curr_inputs [ 0 ] . debugName ( ) : model } for node in trace . graph . nodes ( ) : outputs = [ output for output in node . outputs ( ) ] if 'GetAttr' in node . kind ( ) : assert len ( outputs ) == 1 getattr_node_info = ConnectedGraph . _get_getattr_node_info ( node ) if getattr_node_info . node_input == curr_inputs [ 0 ] . debugName ( ) : getattr_node_info . node_input = None subgraph_model = ConnectedGraph . _get_module_instance ( node , node_name_to_module ) if isinstance ( subgraph_model , torch . Tensor ) : continue if getattr_node_info . node_alias not in node_name_to_module : node_name_to_module [ getattr_node_info . node_alias ] = subgraph_model else : raise ValueError ( "duplicate model for {0} -> {1} and {2}" . format ( getattr_node_info . node_alias , node_name_to_module [ getattr_node_info . node_alias ] , subgraph_model ) ) if not is_leaf_module ( subgraph_model ) : node_name_to_subgraph_model [ getattr_node_info . node_alias ] = ( subgraph_model , getattr_node_info ) elif 'CallMethod' in node . kind ( ) : self . parse_callmethod_node ( node , trace , node_name_to_module , node_name_to_subgraph_model , ir_nodes_list , inputs_map , output_map ) else : ir_nodes_list . append ( _create_functional_ir_node ( node , inputs_map ) ) return [ output for output in trace . graph . return_node ( ) . inputs ( ) ]
Implements a depth-first graph extraction to obtain connectivity information in the form of an IrNodes list.
Implements a depth - first graph extraction to obtain connectivity information in the form of an IrNodes list .
"""
        Implements a depth-first graph extraction to obtain connectivity information in the form of an IrNodes list.
        Depth-first extraction is realized using recursion.

        :param trace: Pytorch JIT trace for model or a submodule
        :param model: Pytorch model to create connected graph from
        :param ir_nodes_list: List of IrNodes created from traversing the trace graph
        :param output_map: Dictionary mapping high recursion level outputs to lower level equivalent outputs
        :param higher_level_inputs: Corresponding inputs from a higher graph level
        :param inputs_map: Dictionary mapping low recursion level inputs to higher level equivalent inputs
        :return: the outputs of the traced module
        """ # A map of sub-graph models and node name that requires recursive parsing # modules that are being referenced within the sub-graph # retrieving a module reference # For GetAttr lines, the output name will be referring to the module, and not the module's output(s) # invoking forward method # functional operations e.g. cat, size etc # return output connections
the outputs of the traced module
the outputs of the traced module
self
trace
Union[torch.jit.TopLevelTracedModule, torch.jit.TracedModule]
Pytorch JIT trace for model or a submodule
Pytorch JIT trace for model or a submodule
model
torch.nn.Module
Pytorch model to create connected graph from
Pytorch model to create connected graph from
ir_nodes_list
List[IrNode]
List of IrNodes created from traversing the trace graph
List of IrNodes created from traversing the trace graph
output_map
Dict[torch._C.TensorType, torch._C.TensorType]
Dictionary mapping high recursion level outputs to lower level equivalent outputs
Dictionary mapping high recursion level outputs to lower level equivalent outputs
higher_level_inputs
Union[List, None]
Corresponding inputs from a higher graph level
Corresponding inputs from a higher graph level
inputs_map
Union[Dict, None]
Dictionary mapping low recursion level inputs to higher level equivalent inputs
Dictionary mapping low recursion level inputs to higher level equivalent inputs