1#![allow(clippy::print_stdout, clippy::print_stderr)]
3
4use tch::Device;
5
6fn main() {
7 println!("ROCm GPU Detection Test");
8 println!("=======================");
9
10 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 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 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 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
54fn 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 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 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}