core.fusions.fused_mla_yarn_rope_apply#
Module Contents#
Classes#
Autograd function for applying RoPE inplace to the trailing emb_dim elements of a multi-head tensor (leaving the first nope_dim elements unchanged). |
|
Autograd function for applying RoPE to MLA’s key and value. Splits KV, applies RoPE to k_pos_emb, concatenates onto key. |
Functions#
Forward pass: apply RoPE inplace to the trailing emb_dim elements. Reads from interleaved layout, writes back to interleaved layout. |
|
Backward pass: inverse RoPE inplace on the trailing emb_dim elements. Reads from interleaved layout, writes to interleaved layout. |
|
Fused RoPE applied inplace to the trailing emb_dim elements of a tensor, leaving the first nope_dim elements unchanged. It supports both sbhd and thd input formats. |
|
Apply the fused RoPE kernel without modifying the input tensor. |
|
Forward pass: split KV into key and value, apply RoPE to k_pos_emb, and concatenate the result onto key. |
|
Backward pass for the KV-split RoPE. |
|
Fused function for applying RoPE to MLA’s key and value. It splits the input tensor kv into key and value, and concatenates the processed RoPE to the key. |
|
Backward-compatible in-place MLA query RoPE API. |
|
Backward-compatible name for the MLA key/value split RoPE API. |
Data#
API#
- core.fusions.fused_mla_yarn_rope_apply._get_thd_token_idx(cu_seqlens, pid_m, seq_num, cp_rank, cp_size)#
- core.fusions.fused_mla_yarn_rope_apply._mla_rope_fwd_inplace_kernel(
- Q,
- COS,
- SIN,
- nope_dim,
- emb_dim: triton.language.constexpr,
- head_num: triton.language.constexpr,
- batch_size,
- seq_num,
- cu_seqlens_q,
- position_ids,
- stride_x_seq,
- stride_x_nheads,
- stride_cos_seq,
- stride_sin_seq,
- cp_rank,
- cp_size,
- INVERSE: triton.language.constexpr,
- REMOVE_INTERLEAVING: triton.language.constexpr,
- BLOCK_H: triton.language.constexpr,
Forward pass: apply RoPE inplace to the trailing emb_dim elements. Reads from interleaved layout, writes back to interleaved layout.
Input: Q: [seq_len, batch_size, head_num, nope_dim + emb_dim] or [total_seq_len, head_num, nope_dim + emb_dim] COS/SIN: [max_seq_len, emb_dim]
batch_size: batch size for sbhd format, not used for thd format seq_num: number of sequences for thd format, not used for sbhd format cu_seqlens_q: [seq_num + 1] accumulated sequence lengths for thd format
- core.fusions.fused_mla_yarn_rope_apply._autotuned_mla_rope_fwd_inplace_kernel#
‘(…)’
- core.fusions.fused_mla_yarn_rope_apply._mla_rope_bwd_inplace_kernel(
- DO,
- COS,
- SIN,
- nope_dim,
- emb_dim: triton.language.constexpr,
- head_num: triton.language.constexpr,
- batch_size,
- seq_num,
- cu_seqlens_q,
- position_ids,
- stride_x_seq,
- stride_x_nheads,
- stride_cos_seq,
- stride_sin_seq,
- cp_rank,
- cp_size,
- INVERSE: triton.language.constexpr,
- REMOVE_INTERLEAVING: triton.language.constexpr,
- BLOCK_H: triton.language.constexpr,
Backward pass: inverse RoPE inplace on the trailing emb_dim elements. Reads from interleaved layout, writes to interleaved layout.
Input: DO: [seq_len, batch_size, head_num, nope_dim + emb_dim] or [total_seq_len, head_num, nope_dim + emb_dim] COS/SIN: [max_seq_len, emb_dim]
batch_size, seq_num, and cu_seqlens_q are the same as in the forward pass
- core.fusions.fused_mla_yarn_rope_apply._autotuned_mla_rope_bwd_inplace_kernel#
‘(…)’
- class core.fusions.fused_mla_yarn_rope_apply._FusedMLARoPEInplace#
Bases:
torch.autograd.FunctionAutograd function for applying RoPE inplace to the trailing emb_dim elements of a multi-head tensor (leaving the first nope_dim elements unchanged).
- static forward(
- ctx,
- q,
- cos,
- sin,
- nope_dim,
- emb_dim,
- cu_seqlens_q,
- cp_rank,
- cp_size,
- rotary_interleaved=False,
- inverse=False,
- remove_interleaving=False,
- position_ids=None,
Forward function for _FusedMLARoPEInplace.
- Parameters:
q – [seq_len, batch_size, head_num, nope_dim + emb_dim] or [total_seq_len, head_num, nope_dim + emb_dim]
cos/sin – [max_seq_len, 1, 1, emb_dim]
cu_seqlens_q – [seq_num + 1] accumulated sequence lengths for thd format
rotary_interleaved – whether to apply RoPE interleaved, only supports False for now
inverse – if True, negate sin inside the kernel to apply the inverse rotation
- static backward(ctx, grad)#
Backward function for _FusedMLARoPEInplace.
- Parameters:
grad – [seq_len, batch_size, head_num, nope_dim + emb_dim] or [total_seq_len, head_num, nope_dim + emb_dim]
- core.fusions.fused_mla_yarn_rope_apply.fused_mla_rope_inplace(
- t: torch.Tensor,
- cos: torch.Tensor,
- sin: torch.Tensor,
- nope_dim: int,
- emb_dim: int,
- cu_seqlens_q: Optional[torch.Tensor] = None,
- cp_rank: int = 0,
- cp_size: int = 1,
- rotary_interleaved: bool = False,
- inverse: bool = False,
- remove_interleaving: bool = False,
- position_ids: Optional[torch.Tensor] = None,
Fused RoPE applied inplace to the trailing emb_dim elements of a tensor, leaving the first nope_dim elements unchanged. It supports both sbhd and thd input formats.
When
inverse=Truethe rotation is reversed, which is useful for undoing RoPE on the attention output.For the notations below, seq_len is the length of the sequence per batch for sbhd format, total_seq_len is the total length of the sequences for thd format. max_seq_len is the maximum length of the sequences in the input tensor.
- Parameters:
t – [seq_len, batch_size, head_num, nope_dim + emb_dim] or [total_seq_len, head_num, nope_dim + emb_dim]
cos/sin – [max_seq_len, 1, 1, emb_dim]
cu_seqlens_q – [seq_num + 1] accumulated sequence lengths for thd format
rotary_interleaved – whether to apply RoPE interleaved, only supports False for now
inverse – if True, apply the inverse rotation
remove_interleaving – if True, output RoPE dims in non-interleaved layout
position_ids – optional THD row positions. When supplied, these positions replace the built-in CP row-to-position mapping.
- Returns:
inplace modified input tensor
- Return type:
t
- core.fusions.fused_mla_yarn_rope_apply.fused_mla_rope_out_of_place(
- t: torch.Tensor,
- cos: torch.Tensor,
- sin: torch.Tensor,
- nope_dim: int,
- emb_dim: int,
- cu_seqlens_q: Optional[torch.Tensor] = None,
- cp_rank: int = 0,
- cp_size: int = 1,
- rotary_interleaved: bool = False,
- inverse: bool = False,
- remove_interleaving: bool = False,
- position_ids: Optional[torch.Tensor] = None,
Apply the fused RoPE kernel without modifying the input tensor.
Use this wrapper when an upstream autograd function may have retained its output for backward. The underlying kernel remains in-place, so a private copy is required to keep the retained tensor unchanged.
- core.fusions.fused_mla_yarn_rope_apply._mla_rope_fwd_kv_split_kernel(
- KV,
- K_POS_EMB,
- O_KEY,
- O_VALUE,
- COS,
- SIN,
- emb_dim: triton.language.constexpr,
- k_dim: triton.language.constexpr,
- v_dim: triton.language.constexpr,
- head_num: triton.language.constexpr,
- batch_size,
- seq_num,
- cu_seqlens_kv,
- stride_kv_seq,
- stride_kv_nheads,
- stride_emb_seq,
- stride_k_seq,
- stride_k_nheads,
- stride_v_seq,
- stride_v_nheads,
- cp_rank,
- cp_size,
- REMOVE_INTERLEAVING: triton.language.constexpr,
- BLOCK_H: triton.language.constexpr,
Forward pass: split KV into key and value, apply RoPE to k_pos_emb, and concatenate the result onto key.
Input: KV: [seq_len, batch_size, head_num, k_dim + v_dim] or [total_seq_len, head_num, k_dim + v_dim] K_POS_EMB: [seq_len, batch_size, emb_dim] or [total_seq_len, emb_dim] COS/SIN: [max_seq_len, emb_dim]
batch_size: batch size for sbhd format, not used for thd format seq_num: number of sequences for thd format, not used for sbhd format cu_seqlens_kv: [seq_num + 1] accumulated sequence lengths for thd format
Output: O_KEY: [seq_len, batch_size, head_num, emb_dim + k_dim] or [total_seq_len, head_num, emb_dim + k_dim] O_VALUE: [seq_len, batch_size, head_num, v_dim] or [total_seq_len, head_num, v_dim]
- core.fusions.fused_mla_yarn_rope_apply._mla_rope_bwd_kv_split_kernel(
- dK,
- dV,
- dKV,
- dEMB,
- COS,
- SIN,
- emb_dim: triton.language.constexpr,
- k_dim: triton.language.constexpr,
- v_dim: triton.language.constexpr,
- head_num: triton.language.constexpr,
- batch_size,
- seq_num,
- cu_seqlens_kv,
- stride_dk_seq,
- stride_dk_nheads,
- stride_dv_seq,
- stride_dv_nheads,
- stride_dkv_seq,
- stride_dkv_nheads,
- stride_demb_seq,
- cp_rank,
- cp_size,
- REMOVE_INTERLEAVING: triton.language.constexpr,
- BLOCK_H: triton.language.constexpr,
Backward pass for the KV-split RoPE.
Input: dK: [seq_len, batch_size, head_num, emb_dim + k_dim] or [total_seq_len, head_num, emb_dim + k_dim] dV: [seq_len, batch_size, head_num, v_dim] or [total_seq_len, head_num, v_dim] COS/SIN: [max_seq_len, emb_dim]
batch_size, seq_num, and cu_seqlens_kv are the same as in the forward pass
Output: dKV: [seq_len, batch_size, head_num, k_dim + v_dim] or [total_seq_len, head_num, k_dim + v_dim] dEMB: [seq_len, batch_size, emb_dim] or [total_seq_len, emb_dim]
- class core.fusions.fused_mla_yarn_rope_apply._FusedMLARoPEKVSplit#
Bases:
torch.autograd.FunctionAutograd function for applying RoPE to MLA’s key and value. Splits KV, applies RoPE to k_pos_emb, concatenates onto key.
- static forward(
- ctx,
- kv,
- k_pos_emb,
- cos,
- sin,
- emb_dim,
- k_dim,
- v_dim,
- cu_seqlens_kv,
- cp_rank,
- cp_size,
- rotary_interleaved=False,
- remove_interleaving=False,
Forward function for _FusedMLARoPEKVSplit.
- Parameters:
kv – [seq_len, batch_size, head_num, k_dim + v_dim] or [total_seq_len, head_num, k_dim + v_dim]
k_pos_emb – [seq_len, batch_size, 1, emb_dim] or [total_seq_len, 1, emb_dim]
cos/sin – [max_seq_len, 1, 1, emb_dim]
cu_seqlens_kv – [seq_num + 1] accumulated sequence lengths for thd format
rotary_interleaved – whether to apply RoPE interleaved, only supports False for now
- static backward(ctx, dk, dv)#
Backward function for _FusedMLARoPEKVSplit.
- Parameters:
dk – [seq_len, batch_size, head_num, emb_dim + k_dim] or [total_seq_len, head_num, emb_dim + k_dim]
dv – [seq_len, batch_size, head_num, v_dim] or [total_seq_len, head_num, v_dim]
- core.fusions.fused_mla_yarn_rope_apply.fused_mla_rope_kv_split(
- kv: torch.Tensor,
- k_pos_emb: torch.Tensor,
- cos: torch.Tensor,
- sin: torch.Tensor,
- emb_dim: int,
- k_dim: int,
- v_dim: int,
- cu_seqlens_kv: Optional[torch.Tensor] = None,
- cp_rank: int = 0,
- cp_size: int = 1,
- rotary_interleaved: bool = False,
- remove_interleaving: bool = False,
Fused function for applying RoPE to MLA’s key and value. It splits the input tensor kv into key and value, and concatenates the processed RoPE to the key.
For the notations below, seq_len is the length of sequence per batch for sbhd format, total_seq_len is the total length of the sequences for thd format. max_seq_len is the maximum length of the sequences in the input tensor.
- Parameters:
kv – [seq_len, batch_size, head_num, k_dim + v_dim] or [total_seq_len, head_num, k_dim + v_dim]
k_pos_emb – [seq_len, batch_size, 1, emb_dim] or [total_seq_len, 1, emb_dim]
cos/sin – [max_seq_len, 1, 1, emb_dim]
cu_seqlens_kv – [seq_num + 1] accumulated sequence lengths for thd format
rotary_interleaved – whether to apply RoPE interleaved, only supports False for now
remove_interleaving – if True, output RoPE dims in non-interleaved layout
- Returns:
[seq_len, batch_size, head_num, emb_dim + k_dim] or [total_seq_len, head_num, emb_dim + k_dim] value: [seq_len, batch_size, head_num, v_dim] or [total_seq_len, head_num, v_dim]
- Return type:
- core.fusions.fused_mla_yarn_rope_apply.fused_apply_mla_rope_for_q(
- t: torch.Tensor,
- cos: torch.Tensor,
- sin: torch.Tensor,
- qk_head_dim: int,
- emb_dim: int,
- cu_seqlens_q: Optional[torch.Tensor] = None,
- cp_rank: int = 0,
- cp_size: int = 1,
- rotary_interleaved: bool = False,
Backward-compatible in-place MLA query RoPE API.
New callers should choose :func:
fused_mla_rope_inplaceor- Func:
fused_mla_rope_out_of_placeexplicitly. This legacy name keeps its original mutation behavior and does not add a clone to the hot path.
- core.fusions.fused_mla_yarn_rope_apply.fused_apply_mla_rope_for_kv(
- kv: torch.Tensor,
- k_pos_emb: torch.Tensor,
- cos: torch.Tensor,
- sin: torch.Tensor,
- emb_dim: int,
- k_dim: int,
- v_dim: int,
- cu_seqlens_kv: Optional[torch.Tensor] = None,
- cp_rank: int = 0,
- cp_size: int = 1,
- rotary_interleaved: bool = False,
Backward-compatible name for the MLA key/value split RoPE API.