
    sg                        d dl mZ d dlmZ d dlmZmZmZ d dlZ ej                  e
      Zg dZdedefdZej                  fdedefd	Zdd
edee   dee   fdZdeeegef   dee   fdZdedefdZdedefdZ G d d      Zy)    wrapsunwrap)CallableListOptionalN)PassManagerinplace_wrapperlog_hook	loop_pass this_before_that_pass_constraint"these_before_those_pass_constraintfnreturnc                 .     t                fd       }|S )z
    Convenience wrapper for passes which modify an object inplace. This
    wrapper makes them return the modified object instead.

    Args:
        fn (Callable[Object, Any])

    Returns:
        wrapped_fn (Callable[Object, Object])
    c                      |       }| S N )gmvalr   s     O/var/www/html/venv/lib/python3.12/site-packages/torch/fx/passes/pass_manager.py
wrapped_fnz#inplace_wrapper.<locals>.wrapped_fn    s    f	    r   )r   r   s   ` r   r   r      s#     2Y  r   c                 2     t                fd       }|S )a  
    Logs callable output.

    This is useful for logging output of passes. Note inplace_wrapper replaces
    the pass output with the modified object. If we want to log the original
    output, apply this wrapper before inplace_wrapper.


    ```
    def my_pass(d: Dict) -> bool:
        changed = False
        if 'foo' in d:
            d['foo'] = 'bar'
            changed = True
        return changed

    pm = PassManager(
        passes=[
            inplace_wrapper(log_hook(my_pass))
        ]
    )
    ```

    Args:
        fn (Callable[Type1, Type2])
        level: logging level (e.g. logging.INFO)

    Returns:
        wrapped_fn (Callable[Type1, Type2])
    c                 H     |       }t         j                  d|       |S )NzRan pass %s	 Return value: %s)loggerlog)r   r   r   levels     r   r   zlog_hook.<locals>.wrapped_fnF   s$    f

5:BD
r   r   )r   r   r   s   `` r   r   r   '   s#    > 2Y 
 r   	base_passn_iter	predicatec                 V     duduz  sJ d       t                fd       }|S )aW  
    Convenience wrapper for passes which need to be applied multiple times.

    Exactly one of `n_iter`or `predicate` must be specified.

    Args:
        base_pass (Callable[Object, Object]): pass to be applied in loop
        n_iter (int, optional): number of times to loop pass
        predicate (Callable[Object, bool], optional):

    Nz8Exactly one of `n_iter`or `predicate` must be specified.c                     | }dkD  rt              D ]
  } |      } |S  |      r |      } |      r|S t        d d d      )Nr   z3loop_pass must be given positive int n_iter (given z) xor predicate (given ))rangeRuntimeError)sourceoutput_r    r!   r"   s      r   new_passzloop_pass.<locals>.new_pass`   s    &1*6] +"6*+  "F#"6* F# 	 E(1)A? r   r   )r    r!   r"   r+   s   ``` r   r   r   P   sN     $ BAB  9  Or   
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.)	enumerater'   )r,   r-   iajbs         r   "_validate_pass_schedule_constraintr5   w   s     &! 1fQUWo. 	DAq!Q>qc!aS
1#U1#Yqc B 	r   thisthatc                 0     dt         dt         f fd}|S )zd
    Defines a partial order ('depends on' function) where `this` must occur
    before `that`.
    r2   r4   c                     | k7  xs |k7  S r   r   )r2   r4   r7   r6   s     r   
depends_onz4this_before_that_pass_constraint.<locals>.depends_on   s    Dy%AI%r   r   )r6   r7   r:   s   `` r   r   r      s    &h &8 & r   thesethosec                 0     dt         dt         f fd}|S )a_  
    Defines a partial order ('depends on' function) where `these` must occur
    before `those`. Where the inputs are 'unwrapped' before comparison.

    For example, the following pass list and constraint list would be invalid.
    ```
    passes = [
        loop_pass(pass_b, 3),
        loop_pass(pass_a, 5),
    ]

    constraints = [
        these_before_those_pass_constraint(pass_a, pass_b)
    ]
    ```

    Args:
        these (Callable): pass which should occur first
        those (Callable): pass which should occur later

    Returns:
        depends_on (Callable[[Object, Object], bool]
    r2   r4   c                 @    t        |       k7  xs t        |      k7  S r   r   )r2   r4   r<   r=   s     r   r:   z6these_before_those_pass_constraint.<locals>.depends_on   s     ayE!7VAY%%77r   r;   )r<   r=   r:   s   `` r   r   r      s    28h 88 8 r   c                       e Zd ZU dZee   ed<   ee   ed<   dZeed<   	 	 ddZ	e
d        Zd	efd
Zd Zdee   fdZd Zd Zd Zy)r
   aT  
    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 modified object
        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.
    r-   constraintsF
_validatedNc                 0    |xs g | _         |xs g | _        y r   )r-   rA   )selfr-   rA   s      r   __init__zPassManager.__init__   s    
 l&,"r   c                     t        |      }|S r   )r
   )clsr-   pms      r   build_from_passlistzPassManager.build_from_passlist   s     	r   _passc                 H    | j                   j                  |       d| _        y NF)r-   appendrB   )rD   rJ   s     r   add_passzPassManager.add_pass   s    5!r   c                 H    | j                   j                  |       d| _        y rL   )rA   rM   rB   rD   r,   s     r   add_constraintzPassManager.add_constraint   s    
+r   _passesc                     |y g }| j                   D ]"  }|j                  |vs|j                  |       $ || _         d| _        y rL   r-   __name__rM   rB   )rD   rR   passes_leftpss       r   remove_passzPassManager.remove_pass   sL    ?++ 	'B{{')""2&	' "r   c                     g }| j                   D ]>  }|j                  |j                  k(  r|j                  |       .|j                  |       @ || _         d| _        y rL   rT   )rD   _target_replacementrV   rW   s        r   replace_passzPassManager.replace_pass   sX    ++ 	'B{{g...""<0""2&		'
 "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)rB   rA   r5   r-   rP   s     r   validatezPassManager.validate   s:    
 ??** 	HJ.z4;;G	Hr   c                 \    | j                          |}| j                  D ]
  } ||      } |S r   )r^   r-   )rD   r(   outrJ   s       r   __call__zPassManager.__call__   s1    [[ 	E*C	
r   NN)rU   
__module____qualname____doc__r   r   __annotations__rB   boolrE   classmethodrI   rN   rQ   strrX   r\   r^   ra   r   r   r   r
   r
      sx     NhJ -  
 h    49   	r   r
   rb   )	functoolsr   inspectr   typingr   r   r	   logging	getLoggerrU   r   __all__r   INFOr   intr   rg   r5   r   r   r
   r   r   r   <module>rr      s      + + 			8	$ X & ") % %( %R   8C=  HU]L^  N(H-t34>B8n	8 	8 	h x >N Nr   