use scirs2_core::ndarray::{Array1, Array2};
use sklears_core::error::{Result, SklearsError};
use sklears_core::types::{Float, Int};
use std::sync::Arc;
#[cfg(feature = "gpu")]
use sklears_core::gpu::{GpuArray, GpuBackend as CoreGpuBackend, GpuMatrixOps};
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum GpuBackend {
Cuda,
OpenCL,
Metal,
Vulkan,
CpuFallback,
}
#[derive(Debug, Clone)]
pub struct GpuConfig {
pub backend: GpuBackend,
pub device_id: usize,
pub memory_limit_mb: Option<usize>,
pub batch_size: usize,
pub n_streams: usize,
pub mixed_precision: bool,
pub tensor_cores: bool,
pub memory_pool_size_mb: usize,
pub enable_profiling: bool,
}
impl Default for GpuConfig {
fn default() -> Self {
Self {
backend: GpuBackend::CpuFallback,
device_id: 0,
memory_limit_mb: None,
batch_size: 1024,
n_streams: 4,
mixed_precision: false,
tensor_cores: false,
memory_pool_size_mb: 1024,
enable_profiling: false,
}
}
}
pub struct GpuContext {
config: GpuConfig,
device_info: GpuDeviceInfo,
memory_manager: GpuMemoryManager,
profiler: Option<GpuProfiler>,
#[cfg(feature = "gpu")]
core_backend: Option<CoreGpuBackend>,
}
#[derive(Debug, Clone)]
pub struct GpuDeviceInfo {
pub name: String,
pub total_memory_mb: usize,
pub available_memory_mb: usize,
pub compute_units: usize,
pub max_work_group_size: usize,
pub supports_mixed_precision: bool,
pub supports_tensor_cores: bool,
}
pub struct GpuMemoryManager {
allocated_bytes: usize,
peak_allocated_bytes: usize,
pool_size_bytes: usize,
kind: MemoryManagerKind,
}
enum MemoryManagerKind {
Host { blocks: Vec<GpuMemoryBlock> },
#[cfg(feature = "gpu")]
Device {
backend: CoreGpuBackend,
blocks: Vec<Option<oxicuda_memory::DeviceBuffer<u8>>>,
},
}
#[derive(Debug)]
pub struct GpuMemoryBlock {
pub ptr: usize,
pub size_bytes: usize,
pub in_use: bool,
}
pub struct GpuProfiler {
enabled: bool,
kernel_times: Vec<(String, f64)>,
memory_transfers: Vec<(String, usize, f64)>,
}
pub struct GpuTensorOps {
#[allow(dead_code)]
context: Arc<GpuContext>,
}
pub struct GpuEnsembleTrainer {
context: Arc<GpuContext>,
tensor_ops: GpuTensorOps,
}
impl GpuContext {
pub fn new(config: GpuConfig) -> Result<Self> {
#[cfg(feature = "gpu")]
{
let (device_info, core_backend) = Self::detect_device(&config)?;
let memory_manager = match &core_backend {
Some(backend) => GpuMemoryManager::new_with_backend(
config.memory_pool_size_mb * 1024 * 1024,
backend.clone(),
),
None => GpuMemoryManager::new(config.memory_pool_size_mb * 1024 * 1024),
};
let profiler = if config.enable_profiling {
Some(GpuProfiler::new())
} else {
None
};
Ok(Self {
config,
device_info,
memory_manager,
profiler,
core_backend,
})
}
#[cfg(not(feature = "gpu"))]
{
let device_info = Self::detect_device(&config)?;
let memory_manager = GpuMemoryManager::new(config.memory_pool_size_mb * 1024 * 1024);
let profiler = if config.enable_profiling {
Some(GpuProfiler::new())
} else {
None
};
Ok(Self {
config,
device_info,
memory_manager,
profiler,
})
}
}
#[cfg(feature = "gpu")]
fn detect_device(config: &GpuConfig) -> Result<(GpuDeviceInfo, Option<CoreGpuBackend>)> {
match config.backend {
GpuBackend::Cuda => Self::detect_cuda_device(config.device_id),
GpuBackend::OpenCL => Err(Self::unsupported_backend_err("OpenCL")),
GpuBackend::Metal => Err(Self::unsupported_backend_err("Metal")),
GpuBackend::Vulkan => Err(Self::unsupported_backend_err("Vulkan")),
GpuBackend::CpuFallback => Ok((Self::create_cpu_fallback_info(), None)),
}
}
#[cfg(not(feature = "gpu"))]
fn detect_device(config: &GpuConfig) -> Result<GpuDeviceInfo> {
match config.backend {
GpuBackend::Cuda => Self::detect_cuda_device(config.device_id),
GpuBackend::OpenCL => Err(Self::unsupported_backend_err("OpenCL")),
GpuBackend::Metal => Err(Self::unsupported_backend_err("Metal")),
GpuBackend::Vulkan => Err(Self::unsupported_backend_err("Vulkan")),
GpuBackend::CpuFallback => Ok(Self::create_cpu_fallback_info()),
}
}
fn unsupported_backend_err(name: &str) -> SklearsError {
SklearsError::NotImplemented(format!(
"{name} is not supported by the oxicuda backend (CUDA-only); \
select GpuBackend::Cuda or GpuBackend::CpuFallback instead"
))
}
#[cfg(feature = "gpu")]
fn detect_cuda_device(device_id: usize) -> Result<(GpuDeviceInfo, Option<CoreGpuBackend>)> {
let Some(backend) = CoreGpuBackend::with_device_id(device_id)? else {
return Err(SklearsError::NotImplemented(format!(
"no CUDA device available at ordinal {device_id} (oxicuda-driver reported none)"
)));
};
let device = backend.context().device();
let name = device
.name()
.unwrap_or_else(|_| "Unknown CUDA device".to_string());
let mem_info = backend.memory_info()?;
let compute_units = device
.multiprocessor_count()
.map(|n| n.max(0) as usize)
.unwrap_or(0);
let max_work_group_size = device
.max_threads_per_block()
.map(|n| n.max(0) as usize)
.unwrap_or(0);
let supports_tensor_cores = device
.compute_capability()
.map(|(major, _)| major >= 7)
.unwrap_or(false);
Ok((
GpuDeviceInfo {
name,
total_memory_mb: mem_info.total / (1024 * 1024),
available_memory_mb: mem_info.free / (1024 * 1024),
compute_units,
max_work_group_size,
supports_mixed_precision: supports_tensor_cores,
supports_tensor_cores,
},
Some(backend),
))
}
#[cfg(not(feature = "gpu"))]
fn detect_cuda_device(_device_id: usize) -> Result<GpuDeviceInfo> {
Err(SklearsError::NotImplemented(
"CUDA support requires building sklears-ensemble with the `gpu` feature".to_string(),
))
}
fn create_cpu_fallback_info() -> GpuDeviceInfo {
let (total_mb, available_mb) = Self::read_system_ram_mb();
GpuDeviceInfo {
name: "CPU Fallback (no GPU runtime)".to_string(),
total_memory_mb: total_mb,
available_memory_mb: available_mb,
compute_units: std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(1),
max_work_group_size: 1,
supports_mixed_precision: false,
supports_tensor_cores: false,
}
}
fn read_system_ram_mb() -> (usize, usize) {
#[cfg(target_os = "linux")]
{
if let Ok(content) = std::fs::read_to_string("/proc/meminfo") {
let mut total_kb: Option<u64> = None;
let mut avail_kb: Option<u64> = None;
for line in content.lines() {
if let Some(rest) = line.strip_prefix("MemTotal:") {
total_kb = rest
.split_whitespace()
.next()
.and_then(|s| s.parse::<u64>().ok());
} else if let Some(rest) = line.strip_prefix("MemAvailable:") {
avail_kb = rest
.split_whitespace()
.next()
.and_then(|s| s.parse::<u64>().ok());
}
if total_kb.is_some() && avail_kb.is_some() {
break;
}
}
if let (Some(t), Some(a)) = (total_kb, avail_kb) {
return ((t / 1024) as usize, (a / 1024) as usize);
}
}
(0, 0)
}
#[cfg(not(target_os = "linux"))]
{
(0, 0)
}
}
pub fn is_gpu_available(&self) -> bool {
#[cfg(feature = "gpu")]
{
self.core_backend.is_some()
}
#[cfg(not(feature = "gpu"))]
{
self.config.backend != GpuBackend::CpuFallback
}
}
pub fn device_info(&self) -> &GpuDeviceInfo {
&self.device_info
}
pub fn config(&self) -> &GpuConfig {
&self.config
}
#[cfg(feature = "gpu")]
pub fn core_backend(&self) -> Option<&CoreGpuBackend> {
self.core_backend.as_ref()
}
pub fn allocate_memory(&mut self, size_bytes: usize) -> Result<usize> {
self.memory_manager.allocate(size_bytes)
}
pub fn free_memory(&mut self, ptr: usize) -> Result<()> {
self.memory_manager.free(ptr)
}
pub fn start_profiling(&mut self) {
if let Some(ref mut profiler) = self.profiler {
profiler.start();
}
}
pub fn stop_profiling(&mut self) -> Option<ProfilingResults> {
self.profiler.as_mut().map(|p| p.stop())
}
}
impl GpuMemoryManager {
pub fn new(pool_size_bytes: usize) -> Self {
Self {
allocated_bytes: 0,
peak_allocated_bytes: 0,
pool_size_bytes,
kind: MemoryManagerKind::Host { blocks: Vec::new() },
}
}
#[cfg(feature = "gpu")]
pub fn new_with_backend(pool_size_bytes: usize, backend: CoreGpuBackend) -> Self {
Self {
allocated_bytes: 0,
peak_allocated_bytes: 0,
pool_size_bytes,
kind: MemoryManagerKind::Device {
backend,
blocks: Vec::new(),
},
}
}
pub fn allocate(&mut self, size_bytes: usize) -> Result<usize> {
if self.allocated_bytes + size_bytes > self.pool_size_bytes {
return Err(SklearsError::InvalidInput(
"GPU memory allocation failed: out of memory".to_string(),
));
}
let handle = match &mut self.kind {
MemoryManagerKind::Host { blocks } => {
let mut reused = None;
for block in blocks.iter_mut() {
if !block.in_use && block.size_bytes >= size_bytes {
block.in_use = true;
reused = Some(block.ptr);
break;
}
}
match reused {
Some(ptr) => ptr,
None => {
let ptr = blocks.len();
blocks.push(GpuMemoryBlock {
ptr,
size_bytes,
in_use: true,
});
ptr
}
}
}
#[cfg(feature = "gpu")]
MemoryManagerKind::Device { backend, blocks } => {
backend
.context()
.set_current()
.map_err(|e| SklearsError::NumericalError(e.to_string()))?;
let buf = oxicuda_memory::DeviceBuffer::<u8>::alloc(size_bytes)
.map_err(|e| SklearsError::NumericalError(e.to_string()))?;
let handle = blocks.len();
blocks.push(Some(buf));
handle
}
};
self.allocated_bytes += size_bytes;
self.peak_allocated_bytes = self.peak_allocated_bytes.max(self.allocated_bytes);
Ok(handle)
}
pub fn free(&mut self, ptr: usize) -> Result<()> {
match &mut self.kind {
MemoryManagerKind::Host { blocks } => {
if let Some(block) = blocks.get_mut(ptr) {
if block.in_use {
block.in_use = false;
self.allocated_bytes =
self.allocated_bytes.saturating_sub(block.size_bytes);
Ok(())
} else {
Err(SklearsError::InvalidInput(
"Attempted to free already freed memory".to_string(),
))
}
} else {
Err(SklearsError::InvalidInput(
"Invalid memory pointer".to_string(),
))
}
}
#[cfg(feature = "gpu")]
MemoryManagerKind::Device { blocks, .. } => {
if let Some(slot) = blocks.get_mut(ptr) {
if let Some(buf) = slot.take() {
self.allocated_bytes = self.allocated_bytes.saturating_sub(buf.len());
Ok(())
} else {
Err(SklearsError::InvalidInput(
"Attempted to free already freed memory".to_string(),
))
}
} else {
Err(SklearsError::InvalidInput(
"Invalid memory pointer".to_string(),
))
}
}
}
}
pub fn memory_stats(&self) -> (usize, usize, usize) {
(
self.allocated_bytes,
self.peak_allocated_bytes,
self.pool_size_bytes,
)
}
}
impl Default for GpuProfiler {
fn default() -> Self {
Self::new()
}
}
impl GpuProfiler {
pub fn new() -> Self {
Self {
enabled: false,
kernel_times: Vec::new(),
memory_transfers: Vec::new(),
}
}
pub fn start(&mut self) {
self.enabled = true;
self.kernel_times.clear();
self.memory_transfers.clear();
}
pub fn stop(&mut self) -> ProfilingResults {
self.enabled = false;
ProfilingResults {
kernel_times: self.kernel_times.clone(),
memory_transfers: self.memory_transfers.clone(),
total_kernel_time: self.kernel_times.iter().map(|(_, t)| t).sum(),
total_memory_transfer_time: self.memory_transfers.iter().map(|(_, _, t)| t).sum(),
}
}
pub fn record_kernel(&mut self, name: String, time_ms: f64) {
if self.enabled {
self.kernel_times.push((name, time_ms));
}
}
pub fn record_memory_transfer(&mut self, name: String, bytes: usize, time_ms: f64) {
if self.enabled {
self.memory_transfers.push((name, bytes, time_ms));
}
}
}
#[derive(Debug, Clone)]
pub struct ProfilingResults {
pub kernel_times: Vec<(String, f64)>,
pub memory_transfers: Vec<(String, usize, f64)>,
pub total_kernel_time: f64,
pub total_memory_transfer_time: f64,
}
impl GpuTensorOps {
pub fn new(context: Arc<GpuContext>) -> Self {
Self { context }
}
pub fn matmul(&self, a: &Array2<Float>, b: &Array2<Float>) -> Result<Array2<Float>> {
#[cfg(feature = "gpu")]
{
if let Some(backend) = self.context.core_backend() {
let ga = GpuArray::from_array2(backend, a)?;
let gb = GpuArray::from_array2(backend, b)?;
let gc = ga.matmul(&gb)?;
return gc.to_array2();
}
}
Ok(a.dot(b))
}
pub fn elementwise_add(&self, a: &Array2<Float>, b: &Array2<Float>) -> Result<Array2<Float>> {
#[cfg(feature = "gpu")]
{
if let Some(backend) = self.context.core_backend() {
let ga = GpuArray::from_array2(backend, a)?;
let gb = GpuArray::from_array2(backend, b)?;
let gc = ga.add(&gb)?;
return gc.to_array2();
}
}
Ok(a + b)
}
pub fn reduce_sum(&self, array: &Array2<Float>, axis: Option<usize>) -> Result<Array1<Float>> {
match axis {
Some(ax) => Ok(array.sum_axis(scirs2_core::ndarray::Axis(ax))),
None => Ok(Array1::from_elem(1, array.sum())),
}
}
pub fn softmax(&self, array: &Array2<Float>) -> Result<Array2<Float>> {
let mut result = array.clone();
for mut row in result.rows_mut() {
let max_val = row.fold(Float::NEG_INFINITY, |a, &b| a.max(b));
row.mapv_inplace(|x| (x - max_val).exp());
let sum = row.sum();
row /= sum;
}
Ok(result)
}
}
impl GpuEnsembleTrainer {
pub fn new(config: GpuConfig) -> Result<Self> {
let context = Arc::new(GpuContext::new(config)?);
let tensor_ops = GpuTensorOps::new(context.clone());
Ok(Self {
context,
tensor_ops,
})
}
pub fn predict_ensemble(
&self,
models: &[Array1<Float>],
x: &Array2<Float>,
) -> Result<Array1<Int>> {
if models.is_empty() {
return Ok(Array1::zeros(x.nrows()));
}
let n_features = x.ncols();
let mut models_matrix = Array2::<Float>::zeros((n_features, models.len()));
for (j, model) in models.iter().enumerate() {
if model.len() != n_features {
return Err(SklearsError::ShapeMismatch {
expected: format!("model feature count = {n_features}"),
actual: format!("model[{j}].len() = {}", model.len()),
});
}
models_matrix.column_mut(j).assign(model);
}
let per_model_scores = self.tensor_ops.matmul(x, &models_matrix)?;
let sums = per_model_scores.sum_axis(scirs2_core::ndarray::Axis(1));
Ok(sums.mapv(|s| if s > 0.0 { 1 } else { 0 }))
}
pub fn context(&self) -> &GpuContext {
&self.context
}
pub fn is_gpu_available(&self) -> bool {
self.context.is_gpu_available()
}
}
pub fn detect_available_backends() -> Vec<GpuBackend> {
let mut backends = Vec::new();
if is_cuda_available() {
backends.push(GpuBackend::Cuda);
}
backends.push(GpuBackend::CpuFallback);
backends
}
#[cfg(feature = "gpu")]
fn is_cuda_available() -> bool {
CoreGpuBackend::is_available()
}
#[cfg(not(feature = "gpu"))]
fn is_cuda_available() -> bool {
false
}
#[allow(non_snake_case)]
#[cfg(test)]
mod tests {
use super::*;
use scirs2_core::ndarray::array;
#[test]
fn test_gpu_config_default() {
let config = GpuConfig::default();
assert_eq!(config.backend, GpuBackend::CpuFallback);
assert_eq!(config.device_id, 0);
assert_eq!(config.batch_size, 1024);
}
#[test]
fn test_gpu_context_creation() {
let config = GpuConfig::default();
let context = GpuContext::new(config).expect("operation should succeed");
assert!(!context.is_gpu_available()); }
#[test]
fn test_memory_manager() {
let mut manager = GpuMemoryManager::new(1024 * 1024);
let ptr1 = manager.allocate(1024).expect("operation should succeed");
let ptr2 = manager.allocate(2048).expect("operation should succeed");
assert_ne!(ptr1, ptr2);
manager.free(ptr1).expect("operation should succeed");
manager.free(ptr2).expect("operation should succeed");
let (allocated, _, total) = manager.memory_stats();
assert_eq!(allocated, 0);
assert_eq!(total, 1024 * 1024);
}
#[test]
fn test_gpu_profiler() {
let mut profiler = GpuProfiler::new();
profiler.start();
profiler.record_kernel("test_kernel".to_string(), 1.5);
profiler.record_memory_transfer("test_transfer".to_string(), 1024, 0.5);
let results = profiler.stop();
assert_eq!(results.kernel_times.len(), 1);
assert_eq!(results.memory_transfers.len(), 1);
assert_eq!(results.total_kernel_time, 1.5);
}
#[test]
fn test_gpu_tensor_ops() {
let config = GpuConfig::default();
let context = Arc::new(GpuContext::new(config).expect("operation should succeed"));
let ops = GpuTensorOps::new(context);
let a = array![[1.0, 2.0], [3.0, 4.0]];
let b = array![[5.0, 6.0], [7.0, 8.0]];
let result = ops
.elementwise_add(&a, &b)
.expect("operation should succeed");
let expected = array![[6.0, 8.0], [10.0, 12.0]];
assert_eq!(result, expected);
}
#[test]
fn test_gpu_tensor_ops_matmul_cpu_fallback() {
let config = GpuConfig::default();
let context = Arc::new(GpuContext::new(config).expect("operation should succeed"));
let ops = GpuTensorOps::new(context);
let a = array![[1.0, 2.0], [3.0, 4.0]];
let b = array![[5.0, 6.0], [7.0, 8.0]];
let result = ops.matmul(&a, &b).expect("operation should succeed");
let expected = a.dot(&b);
assert_eq!(result, expected);
}
#[test]
fn test_available_backends() {
let backends = detect_available_backends();
assert!(!backends.is_empty());
assert!(backends.contains(&GpuBackend::CpuFallback));
}
#[test]
fn test_gpu_ensemble_trainer() {
let config = GpuConfig::default();
let trainer = GpuEnsembleTrainer::new(config).expect("operation should succeed");
assert!(!trainer.is_gpu_available()); assert_eq!(
trainer.context().device_info().name,
"CPU Fallback (no GPU runtime)"
);
}
#[test]
fn test_gpu_ensemble_trainer_predict_ensemble_cpu_fallback() {
let config = GpuConfig::default();
let trainer = GpuEnsembleTrainer::new(config).expect("operation should succeed");
let models = vec![array![1.0, -1.0], array![0.5, 0.5]];
let x = array![[1.0, 0.0], [0.0, 1.0], [-1.0, -1.0]];
let predictions = trainer
.predict_ensemble(&models, &x)
.expect("operation should succeed");
assert_eq!(predictions.len(), 3);
}
}