triton-kernels is a set of kernels that enable fast moe on different architectures. These kernels are compatible with different precision (e.g bf16, mxfp4)
The current version is the following commit 7d0efaa7231661299284a603512fce4fa255e62c
Note that we can't update those kernels as we wish as some commits might rely on triton main. We need to wait for a new release unfortunately.
See releated issue
https://github.com/triton-lang/triton/issues/7818
1# /// script
2# requires-python = ">=3.10"
3# dependencies = [
4# "torch",
5# "triton",
6# "numpy",
7# "kernels",
8# ]
9# ///
10
11import torch
12import sys
13from kernels import get_kernel
14
15torch.manual_seed(42)
16torch.cuda.manual_seed(42)
17
18# Load triton_kernels module via kernels library
19triton_kernels = get_kernel("kernels-community/triton_kernels")
20
21# Access modules directly from the loaded kernel
22swiglu = triton_kernels.swiglu
23routing = triton_kernels.routing
24
25# Setup
26device = "cuda" if torch.cuda.is_available() else "cpu"
27
28# SwiGLU example
29x = torch.randn(512, 1024, device=device, dtype=torch.bfloat16)
30y = swiglu.swiglu_torch(x, 0.5, swiglu.PrecisionConfig(limit=1.0))
31print(f"SwiGLU: {x.shape} -> {y.shape}")
32
33# Routing example
34logits = torch.randn(128, 8, device=device, dtype=torch.float16)
35routing_data, gather_idx, scatter_idx = routing.routing_torch(logits, n_expts_act=2)
36print(f"Routing: {routing_data.expt_hist.sum()} tokens routed")
37
38# MoE integrated
39n_tokens = routing_data.expt_hist.sum().item()
40x_moe = torch.randn(n_tokens, 512, device=device, dtype=torch.bfloat16)
41y_moe = swiglu.swiglu_torch(x_moe, 0.5, swiglu.PrecisionConfig(limit=1.0))
42print(f"MoE SwiGLU: {x_moe.shape} -> {y_moe.shape}")