use crate::dtype::DType;
use crate::error::{Error, Result};
use crate::ops::{ExpansionOperand, IntervalOperand, broadcast_shape};
use crate::runtime::{Device, Runtime, RuntimeClient};
use crate::tensor::Tensor;
pub fn float_dtype<R, C>(client: &C, op: &'static str, values: &[&Tensor<R>]) -> Result<DType>
where
R: Runtime<DType = DType>,
C: RuntimeClient<R>,
{
let Some(first) = values.first() else {
return Err(Error::InvalidArgument {
arg: "operands",
reason: format!("{op} received no operands. Supply at least one tensor."),
});
};
let dtype = first.dtype();
for value in values {
if value.dtype() != dtype {
return Err(Error::DTypeMismatch {
lhs: dtype,
rhs: value.dtype(),
});
}
if !client.device().is_same(value.device()) {
return Err(Error::DeviceMismatch);
}
}
if !matches!(dtype, DType::F32 | DType::F64) {
return Err(Error::UnsupportedDType { dtype, op });
}
Ok(dtype)
}
pub fn status<R, C>(client: &C, status: &Tensor<R>) -> Result<()>
where
R: Runtime<DType = DType>,
C: RuntimeClient<R>,
{
if !client.device().is_same(status.device()) {
return Err(Error::DeviceMismatch);
}
if status.dtype() != DType::U32 || status.numel() != 1 {
return Err(Error::InvalidArgument {
arg: "status",
reason: format!(
"Interval status has dtype {:?} and shape {:?}. Create it with interval_status.",
status.dtype(),
status.shape()
),
});
}
Ok(())
}
pub fn interval<R: Runtime<DType = DType>>(value: IntervalOperand<'_, R>) -> Result<()> {
if value.lower.shape() != value.upper.shape() {
return Err(Error::ShapeMismatch {
expected: value.lower.shape().to_vec(),
got: value.upper.shape().to_vec(),
});
}
Ok(())
}
pub fn expansions<R: Runtime<DType = DType>>(values: &[ExpansionOperand<'_, R>]) -> Result<()> {
let Some(first) = values.first() else {
return Ok(());
};
let shape = first.high.shape();
for value in values {
for component in [value.high, value.low, value.abs_error] {
if component.shape() != shape {
return Err(Error::ShapeMismatch {
expected: shape.to_vec(),
got: component.shape().to_vec(),
});
}
}
}
Ok(())
}
pub fn broadcast<R: Runtime<DType = DType>>(
lhs: &Tensor<R>,
rhs: &Tensor<R>,
) -> Result<Vec<usize>> {
broadcast_shape(lhs.shape(), rhs.shape()).ok_or_else(|| Error::BroadcastError {
lhs: lhs.shape().to_vec(),
rhs: rhs.shape().to_vec(),
})
}
pub fn same_view<R: Runtime<DType = DType>>(a: &Tensor<R>, b: &Tensor<R>) -> bool {
std::ptr::eq(a, b)
|| (a.storage().ptr() == b.storage().ptr()
&& a.offset() == b.offset()
&& a.shape() == b.shape()
&& a.strides() == b.strides())
}