CANNBot分片约束指南 Tiling Constraints【免费下载链接】cannbot-skillsCANNBot 是面向 CANN 开发的用于提升开发效率的系列智能体本仓库为其提供可复用的 Skills 模块。项目地址: https://gitcode.com/cann/cannbot-skillsRead this file when a kernel needs non-trivial tile selection, core split selection, or local-buffer capacity reasoning. Do not read it for tiny untiled examples.GoalChoose a tile strategy that is:legal for local-buffer capacitycompatible with downstream vec-side dependencystable for ownership and tail handlingjustified from the real repository rules1. Separate two decisionsTreat these as separate layers:tile shape:TILE_M,TILE_N,TILE_Kcore split:m_split,n_splitDo not collapse them into one vague best tiling decision.2. Default ownership patternFor standard cube-major matmuls, split by tile index, not by raw row range:tile_m CeilDiv(M, TILE_M)tile_m_per_core CeilDiv(tile_m, GetCubeNum())iteratemt in [tile_m_begin, tile_m_end)If the chosen strategy returns bothm_split 1andn_split 1, mapGetCubeIdx()onto a 2D split grid and partition both axes explicitly.For batched independent small matmuls, flatten batch-like dimensions first and split directly over the flattened batch axis:BHN B * H * Nbhn_per_core CeilDiv(BHN, GetCubeNum())iteratebhn_idx in [bhn_begin, bhn_end)This is simpler than tile splitting when each item is one full small matrix pair.3. Use the estimator when the choice is not obviousUseagent/scripts/estimate_matmul_datamove.pywhen:the matmul is large enough that core split mattersthere are multiple legal tile candidatesdownstream vec work constrains which axis may be splitRead the estimator result as:tile shapecore splitloop modecandidate tie set, if anyChoosesplit_modefrom downstream dependency, not only from cube-side datamove:split_m: keep allNtiles for the sameMrows together on one coresplit_n: keep allMtiles for the sameNcolumns together on one coremix: both axes may be split because vec-side work does not impose a one-axis ownership constraintIf two candidates tie on datamove, break the tie with downstream local-memory fit instead of taking the first row mechanically. Concrete example: forx y.t() 1.0, both256x128x256and128x256x256may tie on datamove for the large shape; the latter is the better fit because the vec stage can reuse a64x256UB tile directly.4. Device-specific capacity and core countBuffer budgets and core count differ between a2 and a5. For exact values, seeagent/references/facts-device-runtime.md. Forsplitk/splitnelement count and byte-budget formulas, seeagent/references/facts-authoring.md.Consequences that matter for tile strategy:a5 tile strategies that fitDBuff L0C(e.g.TILE_M128, TILE_N256 → 128*256*4*2 256 KB) willoverflow on a2(128 KB).a5 core split usesGetCubeNum()32; a2 uses20. Verify load balance separately for each target.a5 vec-sideDBuff UBallocations up to256 KBmust stay within192 KBon a2.Practical rule: when writing an a2 kernel, always verifyL0C DBufffirst.DBuffallocates 2 slots, so a singlel0callocation needs2 * TILE_M * TILE_N * sizeof(dtype) l0c_cap.5. Capacity rules are mandatory authoring checksBefore blaming the simulator, check the real buffer budgets. The per-device caps (L0A,L0B,L0C,UB) live inagent/references/facts-device-runtime.md, and thesplitk/splitnelement count / byte-budget formulas live inagent/references/facts-authoring.md. Convert element counts into bytes using the real local-buffer dtype size, and require each buffer to stay within its cap.6. ChoosingsplitkvssplitnUse the overflowing side to choose the split mode (do not switch blindly).Choosesplitkwhen K-side staging intoL0A/L0Bis too large, or when you want to keep a large outerTILE_Kfor strategy selection but legalize the real inner load size.Choosesplitnwhen N-side staging is too large, or when output-tile width pushes the buffer budget too far.Hard rule (splitk,splitn32) and the validated large-Kaligned MKNK example (TILE_M128, TILE_N256, TILE_K256, SPLIT_K64) live inagent/references/facts-authoring.md.Fallback rules:ifsplitkfails even at32, retileTILE_M/TILE_Nifsplitnfails even at32, trysplitkinstead of pushingsplitnlowerPractical rule for the validated pattern: choosem_split/n_splitfrom the outer tile; usesplitkonly to legalize the inner cube staging.7.L0Cauthoring ruleTreat non-zeroL0Crow offsets on matmul destinations as unsupported in authoring. Even though the simulator has an offset path, the repository rule is:keep matmul destinations anchored at row offset0solve oversizedMwith a higher-levelTILE_Mdecision instead ofl0c[row_offset:..., ...]N-side subdivision is still fine when the destination remains anchored.8. Quick checklistBefore accepting a tiled kernel, verify:target device identified(a2/a5) and device-specific budgets usedtile shape chosen explicitlycore split chosen explicitly, using the correct core count for the devicesplit mode matches downstream dependencyL0A/L0Bbyte budgets checkedL0CDBuff totalchecked against the device-specificl0c_capUBDBuff totalchecked against the device-specificub_cap(when vec stages exist)splitkorsplitnkept at 32L0Cdestination remains row-offset0ownership and counters still make sense for the chosen loop structureFiles to studyagent/scripts/estimate_matmul_datamove.pyagent/scripts/tools_summary.mdagent/example/kernels/a5/matmul_mknk_2dgrid_splitn.pyagent/example/kernels/a5/matmul_mknk_2dgrid_splitk.pyagent/example/kernels/a5/matmul_mknk_2dgrid_splitk_add1.pyagent/example/kernels/a5/matmul_kmkn_blockwise_quant128.py【免费下载链接】cannbot-skillsCANNBot 是面向 CANN 开发的用于提升开发效率的系列智能体本仓库为其提供可复用的 Skills 模块。项目地址: https://gitcode.com/cann/cannbot-skills创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考