|
| 1 | +# FlashInfer on JAX: Notebooks and Scripts |
| 2 | + |
| 3 | +Two tutorials that show how to use FlashInfer GPU kernels from JAX via the [jax-tvm-ffi](https://github.com/NVIDIA/jax-tvm-ffi) bridge. |
| 4 | + |
| 5 | +| File | What it covers | |
| 6 | +|------|---------------| |
| 7 | +| `flashinfer_jax_tvm_ffi.ipynb` / `.py` | The three-step bridge pattern (build & load, register, call) with three kernels: `silu_and_mul`, `apply_rope`, single-request decode attention | |
| 8 | +| `gemma3_flashinfer_jax.ipynb` / `.py` | End-to-end Gemma 3 1B Instruct inference using FlashInfer kernels for prefill and decode | |
| 9 | + |
| 10 | +Each tutorial is available as both a Jupyter notebook (with explanations) and a standalone Python script (for quick reading and running). |
| 11 | + |
| 12 | +## Requirements |
| 13 | + |
| 14 | +| Requirement | Details | |
| 15 | +|-------------|---------| |
| 16 | +| GPU | NVIDIA SM 7.5+ (Turing or later) | |
| 17 | +| CUDA | 12.6+ | |
| 18 | +| Python | 3.10+ | |
| 19 | +| Container (recommended) | [NVIDIA NGC JAX container](https://catalog.ngc.nvidia.com/orgs/nvidia/containers/jax) | |
| 20 | + |
| 21 | +## Installation |
| 22 | + |
| 23 | +Recommended (CUDA 13): |
| 24 | + |
| 25 | +```bash |
| 26 | +# Core dependencies (both tutorials) |
| 27 | +pip install 'jax[cuda13]' |
| 28 | +pip install flashinfer-python -U jax-tvm-ffi \ |
| 29 | + --no-build-isolation \ |
| 30 | + --extra-index-url https://flashinfer.ai/whl/cu130/ |
| 31 | + |
| 32 | +# Additional dependencies (Gemma 3 tutorial only) |
| 33 | +pip install torch --index-url https://download.pytorch.org/whl/cpu |
| 34 | +pip install safetensors huggingface_hub transformers |
| 35 | +``` |
| 36 | + |
| 37 | +Replace `jax[cuda13]` with `jax[cuda12]` for CUDA 12.x. |
| 38 | + |
| 39 | +Replace `cu130` with the appropriate variant for your [CUDA Toolkit version](https://developer.nvidia.com/cuda-toolkit-archive) (e.g., `cu126` for CUDA 12.6). |
| 40 | + |
| 41 | +## Running |
| 42 | + |
| 43 | +### Part 1: FlashInfer JAX TVM FFI bridge |
| 44 | + |
| 45 | +As a notebook: |
| 46 | + |
| 47 | +```bash |
| 48 | +jupyter lab flashinfer_jax_tvm_ffi.ipynb |
| 49 | +``` |
| 50 | + |
| 51 | +As a script: |
| 52 | + |
| 53 | +```bash |
| 54 | +python flashinfer_jax_tvm_ffi.py |
| 55 | +``` |
| 56 | + |
| 57 | +The first run compiles three FlashInfer kernels (~30 s each). Subsequent runs use the cached `.so` files in `~/.cache/flashinfer/`. |
| 58 | + |
| 59 | +### Part 2: Gemma 3 inference |
| 60 | + |
| 61 | +Gemma 3 is a gated model. You must first: |
| 62 | + |
| 63 | +1. Create a [Hugging Face](https://huggingface.co) account |
| 64 | +2. Accept the Gemma 3 licence at [google/gemma-3-1b-it](https://huggingface.co/google/gemma-3-1b-it) |
| 65 | +3. Authenticate using **one** of the methods below: |
| 66 | + |
| 67 | +```bash |
| 68 | +# Option A: environment variable (good for containers and CI) |
| 69 | +export HF_TOKEN=hf_... |
| 70 | + |
| 71 | +# Option B: persistent login (stores the token in ~/.cache/huggingface/token) |
| 72 | +pip install huggingface_hub |
| 73 | +huggingface-cli login |
| 74 | +``` |
| 75 | + |
| 76 | +Then run: |
| 77 | + |
| 78 | +```bash |
| 79 | +# As a notebook |
| 80 | +jupyter lab gemma3_flashinfer_jax.ipynb |
| 81 | + |
| 82 | +# As a script |
| 83 | +python gemma3_flashinfer_jax.py |
| 84 | +``` |
| 85 | + |
| 86 | +If neither method is detected, the script will prompt you to paste your token interactively. |
| 87 | + |
| 88 | +The first run downloads ~2 GB of model weights and compiles six FlashInfer kernels (gelu_tanh, rope, local/global decode, local/global prefill). Both are cached after the first run. |
| 89 | + |
| 90 | +## What you'll learn |
| 91 | + |
| 92 | +**Part 1** teaches the three-step pattern that every FlashInfer kernel follows: |
| 93 | + |
| 94 | +``` |
| 95 | +Step 1 BUILD & LOAD jit_spec.build_and_load() -> tvm_ffi.Module |
| 96 | +Step 2 REGISTER jax_tvm_ffi.register_ffi_target(name, wrapper, arg_spec) |
| 97 | +Step 3 CALL jax.ffi.ffi_call(name, output_shapes)(*inputs, **scalar_attrs) |
| 98 | +``` |
| 99 | + |
| 100 | +Each example adds a new concept: |
| 101 | + |
| 102 | +| Kernel | New concept | |
| 103 | +|--------|------------| |
| 104 | +| `silu_and_mul` | Minimal bridge: one input, one output, no argument reordering | |
| 105 | +| `apply_rope` | Multiple outputs; argument reordering between JAX and TVM conventions | |
| 106 | +| `single_decode` | Type-specialized JIT compilation; scratch buffers; optional-argument sentinels | |
| 107 | + |
| 108 | +**Part 2** applies the same pattern to run Gemma 3 1B Instruct end-to-end, adding: |
| 109 | + |
| 110 | +- `gelu_tanh_and_mul` (one-word change from `silu`) |
| 111 | +- QK-norm (per-head RMSNorm on Q and K, new in Gemma 3) |
| 112 | +- Dual RoPE theta (local layers use 10k, global layers use 1M) |
| 113 | +- Local vs global attention with sliding window |
| 114 | +- Prefill (parallel prompt processing) and decode (autoregressive generation) |
| 115 | + |
| 116 | +## Troubleshooting |
| 117 | + |
| 118 | +**`CUDA_HOME not found`** — Set it manually: `export CUDA_HOME=/usr/local/cuda` |
| 119 | + |
| 120 | +**Compilation errors** — Delete the cache and retry: `rm -rf ~/.cache/flashinfer/` |
| 121 | + |
| 122 | +**HF token errors** — Verify your token works: `huggingface-cli whoami` |
| 123 | + |
| 124 | +**GPU interconnect warnings** — Harmless NVML messages on systems without NVLink. Suppressed by `TF_CPP_MIN_LOG_LEVEL=2` (set automatically in the scripts). |
0 commit comments