triton-npu-optimize
Iteratively optimize a Triton Ascend NPU operator with correctness and performance gates. Use for operator optimization tasks that need repeated correctness validation, benchmark validation, multi-round experiment tracking, reusable optimization notes, and profiler-backed performance analysis when benchmark results need deeper explanation.
- License 许可
- Apache-2.0
- 所属仓库
- Piagari/helix
- 最近更新
- 2026-07-16
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- triton-npu-optimize
- description
- Iteratively optimize a Triton Ascend NPU operator with correctness and performance gates. Use for operator optimization tasks that need repeated correctness validation, benchmark validation, multi-round experiment tracking, reusable optimization notes, and profiler-backed performance analysis when benchmark results need deeper explanation.
02 资源文件 3
- 参考文档references/artifacts.md13.1 KB
- 参考文档references/opt-note-format.md3.6 KB
- 参考文档references/round-failure-handling.md2.6 KB
03 SKILL.md 正文
Optimize
Goal
Optimize one Triton Ascend NPU operator through validated rounds anchored to a canonical baseline/.
Use this skill when the user wants the operator itself improved rather than only generating or running tests and benchmarks.
Optimize target modes:
kernel: optimize the Triton Ascend NPU kernel path itself. Compare rounds with a kernel-orientedcompare-perfview, but if the comparison falls back to total-op for some cases, record the resolvedeffective_metric_sourceand surface that mismatch as a warning instead of discarding the round outright.operator: optimize end-to-end operator latency. Wrapper logic, data movement, scheduling, pre-processing, post-processing, and kernel code are all valid optimization surfaces as long as the Triton Ascend NPU computation path remains real. Show both kernel and total-opcompare-perfviews, but use the total-op view as the canonical round conclusion.
Inputs
- Operator source code or an operator file path
Outputs
baseline/opt-round-N/- completed round entries and one final
## Overall Summaryinopt-note.md - round-local
profile/,ir/,perf-analysis.md, orcompiler-analysis.mdartifacts when deeper investigation is needed
Core Loop
- establish or reuse
baseline/ - open
opt-round-N/, initialize round strategy state throughascend-npu-optimize-state start-round, and startattempts.md - choose the current analysis level
- treat each round as one code-changing optimization attempt followed by canonical validation
- make one coherent optimization attempt
- optionally screen the direction cheaply with
probe-benchwhen the available run-eval surface exposes it - validate correctness and benchmark performance
- After the first canonical
run-benchpluscompare-perfconclusion for that attempt, stop editing the current round. - record the round outcome
Stage 0: Baseline Setup
- Reuse the existing
baseline/only when it remains canonical for the current operator workspace. - Otherwise use the sibling
ascend-npu-prepare-optimize-baselineskill to establish or repair the baseline before creatingopt-round-1/. - Establish or reuse
baseline/before treating anyopt-round-N/directory as a completed optimization round. - Read artifacts.md before choosing authoritative baseline or round artifact paths.
- Read opt-note-format.md before initializing
opt-note.md. - Keep top-level optimize workflow references at the skill boundary: use sibling skills for baseline preparation, evaluation, profiling, IR analysis, compiler-source analysis, and round gating rather than direct helper-script paths here.
Stage 1: Round Entry
- Create
opt-round-N/from a validated parent candidate and keep parent-child traceability explicit. - Use the sibling
ascend-npu-optimize-stateskill'sstart-roundsubcommand to initialize the active round'sround_strategy,analysis_policy, andreasonbefore the first code change in that round. - Start
attempts.mdimmediately so every meaningful attempt and measurement is recorded. - Treat the structured
State Updateblocks inattempts.mdas script-written workflow history; do not manually duplicate the sameround_strategy,analysis_policy, andreasonbookkeeping in bothattempts.mdandsummary.md. - For round 1, record the initial round hypothesis in
opt-round-1/attempts.mdbefore the first code change. - When pattern triage is used, explicitly record the candidate patterns you considered, the selected pattern if one is chosen, and why that pattern looks plausible in
attempts.md. - When a named pattern guides the round, explicitly record the final selected pattern direction in
summary.md. - Record the current analysis level as
Primary analysis level, and recordSupporting evidenceseparately. - Record why that level may help and what evidence supports starting there.
- If the round starts from reused deeper evidence, cite the reused evidence path and explain why the shallower level is already established or insufficient.
- Treat
opt-note.mdas the top-level round ledger plus final## Overall Summary. - If the active round's intent or required evidence depth changes mid-round, use the sibling
ascend-npu-optimize-stateskill'sset-current-round-statesubcommand instead of silently changing the round contract in prose only.
Stage 2: Layered Analysis
Optimize analysis is layered.
- Default escalation order:
pattern triage -> profiling diagnosis -> IR attribution -> compiler-source escalation. - Start each round at the shallowest level that can justify the next move.
- Escalate only when the current level is insufficient.
- Keep
Primary analysis leveldistinct fromSupporting evidencein round records. - Using IR as supporting evidence does not automatically change the round's primary analysis level.
- Record the chosen level and why the round stayed there or escalated deeper.
- Show the level explicitly in
attempts.md, for examplePrimary analysis level: profiling diagnosis. - When a round escalates, record both
Escalation: <from> -> <to>andEscalation reason: <why the previous level was insufficient>. - Do not rely on the presence of
profile/,ir/, orperf-analysis.mdto imply the current level; state it directly.
pattern triage
- Inspect current code structure and benchmark behavior before choosing a direction.
- Use the staged
triton-npu-optimize-knowledgeskill as the generic optimize knowledge library. - When the optimize target is
operator, also use the stagedtorch-npu-optimize-knowledgeskill for Torch NPU and whole-operator pattern routing such as framework-op fallback, wrapper-level changes, or broader operator restructuring. - Read the staged
triton-npu-optimize-knowledgeskill'sreferences/pattern_index.mdbefore detailed pattern references. - Read only the one or two most relevant detailed pattern files under the staged
triton-npu-optimize-knowledgeskill'sreferences/patterns/directory after the generated index has narrowed the candidate set. - When the optimize target is
operatorand the bottleneck looks Torch NPU or framework-op specific, read the stagedtorch-npu-optimize-knowledgeskill'sreferences/pattern_index.mdbefore detailed Torch NPU pattern references. - When code structure is still unclear at pattern triage, inspect the operator file directly and narrow candidates with the generated pattern index.
- Do not leave the chosen pattern implicit in free-form prose; write it down explicitly in
attempts.md, and carry the final named pattern direction intosummary.mdwhen it guided the round. - Very strongly consider using subagents to read pattern references, scan for potentially useful optimization ideas, and report back which patterns look promising for the current kernel.
- When subagents are available, prefer using them to broaden pattern exploration before committing to the round hypothesis.
- Pattern references are helpful guidance, not the only allowed source of ideas.
- If your own Triton, Ascend NPU, or kernel-optimization knowledge suggests a stronger direction than the current pattern library, you may use that direction directly as long as you still record the hypothesis clearly and validate it with the same correctness and benchmark gates.
- You do not need an existing pattern file to justify every optimization round.
- When the kernel is structurally matmul-like, read the staged
triton-npu-optimize-knowledgeskill'sreferences/patterns/classic-matmul.mdbefore rewriting the hot loop so the round records the standard tiled-matmul shape, dtype, and masking rules explicitly. - Do not treat pattern triage as permission for aimless pattern search without tying the candidate patterns back to the kernel structure or observed evidence.
profiling diagnosis
- Use profiling diagnosis as the default deeper entrypoint when pattern triage is not enough.
- Use the sibling
ascend-npu-profile-operatorskill when benchmark numbers need operator-level performance evidence, hotspot diagnosis, bottleneck analysis, or profiler-backed comparison across runs. - Use the sibling
ascend-npu-analyze-round-performanceskill when one round needs a deeper diagnosis that should end inopt-round-N/perf-analysis.md, especially for scalar/vector/cube imbalance, transfer-heavy behavior, or suspected pipeline overlap issues. - Use the sibling knowledge skill's symptom cards to narrow pattern candidates after structured profiler or IR evidence exists, rather than rereading the whole pattern library.
- This deeper diagnosis may end as either
profile-only diagnosisorprofile-plus-IR diagnosis. - Write
opt-round-N/perf-analysis.mdwhen the deeper round-analysis flow is used.
IR attribution
- Use IR attribution only after profiler-backed symptoms still need explanation.
ascend-npu-analyze-round-performancemay still ownopt-round-N/perf-analysis.mdwhen the round deepens from profiler evidence into IR-backed attribution.- In that flow, use
triton-npu-analyze-iras the IR evidence companion for capture, navigation, and stage-level inspection. - Use the sibling
triton-npu-analyze-irskill when compiler lowering details, stage-to-stage IR changes, or round-local IR evidence are needed to explain benchmark behavior. - Keep IR evidence under
opt-round-N/ir/. - In optimize rounds, keep IR capture round-local. Use the
triton-npu-analyze-irskill's helpers with argument shapes such as:capture_ir.py --ir-dir opt-round-N/ir --bench-file bench_<operator>.py --operator-file opt-round-N/<optimized-operator>.py inspect_ir.py list-stages --ir-dir opt-round-N/ir --sort-by interesting --limit 20
compiler-source escalation
- Use compiler-source escalation only when compiler source analysis is enabled and after profiler and IR evidence have narrowed a concrete compiler-side question.
- Use the sibling
triton-npu-analyze-compiler-sourceskill only when the round still needs performance-focused explanation before the next operator change is clear. - When compiler source analysis is enabled, treat the compiler source checkout as read-only and use compiler-source evidence only when it is genuinely needed.
- Write
opt-round-N/compiler-analysis.md.
Kernel Semantic Repairs
- During kernel optimization work, inspect kernel compare-helper call sites such as
tl.maximum()andtl.minimum()across the operator, not only the lines touched by the primary optimization. - If a kernel compare-helper call omits
propagate_nan, you may addpropagate_nan=tl.PropagateNan.ALLas a consistency repair when the intended semantics are explicit NaN propagation. - Treat this as a semantic choice because it can change NaN-input behavior. Do not present it as a no-op cleanup.
Stage 3: Validate And Record
- Run correctness validation before trusting any performance result.
- After correctness passes, you may use the sibling
ascend-npu-run-evalskill'sprobe-benchflow as a fast baseline-vs-candidate screen when you need a cheap directional signal before paying canonical benchmark cost. - Use
probe-benchonly when the active run-eval surface actually exposes it. If the current surface does not exposeprobe-bench, skip the screen and continue with canonical validation. - Treat
probe-benchas non-canonical screening evidence only. Do not use its output as the official round perf artifact, do not write probe artifacts intoround-state.json, and do not claim round speedups from probe output alone. - Use
probe-benchto reject clearly bad candidates early or to justify keeping a clearly promising direction long enough to run canonical benchmarking. - After correctness passes, run benchmark validation and preserve the round-local benchmark evidence.
- In each round directory, keep the optimized operator snapshot as
opt_<original-operator>.py. - In each round directory, keep the benchmark artifact as
opt_<original-operator>_perf.txt, ensure that file is generated by theascend-npu-run-evalskill'srun-benchflow, and record that exact filename inround-state.json. - Use
baseline/<operator>_perf.txtfor canonical optimize-session performance comparisons, even when the current round also compares locally against its parent. - Once baseline and round perf artifacts both exist, use the
ascend-npu-run-evalskill to runcompare-perf. - Even after
probe-benchreportslikely_gainorlikely_regression, still run canonicalrun-benchandcompare-perfbefore recording any official round conclusion. - Treat the
ascend-npu-run-evalskill'scompare-perfflow as the only authority for claimed benchmark deltas and speedups, includingAvg improvement,Geomean speedup, and any claimed benchmark delta. - Record exactly one resolved comparison basis in
round-state.jsonaseffective_metric_source, usingkernel,total-op, ormixed. - In
kerneltarget mode, prefer the kernel-oriented comparison result, but ifcompare-perffalls back to total-op for some or all cases, keep the round eligible and record that fallback as a warning. - In
operatortarget mode, show both kernel and total-op comparison results so you can diagnose whether kernel improvements translated end-to-end, then recordeffective_metric_source: total-opfor the official round conclusion. - If the result is slower, inconclusive, or not worth promoting, close the round and carry the next optimization idea into a new round instead of revising the current round again.
- If correctness repair is no longer worthwhile, canonical benchmark execution keeps failing, or the time cost is no longer justified, close the round as a rejected terminal round instead of exiting silently. Preserve the minimum round artifacts, write
correctness_statusandbenchmark_statushonestly, and still usesubmit-roundso the workflow records the round outcome. - When
benchmark_statusispassed,round-state.jsonmust include the benchmark artifact metadata fields. Whenbenchmark_statusisfailedornot_run, those benchmark metadata fields may be omitted if no canonical perf artifact was produced. - Do not hand-calculate speedups or percentage improvements from raw perf files.
- Use the sibling
ascend-npu-optimize-stateskill'ssubmit-roundsubcommand to submit the current round and repair the round until it passes before continuing or stopping. - After the round submission passes, read the JSON
guidelinefield for the exit signal and follow its stop-or-continue instruction for the current session. - Before opening the next round, use the sibling
ascend-npu-optimize-stateskill'sstart-roundsubcommand to re-check the one-round-at-a-time and no-blind-sweep workflow constraints.
Round Records
attempts.md: chronological round log for the current round, including the script-writtenState Updatehistory,Primary analysis level,Supporting evidence, the starting hypothesis, selected pattern candidates and pivots when pattern triage is used, escalation reasons, meaningful code changes, correctness failures, probe-screening outcomes whenprobe-benchis used, and canonical benchmark outcomes.summary.md: round conclusion, optimization points that mattered, the final selected pattern direction when one guided the round, the final analysis level, supporting evidence that decided the round, and unresolved questions if deeper analysis may still be needed. Do not duplicate the full round strategy state history here.opt-note.md: top-level round ledger plus final## Overall Summary.
Learned Lessons
Maintain learned_lessons.md in the operator workspace as a strict reusable optimization-knowledge distillation log.
Admission criteria:
Append a lesson only when it passes all admission criteria:
- The lesson generalizes to a family of Triton Ascend NPU operators, not only the current operator.
- The lesson is supported by correctness, benchmark, profiler, IR, or compiler-error evidence.
- The lesson is written as a reusable rule, diagnostic mapping, or optimization heuristic.
- The lesson states where it applies or what limits it.
- The lesson could plausibly be promoted into an optimize skill, profiling analysis reference, IR analysis reference, or pattern reference.
Use learned_lessons.md for concise distilled rules such as:
- profile-to-optimization mappings
- IR-to-code-change mappings
- compiler error repairs that reveal recurring Triton or Ascend NPU constraints
- new optimization points inferred from recurring Triton code patterns
- validated benchmark interpretation rules that would help future rounds start faster
Do not use learned_lessons.md for round narrative, local command failures, failed guesses, temporary troubleshooting notes, file names, shape-specific details, or summaries of what happened in one round.
Put round-local narrative, temporary troubleshooting notes, command failures, and shape-specific details in opt-round-N/attempts.md, opt-round-N/summary.md, or opt-note.md instead.
Hard Rules
- Optimize the operator file itself, not the generated tests or benchmark harness.
- Optimize the Triton kernel path, not just the public wrapper surface.
- Do not replace the core computation with pure PyTorch just to improve benchmark numbers.
- Do not claim success for a round without both correctness evidence and benchmark evidence.
- Do not begin with blind tiling, autotune, or launch-parameter search when the available evidence does not justify that direction.
- When launch or tile/block configuration tuning is justified, prefer
autotuneover burning multiple optimization rounds on hand-tuned parameter sweeps. - Do not finish a round by restoring the parent snapshot alone; if edits are discarded, restart from the last validated parent as a new attempt or round instead of claiming rollback as the delivered optimization.
- Do not spend optimization rounds tuning
num_warpsfor Ascend NPU targets; it is CUDA-oriented and is not a meaningful Ascend launch knob. - When an operator file defines multiple Triton kernels on a relevant hot path, fuse them into a single kernel first, then optimize that fused kernel rather than tuning separate kernels in isolation.
- Do not write forward-looking optimization plans or predict what to try next. After each round, the next optimization direction must be driven by fresh profiling or benchmark evidence from the changed code. Write no more than one round at a time.
04 同仓库更多 Skills
triton-npu-repair-guide
Heuristic fixes for Ascend Triton compile/JIT/kernel errors and numerical precision mismatches when editing or converting the operator. Does not replace ascend-npu-gen-test, ascend-npu-gen-bench, or harness specs.
triton-npu-optimize-knowledge
Generic reference-only optimize knowledge for pattern triage and evidence-backed symptom routing. This skill does not define optimize workflow or own round artifacts.
triton-npu-optimize-knowledge-v3
Generic reference-only optimize knowledge (v3 working copy) for pattern triage and evidence-backed symptom routing. This skill does not define optimize workflow or own round artifacts; prefer this tree for ongoing knowledge updates.
triton-npu-optimize-knowledge
Generic reference-only optimize knowledge for pattern triage and evidence-backed symptom routing. This skill does not define optimize workflow or own round artifacts.
triton-npu-convert-pytorch-operator
Convert one PyTorch operator into a Triton NPU-backed PyTorch operator, preserve the trailing input-helper block, and validate the converted output through standalone or differential testing.
triton-npu-cann-ext-api-patterns
A5-only specialized optimization pattern references for Triton Ascend NPU kernels that use CANN Triton extension APIs. This skill does not define the optimize workflow; it only provides pattern material for optimize to consume.