mirror of
https://github.com/FlashML-org/FreeToken.git
synced 2026-11-21 01:52:30 +08:00
Hybrid 45-layer model (34 KDA + 11 DSA) with NVFP4 MoE (288 experts): - KDA linear-attention kernels and op, hybrid-linear pool dispatch generalized to any paged family - NoPE-MLA attention with a kpool DSA indexer backend: 64-token pages, 1/ratio shadow index slab, per-request tail rings, CUDA-graph-safe decode, prefix-cache snapshot contract - mHC (x4 residual streams) layer with a fused split-K triton kernel; torch reference kept as the semantic pin - clamped-SwiGLU activation across the MoE stack - weight loading for NVFP4 exports in the multimodal-wrapper layout: ModelOpt and compressed-tensors (RedHatAI) tensor kinds; Co-authored-by: Shuo Yang <73746844+andy-yang-1@users.noreply.github.com>
1397 lines
40 KiB
Python
1397 lines
40 KiB
Python
# SPDX-License-Identifier: Apache-2.0
|
|
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
|
# SPDX-FileCopyrightText: Songlin Yang, Yu Zhang
|
|
#
|
|
# This file contains code copied from the flash-linear-attention project.
|
|
# The original source code was licensed under the MIT license and included
|
|
# the following copyright notice:
|
|
# Copyright (c) 2023-2025, Songlin Yang, Yu Zhang
|
|
# ruff: noqa: E501
|
|
|
|
|
|
# Vendored from vLLM's third_party/flash_linear_attention (PR #53906, commit 933876c3),
|
|
# itself copied from the flash-linear-attention project (MIT, (c) 2023-2025 Songlin Yang,
|
|
# Yu Zhang). Imports are remapped onto freetoken.kernel.fla's shared helpers; keep this
|
|
# file in sync with upstream when pulling KDA kernel fixes.
|
|
import torch
|
|
import triton
|
|
import triton.language as tl
|
|
|
|
from freetoken.kernel.fla.cumsum import chunk_local_cumsum
|
|
from freetoken.kernel.fla.fused_recurrent import (
|
|
fused_recurrent_gated_delta_rule_fwd_kernel,
|
|
)
|
|
from freetoken.kernel.fla.index import prepare_chunk_indices
|
|
from freetoken.kernel.fla.kda_chunk_delta_h import chunk_gated_delta_rule_fwd_h
|
|
from freetoken.kernel.fla.l2norm import l2norm_fwd
|
|
from freetoken.kernel.fla.op import exp2, log
|
|
from freetoken.kernel.fla.solve_tril import solve_tril
|
|
from freetoken.kernel.fla.utils import FLA_CHUNK_SIZE, is_amd
|
|
|
|
RCP_LN2 = 1.4426950408889634 # 1 / ln(2)
|
|
cdiv = triton.cdiv
|
|
next_power_of_2 = triton.next_power_of_2
|
|
|
|
BT_LIST_AUTOTUNE = [32, 64, 128]
|
|
NUM_WARPS_AUTOTUNE = [2, 4, 8, 16] if is_amd else [4, 8, 16, 32]
|
|
|
|
|
|
def fused_recurrent_kda_fwd(
|
|
q: torch.Tensor,
|
|
k: torch.Tensor,
|
|
v: torch.Tensor,
|
|
g: torch.Tensor,
|
|
beta: torch.Tensor,
|
|
scale: float,
|
|
initial_state: torch.Tensor,
|
|
inplace_final_state: bool = True,
|
|
cu_seqlens: torch.Tensor | None = None,
|
|
ssm_state_indices: torch.Tensor | None = None,
|
|
num_accepted_tokens: torch.Tensor | None = None,
|
|
use_qk_l2norm_in_kernel: bool = False,
|
|
out: torch.Tensor | None = None,
|
|
sigmoid_beta: bool = False,
|
|
a_log: torch.Tensor | None = None,
|
|
g_bias: torch.Tensor | None = None,
|
|
compute_gate: bool = False,
|
|
lower_bound: float | None = -5.0,
|
|
) -> tuple[torch.Tensor, torch.Tensor]:
|
|
B, T, H, K, V = *k.shape, v.shape[-1]
|
|
HV = v.shape[2]
|
|
N = B if cu_seqlens is None else len(cu_seqlens) - 1
|
|
BK, BV = next_power_of_2(K), min(next_power_of_2(V), 8)
|
|
NK, NV = cdiv(K, BK), cdiv(V, BV)
|
|
assert NK == 1, "NK > 1 is not supported yet"
|
|
num_stages = 3
|
|
num_warps = 1
|
|
|
|
if compute_gate:
|
|
assert a_log is not None and g_bias is not None, (
|
|
"compute_gate requires a_log and g_bias"
|
|
)
|
|
assert lower_bound is not None, (
|
|
"compute_gate implements the bounded (safe_gate) branch only"
|
|
)
|
|
a_log = a_log.reshape(-1).contiguous()
|
|
g_bias = g_bias.reshape(-1).contiguous()
|
|
|
|
if out is None:
|
|
o = torch.empty_like(k)
|
|
else:
|
|
# Caller-provided output buffer; must be layout-compatible with the
|
|
# tensor the kernel indexes (contiguous, same shape/dtype as k).
|
|
assert out.shape == k.shape and out.dtype == k.dtype
|
|
assert out.is_contiguous()
|
|
o = out
|
|
if inplace_final_state:
|
|
final_state = initial_state
|
|
else:
|
|
final_state = q.new_empty(T, HV, V, K, dtype=initial_state.dtype)
|
|
|
|
stride_init_state_token = initial_state.stride(0)
|
|
stride_final_state_token = final_state.stride(0)
|
|
|
|
if ssm_state_indices is None:
|
|
stride_indices_seq, stride_indices_tok = 1, 1
|
|
elif ssm_state_indices.ndim == 1:
|
|
stride_indices_seq, stride_indices_tok = ssm_state_indices.stride(0), 1
|
|
else:
|
|
stride_indices_seq, stride_indices_tok = ssm_state_indices.stride()
|
|
|
|
grid = (NK, NV, N * HV)
|
|
fused_recurrent_gated_delta_rule_fwd_kernel[grid](
|
|
q=q,
|
|
k=k,
|
|
v=v,
|
|
g=g,
|
|
beta=beta,
|
|
o=o,
|
|
h0=initial_state,
|
|
ht=final_state,
|
|
cu_seqlens=cu_seqlens,
|
|
ssm_state_indices=ssm_state_indices,
|
|
num_accepted_tokens=num_accepted_tokens,
|
|
scale=scale,
|
|
N=N,
|
|
T=T,
|
|
B=B,
|
|
H=H,
|
|
HV=HV,
|
|
K=K,
|
|
V=V,
|
|
BK=BK,
|
|
BV=BV,
|
|
stride_init_state_token=stride_init_state_token,
|
|
stride_final_state_token=stride_final_state_token,
|
|
stride_indices_seq=stride_indices_seq,
|
|
stride_indices_tok=stride_indices_tok,
|
|
IS_BETA_HEADWISE=beta.ndim == v.ndim,
|
|
USE_QK_L2NORM_IN_KERNEL=use_qk_l2norm_in_kernel,
|
|
INPLACE_FINAL_STATE=inplace_final_state,
|
|
IS_KDA=True,
|
|
SIGMOID_BETA=sigmoid_beta,
|
|
a_log=a_log,
|
|
g_bias=g_bias,
|
|
COMPUTE_GATE=compute_gate,
|
|
SAFE_GATE=True,
|
|
LOWER_BOUND=lower_bound if lower_bound is not None else -5.0,
|
|
num_warps=num_warps,
|
|
num_stages=num_stages,
|
|
)
|
|
|
|
return o, final_state
|
|
|
|
|
|
def fused_recurrent_kda(
|
|
q: torch.Tensor,
|
|
k: torch.Tensor,
|
|
v: torch.Tensor,
|
|
g: torch.Tensor,
|
|
beta: torch.Tensor = None,
|
|
scale: float = None,
|
|
initial_state: torch.Tensor = None,
|
|
inplace_final_state: bool = True,
|
|
use_qk_l2norm_in_kernel: bool = True,
|
|
cu_seqlens: torch.Tensor | None = None,
|
|
ssm_state_indices: torch.LongTensor | None = None,
|
|
num_accepted_tokens: torch.Tensor | None = None,
|
|
out: torch.Tensor | None = None,
|
|
sigmoid_beta: bool = False,
|
|
a_log: torch.Tensor | None = None,
|
|
g_bias: torch.Tensor | None = None,
|
|
compute_gate: bool = False,
|
|
lower_bound: float | None = -5.0,
|
|
**kwargs,
|
|
) -> tuple[torch.Tensor, torch.Tensor]:
|
|
if cu_seqlens is not None and q.shape[0] != 1:
|
|
raise ValueError(
|
|
f"The batch size is expected to be 1 rather than {q.shape[0]} when using `cu_seqlens`."
|
|
f"Please flatten variable-length inputs before processing."
|
|
)
|
|
if scale is None:
|
|
scale = k.shape[-1] ** -0.5
|
|
|
|
o, final_state = fused_recurrent_kda_fwd(
|
|
q=q.contiguous(),
|
|
k=k.contiguous(),
|
|
v=v.contiguous(),
|
|
g=g.contiguous(),
|
|
beta=beta.contiguous(),
|
|
scale=scale,
|
|
initial_state=initial_state,
|
|
inplace_final_state=inplace_final_state,
|
|
cu_seqlens=cu_seqlens,
|
|
ssm_state_indices=ssm_state_indices,
|
|
num_accepted_tokens=num_accepted_tokens,
|
|
use_qk_l2norm_in_kernel=use_qk_l2norm_in_kernel,
|
|
out=out,
|
|
sigmoid_beta=sigmoid_beta,
|
|
a_log=a_log,
|
|
g_bias=g_bias,
|
|
compute_gate=compute_gate,
|
|
lower_bound=lower_bound,
|
|
)
|
|
return o, final_state
|
|
|
|
|
|
@triton.heuristics({"IS_VARLEN": lambda args: args["cu_seqlens"] is not None})
|
|
@triton.autotune(
|
|
configs=[
|
|
triton.Config({"BK": BK}, num_warps=num_warps, num_stages=num_stages)
|
|
for BK in [32, 64]
|
|
for num_warps in [1, 2, 4, 8]
|
|
for num_stages in [2, 3, 4]
|
|
],
|
|
key=["BC"],
|
|
)
|
|
@triton.jit(do_not_specialize=["T"])
|
|
def chunk_kda_scaled_dot_kkt_fwd_kernel_intra_sub_inter(
|
|
q,
|
|
k,
|
|
g,
|
|
beta,
|
|
A,
|
|
Aqk,
|
|
scale,
|
|
cu_seqlens,
|
|
chunk_indices,
|
|
T,
|
|
H: tl.constexpr,
|
|
K: tl.constexpr,
|
|
BT: tl.constexpr,
|
|
BC: tl.constexpr,
|
|
BK: tl.constexpr,
|
|
NC: tl.constexpr,
|
|
IS_VARLEN: tl.constexpr,
|
|
):
|
|
i_t, i_c, i_bh = tl.program_id(0), tl.program_id(1), tl.program_id(2)
|
|
i_b, i_h = i_bh // H, i_bh % H
|
|
i_i, i_j = i_c // NC, i_c % NC
|
|
if IS_VARLEN:
|
|
i_n, i_t = (
|
|
tl.load(chunk_indices + i_t * 2).to(tl.int32),
|
|
tl.load(chunk_indices + i_t * 2 + 1).to(tl.int32),
|
|
)
|
|
bos, eos = (
|
|
tl.load(cu_seqlens + i_n).to(tl.int32),
|
|
tl.load(cu_seqlens + i_n + 1).to(tl.int32),
|
|
)
|
|
T = eos - bos
|
|
else:
|
|
bos, eos = i_b * T, i_b * T + T
|
|
|
|
if i_t * BT + i_i * BC >= T:
|
|
return
|
|
if i_i <= i_j:
|
|
return
|
|
|
|
q += (bos * H + i_h) * K
|
|
k += (bos * H + i_h) * K
|
|
g += (bos * H + i_h) * K
|
|
A += (bos * H + i_h) * BT
|
|
Aqk += (bos * H + i_h) * BT
|
|
|
|
p_b = tl.make_block_ptr(
|
|
beta + bos * H + i_h, (T,), (H,), (i_t * BT + i_i * BC,), (BC,), (0,)
|
|
)
|
|
b_b = tl.load(p_b, boundary_check=(0,))
|
|
|
|
b_A = tl.zeros([BC, BC], dtype=tl.float32)
|
|
b_Aqk = tl.zeros([BC, BC], dtype=tl.float32)
|
|
for i_k in range(tl.cdiv(K, BK)):
|
|
p_q = tl.make_block_ptr(
|
|
q, (T, K), (H * K, 1), (i_t * BT + i_i * BC, i_k * BK), (BC, BK), (1, 0)
|
|
)
|
|
p_k = tl.make_block_ptr(
|
|
k, (T, K), (H * K, 1), (i_t * BT + i_i * BC, i_k * BK), (BC, BK), (1, 0)
|
|
)
|
|
p_g = tl.make_block_ptr(
|
|
g, (T, K), (H * K, 1), (i_t * BT + i_i * BC, i_k * BK), (BC, BK), (1, 0)
|
|
)
|
|
b_kt = tl.make_block_ptr(
|
|
k, (K, T), (1, H * K), (i_k * BK, i_t * BT + i_j * BC), (BK, BC), (0, 1)
|
|
)
|
|
p_gk = tl.make_block_ptr(
|
|
g, (K, T), (1, H * K), (i_k * BK, i_t * BT + i_j * BC), (BK, BC), (0, 1)
|
|
)
|
|
|
|
o_k = i_k * BK + tl.arange(0, BK)
|
|
m_k = o_k < K
|
|
# [BK,]
|
|
b_gn = tl.load(g + (i_t * BT + i_i * BC) * H * K + o_k, mask=m_k, other=0)
|
|
# [BC, BK]
|
|
b_g = tl.load(p_g, boundary_check=(0, 1))
|
|
b_k = tl.load(p_k, boundary_check=(0, 1)) * exp2(b_g - b_gn[None, :])
|
|
# [BK, BC]
|
|
b_gk = tl.load(p_gk, boundary_check=(0, 1))
|
|
b_kt = tl.load(b_kt, boundary_check=(0, 1))
|
|
# [BC, BC]
|
|
b_ktg = b_kt * exp2(b_gn[:, None] - b_gk)
|
|
b_A += tl.dot(b_k, b_ktg)
|
|
|
|
b_q = tl.load(p_q, boundary_check=(0, 1))
|
|
b_qg = b_q * exp2(b_g - b_gn[None, :]) * scale
|
|
b_Aqk += tl.dot(b_qg, b_ktg)
|
|
|
|
b_A *= b_b[:, None]
|
|
|
|
p_A = tl.make_block_ptr(
|
|
A, (T, BT), (H * BT, 1), (i_t * BT + i_i * BC, i_j * BC), (BC, BC), (1, 0)
|
|
)
|
|
tl.store(p_A, b_A.to(A.dtype.element_ty), boundary_check=(0, 1))
|
|
p_Aqk = tl.make_block_ptr(
|
|
Aqk, (T, BT), (H * BT, 1), (i_t * BT + i_i * BC, i_j * BC), (BC, BC), (1, 0)
|
|
)
|
|
tl.store(p_Aqk, b_Aqk.to(Aqk.dtype.element_ty), boundary_check=(0, 1))
|
|
|
|
|
|
@triton.heuristics({"IS_VARLEN": lambda args: args["cu_seqlens"] is not None})
|
|
@triton.autotune(
|
|
configs=[triton.Config({}, num_warps=num_warps) for num_warps in [1, 2, 4, 8]],
|
|
key=["BK", "BT"],
|
|
)
|
|
@triton.jit(do_not_specialize=["T"])
|
|
def chunk_kda_scaled_dot_kkt_fwd_kernel_intra_sub_intra(
|
|
q,
|
|
k,
|
|
g,
|
|
beta,
|
|
A,
|
|
Aqk,
|
|
scale,
|
|
cu_seqlens,
|
|
chunk_indices,
|
|
T,
|
|
H: tl.constexpr,
|
|
K: tl.constexpr,
|
|
BT: tl.constexpr,
|
|
BC: tl.constexpr,
|
|
BK: tl.constexpr,
|
|
IS_VARLEN: tl.constexpr,
|
|
):
|
|
i_t, i_i, i_bh = tl.program_id(0), tl.program_id(1), tl.program_id(2)
|
|
i_b, i_h = i_bh // H, i_bh % H
|
|
if IS_VARLEN:
|
|
i_n, i_t = (
|
|
tl.load(chunk_indices + i_t * 2).to(tl.int32),
|
|
tl.load(chunk_indices + i_t * 2 + 1).to(tl.int32),
|
|
)
|
|
bos, eos = (
|
|
tl.load(cu_seqlens + i_n).to(tl.int32),
|
|
tl.load(cu_seqlens + i_n + 1).to(tl.int32),
|
|
)
|
|
T = eos - bos
|
|
else:
|
|
bos, eos = i_b * T, i_b * T + T
|
|
|
|
if i_t * BT + i_i * BC >= T:
|
|
return
|
|
|
|
o_i = tl.arange(0, BC)
|
|
o_k = tl.arange(0, BK)
|
|
m_k = o_k < K
|
|
m_A = (i_t * BT + i_i * BC + o_i) < T
|
|
o_A = (bos + i_t * BT + i_i * BC + o_i) * H * BT + i_h * BT + i_i * BC
|
|
|
|
p_q = tl.make_block_ptr(
|
|
q + (bos * H + i_h) * K,
|
|
(T, K),
|
|
(H * K, 1),
|
|
(i_t * BT + i_i * BC, 0),
|
|
(BC, BK),
|
|
(1, 0),
|
|
)
|
|
p_k = tl.make_block_ptr(
|
|
k + (bos * H + i_h) * K,
|
|
(T, K),
|
|
(H * K, 1),
|
|
(i_t * BT + i_i * BC, 0),
|
|
(BC, BK),
|
|
(1, 0),
|
|
)
|
|
p_g = tl.make_block_ptr(
|
|
g + (bos * H + i_h) * K,
|
|
(T, K),
|
|
(H * K, 1),
|
|
(i_t * BT + i_i * BC, 0),
|
|
(BC, BK),
|
|
(1, 0),
|
|
)
|
|
b_q = tl.load(p_q, boundary_check=(0, 1))
|
|
b_k = tl.load(p_k, boundary_check=(0, 1))
|
|
b_g = tl.load(p_g, boundary_check=(0, 1))
|
|
|
|
p_b = beta + (bos + i_t * BT + i_i * BC + o_i) * H + i_h
|
|
b_k = b_k * tl.load(p_b, mask=m_A, other=0)[:, None]
|
|
|
|
p_kt = k + (bos + i_t * BT + i_i * BC) * H * K + i_h * K + o_k
|
|
p_gk = g + (bos + i_t * BT + i_i * BC) * H * K + i_h * K + o_k
|
|
|
|
for j in range(0, min(BC, T - i_t * BT - i_i * BC)):
|
|
b_kt = tl.load(p_kt, mask=m_k, other=0).to(tl.float32)
|
|
b_gk = tl.load(p_gk, mask=m_k, other=0).to(tl.float32)
|
|
b_ktg = b_kt[None, :] * exp2(b_g - b_gk[None, :])
|
|
b_A = tl.sum(b_k * b_ktg, 1)
|
|
b_A = tl.where(o_i > j, b_A, 0.0)
|
|
b_Aqk = tl.sum(b_q * b_ktg, 1)
|
|
b_Aqk = tl.where(o_i >= j, b_Aqk * scale, 0.0)
|
|
tl.store(A + o_A + j, b_A, mask=m_A)
|
|
tl.store(Aqk + o_A + j, b_Aqk, mask=m_A)
|
|
p_kt += H * K
|
|
p_gk += H * K
|
|
|
|
|
|
def chunk_kda_scaled_dot_kkt_fwd(
|
|
q: torch.Tensor,
|
|
k: torch.Tensor,
|
|
gk: torch.Tensor | None = None,
|
|
beta: torch.Tensor | None = None,
|
|
scale: float | None = None,
|
|
cu_seqlens: torch.Tensor | None = None,
|
|
chunk_indices: torch.Tensor | None = None,
|
|
chunk_size: int = FLA_CHUNK_SIZE,
|
|
output_dtype: torch.dtype = torch.float32,
|
|
) -> tuple[torch.Tensor, torch.Tensor]:
|
|
r"""
|
|
Compute beta * K * K^T.
|
|
|
|
Args:
|
|
k (torch.Tensor):
|
|
The key tensor of shape `[B, T, H, K]`.
|
|
beta (torch.Tensor):
|
|
The beta tensor of shape `[B, T, H]`.
|
|
gk (torch.Tensor):
|
|
The cumulative sum of the gate tensor of shape `[B, T, H, K]` applied to the key tensor. Default: `None`.
|
|
cu_seqlens (torch.Tensor):
|
|
The cumulative sequence lengths of the input tensor.
|
|
Default: None
|
|
chunk_size (int):
|
|
The chunk size. Default: 64.
|
|
output_dtype (torch.dtype):
|
|
The dtype of the output tensor. Default: `torch.float32`
|
|
|
|
Returns:
|
|
beta * K * K^T of shape `[B, T, H, BT]` where `BT` is the chunk size.
|
|
"""
|
|
B, T, H, K = k.shape
|
|
assert K <= 256
|
|
BT = chunk_size
|
|
if chunk_indices is None and cu_seqlens is not None:
|
|
chunk_indices = prepare_chunk_indices(cu_seqlens, BT)
|
|
NT = cdiv(T, BT) if cu_seqlens is None else len(chunk_indices)
|
|
|
|
BC = min(16, BT)
|
|
NC = cdiv(BT, BC)
|
|
BK = max(next_power_of_2(K), 16)
|
|
A = torch.zeros(B, T, H, BT, device=k.device, dtype=output_dtype)
|
|
Aqk = torch.zeros(B, T, H, BT, device=k.device, dtype=output_dtype)
|
|
grid = (NT, NC * NC, B * H)
|
|
chunk_kda_scaled_dot_kkt_fwd_kernel_intra_sub_inter[grid](
|
|
q=q,
|
|
k=k,
|
|
g=gk,
|
|
beta=beta,
|
|
A=A,
|
|
Aqk=Aqk,
|
|
scale=scale,
|
|
cu_seqlens=cu_seqlens,
|
|
chunk_indices=chunk_indices,
|
|
T=T,
|
|
H=H,
|
|
K=K,
|
|
BT=BT,
|
|
BC=BC,
|
|
NC=NC,
|
|
)
|
|
|
|
grid = (NT, NC, B * H)
|
|
chunk_kda_scaled_dot_kkt_fwd_kernel_intra_sub_intra[grid](
|
|
q=q,
|
|
k=k,
|
|
g=gk,
|
|
beta=beta,
|
|
A=A,
|
|
Aqk=Aqk,
|
|
scale=scale,
|
|
cu_seqlens=cu_seqlens,
|
|
chunk_indices=chunk_indices,
|
|
T=T,
|
|
H=H,
|
|
K=K,
|
|
BT=BT,
|
|
BC=BC,
|
|
BK=BK,
|
|
)
|
|
return A, Aqk
|
|
|
|
|
|
@triton.heuristics(
|
|
{
|
|
"STORE_QG": lambda args: args["qg"] is not None,
|
|
"STORE_KG": lambda args: args["kg"] is not None,
|
|
"IS_VARLEN": lambda args: args["cu_seqlens"] is not None,
|
|
}
|
|
)
|
|
@triton.autotune(
|
|
configs=[
|
|
triton.Config({}, num_warps=num_warps, num_stages=num_stages)
|
|
for num_warps in [2, 4, 8]
|
|
for num_stages in [2, 3, 4]
|
|
],
|
|
key=["H", "K", "V", "BT", "BK", "BV", "IS_VARLEN"],
|
|
)
|
|
@triton.jit(do_not_specialize=["T"])
|
|
def recompute_w_u_fwd_kernel(
|
|
q,
|
|
k,
|
|
qg,
|
|
kg,
|
|
v,
|
|
beta,
|
|
w,
|
|
u,
|
|
A,
|
|
gk,
|
|
cu_seqlens,
|
|
chunk_indices,
|
|
T,
|
|
H: tl.constexpr,
|
|
K: tl.constexpr,
|
|
V: tl.constexpr,
|
|
BT: tl.constexpr,
|
|
BK: tl.constexpr,
|
|
BV: tl.constexpr,
|
|
STORE_QG: tl.constexpr,
|
|
STORE_KG: tl.constexpr,
|
|
IS_VARLEN: tl.constexpr,
|
|
DOT_PRECISION: tl.constexpr,
|
|
):
|
|
i_t, i_bh = tl.program_id(0), tl.program_id(1)
|
|
i_b, i_h = i_bh // H, i_bh % H
|
|
if IS_VARLEN:
|
|
i_n, i_t = (
|
|
tl.load(chunk_indices + i_t * 2).to(tl.int32),
|
|
tl.load(chunk_indices + i_t * 2 + 1).to(tl.int32),
|
|
)
|
|
bos, eos = (
|
|
tl.load(cu_seqlens + i_n).to(tl.int32),
|
|
tl.load(cu_seqlens + i_n + 1).to(tl.int32),
|
|
)
|
|
T = eos - bos
|
|
else:
|
|
bos, eos = i_b * T, i_b * T + T
|
|
p_b = tl.make_block_ptr(beta + bos * H + i_h, (T,), (H,), (i_t * BT,), (BT,), (0,))
|
|
b_b = tl.load(p_b, boundary_check=(0,))
|
|
|
|
p_A = tl.make_block_ptr(
|
|
A + (bos * H + i_h) * BT, (T, BT), (H * BT, 1), (i_t * BT, 0), (BT, BT), (1, 0)
|
|
)
|
|
b_A = tl.load(p_A, boundary_check=(0, 1))
|
|
|
|
for i_v in range(tl.cdiv(V, BV)):
|
|
p_v = tl.make_block_ptr(
|
|
v + (bos * H + i_h) * V,
|
|
(T, V),
|
|
(H * V, 1),
|
|
(i_t * BT, i_v * BV),
|
|
(BT, BV),
|
|
(1, 0),
|
|
)
|
|
p_u = tl.make_block_ptr(
|
|
u + (bos * H + i_h) * V,
|
|
(T, V),
|
|
(H * V, 1),
|
|
(i_t * BT, i_v * BV),
|
|
(BT, BV),
|
|
(1, 0),
|
|
)
|
|
b_v = tl.load(p_v, boundary_check=(0, 1))
|
|
b_vb = (b_v * b_b[:, None]).to(b_v.dtype)
|
|
b_u = tl.dot(b_A, b_vb, input_precision=DOT_PRECISION)
|
|
tl.store(p_u, b_u.to(p_u.dtype.element_ty), boundary_check=(0, 1))
|
|
|
|
for i_k in range(tl.cdiv(K, BK)):
|
|
p_w = tl.make_block_ptr(
|
|
w + (bos * H + i_h) * K,
|
|
(T, K),
|
|
(H * K, 1),
|
|
(i_t * BT, i_k * BK),
|
|
(BT, BK),
|
|
(1, 0),
|
|
)
|
|
p_k = tl.make_block_ptr(
|
|
k + (bos * H + i_h) * K,
|
|
(T, K),
|
|
(H * K, 1),
|
|
(i_t * BT, i_k * BK),
|
|
(BT, BK),
|
|
(1, 0),
|
|
)
|
|
b_k = tl.load(p_k, boundary_check=(0, 1))
|
|
b_kb = b_k * b_b[:, None]
|
|
|
|
p_gk = tl.make_block_ptr(
|
|
gk + (bos * H + i_h) * K,
|
|
(T, K),
|
|
(H * K, 1),
|
|
(i_t * BT, i_k * BK),
|
|
(BT, BK),
|
|
(1, 0),
|
|
)
|
|
b_gk = tl.load(p_gk, boundary_check=(0, 1))
|
|
b_kb *= exp2(b_gk)
|
|
if STORE_QG:
|
|
p_q = tl.make_block_ptr(
|
|
q + (bos * H + i_h) * K,
|
|
(T, K),
|
|
(H * K, 1),
|
|
(i_t * BT, i_k * BK),
|
|
(BT, BK),
|
|
(1, 0),
|
|
)
|
|
p_qg = tl.make_block_ptr(
|
|
qg + (bos * H + i_h) * K,
|
|
(T, K),
|
|
(H * K, 1),
|
|
(i_t * BT, i_k * BK),
|
|
(BT, BK),
|
|
(1, 0),
|
|
)
|
|
b_q = tl.load(p_q, boundary_check=(0, 1))
|
|
b_qg = b_q * exp2(b_gk)
|
|
tl.store(p_qg, b_qg.to(p_qg.dtype.element_ty), boundary_check=(0, 1))
|
|
if STORE_KG:
|
|
last_idx = min(i_t * BT + BT, T) - 1
|
|
|
|
o_k = i_k * BK + tl.arange(0, BK)
|
|
m_k = o_k < K
|
|
b_gn = tl.load(
|
|
gk + ((bos + last_idx) * H + i_h) * K + o_k, mask=m_k, other=0.0
|
|
)
|
|
b_kg = b_k * exp2(b_gn - b_gk)
|
|
|
|
p_kg = tl.make_block_ptr(
|
|
kg + (bos * H + i_h) * K,
|
|
(T, K),
|
|
(H * K, 1),
|
|
(i_t * BT, i_k * BK),
|
|
(BT, BK),
|
|
(1, 0),
|
|
)
|
|
tl.store(p_kg, b_kg.to(p_kg.dtype.element_ty), boundary_check=(0, 1))
|
|
|
|
b_w = tl.dot(b_A, b_kb.to(b_k.dtype))
|
|
tl.store(p_w, b_w.to(p_w.dtype.element_ty), boundary_check=(0, 1))
|
|
|
|
|
|
def recompute_w_u_fwd(
|
|
k: torch.Tensor,
|
|
v: torch.Tensor,
|
|
beta: torch.Tensor,
|
|
A: torch.Tensor,
|
|
q: torch.Tensor | None = None,
|
|
gk: torch.Tensor | None = None,
|
|
cu_seqlens: torch.Tensor | None = None,
|
|
chunk_indices: torch.Tensor | None = None,
|
|
) -> tuple[torch.Tensor, torch.Tensor]:
|
|
B, T, H, K, V = *k.shape, v.shape[-1]
|
|
BT = A.shape[-1]
|
|
BK = 64
|
|
BV = 64
|
|
|
|
if chunk_indices is None and cu_seqlens is not None:
|
|
chunk_indices = prepare_chunk_indices(cu_seqlens, BT)
|
|
NT = cdiv(T, BT) if cu_seqlens is None else len(chunk_indices)
|
|
|
|
w = torch.empty_like(k)
|
|
u = torch.empty_like(v)
|
|
kg = torch.empty_like(k) if gk is not None else None
|
|
recompute_w_u_fwd_kernel[(NT, B * H)](
|
|
q=q,
|
|
k=k,
|
|
qg=None,
|
|
kg=kg,
|
|
v=v,
|
|
beta=beta,
|
|
w=w,
|
|
u=u,
|
|
A=A,
|
|
gk=gk,
|
|
cu_seqlens=cu_seqlens,
|
|
chunk_indices=chunk_indices,
|
|
T=T,
|
|
H=H,
|
|
K=K,
|
|
V=V,
|
|
BT=BT,
|
|
BK=BK,
|
|
BV=BV,
|
|
DOT_PRECISION="ieee",
|
|
)
|
|
return w, u, None, kg
|
|
|
|
|
|
@triton.heuristics({"IS_VARLEN": lambda args: args["cu_seqlens"] is not None})
|
|
@triton.autotune(
|
|
configs=[
|
|
triton.Config({"BK": BK, "BV": BV}, num_warps=num_warps, num_stages=num_stages)
|
|
for BK in [32, 64]
|
|
for BV in [64, 128]
|
|
for num_warps in [2, 4, 8]
|
|
for num_stages in [2, 3, 4]
|
|
],
|
|
key=["BT"],
|
|
)
|
|
@triton.jit(do_not_specialize=["T"])
|
|
def chunk_gla_fwd_kernel_o(
|
|
q,
|
|
v,
|
|
g,
|
|
h,
|
|
o,
|
|
A,
|
|
cu_seqlens,
|
|
chunk_indices,
|
|
scale,
|
|
T,
|
|
H: tl.constexpr,
|
|
K: tl.constexpr,
|
|
V: tl.constexpr,
|
|
BT: tl.constexpr,
|
|
BK: tl.constexpr,
|
|
BV: tl.constexpr,
|
|
IS_VARLEN: tl.constexpr,
|
|
):
|
|
i_v, i_t, i_bh = tl.program_id(0), tl.program_id(1), tl.program_id(2)
|
|
i_b, i_h = i_bh // H, i_bh % H
|
|
if IS_VARLEN:
|
|
i_tg = i_t
|
|
i_n, i_t = (
|
|
tl.load(chunk_indices + i_t * 2).to(tl.int32),
|
|
tl.load(chunk_indices + i_t * 2 + 1).to(tl.int32),
|
|
)
|
|
bos, eos = (
|
|
tl.load(cu_seqlens + i_n).to(tl.int32),
|
|
tl.load(cu_seqlens + i_n + 1).to(tl.int32),
|
|
)
|
|
T = eos - bos
|
|
NT = tl.cdiv(T, BT)
|
|
else:
|
|
NT = tl.cdiv(T, BT)
|
|
i_tg = i_b * NT + i_t
|
|
bos, eos = i_b * T, i_b * T + T
|
|
|
|
m_s = tl.arange(0, BT)[:, None] >= tl.arange(0, BT)[None, :]
|
|
|
|
b_o = tl.zeros([BT, BV], dtype=tl.float32)
|
|
for i_k in range(tl.cdiv(K, BK)):
|
|
p_q = tl.make_block_ptr(
|
|
q + (bos * H + i_h) * K,
|
|
(T, K),
|
|
(H * K, 1),
|
|
(i_t * BT, i_k * BK),
|
|
(BT, BK),
|
|
(1, 0),
|
|
)
|
|
p_g = tl.make_block_ptr(
|
|
g + (bos * H + i_h) * K,
|
|
(T, K),
|
|
(H * K, 1),
|
|
(i_t * BT, i_k * BK),
|
|
(BT, BK),
|
|
(1, 0),
|
|
)
|
|
p_h = tl.make_block_ptr(
|
|
# int64 BEFORE the K*V multiply: the int32 product wraps at chunk
|
|
# index 2048 (131072-token prefill at H=64, K=V=128).
|
|
h + (i_tg * H + i_h).to(tl.int64) * K * V,
|
|
(V, K),
|
|
(K, 1),
|
|
(i_v * BV, i_k * BK),
|
|
(BV, BK),
|
|
(1, 0),
|
|
)
|
|
|
|
# [BT, BK]
|
|
b_q = tl.load(p_q, boundary_check=(0, 1))
|
|
b_q = (b_q * scale).to(b_q.dtype)
|
|
# [BT, BK]
|
|
b_g = tl.load(p_g, boundary_check=(0, 1))
|
|
# [BT, BK]
|
|
b_qg = (b_q * exp2(b_g)).to(b_q.dtype)
|
|
# [BV, BK]
|
|
b_h = tl.load(p_h, boundary_check=(0, 1))
|
|
# [BT, BV]
|
|
if i_k >= 0:
|
|
b_o += tl.dot(b_qg, tl.trans(b_h).to(b_qg.dtype))
|
|
p_v = tl.make_block_ptr(
|
|
v + (bos * H + i_h) * V,
|
|
(T, V),
|
|
(H * V, 1),
|
|
(i_t * BT, i_v * BV),
|
|
(BT, BV),
|
|
(1, 0),
|
|
)
|
|
p_o = tl.make_block_ptr(
|
|
o + (bos * H + i_h) * V,
|
|
(T, V),
|
|
(H * V, 1),
|
|
(i_t * BT, i_v * BV),
|
|
(BT, BV),
|
|
(1, 0),
|
|
)
|
|
p_A = tl.make_block_ptr(
|
|
A + (bos * H + i_h) * BT, (T, BT), (H * BT, 1), (i_t * BT, 0), (BT, BT), (1, 0)
|
|
)
|
|
# [BT, BV]
|
|
b_v = tl.load(p_v, boundary_check=(0, 1))
|
|
# [BT, BT]
|
|
b_A = tl.load(p_A, boundary_check=(0, 1))
|
|
b_A = tl.where(m_s, b_A, 0.0).to(b_v.dtype)
|
|
b_o += tl.dot(b_A, b_v, allow_tf32=False)
|
|
tl.store(p_o, b_o.to(p_o.dtype.element_ty), boundary_check=(0, 1))
|
|
|
|
|
|
def chunk_gla_fwd_o_gk(
|
|
q: torch.Tensor,
|
|
v: torch.Tensor,
|
|
g: torch.Tensor,
|
|
A: torch.Tensor,
|
|
h: torch.Tensor,
|
|
o: torch.Tensor,
|
|
scale: float,
|
|
cu_seqlens: torch.Tensor | None = None,
|
|
chunk_indices: torch.Tensor | None = None,
|
|
chunk_size: int = FLA_CHUNK_SIZE,
|
|
):
|
|
B, T, H, K, V = *q.shape, v.shape[-1]
|
|
BT = chunk_size
|
|
|
|
if chunk_indices is None and cu_seqlens is not None:
|
|
chunk_indices = prepare_chunk_indices(cu_seqlens, chunk_size)
|
|
NT = cdiv(T, BT) if cu_seqlens is None else len(chunk_indices)
|
|
|
|
def grid(meta):
|
|
return (cdiv(V, meta["BV"]), NT, B * H)
|
|
|
|
chunk_gla_fwd_kernel_o[grid](
|
|
q=q,
|
|
v=v,
|
|
g=g,
|
|
h=h,
|
|
o=o,
|
|
A=A,
|
|
cu_seqlens=cu_seqlens,
|
|
chunk_indices=chunk_indices,
|
|
scale=scale,
|
|
T=T,
|
|
H=H,
|
|
K=K,
|
|
V=V,
|
|
BT=BT,
|
|
)
|
|
return o
|
|
|
|
|
|
@triton.heuristics(
|
|
{
|
|
"HAS_BIAS": lambda args: args["g_bias"] is not None,
|
|
"IS_VARLEN": lambda args: args["cu_seqlens"] is not None,
|
|
}
|
|
)
|
|
@triton.autotune(
|
|
configs=[
|
|
triton.Config({"BD": BD}, num_warps=num_warps)
|
|
for BD in [32, 64]
|
|
for num_warps in [2, 4, 8]
|
|
],
|
|
key=["H", "D", "BT", "IS_VARLEN"],
|
|
)
|
|
@triton.jit(do_not_specialize=["T"])
|
|
def kda_gate_cumsum_fwd_kernel(
|
|
g,
|
|
A,
|
|
y,
|
|
g_bias,
|
|
cu_seqlens,
|
|
chunk_indices,
|
|
cumsum_scale,
|
|
beta,
|
|
threshold,
|
|
SAFE_GATE: tl.constexpr,
|
|
LOWER_BOUND: tl.constexpr,
|
|
T,
|
|
H: tl.constexpr,
|
|
D: tl.constexpr,
|
|
BT: tl.constexpr,
|
|
BD: tl.constexpr,
|
|
HAS_BIAS: tl.constexpr,
|
|
IS_VARLEN: tl.constexpr,
|
|
):
|
|
i_d, i_t, i_bh = tl.program_id(0), tl.program_id(1), tl.program_id(2)
|
|
i_b, i_h = i_bh // H, i_bh % H
|
|
if IS_VARLEN:
|
|
i_n, i_t = (
|
|
tl.load(chunk_indices + i_t * 2).to(tl.int32),
|
|
tl.load(chunk_indices + i_t * 2 + 1).to(tl.int32),
|
|
)
|
|
bos, eos = (
|
|
tl.load(cu_seqlens + i_n).to(tl.int32),
|
|
tl.load(cu_seqlens + i_n + 1).to(tl.int32),
|
|
)
|
|
T = eos - bos
|
|
else:
|
|
bos = i_b * T
|
|
|
|
p_g = tl.make_block_ptr(
|
|
g + (bos * H + i_h) * D,
|
|
(T, D),
|
|
(H * D, 1),
|
|
(i_t * BT, i_d * BD),
|
|
(BT, BD),
|
|
(1, 0),
|
|
)
|
|
p_y = tl.make_block_ptr(
|
|
y + (bos * H + i_h) * D,
|
|
(T, D),
|
|
(H * D, 1),
|
|
(i_t * BT, i_d * BD),
|
|
(BT, BD),
|
|
(1, 0),
|
|
)
|
|
|
|
b_g = tl.load(p_g, boundary_check=(0, 1)).to(tl.float32)
|
|
if HAS_BIAS:
|
|
o_d = i_d * BD + tl.arange(0, BD)
|
|
b_bias = tl.load(g_bias + i_h * D + o_d, mask=o_d < D, other=0.0).to(tl.float32)
|
|
b_g = b_g + b_bias[None, :]
|
|
|
|
b_a = tl.load(A + i_h).to(tl.float32)
|
|
b_a = tl.exp(b_a) if SAFE_GATE else -tl.exp(b_a)
|
|
if SAFE_GATE:
|
|
# y = lower_bound * sigmoid(exp(A) * (g + g_bias)), bounded to
|
|
# (lower_bound, 0) for safe-gate checkpoints.
|
|
b_gate = LOWER_BOUND / (1.0 + tl.exp(-(b_a * b_g)))
|
|
else:
|
|
b_g_scaled = b_g * beta
|
|
b_softplus = tl.where(
|
|
b_g_scaled > threshold,
|
|
b_g,
|
|
(1.0 / beta) * log(1.0 + tl.exp(b_g_scaled)),
|
|
)
|
|
b_gate = b_a * b_softplus
|
|
|
|
# Out-of-bounds rows (load returns 0, but softplus/bias can still make
|
|
# b_gate non-zero) participate in the dot product. They only contribute to
|
|
# out-of-bounds output rows, which are masked away by `boundary_check` on
|
|
# the store, so visible output matches unfused gate + chunk-local cumsum.
|
|
o_t = tl.arange(0, BT)
|
|
m_cumsum = tl.where(o_t[:, None] >= o_t[None, :], 1.0, 0.0)
|
|
b_y = tl.dot(m_cumsum, b_gate, allow_tf32=False) * cumsum_scale
|
|
tl.store(p_y, b_y.to(p_y.dtype.element_ty), boundary_check=(0, 1))
|
|
|
|
|
|
def fused_kda_gate_chunk_cumsum(
|
|
raw_g: torch.Tensor,
|
|
A_log: torch.Tensor,
|
|
g_bias: torch.Tensor | None = None,
|
|
beta: float = 1.0,
|
|
threshold: float = 20.0,
|
|
cu_seqlens: torch.Tensor | None = None,
|
|
chunk_indices: torch.Tensor | None = None,
|
|
chunk_size: int = FLA_CHUNK_SIZE,
|
|
output_dtype: torch.dtype | None = torch.float,
|
|
safe_gate: bool = False,
|
|
lower_bound: float = -5.0,
|
|
) -> torch.Tensor:
|
|
if cu_seqlens is not None:
|
|
assert raw_g.shape[0] == 1, (
|
|
"Only batch size 1 is supported when cu_seqlens are provided"
|
|
)
|
|
B, T, H, D = raw_g.shape
|
|
if chunk_indices is None and cu_seqlens is not None:
|
|
chunk_indices = prepare_chunk_indices(cu_seqlens, chunk_size)
|
|
NT = cdiv(T, chunk_size) if cu_seqlens is None else len(chunk_indices)
|
|
|
|
A_log = A_log.reshape(-1)
|
|
if g_bias is not None:
|
|
g_bias = g_bias.reshape(-1)
|
|
y = torch.empty_like(raw_g, dtype=output_dtype or raw_g.dtype)
|
|
|
|
def grid(meta):
|
|
return (cdiv(meta["D"], meta["BD"]), NT, B * H)
|
|
|
|
kda_gate_cumsum_fwd_kernel[grid](
|
|
g=raw_g,
|
|
A=A_log,
|
|
y=y,
|
|
g_bias=g_bias,
|
|
cu_seqlens=cu_seqlens,
|
|
chunk_indices=chunk_indices,
|
|
# RCP_LN2 folds in the natural-log -> log2 conversion so downstream
|
|
# exp2-based kernels reproduce exp(g). Keep this in sync with the
|
|
# `use_exp2=True` path in `_chunk_kda_fwd_with_cumulative_g`.
|
|
cumsum_scale=RCP_LN2,
|
|
beta=beta,
|
|
threshold=threshold,
|
|
SAFE_GATE=safe_gate,
|
|
LOWER_BOUND=lower_bound,
|
|
T=T,
|
|
H=H,
|
|
D=D,
|
|
BT=chunk_size,
|
|
)
|
|
return y
|
|
|
|
|
|
def _chunk_kda_fwd_with_cumulative_g(
|
|
q: torch.Tensor,
|
|
k: torch.Tensor,
|
|
v: torch.Tensor,
|
|
g: torch.Tensor,
|
|
beta: torch.Tensor,
|
|
scale: float,
|
|
initial_state: torch.Tensor,
|
|
output_final_state: bool,
|
|
cu_seqlens: torch.Tensor | None = None,
|
|
chunk_indices: torch.Tensor | None = None,
|
|
chunk_size: int = FLA_CHUNK_SIZE,
|
|
return_h: bool = False,
|
|
):
|
|
# Token-offset addressing (q/k/g at `(bos*H + i_h) * K`) is still int32 in the
|
|
# intra-chunk kernels: refuse a varlen batch long enough to wrap rather than
|
|
# silently corrupting (the chunk-STATE offsets are int64 and do not bind first).
|
|
_, _T, _H, _K = k.shape # [1, total_tokens, H, K] (varlen packs on dim 1)
|
|
if _T * _H * _K >= 2**31:
|
|
raise ValueError(
|
|
f"KDA prefill batch too long for int32 token addressing: {_T} tokens "
|
|
f"x H={_H} x K={_K} exceeds 2**31; keep --max-prefill-length below "
|
|
f"{2**31 // (_H * _K)} tokens."
|
|
)
|
|
# `g` must already be chunk-local cumulatively-summed AND scaled by
|
|
# RCP_LN2 (so the downstream exp2-based kernels reproduce exp(g)).
|
|
# Use `chunk_kda_fwd` or `chunk_kda_with_fused_gate_fwd` instead of
|
|
# calling this helper directly unless that invariant is upheld.
|
|
# the intra Aqk is kept in fp32
|
|
# the computation has very marginal effect on the entire throughput
|
|
A, Aqk = chunk_kda_scaled_dot_kkt_fwd(
|
|
q=q,
|
|
k=k,
|
|
gk=g,
|
|
beta=beta,
|
|
scale=scale,
|
|
cu_seqlens=cu_seqlens,
|
|
chunk_indices=chunk_indices,
|
|
chunk_size=chunk_size,
|
|
output_dtype=torch.float32,
|
|
)
|
|
A = solve_tril(A=A, cu_seqlens=cu_seqlens, output_dtype=k.dtype)
|
|
w, u, _, kg = recompute_w_u_fwd(
|
|
k=k,
|
|
v=v,
|
|
beta=beta,
|
|
A=A,
|
|
gk=g,
|
|
cu_seqlens=cu_seqlens,
|
|
chunk_indices=chunk_indices,
|
|
)
|
|
del A
|
|
h, v_new, final_state = chunk_gated_delta_rule_fwd_h(
|
|
k=kg,
|
|
w=w,
|
|
u=u,
|
|
gk=g,
|
|
initial_state=initial_state,
|
|
output_final_state=output_final_state,
|
|
cu_seqlens=cu_seqlens,
|
|
chunk_indices=chunk_indices,
|
|
use_exp2=True,
|
|
)
|
|
del w, u, kg
|
|
o = chunk_gla_fwd_o_gk(
|
|
q=q,
|
|
v=v_new,
|
|
g=g,
|
|
A=Aqk,
|
|
h=h,
|
|
o=v,
|
|
scale=scale,
|
|
cu_seqlens=cu_seqlens,
|
|
chunk_indices=chunk_indices,
|
|
chunk_size=chunk_size,
|
|
)
|
|
del Aqk, v_new
|
|
if return_h:
|
|
# FreeToken addition: expose the per-chunk state snapshots (h[b, i] is the
|
|
# state at the START of chunk i) for hybrid-radix track checkpoints.
|
|
return o, final_state, h
|
|
del h
|
|
return o, final_state
|
|
|
|
|
|
def chunk_kda_fwd(
|
|
q: torch.Tensor,
|
|
k: torch.Tensor,
|
|
v: torch.Tensor,
|
|
g: torch.Tensor,
|
|
beta: torch.Tensor,
|
|
scale: float,
|
|
initial_state: torch.Tensor,
|
|
output_final_state: bool,
|
|
cu_seqlens: torch.Tensor | None = None,
|
|
):
|
|
chunk_size = FLA_CHUNK_SIZE
|
|
chunk_indices = (
|
|
prepare_chunk_indices(cu_seqlens, chunk_size)
|
|
if cu_seqlens is not None
|
|
else None
|
|
)
|
|
g = chunk_local_cumsum(
|
|
g,
|
|
chunk_size=chunk_size,
|
|
cu_seqlens=cu_seqlens,
|
|
chunk_indices=chunk_indices,
|
|
)
|
|
# KDA evaluates cumulative gate decays with exp2. Convert from natural-log
|
|
# space so exp(x) is preserved as exp2(x / ln(2)).
|
|
g = g * RCP_LN2
|
|
return _chunk_kda_fwd_with_cumulative_g(
|
|
q=q,
|
|
k=k,
|
|
v=v,
|
|
g=g,
|
|
beta=beta,
|
|
scale=scale,
|
|
initial_state=initial_state,
|
|
output_final_state=output_final_state,
|
|
cu_seqlens=cu_seqlens,
|
|
chunk_indices=chunk_indices,
|
|
chunk_size=chunk_size,
|
|
)
|
|
|
|
|
|
def chunk_kda_with_fused_gate_fwd(
|
|
q: torch.Tensor,
|
|
k: torch.Tensor,
|
|
v: torch.Tensor,
|
|
raw_g: torch.Tensor,
|
|
beta: torch.Tensor,
|
|
A_log: torch.Tensor,
|
|
g_bias: torch.Tensor | None,
|
|
scale: float,
|
|
initial_state: torch.Tensor,
|
|
output_final_state: bool,
|
|
cu_seqlens: torch.Tensor | None = None,
|
|
safe_gate: bool = False,
|
|
lower_bound: float = -5.0,
|
|
return_h: bool = False,
|
|
):
|
|
chunk_size = FLA_CHUNK_SIZE
|
|
chunk_indices = (
|
|
prepare_chunk_indices(cu_seqlens, chunk_size)
|
|
if cu_seqlens is not None
|
|
else None
|
|
)
|
|
g = fused_kda_gate_chunk_cumsum(
|
|
raw_g,
|
|
A_log=A_log,
|
|
g_bias=g_bias,
|
|
cu_seqlens=cu_seqlens,
|
|
chunk_indices=chunk_indices,
|
|
chunk_size=chunk_size,
|
|
safe_gate=safe_gate,
|
|
lower_bound=lower_bound,
|
|
)
|
|
return _chunk_kda_fwd_with_cumulative_g(
|
|
q=q,
|
|
k=k,
|
|
v=v,
|
|
g=g,
|
|
beta=beta,
|
|
scale=scale,
|
|
initial_state=initial_state,
|
|
output_final_state=output_final_state,
|
|
cu_seqlens=cu_seqlens,
|
|
chunk_indices=chunk_indices,
|
|
chunk_size=chunk_size,
|
|
return_h=return_h,
|
|
)
|
|
|
|
|
|
def chunk_kda(
|
|
q: torch.Tensor,
|
|
k: torch.Tensor,
|
|
v: torch.Tensor,
|
|
g: torch.Tensor,
|
|
beta: torch.Tensor,
|
|
scale: float = None,
|
|
initial_state: torch.Tensor = None,
|
|
output_final_state: bool = False,
|
|
use_qk_l2norm_in_kernel: bool = False,
|
|
cu_seqlens: torch.Tensor | None = None,
|
|
**kwargs,
|
|
):
|
|
if scale is None:
|
|
scale = k.shape[-1] ** -0.5
|
|
|
|
if use_qk_l2norm_in_kernel:
|
|
q = l2norm_fwd(q.contiguous())
|
|
k = l2norm_fwd(k.contiguous())
|
|
|
|
o, final_state = chunk_kda_fwd(
|
|
q=q,
|
|
k=k,
|
|
v=v.contiguous(),
|
|
g=g.contiguous(),
|
|
beta=beta.contiguous(),
|
|
scale=scale,
|
|
initial_state=initial_state.contiguous(),
|
|
output_final_state=output_final_state,
|
|
cu_seqlens=cu_seqlens,
|
|
)
|
|
return o, final_state
|
|
|
|
|
|
def chunk_kda_with_fused_gate(
|
|
q: torch.Tensor,
|
|
k: torch.Tensor,
|
|
v: torch.Tensor,
|
|
raw_g: torch.Tensor,
|
|
beta: torch.Tensor,
|
|
A_log: torch.Tensor,
|
|
g_bias: torch.Tensor | None,
|
|
scale: float | None = None,
|
|
initial_state: torch.Tensor | None = None,
|
|
output_final_state: bool = False,
|
|
use_qk_l2norm_in_kernel: bool = False,
|
|
cu_seqlens: torch.Tensor | None = None,
|
|
safe_gate: bool = False,
|
|
lower_bound: float = -5.0,
|
|
return_h: bool = False,
|
|
**kwargs,
|
|
):
|
|
"""Run chunk KDA from raw gate projection using fused gate+cumsum.
|
|
|
|
WARNING: the output is written into (and returned as) the ``v`` buffer; never
|
|
pass a tensor that is read again after this call. With ``return_h`` the
|
|
per-chunk state snapshots (h[b, i] = state at the START of chunk i) are
|
|
returned as a third value, for hybrid-radix track checkpoints.
|
|
"""
|
|
if scale is None:
|
|
scale = k.shape[-1] ** -0.5
|
|
|
|
if use_qk_l2norm_in_kernel:
|
|
q = l2norm_fwd(q.contiguous())
|
|
k = l2norm_fwd(k.contiguous())
|
|
|
|
return chunk_kda_with_fused_gate_fwd(
|
|
q=q,
|
|
k=k,
|
|
v=v.contiguous(),
|
|
raw_g=raw_g.contiguous(),
|
|
beta=beta.contiguous(),
|
|
A_log=A_log,
|
|
g_bias=g_bias,
|
|
scale=scale,
|
|
initial_state=initial_state.contiguous() if initial_state is not None else None,
|
|
output_final_state=output_final_state,
|
|
cu_seqlens=cu_seqlens,
|
|
safe_gate=safe_gate,
|
|
lower_bound=lower_bound,
|
|
return_h=return_h,
|
|
)
|
|
|
|
|
|
@triton.autotune(
|
|
configs=[
|
|
triton.Config({"BT": bt}, num_warps=nw, num_stages=ns)
|
|
for bt in BT_LIST_AUTOTUNE
|
|
for nw in NUM_WARPS_AUTOTUNE
|
|
for ns in [2, 3]
|
|
],
|
|
key=["H", "D"],
|
|
)
|
|
@triton.jit
|
|
def kda_gate_fwd_kernel(
|
|
g,
|
|
A,
|
|
y,
|
|
g_bias,
|
|
beta: tl.constexpr,
|
|
threshold: tl.constexpr,
|
|
SAFE_GATE: tl.constexpr,
|
|
LOWER_BOUND: tl.constexpr,
|
|
T,
|
|
H,
|
|
D: tl.constexpr,
|
|
BT: tl.constexpr,
|
|
BD: tl.constexpr,
|
|
HAS_BIAS: tl.constexpr,
|
|
):
|
|
i_t, i_h = tl.program_id(0), tl.program_id(1)
|
|
n_t = i_t * BT
|
|
|
|
b_a = tl.load(A + i_h).to(tl.float32)
|
|
b_a = tl.exp(b_a) if SAFE_GATE else -tl.exp(b_a)
|
|
|
|
stride_row = H * D
|
|
stride_col = 1
|
|
|
|
g_ptr = tl.make_block_ptr(
|
|
base=g + i_h * D,
|
|
shape=(T, D),
|
|
strides=(stride_row, stride_col),
|
|
offsets=(n_t, 0),
|
|
block_shape=(BT, BD),
|
|
order=(1, 0),
|
|
)
|
|
|
|
y_ptr = tl.make_block_ptr(
|
|
base=y + i_h * D,
|
|
shape=(T, D),
|
|
strides=(stride_row, stride_col),
|
|
offsets=(n_t, 0),
|
|
block_shape=(BT, BD),
|
|
order=(1, 0),
|
|
)
|
|
|
|
b_g = tl.load(g_ptr, boundary_check=(0, 1)).to(tl.float32)
|
|
|
|
if HAS_BIAS:
|
|
n_d = tl.arange(0, BD)
|
|
bias_mask = n_d < D
|
|
b_bias = tl.load(g_bias + i_h * D + n_d, mask=bias_mask, other=0.0).to(
|
|
tl.float32
|
|
)
|
|
b_g = b_g + b_bias[None, :]
|
|
|
|
if SAFE_GATE:
|
|
# y = lower_bound * sigmoid(exp(A) * (g + g_bias)), bounded to
|
|
# (lower_bound, 0) for safe-gate checkpoints.
|
|
b_y = LOWER_BOUND / (1.0 + tl.exp(-(b_a * b_g)))
|
|
else:
|
|
# softplus(x, beta) = (1/beta) * log(1 + exp(beta * x))
|
|
# When beta * x > threshold, use linear approximation x
|
|
# Use threshold to switch to linear when beta*x > threshold
|
|
g_scaled = b_g * beta
|
|
use_linear = g_scaled > threshold
|
|
sp = tl.where(use_linear, b_g, (1.0 / beta) * log(1.0 + tl.exp(g_scaled)))
|
|
b_y = b_a * sp
|
|
|
|
tl.store(y_ptr, b_y.to(y.dtype.element_ty), boundary_check=(0, 1))
|
|
|
|
|
|
def fused_kda_gate(
|
|
g: torch.Tensor,
|
|
A: torch.Tensor,
|
|
head_k_dim: int,
|
|
g_bias: torch.Tensor | None = None,
|
|
beta: float = 1.0,
|
|
threshold: float = 20.0,
|
|
safe_gate: bool = False,
|
|
lower_bound: float | None = -5.0,
|
|
) -> torch.Tensor:
|
|
"""
|
|
Forward pass for KDA gate:
|
|
input g: [..., H*D]
|
|
param A: [H] or [1, 1, H, 1]
|
|
beta: softplus beta parameter (softplus branch only)
|
|
threshold: softplus threshold parameter (softplus branch only)
|
|
safe_gate: when False (default) compute y = -exp(A)*softplus(g+g_bias);
|
|
when True compute the bounded y = lower_bound*sigmoid(exp(A)*(g+g_bias))
|
|
lower_bound: floor for the safe_gate branch (default -5.0)
|
|
return : [..., H, D]
|
|
"""
|
|
orig_shape = g.shape[:-1]
|
|
|
|
g = g.view(-1, g.shape[-1])
|
|
T = g.shape[0]
|
|
HD = g.shape[1]
|
|
H = A.numel()
|
|
assert H * head_k_dim == HD
|
|
|
|
y = torch.empty_like(g, dtype=torch.float32)
|
|
|
|
def grid(meta):
|
|
return (cdiv(T, meta["BT"]), H)
|
|
|
|
kda_gate_fwd_kernel[grid](
|
|
g,
|
|
A,
|
|
y,
|
|
g_bias,
|
|
beta,
|
|
threshold,
|
|
safe_gate,
|
|
lower_bound if lower_bound is not None else -5.0,
|
|
T,
|
|
H,
|
|
head_k_dim,
|
|
BD=next_power_of_2(head_k_dim),
|
|
HAS_BIAS=g_bias is not None,
|
|
)
|
|
|
|
y = y.view(*orig_shape, H, head_k_dim)
|
|
return y
|
|
|