triton-cuda-grid-config

triton-cuda-grid-config is a skill for Claude Code, Codex from mindspore-ai/akg. It costs 65 tokens per session (1,986 once invoked), scanned A, original, Apache-2.0.

A guide to choosing the grid and block layout used to launch Triton CUDA kernels. It explains one-, two-, and three-dimensional grids, block sizes, and handling very large inputs.

In plain words
What is it for?
Use it when configuring launches for element-wise operations, reductions, matrix multiplication, or kernels with unusually large inputs.
Why use it?
An unsuitable launch layout can waste GPU capacity or fail on large data shapes. These rules help map elements, rows, and matrix tiles to parallel programs.

Skill for Claude CodeCodex

Written for no agent in particular: nothing here depends on one.

Good fit Use it when configuring launches for element-wise operations, reductions, matrix multiplication, or kernels with unusually large inputs.

Compare 6 skills from other repositories ↓
Install with agentmods
npx agentmods add skills/mindspore-ai/akg/triton-cuda-grid-config
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.

Any agent
npx skills add mindspore-ai/akg --skill triton-cuda-grid-config
Clone the repo
git clone --depth 1 https://github.com/mindspore-ai/akg

Made for: Claude Code, Codex.

Wrote this? Show the measurements

A badge with what this costs and how it scanned, read live from this page, so it follows the numbers instead of freezing them. Markdown for a README, HTML for a documentation site or a project page.

agentmods badge for triton-cuda-grid-config

README.md
[![agentmods](https://agentmods.dev/badge/skills/mindspore-ai/akg/triton-cuda-grid-config/github.svg)](https://agentmods.dev/skills/mindspore-ai/akg/triton-cuda-grid-config)
Your own site
<a href="https://agentmods.dev/skills/mindspore-ai/akg/triton-cuda-grid-config"><img src="https://agentmods.dev/badge/skills/mindspore-ai/akg/triton-cuda-grid-config/github.svg" alt="Measured on agentmods" height="20"></a>

Or the 80×15 button, for a site that already has a row of RSS and ATOM ones. Only the verdict fits; the numbers stay here.

agentmods 80×15 button for triton-cuda-grid-config

Your own site · 80×15
<a href="https://agentmods.dev/skills/mindspore-ai/akg/triton-cuda-grid-config"><img src="https://agentmods.dev/badge/skills/mindspore-ai/akg/triton-cuda-grid-config.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 65 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 1,986 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. A grade says what 26 rules found in the file — not that it is safe. Third-party audits
  • NVIDIA SkillSpector pass 7 Sept 2026
How audits are shown
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.1 $0.00065 $0.01986
Opus 5 $0.00032 $0.00993
Sonnet 5 $0.00013 $0.00397
Haiku 4.5 $0.00006 $0.00199

Measured 9d ago against content hash 65e250d09b03, method: parsed. Prices are Anthropic first-party input rates as of 2026-09-12, from the pricing page.

Security

Grade A, and why

triton-cuda-grid-config 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 9d 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.

akg_agents/python/akg_agents/op/resources/skills/triton-cuda/guides/triton-cuda-grid-config/SKILL.md · 271 lines

How it starts

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

Grid 配置策略

Grid 配置是 Triton Kernel 启动的关键。本文档提供 Triton CUDA 的 Grid 配置策略和大 shape 处理方案。


1. Grid 设置规范

维度格式

  • Grid 必须是 tuple 类型,最多 3 维
  • 支持的格式:(x,), (x, y), (x, y, z)
# 正确
grid = (100,)
grid = (100, 200)
grid = (100, 200, 50)

# 错误
grid = 100  # 必须是 tuple
grid = [100, 200]  # 必须是 tuple,不能是 list

使用 lambda(autotune 场景)

当使用 autotune 时,grid 必须使用 lambda:

# autotune 时必须使用 lambda
grid = lambda meta: (triton.cdiv(M, meta['BLOCK_SIZE_M']) * triton.cdiv(N, meta['BLOCK_SIZE_N']),)

# 非 autotune 时可以直接计算
grid = (triton.cdiv(n_elements, BLOCK_SIZE),)

2. 1D Grid 配置

Element-wise 算子

最常见的配置方式:每个 block 处理 BLOCK_SIZE 个元素。

n_elements = input_tensor.numel()
BLOCK_SIZE = 1024
grid = (triton.cdiv(n_elements, BLOCK_SIZE),)

kernel[grid](input_tensor, output_tensor, n_elements, BLOCK_SIZE=BLOCK_SIZE)

逐行处理(Reduce 类算子)

每个 block 处理一行或多行:

n_rows, n_cols = x.shape
BLOCK_SIZE = triton.next_power_of_2(n_cols)

# 方式 1:每行一个 block
grid = (n_rows,)

# 方式 2:限制并行度(grid stride loop)
num_programs = min(n_rows, 65535)
grid = (num_programs,)

3. 2D Grid 配置

MatMul 类算子

使用 2D Grid 进行行列双向并行:

BLOCK_M, BLOCK_N = 128, 256
grid_m = triton.cdiv(M, BLOCK_M)
grid_n = triton.cdiv(N, BLOCK_N)

# 方式 1:2D Grid
grid = (grid_m, grid_n)

# 方式 2:1D Grid(更灵活,支持 Grouped Ordering)
grid = (grid_m * grid_n,)

1D vs 2D Grid

特性 1D Grid 2D Grid
灵活性 高(支持 Grouped Ordering)
代码复杂度 需要手动计算 pid_m, pid_n 直接获取
L2 缓存优化 容易实现 不易实现
推荐场景 MatMul(需要缓存优化) 简单 2D 算子

推荐: 对于 MatMul 类算子,使用 1D Grid + Grouped Ordering。


4. 大 Shape 处理:Grid Stride Loop

问题描述

CUDA GPU 对 grid 大小也有限制(通常 2^31 - 1 per dimension),但更重要的是,过大的 grid 会导致:

  • 启动开销增加
  • 资源浪费(每个 block 只处理少量数据)

Grid Stride Loop 方案

每个 block 通过循环处理多个数据块:

@triton.jit
def grid_stride_kernel(
    input_ptr, output_ptr, n_elements,
    BLOCK_SIZE: tl.constexpr,
):
    pid = tl.program_id(0)
    num_pids = tl.num_programs(0)
    
    # Grid stride loop
    for block_start in range(pid * BLOCK_SIZE, n_elements, num_pids * BLOCK_SIZE):
        offsets = block_start + tl.arange(0, BLOCK_SIZE)
        mask = offsets < n_elements
        
        data = tl.load(input_ptr + offsets, mask=mask)
        result = compute(data)
        tl.store(output_ptr + offsets, result, mask=mask)

# 限制 grid 大小
MAX_GRID = 65535
num_blocks = min(triton.cdiv(n_elements, BLOCK_SIZE), MAX_GRID)
grid = (num_blocks,)
grid_stride_kernel[grid](input_tensor, output_tensor, n_elements, BLOCK_SIZE=1024)

Read the full file on GitHub · 271 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. 9d ago First seen · 271 lines · 65 tokens per session scan A 65e250d09b03

Subscribe to this mod's changes

triton-cuda-grid-config is a skill published in the GitHub repository mindspore-ai/akg (259 stars, last pushed 1mo ago), licensed Apache-2.0. It adds 65 tokens to every session and 1,986 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-09-03.