cuda-c-examples-torch

cuda-c-examples-torch is a skill for Claude Code, Codex from mindspore-ai/akg. It costs 19 tokens per session (4,204 once invoked), scanned A, original, Apache-2.0.

A collection of runnable examples showing how to write CUDA C kernels and connect them to PyTorch. The examples use PyTorch’s `load_inline` to compile and load the C++ and CUDA code at runtime.

In plain words
What is it for?
Use it as a starting point for PyTorch custom CUDA kernels, such as vector addition and other GPU operations supported by the examples.
Why use it?
It gives developers a concrete integration pattern for adding custom GPU operations to PyTorch without building a separate extension project first. The examples show the surrounding declarations, compilation, launch, and wrapper code.

Skill for Claude CodeCodex

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

Good fit Use it as a starting point for PyTorch custom CUDA kernels, such as vector addition and other GPU operations supported by the examples.

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

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

Your own site · 80×15
<a href="https://agentmods.dev/skills/mindspore-ai/akg/cuda-c-examples-torch"><img src="https://agentmods.dev/badge/skills/mindspore-ai/akg/cuda-c-examples-torch.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 19 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 4,204 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.00019 $0.04204
Opus 5 $0.00010 $0.02102
Sonnet 5 $0.00004 $0.00841
Haiku 4.5 $0.00002 $0.00420

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

Security

Grade A, and why

cuda-c-examples-torch 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 10d 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/cuda-c/guides/cuda-c-examples-torch/SKILL.md · 579 lines

How it starts

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

PyTorch + CUDA C 示例代码

本 Skill 包含完整的可运行示例代码,展示如何在 PyTorch 中使用 CUDA C 编写高性能 kernel,通过 load_inline JIT 编译集成。

集成模式

所有 CUDA C 内核都通过以下模式与 PyTorch 集成:

import torch
from torch.utils.cpp_extension import load_inline

# 1. CUDA 源代码(内核定义 + 调用函数)
cuda_source = """
#include <torch/extension.h>
#include <cuda_runtime.h>

__global__ void my_kernel(const float* input, float* output, int size) {
    // 内核实现
}

torch::Tensor my_kernel_call(torch::Tensor input) {
    auto size = input.numel();
    auto output = torch::zeros_like(input);
    int block_size = 256;
    int num_blocks = (size + block_size - 1) / block_size;
    my_kernel<<<num_blocks, block_size>>>(
        input.data_ptr<float>(), output.data_ptr<float>(), size);
    return output;
}
"""

# 2. C++ 函数声明
cpp_source = "torch::Tensor my_kernel_call(torch::Tensor input);"

# 3. JIT 编译
module = load_inline(
    name="my_cuda",
    cpp_sources=cpp_source,
    cuda_sources=cuda_source,
    functions=["my_kernel_call"],
    verbose=True,
    extra_cflags=[""],
    extra_ldflags=[""],
)

# 4. 调用
def my_op(x):
    return module.my_kernel_call(x)

示例列表

1. 向量加法(Vector Add)

算子类型: Element-wise 关键点:

  • 最简单的 CUDA C 内核示例
  • 一维索引和边界检查
  • 标准五步模式
import torch
from torch.utils.cpp_extension import load_inline

cuda_source = """
#include <torch/extension.h>
#include <cuda_runtime.h>

__global__ void vector_add_kernel(
    const float* a, const float* b, float* c, int n
) {
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    if (idx < n) {
        c[idx] = a[idx] + b[idx];
    }
}

torch::Tensor vector_add_call(torch::Tensor a, torch::Tensor b) {
    auto n = a.numel();
    auto c = torch::empty_like(a);
    int block_size = 256;
    int num_blocks = (n + block_size - 1) / block_size;
    vector_add_kernel<<<num_blocks, block_size>>>(
        a.data_ptr<float>(), b.data_ptr<float>(),
        c.data_ptr<float>(), n);
    return c;
}
"""

cpp_source = "torch::Tensor vector_add_call(torch::Tensor a, torch::Tensor b);"

module = load_inline(
    name="vector_add_cuda",
    cpp_sources=cpp_source,
    cuda_sources=cuda_source,
    functions=["vector_add_call"],
    verbose=True,
    extra_cflags=[""],
    extra_ldflags=[""],
)

def vector_add(a, b):
    return module.vector_add_call(a, b)

Read the full file on GitHub · 579 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. 10d ago First seen · 579 lines · 19 tokens per session scan A 1c1a40230cda

Subscribe to this mod's changes

cuda-c-examples-torch is a skill published in the GitHub repository mindspore-ai/akg (259 stars, last pushed 1mo ago), licensed Apache-2.0. It adds 19 tokens to every session and 4,204 once invoked, about $0.0001 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.