U
    Kc                     @   s   d dl Z d dlmZ d dlmZmZmZ d dlmZ d dl	m
Z
mZmZmZmZmZ dgZG dd deZG d	d
 d
eZG dd deZdS )    N)Tracer)TargetNodeArgument)_FusedModule)ListCallableTupleAnyDictOptionalQuantizationTracerc                       s(   e Zd ZdZeed fddZ  ZS )Scopea/   Scope object that records the module path and the module type
    of a module. Scope is used to track the information of the module
    that contains a Node in a Graph of GraphModule. For example::

        class Sub(torch.nn.Module):
            def forward(self, x):
                # This will be a call_method Node in GraphModule,
                # scope for this would be (module_path="sub", module_type=Sub)
                return x.transpose(1, 2)

        class M(torch.nn.Module):
            def __init__(self):
                self.sub = Sub()

            def forward(self, x):
                # This will be a call_method Node as well,
                # scope for this would be (module_path="", None)
                x = x.transpose(1, 2)
                x = self.sub(x)
                return x

    )module_pathmodule_typec                    s   t    || _|| _d S N)super__init__r   r   )selfr   r   	__class__ C/tmp/pip-unpacked-wheel-gikjz4vx/torch/ao/quantization/fx/tracer.pyr   #   s    
zScope.__init__)__name__
__module____qualname____doc__strr
   r   __classcell__r   r   r   r   r      s   r   c                       s>   e Zd ZdZeejjed fddZ	dd Z
dd Z  ZS )	ScopeContextManagerz A context manager to track the Scope of Node during symbolic tracing.
    When entering a forward function of a Module, we'll update the scope information of
    the current module, and when we exit, we'll restore the previous scope information.
    )scopecurrent_modulecurrent_module_pathc                    s8   t    |j| _|j| _|| _|| j_t|| j_d S r   )r   r   r   prev_module_typer   prev_module_pathr    type)r   r    r!   r"   r   r   r   r   /   s    
zScopeContextManager.__init__c                 C   s   d S r   r   )r   r   r   r   	__enter__9   s    zScopeContextManager.__enter__c                 G   s   | j | j_| j| j_d S r   )r$   r    r   r#   r   )r   argsr   r   r   __exit__<   s    

zScopeContextManager.__exit__)r   r   r   r   r   torchnnModuler   r   r&   r(   r   r   r   r   r   r   )   s     
r   c                	       s   e Zd Zee ee d fddZejj	ee
dddZejj	edef eedf eeef ed fd	d
Zdeeeedf eeef ee ee ed fddZ  ZS )r   )skipped_module_namesskipped_module_classesc                    s2   t    || _|| _tdd | _i | _d| _d S )N T)r   r   r,   r-   r   r    node_name_to_scopeZrecord_stack_traces)r   r,   r-   r   r   r   r   B   s    
zQuantizationTracer.__init__)mmodule_qualified_namereturnc                 C   sJ   |j ds|j dr(t|tjj pH|| jkpHt|| jkpHt|t	S )Nztorch.nnztorch.ao.nn)
r   
startswith
isinstancer)   r*   Z
Sequentialr,   r%   r-   r   )r   r0   r1   r   r   r   is_leaf_moduleQ   s    z!QuantizationTracer.is_leaf_module.)r0   forwardr'   kwargsr2   c              
      sB   |  |}t| j||  t ||||W  5 Q R  S Q R X d S r   )Zpath_of_moduler   r    r   call_module)r   r0   r6   r'   r7   r1   r   r   r   r8   \   s    
zQuantizationTracer.call_moduleN)kindtargetr'   r7   name	type_exprr2   c                    s2   t  ||||||}| jj| jjf| j|j< |S r   )r   create_noder    r   r   r/   r;   )r   r9   r:   r'   r7   r;   r<   noder   r   r   r=   i   s
    	zQuantizationTracer.create_node)NN)r   r   r   r   r   r   r   r)   r*   r+   boolr5   r
   r	   r   r8   r   r   r   r   r=   r   r   r   r   r   r   A   s*    


  

)r)   Ztorch.fx._symbolic_tracer   Ztorch.fx.noder   r   r   Ztorch.nn.intrinsicr   typingr   r   r	   r
   r   r   __all__objectr   r   r   r   r   r   r   <module>   s    