use crate::dist::placement::Local;
use crate::err::Result;
use crate::exec::catalog::{LossAttributes, LossReduction, op};
use crate::exec::dispatch;
use crate::exec::request::TensorHandle;
use crate::shapes::Shape;
use crate::shapes::error::OperationKind;
use crate::shapes::shape::shape_buf_from_dims;
use crate::tensor::backend::Backend;
use crate::tensor::backend::Execute;
use crate::tensor::base::Tensor;
use crate::tensor::grad::RequiresGrad;
use crate::tensor::reduction::{
BceReductionShape, CrossEntropyReductionShape, L1ReductionShape, Mean, MseReductionShape,
Reduction, ReductionMode,
};
use alloc::vec::Vec;
fn execute_loss_descriptor<
O,
S: Shape,
S2: Shape,
B: Backend,
K: crate::tensor::dtype::DType,
G: RequiresGrad,
G2: RequiresGrad,
>(
prediction: &Tensor<S, B, K, G>,
target: &Tensor<S2, B, K, G2>,
reduction: Reduction,
) -> Result<<B as Execute<O>>::Output>
where
O: crate::exec::catalog::Operation<Attributes = crate::exec::catalog::LossAttributes>,
B: Execute<O> + crate::exec::Capabilities,
{
let inputs = [
TensorHandle::from_storage::<B, K, Local>(&prediction.inner),
TensorHandle::from_storage::<B, K, Local>(&target.inner),
];
let reduction = match reduction {
Reduction::None => LossReduction::None,
Reduction::Mean => LossReduction::Mean,
Reduction::Sum => LossReduction::Sum,
};
let context = crate::tensor::grad::execution_context::<B, G>(&prediction._grad);
dispatch::execute::<O, B>(&context, LossAttributes { reduction }, &inputs)
.map_err(crate::err::Error::from)
}
impl<
S: Shape + crate::shapes::DynShape,
B: Backend,
K: crate::tensor::dtype::DType,
G: RequiresGrad,
> Tensor<S, B, K, G>
{
pub fn cross_entropy_loss<S2: Shape, KT: crate::tensor::dtype::DType, G2: RequiresGrad>(
&self,
target: &Tensor<S2, B, KT, G2>,
) -> Result<Tensor<crate::shapes::Nil, B, K, G>>
where
B: Execute<op::CrossEntropyLoss>,
<B as Execute<op::CrossEntropyLoss>>::Output: Into<B::Storage<K>>,
{
self.cross_entropy_loss_with::<Mean, S2, KT, G2>(target)
}
pub fn cross_entropy_loss_with<
R,
S2: Shape,
KT: crate::tensor::dtype::DType,
G2: RequiresGrad,
>(
&self,
target: &Tensor<S2, B, KT, G2>,
) -> Result<Tensor<R::Output, B, K, G>>
where
R: ReductionMode + CrossEntropyReductionShape<S>,
B: Execute<op::CrossEntropyLoss>,
<B as Execute<op::CrossEntropyLoss>>::Output: Into<B::Storage<K>>,
{
let prediction = TensorHandle::from_storage::<B, K, Local>(&self.inner);
let target_handle = TensorHandle::from_storage::<B, KT, Local>(&target.inner);
let reduction = match R::as_enum() {
Reduction::None => LossReduction::None,
Reduction::Mean => LossReduction::Mean,
Reduction::Sum => LossReduction::Sum,
};
let context = crate::tensor::grad::execution_context::<B, G>(&self._grad);
let inner = dispatch::execute::<op::CrossEntropyLoss, B>(
&context,
LossAttributes { reduction },
&[prediction, target_handle],
)
.map_err(crate::err::Error::from)?;
let mut out_shape_dims: Vec<usize> = vec![];
if R::as_enum() == Reduction::None {
out_shape_dims = self.dims().into();
if !out_shape_dims.is_empty() {
out_shape_dims.remove(1); }
}
let out_shape =
shape_buf_from_dims::<R::Output>(OperationKind::Reduction, &out_shape_dims)?;
Tensor::from_parts(
inner.into(),
out_shape,
self._dtype.clone(),
self._device.clone(),
self._grad.clone(),
)
}
pub fn mse_loss<S2: Shape, G2: RequiresGrad>(
&self,
target: &Tensor<S2, B, K, G2>,
) -> Result<Tensor<crate::shapes::Nil, B, K, G>>
where
B: Execute<op::MseLoss> + crate::exec::Capabilities,
<B as Execute<op::MseLoss>>::Output: Into<B::Storage<K>>,
{
self.mse_loss_with::<Mean, S2, G2>(target)
}
pub fn mse_loss_with<R, S2: Shape, G2: RequiresGrad>(
&self,
target: &Tensor<S2, B, K, G2>,
) -> Result<Tensor<R::Output, B, K, G>>
where
R: ReductionMode + MseReductionShape<S>,
B: Execute<op::MseLoss> + crate::exec::Capabilities,
<B as Execute<op::MseLoss>>::Output: Into<B::Storage<K>>,
{
let inner =
execute_loss_descriptor::<op::MseLoss, S, S2, B, K, G, G2>(self, target, R::as_enum())?;
let mut out_shape_dims: Vec<usize> = vec![];
if R::as_enum() == Reduction::None {
out_shape_dims = self.dims().into();
}
let out_shape =
shape_buf_from_dims::<R::Output>(OperationKind::Reduction, &out_shape_dims)?;
Tensor::from_parts(
inner.into(),
out_shape,
self._dtype.clone(),
self._device.clone(),
self._grad.clone(),
)
}
pub fn l1_loss<S2: Shape, G2: RequiresGrad>(
&self,
target: &Tensor<S2, B, K, G2>,
) -> Result<Tensor<crate::shapes::Nil, B, K, G>>
where
B: Execute<op::L1Loss> + crate::exec::Capabilities,
<B as Execute<op::L1Loss>>::Output: Into<B::Storage<K>>,
{
self.l1_loss_with::<Mean, S2, G2>(target)
}
pub fn l1_loss_with<R, S2: Shape, G2: RequiresGrad>(
&self,
target: &Tensor<S2, B, K, G2>,
) -> Result<Tensor<R::Output, B, K, G>>
where
R: ReductionMode + L1ReductionShape<S>,
B: Execute<op::L1Loss> + crate::exec::Capabilities,
<B as Execute<op::L1Loss>>::Output: Into<B::Storage<K>>,
{
let inner =
execute_loss_descriptor::<op::L1Loss, S, S2, B, K, G, G2>(self, target, R::as_enum())?;
let mut out_shape_dims: Vec<usize> = vec![];
if R::as_enum() == Reduction::None {
out_shape_dims = self.dims().into();
}
let out_shape =
shape_buf_from_dims::<R::Output>(OperationKind::Reduction, &out_shape_dims)?;
Tensor::from_parts(
inner.into(),
out_shape,
self._dtype.clone(),
self._device.clone(),
self._grad.clone(),
)
}
pub fn bce_with_logits_loss<S2: Shape, G2: RequiresGrad>(
&self,
target: &Tensor<S2, B, K, G2>,
) -> Result<Tensor<crate::shapes::Nil, B, K, G>>
where
B: Execute<op::BceWithLogitsLoss> + crate::exec::Capabilities,
<B as Execute<op::BceWithLogitsLoss>>::Output: Into<B::Storage<K>>,
{
self.bce_with_logits_loss_with::<Mean, S2, G2>(target)
}
pub fn bce_with_logits_loss_with<R, S2: Shape, G2: RequiresGrad>(
&self,
target: &Tensor<S2, B, K, G2>,
) -> Result<Tensor<R::Output, B, K, G>>
where
R: ReductionMode + BceReductionShape<S>,
B: Execute<op::BceWithLogitsLoss> + crate::exec::Capabilities,
<B as Execute<op::BceWithLogitsLoss>>::Output: Into<B::Storage<K>>,
{
let inner = execute_loss_descriptor::<op::BceWithLogitsLoss, S, S2, B, K, G, G2>(
self,
target,
R::as_enum(),
)?;
let mut out_shape_dims: Vec<usize> = vec![];
if R::as_enum() == Reduction::None {
out_shape_dims = self.dims().into();
}
let out_shape =
shape_buf_from_dims::<R::Output>(OperationKind::Reduction, &out_shape_dims)?;
Tensor::from_parts(
inner.into(),
out_shape,
self._dtype.clone(),
self._device.clone(),
self._grad.clone(),
)
}
}