a
    h                      @   s   d dl Z d dlmZmZmZ d dlZd dlZd dlmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ d dlmZmZ d	d
gZeddG dd	 d	eZdejedddZeddG dd
 d
ejjZdS )    N)Any
NamedTupleOptional)enable_python_dispatcher)detect_fake_mode)'definitely_contiguous_for_memory_format)is_sparse_any)compatibility)map_aggregateNodeTensorMetadata	ShapePropT)Zis_backward_compatiblec                   @   s`   e Zd ZU ejed< ejed< eed< ee	df ed< e
ej ed< eed< eeef ed< d	S )
r   shapedtyperequires_grad.stridememory_formatis_quantizedqparamsN)__name__
__module____qualname__torchSize__annotations__r   booltupleintr   r   dictstrr    r    r    H/var/www/auris/lib/python3.9/site-packages/torch/fx/passes/shape_prop.pyr      s   


)resultreturnc                 C   s   | j }| j}| j}t| s"|  nd}d}|rdt| sdtjtjtjh}|D ]}t	| |drJ|} qdqJ| j
}	i }
|	r|  }||
d< |tjtjhv r|  |
d< |  |
d< n@|tjtjtjhv r|   |
d< |   |
d< |  |
d< t||||||	|
S )zB
    Extract a TensorMetadata NamedTuple describing `result`.
    r    N)r   qschemeZscaleZ
zero_pointZaxis)r   r   r   r   r   r   Zcontiguous_formatZchannels_lastZchannels_last_3dr   r   r$   Zper_tensor_affineZper_tensor_symmetricZq_scaleZq_zero_pointZper_channel_affineZ per_channel_affine_float_qparamsZper_channel_symmetricZq_per_channel_scalestolistZq_per_channel_zero_pointsZq_per_channel_axisr   )r"   Zinclude_contiguityr   r   r   r   r   Zmemory_formatsZquery_formatr   r   r$   r    r    r!   _extract_tensor_metadata(   sF    r&   c                       sB   e Zd ZdZd
 fdd	Zeed fddZ fdd	Z  Z	S )r   aE  
    Execute an FX graph Node-by-Node and
    record the shape and type of the result
    into the corresponding node.

    Example:
         In this example, we record the shape
         and data type of a module given
         an example input ``torch.randn(50, D_in)``.
         We print the name, shape and dtype of each node.

        class TwoLayerNet(torch.nn.Module):
            def __init__(self, D_in, H, D_out):
                super().__init__()
                self.linear1 = torch.nn.Linear(D_in, H)
                self.linear2 = torch.nn.Linear(H, D_out)
            def forward(self, x):
                h_relu = self.linear1(x).clamp(min=0)
                y_pred = self.linear2(h_relu)
                return y_pred
        N, D_in, H, D_out = 64, 1000, 100, 10
        x = torch.randn(N, D_in)
        y = torch.randn(N, D_out)
        model = TwoLayerNet(D_in, H, D_out)
        gm = torch.fx.symbolic_trace(model)
        sample_input = torch.randn(50, D_in)
        ShapeProp(gm).propagate(sample_input)

        for node in gm.graph.nodes:
            print(node.name, node.meta['tensor_meta'].dtype,
                node.meta['tensor_meta'].shape)

        The output of this code is:

        x torch.float32 torch.Size([50, 1000])
        linear1 torch.float32 torch.Size([50, 100])
        clamp_1 torch.float32 torch.Size([50, 100])
        linear2 torch.float32 torch.Size([50, 10])
        output torch.float32 torch.Size([50, 10])

    Args:
         module (GraphModule): The module to be executed
         fake_mode (FakeTensorMode): A fake mode for copying the gm

    Nc                    s\   t  | |d u rt }|d urDddlm} || j|| _|| _nd | _d | _| j| _d S )Nr   )deepcopy_to_fake_tensor)	super__init__r   Ztorch._dynamo.utilsr'   modulefake_module	fake_modereal_module)selfZgmr,   r'   	__class__r    r!   r)      s    zShapeProp.__init__)nr#   c           
   
      sv  ddl m}m} z| jd ur$| j| _z| jd ur| jR t , t |}|| jj	|| W d    n1 sp0    Y  W d    q1 s0    Y  nt |}W | j
| _n
| j
| _0 W nJ ty } z0t  td|  d|j |W Y d }~n
d }~0 0 d  fdd}t||} r4||jd< | jrd| jj	 }rd||| }	rd|	|jd	< t||jd
< |S )Nr   )compute_unbacked_bindingsrebind_unbackedzShapeProp error for: node=z with meta=Fc                    s    t | tjrd t| S | S d S )NT)
isinstancer   Tensorr&   )objZfound_tensorr    r!   extract_tensor_meta   s    z/ShapeProp.run_node.<locals>.extract_tensor_metaZtensor_metaZunbacked_bindingstype)Z%torch.fx.experimental.symbolic_shapesr2   r3   r+   r*   r,   r   r(   run_node	shape_envr-   	Exception	traceback	print_excRuntimeErrorZformat_nodemetar
   r9   )
r.   r1   r2   r3   r"   er8   r@   r;   Zsymbol_to_pathr/   r7   r!   r:      s<    

N


zShapeProp.run_nodec                    s.    j dur fdd|D }n|}t j| S )a  
        Run `module` via interpretation and return the result and
        record the shape and type of each node.

        Args:
            *args (Tensor): the sample input.

        Returns:
            Any: The value returned from executing the Module
        Nc                    s(   g | ] }t |tjr  j|n|qS r    )r4   r   r5   r,   Zfrom_tensor).0tr.   r    r!   
<listcomp>   s   z'ShapeProp.propagate.<locals>.<listcomp>)r,   r(   run)r.   argsZ	fake_argsr/   rD   r!   	propagate   s    

zShapeProp.propagate)N)
r   r   r   __doc__r)   r   r   r:   rH   __classcell__r    r    r/   r!   r   [   s   .1)T)r=   typingr   r   r   r   Ztorch.fxZtorch._dispatch.pythonr   Ztorch._guardsr   Ztorch._prims_commonr   Ztorch._subclasses.meta_utilsr   Ztorch.fx._compatibilityr	   Ztorch.fx.noder
   r   __all__r   r5   r&   ZfxZInterpreterr   r    r    r    r!   <module>   s&    3