use crate::device::WgpuDevice;
use crate::device::driver_profile::GpuArch;
use crate::device::probe::F64BuiltinCapabilities;
use crate::device::vendor::{VENDOR_AMD, VENDOR_INTEL, VENDOR_NVIDIA};
pub const FHE_MIN_BUFFER_SIZE: u64 = 256 * 1024;
pub const HIGH_PERFORMANCE_MIN_INVOCATIONS: u32 = 1024;
const BYTES_PER_MB: u64 = 1024 * 1024;
pub const WORKGROUP_SIZE_1D: u32 = 256;
pub const WORKGROUP_SIZE_MEDIUM: u32 = 128;
pub const WORKGROUP_SIZE_COMPACT: u32 = 64;
pub const WORKGROUP_SIZE_2D: u32 = 16;
#[must_use]
pub fn workgroup_size_for_arch(arch: &GpuArch) -> u32 {
match arch {
GpuArch::Volta | GpuArch::Turing => 64,
GpuArch::Ampere | GpuArch::Ada | GpuArch::Blackwell => 256,
GpuArch::Rdna2 | GpuArch::Rdna3 => 64,
GpuArch::Cdna2 => 256,
GpuArch::IntelArc => 128,
GpuArch::AppleM => 64,
GpuArch::Software | GpuArch::Unknown => 64,
}
}
#[must_use]
pub fn workgroup_size_2d_for_arch(arch: &GpuArch) -> u32 {
match arch {
GpuArch::Ampere | GpuArch::Ada | GpuArch::Blackwell | GpuArch::Cdna2 => 16,
_ => 8,
}
}
#[must_use]
pub fn optimal_workgroup_size_arch(
arch: &GpuArch,
workload: WorkloadType,
max_invocations: u32,
) -> u32 {
let base = workgroup_size_for_arch(arch);
let size = match workload {
WorkloadType::ElementWise | WorkloadType::MatMul | WorkloadType::FHE => base,
WorkloadType::Reduction => base * 2,
WorkloadType::Convolution => base / 2,
};
size.min(max_invocations)
}
fn sanitize_max_buffer_size(reported: u64, device_name: &str) -> u64 {
const MAX_SANE_BUFFER: u64 = 64 * 1024 * 1024 * 1024;
const GB: u64 = 1024 * 1024 * 1024;
const VRAM_HEURISTICS: &[(&[&str], u64)] = &[
(&["H100", "H200", "B100", "B200", "A100 80GB"], 80 * GB),
(&["A100", "MI300", "MI250"], 40 * GB),
(&["RTX 40", "RTX 50"], 24 * GB),
(&["RTX 30"], 24 * GB),
(&["Titan V", "V100", "Titan RTX"], 16 * GB),
(&["RTX 20", "RTX A"], 12 * GB),
];
const VRAM_CAP_CONSERVATIVE: u64 = 8 * GB;
if reported <= MAX_SANE_BUFFER {
return reported;
}
let capped = VRAM_HEURISTICS
.iter()
.find(|(patterns, _)| patterns.iter().any(|p| device_name.contains(p)))
.map_or(VRAM_CAP_CONSERVATIVE, |&(_, cap)| cap);
tracing::warn!(
"Driver reported max_buffer_size={reported} (>64GB), capping to {capped} for {device_name}"
);
capped
}
#[derive(Debug, Clone)]
pub struct DeviceCapabilities {
pub device_name: String,
pub device_type: wgpu::DeviceType,
pub max_buffer_size: u64,
pub max_workgroup_size: (u32, u32, u32),
pub max_compute_workgroups: (u32, u32, u32),
pub max_compute_invocations_per_workgroup: u32,
pub max_storage_buffers_per_shader_stage: u32,
pub max_uniform_buffers_per_shader_stage: u32,
pub max_bind_groups: u32,
pub backend: wgpu::Backend,
pub vendor: u32,
pub gpu_dispatch_threshold_override: Option<usize>,
pub subgroup_min_size: u32,
pub subgroup_max_size: u32,
pub has_subgroups: bool,
pub f64_shaders: bool,
pub f64_shared_memory: bool,
pub f64_capabilities: Option<F64BuiltinCapabilities>,
}
impl DeviceCapabilities {
#[must_use]
pub fn from_device(device: &WgpuDevice) -> Self {
let limits = device.device().limits();
let adapter_info = device.adapter_info();
Self {
device_name: adapter_info.name.clone(),
device_type: adapter_info.device_type,
max_buffer_size: sanitize_max_buffer_size(limits.max_buffer_size, &adapter_info.name),
max_workgroup_size: (
limits.max_compute_workgroup_size_x,
limits.max_compute_workgroup_size_y,
limits.max_compute_workgroup_size_z,
),
max_compute_workgroups: (
limits.max_compute_workgroups_per_dimension,
limits.max_compute_workgroups_per_dimension,
limits.max_compute_workgroups_per_dimension,
),
max_compute_invocations_per_workgroup: limits.max_compute_invocations_per_workgroup,
max_storage_buffers_per_shader_stage: limits.max_storage_buffers_per_shader_stage,
max_uniform_buffers_per_shader_stage: limits.max_uniform_buffers_per_shader_stage,
max_bind_groups: limits.max_bind_groups,
backend: adapter_info.backend,
vendor: adapter_info.vendor,
gpu_dispatch_threshold_override: None,
subgroup_min_size: adapter_info.subgroup_min_size,
subgroup_max_size: adapter_info.subgroup_max_size,
has_subgroups: device.has_subgroups(),
f64_shaders: device.has_f64_shaders(),
f64_shared_memory: false,
f64_capabilities: crate::device::probe::cache::cached_f64_builtins(device),
}
}
#[must_use]
pub fn optimal_workgroup_size(&self, workload: WorkloadType) -> u32 {
match self.device_type {
wgpu::DeviceType::DiscreteGpu => match workload {
WorkloadType::ElementWise | WorkloadType::MatMul | WorkloadType::FHE => 256,
WorkloadType::Reduction => 256,
WorkloadType::Convolution => 128,
},
wgpu::DeviceType::IntegratedGpu => match workload {
WorkloadType::ElementWise => 128,
WorkloadType::MatMul => 64,
WorkloadType::Reduction => 128,
WorkloadType::FHE => 64,
WorkloadType::Convolution => 64,
},
wgpu::DeviceType::Cpu => match workload {
WorkloadType::ElementWise => 32,
WorkloadType::MatMul => 16,
WorkloadType::Reduction => 64,
WorkloadType::FHE => 32,
WorkloadType::Convolution => 16,
},
_ => match workload {
WorkloadType::ElementWise => 64,
WorkloadType::MatMul => 64,
WorkloadType::Reduction => 128,
WorkloadType::FHE => 64,
WorkloadType::Convolution => 32,
},
}
.min(self.max_compute_invocations_per_workgroup)
}
#[must_use]
pub fn optimal_workgroup_size_2d(&self, workload: WorkloadType) -> (u32, u32) {
let total = self.optimal_workgroup_size(workload);
let side = (total as f32).sqrt() as u32;
let x = side.min(self.max_workgroup_size.0);
let y = side.min(self.max_workgroup_size.1);
(x, y)
}
#[must_use]
pub fn optimal_workgroup_size_3d(&self, workload: WorkloadType) -> (u32, u32, u32) {
let total = self.optimal_workgroup_size(workload);
let side = (total as f32).cbrt() as u32;
let x = side.min(self.max_workgroup_size.0);
let y = side.min(self.max_workgroup_size.1);
let z = side.min(self.max_workgroup_size.2);
(x, y, z)
}
#[must_use]
pub fn dispatch_1d(&self, element_count: u32) -> u32 {
element_count.div_ceil(WORKGROUP_SIZE_1D)
}
#[must_use]
pub fn dispatch_2d(&self, width: u32, height: u32) -> (u32, u32) {
(
width.div_ceil(WORKGROUP_SIZE_2D),
height.div_ceil(WORKGROUP_SIZE_2D),
)
}
#[must_use]
pub fn max_allocation_size(&self) -> u64 {
self.max_buffer_size / 4 * 3
}
#[must_use]
pub fn supports_fhe(&self) -> bool {
self.max_buffer_size >= FHE_MIN_BUFFER_SIZE
}
#[must_use]
pub fn supports_large_matmul(&self, m: usize, n: usize, k: usize) -> bool {
let required_bytes = (m * k + k * n + m * n) * 4;
required_bytes as u64 <= self.max_allocation_size()
}
#[must_use]
pub fn optimal_matmul_tile_size(&self) -> u32 {
match self.device_type {
wgpu::DeviceType::DiscreteGpu => {
if self.max_compute_invocations_per_workgroup >= 1024 {
32
} else {
16
}
}
wgpu::DeviceType::IntegratedGpu => 16,
wgpu::DeviceType::Cpu => 8,
_ => 8,
}
}
#[must_use]
pub fn gpu_dispatch_threshold(&self) -> usize {
const DISCRETE_THRESHOLD: usize = 4_096;
const INTEGRATED_THRESHOLD: usize = 16_384;
const OTHER_THRESHOLD: usize = 8_192;
if let Some(t) = self.gpu_dispatch_threshold_override {
return t;
}
match self.device_type {
wgpu::DeviceType::DiscreteGpu => DISCRETE_THRESHOLD,
wgpu::DeviceType::IntegratedGpu => INTEGRATED_THRESHOLD,
wgpu::DeviceType::Cpu => usize::MAX,
_ => OTHER_THRESHOLD,
}
}
#[must_use]
pub fn with_gpu_dispatch_threshold(mut self, threshold: usize) -> Self {
self.gpu_dispatch_threshold_override = Some(threshold);
self
}
#[must_use]
pub fn vendor_name(&self) -> &'static str {
match self.vendor {
VENDOR_NVIDIA => "NVIDIA",
VENDOR_AMD => "AMD",
VENDOR_INTEL => "Intel",
0x13B5 => "ARM",
0x5143 => "Qualcomm",
0x1010 => "ImgTec",
_ => "Unknown",
}
}
#[must_use]
pub fn is_high_performance(&self) -> bool {
matches!(self.device_type, wgpu::DeviceType::DiscreteGpu)
&& self.max_compute_invocations_per_workgroup >= HIGH_PERFORMANCE_MIN_INVOCATIONS
}
#[must_use]
pub fn has_subgroup_info(&self) -> bool {
self.subgroup_min_size > 0
}
#[must_use]
pub fn preferred_subgroup_size(&self) -> Option<u32> {
if self.subgroup_max_size > 0 {
Some(self.subgroup_max_size)
} else {
None
}
}
#[must_use]
pub fn has_f64_shared_memory(&self) -> bool {
self.f64_shared_memory
}
#[must_use]
pub fn with_f64_capabilities(mut self, caps: F64BuiltinCapabilities) -> Self {
self.f64_capabilities = Some(caps);
self
}
#[must_use]
pub fn has_reliable_f64(&self) -> bool {
self.f64_capabilities
.map_or(self.f64_shaders, |c| c.basic_f64)
}
#[must_use]
pub fn needs_exp_f64_workaround(&self) -> bool {
self.f64_capabilities.is_some_and(|c| !c.exp)
}
#[must_use]
pub fn needs_log_f64_workaround(&self) -> bool {
self.f64_capabilities.is_some_and(|c| !c.log)
}
#[must_use]
pub fn needs_sin_f64_workaround(&self) -> bool {
self.f64_capabilities.is_some_and(|c| !c.sin)
}
#[must_use]
pub fn needs_cos_f64_workaround(&self) -> bool {
self.f64_capabilities.is_some_and(|c| !c.cos)
}
#[must_use]
pub fn needs_sqrt_f64_workaround(&self) -> bool {
self.f64_capabilities.is_some_and(|c| !c.sqrt)
}
#[must_use]
pub fn has_f64_transcendentals(&self) -> bool {
self.f64_capabilities
.map_or(self.f64_shaders, |c| c.has_f64_transcendentals())
}
#[must_use]
pub fn df64_transcendentals_safe(&self) -> bool {
self.f64_capabilities
.is_none_or(|c| c.df64_transcendentals_safe)
}
#[must_use]
pub fn fp64_strategy(&self) -> super::Fp64Strategy {
use super::Fp64Strategy;
if let Some(caps) = &self.f64_capabilities {
if !caps.basic_f64 {
return Fp64Strategy::Hybrid;
}
if caps.can_compile_f64() {
return Fp64Strategy::Native;
}
return Fp64Strategy::Hybrid;
}
if self.f64_shaders {
Fp64Strategy::Native
} else {
Fp64Strategy::Hybrid
}
}
#[must_use]
pub fn precision_routing(&self) -> super::super::driver_profile::PrecisionRoutingAdvice {
use super::super::driver_profile::PrecisionRoutingAdvice;
if let Some(caps) = &self.f64_capabilities {
if !caps.basic_f64 {
if self.f64_shaders {
return PrecisionRoutingAdvice::Df64Only;
}
return PrecisionRoutingAdvice::F32Only;
}
if caps.shared_mem_f64 {
return PrecisionRoutingAdvice::F64Native;
}
return PrecisionRoutingAdvice::F64NativeNoSharedMem;
}
if self.f64_shaders {
PrecisionRoutingAdvice::F64NativeNoSharedMem
} else {
PrecisionRoutingAdvice::F32Only
}
}
#[must_use]
pub fn eigensolve_workgroup_size(&self) -> u32 {
if self.subgroup_min_size > 0 {
self.subgroup_min_size
} else {
32
}
}
#[must_use]
pub fn optimal_eigensolve_strategy(&self) -> super::EigensolveStrategy {
use super::EigensolveStrategy;
match self.device_type {
wgpu::DeviceType::DiscreteGpu | wgpu::DeviceType::IntegratedGpu => {
let wg = self.eigensolve_workgroup_size();
if wg >= 64 {
EigensolveStrategy::WavePacked { wave_size: wg }
} else {
EigensolveStrategy::WarpPacked { wg_size: wg }
}
}
_ => EigensolveStrategy::Standard,
}
}
#[must_use]
pub fn max_safe_allocation_bytes(&self) -> Option<u64> {
Some(self.max_allocation_size())
}
#[must_use]
pub fn supports_f64_builtins(&self) -> bool {
if let Some(caps) = &self.f64_capabilities {
caps.can_compile_f64() && caps.exp && caps.log
} else {
self.f64_shaders
}
}
#[must_use]
pub fn has_df64_spir_v_poisoning(&self) -> bool {
!self.df64_transcendentals_safe()
}
#[must_use]
pub fn latency_model(&self) -> crate::device::latency::LatencyModel {
use crate::device::latency::LatencyModel;
use crate::device::vendor::{VENDOR_AMD, VENDOR_APPLE, VENDOR_NVIDIA};
match self.device_type {
wgpu::DeviceType::DiscreteGpu | wgpu::DeviceType::IntegratedGpu => match self.vendor {
VENDOR_NVIDIA => LatencyModel::Sm70,
VENDOR_AMD => LatencyModel::Rdna2,
VENDOR_APPLE => LatencyModel::AppleM,
_ => LatencyModel::Conservative,
},
_ => LatencyModel::Conservative,
}
}
pub fn check_allocation_safe(&self, total_bytes: u64) -> crate::error::Result<()> {
if let Some(limit) = self.max_safe_allocation_bytes()
&& total_bytes > limit
{
return Err(crate::error::BarracudaError::DeviceLimitExceeded {
message: format!(
"Estimated allocation {:.1} MB exceeds safe limit {:.1} MB",
total_bytes as f64 / 1e6,
limit as f64 / 1e6,
),
requested_bytes: total_bytes,
safe_limit_bytes: limit,
});
}
Ok(())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum WorkloadType {
ElementWise,
MatMul,
Reduction,
FHE,
Convolution,
}
mod display;