Skip to content

Commit

Permalink
chore(avm): more stats and codegen cleanup (#7475)
Browse files Browse the repository at this point in the history
* Add trace size stats and proof sections time tracking
* Use `bb:constexpr_for` instead of custom template that did the same

Example public transfer
```
bytecode size: 31218
calldata size: 6
public_inputs size: 481
hints.storage_value_hints size: 2
hints.note_hash_exists_hints size: 0
hints.nullifier_exists_hints size: 1
hints.l1_to_l2_message_exists_hints size: 0
hints.externalcall_hints size: 0
hints.contract_instance_hints size: 0
using cached crs of size 8388609 at "/mnt/user-data/facundo/.bb-crs/bn254_g1.dat"
Deserialized 2524 instructions
------- GENERATING TRACE -------
Trace sizes before padding:
        main_trace_size: 1638
        mem_trace_size: 3880
        alu_trace_size: 811
        range_check_size: 65536
        conv_trace_size: 1
        lookup_table_size: 0
        sha256_trace_size: 0
        poseidon2_trace_size: 0
        pedersen_trace_size: 4
        gas_trace_size: 1620
        fixed_gas_table_size: 65
        slice_trace_size: 7
Final trace size: 65537
------- PROVING EXECUTION -------
proof written to: "/mnt/user-data/facundo/tmp-8Q3xgk/proof"
vk written to: "/mnt/user-data/facundo/tmp-8Q3xgk/vk"
vk as fields written to: "/mnt/user-data/facundo/tmp-8Q3xgk/vk_fields.json"
------- STATS -------
incl_main_tag_err_ms: 78
incl_mem_tag_err_ms: 79
kernel_output_lookup_ms: 79
lookup_byte_lengths_ms: 75
lookup_byte_operations_ms: 81
lookup_cd_value_ms: 77
lookup_div_u16_0_ms: 100
lookup_div_u16_1_ms: 98
lookup_div_u16_2_ms: 99
lookup_div_u16_3_ms: 97
lookup_div_u16_4_ms: 99
lookup_div_u16_5_ms: 103
lookup_div_u16_6_ms: 97
lookup_div_u16_7_ms: 97
lookup_into_kernel_ms: 83
lookup_mem_rng_chk_hi_ms: 79
lookup_mem_rng_chk_lo_ms: 95
lookup_mem_rng_chk_mid_ms: 108
lookup_opcode_gas_ms: 78
lookup_pow_2_0_ms: 84
lookup_pow_2_1_ms: 79
lookup_ret_value_ms: 82
lookup_u16_0_ms: 107
lookup_u16_10_ms: 107
lookup_u16_11_ms: 96
lookup_u16_12_ms: 96
lookup_u16_13_ms: 97
lookup_u16_14_ms: 103
lookup_u16_1_ms: 95
lookup_u16_2_ms: 96
lookup_u16_3_ms: 97
lookup_u16_4_ms: 98
lookup_u16_5_ms: 111
lookup_u16_6_ms: 99
lookup_u16_7_ms: 97
lookup_u16_8_ms: 97
lookup_u16_9_ms: 96
lookup_u8_0_ms: 79
lookup_u8_1_ms: 77
perm_main_alu_ms: 82
perm_main_bin_ms: 76
perm_main_conv_ms: 75
perm_main_mem_a_ms: 80
perm_main_mem_b_ms: 77
perm_main_mem_c_ms: 75
perm_main_mem_d_ms: 77
perm_main_mem_ind_addr_a_ms: 76
perm_main_mem_ind_addr_b_ms: 77
perm_main_mem_ind_addr_c_ms: 74
perm_main_mem_ind_addr_d_ms: 76
perm_main_pedersen_ms: 75
perm_main_pos2_perm_ms: 79
perm_main_slice_ms: 74
perm_slice_mem_ms: 80
prove/check_circuit: 5120
prove/execute_log_derivative_inverse_commitments_round_ms: 532
prove/execute_log_derivative_inverse_round_ms: 5199
prove/execute_pcs_rounds_ms: 413
prove/execute_relation_check_rounds_ms: 1328
prove/execute_wire_commitments_round_ms: 1742
prove/gen_trace: 850
range_check_da_gas_hi_ms: 98
range_check_da_gas_lo_ms: 103
range_check_l2_gas_hi_ms: 100
range_check_l2_gas_lo_ms: 98
```
  • Loading branch information
fcarreiro authored Jul 15, 2024
1 parent 699fb79 commit 1a6c7f2
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 59 deletions.
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
12 changes: 9 additions & 3 deletions barretenberg/cpp/src/barretenberg/vm/avm_trace/avm_execution.cpp
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
1 change: 0 additions & 1 deletion bb-pilcom/bb-pil-backend/templates/circuit_builder.hpp.hbs
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

0 comments on commit 1a6c7f2

Please sign in to comment.