| |
| |
| |
| |
| |
| |
|
|
| |
|
|
| import torch |
| import torch.nn.functional as F |
| import triton |
| import triton.language as tl |
|
|
| from ...ops.utils.cache import fla_cache_autotune |
| from ...ops.utils.index import prepare_chunk_indices |
| from ...ops.utils.op import exp |
| from ...ops.utils.softplus import softplus |
| from ...utils import IS_AMD, autocast_custom_bwd, autocast_custom_fwd, autotune_cache_kwargs, check_shared_mem, input_guard |
|
|
| BS_LIST = [32, 64] if check_shared_mem() else [16, 32] |
| BT_LIST_AUTOTUNE = [32, 64, 128] |
| NUM_WARPS_AUTOTUNE = [2, 4, 8, 16] if IS_AMD else [4, 8, 16, 32] |
|
|
|
|
| def naive_kda_gate( |
| g: torch.Tensor, |
| A_log: torch.Tensor, |
| dt_bias: torch.Tensor | None = None, |
| output_dtype: torch.dtype = torch.float32, |
| ) -> torch.Tensor: |
| """ |
| Torch reference implementation for KDA gate computation. |
| |
| Computes: g = -A_log.exp().unsqueeze(-1) * softplus(g + dt_bias.view(g.shape[-2:])) |
| |
| Args: |
| g (torch.Tensor): |
| Input tensor of shape `[..., H, K]`. |
| A_log (torch.Tensor): |
| Parameter tensor with `H` elements. |
| dt_bias (torch.Tensor | None): |
| Optional bias tensor added to `g` before activation, shape `[H * K]`. |
| |
| Returns: |
| Output tensor of shape `[..., H, K]` . |
| """ |
| H, _ = g.shape[-2:] |
| g = g.float() |
| if dt_bias is not None: |
| g = g + dt_bias.view(H, -1) |
|
|
| g = (-A_log.view(H, 1).float().exp() * F.softplus(g.float())).to(output_dtype) |
| return g |
|
|
|
|
| def naive_kda_lowerbound_gate( |
| g: torch.Tensor, |
| A_log: torch.Tensor, |
| dt_bias: torch.Tensor | None = None, |
| lower_bound: float = -5.0, |
| output_dtype: torch.dtype = torch.float32, |
| ) -> torch.Tensor: |
| H, _ = g.shape[-2:] |
| g = g.float() |
| if dt_bias is not None: |
| g = g + dt_bias.view(H, -1) |
| g = lower_bound * F.sigmoid(A_log.view(H, 1).exp() * g) |
| return g.to(output_dtype) |
|
|
|
|
| @triton.heuristics({ |
| "HAS_BIAS": lambda args: args["dt_bias"] is not None, |
| "HAS_BETA": lambda args: args["beta"] is not None, |
| 'USE_LOWER_BOUND': lambda args: args['lower_bound'] is not None, |
| }) |
| @fla_cache_autotune( |
| configs=[ |
| triton.Config({"BT": BT}, num_warps=num_warps, num_stages=num_stages) |
| for BT in BT_LIST_AUTOTUNE |
| for num_warps in NUM_WARPS_AUTOTUNE |
| for num_stages in [2, 3] |
| ], |
| key=["H", "D"], |
| **autotune_cache_kwargs, |
| ) |
| @triton.jit(do_not_specialize=['T']) |
| def kda_gate_fwd_kernel( |
| g, |
| A_log, |
| dt_bias, |
| beta, |
| yg, |
| yb, |
| lower_bound, |
| T, |
| H: tl.constexpr, |
| D: tl.constexpr, |
| BT: tl.constexpr, |
| BD: tl.constexpr, |
| HAS_BIAS: tl.constexpr, |
| HAS_BETA: tl.constexpr, |
| USE_LOWER_BOUND: tl.constexpr, |
| ): |
| i_t, i_h = tl.program_id(0), tl.program_id(1) |
|
|
| b_A = tl.load(A_log + i_h).to(tl.float32) |
|
|
| p_g = tl.make_block_ptr(g + i_h * D, (T, D), (H * D, 1), (i_t * BT, 0), (BT, BD), (1, 0)) |
| p_yg = tl.make_block_ptr(yg + i_h * D, (T, D), (H * D, 1), (i_t * BT, 0), (BT, BD), (1, 0)) |
| |
| b_g = tl.load(p_g, boundary_check=(0, 1)).to(tl.float32) |
| if HAS_BIAS: |
| p_b = tl.make_block_ptr(dt_bias, (H * D,), (1,), (i_h * D,), (BD,), (0,)) |
| b_g = b_g + tl.load(p_b, boundary_check=(0,)).to(tl.float32) |
| if not USE_LOWER_BOUND: |
| b_yg = -exp(b_A) * softplus(b_g) |
| else: |
| b_yg = lower_bound * tl.sigmoid(exp(b_A) * b_g) |
| tl.store(p_yg, b_yg.to(p_yg.dtype.element_ty), boundary_check=(0, 1)) |
|
|
| if HAS_BETA: |
| p_b = tl.make_block_ptr(beta + i_h, (T,), (H,), (i_t * BT,), (BT,), (0,)) |
| p_yb = tl.make_block_ptr(yb + i_h, (T,), (H,), (i_t * BT,), (BT,), (0,)) |
| b_yb = tl.sigmoid(tl.load(p_b, boundary_check=(0,)).to(tl.float32)) |
| tl.store(p_yb, b_yb.to(p_yb.dtype.element_ty), boundary_check=(0,)) |
|
|
|
|
| @triton.heuristics({ |
| "HAS_BIAS": lambda args: args["dt_bias"] is not None, |
| "HAS_BETA": lambda args: args["beta"] is not None, |
| 'USE_LOWER_BOUND': lambda args: args['lower_bound'] is not None, |
| }) |
| @fla_cache_autotune( |
| configs=[ |
| triton.Config({}, num_warps=num_warps, num_stages=num_stages) |
| for num_warps in NUM_WARPS_AUTOTUNE |
| for num_stages in [2, 3] |
| ], |
| key=["H", "D"], |
| **autotune_cache_kwargs, |
| ) |
| @triton.jit(do_not_specialize=['T']) |
| def kda_gate_bwd_kernel( |
| g, |
| A_log, |
| dt_bias, |
| beta, |
| dyg, |
| dyb, |
| dg, |
| dA, |
| dbeta, |
| lower_bound, |
| T, |
| H: tl.constexpr, |
| D: tl.constexpr, |
| BT: tl.constexpr, |
| BD: tl.constexpr, |
| HAS_BIAS: tl.constexpr, |
| HAS_BETA: tl.constexpr, |
| USE_LOWER_BOUND: tl.constexpr, |
| ): |
| i_t, i_h = tl.program_id(0), tl.program_id(1) |
|
|
| b_A = tl.load(A_log + i_h).to(tl.float32) |
|
|
| p_g = tl.make_block_ptr(g + i_h * D, (T, D), (H * D, 1), (i_t * BT, 0), (BT, BD), (1, 0)) |
| p_dg = tl.make_block_ptr(dg + i_h * D, (T, D), (H * D, 1), (i_t * BT, 0), (BT, BD), (1, 0)) |
| p_dyg = tl.make_block_ptr(dyg + i_h * D, (T, D), (H * D, 1), (i_t * BT, 0), (BT, BD), (1, 0)) |
|
|
| |
| b_g = tl.load(p_g, boundary_check=(0, 1)).to(tl.float32) |
| b_dyg = tl.load(p_dyg, boundary_check=(0, 1)).to(tl.float32) |
|
|
| if HAS_BIAS: |
| p_b = tl.make_block_ptr(dt_bias, (H * D,), (1,), (i_h * D,), (BD,), (0,)) |
| b_g = b_g + tl.load(p_b, boundary_check=(0,)).to(tl.float32) |
|
|
| |
| if not USE_LOWER_BOUND: |
| b_A = -exp(b_A) |
| b_yg = b_A * softplus(b_g) |
| b_dg = b_A * (b_dyg * tl.sigmoid(b_g)) |
| b_dA = tl.sum(tl.sum(b_dyg * b_yg, 1), 0) |
| else: |
| b_A = exp(b_A) |
| b_inner = b_A * b_g |
| b_sig = tl.sigmoid(b_inner) |
| b_dsig = b_sig * (1.0 - b_sig) |
| |
| b_d_inner_term = b_dyg * (lower_bound * b_dsig) |
| |
| b_dg = b_d_inner_term * b_A |
| b_dA = tl.sum(tl.sum(b_dg * b_g, 1), 0) |
|
|
| tl.store(p_dg, b_dg.to(p_dg.dtype.element_ty), boundary_check=(0, 1)) |
| tl.store(dA + i_t * H + i_h, b_dA) |
|
|
| if HAS_BETA: |
| p_b = tl.make_block_ptr(beta + i_h, (T,), (H,), (i_t * BT,), (BT,), (0,)) |
| p_db = tl.make_block_ptr(dbeta + i_h, (T,), (H,), (i_t * BT,), (BT,), (0,)) |
| p_dyb = tl.make_block_ptr(dyb + i_h, (T,), (H,), (i_t * BT,), (BT,), (0,)) |
|
|
| b_b = tl.load(p_b, boundary_check=(0,)).to(tl.float32) |
| b_db = tl.load(p_dyb, boundary_check=(0,)).to(tl.float32) * b_b * (1.0 - b_b) |
| tl.store(p_db, b_db.to(p_db.dtype.element_ty), boundary_check=(0,)) |
|
|
|
|
| def kda_gate_fwd( |
| g: torch.Tensor, |
| A_log: torch.Tensor, |
| dt_bias: torch.Tensor | None = None, |
| lower_bound: float | None = None, |
| output_dtype: torch.dtype = torch.float32, |
| ) -> torch.Tensor: |
| H, K = g.shape[-2:] |
| T = g.numel() // (H * K) |
|
|
| yg = torch.empty_like(g, dtype=output_dtype) |
|
|
| def grid(meta): |
| return (triton.cdiv(T, meta["BT"]), H) |
|
|
| kda_gate_fwd_kernel[grid]( |
| g=g, |
| A_log=A_log, |
| dt_bias=dt_bias, |
| beta=None, |
| yg=yg, |
| yb=None, |
| T=T, |
| H=H, |
| D=K, |
| BD=triton.next_power_of_2(K), |
| lower_bound=lower_bound, |
| ) |
| return yg |
|
|
|
|
| def kda_gate_bwd( |
| g: torch.Tensor, |
| A_log: torch.Tensor, |
| dt_bias: torch.Tensor | None = None, |
| dyg: torch.Tensor | None = None, |
| lower_bound: float | None = None, |
| ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor | None]: |
| H, K = g.shape[-2:] |
| T = g.numel() // (H * K) |
| BT = 32 |
| NT = triton.cdiv(T, BT) |
|
|
| dg = torch.empty_like(g, dtype=torch.float32) |
| dA = A_log.new_empty(NT, H, dtype=torch.float32) |
|
|
| grid = (triton.cdiv(T, BT), H) |
| kda_gate_bwd_kernel[grid]( |
| g=g, |
| A_log=A_log, |
| dt_bias=dt_bias, |
| beta=None, |
| dyg=dyg, |
| dyb=None, |
| dg=dg, |
| dA=dA, |
| dbeta=None, |
| T=T, |
| H=H, |
| D=K, |
| BT=BT, |
| BD=triton.next_power_of_2(K), |
| lower_bound=lower_bound, |
| ) |
|
|
| dg = dg.view_as(g).type_as(g) |
| dA = dA.sum(0).view_as(A_log).type_as(A_log) |
| dbias = dg.view(-1, H * K).sum(0).to(dt_bias) if dt_bias is not None else None |
|
|
| return dg, dA, dbias |
|
|
|
|
| class KDAGateFunction(torch.autograd.Function): |
| @staticmethod |
| @input_guard |
| @autocast_custom_fwd |
| def forward( |
| ctx, |
| g: torch.Tensor, |
| A_log: torch.Tensor, |
| dt_bias: torch.Tensor | None = None, |
| lower_bound: float | None = None, |
| output_dtype: torch.dtype = torch.float32, |
| ) -> torch.Tensor: |
| yg = kda_gate_fwd( |
| g=g, |
| A_log=A_log, |
| dt_bias=dt_bias, |
| lower_bound=lower_bound, |
| output_dtype=output_dtype |
| ) |
| ctx.save_for_backward(g, A_log, dt_bias) |
| ctx.lower_bound = lower_bound |
| return yg |
|
|
| @staticmethod |
| @input_guard |
| @autocast_custom_bwd |
| def backward(ctx, dyg: torch.Tensor): |
| g, A_log, dt_bias = ctx.saved_tensors |
| dg, dA, dbias = kda_gate_bwd( |
| g=g, |
| A_log=A_log, |
| dt_bias=dt_bias, |
| dyg=dyg, |
| lower_bound=ctx.lower_bound |
| ) |
| return dg, dA, dbias, None, None |
|
|
|
|
| @torch.compiler.disable |
| def fused_kda_gate( |
| g: torch.Tensor, |
| A_log: torch.Tensor, |
| dt_bias: torch.Tensor | None = None, |
| lower_bound: float | None = None, |
| output_dtype: torch.dtype = torch.float32, |
| ) -> torch.Tensor | tuple[torch.Tensor, torch.Tensor]: |
| """ |
| Fused KDA gate computation with autograd support. |
| |
| Computes: g = -A_log.exp().unsqueeze(-1) * softplus(g + dt_bias.view(g.shape[-2:])) |
| |
| Args: |
| g (torch.Tensor): |
| Input tensor of shape `[..., H, K]`. |
| A_log (torch.Tensor): |
| Parameter tensor with `H` elements. |
| dt_bias (torch.Tensor | None): |
| Optional bias tensor added to `g` before activation, shape `[H * K]`. |
| |
| Returns: |
| Output tensor of shape `[..., H, K]`. |
| """ |
| return KDAGateFunction.apply(g, A_log, dt_bias, lower_bound, output_dtype) |
|
|
|
|
| @triton.heuristics({ |
| "HAS_BIAS": lambda args: args["dt_bias"] is not None, |
| 'HAS_SCALE': lambda args: args['scale'] is not None, |
| 'IS_VARLEN': lambda args: args['cu_seqlens'] is not None, |
| 'USE_LOWER_BOUND': lambda args: args['lower_bound'] is not None, |
| }) |
| @fla_cache_autotune( |
| configs=[ |
| triton.Config({'BS': BS}, num_warps=num_warps) |
| for BS in BS_LIST |
| for num_warps in [2, 4, 8] |
| ], |
| key=['H', 'S', 'BT', 'IS_VARLEN', 'REVERSE'], |
| **autotune_cache_kwargs, |
| ) |
| @triton.jit(do_not_specialize=['T']) |
| def kda_gate_chunk_cumsum_vector_kernel( |
| s, |
| A_log, |
| dt_bias, |
| o, |
| scale, |
| cu_seqlens, |
| chunk_indices, |
| lower_bound, |
| T, |
| H: tl.constexpr, |
| S: tl.constexpr, |
| BT: tl.constexpr, |
| BS: tl.constexpr, |
| REVERSE: tl.constexpr, |
| HAS_BIAS: tl.constexpr, |
| HAS_SCALE: tl.constexpr, |
| IS_VARLEN: tl.constexpr, |
| USE_LOWER_BOUND: tl.constexpr, |
| ): |
| i_s, 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, eos = i_b * T, i_b * T + T |
|
|
| p_s = tl.make_block_ptr(s + (bos * H + i_h) * S, (T, S), (H*S, 1), (i_t * BT, i_s * BS), (BT, BS), (1, 0)) |
| p_o = tl.make_block_ptr(o + (bos * H + i_h) * S, (T, S), (H*S, 1), (i_t * BT, i_s * BS), (BT, BS), (1, 0)) |
| |
| b_s = tl.load(p_s, boundary_check=(0, 1)).to(tl.float32) |
|
|
| |
| if HAS_BIAS: |
| p_b = tl.make_block_ptr(dt_bias + i_h * S, (S,), (1,), (i_s * BS,), (BS,), (0,)) |
| b_bias = tl.load(p_b, boundary_check=(0,)).to(tl.float32) |
| b_s = b_s + b_bias[None, :] |
|
|
| b_A = tl.load(A_log + i_h).to(tl.float32) |
| if not USE_LOWER_BOUND: |
| |
| b_gate = -exp(b_A) * softplus(b_s) |
| else: |
| b_gate = lower_bound * tl.sigmoid(exp(b_A) * b_s) |
|
|
| |
| if REVERSE: |
| b_o = tl.cumsum(b_gate, axis=0, reverse=True) |
| else: |
| b_o = tl.cumsum(b_gate, axis=0) |
|
|
| if HAS_SCALE: |
| b_o *= scale |
| tl.store(p_o, b_o.to(p_o.dtype.element_ty), boundary_check=(0, 1)) |
|
|
|
|
| @input_guard |
| def kda_gate_chunk_cumsum( |
| g: torch.Tensor, |
| A_log: torch.Tensor, |
| chunk_size: int, |
| scale: float = None, |
| dt_bias: torch.Tensor | None = None, |
| cu_seqlens: torch.Tensor | None = None, |
| output_dtype: torch.dtype | None = torch.float, |
| chunk_indices: torch.LongTensor | None = None, |
| lower_bound: float | None = None, |
| **kwargs, |
| ) -> torch.Tensor: |
| if cu_seqlens is not None: |
| assert g.shape[0] == 1, "Only batch size 1 is supported when cu_seqlens are provided" |
| assert len(g.shape) == 4 |
| B, T, H, S = g.shape |
| BT = chunk_size |
| if chunk_indices is None and cu_seqlens is not None: |
| chunk_indices = prepare_chunk_indices(cu_seqlens, BT) |
| NT = triton.cdiv(T, BT) if cu_seqlens is None else len(chunk_indices) |
| assert chunk_size == 2**(chunk_size.bit_length()-1), "chunk_size must be a power of 2" |
|
|
| g_org, g = g, torch.empty_like(g, dtype=output_dtype or g.dtype) |
| def grid(meta): return (triton.cdiv(meta['S'], meta['BS']), NT, B * H) |
| kda_gate_chunk_cumsum_vector_kernel[grid]( |
| s=g_org, |
| A_log=A_log, |
| dt_bias=dt_bias, |
| o=g, |
| scale=scale, |
| cu_seqlens=cu_seqlens, |
| chunk_indices=chunk_indices, |
| lower_bound=lower_bound, |
| T=T, |
| H=H, |
| S=S, |
| BT=BT, |
| REVERSE=False, |
| ) |
| return g |
|
|