use std::marker::PhantomData;
use faer::Mat;
use crate::data::pack_points;
use crate::error::GprError;
use crate::error::PersistErrorKind;
use crate::kernel::ScalarOps;
use crate::kernel::{
CrossViews, DistanceKernel, DistanceOnly, DistanceSlot, DistanceSource, KernelScalar,
KernelSpec, ModelKernel, NoSupply, PointUse, QueryScratch, QuerySources, SuppliedSpec, Supply,
SupplyViews, WithPoints, column_into, new_inducing_column,
};
use crate::linalg::{chol_rank1_downdate, chol_rank1_update, solve_llt, solve_lower};
use crate::optimizer::{Lbfgs, Optimizer};
use crate::points::PointId;
use crate::points::{IdRegistry, PointRegistry, RegistryId};
use crate::policy::JitterPolicy;
use crate::policy::with_kernel_exp;
use crate::precision::{DoublePrecision, ModelPrecision};
use crate::prediction::{DistanceQuery, QueryPoints, distance_predict};
use crate::sgpr::SgprObjective;
use crate::sparse::{
KernelScratch, PredictScratch, SparseCore, SparseScratch, SparseSets, SparseSupply,
sparse_core_accessors, sparse_kernel_accessor, sparse_point_accessors,
};
use crate::{PredictOptions, Prediction, PredictiveCovariance};
use super::FixedInducing;
use super::factor::{
InducingBorder, PivotGate, VfeExact, VfeState, VfeSystem, a_times_y, append_point,
assemble_vfe, coordinate_border, f64_weights, inducing_append, inducing_delete, kernel_column,
kernel_diag_at, min_pivot_ratio, point_at, predict_vfe_covariance, predict_vfe_into,
publish_sgpr_weights, push_column, remove_column_in_place, remove_point, vfe_loo,
vfe_neg_log_marginal_likelihood,
};
use super::fitted::FittedSgpr;
mod distance;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct InducingId(u64);
impl RegistryId for InducingId {
const PERSIST_KEY: &'static str = "inducing_ids";
fn from_raw(raw: u64) -> Self {
Self(raw)
}
fn raw(self) -> u64 {
self.0
}
fn unknown() -> GprError {
GprError::InvalidInducingId
}
}
pub(crate) type InducingRegistry = IdRegistry<InducingId>;
pub(super) enum InducingChange<T> {
Insert {
border: InducingBorder<T>,
replace: Option<usize>,
},
Delete(usize),
}
#[derive(Clone, Debug)]
pub struct OnlineSgpr<O = Lbfgs, P: ModelPrecision = DoublePrecision, K: ModelKernel = KernelSpec> {
pub(super) state: OnlineState<P, K::Supply>,
pub(super) scratch: SparseScratch<P::Storage, K::Supply>,
optimizer: O,
_kernel: PhantomData<K>,
}
#[derive(Clone, Debug)]
pub(super) struct OnlineState<P: ModelPrecision, U: Supply = NoSupply> {
pub(super) core: SparseCore<U>,
k_mm_l: Mat<P::Storage>,
k_mm_l_f64: <P::Storage as ScalarOps>::F64Factor,
k_mm_jitter: f64,
k_mm_pivot_ratio: f64,
a: Mat<P::Storage>,
b_l: Mat<P::Storage>,
b_l_f64: <P::Storage as ScalarOps>::F64Factor,
w: Vec<P::Storage>,
predict_w: Vec<P::Refine>,
k_diag_sum: f64,
a_frobenius2: f64,
ay: Vec<f64>,
registry: PointRegistry,
inducing: InducingRegistry,
pub(super) inducing_points: <U as SupplyViews>::Held<Vec<PointId>>,
}
impl<P: ModelPrecision, U: Supply> OnlineState<P, U> {
fn start_exact(
&mut self,
exact: <P::Storage as ScalarOps>::F64Held<VfeExact>,
k_diag_sum: P::Storage,
a_frobenius2: P::Storage,
) {
match <P::Storage as ScalarOps>::held_f64(exact) {
Some(VfeExact {
b_l,
mut w,
k_diag_sum,
a_frobenius2,
}) => {
llt_times_in_place(b_l.as_ref(), &mut w);
self.ay = w;
self.k_diag_sum = k_diag_sum;
self.a_frobenius2 = a_frobenius2;
self.b_l_f64 = <P::Storage as ScalarOps>::f64_factor_from(b_l);
}
None => {
self.b_l_f64 = <P::Storage as ScalarOps>::hold_f64_factor(self.b_l.as_ref());
self.ay = a_times_y(self.a.as_ref(), &self.core.y_train);
self.k_diag_sum = k_diag_sum.to_f64();
self.a_frobenius2 = a_frobenius2.to_f64();
}
}
}
fn exact(&self) -> <P::Storage as ScalarOps>::F64Held<VfeExact> {
<P::Storage as ScalarOps>::hold_f64(|| {
let b_l = <P::Storage as ScalarOps>::f64_factor(self.b_l.as_ref(), &self.b_l_f64);
let mut w = Mat::<f64>::from_fn(self.ay.len(), 1, |row, _| self.ay[row]);
solve_llt(b_l, w.as_mut());
VfeExact {
b_l: b_l.to_owned(),
w: (0..self.ay.len()).map(|row| w[(row, 0)]).collect(),
k_diag_sum: self.k_diag_sum,
a_frobenius2: self.a_frobenius2,
}
})
}
}
fn llt_times_in_place(l: faer::MatRef<'_, f64>, w: &mut [f64]) {
let m = w.len();
let lt_w: Vec<f64> = (0..m)
.map(|col| (col..m).map(|row| l[(row, col)] * w[row]).sum())
.collect();
for (row, slot) in w.iter_mut().enumerate() {
*slot = (0..=row).map(|col| l[(row, col)] * lt_w[col]).sum();
}
}
fn inducing_points_of<S: Supply>(
core: &SparseCore<S>,
registry: &PointRegistry,
) -> Result<<S as SupplyViews>::Held<Vec<PointId>>, GprError> {
S::try_map_held(core.supply_held(), |supply| {
supply
.inducing
.iter()
.map(|&row| {
registry.ids().get(row).copied().ok_or_else(|| {
crate::persist::persist_err(
PersistErrorKind::Config,
format!(
"inducing index {row} is not one of the {} points",
registry.len()
),
)
})
})
.collect()
})
}
impl<O, P, K: ModelKernel> OnlineSgpr<O, P, K>
where
P: crate::precision::GpScalar,
{
pub(crate) fn core(&self) -> &SparseCore<K::Supply> {
&self.state.core
}
pub(crate) fn point_registry(&self) -> &PointRegistry {
&self.state.registry
}
pub(crate) fn inducing_registry(&self) -> &InducingRegistry {
&self.state.inducing
}
pub(crate) fn from_persisted<I>(
fitted: FittedSgpr<O, I, P, K>,
points: PointRegistry,
inducing: InducingRegistry,
) -> Result<Self, GprError> {
let mut online = Self::from_fitted(fitted);
if points.len() != online.state.core.n || inducing.len() != online.state.core.m {
return Err(crate::persist::persist_err(
PersistErrorKind::Config,
format!(
"config has {} point ids and {} inducing ids, expected n = {} and m = {}",
points.len(),
inducing.len(),
online.state.core.n,
online.state.core.m
),
));
}
online.state.inducing_points =
inducing_points_of::<K::Supply>(&online.state.core, &points)?;
online.state.registry = points;
online.state.inducing = inducing;
Ok(online)
}
pub(crate) fn from_fitted<I>(fitted: FittedSgpr<O, I, P, K>) -> Self {
let registry = PointRegistry::from_count(fitted.core.n);
let inducing = InducingRegistry::from_count(fitted.core.m);
let inducing_points =
<K::Supply as SupplyViews>::map_held(fitted.core.supply_held(), |supply| {
supply
.inducing
.iter()
.map(|&row| <PointId as RegistryId>::from_raw(row as u64))
.collect()
});
let mut state = OnlineState {
ay: Vec::new(),
core: fitted.core,
k_mm_pivot_ratio: min_pivot_ratio(fitted.k_mm_l.as_ref()),
k_mm_l: fitted.k_mm_l,
k_mm_l_f64: fitted.k_mm_l_f64,
k_mm_jitter: fitted.k_mm_jitter,
a: fitted.a,
b_l_f64: <P::Storage as ScalarOps>::f64_factor_from(Mat::new()),
b_l: fitted.b_l,
w: fitted.w,
predict_w: fitted.predict_w,
k_diag_sum: 0.0,
a_frobenius2: 0.0,
registry,
inducing,
inducing_points,
};
state.start_exact(fitted.exact, fitted.k_diag_sum, fitted.a_frobenius2);
Self {
state,
scratch: fitted.scratch,
optimizer: fitted.optimizer,
_kernel: PhantomData,
}
}
fn snapshot_fitted(&mut self) -> FittedSgpr<O, FixedInducing, P, K>
where
O: Clone,
{
let supplied = <K::Supply>::take_held(&mut self.state.core.supplied);
let mut core = self.state.core.clone();
core.supplied = supplied;
FittedSgpr {
core,
scratch: std::mem::take(&mut self.scratch),
optimizer: self.optimizer.clone(),
inducing: PhantomData,
_kernel: PhantomData,
k_mm_l: self.state.k_mm_l.clone(),
k_mm_l_f64: self.state.k_mm_l_f64.clone(),
k_mm_jitter: self.state.k_mm_jitter,
a: self.state.a.clone(),
b_l: self.state.b_l.clone(),
w: self.state.w.clone(),
predict_w: self.state.predict_w.clone(),
k_diag_sum: P::Storage::from_f64(self.state.k_diag_sum),
a_frobenius2: P::Storage::from_f64(self.state.a_frobenius2),
exact: self.state.exact(),
}
}
fn with_snapshot<R>(
&mut self,
call: impl FnOnce(&mut FittedSgpr<O, FixedInducing, P, K>) -> Result<R, GprError>,
) -> Result<R, GprError>
where
O: Clone,
{
let mut fitted = self.snapshot_fitted();
match call(&mut fitted) {
Ok(value) => {
self.adopt_fitted(fitted);
Ok(value)
}
Err(err) => {
self.state.core.supplied = fitted.core.supplied;
self.scratch = fitted.scratch;
Err(err)
}
}
}
fn adopt_fitted(&mut self, fitted: FittedSgpr<O, FixedInducing, P, K>) {
self.state.core = fitted.core;
self.scratch = fitted.scratch;
self.optimizer = fitted.optimizer;
self.state.k_mm_pivot_ratio = min_pivot_ratio(fitted.k_mm_l.as_ref());
self.state.k_mm_l = fitted.k_mm_l;
self.state.k_mm_l_f64 = fitted.k_mm_l_f64;
self.state.k_mm_jitter = fitted.k_mm_jitter;
self.state.a = fitted.a;
self.state.b_l = fitted.b_l;
self.state.w = fitted.w;
self.state.predict_w = fitted.predict_w;
self.state
.start_exact(fitted.exact, fitted.k_diag_sum, fitted.a_frobenius2);
}
pub(super) fn atomically<R>(
&mut self,
update: impl FnOnce(&mut Self) -> Result<R, GprError>,
) -> Result<R, GprError> {
if !P::REFINES_IN_F64 {
return update(self);
}
let supplied = <K::Supply>::take_held(&mut self.state.core.supplied);
let undo = self.state.clone();
self.state.core.supplied = supplied;
let result = update(self);
if result.is_err() {
let supplied = <K::Supply>::take_held(&mut self.state.core.supplied);
self.state = undo;
self.state.core.supplied = supplied;
}
result
}
fn vfe_state(&self) -> VfeState<P::Storage> {
VfeState::<P::Storage> {
k_mm_l: self.state.k_mm_l.clone(),
k_mm_l_f64: self.state.k_mm_l_f64.clone(),
k_mm_jitter: self.state.k_mm_jitter,
a: self.state.a.clone(),
b_l: self.state.b_l.clone(),
w: self.state.w.clone(),
k_diag_sum: P::Storage::from_f64(self.state.k_diag_sum),
a_frobenius2: P::Storage::from_f64(self.state.a_frobenius2),
exact: self.state.exact(),
}
}
fn apply_vfe(&mut self, state: VfeState<P::Storage>) -> Result<(), GprError> {
self.set_vfe(state);
self.refresh_predict_w()
}
fn set_vfe(&mut self, state: VfeState<P::Storage>) {
self.state.k_mm_pivot_ratio = min_pivot_ratio(state.k_mm_l.as_ref());
self.state.k_mm_l = state.k_mm_l;
self.state.k_mm_l_f64 = state.k_mm_l_f64;
self.state.k_mm_jitter = state.k_mm_jitter;
self.state.a = state.a;
self.state.b_l = state.b_l;
self.state.w = state.w;
self.state
.start_exact(state.exact, state.k_diag_sum, state.a_frobenius2);
}
fn refresh_predict_w(&mut self) -> Result<(), GprError> {
if P::REFINES_IN_F64 {
self.state.predict_w =
with_kernel_exp!(self.state.core.math, M => assemble_vfe::<M, f64, K::Supply>(
&self.state.core.kernel,
self.state.core.jitter,
self.state.core.likelihood,
self.state.core.data(),
&mut self.scratch.f64,
&mut KernelScratch::new(),
))?
.w
.into_iter()
.map(P::Refine::from_f64)
.collect();
return Ok(());
}
self.state.predict_w = with_kernel_exp!(self.state.core.math, M => publish_sgpr_weights::<M, P, K::Supply>(
&self.state.core.kernel,
self.state.core.jitter,
self.state.a.as_ref(),
self.state.b_l.as_ref(),
&self.state.w,
self.state.core.data(),
self.state.core.likelihood.noise_variance(),
))?;
Ok(())
}
fn recompute_w(&mut self) -> Result<(), GprError> {
let m = self.state.core.m;
let mut rhs = Mat::<f64>::zeros(m, 1);
for (row, value) in self.state.ay.iter().enumerate() {
rhs[(row, 0)] = *value;
}
let b_l =
<P::Storage as ScalarOps>::f64_factor(self.state.b_l.as_ref(), &self.state.b_l_f64);
solve_llt(b_l, rhs.as_mut());
self.state.w.clear();
self.state
.w
.extend((0..m).map(|row| P::Storage::from_f64(rhs[(row, 0)])));
self.refresh_predict_w()
}
sparse_core_accessors!(state.core);
pub fn point_ids(&self) -> &[PointId] {
self.state.registry.ids()
}
pub fn inducing_ids(&self) -> &[InducingId] {
self.state.inducing.ids()
}
pub fn num_params(&self) -> usize {
self.state.core.theta_len()
}
pub fn get_params(&self, out: &mut [f64]) -> Result<(), GprError> {
self.state.core.read_theta(out)
}
pub fn set_params(&mut self, params: &[f64]) -> Result<(), GprError>
where
O: Clone,
{
self.with_snapshot(|fitted| fitted.set_params(params))
}
pub fn value_and_gradient_into(
&mut self,
params: &[f64],
out: &mut [f64],
) -> Result<f64, GprError>
where
O: Clone,
{
self.with_snapshot(|fitted| fitted.value_and_gradient_into(params, out))
}
pub fn hessian_into(&mut self, params: &[f64], out: &mut [f64]) -> Result<(), GprError>
where
O: Clone,
{
self.with_snapshot(|fitted| fitted.hessian_into(params, out))
}
pub fn neg_log_marginal_likelihood(&self) -> Result<f64, GprError> {
vfe_neg_log_marginal_likelihood(
self.state.a.as_ref(),
self.state.b_l.as_ref(),
&self.state.w,
&self.state.core.y_train,
P::Storage::from_f64(self.state.k_diag_sum),
P::Storage::from_f64(self.state.a_frobenius2),
self.state.core.likelihood.noise_variance(),
self.state.core.n,
self.state.core.m,
)
}
pub fn loo_predict(&self) -> Result<Prediction<P::Refine>, GprError> {
self.loo_predict_with(PredictOptions::default())
}
pub fn loo_predict_with(
&self,
options: PredictOptions,
) -> Result<Prediction<P::Refine>, GprError> {
vfe_loo::<P, K::Supply>(
&self.state.core,
self.state.a.as_ref(),
self.state.b_l.as_ref(),
&self.state.w,
options,
)
}
pub fn delete(&mut self, id: PointId) -> Result<(), GprError> {
if self.state.core.n <= 1 {
return Err(GprError::InsufficientData {
n: self.state.core.n,
min: 2,
});
}
let idx = self.state.registry.index_of(id)?;
if let Some(at) = self
.state
.core
.supply()
.and_then(|supply| supply.inducing_at(idx))
{
return Err(GprError::InvalidConfig {
reason: format!(
"the point is inducing point {at}: delete it with delete_inducing first"
),
});
}
self.atomically(|model| model.delete_at(idx))
}
fn delete_at(&mut self, idx: usize) -> Result<(), GprError> {
let x_pt = point_at(
&self.state.core.x_train,
self.state.core.n,
self.state.core.d,
idx,
);
let v = if <P::Storage as ScalarOps>::ROUNDS_FROM_F64 {
self.exact_column(idx, &x_pt)?
} else {
(0..self.state.core.m)
.map(|i| self.state.a[(i, idx)].to_f64())
.collect()
};
let diag =
kernel_diag_at::<f64, K::Supply>(&self.state.core.kernel, &x_pt, self.state.core.d)?;
let col_norm: f64 = v.iter().map(|value| value * value).sum();
let x_next = remove_point(
&self.state.core.x_train,
self.state.core.n,
self.state.core.d,
idx,
);
let mut y_next = self.state.core.y_train.clone();
y_next.remove(idx);
let x_obs_next = remove_point(
&self.state.core.x_obs,
self.state.core.n,
self.state.core.d,
idx,
);
let mut y_obs_next = self.state.core.y_obs.clone();
y_obs_next.remove(idx);
let mut b_trial = <P::Storage as ScalarOps>::f64_factor_trial(
self.state.b_l.as_ref(),
&self.state.b_l_f64,
);
let mut v_trial = v.clone();
if self.updates_x_in_place() && chol_rank1_downdate(b_trial.as_mut(), &mut v_trial) {
self.state.k_diag_sum -= diag;
self.state.a_frobenius2 -= col_norm;
let y_idx = self.state.core.y_train[idx];
for (slot, value) in self.state.ay.iter_mut().zip(&v) {
*slot -= value * y_idx;
}
remove_column_in_place(&mut self.state.a, idx);
<P::Storage as ScalarOps>::commit_f64_factor(
&mut self.state.b_l,
&mut self.state.b_l_f64,
b_trial,
);
let mut saved = Vec::new();
if let Some(supply) = self.state.core.supply_mut() {
supply.remove_point(idx, P::REFINES_IN_F64.then_some(&mut saved));
}
self.state.core.x_train = x_next;
self.state.core.y_train = y_next;
self.state.core.x_obs = x_obs_next;
self.state.core.y_obs = y_obs_next;
self.state.core.n -= 1;
if let Err(err) = self.recompute_w() {
if let Some(supply) = self.state.core.supply_mut() {
supply.restore_point(idx, &saved);
}
return Err(err);
}
} else {
self.delete_by_reassembly(x_next, y_next, x_obs_next, y_obs_next, idx)?;
}
self.state.registry.remove_at(idx);
Ok(())
}
fn exact_column(&mut self, idx: usize, x_pt: &[f64]) -> Result<Vec<f64>, GprError> {
let core = &self.state.core;
let (m, d) = (core.m, core.d);
let compiled = core.kernel.compile_as::<f64>();
let z64 = pack_points(&core.z_train, m, d);
let x64 = pack_points(x_pt, 1, d);
let mut row = <K::Supply>::take_held(&mut self.scratch.row);
if let (Some(dst), Some(supply)) = (<K::Supply>::held_mut(&mut row), core.supply()) {
supply.exact().xz.rows_into(&[idx], &mut dst.xz);
}
let sets = SparseSets::<f64, K::Supply>::new(
x64.as_ref(),
z64.as_ref(),
<K::Supply as SupplyViews>::held_ref(&row),
);
let ks = &mut self.scratch.f64;
let col = with_kernel_exp!(core.math, M => ks.cross_mn::<M, K::Supply>(&compiled, sets));
self.scratch.row = row;
let mut col = col?;
solve_lower(
<P::Storage as ScalarOps>::f64_factor(
self.state.k_mm_l.as_ref(),
&self.state.k_mm_l_f64,
),
col.as_mut(),
);
Ok((0..m).map(|i| col[(i, 0)]).collect())
}
pub(super) fn delete_by_reassembly(
&mut self,
x_next: Vec<f64>,
y_next: Vec<f64>,
x_obs_next: Vec<f64>,
y_obs_next: Vec<f64>,
idx: usize,
) -> Result<(), GprError> {
let mut saved = Vec::new();
if let Some(supply) = self.state.core.supply_mut() {
supply.remove_point(idx, Some(&mut saved));
}
let result = self.reassemble_without(x_next, y_next, x_obs_next, y_obs_next);
if result.is_err()
&& let Some(supply) = self.state.core.supply_mut()
{
supply.restore_point(idx, &saved);
}
result
}
fn reassemble_without(
&mut self,
x_next: Vec<f64>,
y_next: Vec<f64>,
x_obs_next: Vec<f64>,
y_obs_next: Vec<f64>,
) -> Result<(), GprError> {
let state = with_kernel_exp!(self.state.core.math, M => assemble_vfe::<M, P::Storage, K::Supply>(
&self.state.core.kernel,
self.state.core.jitter,
self.state.core.likelihood,
crate::sparse::SparseData {
x: &x_next,
n: self.state.core.n - 1,
y: &y_next,
..self.state.core.data()
},
&mut self.scratch.storage,
&mut self.scratch.f64,
))?;
self.state.core.x_train = x_next;
self.state.core.y_train = y_next;
self.state.core.x_obs = x_obs_next;
self.state.core.y_obs = y_obs_next;
self.state.core.n -= 1;
self.commit_assembly(state)
}
fn updates_x_in_place(&self) -> bool {
PivotGate::XStep.holds(self.state.k_mm_pivot_ratio)
}
fn commit_assembly(&mut self, state: VfeState<P::Storage>) -> Result<(), GprError> {
match f64_weights(&state) {
Some(w64) if P::REFINES_IN_F64 => {
self.state.predict_w = w64.iter().copied().map(P::Refine::from_f64).collect();
self.set_vfe(state);
Ok(())
}
_ => self.apply_vfe(state),
}
}
fn updates_inducing_in_place(&self) -> bool {
!<P::Storage as ScalarOps>::ROUNDS_FROM_F64
&& self.state.k_mm_jitter <= 0.0
&& PivotGate::InducingStep.holds(self.state.k_mm_pivot_ratio)
}
fn commit_inducing(
&mut self,
change: impl FnOnce(&mut Self) -> Result<InducingChange<P::Storage>, GprError>,
z_train: Vec<f64>,
z_obs: Vec<f64>,
m: usize,
undo: impl FnOnce(&mut SparseSupply),
) -> Result<(), GprError> {
let result = if self.updates_inducing_in_place() {
change(self).and_then(|change| self.update_inducing(change, z_train, z_obs, m))
} else {
self.factor_inducing(z_train, z_obs, m)
};
if result.is_err()
&& let Some(supply) = self.state.core.supply_mut()
{
undo(supply);
}
result
}
fn update_inducing(
&mut self,
change: InducingChange<P::Storage>,
z_train: Vec<f64>,
z_obs: Vec<f64>,
m: usize,
) -> Result<(), GprError> {
let mut state = self.vfe_state();
let noise = self.state.core.likelihood.noise_variance();
let y = &self.state.core.y_train;
let updated = match change {
InducingChange::Insert { border, replace } => {
inducing_append(&mut state, border, replace, noise, y)
}
InducingChange::Delete(idx) => inducing_delete(&mut state, noise, y, idx),
};
match updated {
Ok(()) => {
self.state.core.z_train = z_train;
self.state.core.z_obs = z_obs;
self.state.core.m = m;
self.apply_vfe(state)
}
Err(GprError::CholeskyFailed { .. }) => self.factor_inducing(z_train, z_obs, m),
Err(err) => Err(err),
}
}
fn factor_inducing(
&mut self,
z_train: Vec<f64>,
z_obs: Vec<f64>,
m: usize,
) -> Result<(), GprError> {
let state = with_kernel_exp!(self.state.core.math, M => assemble_vfe::<M, P::Storage, K::Supply>(
&self.state.core.kernel,
self.state.core.jitter,
self.state.core.likelihood,
crate::sparse::SparseData {
z: &z_train,
m,
..self.state.core.data()
},
&mut self.scratch.storage,
&mut self.scratch.f64,
))?;
self.state.core.z_train = z_train;
self.state.core.z_obs = z_obs;
self.state.core.m = m;
self.commit_assembly(state)
}
pub fn delete_inducing(&mut self, id: InducingId) -> Result<(), GprError> {
if self.state.core.m <= 1 {
return Err(GprError::InsufficientData {
n: self.state.core.m,
min: 2,
});
}
let idx = self.state.inducing.index_of(id)?;
if <K::Supply as SupplyViews>::held(&self.state.inducing_points)
.is_some_and(|points| points.len() != self.state.core.m)
{
return Err(GprError::InvalidConfig {
reason:
"the model's inducing point ids do not match its inducing points (an internal \
error)"
.to_owned(),
});
}
self.atomically(|model| model.delete_inducing_at(idx))
}
fn delete_inducing_at(&mut self, idx: usize) -> Result<(), GprError> {
let (m, d) = (self.state.core.m, self.state.core.d);
let z_train = remove_point(&self.state.core.z_train, m, d, idx);
let z_obs = remove_point(&self.state.core.z_obs, m, d, idx);
let mut saved = Vec::new();
let point = self.state.core.supply_mut().and_then(|supply| {
let point = supply.inducing.get(idx).copied();
if point.is_some() {
supply.remove_inducing(idx, &mut saved);
}
point
});
self.commit_inducing(
|_| Ok(InducingChange::Delete(idx)),
z_train,
z_obs,
m - 1,
|supply| {
if let Some(point) = point {
supply.undo_remove_inducing(idx, point, &saved);
}
},
)?;
self.state.inducing.remove_at(idx);
if let Some(points) = <K::Supply as SupplyViews>::held_mut(&mut self.state.inducing_points)
&& idx < points.len()
{
points.remove(idx);
}
Ok(())
}
pub fn into_fitted(self) -> FittedSgpr<O, FixedInducing, P, K> {
let exact = self.state.exact();
FittedSgpr {
core: self.state.core,
scratch: self.scratch,
optimizer: self.optimizer,
inducing: PhantomData,
_kernel: PhantomData,
k_mm_l: self.state.k_mm_l,
k_mm_l_f64: self.state.k_mm_l_f64,
k_mm_jitter: self.state.k_mm_jitter,
a: self.state.a,
b_l: self.state.b_l,
w: self.state.w,
predict_w: self.state.predict_w,
k_diag_sum: P::Storage::from_f64(self.state.k_diag_sum),
a_frobenius2: P::Storage::from_f64(self.state.a_frobenius2),
exact,
}
}
}
impl<O, P, K> OnlineSgpr<O, P, K>
where
P: crate::precision::GpScalar,
K: crate::kernel::PointKernel,
{
sparse_point_accessors!(state.core);
}
impl<O, P> OnlineSgpr<O, P>
where
P: crate::precision::GpScalar,
{
sparse_kernel_accessor!(state.core);
pub fn predict(
&self,
xs: &[f64],
n_rows: usize,
n_cols: usize,
) -> Result<Prediction<P::Refine>, GprError> {
self.predict_with(xs, n_rows, n_cols, PredictOptions::default())
}
pub fn predict_with(
&self,
xs: &[f64],
n_rows: usize,
n_cols: usize,
options: PredictOptions,
) -> Result<Prediction<P::Refine>, GprError> {
let mut out = Prediction::default();
predict_vfe_into::<P, NoSupply>(
&self.state.core,
&VfeSystem::new(
&self.state.core,
self.state.k_mm_l.as_ref(),
self.state.b_l.as_ref(),
&self.state.predict_w,
),
xs,
n_rows,
n_cols,
std::iter::empty(),
options,
&mut PredictScratch::default(),
&mut out,
)?;
Ok(out)
}
pub fn predict_covariance(
&self,
xs: &[f64],
n_rows: usize,
n_cols: usize,
) -> Result<PredictiveCovariance<P::Refine>, GprError> {
self.predict_covariance_with(xs, n_rows, n_cols, PredictOptions::default())
}
pub fn predict_covariance_with(
&self,
xs: &[f64],
n_rows: usize,
n_cols: usize,
options: PredictOptions,
) -> Result<PredictiveCovariance<P::Refine>, GprError> {
predict_vfe_covariance::<P, NoSupply>(
&self.state.core,
&VfeSystem::new(
&self.state.core,
self.state.k_mm_l.as_ref(),
self.state.b_l.as_ref(),
&self.state.predict_w,
),
xs,
n_rows,
n_cols,
std::iter::empty(),
std::iter::empty(),
options,
)
}
pub fn sample(
&self,
xs: &[f64],
n_rows: usize,
n_cols: usize,
n_draws: usize,
seed: u64,
) -> Result<Vec<P::Refine>, GprError> {
self.sample_with(xs, n_rows, n_cols, PredictOptions::default(), n_draws, seed)
}
pub fn sample_with(
&self,
xs: &[f64],
n_rows: usize,
n_cols: usize,
options: PredictOptions,
n_draws: usize,
seed: u64,
) -> Result<Vec<P::Refine>, GprError> {
self.predict_covariance_with(xs, n_rows, n_cols, options)?
.draw(n_draws, seed, self.state.core.jitter)
}
pub fn predict_into(
&mut self,
xs: &[f64],
n_rows: usize,
n_cols: usize,
out: &mut Prediction<P::Refine>,
) -> Result<(), GprError> {
self.predict_with_into(xs, n_rows, n_cols, PredictOptions::default(), out)
}
pub fn predict_with_into(
&mut self,
xs: &[f64],
n_rows: usize,
n_cols: usize,
options: PredictOptions,
out: &mut Prediction<P::Refine>,
) -> Result<(), GprError> {
predict_vfe_into::<P, NoSupply>(
&self.state.core,
&VfeSystem::new(
&self.state.core,
self.state.k_mm_l.as_ref(),
self.state.b_l.as_ref(),
&self.state.predict_w,
),
xs,
n_rows,
n_cols,
std::iter::empty(),
options,
&mut self.scratch.predict,
out,
)
}
pub fn insert(&mut self, x_new: &[f64], y_new: f64) -> Result<PointId, GprError> {
if x_new.len() != self.state.core.d {
return Err(GprError::DimensionMismatch {
x_dim: x_new.len(),
expected_dim: self.state.core.d,
});
}
if self.state.core.d == 0 {
return Err(GprError::EmptyInput);
}
if x_new.iter().any(|v| !v.is_finite()) || !y_new.is_finite() {
return Err(GprError::NonFiniteInput);
}
self.state.registry.require_room()?;
let mut mapped = std::mem::take(&mut self.scratch.point);
let result = self.atomically(|model| model.insert_mapped(x_new, y_new, &mut mapped));
self.scratch.point = mapped;
result
}
fn insert_mapped(
&mut self,
x_obs: &[f64],
y_obs: f64,
mapped: &mut Vec<f64>,
) -> Result<PointId, GprError> {
self.state.core.map_point(x_obs, mapped)?;
let x_new = mapped.as_slice();
let y_new = self.state.core.map_target(y_obs)?;
let a_col = with_kernel_exp!(self.state.core.math, M => kernel_column::<M, f64>(
&self.state.core.kernel,
&self.state.core.z_train,
self.state.core.m,
x_new,
self.state.core.d,
&mut self.scratch.f64,
))?;
let k_diag =
kernel_diag_at::<f64, NoSupply>(&self.state.core.kernel, x_new, self.state.core.d)?;
let point = NewPoint {
x: x_new,
x_obs,
y: y_new,
y_obs,
};
self.append_point(a_col, k_diag, point, |_| {})
}
}
impl<O, P, K: ModelKernel> OnlineSgpr<O, P, K>
where
P: crate::precision::GpScalar,
{
pub(super) fn append_point(
&mut self,
mut a_col: Mat<f64>,
k_diag: f64,
point: NewPoint<'_>,
supply: impl FnOnce(&mut SparseSupply),
) -> Result<PointId, GprError> {
if !self.updates_x_in_place() {
return self.append_point_by_reassembly(point, supply);
}
let NewPoint {
x: x_new,
x_obs,
y: y_new,
y_obs,
} = point;
solve_lower(
<P::Storage as ScalarOps>::f64_factor(
self.state.k_mm_l.as_ref(),
&self.state.k_mm_l_f64,
),
a_col.as_mut(),
);
let mut v: Vec<f64> = (0..self.state.core.m).map(|i| a_col[(i, 0)]).collect();
self.state.a_frobenius2 += v.iter().map(|value| value * value).sum::<f64>();
self.state.k_diag_sum += k_diag;
for (slot, value) in self.state.ay.iter_mut().zip(&v) {
*slot += value * y_new;
}
push_column(&mut self.state.a, &v);
chol_rank1_update(
<P::Storage as ScalarOps>::f64_factor_mut(&mut self.state.b_l, &mut self.state.b_l_f64),
&mut v,
);
<P::Storage as ScalarOps>::store_f64_factor(&mut self.state.b_l, &self.state.b_l_f64);
append_point(
&mut self.state.core.x_train,
self.state.core.n,
self.state.core.d,
x_new,
);
append_point(
&mut self.state.core.x_obs,
self.state.core.n,
self.state.core.d,
x_obs,
);
self.state.core.y_train.push(y_new);
self.state.core.y_obs.push(y_obs);
if let Some(store) = self.state.core.supply_mut() {
supply(store);
}
self.state.core.n += 1;
if let Err(err) = self.recompute_w() {
if let Some(store) = self.state.core.supply_mut() {
store.pop_point();
}
return Err(err);
}
Ok(self.state.registry.insert())
}
fn append_point_by_reassembly(
&mut self,
point: NewPoint<'_>,
supply: impl FnOnce(&mut SparseSupply),
) -> Result<PointId, GprError> {
let NewPoint {
x: x_new,
x_obs,
y: y_new,
y_obs,
} = point;
let (n, d) = (self.state.core.n, self.state.core.d);
let mut x_next = self.state.core.x_train.clone();
append_point(&mut x_next, n, d, x_new);
let mut y_next = self.state.core.y_train.clone();
y_next.push(y_new);
if let Some(store) = self.state.core.supply_mut() {
supply(store);
}
let assembled = with_kernel_exp!(self.state.core.math, M => assemble_vfe::<M, P::Storage, K::Supply>(
&self.state.core.kernel,
self.state.core.jitter,
self.state.core.likelihood,
crate::sparse::SparseData {
x: &x_next,
n: n + 1,
y: &y_next,
..self.state.core.data()
},
&mut self.scratch.storage,
&mut self.scratch.f64,
));
let state = match assembled {
Ok(assembled) => assembled,
Err(err) => {
if let Some(store) = self.state.core.supply_mut() {
store.pop_point();
}
return Err(err);
}
};
self.state.core.x_train = x_next;
self.state.core.y_train = y_next;
append_point(&mut self.state.core.x_obs, n, d, x_obs);
self.state.core.y_obs.push(y_obs);
self.state.core.n += 1;
self.commit_assembly(state)?;
Ok(self.state.registry.insert())
}
}
impl<O, P> OnlineSgpr<O, P>
where
P: crate::precision::GpScalar,
{
pub fn insert_inducing(&mut self, z_new: &[f64]) -> Result<InducingId, GprError> {
if z_new.len() != self.state.core.d {
return Err(GprError::DimensionMismatch {
x_dim: z_new.len(),
expected_dim: self.state.core.d,
});
}
if self.state.core.d == 0 {
return Err(GprError::EmptyInput);
}
if z_new.iter().any(|v| !v.is_finite()) {
return Err(GprError::NonFiniteInput);
}
self.state.inducing.require_room()?;
self.atomically(|model| model.insert_inducing_checked(z_new))
}
fn insert_inducing_checked(&mut self, z_new: &[f64]) -> Result<InducingId, GprError> {
let z_obs = z_new;
let mut z_new = Vec::with_capacity(z_obs.len());
self.state.core.map_point(z_obs, &mut z_new)?;
let z_new = z_new.as_slice();
let (n, m, d) = (self.state.core.n, self.state.core.m, self.state.core.d);
let mut z_train = self.state.core.z_train.clone();
append_point(&mut z_train, m, d, z_new);
let mut z_obs_next = self.state.core.z_obs.clone();
append_point(&mut z_obs_next, m, d, z_obs);
self.commit_inducing(
|model| {
let core = &model.state.core;
let border = with_kernel_exp!(core.math, M => coordinate_border::<M, _>(
&core.kernel,
&core.x_train,
n,
d,
&core.z_train,
m,
z_new,
&mut model.scratch.storage,
))?;
Ok(InducingChange::Insert {
border,
replace: None,
})
},
z_train,
z_obs_next,
m + 1,
|_| {},
)?;
Ok(self.state.inducing.insert())
}
pub fn save(&self, dir: impl AsRef<std::path::Path>) -> Result<(), GprError> {
crate::persist::save_online_sgpr(self, dir.as_ref())
}
}
impl<O, P, K: ModelKernel> OnlineSgpr<O, P, K>
where
P: crate::precision::GpScalar,
O: Clone + for<'a> Optimizer<SgprObjective<'a, O, FixedInducing, P, K>>,
{
pub fn refit(&mut self) -> Result<(), GprError> {
self.with_snapshot(FittedSgpr::optimize_hyperparameters)
}
}
#[cfg(test)]
mod tests;
pub(super) struct NewPoint<'a> {
x: &'a [f64],
x_obs: &'a [f64],
y: f64,
y_obs: f64,
}