#[test]
fn gemm_matches_the_source_dimension_sweep() {
for (m, n, k) in [
(0, 0, 0),
(0, 2, 3),
(2, 0, 3),
(2, 3, 0),
(1, 1, 1),
(2, 3, 2),
(3, 2, 5),
(4, 4, 4),
(7, 3, 3),
] {
let a = Mat::<Scalar>::from_fn(m, k, |i, j| integer(i, j + 1));
let a_t = a.transpose().to_owned();
let b = Mat::<Scalar>::from_fn(k, n, |i, j| integer(i + 2, j));
let b_t = b.transpose().to_owned();
let c = Mat::<Scalar>::from_fn(m, n, |i, j| integer(i + 4, j + 3));
let (a_before, a_t_before) = (a.to_owned(), a_t.to_owned());
let (b_before, b_t_before) = (b.to_owned(), b_t.to_owned());
for (ta, tb) in [
(0, 0),
(1, 0),
(0, 1),
(1, 1),
(2, 0),
(0, 2),
(2, 2),
] {
let (lhs, lhs_conj) = viewed(&a, &a_t, ta);
let (rhs, rhs_conj) = viewed(&b, &b_t, tb);
for beta in [Accum::Replace, Accum::Add] {
for alpha in [z(0.0), z(1.0), z(-2.0), z(0.5)] {
let mut dst = c.clone();
matmul_with_conj(
&mut dst,
BlockStructure::Rectangular,
beta,
lhs,
BlockStructure::Rectangular,
lhs_conj,
rhs,
BlockStructure::Rectangular,
rhs_conj,
alpha,
Par::Seq,
);
check_blend(
"gemm",
&c,
&dst,
BlockStructure::Rectangular,
lhs,
BlockStructure::Rectangular,
lhs_conj,
rhs,
BlockStructure::Rectangular,
rhs_conj,
beta,
alpha,
);
crate::assert!(
exact(a.as_ref(), a_before.as_ref())
&& exact(a_t.as_ref(), a_t_before.as_ref())
&& exact(b.as_ref(), b_before.as_ref())
&& exact(b_t.as_ref(), b_t_before.as_ref()),
"gemm modified an operand: m={m} n={n} k={k}"
);
}
}
}
}
}
#[test]
fn gemm_reads_the_destination_only_when_it_accumulates() {
for (m, n, k) in [(1, 1, 1), (2, 3, 2), (3, 2, 4), (2, 2, 0)] {
let a = Mat::<Scalar>::from_fn(m, k, |i, j| integer(i, j + 1));
let b = Mat::<Scalar>::from_fn(k, n, |i, j| integer(i + 2, j));
for alpha in [z(1.0), z(-2.0), z(0.0)] {
let poisoned = Mat::<Scalar>::from_fn(m, n, |_, _| nan());
let mut replaced = poisoned.clone();
matmul(
&mut replaced,
Accum::Replace,
a.as_ref(),
b.as_ref(),
alpha,
Par::Seq,
);
for i in 0..m {
for j in 0..n {
crate::assert!(
!real(&replaced[(i, j)]).is_nan()
&& !imag(&replaced[(i, j)]).is_nan(),
"gemm read the replaced destination: m={m} n={n} \
k={k} alpha={:?} at ({i}, {j})",
real(&alpha)
);
}
}
let mut added = poisoned.clone();
matmul(
&mut added,
Accum::Add,
a.as_ref(),
b.as_ref(),
alpha,
Par::Seq,
);
for i in 0..m {
for j in 0..n {
crate::assert!(
real(&added[(i, j)]).is_nan(),
"gemm did not accumulate into the destination: \
m={m} n={n} k={k} alpha={:?} at ({i}, {j})",
real(&alpha)
);
}
}
}
}
}
#[test]
fn gemv_matches_the_source_dimension_sweep() {
for (m, k) in [(0, 0), (0, 3), (3, 0), (1, 1), (3, 2), (2, 3), (5, 4)] {
let a = Mat::<Scalar>::from_fn(m, k, |i, j| integer(i, j + 1));
let a_before = a.to_owned();
let x = Mat::<Scalar>::from_fn(k, 1, |i, _| integer(i + 2, 5));
let x_before = x.to_owned();
let y = Mat::<Scalar>::from_fn(m, 1, |i, _| integer(i + 4, 3));
for beta in [Accum::Replace, Accum::Add] {
for alpha in [z(0.0), z(1.0), z(-2.0), z(0.5)] {
let mut dst = y.clone();
matmul(&mut dst, beta, a.as_ref(), x.as_ref(), alpha, Par::Seq);
check_blend(
"gemv",
&y,
&dst,
BlockStructure::Rectangular,
a.as_ref(),
BlockStructure::Rectangular,
Conj::No,
x.as_ref(),
BlockStructure::Rectangular,
Conj::No,
beta,
alpha,
);
crate::assert!(
exact(a.as_ref(), a_before.as_ref())
&& exact(x.as_ref(), x_before.as_ref()),
"gemv modified an operand: m={m} k={k}"
);
}
}
let xt = Mat::<Scalar>::from_fn(m, 1, |i, _| integer(i + 1, 2));
let xt_before = xt.to_owned();
let yt = Mat::<Scalar>::from_fn(1, k, |_, j| integer(j + 3, 4));
for beta in [Accum::Replace, Accum::Add] {
for alpha in [z(0.0), z(1.0), z(-2.0), z(0.5)] {
let mut dst = yt.clone();
matmul(
&mut dst,
beta,
xt.as_ref().transpose(),
a.as_ref(),
alpha,
Par::Seq,
);
check_blend(
"gemv transposed",
&yt,
&dst,
BlockStructure::Rectangular,
xt.as_ref().transpose(),
BlockStructure::Rectangular,
Conj::No,
a.as_ref(),
BlockStructure::Rectangular,
Conj::No,
beta,
alpha,
);
crate::assert!(
exact(a.as_ref(), a_before.as_ref())
&& exact(xt.as_ref(), xt_before.as_ref()),
"gemv modified an operand: transposed route, m={m} k={k}"
);
}
}
}
}
#[test]
fn gemv_zeroes_the_destination_of_an_empty_reduction() {
let a = Mat::<Scalar>::from_fn(3, 0, |_, _| zero());
let x = Mat::<Scalar>::from_fn(0, 1, |_, _| zero());
let y = Mat::<Scalar>::from_fn(3, 1, |i, _| integer(i + 1, 2));
for beta in [Accum::Replace, Accum::Add] {
let mut dst = y.clone();
matmul(&mut dst, beta, a.as_ref(), x.as_ref(), z(1.0), Par::Seq);
if matches!(beta, Accum::Add) {
crate::assert!(
exact(dst.as_ref(), y.as_ref()),
"gemv changed the destination of a zero-length reduction"
);
} else {
crate::assert!(
(0..3).all(|i| real(&dst[(i, 0)]) == 0.0
&& imag(&dst[(i, 0)]) == 0.0),
"gemv did not replace with a zero-length reduction"
);
}
}
}
fn viewed<'a>(
a: &'a Mat<Scalar>,
a_t: &'a Mat<Scalar>,
trans: u8,
) -> (MatRef<'a, Scalar>, Conj) {
match trans {
0 => (a.as_ref(), Conj::No),
1 => (a_t.as_ref().transpose(), Conj::No),
_ => (a_t.as_ref().transpose(), Conj::Yes),
}
}