use crate::err::Result;
use crate::exec::catalog::op;
use crate::shapes::{DynShape, Shape};
use crate::tensor::backend::Backend;
use crate::tensor::backend::Execute;
use crate::tensor::base::Tensor;
use crate::tensor::grad::RequiresGrad;
use crate::tensor::ops::reduce::ReduceSelector;
macro_rules! impl_unary_op {
(
$(#[$meta:meta])*
$method:ident, $operation:ident
) => {
$(#[$meta])*
pub fn $method(&self) -> Result<Self>
where
B: Execute<op::$operation>,
<B as Execute<op::$operation>>::Output: Into<B::Storage<K>>,
{
execute_unary_descriptor::<op::$operation, S, B, K, G>(self)
}
};
}
impl<S: Shape, B: Backend, K: crate::tensor::dtype::DType, G: RequiresGrad> Tensor<S, B, K, G> {
impl_unary_op!(
tan, Tan
);
impl_unary_op!(
asin, Asin
);
impl_unary_op!(
acos, Acos
);
impl_unary_op!(
atan, Atan
);
impl_unary_op!(
sinh, Sinh
);
impl_unary_op!(
cosh, Cosh
);
impl_unary_op!(
asinh, Asinh
);
impl_unary_op!(
acosh, Acosh
);
impl_unary_op!(
atanh, Atanh
);
impl_unary_op!(
erf, Erf
);
impl_unary_op!(
rsqrt, Rsqrt
);
impl_unary_op!(
trunc, Trunc
);
impl_unary_op!(
frac, Frac
);
pub fn abs(&self) -> Result<Self>
where
B: Execute<op::Abs>,
<B as Execute<op::Abs>>::Output: Into<B::Storage<K>>,
{
execute_unary_descriptor::<op::Abs, S, B, K, G>(self)
}
pub fn relu(&self) -> Result<Self>
where
B: Execute<op::Relu>,
<B as Execute<op::Relu>>::Output: Into<B::Storage<K>>,
{
execute_unary_descriptor::<op::Relu, S, B, K, G>(self)
}
pub fn gelu(&self) -> Result<Self>
where
B: Execute<op::Gelu>,
<B as Execute<op::Gelu>>::Output: Into<B::Storage<K>>,
{
execute_unary_descriptor::<op::Gelu, S, B, K, G>(self)
}
pub fn step(&self) -> Result<Self>
where
B: Execute<op::Step>,
<B as Execute<op::Step>>::Output: Into<B::Storage<K>>,
{
execute_unary_descriptor::<op::Step, S, B, K, G>(self)
}
pub fn mish(&self) -> Result<Self>
where
B: Execute<op::Mish>,
<B as Execute<op::Mish>>::Output: Into<B::Storage<K>>,
{
execute_unary_descriptor::<op::Mish, S, B, K, G>(self)
}
pub fn elu(&self) -> Result<Self>
where
B: Execute<op::Elu>,
<B as Execute<op::Elu>>::Output: Into<B::Storage<K>>,
{
execute_unary_descriptor::<op::Elu, S, B, K, G>(self)
}
pub fn swish(&self) -> Result<Self>
where
B: Execute<op::Swish>,
<B as Execute<op::Swish>>::Output: Into<B::Storage<K>>,
{
execute_unary_descriptor::<op::Swish, S, B, K, G>(self)
}
#[inline]
pub fn softmax<A: ReduceSelector<S>>(&self, axis: A) -> Result<Tensor<S, B, K, G>>
where
S: DynShape,
B: Execute<op::Softmax>,
<B as Execute<op::Softmax>>::Output: Into<B::Storage<K>>,
{
let dim = axis.resolve(self.shape_buf().rank())?;
execute_unary_descriptor_with_attributes::<op::Softmax, S, B, K, G>(
self,
crate::exec::catalog::AxisAttributes { axis: dim },
)
}
pub fn try_neg(&self) -> Result<Self>
where
B: Execute<op::Neg>,
<B as Execute<op::Neg>>::Output: Into<B::Storage<K>>,
{
execute_unary_descriptor::<op::Neg, S, B, K, G>(self)
}
#[doc(hidden)]
pub fn neg(&self) -> Result<Self>
where
B: Execute<op::Neg>,
<B as Execute<op::Neg>>::Output: Into<B::Storage<K>>,
{
self.try_neg()
}
pub fn sqrt(&self) -> Result<Self>
where
B: Execute<op::Sqrt>,
<B as Execute<op::Sqrt>>::Output: Into<B::Storage<K>>,
{
execute_unary_descriptor::<op::Sqrt, S, B, K, G>(self)
}
pub fn exp(&self) -> Result<Self>
where
B: Execute<op::Exp>,
<B as Execute<op::Exp>>::Output: Into<B::Storage<K>>,
{
execute_unary_descriptor::<op::Exp, S, B, K, G>(self)
}
#[inline]
pub fn powf(&self, exponent: f64) -> Result<Self>
where
B: Execute<op::Powf>,
<B as Execute<op::Powf>>::Output: Into<B::Storage<K>>,
{
execute_unary_descriptor_with_attributes::<op::Powf, S, B, K, G>(
self,
crate::exec::catalog::ScalarAttributes { value: exponent },
)
}
#[inline]
pub fn clamp(&self, min: f64, max: f64) -> Result<Self>
where
B: Execute<op::Clamp>,
<B as Execute<op::Clamp>>::Output: Into<B::Storage<K>>,
{
execute_unary_descriptor_with_attributes::<op::Clamp, S, B, K, G>(
self,
crate::exec::catalog::ClampAttributes { min, max },
)
}
impl_unary_op!(
sign, Sign
);
impl_unary_op!(
floor, Floor
);
impl_unary_op!(
ceil, Ceil
);
impl_unary_op!(
round, Round
);
impl_unary_op!(
log2, Log2
);
impl_unary_op!(
log10, Log10
);
impl_unary_op!(
sin, Sin
);
impl_unary_op!(
cos, Cos
);
pub fn log(&self) -> Result<Self>
where
B: Execute<op::Log>,
<B as Execute<op::Log>>::Output: Into<B::Storage<K>>,
{
execute_unary_descriptor::<op::Log, S, B, K, G>(self)
}
pub fn tanh(&self) -> Result<Self>
where
B: Execute<op::Tanh>,
<B as Execute<op::Tanh>>::Output: Into<B::Storage<K>>,
{
execute_unary_descriptor::<op::Tanh, S, B, K, G>(self)
}
pub fn sigmoid(&self) -> Result<Self>
where
B: Execute<op::Sigmoid>,
<B as Execute<op::Sigmoid>>::Output: Into<B::Storage<K>>,
{
execute_unary_descriptor::<op::Sigmoid, S, B, K, G>(self)
}
pub fn mul_scalar<Sc: Into<crate::tensor::backend::ScalarValue>>(
&self,
scalar: Sc,
) -> Result<Self>
where
B: Execute<op::MulScalar>,
<B as Execute<op::MulScalar>>::Output: Into<B::Storage<K>>,
{
execute_unary_descriptor_with_attributes::<op::MulScalar, S, B, K, G>(
self,
crate::exec::catalog::ScalarAttributes {
value: scalar.into().to_f64(),
},
)
}
pub fn add_scalar<Sc: Into<crate::tensor::backend::ScalarValue>>(
&self,
scalar: Sc,
) -> Result<Self>
where
B: Execute<op::AddScalar>,
<B as Execute<op::AddScalar>>::Output: Into<B::Storage<K>>,
{
execute_unary_descriptor_with_attributes::<op::AddScalar, S, B, K, G>(
self,
crate::exec::catalog::ScalarAttributes {
value: scalar.into().to_f64(),
},
)
}
}
use crate::dist::placement::Local;
use crate::exec::catalog::{AttributeContract, CanonicalOperation, NoAttributes};
use crate::exec::context::ExecutionContext;
use crate::exec::dispatch;
use crate::exec::request::TensorHandle;
pub(crate) fn execute_unary_descriptor<
O,
S: Shape,
B: Backend,
K: crate::tensor::dtype::DType,
G: RequiresGrad,
>(
tensor: &Tensor<S, B, K, G>,
) -> Result<Tensor<S, B, K, G>>
where
O: CanonicalOperation + crate::exec::catalog::Operation<Attributes = NoAttributes>,
B: Execute<O>,
<B as Execute<O>>::Output: Into<B::Storage<K>>,
{
let handle = TensorHandle::from_storage::<B, K, Local>(&tensor.inner);
let shape_val = tensor._shape.clone();
let context = crate::tensor::grad::execution_context::<B, G>(&tensor._grad);
let storage = G::grad_mode(&tensor._grad)
.restrict(|| {
dispatch::execute_shaped::<O, B, S>(&context, NoAttributes, &[handle], &shape_val)
})
.map_err(crate::err::Error::from)?;
Tensor::from_shape_value(
storage.into(),
tensor._shape.clone(),
tensor._dtype.clone(),
tensor._device.clone(),
tensor._grad.clone(),
)
}
pub(crate) fn execute_unary_descriptor_with_attributes<
O,
S: Shape,
B: Backend,
K: crate::tensor::dtype::DType,
G: RequiresGrad,
>(
tensor: &Tensor<S, B, K, G>,
attributes: O::Attributes,
) -> Result<Tensor<S, B, K, G>>
where
O: CanonicalOperation,
O::Attributes: AttributeContract,
B: Execute<O>,
<B as Execute<O>>::Output: Into<B::Storage<K>>,
{
let handle = TensorHandle::from_storage::<B, K, Local>(&tensor.inner);
let shape_val = tensor._shape.clone();
let context = crate::tensor::grad::execution_context::<B, G>(&tensor._grad);
let storage = G::grad_mode(&tensor._grad)
.restrict(|| {
dispatch::execute_shaped::<O, B, S>(&context, attributes, &[handle], &shape_val)
})
.map_err(crate::err::Error::from)?;
Tensor::from_shape_value(
storage.into(),
tensor._shape.clone(),
tensor._dtype.clone(),
tensor._device.clone(),
tensor._grad.clone(),
)
}
pub(crate) fn execute_logical_unary_descriptor<O, S: Shape, B: Backend, G: RequiresGrad>(
tensor: &Tensor<S, B, bool, G>,
) -> Result<Tensor<S, B, bool, crate::tensor::grad::NoGrad>>
where
O: CanonicalOperation + crate::exec::catalog::Operation<Attributes = NoAttributes>,
B: Execute<O>,
<B as Execute<O>>::Output: Into<B::Storage<bool>>,
{
let handle = TensorHandle::from_storage::<B, bool, Local>(&tensor.inner);
let shape_val = tensor._shape.clone();
let context =
ExecutionContext::from_scope(B::default()).with_grad_mode(crate::exec::GradMode::Disabled);
let storage = crate::tensor::grad::NoGrad::grad_mode(&Default::default())
.restrict(|| {
dispatch::execute_shaped::<O, B, S>(&context, NoAttributes, &[handle], &shape_val)
})
.map_err(crate::err::Error::from)?;
Tensor::from_shape_value(
storage.into(),
tensor._shape.clone(),
Default::default(),
tensor._device.clone(),
Default::default(),
)
}
impl<S: Shape, B: Backend, G: RequiresGrad> Tensor<S, B, bool, G> {
pub fn logical_not(&self) -> Result<Tensor<S, B, bool, crate::tensor::grad::NoGrad>>
where
B: Execute<op::LogicalNot>,
<B as Execute<op::LogicalNot>>::Output: Into<B::Storage<bool>>,
{
execute_logical_unary_descriptor::<op::LogicalNot, S, B, G>(self)
}
}
macro_rules! impl_std_scalar_ops {
($t:ty) => {
impl<S: Shape, B: Backend, K: crate::tensor::dtype::DType, G: RequiresGrad>
core::ops::Mul<$t> for Tensor<S, B, K, G>
where
B: Execute<op::MulScalar>,
<B as Execute<op::MulScalar>>::Output: Into<B::Storage<K>>,
{
type Output = Tensor<S, B, K, G>;
#[inline]
fn mul(self, rhs: $t) -> Self::Output {
crate::tensor::ops::operator_or_panic("* (scalar)", self.mul_scalar(rhs))
}
}
impl<'a, S: Shape, B: Backend, K: crate::tensor::dtype::DType, G: RequiresGrad>
core::ops::Mul<$t> for &'a Tensor<S, B, K, G>
where
B: Execute<op::MulScalar>,
<B as Execute<op::MulScalar>>::Output: Into<B::Storage<K>>,
{
type Output = Tensor<S, B, K, G>;
#[inline]
fn mul(self, rhs: $t) -> Self::Output {
crate::tensor::ops::operator_or_panic("* (scalar)", self.mul_scalar(rhs))
}
}
impl<S: Shape, B: Backend, K: crate::tensor::dtype::DType, G: RequiresGrad>
core::ops::Add<$t> for Tensor<S, B, K, G>
where
B: Execute<op::AddScalar>,
<B as Execute<op::AddScalar>>::Output: Into<B::Storage<K>>,
{
type Output = Tensor<S, B, K, G>;
#[inline]
fn add(self, rhs: $t) -> Self::Output {
crate::tensor::ops::operator_or_panic("+ (scalar)", self.add_scalar(rhs))
}
}
impl<'a, S: Shape, B: Backend, K: crate::tensor::dtype::DType, G: RequiresGrad>
core::ops::Add<$t> for &'a Tensor<S, B, K, G>
where
B: Execute<op::AddScalar>,
<B as Execute<op::AddScalar>>::Output: Into<B::Storage<K>>,
{
type Output = Tensor<S, B, K, G>;
#[inline]
fn add(self, rhs: $t) -> Self::Output {
crate::tensor::ops::operator_or_panic("+ (scalar)", self.add_scalar(rhs))
}
}
impl<S: Shape, B: Backend, K: crate::tensor::dtype::DType, G: RequiresGrad>
core::ops::Sub<$t> for Tensor<S, B, K, G>
where
B: Execute<op::SubScalar>,
<B as Execute<op::SubScalar>>::Output: Into<B::Storage<K>>,
{
type Output = Tensor<S, B, K, G>;
fn sub(self, rhs: $t) -> Self::Output {
crate::tensor::ops::operator_or_panic("- (scalar)", self.sub_scalar(rhs as f64))
}
}
impl<'a, S: Shape, B: Backend, K: crate::tensor::dtype::DType, G: RequiresGrad>
core::ops::Sub<$t> for &'a Tensor<S, B, K, G>
where
B: Execute<op::SubScalar>,
<B as Execute<op::SubScalar>>::Output: Into<B::Storage<K>>,
{
type Output = Tensor<S, B, K, G>;
fn sub(self, rhs: $t) -> Self::Output {
crate::tensor::ops::operator_or_panic("- (scalar)", self.sub_scalar(rhs as f64))
}
}
impl<S: Shape, B: Backend, K: crate::tensor::dtype::DType, G: RequiresGrad>
core::ops::Div<$t> for Tensor<S, B, K, G>
where
B: Execute<op::DivScalar>,
<B as Execute<op::DivScalar>>::Output: Into<B::Storage<K>>,
{
type Output = Tensor<S, B, K, G>;
fn div(self, rhs: $t) -> Self::Output {
crate::tensor::ops::operator_or_panic("/ (scalar)", self.div_scalar(rhs as f64))
}
}
impl<'a, S: Shape, B: Backend, K: crate::tensor::dtype::DType, G: RequiresGrad>
core::ops::Div<$t> for &'a Tensor<S, B, K, G>
where
B: Execute<op::DivScalar>,
<B as Execute<op::DivScalar>>::Output: Into<B::Storage<K>>,
{
type Output = Tensor<S, B, K, G>;
fn div(self, rhs: $t) -> Self::Output {
crate::tensor::ops::operator_or_panic("/ (scalar)", self.div_scalar(rhs as f64))
}
}
};
}
impl_std_scalar_ops!(f32);
impl_std_scalar_ops!(f64);
impl_std_scalar_ops!(i32);
impl_std_scalar_ops!(i64);
impl<S: Shape, B: Backend, K: crate::tensor::dtype::DType, G: RequiresGrad> core::ops::Neg
for Tensor<S, B, K, G>
where
B: Execute<op::Neg>,
<B as Execute<op::Neg>>::Output: Into<B::Storage<K>>,
{
type Output = Tensor<S, B, K, G>;
fn neg(self) -> Self::Output {
crate::tensor::ops::operator_or_panic("- (unary)", self.try_neg())
}
}
impl<S: Shape, B: Backend, K: crate::tensor::dtype::DType, G: RequiresGrad> core::ops::Neg
for &Tensor<S, B, K, G>
where
B: Execute<op::Neg>,
<B as Execute<op::Neg>>::Output: Into<B::Storage<K>>,
{
type Output = Tensor<S, B, K, G>;
fn neg(self) -> Self::Output {
crate::tensor::ops::operator_or_panic("- (unary)", self.try_neg())
}
}