candding 0.1.0

Pure candle embeddings for Rust with no ONNX: dense, sparse, multi-vector and rerank models today, image models on the roadmap
Documentation
use candle_core::DType;
use candle_core::Device;
use candle_nn::VarBuilder;
use candle_nn::VarMap;

use super::*;

/// Default LayerNorms make a post-norm stack's output exactly zero-mean, unit-variance per token; pre-norm's would not be.
#[test]
fn blocks_are_post_norm_so_the_final_output_is_exactly_normalized() {
  let varmap = VarMap::new();
  let vb = VarBuilder::from_varmap(&varmap, DType::F32, &Device::Cpu);
  let mut config = tiny_config();
  config.num_hidden_layers = 4;
  let model = XlmRobertaFlashModel::load(config, vb, Some(PositionScheme::Rotary)).unwrap();
  let out = run(&model, &[vec![2, 5, 7, 9]]);
  for token in &out[0] {
    let n = token.len() as f32;
    let mean: f32 = token.iter().sum::<f32>() / n;
    let variance: f32 = token.iter().map(|x| (x - mean).powi(2)).sum::<f32>() / n;
    assert!(
      mean.abs() < 1e-3,
      "post-norm output must be centred: mean {mean}"
    );
    assert!(
      (variance - 1.0).abs() < 1e-2,
      "post-norm output must have unit variance: {variance}"
    );
  }
}