# Copyright (c) 2023-2025, Songlin Yang, Yu Zhang import torch import triton import triton.language as tl from fla.ops.utils.op import exp from fla.utils import input_guard @triton.heuristics({ 'USE_GV': lambda args: args['gv'] is not None, 'USE_INITIAL_STATE': lambda args: args['h0'] is not None, 'STORE_FINAL_STATE': lambda args: args['ht'] is not None, 'IS_VARLEN': lambda args: args['cu_seqlens'] is not None }) @triton.jit(do_not_specialize=['T']) def fused_recurrent_oja_fwd_kernel( q, k, v, gv, beta, o, h0, ht, cu_seqlens, scale, T, B: tl.constexpr, H: tl.constexpr, HV: tl.constexpr, K: tl.constexpr, V: tl.constexpr, BK: tl.constexpr, BV: tl.constexpr, USE_GV: tl.constexpr, USE_Q_L2NORM: tl.constexpr, USE_K_L2NORM: tl.constexpr, IS_BETA_HEADWISE: tl.constexpr, USE_INITIAL_STATE: tl.constexpr, STORE_FINAL_STATE: tl.constexpr, IS_VARLEN: tl.constexpr, ): i_v, i_nh = tl.program_id(0), tl.program_id(1) i_n, i_hv = i_nh // HV, i_nh % HV i_h = i_hv // (HV // H) if IS_VARLEN: bos, eos = tl.load(cu_seqlens + i_n).to(tl.int64), tl.load(cu_seqlens + i_n + 1).to(tl.int64) T = eos - bos else: bos, eos = i_n * T, i_n * T + T o_k = tl.arange(0, BK) o_v = i_v * BV + tl.arange(0, BV) p_q = q + (bos * H + i_h) * K + o_k p_k = k + (bos * H + i_h) * K + o_k p_v = v + (bos * HV + i_hv) * V + o_v if USE_GV: p_gv = gv + (bos * HV + i_hv) * V + o_v if IS_BETA_HEADWISE: p_beta = beta + bos * HV + i_hv else: p_beta = beta + (bos * HV + i_hv) * V + o_v p_o = o + (bos * HV + i_hv) * V + o_v mask_k = o_k < K mask_v = o_v < V mask_h = mask_k[:, None] & mask_v[None, :] b_h = tl.zeros([BK, BV], dtype=tl.float32) if USE_INITIAL_STATE: p_h0 = h0 + i_nh * K*V + o_k[:, None] * V + o_v[None, :] b_h += tl.load(p_h0, mask=mask_h, other=0).to(tl.float32) for _ in range(0, T): b_q = tl.load(p_q, mask=mask_k, other=0).to(tl.float32) b_k = tl.load(p_k, mask=mask_k, other=0).to(tl.float32) b_v = tl.load(p_v, mask=mask_v, other=0).to(tl.float32) if USE_Q_L2NORM: b_q = b_q / tl.sqrt(tl.sum(b_q * b_q) + 1e-6) if USE_K_L2NORM: b_k = b_k / tl.sqrt(tl.sum(b_k * b_k) + 1e-6) b_q = b_q * scale if IS_BETA_HEADWISE: b_beta = tl.load(p_beta).to(tl.float32) else: b_beta = tl.load(p_beta, mask=mask_v, other=0).to(tl.float32) # [BK, BV] if USE_GV: b_gv = tl.load(p_gv, mask=mask_v, other=0).to(tl.float32) b_h *= exp(b_gv[None, :]) b_k = b_beta * (b_k - tl.sum(b_h * b_v[None, :], 1)) b_h += b_k[:, None] * b_v # [BV] b_o = tl.sum(b_h * b_q[:, None], 0) tl.store(p_o, b_o.to(p_o.dtype.element_ty), mask=mask_v) p_q += H*K p_k += H*K p_v += HV*V if USE_GV: p_gv += HV*V p_beta += HV * (1 if IS_BETA_HEADWISE else V) p_o += HV*V if STORE_FINAL_STATE: p_ht = ht + i_nh * K*V + o_k[:, None] * V + o_v[None, :] tl.store(p_ht, b_h.to(p_ht.dtype.element_ty), mask=mask_h) def fused_recurrent_oja_fwd( q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, gv: torch.Tensor | None = None, beta: torch.Tensor | None = None, scale: float = None, initial_state: torch.Tensor = None, output_final_state: bool = False, use_q_l2norm: bool = False, use_k_l2norm: bool = False, cu_seqlens: torch.LongTensor | None = None, ) -> tuple[torch.Tensor, torch.Tensor]: B, T, H, K, V = *k.shape, v.shape[-1] assert V <= 128 HV = v.shape[2] N = B if cu_seqlens is None else len(cu_seqlens) - 1 BK, BV = triton.next_power_of_2(K), min(triton.next_power_of_2(V), 256) NV = triton.cdiv(V, BV) num_stages = 3 num_warps = 1 o = torch.empty_like(v) final_state = q.new_empty(N, HV, K, V, dtype=torch.float32) if output_final_state else None grid = (NV, N * HV) fused_recurrent_oja_fwd_kernel[grid]( q=q, k=k, v=v, gv=gv, beta=beta, o=o, h0=initial_state, ht=final_state, cu_seqlens=cu_seqlens, scale=scale, T=T, B=B, H=H, HV=HV, K=K, V=V, BK=BK, BV=BV, IS_BETA_HEADWISE=beta.ndim != v.ndim, USE_Q_L2NORM=use_q_l2norm, USE_K_L2NORM=use_k_l2norm, num_warps=num_warps, num_stages=num_stages, ) return o, final_state class FusedRecurrentFunction(torch.autograd.Function): @staticmethod @input_guard def forward( ctx, q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, gv: torch.Tensor | None = None, beta: torch.Tensor | None = None, scale: float = None, initial_state: torch.Tensor = None, output_final_state: bool = False, use_q_l2norm: bool = False, use_k_l2norm: bool = False, cu_seqlens: torch.LongTensor | None = None, ): o, final_state = fused_recurrent_oja_fwd( q=q, k=k, v=v, gv=gv, beta=beta, scale=scale, initial_state=initial_state, output_final_state=output_final_state, use_q_l2norm=use_q_l2norm, use_k_l2norm=use_k_l2norm, cu_seqlens=cu_seqlens, ) return o, final_state @staticmethod @input_guard def backward(ctx, do, dht): raise NotImplementedError( "Backward pass is not implemented yet and we do not have plans to implement it " "because we haven't figured out how to compute dg without materializing the full " "hidden states for all time steps." ) def fused_recurrent_gated_oja_rule( q: torch.Tensor, k: torch.Tensor, v: torch.Tensor, gv: torch.Tensor | None = None, beta: torch.Tensor | None = None, scale: float = None, initial_state: torch.Tensor = None, output_final_state: bool = False, use_q_l2norm: bool = False, use_k_l2norm: bool = False, cu_seqlens: torch.LongTensor | None = None, **kwargs, ) -> tuple[torch.Tensor, torch.Tensor]: if 'use_qk_l2norm_in_kernel' in kwargs and (not use_q_l2norm and not use_k_l2norm): use_q_l2norm = True use_k_l2norm = True if cu_seqlens is not None: if 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 initial_state is not None and initial_state.shape[0] != len(cu_seqlens) - 1: raise ValueError( f"The number of initial states is expected to be equal to the number of input sequences, " f"i.e., {len(cu_seqlens) - 1} rather than {initial_state.shape[0]}." ) if scale is None: scale = k.shape[-1] ** -0.5 if beta is None: beta = torch.ones_like(q[..., 0]) o, final_state = FusedRecurrentFunction.apply( q, k, v, gv, beta, scale, initial_state, output_final_state, use_q_l2norm, use_k_l2norm, cu_seqlens, ) return o, final_state