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

chore(avm): more stats and codegen cleanup #7475

Merged
merged 1 commit into from
Jul 15, 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 change: 0 additions & 1 deletion barretenberg/cpp/src/barretenberg/bb/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,6 @@ void avm_prove(const std::filesystem::path& bytecode_path,
// Prove execution and return vk
auto const [verification_key, proof] =
avm_trace::Execution::prove(bytecode, calldata, public_inputs_vec, avm_hints);
vinfo("------- PROVING DONE -------");

// TODO(ilyas): <#4887>: Currently we only need these two parts of the vk, look into pcs_verification key reqs
std::vector<uint64_t> vk_vector = { verification_key.circuit_size, verification_key.num_public_inputs };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "barretenberg/vm/avm_trace/avm_trace.hpp"
#include "barretenberg/vm/avm_trace/aztec_constants.hpp"
#include "barretenberg/vm/avm_trace/constants.hpp"
#include "barretenberg/vm/avm_trace/stats.hpp"
#include "barretenberg/vm/generated/avm_circuit_builder.hpp"
#include "barretenberg/vm/generated/avm_composer.hpp"

Expand Down Expand Up @@ -64,20 +65,23 @@ std::tuple<AvmFlavor::VerificationKey, HonkProof> Execution::prove(std::vector<u
vinfo("Deserialized " + std::to_string(instructions.size()) + " instructions");

std::vector<FF> returndata;
auto trace = gen_trace(instructions, returndata, calldata, public_inputs_vec, execution_hints);
std::vector<Row> trace;
AVM_TRACK_TIME("prove/gen_trace",
(trace = gen_trace(instructions, returndata, calldata, public_inputs_vec, execution_hints)));
if (!avm_dump_trace_path.empty()) {
info("Dumping trace as CSV to: " + avm_dump_trace_path.string());
dump_trace_as_csv(trace, avm_dump_trace_path);
}
auto circuit_builder = bb::AvmCircuitBuilder();
circuit_builder.set_trace(std::move(trace));

circuit_builder.check_circuit();
AVM_TRACK_TIME("prove/check_circuit", circuit_builder.check_circuit());

auto composer = AvmComposer();
auto prover = composer.create_prover(circuit_builder);
auto verifier = composer.create_verifier(circuit_builder);

vinfo("------- PROVING EXECUTION -------");
// Proof structure: public_inputs | calldata_size | calldata | returndata_size | returndata | raw proof
HonkProof proof(public_inputs_vec);
proof.emplace_back(calldata.size());
Expand Down Expand Up @@ -746,7 +750,9 @@ std::vector<Row> Execution::gen_trace(std::vector<Instruction> const& instructio
}
}

return trace_builder.finalize();
auto trace = trace_builder.finalize();
vinfo("Final trace size: ", trace.size());
return trace;
}

} // namespace bb::avm_trace
26 changes: 26 additions & 0 deletions barretenberg/cpp/src/barretenberg/vm/avm_trace/avm_trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "barretenberg/vm/avm_trace/fixed_gas.hpp"
#include "barretenberg/vm/avm_trace/fixed_powers.hpp"
#include "barretenberg/vm/avm_trace/gadgets/avm_slice_trace.hpp"
#include "barretenberg/vm/avm_trace/stats.hpp"

namespace bb::avm_trace {

Expand Down Expand Up @@ -3772,6 +3773,31 @@ std::vector<Row> AvmTraceBuilder::finalize(uint32_t min_trace_size, bool range_c
gas_trace_size + 1, KERNEL_INPUTS_LENGTH, KERNEL_OUTPUTS_LENGTH,
min_trace_size, fixed_gas_table.size(), slice_trace_size,
calldata.size() };
vinfo("Trace sizes before padding:",
"\n\tmain_trace_size: ",
main_trace_size,
"\n\tmem_trace_size: ",
mem_trace_size,
"\n\talu_trace_size: ",
alu_trace_size,
"\n\trange_check_size: ",
range_check_size,
"\n\tconv_trace_size: ",
conv_trace_size,
"\n\tlookup_table_size: ",
lookup_table_size,
"\n\tsha256_trace_size: ",
sha256_trace_size,
"\n\tposeidon2_trace_size: ",
poseidon2_trace_size,
"\n\tpedersen_trace_size: ",
pedersen_trace_size,
"\n\tgas_trace_size: ",
gas_trace_size,
"\n\tfixed_gas_table_size: ",
fixed_gas_table.size(),
"\n\tslice_trace_size: ",
slice_trace_size);
auto trace_size = std::max_element(trace_sizes.begin(), trace_sizes.end());

// We only need to pad with zeroes to the size to the largest trace here, pow_2 padding is handled in the
Expand Down
46 changes: 19 additions & 27 deletions barretenberg/cpp/src/barretenberg/vm/generated/avm_prover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "barretenberg/commitment_schemes/claim.hpp"
#include "barretenberg/commitment_schemes/commitment_key.hpp"
#include "barretenberg/common/constexpr_utils.hpp"
#include "barretenberg/honk/proof_system/logderivative_library.hpp"
#include "barretenberg/honk/proof_system/permutation_library.hpp"
#include "barretenberg/plonk_honk_shared/library/grand_product_library.hpp"
Expand All @@ -16,27 +17,6 @@ namespace bb {
using Flavor = AvmFlavor;
using FF = Flavor::FF;

namespace {

// Loops through LookupRelations and calculates the logderivatives.
// Metaprogramming is used to loop through the relations, because they are types.
template <size_t relation_idx = 0, typename PP>
void compute_logderivative_rel(const RelationParameters<FF>& relation_parameters,
PP& prover_polynomials,
size_t circuit_size)
{
using Relation = std::tuple_element_t<relation_idx, Flavor::LookupRelations>;
AVM_TRACK_TIME(
Relation::NAME + std::string("_ms"),
(compute_logderivative_inverse<Flavor, Relation>(prover_polynomials, relation_parameters, circuit_size)));

if constexpr (relation_idx + 1 < std::tuple_size_v<Flavor::LookupRelations>) {
compute_logderivative_rel<relation_idx + 1, PP>(relation_parameters, prover_polynomials, circuit_size);
}
}

} // namespace

/**
* Create AvmProver from proving key, witness and manifest.
*
Expand Down Expand Up @@ -93,8 +73,16 @@ void AvmProver::execute_log_derivative_inverse_round()
relation_parameters.gamma = gamm;

auto prover_polynomials = ProverPolynomials(*key);
compute_logderivative_rel(relation_parameters, prover_polynomials, key->circuit_size);
bb::constexpr_for<0, std::tuple_size_v<Flavor::LookupRelations>, 1>([&]<size_t relation_idx>() {
using Relation = std::tuple_element_t<relation_idx, Flavor::LookupRelations>;
AVM_TRACK_TIME(Relation::NAME + std::string("_ms"),
(compute_logderivative_inverse<Flavor, Relation>(
prover_polynomials, relation_parameters, key->circuit_size)));
});
}

void AvmProver::execute_log_derivative_inverse_commitments_round()
{
// Commit to all logderivative inverse polynomials
for (auto [commitment, key_poly] : zip_view(witness_commitments.get_derived(), key->get_derived())) {
commitment = commitment_key->commit(key_poly);
Expand Down Expand Up @@ -154,18 +142,22 @@ HonkProof AvmProver::construct_proof()
execute_preamble_round();

// Compute wire commitments
execute_wire_commitments_round();
AVM_TRACK_TIME("prove/execute_wire_commitments_round_ms", execute_wire_commitments_round());

// Compute sorted list accumulator
AVM_TRACK_TIME("prove/execute_log_derivative_inverse_round_ms", execute_log_derivative_inverse_round());

// Compute sorted list accumulator and commitment
execute_log_derivative_inverse_round();
// Compute commitments to logderivative inverse polynomials
AVM_TRACK_TIME("prove/execute_log_derivative_inverse_commitments_round_ms",
execute_log_derivative_inverse_commitments_round());

// Fiat-Shamir: alpha
// Run sumcheck subprotocol.
execute_relation_check_rounds();
AVM_TRACK_TIME("prove/execute_relation_check_rounds_ms", execute_relation_check_rounds());

// Fiat-Shamir: rho, y, x, z
// Execute Zeromorph multilinear PCS
execute_pcs_rounds();
AVM_TRACK_TIME("prove/execute_pcs_rounds_ms", execute_pcs_rounds());

return export_proof();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class AvmProver {
void execute_preamble_round();
void execute_wire_commitments_round();
void execute_log_derivative_inverse_round();
void execute_log_derivative_inverse_commitments_round();
void execute_relation_check_rounds();
void execute_pcs_rounds();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ class {{name}}CircuitBuilder {
return true;
}


[[nodiscard]] size_t get_num_gates() const { return rows.size(); }

[[nodiscard]] size_t get_circuit_subgroup_size() const
Expand Down
45 changes: 18 additions & 27 deletions bb-pilcom/bb-pil-backend/templates/prover.cpp.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "barretenberg/commitment_schemes/claim.hpp"
#include "barretenberg/commitment_schemes/commitment_key.hpp"
#include "barretenberg/common/constexpr_utils.hpp"
#include "barretenberg/honk/proof_system/logderivative_library.hpp"
#include "barretenberg/honk/proof_system/permutation_library.hpp"
#include "barretenberg/plonk_honk_shared/library/grand_product_library.hpp"
Expand All @@ -16,27 +17,6 @@ namespace bb {
using Flavor = {{name}}Flavor;
using FF = Flavor::FF;

namespace {

// Loops through LookupRelations and calculates the logderivatives.
// Metaprogramming is used to loop through the relations, because they are types.
template <size_t relation_idx = 0, typename PP>
void compute_logderivative_rel(const RelationParameters<FF>& relation_parameters,
PP& prover_polynomials,
size_t circuit_size)
{
using Relation = std::tuple_element_t<relation_idx, Flavor::LookupRelations>;
AVM_TRACK_TIME(
Relation::NAME + std::string("_ms"),
(compute_logderivative_inverse<Flavor, Relation>(prover_polynomials, relation_parameters, circuit_size)));

if constexpr (relation_idx + 1 < std::tuple_size_v<Flavor::LookupRelations>) {
compute_logderivative_rel<relation_idx + 1, PP>(relation_parameters, prover_polynomials, circuit_size);
}
}

} // namespace

/**
* Create {{name}}Prover from proving key, witness and manifest.
*
Expand Down Expand Up @@ -94,8 +74,16 @@ void {{name}}Prover::execute_log_derivative_inverse_round()
relation_parameters.gamma = gamm;

auto prover_polynomials = ProverPolynomials(*key);
compute_logderivative_rel(relation_parameters, prover_polynomials, key->circuit_size);
bb::constexpr_for<0, std::tuple_size_v<Flavor::LookupRelations>, 1>([&]<size_t relation_idx>() {
using Relation = std::tuple_element_t<relation_idx, Flavor::LookupRelations>;
AVM_TRACK_TIME(Relation::NAME + std::string("_ms"),
(compute_logderivative_inverse<Flavor, Relation>(
prover_polynomials, relation_parameters, key->circuit_size)));
});
}

void {{name}}Prover::execute_log_derivative_inverse_commitments_round()
{
// Commit to all logderivative inverse polynomials
for (auto [commitment, key_poly] : zip_view(witness_commitments.get_derived(), key->get_derived())) {
commitment = commitment_key->commit(key_poly);
Expand Down Expand Up @@ -155,18 +143,21 @@ HonkProof {{name}}Prover::construct_proof()
execute_preamble_round();

// Compute wire commitments
execute_wire_commitments_round();
AVM_TRACK_TIME("prove/execute_wire_commitments_round_ms", execute_wire_commitments_round());

// Compute sorted list accumulator
AVM_TRACK_TIME("prove/execute_log_derivative_inverse_round_ms", execute_log_derivative_inverse_round());

// Compute sorted list accumulator and commitment
execute_log_derivative_inverse_round();
// Compute commitments to logderivative inverse polynomials
AVM_TRACK_TIME("prove/execute_log_derivative_inverse_commitments_round_ms", execute_log_derivative_inverse_commitments_round());

// Fiat-Shamir: alpha
// Run sumcheck subprotocol.
execute_relation_check_rounds();
AVM_TRACK_TIME("prove/execute_relation_check_rounds_ms", execute_relation_check_rounds());

// Fiat-Shamir: rho, y, x, z
// Execute Zeromorph multilinear PCS
execute_pcs_rounds();
AVM_TRACK_TIME("prove/execute_pcs_rounds_ms", execute_pcs_rounds());

return export_proof();
}
Expand Down
1 change: 1 addition & 0 deletions bb-pilcom/bb-pil-backend/templates/prover.hpp.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class {{name}}Prover {
void execute_preamble_round();
void execute_wire_commitments_round();
void execute_log_derivative_inverse_round();
void execute_log_derivative_inverse_commitments_round();
void execute_relation_check_rounds();
void execute_pcs_rounds();

Expand Down
Loading