model-infer-prefetch

Adds torch_npu.npu_prefetch weight prefetching to NPU models to overlap memory-bound MatMul weight transfers with computation.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires torch_npu.

What problem does it solve? Memory-bound operators like MatMul, QBMM, and GMM stall NPU inference while waiting for weight data transfers. This Skill guides the end-to-end process of adding torch_npu.npu_prefetch weight prefetching so weight loading overlaps with preceding computation, reducing operator latency on Ascend NPU hardware. ## Core Features & Use Cases - Prefetch Position Selection: Identify candidate memory-bound operators via manual specification or automated roofline analysis, and choose safe non-memory-bound dependency windows. - Prefetch Size Calculation: Compute max_size from weight tensor dimensions and data type (bf16/int8/fp32), starting conservatively at 50% and tuning based on profiling results. - Guarded Implementation: Insert prefetch calls behind an enable_prefetch switch (default False) with graph-mode-compatible dependency nodes, then validate with before/after profiling comparisons. - Use Case: Profiling shows gate_up_proj MatMul is memory-bound in an LLM. Use this Skill to prefetch its weights during the preceding o_proj execution, verify the operator time drops, and produce a performance comparison report. ## Quick Start Analyze my model's profiling data and add weight prefetching for the memory-bound MatMul operators with an enable_prefetch switch.

Frequently Asked Questions about model-infer-prefetch

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

FAQPage Schema
How do I add weight prefetching to an NPU model?

First confirm memory-bound hotspots via profiling, then select target operators and non-memory-bound dependency windows, compute max_size from weight dimensions and dtype, insert torch_npu.npu_prefetch calls behind an enable_prefetch switch, and validate with before/after profiling.

Which operators benefit from npu_prefetch optimization?

Memory-bound weight-heavy operators like MatMul, BatchMatMul, QBMM, and GMM benefit most. Compute-bound operators gain nothing, and small operators like LayerNorm, ROPE, or SwiGLU serve as dependency windows rather than prefetch targets.

How do I choose the prefetch dependency window?

Pick a preceding operator on the same logical path that is not memory-bound and has enough execution time to cover the weight transfer. Large MatMul outputs can serve as windows since bandwidth frees up after they complete; avoid communication or heavy data-movement operators.

How should I set the npu_prefetch max_size parameter?

Compute max_size as weight dimension product times dtype bytes (2 for bf16, 1 for int8), then start at 50% of the theoretical value. Increase toward 70-100% if the target speeds up, or decrease if the dependency window degrades from bandwidth contention.

Why does prefetching fail or slow down my model in graph mode?

Graph mode requires the dependency tensor to be on the same logical path as the target operator; cross-branch dependencies cause compile failures or merely shift waiting. Oversized max_size also steals bandwidth from the dependency window, so reduce it or reposition the prefetch.

When should I not use weight prefetching?

Skip prefetching when profiling shows no memory-bound bottleneck, when all candidate dependency windows are communication or heavy data-movement operators, or when the preceding path offers no safe bandwidth window. Forcing prefetch in these cases adds contention without benefit.