triton-ascend-elementwise

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

An optimization guide for element-wise operations, where each output value depends on the matching input value. It covers operations such as ReLU, sigmoid, arithmetic, casting, clamping, and copying on Ascend devices.

In plain words
What is it for?
Use it to write or optimize Triton Ascend kernels for point-by-point tensor operations and scalar broadcasting.
Why use it?
It shows how to map independent element calculations onto vector cores while handling tensor size, memory boundaries, and device layout.

Skill for Claude CodeCodex

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

Good fit Use it to write or optimize Triton Ascend kernels for point-by-point tensor operations and scalar broadcasting.

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

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

Your own site · 80×15
<a href="https://agentmods.dev/skills/mindspore-ai/akg/triton-ascend-elementwise"><img src="https://agentmods.dev/badge/skills/mindspore-ai/akg/triton-ascend-elementwise.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 205 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 1,077 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.00205 $0.01077
Opus 5 $0.00102 $0.00539
Sonnet 5 $0.00041 $0.00215
Haiku 4.5 $0.00020 $0.00108

Measured 6d ago against content hash 90748dfa63aa, method: parsed. Prices are Anthropic first-party input rates as of 2026-09-09, from the pricing page.

Security

Grade A, and why

triton-ascend-elementwise 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 6d 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-elementwise/SKILL.md · 102 lines

How it starts

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

Element-wise 算子编写指南

编写模式

Element-wise 算子的核心特征:每个输出元素仅依赖对应位置的输入元素,无跨元素依赖。 通用写法是将张量展平为 1D,用交错循环按 block 遍历全部元素。

标准写法

@triton.jit
def elementwise_kernel(
    input_ptr, output_ptr, n_elements,
    BLOCK_SIZE: tl.constexpr, CORE_NUM: tl.constexpr,
):
    pid = tl.program_id(0)
    num_blocks = tl.cdiv(n_elements, BLOCK_SIZE)
    for block_id in range(pid, num_blocks, CORE_NUM):
        offsets = block_id * BLOCK_SIZE + tl.arange(0, BLOCK_SIZE)
        mask = offsets < n_elements
        x = tl.load(input_ptr + offsets, mask=mask, other=0.0)
        y = compute(x)  # 替换为具体计算
        tl.store(output_ptr + offsets, y, mask=mask)

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

    def forward(self, x):
        if not x.is_contiguous():
            x = x.contiguous()
        y = torch.empty_like(x)
        n = x.numel()
        grid = (self.VEC_CORE_NUM,)
        elementwise_kernel[grid](x, y, n, BLOCK_SIZE=1024, CORE_NUM=self.VEC_CORE_NUM)
        return y

要点

  • .contiguous() 保证一维指针连续访问,避免 stride 计算
  • torch.empty_like 创建输出(不用 zeros,省初始化开销)
  • forward 的参数签名和数量必须与原始 Model.forward 一致

优化技巧

1. 连续内存访问

展平为一维后用连续偏移访问,缓存命中率最高:

  • 非连续张量先 .contiguous()
  • x.numel() 获取总元素数,忽略原始 shape

2. BLOCK_SIZE 选择

  • 推荐 1024-2048,平衡流水效率和 UB 占用
  • 数据量很小时可降到 256-512
  • 数据量很大时不需要增大 BLOCK_SIZE,交错循环自动均衡

3. 数值稳定性

  • exp 前减最大值防溢出
  • sqrt 前确保非负:tl.maximum(x, 0.0)tl.maximum(x, eps)
  • 中间计算用 float32 累加,最后转回目标精度

4. 融合多步计算

连续的 elementwise 操作应融合在同一个 kernel 内,避免多次 GM 读写:

# 融合 x -> relu -> scale -> add_bias
y = tl.maximum(x, 0.0)  # relu
y = y * scale            # scale
y = y + bias             # add_bias

5. 广播处理

当一个输入是标量或需要广播时,在 kernel 外部处理或在 kernel 中用常量加载:

# 标量作为 kernel 参数传入
@triton.jit
def scale_kernel(x_ptr, out_ptr, scale_val, n, BLOCK_SIZE: tl.constexpr, CORE_NUM: tl.constexpr):
    ...
    y = tl.load(x_ptr + offs, mask=mask, other=0.0) * scale_val

Read the full file on GitHub · 102 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. 6d ago First seen · 102 lines · 205 tokens per session scan A 90748dfa63aa

Subscribe to this mod's changes

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