aprender-cgp 0.31.2

Compute-GPU-Profile: Unified performance analysis CLI for scalar, SIMD, wgpu, and CUDA workloads
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
use anyhow::Result;
use cgp::{analysis, doctor, profilers};
use clap::{Parser, Subcommand};

/// CGP: Compute-GPU-Profile — Unified Performance Analysis CLI
///
/// Own the Stack: One Binary, All Backends, Zero Blind Spots.
/// Profiles scalar, SIMD (SSE2/AVX2/AVX-512/NEON/WASM SIMD128),
/// wgpu (Vulkan/Metal/DX12/WebGPU), and CUDA workloads.
#[derive(Parser)]
#[command(name = "cgp", version, about, long_about = None)]
struct Cli {
    /// Output JSON instead of human-readable text
    #[arg(long, global = true)]
    json: bool,

    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Profile a kernel or function (runtime execution)
    Profile {
        #[command(subcommand)]
        target: ProfileTarget,
    },
    /// Enhanced criterion benchmarking with hardware counters
    Bench {
        /// Benchmark name
        #[arg(long)]
        bench: String,
        /// Hardware counters to collect (comma-separated)
        #[arg(long)]
        counters: Option<String>,
        /// Check regression against saved baseline
        #[arg(long)]
        check_regression: bool,
        /// Regression threshold percentage
        #[arg(long, default_value = "5")]
        threshold: f64,
        /// Overlay roofline model
        #[arg(long)]
        roofline: bool,
    },
    /// Generate roofline model for target hardware
    Roofline {
        /// Target backend (cuda, avx2, avx512, neon, wgpu)
        #[arg(long)]
        target: String,
        /// Kernels to plot on roofline
        #[arg(long)]
        kernels: Option<String>,
        /// Export to file
        #[arg(long)]
        export: Option<String>,
        /// Use empirical measurement instead of spec values
        #[arg(long)]
        empirical: bool,
    },
    /// Compare two profiles (git integration)
    Diff {
        /// Baseline commit or profile path
        #[arg(long)]
        baseline: Option<String>,
        /// Current commit or profile path
        #[arg(long)]
        current: Option<String>,
        /// Before commit
        #[arg(long)]
        before: Option<String>,
        /// After commit
        #[arg(long)]
        after: Option<String>,
    },
    /// Verify performance contracts (CI/CD gate)
    Contract {
        #[command(subcommand)]
        action: ContractAction,
    },
    /// System-wide timeline (wraps nsys)
    Trace {
        /// Binary to trace
        binary: String,
        /// Trace duration
        #[arg(long)]
        duration: Option<String>,
    },
    /// Static code analysis (wraps trueno-explain)
    Explain {
        /// Analysis target (ptx, simd, wgsl)
        target: String,
        /// Kernel name
        #[arg(long)]
        kernel: Option<String>,
    },
    /// Interactive TUI exploration mode
    Tui,
    /// Save/load performance baselines
    Baseline {
        /// Save current profile as baseline
        #[arg(long)]
        save: Option<String>,
        /// Load baseline from file
        #[arg(long)]
        load: Option<String>,
    },
    /// Check tool availability and hardware capabilities
    Doctor,
    /// Head-to-head competitor comparison
    Compete {
        /// Workload name (e.g., gemm)
        workload: String,
        /// Our command
        #[arg(long)]
        ours: String,
        /// Competitor commands (can be repeated)
        #[arg(long)]
        theirs: Vec<String>,
        /// Labels for each entry (comma-separated)
        #[arg(long)]
        label: Option<String>,
    },
}

#[derive(Subcommand)]
enum ProfileTarget {
    /// Profile a CUDA PTX kernel via ncu + CUPTI
    Kernel {
        /// Kernel name
        #[arg(long)]
        name: String,
        /// Problem size (e.g., 512 for square matrix)
        #[arg(long)]
        size: u32,
        /// Generate roofline overlay
        #[arg(long)]
        roofline: bool,
        /// Specific ncu metrics to collect
        #[arg(long)]
        metrics: Option<String>,
    },
    /// Profile cuBLAS/cuBLASLt operations
    Cublas {
        /// Operation (gemm_f16, gemm_f32, etc.)
        #[arg(long)]
        op: String,
        /// Problem size
        #[arg(long)]
        size: u32,
    },
    /// Profile wgpu compute shaders
    Wgpu {
        /// WGSL shader path
        #[arg(long)]
        shader: String,
        /// Dispatch dimensions (e.g., 256,256,1)
        #[arg(long)]
        dispatch: Option<String>,
        /// Target (native or web)
        #[arg(long)]
        target: Option<String>,
    },
    /// Profile Apple Metal compute kernels
    Metal {
        /// Metal shader name
        #[arg(long)]
        shader: String,
        /// Dispatch size
        #[arg(long)]
        dispatch: Option<u32>,
    },
    /// Profile CPU SIMD functions
    Simd {
        /// Function name
        #[arg(long)]
        function: String,
        /// Problem size
        #[arg(long)]
        size: u32,
        /// Target architecture (avx2, avx512, neon, sse2)
        #[arg(long)]
        arch: String,
    },
    /// Profile WASM SIMD128 via wasmtime
    Wasm {
        /// Function name
        #[arg(long)]
        function: String,
        /// Problem size
        #[arg(long)]
        size: u32,
    },
    /// Profile quantized CPU kernels (Q4K/Q6K)
    Quant {
        /// Kernel name (q4k_gemv, q6k_gemv, q5k_gemv, q8_gemv, nf4_gemv)
        #[arg(long, required_unless_present = "all")]
        kernel: Option<String>,
        /// Dimensions (MxNxK format)
        #[arg(long, required_unless_present = "all")]
        size: Option<String>,
        /// Profile all standard LLM layer sizes (ffn_up, ffn_down, attn_qkv, generic_4K)
        #[arg(long)]
        all: bool,
    },
    /// Profile scalar baseline
    Scalar {
        /// Function name
        #[arg(long)]
        function: String,
        /// Problem size
        #[arg(long)]
        size: u32,
    },
    /// Profile Rayon parallel workloads
    Parallel {
        /// Function name
        #[arg(long)]
        function: String,
        /// Problem size
        #[arg(long)]
        size: u32,
        /// Thread count (or "auto")
        #[arg(long)]
        threads: Option<String>,
    },
    /// Cross-backend comparison
    Compare {
        /// Kernel name
        #[arg(long)]
        kernel: String,
        /// Problem size
        #[arg(long)]
        size: u32,
        /// Backends to compare (comma-separated)
        #[arg(long)]
        backends: String,
    },
    /// Parallel scaling sweep (thread count vs throughput)
    Scaling {
        /// Problem size
        #[arg(long)]
        size: u32,
        /// Max threads to test (default: num_cpus)
        #[arg(long)]
        max_threads: Option<usize>,
        /// Runs per thread count for min-of-N timing
        #[arg(long, default_value = "3")]
        runs: usize,
    },
    /// Profile an arbitrary binary
    Binary {
        /// Binary path
        path: String,
        /// Kernel name filter
        #[arg(long)]
        kernel_filter: Option<String>,
        /// Enable system trace
        #[arg(long)]
        trace: bool,
        /// Trace duration
        #[arg(long)]
        duration: Option<String>,
    },
    /// Profile a Python script
    Python {
        /// Arguments after --
        #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
        args: Vec<String>,
    },
    /// Profile a shared library function
    Library {
        /// Path to .so file
        #[arg(long)]
        so: String,
        /// Symbol name
        #[arg(long)]
        symbol: String,
        /// Arguments (key=value pairs)
        #[arg(long)]
        args: Option<String>,
    },
}

#[derive(Subcommand)]
enum ContractAction {
    /// Verify performance contracts
    Verify {
        /// Directory containing contract YAML files
        #[arg(long)]
        contracts_dir: Option<String>,
        /// Specific contract file
        #[arg(long)]
        contract: Option<String>,
        /// Fail on any regression
        #[arg(long)]
        fail_on_regression: bool,
        /// Verify cgp's own contracts
        #[arg(long, name = "self")]
        self_verify: bool,
    },
    /// Generate contract from current measurement
    Generate {
        /// Kernel name
        #[arg(long)]
        kernel: String,
        /// Problem size
        #[arg(long)]
        size: u32,
        /// Regression tolerance percentage
        #[arg(long, default_value = "10")]
        tolerance: f64,
    },
}

fn main() -> Result<()> {
    let cli = Cli::parse();
    let json = cli.json;

    match cli.command {
        Commands::Doctor => doctor::run_doctor(json),
        Commands::Profile { target } => dispatch_profile(target, json),
        Commands::Roofline {
            target,
            kernels,
            export,
            empirical,
        } => analysis::roofline::run_roofline(
            &target,
            kernels.as_deref(),
            export.as_deref(),
            empirical,
            json,
        ),
        Commands::Bench {
            bench,
            counters,
            check_regression,
            threshold,
            roofline,
        } => analysis::bench::run_bench(
            &bench,
            counters.as_deref(),
            check_regression,
            threshold,
            roofline,
        ),
        Commands::Diff {
            baseline,
            current,
            before,
            after,
        } => analysis::diff::run_diff(
            baseline.as_deref(),
            current.as_deref(),
            before.as_deref(),
            after.as_deref(),
            json,
        ),
        Commands::Contract { action } => dispatch_contract(action),
        Commands::Trace { binary, duration } => {
            profilers::cuda::run_trace(&binary, duration.as_deref())
        }
        Commands::Explain { target, kernel } => {
            analysis::explain::run_explain(&target, kernel.as_deref())
        }
        Commands::Tui => {
            println!("cgp tui: interactive mode (requires presentar)");
            println!("  (Not yet implemented — use stdout commands for now)");
            Ok(())
        }
        Commands::Baseline { save, load } => {
            analysis::baseline::run_baseline(save.as_deref(), load.as_deref())
        }
        Commands::Compete {
            workload,
            ours,
            theirs,
            label,
        } => analysis::compete::run_compete(&workload, &ours, &theirs, label.as_deref(), json),
    }
}

fn dispatch_profile(target: ProfileTarget, json: bool) -> Result<()> {
    match target {
        ProfileTarget::Kernel {
            name,
            size,
            roofline,
            metrics,
        } => profilers::cuda::profile_kernel(&name, size, roofline, metrics.as_deref()),
        ProfileTarget::Cublas { op, size } => profilers::cuda::profile_cublas(&op, size),
        ProfileTarget::Wgpu {
            shader,
            dispatch,
            target,
        } => {
            profilers::wgpu_profiler::profile_wgpu(&shader, dispatch.as_deref(), target.as_deref())
        }
        ProfileTarget::Metal { shader, dispatch } => {
            #[cfg(target_os = "macos")]
            {
                println!("cgp profile metal: shader={shader} dispatch={dispatch:?}");
                Ok(())
            }
            #[cfg(not(target_os = "macos"))]
            {
                let _ = (&shader, dispatch);
                anyhow::bail!("Metal backend requires macOS -- use --backend wgpu for Vulkan")
            }
        }
        ProfileTarget::Simd {
            function,
            size,
            arch,
        } => profilers::simd::profile_simd(&function, size, &arch),
        ProfileTarget::Wasm { function, size } => profilers::wasm::profile_wasm(&function, size),
        ProfileTarget::Quant { kernel, size, all } => {
            if all {
                profilers::quant::profile_quant_all()
            } else {
                profilers::quant::profile_quant(
                    kernel.as_deref().unwrap_or("q4k_gemv"),
                    size.as_deref().unwrap_or("4096x1x4096"),
                )
            }
        }
        ProfileTarget::Scalar { function, size } => {
            profilers::scalar::profile_scalar(&function, size)
        }
        ProfileTarget::Parallel {
            function,
            size,
            threads,
        } => profilers::rayon_parallel::profile_parallel(&function, size, threads.as_deref()),
        ProfileTarget::Compare {
            kernel,
            size,
            backends,
        } => analysis::compare::run_compare(&kernel, size, &backends, json),
        ProfileTarget::Scaling {
            size,
            max_threads,
            runs,
        } => profilers::rayon_parallel::profile_scaling(size, max_threads, runs, json),
        ProfileTarget::Binary {
            path,
            kernel_filter,
            trace,
            duration,
        } => profilers::cuda::profile_binary(
            &path,
            kernel_filter.as_deref(),
            trace,
            duration.as_deref(),
        ),
        ProfileTarget::Python { args } => profilers::cuda::profile_python(&args),
        ProfileTarget::Library { so, symbol, args } => {
            println!("cgp profile library: {so}::{symbol} args={args:?}");
            Ok(())
        }
    }
}

fn dispatch_contract(action: ContractAction) -> Result<()> {
    match action {
        ContractAction::Verify {
            contracts_dir,
            contract,
            fail_on_regression,
            self_verify,
        } => analysis::contracts::run_verify(
            contracts_dir.as_deref(),
            contract.as_deref(),
            self_verify,
            fail_on_regression,
        ),
        ContractAction::Generate {
            kernel,
            size,
            tolerance,
        } => analysis::contracts::run_generate(&kernel, size, tolerance),
    }
}