pypto-golden-generate

Generates torch_npu golden reference implementations with validation and NPU performance profiling.

Updated Sep 15, 2026
One-click install
npx skills add https://github.com/WangWindow/CANN-BatchMatMulMaxsum --skill pypto-golden-generate-wangwindow
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: pypto-golden-generate
Source: https://github.com/WangWindow/CANN-BatchMatMulMaxsum/tree/main/.agents/skills/pypto-golden-generate
Command: npx skills add https://github.com/WangWindow/CANN-BatchMatMulMaxsum --skill pypto-golden-generate-wangwindow

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires torch, torch_npu, and includes scripts (resource) and references (resource) components.

What problem does it solve? Developing custom Ascend NPU operators requires a trusted numerical baseline to verify correctness, but hand-writing PyTorch reference implementations and their validation harnesses is repetitive and error-prone. This Skill automates the generation of {op}_golden.py files from operator specifications, complete with input factories, self-validation, and NPU performance profiling. ## Core Features & Use Cases - Deterministic scaffold generation: gen_golden_scaffold.py parses SPEC.md front matter and input/output tables to emit function signatures, _make_inputs() factories, and _validate() harnesses, leaving only the math body as TODOs. - NPU-first execution with controlled fallback: golden code runs on NPU via torch_npu, errors out with install guidance when torch_npu is missing, and falls back to CPU only when no NPU hardware exists. - Mandatory verification and profiling gates: generated files must pass python3 {op}_golden.py (exit code 0) and produce GOLDEN_PERF_REPORT.md via profile_golden.py before downstream stages. - Use Case: Given a SPEC.md describing a fused BatchMatmulMaxSum operator with P0 shapes and dtypes, generate a validated batchmatmulmaxsum_golden.py that aligns matmul FP32 accumulation semantics and profiles each P0 shape on an Ascend NPU. ## Quick Start Generate a golden reference implementation for my operator from its SPEC.md, validate it on the NPU, and produce the performance report.

Frequently Asked Questions about pypto-golden-generate

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I generate a golden reference implementation for an NPU operator?

Provide the operator specification with name, math formula, and input/output specs. The scaffold script parses SPEC.md front matter and tables to emit the function signature, _make_inputs factory, and _validate harness; you only fill in the math body TODOs, then run python3 {op}_golden.py to validate.

How do I profile a PyTorch golden function on Ascend NPU?

Run scripts/profile_golden.py against the golden file after validation passes. Use --input/--arg for random-value-safe operators or --factory _make_inputs for constrained inputs; it writes GOLDEN_PERF_REPORT.md with per-op kernel durations extracted from kernel_details.csv.

Can golden code fall back to CPU when torch_npu is missing?

No. If torch_npu is not installed, the golden script raises an ImportError with install guidance. CPU fallback is allowed only when torch_npu is installed but no NPU hardware exists, detected via device_count() == 0.

Why must torch.matmul inputs be converted to float before multiplication?

Calling torch.matmul on BF16 tensors and converting afterward still performs BF16 accumulation, which diverges from the NPU Cube FP32 accumulation path. Converting both inputs with .float() before matmul aligns the golden numerics with PyPTO hardware behavior.

How do I run golden validation on a specific NPU card?

Set the TILE_FWK_DEVICE_ID environment variable before running, for example export TILE_FWK_DEVICE_ID=3. Hardcoding torch.device("npu:3") in generated code is forbidden; the template's _get_device() reads the variable and defaults to card 0.

What are the limitations of random-input profiling mode?

The --input mode fails for operators with semantic constraints such as block table indices, state caches, or shape interdependencies, because random values cause out-of-range or mismatch crashes. Such operators must use --factory _make_inputs with manually constructed legal inputs.