Run this model with HF Jobs
Run replicate/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/replicate/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/replicate/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 replicate/paged-attention with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Kernels
How to use replicate/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("replicate/paged-attention", version=1) - Notebooks
- Google Colab
- Kaggle
Improve tests for mps
Browse filesFiles changed (4)
35 unmodified lines3637383940414235 unmodified linesseed: int = 0,device: Optional[str] = "cuda",) -> Tuple[List[torch.Tensor], List[torch.Tensor]]:if cache_dtype == "fp8" and head_size % 16:raise ValueError(f"Does not support key cache of type fp8 with head_size {head_size}"35 unmodified lines36373839404135 unmodified linesseed: int = 0,device: Optional[str] = "cuda",) -> Tuple[List[torch.Tensor], List[torch.Tensor]]:if cache_dtype == "fp8" and head_size % 16:raise ValueError(f"Does not support key cache of type fp8 with head_size {head_size}"
42 unmodified lines434445464748183 unmodified lines23223323423523623723856 unmodified lines29529629729829930030138 unmodified lines34034134234334434534642 unmodified lineselse:DEVICES = [f"cuda:{i}" for i in range(1 if torch.cuda.device_count() == 1 else 2)]def ref_masked_attention(query: torch.Tensor,key: torch.Tensor,183 unmodified lines64,0,),cond=(head_size == HEAD_SIZES[0] and block_size == BLOCK_SIZES[0] and not device.startswith("mps")),)elif version in ("v2", "rocm"):56 unmodified lines64,0,),cond=(head_size == HEAD_SIZES[0] and block_size == BLOCK_SIZES[0] and not device.startswith("mps")),)else:38 unmodified linesk_scale,v_scale,),cond=(head_size == HEAD_SIZES[0] and block_size == BLOCK_SIZES[0] and not device.startswith("mps")),)else:42 unmodified lines43444546474849183 unmodified lines23323423523623723823924024124224356 unmodified lines30030130230330430530630730830931038 unmodified lines34935035135235335435535635735835942 unmodified lineselse:DEVICES = [f"cuda:{i}" for i in range(1 if torch.cuda.device_count() == 1 else 2)]def ref_masked_attention(query: torch.Tensor,key: torch.Tensor,183 unmodified lines64,0,),cond=(head_size == HEAD_SIZES[0]and block_size == BLOCK_SIZES[0]and not device.startswith("mps")),)elif version in ("v2", "rocm"):56 unmodified lines64,0,),cond=(head_size == HEAD_SIZES[0]and block_size == BLOCK_SIZES[0]and not device.startswith("mps")),)else:38 unmodified linesk_scale,v_scale,),cond=(head_size == HEAD_SIZES[0]and block_size == BLOCK_SIZES[0]and not device.startswith("mps")),)else:
59 unmodified lines6061626364656677 unmodified lines144145146147148149150151152153154155156105 unmodified lines26226326426526626726859 unmodified linesif kv_cache_dtype == "fp8" and head_size % 16:pytest.skip()current_platform.seed_everything(seed)torch.set_default_device(device)# Generate random block mappings where each source block is mapped to two# destination blocks.assert 2 * num_mappings <= num_blocks77 unmodified linesif kv_cache_dtype == "fp8" and head_size % 16:pytest.skip()current_platform.seed_everything(seed)torch.set_default_device(device)# Create a random slot mapping.num_slots = block_size * num_blocksslot_mapping_lst = random.sample(range(num_slots), num_tokens)slot_mapping = torch.tensor(slot_mapping_lst, dtype=torch.long)qkv = torch.randn(num_tokens, 3, num_heads, head_size, dtype=dtype)_, key, value = qkv.unbind(dim=1)# Create the KV caches.105 unmodified linesif current_platform.is_mps() and kv_cache_dtype == "fp8":pytest.skip("reshape_and_cache_flash doesn't support FP8 on MPS")current_platform.seed_everything(seed)torch.set_default_device(device)# Create a random slot mapping.num_slots = block_size * num_blocks59 unmodified lines60616263646566676877 unmodified lines146147148149150151152153154155156157158159160105 unmodified lines26626726826927027127227327459 unmodified linesif kv_cache_dtype == "fp8" and head_size % 16:pytest.skip()current_platform.seed_everything(seed)# Don't set MPS as default device to avoid placeholder storage errorif not device.startswith("mps"):torch.set_default_device(device)# Generate random block mappings where each source block is mapped to two# destination blocks.assert 2 * num_mappings <= num_blocks77 unmodified linesif kv_cache_dtype == "fp8" and head_size % 16:pytest.skip()current_platform.seed_everything(seed)# Don't set MPS as default device to avoid placeholder storage errorif not device.startswith("mps"):torch.set_default_device(device)# Create a random slot mapping.num_slots = block_size * num_blocksslot_mapping_lst = random.sample(range(num_slots), num_tokens)slot_mapping = torch.tensor(slot_mapping_lst, dtype=torch.long, device=device)qkv = torch.randn(num_tokens, 3, num_heads, head_size, dtype=dtype, device=device)_, key, value = qkv.unbind(dim=1)# Create the KV caches.105 unmodified linesif current_platform.is_mps() and kv_cache_dtype == "fp8":pytest.skip("reshape_and_cache_flash doesn't support FP8 on MPS")current_platform.seed_everything(seed)# Don't set MPS as default device to avoid placeholder storage errorif not device.startswith("mps"):torch.set_default_device(device)# Create a random slot mapping.num_slots = block_size * num_blocks
39 unmodified lines4041424344454647484918 unmodified lines6869707172737475767778798081828384858687888990919239 unmodified lines"""torch._refs._check_close_args(name="torch.allclose", a=a, b=b, rtol=rtol, atol=atol)return bool(torch.all(torch.isclose(a.double(), b.double(), rtol=rtol, atol=atol, equal_nan=equal_nan)).item())18 unmodified lines*,test_utils: Union[str, Sequence[str]] = ALL_OPCHECK_TEST_UTILS,raise_exception: bool = True,cond: bool = True) -> Dict[str, str]:with unittest.mock.patch("torch.allclose", new=fp8_allclose):if not cond:return {}# Check if any arguments are on MPS device and skip opcheck if so# as MPS has issues with placeholder storage allocation in opcheckdef is_mps_tensor(x):return hasattr(x, 'device') and x.device.type == 'mps'def check_args_for_mps(args):if isinstance(args, (list, tuple)):return any(check_args_for_mps(arg) for arg in args)return is_mps_tensor(args)if check_args_for_mps(args):return {}return torch.library.opcheck(op, args, kwargs, test_utils=test_utils, raise_exception=raise_exception)39 unmodified lines40414243444546474849505152535455565718 unmodified lines76777879808182838485868739 unmodified lines"""torch._refs._check_close_args(name="torch.allclose", a=a, b=b, rtol=rtol, atol=atol)# MPS doesn't support float64, so use float32 for comparisonif a.device.type == "mps" or b.device.type == "mps":a_cmp = a.float()b_cmp = b.float()else:a_cmp = a.double()b_cmp = b.double()return bool(torch.all(torch.isclose(a_cmp, b_cmp, rtol=rtol, atol=atol, equal_nan=equal_nan)).item())18 unmodified lines*,test_utils: Union[str, Sequence[str]] = ALL_OPCHECK_TEST_UTILS,raise_exception: bool = True,cond: bool = True,) -> Dict[str, str]:with unittest.mock.patch("torch.allclose", new=fp8_allclose):if not cond:return {}return torch.library.opcheck(op, args, kwargs, test_utils=test_utils, raise_exception=raise_exception)
tests/kernels/conftest.py-1
tests/kernels/test_attention.py+16-3
tests/kernels/test_cache.py+11-5
tests/kernels/utils.py+11-16