
    `i                     (   d dl Z d dlmZ d dlmZ d dlmZm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 )    Nwrapsunwrap)CallableOptional)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 )gmr   s    p/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torch/fx/passes/pass_manager.py
wrapped_fnz#inplace_wrapper.<locals>.wrapped_fn"   s    
2	    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   valr   levels     r   r   zlog_hook.<locals>.wrapped_fnG   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_passd   s    &1**6]] + +"6**+")F## +"6** )F## + ?? ?2;? ? ?   r   r   )r   r    r!   r*   s   ``` r   r   r   P   sr      $9D#89  B 9 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_constraintr4   {   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`.
    r1   r3   c                     | k    p|k    S r   r   )r1   r3   r6   r5   s     r   
depends_onz4this_before_that_pass_constraint.<locals>.depends_on   s    Dy%AI%r   r   )r5   r6   r9   s   `` r   r   r      s=    &h &8 & & & & & & & r   thesethosec                 4     dt           dt           f fd}|S )aQ  
    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]
    r1   r3   c                 P    t          |           k    pt          |          k    S r   r   )r1   r3   r;   r<   s     r   r9   z6these_before_those_pass_constraint.<locals>.depends_on   s$    ayyE!7VAYY%%77r   r:   )r;   r<   r9   s   `` r   r   r      s=    .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,   r@   )selfr,   r@   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,   appendrA   )rC   rI   s     r   add_passzPassManager.add_pass   s#    5!!!r   c                 H    | j                             |           d| _        d S rK   )r@   rL   rA   rC   r+   s     r   add_constraintzPassManager.add_constraint   s$    
+++r   _passesc                 R    d S fd| j         D             }|| _         d| _        d S )Nc                 &    g | ]}|j         v|S r   )__name__).0psrQ   s     r   
<listcomp>z+PassManager.remove_pass.<locals>.<listcomp>   s%    NNNb2;g3M3Mr3M3M3Mr   F)r,   rA   )rC   rQ   passes_lefts    ` r   remove_passzPassManager.remove_pass   s:    ?FNNNNDKNNN!r   c                     g }| j         D ]=}|j        |j        k    r|                    |           (|                    |           >|| _         d| _        d S rK   )r,   rT   rL   rA   )rC   _target_replacementrX   rV   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)rA   r@   r4   r,   rO   s     r   validatezPassManager.validate   sG    
 ? 	F* 	H 	HJ.z4;GGGGr   c                 \    |                                   |}| j        D ]} ||          }|S r   )r_   r,   )rC   r'   outrI   s       r   __call__zPassManager.__call__   s9    [ 	 	E%**CC
r   NN)rT   
__module____qualname____doc__listr   __annotations__rA   boolrD   classmethodrH   rM   rP   strrY   r]   r_   rb   r   r   r   r	   r	      s           NhJ - - - -   [
 h              49             	 	 	    r   r	   rc   )logging	functoolsr   inspectr   typingr   r   	getLoggerrT   r   __all__r
   INFOr   intr   ri   rg   r4   r   r   r	   r   r   r   <module>rt      s                % % % % % % % % 
	8	$	$   X    ( ") # # #( # # # #P !$($ $$SM$ !$ $ $ $V(H-t34>B8n   	8 	8 	 	 	 	h x    :K K K K K K K K K Kr   