Ë
    f^(hi(  ã                   ó˜  — 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  ej                   e«      Zej'                  ej(                  «       g d¢Z ed	¬
«      dedefd„«       Zdeeegef   dee   ddfd„Zdee   dee   dee   fd„Z ed	¬
«      dededefd„«       Z ed	¬
«       G d„ d«      «       Zy)é    N)Úwraps)ÚQueue©ÚCallable)Úcompatibility)ÚGraphModule)Ú
PassResult)Úpass_result_wrapperÚ this_before_that_pass_constraintÚPassManagerF)Úis_backward_compatibleÚfnÚreturnc                 ó’   ‡ — ‰ €yt        ‰ «      ˆ 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])
    Nc                 ó¢   •—  ‰| «      }|€t        | d«      S t        |t         «      r|S t        |t        j                  «      rt        |d«      S y )NT)r	   Ú
isinstanceÚnnÚModule)ÚgmÚresr   s     €ú`/var/www/skyplay_api_hub/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   ÚinspectÚ
isfunctionÚtypeÚ__name__)r   r   s   ` r   r
   r
      sK   ø€ ð 
€zØä
ˆ2ƒYó)ó ð)ô ×Ñ˜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	 at indexz in pass list.)Ú	enumerateÚRuntimeError)r   r   ÚiÚaÚjÚbs         r   Ú"_validate_pass_schedule_constraintr(   4   s‰   € ô ˜&Ó!ò ‰ˆˆ1Ü˜f Q¨¡U W˜oÓ.ò 	‰DˆAˆqÙ˜!˜QÔØÜØ>¸q¸cÀÈ!ÈØ˜a˜S 
¨1¨#¨U°1°#°Y¸q¸cð Bðóð ñ	ñ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>q   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   B   s¨  ø€ ô ˆ;Ó˜1ÒØˆð =CÖ,C°q¨Q°©UÐ,C€EÐ,CÜ(,¯©°f¸aÓ(@€LÜ›€JØò ˆØò 	)ˆAØ�AŠvØà)ò )�
Ù! ! QÕ'Ø˜!‘H—O‘O AÔ&Ø  “O qÑ(”Oñ)ð		)ð ˜‰?˜aÓØ�N‰N˜1Õðô %)§M¡M°&¸%Ó$@€GØ$&€Mà×ÑÔ Ø�N‰NÓˆØ×Ñ˜QÔØˆ�‰
à�q‘ò 	&ˆAØ˜1“:Ø˜Q“ 1Ñ$“Ø ‘? aÓ'Ø—N‘N 1Õ%ð		&ð ×ÑÕ ô œÓ=¸|×?PÑ?PÓ?RÓSÓT€LÜ
ˆ<Ó˜AÒàHÈÈÐWð 	ô ˜5Ó!Ð!àÐùòM -Ds   –
E>ÚthisÚthatc                 ó0   ‡ ‡— dt         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'   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   )rB   rC   rG   s   `` r   r   r   {   s   ù€ ð*&”hð &¤8ö &ð Ðr   c                   ó  — e Zd ZU dZeeej                  gef      e	d<   eeeege
f      e	d<   dZe
e	d<   dZee	d<   	 	 	 	 	 dd	e
d
e
fd„Zdefd„Zdefd„Zd„ Zd„ Zde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
    r   r)   FÚ
_validatedr!   ÚstepsNÚrun_checks_after_each_passÚsuppress_check_failuresc                 ó^   — |xs g | _         |xs g | _        |r|| _        || _        || _        y rF   )r   r)   rJ   rK   rL   )Úselfr   r)   rJ   rK   rL   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   rI   )rN   rP   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   rI   ©rN   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)rI   r)   r(   r   rT   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)   rI   )rN   s    r   Úsolve_constraintszPassManager.solve_constraintsÚ   s#   € ô /¨t¯{©{¸D×<LÑ<LÓMˆŒØˆ�r   Úcheckr   c                 ó¼   — 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 modulerZ   N)r   Ú	signaturer0   r7   Ú
parametersÚvaluesÚ	TypeErrorÚsetattr)rN   rZ   Úsigs      r   Ú
add_checkszPassManager.add_checksæ   sQ   € ô ×Ñ Ó&ˆäŒt�C—N‘N×)Ñ)Ó+Ó,Ó-°Ò2ÜØWóð ô 	��g˜uÕ%r   Úmodulec                  ó   — y rF   r,   )rN   rc   s     r   rZ   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: )rI   rY   rZ   ÚrangerJ   r"   r   r   r   r   r   ÚloggerÚdebugr   r	   Úhasattrr_   rf   Úmodifiedr   r:   Ú	recompilerK   Ú	Exception)rN   rc   Úoverall_modifiedÚ_rk   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Ô*ùð/0ðD  0Ò;°8ÐÛÙðQ(	ôT ˜&Ð"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   Ú
__module__Ú__qualname__Ú__doc__r7   r   r   r   r	   Ú__annotations__ÚboolrI   rJ   ÚintrO   rR   rU   rW   rY   rb   rZ   rt   r,   r   r   r   r   –   sÞ   … ñð( �˜2Ÿ9™9˜+ zÐ1Ñ2Ñ3Ó3Ø�h ¨(Ð3°TÐ9Ñ:Ñ;Ó;Ø€J�ÓØ€Eˆ3ƒNð ØØØ+0Ø(-ñ?ð
 %)ð?ð "&ó?ð  ˜hó  ð ¨ó  ò	ò
ð& ð &¨Tó &ð˜BŸI™Ið ¨$ó ð=4˜rŸy™yð =4¨Zô =4r   r   )r   ÚloggingÚ	functoolsr   Úqueuer   Útypingr   Útorch.nnr   Útorch.fx._compatibilityr   Útorch.fx.graph_moduler   Útorch.fx.passes.infra.pass_baser	   Ú	getLoggerr   rh   ÚsetLevelÚWARNINGÚ__all__r
   ry   r7   r(   rA   r   r   r,   r   r   ú<module>r‡      s  ðã Û Ý Ý Ý å Ý 1Ý -Ý 6ð 
ˆ×	Ñ	˜8Ó	$€Ø ‡��—‘Ô  â
T€ñ  eÔ,ð˜Hð ¨ò ó -ðð>Ø˜( HÐ-¨tÐ3Ñ4ðØ>BÀ8¹nðà	óð6Ø�‰Nð6Ø)-¨h©ð6à	ˆ(�^ó6ñr  eÔ,ð¨8ð ¸8ð Èò ó -ðñ4  eÔ,÷^4ð ^4ó -ñ^4r   