use super::kernel::elementwise_binary;
use crate::backend_ops::{BinaryOp, ElementwiseOps};
use coeus_core::{BackendError, Scalar};
use coeus_tensor::Tensor;
#[inline]
pub fn add<T: Scalar, B: ElementwiseOps<T>>(
a: &Tensor<T, B>,
b: &Tensor<T, B>,
backend: &B,
) -> Tensor<T, B> {
elementwise_binary(a, b, backend, BinaryOp::Add).expect("add: incompatible shapes")
}
#[inline]
pub fn sub<T: Scalar, B: ElementwiseOps<T>>(
a: &Tensor<T, B>,
b: &Tensor<T, B>,
backend: &B,
) -> Tensor<T, B> {
elementwise_binary(a, b, backend, BinaryOp::Sub).expect("sub: incompatible shapes")
}
#[inline]
pub fn mul<T: Scalar, B: ElementwiseOps<T>>(
a: &Tensor<T, B>,
b: &Tensor<T, B>,
backend: &B,
) -> Tensor<T, B> {
elementwise_binary(a, b, backend, BinaryOp::Mul).expect("mul: incompatible shapes")
}
#[inline]
pub fn div<T: Scalar, B: ElementwiseOps<T>>(
a: &Tensor<T, B>,
b: &Tensor<T, B>,
backend: &B,
) -> Tensor<T, B> {
elementwise_binary(a, b, backend, BinaryOp::Div).expect("div: incompatible shapes")
}
macro_rules! binary_assign_op {
($name:ident, $op:expr, $doc:expr) => {
#[doc = $doc]
#[inline]
pub fn $name<T: Scalar, B: ElementwiseOps<T>>(
a: &mut Tensor<T, B>,
b: &Tensor<T, B>,
backend: &B,
) -> Result<(), B::Error> {
use coeus_tensor::broadcast::broadcast_shapes;
if a.shape() != b.shape() {
let out_shape = broadcast_shapes(a.shape(), b.shape()).ok_or_else(|| {
B::Error::from(BackendError::IncompatibleBroadcast {
operation: "elementwise_binary_assign",
from: b.shape().to_vec(),
to: a.shape().to_vec(),
})
})?;
if &out_shape[..] != a.shape() {
return Err(B::Error::from(BackendError::IncompatibleBroadcast {
operation: "elementwise_binary_assign",
from: b.shape().to_vec(),
to: a.shape().to_vec(),
}));
}
}
let (a_dest, a_layout) = a.storage_and_layout_mut();
backend.elementwise_binary_assign($op, a_dest, a_layout, b.storage(), b.layout())?;
Ok(())
}
};
}
binary_assign_op!(add_assign, BinaryOp::Add, "In-place element-wise addition.");
binary_assign_op!(
sub_assign,
BinaryOp::Sub,
"In-place element-wise subtraction."
);
binary_assign_op!(
mul_assign,
BinaryOp::Mul,
"In-place element-wise multiplication."
);
binary_assign_op!(div_assign, BinaryOp::Div, "In-place element-wise division.");
#[cfg(test)]
mod tests {
use super::*;
use coeus_core::SequentialBackend;
#[test]
fn incompatible_broadcast_panics() {
let backend = SequentialBackend::new();
let lhs = Tensor::from_slice([2], &[1.0_f32, 2.0]);
let rhs = Tensor::from_slice([3], &[3.0_f32, 4.0, 5.0]);
let Err(panic) = std::panic::catch_unwind(|| add(&lhs, &rhs, &backend)) else {
panic!("incompatible shapes must panic");
};
let reason = panic
.downcast_ref::<String>()
.map(String::as_str)
.or_else(|| panic.downcast_ref::<&str>().copied())
.expect("invariant: an expect payload is a string");
assert!(
reason.starts_with("add: incompatible shapes"),
"the panic must be add's shape rejection, not a later failure: {reason}"
);
assert!(
elementwise_binary(&lhs, &rhs, &backend, BinaryOp::Add).is_err(),
"the fallible form must reject the same pair"
);
}
#[test]
fn cpu_assign_broadcasts_without_replacing_storage() {
let backend = SequentialBackend::new();
let mut lhs = Tensor::from_slice([2, 2], &[1.0_f32, 2.0, 3.0, 4.0]);
let rhs = Tensor::from_slice([1, 2], &[10.0_f32, 20.0]);
let allocation = lhs.as_slice().as_ptr();
add_assign(&mut lhs, &rhs, &backend).expect("valid row broadcast");
assert_eq!(lhs.as_slice(), &[11.0, 22.0, 13.0, 24.0]);
assert_eq!(lhs.as_slice().as_ptr(), allocation);
}
}