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]>
83 lines
2.6 KiB
Rust
83 lines
2.6 KiB
Rust
//! EVA-02: Explore the limits of Visual representation at scAle
|
|
//! https://github.com/baaivision/EVA
|
|
|
|
#[cfg(feature = "mkl")]
|
|
extern crate intel_mkl_src;
|
|
|
|
#[cfg(feature = "accelerate")]
|
|
extern crate accelerate_src;
|
|
|
|
use clap::Parser;
|
|
|
|
use hanzo_ml::{DType, Device, IndexOp, Result, Tensor, D};
|
|
use hanzo_nn::{Module, VarBuilder};
|
|
use hanzo_transformers::models::eva2;
|
|
|
|
/// Loads an image from disk using the image crate, this returns a tensor with shape
|
|
/// (3, 448, 448). OpenAI normalization is applied.
|
|
pub fn load_image448_openai_norm<P: AsRef<std::path::Path>>(p: P) -> Result<Tensor> {
|
|
let img = image::ImageReader::open(p)?
|
|
.decode()
|
|
.map_err(hanzo_ml::Error::wrap)?
|
|
.resize_to_fill(448, 448, image::imageops::FilterType::Triangle);
|
|
let img = img.to_rgb8();
|
|
let data = img.into_raw();
|
|
let data = Tensor::from_vec(data, (448, 448, 3), &Device::Cpu)?.permute((2, 0, 1))?;
|
|
let mean =
|
|
Tensor::new(&[0.48145466f32, 0.4578275, 0.40821073], &Device::Cpu)?.reshape((3, 1, 1))?;
|
|
let std = Tensor::new(&[0.26862954f32, 0.261_302_6, 0.275_777_1], &Device::Cpu)?
|
|
.reshape((3, 1, 1))?;
|
|
(data.to_dtype(hanzo_ml::DType::F32)? / 255.)?
|
|
.broadcast_sub(&mean)?
|
|
.broadcast_div(&std)
|
|
}
|
|
|
|
#[derive(Parser)]
|
|
struct Args {
|
|
#[arg(long)]
|
|
model: Option<String>,
|
|
|
|
#[arg(long)]
|
|
image: String,
|
|
|
|
/// Run on CPU rather than on GPU.
|
|
#[arg(long)]
|
|
cpu: bool,
|
|
}
|
|
|
|
pub fn main() -> anyhow::Result<()> {
|
|
let args = Args::parse();
|
|
|
|
let device = hanzo_ml_examples::device(args.cpu)?;
|
|
|
|
let image = load_image448_openai_norm(args.image)?.to_device(&device)?;
|
|
println!("loaded image {image:?}");
|
|
|
|
let model_file = match args.model {
|
|
None => {
|
|
let api = hanzo_ml_examples::hub::Api::new()?;
|
|
let api = api.model("vincent-espitalier/hanzo-ml-eva2");
|
|
api.get("eva02_base_patch14_448.mim_in22k_ft_in22k_in1k_adapted.safetensors")?
|
|
}
|
|
Some(model) => model.into(),
|
|
};
|
|
let vb = unsafe { VarBuilder::from_mmaped_safetensors(&[model_file], DType::F32, &device)? };
|
|
|
|
let model = eva2::vit_base(vb)?;
|
|
println!("model built");
|
|
let logits = model.forward(&image.unsqueeze(0)?)?;
|
|
let prs = hanzo_nn::ops::softmax(&logits, D::Minus1)?
|
|
.i(0)?
|
|
.to_vec1::<f32>()?;
|
|
let mut prs = prs.iter().enumerate().collect::<Vec<_>>();
|
|
prs.sort_by(|(_, p1), (_, p2)| p2.total_cmp(p1));
|
|
for &(category_idx, pr) in prs.iter().take(5) {
|
|
println!(
|
|
"{:24}: {:.2}%",
|
|
hanzo_ml_examples::imagenet::CLASSES[category_idx],
|
|
100. * pr
|
|
);
|
|
}
|
|
Ok(())
|
|
}
|