Skip to content

Commit

Permalink
feat: make max_rotations configurable (zkonduit#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
Genysys authored Dec 21, 2022
1 parent f228d23 commit 4e0b7f0
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 21 deletions.
8 changes: 4 additions & 4 deletions benches/affine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ impl<F: FieldExt + TensorType> Circuit<F> for MyCircuit<F> {
fn configure(cs: &mut ConstraintSystem<F>) -> Self::Config {
let len = unsafe { LEN };

let input = VarTensor::new_advice(cs, K, len, vec![len], true);
let kernel = VarTensor::new_advice(cs, K, len * len, vec![len, len], true);
let bias = VarTensor::new_advice(cs, K, len, vec![len], true);
let output = VarTensor::new_advice(cs, K, len, vec![len], true);
let input = VarTensor::new_advice(cs, K, len, vec![len], true, 512);
let kernel = VarTensor::new_advice(cs, K, len * len, vec![len, len], true, 512);
let bias = VarTensor::new_advice(cs, K, len, vec![len], true, 512);
let output = VarTensor::new_advice(cs, K, len, vec![len], true, 512);
// tells the config layer to add an affine op to a circuit gate
let affine_node = FusedNode {
op: FusedOp::Affine,
Expand Down
5 changes: 4 additions & 1 deletion benches/cnvrl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,25 @@ where
IN_CHANNELS * IMAGE_HEIGHT * IMAGE_WIDTH,
vec![IN_CHANNELS, IMAGE_HEIGHT, IMAGE_WIDTH],
true,
512,
);
let kernel = VarTensor::new_advice(
cs,
K,
OUT_CHANNELS * IN_CHANNELS * KERNEL_HEIGHT * KERNEL_WIDTH,
vec![OUT_CHANNELS, IN_CHANNELS, KERNEL_HEIGHT, KERNEL_WIDTH],
true,
512
);

let bias = VarTensor::new_advice(cs, K, OUT_CHANNELS, vec![OUT_CHANNELS], true);
let bias = VarTensor::new_advice(cs, K, OUT_CHANNELS, vec![OUT_CHANNELS], true, 512);
let output = VarTensor::new_advice(
cs,
K,
OUT_CHANNELS * output_height * output_width,
vec![OUT_CHANNELS, output_height, output_width],
true,
512,
);

// tells the config layer to add a conv op to a circuit gate
Expand Down
2 changes: 1 addition & 1 deletion benches/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl<F: FieldExt + TensorType> Circuit<F> for MyCircuit<F> {
fn configure(cs: &mut ConstraintSystem<F>) -> Self::Config {
let len = unsafe { LEN };
let advices = (0..2)
.map(|_| VarTensor::new_advice(cs, K, len, vec![len], true))
.map(|_| VarTensor::new_advice(cs, K, len, vec![len], true, 512))
.collect_vec();

RangeCheckConfig::configure(cs, &advices[0], &advices[1], RANGE)
Expand Down
2 changes: 1 addition & 1 deletion benches/relu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl<F: FieldExt + TensorType, NL: 'static + Nonlinearity<F> + Clone> Circuit<F>
fn configure(cs: &mut ConstraintSystem<F>) -> Self::Config {
unsafe {
let advices = (0..2)
.map(|_| VarTensor::new_advice(cs, K, LEN, vec![LEN], true))
.map(|_| VarTensor::new_advice(cs, K, LEN, vec![LEN], true, 512))
.collect::<Vec<_>>();

Self::Config::configure(cs, &advices[0], &advices[1], Some(&[BITS, 128]))
Expand Down
5 changes: 4 additions & 1 deletion examples/conv2d_mnist/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ where
max(IN_CHANNELS * IMAGE_HEIGHT * IMAGE_WIDTH, LEN),
vec![IN_CHANNELS, IMAGE_HEIGHT, IMAGE_WIDTH],
true,
512,
);
let kernel = VarTensor::new_advice(
cs,
Expand All @@ -159,16 +160,18 @@ where
),
vec![OUT_CHANNELS, IN_CHANNELS, KERNEL_HEIGHT, KERNEL_WIDTH],
true,
512,
);

let bias =
VarTensor::new_advice(cs, K, max(OUT_CHANNELS, CLASSES), vec![OUT_CHANNELS], true);
VarTensor::new_advice(cs, K, max(OUT_CHANNELS, CLASSES), vec![OUT_CHANNELS], true, 512);
let output = VarTensor::new_advice(
cs,
K,
max(OUT_CHANNELS * output_height * output_width, LEN),
vec![OUT_CHANNELS, output_height, output_width],
true,
512,
);

// tells the config layer to add a conv op to a circuit gate
Expand Down
8 changes: 4 additions & 4 deletions examples/mlp_4d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ impl<F: FieldExt + TensorType, const LEN: usize, const BITS: usize> Circuit<F>
// 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<F>) -> Self::Config {
let input = VarTensor::new_advice(cs, K, LEN, vec![LEN], true);
let kernel = VarTensor::new_advice(cs, K, LEN * LEN, vec![LEN, LEN], true);
let bias = VarTensor::new_advice(cs, K, LEN, vec![LEN], true);
let output = VarTensor::new_advice(cs, K, LEN, vec![LEN], true);
let input = VarTensor::new_advice(cs, K, LEN, vec![LEN], true, 512);
let kernel = VarTensor::new_advice(cs, K, LEN * LEN, vec![LEN, LEN], true, 512);
let bias = VarTensor::new_advice(cs, K, LEN, vec![LEN], true, 512);
let output = VarTensor::new_advice(cs, K, LEN, vec![LEN], true, 512);
// tells the config layer to add an affine op to the circuit gate
let affine_node = FusedNode {
op: FusedOp::Affine,
Expand Down
2 changes: 1 addition & 1 deletion src/circuit/eltwise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ mod tests {

fn configure(cs: &mut ConstraintSystem<F>) -> Self::Config {
let advices = (0..2)
.map(|_| VarTensor::new_advice(cs, 4, 1, vec![1], true))
.map(|_| VarTensor::new_advice(cs, 4, 1, vec![1], true, 512))
.collect::<Vec<_>>();

Self::Config::configure(cs, &advices[0], &advices[1], Some(&[2, 1]))
Expand Down
8 changes: 4 additions & 4 deletions src/circuit/fused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,10 @@ mod tests {
}

fn configure(cs: &mut ConstraintSystem<F>) -> Self::Config {
let input = VarTensor::new_advice(cs, K, LEN, vec![LEN], true);
let kernel = VarTensor::new_advice(cs, K, LEN * LEN, vec![LEN, LEN], true);
let bias = VarTensor::new_advice(cs, K, LEN, vec![LEN], true);
let output = VarTensor::new_advice(cs, K, LEN, vec![LEN], true);
let input = VarTensor::new_advice(cs, K, LEN, vec![LEN], true, 512);
let kernel = VarTensor::new_advice(cs, K, LEN * LEN, vec![LEN, LEN], true, 512);
let bias = VarTensor::new_advice(cs, K, LEN, vec![LEN], true, 512);
let output = VarTensor::new_advice(cs, K, LEN, vec![LEN], true, 512);
// tells the config layer to add an affine op to a circuit gate
let affine_node = FusedNode {
op: FusedOp::Affine,
Expand Down
2 changes: 1 addition & 1 deletion src/circuit/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ mod tests {

fn configure(cs: &mut ConstraintSystem<F>) -> Self::Config {
let advices = (0..2)
.map(|_| VarTensor::new_advice(cs, 4, 1, vec![1], true))
.map(|_| VarTensor::new_advice(cs, 4, 1, vec![1], true, 512))
.collect_vec();
let input = &advices[0];
let expected = &advices[1];
Expand Down
3 changes: 3 additions & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ pub struct Cli {
/// Flags whether params are public
#[arg(long, default_value = "false")]
pub public_params: bool,
/// Flags to set maximum rotations
#[arg(short = 'M', long, default_value = "512")]
pub max_rotations: usize,
}

#[derive(ValueEnum, Copy, Clone, Debug, PartialEq, Eq)]
Expand Down
12 changes: 11 additions & 1 deletion src/graph/vars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ impl<F: FieldExt + TensorType> ModelVars<F> {
fixed_dims: (usize, usize),
instance_dims: (usize, Vec<Vec<usize>>),
) -> Self {
let tensor_max = Cli::parse().max_rotations;

let advices = (0..advice_dims.0)
.map(|_| {
VarTensor::new_advice(
Expand All @@ -130,12 +132,20 @@ impl<F: FieldExt + TensorType> ModelVars<F> {
advice_dims.1,
vec![advice_dims.1],
true,
tensor_max,
)
})
.collect_vec();
let fixed = (0..fixed_dims.0)
.map(|_| {
VarTensor::new_fixed(cs, logrows as usize, fixed_dims.1, vec![fixed_dims.1], true)
VarTensor::new_fixed(
cs,
logrows as usize,
fixed_dims.1,
vec![fixed_dims.1],
true,
tensor_max,
)
})
.collect_vec();
let instances = (0..instance_dims.0)
Expand Down
6 changes: 4 additions & 2 deletions src/tensor/var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ impl VarTensor {
capacity: usize,
dims: Vec<usize>,
equality: bool,
v1: usize,
) -> Self {
let base = 2u32;
let max_rows = min(512, base.pow(k as u32) as usize - cs.blinding_factors() - 1);
let max_rows = min(v1, base.pow(k as u32) as usize - cs.blinding_factors() - 1);
let modulo = (capacity / max_rows) + 1;
let mut advices = vec![];
for _ in 0..modulo {
Expand All @@ -60,9 +61,10 @@ impl VarTensor {
capacity: usize,
dims: Vec<usize>,
equality: bool,
v1: usize,
) -> Self {
let base = 2u32;
let max_rows = min(512, base.pow(k as u32) as usize - cs.blinding_factors() - 1);
let max_rows = min(v1, base.pow(k as u32) as usize - cs.blinding_factors() - 1);
let modulo = (capacity / max_rows) + 1;
let mut fixed = vec![];
for _ in 0..modulo {
Expand Down

0 comments on commit 4e0b7f0

Please sign in to comment.