triton-ascend-case-matmul-swizzle2d

triton-ascend-case-matmul-swizzle2d is a skill for Claude Code, Codex from mindspore-ai/akg. It costs 88 tokens per session (1,017 once invoked), scanned A, original, Apache-2.0.

An optimization guide for Triton matrix-multiplication kernels on Ascend processors. Matrix multiplication combines rows and columns of two number grids to produce a third grid.

In plain words
What is it for?
Use it for large, compute-heavy matrix multiplications on Ascend devices. It covers fixed core counts and two-dimensional block reordering, called Swizzle2D.
Why use it?
It avoids starting more work groups than the processor can handle at once and rearranges work to improve cache use and workload balance.

Skill for Claude CodeCodex

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

Good fit Use it for large, compute-heavy matrix multiplications on Ascend devices. It covers fixed core counts and two-dimensional block reordering, called Swizzle2D.

Compare 6 skills from other repositories ↓
Install with agentmods
npx agentmods add skills/mindspore-ai/akg/triton-ascend-case-matmul-swizzle2d
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-ascend-case-matmul-swizzle2d
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-ascend-case-matmul-swizzle2d

README.md
[![agentmods](https://agentmods.dev/badge/skills/mindspore-ai/akg/triton-ascend-case-matmul-swizzle2d/github.svg)](https://agentmods.dev/skills/mindspore-ai/akg/triton-ascend-case-matmul-swizzle2d)
Your own site
<a href="https://agentmods.dev/skills/mindspore-ai/akg/triton-ascend-case-matmul-swizzle2d"><img src="https://agentmods.dev/badge/skills/mindspore-ai/akg/triton-ascend-case-matmul-swizzle2d/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-ascend-case-matmul-swizzle2d

Your own site · 80×15
<a href="https://agentmods.dev/skills/mindspore-ai/akg/triton-ascend-case-matmul-swizzle2d"><img src="https://agentmods.dev/badge/skills/mindspore-ai/akg/triton-ascend-case-matmul-swizzle2d.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 88 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 1,017 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.00088 $0.01017
Opus 5 $0.00044 $0.00508
Sonnet 5 $0.00018 $0.00203
Haiku 4.5 $0.00009 $0.00102

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

Security

Grade A, and why

triton-ascend-case-matmul-swizzle2d 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 13d 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-ascend/cases/triton-ascend-case-matmul-swizzle2d/SKILL.md · 96 lines

How it starts

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

矩阵乘法 Swizzle2D 优化案例

任务特征

  • 操作类型:矩阵乘法 A[M, K] @ B[K, N] = C[M, N]
  • 数据尺寸:A[2048, 7168] @ B[7168, 16384] = C[2048, 16384]
  • 特点:计算密集型,核心分配策略对缓存命中率和负载均衡影响显著

优化 1:固定核心数启动(最重要!)

错误:错误:启动所有块

grid = (NUM_BLOCKS_M * NUM_BLOCKS_N,)  # 启动1024个程序

正确:正确:固定核心数启动

num_cores = 20  # Ascend 910B4有20个AI Core

@triton.jit
def matmul_kernel(..., num_cores: tl.constexpr):
    pid = tl.program_id(axis=0)  # 0~19
    NUM_BLOCKS = NUM_BLOCKS_M * NUM_BLOCKS_N
    
    # 每个核心循环处理多个块
    for block_idx in range(pid, NUM_BLOCKS, num_cores):
        # 处理块...
        pass

matmul_kernel[(num_cores,)](...)  # grid=(20,)

核心要点:Ascend NPU必须使用固定核心数启动,每个核心循环处理多个块。

优化 2:Swizzle2D 块重排

@triton.jit
def matmul_kernel_swizzle2d(..., GROUP_SIZE: tl.constexpr, DIRECTION: tl.constexpr):
    for block_idx in range(pid, NUM_BLOCKS, num_cores):
        block_m = block_idx // NUM_BLOCKS_N
        block_n = block_idx % NUM_BLOCKS_N
        
        if DIRECTION == 0:  # M≥N: 行优先分组
            task_m_idx, task_n_idx = tl.swizzle2d(
                block_m, block_n, NUM_BLOCKS_M, NUM_BLOCKS_N, GROUP_SIZE
            )
        else:  # M<N: 列优先分组(手动实现)
            size_gj = GROUP_SIZE * NUM_BLOCKS_M
            group_id = block_idx // size_gj
            off_n = group_id * GROUP_SIZE
            cur_size_g = tl.minimum(NUM_BLOCKS_N - off_n, GROUP_SIZE)
            local_ij = block_idx % size_gj
            task_m_idx = local_ij // cur_size_g
            task_n_idx = off_n + local_ij % cur_size_g

优化内容

  • Swizzle2D通过GROUP_SIZE将块按组重排,组内块共享数据
  • GROUP_SIZE推荐值为4,可通过autotune搜索[1,2,3,4,5,8]

优化 3:矩阵形状自适应

DIRECTION = 1 if m < n else 0  # M<N列优先, M≥N行优先
  • M≥N时:行优先分组,减少mat_a重复加载
  • M<N时:列优先分组,减少mat_b重复加载

优化 4:分块大小选择

# float16/bfloat16
BLOCK_M, BLOCK_K, BLOCK_N = 128, 256, 256

# float32
BLOCK_M, BLOCK_K, BLOCK_N = 128, 128, 128

总结

  1. 固定核心数启动grid=(20,),每个核心循环处理多个块
  2. Swizzle2D 重排:通过块分组提升缓存局部性
  3. 自适应分组方向:根据M/N比例选择行优先或列优先
  4. 合适的分块大小:根据数据类型和缓存容量选择

Read the full file on GitHub · 96 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. 13d ago First seen · 96 lines · 88 tokens per session scan A 9278a8853a73

Subscribe to this mod's changes

triton-ascend-case-matmul-swizzle2d is a skill published in the GitHub repository mindspore-ai/akg (259 stars, last pushed 1mo ago), licensed Apache-2.0. It adds 88 tokens to every session and 1,017 once invoked, about $0.0004 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