
    קg                     2   d dl Z d dlZ d dlZd dlmZ d dlmZmZ d dlm	Z	m
Z
mZmZmZ d dlmZ d dlmZ d dlmZ dd	gZ ed
           G d de                      Zdde j        defdZ ed
           G d d	e j        j                              ZdS )    N)enable_python_dispatcher)Nodemap_aggregate)AnyTuple
NamedTupleOptionalDict)compatibility)detect_fake_mode)is_sparse_anyTensorMetadata	ShapePropT)is_backward_compatiblec                       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   boolr   intr	   r   r
   strr        V/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/fx/passes/shape_prop.pyr   r      s          JK38_U01111 #s(^r#   resultreturnc           	      @   | j         }| j        }| j        }t          |           s|                                 nd}d}|rQt          |           sBt
          j        t
          j        t
          j        h}|D ]}| 	                    |          r|} n| j
        }	i }
|	r|                                 }||
d<   |t
          j        t
          j        hv r/|                                 |
d<   |                                 |
d<   n|t
          j        t
          j        t
          j        hv ri|                                                                 |
d<   |                                                                 |
d<   |                                 |
d<   t/          ||||||	|
          S )zB
    Extract a TensorMetadata NamedTuple describing `result`.
    N)r   qschemescale
zero_pointaxis)r   r   r   r   r   r   contiguous_formatchannels_lastchannels_last_3dis_contiguousr   r(   per_tensor_affineper_tensor_symmetricq_scaleq_zero_pointper_channel_affine per_channel_affine_float_qparamsper_channel_symmetricq_per_channel_scalestolistq_per_channel_zero_pointsq_per_channel_axisr   )r%   include_contiguityr   r   r   r   r   memory_formatsquery_formatr   r   r(   s               r$   _extract_tensor_metadatar>       s    LELE(M$1&$9$9CV]]___tFM 	-"7"7 	#"

 + 	 	L##,#??  , &L G :..""$	u.0JKKK%~~//GG$*$7$7$9$9GL!!153Y[`[vwww  &::<<CCEEGG$*$D$D$F$F$M$M$O$OGL!$7799GFOumV]L'S S Sr#   c                   B     e Zd ZdZd fd	Zdedef fdZ fdZ x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                     t                                          |           |t                      }|$ddlm}  || j        |          | _        || _        nd | _        d | _        | j        | _        d S )Nr   )deepcopy_to_fake_tensor)	super__init__r   torch._dynamo.utilsrA   modulefake_module	fake_modereal_module)selfgmrG   rA   	__class__s       r$   rC   zShapeProp.__init__x   s    (**I CCCCCC  76t{INND&DNN#D!DN;r#   nr&   c                    	 | j         | j         | _        	 | j        g| j        5  t                      5  t	                                          |          }d d d            n# 1 swxY w Y   d d d            n# 1 swxY w Y   n!t	                                          |          }| j        | _        n# | j        | _        w xY wnR# t          $ rE}t          j	                     t          d|                                 d|j                   |d }~ww xY wdfd}t          ||          }r
||j        d<   t          |          |j        d<   |S )NzShapeProp error for: node=z with meta=Fc                 ^    t          | t          j                  rdt          |           S | S )NT)
isinstancer   Tensorr>   )objfound_tensors    r$   extract_tensor_metaz/ShapeProp.run_node.<locals>.extract_tensor_meta   s/    #u|,, #/444
r#   tensor_metatype)rF   rE   rG   r   rB   run_noderH   	Exception	traceback	print_excRuntimeErrorformat_nodemetar   rU   )rI   rL   r%   erS   r\   rR   rK   s         @r$   rV   zShapeProp.run_node   s   	+ #./>- 5 5)A)C)C 5 5!&!1!1!!4!45 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 #WW--a00F".d..... 	 	 	!!!!Q]]__ ! !! !  	 	 	 	 	 	 V%899 	)$(AF=!fvsu   C B4 A:"A#A:#A'	'A:*A'	+A:.B4 :A>>B4 A>%B4 'C 4CC 
DA DDc                 b      j          f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                 |    g | ]8}t          |t          j                  rj                            |          n|9S r"   )rO   r   rP   rG   from_tensor).0trI   s     r$   
<listcomp>z'ShapeProp.propagate.<locals>.<listcomp>   sA    kkkab*Q:U:U\33A666[\kkkr#   )rG   rB   run)rI   args	fake_argsrK   s   `  r$   	propagatezShapeProp.propagate   sB     >%kkkkfjkkkIIIuww{I&&r#   )N)
r   r   r   __doc__rC   r   r   rV   rg   __classcell__)rK   s   @r$   r   r   I   s        , ,Z' ' ' ' ' '.$4 $C $ $ $ $ $ $L' ' ' ' ' ' ' ' 'r#   )T)r   torch.fxrX   torch._dispatch.pythonr   torch.fx.noder   r   typingr   r   r   r	   r
   torch.fx._compatibilityr   torch._guardsr   torch._subclasses.meta_utilsr   __all__r   rP   r>   fxInterpreterr   r"   r#   r$   <module>rt      s         ; ; ; ; ; ; - - - - - - - - 9 9 9 9 9 9 9 9 9 9 9 9 9 9 1 1 1 1 1 1 * * * * * * 6 6 6 6 6 6[
)d+++    Z   ,+'S 'Sel 'SP^ 'S 'S 'S 'SR d+++z' z' z' z' z'$ z' z' ,+z' z' z'r#   