oxidelm 0.2.0

Bare-metal pure-Rust Transformer engine with reverse-mode autograd, BPE tokenization, and wgpu acceleration
Documentation
use oxidelm::anomaly::{set_anomaly_training_step, set_detect_anomaly, test_lock};
use oxidelm::Tensor;
use std::panic::{catch_unwind, AssertUnwindSafe};

fn assert_close(actual: &[f32], expected: &[f32]) {
    assert_eq!(actual.len(), expected.len());
    for (actual, expected) in actual.iter().zip(expected.iter()) {
        assert!((actual - expected).abs() < 1e-5);
    }
}

#[test]
fn public_api_supports_forward_backward_and_sgd() {
    let input = Tensor::new(vec![1.0, 2.0], vec![2], false);
    let weights = Tensor::new(vec![0.5, -0.5], vec![2], true);
    let target = Tensor::scalar(1.0, false);

    let prediction = (&input * &weights).sum();
    let error = &prediction - &target;
    let loss = error.powf(2.0);
    loss.backward();

    assert_close(&weights.grad().unwrap(), &[-3.0, -6.0]);
    weights.sgd_step(0.1);
    assert_close(&weights.data(), &[0.8, 0.1]);
}

#[test]
fn anomaly_mode_reports_operation_shape_and_training_step() {
    let _guard = test_lock();
    set_detect_anomaly(true);
    set_anomaly_training_step(7);
    let result = catch_unwind(AssertUnwindSafe(|| {
        let left = Tensor::new(vec![1.0, 0.0], vec![2], false);
        let right = Tensor::new(vec![1.0, 0.0], vec![2], false);
        let _ = left.div(&right);
    }));
    set_detect_anomaly(false);
    set_anomaly_training_step(0);

    let message = result.expect_err("div by zero should trip anomaly mode");
    let message = message
        .downcast_ref::<String>()
        .map(String::as_str)
        .or_else(|| message.downcast_ref::<&str>().copied())
        .expect("anomaly panic payload should be a string");
    assert!(message.contains("operation 'div'"), "{message}");
    assert!(message.contains("shape [2]"), "{message}");
    assert!(message.contains("training step 7"), "{message}");
}

#[test]
fn anomaly_mode_is_off_by_default() {
    let _guard = test_lock();
    set_detect_anomaly(false);
    let left = Tensor::new(vec![0.0], vec![1], false);
    let right = Tensor::new(vec![0.0], vec![1], false);
    let output = left.div(&right);
    assert!(output.data()[0].is_nan());
}