Ë
    P/æj©  ã                   ó*  — 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 j0                  j2                  «      «       Zy)é    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<   y	)
r   ÚshapeÚdtypeÚrequires_grad.ÚstrideÚmemory_formatÚis_quantizedÚqparamsN)Ú__name__Ú
__module__Ú__qualname__ÚtorchÚSizeÚ__annotations__r   Úboolr   Úintr	   r   r
   Ústrr   © ó    ú]/root/repos/pocs/video-evals/.venv/lib/python3.12/site-packages/torch/fx/passes/shape_prop.pyr   r      sS   … ð �J‰JÓØ�K‰KÓØÓØ�3˜�8‰_ÓØ˜U×0Ñ0Ñ1Ó1ð ÓØ�#�s�(‰^Ôr#   ÚresultÚreturnc           	      óD  — | j                   }| j                  }| j                  }t        | «      s| j	                  «       nd}d}|rWt        | «      sLt
        j                  t
        j                  t
        j                  h}|D ]  }| j                  |¬«      sŒ|} n | j                  }	i }
|	rä| j                  «       }||
d<   |t
        j                  t
        j                  hv r'| j                  «       |
d<   | j                  «       |
d<   n†|t
        j                   t
        j"                  t
        j$                  hv rU| j'                  «       j)                  «       |
d<   | j+                  «       j)                  «       |
d<   | j-                  «       |
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„  € ð �L‰L€EØ�L‰L€EØ×(Ñ(€MÜ$1°&Ô$9ˆV�]‰]Œ_¸t€Fà€Má¤-°Ô"7ä×#Ñ#Ü×ÑÜ×"Ñ"ð
ˆð
 +ò 	ˆLØ×#Ñ#°,Ð#Õ?Ø ,�Ùð	ð
 ×&Ñ&€LØ €GÙØ—.‘.Ó"ˆØ$ˆ�	ÑØ”u×.Ñ.´×0JÑ0JÐKÑKØ%Ÿ~™~Ó/ˆG�GÑØ$*×$7Ñ$7Ó$9ˆG�LÒ!Øœ×1Ñ1´5×3YÑ3YÔ[`×[vÑ[vÐwÑwð  &×:Ñ:Ó<×CÑCÓEˆG�GÑØ$*×$DÑ$DÓ$F×$MÑ$MÓ$OˆG�LÑ!Ø$×7Ñ7Ó9ˆG�F‰OäØˆu�m V¨]¸LÈ'ó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

    c                 óÊ   •— t         ‰| �  |«       |€
t        «       }|�&ddlm}  || j
                  |«      | _        || _        nd | _        d | _        | j
                  | _        y )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ñ  7°t·{±{ÀIÓNˆDÔØ&ˆD�Nà#ˆDÔØ!ˆDŒNàŸ;™;ˆÕr#   Únr&   c                 ó„  •‡— 	 | j                   �| j                   | _        	 | j                  �8| j                  5  t        «       5  t        ‰| �  |«      }d d d «       d d d «       nt        ‰| �  |«      }| j                  | _        	 dŠˆfd„}t        |«      }‰r||j                  d<   t        |«      |j                  d<   |S # 1 sw Y   ŒqxY w# 1 sw Y   ŒexY w# | j                  | _        w xY w# t        $ rC}t        j                  «        t        d|j                  «       › d|j                  › �«      |‚d }~ww xY w)NzShapeProp error for: node=z with meta=Fc                 óV   •— 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à�
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Ô)AÓ)Cñ 5Ü!&¡Ñ!1°!Ó!4˜÷5÷ 5ð 5ô #™WÑ-¨aÓ0�Fà"×.Ñ.�•ð ˆô	ô ˜VÐ%8Ó9ˆÙØ$(ˆA�F‰F�=Ñ!ä˜f›ˆ�‰ˆv‰Øˆ÷95ð 5ú÷ 5ð 5ûð
 #×.Ñ.�•ûÜò 	Ü×ÑÔ!ÜØ,¨Q¯]©]«_Ð,=ð >ØŸ™�xð!óð ðûð	ús]   „C3 ¢C ºCÁCÁCÁC Á5C3 ÃC	Ã
CÃCÃC ÃC0Ã0C3 Ã3	D?Ã<>D:Ä:D?c                 óÌ   •— | j                   �E|D �cg c]9  }t        |t        j                  «      r| j                   j	                  |«      n|‘Œ; }}n|}t        ‰| �  |Ž S c c}w )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
        )rG   rO   r   rP   Úfrom_tensorrB   Úrun)rI   ÚargsÚtÚ	fake_argsrK   s       €r$   Ú	propagatezShapeProp.propagateµ   s_   ø€ ð �>‰>Ð%ØfjÖkÐab¼*ÀQÌÏÉÔ:U˜Ÿ™×3Ñ3°AÔ6Ð[\Ñ\ÐkˆIÑkàˆIÜ‰w‰{˜IÐ&Ð&ùò ls   ’>A!)N)
r   r   r   Ú__doc__rC   r   r   rV   rd   Ú__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>rq      s–   ðó Û Û å ;ß -ß 9Õ 9Ý 1Ý *Ý 6à˜[Ð
)€á dÔ+ô�Zó ó ,ðñ'S e§l¡lð 'SÐP^ó 'SñR  dÔ+ôz'�—‘×$Ñ$ó z'ó ,ñz'r#   