metal-optimize

Guides development and optimization of MNN Metal backend kernels for LLM inference.

16.0k|2.4k|Updated Apr 15, 2019
One-click install
npx skills add https://github.com/alibaba/MNN --skill metal-optimize
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: metal-optimize
Source: https://github.com/alibaba/MNN/tree/main/skills/metal-optimize
Command: npx skills add https://github.com/alibaba/MNN --skill metal-optimize

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Developing and optimizing Metal GPU kernels in the MNN inference engine involves many silent-failure traps: stale build artifacts, pipeline cache hits, macro alias conflicts, memory aliasing in fused dispatches, and misleading profiling data. This Skill routes engineers to the right sub-document so kernel changes, operator fusion, and runtime scheduling work are done correctly and verified with proper A/B methodology.

Core Features & Use Cases

  • Kernel development standards: Naming conventions, shader string organization, preprocessor macro variants with four-point synchronization, Execution skeleton registration, and six common pitfalls (macro aliasing, dispatcher gaps, fp16 scratch buffer sizing).
  • Optimization knowledge base: Documented GEMV, GEMM, and Attention optimizations with measured data (deferred dequant +28%, SPLIT_K_2, causal-tri dispatch, flash-attention routing) across M3/M4/M5 Apple GPUs.
  • Operator fusion pipeline: Full chain from Python export declarations through converter passes to Metal single-dispatch leader/follower assembly, including memory aliasing checks and troubleshooting checklists.
  • Runtime scheduling and verification: Fence, content-cache, H2D upload, encode replay guidance, plus a mandatory build/test/correctness matrix and an environment variable registry.
  • Use Case: When adding a new quantized GEMV kernel for Qwen model decode on Apple Silicon, consult the kernel-dev sub-doc for naming and macro sync rules, then follow the build-and-test sub-doc's forced recompile and greedy byte-identical validation flow before trusting any performance numbers.

Quick Start

Ask the assistant to help you add or optimize a Metal kernel in the MNN backend, describing your target model, quantization bit width, and whether the workload is prefill or decode.

Frequently Asked Questions about metal-optimize

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

FAQPage Schema
How do I add a new Metal kernel to the MNN backend?

Write the kernel as a C++ string literal in a *Shader.hpp file, create an Execution class implementing onResize and onEncode, and register it with REGISTER_METAL_OP_CREATOR. Follow the naming convention of operator_stage_variant_suffix and synchronize any new macro across shader, pipeline cache key, macro dictionary, and grid computation.

How do I optimize LLM decode performance on Apple Silicon with Metal?

Focus on GEMV kernels which take about 70% of decode time. Documented techniques include deferred dequantization, split-K simdgroup pairing, and operator fusion of QKV and gate/up projections. Always verify with paired A/B runs since thermal drift can fake large gains.

Why does my Metal shader change have no effect after rebuilding?

Incremental make may not relink libMNN.dylib after editing shader strings in headers, and the Metal pipeline binary cache can load stale binaries. Touch the modified header, rebuild with make -B, delete mnn_cachefile.bin, and assert new symbols exist in the dylib before measuring.

Does MNN Metal support non-causal attention models like sliding window?

Yes, since the causal detection became data-driven: models exporting a real mask tensor automatically disable causal-tri, causal-bound, and flash-attention paths. Standard causal models like Qwen and Llama use a scalar sentinel mask and keep all optimizations enabled.

Why is my fused operator output different across runs in MNN Metal?

Non-deterministic fused output usually indicates memory aliasing: a predecessor input was reused by the allocator as a successor output within one dispatch. The fix is alias detection with tensorsOverlap plus re-homing outputs to STATIC memory, or skipping fusion when re-homing fails.

Can I trust profiling numbers from the MNN Metal profile build?

No, profile build absolute numbers are artifacts because counter sample buffers inflate CPU encode cost roughly 20x and create fake GPU idle gaps. Use profile builds only for relative proportions, and validate any optimization with production-build paired A/B runs.