mirror of
https://github.com/hanzoai/ml.git
synced 2026-11-20 22:12:15 +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)
40 lines
1.3 KiB
Rust
40 lines
1.3 KiB
Rust
#[cfg(feature = "mkl")]
|
|
extern crate intel_mkl_src;
|
|
|
|
#[cfg(feature = "accelerate")]
|
|
extern crate accelerate_src;
|
|
|
|
use hanzo_ml::{DType, Device, Result, Tensor};
|
|
use hanzo_nn::{linear, AdamW, Linear, Module, Optimizer, ParamsAdamW, VarBuilder, VarMap};
|
|
|
|
fn gen_data() -> Result<(Tensor, Tensor)> {
|
|
// Generate some sample linear data.
|
|
let w_gen = Tensor::new(&[[3f32, 1.]], &Device::Cpu)?;
|
|
let b_gen = Tensor::new(-2f32, &Device::Cpu)?;
|
|
let gen = Linear::new(w_gen, Some(b_gen));
|
|
let sample_xs = Tensor::new(&[[2f32, 1.], [7., 4.], [-4., 12.], [5., 8.]], &Device::Cpu)?;
|
|
let sample_ys = gen.forward(&sample_xs)?;
|
|
Ok((sample_xs, sample_ys))
|
|
}
|
|
|
|
fn main() -> Result<()> {
|
|
let (sample_xs, sample_ys) = gen_data()?;
|
|
|
|
// Use backprop to run a linear regression between samples and get the coefficients back.
|
|
let varmap = VarMap::new();
|
|
let vb = VarBuilder::from_varmap(&varmap, DType::F32, &Device::Cpu);
|
|
let model = linear(2, 1, vb.pp("linear"))?;
|
|
let params = ParamsAdamW {
|
|
lr: 0.1,
|
|
..Default::default()
|
|
};
|
|
let mut opt = AdamW::new(varmap.all_vars(), params)?;
|
|
for step in 0..10000 {
|
|
let ys = model.forward(&sample_xs)?;
|
|
let loss = ys.sub(&sample_ys)?.sqr()?.sum_all()?;
|
|
opt.backward_step(&loss)?;
|
|
println!("{step} {}", loss.to_vec0::<f32>()?);
|
|
}
|
|
Ok(())
|
|
}
|