o
    -i	                     @   sL   d dl Z ddlmZ ddlmZ dddZdde jd	e jd
e jfddZdS )    N   )_masked_compaction)	Bitmatrixc                 C   sd   |j \}}t| }t|}t|tr|jj}t|f | |||d|d||||d	 ||fS )un  
    Return compacted copies of *yv* and *yi* based on a per-row bitmask.

    Only the elements whose index appears among the active bits of *bitmask*
    are kept; the rest are replaced by *sentinel*.  Kept elements preserve
    their original left-to-right order.

    Parameters
    ----------
    yv : torch.Tensor, shape (B, K)
        Values tensor.
    yi : torch.Tensor, shape (B, K), dtype torch.long
        Integer indices (0 ≤ index < 32) associated with *yv*.
    bitmask : torch.Tensor, shape (B,) **or** (B, 32)
        Per-row mask of active indices.  See the in-place version for details.
    sentinel : int, default -1
        Value written into dropped positions of the returned tensors.

    Returns
    -------
    (yv_out, yi_out) : Tuple[torch.Tensor, torch.Tensor], each shape (B, K)
        New tensors with the same dtype/device as the inputs.

    r   r   )K)	shapetorch
empty_like
isinstancer   storagedatar   stride)yvyibitmasksentineln_rowsn_colsret_yvret_yi r   g/home/ubuntu/veenaModal/venv/lib/python3.10/site-packages/vllm/third_party/triton_kernels/compaction.py
compaction   s   



r   r   r   r   c                 C   s   |j \}}|j}dtjd||jd> }|d|@ dk}|jdd}	|	d| }
|
 	tj
jddd	}|d|}| d|}|
d|}||| < ||| < ||fS )
z6
    reference implementation of `masked_compact`
    r       )devicedtyper   r   )	start_dimT)dimstable)r   r   r   aranger   	unsqueezeflattengatherlongtointargsort)r   r   r   r   Br   r   wbitsmaskkeeporder	yi_sorted	yv_sortedkeep_sortedr   r   r   compaction_torch/   s   


r1   )r   )r   %compaction_details._masked_compactionr   tensorr   r   Tensorr1   r   r   r   r   <module>   s
    
")