use std::marker::PhantomData;
use faer::Mat;
use crate::kernel::{KernelSpec, ModelKernel, NoSupply, PointKernel};
use crate::error::GprError;
use crate::policy::with_kernel_exp;
use crate::sparse::{
PredictScratch, SparseCore, SparseScratch, sparse_core_accessors, sparse_kernel_accessor,
sparse_point_accessors,
};
use crate::precision::{DoublePrecision, GpScalar, ModelPrecision};
use crate::{PredictOptions, Prediction, PredictiveCovariance};
use super::factor::{
SvgpSystem, assemble_data_terms, assemble_kmm, assemble_svgp, pack_q, predict_svgp_covariance,
predict_svgp_into, q_param_len, svgp_neg_elbo, svgp_value_and_gradient, unpack_q,
};
#[derive(Clone, Debug)]
pub struct FittedSvgp<P: ModelPrecision = DoublePrecision, K: ModelKernel = KernelSpec> {
pub(super) core: SparseCore<K::Supply>,
pub(super) scratch: SparseScratch<P::Storage, K::Supply>,
pub(super) k_mm_l: Mat<P::Storage>,
pub(super) a: Mat<P::Storage>,
pub(super) q_mean: Vec<f64>,
pub(super) q_l: Mat<f64>,
pub(super) k_diag: Vec<P::Storage>,
pub(super) _kernel: PhantomData<K>,
}
impl<P, K: ModelKernel> FittedSvgp<P, K>
where
P: GpScalar,
{
sparse_core_accessors!();
pub(crate) fn core(&self) -> &SparseCore<K::Supply> {
&self.core
}
pub(crate) fn q(&self) -> (&[f64], faer::MatRef<'_, f64>) {
(&self.q_mean, self.q_l.as_ref())
}
pub fn num_params(&self) -> usize {
self.core.theta_len() + q_param_len(self.core.m)
}
pub fn get_params(&self, out: &mut [f64]) -> Result<(), GprError> {
crate::data::require_count(out.len(), self.num_params(), "parameters")?;
let n_theta = self.core.theta_len();
self.core.read_theta(&mut out[..n_theta])?;
pack_q(&self.q_mean, self.q_l.as_ref(), &mut out[n_theta..]);
Ok(())
}
fn same_stored_params(&self, params: &[f64]) -> Result<bool, GprError> {
let mut current = vec![0.0; params.len()];
self.get_params(&mut current)?;
Ok(current == params)
}
pub fn set_params(&mut self, params: &[f64]) -> Result<(), GprError> {
let n_theta = self.core.theta_len();
crate::data::require_count(params.len(), self.num_params(), "parameters")?;
if self.same_stored_params(params)? {
return Ok(());
}
let (kernel, likelihood) = self.core.stage_theta(¶ms[..n_theta])?;
let q = unpack_q(¶ms[n_theta..], self.core.m)?;
let state = with_kernel_exp!(self.core.math, M => assemble_svgp::<M, P::Storage, K::Supply>(
&kernel,
self.core.jitter,
self.core.data(),
Some(q),
&mut self.scratch.storage,
))?;
self.core.kernel = kernel;
self.core.likelihood = likelihood;
self.k_mm_l = state.k_mm_l;
self.a = state.a;
self.q_mean = state.q_mean;
self.q_l = state.q_l;
self.k_diag = state.k_diag;
Ok(())
}
pub(crate) fn set_params_light(&mut self, params: &[f64]) -> Result<(), GprError> {
let n_theta = self.core.theta_len();
crate::data::require_count(params.len(), self.num_params(), "parameters")?;
let (kernel, likelihood) = self.core.stage_theta(¶ms[..n_theta])?;
let (q_mean, q_l) = unpack_q(¶ms[n_theta..], self.core.m)?;
let k_mm_l = with_kernel_exp!(self.core.math, M => assemble_kmm::<M, P::Storage, K::Supply>(
&kernel,
self.core.jitter,
self.core.data(),
&mut self.scratch.storage,
))?;
self.core.kernel = kernel;
self.core.likelihood = likelihood;
self.k_mm_l = k_mm_l;
self.q_mean = q_mean;
self.q_l = q_l;
Ok(())
}
pub(crate) fn rebuild_data_terms(&mut self) -> Result<(), GprError> {
let (a, k_diag) = with_kernel_exp!(self.core.math, M => assemble_data_terms::<M, P::Storage, K::Supply>(
&self.core.kernel,
self.core.data(),
self.k_mm_l.as_ref(),
&mut self.scratch.storage,
))?;
self.a = a;
self.k_diag = k_diag;
Ok(())
}
pub fn neg_elbo(&self) -> Result<f64, GprError> {
Ok(svgp_neg_elbo(
self.a.as_ref(),
&self.q_mean,
self.q_l.as_ref(),
&self.core.y_train,
&self.k_diag,
self.core.likelihood.noise_variance(),
self.core.n,
self.core.m,
))
}
pub fn value_and_gradient_into(
&mut self,
params: &[f64],
out: &mut [f64],
) -> Result<f64, GprError> {
let n_params = self.num_params();
crate::data::require_count(params.len(), n_params, "parameters")?;
crate::data::require_count(out.len(), n_params, "parameters")?;
self.set_params(params)?;
let batch: Vec<usize> = (0..self.core.n).collect();
let mut scratch = std::mem::take(&mut self.scratch);
let result = with_kernel_exp!(
self.core.math,
M => svgp_value_and_gradient::<M, _, K>(self, out, &batch, &mut scratch)
);
self.scratch = scratch;
result
}
}
impl<P, K> FittedSvgp<P, K>
where
P: GpScalar,
K: PointKernel,
{
sparse_point_accessors!();
}
impl<P: GpScalar, K: ModelKernel> FittedSvgp<P, K> {
pub(crate) fn from_persisted(
core: SparseCore<K::Supply>,
q_mean: Vec<f64>,
q_l: Mat<f64>,
) -> Result<Self, GprError> {
with_kernel_exp!(core.math, M => super::factor::assemble_fitted::<M, P, K>(
core,
Some((q_mean, q_l))
))
}
}
impl<P> FittedSvgp<P>
where
P: GpScalar,
{
sparse_kernel_accessor!();
pub fn save(&self, dir: impl AsRef<std::path::Path>) -> Result<(), GprError> {
crate::persist::save_svgp(self, dir.as_ref())
}
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_svgp_into::<P, NoSupply>(
&self.core,
&SvgpSystem::new(
&self.core,
self.k_mm_l.as_ref(),
&self.q_mean,
self.q_l.as_ref(),
),
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_svgp_covariance::<P, NoSupply>(
&self.core,
&SvgpSystem::new(
&self.core,
self.k_mm_l.as_ref(),
&self.q_mean,
self.q_l.as_ref(),
),
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.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_svgp_into::<P, NoSupply>(
&self.core,
&SvgpSystem::new(
&self.core,
self.k_mm_l.as_ref(),
&self.q_mean,
self.q_l.as_ref(),
),
xs,
n_rows,
n_cols,
std::iter::empty(),
options,
&mut self.scratch.predict,
out,
)
}
}