Kernels

Run this model with HF Jobs

Run replicate/triton_kernels 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/triton_kernels. 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/triton_kernels 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.
triton_kernels / tests /test_compaction.py
marcsun13's picture
marcsun13 HF Staff
Upload folder using huggingface_hub
567c8ad verified
Raw History Blame Contribute Delete
1.16 kB
import pytest
import torch
from triton_kernels.compaction import compaction, compaction_torch
@pytest.mark.parametrize("n_tokens, n_cols, k, p", [
(8192, 64, 4, 0.5),
(8192, 64, 4, 1.0),
(131, 128, 16, 0.6),
(496, 128, 16, 0.),
])
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)