use super::binary::binary_op_kernel;
use crate::dtype::{DType, Element};
use crate::ops::BinaryOp;
const MIN_ROW_LEN: usize = 8;
fn shared_contiguous_suffix(
out_shape: &[usize],
a_strides: &[isize],
b_strides: &[isize],
) -> (usize, usize) {
let mut expected: isize = 1;
let mut dims = 0;
for d in (0..out_shape.len()).rev() {
if out_shape[d] != 1 && (a_strides[d] != expected || b_strides[d] != expected) {
break;
}
dims += 1;
expected *= out_shape[d] as isize;
}
(dims, expected as usize)
}
#[allow(clippy::too_many_arguments)]
pub(super) unsafe fn try_binary_rows<T: Element>(
op: BinaryOp,
a: *const T,
b: *const T,
out: *mut T,
out_shape: &[usize],
a_strides: &[isize],
b_strides: &[isize],
a_offset: usize,
b_offset: usize,
) -> bool {
if !matches!(T::DTYPE, DType::F32 | DType::F64)
|| !matches!(
op,
BinaryOp::Add | BinaryOp::Sub | BinaryOp::Mul | BinaryOp::Div
)
{
return false;
}
let (suffix, row_len) = shared_contiguous_suffix(out_shape, a_strides, b_strides);
if suffix == 0 || row_len < MIN_ROW_LEN {
return false;
}
let outer_dims = out_shape.len() - suffix;
let rows: usize = out_shape[..outer_dims].iter().product();
let mut idx = vec![0usize; outer_dims];
let mut a_idx = a_offset as isize;
let mut b_idx = b_offset as isize;
for row in 0..rows {
binary_op_kernel(
op,
a.offset(a_idx),
b.offset(b_idx),
out.add(row * row_len),
row_len,
);
for d in (0..outer_dims).rev() {
idx[d] += 1;
a_idx += a_strides[d];
b_idx += b_strides[d];
if idx[d] < out_shape[d] {
break;
}
idx[d] = 0;
a_idx -= out_shape[d] as isize * a_strides[d];
b_idx -= out_shape[d] as isize * b_strides[d];
}
}
true
}
#[cfg(test)]
mod tests {
use super::super::binary::binary_op_strided_kernel;
use super::*;
fn reference<T: Element>(
op: BinaryOp,
a: &[T],
b: &[T],
out_shape: &[usize],
a_strides: &[isize],
b_strides: &[isize],
) -> Vec<T> {
let total: usize = out_shape.iter().product();
let ndim = out_shape.len();
let mut out = Vec::with_capacity(total);
for flat in 0..total {
let mut rem = flat;
let (mut ai, mut bi) = (0isize, 0isize);
for d in (0..ndim).rev() {
let i = (rem % out_shape[d]) as isize;
rem /= out_shape[d];
ai += i * a_strides[d];
bi += i * b_strides[d];
}
let (x, y) = (a[ai as usize], b[bi as usize]);
out.push(match op {
BinaryOp::Add => x + y,
BinaryOp::Sub => x - y,
BinaryOp::Mul => x * y,
BinaryOp::Div => x / y,
_ => unreachable!("reference covers the fast-path ops only"),
});
}
out
}
fn values(n: usize, seed: f64) -> Vec<f32> {
(0..n)
.map(|i| ((i as f64 * 0.731 + seed).sin() * 3.7 + 0.01) as f32)
.collect()
}
fn check_f32(
out_shape: &[usize],
a_strides: &[isize],
b_strides: &[isize],
a_len: usize,
b_len: usize,
) {
let a = values(a_len, 0.3);
let b = values(b_len, 1.9);
let total: usize = out_shape.iter().product();
for op in [BinaryOp::Add, BinaryOp::Sub, BinaryOp::Mul, BinaryOp::Div] {
let want = reference(op, &a, &b, out_shape, a_strides, b_strides);
let mut got = vec![0.0f32; total];
unsafe {
binary_op_strided_kernel(
op,
a.as_ptr(),
b.as_ptr(),
got.as_mut_ptr(),
out_shape,
a_strides,
b_strides,
0,
0,
);
}
for i in 0..total {
assert_eq!(
got[i].to_bits(),
want[i].to_bits(),
"{op:?} {out_shape:?} i={i}"
);
}
}
}
#[test]
fn test_bias_add_rows_bit_identical() {
for &d in &[8usize, 31, 32, 100] {
check_f32(&[5, d], &[d as isize, 1], &[0, 1], 5 * d, d);
check_f32(&[5, d], &[0, 1], &[d as isize, 1], d, 5 * d);
}
}
#[test]
fn test_positional_add_rows_bit_identical() {
check_f32(&[3, 4, 16], &[64, 16, 1], &[0, 16, 1], 192, 64);
check_f32(&[3, 4, 16], &[64, 16, 1], &[16, 0, 1], 192, 48);
}
#[test]
fn test_rows_path_declines_and_general_path_matches() {
check_f32(&[5, 16], &[16, 1], &[1, 0], 80, 5);
check_f32(&[5, 4], &[4, 1], &[0, 1], 20, 4);
assert_eq!(
shared_contiguous_suffix(&[5, 16], &[16, 1], &[1, 0]),
(0, 1)
);
assert_eq!(
shared_contiguous_suffix(&[3, 4, 16], &[64, 16, 1], &[0, 16, 1]),
(2, 64)
);
}
#[test]
fn test_rows_path_f64_bit_identical() {
let a: Vec<f64> = (0..6 * 40).map(|i| (i as f64 * 0.37).cos() * 2.5).collect();
let b: Vec<f64> = (0..40).map(|i| (i as f64 * 1.3).sin() + 0.02).collect();
for op in [BinaryOp::Add, BinaryOp::Sub, BinaryOp::Mul, BinaryOp::Div] {
let want = reference(op, &a, &b, &[6, 40], &[40, 1], &[0, 1]);
let mut got = vec![0.0f64; 240];
unsafe {
binary_op_strided_kernel(
op,
a.as_ptr(),
b.as_ptr(),
got.as_mut_ptr(),
&[6, 40],
&[40, 1],
&[0, 1],
0,
0,
);
}
for i in 0..240 {
assert_eq!(got[i].to_bits(), want[i].to_bits(), "{op:?} i={i}");
}
}
}
}