opencl-optimize

Optimizes MNN OpenCL backend kernels through benchmarking, iterative tuning, and on-device validation.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Optimizing GPU kernels in the MNN OpenCL backend is error-prone: edited .cl files silently fail without codegen, dispatch routing sends shapes to unexpected kernels, and performance numbers on mobile GPUs are distorted by cold starts and thermal drift. This Skill provides a structured workflow covering baseline benchmarking, kernel-level and operator-level optimization, correctness oracles, and real-device verification on Android.

Core Features & Use Cases

  • Guided optimization workflows: Three directions—model-level profiling to find bottlenecks, targeted operator optimization, and new OpenCL feature/extension integration with mandatory fallback paths.
  • Optimization handbook: 11 proven kernel/memory-level techniques (local memory reduction, image1d_buffer texture cache, register tiling, vectorization) plus 16 documented pitfalls with measured speedups.
  • Rigorous validation: Three-layer correctness oracles (CPU numeric comparison, op tests, end-to-end), interleaved A/B performance measurement to defeat thermal drift, and codegen enforcement after every .cl edit.
  • Use Case: An engineer needs to speed up int4 GEMM prefill on an Adreno GPU. The Skill walks them through profiling with MNN_GPU_TIME_PROFILE, identifying the register/occupancy wall, testing tile widths, and verifying results on a real Android device.

Quick Start

Ask the AI to optimize the LinearAttention operator's OpenCL kernel performance on an Android Adreno device using the MNN benchmark-then-iterate workflow.

Frequently Asked Questions about opencl-optimize

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

FAQPage Schema
How do I optimize an OpenCL kernel in the MNN framework?

Start by profiling with a build using MNN_GPU_TIME_PROFILE=ON to find the bottleneck kernel, then check the dispatcher's onResize code to confirm which kernel your target shape actually uses. Apply one optimization technique at a time, verifying correctness against a CPU baseline after each change.

Why did my .cl kernel edit have no effect in MNN?

MNN does not compile .cl files directly; the runtime reads embedded strings in generated *_mnn_cl.cpp files. After editing any .cl file you must run python3 opencl_codegen.py in the cl directory, then rebuild and confirm the new macro appears in the binary.

How do I measure GPU kernel performance accurately on Android?

Use interleaved A/B testing: keep baseline and optimized binaries, alternate base-opt-base-opt runs back to back, and discard cold-start first runs. Single-session numbers look stable but cross-session thermal drift of about 8 percent makes sequential before/after comparisons unreliable.

Does MNN OpenCL optimization require a physical Android device?

Yes, real Android devices with Adreno or Mali GPUs are required for meaningful results. Mac and desktop OpenCL behavior differs significantly from mobile GPUs and cannot represent phone performance or stability.

How do I integrate a new OpenCL extension into MNN?

Obtain runnable sample code from the user first, add runtime feature detection in OpenCLRuntime, and implement the new kernel behind an #ifdef with the original path kept as fallback. Test on both Adreno and Mali since extension behavior differs across vendors.

Why does my OpenCL unit test pass but the kernel never ran on GPU?

Operators that only support Buffer execution can silently fall back to Image mode or CPU when only backend=3 is specified, producing false passes. Force buffer mode explicitly (e.g., numThread 68 for LLMs) and confirm GPU execution via profiler output or backend logs.