mirror of
https://github.com/hanzoai/ml.git
synced 2026-11-20 23:08:29 +00:00
Three-way per file through the rename: base 39355c6c91, theirs the upstream
merge of main with 35d7ae7c (via 7ef6a694d), ours origin/main.
Lands the barrier thread pool, runtime CPU feature dispatch, aarch64 and x86
repacked quantized kernels (indexed_gemv, gemv_fused_shared_lhs), quantized
embedding on CPU/Metal/CUDA, paged flash-attn kernels, cutile fused MoE, the
in-place op aliasing API, parking_lot storage locks, Metal buffer builders and
debug labels, safetensors 0.8, hf-hub 1.0, tokenizers 0.23.
Ours keeps ROCm, Vulkan, wgpu, the IQ/MXFP4/NVFP4/TQ dtypes, and the i-quant
weight/activation block split in matmul. Our backends ride InplaceOpN and the
quantized embedding; Metal get_rows gains the IQ kernels. ug leaves, as upstream.
Co-authored-by: Hanzo Dev <[email protected]>
67 lines
1.9 KiB
Rust
67 lines
1.9 KiB
Rust
#![allow(unused)]
|
|
use hanzo_ml::{DType, Device, Result, Tensor};
|
|
use wasm_bindgen_test::*;
|
|
|
|
wasm_bindgen_test_configure!(run_in_browser);
|
|
|
|
const LEN: usize = 20;
|
|
|
|
fn repeat(values: [f32; 4]) -> Vec<f32> {
|
|
values.into_iter().cycle().take(LEN).collect()
|
|
}
|
|
|
|
fn as_f32(tensor: &Tensor) -> Result<Vec<f32>> {
|
|
tensor.to_dtype(DType::F32)?.to_vec1::<f32>()
|
|
}
|
|
|
|
fn add_vectors_with_simd_block_and_tail(dtype: DType) -> Result<()> {
|
|
let lhs = repeat([0.1, 0.3, 0.7, -0.2]);
|
|
let rhs = repeat([0.2, -0.1, 0.05, 1.3]);
|
|
|
|
let lhs = Tensor::from_slice(&lhs, LEN, &Device::Cpu)?.to_dtype(dtype)?;
|
|
let rhs = Tensor::from_slice(&rhs, LEN, &Device::Cpu)?.to_dtype(dtype)?;
|
|
let expected = lhs
|
|
.to_dtype(DType::F32)?
|
|
.add(&rhs.to_dtype(DType::F32)?)?
|
|
.to_dtype(dtype)?;
|
|
let sum = lhs.add(&rhs)?;
|
|
assert_eq!(as_f32(&sum)?, as_f32(&expected)?);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn add_scalar_with_simd_block_and_tail(dtype: DType) -> Result<()> {
|
|
let lhs = repeat([0.1, 0.3, 0.7, -0.2]);
|
|
|
|
let lhs = Tensor::from_slice(&lhs, LEN, &Device::Cpu)?.to_dtype(dtype)?;
|
|
let scalar = Tensor::new(0.2f32, &Device::Cpu)?.to_dtype(dtype)?;
|
|
let expected = lhs
|
|
.to_dtype(DType::F32)?
|
|
.broadcast_add(&scalar.to_dtype(DType::F32)?)?
|
|
.to_dtype(dtype)?;
|
|
let sum = lhs.broadcast_add(&scalar)?;
|
|
assert_eq!(as_f32(&sum)?, as_f32(&expected)?);
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[wasm_bindgen_test]
|
|
fn add_f16_vectors_with_simd128() -> Result<()> {
|
|
add_vectors_with_simd_block_and_tail(DType::F16)
|
|
}
|
|
|
|
#[wasm_bindgen_test]
|
|
fn add_f16_scalar_with_simd128() -> Result<()> {
|
|
add_scalar_with_simd_block_and_tail(DType::F16)
|
|
}
|
|
|
|
#[wasm_bindgen_test]
|
|
fn add_bf16_vectors_with_simd128() -> Result<()> {
|
|
add_vectors_with_simd_block_and_tail(DType::BF16)
|
|
}
|
|
|
|
#[wasm_bindgen_test]
|
|
fn add_bf16_scalar_with_simd128() -> Result<()> {
|
|
add_scalar_with_simd_block_and_tail(DType::BF16)
|
|
}
|