triton-cuda-elementwise

triton-cuda-elementwise is a skill for Claude Code, Codex from mindspore-ai/akg. It costs 71 tokens per session (2,043 once invoked), scanned A, original, Apache-2.0.

A guide to implementing and optimizing operations that process tensor elements independently, such as addition, multiplication, activation functions, and mathematical functions. It includes vectorized access and operation fusion patterns.

In plain words
What is it for?
Use it to write Triton kernels for arithmetic, activation, broadcasting, and other element-by-element tensor operations.
Why use it?
Basic tensor operations can spend too much time moving data between memory and the GPU. These patterns help structure them with fewer unnecessary steps.

Skill for Claude CodeCodex

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

Good fit Use it to write Triton kernels for arithmetic, activation, broadcasting, and other element-by-element tensor operations.

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

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

Your own site · 80×15
<a href="https://agentmods.dev/skills/mindspore-ai/akg/triton-cuda-elementwise"><img src="https://agentmods.dev/badge/skills/mindspore-ai/akg/triton-cuda-elementwise.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 71 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 2,043 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.00071 $0.02043
Opus 5 $0.00036 $0.01022
Sonnet 5 $0.00014 $0.00409
Haiku 4.5 $0.00007 $0.00204

Measured 8d ago against content hash 55b54b496a0d, method: parsed. Prices are Anthropic first-party input rates as of 2026-09-11, from the pricing page.

Security

Grade A, and why

triton-cuda-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 8d 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-elementwise/SKILL.md · 234 lines

How it starts

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

Element-wise 算子优化

适用于逐元素独立计算的算子

适用算子

算术运算: add, mul, div, sub, pow 激活函数: relu, sigmoid, tanh(需用 tl.extra.cuda.libdevice.tanh), gelu, silu, swish 数学函数: exp, log, sqrt, sin, cos, abs

优化策略

1. 连续内存访问优化

张量在内存中连续存储时,可用一维指针遍历,避免多维索引开销。

方案 1: 转连续 + 一维访问(推荐)

class ModelNew(torch.nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self, input_tensor):
        # 非连续张量转为连续(一次性开销)
        if not input_tensor.is_contiguous():
            input_tensor = input_tensor.contiguous()
        
        output_tensor = torch.empty_like(input_tensor)
        n_elements = input_tensor.numel()
        grid = (triton.cdiv(n_elements, BLOCK_SIZE),)
        
        elementwise_kernel[grid](input_tensor, output_tensor, n_elements, BLOCK_SIZE)
        return output_tensor

@triton.jit
def elementwise_kernel(input_ptr, output_ptr, n_elements, BLOCK_SIZE: tl.constexpr):
    pid = tl.program_id(0)
    offsets = pid * BLOCK_SIZE + 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)

优势:

  • .contiguous() 一次性开销 vs stride 每次访问都有开销
  • 更好的合并访问(coalesced access)
  • 编译器优化更容易

方案 2: 使用 stride 访问(不推荐)

仅当无法调用 .contiguous() 时使用。

2. BLOCK_SIZE 选择

  • 推荐值: 256, 512, 1024
  • 原则: 平衡并行度和资源占用
  • GPU 考量:
    • 更大的 BLOCK_SIZE → 更少的 block 启动开销,但可能降低 occupancy
    • 更小的 BLOCK_SIZE → 更细粒度的并行,但启动开销增加
    • 确保 Grid 大小足够大以充分利用 GPU

3. Warp 配置

Element-wise 算子通常使用较少的 warp:

@triton.autotune(
    configs=[
        triton.Config({'BLOCK_SIZE': 1024}, num_warps=4),
        triton.Config({'BLOCK_SIZE': 512}, num_warps=2),
        triton.Config({'BLOCK_SIZE': 2048}, num_warps=8),
    ],
    key=['n_elements'],
    restore_value=['output_ptr'],  # 必须:列出所有输出指针参数名
)
@triton.jit
def optimized_kernel(input_ptr, output_ptr, n_elements, BLOCK_SIZE: tl.constexpr):
    pid = tl.program_id(0)
    offsets = pid * BLOCK_SIZE + 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)

Read the full file on GitHub · 234 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. 8d ago First seen · 234 lines · 71 tokens per session scan A 55b54b496a0d

Subscribe to this mod's changes

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