Skip to main content

test_gpu/
test_gpu.rs

1// Test binary intentionally prints diagnostic output
2#![allow(clippy::print_stdout, clippy::print_stderr)]
3
4use tch::Device;
5
6fn main() {
7    println!("ROCm GPU Detection Test");
8    println!("=======================");
9
10    // Set environment for ROCm — use /opt/rocm (standard install path) and let
11    // the runtime auto-detect the GPU ISA. HSA_OVERRIDE_GFX_VERSION=9.0.0 was
12    // removed: it forced MI210/gfx90a kernels on all GPUs, breaking gfx1151
13    // (Strix Halo / Radeon 8060S) by loading the wrong ISA and silently
14    // falling back to CPU (#228).
15    std::env::set_var("ROCM_PATH", "/opt/rocm");
16    std::env::set_var("HIP_VISIBLE_DEVICES", "0");
17
18    println!("Environment set for ROCm (auto-detect GPU architecture)");
19
20    // Check device
21    let device = Device::cuda_if_available();
22
23    match device {
24        Device::Cpu => {
25            println!("Result: CPU mode");
26            println!("\nDebug: Trying to force GPU...");
27
28            // Try to force GPU allocation
29            match std::panic::catch_unwind(|| {
30                let _tensor = tch::Tensor::zeros([1], (tch::Kind::Float, Device::Cuda(0)));
31                println!("Force GPU: Success!");
32            }) {
33                Ok(_) => {}
34                Err(_) => println!("Force GPU: Failed - GPU not accessible"),
35            }
36        }
37        Device::Cuda(n) => {
38            println!("Result: ✅ GPU {n} detected!");
39            // Detection alone is not validation: a libtorch without kernels for
40            // the detected ISA (e.g. gfx1151 / Strix Halo) allocates fine but
41            // errors or silently falls back on first compute. Exercise real
42            // matmul/softmax/reduction on-device and compare against CPU.
43            if let Err(err) = compute_smoke(Device::Cuda(n)) {
44                eprintln!("❌ GPU compute smoke FAILED: {err:#}");
45                std::process::exit(1);
46            }
47        }
48        _ => {
49            println!("Result: Other device type detected");
50        }
51    }
52}
53
54/// Run a small end-to-end compute check on `device`: y = softmax(x @ w + b),
55/// compared elementwise against the same computation on CPU. Catches
56/// missing-ISA kernels, mislinked HIP libraries, and silent CPU fallback.
57fn compute_smoke(device: Device) -> anyhow::Result<()> {
58    use tch::Kind;
59
60    let shape: [i64; 2] = [256, 256];
61    let x = tch::Tensor::f_randn(shape, (Kind::Float, device))?;
62    let w = tch::Tensor::f_randn(shape, (Kind::Float, device))?;
63    let b = tch::Tensor::f_randn([shape[1]], (Kind::Float, device))?;
64
65    let y_gpu = tch::Tensor::f_softmax(&x.f_matmul(&w)?.f_add(&b)?, -1, Kind::Float)?;
66
67    // Same math on CPU for the reference result (reuse the exact same tensors).
68    let (x_c, w_c, b_c) = (
69        x.f_to_device(Device::Cpu)?,
70        w.f_to_device(Device::Cpu)?,
71        b.f_to_device(Device::Cpu)?,
72    );
73    let y_cpu = tch::Tensor::f_softmax(&x_c.f_matmul(&w_c)?.f_add(&b_c)?, -1, Kind::Float)?;
74
75    let max_diff = y_gpu
76        .f_to_device(Device::Cpu)?
77        .f_sub(&y_cpu)?
78        .f_abs()?
79        .f_max()?;
80    let max_diff = f32::try_from(&max_diff)?;
81    println!(
82        "GPU compute smoke: softmax(matmul+bias) on {device:?} — max |GPU−CPU| = {max_diff:.2e}"
83    );
84    if !max_diff.is_finite() || max_diff > 1e-4 {
85        anyhow::bail!("GPU result diverges from CPU reference by {max_diff}");
86    }
87
88    // fp16 path too — HIP matmul requires matching dtypes (see torch_engine
89    // TTT notes) and inference weights are commonly half precision. Tolerance
90    // is loose: fp16 input rounding on ±N(0,16) dot products, not exact math.
91    let y_h = x
92        .f_to_kind(Kind::Half)?
93        .f_matmul(&w.f_to_kind(Kind::Half)?)?;
94    let h_diff = y_h
95        .f_to_device(Device::Cpu)?
96        .f_to_kind(Kind::Float)?
97        .f_sub(&x_c.f_matmul(&w_c)?)?
98        .f_abs()?
99        .f_max()?;
100    let h_diff = f32::try_from(&h_diff)?;
101    println!("GPU fp16 matmul smoke: max |fp16−fp32| = {h_diff:.2e} (tolerance 1.0)");
102    if !h_diff.is_finite() || h_diff > 1.0 {
103        anyhow::bail!("fp16 GPU matmul diverges unexpectedly from fp32 ({h_diff})");
104    }
105
106    println!("✅ GPU compute validation passed");
107    Ok(())
108}