Skip to main content

test_rocm_ffi/
test_rocm_ffi.rs

1// Direct FFI test bypassing tch-rs wrappers
2// Test binary intentionally prints diagnostic output
3#![allow(clippy::print_stdout, clippy::print_stderr)]
4
5use std::os::raw::c_int;
6
7#[link(name = "torch")]
8extern "C" {
9    // Direct binding to PyTorch C++ API
10    #[link_name = "_ZN5torch4cuda12is_availableEv"]
11    fn torch_cuda_is_available() -> bool;
12
13    #[link_name = "_ZN5torch4cuda12device_countEv"]
14    fn torch_cuda_device_count() -> c_int;
15}
16
17fn main() {
18    println!("ROCm Direct FFI Test");
19    println!("====================");
20
21    // Set ROCm environment — use /opt/rocm (standard install path); let runtime
22    // auto-detect PYTORCH_ROCM_ARCH. The hardcoded gfx90a (MI210) caused silent
23    // CPU fallback on gfx1151 (Strix Halo / Radeon 8060S) (#228).
24    std::env::set_var("ROCM_PATH", "/opt/rocm");
25    std::env::set_var("HIP_VISIBLE_DEVICES", "0");
26
27    unsafe {
28        let available = torch_cuda_is_available();
29        let count = torch_cuda_device_count();
30
31        println!("GPU Available (direct FFI): {available}");
32        println!("Device Count (direct FFI): {count}");
33
34        if available {
35            println!("✅ ROCm GPU detected via direct FFI!");
36        } else {
37            println!("❌ No GPU detected");
38        }
39    }
40
41    // Now compare with tch-rs
42    println!("\nComparing with tch-rs:");
43    let device = tch::Device::cuda_if_available();
44    match device {
45        tch::Device::Cpu => println!("tch-rs: CPU (not detecting GPU)"),
46        tch::Device::Cuda(n) => println!("tch-rs: GPU {n} detected"),
47        _ => println!("tch-rs: Other device"),
48    }
49}