#[allow(
unused_imports,
reason = "a split file takes its parent's imports whole; each uses some"
)]
use super::*;
#[derive(Clone, Debug, Default)]
pub(crate) struct SparseSupply {
pub(crate) inducing: Vec<usize>,
f64: SupplyAt<f64>,
f32: std::sync::OnceLock<Result<SupplyAt<f32>, GprError>>,
}
pub(crate) fn check_inducing(inducing: &[usize], n: usize) -> Result<(), GprError> {
for (at, &i) in inducing.iter().enumerate() {
if i >= n {
return Err(GprError::IndexOutOfRange {
reason: format!("inducing index {i} is not below the {n} training points"),
});
}
if inducing[..at].contains(&i) {
return Err(GprError::InvalidConfig {
reason: format!("inducing index {i} is listed twice"),
});
}
}
Ok(())
}
#[derive(Clone, Debug, Default)]
pub(crate) struct SupplyAt<T: KernelScalar> {
pub(crate) zz: BlockStore<T>,
pub(crate) xz: BlockStore<T>,
}
impl SparseSupply {
pub(crate) fn bind<'s, S: KernelScalar>(
slots: &[crate::kernel::DistanceSlot],
sources: impl IntoIterator<Item = crate::kernel::DistanceSource<'s>>,
n: usize,
inducing: &[usize],
) -> Result<Self, GprError> {
check_inducing(inducing, n)?;
let (zz, xz) = crate::kernel::bind_inducing(slots, sources, n, inducing)?;
Self::new::<S>(slots, inducing.to_vec(), zz, xz)
}
pub(super) fn new<S: KernelScalar>(
slots: &[crate::kernel::DistanceSlot],
inducing: Vec<usize>,
zz: BlockStore<f64>,
xz: BlockStore<f64>,
) -> Result<Self, GprError> {
xz.require_in_range::<S>(slots)?;
Ok(Self {
inducing,
f64: SupplyAt { zz, xz },
f32: std::sync::OnceLock::new(),
})
}
pub(crate) fn exact(&self) -> &SupplyAt<f64> {
&self.f64
}
pub(crate) fn inducing_at(&self, index: usize) -> Option<usize> {
self.inducing.iter().position(|&i| i == index)
}
fn copies_mut(&mut self) -> (&mut SupplyAt<f64>, Option<&mut SupplyAt<f32>>) {
if matches!(self.f32.get(), Some(Err(_))) {
self.f32 = std::sync::OnceLock::new();
}
let f32 = self.f32.get_mut().and_then(|made| made.as_mut().ok());
(&mut self.f64, f32)
}
fn rebuild_squares(&mut self) {
let inducing = std::mem::take(&mut self.inducing);
let (f64, f32) = self.copies_mut();
let SupplyAt { zz, xz } = f64;
xz.rows_into(&inducing, zz);
if let Some(SupplyAt { zz, xz }) = f32 {
xz.rows_into(&inducing, zz);
}
self.inducing = inducing;
}
pub(crate) fn reserve_point(&mut self) -> Result<(), GprError> {
let (f64, f32) = self.copies_mut();
f64.xz.reserve_row()?;
if let Some(f32) = f32 {
f32.xz.reserve_row()?;
}
Ok(())
}
pub(crate) fn push_point(&mut self, value: impl Fn(usize, usize) -> f64) {
let (f64, f32) = self.copies_mut();
f64.xz.push_row(&value);
if let Some(f32) = f32 {
f32.xz.push_row(|b, col| f32::from_f64(value(b, col)));
}
}
pub(crate) fn pop_point(&mut self) {
let (f64, f32) = self.copies_mut();
f64.xz.pop_row();
if let Some(f32) = f32 {
f32.xz.pop_row();
}
}
pub(crate) fn remove_point(&mut self, index: usize, saved: Option<&mut Vec<f64>>) {
debug_assert!(self.inducing_at(index).is_none(), "an inducing point");
if let Some(saved) = saved {
self.f64.xz.row_into(index, saved);
}
let (f64, f32) = self.copies_mut();
f64.xz.remove_row(index);
if let Some(f32) = f32 {
f32.xz.remove_row(index);
}
for i in &mut self.inducing {
if *i > index {
*i -= 1;
}
}
}
pub(crate) fn restore_point(&mut self, index: usize, saved: &[f64]) {
for i in &mut self.inducing {
if *i >= index {
*i += 1;
}
}
let (f64, f32) = self.copies_mut();
f64.xz.insert_row(index, saved);
if let Some(f32) = f32 {
f32.xz.insert_row(index, saved);
}
}
pub(crate) fn reserve_inducing(&mut self) -> Result<(), GprError> {
let (f64, f32) = self.copies_mut();
f64.xz.reserve_col()?;
if let Some(f32) = f32 {
f32.xz.reserve_col()?;
}
Ok(())
}
pub(crate) fn add_inducing(
&mut self,
point: usize,
value: impl Fn(usize, usize) -> f64,
mirror: impl Fn(usize, usize) -> f64,
saved: &mut Vec<f64>,
) {
let m = self.inducing.len();
saved.reserve(m * self.f64.xz.block_count());
self.f64.xz.row_into(point, saved);
let (f64, f32) = self.copies_mut();
f64.xz.set_row(point, m, &mirror);
f64.xz.push_col(&value);
if let Some(f32) = f32 {
f32.xz
.set_row(point, m, |b, col| f32::from_f64(mirror(b, col)));
f32.xz.push_col(|b, row| f32::from_f64(value(b, row)));
}
self.inducing.push(point);
self.rebuild_squares();
}
pub(crate) fn undo_add_inducing(&mut self, point: usize, saved: &[f64]) {
debug_assert_eq!(self.inducing.last(), Some(&point), "not the last added");
self.inducing.retain(|&i| i != point);
let m = self.inducing.len();
let (f64, f32) = self.copies_mut();
f64.xz.pop_col();
f64.xz.set_row(point, m, |b, col| saved[b * m + col]);
if let Some(f32) = f32 {
f32.xz.pop_col();
f32.xz
.set_row(point, m, |b, col| f32::from_f64(saved[b * m + col]));
}
self.rebuild_squares();
}
pub(crate) fn remove_inducing(&mut self, at: usize, saved: &mut Vec<f64>) {
saved.reserve(self.f64.xz.rows() * self.f64.xz.block_count());
let (f64, f32) = self.copies_mut();
f64.xz.remove_col(at, Some(saved));
if let Some(f32) = f32 {
f32.xz.remove_col(at, None);
}
self.inducing.remove(at);
self.rebuild_squares();
}
pub(crate) fn undo_remove_inducing(&mut self, at: usize, point: usize, saved: &[f64]) {
let (f64, f32) = self.copies_mut();
f64.xz.insert_col(at, saved);
if let Some(f32) = f32 {
f32.xz.insert_col(at, saved);
}
self.inducing.insert(at, point);
self.rebuild_squares();
}
pub(crate) fn at<T: KernelScalar>(&self) -> Result<&SupplyAt<T>, GprError> {
let f64: &dyn std::any::Any = &self.f64;
if let Some(at) = f64.downcast_ref() {
return Ok(at);
}
let f32 = self
.f32
.get_or_init(|| {
Ok(SupplyAt {
zz: self.f64.zz.cast()?,
xz: self.f64.xz.cast()?,
})
})
.as_ref()
.map_err(Clone::clone)?;
let f32: &dyn std::any::Any = f32;
f32.downcast_ref().ok_or_else(crate::kernel::unbound)
}
}