
    .`i                         d dl Z d dlZd dlmZ d dlmZ d dlmZmZ d dl	m
Z
mZ ddZd	 Z G d
 de j        j                  Z	 	 	 	 	 ddeee j        f         dedededede
e j                 de
e         fdZdS )    N)_topk_forward)_topk_backward)Tensor	Bitmatrix)OptionalUnionT   c                    t          | t                    sJ|| j        d         n|| j        d         g}| j        d         | j        d         g}t          | ||          } d }	d}
d}d}t          | j                  dk    sJ | j        d         d	k     sJ |dk    sJ |sJ | j        \  }}| j        \  }}| j        }t          j        ||f| j        |
          }|d}n%t          j        ||ft          j	        |
          }d} |	||          |z  }|dz  }t          j        | |	|d          dz  ft          j
        |
          }t          j        |dd          d |         } |	||          }||z  }t          j        |ft          j        |
          }t           |	||
          |          }t          |f         | |                     d          |||                    d          |||                    d          |                    d          ||||||
||||           ||dz  g}|d g}t!          ||||          }|||fS )Nr   r	   )shape	shape_maxc                     | |z   dz
  |z  S )Nr	    )abs     x/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/third_party/triton_kernels/topk.py<lambda>ztopk_forward.<locals>.<lambda>   s    Qq(               i   )dtypedeviceTF)BLOCK_MBLOCK_NAPPLY_SOFTMAXN_EXPTS_PADN_EXPTS_ACT)r   r   
scratchpad)
isinstancer   r   lenr   r   torchemptyr   int16uint32	transposeint32maxr   strider   )xkapply_softmaxdimreturn_bitmatrixy_indxn_rowsx_shapex_shape_maxcdivr   r   BLOCK_Sn_cols
n_rows_max_devy_valsuse_provided_indx
n_cols_padn_cols_words	bitmatrixs_blockss_colsr   pidsbitmatrix_shapebitmatrix_shape_maxs                               r   topk_forwardrC   	   s   a   <!'171::VQWQZHwqz171:.1G{;;;((DGGGqw<<1;r?U""""!8888WNFFKMJ
(C [*aDDDF j!_EKLLL!fg&&0J#L\44
B+?+?"+DEU\befffI	1a00*=ItFG$$HFfZu{3GGGJttJ(((33D4(	188A;;a((*;9##A&&	(8(8(;(;GX#    |b01O%t,)?FYfpqqqI69$$r   c                    |j         d         |k    sJ t          j        | j         d                   }t          j        |           }t          |j         d         f         ||                    d          ||                    d          | |                     d          ||                    d          | j         d         || j         d         |||           |S )Nr   r   )r   r   r   )r   tritonnext_power_of_2r"   
empty_liker   r)   )r*   r/   dy_valsr+   r0   r,   n_expts_paddxs           r   topk_backwardrK   9   s    =!!!!(55K		!		BGM!$'(a  '7>>!+<+<a!

		!agaj&!'"+]`a	! ! ! !
 Ir   c                   :    e Zd Zed             Zed             ZdS )TopKc           	          t          |||||||          \  }}}	|                     ||           || _        || _        || _        |||	fS N)rC   save_for_backwardr,   r+   r0   )
ctxr*   r+   r,   r-   r.   r/   r0   r9   r=   s
             r   forwardzTopK.forwardG   s^    $0A}cK[]cek$l$l!	a((()
vy((r   c                 n    | j         \  }}t          |||| j        | j        | j                  }|d d d d d d fS rO   )saved_tensorsrK   r+   r0   r,   )rQ   rH   _0_1r*   r/   rJ   s          r   backwardzTopK.backwardP   sA    %	61fgsucj#BSTT4tT455r   N)__name__
__module____qualname__staticmethodrR   rW   r   r   r   rM   rM   E   sH        ) ) \) 6 6 \6 6 6r   rM   r*   r+   r,   r-   r.   r/   r0   c           	      F    t                               | ||||||          }|S )a  
    Computes the top-k values and indices along a specified dimension of a tensor.
    Note that the input can be either a `Tensor` or a `torch.Tensor`, but the output will always be a `torch.Tensor`.

    Parameters
    ----------
    x : Union[triton_kernels.Tensor, torch.Tensor]
        Input tensor of shape (n_tokens, n_expts).
    k : int
        Number of top elements to retrieve.
    apply_softmax : bool, default True
        Whether to apply softmax to the input tensor before computing top-k.
    dim : int, default 1
        Dimension along which to compute top-k.
    return_bitmatrix : bool, default True
        A bitmatrix of shape (n_tokens, cdiv(n_expts, 32)).
        Each bit on [t, b] indicates whether the b-th expert was selected for the t-th token.
    y_indx : torch.Tensor, optional
        Pre-allocated tensor for storing indices of top-k elements with shape (n_tokens, k).
        If provided, we skip the computation of top-k indices and use this tensor instead.
    n_rows : int, optional
        Number of rows to apply top-k on. If None, we consider all rows in `x`.

    Returns
    -------
    (expt_scal, expt_indx, bitmatrix) : Tuple[torch.Tensor, torch.Tensor, Bitmatrix]
    )rM   apply)r*   r+   r,   r-   r.   r/   r0   rets           r   topkr_   W   s(    H **Q=#/?
P
PCJr   )Tr	   TNN)r"   rE   )triton_kernels.topk_details._topk_forwardr   *triton_kernels.topk_details._topk_backwardr   triton_kernels.tensorr   r   typingr   r   rC   rK   autogradFunctionrM   intboolr_   r   r   r   <module>rh      sK     C C C C C C E E E E E E 3 3 3 3 3 3 3 3 " " " " " " " "-% -% -% -%`	 	 	6 6 6 6 65>" 6 6 6* !%) % %VU\!"%
% % 
	%
 % U\"% SM% % % % % %r   