mirror of
https://github.com/hanzoai/ml.git
synced 2026-11-20 22:46:50 +00:00
- Updated all crate versions to 0.9.2-alpha.2 - Synced source code with latest upstream candle changes - Added hanzo-ug crate as wrapper for ug functionality - Fixed all import statements to use hanzo-* naming - Updated dependencies to latest versions: * cudarc: 0.16.3 -> 0.18.2 * gemm: 0.17.0 -> 0.18.2 * safetensors: 0.4.1 -> 0.6.0 * ug: 0.4.0 -> 0.5.0 * criterion: 0.5.1 -> 0.7.0 - Added float8 support for enhanced precision - Updated Metal backend to use objc2-metal/objc2-foundation - All core crates now compile and build successfully Core working crates: - hanzo-ml (main ML framework) - hanzo-nn (neural network layers) - hanzo-transformers (transformer models) - hanzo-datasets (data loading utilities) - hanzo-ug (unified GPU backend) - hanzo-kernels (CUDA kernels) - hanzo-metal-kernels (Metal kernels) - hanzo-flash-attn (flash attention) - hanzo-onnx (ONNX support)
79 lines
2.4 KiB
Rust
79 lines
2.4 KiB
Rust
use hanzo_ml::{Device, Result, Tensor};
|
|
use hanzo_transformers::generation::LogitsProcessor;
|
|
|
|
#[test]
|
|
fn sample_with_zero_temperature() -> Result<()> {
|
|
let mut logits_process = LogitsProcessor::new(1337, None, None);
|
|
let logits = Tensor::new(&[0.1, 0.2, 0.3, 0.4], &Device::Cpu)?;
|
|
let token = logits_process.sample(&logits)?;
|
|
assert_eq!(token, 3);
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn sample_with_temperature() -> Result<()> {
|
|
let mut logits_process = LogitsProcessor::new(42, Some(0.9), None);
|
|
let logits = Tensor::new(&[0.1, 0.2, 0.3, 0.4], &Device::Cpu)?;
|
|
let token = logits_process.sample(&logits)?;
|
|
assert_eq!(token, 0);
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn sample_with_top_p() -> Result<()> {
|
|
let mut logits_process = LogitsProcessor::new(42, Some(1.0), Some(0.5));
|
|
let logits = Tensor::new(&[0.1, 0.2, 0.3, 0.4], &Device::Cpu)?;
|
|
let token = logits_process.sample(&logits)?;
|
|
assert_eq!(token, 2);
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn sample_with_top_k() -> Result<()> {
|
|
let mut logits_process = LogitsProcessor::from_sampling(
|
|
42,
|
|
hanzo_transformers::generation::Sampling::TopK {
|
|
k: 1,
|
|
temperature: 1.0,
|
|
},
|
|
);
|
|
let logits = Tensor::new(&[0.1, 0.2, 0.3, 0.4], &Device::Cpu)?;
|
|
let token = logits_process.sample(&logits)?;
|
|
assert_eq!(token, 3);
|
|
let mut logits_process = LogitsProcessor::from_sampling(
|
|
42,
|
|
hanzo_transformers::generation::Sampling::TopK {
|
|
k: 2,
|
|
temperature: 1.0,
|
|
},
|
|
);
|
|
let logits = Tensor::new(&[0.1, 0.2, 0.3, 0.4], &Device::Cpu)?;
|
|
let token = logits_process.sample(&logits)?;
|
|
assert_eq!(token, 3);
|
|
let token = logits_process.sample(&logits)?;
|
|
assert_eq!(token, 2);
|
|
Ok(())
|
|
}
|
|
|
|
#[test]
|
|
fn sample_gumbel() -> Result<()> {
|
|
let mut logits_process = LogitsProcessor::from_sampling(
|
|
42,
|
|
hanzo_transformers::generation::Sampling::GumbelSoftmax { temperature: 1.0 },
|
|
);
|
|
let logits = Tensor::new(&[-1.0, 0.0, 0.2, 1.0], &Device::Cpu)?;
|
|
let sm = hanzo_nn::ops::softmax(&logits, 0)?.to_vec1::<f64>()?;
|
|
let mut counts = vec![0f64; 4];
|
|
let samples = 100000;
|
|
for _ in 0..samples {
|
|
let token = logits_process.sample(&logits)?;
|
|
counts[token as usize] += 1f64 / samples as f64;
|
|
}
|
|
for i in 0..4 {
|
|
if (counts[i] - sm[i]).abs() > 0.05 {
|
|
panic!("pr mismatch {counts:?} {sm:?}");
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|