Skip to content

Commit

Permalink
chore(avm): basic stat collection
Browse files Browse the repository at this point in the history
  • Loading branch information
fcarreiro committed Jul 3, 2024
1 parent 6fafff6 commit 2de19aa
Show file tree
Hide file tree
Showing 8 changed files with 284 additions and 127 deletions.
6 changes: 6 additions & 0 deletions barretenberg/cpp/src/barretenberg/bb/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,12 @@ void avm_prove(const std::filesystem::path& bytecode_path,
vinfo("vk written to: ", vk_path);
write_file(vk_fields_path, { vk_json.begin(), vk_json.end() });
vinfo("vk as fields written to: ", vk_fields_path);

#ifdef AVM_TRACK_STATS
info("------- STATS -------");
const auto& stats = avm_trace::Stats::get();
info(stats.to_string());
#endif
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "barretenberg/common/throw_or_abort.hpp"
#include "barretenberg/serialize/msgpack.hpp"
#include "barretenberg/vm/avm_trace/constants.hpp"
#include "barretenberg/vm/avm_trace/stats.hpp"
#include "barretenberg/vm/generated/avm_flavor.hpp"

#include <array>
Expand Down
60 changes: 60 additions & 0 deletions barretenberg/cpp/src/barretenberg/vm/avm_trace/stats.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "barretenberg/vm/avm_trace/stats.hpp"

#include <chrono>
#include <cstdint>
#include <string>
#include <vector>

namespace bb::avm_trace {

Stats& Stats::get()
{
static Stats stats;
return stats;
}

void Stats::reset()
{
stats.clear();
}

void Stats::increment(const std::string& key, uint64_t value)
{
stats[key] += value;
}

void Stats::time(const std::string& key, std::function<void()> f)
{
auto start = std::chrono::system_clock::now();
f();
auto elapsed = std::chrono::system_clock::now() - start;
increment(key, static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count()));
}

std::string Stats::to_string() const
{
std::vector<std::string> result;
result.reserve(stats.size());
for (const auto& [key, value] : stats) {
result.push_back(key + ": " + std::to_string(value));
}
std::sort(result.begin(), result.end());
std::string joined;
for (auto& s : result) {
joined += std::move(s) + "\n";
}
return joined;
}

std::string Stats::aggregate_to_string(const std::string& key_prefix) const
{
uint64_t result = 0;
for (const auto& [key, value] : stats) {
if (key.starts_with(key_prefix)) {
result += value;
}
}
return key_prefix + ": " + std::to_string(result);
}

} // namespace bb::avm_trace
37 changes: 37 additions & 0 deletions barretenberg/cpp/src/barretenberg/vm/avm_trace/stats.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#pragma once

#include <cstdint>
#include <functional>
#include <string>
#include <unordered_map>

// To enable stats tracking, compile in RelWithAssert mode.
// cmake --preset $PRESET -DCMAKE_BUILD_TYPE=RelWithAssert
#ifndef NDEBUG
#define AVM_TRACK_STATS
#endif

#ifdef AVM_TRACK_STATS
#define AVM_TRACK_TIME(key, body) ::bb::avm_trace::Stats::get().time(key, [&]() { body; });
#else
#define AVM_TRACK_TIME(key, body) body
#endif

namespace bb::avm_trace {

class Stats {
public:
static Stats& get();
void reset();
void increment(const std::string& key, uint64_t value);
void time(const std::string& key, std::function<void()> f);
std::string to_string() const;
std::string aggregate_to_string(const std::string& key_prefix) const;

private:
Stats() = default;

std::unordered_map<std::string, uint64_t> stats;
};

} // namespace bb::avm_trace
271 changes: 163 additions & 108 deletions barretenberg/cpp/src/barretenberg/vm/generated/avm_flavor.hpp

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions barretenberg/cpp/src/barretenberg/vm/generated/avm_prover.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "barretenberg/polynomials/polynomial.hpp"
#include "barretenberg/relations/permutation_relation.hpp"
#include "barretenberg/sumcheck/sumcheck.hpp"
#include "barretenberg/vm/avm_trace/stats.hpp"

namespace bb {

Expand Down Expand Up @@ -991,18 +992,18 @@ HonkProof AvmProver::construct_proof()
execute_preamble_round();

// Compute wire commitments
execute_wire_commitments_round();
AVM_TRACK_TIME("proving/wire_commitments_round_ms", execute_wire_commitments_round());

// Compute sorted list accumulator and commitment
execute_log_derivative_inverse_round();
AVM_TRACK_TIME("proving/log_derivative_inverse_round_ms", execute_log_derivative_inverse_round());

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

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

return export_proof();
}
Expand Down
3 changes: 2 additions & 1 deletion bb-pilcom/bb-pil-backend/src/flavor_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ fn flavor_includes(name: &str, relation_file_names: &[String], lookups: &[String
#include \"barretenberg/relations/generic_permutation/generic_permutation_relation.hpp\"
#include \"barretenberg/vm/avm_trace/stats.hpp\"
#include \"barretenberg/flavor/flavor_macros.hpp\"
#include \"barretenberg/transcript/transcript.hpp\"
#include \"barretenberg/polynomials/evaluation_domain.hpp\"
Expand Down Expand Up @@ -502,7 +503,7 @@ fn create_compute_logderivative_inverses(flavor_name: &str, lookups: &[String])
}

let compute_inverse_transformation = |lookup_name: &String| {
format!("bb::compute_logderivative_inverse<{flavor_name}Flavor, {lookup_name}_relation<FF>>(prover_polynomials, relation_parameters, this->circuit_size);")
format!("AVM_TRACK_TIME(\"compute_logderivative_inverse/{lookup_name}_ms\", (bb::compute_logderivative_inverse<{flavor_name}Flavor, {lookup_name}_relation<FF>>(prover_polynomials, relation_parameters, this->circuit_size)));")
};

let compute_inverses = map_with_newline(lookups, compute_inverse_transformation);
Expand Down
24 changes: 10 additions & 14 deletions bb-pilcom/bb-pil-backend/src/prover_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,7 @@ impl ProverBuilder for BBFiles {
let include_str = includes_cpp(&snake_case(name));

let polynomial_commitment_phase = create_commitments_phase(commitment_polys);

let (call_log_derivative_phase, log_derivative_inverse_phase): (String, String) =
if lookup_names.is_empty() {
("".to_owned(), "".to_owned())
} else {
(
"execute_log_derivative_inverse_round();".to_owned(),
create_log_derivative_inverse_round(lookup_names),
)
};
let log_derivative_inverse_phase = create_log_derivative_inverse_round(lookup_names);

let prover_cpp = format!("
{include_str}
Expand Down Expand Up @@ -216,18 +207,18 @@ impl ProverBuilder for BBFiles {
execute_preamble_round();
// Compute wire commitments
execute_wire_commitments_round();
AVM_TRACK_TIME(\"proving/wire_commitments_round_ms\", execute_wire_commitments_round());
// Compute sorted list accumulator and commitment
{call_log_derivative_phase}
AVM_TRACK_TIME(\"proving/log_derivative_inverse_round_ms\", execute_log_derivative_inverse_round());
// Fiat-Shamir: alpha
// Run sumcheck subprotocol.
execute_relation_check_rounds();
AVM_TRACK_TIME(\"proving/relation_check_rounds_ms\", execute_relation_check_rounds());
// Fiat-Shamir: rho, y, x, z
// Execute Zeromorph multilinear PCS
execute_pcs_rounds();
AVM_TRACK_TIME(\"proving/pcs_rounds_ms\", execute_pcs_rounds());
return export_proof();
}}
Expand Down Expand Up @@ -274,6 +265,7 @@ fn includes_cpp(name: &str) -> String {
#include \"barretenberg/polynomials/polynomial.hpp\"
#include \"barretenberg/relations/permutation_relation.hpp\"
#include \"barretenberg/sumcheck/sumcheck.hpp\"
#include \"barretenberg/vm/avm_trace/stats.hpp\"
"
)
}
Expand Down Expand Up @@ -309,6 +301,10 @@ fn create_commitments_phase(polys_to_commit_to: &[String]) -> String {
}

fn create_log_derivative_inverse_round(lookup_operations: &[String]) -> String {
if lookup_operations.is_empty() {
return "".to_owned();
}

let all_commit_operations = map_with_newline(lookup_operations, commitment_transform);
let send_to_verifier_operations =
map_with_newline(lookup_operations, send_to_verifier_transform);
Expand Down

0 comments on commit 2de19aa

Please sign in to comment.