Skip to content

Loss trait must be Send + Sync for multithreaded training #279

@noahgift

Description

@noahgift

Problem

The Loss trait in src/loss/mod.rs has no Send + Sync bounds:

pub trait Loss {
    fn compute(&self, y_pred: &Vector<f32>, y_true: &Vector<f32>) -> f32;
    fn name(&self) -> &str;
}

Entrenar uses multithreaded training loops and needs to share loss functions across threads. Without Send + Sync, loss functions from aprender cannot be used in Arc<dyn Loss> or sent to worker threads.

Proposed Fix

pub trait Loss: Send + Sync {
    fn compute(&self, y_pred: &Vector<f32>, y_true: &Vector<f32>) -> f32;
    fn name(&self) -> &str;
}

All existing implementations (MSELoss, MAELoss, HuberLoss, etc.) are plain structs with no interior mutability, so they already satisfy Send + Sync — this is a zero-breaking-change addition.

Context

Entrenar delegates forward loss computation to aprender's standalone functions (mse_loss, mae_loss, huber_loss). Adding Send + Sync to the trait would also allow entrenar to accept Box<dyn Loss + Send + Sync> for dynamic dispatch in training loops.

Cross-ref: entrenar docs/specifications/simplify-entrenar.md Section 6

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions