pattern-dev

Develops and debugs PyTorch Inductor pattern matcher fusions for NPU compile graphs.

14|5|Updated Mar 30, 2026
One-click install
npx skills add https://github.com/Ascend/MindIE-SD --skill pattern-dev-ascend
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: pattern-dev
Source: https://github.com/Ascend/MindIE-SD/tree/main/.agents/skills/pattern-dev
Command: npx skills add https://github.com/Ascend/MindIE-SD --skill pattern-dev-ascend

SYSTEM DOCUMENTATION & REQUIREMENTS

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

What problem does it solve? Writing graph fusion patterns for PyTorch Inductor's pattern matcher often fails silently: patterns register and pass unit tests but never match the real compiled model graph, or they match yet deliver no wall-clock benefit. This Skill provides a disciplined Phase 1-7 lifecycle for creating, registering, debugging, and validating fusion patterns on torch + torch_npu NPU environments. ## Core Features & Use Cases - Pattern lifecycle management: Covers model analysis, pattern creation via register_replacement or GraphPatternEntry manual graph rewriting, three-file registration, unit verification, mismatch debugging, integration validation, and Copy kernel elimination. - Mismatch diagnosis: A catalog of 7 real mismatch types (decomposition differences, placeholder vs get_attr, scalar literal drift) plus graph dump comparison methods and real-graph probe scripts. - Benefit root-cause analysis: Diagnoses hit-but-no-benefit cases (dtype promotion, input materialization, format conversion, fallback mismatches, inefficient custom kernels) with kernel diff tooling. - Use Case: When a user says "my compile fusion is not hitting" or "InplaceCopy kernels doubled after compile", this Skill walks them through dumping the real FX graph, aligning nodes against the pattern, and confirming fusion via kernel_details.csv counts. ## Quick Start Ask the assistant to help write or debug an Inductor fusion pattern for a model, for example to create an RMSNorm fusion pattern and verify it actually hits in the compiled graph.

Frequently Asked Questions about pattern-dev

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

FAQPage Schema
How do I write a PyTorch Inductor fusion pattern that actually matches?

Extract the target op's full implementation from the model source, check whether weights appear as placeholder or get_attr in the real graph, then use register_replacement with weights as pattern inputs. Verify hits via kernel_details.csv counts, not just unit tests.

Why does my pattern pass unit tests but not match the full model graph?

Unit tests share code with the pattern so they always match. Dump the real compiled FX graph, align nodes one by one against the pattern, and check the 7 mismatch types such as decomposition differences or placeholder versus get_attr parameter sources.

When should I use GraphPatternEntry instead of register_replacement?

Use GraphPatternEntry with a manual rewrite handler when the pattern contains dynamic shape nodes like view calls whose sizes vary with batch or sequence length. Trace-based patterns freeze those constants and never match the real graph.

Why do InplaceCopy kernels increase after torch.compile on NPU?

The aot_autograd functionalization converts in-place view and reshape ops into _to_copy nodes, which Inductor lowers to InplaceCopy kernels. Detect them in kernel_details.csv and reduce them by fixing pattern hits, using mixed eager mode, or graph capture for static shapes.

Why does my fusion pattern hit but give no speedup?

A hit does not guarantee benefit. Check for new auxiliary kernels like dtype casts, input materialization copies, or format conversions around the fused kernel, and compare fused kernel time against the replaced kernel chain using a kernel diff.