Run this model with HF Jobs
Run kernels-community/paged-attention 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/kernels-community/paged-attention. 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/kernels-community/paged-attention 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 kernels-community/paged-attention with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Kernels
How to use kernels-community/paged-attention 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("kernels-community/paged-attention", version=1) - Notebooks
- Google Colab
- Kaggle
Uploaded using `kernel-builder`.
Browse filesFiles changed (39)
- build/torch210-cxx11-cu126-aarch64-linux/_ops.py+3-3
- build/torch210-cxx11-cu126-aarch64-linux/_paged_attention_cuda_86f75d9.abi3.so → build/torch210-cxx11-cu126-aarch64-linux/_paged_attention_cuda_2567cd6.abi3.soRenamed+1-1
- build/torch210-cxx11-cu126-aarch64-linux/metadata.json+1-1
- build/torch210-cxx11-cu128-aarch64-linux/_ops.py+3-3
- build/torch210-cxx11-cu128-aarch64-linux/_paged_attention_cuda_86f75d9.abi3.so → build/torch210-cxx11-cu128-aarch64-linux/_paged_attention_cuda_2567cd6.abi3.soRenamed+1-1
- build/torch210-cxx11-cu128-aarch64-linux/metadata.json+1-1
- build/torch210-cxx11-cu130-aarch64-linux/_ops.py+3-3
- build/torch210-cxx11-cu130-aarch64-linux/_paged_attention_cuda_86f75d9.abi3.so → build/torch210-cxx11-cu130-aarch64-linux/_paged_attention_cuda_2567cd6.abi3.soRenamed+1-1
- build/torch210-cxx11-cu130-aarch64-linux/metadata.json+1-1
- build/torch211-cxx11-cu126-aarch64-linux/_ops.py+3-3
- build/torch211-cxx11-cu126-aarch64-linux/_paged_attention_cuda_86f75d9.abi3.so → build/torch211-cxx11-cu126-aarch64-linux/_paged_attention_cuda_2567cd6.abi3.soRenamed+1-1
- build/torch211-cxx11-cu126-aarch64-linux/metadata.json+1-1
- build/torch211-cxx11-cu128-aarch64-linux/_ops.py+3-3
- build/torch211-cxx11-cu128-aarch64-linux/_paged_attention_cuda_86f75d9.abi3.so → build/torch211-cxx11-cu128-aarch64-linux/_paged_attention_cuda_2567cd6.abi3.soRenamed+1-1
- build/torch211-cxx11-cu128-aarch64-linux/metadata.json+1-1
- build/torch211-cxx11-cu130-aarch64-linux/_ops.py+3-3
- build/torch211-cxx11-cu130-aarch64-linux/_paged_attention_cuda_86f75d9.abi3.so → build/torch211-cxx11-cu130-aarch64-linux/_paged_attention_cuda_2567cd6.abi3.soRenamed+1-1
- build/torch211-cxx11-cu130-aarch64-linux/metadata.json+1-1
- build/torch212-cxx11-cu126-aarch64-linux/__init__.pyAdded+21
- build/torch212-cxx11-cu126-aarch64-linux/_custom_ops.pyAdded+173
- build/torch212-cxx11-cu126-aarch64-linux/_ops.pyAdded+9
- build/torch212-cxx11-cu126-aarch64-linux/_paged_attention_cuda_2567cd6.abi3.soAdded+3
- build/torch212-cxx11-cu126-aarch64-linux/metadata.jsonAdded+20
- build/torch212-cxx11-cu126-aarch64-linux/paged_attention/__init__.pyAdded+26
- build/torch212-cxx11-cu126-aarch64-linux/platforms.pyAdded+92
- build/torch212-cxx11-cu130-aarch64-linux/__init__.pyAdded+21
- build/torch212-cxx11-cu130-aarch64-linux/_custom_ops.pyAdded+173
- build/torch212-cxx11-cu130-aarch64-linux/_ops.pyAdded+9
- build/torch212-cxx11-cu130-aarch64-linux/_paged_attention_cuda_2567cd6.abi3.soAdded+3
- build/torch212-cxx11-cu130-aarch64-linux/metadata.jsonAdded+21
- build/torch212-cxx11-cu130-aarch64-linux/paged_attention/__init__.pyAdded+26
- build/torch212-cxx11-cu130-aarch64-linux/platforms.pyAdded+92
- build/torch212-cxx11-cu132-aarch64-linux/__init__.pyAdded+21
- build/torch212-cxx11-cu132-aarch64-linux/_custom_ops.pyAdded+173
- build/torch212-cxx11-cu132-aarch64-linux/_ops.pyAdded+9
- build/torch212-cxx11-cu132-aarch64-linux/_paged_attention_cuda_2567cd6.abi3.soAdded+3
- build/torch212-cxx11-cu132-aarch64-linux/metadata.jsonAdded+21
- build/torch212-cxx11-cu132-aarch64-linux/paged_attention/__init__.pyAdded+26
- build/torch212-cxx11-cu132-aarch64-linux/platforms.pyAdded+92
123456789import torchfrom . import _paged_attention_cuda_86f75d9ops = torch.ops._paged_attention_cuda_86f75d9def add_op_namespace_prefix(op_name: str):"""Prefix op by namespace."""return f"_paged_attention_cuda_86f75d9::{op_name}"123456789import torchfrom . import _paged_attention_cuda_2567cd6ops = torch.ops._paged_attention_cuda_2567cd6def add_op_namespace_prefix(op_name: str):"""Prefix op by namespace."""return f"_paged_attention_cuda_2567cd6::{op_name}"
123version https://git-lfs.github.com/spec/v1oid sha256:ccb30dbb13f2f96b9dbb69415bdaa8edca7caaf9b4ff54fd003c0ca7c143d432size 140013424123version https://git-lfs.github.com/spec/v1oid sha256:9e3b1c9befa49206a2bd8d5e5cdf577c72d12621018a909013811ccf68142772size 140013424
123456{"name": "paged-attention","id": "_paged_attention_cuda_86f75d9","version": 1,"license": "Apache-2.0","python-depends": [],123456{"name": "paged-attention","id": "_paged_attention_cuda_2567cd6","version": 1,"license": "Apache-2.0","python-depends": [],
123456789import torchfrom . import _paged_attention_cuda_86f75d9ops = torch.ops._paged_attention_cuda_86f75d9def add_op_namespace_prefix(op_name: str):"""Prefix op by namespace."""return f"_paged_attention_cuda_86f75d9::{op_name}"123456789import torchfrom . import _paged_attention_cuda_2567cd6ops = torch.ops._paged_attention_cuda_2567cd6def add_op_namespace_prefix(op_name: str):"""Prefix op by namespace."""return f"_paged_attention_cuda_2567cd6::{op_name}"
123version https://git-lfs.github.com/spec/v1oid sha256:c7577cfb5ab4acf981e99dcd288d4c5b416bddac1add8724ead08ade73b7432csize 167603936123version https://git-lfs.github.com/spec/v1oid sha256:53dfe24d07a74182292d12d6d6376644305e1fe715f7b58f101e324a28b9d1f1size 167603936
123456{"name": "paged-attention","id": "_paged_attention_cuda_86f75d9","version": 1,"license": "Apache-2.0","python-depends": [],123456{"name": "paged-attention","id": "_paged_attention_cuda_2567cd6","version": 1,"license": "Apache-2.0","python-depends": [],
123456789import torchfrom . import _paged_attention_cuda_86f75d9ops = torch.ops._paged_attention_cuda_86f75d9def add_op_namespace_prefix(op_name: str):"""Prefix op by namespace."""return f"_paged_attention_cuda_86f75d9::{op_name}"123456789import torchfrom . import _paged_attention_cuda_2567cd6ops = torch.ops._paged_attention_cuda_2567cd6def add_op_namespace_prefix(op_name: str):"""Prefix op by namespace."""return f"_paged_attention_cuda_2567cd6::{op_name}"
123version https://git-lfs.github.com/spec/v1oid sha256:a68308a8e0157354f879eb6d6442b0cff86fe740f12a7ee0d0eebd80643d5d61size 86069336123version https://git-lfs.github.com/spec/v1oid sha256:b8f25d8a0b39bb6710dded0e031c96fc2be3d615690e3bc95126d620f5a5c756size 86069336
123456{"name": "paged-attention","id": "_paged_attention_cuda_86f75d9","version": 1,"license": "Apache-2.0","python-depends": [],123456{"name": "paged-attention","id": "_paged_attention_cuda_2567cd6","version": 1,"license": "Apache-2.0","python-depends": [],
123456789import torchfrom . import _paged_attention_cuda_86f75d9ops = torch.ops._paged_attention_cuda_86f75d9def add_op_namespace_prefix(op_name: str):"""Prefix op by namespace."""return f"_paged_attention_cuda_86f75d9::{op_name}"123456789import torchfrom . import _paged_attention_cuda_2567cd6ops = torch.ops._paged_attention_cuda_2567cd6def add_op_namespace_prefix(op_name: str):"""Prefix op by namespace."""return f"_paged_attention_cuda_2567cd6::{op_name}"
123version https://git-lfs.github.com/spec/v1oid sha256:5318f7d4226f2b7e4d4683e6ad576edcd03a07f898a5455942779a2e97083131size 140009304123version https://git-lfs.github.com/spec/v1oid sha256:16d77f814330ce56502186eb42be3ac21b1e862f254ba57f4eee212c85c17886size 140009304
123456{"name": "paged-attention","id": "_paged_attention_cuda_86f75d9","version": 1,"license": "Apache-2.0","python-depends": [],123456{"name": "paged-attention","id": "_paged_attention_cuda_2567cd6","version": 1,"license": "Apache-2.0","python-depends": [],
123456789import torchfrom . import _paged_attention_cuda_86f75d9ops = torch.ops._paged_attention_cuda_86f75d9def add_op_namespace_prefix(op_name: str):"""Prefix op by namespace."""return f"_paged_attention_cuda_86f75d9::{op_name}"123456789import torchfrom . import _paged_attention_cuda_2567cd6ops = torch.ops._paged_attention_cuda_2567cd6def add_op_namespace_prefix(op_name: str):"""Prefix op by namespace."""return f"_paged_attention_cuda_2567cd6::{op_name}"
123version https://git-lfs.github.com/spec/v1oid sha256:501f09fe11ea475737175f7fcab105412acdfdba36dab0dd0ccc6d67094a1046size 167599784123version https://git-lfs.github.com/spec/v1oid sha256:60277de4e122424d6e5430299c37543b75f62b29c977189cf65b9449eeb1a705size 167599784
123456{"name": "paged-attention","id": "_paged_attention_cuda_86f75d9","version": 1,"license": "Apache-2.0","python-depends": [],123456{"name": "paged-attention","id": "_paged_attention_cuda_2567cd6","version": 1,"license": "Apache-2.0","python-depends": [],
123456789import torchfrom . import _paged_attention_cuda_86f75d9ops = torch.ops._paged_attention_cuda_86f75d9def add_op_namespace_prefix(op_name: str):"""Prefix op by namespace."""return f"_paged_attention_cuda_86f75d9::{op_name}"123456789import torchfrom . import _paged_attention_cuda_2567cd6ops = torch.ops._paged_attention_cuda_2567cd6def add_op_namespace_prefix(op_name: str):"""Prefix op by namespace."""return f"_paged_attention_cuda_2567cd6::{op_name}"
123version https://git-lfs.github.com/spec/v1oid sha256:b347d4485444ece322c18ec55ffc10d389d8fdb52b638d36bc963b60036b442dsize 86065184123version https://git-lfs.github.com/spec/v1oid sha256:dc3151992ff276d6413339e601a6c708475185d6f1e0349dc93fc25890542347size 86065184
123456{"name": "paged-attention","id": "_paged_attention_cuda_86f75d9","version": 1,"license": "Apache-2.0","python-depends": [],123456{"name": "paged-attention","id": "_paged_attention_cuda_2567cd6","version": 1,"license": "Apache-2.0","python-depends": [],
123456789101112131415161718192021from ._custom_ops import ( convert_fp8, copy_blocks, paged_attention_v1, paged_attention_v2, reshape_and_cache, reshape_and_cache_flash, swap_blocks,)from ._ops import ops
__all__ = [ "convert_fp8", "copy_blocks", "ops", "paged_attention_v1", "paged_attention_v2", "reshape_and_cache", "reshape_and_cache_flash", "swap_blocks",]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173from typing import List, Optional
import torch
from ._ops import ops
# page attention opsdef paged_attention_v1( out: torch.Tensor, query: torch.Tensor, key_cache: torch.Tensor, value_cache: torch.Tensor, num_kv_heads: int, scale: float, block_tables: torch.Tensor, seq_lens: torch.Tensor, block_size: int, max_seq_len: int, alibi_slopes: Optional[torch.Tensor], kv_cache_dtype: str, k_scale: float, v_scale: float, tp_rank: int = 0, blocksparse_local_blocks: int = 0, blocksparse_vert_stride: int = 0, blocksparse_block_size: int = 64, blocksparse_head_sliding_step: int = 0,) -> None: ops.paged_attention_v1( out, query, key_cache, value_cache, num_kv_heads, scale, block_tables, seq_lens, block_size, max_seq_len, alibi_slopes, kv_cache_dtype, k_scale, v_scale, tp_rank, blocksparse_local_blocks, blocksparse_vert_stride, blocksparse_block_size, blocksparse_head_sliding_step, )
def paged_attention_v2( out: torch.Tensor, exp_sum: torch.Tensor, max_logits: torch.Tensor, tmp_out: torch.Tensor, query: torch.Tensor, key_cache: torch.Tensor, value_cache: torch.Tensor, num_kv_heads: int, scale: float, block_tables: torch.Tensor, seq_lens: torch.Tensor, block_size: int, max_seq_len: int, alibi_slopes: Optional[torch.Tensor], kv_cache_dtype: str, k_scale: float, v_scale: float, tp_rank: int = 0, blocksparse_local_blocks: int = 0, blocksparse_vert_stride: int = 0, blocksparse_block_size: int = 64, blocksparse_head_sliding_step: int = 0,) -> None: ops.paged_attention_v2( out, exp_sum, max_logits, tmp_out, query, key_cache, value_cache, num_kv_heads, scale, block_tables, seq_lens, block_size, max_seq_len, alibi_slopes, kv_cache_dtype, k_scale, v_scale, tp_rank, blocksparse_local_blocks, blocksparse_vert_stride, blocksparse_block_size, blocksparse_head_sliding_step, )
def reshape_and_cache( key: torch.Tensor, value: torch.Tensor, key_cache: torch.Tensor, value_cache: torch.Tensor, slot_mapping: torch.Tensor, kv_cache_dtype: str, k_scale: float, v_scale: float,) -> None: ops.reshape_and_cache( key, value, key_cache, value_cache, slot_mapping, kv_cache_dtype, k_scale, v_scale, )
def reshape_and_cache_flash( key: torch.Tensor, value: torch.Tensor, key_cache: torch.Tensor, value_cache: torch.Tensor, slot_mapping: torch.Tensor, kv_cache_dtype: str, k_scale: torch.Tensor, v_scale: torch.Tensor,) -> None: ops.reshape_and_cache_flash( key, value, key_cache, value_cache, slot_mapping, kv_cache_dtype, k_scale, v_scale, )
def copy_blocks( key_caches: List[torch.Tensor], value_caches: List[torch.Tensor], block_mapping: torch.Tensor,) -> None: ops.copy_blocks(key_caches, value_caches, block_mapping)
def swap_blocks( src: torch.Tensor, dst: torch.Tensor, block_mapping: torch.Tensor) -> None: ops.swap_blocks(src, dst, block_mapping)
def convert_fp8( output: torch.Tensor, input: torch.Tensor, scale: float = 1.0, kv_dtype: str = "fp8") -> None: ops.convert_fp8(output, input, scale, kv_dtype)
__all__ = [ "convert_fp8", "paged_attention_v1", "paged_attention_v2", "reshape_and_cache", "copy_blocks",]123456789import torchfrom . import _paged_attention_cuda_2567cd6ops = torch.ops._paged_attention_cuda_2567cd6
def add_op_namespace_prefix(op_name: str): """ Prefix op by namespace. """ return f"_paged_attention_cuda_2567cd6::{op_name}"123version https://git-lfs.github.com/spec/v1oid sha256:4a3102d6efd314c44aff63ccff2c1f38cc6bfd0c54c30ee6dfa9d98d3b2a6e67size 1400124161234567891011121314151617181920{ "name": "paged-attention", "id": "_paged_attention_cuda_2567cd6", "version": 1, "license": "Apache-2.0", "python-depends": [], "backend": { "type": "cuda", "archs": [ "7.0", "7.2", "7.5", "8.0", "8.6", "8.7", "8.9", "9.0+PTX" ] }}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")))1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192import osimport randomfrom abc import ABC, abstractmethodfrom functools import lru_cache, wrapsfrom typing import Callable, ParamSpec, TypeVar
import numpy as npimport torch
IS_ROCM = torch.version.hip is not NoneIS_MPS = torch.backends.mps.is_available()
class Platform(ABC): @classmethod def seed_everything(cls, seed: int) -> None: """ Set the seed of each random module. `torch.manual_seed` will set seed on all devices.
Loosely based on: https://github.com/Lightning-AI/pytorch-lightning/blob/2.4.0/src/lightning/fabric/utilities/seed.py#L20 """ random.seed(seed) np.random.seed(seed) torch.manual_seed(seed)
@abstractmethod def get_device_name(self, device_id: int = 0) -> str: ...
@abstractmethod def is_cuda(self) -> bool: ...
@abstractmethod def is_rocm(self) -> bool: ...
@abstractmethod def is_mps(self) -> bool: ...
class CudaPlatform(Platform): @classmethod @lru_cache(maxsize=8) def get_device_name(cls, device_id: int = 0) -> str: return torch.cuda.get_device_name(0)
def is_cuda(self) -> bool: return True
def is_rocm(self) -> bool: return False
def is_mps(self) -> bool: return False
class RocmPlatform(Platform): @classmethod @lru_cache(maxsize=8) def get_device_name(cls, device_id: int = 0) -> str: return torch.cuda.get_device_name(device_id)
def is_cuda(self) -> bool: return False
def is_rocm(self) -> bool: return True
def is_mps(self) -> bool: return False
class MpsPlatform(Platform): @classmethod @lru_cache(maxsize=8) def get_device_name(cls, device_id: int = 0) -> str: return torch.cuda.get_device_name(device_id)
def is_cuda(self) -> bool: return False
def is_rocm(self) -> bool: return False
def is_mps(self) -> bool: return True
current_platform = ( RocmPlatform() if IS_ROCM else MpsPlatform() if IS_MPS else CudaPlatform() if torch.cuda.is_available() else None)123456789101112131415161718192021from ._custom_ops import ( convert_fp8, copy_blocks, paged_attention_v1, paged_attention_v2, reshape_and_cache, reshape_and_cache_flash, swap_blocks,)from ._ops import ops
__all__ = [ "convert_fp8", "copy_blocks", "ops", "paged_attention_v1", "paged_attention_v2", "reshape_and_cache", "reshape_and_cache_flash", "swap_blocks",]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173from typing import List, Optional
import torch
from ._ops import ops
# page attention opsdef paged_attention_v1( out: torch.Tensor, query: torch.Tensor, key_cache: torch.Tensor, value_cache: torch.Tensor, num_kv_heads: int, scale: float, block_tables: torch.Tensor, seq_lens: torch.Tensor, block_size: int, max_seq_len: int, alibi_slopes: Optional[torch.Tensor], kv_cache_dtype: str, k_scale: float, v_scale: float, tp_rank: int = 0, blocksparse_local_blocks: int = 0, blocksparse_vert_stride: int = 0, blocksparse_block_size: int = 64, blocksparse_head_sliding_step: int = 0,) -> None: ops.paged_attention_v1( out, query, key_cache, value_cache, num_kv_heads, scale, block_tables, seq_lens, block_size, max_seq_len, alibi_slopes, kv_cache_dtype, k_scale, v_scale, tp_rank, blocksparse_local_blocks, blocksparse_vert_stride, blocksparse_block_size, blocksparse_head_sliding_step, )
def paged_attention_v2( out: torch.Tensor, exp_sum: torch.Tensor, max_logits: torch.Tensor, tmp_out: torch.Tensor, query: torch.Tensor, key_cache: torch.Tensor, value_cache: torch.Tensor, num_kv_heads: int, scale: float, block_tables: torch.Tensor, seq_lens: torch.Tensor, block_size: int, max_seq_len: int, alibi_slopes: Optional[torch.Tensor], kv_cache_dtype: str, k_scale: float, v_scale: float, tp_rank: int = 0, blocksparse_local_blocks: int = 0, blocksparse_vert_stride: int = 0, blocksparse_block_size: int = 64, blocksparse_head_sliding_step: int = 0,) -> None: ops.paged_attention_v2( out, exp_sum, max_logits, tmp_out, query, key_cache, value_cache, num_kv_heads, scale, block_tables, seq_lens, block_size, max_seq_len, alibi_slopes, kv_cache_dtype, k_scale, v_scale, tp_rank, blocksparse_local_blocks, blocksparse_vert_stride, blocksparse_block_size, blocksparse_head_sliding_step, )
def reshape_and_cache( key: torch.Tensor, value: torch.Tensor, key_cache: torch.Tensor, value_cache: torch.Tensor, slot_mapping: torch.Tensor, kv_cache_dtype: str, k_scale: float, v_scale: float,) -> None: ops.reshape_and_cache( key, value, key_cache, value_cache, slot_mapping, kv_cache_dtype, k_scale, v_scale, )
def reshape_and_cache_flash( key: torch.Tensor, value: torch.Tensor, key_cache: torch.Tensor, value_cache: torch.Tensor, slot_mapping: torch.Tensor, kv_cache_dtype: str, k_scale: torch.Tensor, v_scale: torch.Tensor,) -> None: ops.reshape_and_cache_flash( key, value, key_cache, value_cache, slot_mapping, kv_cache_dtype, k_scale, v_scale, )
def copy_blocks( key_caches: List[torch.Tensor], value_caches: List[torch.Tensor], block_mapping: torch.Tensor,) -> None: ops.copy_blocks(key_caches, value_caches, block_mapping)
def swap_blocks( src: torch.Tensor, dst: torch.Tensor, block_mapping: torch.Tensor) -> None: ops.swap_blocks(src, dst, block_mapping)
def convert_fp8( output: torch.Tensor, input: torch.Tensor, scale: float = 1.0, kv_dtype: str = "fp8") -> None: ops.convert_fp8(output, input, scale, kv_dtype)
__all__ = [ "convert_fp8", "paged_attention_v1", "paged_attention_v2", "reshape_and_cache", "copy_blocks",]123456789import torchfrom . import _paged_attention_cuda_2567cd6ops = torch.ops._paged_attention_cuda_2567cd6
def add_op_namespace_prefix(op_name: str): """ Prefix op by namespace. """ return f"_paged_attention_cuda_2567cd6::{op_name}"123version https://git-lfs.github.com/spec/v1oid sha256:b181e6a863e320fe528e28d30b0d4dd3bb92e8d4a54f7a92c25627966dc8f32bsize 86068296123456789101112131415161718192021{ "name": "paged-attention", "id": "_paged_attention_cuda_2567cd6", "version": 1, "license": "Apache-2.0", "python-depends": [], "backend": { "type": "cuda", "archs": [ "10.0", "11.0", "12.0+PTX", "7.5", "8.0", "8.6", "8.7", "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")))1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192import osimport randomfrom abc import ABC, abstractmethodfrom functools import lru_cache, wrapsfrom typing import Callable, ParamSpec, TypeVar
import numpy as npimport torch
IS_ROCM = torch.version.hip is not NoneIS_MPS = torch.backends.mps.is_available()
class Platform(ABC): @classmethod def seed_everything(cls, seed: int) -> None: """ Set the seed of each random module. `torch.manual_seed` will set seed on all devices.
Loosely based on: https://github.com/Lightning-AI/pytorch-lightning/blob/2.4.0/src/lightning/fabric/utilities/seed.py#L20 """ random.seed(seed) np.random.seed(seed) torch.manual_seed(seed)
@abstractmethod def get_device_name(self, device_id: int = 0) -> str: ...
@abstractmethod def is_cuda(self) -> bool: ...
@abstractmethod def is_rocm(self) -> bool: ...
@abstractmethod def is_mps(self) -> bool: ...
class CudaPlatform(Platform): @classmethod @lru_cache(maxsize=8) def get_device_name(cls, device_id: int = 0) -> str: return torch.cuda.get_device_name(0)
def is_cuda(self) -> bool: return True
def is_rocm(self) -> bool: return False
def is_mps(self) -> bool: return False
class RocmPlatform(Platform): @classmethod @lru_cache(maxsize=8) def get_device_name(cls, device_id: int = 0) -> str: return torch.cuda.get_device_name(device_id)
def is_cuda(self) -> bool: return False
def is_rocm(self) -> bool: return True
def is_mps(self) -> bool: return False
class MpsPlatform(Platform): @classmethod @lru_cache(maxsize=8) def get_device_name(cls, device_id: int = 0) -> str: return torch.cuda.get_device_name(device_id)
def is_cuda(self) -> bool: return False
def is_rocm(self) -> bool: return False
def is_mps(self) -> bool: return True
current_platform = ( RocmPlatform() if IS_ROCM else MpsPlatform() if IS_MPS else CudaPlatform() if torch.cuda.is_available() else None)123456789101112131415161718192021from ._custom_ops import ( convert_fp8, copy_blocks, paged_attention_v1, paged_attention_v2, reshape_and_cache, reshape_and_cache_flash, swap_blocks,)from ._ops import ops
__all__ = [ "convert_fp8", "copy_blocks", "ops", "paged_attention_v1", "paged_attention_v2", "reshape_and_cache", "reshape_and_cache_flash", "swap_blocks",]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173from typing import List, Optional
import torch
from ._ops import ops
# page attention opsdef paged_attention_v1( out: torch.Tensor, query: torch.Tensor, key_cache: torch.Tensor, value_cache: torch.Tensor, num_kv_heads: int, scale: float, block_tables: torch.Tensor, seq_lens: torch.Tensor, block_size: int, max_seq_len: int, alibi_slopes: Optional[torch.Tensor], kv_cache_dtype: str, k_scale: float, v_scale: float, tp_rank: int = 0, blocksparse_local_blocks: int = 0, blocksparse_vert_stride: int = 0, blocksparse_block_size: int = 64, blocksparse_head_sliding_step: int = 0,) -> None: ops.paged_attention_v1( out, query, key_cache, value_cache, num_kv_heads, scale, block_tables, seq_lens, block_size, max_seq_len, alibi_slopes, kv_cache_dtype, k_scale, v_scale, tp_rank, blocksparse_local_blocks, blocksparse_vert_stride, blocksparse_block_size, blocksparse_head_sliding_step, )
def paged_attention_v2( out: torch.Tensor, exp_sum: torch.Tensor, max_logits: torch.Tensor, tmp_out: torch.Tensor, query: torch.Tensor, key_cache: torch.Tensor, value_cache: torch.Tensor, num_kv_heads: int, scale: float, block_tables: torch.Tensor, seq_lens: torch.Tensor, block_size: int, max_seq_len: int, alibi_slopes: Optional[torch.Tensor], kv_cache_dtype: str, k_scale: float, v_scale: float, tp_rank: int = 0, blocksparse_local_blocks: int = 0, blocksparse_vert_stride: int = 0, blocksparse_block_size: int = 64, blocksparse_head_sliding_step: int = 0,) -> None: ops.paged_attention_v2( out, exp_sum, max_logits, tmp_out, query, key_cache, value_cache, num_kv_heads, scale, block_tables, seq_lens, block_size, max_seq_len, alibi_slopes, kv_cache_dtype, k_scale, v_scale, tp_rank, blocksparse_local_blocks, blocksparse_vert_stride, blocksparse_block_size, blocksparse_head_sliding_step, )
def reshape_and_cache( key: torch.Tensor, value: torch.Tensor, key_cache: torch.Tensor, value_cache: torch.Tensor, slot_mapping: torch.Tensor, kv_cache_dtype: str, k_scale: float, v_scale: float,) -> None: ops.reshape_and_cache( key, value, key_cache, value_cache, slot_mapping, kv_cache_dtype, k_scale, v_scale, )
def reshape_and_cache_flash( key: torch.Tensor, value: torch.Tensor, key_cache: torch.Tensor, value_cache: torch.Tensor, slot_mapping: torch.Tensor, kv_cache_dtype: str, k_scale: torch.Tensor, v_scale: torch.Tensor,) -> None: ops.reshape_and_cache_flash( key, value, key_cache, value_cache, slot_mapping, kv_cache_dtype, k_scale, v_scale, )
def copy_blocks( key_caches: List[torch.Tensor], value_caches: List[torch.Tensor], block_mapping: torch.Tensor,) -> None: ops.copy_blocks(key_caches, value_caches, block_mapping)
def swap_blocks( src: torch.Tensor, dst: torch.Tensor, block_mapping: torch.Tensor) -> None: ops.swap_blocks(src, dst, block_mapping)
def convert_fp8( output: torch.Tensor, input: torch.Tensor, scale: float = 1.0, kv_dtype: str = "fp8") -> None: ops.convert_fp8(output, input, scale, kv_dtype)
__all__ = [ "convert_fp8", "paged_attention_v1", "paged_attention_v2", "reshape_and_cache", "copy_blocks",]123456789import torchfrom . import _paged_attention_cuda_2567cd6ops = torch.ops._paged_attention_cuda_2567cd6
def add_op_namespace_prefix(op_name: str): """ Prefix op by namespace. """ return f"_paged_attention_cuda_2567cd6::{op_name}"123version https://git-lfs.github.com/spec/v1oid sha256:ffc6f41ff69cf6222fac5e183b60bd1fc371addbc570f795daf3371d57710282size 84956984123456789101112131415161718192021{ "name": "paged-attention", "id": "_paged_attention_cuda_2567cd6", "version": 1, "license": "Apache-2.0", "python-depends": [], "backend": { "type": "cuda", "archs": [ "10.0", "11.0", "12.0+PTX", "7.5", "8.0", "8.6", "8.7", "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")))1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192import osimport randomfrom abc import ABC, abstractmethodfrom functools import lru_cache, wrapsfrom typing import Callable, ParamSpec, TypeVar
import numpy as npimport torch
IS_ROCM = torch.version.hip is not NoneIS_MPS = torch.backends.mps.is_available()
class Platform(ABC): @classmethod def seed_everything(cls, seed: int) -> None: """ Set the seed of each random module. `torch.manual_seed` will set seed on all devices.
Loosely based on: https://github.com/Lightning-AI/pytorch-lightning/blob/2.4.0/src/lightning/fabric/utilities/seed.py#L20 """ random.seed(seed) np.random.seed(seed) torch.manual_seed(seed)
@abstractmethod def get_device_name(self, device_id: int = 0) -> str: ...
@abstractmethod def is_cuda(self) -> bool: ...
@abstractmethod def is_rocm(self) -> bool: ...
@abstractmethod def is_mps(self) -> bool: ...
class CudaPlatform(Platform): @classmethod @lru_cache(maxsize=8) def get_device_name(cls, device_id: int = 0) -> str: return torch.cuda.get_device_name(0)
def is_cuda(self) -> bool: return True
def is_rocm(self) -> bool: return False
def is_mps(self) -> bool: return False
class RocmPlatform(Platform): @classmethod @lru_cache(maxsize=8) def get_device_name(cls, device_id: int = 0) -> str: return torch.cuda.get_device_name(device_id)
def is_cuda(self) -> bool: return False
def is_rocm(self) -> bool: return True
def is_mps(self) -> bool: return False
class MpsPlatform(Platform): @classmethod @lru_cache(maxsize=8) def get_device_name(cls, device_id: int = 0) -> str: return torch.cuda.get_device_name(device_id)
def is_cuda(self) -> bool: return False
def is_rocm(self) -> bool: return False
def is_mps(self) -> bool: return True
current_platform = ( RocmPlatform() if IS_ROCM else MpsPlatform() if IS_MPS else CudaPlatform() if torch.cuda.is_available() else None)build/torch210-cxx11-cu126-aarch64-linux/_ops.py+3-3
build/torch210-cxx11-cu126-aarch64-linux/_paged_attention_cuda_86f75d9.abi3.so → build/torch210-cxx11-cu126-aarch64-linux/_paged_attention_cuda_2567cd6.abi3.soRenamed+1-1
build/torch210-cxx11-cu126-aarch64-linux/metadata.json+1-1
build/torch210-cxx11-cu128-aarch64-linux/_ops.py+3-3
build/torch210-cxx11-cu128-aarch64-linux/_paged_attention_cuda_86f75d9.abi3.so → build/torch210-cxx11-cu128-aarch64-linux/_paged_attention_cuda_2567cd6.abi3.soRenamed+1-1
build/torch210-cxx11-cu128-aarch64-linux/metadata.json+1-1
build/torch210-cxx11-cu130-aarch64-linux/_ops.py+3-3
build/torch210-cxx11-cu130-aarch64-linux/_paged_attention_cuda_86f75d9.abi3.so → build/torch210-cxx11-cu130-aarch64-linux/_paged_attention_cuda_2567cd6.abi3.soRenamed+1-1
build/torch210-cxx11-cu130-aarch64-linux/metadata.json+1-1
build/torch211-cxx11-cu126-aarch64-linux/_ops.py+3-3
build/torch211-cxx11-cu126-aarch64-linux/_paged_attention_cuda_86f75d9.abi3.so → build/torch211-cxx11-cu126-aarch64-linux/_paged_attention_cuda_2567cd6.abi3.soRenamed+1-1
build/torch211-cxx11-cu126-aarch64-linux/metadata.json+1-1
build/torch211-cxx11-cu128-aarch64-linux/_ops.py+3-3
build/torch211-cxx11-cu128-aarch64-linux/_paged_attention_cuda_86f75d9.abi3.so → build/torch211-cxx11-cu128-aarch64-linux/_paged_attention_cuda_2567cd6.abi3.soRenamed+1-1
build/torch211-cxx11-cu128-aarch64-linux/metadata.json+1-1
build/torch211-cxx11-cu130-aarch64-linux/_ops.py+3-3
build/torch211-cxx11-cu130-aarch64-linux/_paged_attention_cuda_86f75d9.abi3.so → build/torch211-cxx11-cu130-aarch64-linux/_paged_attention_cuda_2567cd6.abi3.soRenamed+1-1
build/torch211-cxx11-cu130-aarch64-linux/metadata.json+1-1
build/torch212-cxx11-cu126-aarch64-linux/__init__.pyAdded+21
build/torch212-cxx11-cu126-aarch64-linux/_custom_ops.pyAdded+173
build/torch212-cxx11-cu126-aarch64-linux/_ops.pyAdded+9
build/torch212-cxx11-cu126-aarch64-linux/_paged_attention_cuda_2567cd6.abi3.soAdded+3
build/torch212-cxx11-cu126-aarch64-linux/metadata.jsonAdded+20
build/torch212-cxx11-cu126-aarch64-linux/paged_attention/__init__.pyAdded+26
build/torch212-cxx11-cu126-aarch64-linux/platforms.pyAdded+92
build/torch212-cxx11-cu130-aarch64-linux/__init__.pyAdded+21
build/torch212-cxx11-cu130-aarch64-linux/_custom_ops.pyAdded+173
build/torch212-cxx11-cu130-aarch64-linux/_ops.pyAdded+9
build/torch212-cxx11-cu130-aarch64-linux/_paged_attention_cuda_2567cd6.abi3.soAdded+3
build/torch212-cxx11-cu130-aarch64-linux/metadata.jsonAdded+21
build/torch212-cxx11-cu130-aarch64-linux/paged_attention/__init__.pyAdded+26
build/torch212-cxx11-cu130-aarch64-linux/platforms.pyAdded+92
build/torch212-cxx11-cu132-aarch64-linux/__init__.pyAdded+21
build/torch212-cxx11-cu132-aarch64-linux/_custom_ops.pyAdded+173
build/torch212-cxx11-cu132-aarch64-linux/_ops.pyAdded+9
build/torch212-cxx11-cu132-aarch64-linux/_paged_attention_cuda_2567cd6.abi3.soAdded+3
build/torch212-cxx11-cu132-aarch64-linux/metadata.jsonAdded+21
build/torch212-cxx11-cu132-aarch64-linux/paged_attention/__init__.pyAdded+26
build/torch212-cxx11-cu132-aarch64-linux/platforms.pyAdded+92