
    קg                     (   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dS )    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     X/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/fx/passes/pass_manager.py
wrapped_fnz#inplace_wrapper.<locals>.wrapped_fn    s    bff	    r   )r   r   s   ` r   r   r      s6     2YY    Y r   c                 @     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                 X     |           }t                               d|           |S )NzRan pass %s	 Return value: %s)loggerlog)r   r   r   levels     r   r   zlog_hook.<locals>.wrapped_fnF   s.    bff

5:BDDD
r   r   )r   r   r   s   `` r   r   r   '   s<    > 2YY     Y
 r   	base_passn_iter	predicatec                 j     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k    rt                    D ]} |          }n:" |          r |          } |          nt          d d d          |S )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## + ?? ?2;? ? ?   r   r   )r    r!   r"   r+   s   ``` r   r   r   P   s|     $ B BAB B B 9       Or   
constraintpassesc                     t          |          D ]S\  }}t          ||dz   d                    D ]3\  }} | ||          rt          d| d| d| d| d| d| d          Td S )	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UWWo.. 	 	DAqz!Q q  !   +, 34 ?@    	 r   thisthatc                 4     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                     | k    p|k    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                 4     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                 P    t          |           k    pt          |          k    S r   r   )r2   r4   r<   r=   s     r   r:   z6these_before_those_pass_constraint.<locals>.depends_on   s$    ayyE!7VAYY%%77r   r;   )r<   r=   r:   s   `` r   r   r      s=    28h 88 8 8 8 8 8 8 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dS )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                 *    |pg | _         |pg | _        d S 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                             |           d| _        d S NF)r-   appendrB   )rD   rJ   s     r   add_passzPassManager.add_pass   s#    5!!!r   c                 H    | j                             |           d| _        d S rL   )rA   rM   rB   rD   r,   s     r   add_constraintzPassManager.add_constraint   s$    
+++r   _passesc                 ~    |d S g }| j         D ] }|j        |vr|                    |           !|| _         d| _        d S rL   r-   __name__rM   rB   )rD   rR   passes_leftpss       r   remove_passzPassManager.remove_pass   sV    ?F+ 	' 	'B{'))""2&&&!r   c                     g }| j         D ]=}|j        |j        k    r|                    |           (|                    |           >|| _         d| _        d S rL   rT   )rD   _target_replacementrV   rW   s        r   replace_passzPassManager.replace_pass   sh    + 	' 	'B{g...""<0000""2&&&&!r   c                 d    | j         rdS | j        D ]}t          || j                   d| _         dS )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   sG    
 ? 	F* 	H 	HJ.z4;GGGGr   c                 \    |                                   |}| j        D ]} ||          }|S r   )r^   r-   )rD   r(   outrJ   s       r   __call__zPassManager.__call__   s9    [ 	 	E%**CC
r   NN)rU   
__module____qualname____doc__r   r   __annotations__rB   boolrE   classmethodrI   rN   rQ   strrX   r\   r^   ra   r   r   r   r
   r
      s           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 N N N N N N N Nr   