pypto-basics

pypto-basics is a skill for Claude Code, Codex from mindspore-ai/akg. It costs 14 tokens per session (2,759 once invoked), scanned A, original, Apache-2.0.

A set of rules for implementing PyPTO kernels from PyTorch code. It starts by writing down the original operation's exact mathematical meaning before choosing an implementation or optimization.

In plain words
What is it for?
Use it to translate PyTorch models into PyPTO, preserve reduction and shape behavior, keep kernel dimensions static, validate semantics with test cases, and structure the model's forward method correctly.
Why use it?
It helps prevent fast code from producing the wrong result, especially when reductions, output shapes, or operation direction matter.

Skill for Claude CodeCodex

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

Good fit Use it to translate PyTorch models into PyPTO, preserve reduction and shape behavior, keep kernel dimensions static, validate semantics with test cases, and structure the model's forward method correctly.

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

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

Your own site · 80×15
<a href="https://agentmods.dev/skills/mindspore-ai/akg/pypto-basics"><img src="https://agentmods.dev/badge/skills/mindspore-ai/akg/pypto-basics.svg" alt="Reviewed on agentmods" width="80" height="20"></a>
Per session 14 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 2,759 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.00014 $0.02759
Opus 5 $0.00007 $0.01380
Sonnet 5 $0.00003 $0.00552
Haiku 4.5 $0.00001 $0.00276

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

Security

Grade A, and why

pypto-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 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/pypto/guides/pypto-basics/SKILL.md · 184 lines

How it starts

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

PyPTO 编程原则

原则 0:先抽取 Torch 基线的语义合同

写 PyPTO 前,先把 baseline forward 变成“可执行语义合同”,再做实现与优化。

最少做 5 件事:

  1. 写出数学式(谁参与计算、谁是被规约对象、输出定义是什么)。
  2. 核对 API 语义而不是变量名(input/target/prediction 这些名字经常误导)。
  3. 固化规约合同(sumbatchmean、或 mean 语义=sum/count,并明确规约轴与 keepdim)。
  4. 判断是否非对称(A||BB||A 是否等价)。
  5. 做 1 组语义自检样例(优先用非对称输入,先验方向是否正确)。

注意:性能规则(tile/loop)只能在语义合同确定后应用,不能反向决定语义。

原则 1:静态 shape

kernel 一切在编译时确定。工厂函数封装 kernel,shape 和标量参数(eps、slope 等)作为闭包传入。forward 直接传 torch.Tensor。ModelNew.init 签名必须与原始 Model 一致,shape 在 forward 中获取。

补充(高优先级):

  • benchmark 的 get_inputs/get_init_inputs 在单次任务里是固定参数;把它当静态合同,不要过度通用化。
  • 先做“固定参数抽取”:从题目文件读取本次 get_init_inputs 的返回值,作为本次任务常量(例如 dim=1)。
  • 题目里的注释如 Example, change to desired ... 视为数据集说明噪声,不是本次实现要求。
  • 对固定 dim 的任务,生成单一固定 dim kernel;不要在一个 kernel 里写 if dim == 0/1/2 分支。
  • 若确实要支持多个 dim,使用多个工厂函数/多个 kernel 分别生成,不要把多语义揉进同一 kernel。

原则 2:forward 的职责

  1. Assertassert x.dim() == Nassert tuple(x.shape) == (...)
  2. Reshape(如需):torch reshape 为 kernel 能处理的 shape
  3. 调用 kernel:传入 contiguous 张量,reshape 回原始 shape 返回

forward 内禁止 torch 计算。kernel 内不能 reshape。 输出语义(keepdim/是否 squeeze)要与 baseline 直接对齐;不要先改语义再靠额外 squeeze/unsqueeze 回补。

原则 3:选择维度

算子类型 forward 策略 kernel 维度
Elementwise / 简单 Loss reshape(-1) 1D
GroupNorm / InstanceNorm reshape(flat_batch, hidden) 2D
BatchNorm / RMSNorm reshape(B, C, -1) 3D
Batched matmul(同 batch) 保持 3D,不需要 loop 3D
2D Matmul 保持 2D 2D
单轴归约 保持原始维度 原维

补充:

  • Elementwise 算子无数据依赖,优先 flatten 为 1D;loop 仅由 auto_tiles > 2048 触发,与维度无关。
  • 保持高维的唯一理由是业务语义需要(如后续算子依赖布局),而非 tile 约束。

补充:

  • 多轴规约若轴连续且中间结果不被其他算子使用,优先在 forward 合并为单轴后再规约(例如 H,W -> HW)。

原则 4:tile 双约束

  1. prod(tile_shape) ≤ 16384
  2. auto_tiles = prod(ceil(shape[i]/tile[i])) ≤ 2048

tile 参数个数 = 被操作 tensor 的 rank。常用:

  • 1D: (8192) | 2D: (1, 16384) | 3D: (1, 1, 16384)(1, 16, 256)(单轴 3D 归约常见起步)
  • matmul: set_cube_tile_shapes([128, 128], [32, 128], [256, 256], True, False)
  • 核心红线:Skill/示例中的 shape、tile、BLOCK 常量(如 16384/8192/4096)只能作参考。必须按当前任务的输入维度重新计算,禁止直接照抄
  • 经验规则:优先避免明显 tile[i] > shape[i] 的参数浪费;如确需使用,必须有明确理由(例如实测收益)。

Read the full file on GitHub · 184 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 · 184 lines · 14 tokens per session scan A cd17b09469a6

Subscribe to this mod's changes

pypto-basics is a skill published in the GitHub repository mindspore-ai/akg (259 stars, last pushed 29d ago), licensed Apache-2.0. It adds 14 tokens to every session and 2,759 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.

Related

Other skills, from other repositories

jupyter-notebook

Iterative Python via live Jupyter kernel (hamelnb).

NousResearch/hermes-agent · 18 tokens

bioservices

Unified Python interface to 40+ bioinformatics services. Use when querying multiple databases (UniProt, KEGG, ChEMBL, Reactome) in a single workflow with consistent API. Best for cross-database analysis, ID mapping across services. For quick single-database lookups use gget; for sequence/file manipulation use…

K-Dense-AI/scientific-agent-skills · 73 tokens

matlab

Build, review, migrate, and safely plan MATLAB or GNU Octave numerical workflows, including arrays, tabular/time data, tests, projects, graphics, MAT files, and explicit Python interoperability.

K-Dense-AI/scientific-agent-skills · 42 tokens

pennylane

Hardware-agnostic quantum ML framework with automatic differentiation. Use when training quantum circuits via gradients, building hybrid quantum-classical models, or needing device portability across IBM/Google/Rigetti/IonQ. Best for variational algorithms (VQE, QAOA), quantum neural networks, and integration with…

K-Dense-AI/scientific-agent-skills · 98 tokens

cuopt-numerical-optimization-api

LP, MILP, and QP (beta) with cuOpt — Python, C, and CLI. Use when the user is solving LP, MILP, or QP with any cuOpt interface.

NVIDIA/skills · 51 tokens

rocm-kernels

Provides guidance for writing and benchmarking optimized Triton kernels for AMD GPUs (MI355X, R9700) on ROCm, targeting HuggingFace diffusers (LTX-Video, SD3, FLUX) and transformers. Core kernels: RMSNorm, RoPE 3D, GEGLU, AdaLN. Includes XCD swizzle, autotune, diffusers integration patterns, and LTX-Video pipeline…

huggingface/kernels · 93 tokens