Files
foxhunt/crates/ml/examples/cuda_test.rs
jgrusewski afd85b2f8f chore: clean up examples, update ML binaries and risk tests
- Delete 14 unused example files (-3,543 lines): config, adaptive-strategy,
  data, storage, trading_engine, api_gateway, backtesting, trading_service, chaos
- Update ML training/eval binaries: improved CLI args, completion tracking,
  CUDA test cleanup, hyperopt enhancements
- Fix KAN network and TFT module adjustments
- Update risk test assertions for consistency
- Fix backtesting repositories and promotion manager
- Update .serena project config and Cargo dependencies

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-27 01:33:18 +01:00

92 lines
2.9 KiB
Rust

//! Simple CUDA functionality test to verify compatibility
//!
//! This test verifies that the updated candle-core with CUDA support
//! can successfully create tensors and perform basic operations.
#![allow(unused_crate_dependencies)]
use candle_core::{Device, Tensor};
use candle_nn::{linear, Module, VarBuilder, VarMap};
/// Format a tensor's dimensions as a display string without using Debug formatting.
fn fmt_dims(dims: &[usize]) -> String {
let parts: Vec<String> = dims.iter().map(|d| d.to_string()).collect();
format!("[{}]", parts.join(", "))
}
/// Test basic CUDA tensor operations
pub fn test_cuda_basic() -> Result<(), Box<dyn std::error::Error>> {
println!("Testing CUDA compatibility...");
// Try to get CUDA device
match Device::new_cuda(0) {
Ok(device) => {
println!("[OK] CUDA device 0 available");
// Create a simple tensor
let tensor = Tensor::randn(0_f32, 1.0, (4, 4), &device)?;
println!("[OK] Created CUDA tensor: {}", fmt_dims(tensor.dims()));
// Perform basic operations
let result = tensor.matmul(&tensor.t()?)?;
println!(
"[OK] Matrix multiplication successful: {}",
fmt_dims(result.dims())
);
Ok(())
},
Err(e) => {
println!("[WARN] CUDA device not available: {}", e);
println!("This is expected if no GPU is present, but CUDA compilation succeeded");
Ok(())
},
}
}
/// Test candle-nn components with CUDA
pub fn test_cuda_neural_network() -> Result<(), Box<dyn std::error::Error>> {
println!("Testing CUDA neural network components...");
if let Ok(device) = Device::new_cuda(0) {
let varmap = VarMap::new();
let vs = VarBuilder::from_varmap(&varmap, candle_core::DType::F32, &device);
// Create a simple linear layer
let linear_layer = linear(10, 5, vs.pp("linear"))?;
let input = Tensor::randn(0_f32, 1.0, (1, 10), &device)?;
let output = linear_layer.forward(&input)?;
println!(
"[OK] Neural network forward pass successful: {}",
fmt_dims(output.dims())
);
} else {
println!("[WARN] CUDA neural network test skipped (no GPU)");
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn verify_cuda_compilation() {
// This test just verifies that CUDA code compiles
// The actual runtime test is optional since CI may not have GPU
println!("CUDA compilation test passed!");
// Try to run basic test but don't fail if no GPU
let _ = test_cuda_basic();
let _ = test_cuda_neural_network();
}
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
test_cuda_basic()?;
test_cuda_neural_network()?;
println!("[DONE] CUDA compatibility verification complete!");
Ok(())
}