use std::marker::PhantomData;
use faer::Mat;
use crate::error::GprError;
use crate::error::PersistErrorKind;
use crate::kernel::KernelScalar;
use crate::linalg::{chol_rank1_downdate, chol_rank1_update, frobenius2, solve_llt};
use crate::optimizer::{Lbfgs, Optimizer};
use crate::points::PointId;
use crate::points::{IdRegistry, PointRegistry, RegistryId};
use crate::policy::with_kernel_exp;
use crate::precision::{DoublePrecision, ModelPrecision};
use crate::sgpr::SgprObjective;
use crate::sparse::{
KernelScratch, PredictScratch, SparseCore, SparseScratch, sparse_core_accessors,
};
use crate::{PredictOptions, Prediction, PredictiveCovariance};
use super::FixedInducing;
use super::factor::{
VfeState, VfeSystem, a_times_y, append_point, assemble_vfe, assemble_vfe_with_f64_w,
inducing_delete, inducing_insert, kernel_column, kernel_diag_at, point_at,
predict_vfe_covariance, predict_vfe_into, publish_sgpr_weights, push_column,
remove_column_in_place, remove_point, solve_lmm, vfe_loo, vfe_neg_log_marginal_likelihood,
};
use super::fitted::FittedSgpr;
#[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>;
#[derive(Clone, Debug)]
pub struct OnlineSgpr<O = Lbfgs, P: ModelPrecision = DoublePrecision> {
pub(super) state: OnlineState<P>,
pub(super) scratch: SparseScratch<P::Storage>,
optimizer: O,
}
#[derive(Clone, Debug)]
pub(super) struct OnlineState<P: ModelPrecision> {
pub(super) core: SparseCore,
k_mm_l: Mat<P::Storage>,
a: Mat<P::Storage>,
b_l: Mat<P::Storage>,
w: Vec<P::Storage>,
predict_w: Vec<P::Refine>,
k_diag_sum: P::Storage,
a_frobenius2: P::Storage,
ay: Vec<f64>,
registry: PointRegistry,
inducing: InducingRegistry,
}
impl<O, P> OnlineSgpr<O, P>
where
P: crate::precision::GpScalar,
{
pub(crate) fn from_fitted<I>(fitted: FittedSgpr<O, I, P>) -> Self {
let registry = PointRegistry::from_count(fitted.core.n);
let inducing = InducingRegistry::from_count(fitted.core.m);
let ay = a_times_y(fitted.a.as_ref(), &fitted.core.y_train);
Self {
state: OnlineState {
ay,
core: fitted.core,
k_mm_l: fitted.k_mm_l,
a: fitted.a,
b_l: fitted.b_l,
w: fitted.w,
predict_w: fitted.predict_w,
k_diag_sum: fitted.k_diag_sum,
a_frobenius2: fitted.a_frobenius2,
registry,
inducing,
},
scratch: fitted.scratch,
optimizer: fitted.optimizer,
}
}
fn snapshot_fitted(&mut self) -> FittedSgpr<O, FixedInducing, P>
where
O: Clone,
{
FittedSgpr {
core: self.state.core.clone(),
scratch: std::mem::take(&mut self.scratch),
optimizer: self.optimizer.clone(),
inducing: PhantomData,
k_mm_l: self.state.k_mm_l.clone(),
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: self.state.k_diag_sum,
a_frobenius2: self.state.a_frobenius2,
}
}
fn adopt_fitted(&mut self, fitted: FittedSgpr<O, FixedInducing, P>) {
self.state.core = fitted.core;
self.scratch = fitted.scratch;
self.optimizer = fitted.optimizer;
self.state.k_mm_l = fitted.k_mm_l;
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.k_diag_sum = fitted.k_diag_sum;
self.state.a_frobenius2 = fitted.a_frobenius2;
self.state.ay = a_times_y(self.state.a.as_ref(), &self.state.core.y_train);
}
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 undo = self.state.clone();
let result = update(self);
if result.is_err() {
self.state = undo;
}
result
}
fn vfe_state(&self) -> VfeState<P::Storage> {
VfeState::<P::Storage> {
k_mm_l: self.state.k_mm_l.clone(),
a: self.state.a.clone(),
b_l: self.state.b_l.clone(),
w: self.state.w.clone(),
k_diag_sum: self.state.k_diag_sum,
a_frobenius2: self.state.a_frobenius2,
}
}
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_l = state.k_mm_l;
self.state.a = state.a;
self.state.b_l = state.b_l;
self.state.w = state.w;
self.state.k_diag_sum = state.k_diag_sum;
self.state.a_frobenius2 = state.a_frobenius2;
self.state.ay = a_times_y(self.state.a.as_ref(), &self.state.core.y_train);
}
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>(
&self.state.core.kernel,
self.state.core.jitter,
self.state.core.likelihood,
&self.state.core.x_train,
self.state.core.n,
self.state.core.d,
&self.state.core.y_train,
&self.state.core.z_train,
self.state.core.m,
&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>(
&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.x_train,
&self.state.core.y_train,
&self.state.core.z_train,
self.state.core.likelihood.noise_variance(),
self.state.core.n,
self.state.core.m,
self.state.core.d,
))?;
Ok(())
}
fn recompute_w(&mut self) -> Result<(), GprError> {
let m = self.state.core.m;
let mut rhs = Mat::<P::Storage>::zeros(m, 1);
for (row, value) in self.state.ay.iter().enumerate() {
rhs[(row, 0)] = P::Storage::from_f64(*value);
}
solve_llt(self.state.b_l.as_ref(), rhs.as_mut());
self.state.w.clear();
self.state.w.extend((0..m).map(|row| 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,
{
let mut fitted = self.snapshot_fitted();
fitted.set_params(params)?;
self.adopt_fitted(fitted);
Ok(())
}
pub fn value_and_gradient_into(
&mut self,
params: &[f64],
out: &mut [f64],
) -> Result<f64, GprError>
where
O: Clone,
{
let mut fitted = self.snapshot_fitted();
let value = fitted.value_and_gradient_into(params, out)?;
self.adopt_fitted(fitted);
Ok(value)
}
pub fn hessian_into(&mut self, params: &[f64], out: &mut [f64]) -> Result<(), GprError>
where
O: Clone,
{
let mut fitted = self.snapshot_fitted();
fitted.hessian_into(params, out)?;
self.adopt_fitted(fitted);
Ok(())
}
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,
self.state.k_diag_sum,
self.state.a_frobenius2,
self.state.core.likelihood.noise_variance(),
self.state.core.n,
self.state.core.m,
)
}
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>(
&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,
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>(
&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,
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 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>(
&self.state.core,
self.state.a.as_ref(),
self.state.b_l.as_ref(),
&self.state.w,
options,
)
}
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>(
&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,
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 mut a_col = with_kernel_exp!(self.state.core.math, M => kernel_column::<M, P::Storage>(
&self.state.core.kernel,
&self.state.core.z_train,
self.state.core.m,
x_new,
self.state.core.d,
&mut self.scratch.storage,
))?;
solve_lmm(self.state.k_mm_l.as_ref(), a_col.as_mut());
let mut v = vec![P::Storage::from_f64(0.0); self.state.core.m];
for (i, slot) in v.iter_mut().enumerate() {
*slot = a_col[(i, 0)];
}
let k_diag =
kernel_diag_at::<P::Storage>(&self.state.core.kernel, x_new, self.state.core.d)?;
self.state.a_frobenius2 += frobenius2(a_col.as_ref());
self.state.k_diag_sum += k_diag;
for (row, slot) in self.state.ay.iter_mut().enumerate() {
*slot += a_col[(row, 0)].to_f64() * y_new;
}
push_column(&mut self.state.a, a_col.as_ref());
chol_rank1_update(&mut self.state.b_l, &mut v);
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);
self.state.core.n += 1;
self.recompute_w()?;
Ok(self.state.registry.insert())
}
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)?;
self.atomically(|model| model.delete_at(idx))
}
fn delete_at(&mut self, idx: usize) -> Result<(), GprError> {
let mut v = vec![P::Storage::from_f64(0.0); self.state.core.m];
for (i, slot) in v.iter_mut().enumerate() {
*slot = self.state.a[(i, idx)];
}
let x_pt = point_at(
&self.state.core.x_train,
self.state.core.n,
self.state.core.d,
idx,
);
let diag = kernel_diag_at::<P::Storage>(&self.state.core.kernel, &x_pt, self.state.core.d)?;
let mut col_norm = P::Storage::from_f64(0.0);
for value in &v {
col_norm += *value * *value;
}
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 = self.state.b_l.clone();
let mut v_trial = v;
if chol_rank1_downdate(&mut b_trial, &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 (row, slot) in self.state.ay.iter_mut().enumerate() {
*slot -= self.state.a[(row, idx)].to_f64() * y_idx;
}
remove_column_in_place(&mut self.state.a, idx);
self.state.b_l = b_trial;
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.recompute_w()?;
} else {
self.delete_by_reassembly(x_next, y_next, x_obs_next, y_obs_next)?;
}
self.state.registry.remove_at(idx);
Ok(())
}
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>,
) -> Result<(), GprError> {
let state = with_kernel_exp!(self.state.core.math, M => assemble_vfe::<M, P::Storage>(
&self.state.core.kernel,
self.state.core.jitter,
self.state.core.likelihood,
&x_next,
self.state.core.n - 1,
self.state.core.d,
&y_next,
&self.state.core.z_train,
self.state.core.m,
&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.apply_vfe(state)
}
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 mut state = self.vfe_state();
match with_kernel_exp!(self.state.core.math, M => inducing_insert::<M, _>(
&mut state,
&self.state.core.kernel,
self.state.core.likelihood.noise_variance(),
&self.state.core.x_train,
self.state.core.n,
self.state.core.d,
&self.state.core.y_train,
&self.state.core.z_train,
self.state.core.m,
z_new,
&mut self.scratch.storage,
)) {
Ok(()) | Err(GprError::CholeskyFailed { .. }) => {}
Err(err) => return Err(err),
}
let (m, d) = (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(z_train, z_obs_next, m + 1)?;
Ok(self.state.inducing.insert())
}
fn commit_inducing(
&mut self,
z_train: Vec<f64>,
z_obs: Vec<f64>,
m: usize,
) -> Result<(), GprError> {
let (state, w64) = with_kernel_exp!(self.state.core.math, M => assemble_vfe_with_f64_w::<M, P::Storage>(
&self.state.core.kernel,
self.state.core.jitter,
self.state.core.likelihood,
&self.state.core.x_train,
self.state.core.n,
self.state.core.d,
&self.state.core.y_train,
&z_train,
m,
&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;
match w64 {
Some(w64) if P::REFINES_IN_F64 => {
self.state.predict_w = w64.into_iter().map(P::Refine::from_f64).collect();
self.set_vfe(state);
Ok(())
}
_ => self.apply_vfe(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)?;
self.atomically(|model| model.delete_inducing_at(idx))
}
fn delete_inducing_at(&mut self, idx: usize) -> Result<(), GprError> {
let mut state = self.vfe_state();
inducing_delete(
&mut state,
self.state.core.likelihood.noise_variance(),
&self.state.core.y_train,
idx,
)?;
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);
self.commit_inducing(z_train, z_obs, m - 1)?;
self.state.inducing.remove_at(idx);
Ok(())
}
pub(crate) fn core(&self) -> &SparseCore {
&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>,
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.registry = points;
online.state.inducing = inducing;
Ok(online)
}
pub fn save(&self, dir: impl AsRef<std::path::Path>) -> Result<(), GprError> {
crate::persist::save_online_sgpr(self, dir.as_ref())
}
pub fn into_fitted(self) -> FittedSgpr<O, FixedInducing, P> {
FittedSgpr {
core: self.state.core,
scratch: self.scratch,
optimizer: self.optimizer,
inducing: PhantomData,
k_mm_l: self.state.k_mm_l,
a: self.state.a,
b_l: self.state.b_l,
w: self.state.w,
predict_w: self.state.predict_w,
k_diag_sum: self.state.k_diag_sum,
a_frobenius2: self.state.a_frobenius2,
}
}
}
impl<O, P> OnlineSgpr<O, P>
where
P: crate::precision::GpScalar,
O: Clone + for<'a> Optimizer<SgprObjective<'a, O, FixedInducing, P>>,
{
pub fn refit(&mut self) -> Result<(), GprError> {
let mut fitted = self.snapshot_fitted();
fitted.optimize_hyperparameters()?;
self.adopt_fitted(fitted);
Ok(())
}
}