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]>
78 lines
2.1 KiB
Rust
78 lines
2.1 KiB
Rust
#[cfg(feature = "mkl")]
|
|
extern crate intel_mkl_src;
|
|
|
|
#[cfg(feature = "accelerate")]
|
|
extern crate accelerate_src;
|
|
|
|
use clap::{Parser, ValueEnum};
|
|
use hanzo_ml::{DType, IndexOp, D};
|
|
use hanzo_nn::{ModuleT, VarBuilder};
|
|
use hanzo_transformers::models::vgg::{Models, Vgg};
|
|
|
|
#[derive(Clone, Copy, Debug, ValueEnum)]
|
|
enum Which {
|
|
Vgg13,
|
|
Vgg16,
|
|
Vgg19,
|
|
}
|
|
|
|
#[derive(Parser)]
|
|
struct Args {
|
|
#[arg(long)]
|
|
image: String,
|
|
|
|
/// Run on CPU rather than on GPU.
|
|
#[arg(long)]
|
|
cpu: bool,
|
|
|
|
/// Variant of the model to use.
|
|
#[arg(value_enum, long, default_value_t = Which::Vgg13)]
|
|
which: Which,
|
|
}
|
|
|
|
pub fn main() -> anyhow::Result<()> {
|
|
let args = Args::parse();
|
|
let device = hanzo_ml_examples::device(args.cpu)?;
|
|
let image = hanzo_ml_examples::imagenet::load_image224(args.image)?.to_device(&device)?;
|
|
|
|
println!("loaded image {image:?}");
|
|
|
|
let api = hanzo_ml_examples::hub::Api::new()?;
|
|
let repo = match args.which {
|
|
Which::Vgg13 => "timm/vgg13.tv_in1k",
|
|
Which::Vgg16 => "timm/vgg16.tv_in1k",
|
|
Which::Vgg19 => "timm/vgg19.tv_in1k",
|
|
};
|
|
let api = api.model(repo);
|
|
let filename = "model.safetensors";
|
|
let model_file = api.get(filename)?;
|
|
|
|
let vb = unsafe { VarBuilder::from_mmaped_safetensors(&[model_file], DType::F32, &device)? };
|
|
let model = match args.which {
|
|
Which::Vgg13 => Vgg::new(vb, Models::Vgg13)?,
|
|
Which::Vgg16 => Vgg::new(vb, Models::Vgg16)?,
|
|
Which::Vgg19 => Vgg::new(vb, Models::Vgg19)?,
|
|
};
|
|
let logits = model.forward_t(&image, /*train=*/ false)?;
|
|
|
|
let prs = hanzo_nn::ops::softmax(&logits, D::Minus1)?
|
|
.i(0)?
|
|
.to_vec1::<f32>()?;
|
|
|
|
// Sort the predictions and take the top 5
|
|
let mut top: Vec<_> = prs.iter().enumerate().collect();
|
|
top.sort_by(|a, b| b.1.partial_cmp(a.1).unwrap());
|
|
let top = top.into_iter().take(5).collect::<Vec<_>>();
|
|
|
|
// Print the top predictions
|
|
for &(i, p) in &top {
|
|
println!(
|
|
"{:50}: {:.2}%",
|
|
hanzo_ml_examples::imagenet::CLASSES[i],
|
|
p * 100.0
|
|
);
|
|
}
|
|
|
|
Ok(())
|
|
}
|