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 # a version (or an explicit revision) is required; see the "Files and versions" tab for the available ones kernel = get_kernel("replicate/triton_kernels", version=1) - Notebooks
- Google Colab
- Kaggle
Download tests/test_swiglu.py from replicate/triton_kernels: direct link, hf CLI and curl.
- Browser
- Download file 1.36 kB
-
https://huggingface.co/replicate/triton_kernels/resolve/main/tests/test_swiglu.py
- Command line
-
hf download hf://replicate/triton_kernels/tests/test_swiglu.py
-
curl -L -o test_swiglu.py https://huggingface.co/replicate/triton_kernels/resolve/main/tests/test_swiglu.py
1.36 kB
| from triton_kernels.routing import routing_torch | |
| from triton_kernels.swiglu import swiglu, swiglu_torch, PrecisionConfig | |
| from triton_kernels.testing import assert_close | |
| import torch | |
| import pytest | |
| from .test_routing import init_data as init_routing_data | |
| # --------------- | |
| # initialize data | |
| # --------------- | |
| def alloc_rand(shape, device, dtype, requires_grad=True): | |
| if dtype.itemsize == 1: | |
| tmp = 2**-(torch.randint(4, 8, shape, device=device, dtype=torch.float16)) | |
| return tmp.to(dtype).requires_grad_(requires_grad) | |
| return torch.randn(shape, device=device, dtype=dtype, requires_grad=requires_grad) | |
| # --------------- | |
| # unit tests | |
| # --------------- | |
| def test_op(M, N, limit, device, alpha=0.5): | |
| torch.manual_seed(2) | |
| # initialize expert data | |
| n_expts_tot = 6 | |
| n_expts_act = 2 | |
| logits = init_routing_data(M, n_expts_tot).detach() | |
| routing_data, _, _ = routing_torch(logits, n_expts_act) | |
| n_tokens = routing_data.expt_hist.sum() | |
| # initialize data | |
| x = alloc_rand([n_tokens, N], device=device, dtype=torch.bfloat16) | |
| precision_config = PrecisionConfig(limit=limit) | |
| tri_y = swiglu(x, alpha, precision_config, routing_data) | |
| ref_y = swiglu_torch(x, alpha, precision_config) | |
| assert_close(tri_y, ref_y) | |