Merge branch 'main' into remove_wrapper

This commit is contained in:
Nicolas Patry
2023-07-19 18:53:55 +02:00
committed by GitHub
24 changed files with 282 additions and 1730 deletions

View File

@ -4,9 +4,9 @@ mod model;
use anyhow::{anyhow, Error as E, Result};
use candle::Tensor;
use candle_hub::{api::sync::Api, Cache, Repo, RepoType};
use candle_nn::VarBuilder;
use clap::Parser;
use hf_hub::{api::sync::Api, Cache, Repo, RepoType};
use model::{BertModel, Config, DTYPE};
use tokenizers::{PaddingParams, Tokenizer};

View File

@ -5,10 +5,10 @@ extern crate intel_mkl_src;
use anyhow::{Error as E, Result};
use candle::{DType, Device, Tensor};
use candle_hub::{api::sync::Api, Repo, RepoType};
use candle_nn::VarBuilder;
use candle_transformers::generation::LogitsProcessor;
use clap::Parser;
use hf_hub::{api::sync::Api, Repo, RepoType};
use tokenizers::Tokenizer;
mod model;

View File

@ -16,9 +16,9 @@ use anyhow::{Error as E, Result};
use clap::Parser;
use candle::{DType, Device, Tensor, D};
use candle_hub::{api::sync::Api, Repo, RepoType};
use candle_nn::VarBuilder;
use candle_transformers::generation::LogitsProcessor;
use hf_hub::{api::sync::Api, Repo, RepoType};
mod model;
use model::{Config, Llama};

View File

@ -0,0 +1,44 @@
// This should rearch 91.5% accuracy.
#[cfg(feature = "mkl")]
extern crate intel_mkl_src;
use anyhow::Result;
use candle::{DType, Var, D};
const IMAGE_DIM: usize = 784;
const LABELS: usize = 10;
pub fn main() -> Result<()> {
let dev = candle::Device::cuda_if_available(0)?;
let m = candle_nn::vision::mnist::load_dir("data")?;
println!("train-images: {:?}", m.train_images.shape());
println!("train-labels: {:?}", m.train_labels.shape());
println!("test-images: {:?}", m.test_images.shape());
println!("test-labels: {:?}", m.test_labels.shape());
let ws = Var::zeros((IMAGE_DIM, LABELS), DType::F32, &dev)?;
let bs = Var::zeros(LABELS, DType::F32, &dev)?;
let sgd = candle_nn::SGD::new(&[&ws, &bs], 0.1);
for epoch in 1..200 {
let logits = m.train_images.matmul(&ws)?.broadcast_add(&bs)?;
let loss = logits.softmax(D::Minus1)?;
// TODO: log_softmax + let loss = loss.nll_loss(&m.train_labels);
sgd.backward_step(&loss)?;
let _test_logits = m.test_images.matmul(&ws)?.broadcast_add(&bs)?;
/* TODO
let test_accuracy = test_logits
.argmax(Some(-1), false)
.eq_tensor(&m.test_labels)
.to_kind(Kind::Float)
.mean(Kind::Float)
.double_value(&[]);
*/
let test_accuracy = 0.;
println!(
"{epoch:4} train loss: {:8.5} test acc: {:5.2}%",
loss.to_scalar::<f32>()?,
100. * test_accuracy
)
}
Ok(())
}

View File

@ -11,9 +11,9 @@ extern crate intel_mkl_src;
use anyhow::{Error as E, Result};
use candle::{safetensors::Load, DType, Device, Tensor};
use candle_hub::{api::sync::Api, Repo, RepoType};
use candle_nn::VarBuilder;
use clap::Parser;
use hf_hub::{api::sync::Api, Repo, RepoType};
use rand::{distributions::Distribution, SeedableRng};
use tokenizers::Tokenizer;