use super::super::super::jacobi::LinalgElement;
use super::super::super::{CpuClient, CpuRuntime};
use crate::algorithm::linalg::{
linalg_demote, linalg_promote, validate_linalg_dtype, validate_matrix_2d,
};
use crate::dtype::{DType, Element};
use crate::error::{Error, Result};
use crate::runtime::RuntimeClient;
use crate::tensor::Tensor;
pub fn khatri_rao_impl(
client: &CpuClient,
a: &Tensor<CpuRuntime>,
b: &Tensor<CpuRuntime>,
) -> Result<Tensor<CpuRuntime>> {
validate_linalg_dtype(a.dtype())?;
if a.dtype() != b.dtype() {
return Err(Error::DTypeMismatch {
lhs: a.dtype(),
rhs: b.dtype(),
});
}
let (a, original_dtype) = linalg_promote(client, a)?;
let (b, _) = linalg_promote(client, b)?;
let (m, k_a) = validate_matrix_2d(a.shape())?;
let (n, k_b) = validate_matrix_2d(b.shape())?;
if k_a != k_b {
return Err(Error::Internal(format!(
"khatri_rao: column count mismatch. A has shape [{}, {}], B has shape [{}, {}]. \
Matrices must have the same number of columns.",
m, k_a, n, k_b
)));
}
let k = k_a;
let result = match a.dtype() {
DType::F32 => khatri_rao_typed::<f32>(client, &a, &b, m, n, k),
DType::F64 => khatri_rao_typed::<f64>(client, &a, &b, m, n, k),
_ => unreachable!(),
}?;
linalg_demote(client, result, original_dtype)
}
fn khatri_rao_typed<T: Element + LinalgElement>(
client: &CpuClient,
a: &Tensor<CpuRuntime>,
b: &Tensor<CpuRuntime>,
m: usize,
n: usize,
k: usize,
) -> Result<Tensor<CpuRuntime>> {
let device = client.device();
let a_data: Vec<T> = a.to_vec();
let b_data: Vec<T> = b.to_vec();
let m_out = m * n;
let mut out: Vec<T> = vec![T::zero(); m_out * k];
for c in 0..k {
for i in 0..m {
let a_val = a_data[i * k + c];
for j in 0..n {
let out_row = i * n + j;
out[out_row * k + c] = a_val * b_data[j * k + c];
}
}
}
Tensor::<CpuRuntime>::from_slice(&out, &[m_out, k], device)
}
#[cfg(test)]
mod tests {
use super::super::super::test_support::*;
use super::*;
use crate::algorithm::LinearAlgebraAlgorithms;
#[test]
fn test_khatri_rao_2x2() {
let client = create_client();
let device = client.device();
let a =
Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0, 3.0, 4.0], &[2, 2], device).unwrap();
let b =
Tensor::<CpuRuntime>::from_slice(&[5.0f32, 6.0, 7.0, 8.0], &[2, 2], device).unwrap();
let kr = client.khatri_rao(&a, &b).unwrap();
assert_eq!(kr.shape(), &[4, 2]);
let data: Vec<f32> = kr.to_vec();
let expected = [5.0f32, 12.0, 7.0, 16.0, 15.0, 24.0, 21.0, 32.0];
for (i, (got, exp)) in data.iter().zip(expected.iter()).enumerate() {
assert!(
(got - exp).abs() < 1e-5,
"element {} differs: {} vs {}",
i,
got,
exp
);
}
}
#[test]
fn test_khatri_rao_different_rows() {
let client = create_client();
let device = client.device();
let a = Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0, 3.0], &[1, 3], device).unwrap();
let b =
Tensor::<CpuRuntime>::from_slice(&[4.0f32, 5.0, 6.0, 7.0, 8.0, 9.0], &[2, 3], device)
.unwrap();
let kr = client.khatri_rao(&a, &b).unwrap();
assert_eq!(kr.shape(), &[2, 3]);
let data: Vec<f32> = kr.to_vec();
let expected = [4.0f32, 10.0, 18.0, 7.0, 16.0, 27.0];
for (i, (got, exp)) in data.iter().zip(expected.iter()).enumerate() {
assert!(
(got - exp).abs() < 1e-5,
"element {} differs: {} vs {}",
i,
got,
exp
);
}
}
#[test]
fn test_khatri_rao_column_mismatch() {
let client = create_client();
let device = client.device();
let a = Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0], &[1, 2], device).unwrap();
let b = Tensor::<CpuRuntime>::from_slice(&[3.0f32, 4.0, 5.0], &[1, 3], device).unwrap();
let result = client.khatri_rao(&a, &b);
assert!(
result.is_err(),
"Khatri-Rao with mismatched columns should fail"
);
}
#[test]
fn test_khatri_rao_f64() {
let client = create_client();
let device = client.device();
let a =
Tensor::<CpuRuntime>::from_slice(&[1.0f64, 2.0, 3.0, 4.0], &[2, 2], device).unwrap();
let b =
Tensor::<CpuRuntime>::from_slice(&[5.0f64, 6.0, 7.0, 8.0], &[2, 2], device).unwrap();
let kr = client.khatri_rao(&a, &b).unwrap();
assert_eq!(kr.shape(), &[4, 2]);
let data: Vec<f64> = kr.to_vec();
let expected = [5.0f64, 12.0, 7.0, 16.0, 15.0, 24.0, 21.0, 32.0];
for (i, (got, exp)) in data.iter().zip(expected.iter()).enumerate() {
assert!(
(got - exp).abs() < 1e-10,
"element {} differs: {} vs {}",
i,
got,
exp
);
}
}
#[test]
fn test_khatri_rao_gram_property() {
let client = create_client();
let device = client.device();
let a =
Tensor::<CpuRuntime>::from_slice(&[1.0f64, 2.0, 3.0, 4.0], &[2, 2], device).unwrap();
let b =
Tensor::<CpuRuntime>::from_slice(&[5.0f64, 6.0, 7.0, 8.0], &[2, 2], device).unwrap();
let kr = client.khatri_rao(&a, &b).unwrap();
assert_eq!(kr.shape(), &[4, 2]);
}
}