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]>
127 lines
3.8 KiB
Rust
127 lines
3.8 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::{Module, VarBuilder};
|
|
use hanzo_transformers::models::convnext;
|
|
|
|
#[derive(Clone, Copy, Debug, ValueEnum)]
|
|
enum Which {
|
|
Atto,
|
|
Femto,
|
|
Pico,
|
|
Nano,
|
|
Tiny,
|
|
Small,
|
|
Base,
|
|
Large,
|
|
AttoV2,
|
|
FemtoV2,
|
|
PicoV2,
|
|
NanoV2,
|
|
TinyV2,
|
|
BaseV2,
|
|
LargeV2,
|
|
XLarge,
|
|
Huge,
|
|
}
|
|
|
|
impl Which {
|
|
fn model_filename(&self) -> String {
|
|
let name = match self {
|
|
Self::Atto => "convnext_atto.d2_in1k",
|
|
Self::Femto => "convnext_femto.d1_in1k",
|
|
Self::Pico => "convnext_pico.d1_in1k",
|
|
Self::Nano => "convnext_nano.d1h_in1k",
|
|
Self::Tiny => "convnext_tiny.fb_in1k",
|
|
Self::Small => "convnext_small.fb_in1k",
|
|
Self::Base => "convnext_base.fb_in1k",
|
|
Self::Large => "convnext_large.fb_in1k",
|
|
Self::AttoV2 => "convnextv2_atto.fcmae_ft_in1k",
|
|
Self::FemtoV2 => "convnextv2_femto.fcmae_ft_in1k",
|
|
Self::PicoV2 => "convnextv2_pico.fcmae_ft_in1k",
|
|
Self::NanoV2 => "convnextv2_nano.fcmae_ft_in1k",
|
|
Self::TinyV2 => "convnextv2_tiny.fcmae_ft_in1k",
|
|
Self::BaseV2 => "convnextv2_base.fcmae_ft_in1k",
|
|
Self::LargeV2 => "convnextv2_large.fcmae_ft_in1k",
|
|
Self::XLarge => "convnext_xlarge.fb_in22k_ft_in1k",
|
|
Self::Huge => "convnextv2_huge.fcmae_ft_in1k",
|
|
};
|
|
|
|
format!("timm/{name}")
|
|
}
|
|
|
|
fn config(&self) -> convnext::Config {
|
|
match self {
|
|
Self::Atto | Self::AttoV2 => convnext::Config::atto(),
|
|
Self::Femto | Self::FemtoV2 => convnext::Config::femto(),
|
|
Self::Pico | Self::PicoV2 => convnext::Config::pico(),
|
|
Self::Nano | Self::NanoV2 => convnext::Config::nano(),
|
|
Self::Tiny | Self::TinyV2 => convnext::Config::tiny(),
|
|
Self::Small => convnext::Config::small(),
|
|
Self::Base | Self::BaseV2 => convnext::Config::base(),
|
|
Self::Large | Self::LargeV2 => convnext::Config::large(),
|
|
Self::XLarge => convnext::Config::xlarge(),
|
|
Self::Huge => convnext::Config::huge(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Parser)]
|
|
struct Args {
|
|
#[arg(long)]
|
|
model: Option<String>,
|
|
|
|
#[arg(long)]
|
|
image: String,
|
|
|
|
/// Run on CPU rather than on GPU.
|
|
#[arg(long)]
|
|
cpu: bool,
|
|
|
|
#[arg(value_enum, long, default_value_t=Which::Tiny)]
|
|
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 model_file = match args.model {
|
|
None => {
|
|
let model_name = args.which.model_filename();
|
|
let api = hanzo_ml_examples::hub::Api::new()?;
|
|
let api = api.model(model_name);
|
|
api.get("model.safetensors")?
|
|
}
|
|
Some(model) => model.into(),
|
|
};
|
|
|
|
let vb = unsafe { VarBuilder::from_mmaped_safetensors(&[model_file], DType::F32, &device)? };
|
|
let model = convnext::convnext(&args.which.config(), 1000, 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(())
|
|
}
|