Run this model with HF Jobs
Run replicate/rwkv on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs to fine-tune https://huggingface.co/replicate/rwkv. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/replicate/rwkv on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Instructions to use replicate/rwkv with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Kernels
How to use replicate/rwkv with Kernels:
# !pip install kernels from kernels import get_kernel # a version (or an explicit revision) is required; see the "Files and versions" tab for the available ones kernel = get_kernel("replicate/rwkv", version=1) - Notebooks
- Google Colab
- Kaggle
Uploaded using `kernel-builder`.
Browse filesFiles changed (34)
- .gitattributes+9
- build/torch210-cxx11-cu126-x86_64-linux/_ops.py+3-3
- build/torch210-cxx11-cu126-x86_64-linux/_rwkv_cuda_86f75d9.abi3.so → build/torch210-cxx11-cu126-x86_64-linux/_rwkv_cuda_dfdeb22.abi3.soRenamed+1-1
- build/torch210-cxx11-cu126-x86_64-linux/metadata.json+1-1
- build/torch210-cxx11-cu128-x86_64-linux/_ops.py+3-3
- build/torch211-cxx11-cu128-x86_64-linux/_rwkv_cuda_86f75d9.abi3.so → build/torch210-cxx11-cu128-x86_64-linux/_rwkv_cuda_dfdeb22.abi3.soRenamed+1-1
- build/torch210-cxx11-cu128-x86_64-linux/metadata.json+1-1
- build/torch210-cxx11-cu130-x86_64-linux/_ops.py+3-3
- build/torch211-cxx11-cu130-x86_64-linux/_rwkv_cuda_86f75d9.abi3.so → build/torch210-cxx11-cu130-x86_64-linux/_rwkv_cuda_dfdeb22.abi3.soRenamed+1-1
- build/torch210-cxx11-cu130-x86_64-linux/metadata.json+1-1
- build/torch211-cxx11-cu126-x86_64-linux/_ops.py+3-3
- build/torch211-cxx11-cu126-x86_64-linux/_rwkv_cuda_86f75d9.abi3.so → build/torch211-cxx11-cu126-x86_64-linux/_rwkv_cuda_dfdeb22.abi3.soRenamed+1-1
- build/torch211-cxx11-cu126-x86_64-linux/metadata.json+1-1
- build/torch211-cxx11-cu128-x86_64-linux/_ops.py+3-3
- build/torch210-cxx11-cu128-x86_64-linux/_rwkv_cuda_86f75d9.abi3.so → build/torch211-cxx11-cu128-x86_64-linux/_rwkv_cuda_dfdeb22.abi3.soRenamed+1-1
- build/torch211-cxx11-cu128-x86_64-linux/metadata.json+1-1
- build/torch211-cxx11-cu130-x86_64-linux/_ops.py+3-3
- build/torch210-cxx11-cu130-x86_64-linux/_rwkv_cuda_86f75d9.abi3.so → build/torch211-cxx11-cu130-x86_64-linux/_rwkv_cuda_dfdeb22.abi3.soRenamed+1-1
- build/torch211-cxx11-cu130-x86_64-linux/metadata.json+1-1
- build/torch212-cxx11-cu126-x86_64-linux/__init__.pyAdded+170
- build/torch212-cxx11-cu126-x86_64-linux/_ops.pyAdded+9
- build/torch212-cxx11-cu126-x86_64-linux/_rwkv_cuda_dfdeb22.abi3.soAdded+3
- build/torch212-cxx11-cu126-x86_64-linux/metadata.jsonAdded+15
- build/torch212-cxx11-cu126-x86_64-linux/rwkv/__init__.pyAdded+26
- build/torch212-cxx11-cu130-x86_64-linux/__init__.pyAdded+170
- build/torch212-cxx11-cu130-x86_64-linux/_ops.pyAdded+9
- build/torch212-cxx11-cu130-x86_64-linux/_rwkv_cuda_dfdeb22.abi3.soAdded+3
- build/torch212-cxx11-cu130-x86_64-linux/metadata.jsonAdded+17
- build/torch212-cxx11-cu130-x86_64-linux/rwkv/__init__.pyAdded+26
- build/torch212-cxx11-cu132-x86_64-linux/__init__.pyAdded+170
- build/torch212-cxx11-cu132-x86_64-linux/_ops.pyAdded+9
- build/torch212-cxx11-cu132-x86_64-linux/_rwkv_cuda_dfdeb22.abi3.soAdded+3
- build/torch212-cxx11-cu132-x86_64-linux/metadata.jsonAdded+17
- build/torch212-cxx11-cu132-x86_64-linux/rwkv/__init__.pyAdded+26
149 unmodified lines150151152149 unmodified linesbuild/torch211-cxx11-cu126-x86_64-linux/_rwkv_cuda_86f75d9.abi3.so filter=lfs diff=lfs merge=lfs -textbuild/torch211-cxx11-cu128-x86_64-linux/_rwkv_cuda_86f75d9.abi3.so filter=lfs diff=lfs merge=lfs -textbuild/torch211-cxx11-cu130-x86_64-linux/_rwkv_cuda_86f75d9.abi3.so filter=lfs diff=lfs merge=lfs -text149 unmodified lines150151152153154155156157158159160161149 unmodified linesbuild/torch211-cxx11-cu126-x86_64-linux/_rwkv_cuda_86f75d9.abi3.so filter=lfs diff=lfs merge=lfs -textbuild/torch211-cxx11-cu128-x86_64-linux/_rwkv_cuda_86f75d9.abi3.so filter=lfs diff=lfs merge=lfs -textbuild/torch211-cxx11-cu130-x86_64-linux/_rwkv_cuda_86f75d9.abi3.so filter=lfs diff=lfs merge=lfs -textbuild/torch210-cxx11-cu126-x86_64-linux/_rwkv_cuda_dfdeb22.abi3.so filter=lfs diff=lfs merge=lfs -textbuild/torch210-cxx11-cu128-x86_64-linux/_rwkv_cuda_dfdeb22.abi3.so filter=lfs diff=lfs merge=lfs -textbuild/torch210-cxx11-cu130-x86_64-linux/_rwkv_cuda_dfdeb22.abi3.so filter=lfs diff=lfs merge=lfs -textbuild/torch211-cxx11-cu126-x86_64-linux/_rwkv_cuda_dfdeb22.abi3.so filter=lfs diff=lfs merge=lfs -textbuild/torch211-cxx11-cu128-x86_64-linux/_rwkv_cuda_dfdeb22.abi3.so filter=lfs diff=lfs merge=lfs -textbuild/torch211-cxx11-cu130-x86_64-linux/_rwkv_cuda_dfdeb22.abi3.so filter=lfs diff=lfs merge=lfs -textbuild/torch212-cxx11-cu126-x86_64-linux/_rwkv_cuda_dfdeb22.abi3.so filter=lfs diff=lfs merge=lfs -textbuild/torch212-cxx11-cu130-x86_64-linux/_rwkv_cuda_dfdeb22.abi3.so filter=lfs diff=lfs merge=lfs -textbuild/torch212-cxx11-cu132-x86_64-linux/_rwkv_cuda_dfdeb22.abi3.so filter=lfs diff=lfs merge=lfs -text
123456789import torchfrom . import _rwkv_cuda_86f75d9ops = torch.ops._rwkv_cuda_86f75d9def add_op_namespace_prefix(op_name: str):"""Prefix op by namespace."""return f"_rwkv_cuda_86f75d9::{op_name}"123456789import torchfrom . import _rwkv_cuda_dfdeb22ops = torch.ops._rwkv_cuda_dfdeb22def add_op_namespace_prefix(op_name: str):"""Prefix op by namespace."""return f"_rwkv_cuda_dfdeb22::{op_name}"
123version https://git-lfs.github.com/spec/v1oid sha256:8f567d94d511889cb5dc70189c25c643175ddb28a9e3c4133d777d0b653f9357size 2116408123version https://git-lfs.github.com/spec/v1oid sha256:25f82150a3c8ab2f120f66ec5686d775a0d46d57ed13bf176cd2cc1f3f40be42size 2116408
123456{"name": "rwkv","id": "_rwkv_cuda_86f75d9","version": 1,"license": "Apache-2.0","python-depends": [],123456{"name": "rwkv","id": "_rwkv_cuda_dfdeb22","version": 1,"license": "Apache-2.0","python-depends": [],
123456789import torchfrom . import _rwkv_cuda_86f75d9ops = torch.ops._rwkv_cuda_86f75d9def add_op_namespace_prefix(op_name: str):"""Prefix op by namespace."""return f"_rwkv_cuda_86f75d9::{op_name}"123456789import torchfrom . import _rwkv_cuda_dfdeb22ops = torch.ops._rwkv_cuda_dfdeb22def add_op_namespace_prefix(op_name: str):"""Prefix op by namespace."""return f"_rwkv_cuda_dfdeb22::{op_name}"
123version https://git-lfs.github.com/spec/v1oid sha256:6e5d9ce2b8c9d6d37d3cb70263c2f0995f6d8a383fd6e2ffc290966747b11647size 2318768123version https://git-lfs.github.com/spec/v1oid sha256:867c86831769875c08bc91e9d371d5a0d6e55fef11694eb206d0a80491dc6188size 2318768
123456{"name": "rwkv","id": "_rwkv_cuda_86f75d9","version": 1,"license": "Apache-2.0","python-depends": [],123456{"name": "rwkv","id": "_rwkv_cuda_dfdeb22","version": 1,"license": "Apache-2.0","python-depends": [],
123456789import torchfrom . import _rwkv_cuda_86f75d9ops = torch.ops._rwkv_cuda_86f75d9def add_op_namespace_prefix(op_name: str):"""Prefix op by namespace."""return f"_rwkv_cuda_86f75d9::{op_name}"123456789import torchfrom . import _rwkv_cuda_dfdeb22ops = torch.ops._rwkv_cuda_dfdeb22def add_op_namespace_prefix(op_name: str):"""Prefix op by namespace."""return f"_rwkv_cuda_dfdeb22::{op_name}"
123version https://git-lfs.github.com/spec/v1oid sha256:a5fa0cced1e6b4698d42b8d609302395f655e5479a6ea4987be457bb738d659fsize 2340632123version https://git-lfs.github.com/spec/v1oid sha256:49dbbfafcf580a6ca7711d3af93f5ee126bd577e1bc7e3406d6506289798d3c0size 2340632
123456{"name": "rwkv","id": "_rwkv_cuda_86f75d9","version": 1,"license": "Apache-2.0","python-depends": [],123456{"name": "rwkv","id": "_rwkv_cuda_dfdeb22","version": 1,"license": "Apache-2.0","python-depends": [],
123456789import torchfrom . import _rwkv_cuda_86f75d9ops = torch.ops._rwkv_cuda_86f75d9def add_op_namespace_prefix(op_name: str):"""Prefix op by namespace."""return f"_rwkv_cuda_86f75d9::{op_name}"123456789import torchfrom . import _rwkv_cuda_dfdeb22ops = torch.ops._rwkv_cuda_dfdeb22def add_op_namespace_prefix(op_name: str):"""Prefix op by namespace."""return f"_rwkv_cuda_dfdeb22::{op_name}"
123version https://git-lfs.github.com/spec/v1oid sha256:be26d311bbd64bbd336a36388d38e6d1ff071c54b8e62b6bb15e02b7f227064bsize 2116408123version https://git-lfs.github.com/spec/v1oid sha256:93c09143e055a841ee1be16903fe9427bd9ce5c520170470b91b89d1d467cff8size 2116408
123456{"name": "rwkv","id": "_rwkv_cuda_86f75d9","version": 1,"license": "Apache-2.0","python-depends": [],123456{"name": "rwkv","id": "_rwkv_cuda_dfdeb22","version": 1,"license": "Apache-2.0","python-depends": [],
123456789import torchfrom . import _rwkv_cuda_86f75d9ops = torch.ops._rwkv_cuda_86f75d9def add_op_namespace_prefix(op_name: str):"""Prefix op by namespace."""return f"_rwkv_cuda_86f75d9::{op_name}"123456789import torchfrom . import _rwkv_cuda_dfdeb22ops = torch.ops._rwkv_cuda_dfdeb22def add_op_namespace_prefix(op_name: str):"""Prefix op by namespace."""return f"_rwkv_cuda_dfdeb22::{op_name}"
123version https://git-lfs.github.com/spec/v1oid sha256:7ea2502fa58b9778ed35b63c22b461febfbb49fcf83b6b69190127107bc4e10esize 2318768123version https://git-lfs.github.com/spec/v1oid sha256:adb67c2381a33252cec6424b3c5db0c727667dffc423cc5e31c9f390de3191fdsize 2318768
123456{"name": "rwkv","id": "_rwkv_cuda_86f75d9","version": 1,"license": "Apache-2.0","python-depends": [],123456{"name": "rwkv","id": "_rwkv_cuda_dfdeb22","version": 1,"license": "Apache-2.0","python-depends": [],
123456789import torchfrom . import _rwkv_cuda_86f75d9ops = torch.ops._rwkv_cuda_86f75d9def add_op_namespace_prefix(op_name: str):"""Prefix op by namespace."""return f"_rwkv_cuda_86f75d9::{op_name}"123456789import torchfrom . import _rwkv_cuda_dfdeb22ops = torch.ops._rwkv_cuda_dfdeb22def add_op_namespace_prefix(op_name: str):"""Prefix op by namespace."""return f"_rwkv_cuda_dfdeb22::{op_name}"
123version https://git-lfs.github.com/spec/v1oid sha256:8798eee6632178aa2e653716588bd9829c4c6367bf7f5771b256186b8f927807size 2340632123version https://git-lfs.github.com/spec/v1oid sha256:c9742ce997437c6ac57000ac5a7bb790a39e424208e45a8c224ecd09c7c5c88asize 2340632
123456{"name": "rwkv","id": "_rwkv_cuda_86f75d9","version": 1,"license": "Apache-2.0","python-depends": [],123456{"name": "rwkv","id": "_rwkv_cuda_dfdeb22","version": 1,"license": "Apache-2.0","python-depends": [],
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170from ._ops import opsfrom typing import Tuple, Any
# Use a broad Tensor alias to avoid importing torch at import time.from torch import Tensor
def forward(w: Tensor, u: Tensor, k: Tensor, v: Tensor, y: Tensor) -> None: """RWKV WKV forward pass (float32).
Runs the CUDA kernel and writes the result into ``y`` in-place.
Args: w: Decay weights, shape ``[C]``, dtype ``torch.float32``. u: Input tensor, shape ``[B, T, C]``, dtype ``torch.float32``. k: Key tensor, shape ``[B, T, C]``, dtype ``torch.float32``. v: Value tensor, shape ``[B, T, C]``, dtype ``torch.float32``. y: Output tensor, shape ``[B, T, C]``, dtype ``torch.float32`` (written in-place).
Notes: - All tensors must be on the same CUDA device. - Shapes must agree on ``B``, ``T`` and ``C``. """ _validate_device_match((w, u, k, v, y)) ops.forward(w, u, k, v, y)
def forward_bf16(w: Tensor, u: Tensor, k: Tensor, v: Tensor, y: Tensor) -> None: """RWKV WKV forward pass (bfloat16 inputs/outputs, float32 ``w``).
Runs the CUDA kernel and writes the result into ``y`` in-place.
Args: w: Decay weights, shape ``[C]``, dtype ``torch.float32``. u: Input tensor, shape ``[B, T, C]``, dtype ``torch.bfloat16``. k: Key tensor, shape ``[B, T, C]``, dtype ``torch.bfloat16``. v: Value tensor, shape ``[B, T, C]``, dtype ``torch.bfloat16``. y: Output tensor, shape ``[B, T, C]``, dtype ``torch.bfloat16`` (written in-place).
Notes: - All tensors must be on the same CUDA device. - Shapes must agree on ``B``, ``T`` and ``C``. """ _validate_device_match((w, u, k, v, y)) ops.forward_bf16(w, u, k, v, y)
def forward_with_state(w: Tensor, u: Tensor, k: Tensor, v: Tensor, y: Tensor, s: Tensor) -> None: """RWKV WKV forward pass with persistent state (float32).
Runs the CUDA kernel using and updating state ``s`` and writes the result into ``y``.
Args: w: Decay weights, shape ``[C]``, dtype ``torch.float32``. u: Input tensor, shape ``[B, T, C]``, dtype ``torch.float32``. k: Key tensor, shape ``[B, T, C]``, dtype ``torch.float32``. v: Value tensor, shape ``[B, T, C]``, dtype ``torch.float32``. y: Output tensor, shape ``[B, T, C]``, dtype ``torch.float32`` (written in-place). s: Stateful tensor, shape ``[B, C]``, dtype ``torch.float32`` (updated in-place).
Notes: - All tensors must be on the same CUDA device. - Shapes must agree on ``B`` and ``C``; ``y`` shares ``[B, T, C]`` with inputs. """ _validate_device_match((w, u, k, v, y, s)) ops.forward_with_state(w, u, k, v, y, s)
def forward_with_state_bf16(w: Tensor, u: Tensor, k: Tensor, v: Tensor, y: Tensor, s: Tensor) -> None: """RWKV WKV forward pass with persistent state (bfloat16 inputs/outputs, float32 ``w`` and ``s``).
Runs the CUDA kernel using and updating state ``s`` and writes the result into ``y``.
Args: w: Decay weights, shape ``[C]``, dtype ``torch.float32``. u: Input tensor, shape ``[B, T, C]``, dtype ``torch.bfloat16``. k: Key tensor, shape ``[B, T, C]``, dtype ``torch.bfloat16``. v: Value tensor, shape ``[B, T, C]``, dtype ``torch.bfloat16``. y: Output tensor, shape ``[B, T, C]``, dtype ``torch.bfloat16`` (written in-place). s: Stateful tensor, shape ``[B, C]``, dtype ``torch.float32`` (updated in-place).
Notes: - All tensors must be on the same CUDA device. - Shapes must agree on ``B`` and ``C``; ``y`` shares ``[B, T, C]`` with inputs. """ _validate_device_match((w, u, k, v, y, s)) ops.forward_with_state_bf16(w, u, k, v, y, s)
def backward( w: Tensor, u: Tensor, k: Tensor, v: Tensor, y: Tensor, gy: Tensor, gw: Tensor, gu: Tensor, gk: Tensor, gv: Tensor,) -> None: """RWKV WKV backward pass (float32).
Writes gradients into the provided tensors in-place.
Args: w: Decay weights, shape ``[C]``, dtype ``torch.float32``. u, k, v, y: Forward-pass tensors, shape ``[B, T, C]``, dtype ``torch.float32``. gy: Gradient of ``y``, shape ``[B, T, C]``, dtype ``torch.float32``. gw: Gradient for ``w``, shape ``[C]``, dtype ``torch.float32`` (written in-place). gu, gk, gv: Gradients for ``u``, ``k``, ``v`` respectively, shape ``[B, T, C]``, dtype ``torch.float32`` (written in-place).
Notes: - All tensors must be on the same CUDA device. - Shapes must agree on ``B``, ``T`` and ``C``. """ _validate_device_match((w, u, k, v, y, gy, gw, gu, gk, gv)) ops.backward(w, u, k, v, y, gy, gw, gu, gk, gv)
def backward_bf16( w: Tensor, u: Tensor, k: Tensor, v: Tensor, y: Tensor, gy: Tensor, gw: Tensor, gu: Tensor, gk: Tensor, gv: Tensor,) -> None: """RWKV WKV backward pass (bfloat16 inputs/outputs/gradients, float32 ``w``).
Writes gradients into the provided tensors in-place.
Args: w: Decay weights, shape ``[C]``, dtype ``torch.float32``. u, k, v, y: Forward-pass tensors, shape ``[B, T, C]``, dtype ``torch.bfloat16``. gy: Gradient of ``y``, shape ``[B, T, C]``, dtype ``torch.bfloat16``. gw: Gradient for ``w``, shape ``[C]``, dtype ``torch.bfloat16`` (written in-place). gu, gk, gv: Gradients for ``u``, ``k``, ``v`` respectively, shape ``[B, T, C]``, dtype ``torch.bfloat16`` (written in-place).
Notes: - All tensors must be on the same CUDA device. - Shapes must agree on ``B``, ``T`` and ``C``. """ _validate_device_match((w, u, k, v, y, gy, gw, gu, gk, gv)) ops.backward_bf16(w, u, k, v, y, gy, gw, gu, gk, gv)
def _validate_device_match(tensors: Tuple[Tensor, ...]) -> None: """Minimal runtime validation that all tensors live on the same CUDA device.""" if not tensors: return device = tensors[0].device if not device.type == "cuda": raise RuntimeError("RWKV CUDA ops require CUDA tensors") for t in tensors[1:]: if t.device != device: raise RuntimeError("All tensors must be on the same CUDA device")
__all__ = [ "forward", "forward_bf16", "forward_with_state", "forward_with_state_bf16", "backward", "backward_bf16",]123456789import torchfrom . import _rwkv_cuda_dfdeb22ops = torch.ops._rwkv_cuda_dfdeb22
def add_op_namespace_prefix(op_name: str): """ Prefix op by namespace. """ return f"_rwkv_cuda_dfdeb22::{op_name}"123version https://git-lfs.github.com/spec/v1oid sha256:d42f0568f9eccc5d9af13e66d6c2d53149d0fbff7bb7964abab4a1a1c69bafc2size 2111384123456789101112131415{ "name": "rwkv", "id": "_rwkv_cuda_dfdeb22", "version": 1, "license": "Apache-2.0", "python-depends": [], "backend": { "type": "cuda", "archs": [ "8.0", "8.9", "9.0" ] }}1234567891011121314151617181920212223242526import ctypesimport importlib.utilimport sysfrom pathlib import Pathfrom types import ModuleType
def _import_from_path(file_path: Path) -> ModuleType: # We cannot use the module name as-is, after adding it to `sys.modules`, # it would also be used for other imports. So, we make a module name that # depends on the path for it to be unique using the hex-encoded hash of # the path. path_hash = "{:x}".format(ctypes.c_size_t(hash(file_path.absolute())).value) module_name = path_hash spec = importlib.util.spec_from_file_location(module_name, file_path) if spec is None: raise ImportError(f"Cannot load spec for {module_name} from {file_path}") module = importlib.util.module_from_spec(spec) if module is None: raise ImportError(f"Cannot load module {module_name} from spec") sys.modules[module_name] = module spec.loader.exec_module(module) # type: ignore return module
globals().update(vars(_import_from_path(Path(__file__).parent.parent / "__init__.py")))123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170from ._ops import opsfrom typing import Tuple, Any
# Use a broad Tensor alias to avoid importing torch at import time.from torch import Tensor
def forward(w: Tensor, u: Tensor, k: Tensor, v: Tensor, y: Tensor) -> None: """RWKV WKV forward pass (float32).
Runs the CUDA kernel and writes the result into ``y`` in-place.
Args: w: Decay weights, shape ``[C]``, dtype ``torch.float32``. u: Input tensor, shape ``[B, T, C]``, dtype ``torch.float32``. k: Key tensor, shape ``[B, T, C]``, dtype ``torch.float32``. v: Value tensor, shape ``[B, T, C]``, dtype ``torch.float32``. y: Output tensor, shape ``[B, T, C]``, dtype ``torch.float32`` (written in-place).
Notes: - All tensors must be on the same CUDA device. - Shapes must agree on ``B``, ``T`` and ``C``. """ _validate_device_match((w, u, k, v, y)) ops.forward(w, u, k, v, y)
def forward_bf16(w: Tensor, u: Tensor, k: Tensor, v: Tensor, y: Tensor) -> None: """RWKV WKV forward pass (bfloat16 inputs/outputs, float32 ``w``).
Runs the CUDA kernel and writes the result into ``y`` in-place.
Args: w: Decay weights, shape ``[C]``, dtype ``torch.float32``. u: Input tensor, shape ``[B, T, C]``, dtype ``torch.bfloat16``. k: Key tensor, shape ``[B, T, C]``, dtype ``torch.bfloat16``. v: Value tensor, shape ``[B, T, C]``, dtype ``torch.bfloat16``. y: Output tensor, shape ``[B, T, C]``, dtype ``torch.bfloat16`` (written in-place).
Notes: - All tensors must be on the same CUDA device. - Shapes must agree on ``B``, ``T`` and ``C``. """ _validate_device_match((w, u, k, v, y)) ops.forward_bf16(w, u, k, v, y)
def forward_with_state(w: Tensor, u: Tensor, k: Tensor, v: Tensor, y: Tensor, s: Tensor) -> None: """RWKV WKV forward pass with persistent state (float32).
Runs the CUDA kernel using and updating state ``s`` and writes the result into ``y``.
Args: w: Decay weights, shape ``[C]``, dtype ``torch.float32``. u: Input tensor, shape ``[B, T, C]``, dtype ``torch.float32``. k: Key tensor, shape ``[B, T, C]``, dtype ``torch.float32``. v: Value tensor, shape ``[B, T, C]``, dtype ``torch.float32``. y: Output tensor, shape ``[B, T, C]``, dtype ``torch.float32`` (written in-place). s: Stateful tensor, shape ``[B, C]``, dtype ``torch.float32`` (updated in-place).
Notes: - All tensors must be on the same CUDA device. - Shapes must agree on ``B`` and ``C``; ``y`` shares ``[B, T, C]`` with inputs. """ _validate_device_match((w, u, k, v, y, s)) ops.forward_with_state(w, u, k, v, y, s)
def forward_with_state_bf16(w: Tensor, u: Tensor, k: Tensor, v: Tensor, y: Tensor, s: Tensor) -> None: """RWKV WKV forward pass with persistent state (bfloat16 inputs/outputs, float32 ``w`` and ``s``).
Runs the CUDA kernel using and updating state ``s`` and writes the result into ``y``.
Args: w: Decay weights, shape ``[C]``, dtype ``torch.float32``. u: Input tensor, shape ``[B, T, C]``, dtype ``torch.bfloat16``. k: Key tensor, shape ``[B, T, C]``, dtype ``torch.bfloat16``. v: Value tensor, shape ``[B, T, C]``, dtype ``torch.bfloat16``. y: Output tensor, shape ``[B, T, C]``, dtype ``torch.bfloat16`` (written in-place). s: Stateful tensor, shape ``[B, C]``, dtype ``torch.float32`` (updated in-place).
Notes: - All tensors must be on the same CUDA device. - Shapes must agree on ``B`` and ``C``; ``y`` shares ``[B, T, C]`` with inputs. """ _validate_device_match((w, u, k, v, y, s)) ops.forward_with_state_bf16(w, u, k, v, y, s)
def backward( w: Tensor, u: Tensor, k: Tensor, v: Tensor, y: Tensor, gy: Tensor, gw: Tensor, gu: Tensor, gk: Tensor, gv: Tensor,) -> None: """RWKV WKV backward pass (float32).
Writes gradients into the provided tensors in-place.
Args: w: Decay weights, shape ``[C]``, dtype ``torch.float32``. u, k, v, y: Forward-pass tensors, shape ``[B, T, C]``, dtype ``torch.float32``. gy: Gradient of ``y``, shape ``[B, T, C]``, dtype ``torch.float32``. gw: Gradient for ``w``, shape ``[C]``, dtype ``torch.float32`` (written in-place). gu, gk, gv: Gradients for ``u``, ``k``, ``v`` respectively, shape ``[B, T, C]``, dtype ``torch.float32`` (written in-place).
Notes: - All tensors must be on the same CUDA device. - Shapes must agree on ``B``, ``T`` and ``C``. """ _validate_device_match((w, u, k, v, y, gy, gw, gu, gk, gv)) ops.backward(w, u, k, v, y, gy, gw, gu, gk, gv)
def backward_bf16( w: Tensor, u: Tensor, k: Tensor, v: Tensor, y: Tensor, gy: Tensor, gw: Tensor, gu: Tensor, gk: Tensor, gv: Tensor,) -> None: """RWKV WKV backward pass (bfloat16 inputs/outputs/gradients, float32 ``w``).
Writes gradients into the provided tensors in-place.
Args: w: Decay weights, shape ``[C]``, dtype ``torch.float32``. u, k, v, y: Forward-pass tensors, shape ``[B, T, C]``, dtype ``torch.bfloat16``. gy: Gradient of ``y``, shape ``[B, T, C]``, dtype ``torch.bfloat16``. gw: Gradient for ``w``, shape ``[C]``, dtype ``torch.bfloat16`` (written in-place). gu, gk, gv: Gradients for ``u``, ``k``, ``v`` respectively, shape ``[B, T, C]``, dtype ``torch.bfloat16`` (written in-place).
Notes: - All tensors must be on the same CUDA device. - Shapes must agree on ``B``, ``T`` and ``C``. """ _validate_device_match((w, u, k, v, y, gy, gw, gu, gk, gv)) ops.backward_bf16(w, u, k, v, y, gy, gw, gu, gk, gv)
def _validate_device_match(tensors: Tuple[Tensor, ...]) -> None: """Minimal runtime validation that all tensors live on the same CUDA device.""" if not tensors: return device = tensors[0].device if not device.type == "cuda": raise RuntimeError("RWKV CUDA ops require CUDA tensors") for t in tensors[1:]: if t.device != device: raise RuntimeError("All tensors must be on the same CUDA device")
__all__ = [ "forward", "forward_bf16", "forward_with_state", "forward_with_state_bf16", "backward", "backward_bf16",]123456789import torchfrom . import _rwkv_cuda_dfdeb22ops = torch.ops._rwkv_cuda_dfdeb22
def add_op_namespace_prefix(op_name: str): """ Prefix op by namespace. """ return f"_rwkv_cuda_dfdeb22::{op_name}"123version https://git-lfs.github.com/spec/v1oid sha256:e360f5dc52591a543d0dbf513687bf994c60916c5bbcb60626a58232c800ec78size 23355601234567891011121314151617{ "name": "rwkv", "id": "_rwkv_cuda_dfdeb22", "version": 1, "license": "Apache-2.0", "python-depends": [], "backend": { "type": "cuda", "archs": [ "10.0", "12.0", "8.0", "8.9", "9.0" ] }}1234567891011121314151617181920212223242526import ctypesimport importlib.utilimport sysfrom pathlib import Pathfrom types import ModuleType
def _import_from_path(file_path: Path) -> ModuleType: # We cannot use the module name as-is, after adding it to `sys.modules`, # it would also be used for other imports. So, we make a module name that # depends on the path for it to be unique using the hex-encoded hash of # the path. path_hash = "{:x}".format(ctypes.c_size_t(hash(file_path.absolute())).value) module_name = path_hash spec = importlib.util.spec_from_file_location(module_name, file_path) if spec is None: raise ImportError(f"Cannot load spec for {module_name} from {file_path}") module = importlib.util.module_from_spec(spec) if module is None: raise ImportError(f"Cannot load module {module_name} from spec") sys.modules[module_name] = module spec.loader.exec_module(module) # type: ignore return module
globals().update(vars(_import_from_path(Path(__file__).parent.parent / "__init__.py")))123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170from ._ops import opsfrom typing import Tuple, Any
# Use a broad Tensor alias to avoid importing torch at import time.from torch import Tensor
def forward(w: Tensor, u: Tensor, k: Tensor, v: Tensor, y: Tensor) -> None: """RWKV WKV forward pass (float32).
Runs the CUDA kernel and writes the result into ``y`` in-place.
Args: w: Decay weights, shape ``[C]``, dtype ``torch.float32``. u: Input tensor, shape ``[B, T, C]``, dtype ``torch.float32``. k: Key tensor, shape ``[B, T, C]``, dtype ``torch.float32``. v: Value tensor, shape ``[B, T, C]``, dtype ``torch.float32``. y: Output tensor, shape ``[B, T, C]``, dtype ``torch.float32`` (written in-place).
Notes: - All tensors must be on the same CUDA device. - Shapes must agree on ``B``, ``T`` and ``C``. """ _validate_device_match((w, u, k, v, y)) ops.forward(w, u, k, v, y)
def forward_bf16(w: Tensor, u: Tensor, k: Tensor, v: Tensor, y: Tensor) -> None: """RWKV WKV forward pass (bfloat16 inputs/outputs, float32 ``w``).
Runs the CUDA kernel and writes the result into ``y`` in-place.
Args: w: Decay weights, shape ``[C]``, dtype ``torch.float32``. u: Input tensor, shape ``[B, T, C]``, dtype ``torch.bfloat16``. k: Key tensor, shape ``[B, T, C]``, dtype ``torch.bfloat16``. v: Value tensor, shape ``[B, T, C]``, dtype ``torch.bfloat16``. y: Output tensor, shape ``[B, T, C]``, dtype ``torch.bfloat16`` (written in-place).
Notes: - All tensors must be on the same CUDA device. - Shapes must agree on ``B``, ``T`` and ``C``. """ _validate_device_match((w, u, k, v, y)) ops.forward_bf16(w, u, k, v, y)
def forward_with_state(w: Tensor, u: Tensor, k: Tensor, v: Tensor, y: Tensor, s: Tensor) -> None: """RWKV WKV forward pass with persistent state (float32).
Runs the CUDA kernel using and updating state ``s`` and writes the result into ``y``.
Args: w: Decay weights, shape ``[C]``, dtype ``torch.float32``. u: Input tensor, shape ``[B, T, C]``, dtype ``torch.float32``. k: Key tensor, shape ``[B, T, C]``, dtype ``torch.float32``. v: Value tensor, shape ``[B, T, C]``, dtype ``torch.float32``. y: Output tensor, shape ``[B, T, C]``, dtype ``torch.float32`` (written in-place). s: Stateful tensor, shape ``[B, C]``, dtype ``torch.float32`` (updated in-place).
Notes: - All tensors must be on the same CUDA device. - Shapes must agree on ``B`` and ``C``; ``y`` shares ``[B, T, C]`` with inputs. """ _validate_device_match((w, u, k, v, y, s)) ops.forward_with_state(w, u, k, v, y, s)
def forward_with_state_bf16(w: Tensor, u: Tensor, k: Tensor, v: Tensor, y: Tensor, s: Tensor) -> None: """RWKV WKV forward pass with persistent state (bfloat16 inputs/outputs, float32 ``w`` and ``s``).
Runs the CUDA kernel using and updating state ``s`` and writes the result into ``y``.
Args: w: Decay weights, shape ``[C]``, dtype ``torch.float32``. u: Input tensor, shape ``[B, T, C]``, dtype ``torch.bfloat16``. k: Key tensor, shape ``[B, T, C]``, dtype ``torch.bfloat16``. v: Value tensor, shape ``[B, T, C]``, dtype ``torch.bfloat16``. y: Output tensor, shape ``[B, T, C]``, dtype ``torch.bfloat16`` (written in-place). s: Stateful tensor, shape ``[B, C]``, dtype ``torch.float32`` (updated in-place).
Notes: - All tensors must be on the same CUDA device. - Shapes must agree on ``B`` and ``C``; ``y`` shares ``[B, T, C]`` with inputs. """ _validate_device_match((w, u, k, v, y, s)) ops.forward_with_state_bf16(w, u, k, v, y, s)
def backward( w: Tensor, u: Tensor, k: Tensor, v: Tensor, y: Tensor, gy: Tensor, gw: Tensor, gu: Tensor, gk: Tensor, gv: Tensor,) -> None: """RWKV WKV backward pass (float32).
Writes gradients into the provided tensors in-place.
Args: w: Decay weights, shape ``[C]``, dtype ``torch.float32``. u, k, v, y: Forward-pass tensors, shape ``[B, T, C]``, dtype ``torch.float32``. gy: Gradient of ``y``, shape ``[B, T, C]``, dtype ``torch.float32``. gw: Gradient for ``w``, shape ``[C]``, dtype ``torch.float32`` (written in-place). gu, gk, gv: Gradients for ``u``, ``k``, ``v`` respectively, shape ``[B, T, C]``, dtype ``torch.float32`` (written in-place).
Notes: - All tensors must be on the same CUDA device. - Shapes must agree on ``B``, ``T`` and ``C``. """ _validate_device_match((w, u, k, v, y, gy, gw, gu, gk, gv)) ops.backward(w, u, k, v, y, gy, gw, gu, gk, gv)
def backward_bf16( w: Tensor, u: Tensor, k: Tensor, v: Tensor, y: Tensor, gy: Tensor, gw: Tensor, gu: Tensor, gk: Tensor, gv: Tensor,) -> None: """RWKV WKV backward pass (bfloat16 inputs/outputs/gradients, float32 ``w``).
Writes gradients into the provided tensors in-place.
Args: w: Decay weights, shape ``[C]``, dtype ``torch.float32``. u, k, v, y: Forward-pass tensors, shape ``[B, T, C]``, dtype ``torch.bfloat16``. gy: Gradient of ``y``, shape ``[B, T, C]``, dtype ``torch.bfloat16``. gw: Gradient for ``w``, shape ``[C]``, dtype ``torch.bfloat16`` (written in-place). gu, gk, gv: Gradients for ``u``, ``k``, ``v`` respectively, shape ``[B, T, C]``, dtype ``torch.bfloat16`` (written in-place).
Notes: - All tensors must be on the same CUDA device. - Shapes must agree on ``B``, ``T`` and ``C``. """ _validate_device_match((w, u, k, v, y, gy, gw, gu, gk, gv)) ops.backward_bf16(w, u, k, v, y, gy, gw, gu, gk, gv)
def _validate_device_match(tensors: Tuple[Tensor, ...]) -> None: """Minimal runtime validation that all tensors live on the same CUDA device.""" if not tensors: return device = tensors[0].device if not device.type == "cuda": raise RuntimeError("RWKV CUDA ops require CUDA tensors") for t in tensors[1:]: if t.device != device: raise RuntimeError("All tensors must be on the same CUDA device")
__all__ = [ "forward", "forward_bf16", "forward_with_state", "forward_with_state_bf16", "backward", "backward_bf16",]123456789import torchfrom . import _rwkv_cuda_dfdeb22ops = torch.ops._rwkv_cuda_dfdeb22
def add_op_namespace_prefix(op_name: str): """ Prefix op by namespace. """ return f"_rwkv_cuda_dfdeb22::{op_name}"123version https://git-lfs.github.com/spec/v1oid sha256:662de8c2b38b29045813a8c8008d90fa0ceabd54979ab643f4a7cfa56e3fbec3size 23743681234567891011121314151617{ "name": "rwkv", "id": "_rwkv_cuda_dfdeb22", "version": 1, "license": "Apache-2.0", "python-depends": [], "backend": { "type": "cuda", "archs": [ "10.0", "12.0", "8.0", "8.9", "9.0" ] }}1234567891011121314151617181920212223242526import ctypesimport importlib.utilimport sysfrom pathlib import Pathfrom types import ModuleType
def _import_from_path(file_path: Path) -> ModuleType: # We cannot use the module name as-is, after adding it to `sys.modules`, # it would also be used for other imports. So, we make a module name that # depends on the path for it to be unique using the hex-encoded hash of # the path. path_hash = "{:x}".format(ctypes.c_size_t(hash(file_path.absolute())).value) module_name = path_hash spec = importlib.util.spec_from_file_location(module_name, file_path) if spec is None: raise ImportError(f"Cannot load spec for {module_name} from {file_path}") module = importlib.util.module_from_spec(spec) if module is None: raise ImportError(f"Cannot load module {module_name} from spec") sys.modules[module_name] = module spec.loader.exec_module(module) # type: ignore return module
globals().update(vars(_import_from_path(Path(__file__).parent.parent / "__init__.py"))).gitattributes+9
build/torch210-cxx11-cu126-x86_64-linux/_ops.py+3-3
build/torch210-cxx11-cu126-x86_64-linux/_rwkv_cuda_86f75d9.abi3.so → build/torch210-cxx11-cu126-x86_64-linux/_rwkv_cuda_dfdeb22.abi3.soRenamed+1-1
build/torch210-cxx11-cu126-x86_64-linux/metadata.json+1-1
build/torch210-cxx11-cu128-x86_64-linux/_ops.py+3-3
build/torch211-cxx11-cu128-x86_64-linux/_rwkv_cuda_86f75d9.abi3.so → build/torch210-cxx11-cu128-x86_64-linux/_rwkv_cuda_dfdeb22.abi3.soRenamed+1-1
build/torch210-cxx11-cu128-x86_64-linux/metadata.json+1-1
build/torch210-cxx11-cu130-x86_64-linux/_ops.py+3-3
build/torch211-cxx11-cu130-x86_64-linux/_rwkv_cuda_86f75d9.abi3.so → build/torch210-cxx11-cu130-x86_64-linux/_rwkv_cuda_dfdeb22.abi3.soRenamed+1-1
build/torch210-cxx11-cu130-x86_64-linux/metadata.json+1-1
build/torch211-cxx11-cu126-x86_64-linux/_ops.py+3-3
build/torch211-cxx11-cu126-x86_64-linux/_rwkv_cuda_86f75d9.abi3.so → build/torch211-cxx11-cu126-x86_64-linux/_rwkv_cuda_dfdeb22.abi3.soRenamed+1-1
build/torch211-cxx11-cu126-x86_64-linux/metadata.json+1-1
build/torch211-cxx11-cu128-x86_64-linux/_ops.py+3-3
build/torch210-cxx11-cu128-x86_64-linux/_rwkv_cuda_86f75d9.abi3.so → build/torch211-cxx11-cu128-x86_64-linux/_rwkv_cuda_dfdeb22.abi3.soRenamed+1-1
build/torch211-cxx11-cu128-x86_64-linux/metadata.json+1-1
build/torch211-cxx11-cu130-x86_64-linux/_ops.py+3-3
build/torch210-cxx11-cu130-x86_64-linux/_rwkv_cuda_86f75d9.abi3.so → build/torch211-cxx11-cu130-x86_64-linux/_rwkv_cuda_dfdeb22.abi3.soRenamed+1-1
build/torch211-cxx11-cu130-x86_64-linux/metadata.json+1-1
build/torch212-cxx11-cu126-x86_64-linux/__init__.pyAdded+170
build/torch212-cxx11-cu126-x86_64-linux/_ops.pyAdded+9
build/torch212-cxx11-cu126-x86_64-linux/_rwkv_cuda_dfdeb22.abi3.soAdded+3
build/torch212-cxx11-cu126-x86_64-linux/metadata.jsonAdded+15
build/torch212-cxx11-cu126-x86_64-linux/rwkv/__init__.pyAdded+26
build/torch212-cxx11-cu130-x86_64-linux/__init__.pyAdded+170
build/torch212-cxx11-cu130-x86_64-linux/_ops.pyAdded+9
build/torch212-cxx11-cu130-x86_64-linux/_rwkv_cuda_dfdeb22.abi3.soAdded+3
build/torch212-cxx11-cu130-x86_64-linux/metadata.jsonAdded+17
build/torch212-cxx11-cu130-x86_64-linux/rwkv/__init__.pyAdded+26
build/torch212-cxx11-cu132-x86_64-linux/__init__.pyAdded+170
build/torch212-cxx11-cu132-x86_64-linux/_ops.pyAdded+9
build/torch212-cxx11-cu132-x86_64-linux/_rwkv_cuda_dfdeb22.abi3.soAdded+3
build/torch212-cxx11-cu132-x86_64-linux/metadata.jsonAdded+17
build/torch212-cxx11-cu132-x86_64-linux/rwkv/__init__.pyAdded+26