ó
    Eñi†   ã                   ó(  • S SK r S SKJrJrJr  S SKrS SKrS SKJr  S SK	J
r
  S SKJr  S SKJr  S SKJr  S SKJrJr  S	S
/r\" SS9 " S S	\5      5       r SS\R,                  S\4S jjr\" SS9 " S S
\R0                  R2                  5      5       rg)é    N)ÚAnyÚ
NamedTupleÚOptional)Úenable_python_dispatcher)Údetect_fake_mode)Ú(is_contiguous_for_memory_format_or_false)Úis_sparse_any)Úcompatibility)Úmap_aggregateÚNodeÚTensorMetadataÚ	ShapePropT)Úis_backward_compatiblec                   ó²   • \ rS rSr% \R
                  \S'   \R                  \S'   \\S'   \	\
S4   \S'   \\R                     \S'   \\S'   \\\4   \S	'   S
rg)r   é   ÚshapeÚdtypeÚrequires_grad.ÚstrideÚmemory_formatÚis_quantizedÚqparams© N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__ÚtorchÚSizeÚ__annotations__r   ÚboolÚtupleÚintr   r   ÚdictÚstrr   Ú__static_attributes__r   ó    ÚW/home/mande/repo/quber/.venv/lib/python3.13/site-packages/torch/fx/passes/shape_prop.pyr   r      sS   ‡ ð �:‰:ÓØ�;‰;ÓØÓØ�#�s�(‰OÓØ˜E×/Ñ/Ñ0Ó0ð ÓØ�#�s�(‰^Ör'   ÚresultÚreturnc           	      óh  • U R                   nU R                  nU R                  n[        U 5      (       d  U R	                  5       OSnSnU(       aZ  [        U 5      (       dJ  [
        R                  [
        R                  [
        R                  4nU H  n[        XS9(       d  M  Un  O   U R                  n	0 n
U	(       aç  U R                  5       nXºS'   U[
        R                  [
        R                  4;   a'  U R                  5       U
S'   U R                  5       U
S'   OˆU[
        R                   [
        R"                  [
        R$                  4;   aU  U R'                  5       R)                  5       U
S'   U R+                  5       R)                  5       U
S'   U R-                  5       U
S'   [/        X#XEXiU
5      $ )z:
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_3dr   r   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_metadatarA   (   s‚  € ð �L‰L€EØ�L‰L€EØ×(Ñ(€MÜ$1°&×$9Ñ$9ˆV�]‰]Œ_¸r€Fà€Mæ¤-°×"7Ñ"7ä×#Ñ#Ü×ÑÜ×"Ñ"ð
ˆó
 +ˆLÜ7Ø÷ñ ð !-�Ùñ +ð ×&Ñ&€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äØ�m¨]È'óð r'   c                   óT   ^ • \ rS rSrSrS	U 4S jjrS\S\4U 4S jjrU 4S jr	Sr
U =r$ )
r   é[   a±  
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U ]  U5        Uc
  [        5       nUb%  SSKJn  U" U R
                  U5      U l        X l        OS U l        S U l        U R
                  U l        g )Nr   )Údeepcopy_to_fake_tensor)	ÚsuperÚ__init__r   Útorch._dynamo.utilsrE   ÚmoduleÚfake_moduleÚ	fake_modeÚreal_module)ÚselfÚgmrK   rE   Ú	__class__s       €r(   rG   ÚShapeProp.__init__‹   s\   ø€ Ü‰Ñ˜ÔØÑÜ(Ó*ˆIØÑ ÝCñ  7°t·{±{ÀIÓNˆDÔØ&�Nà#ˆDÔØ!ˆDŒNàŸ;™;ˆÕr'   Únr*   c                 óˆ  >^
• SSK JnJn   U R                  b  U R                  U l         U R
                  bU  U R
                     [        5          [        TU ]!  U5      nU" U R
                  R                  X5        S S S 5        S S S 5        O[        TU ]!  U5      nU R                  U l        Sm
U
4S jn[#        WU5      nT
(       a  XqR                   S'   U R
                  (       a:  U R
                  R                  =n(       a  U" X„5      =n	(       a  X‘R                   S'   [%        U5      UR                   S	'   U$ ! , (       d  f       NÅ= f! , (       d  f       N¾= f! U R                  U l        f = f! [         aD  n[        R                  " 5         [        SUR                  5        SUR                    35      UeS nAff = f)
Nr   )Úcompute_unbacked_bindingsÚrebind_unbackedzShapeProp error for: node=z with meta=Fc                 ó`   >• [        U [        R                  5      (       a  Sm[        U 5      $ U $ )NT)Ú
isinstancer   ÚTensorrA   )ÚobjÚfound_tensors    €r(   Úextract_tensor_metaÚ/ShapeProp.run_node.<locals>.extract_tensor_meta¿   s)   ø€ Ü˜#œuŸ|™|×,Ñ,à#�Ü/°Ó4Ð4à�
r'   Útensor_metaÚunbacked_bindingsÚtype)Ú%torch.fx.experimental.symbolic_shapesrS   rT   rJ   rI   rK   r   rF   Úrun_nodeÚ	shape_envrL   Ú	ExceptionÚ	tracebackÚ	print_excÚRuntimeErrorÚformat_nodeÚmetar   r^   )rM   rQ   rS   rT   r)   ÚerZ   rg   ra   Úsymbol_to_pathrY   rO   s             @€r(   r`   ÚShapeProp.run_node£   sn  ù€ ÷	
ð
	Ø×ÑÑ+ð #×.Ñ.�”ð/Ø—>‘>Ñ-ØŸ›Ô)AÕ)CÜ!&¡Ñ!1°!Ó!4˜Ù'¨¯©×(@Ñ(@À!ÔL÷ *DŸ˜ô #™WÑ-¨aÓ0�Fà"×.Ñ.�”ð ˆõ	ô ˜VÐ%8Ó9ˆÞØ$(�F‰F�=Ñ!à�>�>Ø!Ÿ^™^×5Ñ5Ð5�	Õ5Ù";¸IÓ"NÐN�ÕNà.<—‘Ð*Ñ+ä˜f›ˆ�‰ˆv‰Øˆ÷E *DÕ)CúŸ�ûð #×.Ñ.�•ûÜó 	Ü×ÒÔ!ÜØ,¨Q¯]©]«_Ð,=¸[ÈÏÉÈÐQóàðûð	ús^   ŒE3 «E ÁEÁ-D;Á<EÂE ÂE3 Ä;
E		ÅEÅ
EÅE ÅE0Å0E3 Å3
GÅ=?F<Æ<Gc                 óà   >• U R                   bM  U Vs/ s H?  n[        U[        R                  5      (       a  U R                   R	                  U5      OUPMA     nnOUn[
        TU ]  " U6 $ s  snf )zÐ
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
)rK   rV   r   rW   Úfrom_tensorrF   Úrun)rM   ÚargsÚtÚ	fake_argsrO   s       €r(   Ú	propagateÚShapeProp.propagateÔ   ss   ø€ ð �>‰>Ñ%ñ óâ�Aô 2<¸A¼u¿|¹|×1LÑ1L�—‘×*Ñ*¨1Ô-ÐRSÒSÙð ð ˆIð
 ˆIÜ‰wŠ{˜IÐ&Ð&ùòs   “AA+)rK   rJ   rI   rL   )N)r   r   r   r   Ú__doc__rG   r   r   r`   rq   r&   Ú__classcell__)rO   s   @r(   r   r   [   s,   ø† ñ,÷\'ð0/˜$ð / 3÷ /÷b'ó 'r'   )T)rc   Útypingr   r   r   r   Útorch.fxÚtorch._dispatch.pythonr   Útorch._guardsr   Útorch._prims_commonr   Útorch._subclasses.meta_utilsr	   Útorch.fx._compatibilityr
   Útorch.fx.noder   r   Ú__all__r   rW   rA   ÚfxÚInterpreterr   r   r'   r(   Ú<module>r€      s    ðó ß ,Ñ ,ã Û Ý ;Ý *Ý HÝ 6Ý 1ß -ð ˜[Ð
)€ñ  dÑ+ô�Zó ó ,ðð* .2ñ0Ø�L‰Lð0àõ0ñf  dÑ+ôJ'�—‘×$Ñ$ó J'ó ,ñJ'r'   