cpu-basics

cpu-basics is a skill for Claude Code, Codex from mindspore-ai/akg. It costs 31 tokens per session (4,040 once invoked), scanned A, original, Apache-2.0.

A guide to writing CPU operations in C++ for PyTorch, including functions compiled and loaded as extensions. It covers tensor data, memory, types, bounds, and a standard five-step kernel structure.

In plain words
What is it for?
Use it when building or reviewing CPU C++ kernels, especially code that accesses tensor data directly through pointers.
Why use it?
It gives a consistent way to handle tensor layouts, data types, memory, and output creation when writing low-level CPU code.

Skill for Claude CodeCodex

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.

agentmods
npx agentmods add skills/mindspore-ai/akg/cpu-basics
Any agent
npx skills add mindspore-ai/akg --skill cpu-basics
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 cpu-basics

README.md
[![agentmods](https://agentmods.dev/badge/skills/mindspore-ai/akg/cpu-basics.svg)](https://agentmods.dev/skills/mindspore-ai/akg/cpu-basics)
Your own site
<a href="https://agentmods.dev/skills/mindspore-ai/akg/cpu-basics"><img src="https://agentmods.dev/badge/skills/mindspore-ai/akg/cpu-basics.svg" alt="Measured on agentmods" height="20"></a>
Per session 31 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 4,040 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
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.00031 $0.04040
Opus 5 $0.00015 $0.02020
Sonnet 5 $0.00006 $0.00808
Haiku 4.5 $0.00003 $0.00404

Measured 5d ago against content hash 446e70fb89e3, method: parsed. Prices are Anthropic first-party input rates as of 2026-09-05, from the pricing page.

Security

Grade A, and why

cpu-basics 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 5d 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/cpp/guides/cpu-basics/SKILL.md · 428 lines

How it starts

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

CPU C++ 编程基础

1. 核心概念

内核 (Kernel)

  • 定义: 使用 PYBIND11_MODULE 注册的 C++ 函数,编译后在 CPU 上执行
  • 特点: 直接操作张量数据指针,支持多种数据类型
  • 形式: 使用 PyTorch C++ 扩展,通过 load_inline 动态编译加载

张量处理

  • 连续性: 确保张量内存布局连续,避免非连续访问
  • 类型统一: 内部计算使用统一类型(优先 float32/float64/int32/int64),最后转换回原类型
  • 边界检查: 所有数组访问前必须检查边界

内存管理

  • 自动管理: PyTorch 自动管理张量内存生命周期
  • 指针操作: 直接操作数据指针进行高效计算
  • 类型安全: 确保指针类型与张量类型匹配

2. 标准内核结构(五步模式)

所有 CPU C++ 内核都遵循相同的五步结构模式:

torch::Tensor standard_kernel(torch::Tensor x) {
    // 1. 确保输入张量是连续的
    if (!x.is_contiguous()) {
        x = x.contiguous();
    }
    
    // 2. 检查数据类型,支持多种类型
    torch::ScalarType dtype = x.scalar_type();
    bool need_convert = (dtype != torch::kFloat32 && dtype != torch::kFloat64 && 
                        dtype != torch::kInt32 && dtype != torch::kInt64);
    torch::Tensor input = need_convert ? x.to(torch::kFloat32) : x;

    // 3. 创建输出张量
    torch::Tensor output = torch::zeros_like(input);

    // 4. 根据数据类型分发计算
    if (input.scalar_type() == torch::kFloat32) {
        auto x_ptr = input.data_ptr<float>();
        auto out_ptr = output.data_ptr<float>();
        int64_t numel = input.numel();
        for (int64_t i = 0; i < numel; ++i) {
            out_ptr[i] = std::max(0.0f, x_ptr[i]);  // ReLU: max(0, x)
        }
    } else if (input.scalar_type() == torch::kFloat64) {
        auto x_ptr = input.data_ptr<double>();
        auto out_ptr = output.data_ptr<double>();
        int64_t numel = input.numel();
        for (int64_t i = 0; i < numel; ++i) {
            out_ptr[i] = std::max(0.0, x_ptr[i]);
        }
    } else if (input.scalar_type() == torch::kInt32) {
        auto x_ptr = input.data_ptr<int32_t>();
        auto out_ptr = output.data_ptr<int32_t>();
        int64_t numel = input.numel();
        for (int64_t i = 0; i < numel; ++i) {
            out_ptr[i] = std::max(0, x_ptr[i]);
        }
    } else if (input.scalar_type() == torch::kInt64) {
        auto x_ptr = input.data_ptr<int64_t>();
        auto out_ptr = output.data_ptr<int64_t>();
        int64_t numel = input.numel();
        for (int64_t i = 0; i < numel; ++i) {
            out_ptr[i] = std::max(0L, x_ptr[i]);
        }
    }

    // 5. 转换回原类型
    if (need_convert) {
        output = output.to(dtype);
    }
    return output;
}

Read the full file on GitHub · 428 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. 5d ago First seen · 428 lines · 31 tokens per session scan A 446e70fb89e3

Subscribe to this mod's changes

cpu-basics is a skill published in the GitHub repository mindspore-ai/akg (259 stars, last pushed 25d ago), licensed Apache-2.0. It adds 31 tokens to every session and 4,040 once invoked, about $0.0002 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.