|
| 1 | +# Copyright 2025 The EasyDeL/ejKernel Author @erfanzar (Erfan Zare Chavoshi). |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""RWKV-4 recurrent time-mix kernel (Triton).""" |
| 16 | + |
| 17 | +from __future__ import annotations |
| 18 | + |
| 19 | +from functools import partial |
| 20 | + |
| 21 | +import jax |
| 22 | +import jax.numpy as jnp |
| 23 | +import jaxtyping |
| 24 | +from beartype import beartype |
| 25 | +from jaxtyping import Array, Float |
| 26 | + |
| 27 | +from ..._registry import Backend, Platform, kernel_registry |
| 28 | +from ..._xla.rwkv4 import rwkv4 as xla_rwkv4 |
| 29 | +from ._triton_impl_fwd import fwd_triton_impl |
| 30 | + |
| 31 | + |
| 32 | +def _fwd_call( |
| 33 | + w: Float[Array, "chans"], |
| 34 | + u: Float[Array, "chans"], |
| 35 | + k: Float[Array, "batch seq_len chans"], |
| 36 | + v: Float[Array, "batch seq_len chans"], |
| 37 | + state: Float[Array, "batch three chans"] | None, |
| 38 | +): |
| 39 | + state_was_none = state is None |
| 40 | + if state is None: |
| 41 | + bsz, _, chans = k.shape |
| 42 | + alpha0 = jnp.zeros((bsz, chans), dtype=jnp.float32) |
| 43 | + beta0 = jnp.zeros((bsz, chans), dtype=jnp.float32) |
| 44 | + eps0 = jnp.full((bsz, chans), -1e30, dtype=jnp.float32) |
| 45 | + state = jnp.stack([alpha0, beta0, eps0], axis=1) |
| 46 | + |
| 47 | + w_neg = -jnp.exp(w.astype(jnp.float32)) |
| 48 | + o, final_state = fwd_triton_impl(w_neg, u.astype(jnp.float32), k, v, state.astype(jnp.float32)) |
| 49 | + residual = (w, u, k, v, state, state_was_none) |
| 50 | + return (o, final_state), residual |
| 51 | + |
| 52 | + |
| 53 | +def _bwd_call( |
| 54 | + residual, |
| 55 | + grads, |
| 56 | +): |
| 57 | + (w, u, k, v, state, state_was_none) = residual |
| 58 | + do, dstate = grads |
| 59 | + |
| 60 | + def f(w_, u_, k_, v_, state_): |
| 61 | + return xla_rwkv4(w_, u_, k_, v_, state_) |
| 62 | + |
| 63 | + (o_ref, state_ref), vjp = jax.vjp(f, w, u, k, v, state) |
| 64 | + del o_ref, state_ref |
| 65 | + dw, du, dk, dv, dstate_in = vjp((do, dstate)) |
| 66 | + if state_was_none: |
| 67 | + dstate_in = None |
| 68 | + return dw, du, dk, dv, dstate_in |
| 69 | + |
| 70 | + |
| 71 | +@partial(jax.custom_vjp) |
| 72 | +def _rwkv4( |
| 73 | + w: Float[Array, "chans"], |
| 74 | + u: Float[Array, "chans"], |
| 75 | + k: Float[Array, "batch seq_len chans"], |
| 76 | + v: Float[Array, "batch seq_len chans"], |
| 77 | + state: Float[Array, "batch three chans"] | None = None, |
| 78 | +) -> tuple[Float[Array, "batch seq_len chans"], Float[Array, "batch three chans"]]: |
| 79 | + if state is None: |
| 80 | + bsz, _, chans = k.shape |
| 81 | + alpha0 = jnp.zeros((bsz, chans), dtype=jnp.float32) |
| 82 | + beta0 = jnp.zeros((bsz, chans), dtype=jnp.float32) |
| 83 | + eps0 = jnp.full((bsz, chans), -1e30, dtype=jnp.float32) |
| 84 | + state = jnp.stack([alpha0, beta0, eps0], axis=1) |
| 85 | + |
| 86 | + w_neg = -jnp.exp(w.astype(jnp.float32)) |
| 87 | + return fwd_triton_impl(w_neg, u.astype(jnp.float32), k, v, state.astype(jnp.float32)) |
| 88 | + |
| 89 | + |
| 90 | +_rwkv4.defvjp(_fwd_call, _bwd_call) |
| 91 | + |
| 92 | + |
| 93 | +@kernel_registry.register("rwkv4", Platform.TRITON, Backend.GPU) |
| 94 | +@jaxtyping.jaxtyped(typechecker=beartype) |
| 95 | +def rwkv4( |
| 96 | + w: Float[Array, "chans"], |
| 97 | + u: Float[Array, "chans"], |
| 98 | + k: Float[Array, "batch seq_len chans"], |
| 99 | + v: Float[Array, "batch seq_len chans"], |
| 100 | + state: Float[Array, "batch three chans"] | None = None, |
| 101 | +) -> tuple[Float[Array, "batch seq_len chans"], Float[Array, "batch three chans"]]: |
| 102 | + """RWKV-4 time-mix recurrence (Triton GPU implementation). |
| 103 | +
|
| 104 | + Args: |
| 105 | + w: Time-decay parameter in log space `[C]`. |
| 106 | + u: Time-mix bias `[C]`. |
| 107 | + k: Key tensor `[B, T, C]`. |
| 108 | + v: Value tensor `[B, T, C]`. |
| 109 | + state: Optional initial state `[B, 3, C]` (alpha, beta, eps). |
| 110 | +
|
| 111 | + Returns: |
| 112 | + Tuple of (output `[B, T, C]`, final_state `[B, 3, C]`). |
| 113 | + """ |
| 114 | + return _rwkv4(w, u, k, v, state) |
0 commit comments