Skip to content

Commit

Permalink
chore(avm): count non-zero elems and others (#7498)
Browse files Browse the repository at this point in the history
```
Final trace size: 65537
Number of columns: 411
Number of non-zero elements: 618777/26935707 (2%)
Relation degrees: 
        alu: [7°: 1, 5°: 3, 4°: 13, 3°: 24, 2°: 41, 1°: 5]
        binary: [3°: 4, 2°: 6]
        conversion: [2°: 1]
        gas: [1°: 3]
        keccakf1600: [2°: 1]
        kernel: [3°: 2, 2°: 8]
        main: [4°: 4, 3°: 7, 2°: 138, 1°: 2]
        mem: [5°: 1, 3°: 9, 2°: 33, 1°: 1]
        mem_slice: [3°: 3, 2°: 7, 1°: 1]
        pedersen: [2°: 1]
        poseidon2: [2°: 1]
        powers: [1°: 1]
        sha256: [2°: 1]
```
  • Loading branch information
fcarreiro authored Jul 17, 2024
1 parent 0d5c066 commit 7d97c0f
Show file tree
Hide file tree
Showing 6 changed files with 526 additions and 420 deletions.
77 changes: 77 additions & 0 deletions barretenberg/cpp/src/barretenberg/vm/avm_trace/avm_execution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
#include "barretenberg/vm/avm_trace/stats.hpp"
#include "barretenberg/vm/generated/avm_circuit_builder.hpp"
#include "barretenberg/vm/generated/avm_composer.hpp"
#include "barretenberg/vm/generated/avm_flavor.hpp"

#include <cassert>
#include <cstddef>
#include <cstdint>
#include <filesystem>
#include <string>
#include <tuple>
#include <unordered_map>
#include <variant>
#include <vector>

Expand All @@ -30,6 +32,53 @@ using namespace bb;
std::filesystem::path avm_dump_trace_path;

namespace bb::avm_trace {
namespace {

template <typename K, typename V>
std::vector<std::pair<K, V>> sorted_entries(const std::unordered_map<K, V>& map, bool invert = false)
{
std::vector<std::pair<K, V>> entries;
entries.reserve(map.size());
for (const auto& [key, value] : map) {
entries.emplace_back(key, value);
}
std::sort(entries.begin(), entries.end(), [invert](const auto& a, const auto& b) {
bool r = a.first < b.first;
if (invert) {
r = !r;
}
return r;
});
return entries;
}

// Returns degree distribution information for main relations (e.g., not lookups/perms).
std::unordered_map</*relation*/ std::string, /*degrees*/ std::string> get_relations_degrees()
{
std::unordered_map<std::string, std::string> relations_degrees;

bb::constexpr_for<0, std::tuple_size_v<AvmFlavor::MainRelations>, 1>([&]<size_t i>() {
std::unordered_map<int, int> degree_distribution;
using Relation = std::tuple_element_t<i, AvmFlavor::Relations>;
for (const auto& len : Relation::SUBRELATION_PARTIAL_LENGTHS) {
degree_distribution[static_cast<int>(len - 1)]++;
}
std::string degrees_string;
auto entries = sorted_entries(degree_distribution, /*invert=*/true);
for (size_t n = 0; n < entries.size(); n++) {
const auto& [degree, count] = entries[n];
if (n > 0) {
degrees_string += ", ";
}
degrees_string += std::to_string(degree) + "°: " + std::to_string(count);
}
relations_degrees.insert({ Relation::NAME, std::move(degrees_string) });
});

return relations_degrees;
}

} // namespace

/**
* @brief Temporary routine to generate default public inputs (gas values) until we get
Expand Down Expand Up @@ -752,6 +801,34 @@ std::vector<Row> Execution::gen_trace(std::vector<Instruction> const& instructio

auto trace = trace_builder.finalize();
vinfo("Final trace size: ", trace.size());
vinfo("Number of columns: ", trace.front().SIZE);
const size_t total_elements = trace.front().SIZE * trace.size();
const size_t nonzero_elements = [&]() {
size_t count = 0;
for (auto const& row : trace) {
for (const auto& ff : row.as_vector()) {
if (!ff.is_zero()) {
count++;
}
}
}
return count;
}();
vinfo("Number of non-zero elements: ",
nonzero_elements,
"/",
total_elements,
" (",
100 * nonzero_elements / total_elements,
"%)");
vinfo("Relation degrees: ", []() {
std::string result;
for (const auto& [key, value] : sorted_entries(get_relations_degrees())) {
result += "\n\t" + key + ": [" + value + "]";
}
return result;
}());

return trace;
}

Expand Down
4 changes: 2 additions & 2 deletions barretenberg/cpp/src/barretenberg/vm/avm_trace/avm_trace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3786,8 +3786,8 @@ std::vector<Row> AvmTraceBuilder::finalize(uint32_t min_trace_size, bool range_c
range_check_size + 1, // The manually inserted first row is part of the range check
"\n\tconv_trace_size: ",
conv_trace_size,
"\n\tlookup_table_size: ",
lookup_table_size,
"\n\tbin_trace_size: ",
bin_trace_size,
"\n\tsha256_trace_size: ",
sha256_trace_size,
"\n\tposeidon2_trace_size: ",
Expand Down
Loading

0 comments on commit 7d97c0f

Please sign in to comment.