技能创建器
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 快照 · 自动分类 · 以原作者文档为准
Identify GPU kernel = compute-bound, memory-bound, latency-bound. Baseline perf → roofline classify → occupancy + compute/load ratio/tile → SASS instr mix + stall codes → smem cliff → decision matrix → right opt strategy.
.cubin or .cu + build cmd)Run kernel w/ CUDA events (BenchTimer), record ms. Calc effective throughput:
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→ Baseline: kernel ms, effective GFLOPS, effective BW.
If err: Check launches no err (CHECK_CU). Warmup pre-measurement. Dims large enough saturate GPU (small → launch overhead bottleneck).
Arithmetic intensity vs machine balance → classify:
AI = FLOPs / bytes_loaded_from_global_memory. Count only unique bytes from DRAM (not shared mem or register reuse).balance = peak_compute / peak_bandwidth.AI < balance → memory-bound. AI > balance → compute-bound.GA104 (RTX 3070 Ti) Reference:
| 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 |
Derived Balance Points:
| 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. Memory-bound → compare effective BW to 608 GB/s. Compute-bound → compare effective GFLOPS to relevant peak.→ Classification: compute-bound, memory-bound, latency-bound (low occupancy → neither saturated) + numerical justification.
If err: Recheck byte counting. Watch redundant re-reads (e.g., 9x in direct conv2d no im2col). Neither saturated → latency-bound (Step 3).
Active warps/SM from launch config + resource usage:
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) → see Step 6 clifffloor(48 / warps_per_block) (GA104 max: 48 warps/SM)min(register_limit, smem_limit, warp_limit, block_limit).blocks_per_SM * warps_per_block.→ Occupancy table: blocks/SM, active warps/SM, limiting factor (registers, smem, warps).
If err: Check cuFuncSetAttribute for dynamic smem. Verify --resource-usage matches actual launch config. High register → --maxrregcount=N (trade spills for occupancy).
Count compute instrs + load bytes/K-tile from SASS (not src):
cuobjdump -sass kernel.sm_86.cubin > kernel.sass
grep -c 'HMMA' kernel.sass → FP16 TC opsgrep -c 'IMMA' kernel.sass → INT8 TC opsgrep -c 'FFMA' kernel.sass → FP32 FMAgrep -c 'LDG' kernel.sass → global mem loadscompute_ops / load_ops per tile.→ Compute/load ratio + classification (high/medium/low) + cp.async rec.
If err: Count from SASS not src — compiler may fuse, eliminate, reorder. Inner loop only (K-tile iter) not entire kernel.
Full SASS instr mix + stall codes:
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)
→ Instr count table + stall code summary + ID'd opt targets.
If err: cuobjdump arch matches kernel compile target (both sm_86). SASS out empty → cubin corrupt → recompile.
Smem usage crosses arch-specific occupancy cliff?
--resource-usage (Step 3) or cuobjdump --res-usage kernel.sm_86.cubin.→ Smem/block + blocks/SM + explicit statement cliff crossed.
If err: Above cliff + occupancy bottleneck → change strategy: reduce tile → smem <50 KB, or accept 1 block/SM + compensate higher compute/load ratio (more register reuse, longer K-tiles).
Synthesize Steps 2-6 → opt strategy:
| Condition | Strategy |
|---|---|
| Memory-bound + low compute/load (<5:1) + smem under cliff | SW pipelining cp.async (LDGSTS). Overlap global loads w/ compute. |
| Memory-bound + high compute/load (>20:1) + 8+ warps | Warp interleaving already hides. Focus algorithmic: implicit GEMM, split-Q, im2col. |
| Compute-bound + FFMA-heavy | CuAssembler stall tighten: S04 → S01 on independent FFMAs. |
| Compute-bound + HMMA-heavy | S08 hardware min, no reduce. Increase tile reuse (larger M/N, longer K-loop). |
| Compute-bound + IMMA-heavy | CuAssembler: S04 → S02 on IMMA (compiler conservative). |
| Latency-bound (low occupancy) | Reduce smem/registers → more blocks/SM. >8 warps/SM. |
| Smem above cliff | Reduce tile or restructure → smem/block <50 KB (GA104). |
→ Ranked list recommended opts + predicted gain + conflicts.
If err: No clear winner → micro-benchmarks isolate each (cp.async alone, reduced tile alone) → measure actual pre-combine.
Structured bottleneck report:
## 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
→ Complete MD report consumable by kernel-optimizer agent or dev.
If err: Re-run different sizes (1024, 2048, 4096, 8192) → confirm not size-specific. Small may appear latency-bound when real bottleneck at scale is BW.
cuobjdump -sass.pipeline-gpu-kernel — impl SW pipelining cp.async when memory-bound + low compute/loadsimulate-cpu-architecture — complementary arch analysis CPU-side bottlenecks in host-device workflows