Ë
    f^(h¸  ã                   ó$  — 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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y)é    N)ÚAnyÚ
NamedTupleÚOptional)Úenable_python_dispatcher)Údetect_fake_mode)Úis_sparse_any)Úcompatibility)Úmap_aggregateÚNodeÚ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   ÚboolÚtupleÚintr   r   ÚdictÚstrr   © ó    úX/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/torch/fx/passes/shape_prop.pyr   r      sS   … ð �:‰:ÓØ�;‰;ÓØÓØ�#�s�(‰OÓØ˜E×/Ñ/Ñ0Ó0ð ÓØ�#�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`.
    r"   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�]‰]Œ_¸r€Fà€Má¤-°Ô"7ä×#Ñ#Ü×ÑÜ×"Ñ"ð
ˆð
 +ò 	ˆLØ×#Ñ#°,Ð#Õ?Ø ,�Ùð	ð
 ×&Ñ&€LØ €GÙØ—.‘.Ó"ˆØ$ˆ�	ÑØ”u×.Ñ.´×0JÑ0JÐKÑKØ%Ÿ~™~Ó/ˆG�GÑØ$*×$7Ñ$7Ó$9ˆG�LÒ!ØÜ×$Ñ$Ü×2Ñ2Ü×'Ñ'ð
ñ 
ð  &×:Ñ:Ó<×CÑCÓEˆG�GÑØ$*×$DÑ$DÓ$F×$MÑ$MÓ$OˆG�LÑ!Ø$×7Ñ7Ó9ˆG�F‰OäØˆu�m V¨]¸LÈ'óð 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__„   s^   ø€ Ü‰Ñ˜ÔØÐÜ(Ó*ˆIØÐ ÝCñ  7°t·{±{ÀIÓNˆDÔØ&ˆD�Nà#ˆDÔØ!ˆDŒNàŸ;™;ˆÕr#   Únr&   c                 óL  •‡
— ddl m}m} 	 | j                  �| j                  | _        	 | j
                  �V| j
                  5  t        «       5  t        ‰| �!  |«      } || j
                  j                  ||«       d d d «       d d d «       nt        ‰| �!  |«      }| j                  | _        	 dŠ
ˆ
fd„}t#        |«      }‰
r||j                   d<   | j
                  r2| j
                  j                  x}r |||«      x}	r|	|j                   d<   t%        |«      |j                   d	<   |S # 1 sw Y   Œ¯xY w# 1 sw Y   Œ£xY w# | j                  | _        w xY w# t        $ rC}t        j                  «        t        d|j                  «       › d|j                   › �«      |‚d }~ww xY w)
Nr   )Úcompute_unbacked_bindingsÚrebind_unbackedz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Úunbacked_bindingsÚtype)Ú%torch.fx.experimental.symbolic_shapesrN   rO   rF   rE   rG   r   rB   Úrun_nodeÚ	shape_envrH   Ú	ExceptionÚ	tracebackÚ	print_excÚRuntimeErrorÚformat_nodeÚmetar
   rX   )rI   rL   rN   rO   r%   ÚerU   ra   r[   Úsymbol_to_pathrT   rK   s             @€r$   rZ   zShapeProp.run_nodeœ   s•  ù€ ÷	
ð
	Ø×ÑÐ+ð #×.Ñ.�”ð/Ø—>‘>Ð-ØŸ™ñ MÔ)AÓ)Cñ MÜ!&¡Ñ!1°!Ó!4˜Ù'¨¯©×(@Ñ(@À!ÀVÔL÷M÷ Mð Mô #™WÑ-¨aÓ0�Fà"×.Ñ.�•ð ˆô	ô ˜VÐ%8Ó9ˆÙØ$(ˆA�F‰F�=Ñ!à�>Š>Ø!Ÿ^™^×5Ñ5Ð5�	Ð5Ù";¸IÀvÓ"NÐN�ÐNà.<�—‘Ð*Ñ+ä˜f›ˆ�‰ˆv‰Øˆ÷EMð Mú÷ Mð Mûð #×.Ñ.�•ûÜò 	Ü×ÑÔ!ÜØ,¨Q¯]©]«_Ð,=¸[ÈÏÉÈÐQóàðûð	ús^   ŒE ªE ÁD5Á.D)Á;D5ÂE ÂE Ä)D2	Ä.D5Ä5D>Ä:E ÅEÅE Å	F#Å >FÆF#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   rQ   r   rR   Úfrom_tensorrB   Úrun)rI   ÚargsÚtÚ	fake_argsrK   s       €r$   Ú	propagatezShapeProp.propagateÍ   sk   ø€ ð �>‰>Ð%ð öàô 2<¸A¼u¿|¹|Ô1L�—‘×*Ñ*¨1Ô-ÐRSÑSðˆIñ ð
 ˆIÜ‰w‰{˜IÐ&Ð&ùòs   ’>A!)N)
r   r   r   Ú__doc__rC   r   r   rZ   rj   Ú__classcell__)rK   s   @r$   r   r   T   s,   ø„ ñ,õ\'ð0/˜$ð / 3õ /÷b'ð 'r#   )T)r]   Útypingr   r   r   r   Útorch.fxÚtorch._dispatch.pythonr   Útorch._guardsr   Útorch._subclasses.meta_utilsr   Útorch.fx._compatibilityr	   Útorch.fx.noder
   r   Ú__all__r   rR   r>   ÚfxÚInterpreterr   r"   r#   r$   ú<module>rw      s�   ðó ß ,Ñ ,ã Û Ý ;Ý *Ý 6Ý 1ß -ð ˜[Ð
)€ñ  dÔ+ô�Zó ó ,ðð" .2ñ.Ø�L‰Lð.àó.ñb  dÔ+ôJ'�—‘×$Ñ$ó J'ó ,ñJ'r#   