use std::fmt;
use dyn_stack::MemBuffer;
use faer::linalg::cholesky::llt;
use faer::{Mat, MatRef};
use crate::data::{pack_points, validate_training};
use crate::error::GprError;
use crate::error::PersistErrorKind;
use crate::gpr::GprObjective;
use crate::kernel::ScalarOps;
use crate::kernel::{KernelScalar, KernelSpec};
use crate::likelihood::GaussianLikelihood;
use crate::linalg::{faer_par_dims, solve_llt_in_place};
use crate::optimizer::{Fixed, Lbfgs, Optimizer};
use crate::persist::{self, PersistedModel};
use crate::precision::{DoublePrecision, GpScalar, StoredFactor};
use crate::transform::{TargetTransform, Transform, UnfittedTarget, UnfittedTransform};
use crate::workspace::{FitWorkspace, QueryWorkspace};
use crate::{PredictOptions, Prediction, PredictiveCovariance};
use super::{ExactFit, Gpr, GprCore, LdltStore, LltStore, OnlineGpr, Policies, fit_buffers};
pub struct FittedGpr<O = Lbfgs, P: GpScalar = DoublePrecision> {
pub(super) core: GprCore<P>,
pub(super) optimizer: O,
pub(super) store: LltStore<P>,
}
impl<O, P> Clone for FittedGpr<O, P>
where
O: Clone,
P: GpScalar,
{
fn clone(&self) -> Self {
Self {
core: self.core.clone(),
optimizer: self.optimizer.clone(),
store: self.store.clone(),
}
}
}
impl<O, P> fmt::Debug for FittedGpr<O, P>
where
O: fmt::Debug,
P: GpScalar,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("FittedGpr")
.field("n", &self.core.n)
.field("d", &self.core.d)
.field("kernel", &self.core.kernel)
.field("likelihood", &self.core.likelihood)
.field("optimizer", &self.optimizer)
.field("distance_cache", &self.core.policies.distance_cache)
.field("cholesky_buffer", &self.core.policies.cholesky_buffer)
.field("math", &self.core.policies.math)
.field("jitter_policy", &self.core.policies.jitter)
.finish_non_exhaustive()
}
}
impl<O, P> FittedGpr<O, P>
where
P: GpScalar,
{
#[allow(clippy::result_large_err, clippy::type_complexity)] #[allow(clippy::result_large_err)] pub(crate) fn prepare(
gpr: Gpr<O, P>,
x: &[f64],
n_rows: usize,
n_cols: usize,
y: &[f64],
) -> Result<Self, (Gpr<O, P>, GprError)> {
if let Err(err) = validate_training(x, n_rows, n_cols, y) {
return Err((gpr, err));
}
let mut x_buf = x.to_vec();
let x_fitted = match gpr.x_transform.clone_box().fit(&x_buf, n_rows, n_cols) {
Ok(t) => t,
Err(err) => return Err((gpr, err)),
};
if let Err(err) = x_fitted.apply(&mut x_buf, n_rows, n_cols) {
return Err((gpr, err));
}
let mut y_buf = y.to_vec();
let y_fitted = match gpr.y_transform.clone_box().fit(&y_buf) {
Ok(t) => t,
Err(err) => return Err((gpr, err)),
};
if let Err(err) = y_fitted.transform(&mut y_buf) {
return Err((gpr, err));
}
let compiled = gpr.kernel.compile_as::<P::Storage>();
let workspace = match fit_buffers::<P>(n_rows, gpr.policies, &compiled) {
Ok(ws) => ws,
Err(err) => return Err((gpr, err)),
};
Ok(Self {
core: GprCore {
kernel: gpr.kernel,
compiled,
likelihood: gpr.likelihood,
x_unfitted: gpr.x_transform,
y_unfitted: gpr.y_transform,
x_transform: x_fitted,
y_transform: y_fitted,
policies: gpr.policies,
query: QueryWorkspace::new(),
x_obs: x.to_vec(),
y_obs: y.to_vec(),
x: pack_points(&x_buf, n_rows, n_cols),
y_train: y_buf,
factor_alpha: vec![P::Storage::from_f64(0.0); n_rows],
alpha: vec![P::Refine::from_f64(0.0); n_rows],
x_cast: P::Storage::empty_cols(),
y_cast: P::Storage::empty_rows(),
n: n_rows,
d: n_cols,
},
optimizer: gpr.optimizer,
store: LltStore::new(workspace),
})
}
pub fn into_trainer(self) -> Gpr<O, P> {
self.core.into_trainer(self.optimizer)
}
pub fn into_online(self) -> Result<OnlineGpr<O, P>, GprError> {
let n = self.core.n;
let mut workspace = LdltStore::from_active(n)?;
workspace.fill_ld_from_llt(self.chol_l(), n)?;
workspace.factor_jitter = self.store.buffers.core().factor_jitter;
LdltStore::set_f64_prefix(&mut workspace.y, &self.core.y_train);
LdltStore::set_vector_prefix(&mut workspace.alpha, &self.core.factor_alpha);
Ok(OnlineGpr::from_core(self.core, self.optimizer, workspace))
}
pub fn n(&self) -> usize {
self.core.n
}
pub fn d(&self) -> usize {
self.core.d
}
pub fn kernel(&self) -> &KernelSpec {
&self.core.kernel
}
pub fn likelihood(&self) -> &GaussianLikelihood {
&self.core.likelihood
}
pub fn alpha(&self) -> &[P::Refine] {
&self.core.alpha
}
pub fn x(&self) -> &[f64] {
&self.core.x_obs
}
pub fn y(&self) -> &[f64] {
&self.core.y_obs
}
pub fn save(&self, dir: impl AsRef<std::path::Path>) -> Result<(), GprError> {
persist::save_fitted(self, dir.as_ref(), false)
}
pub fn save_with_factor(&self, dir: impl AsRef<std::path::Path>) -> Result<(), GprError> {
persist::save_fitted(self, dir.as_ref(), true)
}
pub fn with_optimizer<O2>(self, optimizer: O2) -> FittedGpr<O2, P> {
FittedGpr {
core: self.core,
optimizer,
store: self.store,
}
}
pub(crate) fn factor_jitter(&self) -> f64 {
self.store.buffers.core().factor_jitter
}
pub(crate) fn policies(&self) -> Policies {
self.core.policies
}
pub fn distance_cache_policy(&self) -> crate::DistanceCachePolicy {
self.core.policies.distance_cache
}
pub fn cholesky_buffer(&self) -> crate::CholeskyBuffer {
self.core.policies.cholesky_buffer
}
pub fn math(&self) -> crate::KernelExp {
self.core.policies.math
}
pub fn jitter_policy(&self) -> crate::JitterPolicy {
self.core.policies.jitter
}
pub(crate) fn x_unfitted(&self) -> &dyn UnfittedTransform {
self.core.x_unfitted.as_ref()
}
pub(crate) fn y_unfitted(&self) -> &dyn UnfittedTarget {
self.core.y_unfitted.as_ref()
}
pub(crate) fn x_transform(&self) -> &dyn Transform {
self.core.x_transform.as_ref()
}
pub(crate) fn y_transform(&self) -> &dyn TargetTransform {
self.core.y_transform.as_ref()
}
pub(crate) fn chol_l(&self) -> MatRef<'_, P::Storage> {
self.store.l()
}
pub(crate) fn factor(&self) -> StoredFactor<'_, P::Storage> {
StoredFactor::Llt(self.chol_l())
}
pub(crate) fn fit_view(&mut self) -> ExactFit<'_, P> {
ExactFit {
core: &mut self.core,
store: &mut self.store,
}
}
pub fn neg_log_marginal_likelihood(&self) -> Result<f64, GprError> {
Ok(self
.core
.neg_log_marginal_likelihood(self.factor(), &self.core.factor_alpha))
}
pub fn num_params(&self) -> usize {
self.core.num_params()
}
pub fn get_params(&self, out: &mut [f64]) -> Result<(), GprError> {
self.core.get_params(out)
}
pub fn set_params(&mut self, params: &[f64]) -> Result<(), GprError> {
self.fit_view().set_params(params)
}
#[cfg(test)]
pub(crate) fn objective(&mut self) -> GprObjective<'_, P> {
GprObjective::new(self.fit_view())
}
pub fn value_and_gradient_into(
&mut self,
params: &[f64],
out: &mut [f64],
) -> Result<f64, GprError> {
self.fit_view().value_and_gradient_into(params, out)
}
pub fn hessian_into(&mut self, params: &[f64], out: &mut [f64]) -> Result<(), GprError> {
self.fit_view().hessian_into(params, out)
}
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_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(
&self,
xs: &[f64],
n_rows: usize,
n_cols: usize,
options: PredictOptions,
) -> Result<Prediction<P::Refine>, GprError> {
let mut out = Prediction::default();
self.core.write_prediction(
self.factor(),
&self.core.alpha,
xs,
n_rows,
n_cols,
options,
&mut out,
)?;
Ok(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> {
let (l, thread_scratch) = self.store.l_and_thread_scratch();
let factor = StoredFactor::Llt(l);
self.core
.predict_with_into(factor, thread_scratch, xs, n_rows, n_cols, options, 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> {
self.core
.write_covariance(self.factor(), &self.core.alpha, 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.core.sample_with(
self.factor(),
&self.core.alpha,
xs,
n_rows,
n_cols,
options,
n_draws,
seed,
)
}
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> {
self.core
.loo_predict_with(self.factor(), &self.core.alpha, options)
}
}
impl<O, P> FittedGpr<O, P>
where
P: GpScalar,
O: for<'a> Optimizer<GprObjective<'a, P>>,
{
pub fn refit(&mut self) -> Result<(), GprError> {
let mut view = ExactFit {
core: &mut self.core,
store: &mut self.store,
};
view.optimize(&self.optimizer)
}
}
impl<P> FittedGpr<Fixed, P>
where
P: GpScalar,
{
pub(crate) fn into_online_preserving_factor(self) -> Result<OnlineGpr<Fixed, P>, GprError> {
let n = self.core.n;
let mut workspace = LdltStore::<P::Storage>::from_active(n)?;
workspace.copy_ld_from(self.chol_l(), n)?;
workspace.factor_jitter = self.store.buffers.core().factor_jitter;
LdltStore::set_f64_prefix(&mut workspace.y, &self.core.y_train);
LdltStore::set_vector_prefix(&mut workspace.alpha, &self.core.factor_alpha);
Ok(OnlineGpr::from_core(self.core, self.optimizer, workspace))
}
pub(crate) fn from_persisted(mut parts: PersistedModel<P>) -> Result<Self, GprError> {
let n = parts.y_obs.len();
if n == 0 {
return Err(GprError::EmptyInput);
}
if parts.x_obs.len() % n != 0 {
return Err(persist::persist_err(
PersistErrorKind::Tensor,
"persisted x length is not n * d",
));
}
let d = parts.x_obs.len() / n;
if parts.alpha.len() != n {
return Err(persist::persist_err(
PersistErrorKind::Tensor,
format!("alpha has {} values, expected n = {n}", parts.alpha.len()),
));
}
let mut x_buf = parts.x_obs.clone();
parts.x_transform.apply(&mut x_buf, n, d)?;
let mut y_buf = parts.y_obs.clone();
parts.y_transform.transform(&mut y_buf)?;
let compiled = parts.kernel.compile_as::<P::Storage>();
let mut workspace = fit_buffers::<P>(n, parts.policies, &compiled)?;
workspace.core_mut().factor_jitter = parts.factor_jitter;
if let Some(l) = parts.owned_l.take() {
let mut dest = workspace.core_mut().k_matrix.as_mut();
for col in 0..n {
for row in 0..n {
dest[(row, col)] = l[(row, col)];
}
}
}
let factor_alpha = storage_alpha_from_saved::<P>(
workspace.core().k_matrix.as_ref(),
&y_buf,
&parts.alpha,
)?;
Ok(Self {
core: GprCore {
kernel: parts.kernel,
compiled,
likelihood: parts.likelihood,
x_unfitted: parts.x_unfitted,
y_unfitted: parts.y_unfitted,
x_transform: parts.x_transform,
y_transform: parts.y_transform,
policies: parts.policies,
query: QueryWorkspace::new(),
x_obs: parts.x_obs,
y_obs: parts.y_obs,
x: pack_points(&x_buf, n, d),
y_train: y_buf,
factor_alpha,
alpha: parts.alpha,
x_cast: P::Storage::empty_cols(),
y_cast: P::Storage::empty_rows(),
n,
d,
},
optimizer: Fixed,
store: LltStore::with_mapped(workspace, parts.mapped),
})
}
pub fn refit(&mut self) -> Result<(), GprError> {
self.fit_view().refactor()
}
}
fn storage_alpha_from_saved<P: GpScalar>(
l: MatRef<'_, P::Storage>,
y: &[f64],
saved: &[P::Refine],
) -> Result<Vec<P::Storage>, GprError> {
let mixed = P::REFINES_IN_F64;
if !mixed {
return Ok(saved
.iter()
.map(|weight| P::Storage::from_f64(weight.to_f64()))
.collect());
}
let n = y.len();
let mut rhs = Mat::<P::Storage>::zeros(n, 1);
for (i, &yi) in y.iter().enumerate() {
rhs[(i, 0)] = P::Storage::from_f64(yi);
}
let par = faer_par_dims(n, 1);
let req = llt::solve::solve_in_place_scratch::<P::Storage>(n, 1, par);
let mut scratch = MemBuffer::new(req);
solve_llt_in_place(l, rhs.as_mut(), &mut scratch);
Ok((0..n).map(|i| rhs[(i, 0)]).collect())
}