
    Vh                        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y)    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    L/home/dcms/DCMS/lib/python3.12/site-packages/torch/fx/passes/pass_manager.py
wrapped_fnz#inplace_wrapper.<locals>.wrapped_fn"   s    
2	    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   valr   levels     r   r   zlog_hook.<locals>.wrapped_fnG   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_passd   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_constraintr4   {   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`.
    r1   r3   c                     | k7  xs |k7  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                 0     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                 @    t        |       k7  xs t        |      k7  S r   r   )r1   r3   r;   r<   s     r   r9   z6these_before_those_pass_constraint.<locals>.depends_on   s     ayE!7VAY%%77r   r:   )r;   r<   r9   s   `` r   r   r      s    .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,   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                   j                  |       d| _        y NF)r,   appendrA   )rC   rI   s     r   add_passzPassManager.add_pass   s    5!r   c                 H    | j                   j                  |       d| _        y rK   )r@   rL   rA   rC   r+   s     r   add_constraintzPassManager.add_constraint   s    
+r   _passesc                     |y | j                   D cg c]  }|j                  |vs| }}|| _         d| _        y c c}w rK   )r,   __name__rA   )rC   rQ   pspasses_lefts       r   remove_passzPassManager.remove_pass   s@    ?$(KKNb2;;g3MrNN! Os   ;;c                     g }| j                   D ]>  }|j                  |j                  k(  r|j                  |       .|j                  |       @ || _         d| _        y rK   )r,   rS   rL   rA   )rC   _target_replacementrU   rT   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)rA   r@   r4   r,   rO   s     r   validatezPassManager.validate   s:    
 ??** 	HJ.z4;;G	Hr   c                 \    | j                          |}| j                  D ]
  } ||      } |S r   )r\   r,   )rC   r'   outrI   s       r   __call__zPassManager.__call__   s1    [[ 	E*C	
r   NN)rS   
__module____qualname____doc__listr   __annotations__rA   boolrD   classmethodrH   rM   rP   strrV   rZ   r\   r_   r   r   r   r	   r	      sx     NhJ -  
 h    49   	r   r	   r`   )logging	functoolsr   inspectr   typingr   r   	getLoggerrS   r   __all__r
   INFOr   intr   rf   rd   r4   r   r   r	   r   r   r   <module>rq      s       % 
		8	$ X ( ") # #( #P !$($$SM$ !$V(H-t34>B8n	8 	8 	h x :K Kr   