Ë
    ÄÓ^jk+  ã                   ó0  — 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 d dlmZ d dlmZ  ej$                  e«      Zej+                  ej,                  «       g d	¢Z ed
¬«      dede	f   dededz  f   fd„«       Zdeede	f   ede	f   gef   deede	f      ddfd„Zdeede	f      deede	f      deede	f      fd„Z ed
¬«      dede	f   dede	f   deede	f   ede	f   gef   fd„«       Z ed
¬«       G d„ d«      «       Zy)é    N)ÚCallable)Úwraps)ÚQueue)ÚAny)Úcompatibility)ÚGraphModule)Ú
PassResult)Úpass_result_wrapperÚ this_before_that_pass_constraintÚPassManagerF)Úis_backward_compatibleÚfn.Úreturnc                 óÆ   ‡ — ‰ €yt        ‰ «      dt        j                  dt        dz  fˆ fd„«       }t	        j
                  ‰ «      st        ‰ «      j                  |_        |S )a+  
    Wrapper for passes which currently do not return a PassResult.
    This wrapper makes them return a PassResult containing the modified object
    and True for the "modified" flag.

    Args:
        fn (Callable[Module, Any])

    Returns:
        wrapped_fn (Callable[Module, PassResult])
    NÚgmr   c                 ó¢   •—  ‰| «      }|€t        | d«      S t        |t         «      r|S t        |t        j                  «      rt        |d«      S y )NT)r	   Ú
isinstanceÚnnÚModule)r   Úresr   s     €úm/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/fx/passes/infra/pass_manager.pyÚ
wrapped_fnz'pass_result_wrapper.<locals>.wrapped_fn%   sM   ø€ á�‹fˆØˆ;Ü˜b $Ó'Ð'Ü�cœ:Ô&ØˆJÜ˜œRŸY™YÔ'Ü˜c 4Ó(Ð(ð (ó    )r   r   r   r	   ÚinspectÚ
isfunctionÚtypeÚ__name__)r   r   s   ` r   r
   r
      sb   ø€ ð 
€zàä
ˆ2ƒYð)”r—y‘yð )¤Z°$Ñ%6ô )ó ð)ô ×Ñ˜bÔ!Ü" 2›h×/Ñ/ˆ
ÔàÐr   Ú
constraintÚpassesc                 óª   — t        |«      D ]E  \  }}t        ||dz   d  «      D ],  \  }} | ||«      rŒt        d|› d|› d|› d|› d|› d|› d�«      ‚ ŒG y )Né   z,pass schedule constraint violated. Expected z before z but found z
 at index z and z in pass list.)Ú	enumerateÚRuntimeError)r   r   ÚiÚaÚjÚbs         r   Ú"_validate_pass_schedule_constraintr(   5   s‰   € ô ˜&Ó!ò ‰ˆˆ1Ü˜f Q¨¡U W˜oÓ.ò 	‰DˆAˆqÙ˜!˜QÔØÜØ>¸q¸cÀÈ!ÈØ˜a˜S 
¨1¨#¨U°1°#°ZÀ¸sð Cðóð ñ	ñr   Úconstraintsc                 ó  ‡— t        |«      dk(  r| S | D �ci c]  }|g “Œ }}t        j                  | d«      Št        «       }| D ][  }| D ]:  }||k(  rŒ	|D ]-  } |||«      rŒ||   j	                  |«       ‰|xx   dz  cc<   Œ/ Œ< ‰|   dk(  sŒK|j                  |«       Œ] t        j                  | d«      }g }	|j                  «       sn|j                  «       }|	j	                  |«       d||<   ||   D ]/  }
||
   rŒ	‰|
xx   dz  cc<   ‰|
   dk(  sŒ|j                  |
«       Œ1 |j                  «       sŒnt        t        ˆfd„‰j                  «       «      «      }t        |«      dk7  rd|› �}t        |«      ‚|	S c c}w )zÝ
    Args
        passes: Passes that we are ordering
        constraints: Constraints applied on these passes

    Returns
        A sorted list of callables and a boolean of if a circular dependency
        existed
    r   r!   FTc                 ó   •— ‰|    dk7  S )Nr   © )ÚpÚindegree_maps    €r   ú<lambda>z*_topological_sort_passes.<locals>.<lambda>s   s   ø€ ¨°a©¸AÑ)=€ r   z:Circular dependency detected within the following passes: )ÚlenÚdictÚfromkeysr   ÚappendÚputÚemptyÚgetÚlistÚfilterÚkeysr#   )r   r)   r-   ÚgraphÚ
candidatesr%   r'   r   ÚvisitedÚsorted_passesÚnÚcycle_passesÚerrorr.   s                @r   Ú_topological_sort_passesrA   D   sª  ø€ ô ˆ;Ó˜1ÒØˆð QWÖ@WÈ1ÀÀBÁÐ@W€EÐ@WÜ26·-±-ÀÈÓ2J€LÜ,1«G€JØò ˆØò 	)ˆAØ�AŠvØà)ò )�
Ù! ! QÕ'Ø˜!‘H—O‘O AÔ&Ø  “O qÑ(”Oñ)ð		)ð ˜‰?˜aÓØ�N‰N˜1Õðô /3¯m©m¸FÀEÓ.J€GØ.0€Mà×ÑÔ Ø�N‰NÓˆØ×Ñ˜QÔØˆ�‰
à�q‘ò 	&ˆAØ˜1“:Ø˜Q“ 1Ñ$“Ø ‘? aÓ'Ø—N‘N 1Õ%ð		&ð ×ÑÕ ô œÓ=¸|×?PÑ?PÓ?RÓSÓT€LÜ
ˆ<Ó˜AÒàHÈÈÐWð 	ô ˜5Ó!Ð!àÐùòM AXs   –
E>ÚthisÚthatc                 ó`   ‡ ‡— dt         dt        f   dt         dt        f   dt        fˆˆ fd„}|S )aã  
    Defines a partial order ('depends on' function) where ``this`` must occur
    before ``that``.

    For example, the following pass list and constraint list would be invalid::

        passes = [pass_b, pass_a]

        constraints = [this_before_that_pass_constraint(pass_a, pass_b)]

    Args:
        this (Callable): pass which should occur first
        that (Callable): pass which should occur later

    Returns:
        depends_on (Callable[[Object, Object], bool])
    r%   .r'   r   c                 ó   •— | ‰k7  xs |‰k7  S ©Nr,   )r%   r'   rC   rB   s     €€r   Ú
depends_onz4this_before_that_pass_constraint.<locals>.depends_on“   s   ø€ Ø�D‰yÒ%˜A ™IÐ%r   )r   r   Úbool)rB   rC   rG   s   `` r   r   r   }   s7   ù€ ð,&”h˜s¤C˜xÑ(ð &¬X°c¼3°hÑ-?ð &ÄDö &ð Ðr   c                   óÖ  — e Zd ZU dZeededz  f      ed<   eeedef   edef   ge	f      ed<   dZ
e	ed<   dZeed	<   	 	 	 	 	 ddeededz  f      dz  deeedef   edef   ge	f      dz  d	edz  d
e	de	ddfd„Zdededz  f   ddfd„Zdeedef   edef   ge	f   ddfd„Zdd„Zdd„Zdeej&                  gdf   ddfd„Zdej&                  ddfd„Zdej&                  defd„Zy)r   ad  
    Construct a PassManager.

    Collects passes and constraints. This defines the pass schedule, manages
    pass constraints and pass execution.

    Args:
        passes (Optional[List[Callable]]): List of passes. A pass is a
            callable which modifies an object and returns a PassResult
        constraint (Optional[List[Callable]]): List of constraints. A
            constraint is a callable which takes two passes (A, B) and returns
            True if A depends on B and False otherwise. See implementation of
            `this_before_that_pass_constraint` for example.
        steps (int): Max number of times we run the passes (default = 1).
        run_checks_after_each_pass (bool): Whether to run checks and linting
            after each pass
        suppress_check_failures (bool): Whether to raise errors when running
            checks
    .Nr   r)   FÚ
_validatedr!   ÚstepsÚrun_checks_after_each_passÚsuppress_check_failuresr   c                 ó^   — |xs g | _         |xs g | _        |r|| _        || _        || _        y rF   )r   r)   rK   rL   rM   )Úselfr   r)   rK   rL   rM   s         r   Ú__init__zPassManager.__init__´   s6   € ð ’l ˆŒØ&Ò,¨"ˆÔÙØˆDŒJà*DˆÔ'Ø'>ˆÕ$r   Ú_passc                 óH   — | j                   j                  |«       d| _        y)z>
        Adds a pass into the current list of passes.
        FN)r   r3   rJ   )rO   rQ   s     r   Úadd_passzPassManager.add_passÅ   s   € ð 	�‰×Ñ˜5Ô!Øˆ�r   r   c                 óH   — | j                   j                  |«       d| _        y)zI
        Adds a constraint into the current list of constraints.
        FN)r)   r3   rJ   ©rO   r   s     r   Úadd_constraintzPassManager.add_constraintÌ   s   € ð 	×Ñ×Ñ 
Ô+Øˆ�r   c                 óz   — | j                   ry| j                  D ]  }t        || j                  «       Œ d| _         y)z“
        Validates that current pass schedule defined by `self.passes` is valid
        according to all constraints in `self.constraints`
        NT)rJ   r)   r(   r   rU   s     r   Úvalidate_constraintsz PassManager.validate_constraintsÕ   s:   € ð
 �?Š?ØØ×*Ñ*ò 	HˆJÜ.¨z¸4¿;¹;ÕGð	Hàˆ�r   c                 ó\   — t        | j                  | j                  «      | _        d| _        y)ab  
        Finds a valid traversal order based on the given constraints and orders
        the passes based on this order.

        If a circular dependency exists between the constraints and steps = 1,
        then we will raise an error because if steps != 1 this means that we
        will re-run the passes, allowing for circular dependencies.
        TN)rA   r   r)   rJ   )rO   s    r   Úsolve_constraintszPassManager.solve_constraintsà   s#   € ô /¨t¯{©{¸D×<LÑ<LÓMˆŒØˆ�r   Úcheckc                 ó¼   — t        j                  |«      }t        t        |j                  j                  «       «      «      dk7  rt        d«      ‚t        | d|«       y)zÏ
        Adds a function which takes runs various checks on a given graph module.
        This function is run before and after each pass if the
        `run_checks_after_each_pass` flag is enabled.
        r!   zEPassManager check function should only take in one variable, a moduler[   N)r   Ú	signaturer0   r7   Ú
parametersÚvaluesÚ	TypeErrorÚsetattr)rO   r[   Úsigs      r   Ú
add_checkszPassManager.add_checksì   sQ   € ô ×Ñ Ó&ˆäŒt�C—N‘N×)Ñ)Ó+Ó,Ó-°Ò2ÜØWóð ô 	��g˜uÕ%r   Úmodulec                  ó   — y rF   r,   )rO   rd   s     r   r[   zPassManager.checkû   s   € Ør   c           	      óö  — | j                   s| j                  «        | j                  |«       d}t        | j                  «      D �]+  }d}t        | j                  «      D �]  \  }}t        j                  |«      r|j                  nt        |«      j                  }t        j                  d|«       	  ||«      }t        |t        «      st        |d«      st!        d|› d�dz   «      ‚|j"                  }|xs |j$                  }t        |t&        «      r1t        j                  d||j(                  «       |j+                  «        | j,                  r| j                  |«       �Œ |xs |}|r�Œ, n t        ||«      S # t.        $ ro}	| j                  d| D �
cg c]8  }
t        j                  |
«      r|
j                  nt        |
«      j                  ‘Œ: nc c}
w }}
d	|› d
|› �}t/        |«      |	‚d}	~	ww xY w)a}  
        Runs a list of passes in the order based on `self.passes` on the given
        graph module. Each time a pass is run, checks and linting will be run on
        the graph module if `run_checks_after_each_pass` is set.

        If the module is a graph module, we will run the list of passes until
        the graph stops changing, or until `steps` number of times.
        FzRunning pass '%s'Úgraph_modulezThe result of the pass z should be type PassResult.z)Please wrap it with pass_result_wrapper()zGraph after pass '%s': %sNz$An error occurred when running the 'z#' pass after the following passes: )rJ   rZ   r[   ÚrangerK   r"   r   r   r   r   r   ÚloggerÚdebugr   r	   Úhasattrr`   rg   Úmodifiedr   r:   Ú	recompilerL   Ú	Exception)rO   rd   Úoverall_modifiedÚ_rl   r$   r   Úfn_namer   Úer-   Úprev_pass_namesÚmsgs                r   Ú__call__zPassManager.__call__þ   së  € ð �ŠØ×"Ñ"Ô$ð 	�
‰
�6Ôð !ÐÜ�t—z‘zÓ"ó *	ˆAØˆHô # 4§;¡;Ó/ó !0‘��2Ü)0×);Ñ);¸BÔ)?˜"Ÿ+š+ÄTÈ"ÃX×EVÑEV�Ü—‘Ð0°'Ô:ð0Ù˜V›*�Cä% c¬:Ô6¼wØ˜^ô@ô (Ø5°g°YÐ>YÐZØIñJóð ð
 !×-Ñ-�Fà'Ò7¨3¯<©<�Hä! &¬+Ô6ÜŸ™Ð%@À'È6Ï<É<ÔXØ×(Ñ(Ô*ð ×6Ò6ØŸ
™
 6Ô*ùð3!0ðH  0Ò;°8ÐÛÙðU*	ôX ˜&Ð"2Ó3Ð3øô !ò 0ð "&§¡¨R¨a ö'àô '.×&8Ñ&8¸Ô&;˜Ÿ
š
ÄÀaÃ×AQÑAQÑQñ'ùò '�Oð 'ð AÀÀ	ÐIlÐm|Ðl}Ð~�CÜ# C›.¨aÐ/ûð0ús%   Â4B0F Æ 	G8Æ	G3Æ=G
ÇG3Ç3G8)NNNFF)r   N)r   Ú
__module__Ú__qualname__Ú__doc__r7   r   r	   Ú__annotations__r   rH   rJ   rK   ÚintrP   rS   rV   rX   rZ   r   r   rc   r[   ru   r,   r   r   r   r   ™   s²  … ñð( �˜#˜z¨DÑ0Ð0Ñ1Ñ2Ó2Ø�h ¨¨c¨Ñ 2°H¸SÀ#¸XÑ4FÐGÈÐMÑNÑOÓOØ€J�ÓØ€Eˆ3ƒNð AEàØ Ø+0Ø(-ñ?à�X˜c :°Ñ#4Ð4Ñ5Ñ6¸Ñ=ð?ð ˜( H¨S°#¨XÑ$6¸ÀÀcÀÑ8JÐ#KÈTÐ#QÑRÑSØ
ñð?ð
 �T‰zð?ð %)ð?ð "&ð?ð 
ó?ð" ˜h s¨J¸Ñ,=Ð'=Ñ>ð  À4ó  ð Ø" H¨S°#¨XÑ$6¸ÀÀcÀÑ8JÐ#KÈTÐ#QÑRð à	ó ó	ó
ð& ¨"¯)©)¨°dÐ):Ñ ;ð &Àó &ð˜BŸI™Ið ¨$ó ð?4˜rŸy™yð ?4¨Zô ?4r   r   )r   ÚloggingÚcollections.abcr   Ú	functoolsr   Úqueuer   Útypingr   Útorch.nnr   Útorch.fx._compatibilityr   Útorch.fx.graph_moduler   Útorch.fx.passes.infra.pass_baser	   Ú	getLoggerr   ri   ÚsetLevelÚWARNINGÚ__all__r
   rH   r7   r(   rA   r   r   r,   r   r   ú<module>rˆ      s§  ðÛ Û Ý $Ý Ý Ý å Ý 1Ý -Ý 6ð 
ˆ×	Ñ	˜8Ó	$€Ø ‡��—‘Ô  â
T€ñ  eÔ,ð˜H S¨# XÑ.ð °8¸CÀÈdÑARÐ<RÑ3Sò ó -ðð@Ø˜( 3¨ 8Ñ,¨h°s¸C°xÑ.@ÐAÀ4ÐGÑHðà�˜#˜s˜(Ñ#Ñ$ðð 
óð6Ø�˜#˜s˜(Ñ#Ñ$ð6Ø37¸ÀÀcÀÑ8JÑ3Kð6à	ˆ(�3˜�8Ñ
Ñó6ñr  eÔ,ðØ
�3˜�8Ñ
ðØ$,¨S°#¨XÑ$6ðàˆx˜˜S˜Ñ! 8¨C°¨HÑ#5Ð6¸Ð<Ñ=òó -ðñ6  eÔ,÷c4ð c4ó -ñc4r   