o
    i                     @   s`   d Z 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	 ee
ZG dd	 d	e	ZdS )
a  
Coalesce duplicate ``split_with_sizes`` nodes that operate on the same
input tensor with the same split sizes.

On certain hardware/dtype combinations (e.g. B200 + FP8) the Inductor
graph may contain multiple ``split_with_sizes`` calls on the same tensor
that CSE fails to merge. This pass detects and replaces the duplicates
so that downstream pattern-matching passes (e.g. QK-Norm+RoPE fusion)
see a single split node with all users attached.

See also:
  - vLLM  #33295  (original issue)
  - PyTorch #174472 (upstream CSE gap)
    N)fx)init_logger   )is_func)VllmInductorPassc                   @   s*   e Zd ZdZejdejddfddZdS )SplitCoalescingPasszReplace duplicate ``split_with_sizes`` nodes with a single canonical
    node when they share the same input tensor and split sizes.graphreturnNc                    s   d}i }|j D ]R}t|tjjjjsqtdd |jD sq|j	d d \} ||vr1|g||< qt
 fdd|| D d }|d urR|| || |d7 }q|| | qtd| d S )Nr   c                 s   s    | ]	}t |tjV  qd S )N)r   operatorgetitem).0user r   f/home/ubuntu/vllm_env/lib/python3.10/site-packages/vllm/compilation/passes/utility/split_coalescing.py	<genexpr>-   s    z/SplitCoalescingPass.__call__.<locals>.<genexpr>r   c                 3   s*    | ]}t |jd  t  kr|V  qdS )   N)listargs)r   nsplit_sizesr   r   r   8   s    r   z-Coalesced %d duplicate split_with_sizes nodes)nodesr   torchopsatensplit_with_sizesdefaultallusersr   nextreplace_all_uses_with
erase_nodeappendloggerdebug)selfr   countsplit_nodesnodearg_node	canonicalr   r   r   __call__#   s.   





zSplitCoalescingPass.__call__)	__name__
__module____qualname____doc__r   time_and_logr   Graphr+   r   r   r   r   r      s    r   )r/   r
   r   r   vllm.loggerr   fx_utilsr   vllm_inductor_passr   r,   r#   r   r   r   r   r   <module>   s   