Initial commit

This commit is contained in:
redclawsystems
2026-03-04 00:08:42 +00:00
commit 4d88dc0584
4449 changed files with 1556714 additions and 0 deletions
@@ -0,0 +1,51 @@
//! Loss functions for RTX Transformers
//!
//! This module provides a comprehensive set of loss functions optimized for transformer training,
//! with full autograd integration and support for various reduction modes.
pub mod cross_entropy;
pub mod logcosh;
#[cfg(all(test, feature = "disabled_tests"))]
mod logcosh_simple_test;
// Re-export key types
pub use cross_entropy::{CrossEntropyConfig, CrossEntropyLoss};
pub use logcosh::{
ComparisonResult, LogCoshLoss, LogCoshLossBuilder, LossCharacteristics, LossComparison,
LossStatistics,
};
use crate::Result;
use rtx_tensor::Tensor;
/// Reduction modes for loss computation
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Default)]
pub enum Reduction {
/// No reduction - return per-sample losses
None,
/// Mean reduction - return mean of all losses
#[default]
Mean,
/// Sum reduction - return sum of all losses
Sum,
}
/// Base trait for all loss functions
pub trait Loss {
/// Compute the forward pass of the loss
fn forward(&self, predictions: &Tensor, targets: &Tensor) -> Result<Tensor>;
/// Get the reduction mode used by this loss
fn reduction(&self) -> Reduction;
/// Check if this loss supports backpropagation
fn supports_backprop(&self) -> bool {
true
}
/// Get a human-readable name for this loss
fn name(&self) -> &'static str;
}