use crate::error::Result; use candle_core::{Device, Tensor}; pub fn pick_device() -> Result { #[cfg(feature = "cuda")] if let Ok(d) = Device::new_cuda(0) { return Ok(d); } #[cfg(feature = "metal")] if let Ok(d) = Device::new_metal(0) { return Ok(d); } Ok(Device::Cpu) } /// Root-mean-square error between two equal-length f32 waveforms. pub fn rms(a: &[f32], b: &[f32]) -> f32 { debug_assert_eq!(a.len(), b.len()); let n = a.len() as f32; let sum_sq: f32 = a.iter().zip(b).map(|(x, y)| (x - y).powi(2)).sum(); (sum_sq / n).sqrt() } /// CSM EOT signal: every codebook in the frame is zero. Caller must gate this /// to frame_idx >= 1 to avoid spurious EOT on the first frame. pub fn all_zero_codebooks(frame: &Tensor) -> Result { // frame shape: (1, num_codebooks) i64 let v = frame.flatten_all()?.to_vec1::()?; Ok(v.iter().all(|x| *x == 0)) }