技能创建器
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 快照 · 自动分类 · 以原作者文档为准
藉量基線效能、於 roofline 分類、計算每瓦片之佔用率與計算/載入比、檢查 SASS 指令組合與停滯碼、檢查共享記憶體懸崖,並施決策矩陣以擇優化策略,系統化辨 GPU 核心為計算受限、記憶體受限或延遲受限。
.cubin 或 .cu 源附建構命令)以 CUDA event(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預期: 基線數字:核心時於 ms、有效 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 致 9 倍重讀)。若計算與記憶體皆未飽和,核心恐為延遲受限(見步驟三)。
由啟動配置與資源使用判定每 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 最大: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 Core opsgrep -c 'IMMA' kernel.sass — INT8 Tensor Core opsgrep -c 'FFMA' kernel.sass — FP32 fused multiply-addgrep -c 'LDG' kernel.sass — global memory loadscompute_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/block 值附 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.async 實作軟體管線simulate-cpu-architecture — 主機-裝置工作流中,CPU 端瓶頸之互補架構分析