triton-ascend-matmul

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

An optimization guide for matrix multiplication, the operation that combines rows and columns of numbers to produce a new matrix. It explains tiling, matrix-compute cores, memory limits, alignment, and core selection on Ascend devices.

In plain words
What is it for?
Use it when implementing or tuning matmul, linear layers, batched matmul, GEMM, or similar matrix-based kernels in Triton.
Why use it?
It helps prevent hardware-buffer overflow and incorrect results caused by using the wrong core type or tile dimensions.

Skill for Claude CodeCodex

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

Good fit Use it when implementing or tuning matmul, linear layers, batched matmul, GEMM, or similar matrix-based kernels in Triton.

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

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

Your own site · 80×15
<a href="https://agentmods.dev/skills/mindspore-ai/akg/triton-ascend-matmul"><img src="https://agentmods.dev/badge/skills/mindspore-ai/akg/triton-ascend-matmul.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 162 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 1,700 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.00162 $0.01700
Opus 5 $0.00081 $0.00850
Sonnet 5 $0.00032 $0.00340
Haiku 4.5 $0.00016 $0.00170

Measured 9d ago against content hash 4b90502e8803, 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-matmul 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-ascend/guides/triton-ascend-matmul/SKILL.md · 148 lines

How it starts

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

MatMul 算子优化

适用于矩阵乘法及相关运算

核心数选择(硬约束)

  • 涉及 tl.dot / 矩阵乘法运算 → 必须使用 CUBE_CORE_NUM
  • 混合运算(先 matmul 再 elementwise 后处理)→ CUBE_CORE_NUM
  • 纯 elementwise / 标量运算 → VEC_CORE_NUM

使用 VEC_CORE_NUM 启动 matmul kernel 会导致数值结果错误。

Tile Size 限制(硬件约束)

MatMul 数据走 L0A/L0B/L0C,tile 大小受硬件存储容量限制,超出会导致 ub overflow / cbuf overflow 编译错误。

约束公式(具体容量参考目标硬件信息文档):

  • L0A:BLOCK_M × BLOCK_K × sizeof(dtype) ≤ L0A容量
  • L0B:BLOCK_K × BLOCK_N × sizeof(dtype) ≤ L0B容量
  • L0C:BLOCK_M × BLOCK_N × sizeof(acc_dtype) ≤ L0C容量

ub overflow / cbuf overflow缩小 BLOCK_M, BLOCK_N 或 BLOCK_K

Ascend 后端切分优化

关键原则: 充分发挥带宽,算子行宽为 512B 的整数倍。

以 fp16/bf16 为例(每个元素 2 字节):

切分配置(根据转置情况)

  1. A、B 都不转置

    • 分块行宽分别为 K0 和 N0
    • 推荐: M0=128, K0=256, N0=256
  2. A 不转置,B 转置

    • 分块行宽都是 K0
    • 推荐: K0=256, M0 和 N0 影响较小
  3. A、B 都转置

    • 分块行宽分别为 M0 和 K0
    • 推荐: M0=256, K0=256, N0=128
  4. A 转置,B 不转置

    • 分块行宽分别为 M0 和 N0
    • 注意: 左右矩阵均无法同时满足 512B 的整数倍,需根据实际情况调整

为什么是 512B?

  • 512B = 256 个 fp16/bf16 元素(256 × 2 字节)
  • NPU 的最佳带宽对齐
  • 确保每次内存访问充分利用带宽

固定核心数启动

MatMul 算子使用 CUBE核心数(矩阵计算核心)。

关键: 使用 grid=(num_cores,) 而非 (NUM_BLOCKS,)

@triton.jit
def matmul_kernel(
    a_ptr, b_ptr, c_ptr,
    M, N, K,
    num_cores: tl.constexpr,
    BLOCK_M: tl.constexpr,
    BLOCK_N: tl.constexpr,
    BLOCK_K: tl.constexpr,
):
    # 关键:使用固定核心数启动,每个核心处理多个块
    pid = tl.program_id(0)  # 核心ID: 0~num_cores-1
    NUM_BLOCKS_M = triton.cdiv(M, BLOCK_M)
    NUM_BLOCKS_N = triton.cdiv(N, BLOCK_N)
    NUM_BLOCKS = NUM_BLOCKS_M * NUM_BLOCKS_N

    # 每个核心循环处理多个块
    for block_idx in range(pid, NUM_BLOCKS, num_cores):
        # 计算当前块的2D索引
        block_m = block_idx // NUM_BLOCKS_N
        block_n = block_idx % NUM_BLOCKS_N

        # 初始化累加器
        accumulator = tl.zeros((BLOCK_M, BLOCK_N), dtype=tl.float32)

        # K维度循环
        for k in range(0, K, BLOCK_K):
            # 加载A块
            a_offset = (block_m * BLOCK_M + tl.arange(0, BLOCK_M))[:, None] * K + \
                       (k + tl.arange(0, BLOCK_K))[None, :]
            a_mask = (block_m * BLOCK_M + tl.arange(0, BLOCK_M))[:, None] < M
            a = tl.load(a_ptr + a_offset, mask=a_mask, other=0.0)

            # 加载B块
            b_offset = (k + tl.arange(0, BLOCK_K))[:, None] * N + \
                       (block_n * BLOCK_N + tl.arange(0, BLOCK_N))[None, :]
            b_mask = (block_n * BLOCK_N + tl.arange(0, BLOCK_N))[None, :] < N
            b = tl.load(b_ptr + b_offset, mask=b_mask, other=0.0)

            # 矩阵乘累加
            accumulator += tl.dot(a, b)

        # 存储结果
        c_offset = (block_m * BLOCK_M + tl.arange(0, BLOCK_M))[:, None] * N + \
                   (block_n * BLOCK_N + tl.arange(0, BLOCK_N))[None, :]
        c_mask = ((block_m * BLOCK_M + tl.arange(0, BLOCK_M))[:, None] < M) & \
                 ((block_n * BLOCK_N + tl.arange(0, BLOCK_N))[None, :] < N)
        tl.store(c_ptr + c_offset, accumulator, mask=c_mask)

class ModelNew(torch.nn.Module):
    def __init__(self):
        super().__init__()
        try:
            self.CUBE_CORE_NUM = torch_npu.npu.npu_config.get_device_limit(0).get("cube_core_num", 20)
        except:
            self.CUBE_CORE_NUM = 20

    def forward(self, a, b):
        M, K = a.shape
        K2, N = b.shape
        assert K == K2
        c = torch.empty((M, N), device=a.device, dtype=a.dtype)

        num_cores = self.CUBE_CORE_NUM
        BLOCK_M, BLOCK_N, BLOCK_K = 128, 256, 256

        matmul_kernel[(num_cores,)](
            a, b, c, M, N, K, num_cores,
            BLOCK_M, BLOCK_N, BLOCK_K
        )
        return c

Read the full file on GitHub · 148 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 · 148 lines · 162 tokens per session scan A 4b90502e8803

Subscribe to this mod's changes

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

Related

Other skills, from other repositories

fs-notebook-tabs

A computer-science capstone: an on-device ML keyboard that predicts next words privately — problem, method, evaluation, and defense answers. Built as a decision-grade coursework defense deck for professor, defense committee.

nexu-io/open-design · 47 tokens

implementing-llms-litgpt

Implements and trains LLMs using Lightning AI's LitGPT with 20+ pretrained architectures (Llama, Gemma, Phi, Qwen, Mistral). Use when need clean model implementations, educational understanding of architectures, or production fine-tuning with LoRA/QLoRA. Single-file implementations, no abstraction layers.

davila7/claude-code-templates · 77 tokens

nanogpt

Educational GPT implementation in 300 lines. Reproduces GPT-2 (124M) on OpenWebText. Clean, hackable code for learning transformers. By Andrej Karpathy. Perfect for understanding GPT architecture from scratch. Train on Shakespeare (CPU) or OpenWebText (multi-GPU).

davila7/claude-code-templates · 65 tokens

rwkv-architecture

RNN+Transformer hybrid with O(n) inference. Linear time, infinite context, no KV cache. Train like GPT (parallel), infer like RNN (sequential). Linux Foundation AI project. Production at Windows, Office, NeMo. RWKV-7 (March 2025). Models up to 14B parameters.

davila7/claude-code-templates · 72 tokens

lesson-quiz

Test a learner on a single Claude Code tutorial lesson (01-10) with 10 questions, scoring answers and flagging weak spots. Use before, during, or after a lesson. Don't use for whole-tutorial assessment or explaining a topic instead of testing it.

luongnv89/claude-howto · 58 tokens

kaggle-learner

This skill should be used when the user asks to "learn from Kaggle", "study Kaggle solutions", "analyze Kaggle competitions", or mentions Kaggle competition URLs. Provides access to extracted knowledge from winning Kaggle solutions across NLP, CV, time series, tabular, and multimodal domains.

Galaxy-Dawn/claude-scholar · 69 tokens