use rlx_llama32::MetalGgufPrefillMode;
use rlx_runtime::Device;
#[derive(Debug, Clone)]
pub struct BackboneLoadOptions {
pub metal_prefill: MetalGgufPrefillMode,
pub use_fast_kv: Option<bool>,
pub memory_efficient: bool,
}
impl Default for BackboneLoadOptions {
fn default() -> Self {
Self::synthesis()
}
}
impl BackboneLoadOptions {
pub fn synthesis() -> Self {
Self {
metal_prefill: MetalGgufPrefillMode::CpuF32,
use_fast_kv: Some(true),
memory_efficient: true,
}
}
pub fn gpu(device: Device) -> Self {
Self {
metal_prefill: metal_prefill_for_device(device),
use_fast_kv: Some(true),
memory_efficient: true,
}
}
pub fn for_device(device: Device) -> Self {
if super::rlx::low_mem_mode() || matches!(device, Device::Cpu) {
Self::synthesis()
} else if matches!(
device,
Device::Metal | Device::Cuda | Device::Rocm | Device::Gpu | Device::Vulkan
) {
Self::gpu(device)
} else {
Self::synthesis()
}
}
pub fn reference_parity() -> Self {
Self {
metal_prefill: MetalGgufPrefillMode::CpuF32,
use_fast_kv: Some(true),
memory_efficient: false,
}
}
pub fn fast_prefill() -> Self {
Self {
metal_prefill: MetalGgufPrefillMode::PackedGguf,
use_fast_kv: Some(true),
memory_efficient: true,
}
}
pub fn with_memory_efficient(mut self, enabled: bool) -> Self {
self.memory_efficient = enabled;
self
}
pub fn with_metal_prefill(mut self, mode: MetalGgufPrefillMode) -> Self {
self.metal_prefill = mode;
self
}
pub fn with_fast_kv(mut self, enabled: bool) -> Self {
self.use_fast_kv = Some(enabled);
self
}
}
fn metal_prefill_for_device(device: Device) -> MetalGgufPrefillMode {
if let Ok(s) = std::env::var("ORPHEUS_METAL_PREFILL") {
if let Some(m) = MetalGgufPrefillMode::parse(&s) {
return m;
}
}
match device {
Device::Metal => MetalGgufPrefillMode::MetalF32,
Device::Gpu | Device::Vulkan => MetalGgufPrefillMode::CpuF32,
_ => MetalGgufPrefillMode::Auto,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn gpu_on_metal_uses_metal_f32_prefill() {
let opts = BackboneLoadOptions::gpu(Device::Metal);
assert_eq!(opts.metal_prefill, MetalGgufPrefillMode::MetalF32);
}
#[test]
fn for_device_wgpu_uses_cpu_gguf_path() {
let opts = BackboneLoadOptions::for_device(Device::Gpu);
assert_eq!(opts.metal_prefill, MetalGgufPrefillMode::CpuF32);
assert_eq!(opts.use_fast_kv, Some(true));
}
}