use std::fmt;
use std::marker::PhantomData;
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::{
DistanceKernel, DistanceSlot, DistanceSource, KernelScalar, KernelSpec, ModelKernel,
ModelKernelParts, PointKernel, PointUse, SupplyViews, spec_slots,
};
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::IdentityInput;
use crate::transform::{TargetTransform, Transform, UnfittedTarget, UnfittedTransform};
use crate::workspace::{FitWorkspace, QueryWorkspace};
use crate::{PredictOptions, Prediction, PredictiveCovariance};
use super::shared::{ExactSupplied, Query};
use super::{ExactFit, Gpr, GprCore, LdltStore, LltStore, OnlineGpr, Policies, fit_buffers};
pub struct FittedGpr<O = Lbfgs, P: GpScalar = DoublePrecision, K: ModelKernel = KernelSpec> {
pub(super) core: GprCore<P, K>,
pub(super) optimizer: O,
pub(super) store: LltStore<P>,
pub(super) _kernel: PhantomData<K>,
}
impl<O, P, K> Clone for FittedGpr<O, P, K>
where
O: Clone,
P: GpScalar,
K: ModelKernel,
{
fn clone(&self) -> Self {
Self {
core: self.core.clone(),
optimizer: self.optimizer.clone(),
store: self.store.clone(),
_kernel: PhantomData,
}
}
}
pub(crate) struct TrainInput<'a> {
pub(crate) x: &'a [f64],
pub(crate) n_rows: usize,
pub(crate) n_cols: usize,
pub(crate) y: &'a [f64],
pub(crate) sources: Vec<DistanceSource<'a>>,
}
impl<'a> TrainInput<'a> {
pub(crate) fn points(x: &'a [f64], n_rows: usize, n_cols: usize, y: &'a [f64]) -> Self {
Self {
x,
n_rows,
n_cols,
y,
sources: Vec::new(),
}
}
fn validate(&self, points: bool) -> Result<(), GprError> {
if points {
return validate_training(self.x, self.n_rows, self.n_cols, self.y);
}
crate::data::require_nonempty(self.n_rows)?;
crate::data::require_count(self.x.len(), 0, "feature values")?;
crate::data::require_count(self.y.len(), self.n_rows, "targets")?;
crate::data::require_finite(self.y)
}
}
impl<O, P, K> fmt::Debug for FittedGpr<O, P, K>
where
O: fmt::Debug,
P: GpScalar,
K: ModelKernel,
{
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, K> FittedGpr<O, P, K>
where
P: GpScalar,
K: ModelKernel,
{
#[allow(clippy::result_large_err)] pub(crate) fn prepare(
gpr: Gpr<O, P, K>,
input: TrainInput<'_>,
) -> Result<Self, (Gpr<O, P, K>, GprError)> {
if let Err(err) = input.validate(<K as ModelKernelParts>::POINTS) {
return Err((gpr, err));
}
let TrainInput {
x,
n_rows,
n_cols,
y,
sources,
} = input;
let supplied = <K::Supply as SupplyViews>::try_hold(|| {
let slots = spec_slots(&gpr.kernel);
let bound = <P::Sources as crate::kernel::SourceStore<P::Storage>>::bind(
&slots, sources, n_rows,
)?;
Ok(ExactSupplied::new(bound, slots))
});
let supplied = match supplied {
Ok(supplied) => supplied,
Err(err) => return Err((gpr, err)),
};
let mut x_buf = x.to_vec();
let x_fitted: Box<dyn Transform> = if n_cols == 0 {
Box::new(IdentityInput)
} else {
match gpr.x_transform.clone_box().fit(&x_buf, n_rows, n_cols) {
Ok(t) => t,
Err(err) => return Err((gpr, err)),
}
};
if n_cols > 0
&& 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 {
supplied,
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),
_kernel: PhantomData,
})
}
pub fn into_trainer(self) -> Gpr<O, P, K> {
self.core.into_trainer(self.optimizer)
}
pub fn n(&self) -> usize {
self.core.n
}
pub fn likelihood(&self) -> &GaussianLikelihood {
&self.core.likelihood
}
pub fn alpha(&self) -> &[P::Refine] {
&self.core.alpha
}
pub fn y(&self) -> &[f64] {
&self.core.y_obs
}
pub fn with_optimizer<O2>(self, optimizer: O2) -> FittedGpr<O2, P, K> {
FittedGpr {
core: self.core,
optimizer,
store: self.store,
_kernel: PhantomData,
}
}
pub(crate) fn factor_jitter(&self) -> f64 {
self.store.buffers.core().factor_jitter
}
pub(crate) fn core(&self) -> &GprCore<P, K> {
&self.core
}
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, K> {
ExactFit {
core: &mut self.core,
store: &mut self.store,
}
}
pub(crate) fn predict_query_into(
&mut self,
q: Query<'_, P::Storage, K::Supply>,
options: PredictOptions,
out: &mut Prediction<P::Refine>,
) -> Result<(), GprError> {
let (l, thread_scratch) = self.store.l_and_thread_scratch();
self.core
.predict_with_into(StoredFactor::Llt(l), thread_scratch, q, options, out)
}
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, K> {
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 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: GpScalar, K: ModelKernel> FittedGpr<O, P, K> {
pub fn into_online(self) -> Result<OnlineGpr<O, P, K>, 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))
}
}
impl<O, P: GpScalar, K: ModelKernel> FittedGpr<O, P, K> {
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)
}
}
impl<O, P: GpScalar> FittedGpr<O, P> {
pub fn kernel(&self) -> &KernelSpec {
&self.core.kernel
}
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,
Query::points(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> {
self.predict_query_into(Query::points(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,
Query::points(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,
Query::points(xs, n_rows, n_cols),
options,
n_draws,
seed,
)
}
}
impl<O, P: GpScalar, K: PointKernel> FittedGpr<O, P, K> {
pub fn d(&self) -> usize {
self.core.d
}
pub fn x(&self) -> &[f64] {
&self.core.x_obs
}
}
impl<O, P: GpScalar, C: PointUse> FittedGpr<O, P, DistanceKernel<C>> {
pub fn to_kernel(&self) -> DistanceKernel<C> {
<DistanceKernel<C> as ModelKernelParts>::from_spec(self.core.kernel.clone())
}
pub fn slots(&self) -> Vec<DistanceSlot> {
spec_slots(&self.core.kernel)
}
}
impl<O, P, K> FittedGpr<O, P, K>
where
P: GpScalar,
K: ModelKernel,
O: for<'a> Optimizer<GprObjective<'a, P, K>>,
{
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: GpScalar, K: ModelKernel> FittedGpr<Fixed, P, K> {
pub fn refit(&mut self) -> Result<(), GprError> {
self.fit_view().refactor()
}
pub(crate) fn into_online_preserving_factor(self) -> Result<OnlineGpr<Fixed, P, K>, 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))
}
}
impl<P: GpScalar, K: ModelKernel> FittedGpr<Fixed, P, K> {
pub(crate) fn from_persisted(mut parts: PersistedModel<P, K>) -> Result<Self, GprError> {
let n = parts.y_obs.len();
if n == 0 {
return Err(GprError::EmptyInput);
}
if !parts.x_obs.len().is_multiple_of(n) {
return Err(persist::persist_err(
PersistErrorKind::Tensor,
"persisted x length is not n * d",
));
}
let d = parts.x_obs.len() / n;
if let Some(alpha) = &parts.alpha
&& alpha.len() != n
{
return Err(persist::persist_err(
PersistErrorKind::Tensor,
format!("alpha has {} values, expected n = {n}", alpha.len()),
));
}
let mut x_buf = parts.x_obs.clone();
if d > 0 {
parts.x_transform.apply(&mut x_buf, n, d)?;
}
let mut y_buf = parts.y_obs.clone();
parts.y_transform.transform(&mut y_buf)?;
crate::data::require_finite(&x_buf)?;
crate::data::require_finite(&y_buf)?;
let supplied = parts.supplied;
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 saved = parts.alpha.is_some();
let (factor_alpha, alpha) = match parts.alpha {
Some(alpha) => (
storage_alpha_from_saved::<P>(workspace.core().k_matrix.as_ref(), &y_buf, &alpha)?,
alpha,
),
None => (
vec![P::Storage::from_f64(0.0); n],
vec![P::Refine::from_f64(0.0); n],
),
};
let mut model = Self {
core: GprCore {
supplied,
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,
x_cast: P::Storage::empty_cols(),
y_cast: P::Storage::empty_rows(),
n,
d,
},
optimizer: Fixed,
store: LltStore::with_mapped(workspace, parts.mapped),
_kernel: PhantomData,
};
if !saved {
model.refit()?;
}
Ok(model)
}
}
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())
}