npx skills add ...
npx skills add huggingface/kernels --skill xpu-kernels
Provides guidance for writing, optimizing, and benchmarking Triton kernels for Intel XPU GPUs (Battlemage/Arc Pro B50) using the Xe-Forge optimization framework. Includes an LLM-driven trial-loop workflow (analyze, validate, benchmark, profile, finalize), XPU-specific patterns (tensor descriptors, GRF mode, tile swizzling), KernelBench fused kernels, and Flash Attention.
npx skills add huggingface/kernels --skill xpu-kernels
This skill provides patterns and guidance for developing optimized Triton kernels targeting Intel XPU GPUs (Battlemage/Arc Pro B50). It integrates the Xe-Forge optimization framework — an LLM-driven loop that transforms PyTorch code into fast Triton kernels.
The full optimization workflow analyzes a PyTorch baseline, generates Triton kernel variants in a branching trial tree, benchmarks each on XPU hardware, and finalizes the best result.
| GPU | Architecture | XVEs | Mem BW | Key Feature | Verified |
|---|---|---|---|---|---|
| Battlemage G21 / Arc Pro B50 | Xe2 | 128 | ~500 GB/s | Tensor descriptors, GRF 256 | Yes |
See the Intel XPU Backend for Triton for supported hardware.
Use this skill when:
Transform PyTorch code into optimized Triton kernels for Intel XPU. Kernels must be numerically equivalent and faster than baseline.
config.yaml firstAt the start of every session, read scripts/config.yaml. It controls:
max_trials — hard cap on optimization trials; always run all of them (use this instead of hardcoded "10")vtune_enabled — if false, skip ALL VTune profiling steps (Step 3.6 and profiler-related decisions)vtune_bin — path to the VTune binary (also settable via VTUNE_BIN env var)test_kernels/*_triton.py or trial files t<trial_id>.py).scripts/benchmark.py.max_trials trials from config.yaml. Do NOT stop early due to plateau — LLM sampling can discover new ideas at any point. The only valid early stop is speedup > 5x.CRITICAL — Single-XPU serialization: There is only ONE XPU on this machine. You MUST NOT run multiple GPU workloads in parallel. benchmark.py and xpu_profiler.py must execute strictly one at a time — concurrent GPU jobs produce wrong results. CPU-only tools (analyze_kernel.py, validate_triton.py, trial_manager.py) are safe to parallelize with each other and with anything else.
| Tool | Command | Purpose |
|---|---|---|
| Analyze | python scripts/analyze_kernel.py <file> | Static analysis: operations, shapes, fusion opportunities |
| Validate | python scripts/validate_triton.py <file> | Syntax + constraint checks before GPU time |
| Benchmark | python scripts/benchmark.py <baseline> <triton> [--triton-baseline] [--baseline-us <cached>] | Correctness + performance via ai-bench |
| Profile | python scripts/xpu_profiler.py <file> | VTune GPU hardware counters + recommendations |
| Init trials | python scripts/trial_manager.py init <kernel_name> <baseline_file> [--triton-baseline] | Initialize trial tracking |
| Save trial | python scripts/trial_manager.py save <kernel_name> <file> [--parent <parent_id>] [--strategy "..."] | Save trial to tree |
| Record result | python scripts/trial_manager.py result <kernel_name> <trial_id> --validation pass --correctness <pass|fail> --speedup <float> --baseline_us <float> --triton_us <float> | Record benchmark result |
| Check status | python scripts/trial_manager.py status <kernel_name> | View trial tree |
| Best trial | python scripts/trial_manager.py best <kernel_name> | Get best trial |
| Baseline time | python scripts/trial_manager.py baseline-us <kernel_name> | Cached baseline time for --baseline-us |
| Finalize | python scripts/trial_manager.py finalize <kernel_name> <name>_triton.py | Copy best trial to output |
python scripts/analyze_kernel.py <pytorch_file>.--triton-baseline): skip analyze_kernel.py (it only supports PyTorch). Read the Triton file directly.references/correctness.yaml and references/xpu_optimizations.yaml.references/implementation_reference.md for templates and the Model class pattern.max_trials from config.yaml)For each trial:
references/implementation_reference.md.python scripts/validate_triton.py <triton_file> (fix until passing; doesn't count as a trial).python scripts/trial_manager.py save <kernel_name> <triton_file> --parent <parent_id> --strategy "description". Omit --parent for the first trial (t0).python scripts/benchmark.py <baseline_file> <triton_file> [--triton-baseline] (measures both baseline and triton).python scripts/trial_manager.py baseline-us <kernel_name>, then run python scripts/benchmark.py <baseline_file> <triton_file> [--triton-baseline] --baseline-us <cached_value> (skips baseline perf, saves time).finalize: Re-run benchmark.py without --baseline-us for final accurate comparison.python scripts/trial_manager.py result <kernel_name> <trial_id> --validation pass --correctness <pass|fail> --speedup <float> --baseline_us <float> --triton_us <float> (runtimes from benchmark output).vtune_enabled is true in config.yaml) — Run python scripts/xpu_profiler.py <triton_file> after your first benchmarked trial. Use its output to guide subsequent trial strategies. Run again if speedup plateaus after 2+ additional trials. Skip this step entirely if vtune_enabled is false.references/xpu_optimizations.yamlreferences/optimization_levels.yamlreferences/optimization_strategies.md for the full "try harder" decision tree| Doc | Contents |
|---|---|
references/implementation_reference.md | Code templates, Model class pattern, GEMM example |
references/optimization_strategies.md | Strategy reference, optimization levels, checklist |
references/workflow_details.md | Detailed workflow, decision tree, benchmarking/validation details |
references/correctness.yaml | Critical constraints to avoid bugs |
references/xpu_optimizations.yaml | XPU-specific patterns (tensor descriptors, GRF, swizzling) |
references/fusion_patterns.yaml | When to fuse vs split operations |
references/optimization_levels.yaml | Progressive optimization with "try harder" decision tree |
The test_kernels/*.py Triton files (non-pytorch) are unoptimized baselines. They use manual pointer arithmetic, lack autotune, and miss XPU optimizations. Do NOT copy their patterns. Use references/implementation_reference.md instead.
Tensor descriptors produce better address generation and memory access codegen than block pointers on Intel XPU.
Use the large register file for compute-heavy kernels:
For autotuned XPU kernels, prune configs that are likely to spill even after large-GRF recompilation. This avoids slow double-compile/benchmark cycles where the Intel backend reports spills, recompiles in large GRF mode, and still spills.
Use an early_config_prune that estimates per-thread GRF pressure from the live accumulator and operand tiles:
Guidelines:
BM, BN, and BK from either autotune config kwargs or launch args.num_stages for pipelined MMA paths; use 1 for scalar/non-pipelined paths.This complements grf_mode='256': large GRF increases the register budget, while pruning removes configs that are still too register-heavy to be useful.
For an implementation pattern, see references/xpu_optimizations.yaml (xpu_register_spill_autotune_pruning).
Use 1D grid with GROUP_SIZE_M for L2 locality:
@triton.autotune meta-parameters in kernel signatureboundary_check uses dimension indices (0, 1), not booleansint64 before stride multiplicationnn.Module with nn.Linear)get_inputs(), get_init_inputs(), and module-level constants from *_pytorch.pyFull constraint list: correctness.yaml
Measured on Intel Battlemage G21 / Arc Pro B50 (128 XVEs). All runtimes are median of benchmark trials.
Speedup is vs. PyTorch eager baseline. Includes GEMM+Sigmoid+Scaling, GEMM+GELU+Softmax, Conv+BatchNorm+ReLU, and other fused patterns.
Baseline is the flash attention kernel from the Intel XPU Triton backend; speedup is vs. that kernel across multiple sequence lengths.
Full results: see the Xe-Forge repository.
| Issue | Symptom | Fix |
|---|---|---|
| Autotune BLOCK_D | Wrong results (max_abs 4-8+) | Never autotune BLOCK_D. Use triton.next_power_of_2(D) |
| Python min/max | Runtime error | tl.minimum()/tl.maximum() |