use faer::{MatMut, MatRef};
use std::fmt::{self, Debug};
use std::marker::PhantomData;
use crate::error::GprError;
use crate::kernel::KernelScalar;
use crate::param::Interval;
use super::Triangle;
pub trait KernelTerm<T: KernelScalar = f64>: Send + Sync + Debug + 'static {
fn num_params(&self) -> usize;
fn get_params(&self, out: &mut [f64]) -> Result<(), GprError>;
fn set_params(&mut self, params: &[f64]) -> Result<(), GprError>;
fn bounds_into(&self, out: &mut [Interval]) -> Result<(), GprError>;
fn apply(
&self,
dist: MatRef<'_, T>,
out: MatMut<'_, T>,
uplo: Triangle,
) -> Result<(), GprError>;
fn apply_cross(&self, dist: MatRef<'_, T>, out: MatMut<'_, T>) -> Result<(), GprError>;
fn fill_diag(&self, out: &mut [T]) -> Result<(), GprError>;
fn grad(
&self,
dist: MatRef<'_, T>,
d_k: MatMut<'_, T>,
param_idx: usize,
uplo: Triangle,
) -> Result<(), GprError>;
fn hess(
&self,
dist: MatRef<'_, T>,
d2_k: MatMut<'_, T>,
i: usize,
j: usize,
uplo: Triangle,
) -> Result<(), GprError>;
fn hess_points(
&self,
x: MatRef<'_, T>,
d2_k: MatMut<'_, T>,
i: usize,
j: usize,
uplo: Triangle,
) -> Result<(), GprError>;
fn grad_cross(
&self,
_dist: MatRef<'_, T>,
_d_k: MatMut<'_, T>,
_param_idx: usize,
) -> Result<(), GprError> {
Err(GprError::CoordGradientUnsupported)
}
fn hess_cross(
&self,
_dist: MatRef<'_, T>,
_d2_k: MatMut<'_, T>,
_i: usize,
_j: usize,
) -> Result<(), GprError> {
Err(GprError::CoordGradientUnsupported)
}
fn grad_wrt_sq_dist(&self, _dist: MatRef<'_, T>, _out: MatMut<'_, T>) -> Result<(), GprError> {
Err(GprError::CoordGradientUnsupported)
}
fn hess_wrt_sq_dist(&self, _dist: MatRef<'_, T>, _out: MatMut<'_, T>) -> Result<(), GprError> {
Err(GprError::CoordGradientUnsupported)
}
fn grad_wrt_sq_dist_theta(
&self,
_dist: MatRef<'_, T>,
_out: MatMut<'_, T>,
_param_idx: usize,
) -> Result<(), GprError> {
Err(GprError::CoordGradientUnsupported)
}
fn persist_id(&self) -> &'static str {
""
}
fn persist_state(&self) -> Result<serde_json::Value, GprError> {
Err(GprError::PersistFailed {
kind: crate::error::PersistErrorKind::NotPersistable,
reason: "this custom kernel does not implement persist_state".to_owned(),
})
}
}
trait DualLeaf: Send + Sync + Debug {
fn f64_term(&self) -> &dyn KernelTerm<f64>;
fn f64_term_mut(&mut self) -> &mut dyn KernelTerm<f64>;
fn f32_term(&self) -> &dyn KernelTerm<f32>;
fn clone_dual(&self) -> Box<dyn DualLeaf>;
fn type_label(&self) -> &'static str;
}
#[derive(Debug)]
struct Erased<K>(K);
impl<K> DualLeaf for Erased<K>
where
K: KernelTerm<f64> + KernelTerm<f32> + Clone + Debug + Send + Sync + 'static,
{
fn f64_term(&self) -> &dyn KernelTerm<f64> {
&self.0
}
fn f64_term_mut(&mut self) -> &mut dyn KernelTerm<f64> {
&mut self.0
}
fn f32_term(&self) -> &dyn KernelTerm<f32> {
&self.0
}
fn clone_dual(&self) -> Box<dyn DualLeaf> {
Box::new(Erased(self.0.clone()))
}
fn type_label(&self) -> &'static str {
std::any::type_name::<K>()
}
}
pub struct CustomKernel<T: KernelScalar = f64> {
inner: Box<dyn DualLeaf>,
_scalar: PhantomData<fn() -> T>,
}
impl CustomKernel<f64> {
pub fn new<K>(term: K) -> Self
where
K: KernelTerm<f64> + KernelTerm<f32> + Clone + Debug + Send + Sync + 'static,
{
Self {
inner: Box::new(Erased(term)),
_scalar: PhantomData,
}
}
pub(crate) fn with_scalar<T: KernelScalar>(&self) -> CustomKernel<T> {
CustomKernel {
inner: self.inner.clone_dual(),
_scalar: PhantomData,
}
}
}
impl<T: KernelScalar> CustomKernel<T> {
fn term(&self) -> &dyn KernelTerm<T> {
T::pick_term(self.inner.f64_term(), self.inner.f32_term())
}
fn params(&self) -> &dyn KernelTerm<f64> {
self.inner.f64_term()
}
pub(super) fn num_params(&self) -> usize {
self.params().num_params()
}
pub(crate) fn persist_id(&self) -> &'static str {
self.params().persist_id()
}
pub(crate) fn persist_state(&self) -> Result<serde_json::Value, GprError> {
self.params().persist_state()
}
pub(super) fn write_params(&self, out: &mut [f64], offset: &mut usize) -> Result<(), GprError> {
let n = self.num_params();
self.params().get_params(&mut out[*offset..*offset + n])?;
*offset += n;
Ok(())
}
pub(super) fn write_intervals(
&self,
out: &mut [Interval],
offset: &mut usize,
) -> Result<(), GprError> {
let n = self.num_params();
self.params().bounds_into(&mut out[*offset..*offset + n])?;
*offset += n;
Ok(())
}
pub(super) fn apply_params(
&mut self,
params: &[f64],
offset: &mut usize,
) -> Result<(), GprError> {
let n = self.num_params();
self.inner
.f64_term_mut()
.set_params(¶ms[*offset..*offset + n])?;
*offset += n;
Ok(())
}
pub(super) fn apply(
&self,
dist: MatRef<'_, T>,
out: MatMut<'_, T>,
uplo: Triangle,
) -> Result<(), GprError> {
self.term().apply(dist, out, uplo)
}
pub(super) fn apply_cross(
&self,
dist: MatRef<'_, T>,
out: MatMut<'_, T>,
) -> Result<(), GprError> {
self.term().apply_cross(dist, out)
}
pub(super) fn fill_diag(&self, out: &mut [T]) -> Result<(), GprError> {
self.term().fill_diag(out)
}
pub(super) fn grad(
&self,
dist: MatRef<'_, T>,
d_k: MatMut<'_, T>,
param_idx: usize,
uplo: Triangle,
) -> Result<(), GprError> {
self.term().grad(dist, d_k, param_idx, uplo)
}
pub(super) fn hess(
&self,
dist: MatRef<'_, T>,
d2_k: MatMut<'_, T>,
i: usize,
j: usize,
uplo: Triangle,
) -> Result<(), GprError> {
self.term().hess(dist, d2_k, i, j, uplo)
}
pub(super) fn hess_points(
&self,
x: MatRef<'_, T>,
d2_k: MatMut<'_, T>,
i: usize,
j: usize,
uplo: Triangle,
) -> Result<(), GprError> {
self.term().hess_points(x, d2_k, i, j, uplo)
}
pub(super) fn grad_cross(
&self,
dist: MatRef<'_, T>,
d_k: MatMut<'_, T>,
param_idx: usize,
) -> Result<(), GprError> {
self.term().grad_cross(dist, d_k, param_idx)
}
pub(super) fn hess_cross(
&self,
dist: MatRef<'_, T>,
d2_k: MatMut<'_, T>,
i: usize,
j: usize,
) -> Result<(), GprError> {
self.term().hess_cross(dist, d2_k, i, j)
}
pub(super) fn grad_wrt_sq_dist(
&self,
dist: MatRef<'_, T>,
out: MatMut<'_, T>,
) -> Result<(), GprError> {
self.term().grad_wrt_sq_dist(dist, out)
}
pub(super) fn hess_wrt_sq_dist(
&self,
dist: MatRef<'_, T>,
out: MatMut<'_, T>,
) -> Result<(), GprError> {
self.term().hess_wrt_sq_dist(dist, out)
}
pub(super) fn grad_wrt_sq_dist_theta(
&self,
dist: MatRef<'_, T>,
out: MatMut<'_, T>,
param_idx: usize,
) -> Result<(), GprError> {
self.term().grad_wrt_sq_dist_theta(dist, out, param_idx)
}
}
impl<T: KernelScalar> Clone for CustomKernel<T> {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone_dual(),
_scalar: PhantomData,
}
}
}
impl<T: KernelScalar> Debug for CustomKernel<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("CustomKernel").field(&self.inner).finish()
}
}
impl<T: KernelScalar> PartialEq for CustomKernel<T> {
fn eq(&self, other: &Self) -> bool {
if self.inner.type_label() != other.inner.type_label() {
return false;
}
let n = self.num_params();
if n != other.num_params() {
return false;
}
let mut a = vec![0.0; n];
let mut b = vec![0.0; n];
match (
self.params().get_params(&mut a),
other.params().get_params(&mut b),
) {
(Ok(()), Ok(())) => a.iter().zip(&b).all(|(x, y)| x.to_bits() == y.to_bits()),
_ => false,
}
}
}
#[cfg(test)]
mod tests {
use super::{CustomKernel, KernelTerm};
use crate::kernel::{KernelSpec, RbfKernel, Triangle};
use crate::param::Interval;
use crate::{GaussianLikelihood, Gpr};
use faer::{MatMut, MatRef};
use crate::test_check::assert_send_sync;
#[derive(Clone, Debug)]
struct UnitKernel;
impl<T: crate::kernel::KernelScalar> KernelTerm<T> for UnitKernel {
fn num_params(&self) -> usize {
0
}
fn get_params(&self, out: &mut [f64]) -> Result<(), crate::GprError> {
if out.is_empty() {
Ok(())
} else {
Err(crate::GprError::IndexOutOfRange {
reason: "unit kernel has no parameters".to_owned(),
})
}
}
fn set_params(&mut self, params: &[f64]) -> Result<(), crate::GprError> {
KernelTerm::<T>::get_params(self, &mut params.to_vec())
}
fn bounds_into(&self, out: &mut [Interval]) -> Result<(), crate::GprError> {
if out.is_empty() {
Ok(())
} else {
Err(crate::GprError::IndexOutOfRange {
reason: "unit kernel has no parameters".to_owned(),
})
}
}
fn apply(
&self,
dist: MatRef<'_, T>,
mut out: MatMut<'_, T>,
uplo: Triangle,
) -> Result<(), crate::GprError> {
if dist.nrows() != dist.ncols()
|| out.nrows() != dist.nrows()
|| out.ncols() != dist.ncols()
{
return Err(crate::GprError::ShapeMismatch {
reason: "unit kernel needs matching square matrices".to_owned(),
});
}
let n = dist.nrows();
if n == 0 {
return Err(crate::GprError::EmptyInput);
}
for col in 0..n {
let start = match uplo {
Triangle::Lower => col,
Triangle::Upper | Triangle::Full => 0,
};
let end = match uplo {
Triangle::Upper => col + 1,
Triangle::Lower | Triangle::Full => n,
};
for row in start..end {
out[(row, col)] = T::from_f64(1.0);
}
}
Ok(())
}
fn apply_cross(
&self,
dist: MatRef<'_, T>,
mut out: MatMut<'_, T>,
) -> Result<(), crate::GprError> {
if out.nrows() != dist.nrows() || out.ncols() != dist.ncols() {
return Err(crate::GprError::ShapeMismatch {
reason: "unit kernel cross shape mismatch".to_owned(),
});
}
if dist.nrows() == 0 || dist.ncols() == 0 {
return Err(crate::GprError::EmptyInput);
}
for col in 0..out.ncols() {
for row in 0..out.nrows() {
out[(row, col)] = T::from_f64(1.0);
}
}
Ok(())
}
fn fill_diag(&self, out: &mut [T]) -> Result<(), crate::GprError> {
out.fill(T::from_f64(1.0));
Ok(())
}
fn grad(
&self,
_dist: MatRef<'_, T>,
_d_k: MatMut<'_, T>,
param_idx: usize,
_uplo: Triangle,
) -> Result<(), crate::GprError> {
Err(crate::GprError::IndexOutOfRange {
reason: format!("unit kernel has no parameter {param_idx}"),
})
}
fn hess(
&self,
_dist: MatRef<'_, T>,
_d2_k: MatMut<'_, T>,
i: usize,
j: usize,
_uplo: Triangle,
) -> Result<(), crate::GprError> {
Err(crate::GprError::IndexOutOfRange {
reason: format!("unit kernel has no parameter pair ({i}, {j})"),
})
}
fn hess_points(
&self,
_x: MatRef<'_, T>,
_d2_k: MatMut<'_, T>,
i: usize,
j: usize,
_uplo: Triangle,
) -> Result<(), crate::GprError> {
Err(crate::GprError::IndexOutOfRange {
reason: format!("unit kernel has no parameter pair ({i}, {j})"),
})
}
}
#[test]
fn is_send_sync() {
assert_send_sync::<CustomKernel>();
assert_send_sync::<KernelSpec>();
}
#[test]
fn clone_preserves_params() {
let a = CustomKernel::new(UnitKernel);
let b = a.clone();
assert_eq!(a, b);
}
#[test]
fn custom_plus_rbf_fits() {
let spec = KernelSpec::custom(UnitKernel) + KernelSpec::from(RbfKernel::new(1.0).unwrap());
let fitted = Gpr::new(spec, GaussianLikelihood::new(0.1).unwrap())
.fit(&[0.0, 1.0], 2, 1, &[0.0, 1.0])
.expect("spd");
assert!(fitted.neg_log_marginal_likelihood().unwrap().is_finite());
}
#[derive(Clone, Debug)]
struct FailingRead;
impl<T: crate::kernel::KernelScalar> KernelTerm<T> for FailingRead {
fn num_params(&self) -> usize {
1
}
fn get_params(&self, out: &mut [f64]) -> Result<(), crate::GprError> {
if out.len() != 1 {
return Err(crate::GprError::LengthMismatch {
reason: format!("expected 1 parameter, got {}", out.len()),
});
}
Err(crate::GprError::InvalidHyperparameter {
reason: "cannot read parameters".to_owned(),
})
}
fn set_params(&mut self, params: &[f64]) -> Result<(), crate::GprError> {
if params.len() == 1 {
Ok(())
} else {
Err(crate::GprError::LengthMismatch {
reason: format!("expected 1 parameter, got {}", params.len()),
})
}
}
fn bounds_into(&self, out: &mut [Interval]) -> Result<(), crate::GprError> {
if out.len() != 1 {
return Err(crate::GprError::LengthMismatch {
reason: format!("expected 1 bound, got {}", out.len()),
});
}
Err(crate::GprError::InvalidHyperparameter {
reason: "cannot write bounds".to_owned(),
})
}
fn apply(
&self,
_dist: MatRef<'_, T>,
_out: MatMut<'_, T>,
_uplo: Triangle,
) -> Result<(), crate::GprError> {
Ok(())
}
fn apply_cross(
&self,
_dist: MatRef<'_, T>,
_out: MatMut<'_, T>,
) -> Result<(), crate::GprError> {
Ok(())
}
fn fill_diag(&self, _out: &mut [T]) -> Result<(), crate::GprError> {
Ok(())
}
fn grad(
&self,
_dist: MatRef<'_, T>,
_d_k: MatMut<'_, T>,
_param_idx: usize,
_uplo: Triangle,
) -> Result<(), crate::GprError> {
Ok(())
}
fn hess(
&self,
_dist: MatRef<'_, T>,
_d2_k: MatMut<'_, T>,
_i: usize,
_j: usize,
_uplo: Triangle,
) -> Result<(), crate::GprError> {
Ok(())
}
fn hess_points(
&self,
_x: MatRef<'_, T>,
_d2_k: MatMut<'_, T>,
_i: usize,
_j: usize,
_uplo: Triangle,
) -> Result<(), crate::GprError> {
Ok(())
}
}
#[test]
fn get_params_propagates_custom_error() {
let spec = KernelSpec::custom(FailingRead);
let mut out = [0.0];
assert!(matches!(
spec.get_params(&mut out),
Err(crate::GprError::InvalidHyperparameter { .. })
));
assert!(matches!(
spec.compile().get_params(&mut out),
Err(crate::GprError::InvalidHyperparameter { .. })
));
}
#[test]
fn write_intervals_propagates_custom_error() {
let spec = KernelSpec::custom(FailingRead);
let mut out = [Interval::DEFAULT_POSITIVE];
let mut offset = 0;
assert!(matches!(
spec.write_intervals(&mut out, &mut offset),
Err(crate::GprError::InvalidHyperparameter { .. })
));
}
}