Skip to content

Commit

Permalink
chore: rm recip_int helper (#733)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-camuto authored Mar 5, 2024
1 parent 345fb56 commit a59e378
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 105 deletions.
6 changes: 0 additions & 6 deletions src/circuit/ops/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ pub enum BaseOp {
Sub,
SumInit,
Sum,
IsZero,
IsBoolean,
}

Expand All @@ -35,7 +34,6 @@ impl BaseOp {
BaseOp::Add => a + b,
BaseOp::Sub => a - b,
BaseOp::Mult => a * b,
BaseOp::IsZero => b,
BaseOp::IsBoolean => b,
_ => panic!("nonaccum_f called on accumulating operation"),
}
Expand Down Expand Up @@ -76,7 +74,6 @@ impl BaseOp {
BaseOp::Mult => "MULT",
BaseOp::Sum => "SUM",
BaseOp::SumInit => "SUMINIT",
BaseOp::IsZero => "ISZERO",
BaseOp::IsBoolean => "ISBOOLEAN",
}
}
Expand All @@ -93,7 +90,6 @@ impl BaseOp {
BaseOp::Mult => (0, 1),
BaseOp::Sum => (-1, 2),
BaseOp::SumInit => (0, 1),
BaseOp::IsZero => (0, 1),
BaseOp::IsBoolean => (0, 1),
}
}
Expand All @@ -110,7 +106,6 @@ impl BaseOp {
BaseOp::Mult => 2,
BaseOp::Sum => 1,
BaseOp::SumInit => 1,
BaseOp::IsZero => 0,
BaseOp::IsBoolean => 0,
}
}
Expand All @@ -127,7 +122,6 @@ impl BaseOp {
BaseOp::SumInit => 0,
BaseOp::CumProd => 1,
BaseOp::CumProdInit => 0,
BaseOp::IsZero => 0,
BaseOp::IsBoolean => 0,
}
}
Expand Down
7 changes: 0 additions & 7 deletions src/circuit/ops/chip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,6 @@ impl<F: PrimeField + TensorType + PartialOrd> BaseConfig<F> {
nonaccum_selectors.insert((BaseOp::Add, i, j), meta.selector());
nonaccum_selectors.insert((BaseOp::Sub, i, j), meta.selector());
nonaccum_selectors.insert((BaseOp::Mult, i, j), meta.selector());
nonaccum_selectors.insert((BaseOp::IsZero, i, j), meta.selector());
nonaccum_selectors.insert((BaseOp::IsBoolean, i, j), meta.selector());
}
}
Expand Down Expand Up @@ -432,12 +431,6 @@ impl<F: PrimeField + TensorType + PartialOrd> BaseConfig<F> {

vec![(output.clone()) * (output.clone() - Expression::Constant(F::from(1)))]
}
BaseOp::IsZero => {
let expected_output: Tensor<Expression<F>> = output
.query_rng(meta, *block_idx, *inner_col_idx, 0, 1)
.expect("non accum: output query failed");
vec![expected_output[base_op.constraint_idx()].clone()]
}
_ => {
let expected_output: Tensor<Expression<F>> = output
.query_rng(meta, *block_idx, *inner_col_idx, rotation_offset, rng)
Expand Down
102 changes: 10 additions & 92 deletions src/circuit/ops/layouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,41 +132,6 @@ pub(crate) fn div<F: PrimeField + TensorType + PartialOrd>(
Ok(claimed_output)
}

fn recip_int<F: PrimeField + TensorType + PartialOrd>(
config: &BaseConfig<F>,
region: &mut RegionCtx<F>,
input: &[ValTensor<F>; 1],
) -> Result<ValTensor<F>, Box<dyn Error>> {
// assert is boolean
let zero_inverse_val = tensor::ops::nonlinearities::zero_recip(1.0)[0];
// get values where input is 0
let zero_mask = equals_zero(config, region, input)?;

let zero_mask_minus_one = pairwise(
config,
region,
&[zero_mask.clone(), create_unit_tensor(1)],
BaseOp::Sub,
)?;

let zero_inverse_val = pairwise(
config,
region,
&[
zero_mask,
create_constant_tensor(i128_to_felt(zero_inverse_val), 1),
],
BaseOp::Mult,
)?;

pairwise(
config,
region,
&[zero_mask_minus_one, zero_inverse_val],
BaseOp::Add,
)
}

/// recip accumulated layout
pub(crate) fn recip<F: PrimeField + TensorType + PartialOrd>(
config: &BaseConfig<F>,
Expand All @@ -175,10 +140,6 @@ pub(crate) fn recip<F: PrimeField + TensorType + PartialOrd>(
input_scale: F,
output_scale: F,
) -> Result<ValTensor<F>, Box<dyn Error>> {
if output_scale == F::ONE || output_scale == F::ZERO {
return recip_int(config, region, value);
}

let input = value[0].clone();
let input_dims = input.dims();

Expand All @@ -188,8 +149,11 @@ pub(crate) fn recip<F: PrimeField + TensorType + PartialOrd>(
// range_check_bracket is min of input_scale * output_scale and 2^F::S - 3
let range_check_len = std::cmp::min(integer_output_scale, 2_i128.pow(F::S - 4));

let input_scale_ratio =
i128_to_felt(integer_input_scale * integer_output_scale / range_check_len);
let input_scale_ratio = if range_check_len > 0 {
i128_to_felt(integer_input_scale * integer_output_scale / range_check_len)
} else {
F::ONE
};

let range_check_bracket = range_check_len / 2;

Expand Down Expand Up @@ -234,11 +198,7 @@ pub(crate) fn recip<F: PrimeField + TensorType + PartialOrd>(

let equal_zero_mask = equals_zero(config, region, &[input.clone()])?;

let equal_inverse_mask = equals(
config,
region,
&[claimed_output.clone(), zero_inverse],
)?;
let equal_inverse_mask = equals(config, region, &[claimed_output.clone(), zero_inverse])?;

// assert the two masks are equal
enforce_equality(
Expand All @@ -249,12 +209,7 @@ pub(crate) fn recip<F: PrimeField + TensorType + PartialOrd>(

let unit_scale = create_constant_tensor(i128_to_felt(range_check_len), 1);

let unit_mask = pairwise(
config,
region,
&[equal_zero_mask, unit_scale],
BaseOp::Mult,
)?;
let unit_mask = pairwise(config, region, &[equal_zero_mask, unit_scale], BaseOp::Mult)?;

// now add the unit mask to the rebased_div
let rebased_offset_div = pairwise(config, region, &[rebased_div, unit_mask], BaseOp::Add)?;
Expand Down Expand Up @@ -1851,7 +1806,8 @@ pub(crate) fn equals_zero<F: PrimeField + TensorType + PartialOrd>(
// take the product of diff and output
let prod_check = pairwise(config, region, &[values, output.clone()], BaseOp::Mult)?;

is_zero_identity(config, region, &[prod_check], false)?;
let zero_tensor = create_zero_tensor(prod_check.len());
enforce_equality(config, region, &[prod_check, zero_tensor])?;

Ok(output)
}
Expand Down Expand Up @@ -1963,13 +1919,7 @@ pub(crate) fn sumpool<F: PrimeField + TensorType + PartialOrd>(
.map(|coord| {
let (b, i) = (coord[0], coord[1]);
let input = values[0].get_slice(&[b..b + 1, i..i + 1])?;
let output = conv(
config,
region,
&[input, kernel.clone()],
padding,
stride,
)?;
let output = conv(config, region, &[input, kernel.clone()], padding, stride)?;
res.push(output);
Ok(())
})
Expand Down Expand Up @@ -2448,38 +2398,6 @@ pub(crate) fn identity<F: PrimeField + TensorType + PartialOrd>(
Ok(output)
}

/// is zero identity constraint.
pub(crate) fn is_zero_identity<F: PrimeField + TensorType + PartialOrd>(
config: &BaseConfig<F>,
region: &mut RegionCtx<F>,
values: &[ValTensor<F>; 1],
assign: bool,
) -> Result<ValTensor<F>, Box<dyn Error>> {
let output = if assign || !values[0].get_const_indices()?.is_empty() {
let output = region.assign(&config.custom_gates.output, &values[0])?;
region.increment(output.len());
output
} else {
values[0].clone()
};
// Enable the selectors
if !region.is_dummy() {
(0..output.len())
.map(|j| {
let index = region.linear_coord() - j - 1;

let (x, y, z) = config.custom_gates.output.cartesian_coord(index);
let selector = config.custom_gates.selectors.get(&(BaseOp::IsZero, x, y));

region.enable(selector, z)?;
Ok(())
})
.collect::<Result<Vec<_>, Box<dyn Error>>>()?;
}

Ok(output)
}

/// Boolean identity constraint. Usually used to constrain an instance column to an advice so the returned cells / values can be operated upon.
pub(crate) fn boolean_identity<F: PrimeField + TensorType + PartialOrd>(
config: &BaseConfig<F>,
Expand Down

0 comments on commit a59e378

Please sign in to comment.