diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..16411f16 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +target +Cargo.lock +data diff --git a/README.md b/README.md index 3b4a4365..071a043d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Halo2 Deep Learning +# Halo2 Deep Learning [![Test](https://github.com/jasonmorton/halo2deeplearning/workflows/Rust/badge.svg)](https://github.com/jasonmorton/halo2deeplearning/actions?query=workflow%3ARust) @@ -14,3 +14,8 @@ The MNIST inference example (`test_prove_mnist_inference`) is by default ignored cargo test --release -- --ignored --nocapture ``` or ``--include-ignored` to run together with the rest. + +Note that the library requires a nightly version of the rust toolchain. You can change the default toolchain by running: +```bash +rustup override set nightly +``` diff --git a/src/affine1d.rs b/src/affine1d.rs index 37a1ff38..5537406e 100644 --- a/src/affine1d.rs +++ b/src/affine1d.rs @@ -1,10 +1,7 @@ use halo2_proofs::{ arithmetic::FieldExt, - circuit::{layouter, AssignedCell, Layouter, Region, Value}, - plonk::{ - create_proof, keygen_pk, keygen_vk, verify_proof, Advice, Assigned, Circuit, Column, - ConstraintSystem, Constraints, Error, Expression, Selector, - }, + circuit::{AssignedCell, Layouter, Region, Value}, + plonk::{Advice, Assigned, Column, ConstraintSystem, Constraints, Expression, Selector}, poly::Rotation, }; use std::marker::PhantomData; @@ -129,7 +126,7 @@ where // calculate value of output let mut output: Vec>> = - (0..OUT).map(|i| Value::known(F::zero().into())).collect(); + (0..OUT).map(|_| Value::known(F::zero().into())).collect(); for i in 0..OUT { for j in 0..IN { @@ -189,7 +186,7 @@ where constraints[i].clone() + virtual_cells.query_advice(bias, Rotation(i as i32)); } - let constraints = (0..OUT).map(|i| "c").zip(constraints); + let constraints = (0..OUT).map(|_| "c").zip(constraints); Constraints::with_selector(q, constraints) }); @@ -274,8 +271,8 @@ impl Affine1d>>> = map2::<_, _, OUT, IN>(|i, j| Value::known(i32tofelt::(weights[i][j]).into())); - let input: Vec>> = (0..IN).map(|i| Value::default()).collect(); - let output: Vec>> = (0..OUT).map(|i| Value::default()).collect(); + let input: Vec>> = (0..IN).map(|_| Value::default()).collect(); + let output: Vec>> = (0..OUT).map(|_| Value::default()).collect(); Affine1d { input, @@ -292,7 +289,7 @@ impl Affine1d>> = - (0..OUT).map(|i| Value::known(F::zero().into())).collect(); + (0..OUT).map(|_| Value::known(F::zero().into())).collect(); for i in 0..OUT { for j in 0..IN { @@ -309,11 +306,3 @@ impl Affine1d, advices: Vec>) -> Self { - let output_height = (IMAGE_HEIGHT + 2 * PADDING - KERNEL_HEIGHT) / STRIDE + 1; + let _output_height = (IMAGE_HEIGHT + 2 * PADDING - KERNEL_HEIGHT) / STRIDE + 1; let output_width = (IMAGE_WIDTH + 2 * PADDING - KERNEL_WIDTH) / STRIDE + 1; for advice in advices.iter() { @@ -408,7 +408,7 @@ mod tests { config: Self::Config, mut layouter: impl Layouter, ) -> Result<(), Error> { - let output = config.assign(&mut layouter, self.image, self.kernels)?; + let _output = config.assign(&mut layouter, self.image, self.kernels)?; Ok(()) } } @@ -416,7 +416,7 @@ mod tests { #[test] fn test_cnvrl() { // use halo2_proofs::pasta::pallas; - use halo2curves::pasta::{pallas, vesta}; + use halo2curves::pasta::{pallas}; const KERNEL_HEIGHT: usize = 3; const KERNEL_WIDTH: usize = 3; diff --git a/src/conv2d_mnist_example.rs b/src/conv2d_mnist_example.rs index 99721fd4..bc091a17 100644 --- a/src/conv2d_mnist_example.rs +++ b/src/conv2d_mnist_example.rs @@ -1,39 +1,21 @@ -use halo2_proofs::{ - poly::{ - commitment::ParamsProver, - ipa::{ - commitment::{IPACommitmentScheme, ParamsIPA}, - multiopen::ProverIPA, - strategy::SingleStrategy, - }, - VerificationStrategy, - }, - transcript::{TranscriptReadBuffer, TranscriptWriterBuffer}, -}; - use halo2_proofs::{ arithmetic::FieldExt, circuit::{Layouter, SimpleFloorPlanner, Value}, - plonk::{ - create_proof, keygen_pk, keygen_vk, verify_proof, Advice, Assigned, Circuit, Column, - ConstraintSystem, Error, Instance, - }, - poly::{commitment::Params, Rotation}, - transcript::{Blake2bRead, Blake2bWrite, Challenge255}, + plonk::{Circuit, Column, ConstraintSystem, Error, Instance}, }; -use halo2curves::pasta::{pallas, vesta}; + //use pasta_curves::{pallas, vesta}; // use rand::rngs::OsRng; // use std::marker::PhantomData; -use crate::fieldutils::{felt_to_i32, i32tofelt}; +use crate::fieldutils::felt_to_i32; //use crate::tensorutils::{dot3, flatten3, flatten4, map2, map3, map3r, map4, map4r}; use std::cmp::max; -use crate::affine1d::{Affine1d, Affine1dConfig}; +use crate::affine1d::Affine1dConfig; use crate::cnvrl_generic; -use crate::eltwise::{DivideBy, Nonlin1d, NonlinConfig1d, ReLu}; +use crate::eltwise::{DivideBy, NonlinConfig1d, ReLu}; #[derive(Clone)] struct MyConfig< @@ -179,7 +161,7 @@ where // Here we wire together the layers by using the output advice in each layer as input advice in the next (not with copying / equality). // This can be automated but we will sometimes want skip connections, etc. so we need the flexibility. fn configure(cs: &mut ConstraintSystem) -> Self::Config { - let output_height = (IMAGE_HEIGHT + 2 * PADDING - KERNEL_HEIGHT) / STRIDE + 1; + let _output_height = (IMAGE_HEIGHT + 2 * PADDING - KERNEL_HEIGHT) / STRIDE + 1; let output_width = (IMAGE_WIDTH + 2 * PADDING - KERNEL_WIDTH) / STRIDE + 1; // (IMAGE_HEIGHT + 2 * PADDING - KERNEL_HEIGHT) / STRIDE + 1 }, @@ -295,13 +277,13 @@ where #[cfg(test)] mod tests { use super::*; - use crate::fieldutils::felt_to_i32; + + use crate::fieldutils::i32tofelt; use halo2_proofs::{ - arithmetic::{Field, FieldExt}, - dev::{FailureLocation, MockProver, VerifyFailure}, - // pasta::Fp as F, - plonk::{Any, Circuit}, + plonk::{create_proof, keygen_pk, keygen_vk, verify_proof, Circuit}, + transcript::{Blake2bRead, Blake2bWrite, Challenge255}, }; + use halo2curves::pasta::vesta; use halo2_proofs::{ poly::{ @@ -318,16 +300,16 @@ mod tests { use halo2curves::pasta::Fp as F; // use nalgebra; - use crate::cnvrl_generic::matrix; + use crate::fieldutils; use crate::moreparams; use crate::tensorutils::map4; - use halo2curves::pasta::pallas; + use mnist::*; use ndarray::prelude::*; - use rand::prelude::*; + // use rand::prelude::*; use rand::rngs::OsRng; - use std::time::{Duration, Instant}; + use std::time::Instant; const K: u32 = 17; @@ -353,8 +335,8 @@ mod tests { let Mnist { trn_img, trn_lbl, - tst_img, - tst_lbl, + tst_img: _, + tst_lbl: _, .. } = MnistBuilder::new() .label_format_digit() diff --git a/src/eltwise.rs b/src/eltwise.rs index 65f1f983..43ce8ccb 100644 --- a/src/eltwise.rs +++ b/src/eltwise.rs @@ -1,18 +1,13 @@ use halo2_proofs::{ arithmetic::FieldExt, circuit::{AssignedCell, Layouter, Region, SimpleFloorPlanner, Value}, - plonk::{ - create_proof, keygen_pk, keygen_vk, verify_proof, Advice, Assigned, Circuit, Column, - ConstraintSystem, Constraints, Error, Expression, Instance, Selector, TableColumn, - }, - poly::{commitment::Params, Rotation}, - transcript::{Blake2bRead, Blake2bWrite, Challenge255}, + plonk::{Advice, Assigned, Circuit, Column, ConstraintSystem, Error, Selector, TableColumn}, + poly::Rotation, }; -use halo2curves::pasta::{pallas, vesta}; -use std::{fmt::format, marker::PhantomData}; + +use std::marker::PhantomData; use crate::fieldutils::{self, felt_to_i32, i32tofelt}; -use crate::tensorutils::flatten3; pub trait Nonlinearity { fn nonlinearity(x: i32) -> F; @@ -36,7 +31,7 @@ impl> Nonlin1d Nonlin1d>, LEN, NL> { - Nonlin1d::>, LEN, NL>::fill(|i| Value::default()) + Nonlin1d::>, LEN, NL>::fill(|_| Value::default()) } } @@ -259,11 +254,6 @@ impl< fn without_witnesses(&self) -> Self { self.clone() - // let assigned = Nonlin1d::>, LEN, NL>::fill(|i| Value::default()); - // Self { - // assigned, - // _marker: PhantomData, - // } } fn configure(cs: &mut ConstraintSystem) -> Self::Config { @@ -334,15 +324,10 @@ impl Nonlinearity for DivideBy { #[cfg(test)] mod tests { use super::*; - use halo2_proofs::{ - dev::{FailureLocation, MockProver, VerifyFailure}, - plonk::{Any, Circuit}, - }; + use crate::tensorutils::flatten3; + use halo2_proofs::dev::MockProver; use halo2curves::pasta::Fp as F; - // use nalgebra; - use std::time::{Duration, Instant}; - #[test] fn test_eltrelunl() { let k = 9; //2^k rows @@ -368,7 +353,7 @@ mod tests { #[test] fn test_eltsigmoid() { for i in -127..127 { - let r = as Nonlinearity>::nonlinearity(i); + let _r = as Nonlinearity>::nonlinearity(i); // println!("{i}, {:?}", r); } } @@ -376,7 +361,7 @@ mod tests { #[test] fn test_eltdivide() { for i in -127..127 { - let r = as Nonlinearity>::nonlinearity(i); + let _r = as Nonlinearity>::nonlinearity(i); // println!("{i}, {:?}", r); } } diff --git a/src/fieldutils.rs b/src/fieldutils.rs index 147e095d..475f9d0a 100644 --- a/src/fieldutils.rs +++ b/src/fieldutils.rs @@ -26,7 +26,6 @@ pub fn felt_to_i32(x: F) -> i32 { mod test { use super::*; - // use halo2_proofs::pasta::Fp as F; use halo2curves::pasta::Fp as F; #[test] diff --git a/src/inputlayer.rs b/src/inputlayer.rs index 822ec9ee..e08d4f85 100644 --- a/src/inputlayer.rs +++ b/src/inputlayer.rs @@ -1,11 +1,10 @@ use halo2_proofs::{ arithmetic::FieldExt, - circuit::{layouter, AssignedCell, Layouter, Region, Value}, + circuit::{AssignedCell, Layouter, Value}, plonk::{ - create_proof, keygen_pk, keygen_vk, verify_proof, Advice, Assigned, Circuit, Column, - ConstraintSystem, Constraints, Error, Expression, Selector, + Advice, Assigned, Column, + ConstraintSystem, Selector, }, - poly::Rotation, }; use std::marker::PhantomData; diff --git a/src/mlprescale.rs b/src/mlprescale.rs index 31b7073d..eb5ae2f1 100644 --- a/src/mlprescale.rs +++ b/src/mlprescale.rs @@ -1,9 +1,8 @@ use halo2_proofs::{ arithmetic::FieldExt, - circuit::{AssignedCell, Layouter, SimpleFloorPlanner, Value}, + circuit::{Layouter, SimpleFloorPlanner}, plonk::{ //create_proof, keygen_pk, keygen_vk, verify_proof, Advice, - Assigned, Circuit, Column, ConstraintSystem, @@ -16,7 +15,7 @@ use halo2_proofs::{ //use pasta_curves::{pallas, vesta}; // use rand::rngs::OsRng; // use std::marker::PhantomData; -use crate::fieldutils::i32tofelt; + use std::marker::PhantomData; //use crate::tensorutils::{dot3, flatten3, flatten4, map2, map3, map3r, map4, map4r}; @@ -154,11 +153,11 @@ where #[cfg(test)] mod tests { use super::*; - use crate::fieldutils::felt_to_i32; - use halo2_proofs::dev::{FailureLocation, MockProver, VerifyFailure}; + + use crate::fieldutils::i32tofelt; + use halo2_proofs::dev::MockProver; use halo2curves::pasta::Fp as F; - // use rand::prelude::*; - // use std::time::{Duration, Instant}; + #[test] fn test_rescale() { diff --git a/src/tensorutils.rs b/src/tensorutils.rs index 486dd840..04d63ee3 100644 --- a/src/tensorutils.rs +++ b/src/tensorutils.rs @@ -120,32 +120,26 @@ pub fn flatten4(nested: Vec>>>) -> Vec { } pub fn dot3( - a: &Vec>>>, - b: &Vec>>>, + a: &[Vec>>], + b: &[Vec>>], ) -> Expression { - let aflat = a.into_iter().flatten().flatten(); //flatten3(a); - let bflat = b.into_iter().flatten().flatten(); - let result = aflat + let aflat = a.iter().flatten().flatten(); //flatten3(a); + let bflat = b.iter().flatten().flatten(); + aflat .zip(bflat) .map(|(x, y)| x.clone() * y.clone()) - .fold(Expression::Constant(F::zero()), |sum, next| { - sum.clone() + next.clone() - }); - result + .fold(Expression::Constant(F::zero()), |sum, next| sum + next) } -pub fn dot3u(a: &Vec>>, b: &Vec>>) -> u64 { - let aflat = a.into_iter().flatten().flatten(); //flatten3(a); - let bflat = b.into_iter().flatten().flatten(); - let result = aflat - .zip(bflat) - .map(|(x, y)| x.clone() * y.clone()) - .fold(0u64, |sum, next| sum.clone() + next.clone()); - result +pub fn dot3u(a: &[Vec>], b: &[Vec>]) -> u64 { + let aflat = a.iter().flatten().flatten(); //flatten3(a); + let bflat = b.iter().flatten().flatten(); + aflat.zip(bflat).map(|(x, y)| x.clone() * y.clone()).sum() } mod test { use super::*; + #[test] fn indices4() { let t: Vec>>> =