axonml-cli 0.4.2

Command-line interface for the Axonml ML framework
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
//! GPU - GPU Detection and Management
//!
//! # File
//! `crates/axonml-cli/src/commands/gpu.rs`
//!
//! # Author
//! Andrew Jewell Sr - AutomataNexus
//!
//! # Updated
//! March 8, 2026
//!
//! # Disclaimer
//! Use at own risk. This software is provided "as is", without warranty of any
//! kind, express or implied. The author and AutomataNexus shall not be held
//! liable for any damages arising from the use of this software.

use std::time::Instant;

use wgpu::{Backends, DeviceType, Instance, InstanceDescriptor, MemoryHints};

use super::utils::{print_header, print_info, print_kv, print_success, print_warning};
use crate::cli::{GpuArgs, GpuBenchArgs, GpuSelectArgs, GpuSubcommand};
use crate::error::{CliError, CliResult};

// =============================================================================
// GPU Info Structures
// =============================================================================

/// Information about a detected GPU
#[derive(Debug, Clone)]
pub struct GpuInfo {
    pub id: usize,
    pub name: String,
    pub vendor: String,
    pub device_type: String,
    pub backend: String,
    pub driver: String,
    pub memory_total: u64,
    pub is_available: bool,
}

// =============================================================================
// Execute Command
// =============================================================================

/// Execute the `gpu` command
pub fn execute(args: GpuArgs) -> CliResult<()> {
    match args.action {
        GpuSubcommand::List => execute_list(),
        GpuSubcommand::Info => execute_info(),
        GpuSubcommand::Select(select_args) => execute_select(select_args),
        GpuSubcommand::Bench(bench_args) => execute_bench(bench_args),
        GpuSubcommand::Memory => execute_memory(),
        GpuSubcommand::Status => execute_status(),
    }
}

// =============================================================================
// List Subcommand
// =============================================================================

fn execute_list() -> CliResult<()> {
    print_header("CUDA GPUs (NVIDIA)");
    println!();

    let gpus = detect_gpus()?;

    if gpus.is_empty() {
        print_warning("No NVIDIA CUDA GPUs detected.");
        println!();
        print_info("Ensure you have:");
        println!("  - NVIDIA GPU installed");
        println!("  - NVIDIA drivers with CUDA support");
        println!("  - For WSL2: GPU passthrough enabled");
        return Ok(());
    }

    println!(
        "{:<4} {:<35} {:<12} {:>12} {:>10}",
        "ID", "Name", "Backend", "Memory", "Type"
    );
    println!("{}", "-".repeat(77));

    for gpu in &gpus {
        println!(
            "{:<4} {:<35} {:<12} {:>12} {:>10}",
            gpu.id,
            truncate_string(&gpu.name, 34),
            gpu.backend,
            format_size(gpu.memory_total),
            gpu.device_type
        );
    }

    println!();
    print_kv("Total CUDA GPUs", &gpus.len().to_string());

    Ok(())
}

// =============================================================================
// Info Subcommand
// =============================================================================

fn execute_info() -> CliResult<()> {
    print_header("CUDA GPU Information");
    println!();

    // Detected CUDA devices
    let gpus = detect_gpus()?;

    if gpus.is_empty() {
        print_warning("No NVIDIA CUDA GPUs detected.");
        println!();
        print_info("Ensure you have:");
        println!("  - NVIDIA GPU installed");
        println!("  - NVIDIA drivers installed (with CUDA support)");
        println!("  - For WSL2: GPU passthrough enabled in Windows");
        return Ok(());
    }

    print_kv("CUDA Devices", &gpus.len().to_string());

    for gpu in &gpus {
        println!();
        print_header(&format!("CUDA Device {} - {}", gpu.id, gpu.name));
        print_kv("Vendor", &gpu.vendor);
        print_kv("Device Type", &gpu.device_type);
        print_kv("Backend", &gpu.backend);
        print_kv("Driver", &gpu.driver);
        print_kv("Memory", &format_size(gpu.memory_total));
        print_kv(
            "Status",
            if gpu.is_available {
                "Available"
            } else {
                "Busy"
            },
        );
    }

    Ok(())
}

// =============================================================================
// Select Subcommand
// =============================================================================

fn execute_select(args: GpuSelectArgs) -> CliResult<()> {
    print_header("GPU Selection");
    println!();

    let gpus = detect_gpus()?;

    if gpus.is_empty() {
        return Err(CliError::Gpu("No GPU devices available".to_string()));
    }

    // Parse device selection
    let device_id: usize = if args.device.to_lowercase() == "auto" {
        // Auto-select best available GPU (prefer discrete over integrated)
        gpus.iter()
            .filter(|g| g.device_type == "Discrete")
            .map(|g| g.id)
            .next()
            .unwrap_or(0)
    } else {
        args.device.parse().map_err(|_| {
            CliError::InvalidArgument(format!("Invalid device specifier: {}", args.device))
        })?
    };

    // Validate device exists
    let gpu = gpus
        .iter()
        .find(|g| g.id == device_id)
        .ok_or_else(|| CliError::Gpu(format!("GPU {device_id} not found")))?;

    // Save selection to config
    save_gpu_selection(device_id)?;

    print_success(&format!(
        "Selected GPU {}: {} ({})",
        device_id, gpu.name, gpu.backend
    ));

    if args.persistent {
        print_info("Selection saved to .axonml/gpu_config.json");
    }

    Ok(())
}

// =============================================================================
// Bench Subcommand
// =============================================================================

fn execute_bench(args: GpuBenchArgs) -> CliResult<()> {
    print_header("GPU Benchmark");
    println!();

    let gpus = detect_gpus()?;

    if gpus.is_empty() {
        return Err(CliError::Gpu(
            "No GPU devices available for benchmarking".to_string(),
        ));
    }

    // Determine which GPUs to benchmark
    let gpus_to_bench: Vec<&GpuInfo> = if args.all {
        gpus.iter().collect()
    } else if let Some(device) = &args.device {
        let id: usize = device
            .parse()
            .map_err(|_| CliError::InvalidArgument(format!("Invalid device ID: {device}")))?;
        gpus.iter().filter(|g| g.id == id).collect()
    } else {
        // Benchmark first available GPU
        gpus.iter().take(1).collect()
    };

    if gpus_to_bench.is_empty() {
        return Err(CliError::Gpu("No matching GPU found".to_string()));
    }

    print_kv("GPUs to benchmark", &gpus_to_bench.len().to_string());
    print_kv("Iterations", &args.iterations.to_string());
    println!();

    for gpu in gpus_to_bench {
        print_header(&format!("Benchmarking GPU {}: {}", gpu.id, gpu.name));
        println!();

        // Run actual GPU benchmarks using wgpu
        let results = run_gpu_benchmark(gpu.id, args.iterations)?;

        print_kv(
            "Buffer Copy (1MB)",
            &format!("{:.2} ms", results.buffer_copy_1mb_ms),
        );
        print_kv(
            "Buffer Copy (16MB)",
            &format!("{:.2} ms", results.buffer_copy_16mb_ms),
        );
        print_kv(
            "Buffer Copy (64MB)",
            &format!("{:.2} ms", results.buffer_copy_64mb_ms),
        );
        print_kv(
            "Compute Dispatch",
            &format!("{:.2} ms", results.compute_dispatch_ms),
        );

        println!();
        print_kv(
            "Effective Bandwidth (1MB)",
            &format!(
                "{}/s",
                format_size((1024.0 * 1024.0 / (results.buffer_copy_1mb_ms / 1000.0)) as u64)
            ),
        );
        print_kv(
            "Effective Bandwidth (16MB)",
            &format!(
                "{}/s",
                format_size(
                    (16.0 * 1024.0 * 1024.0 / (results.buffer_copy_16mb_ms / 1000.0)) as u64
                )
            ),
        );
        print_kv(
            "Effective Bandwidth (64MB)",
            &format!(
                "{}/s",
                format_size(
                    (64.0 * 1024.0 * 1024.0 / (results.buffer_copy_64mb_ms / 1000.0)) as u64
                )
            ),
        );

        println!();
    }

    print_success("Benchmark complete!");

    Ok(())
}

/// Benchmark results
struct BenchmarkResults {
    buffer_copy_1mb_ms: f64,
    buffer_copy_16mb_ms: f64,
    buffer_copy_64mb_ms: f64,
    compute_dispatch_ms: f64,
}

/// Run actual GPU benchmarks using wgpu
fn run_gpu_benchmark(gpu_id: usize, iterations: usize) -> CliResult<BenchmarkResults> {
    // Use all backends to support WSL2, native Windows, and native Linux
    let backends = Backends::all();

    let instance = Instance::new(InstanceDescriptor {
        backends,
        ..Default::default()
    });

    // Get only NVIDIA (CUDA) adapters
    // Check both vendor ID and name (for WSL2/D3D12 passthrough)
    let adapters: Vec<_> = instance
        .enumerate_adapters(backends)
        .into_iter()
        .filter(|a| {
            let info = a.get_info();
            let name_upper = info.name.to_uppercase();
            info.vendor == 0x10DE
                || name_upper.contains("NVIDIA")
                || name_upper.contains("GEFORCE")
                || name_upper.contains("RTX")
                || name_upper.contains("GTX")
                || name_upper.contains("QUADRO")
                || name_upper.contains("TESLA")
        })
        .collect();

    if gpu_id >= adapters.len() {
        return Err(CliError::Gpu(format!("CUDA GPU {gpu_id} not found")));
    }

    let adapter = &adapters[gpu_id];

    // Request device
    let (device, queue) = pollster::block_on(async {
        adapter
            .request_device(
                &wgpu::DeviceDescriptor {
                    label: Some("Benchmark Device"),
                    required_features: wgpu::Features::empty(),
                    required_limits: wgpu::Limits::default(),
                    memory_hints: MemoryHints::Performance,
                },
                None,
            )
            .await
    })
    .map_err(|e| CliError::Gpu(format!("Failed to create device: {e}")))?;

    // Benchmark buffer copies at different sizes
    let buffer_copy_1mb_ms = benchmark_buffer_copy(&device, &queue, 1024 * 1024, iterations);
    let buffer_copy_16mb_ms = benchmark_buffer_copy(&device, &queue, 16 * 1024 * 1024, iterations);
    let buffer_copy_64mb_ms = benchmark_buffer_copy(&device, &queue, 64 * 1024 * 1024, iterations);

    // Benchmark compute dispatch
    let compute_dispatch_ms = benchmark_compute_dispatch(&device, &queue, iterations);

    Ok(BenchmarkResults {
        buffer_copy_1mb_ms,
        buffer_copy_16mb_ms,
        buffer_copy_64mb_ms,
        compute_dispatch_ms,
    })
}

/// Benchmark buffer copy operations
fn benchmark_buffer_copy(
    device: &wgpu::Device,
    queue: &wgpu::Queue,
    size: u64,
    iterations: usize,
) -> f64 {
    // Create source buffer with data
    let data: Vec<u8> = (0..size).map(|i| (i % 256) as u8).collect();

    let src_buffer = device.create_buffer(&wgpu::BufferDescriptor {
        label: Some("Source Buffer"),
        size,
        usage: wgpu::BufferUsages::COPY_SRC | wgpu::BufferUsages::COPY_DST,
        mapped_at_creation: false,
    });

    let dst_buffer = device.create_buffer(&wgpu::BufferDescriptor {
        label: Some("Destination Buffer"),
        size,
        usage: wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::MAP_READ,
        mapped_at_creation: false,
    });

    // Write data to source buffer
    queue.write_buffer(&src_buffer, 0, &data);

    // Warmup
    for _ in 0..3 {
        let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("Copy Encoder"),
        });
        encoder.copy_buffer_to_buffer(&src_buffer, 0, &dst_buffer, 0, size);
        queue.submit(std::iter::once(encoder.finish()));
        device.poll(wgpu::Maintain::Wait);
    }

    // Benchmark
    let start = Instant::now();
    for _ in 0..iterations {
        let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("Copy Encoder"),
        });
        encoder.copy_buffer_to_buffer(&src_buffer, 0, &dst_buffer, 0, size);
        queue.submit(std::iter::once(encoder.finish()));
        device.poll(wgpu::Maintain::Wait);
    }
    let elapsed = start.elapsed();

    elapsed.as_secs_f64() * 1000.0 / iterations as f64
}

/// Benchmark compute shader dispatch
fn benchmark_compute_dispatch(
    device: &wgpu::Device,
    queue: &wgpu::Queue,
    iterations: usize,
) -> f64 {
    // Simple compute shader that does vector addition
    let shader_source = r"
        @group(0) @binding(0) var<storage, read> input_a: array<f32>;
        @group(0) @binding(1) var<storage, read> input_b: array<f32>;
        @group(0) @binding(2) var<storage, read_write> output: array<f32>;

        @compute @workgroup_size(256)
        fn main(@builtin(global_invocation_id) global_id: vec3<u32>) {
            let idx = global_id.x;
            if (idx < arrayLength(&output)) {
                output[idx] = input_a[idx] + input_b[idx];
            }
        }
    ";

    let shader = device.create_shader_module(wgpu::ShaderModuleDescriptor {
        label: Some("Compute Shader"),
        source: wgpu::ShaderSource::Wgsl(shader_source.into()),
    });

    let buffer_size: u64 = 1024 * 1024 * 4; // 1M floats = 4MB
    let num_elements = buffer_size / 4;

    // Create buffers
    let input_a = device.create_buffer(&wgpu::BufferDescriptor {
        label: Some("Input A"),
        size: buffer_size,
        usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
        mapped_at_creation: false,
    });

    let input_b = device.create_buffer(&wgpu::BufferDescriptor {
        label: Some("Input B"),
        size: buffer_size,
        usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_DST,
        mapped_at_creation: false,
    });

    let output = device.create_buffer(&wgpu::BufferDescriptor {
        label: Some("Output"),
        size: buffer_size,
        usage: wgpu::BufferUsages::STORAGE | wgpu::BufferUsages::COPY_SRC,
        mapped_at_creation: false,
    });

    // Initialize input buffers
    let data_a: Vec<f32> = (0..num_elements).map(|i| i as f32).collect();
    let data_b: Vec<f32> = (0..num_elements).map(|i| (i * 2) as f32).collect();
    queue.write_buffer(&input_a, 0, bytemuck::cast_slice(&data_a));
    queue.write_buffer(&input_b, 0, bytemuck::cast_slice(&data_b));

    // Create bind group layout and pipeline
    let bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor {
        label: Some("Bind Group Layout"),
        entries: &[
            wgpu::BindGroupLayoutEntry {
                binding: 0,
                visibility: wgpu::ShaderStages::COMPUTE,
                ty: wgpu::BindingType::Buffer {
                    ty: wgpu::BufferBindingType::Storage { read_only: true },
                    has_dynamic_offset: false,
                    min_binding_size: None,
                },
                count: None,
            },
            wgpu::BindGroupLayoutEntry {
                binding: 1,
                visibility: wgpu::ShaderStages::COMPUTE,
                ty: wgpu::BindingType::Buffer {
                    ty: wgpu::BufferBindingType::Storage { read_only: true },
                    has_dynamic_offset: false,
                    min_binding_size: None,
                },
                count: None,
            },
            wgpu::BindGroupLayoutEntry {
                binding: 2,
                visibility: wgpu::ShaderStages::COMPUTE,
                ty: wgpu::BindingType::Buffer {
                    ty: wgpu::BufferBindingType::Storage { read_only: false },
                    has_dynamic_offset: false,
                    min_binding_size: None,
                },
                count: None,
            },
        ],
    });

    let pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
        label: Some("Pipeline Layout"),
        bind_group_layouts: &[&bind_group_layout],
        push_constant_ranges: &[],
    });

    let pipeline = device.create_compute_pipeline(&wgpu::ComputePipelineDescriptor {
        label: Some("Compute Pipeline"),
        layout: Some(&pipeline_layout),
        module: &shader,
        entry_point: Some("main"),
        compilation_options: Default::default(),
        cache: None,
    });

    let bind_group = device.create_bind_group(&wgpu::BindGroupDescriptor {
        label: Some("Bind Group"),
        layout: &bind_group_layout,
        entries: &[
            wgpu::BindGroupEntry {
                binding: 0,
                resource: input_a.as_entire_binding(),
            },
            wgpu::BindGroupEntry {
                binding: 1,
                resource: input_b.as_entire_binding(),
            },
            wgpu::BindGroupEntry {
                binding: 2,
                resource: output.as_entire_binding(),
            },
        ],
    });

    let workgroup_count = num_elements.div_ceil(256) as u32;

    // Warmup
    for _ in 0..3 {
        let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("Compute Encoder"),
        });
        {
            let mut compute_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("Compute Pass"),
                timestamp_writes: None,
            });
            compute_pass.set_pipeline(&pipeline);
            compute_pass.set_bind_group(0, &bind_group, &[]);
            compute_pass.dispatch_workgroups(workgroup_count, 1, 1);
        }
        queue.submit(std::iter::once(encoder.finish()));
        device.poll(wgpu::Maintain::Wait);
    }

    // Benchmark
    let start = Instant::now();
    for _ in 0..iterations {
        let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
            label: Some("Compute Encoder"),
        });
        {
            let mut compute_pass = encoder.begin_compute_pass(&wgpu::ComputePassDescriptor {
                label: Some("Compute Pass"),
                timestamp_writes: None,
            });
            compute_pass.set_pipeline(&pipeline);
            compute_pass.set_bind_group(0, &bind_group, &[]);
            compute_pass.dispatch_workgroups(workgroup_count, 1, 1);
        }
        queue.submit(std::iter::once(encoder.finish()));
        device.poll(wgpu::Maintain::Wait);
    }
    let elapsed = start.elapsed();

    elapsed.as_secs_f64() * 1000.0 / iterations as f64
}

// =============================================================================
// Memory Subcommand
// =============================================================================

fn execute_memory() -> CliResult<()> {
    print_header("GPU Memory Information");
    println!();

    let gpus = detect_gpus()?;

    if gpus.is_empty() {
        print_warning("No GPU devices detected.");
        return Ok(());
    }

    println!("{:<4} {:<35} {:>15}", "ID", "Name", "Total Memory");
    println!("{}", "-".repeat(56));

    for gpu in &gpus {
        println!(
            "{:<4} {:<35} {:>15}",
            gpu.id,
            truncate_string(&gpu.name, 34),
            format_size(gpu.memory_total),
        );
    }

    println!();

    // Total across all GPUs
    let total_memory: u64 = gpus.iter().map(|g| g.memory_total).sum();
    print_kv("Total GPU Memory", &format_size(total_memory));

    // Note about memory reporting
    print_info(
        "Note: wgpu reports total device memory. For real-time usage, use nvidia-smi or similar tools.",
    );

    Ok(())
}

// =============================================================================
// Status Subcommand
// =============================================================================

fn execute_status() -> CliResult<()> {
    print_header("GPU Status");
    println!();

    // Current selection
    if let Some(device_id) = load_gpu_selection() {
        print_kv("Selected Device", &device_id.to_string());
    } else {
        print_kv("Selected Device", "None (auto-select)");
    }

    println!();

    // Show ALL adapters for diagnostics (not just NVIDIA)
    print_header("All Detected Adapters (Diagnostics)");
    println!();

    let instance = Instance::new(InstanceDescriptor {
        backends: Backends::all(),
        ..Default::default()
    });

    let all_adapters = instance.enumerate_adapters(Backends::all());

    if all_adapters.is_empty() {
        print_warning("No GPU adapters detected by wgpu.");
        println!();
        print_info("This may indicate:");
        println!("  - Missing graphics drivers");
        println!("  - WSL2 GPU passthrough not configured");
        println!("  - No compatible GPU hardware");
        return Ok(());
    }

    println!(
        "{:<4} {:<30} {:>10} {:>12} {:>12}",
        "ID", "Name", "Vendor", "Backend", "Type"
    );
    println!("{}", "-".repeat(70));

    for (i, adapter) in all_adapters.iter().enumerate() {
        let info = adapter.get_info();
        let name_upper = info.name.to_uppercase();

        // Detect NVIDIA by vendor ID or name
        let is_nvidia = info.vendor == 0x10DE
            || name_upper.contains("NVIDIA")
            || name_upper.contains("GEFORCE")
            || name_upper.contains("RTX")
            || name_upper.contains("GTX")
            || name_upper.contains("QUADRO")
            || name_upper.contains("TESLA");

        let vendor = if is_nvidia {
            "NVIDIA"
        } else {
            match info.vendor {
                0x1002 => "AMD",
                0x10DE => "NVIDIA",
                0x8086 => "Intel",
                0x13B5 => "ARM",
                0x5143 => "Qualcomm",
                0x106B => "Apple",
                0x1414 => "Microsoft",
                _ => "Unknown",
            }
        };
        let backend = format!("{:?}", info.backend);
        let device_type = match info.device_type {
            DeviceType::DiscreteGpu => "Discrete",
            DeviceType::IntegratedGpu => "Integrated",
            DeviceType::VirtualGpu => "Virtual",
            DeviceType::Cpu => "CPU",
            DeviceType::Other => "Other",
        };
        let is_cuda = if is_nvidia { " [CUDA]" } else { "" };

        println!(
            "{:<4} {:<30} {:>10} {:>12} {:>12}{}",
            i,
            truncate_string(&info.name, 29),
            vendor,
            backend,
            device_type,
            is_cuda
        );
    }

    // Now show CUDA GPUs
    println!();
    let gpus = detect_gpus()?;

    if gpus.is_empty() {
        print_kv("CUDA GPU Count", "0");
        print_warning("No NVIDIA CUDA GPUs detected.");
    } else {
        print_kv("CUDA GPU Count", &gpus.len().to_string());

        // Recommendations
        println!();
        print_header("Recommendations");

        let best_gpu = gpus
            .iter()
            .filter(|g| g.device_type == "Discrete")
            .max_by_key(|g| g.memory_total);

        if let Some(gpu) = best_gpu {
            print_info(&format!(
                "Recommended device: GPU {} ({}) with {} memory",
                gpu.id,
                gpu.name,
                format_size(gpu.memory_total)
            ));
        } else if let Some(gpu) = gpus.first() {
            print_info(&format!(
                "Available device: GPU {} ({}) with {} memory",
                gpu.id,
                gpu.name,
                format_size(gpu.memory_total)
            ));
        }
    }

    Ok(())
}

// =============================================================================
// GPU Detection Functions
// =============================================================================

/// Detect CUDA-capable GPUs (NVIDIA only)
///
/// This function only returns NVIDIA GPUs which are CUDA-capable.
/// Uses all available backends to support various environments including WSL2.
fn detect_gpus() -> CliResult<Vec<GpuInfo>> {
    // Use all backends to support WSL2, native Windows, and native Linux
    // WSL2 exposes GPUs through various backends depending on driver setup
    let backends = Backends::all();

    let instance = Instance::new(InstanceDescriptor {
        backends,
        ..Default::default()
    });

    let adapters = instance.enumerate_adapters(backends);

    let mut gpus = Vec::new();
    let mut gpu_id = 0;

    for adapter in &adapters {
        let info = adapter.get_info();

        // Check if this is an NVIDIA GPU (CUDA-capable)
        // Method 1: Vendor ID 0x10DE = NVIDIA
        // Method 2: Name contains NVIDIA identifiers (for WSL2/D3D12 passthrough)
        let name_upper = info.name.to_uppercase();
        let is_nvidia = info.vendor == 0x10DE
            || name_upper.contains("NVIDIA")
            || name_upper.contains("GEFORCE")
            || name_upper.contains("RTX")
            || name_upper.contains("GTX")
            || name_upper.contains("QUADRO")
            || name_upper.contains("TESLA");

        if !is_nvidia {
            continue;
        }

        let device_type = match info.device_type {
            DeviceType::DiscreteGpu => "Discrete",
            DeviceType::IntegratedGpu => "Integrated",
            DeviceType::VirtualGpu => "Virtual",
            DeviceType::Cpu => "CPU",
            DeviceType::Other => "Other",
        };

        // Get the actual backend being used
        // For NVIDIA GPUs, all backends can use CUDA compute
        let backend = match info.backend {
            wgpu::Backend::Vulkan => "Vulkan/CUDA",
            wgpu::Backend::Dx12 => "DX12/CUDA",
            wgpu::Backend::Metal => "Metal",
            wgpu::Backend::Gl => "GL/CUDA", // WSL2 passthrough
            wgpu::Backend::BrowserWebGpu => "WebGPU",
            wgpu::Backend::Empty => "None",
        };

        // Get memory limits
        let limits = adapter.limits();
        let memory_total = limits.max_buffer_size;

        gpus.push(GpuInfo {
            id: gpu_id,
            name: info.name.clone(),
            vendor: "NVIDIA".to_string(),
            device_type: device_type.to_string(),
            backend: backend.to_string(),
            driver: info.driver.clone(),
            memory_total,
            is_available: true,
        });

        gpu_id += 1;
    }

    Ok(gpus)
}

// =============================================================================
// Helper Functions
// =============================================================================

fn save_gpu_selection(device_id: usize) -> CliResult<()> {
    let config_dir = std::path::PathBuf::from(".axonml");
    std::fs::create_dir_all(&config_dir)?;

    let config = serde_json::json!({
        "device_id": device_id,
    });

    std::fs::write(
        config_dir.join("gpu_config.json"),
        serde_json::to_string_pretty(&config)?,
    )?;

    Ok(())
}

fn load_gpu_selection() -> Option<usize> {
    let config_path = std::path::PathBuf::from(".axonml").join("gpu_config.json");
    let content = std::fs::read_to_string(config_path).ok()?;
    let config: serde_json::Value = serde_json::from_str(&content).ok()?;
    config.get("device_id")?.as_u64().map(|id| id as usize)
}

fn format_size(bytes: u64) -> String {
    const KB: u64 = 1024;
    const MB: u64 = KB * 1024;
    const GB: u64 = MB * 1024;
    const TB: u64 = GB * 1024;

    if bytes >= TB {
        format!("{:.2} TB", bytes as f64 / TB as f64)
    } else if bytes >= GB {
        format!("{:.2} GB", bytes as f64 / GB as f64)
    } else if bytes >= MB {
        format!("{:.2} MB", bytes as f64 / MB as f64)
    } else if bytes >= KB {
        format!("{:.2} KB", bytes as f64 / KB as f64)
    } else {
        format!("{bytes} B")
    }
}

fn truncate_string(s: &str, max_len: usize) -> String {
    if max_len <= 3 {
        return "...".to_string();
    }
    if s.len() <= max_len {
        s.to_string()
    } else {
        format!("{}...", &s[..max_len - 3])
    }
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_format_size() {
        assert!(format_size(500).contains('B'));
        assert!(format_size(1500).contains("KB"));
        assert!(format_size(1500000).contains("MB"));
        assert!(format_size(1500000000).contains("GB"));
    }

    #[test]
    fn test_truncate_string() {
        assert_eq!(truncate_string("short", 10), "short");
        // "a very long string"[..7] = "a very " -> "a very ..." (10 chars)
        assert_eq!(truncate_string("a very long string", 10), "a very ...");
        // Edge case: max_len <= 3
        assert_eq!(truncate_string("test", 3), "...");
        assert_eq!(truncate_string("test", 2), "...");
    }

    // Note: test_detect_gpus is intentionally omitted as it requires
    // actual GPU hardware and can cause SIGSEGV in test environments
    // without proper GPU drivers
}