use super::functional::input_device;
use crate::{Reduction, Result, RustTorchError, Tensor, device::ensure_device};
#[derive(Debug, Clone, Copy)]
pub struct CrossEntropyOptions<'a> {
pub weight: Option<&'a Tensor>,
pub reduction: Reduction,
pub ignore_index: i64,
pub label_smoothing: f64,
}
impl Default for CrossEntropyOptions<'_> {
fn default() -> Self {
Self {
weight: None,
reduction: Reduction::Mean,
ignore_index: -100,
label_smoothing: 0.0,
}
}
}
pub fn cross_entropy_with_options(
input: &Tensor,
target: &Tensor,
options: CrossEntropyOptions<'_>,
) -> Result<Tensor> {
validate(input, target, options.reduction)?;
if !options.label_smoothing.is_finite() || !(0.0..=1.0).contains(&options.label_smoothing) {
return Err(invalid("label_smoothing", "must be finite and in [0, 1]"));
}
if let Some(weight) = options.weight {
ensure_device("class weights", weight, input_device(input)?)?;
}
input
.f_cross_entropy_loss(
target,
options.weight,
options.reduction,
options.ignore_index,
options.label_smoothing,
)
.map_err(Into::into)
}
pub fn mse_loss_with_reduction(
input: &Tensor,
target: &Tensor,
reduction: Reduction,
) -> Result<Tensor> {
validate(input, target, reduction)?;
input.f_mse_loss(target, reduction).map_err(Into::into)
}
pub fn l1_loss(input: &Tensor, target: &Tensor, reduction: Reduction) -> Result<Tensor> {
validate(input, target, reduction)?;
input.f_l1_loss(target, reduction).map_err(Into::into)
}
pub fn smooth_l1_loss(
input: &Tensor,
target: &Tensor,
beta: f64,
reduction: Reduction,
) -> Result<Tensor> {
validate(input, target, reduction)?;
if !beta.is_finite() || beta < 0.0 {
return Err(invalid("beta", "must be finite and nonnegative"));
}
input
.f_smooth_l1_loss(target, reduction, beta)
.map_err(Into::into)
}
pub fn huber_loss(
input: &Tensor,
target: &Tensor,
delta: f64,
reduction: Reduction,
) -> Result<Tensor> {
validate(input, target, reduction)?;
if !delta.is_finite() || delta <= 0.0 {
return Err(invalid("delta", "must be finite and positive"));
}
input
.f_huber_loss(target, reduction, delta)
.map_err(Into::into)
}
pub fn binary_cross_entropy(
input: &Tensor,
target: &Tensor,
weight: Option<&Tensor>,
reduction: Reduction,
) -> Result<Tensor> {
validate_binary(input, target, weight, reduction)?;
input
.f_binary_cross_entropy(target, weight, reduction)
.map_err(Into::into)
}
pub fn binary_cross_entropy_with_logits(
input: &Tensor,
target: &Tensor,
weight: Option<&Tensor>,
pos_weight: Option<&Tensor>,
reduction: Reduction,
) -> Result<Tensor> {
validate_binary(input, target, weight, reduction)?;
if let Some(weight) = pos_weight {
ensure_device("positive weights", weight, input_device(input)?)?;
}
input
.f_binary_cross_entropy_with_logits(target, weight, pos_weight, reduction)
.map_err(Into::into)
}
pub fn nll_loss(
input: &Tensor,
target: &Tensor,
weight: Option<&Tensor>,
ignore_index: i64,
reduction: Reduction,
) -> Result<Tensor> {
validate(input, target, reduction)?;
if let Some(weight) = weight {
ensure_device("class weights", weight, input_device(input)?)?;
}
input
.f_nll_loss_nd(target, weight, reduction, ignore_index)
.map_err(Into::into)
}
fn validate(input: &Tensor, target: &Tensor, reduction: Reduction) -> Result<()> {
ensure_device("loss target", target, input_device(input)?)?;
if !matches!(
reduction,
Reduction::None | Reduction::Mean | Reduction::Sum
) {
return Err(invalid("reduction", "must be None, Mean or Sum"));
}
Ok(())
}
fn validate_binary(
input: &Tensor,
target: &Tensor,
weight: Option<&Tensor>,
reduction: Reduction,
) -> Result<()> {
validate(input, target, reduction)?;
if input.size() != target.size() {
return Err(RustTorchError::ShapeMismatch {
name: "binary loss target".into(),
expected: input.size(),
actual: target.size(),
});
}
if let Some(weight) = weight {
ensure_device("loss weight", weight, input_device(input)?)?;
}
Ok(())
}
fn invalid(field: &'static str, reason: &str) -> RustTorchError {
RustTorchError::InvalidConfiguration {
field,
reason: reason.into(),
}
}