Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(Protogalaxy): Move state out of Instances #8177

Merged
merged 17 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "barretenberg/eccvm/eccvm_flavor.hpp"
#include "barretenberg/protogalaxy/protogalaxy_prover.hpp"
#include "barretenberg/protogalaxy/protogalaxy_prover_internal.hpp" // just for an alias; should perhaps move to prover
#include "barretenberg/stdlib_circuit_builders/mega_flavor.hpp"
#include "barretenberg/stdlib_circuit_builders/ultra_flavor.hpp"
#include "barretenberg/sumcheck/instance/instances.hpp"
#include "barretenberg/translator_vm/translator_flavor.hpp"
#include <benchmark/benchmark.h>

Expand Down Expand Up @@ -53,8 +54,7 @@ template <typename Flavor, typename Relation> void execute_relation_for_univaria
template <typename Flavor, typename Relation> void execute_relation_for_pg_univariates(::benchmark::State& state)
{
using ProverInstances = ProverInstances_<Flavor>;
using ProtoGalaxyProver = ProtoGalaxyProver_<ProverInstances>;
using Input = ProtoGalaxyProver::ExtendedUnivariates;
using Input = ProtogalaxyProverInternal<ProverInstances>::ExtendedUnivariates;
using Accumulator = typename Relation::template ProtogalaxyTupleOfUnivariatesOverSubrelations<ProverInstances::NUM>;

execute_relation<Flavor, Relation, Input, Accumulator>(state);
Expand Down
3 changes: 1 addition & 2 deletions barretenberg/cpp/src/barretenberg/polynomials/pow.bench.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ void compute_pow_poly(benchmark::State& state)
for (auto _ : state) {
int64_t num_betas = state.range(0);
std::vector<bb::fr> cur_betas(betas.begin(), betas.begin() + num_betas);
PowPolynomial pow{ cur_betas };
pow.compute_values(static_cast<size_t>(num_betas));
PowPolynomial pow{ cur_betas, cur_betas.size() };
}
}

Expand Down
32 changes: 26 additions & 6 deletions barretenberg/cpp/src/barretenberg/polynomials/pow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,27 @@ template <typename FF> struct PowPolynomial {
*/
FF partial_evaluation_result = FF(1);

explicit PowPolynomial(const std::vector<FF>& betas)
/**
* @brief Construct a new PowPolynomial
*
* @param betas
* @param log_num_monomials
*/
PowPolynomial(const std::vector<FF>& betas, const size_t log_num_monomials)
: betas(betas)
, pow_betas(compute_pow_betas(betas, log_num_monomials))
{}

/**
* @brief Construct a new PowPolynomial object without expanding to a vector of monomials
* @details The sumcheck verifier does not use pow_betas
*
* @param betas
*/
PowPolynomial(const std::vector<FF>& betas)
: betas(betas)
{}

/**
* @brief Retruns the element in #pow_betas at place #idx.
*
Expand Down Expand Up @@ -116,15 +134,15 @@ template <typename FF> struct PowPolynomial {
* @brief Given \f$ \vec\beta = (\beta_0,...,\beta_{d-1})\f$ compute \f$ pow_{\ell}(\vec \beta) = pow_{\beta}(\vec
* \ell)\f$ for \f$ \ell =0,\ldots,2^{d}-1\f$.
*
* @param log_circuit_size Determines the number of beta challenges used to compute pow_betas (required because when
* we generate CONST_SIZE_PROOF_LOG_N, currently 28, challenges but the real circuit size is less than 1 <<
* @param log_num_monomials Determines the number of beta challenges used to compute pow_betas (required because
* when we generate CONST_SIZE_PROOF_LOG_N, currently 28, challenges but the real circuit size is less than 1 <<
* CONST_SIZE_PROOF_LOG_N, we should compute unnecessarily a vector of pow_betas of length 1 << 28 )
*/
BB_PROFILE void compute_values(size_t log_circuit_size)
BB_PROFILE static std::vector<FF> compute_pow_betas(const std::vector<FF>& betas, const size_t log_num_monomials)
{

size_t pow_size = 1 << log_circuit_size;
pow_betas = std::vector<FF>(pow_size);
size_t pow_size = 1 << log_num_monomials;
std::vector<FF> pow_betas(pow_size);

// Determine number of threads for multithreading.
// Note: Multithreading is "on" for every round but we reduce the number of threads from the max available based
Expand Down Expand Up @@ -154,6 +172,8 @@ template <typename FF> struct PowPolynomial {
pow_betas[i] = res;
}
});

return pow_betas;
}
};
/**<
Expand Down
7 changes: 3 additions & 4 deletions barretenberg/cpp/src/barretenberg/polynomials/pow.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ TEST(PowPolynomial, FullPowConsistency)

TEST(PowPolynomial, PowPolynomialsOnPowers)
{
auto betas = std::vector<fr>{ 2, 4, 16 };
auto pow = PowPolynomial(betas);
pow.compute_values(betas.size());
auto expected_values = std::vector<fr>{ 1, 2, 4, 8, 16, 32, 64, 128 };
std::vector<fr> betas{ 2, 4, 16 };
PowPolynomial<fr> pow(betas, betas.size());
std::vector<fr> expected_values{ 1, 2, 4, 8, 16, 32, 64, 128 };
EXPECT_EQ(expected_values, pow.pow_betas);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ template <class Fr, size_t view_domain_end, size_t view_domain_start, size_t ski
template <class Fr, size_t domain_end, size_t domain_start = 0, size_t skip_count = 0> class Univariate {
public:
static constexpr size_t LENGTH = domain_end - domain_start;
static constexpr size_t SKIP_COUNT = skip_count;
using View = UnivariateView<Fr, domain_end, domain_start, skip_count>;

using value_type = Fr; // used to get the type of the elements consistently with std::array
Expand Down
118 changes: 46 additions & 72 deletions barretenberg/cpp/src/barretenberg/protogalaxy/combiner.test.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "barretenberg/honk/utils/testing.hpp"
#include "barretenberg/polynomials/pow.hpp"
#include "barretenberg/protogalaxy/protogalaxy_prover.hpp"
#include "barretenberg/relations/relation_parameters.hpp"
#include "barretenberg/protogalaxy/protogalaxy_prover_internal.hpp"
#include "barretenberg/relations/ultra_arithmetic_relation.hpp"
#include "barretenberg/stdlib_circuit_builders/ultra_flavor.hpp"
#include "barretenberg/sumcheck/instance/instances.hpp"
Expand All @@ -21,6 +20,7 @@ TEST(Protogalaxy, CombinerOn2Instances)
using ProverInstance = ProverInstance_<Flavor>;
using ProverInstances = ProverInstances_<Flavor, NUM_INSTANCES>;
using ProtoGalaxyProver = ProtoGalaxyProver_<ProverInstances>;
using Fun = ProtogalaxyProverInternal<ProverInstances>;

const auto restrict_to_standard_arithmetic_relation = [](auto& polys) {
std::fill(polys.q_arith.begin(), polys.q_arith.end(), 1);
Expand Down Expand Up @@ -54,9 +54,13 @@ TEST(Protogalaxy, CombinerOn2Instances)
}

ProverInstances instances{ instance_data };
instances.alphas.fill(bb::Univariate<FF, 12>(FF(0))); // focus on the arithmetic relation only
auto pow_polynomial = PowPolynomial(std::vector<FF>{ 2 });
auto result = prover.compute_combiner</*OptimisationEnabled=*/false>(instances, pow_polynomial);
prover.state.alphas.fill(bb::Univariate<FF, 12>(FF(0))); // focus on the arithmetic relation only
PowPolynomial<FF> pow_polynomial({ 2 }, /*log_num_monomials=*/1);
auto result = Fun::compute_combiner(instances,
pow_polynomial,
prover.state.relation_parameters,
prover.state.alphas,
prover.state.univariate_accumulators);
// The expected_result values are computed by running the python script combiner_example_gen.py
auto expected_result = Univariate<FF, 12>(std::array<FF, 12>{ 9704UL,
13245288UL,
Expand Down Expand Up @@ -87,7 +91,7 @@ TEST(Protogalaxy, CombinerOn2Instances)
}

ProverInstances instances{ instance_data };
instances.alphas.fill(bb::Univariate<FF, 12>(FF(0))); // focus on the arithmetic relation only
prover.state.alphas.fill(bb::Univariate<FF, 12>(FF(0))); // focus on the arithmetic relation only

const auto create_add_gate = [](auto& polys, const size_t idx, FF w_l, FF w_r) {
polys.w_l[idx] = w_l;
Expand Down Expand Up @@ -133,9 +137,17 @@ TEST(Protogalaxy, CombinerOn2Instances)
relation value:
0 0 0 0 0 0 0 0 0 6 18 36 60 90 */

auto pow_polynomial = PowPolynomial(std::vector<FF>{ 2 });
auto result = prover.compute_combiner</*OptimisationEnabled=*/false>(instances, pow_polynomial);
auto optimised_result = prover.compute_combiner(instances, pow_polynomial);
PowPolynomial<FF> pow_polynomial({ 2 }, /*log_num_monomials=*/1);
auto result = Fun::compute_combiner(instances,
pow_polynomial,
prover.state.relation_parameters,
prover.state.alphas,
prover.state.univariate_accumulators);
auto optimised_result = Fun::compute_combiner(instances,
pow_polynomial,
prover.state.optimised_relation_parameters,
prover.state.alphas,
prover.state.optimised_univariate_accumulators);
auto expected_result =
Univariate<FF, 12>(std::array<FF, 12>{ 0, 0, 12, 36, 72, 120, 180, 252, 336, 432, 540, 660 });

Expand All @@ -154,6 +166,7 @@ TEST(Protogalaxy, CombinerOptimizationConsistency)
using ProverInstance = ProverInstance_<Flavor>;
using ProverInstances = ProverInstances_<Flavor, NUM_INSTANCES>;
using ProtoGalaxyProver = ProtoGalaxyProver_<ProverInstances>;
using Fun = ProtogalaxyProverInternal<ProverInstances>;
using UltraArithmeticRelation = UltraArithmeticRelation<FF>;

constexpr size_t UNIVARIATE_LENGTH = 12;
Expand Down Expand Up @@ -188,10 +201,9 @@ TEST(Protogalaxy, CombinerOptimizationConsistency)
}

ProverInstances instances{ instance_data };
instances.alphas.fill(
prover.state.alphas.fill(
bb::Univariate<FF, UNIVARIATE_LENGTH>(FF(0))); // focus on the arithmetic relation only
auto pow_polynomial = PowPolynomial(std::vector<FF>{ 2 });
pow_polynomial.compute_values(1);
PowPolynomial<FF> pow_polynomial({ 2 }, /*log_num_monomials=*/1);

// Relation parameters are all zeroes
RelationParameters<FF> relation_parameters;
Expand Down Expand Up @@ -252,8 +264,16 @@ TEST(Protogalaxy, CombinerOptimizationConsistency)
precomputed_result[idx] = std::get<0>(accumulator)[0];
}
auto expected_result = Univariate<FF, UNIVARIATE_LENGTH>(precomputed_result);
auto result = prover.compute_combiner</*OptimisationEnabled=*/false>(instances, pow_polynomial);
auto optimised_result = prover.compute_combiner(instances, pow_polynomial);
auto result = Fun::compute_combiner(instances,
pow_polynomial,
prover.state.relation_parameters,
prover.state.alphas,
prover.state.univariate_accumulators);
auto optimised_result = Fun::compute_combiner(instances,
pow_polynomial,
prover.state.optimised_relation_parameters,
prover.state.alphas,
prover.state.optimised_univariate_accumulators);

EXPECT_EQ(result, expected_result);
EXPECT_EQ(optimised_result, expected_result);
Expand All @@ -273,7 +293,7 @@ TEST(Protogalaxy, CombinerOptimizationConsistency)
}

ProverInstances instances{ instance_data };
instances.alphas.fill(bb::Univariate<FF, 12>(FF(0))); // focus on the arithmetic relation only
prover.state.alphas.fill(bb::Univariate<FF, 12>(FF(0))); // focus on the arithmetic relation only

const auto create_add_gate = [](auto& polys, const size_t idx, FF w_l, FF w_r) {
polys.w_l[idx] = w_l;
Expand Down Expand Up @@ -319,9 +339,17 @@ TEST(Protogalaxy, CombinerOptimizationConsistency)
relation value:
0 0 0 0 0 0 0 0 0 6 18 36 60 90 */

auto pow_polynomial = PowPolynomial(std::vector<FF>{ 2 });
auto result = prover.compute_combiner</*OptimisationEnabled=*/false>(instances, pow_polynomial);
auto optimised_result = prover.compute_combiner(instances, pow_polynomial);
PowPolynomial<FF> pow_polynomial({ 2 }, /*log_num_monomials=*/1);
auto result = Fun::compute_combiner(instances,
pow_polynomial,
prover.state.relation_parameters,
prover.state.alphas,
prover.state.univariate_accumulators);
auto optimised_result = Fun::compute_combiner(instances,
pow_polynomial,
prover.state.optimised_relation_parameters,
prover.state.alphas,
prover.state.optimised_univariate_accumulators);
auto expected_result =
Univariate<FF, 12>(std::array<FF, 12>{ 0, 0, 12, 36, 72, 120, 180, 252, 336, 432, 540, 660 });

Expand All @@ -332,57 +360,3 @@ TEST(Protogalaxy, CombinerOptimizationConsistency)
run_test(true);
run_test(false);
};

// Tests a combiner on 4 instances, note currently we don't plan
// to fold with num instances > 2, this would require an additional explicit instantiation in
// protogalaxy_prover_ultra.cpp. Currently, we rather save the compile time.
// TEST(Protogalaxy, CombinerOn4Instances)
// {
// constexpr size_t NUM_INSTANCES = 4;
// using ProverInstance = ProverInstance_<Flavor>;
// using ProverInstances = ProverInstances_<Flavor, NUM_INSTANCES>;
// using ProtoGalaxyProver = ProtoGalaxyProver_<ProverInstances>;

// const auto zero_all_selectors = [](auto& polys) {
// std::fill(polys.q_arith.begin(), polys.q_arith.end(), 0);
// std::fill(polys.q_delta_range.begin(), polys.q_delta_range.end(), 0);
// std::fill(polys.q_elliptic.begin(), polys.q_elliptic.end(), 0);
// std::fill(polys.q_aux.begin(), polys.q_aux.end(), 0);
// std::fill(polys.q_lookup.begin(), polys.q_lookup.end(), 0);
// std::fill(polys.q_4.begin(), polys.q_4.end(), 0);
// std::fill(polys.w_4.begin(), polys.w_4.end(), 0);
// std::fill(polys.w_4_shift.begin(), polys.w_4_shift.end(), 0);
// };

// auto run_test = [&]() {
// std::vector<std::shared_ptr<ProverInstance>> instance_data(NUM_INSTANCES);
// ProtoGalaxyProver prover;

// for (size_t idx = 0; idx < NUM_INSTANCES; idx++) {
// auto instance = std::make_shared<ProverInstance>();
// auto prover_polynomials = get_zero_prover_polynomials<Flavor>(
// /*log_circuit_size=*/1);
// instance->proving_key.polynomials = std::move(prover_polynomials);
// instance->proving_key.circuit_size = 2;
// instance_data[idx] = instance;
// }

// ProverInstances instances{ instance_data };
// instances.alphas.fill(bb::Univariate<FF, 40>(FF(0))); // focus on the arithmetic relation only

// zero_all_selectors(instances[0]->proving_key.polynomials);
// zero_all_selectors(instances[1]->proving_key.polynomials);
// zero_all_selectors(instances[2]->proving_key.polynomials);
// zero_all_selectors(instances[3]->proving_key.polynomials);

// auto pow_polynomial = PowPolynomial(std::vector<FF>{ 2 });
// auto result = prover.compute_combiner</*OptimisationEnabled=*/false>(instances, pow_polynomial);
// auto optimised_result = prover.compute_combiner(instances, pow_polynomial);
// std::array<FF, 40> zeroes;
// std::fill(zeroes.begin(), zeroes.end(), 0);
// auto expected_result = Univariate<FF, 40>(zeroes);
// EXPECT_EQ(result, expected_result);
// EXPECT_EQ(optimised_result, expected_result);
// };
// run_test();
// };
Loading
Loading