o
    پi"                     @   sF  d dl m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mZmZ d dlmZ 	ddejdejdejdejdejdedejdejdeej fddZG dd dejjZejj						ddejdejdejdejdejdedejdejdeej de de fddZ!dS )     )OptionalN	rearrange)chunk_gated_delta_rule_fwd_h)chunk_fwd_o)chunk_scaled_dot_kkt_fwd)chunk_local_cumsum)
l2norm_fwd)
solve_tril)SUPPRESS_LEVELautocast_custom_fwdinput_guard)recompute_w_u_fwdqkvgbetascaleinitial_stateinitial_state_indices
cu_seqlensc	              	   C   s   t |d|d}t||||tjd}	t|	||jd}	t||||	||d\}
}t||
|||||d\}}t| ||||||d}t	dk rI|||	d |d fS t	dkrU|||	|
||fS d S )	N@   )
chunk_sizer   )r   r   g_cumsumr   output_dtype)Ar   r   )r   r   r   r   r   r   )r   wur   r   r   r   )r   r   r   hr   r   r      )
r   r   torchfloat32r
   dtyper   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   v_newo r&   Y/home/ubuntu/.local/lib/python3.10/site-packages/sglang/srt/layers/attention/fla/chunk.pychunk_gated_delta_rule_fwd   sH   

		r(   c                   @   sd   e Zd Zeee		ddejdejdejdejdejded	ejd
ejde	ej
 defddZdS )ChunkGatedDeltaRuleFunctionNFr   r   r   r   r   r   r   r   r   use_qk_l2norm_in_kernelc                 C   sR   |}|}|
rt |}t |}t|||||||||	d	\}}}}}}||j|fS )N)	r   r   r   r   r   r   r   r   r   )r	   r(   tor#   )ctxr   r   r   r   r   r   r   r   r   r*   q_origk_origr%   r   r   r   r$   r&   r&   r'   forwardM   s"   z#ChunkGatedDeltaRuleFunction.forward)NF)__name__
__module____qualname__staticmethodr   r   r!   Tensorfloatr   
LongTensorboolr/   r&   r&   r&   r'   r)   K   s6    	
r)   F
head_firstr*   c                 C   s  | j |j   kr|j ksJ  J | j tjksJ dt|jdks&J d|	r,td|durc| jd d	krBtd
| jd  d|durc|jd t|d	 krctdt|d	  d|jd  d|du rn|jd d }t	| |||||||||

\}}|	rt
|d}|d|fS )a  
    Args:
        q (torch.Tensor):
            queries of shape `[B, T, H, K]` if `head_first=False` else `[B, H, T, K]`.
        k (torch.Tensor):
            keys of shape `[B, T, H, K]` if `head_first=False` else `[B, H, T, K]`.
        v (torch.Tensor):
            values of shape `[B, T, H, V]` if `head_first=False` else `[B, H, T, V]`.
        g (torch.Tensor):
            (forget) gating tensor (in log space!) of shape `[B, T, H]` if `head_first=False` else `[B, H, T]`.
        beta (torch.Tensor):
            betas of shape `[B, T, H]` if `head_first=False` else `[B, H, T]`.
        scale (Optional[int]):
            Scale factor for the RetNet attention scores.
            If not provided, it will default to `1 / sqrt(K)`. Default: `None`.
        initial_state (Optional[torch.Tensor]):
            Initial state of shape `[N, H, K, V]` for `N` input sequences.
            For equal-length input sequences, `N` equals the batch size `B`.
            Default: `None`.
        output_final_state (Optional[bool]):
            Whether to output the final state of shape `[N, H, K, V]`. Default: `False`.
        cu_seqlens (torch.LongTensor):
            Cumulative sequence lengths of shape `[N+1]` used for variable-length training,
            consistent with the FlashAttention API.
        head_first (Optional[bool]):
            Whether the inputs are in the head-first format, which is not supported for variable-length inputs.
            Default: `False`.

    Returns:
        o (torch.Tensor):
            Outputs of shape `[B, T, H, V]` if `head_first=False` else `[B, H, T, V]`.
        final_state (torch.Tensor):
            Final state of shape `[N, H, K, V]` if `output_final_state=True` else `None`.

    Examples::
        >>> import torch
        >>> import torch.nn.functional as F
        >>> from einops import rearrange
        >>> from fla.ops.gated_delta_rule import chunk_gated_delta_rule
        # inputs with equal lengths
        >>> B, T, H, K, V = 4, 2048, 4, 512, 512
        >>> q = torch.randn(B, T, H, K, dtype=torch.bfloat16, device='cuda')
        >>> k = F.normalize(torch.randn(B, T, H, K, dtype=torch.bfloat16, device='cuda'), p=2, dim=-1)
        >>> v = torch.randn(B, T, H, V, dtype=torch.bfloat16, device='cuda')
        >>> beta = torch.rand(B, T, H, dtype=torch.bfloat16, device='cuda').sigmoid()
        >>> g = F.logsigmoid(torch.rand(B, T, H, dtype=torch.bfloat16, device='cuda'))
        >>> h0 = torch.randn(B, H, K, V, dtype=torch.bfloat16, device='cuda')
        >>> o, ht = chunk_gated_delta_rule(
            q, k, v, g, beta,
            initial_state=h0,
            output_final_state=True
        )
        # for variable-length inputs, the batch size `B` is expected to be 1 and `cu_seqlens` is required
        >>> q, k, v, beta, g = map(lambda x: rearrange(x, 'b t ... -> 1 (b t) ...'), (q, k, v, beta, g))
        # for a batch with 4 sequences, `cu_seqlens` with 5 start/end positions are expected
        >>> cu_seqlens = q.new_tensor([0, 2048, 4096, 6144, 8192], dtype=torch.long)
        >>> o_var, ht_var = chunk_gated_delta_rule(
            q, k, v, g, beta,
            initial_state=h0,
            output_final_state=True,
            cu_seqlens=cu_seqlens
        )
    zJChunkGatedDeltaRuleFunction does not support float32. Please use bfloat16.r    zLbeta must be of shape [B, T, H] if head_first=False, or [B, H, T] otherwise.znhead_first is deprecated and will be removed in a future version. Please use head_first=False for now instead.c                 S   s
   t | dS )Nzb h t ... -> b t h ...r   )xr&   r&   r'   <lambda>   s   
 z(chunk_gated_delta_rule.<locals>.<lambda>Nr      z/The batch size is expected to be 1 rather than zQ when using `cu_seqlens`.Please flatten variable-length inputs before processing.z]The number of initial states is expected to be equal to the number of input sequences, i.e., z rather than .g      zb t h ... -> b h t ...)r#   r!   r"   lenshapeDeprecationWarningmap
ValueErrorr)   applyr   )r   r   r   r   r   r   r   r   r   r8   r*   r%   r   r&   r&   r'   chunk_gated_delta_ruler   sT   "M


rD   )N)NNNNFF)"typingr   r!   einopsr   -sglang.srt.layers.attention.fla.chunk_delta_hr   'sglang.srt.layers.attention.fla.chunk_or   4sglang.srt.layers.attention.fla.chunk_scaled_dot_kktr   &sglang.srt.layers.attention.fla.cumsumr   &sglang.srt.layers.attention.fla.l2normr	   *sglang.srt.layers.attention.fla.solve_trilr
   %sglang.srt.layers.attention.fla.utilsr   r   r   'sglang.srt.layers.attention.fla.wy_fastr   r4   r5   r6   r(   autogradFunctionr)   compilerdisabler7   rD   r&   r&   r&   r'   <module>   s|   	
1'	
