
    shi(                        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  S SKJr  \R                   " \5      r\R'                  \R(                  5        / SQr\" S	S
9S\S\4S j5       rS\\\/\4   S\\   SS4S jrS\\   S\\   S\\   4S jr\" S	S
9S\S\S\4S j5       r\" S	S
9 " S S5      5       rg)    N)wraps)QueueCallable)compatibility)GraphModule)
PassResult)pass_result_wrapper this_before_that_pass_constraintPassManagerF)is_backward_compatiblefnreturnc                    ^  T c  g[        T 5      U 4S j5       n[        R                  " T 5      (       d  [        T 5      R                  Ul        U$ )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" U 5      nUc  [        U S5      $ [        U[         5      (       a  U$ [        U[        R                  5      (       a  [        US5      $ g )NT)r	   
isinstancennModule)gmresr   s     v/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/torch/fx/passes/infra/pass_manager.py
wrapped_fn'pass_result_wrapper.<locals>.wrapped_fn$   sS    f;b$''c:&&JRYY''c4(( (    )r   inspect
isfunctiontype__name__)r   r   s   ` r   r
   r
      sN     
z
2Y) ) b!!"2h//
r   
constraintpassesc                     [        U5       HI  u  p#[        XS-   S  5       H0  u  pEU " X55      (       a  M  [        SU SU SU SU SU SU S35      e   MK     g )	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   sz     &!fUWo.DA!>qc!aS
1#U1#Yqc B  / "r   constraintsc                 N  ^ [        U5      S:X  a  U $ U  Vs0 s H  o"/ _M     nn[        R                  U S5      m[        5       nU  Hg  nU  HB  nXV:X  a  M
  U H2  nU" XV5      (       a  M  X6   R	                  U5        TU==   S-  ss'   M4     MD     TU   S:X  d  MV  UR                  U5        Mi     [        R                  U S5      n/ n	UR                  5       (       d{  UR                  5       nU	R	                  U5        SX'   X2    H7  n
X   (       a  M  TU
==   S-  ss'   TU
   S:X  d  M&  UR                  U
5        M9     UR                  5       (       d  M{  [        [        U4S jTR                  5       5      5      n[        U5      S:w  a  SU 3n[        U5      eU	$ s  snf )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                    > TU    S:g  $ )Nr    )pindegree_maps    r   <lambda>*_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_passesrC   B   s    ;1 =C,CFqUFE,C(,fa(@LJAv)
!!''HOOA& Oq(O *	  ?aNN1  %)MM&%$@G$&M  NNQ
A::Q1$?a'NN1%	     =|?P?P?RSTL
<AHW 	 5!!M -Ds   F"thisthatc                 4   ^ ^ S[         S[         4UU 4S jjnU$ )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                 &   > U T:g  =(       d    UT:g  $ Nr-   )r&   r(   rE   rD   s     r   
depends_on4this_before_that_pass_constraint.<locals>.depends_on   s    Dy%AI%r   r   )rD   rE   rI   s   `` r   r   r   {   s#    *&h &8 & & r   c                   *   \ rS rSr% Sr\\\R                  /\	4      \
S'   \\\\/\4      \
S'   Sr\\
S'   Sr\\
S'        SS
\S\4S jjrS\4S jrS\4S jrS rS rS\SS	4S jrS\R                  SS	4S jrS\R                  S\	4S jrSrg	)r      a$  
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                 v    U=(       d    / U l         U=(       d    / U l        U(       a  X0l        X@l        XPl        g rH   )r    r*   rN   rO   rP   )selfr    r*   rN   rO   rP   s         r   __init__PassManager.__init__   s0     l&,"J*D''>$r   _passc                 H    U R                   R                  U5        SU l        g)z.
Adds a pass into the current list of passes.
FN)r    r5   rM   )rR   rU   s     r   add_passPassManager.add_pass   s     	5!r   r   c                 H    U R                   R                  U5        SU l        g)z9
Adds a constraint into the current list of constraints.
FN)r*   r5   rM   rR   r   s     r   add_constraintPassManager.add_constraint   s     	
+r   c                     U R                   (       a  gU R                   H  n[        XR                  5        M     SU l         g)z{
Validates that current pass schedule defined by `self.passes` is valid
according to all constraints in `self.constraints`
NT)rM   r*   r)   r    rZ   s     r   validate_constraints PassManager.validate_constraints   s1    
 ??**J.z;;G +r   c                 \    [        U R                  U R                  5      U l        SU l        g)a2  
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)rC   r    r*   rM   )rR   s    r   solve_constraintsPassManager.solve_constraints   s#     /t{{D<L<LMr   checkr   c                     [         R                  " U5      n[        [        UR                  R                  5       5      5      S:w  a  [        S5      e[        U SU5        g)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 modulerc   N)r   	signaturer2   r9   
parametersvalues	TypeErrorsetattr)rR   rc   sigs      r   
add_checksPassManager.add_checks   sQ     &tCNN))+,-2W  	gu%r   modulec                     g rH   r-   )rR   rm   s     r   rc   PassManager.check   s    r   c           	      n   U R                   (       d  U R                  5         U R                  U5        Sn[        U R                  5       GHX  nSn[        U R                  5       GH$  u  pV[        R                  " U5      (       a  UR                  O[        U5      R                  n[        R                  SU5         U" U5      n[        U[        5      (       d#  [        US5      (       d  [!        SU S3S-   5      eUR"                  nU=(       d    UR$                  n[        U[&        5      (       a0  [        R                  SXqR(                  5        UR+                  5         U R,                  (       a  U R                  U5        GM$  GM'     U=(       d    UnU(       a  GMY    O   [        X5      $ ! [.         ax  n	U R                  SU  V
s/ s H?  n
[        R                  " U
5      (       a  U
R                  O[        U
5      R                  PMA     Os  sn
f nn
S	U S
U 3n[/        U5      U	eSn	A	ff = f)aM  
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: )rM   ra   rc   rangerN   r#   r    r   r   r   r   loggerdebugr   r	   hasattrrh   rq   modifiedr   r<   	recompilerO   	Exception)rR   rm   overall_modified_rv   r%   r   fn_namer   er.   prev_pass_namesmsgs                r   __call__PassManager.__call__   s    ""$ 	

6 !tzz"AH #4;;/)0););B)?)?"++T"XEVEV0':0V*C%c:66w^@ @ (5gY>YZIJ  !--F'73<<H!&+66%@'<<X((* 66

6* 7- 0D  0;88Q #T &33 ! 0 "&Ra'!0A '.&8&8&;&;

aAQAQQ!0'O ' A	Ilm|l}~C#C.a/0s&   >CF22
H4<H/AH
H//H4)rM   r*   r    rO   rN   rP   )NNNFF)r   
__module____qualname____firstlineno____doc__r9   r   r   r   r	   __annotations__boolrM   rN   intrS   rW   r[   r^   ra   rk   rc   r   __static_attributes__r-   r   r   r   r      s    ( 299+z1233h(3T9:;;JE3N +0(-?
 %)? "&?  h     	
& &T &BII $ =4ryy =4Z =4r   r   )r   logging	functoolsr   queuer   typingr   torch.nnr   torch.fx._compatibilityr   torch.fx.graph_moduler   torch.fx.passes.infra.pass_baser	   	getLoggerr   rs   setLevelWARNING__all__r
   r   r9   r)   rC   r   r   r-   r   r   <module>r      s         1 - 6 
		8	$   
T e,H   ->(H-t34>B8n	6N6)-h6	(^6r e,8 8   -4 e,^4 ^4 -^4r   