Instructions to use replicate/triton_kernels with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Kernels
How to use replicate/triton_kernels with Kernels:
# !pip install kernels from kernels import get_kernel kernel = get_kernel("replicate/triton_kernels") - Notebooks
- Google Colab
- Kaggle
| import pytest | |
| import torch | |
| from triton_kernels.compaction import compaction, compaction_torch | |
| def test_compaction(n_tokens, n_cols, k, p, device): | |
| yi = torch.rand((n_tokens, n_cols), device=device).argsort(dim=-1) | |
| yi = yi[:, :k].to(torch.int32) | |
| yv = torch.randn((n_tokens, k), dtype=torch.bfloat16, device=device) | |
| # "drop" indices from yi with probability `p` | |
| mask = torch.zeros((n_tokens, n_cols), dtype=torch.int32, device=device) | |
| keep = (torch.rand(yi.shape, device=device) < p) | |
| if keep.any(): | |
| rows = torch.arange(yi.size(0), device=device).unsqueeze(1).expand_as(yi) | |
| mask[rows[keep], yi[keep]] = 1 | |
| chunks = mask.view(*mask.shape[:-1], -1, 32) | |
| weights = (1 << torch.arange(32, dtype=torch.int32, device=device)) | |
| bitmask = (chunks.int() * weights).sum(dim=-1) | |
| yv_ref, yi_ref = compaction_torch(yv, yi, bitmask) | |
| yv_tri, yi_tri = compaction(yv, yi, bitmask) | |
| assert torch.all(yi_ref == yi_tri) | |
| assert torch.all(yv_ref == yv_tri) | |