1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
//! GPU capabilities detection for optimal resource allocation.
//!
//! This module provides runtime detection of GPU capabilities to enable:
//! - Optimal batch size selection based on available VRAM
//! - Device type classification (Discrete, Integrated, Virtual, CPU)
//! - Intelligent resource allocation for embedding workloads
//!
//! ## Usage
//!
//! ```rust,ignore
//! use gllm::GpuCapabilities;
//!
//! // Detect GPU capabilities (cached after first call)
//! let caps = GpuCapabilities::detect();
//!
//! println!("GPU: {} ({:?})", caps.name, caps.gpu_type);
//! println!("VRAM: {}MB", caps.vram_mb);
//! println!("Recommended batch size: {}", caps.recommended_batch_size);
//! ```
use std::sync::OnceLock;
/// Cached GPU capabilities for the current system.
static GPU_CAPABILITIES: OnceLock<GpuCapabilities> = OnceLock::new();
/// GPU device type classification.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GpuType {
/// Dedicated GPU (NVIDIA, AMD discrete)
Discrete,
/// Integrated GPU (Intel UHD, AMD APU)
Integrated,
/// Virtual GPU (cloud instances)
Virtual,
/// CPU fallback (no GPU available)
Cpu,
/// Unknown GPU type
Unknown,
}
/// GPU capabilities detected from the system.
#[derive(Debug, Clone)]
pub struct GpuCapabilities {
/// GPU device type
pub gpu_type: GpuType,
/// GPU name (e.g., "NVIDIA GeForce RTX 3080")
pub name: String,
/// Estimated VRAM in MB (0 if unknown or CPU)
pub vram_mb: u64,
/// Recommended batch size based on capabilities
pub recommended_batch_size: usize,
/// Whether GPU is available and working
pub gpu_available: bool,
/// Backend type that will be used
pub backend_name: &'static str,
}
impl Default for GpuCapabilities {
fn default() -> Self {
Self {
gpu_type: GpuType::Cpu,
name: "CPU".to_string(),
vram_mb: 0,
recommended_batch_size: 4, // Conservative CPU default
gpu_available: false,
backend_name: "ndarray",
}
}
}
impl GpuCapabilities {
/// Detect GPU capabilities from the system.
///
/// Results are cached after first call for performance.
/// This function is thread-safe.
pub fn detect() -> &'static GpuCapabilities {
GPU_CAPABILITIES.get_or_init(|| {
let caps = detect_gpu_capabilities_impl();
log::info!(
"gllm: GPU detected: {} ({:?}, {}MB VRAM, batch_size={})",
caps.name,
caps.gpu_type,
caps.vram_mb,
caps.recommended_batch_size
);
caps
})
}
/// Get recommended batch size for embedding operations.
///
/// The batch size is calculated based on:
/// - Available VRAM (for GPU)
/// - Device type (discrete vs integrated)
/// - Model memory requirements (~500MB base + ~50MB per batch item)
pub fn recommended_batch_size(&self) -> usize {
self.recommended_batch_size
}
/// Check if GPU is available.
pub fn is_gpu_available(&self) -> bool {
self.gpu_available
}
/// Calculate batch size for a specific model's memory footprint.
///
/// # Arguments
/// * `base_memory_mb` - Model's base memory usage in MB
/// * `per_item_mb` - Memory per batch item in MB
/// * `headroom` - Safety headroom (0.0 - 1.0, e.g., 0.3 for 30%)
pub fn calculate_batch_size(
&self,
base_memory_mb: u64,
per_item_mb: u64,
headroom: f64,
) -> usize {
if self.vram_mb == 0 || self.gpu_type == GpuType::Cpu {
return 4; // Conservative CPU default
}
let usable_vram = (self.vram_mb as f64 * (1.0 - headroom.clamp(0.0, 0.9))) as u64;
let available_for_batch = usable_vram.saturating_sub(base_memory_mb);
let batch_size = if per_item_mb > 0 {
(available_for_batch / per_item_mb).max(1).min(128) as usize
} else {
self.recommended_batch_size
};
// Apply device-specific limits
match self.gpu_type {
GpuType::Integrated => batch_size.min(16), // Shared memory
GpuType::Discrete | GpuType::Virtual => batch_size.min(128),
GpuType::Cpu | GpuType::Unknown => batch_size.min(8),
}
}
}
/// Internal implementation of GPU detection.
fn detect_gpu_capabilities_impl() -> GpuCapabilities {
// Check if test mode is enabled (skip GPU detection)
if std::env::var("GLLM_TEST_MODE").is_ok() {
return GpuCapabilities::default();
}
// Try wgpu-based detection (only when wgpu-detect feature is enabled)
#[cfg(feature = "wgpu-detect")]
{
match detect_wgpu_basic() {
Ok(caps) => return caps,
Err(e) => {
log::debug!("gllm: GPU detection failed: {}", e);
}
}
}
#[cfg(not(feature = "wgpu-detect"))]
{
log::debug!("gllm: GPU detection disabled (wgpu-detect feature not enabled)");
}
log::debug!("gllm: Using CPU defaults");
GpuCapabilities::default()
}
/// Detect GPU capabilities using wgpu.
///
/// This function actually requests a wgpu Device to verify GPU availability.
/// This catches OOM errors that would otherwise panic during burn/cubecl initialization.
#[cfg(feature = "wgpu-detect")]
fn detect_wgpu_basic() -> Result<GpuCapabilities, String> {
use wgpu::{DeviceType, Instance, InstanceDescriptor};
// Create wgpu instance
let instance = Instance::new(&InstanceDescriptor::default());
// Request adapter (blocking)
let adapter_future = instance.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::HighPerformance,
compatible_surface: None,
force_fallback_adapter: false,
});
let adapter = pollster::block_on(adapter_future)
.map_err(|e| format!("No GPU adapter found: {}", e))?;
let info = adapter.get_info();
// Determine GPU type
let gpu_type = match info.device_type {
DeviceType::DiscreteGpu => GpuType::Discrete,
DeviceType::IntegratedGpu => GpuType::Integrated,
DeviceType::VirtualGpu => GpuType::Virtual,
DeviceType::Cpu => GpuType::Cpu,
DeviceType::Other => GpuType::Unknown,
};
// CRITICAL: Actually request a device to verify GPU is usable
// This catches OOM errors that would otherwise panic in cubecl-wgpu's .unwrap()
// The panic happens in burn/cubecl initialization, which can't be caught with
// panic=abort in release builds.
let device_result = pollster::block_on(adapter.request_device(
&wgpu::DeviceDescriptor {
label: Some("gllm-gpu-probe"),
required_features: wgpu::Features::empty(),
required_limits: wgpu::Limits::default(),
memory_hints: wgpu::MemoryHints::MemoryUsage,
trace: wgpu::Trace::Off,
},
));
let gpu_available = match device_result {
Ok(_device) => {
log::debug!("gllm: GPU device probe successful for {}", info.name);
gpu_type != GpuType::Cpu
}
Err(e) => {
log::warn!("gllm: GPU device probe failed for {}: {} - will use CPU fallback",
info.name, e);
false
}
};
let vram_mb = 0;
let recommended_batch_size = if gpu_available {
match gpu_type {
GpuType::Integrated => 8,
GpuType::Discrete | GpuType::Virtual => 32,
GpuType::Unknown | GpuType::Cpu => 4,
}
} else {
4
};
let backend_name = if gpu_available {
"wgpu"
} else {
"ndarray"
};
Ok(GpuCapabilities {
gpu_type,
name: info.name.clone(),
vram_mb,
recommended_batch_size,
gpu_available,
backend_name,
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_gpu_capabilities_default() {
let caps = GpuCapabilities::default();
assert_eq!(caps.gpu_type, GpuType::Cpu);
assert!(!caps.gpu_available);
assert_eq!(caps.recommended_batch_size, 4);
}
#[test]
fn test_calculate_batch_size_discrete() {
let caps = GpuCapabilities {
gpu_type: GpuType::Discrete,
name: "Test GPU".to_string(),
vram_mb: 8192,
recommended_batch_size: 32,
gpu_available: true,
backend_name: "wgpu",
};
// Test with default embedding model footprint
let batch = caps.calculate_batch_size(500, 50, 0.3);
assert!(batch > 0 && batch <= 128);
}
#[test]
fn test_calculate_batch_size_cpu() {
let caps = GpuCapabilities::default();
let batch = caps.calculate_batch_size(500, 50, 0.3);
assert_eq!(batch, 4);
}
}