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

feat(avm): parallelize polynomial alloc and set #8520

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1,343 changes: 679 additions & 664 deletions barretenberg/cpp/src/barretenberg/vm/avm/generated/circuit_builder.cpp

Large diffs are not rendered by default.

21 changes: 10 additions & 11 deletions barretenberg/cpp/src/barretenberg/vm/avm/generated/composer.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// AUTOGENERATED FILE
#include "barretenberg/vm/avm/generated/composer.hpp"
#include "barretenberg/vm/stats.hpp"

namespace bb {

Expand All @@ -14,20 +15,20 @@ void AvmComposer::compute_witness(CircuitConstructor& circuit)

for (auto [key_poly, prover_poly] : zip_view(proving_key->get_all(), polynomials.get_unshifted())) {
ASSERT(flavor_get_label(*proving_key, key_poly) == flavor_get_label(polynomials, prover_poly));
key_poly = prover_poly;
key_poly = std::move(prover_poly);
}

computed_witness = true;
}

AvmProver AvmComposer::create_prover(CircuitConstructor& circuit_constructor)
{
compute_proving_key(circuit_constructor);
compute_witness(circuit_constructor);
compute_commitment_key(circuit_constructor.get_circuit_subgroup_size());

AvmProver output_state(proving_key, proving_key->commitment_key);

AVM_TRACK_TIME("composer/create_prover:proving_key", compute_proving_key(circuit_constructor));
AVM_TRACK_TIME("composer/create_prover:witness", compute_witness(circuit_constructor));
AVM_TRACK_TIME("composer/create_prover:commitment_key",
compute_commitment_key(circuit_constructor.get_circuit_subgroup_size()));
auto output_state = AVM_TRACK_TIME_V("composer/create_prover:construct_prover",
AvmProver(proving_key, proving_key->commitment_key));
return output_state;
}

Expand All @@ -44,10 +45,8 @@ std::shared_ptr<Flavor::ProvingKey> AvmComposer::compute_proving_key(CircuitCons
}

// Initialize proving_key
{
const size_t subgroup_size = circuit_constructor.get_circuit_subgroup_size();
proving_key = std::make_shared<Flavor::ProvingKey>(subgroup_size, 0);
}
const size_t subgroup_size = circuit_constructor.get_circuit_subgroup_size();
proving_key = std::make_shared<Flavor::ProvingKey>(subgroup_size, 0);

return proving_key;
}
Expand Down
10 changes: 2 additions & 8 deletions barretenberg/cpp/src/barretenberg/vm/avm/generated/flavor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2273,14 +2273,8 @@ AvmFlavor::ProvingKey::ProvingKey(const size_t circuit_size, const size_t num_pu
this->log_circuit_size = numeric::get_msb(circuit_size);
this->num_public_inputs = num_public_inputs;

// Allocate memory for precomputed polynomials
for (auto& poly : PrecomputedEntities<Polynomial>::get_all()) {
poly = Polynomial(circuit_size);
}
// Allocate memory for witness polynomials
for (auto& poly : WitnessEntities<Polynomial>::get_all()) {
poly = Polynomial(circuit_size);
}
// The proving key's polynomials are not allocated here because they are later overwritten
// AvmComposer::compute_witness(). We should probably refactor this flow.
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ using FF = Flavor::FF;
* @tparam settings Settings class.
*/
AvmProver::AvmProver(std::shared_ptr<Flavor::ProvingKey> input_key, std::shared_ptr<PCSCommitmentKey> commitment_key)
: key(input_key)
, commitment_key(commitment_key)
: key(std::move(input_key))
, commitment_key(std::move(commitment_key))
{
for (auto [prover_poly, key_poly] : zip_view(prover_polynomials.get_unshifted(), key->get_all())) {
ASSERT(bb::flavor_get_label(prover_polynomials, prover_poly) == bb::flavor_get_label(*key, key_poly));
Expand Down
1 change: 0 additions & 1 deletion bb-pilcom/bb-pil-backend/src/lookup_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ pub fn get_counts_from_lookups(lookups: &[Lookup]) -> Vec<String> {

fn create_lookup_settings_data(lookup: &Lookup) -> Json {
let columns_per_set = lookup.left.cols.len();
let counts_poly_name = lookup.counts_poly.to_owned();

// NOTE: https://github.com/AztecProtocol/aztec-packages/issues/3879
// Settings are not flexible enough to combine inverses
Expand Down
45 changes: 30 additions & 15 deletions bb-pilcom/bb-pil-backend/templates/circuit_builder.cpp.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "barretenberg/relations/generic_permutation/generic_permutation_relation.hpp"
#include "barretenberg/relations/generic_lookup/generic_lookup_relation.hpp"
#include "barretenberg/honk/proof_system/logderivative_library.hpp"
#include "barretenberg/vm/stats.hpp"

namespace bb {

Expand All @@ -18,26 +19,40 @@ namespace bb {
ProverPolynomials polys;

// Allocate mem for each column

for (auto& poly : polys.get_to_be_shifted()) {
poly = Polynomial::shiftable(num_rows);
}
AVM_TRACK_TIME("circuit_builder/init_polys_to_be_shifted", ({
for (auto& poly : polys.get_to_be_shifted()) {
poly = Polynomial{ /*memory size*/ num_rows - 1,
/*largest possible index*/ num_rows,
/*make shiftable with offset*/ 1 };
}
}));
// catch-all with fully formed polynomials
for (auto& poly : polys.get_unshifted()) {
if (poly.is_empty()) {
poly = Polynomial{ num_rows };
}
}

for (size_t i = 0; i < rows.size(); i++) {
AVM_TRACK_TIME("circuit_builder/init_polys_unshifted", ({
auto unshifted = polys.get_unshifted();
bb::parallel_for(unshifted.size(), [&](size_t i) {
auto& poly = unshifted[i];
if (poly.is_empty()) {
// Not set above
poly = Polynomial{ /*memory size*/ num_rows, /*largest possible index*/ num_rows };
}
});
}));

AVM_TRACK_TIME(
"circuit_builder/set_polys_unshifted", ({
bb::parallel_for(rows.size(), [&](size_t i) {
{{#each all_cols_without_inverses as |poly|}}
polys.{{poly}}.set_if_valid_index(i, rows[i].{{poly}});
{{/each}}
}
});
}));

for (auto [shifted, to_be_shifted] : zip_view(polys.get_shifted(), polys.get_to_be_shifted())) {
shifted = to_be_shifted.shifted();
}
AVM_TRACK_TIME(
"circuit_builder/set_polys_shifted", ({
for (auto [shifted, to_be_shifted] : zip_view(polys.get_shifted(), polys.get_to_be_shifted())) {
shifted = to_be_shifted.shifted();
}
}));

return polys;
}
Expand Down
15 changes: 8 additions & 7 deletions bb-pilcom/bb-pil-backend/templates/composer.cpp.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// AUTOGENERATED FILE
#include "barretenberg/vm/{{snakeCase name}}/generated/composer.hpp"
#include "barretenberg/vm/stats.hpp"

namespace bb {

Expand All @@ -14,20 +15,20 @@ void {{name}}Composer::compute_witness(CircuitConstructor& circuit)

for (auto [key_poly, prover_poly] : zip_view(proving_key->get_all(), polynomials.get_unshifted())) {
ASSERT(flavor_get_label(*proving_key, key_poly) == flavor_get_label(polynomials, prover_poly));
key_poly = prover_poly;
key_poly = std::move(prover_poly);
}

computed_witness = true;
}

{{name}}Prover {{name}}Composer::create_prover(CircuitConstructor& circuit_constructor)
{
compute_proving_key(circuit_constructor);
compute_witness(circuit_constructor);
compute_commitment_key(circuit_constructor.get_circuit_subgroup_size());

{{name}}Prover output_state(proving_key, proving_key->commitment_key);

AVM_TRACK_TIME("composer/create_prover:proving_key", compute_proving_key(circuit_constructor));
AVM_TRACK_TIME("composer/create_prover:witness", compute_witness(circuit_constructor));
AVM_TRACK_TIME("composer/create_prover:commitment_key",
compute_commitment_key(circuit_constructor.get_circuit_subgroup_size()));
auto output_state = AVM_TRACK_TIME_V("composer/create_prover:construct_prover",
AvmProver(proving_key, proving_key->commitment_key));
return output_state;
}

Expand Down
10 changes: 2 additions & 8 deletions bb-pilcom/bb-pil-backend/templates/flavor.cpp.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,8 @@ AvmFlavor::ProvingKey::ProvingKey(const size_t circuit_size, const size_t num_pu
this->log_circuit_size = numeric::get_msb(circuit_size);
this->num_public_inputs = num_public_inputs;

// Allocate memory for precomputed polynomials
for (auto& poly : PrecomputedEntities<Polynomial>::get_all()) {
poly = Polynomial(circuit_size);
}
// Allocate memory for witness polynomials
for (auto& poly : WitnessEntities<Polynomial>::get_all()) {
poly = Polynomial(circuit_size);
}
// The proving key's polynomials are not allocated here because they are later overwritten
// AvmComposer::compute_witness(). We should probably refactor this flow.
};

/**
Expand Down
4 changes: 2 additions & 2 deletions bb-pilcom/bb-pil-backend/templates/prover.cpp.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ using FF = Flavor::FF;
*/
{{name}}Prover::{{name}}Prover(std::shared_ptr<Flavor::ProvingKey> input_key,
std::shared_ptr<PCSCommitmentKey> commitment_key)
: key(input_key)
, commitment_key(commitment_key)
: key(std::move(input_key))
, commitment_key(std::move(commitment_key))
{
for (auto [prover_poly, key_poly] : zip_view(prover_polynomials.get_unshifted(), key->get_all())) {
ASSERT(bb::flavor_get_label(prover_polynomials, prover_poly) ==
Expand Down
Loading