技能创建器
skill-creator
创建、改进和评估自定义技能,把专业流程转化为可重复使用的 AI 能力。
analyze-kernel-bottleneck【数据与研究】Systematically identify whether a GPU kernel is compute-bound, memory-bound, or latency-bound using roofline analysis, occupancy calculations, compute/load ratio per tile, and SASS instruction inspection. Produces a decision matrix for optimization strategy selection (cp.async, warp interleaving, tiling, double-buffering, or CuAssembler hand-tuning).
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
定 GPU 核為算限、記限、或延限:roofline、佔、計/載比、SASS 察。
.cubin 或 .cu 附建命)行核以 CUDA 事(BenchTimer),記時毫。算實效流:
nvcc --cubin -arch=sm_86 -O2 -o kernel.sm_86.cubin kernel.cu
nvcc -arch=sm_86 -O2 -o bench bench.cu -lcuda -I../../phase2/common
./bench 4096 4096 4096
effective_gflops = (2 * M * N * K) / (time_ms / 1000) / 1e9effective_bw = total_bytes / (time_ms / 1000) / 1e9effective_gflops = (4 * batch * heads * seq_len^2 * head_dim) / (time_ms / 1000) / 1e9得:基數:核時毫、實 GFLOPS、實帶寬。
敗:察核啟無誤(CHECK_CU 巨)。驗熱身先。題維足以飽 GPU(小題或瓶於啟頭)。
算算強而比機衡:
AI = FLOPs / bytes_loaded_from_global_memory。唯計獨自 DRAM 載字(非共記或暫器重用)。balance = peak_compute / peak_bandwidth。AI < balance 為記限。AI > balance 為算限。GA104 (RTX 3070 Ti) 參值:
| Resource | Peak | Unit |
|---|---|---|
| FP32 FFMA | 21.7 | TFLOPS |
| FP16 Tensor Core (HMMA) | 174 | TFLOPS |
| INT8 Tensor Core (IMMA) | 696 | TOPS |
| DRAM Bandwidth | 608 | GB/s |
| L2 Cache | 4 | MB |
| SMs | 48 |
衡點:
| Precision | Balance Point (FLOP/byte) |
|---|---|
| FP32 FFMA | 21700 / 608 = 35.7 |
| FP16 TC | 174000 / 608 = 286.2 |
| INT8 TC | 696000 / 608 = 1144.7 |
attained = effective_throughput / peak_throughput。記限:比實帶於 608 GB/s。算限:比實 GFLOPS 於關峰。得:分為算限、記限、或延限(低佔致兩皆未飽)附數由。
敗:重核位元數。察重讀(如直 conv2d 無 im2col 之 9x)。兩皆未飽→或延限(見三)。
由啟設與資用定每 SM 活 warp:
nvcc --cubin -arch=sm_86 -O2 --resource-usage -o kernel.sm_86.cubin kernel.cu 2>&1 | grep -E 'registers|smem'
warps_per_block = threads_per_block / 32。floor(65536 / (registers_per_thread * threads_per_block))floor(available_smem_per_SM / smem_per_block)——見步六崖floor(48 / warps_per_block)(GA104 max:48 warps/SM)min(register_limit, smem_limit, warp_limit, block_limit)。blocks_per_SM * warps_per_block。得:佔表示 blocks/SM、活 warps/SM、限因(暫器、smem、或 warp)。
敗:察 cuFuncSetAttribute 為動共記。驗 --resource-usage 報配實啟設。暫器數意外高→試 --maxrregcount=N 限暫器(換溢以佔)。
由 SASS(非源)計每 K 塊計指與載字:
cuobjdump -sass kernel.sm_86.cubin > kernel.sass
grep -c 'HMMA' kernel.sass —— FP16 Tensor Coregrep -c 'IMMA' kernel.sass —— INT8 Tensor Coregrep -c 'FFMA' kernel.sass —— FP32 fused multiply-addgrep -c 'LDG' kernel.sass —— 全記載compute_ops / load_ops 每塊。得:計/載比附分(高/中/低)與 cp.async 薦。
敗:由 SASS 解非源計——編或融、消、或重序指。確唯計內環指(K 塊迭),非全核。
審全 SASS 指混與停碼:
cuobjdump -sass kernel.sm_86.cubin > kernel.sass
grep -c 'HMMA.16816' kernel.sass # FP16 Tensor Core
grep -c 'IMMA.16816' kernel.sass # INT8 Tensor Core
grep -c 'FFMA' kernel.sass # FP32 fused multiply-add
grep -c 'LDGSTS' kernel.sass # cp.async (global->shared)
grep -c 'LDG' kernel.sass # Global load
grep -c 'STS' kernel.sass # Shared store
grep -c 'LDS' kernel.sass # Shared load
grep -c 'BAR.SYNC' kernel.sass # Barrier synchronization
grep -c 'SHFL' kernel.sass # Warp shuffle (reductions)
grep -c 'MUFU' kernel.sass # Special function unit
grep 'HMMA' kernel.sass | head -5 # Expect S08 minimum (hardware constraint)
grep 'IMMA' kernel.sass | head -5 # Compiler emits S04, reducible to S02 via CuAssembler
grep 'FFMA' kernel.sass | head -5 # Check for S04 (reducible to S01 on independent FFMAs)
得:指數表與停碼要附識優標。
敗:確 cuobjdump 構配核編標(皆 sm_86)。SASS 出空→cubin 或壞,重編。
定共記用越構特佔崖否:
--resource-usage(步三)或 cuobjdump --res-usage kernel.sm_86.cubin。得:smem/塊值附 blocks/SM 數與 50 KB 崖明越否。
敗:上崖且佔為瓶→優策須變:減塊大致 smem <50 KB,或受 1 block/SM 而以高計/載比補(更暫器重用、長 K 塊)。
合二至六之發為優策:
| Condition | Strategy |
|---|---|
| Memory-bound + low compute/load ratio (<5:1) + smem under cliff | Software pipelining with cp.async (LDGSTS). Overlap global loads with compute. |
| Memory-bound + high compute/load ratio (>20:1) + 8+ warps | Warp interleaving already hides latency. Focus on algorithmic changes: implicit GEMM, split-Q, im2col. |
| Compute-bound + FFMA-heavy | CuAssembler stall code tightening: S04 -> S01 on independent FFMAs. |
| Compute-bound + HMMA-heavy | S08 is hardware minimum, cannot reduce. Increase tile reuse (larger M/N tiles, longer K-loop). |
| Compute-bound + IMMA-heavy | CuAssembler: S04 -> S02 on IMMA instructions (compiler is conservative). |
| Latency-bound (low occupancy, neither saturated) | Reduce smem or registers to get more blocks/SM. Get above 8 warps/SM. |
| Smem above cliff | Reduce tile size or restructure to get smem/block under 50 KB (GA104). |
得:薦優排列附預益範與潛衝。
敗:無明勝→行微基孤各策(如試 cp.async 獨、試減塊獨)以量真效再合。
出構瓶報:
## Bottleneck Analysis Report: [kernel_name]
### Baseline
- Problem: [dimensions]
- Kernel time: [X] ms
- Effective GFLOPS: [Y] | Effective BW: [Z] GB/s
### Roofline Classification
- Arithmetic intensity: [AI] FLOP/byte
- Balance point: [BP] FLOP/byte ([precision])
- Classification: **[compute|memory|latency]-bound**
- Attained fraction: [X]% of peak
### Occupancy
| Resource | Per Block | Limit/SM | Blocks/SM |
|----------|-----------|----------|-----------|
| Registers | [N]/thread | 65536 | [B] |
| Shared mem | [X] KB | 100 KB (cliff: 50 KB) | [B] |
| Warps | [W] | 48 | [B] |
| **Limiting** | | | **[min(B)]** |
- Active warps/SM: [W] ([sufficient|insufficient] for latency hiding)
### Compute/Load Ratio
- Compute ops/tile: [N] [HMMA|IMMA|FFMA]
- Load bytes/tile: [N] bytes ([N] LDG x [N] bytes)
- Ratio: [X]:1 — **[high|medium|low]**
- cp.async recommendation: [beneficial|neutral|detrimental]
### SASS Instruction Mix
| Instruction | Count | Notes |
|-------------|-------|-------|
| HMMA.16816 | [N] | Stall: S08 (hardware min) |
| IMMA.16816 | [N] | Stall: S04 (reducible to S02) |
| FFMA | [N] | Stall: S04 (reducible to S01) |
| LDG | [N] | |
| LDGSTS | [N] | cp.async |
| BAR.SYNC | [N] | |
### Smem Cliff
- Smem/block: [X] KB — [under|over] 50 KB cliff
- Blocks/SM: [B] — [no occupancy loss|occupancy halved]
### Recommended Optimizations (ranked)
1. [Strategy] — estimated [X-Y]% gain
2. [Strategy] — estimated [X-Y]% gain
3. [Strategy] — estimated [X-Y]% gain
得:完整 markdown 報,核優師或人開發可消。
敗:以異題大重行(如 1024、2048、4096、8192)以驗發非大專。小題或似延限而真瓶於大為記帶。
cuobjdump -sass 出計pipeline-gpu-kernel —— 析識記限低計/載比核時施軟管以 cp.asyncsimulate-cpu-architecture —— 主機側瓶之構析補