Skip to content

Commit

Permalink
Fix mockprover memory (#85)
Browse files Browse the repository at this point in the history
* change mock prover cs, instance to arc type

* remove debugging logs

* clippy fix

* fix ff::BatchInvert clippy complain
  • Loading branch information
DreamWuGit authored Mar 14, 2024
1 parent a244c33 commit c90df71
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
24 changes: 13 additions & 11 deletions halo2_proofs/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use std::ops::{Add, Mul, Neg, Range};
use std::sync::Arc;

use blake2b_simd::blake2b;
#[cfg(feature = "mock-batch-inv")]
#[cfg(any(feature = "mock-batch-inv", feature = "multiphase-mock-prover"))]
use ff::BatchInvert;

use ff::Field;
use ff::FromUniformBytes;

Expand All @@ -23,8 +24,6 @@ use crate::{

#[cfg(feature = "multiphase-mock-prover")]
use crate::{plonk::sealed::SealedPhase, plonk::FirstPhase, plonk::Phase};
#[cfg(feature = "multiphase-mock-prover")]
use ff::BatchInvert;

#[cfg(feature = "multicore")]
use crate::multicore::{
Expand Down Expand Up @@ -427,7 +426,8 @@ impl<F: Field> Mul<F> for Value<F> {
pub struct MockProver<'a, F: Field> {
k: u32,
n: u32,
cs: ConstraintSystem<F>,
// use Arc type to reduce cs.clone when fork lots of time.
cs: Arc<ConstraintSystem<F>>,

/// The regions in the circuit.
regions: Vec<Region>,
Expand All @@ -444,7 +444,8 @@ pub struct MockProver<'a, F: Field> {
// This field is used only if the "phase_check" feature is turned on.
advice_prev: Vec<Vec<CellValue<F>>>,
// The instance cells in the circuit, arranged as [column][row].
instance: Vec<Vec<InstanceValue<F>>>,
// use Arc type to reduce instance.clone when fork lots of time.
instance: Arc<Vec<Vec<InstanceValue<F>>>>,

selectors_vec: Arc<Vec<Vec<bool>>>,
selectors: Vec<&'a mut [bool]>,
Expand Down Expand Up @@ -847,6 +848,7 @@ impl<'a, F: Field> Assignment<F> for MockProver<'a, F> {
let advice_anno = anno().into();
#[cfg(not(feature = "mock-batch-inv"))]
let val_res = to().into_field().evaluate().assign();

#[cfg(feature = "mock-batch-inv")]
let val_res = to().into_field().assign();
if val_res.is_err() {
Expand Down Expand Up @@ -1084,13 +1086,13 @@ impl<'a, F: FromUniformBytes<64> + Ord> MockProver<'a, F> {
instance: Vec<Vec<F>>,
) -> Result<Self, Error> {
let n = 1 << k;

let mut cs = ConstraintSystem::default();
#[cfg(feature = "circuit-params")]
let config = ConcreteCircuit::configure_with_params(&mut cs, circuit.params());
#[cfg(not(feature = "circuit-params"))]
let config = ConcreteCircuit::configure(&mut cs);
let cs = cs.chunk_lookups();
let cs = Arc::new(cs);

assert!(
n >= cs.minimum_rows(),
Expand Down Expand Up @@ -1122,6 +1124,7 @@ impl<'a, F: FromUniformBytes<64> + Ord> MockProver<'a, F> {
})
.collect::<Vec<_>>();

let instance = Arc::new(instance);
// Fixed columns contain no blinding factors.
let fixed_vec = Arc::new(vec![vec![CellValue::Unassigned; n]; cs.num_fixed_columns]);
let fixed = two_dim_vec_to_vec_of_slice!(fixed_vec);
Expand Down Expand Up @@ -1260,11 +1263,10 @@ impl<'a, F: FromUniformBytes<64> + Ord> MockProver<'a, F> {
ConcreteCircuit::FloorPlanner::synthesize(&mut prover, circuit, config, constants)?;
log::info!("MockProver synthesize took {:?}", syn_time.elapsed());
}

let (cs, selector_polys) = prover
.cs
.compress_selectors(prover.selectors_vec.as_ref().clone());
prover.cs = cs;
let prover_cs = Arc::try_unwrap(prover.cs).unwrap();
let (cs, selector_polys) =
prover_cs.compress_selectors(prover.selectors_vec.as_ref().clone());
prover.cs = Arc::new(cs);

// batch invert
#[cfg(feature = "mock-batch-inv")]
Expand Down
6 changes: 5 additions & 1 deletion halo2_proofs/src/plonk/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1846,7 +1846,11 @@ impl<F: Field> ConstraintSystem<F> {

let mut lookups: Vec<_> = vec![];
for v in self.lookups_map.values() {
let LookupTracker { table, inputs, name } = v;
let LookupTracker {
table,
inputs,
name,
} = v;
let name = Box::leak(name.clone().into_boxed_str());
let mut args = vec![super::mv_lookup::Argument::new(
name,
Expand Down

0 comments on commit c90df71

Please sign in to comment.