o
    )i@                     @   s   d dl Z d dlZd dl mZ d dlmZ d dlmZ d dlmZ d dl	m
Z d dlmZ d dlmZ d d	lmZ G d
d de jZdS )    N)abstractmethod)Iterable)fx)auto_functionalized)pattern_matcher)
OpOverload)Node)find_auto_fnc                   @   s   e Zd ZdZdejfddZedd Ze	de
ej fdd	Ze	dejfd
dZdejfddZdd Zdejdee deejdf fddZdedefddZdS )MultiOutputMatchz
    This class provides utilities to process multi-output matches and
    manually insert replacements.

    This is necessary because the automatic replacement for multi-output
    matches is broken: https://github.com/pytorch/pytorch/issues/137280
    matchc                 C   s
   || _ d S N)r   )selfr    r   o/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/vllm/compilation/multi_output_match.py__init__   s   
zMultiOutputMatch.__init__c                 C   s   t )ae  
        Process a multi-output match and manually insert the replacement.

        This method should:
        1. Insert the replacement nodes after the last node in the match.
        2. Rebind the users of nodes in the match to use the new nodes.
        3. Set meta["val"] for de-functionalization.

        The result of an auto-functionalized node is a tuple of tensors.
        The first element is the return value of the function, usually None.
        The remaining elements are the mutated args of the function.

        All auto-functionalized nodes must contain a proper meta["val"],
        as it is used by de-functionalization. meta["val"] has to contain the
        value of the node (tuple of tensors) that would be returned by the
        functionalized node during tracing.

        Existing nodes in the graph all have this property set, but we have
        to set it manually for new nodes we insert.

        Example:
        # op schema: foo(a: Tensor!, b: Tensor, c: Tensor!) -> None
        at = auto_functionalized(torch.ops._C.foo.default, a, b, c)
        # at.meta["val"] = (None, a, c)
        )NotImplementedErrorr   r   r   r   process   s   zMultiOutputMatch.processreturnc                 C      | j jS r   )r   nodesr   r   r   r   r   ;      zMultiOutputMatch.nodesc                 C   r   r   )r   graphr   r   r   r   r   ?   r   zMultiOutputMatch.graphc                 C   s   t | j|S )zY
        Find the first auto_functionalized node with the given op in the match.
        )r	   r   )r   opr   r   r   r	   C   s   zMultiOutputMatch.find_auto_fnc                 C   s6   t | jjD ]
}|| jjv r nqtd| j|S )z
        Insert nodes after the last node in the match.
        This is done to avoid use-before-definition errors after inserting
        replacement nodes.
        zNo nodes in graph)reversedr   r   r   
ValueErrorinserting_after)r   Zlast_node_in_matchr   r   r   inserting_after_matchI   s   	z&MultiOutputMatch.inserting_after_match
tuple_nodeindices.c                    sH    j  t fdd|D W  d   S 1 sw   Y  dS )a*  
        Insert operator.getitem nodes to extract elements from a tuple node.

        :param tuple_node: The tuple node to extract elements from.
        :param indices: The indices of the elements to extract.
        :return: Tuple of the new getitem nodes, corresponding to the indices.
        c                 3   s$    | ]} j tj|fV  qd S r   )r   call_functionoperatorgetitem).0idxr   r   r   r   	<genexpr>d   s
    
z3MultiOutputMatch.insert_getitems.<locals>.<genexpr>N)r   r   tuple)r   r   r   r   r%   r   insert_getitemsZ   s
   	$z MultiOutputMatch.insert_getitemsr   c                 C   s   | j jt|f|dS )zR
        Insert an auto_functionalized node with the given op and kwargs.
        )kwargs)r   r    r   )r   r   r)   r   r   r   insert_auto_fnh   s   zMultiOutputMatch.insert_auto_fnN)__name__
__module____qualname____doc__pmMatchr   r   r   propertylistr   r   r   ZGraphr   r	   r   r   intr'   r(   r   r*   r   r   r   r   r
      s"    

r
   )abcr!   r   collections.abcr   Ztorchr   Z*torch._higher_order_ops.auto_functionalizer   Ztorch._inductorr   r/   Z
torch._opsr   Ztorch.fxr   Zvllm.compilation.fx_utilsr	   ABCr
   r   r   r   r   <module>   s   