CANN内核编写指南

发布时间:2026/7/12 17:27:20

CANN内核编写指南 Kernel Authoring Playbook【免费下载链接】cannbot-skillsCANNBot 是面向 CANN 开发的用于提升开发效率的系列智能体本仓库为其提供可复用的 Skills 模块。项目地址: https://gitcode.com/cann/cannbot-skillsUse this playbook when writing a new kernel or replacing a kernel body in a major way. Keep the path short, tool-first, and incremental.Step 0 (prerequisite): settle the contract firstBefore anything below, do:agent/playbooks/clarify-first.mdIf the contract is still ambiguous, stop and ask the user before you start coding.GoalProduce a kernel that:matches the exact PyTorch contractuses a justified topologystays within device, sync, and precision rulesis validated stage by stage instead of guessed into existence1. Fast path: prefer tools before large proseFor most new kernels, the cheapest route is:shortlist examples with the selector toolpick the topology and print a fresh scaffoldestimate tile/core split only when the shape is non-trivialopen one source example only after the selector or index narrowed it downRecommended commands:conda run -n torch210npu python agent/scripts/select_kernel_example.py --query qk softmax pv --topology cube-vec-cube-vec --limit 3 --catalog conda run -n torch210npu python agent/scripts/gen_kernel_skeleton.py --name preview_kernel --topology cube-vec --print conda run -n torch210npu python agent/scripts/estimate_matmul_datamove.py --helpIf the selector tool is not enough, fall back to:agent/references/examples/kernel-index.mdone matchingagent/references/examples/kernel-catalog.mdentryone source file underagent/example/kernels/2. Minimal read map by scenarioIf you need raw values before choosing a deeper route, jump straight to one focused facts page:agent/references/facts-device-runtime.mdfor device caps, pipe pairs, and mutex signaturesagent/references/facts-authoring.mdfor hard rules, DBuff formulas, and a2 bridge remindersagent/references/facts-simulator-opexec.mdfor simulator /shape_bindings/OpExecgotchasThen branch to only one focused follow-up when possible.SituationRead nextOpen more only if...pure cube matmul or layout rewriteagent/references/patterns/cube-only.mdsplit choice, unusual layout, or family tile is still uncleara5 kernel with any vec-side stageagent/references/constraints/a5-device.mdvf()is not enough and you needmicro/ sort specificsa2 cube - vecagent/references/patterns/a2-cube-vec.mdreduction, tail, or workspace behavior is still uncleara2 cube - vec - cubeagent/references/patterns/a2-cube-vec-cube.mddelayed stage ownership still feels ambiguousa2 normalized online softmax (score - p - pv - final divide)agent/references/patterns/a2-cube-vec-cube-vec-softmax.mdyou hit a special failure mode that page does not already coversync / counter warningagent/references/constraints/autosync.mdoragent/references/constraints/counters.mdthe warning persists after you traced the producer / consumer lifetimelowering or simulator behavior looks wrongagent/references/code-paths.mdthe problem is clearly in runtime / parser behavior, not the kernel math3. Authoring rules that still matter on every kernelKeep the file layout simple:imports and constantshelpervf/funcblocksmainkernelvisible__main__validation storyPrefer normal Pythonif/elif/elseinside kernels; the repository rewrites them before instruction capture.On a5, vec-side math belongs invf()/micro; do not copy an a2-style direct vec body into an a5 kernel.Keep local buffers full-tile sized. Usevalid_m/valid_nonly at GM read/write boundaries unless a focused constraint file says otherwise.auto_sync()is same-side ordering only. Cross-side ownership changes still needCvMutexorVcMutex.Keep matmul accumulation infloatunless the contract or an established family says otherwise.If scalar dimensions may alias at runtime, addshape_bindingsat theOpExec(...)(...)call site before rewriting the kernel body.4. Implementation loopUse the same small loop for every serious kernel:build the smallest honest slice of the formulavalidate one aligned caseadd the next stage or tail pathvalidate againonly then optimize or fuse furtherPractical rules:keep the PyTorch reference inline in__main__keep one aligned case and one tail case visiblefor large fused kernels, keep standalone stage runners alive until the merged version passesstudy examples for legal patterns, not for copy-paste5. When to leave the fast pathOpen lower-level implementation files only when the smaller guidance layers stop being enough. Typical escalation path:easyasc/stub_functions/easyasc/parser/easyasc/simulator_v2/Do not treat passing output with unresolved warnings as good enough. Warnings usually mean the model is incomplete.【免费下载链接】cannbot-skillsCANNBot 是面向 CANN 开发的用于提升开发效率的系列智能体本仓库为其提供可复用的 Skills 模块。项目地址: https://gitcode.com/cann/cannbot-skills创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

相关新闻