triton-kernel-optimization

A guide for writing and tuning Triton GPU kernels, which are GPU programs written with Triton's Python-based programming system. It covers automatic block-size tuning, tiled matrix multiplication, fused operations, reductions, attention, quantization, gradients, and profiling.

In plain words
What is it for?
Use it when creating, porting, profiling, or benchmarking Triton kernels for PyTorch or standalone use.
Why use it?
It provides concrete ways to test different GPU configurations and improve memory access and computation in Triton kernels.

Skill for Claude CodeCodex

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add skills/amd-agi/apex/triton-kernel-optimization
Any agent
npx skills add AMD-AGI/Apex --skill triton-kernel-optimization
Clone the repo
git clone --depth 1 https://github.com/AMD-AGI/Apex

Made for: Claude Code, Codex.

Per session 56 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 3,577 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00056 $0.03577
Opus 5 $0.00028 $0.01788
Sonnet 5 $0.00011 $0.00715
Haiku 4.5 $0.00006 $0.00358

Measured 3d ago against content hash 6e7d269fc2a2, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

triton-kernel-optimization scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 3d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

tools/skills/triton-kernel-optimization/SKILL.md · 386 lines

How it starts

The opening of the file, as written. The whole thing — 386 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Triton Kernel Optimization

Purpose

Provide production-validated patterns and tuning tactics for performant Triton kernels on AMD MI-series GPUs.

When to Use

  • Authoring new Triton kernels for PyTorch or standalone use
  • Porting CUDA/HIP concepts into Triton with equivalent performance
  • Profiling and benchmarking Triton kernels

Optimization Priority

Phase 1: Foundation (correct and basic performance)

  1. Use @triton.autotune with configs covering key block sizes (64/128/256)
  2. Use @triton.heuristics for compile-time optimizations (e.g., EVEN_K)
  3. Apply tl.assume for stride positivity to help compiler optimize
  4. Separate boundary handling from main computation path
  5. Use tl.constexpr for all compile-time constants

Phase 2: Memory Optimization 6. Implement cache modifiers (.ca, .cg) for L2 cache control 7. Use split-K for improved L2 reuse on large K dimensions 8. Apply XCD remapping (remap_xcd) for multi-die GPUs (MI250X, MI300) 9. Optimize GROUP_SIZE_M for better L2 locality 10. Pre-shuffle weight layouts for better vectorization

Phase 3: Advanced Techniques 11. Implement persistent kernels for repeated operations 12. Use attention sink for stable long-context attention 13. Fuse quantization with GEMM (e.g., blockscale + matmul) 14. Apply per-token or per-tensor quantization strategies 15. Use grouped GEMM for mixture-of-experts workloads

Anti-patterns:

  • Hardcoding block sizes without autotune
  • Ignoring tail handling (non-divisible shapes)
  • Not using tl.assume for known constraints
  • Excessive register pressure from large tile sizes
  • Unnecessary synchronization or atomic operations

Core Optimization Patterns

1. Autotuning and Heuristics

Autotune configuration:

@triton.autotune(
    configs=[
        triton.Config({'BLOCK_SIZE_M': 128, 'BLOCK_SIZE_N': 256, 'BLOCK_SIZE_K': 64,
                       'GROUP_SIZE_M': 8}, num_warps=8, num_stages=4),
        triton.Config({'BLOCK_SIZE_M': 64, 'BLOCK_SIZE_N': 256, 'BLOCK_SIZE_K': 32,
                       'GROUP_SIZE_M': 8}, num_warps=4, num_stages=5),
        triton.Config({'BLOCK_SIZE_M': 128, 'BLOCK_SIZE_N': 128, 'BLOCK_SIZE_K': 32,
                       'GROUP_SIZE_M': 4}, num_warps=4, num_stages=3),
    ],
    key=['M', 'N', 'K'],  # Tune based on problem dimensions
)
@triton.heuristics({
    'EVEN_K': lambda args: args['K'] % args['BLOCK_SIZE_K'] == 0,
    'GRID_MN': lambda args: triton.cdiv(args['M'], args['BLOCK_SIZE_M'])
                          * triton.cdiv(args['N'], args['BLOCK_SIZE_N']),
})
@triton.jit
def gemm_kernel(..., EVEN_K: tl.constexpr, GRID_MN: tl.constexpr):
    # Use EVEN_K to skip boundary checks in hot loop
    if EVEN_K:
        a = tl.load(a_ptrs)  # No mask needed
    else:
        a = tl.load(a_ptrs, mask=mask_k)

Read the full file on GitHub · 386 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 3d ago First seen · 386 lines · 56 tokens per session scan A 6e7d269fc2a2

Subscribe to this mod's changes

triton-kernel-optimization is a skill published in the GitHub repository AMD-AGI/Apex (76 stars, last pushed 6d ago), licensed MIT. It adds 56 tokens to every session and 3,577 once invoked, about $0.0003 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.

Related

Other skills, from other repositories

dali-dynamic-mode

DALI imperative dynamic mode (nvidia.dali.experimental.dynamic, ndd): use when working on ndd code or migrating pipelines; skip pipeline-only tasks.

NVIDIA/skills · 38 tokens

datachain-core

Use ONLY for abstract DataChain SDK questions — API usage, method signatures, or code patterns — when no specific dataset or bucket is referenced. If the request mentions creating, saving, listing, exploring datasets or buckets, use datachain-knowledge instead.

datachain-ai/datachain · 54 tokens

mflux-model-porting

Port ML models into mflux/MLX with correctness-first validation, then refactor toward mflux style.

mflux-community/mflux · 28 tokens

minicpm5-finetune-trl

Fine-tune MiniCPM5-1B with bare-metal TRL + PEFT, including assistant-only loss via a chat-template patch. Use when the user wants minimal Python, no YAML, full control, or asks for "TRL", "SFTTrainer", "PEFT", "LoraConfig", "assistantonlyloss".

OpenBMB/MiniCPM · 78 tokens

minicpm5-finetune-unsloth

Fine-tune MiniCPM5-1B with unsloth for tight-VRAM single-GPU LoRA / QLoRA. Use when the user wants "unsloth", "FastLanguageModel", QLoRA on a 24 GB consumer GPU, or asks for the smallest VRAM footprint.

OpenBMB/MiniCPM · 74 tokens

minicpm5-deploy-transformers

Run MiniCPM5-1B with Hugging Face Transformers for one-shot Python generation on GPU (bfloat16) or CPU (float32). Use when the user wants a quick Python script, no server, no extra deps, or asks for "Transformers", "AutoModelForCausalLM", "model.generate" with MiniCPM5.

OpenBMB/MiniCPM · 82 tokens