From 8212f7879bb51872bcc92f95b38fd658029b66f2 Mon Sep 17 00:00:00 2001 From: Cody Date: Thu, 7 Nov 2024 17:50:00 +0000 Subject: [PATCH 01/24] This works to fill all of the gates --- .../src/barretenberg/benchmark/CMakeLists.txt | 1 + .../mega_memory_bench/CMakeLists.txt | 5 + .../mega_memory_bench/mega_memory.bench.cpp | 364 ++++++++++++++++++ .../mega_circuit_builder.hpp | 4 +- 4 files changed, 372 insertions(+), 2 deletions(-) create mode 100644 barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/CMakeLists.txt create mode 100644 barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp diff --git a/barretenberg/cpp/src/barretenberg/benchmark/CMakeLists.txt b/barretenberg/cpp/src/barretenberg/benchmark/CMakeLists.txt index e76758ca571..31db5c02858 100644 --- a/barretenberg/cpp/src/barretenberg/benchmark/CMakeLists.txt +++ b/barretenberg/cpp/src/barretenberg/benchmark/CMakeLists.txt @@ -17,3 +17,4 @@ add_subdirectory(append_only_tree_bench) add_subdirectory(ultra_bench) add_subdirectory(stdlib_hash) add_subdirectory(circuit_construction_bench) +add_subdirectory(mega_memory_bench) diff --git a/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/CMakeLists.txt b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/CMakeLists.txt new file mode 100644 index 00000000000..f35647268d7 --- /dev/null +++ b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/CMakeLists.txt @@ -0,0 +1,5 @@ +barretenberg_module( + mega_memory_bench + ultra_honk + stdlib_primitives +) \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp new file mode 100644 index 00000000000..de3405d9d0f --- /dev/null +++ b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp @@ -0,0 +1,364 @@ +#include "barretenberg/stdlib/primitives/field/field.hpp" +#include "barretenberg/stdlib/primitives/plookup/plookup.hpp" +#include "barretenberg/stdlib_circuit_builders/ultra_circuit_builder.hpp" +#include "barretenberg/ultra_honk/decider_proving_key.hpp" +#include +#pragma GCC diagnostic ignored "-Wunused-variable" + +using namespace benchmark; +using namespace bb; +using namespace bb::plookup; + +namespace { +auto& engine = numeric::get_debug_randomness(); +} + +using DeciderProvingKey = DeciderProvingKey_; +using Builder = MegaCircuitBuilder; +using field_ct = stdlib::field_t; +using witness_ct = stdlib::witness_t; +using plookup_read = stdlib::plookup_read; + +// void to_run(State& state, TraceStructure structure) +// { +// Builder builder; +// builder.blocks.set_fixed_block_sizes(structure); + +// for (const auto& block : builder.blocks.get()) { +// info(block.size(), " / ", block.get_fixed_size()); +// } + +// for (auto& block : builder.blocks.get()) { +// const size_t target_size = block.get_fixed_size() - 10; +// info("target size: ", target_size); + +// for (auto& vec : block.selectors) { +// vec.reserve(target_size); +// std::generate_n(std::back_inserter(vec), target_size, []() { return fr::random_element(); }); +// } + +// for (auto& vec : block.wires) { +// vec.reserve(target_size); +// std::generate_n( +// std::back_inserter(vec), target_size, []() { return static_cast(fr::random_element()); }); +// } +// } + +// for (const auto& block : builder.blocks.get()) { +// info(block.size(), " / ", block.get_fixed_size()); +// } + +// for (auto _ : state) { +// auto proving_key = std::make_shared(builder, structure); +// benchmark::DoNotOptimize(proving_key); +// } +// } + +static constexpr size_t NUM_SHORT = 10; + +void fill_ecc_op_block(Builder& builder) +{ + const auto point = g1::affine_element::random_element(); + const auto scalar = fr::random_element(); + const size_t num_to_add((builder.blocks.ecc_op.get_fixed_size() - NUM_SHORT) >> 1); // each accum call adds two rows + for (size_t idx = 0; idx < num_to_add; idx++) { + builder.queue_ecc_mul_accum(point, scalar); + } +} + +void fill_pub_inputs_block(Builder& builder) +{ + for (size_t idx = 0; idx < builder.blocks.pub_inputs.get_fixed_size() - NUM_SHORT; idx++) { + builder.add_public_variable(fr::random_element()); + } +} + +void fill_databus_blocks(Builder& builder) +{ + static constexpr size_t NUM_BUS_IDS(3); + const size_t num_gates_per_bus_id((builder.blocks.busread.get_fixed_size() - NUM_SHORT) / NUM_BUS_IDS); + for (size_t idx = 1; idx < num_gates_per_bus_id + 1; idx++) { // start at 1 to avoid / by zero below + const uint32_t idx_1_1 = builder.add_variable(fr::random_element()); + const uint32_t idx_1_2 = builder.add_variable(static_cast(fr::random_element()) % idx); + builder.add_public_calldata(idx_1_1); + builder.read_calldata(idx_1_2); + const uint32_t idx_2_1 = builder.add_variable(fr::random_element()); + const uint32_t idx_2_2 = builder.add_variable(static_cast(fr::random_element()) % idx); + builder.add_public_secondary_calldata(idx_2_1); + builder.read_secondary_calldata(idx_2_2); + const uint32_t idx_3_1 = builder.add_variable(fr::random_element()); + const uint32_t idx_3_2 = builder.add_variable(static_cast(fr::random_element()) % idx); + builder.add_public_return_data(idx_3_1); + builder.read_return_data(idx_3_2); + } +} + +void fill_delta_range_block(Builder& builder) +{ + // At the moment the trace has space for 90k delta range gates but I don't think it's possible to use them all + // because there is not enough capacity in the arithmetic block! + + const uint32_t idx_1 = builder.add_variable(1 << 0); + builder.create_range_constraint(idx_1, 1, "whoops"); + const uint32_t idx_2 = builder.add_variable(1 << 1); + builder.create_range_constraint(idx_2, 2, "whoops"); + const uint32_t idx_3 = builder.add_variable(1 << 2); + builder.create_range_constraint(idx_3, 3, "whoops"); + const uint32_t idx_4 = builder.add_variable(1 << 3); + builder.create_range_constraint(idx_4, 4, "whoops"); + const uint32_t idx_5 = builder.add_variable(1 << 4); + builder.create_range_constraint(idx_5, 5, "whoops"); + const uint32_t idx_6 = builder.add_variable(1 << 5); + builder.create_range_constraint(idx_6, 6, "whoops"); + const uint32_t idx_7 = builder.add_variable(1 << 6); + builder.create_range_constraint(idx_7, 7, "whoops"); + const uint32_t idx_8 = builder.add_variable(1 << 7); + builder.create_range_constraint(idx_8, 8, "whoops"); + const uint32_t idx_9 = builder.add_variable(1 << 8); + builder.create_range_constraint(idx_9, 9, "whoops"); + const uint32_t idx_10 = builder.add_variable(1 << 9); + builder.create_range_constraint(idx_10, 10, "whoops"); + const uint32_t idx_11 = builder.add_variable(1 << 10); + builder.create_range_constraint(idx_11, 11, "whoops"); + const uint32_t idx_12 = builder.add_variable(1 << 11); + builder.create_range_constraint(idx_12, 12, "whoops"); + const uint32_t idx_13 = builder.add_variable(1 << 12); + builder.create_range_constraint(idx_13, 13, "whoops"); + const uint32_t idx_14 = builder.add_variable(1 << 13); + builder.create_range_constraint(idx_14, 14, "whoops"); + // the above range constraints as 2759 gates + static constexpr size_t NUM_GATES_ADDED_FOR_ALL_DEFAULT_RANGES = 2759; + + size_t num_range_constraints = 14; + + auto& range_block = builder.blocks.delta_range; + auto& arith_block = builder.blocks.arithmetic; + + const auto range_block_has_space = [&range_block, &num_range_constraints]() { + return num_range_constraints < + 4 * (range_block.get_fixed_size() - NUM_GATES_ADDED_FOR_ALL_DEFAULT_RANGES - NUM_SHORT); + }; + + const auto arith_block_has_space = [&arith_block]() { + return arith_block.size() < arith_block.get_fixed_size() - 100; + }; + + while (range_block_has_space() && arith_block_has_space()) { + const uint32_t w_idx = builder.add_variable(1023); + builder.create_range_constraint(w_idx, 10, "failed to create range constraint"); + num_range_constraints++; + } +} + +void fill_arithmetic_block(Builder& builder) +{ + const uint32_t idx_1 = builder.add_variable(fr::random_element()); + const uint32_t idx_2 = builder.add_variable(fr::random_element()); + const uint32_t idx_3 = builder.add_variable(fr::random_element()); + const uint32_t idx_4 = builder.add_variable(fr::random_element()); + while (builder.blocks.arithmetic.size() < builder.blocks.arithmetic.get_fixed_size() - 10 * NUM_SHORT) { + builder.create_big_add_gate({ idx_1, idx_2, idx_3, idx_4, 1, 1, 1, 1, 1 }); + } +} + +void fill_elliptic_block(Builder& builder) +{ + const uint32_t x1_idx = builder.add_variable(fr::random_element()); + const uint32_t y1_idx = builder.add_variable(fr::random_element()); + const uint32_t x2_idx = builder.add_variable(fr::random_element()); + const uint32_t y2_idx = builder.add_variable(fr::random_element()); + const uint32_t x3_idx = builder.add_variable(fr::random_element()); + const uint32_t y3_idx = builder.add_variable(fr::random_element()); + while (builder.blocks.elliptic.size() < builder.blocks.elliptic.get_fixed_size() - 100) { + builder.create_ecc_add_gate({ x1_idx, y1_idx, x2_idx, y2_idx, x3_idx, y3_idx, 1 }); + } +} + +void fill_aux_block(Builder& builder) +{ + // static constexpr size_t NUM_AUX_TYPES = 11; + auto& block = builder.blocks.aux; + + const uint32_t idx_1 = builder.add_variable(fr::random_element()); + const uint32_t idx_2 = builder.add_variable(fr::random_element()); + const uint32_t idx_3 = builder.add_variable(fr::random_element()); + const uint32_t idx_4 = builder.add_variable(fr::random_element()); + while (block.size() < block.get_fixed_size() - 100) { + builder.apply_aux_selectors(Builder::AUX_SELECTORS::ROM_READ); + block.populate_wires(idx_1, idx_2, idx_3, idx_4); + builder.apply_aux_selectors(Builder::AUX_SELECTORS::LIMB_ACCUMULATE_1); + block.populate_wires(idx_1, idx_2, idx_3, idx_4); + builder.apply_aux_selectors(Builder::AUX_SELECTORS::LIMB_ACCUMULATE_2); + block.populate_wires(idx_1, idx_2, idx_3, idx_4); + builder.apply_aux_selectors(Builder::AUX_SELECTORS::NON_NATIVE_FIELD_1); + block.populate_wires(idx_1, idx_2, idx_3, idx_4); + builder.apply_aux_selectors(Builder::AUX_SELECTORS::NON_NATIVE_FIELD_2); + block.populate_wires(idx_1, idx_2, idx_3, idx_4); + builder.apply_aux_selectors(Builder::AUX_SELECTORS::NON_NATIVE_FIELD_3); + block.populate_wires(idx_1, idx_2, idx_3, idx_4); + builder.apply_aux_selectors(Builder::AUX_SELECTORS::RAM_CONSISTENCY_CHECK); + block.populate_wires(idx_1, idx_2, idx_3, idx_4); + builder.apply_aux_selectors(Builder::AUX_SELECTORS::ROM_CONSISTENCY_CHECK); + block.populate_wires(idx_1, idx_2, idx_3, idx_4); + builder.apply_aux_selectors(Builder::AUX_SELECTORS::RAM_TIMESTAMP_CHECK); + block.populate_wires(idx_1, idx_2, idx_3, idx_4); + builder.apply_aux_selectors(Builder::AUX_SELECTORS::ROM_READ); + block.populate_wires(idx_1, idx_2, idx_3, idx_4); + builder.apply_aux_selectors(Builder::AUX_SELECTORS::RAM_READ); + block.populate_wires(idx_1, idx_2, idx_3, idx_4); + builder.apply_aux_selectors(Builder::AUX_SELECTORS::RAM_WRITE); + block.populate_wires(idx_1, idx_2, idx_3, idx_4); + } +} + +void fill_poseidon2_internal_block(Builder& builder) +{ + auto& block = builder.blocks.poseidon2_internal; + const uint32_t idx_1 = builder.add_variable(fr::random_element()); + const uint32_t idx_2 = builder.add_variable(fr::random_element()); + const uint32_t idx_3 = builder.add_variable(fr::random_element()); + const uint32_t idx_4 = builder.add_variable(fr::random_element()); + + while (block.size() < block.get_fixed_size() - NUM_SHORT) { + builder.create_poseidon2_internal_gate({ idx_1, idx_2, idx_3, idx_4, 1 }); + } +} + +void fill_poseidon2_external_block(Builder& builder) +{ + auto& block = builder.blocks.poseidon2_external; + const uint32_t idx_1 = builder.add_variable(fr::random_element()); + const uint32_t idx_2 = builder.add_variable(fr::random_element()); + const uint32_t idx_3 = builder.add_variable(fr::random_element()); + const uint32_t idx_4 = builder.add_variable(fr::random_element()); + + while (block.size() < block.get_fixed_size() - NUM_SHORT) { + builder.create_poseidon2_external_gate({ idx_1, idx_2, idx_3, idx_4, 1 }); + } +} + +void fill_lookup_block(Builder& builder) +{ + auto& block = builder.blocks.lookup; + + // static constexpr size_t NUM_LOOKUP_TYPES_USED(15); + + while (block.size() < (block.get_fixed_size() - 20 * NUM_SHORT)) { + uint256_t left_value = (engine.get_random_uint256() & 0xffffffffULL); + uint256_t right_value = (engine.get_random_uint256() & 0xffffffffULL); + + field_ct left = witness_ct(&builder, bb::fr(left_value)); + field_ct right = witness_ct(&builder, bb::fr(right_value)); + + plookup_read::get_lookup_accumulators(MultiTableId::SHA256_CH_INPUT, left, right, true); + // info("read SHA256_CH_INPUT"); + plookup_read::get_lookup_accumulators(MultiTableId::SHA256_CH_OUTPUT, left, right, true); + // info("read SHA256_CH_OUTPUT"); + plookup_read::get_lookup_accumulators(MultiTableId::SHA256_MAJ_INPUT, left, right, true); + // info("read SHA256_MAJ_INPUT"); + plookup_read::get_lookup_accumulators(MultiTableId::SHA256_MAJ_OUTPUT, left, right, true); + // info("read SHA256_MAJ_OUTPUT"); + plookup_read::get_lookup_accumulators(MultiTableId::SHA256_WITNESS_INPUT, left, right, true); + // info("read SHA256_WITNESS_INPUT"); + plookup_read::get_lookup_accumulators(MultiTableId::SHA256_WITNESS_OUTPUT, left, right, true); + // // plookup_read::get_lookup_accumulators(MultiTableId::BN254_XLO, left, right, true); + // // info("read BN254_XLO"); + // // plookup_read::get_lookup_accumulators(MultiTableId::BN254_XHI, left, right, true); + // // info("read BN254_XHI"); + // // plookup_read::get_lookup_accumulators(MultiTableId::BN254_YLO, left, right, true); + // // info("read BN254_YLO"); + // // plookup_read::get_lookup_accumulators(MultiTableId::BN254_YHI, left, right, true); + // // info("read BN254_YHI"); + // // plookup_read::get_lookup_accumulators(MultiTableId::BN254_XYPRIME, left, right, true); + // // info("read BN254_XYPRIME"); + // // plookup_read::get_lookup_accumulators(MultiTableId::BN254_XLO_ENDO, left, right, true); + // // info("read BN254_XLO_ENDO"); + // // plookup_read::get_lookup_accumulators(MultiTableId::BN254_XHI_ENDO, left, right, true); + // // info("read BN254_XHI_ENDO"); + // // plookup_read::get_lookup_accumulators(MultiTableId::BN254_XYPRIME_ENDO, left, right, true); + // // info("read BN254_XYPRIME_ENDO"); + // // plookup_read::get_lookup_accumulators(MultiTableId::SECP256K1_XLO, left, right, true); + // // info("read SECP256K1_XLO"); + // // plookup_read::get_lookup_accumulators(MultiTableId::SECP256K1_XHI, left, right, true); + // // info("read SECP256K1_XHI"); + // // plookup_read::get_lookup_accumulators(MultiTableId::SECP256K1_YLO, left, right, true); + // // info("read SECP256K1_YLO"); + // // plookup_read::get_lookup_accumulators(MultiTableId::SECP256K1_YHI, left, right, true); + // // info("read SECP256K1_YHI"); + // // plookup_read::get_lookup_accumulators(MultiTableId::SECP256K1_XYPRIME, left, right, true); + // // info("read SECP256K1_XYPRIME"); + // // plookup_read::get_lookup_accumulators(MultiTableId::SECP256K1_XLO_ENDO, left, right, true); + // // info("read SECP256K1_XLO_ENDO"); + // // plookup_read::get_lookup_accumulators(MultiTableId::SECP256K1_XHI_ENDO, left, right, true); + // // info("read SECP256K1_XHI_ENDO"); + // // plookup_read::get_lookup_accumulators(MultiTableId::SECP256K1_XYPRIME_ENDO, left, right, true); + // // info("read SECP256K1_XYPRIME_ENDO"); + // plookup_read::get_lookup_accumulators(MultiTableId::BLAKE_XOR, left, right, true); + // // info("read BLAKE_XOR"); + // plookup_read::get_lookup_accumulators(MultiTableId::BLAKE_XOR_ROTATE_16, left, right, true); + // // info("read BLAKE_XOR_ROTATE_16"); + // plookup_read::get_lookup_accumulators(MultiTableId::BLAKE_XOR_ROTATE_8, left, right, true); + // // info("read BLAKE_XOR_ROTATE_8"); + // plookup_read::get_lookup_accumulators(MultiTableId::BLAKE_XOR_ROTATE_7, left, right, true); + // // info("read BLAKE_XOR_ROTATE_7"); + // // plookup_read::get_lookup_accumulators(MultiTableId::PEDERSEN_IV, left, right, true); + // // info("read PEDERSEN_IV"); + // // plookup_read::get_lookup_accumulators(MultiTableId::KECCAK_THETA_OUTPUT, left, right, true); + // // info("read KECCAK_THETA_OUTPUT"); + // // plookup_read::get_lookup_accumulators(MultiTableId::KECCAK_CHI_OUTPUT, left, right, true); + // // info("read KECCAK_CHI_OUTPUT"); + // // plookup_read::get_lookup_accumulators(MultiTableId::KECCAK_FORMAT_INPUT, left, right, true); + // // info("read KECCAK_FORMAT_INPUT"); + // // plookup_read::get_lookup_accumulators(MultiTableId::KECCAK_FORMAT_OUTPUT, left, right, true); + // // info("read KECCAK_FORMAT_OUTPUT"); + // // plookup_read::get_lookup_accumulators(MultiTableId::KECCAK_NORMALIZE_AND_ROTATE, left, right, true); + // // info("read KECCAK_NORMALIZE_AND_ROTATE"); + } +} + +void to_run(State& state, TraceStructure structure) +{ + Builder builder; + builder.blocks.set_fixed_block_sizes(structure); + + for (const auto& block : builder.blocks.get()) { + info(block.size(), " / ", block.get_fixed_size()); + } + + fill_ecc_op_block(builder); + fill_pub_inputs_block(builder); + fill_databus_blocks(builder); + fill_delta_range_block(builder); + fill_arithmetic_block(builder); // must come after fill_delta_range_block + fill_elliptic_block(builder); + fill_aux_block(builder); + fill_poseidon2_external_block(builder); + fill_poseidon2_internal_block(builder); + fill_lookup_block(builder); + builder.finalize_circuit(/* ensure_nonzero */ false); + info("DONE FILLING BLOCKS"); + + for (size_t idx = 0; const auto& block : builder.blocks.get()) { + bool overfilled = block.size() >= block.get_fixed_size(); + // ASSERT(!overfilled); + if (overfilled) { + info("block overfilled at index ", idx); + } + info(block.size(), " / ", block.get_fixed_size()); + idx++; + } + + for (auto _ : state) { + auto proving_key = std::make_shared(builder, structure); + benchmark::DoNotOptimize(proving_key); + } +} + +static void construct_dpk(State& state, void (*test_circuit_function)(State&, TraceStructure)) noexcept +{ + test_circuit_function(state, TraceStructure::E2E_FULL_TEST); +} + +BENCHMARK_CAPTURE(construct_dpk, TraceStructure::CLIENT_IVC_BENCH, &to_run)->Unit(kMillisecond)->Iterations(1); + +BENCHMARK_MAIN(); diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_circuit_builder.hpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_circuit_builder.hpp index dd51a5424f9..155eadea729 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_circuit_builder.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_circuit_builder.hpp @@ -8,7 +8,7 @@ namespace bb { template class MegaCircuitBuilder_ : public UltraCircuitBuilder_> { - private: + public: DataBus databus; // Container for public calldata/returndata public: @@ -36,7 +36,7 @@ template class MegaCircuitBuilder_ : public UltraCircuitBuilder_& in, BusId bus_idx); From 81ec7e830c87e555fb5330e18110d9c360137622 Mon Sep 17 00:00:00 2001 From: Cody Date: Thu, 7 Nov 2024 18:50:37 +0000 Subject: [PATCH 02/24] Use most tables --- .../mega_memory_bench/mega_memory.bench.cpp | 118 +++++++++--------- .../plookup_tables/plookup_tables.cpp | 3 +- .../plookup_tables/types.hpp | 2 +- 3 files changed, 62 insertions(+), 61 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp index de3405d9d0f..558976de50c 100644 --- a/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp +++ b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp @@ -1,7 +1,9 @@ #include "barretenberg/stdlib/primitives/field/field.hpp" #include "barretenberg/stdlib/primitives/plookup/plookup.hpp" +#include "barretenberg/stdlib_circuit_builders/plookup_tables/fixed_base/fixed_base.hpp" #include "barretenberg/stdlib_circuit_builders/ultra_circuit_builder.hpp" #include "barretenberg/ultra_honk/decider_proving_key.hpp" + #include #pragma GCC diagnostic ignored "-Wunused-variable" @@ -244,75 +246,73 @@ void fill_lookup_block(Builder& builder) // static constexpr size_t NUM_LOOKUP_TYPES_USED(15); while (block.size() < (block.get_fixed_size() - 20 * NUM_SHORT)) { + // SHA uint256_t left_value = (engine.get_random_uint256() & 0xffffffffULL); uint256_t right_value = (engine.get_random_uint256() & 0xffffffffULL); - field_ct left = witness_ct(&builder, bb::fr(left_value)); field_ct right = witness_ct(&builder, bb::fr(right_value)); - plookup_read::get_lookup_accumulators(MultiTableId::SHA256_CH_INPUT, left, right, true); - // info("read SHA256_CH_INPUT"); plookup_read::get_lookup_accumulators(MultiTableId::SHA256_CH_OUTPUT, left, right, true); - // info("read SHA256_CH_OUTPUT"); plookup_read::get_lookup_accumulators(MultiTableId::SHA256_MAJ_INPUT, left, right, true); - // info("read SHA256_MAJ_INPUT"); plookup_read::get_lookup_accumulators(MultiTableId::SHA256_MAJ_OUTPUT, left, right, true); - // info("read SHA256_MAJ_OUTPUT"); plookup_read::get_lookup_accumulators(MultiTableId::SHA256_WITNESS_INPUT, left, right, true); - // info("read SHA256_WITNESS_INPUT"); plookup_read::get_lookup_accumulators(MultiTableId::SHA256_WITNESS_OUTPUT, left, right, true); - // // plookup_read::get_lookup_accumulators(MultiTableId::BN254_XLO, left, right, true); - // // info("read BN254_XLO"); - // // plookup_read::get_lookup_accumulators(MultiTableId::BN254_XHI, left, right, true); - // // info("read BN254_XHI"); - // // plookup_read::get_lookup_accumulators(MultiTableId::BN254_YLO, left, right, true); - // // info("read BN254_YLO"); - // // plookup_read::get_lookup_accumulators(MultiTableId::BN254_YHI, left, right, true); - // // info("read BN254_YHI"); - // // plookup_read::get_lookup_accumulators(MultiTableId::BN254_XYPRIME, left, right, true); - // // info("read BN254_XYPRIME"); - // // plookup_read::get_lookup_accumulators(MultiTableId::BN254_XLO_ENDO, left, right, true); - // // info("read BN254_XLO_ENDO"); - // // plookup_read::get_lookup_accumulators(MultiTableId::BN254_XHI_ENDO, left, right, true); - // // info("read BN254_XHI_ENDO"); - // // plookup_read::get_lookup_accumulators(MultiTableId::BN254_XYPRIME_ENDO, left, right, true); - // // info("read BN254_XYPRIME_ENDO"); - // // plookup_read::get_lookup_accumulators(MultiTableId::SECP256K1_XLO, left, right, true); - // // info("read SECP256K1_XLO"); - // // plookup_read::get_lookup_accumulators(MultiTableId::SECP256K1_XHI, left, right, true); - // // info("read SECP256K1_XHI"); - // // plookup_read::get_lookup_accumulators(MultiTableId::SECP256K1_YLO, left, right, true); - // // info("read SECP256K1_YLO"); - // // plookup_read::get_lookup_accumulators(MultiTableId::SECP256K1_YHI, left, right, true); - // // info("read SECP256K1_YHI"); - // // plookup_read::get_lookup_accumulators(MultiTableId::SECP256K1_XYPRIME, left, right, true); - // // info("read SECP256K1_XYPRIME"); - // // plookup_read::get_lookup_accumulators(MultiTableId::SECP256K1_XLO_ENDO, left, right, true); - // // info("read SECP256K1_XLO_ENDO"); - // // plookup_read::get_lookup_accumulators(MultiTableId::SECP256K1_XHI_ENDO, left, right, true); - // // info("read SECP256K1_XHI_ENDO"); - // // plookup_read::get_lookup_accumulators(MultiTableId::SECP256K1_XYPRIME_ENDO, left, right, true); - // // info("read SECP256K1_XYPRIME_ENDO"); - // plookup_read::get_lookup_accumulators(MultiTableId::BLAKE_XOR, left, right, true); - // // info("read BLAKE_XOR"); - // plookup_read::get_lookup_accumulators(MultiTableId::BLAKE_XOR_ROTATE_16, left, right, true); - // // info("read BLAKE_XOR_ROTATE_16"); - // plookup_read::get_lookup_accumulators(MultiTableId::BLAKE_XOR_ROTATE_8, left, right, true); - // // info("read BLAKE_XOR_ROTATE_8"); - // plookup_read::get_lookup_accumulators(MultiTableId::BLAKE_XOR_ROTATE_7, left, right, true); - // // info("read BLAKE_XOR_ROTATE_7"); - // // plookup_read::get_lookup_accumulators(MultiTableId::PEDERSEN_IV, left, right, true); - // // info("read PEDERSEN_IV"); - // // plookup_read::get_lookup_accumulators(MultiTableId::KECCAK_THETA_OUTPUT, left, right, true); - // // info("read KECCAK_THETA_OUTPUT"); - // // plookup_read::get_lookup_accumulators(MultiTableId::KECCAK_CHI_OUTPUT, left, right, true); - // // info("read KECCAK_CHI_OUTPUT"); - // // plookup_read::get_lookup_accumulators(MultiTableId::KECCAK_FORMAT_INPUT, left, right, true); - // // info("read KECCAK_FORMAT_INPUT"); - // // plookup_read::get_lookup_accumulators(MultiTableId::KECCAK_FORMAT_OUTPUT, left, right, true); - // // info("read KECCAK_FORMAT_OUTPUT"); - // // plookup_read::get_lookup_accumulators(MultiTableId::KECCAK_NORMALIZE_AND_ROTATE, left, right, true); - // // info("read KECCAK_NORMALIZE_AND_ROTATE"); + + // AES tables not actually used anywhere... + + // fixed base + auto pedersen_input_value = fr::random_element(); + const auto input_hi = + uint256_t(pedersen_input_value) + .slice(plookup::fixed_base::table::BITS_PER_LO_SCALAR, + plookup::fixed_base::table::BITS_PER_LO_SCALAR + plookup::fixed_base::table::BITS_PER_HI_SCALAR); + const auto input_lo = + uint256_t(pedersen_input_value).slice(0, bb::plookup::fixed_base::table::BITS_PER_LO_SCALAR); + const auto input_hi_index = builder.add_variable(input_hi); + const auto input_lo_index = builder.add_variable(input_lo); + plookup::get_lookup_accumulators(bb::plookup::MultiTableId::FIXED_BASE_LEFT_HI, input_hi); + plookup::get_lookup_accumulators(bb::plookup::MultiTableId::FIXED_BASE_LEFT_LO, input_lo); + plookup::get_lookup_accumulators(bb::plookup::MultiTableId::FIXED_BASE_RIGHT_HI, input_hi); + plookup::get_lookup_accumulators(bb::plookup::MultiTableId::FIXED_BASE_RIGHT_LO, input_lo); + + // bit ops + plookup_read::get_lookup_accumulators(MultiTableId::UINT32_XOR, left, right, true); + plookup_read::get_lookup_accumulators(MultiTableId::UINT32_AND, left, right, true); + + // bn254 generator slices + auto byte = field_ct::from_witness(&builder, engine.get_random_uint256() & 0xffULL); + plookup_read::get_lookup_accumulators(MultiTableId::BN254_XLO, byte, 0, false); + plookup_read::get_lookup_accumulators(MultiTableId::BN254_XHI, byte, 0, false); + plookup_read::get_lookup_accumulators(MultiTableId::BN254_YLO, byte, 0, false); + plookup_read::get_lookup_accumulators(MultiTableId::BN254_YHI, byte, 0, false); + plookup_read::get_lookup_accumulators(MultiTableId::BN254_XYPRIME, byte, 0, false); + plookup_read::get_lookup_accumulators(MultiTableId::BN254_XLO_ENDO, byte, 0, false); + plookup_read::get_lookup_accumulators(MultiTableId::BN254_XHI_ENDO, byte, 0, false); + plookup_read::get_lookup_accumulators(MultiTableId::BN254_XYPRIME_ENDO, byte, 0, false); + + // secp256k1 generator slices + plookup_read::get_lookup_accumulators(MultiTableId::SECP256K1_XLO, byte, 0, false); + plookup_read::get_lookup_accumulators(MultiTableId::SECP256K1_XHI, byte, 0, false); + plookup_read::get_lookup_accumulators(MultiTableId::SECP256K1_YLO, byte, 0, false); + plookup_read::get_lookup_accumulators(MultiTableId::SECP256K1_YHI, byte, 0, false); + plookup_read::get_lookup_accumulators(MultiTableId::SECP256K1_XYPRIME, byte, 0, false); + plookup_read::get_lookup_accumulators(MultiTableId::SECP256K1_XLO_ENDO, byte, 0, false); + plookup_read::get_lookup_accumulators(MultiTableId::SECP256K1_XHI_ENDO, byte, 0, false); + plookup_read::get_lookup_accumulators(MultiTableId::SECP256K1_XYPRIME_ENDO, byte, 0, false); + + // blake xor + plookup_read::get_lookup_accumulators(MultiTableId::BLAKE_XOR, left, right, true); + plookup_read::get_lookup_accumulators(MultiTableId::BLAKE_XOR_ROTATE_16, left, right, true); + plookup_read::get_lookup_accumulators(MultiTableId::BLAKE_XOR_ROTATE_8, left, right, true); + plookup_read::get_lookup_accumulators(MultiTableId::BLAKE_XOR_ROTATE_7, left, right, true); + + // keccak tests trigger + // SharedShiftedVirtualZeroesArray ... Assertion `(index >= start_ && index < end_)' failed. + // plookup_read::get_lookup_accumulators(MultiTableId::KECCAK_THETA_OUTPUT, left, right, true); + // plookup_read::get_lookup_accumulators(MultiTableId::KECCAK_CHI_OUTPUT, left, right, true); + // plookup_read::get_lookup_accumulators(MultiTableId::KECCAK_FORMAT_INPUT, left, right, true); + // plookup_read::get_lookup_accumulators(MultiTableId::KECCAK_FORMAT_OUTPUT, left, right, true); + // plookup_read::get_lookup_accumulators(MultiTableId::KECCAK_NORMALIZE_AND_ROTATE, left, right, true); } } diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/plookup_tables.cpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/plookup_tables.cpp index 0440eb17b9c..314013e4cfa 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/plookup_tables.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/plookup_tables.cpp @@ -49,7 +49,8 @@ void init_multi_tables() sha256_tables::get_majority_output_table(MultiTableId::SHA256_MAJ_OUTPUT); MULTI_TABLES[MultiTableId::SHA256_WITNESS_OUTPUT] = sha256_tables::get_witness_extension_output_table(MultiTableId::SHA256_WITNESS_OUTPUT); - MULTI_TABLES[MultiTableId::AES_NORMALIZE] = aes128_tables::get_aes_normalization_table(MultiTableId::AES_NORMALIZE); + MULTI_TABLES[MultiTableId::AES_NORMALIZE] = + aes128_tables::get_aes_normalization_table(MultiTableId::AES_NORMALIZE); // WORKTODO: table not used anywhere? MULTI_TABLES[MultiTableId::AES_INPUT] = aes128_tables::get_aes_input_table(MultiTableId::AES_INPUT); MULTI_TABLES[MultiTableId::AES_SBOX] = aes128_tables::get_aes_sbox_table(MultiTableId::AES_SBOX); MULTI_TABLES[MultiTableId::UINT32_XOR] = uint_tables::get_uint32_xor_table(MultiTableId::UINT32_XOR); diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/types.hpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/types.hpp index 259082820eb..8c7de0789b4 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/types.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/types.hpp @@ -111,7 +111,7 @@ enum MultiTableId { BLAKE_XOR_ROTATE_16, BLAKE_XOR_ROTATE_8, BLAKE_XOR_ROTATE_7, - PEDERSEN_IV, + PEDERSEN_IV, // WORKTODO: unused HONK_DUMMY_MULTI, KECCAK_THETA_OUTPUT, KECCAK_CHI_OUTPUT, From 272a6b3908fbea399af4feeb07712bbbfae0b780 Mon Sep 17 00:00:00 2001 From: Cody Date: Thu, 7 Nov 2024 19:29:33 +0000 Subject: [PATCH 03/24] OK filling verified for public inputs --- .../mega_memory_bench/mega_memory.bench.cpp | 88 ++++++++----------- .../execution_trace/execution_trace.cpp | 1 + .../ultra_honk/decider_proving_key.hpp | 4 +- 3 files changed, 38 insertions(+), 55 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp index 558976de50c..4ebc0afc575 100644 --- a/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp +++ b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp @@ -21,41 +21,6 @@ using field_ct = stdlib::field_t; using witness_ct = stdlib::witness_t; using plookup_read = stdlib::plookup_read; -// void to_run(State& state, TraceStructure structure) -// { -// Builder builder; -// builder.blocks.set_fixed_block_sizes(structure); - -// for (const auto& block : builder.blocks.get()) { -// info(block.size(), " / ", block.get_fixed_size()); -// } - -// for (auto& block : builder.blocks.get()) { -// const size_t target_size = block.get_fixed_size() - 10; -// info("target size: ", target_size); - -// for (auto& vec : block.selectors) { -// vec.reserve(target_size); -// std::generate_n(std::back_inserter(vec), target_size, []() { return fr::random_element(); }); -// } - -// for (auto& vec : block.wires) { -// vec.reserve(target_size); -// std::generate_n( -// std::back_inserter(vec), target_size, []() { return static_cast(fr::random_element()); }); -// } -// } - -// for (const auto& block : builder.blocks.get()) { -// info(block.size(), " / ", block.get_fixed_size()); -// } - -// for (auto _ : state) { -// auto proving_key = std::make_shared(builder, structure); -// benchmark::DoNotOptimize(proving_key); -// } -// } - static constexpr size_t NUM_SHORT = 10; void fill_ecc_op_block(Builder& builder) @@ -316,15 +281,11 @@ void fill_lookup_block(Builder& builder) } } -void to_run(State& state, TraceStructure structure) +void fill_trace(State& state, TraceStructure structure) { Builder builder; builder.blocks.set_fixed_block_sizes(structure); - for (const auto& block : builder.blocks.get()) { - info(block.size(), " / ", block.get_fixed_size()); - } - fill_ecc_op_block(builder); fill_pub_inputs_block(builder); fill_databus_blocks(builder); @@ -336,29 +297,50 @@ void to_run(State& state, TraceStructure structure) fill_poseidon2_internal_block(builder); fill_lookup_block(builder); builder.finalize_circuit(/* ensure_nonzero */ false); - info("DONE FILLING BLOCKS"); - - for (size_t idx = 0; const auto& block : builder.blocks.get()) { - bool overfilled = block.size() >= block.get_fixed_size(); - // ASSERT(!overfilled); - if (overfilled) { - info("block overfilled at index ", idx); + { + // finalize doesn't populate public inputs block, so copy to verify that the block is being filled well + // otherwise the pk construction will overflow the block + // alternative: add to finalize or add a flag to check whether PIs have already been populated + auto builder_copy = builder; + DeciderProvingKey::Trace::populate_public_inputs_block(builder_copy); + + for (size_t idx = 0; const auto& block : builder_copy.blocks.get()) { + bool overfilled = block.size() >= block.get_fixed_size(); + if (overfilled) { + info("block overfilled at index ", idx); + } + ASSERT(!overfilled); + info(block.size(), " / ", block.get_fixed_size()); + idx++; } - info(block.size(), " / ", block.get_fixed_size()); - idx++; } - for (auto _ : state) { auto proving_key = std::make_shared(builder, structure); benchmark::DoNotOptimize(proving_key); } } -static void construct_dpk(State& state, void (*test_circuit_function)(State&, TraceStructure)) noexcept +void fill_trace_client_ivc_bench(State& state) +{ + fill_trace(state, TraceStructure::CLIENT_IVC_BENCH); +} + +void fill_trace_e2e_full_test(State& state) +{ + fill_trace(state, TraceStructure::E2E_FULL_TEST); +} + +static void construct_pk(State& state, void (*test_circuit_function)(State&)) noexcept { - test_circuit_function(state, TraceStructure::E2E_FULL_TEST); + test_circuit_function(state); } -BENCHMARK_CAPTURE(construct_dpk, TraceStructure::CLIENT_IVC_BENCH, &to_run)->Unit(kMillisecond)->Iterations(1); +BENCHMARK_CAPTURE(construct_pk, TraceStructure::E2E_FULL_TEST, &fill_trace_e2e_full_test) + ->Unit(kMillisecond) + ->Iterations(1); + +BENCHMARK_CAPTURE(construct_pk, TraceStructure::CLIENT_IVC_BENCH, &fill_trace_client_ivc_bench) + ->Unit(kMillisecond) + ->Iterations(1); BENCHMARK_MAIN(); diff --git a/barretenberg/cpp/src/barretenberg/execution_trace/execution_trace.cpp b/barretenberg/cpp/src/barretenberg/execution_trace/execution_trace.cpp index 38b20b9891b..c47fe502a82 100644 --- a/barretenberg/cpp/src/barretenberg/execution_trace/execution_trace.cpp +++ b/barretenberg/cpp/src/barretenberg/execution_trace/execution_trace.cpp @@ -9,6 +9,7 @@ namespace bb { template void ExecutionTrace_::populate_public_inputs_block(Builder& builder) { PROFILE_THIS_NAME("populate_public_inputs_block"); + info("populating public inputs block"); // Update the public inputs block for (const auto& idx : builder.public_inputs) { diff --git a/barretenberg/cpp/src/barretenberg/ultra_honk/decider_proving_key.hpp b/barretenberg/cpp/src/barretenberg/ultra_honk/decider_proving_key.hpp index 1de641beec9..f9e3c2501ac 100644 --- a/barretenberg/cpp/src/barretenberg/ultra_honk/decider_proving_key.hpp +++ b/barretenberg/cpp/src/barretenberg/ultra_honk/decider_proving_key.hpp @@ -30,12 +30,12 @@ template class DeciderProvingKey_ { using Polynomial = typename Flavor::Polynomial; using RelationSeparator = typename Flavor::RelationSeparator; - using Trace = ExecutionTrace_; - // Flag indicating whether the polynomials will be constructed with fixed block sizes for each gate type bool is_structured; public: + using Trace = ExecutionTrace_; + ProvingKey proving_key; bool is_accumulator = false; From f023c3ffa649a56a8044aa4b0518c5ecc4fa2214 Mon Sep 17 00:00:00 2001 From: Cody Date: Thu, 7 Nov 2024 23:11:11 +0000 Subject: [PATCH 04/24] Trying overloads but getting segfaults --- barretenberg/barretenberg.code-workspace | 6 +-- barretenberg/cpp/src/CMakeLists.txt | 1 + .../mega_memory_bench/CMakeLists.txt | 1 + .../mega_memory_bench/mega_memory.bench.cpp | 36 ++++++++++++- .../benchmark_memory_manager/CMakeLists.txt | 15 ++++++ .../custom_allocator.cpp | 43 +++++++++++++++ .../custom_allocator.hpp | 52 +++++++++++++++++++ .../benchmark_memory_manager/empty.bench.cpp | 16 ++++++ .../barretenberg/common/slab_allocator.hpp | 2 +- proving-systems.code-workspace | 6 +-- 10 files changed, 169 insertions(+), 9 deletions(-) create mode 100644 barretenberg/cpp/src/barretenberg/benchmark_memory_manager/CMakeLists.txt create mode 100644 barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.cpp create mode 100644 barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.hpp create mode 100644 barretenberg/cpp/src/barretenberg/benchmark_memory_manager/empty.bench.cpp diff --git a/barretenberg/barretenberg.code-workspace b/barretenberg/barretenberg.code-workspace index cd08c7a3166..9af9ca53402 100644 --- a/barretenberg/barretenberg.code-workspace +++ b/barretenberg/barretenberg.code-workspace @@ -1,5 +1,5 @@ { - // Each "folder" can define a different project in the main repo, + // Each "folder" can define a different project in the main repo, // relative to `.code-workspace`. "folders": [ { @@ -101,7 +101,7 @@ // which ensures SRS can be read "testMate.cpp.test.workingDirectory": "${command:cmake.buildDirectory}", // Filter all binaries that are not tests or benchmarks - "testMate.cpp.test.executables": "${command:cmake.buildDirectory}/bin/*{test,Test,TEST,bench}*", + "testMate.cpp.test.executables": "${command:cmake.buildDirectory}/bin/*{test,bench}*", // // Other // @@ -145,6 +145,6 @@ "cwd": "${command:cmake.buildDirectory}", "internalConsoleOptions": "openOnSessionStart", "console": "internalConsole", - } + } }, } \ No newline at end of file diff --git a/barretenberg/cpp/src/CMakeLists.txt b/barretenberg/cpp/src/CMakeLists.txt index 85fbaf19f38..c80dfc94849 100644 --- a/barretenberg/cpp/src/CMakeLists.txt +++ b/barretenberg/cpp/src/CMakeLists.txt @@ -78,6 +78,7 @@ add_subdirectory(barretenberg/examples) add_subdirectory(barretenberg/flavor) add_subdirectory(barretenberg/goblin) add_subdirectory(barretenberg/grumpkin_srs_gen) +add_subdirectory(barretenberg/benchmark_memory_manager) add_subdirectory(barretenberg/numeric) add_subdirectory(barretenberg/plonk) add_subdirectory(barretenberg/plonk_honk_shared) diff --git a/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/CMakeLists.txt b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/CMakeLists.txt index f35647268d7..deca89aaec4 100644 --- a/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/CMakeLists.txt +++ b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/CMakeLists.txt @@ -2,4 +2,5 @@ barretenberg_module( mega_memory_bench ultra_honk stdlib_primitives + benchmark_memory_manager_objects ) \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp index 4ebc0afc575..95dee9e577d 100644 --- a/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp +++ b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp @@ -1,3 +1,6 @@ +#pragma GCC diagnostic ignored "-Wunused-variable" + +#include "barretenberg/benchmark_memory_manager/custom_allocator.hpp" #include "barretenberg/stdlib/primitives/field/field.hpp" #include "barretenberg/stdlib/primitives/plookup/plookup.hpp" #include "barretenberg/stdlib_circuit_builders/plookup_tables/fixed_base/fixed_base.hpp" @@ -5,7 +8,6 @@ #include "barretenberg/ultra_honk/decider_proving_key.hpp" #include -#pragma GCC diagnostic ignored "-Wunused-variable" using namespace benchmark; using namespace bb; @@ -25,6 +27,7 @@ static constexpr size_t NUM_SHORT = 10; void fill_ecc_op_block(Builder& builder) { + info("calling fill_ecc_op_block"); const auto point = g1::affine_element::random_element(); const auto scalar = fr::random_element(); const size_t num_to_add((builder.blocks.ecc_op.get_fixed_size() - NUM_SHORT) >> 1); // each accum call adds two rows @@ -35,6 +38,7 @@ void fill_ecc_op_block(Builder& builder) void fill_pub_inputs_block(Builder& builder) { + info("calling fill_pub_inputs_block"); for (size_t idx = 0; idx < builder.blocks.pub_inputs.get_fixed_size() - NUM_SHORT; idx++) { builder.add_public_variable(fr::random_element()); } @@ -42,6 +46,7 @@ void fill_pub_inputs_block(Builder& builder) void fill_databus_blocks(Builder& builder) { + info("calling fill_databus_blocks"); static constexpr size_t NUM_BUS_IDS(3); const size_t num_gates_per_bus_id((builder.blocks.busread.get_fixed_size() - NUM_SHORT) / NUM_BUS_IDS); for (size_t idx = 1; idx < num_gates_per_bus_id + 1; idx++) { // start at 1 to avoid / by zero below @@ -62,6 +67,7 @@ void fill_databus_blocks(Builder& builder) void fill_delta_range_block(Builder& builder) { + info("calling fill_delta_range_block"); // At the moment the trace has space for 90k delta range gates but I don't think it's possible to use them all // because there is not enough capacity in the arithmetic block! @@ -119,6 +125,7 @@ void fill_delta_range_block(Builder& builder) void fill_arithmetic_block(Builder& builder) { + info("calling fill_arithmetic_block"); const uint32_t idx_1 = builder.add_variable(fr::random_element()); const uint32_t idx_2 = builder.add_variable(fr::random_element()); const uint32_t idx_3 = builder.add_variable(fr::random_element()); @@ -130,6 +137,7 @@ void fill_arithmetic_block(Builder& builder) void fill_elliptic_block(Builder& builder) { + info("calling fill_elliptic_block"); const uint32_t x1_idx = builder.add_variable(fr::random_element()); const uint32_t y1_idx = builder.add_variable(fr::random_element()); const uint32_t x2_idx = builder.add_variable(fr::random_element()); @@ -143,6 +151,7 @@ void fill_elliptic_block(Builder& builder) void fill_aux_block(Builder& builder) { + info("calling fill_aux_block"); // static constexpr size_t NUM_AUX_TYPES = 11; auto& block = builder.blocks.aux; @@ -180,6 +189,7 @@ void fill_aux_block(Builder& builder) void fill_poseidon2_internal_block(Builder& builder) { + info("calling fill_poseidon2_internal_block"); auto& block = builder.blocks.poseidon2_internal; const uint32_t idx_1 = builder.add_variable(fr::random_element()); const uint32_t idx_2 = builder.add_variable(fr::random_element()); @@ -193,6 +203,7 @@ void fill_poseidon2_internal_block(Builder& builder) void fill_poseidon2_external_block(Builder& builder) { + info("calling fill_poseidon2_external_block"); auto& block = builder.blocks.poseidon2_external; const uint32_t idx_1 = builder.add_variable(fr::random_element()); const uint32_t idx_2 = builder.add_variable(fr::random_element()); @@ -206,6 +217,7 @@ void fill_poseidon2_external_block(Builder& builder) void fill_lookup_block(Builder& builder) { + info("calling fill_lookup_block"); auto& block = builder.blocks.lookup; // static constexpr size_t NUM_LOOKUP_TYPES_USED(15); @@ -283,6 +295,7 @@ void fill_lookup_block(Builder& builder) void fill_trace(State& state, TraceStructure structure) { + info("calling fill_trace"); Builder builder; builder.blocks.set_fixed_block_sizes(structure); @@ -297,6 +310,7 @@ void fill_trace(State& state, TraceStructure structure) fill_poseidon2_internal_block(builder); fill_lookup_block(builder); builder.finalize_circuit(/* ensure_nonzero */ false); + { // finalize doesn't populate public inputs block, so copy to verify that the block is being filled well // otherwise the pk construction will overflow the block @@ -314,6 +328,7 @@ void fill_trace(State& state, TraceStructure structure) idx++; } } + for (auto _ : state) { auto proving_key = std::make_shared(builder, structure); benchmark::DoNotOptimize(proving_key); @@ -322,17 +337,25 @@ void fill_trace(State& state, TraceStructure structure) void fill_trace_client_ivc_bench(State& state) { + info("calling fill_trace_client_ivc_bench"); fill_trace(state, TraceStructure::CLIENT_IVC_BENCH); } void fill_trace_e2e_full_test(State& state) { + info("calling fill_trace_e2e_full_test"); fill_trace(state, TraceStructure::E2E_FULL_TEST); } +BenchmarkMemoryManager memory_manager; + static void construct_pk(State& state, void (*test_circuit_function)(State&)) noexcept { + // BenchmarkMemoryManager memory_manager; + // benchmark::RegisterMemoryManager(&memory_manager); test_circuit_function(state); + // ::g_allocator.printStatistics(); + // benchmark::RegisterMemoryManager(nullptr); } BENCHMARK_CAPTURE(construct_pk, TraceStructure::E2E_FULL_TEST, &fill_trace_e2e_full_test) @@ -343,4 +366,13 @@ BENCHMARK_CAPTURE(construct_pk, TraceStructure::CLIENT_IVC_BENCH, &fill_trace_cl ->Unit(kMillisecond) ->Iterations(1); -BENCHMARK_MAIN(); +int main(int argc, char** argv) +{ + ::benchmark::RegisterMemoryManager(&memory_manager); + info("registered memory manager"); + ::benchmark::Initialize(&argc, argv); + info("initialized"); + ::benchmark::RunSpecifiedBenchmarks(); + info("ran specified benchmarks"); + ::benchmark::RegisterMemoryManager(nullptr); +} \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/CMakeLists.txt b/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/CMakeLists.txt new file mode 100644 index 00000000000..7e680a52fd5 --- /dev/null +++ b/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/CMakeLists.txt @@ -0,0 +1,15 @@ +# barretenberg_module( +# benchmark_memory_manager +# common +# ) +add_library( + benchmark_memory_manager_objects + custom_allocator.cpp + +) + +target_link_libraries( + benchmark_memory_manager_objects + PRIVATE + benchmark::benchmark +) diff --git a/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.cpp b/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.cpp new file mode 100644 index 00000000000..9db61459498 --- /dev/null +++ b/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.cpp @@ -0,0 +1,43 @@ +#include "custom_allocator.hpp" +#include +#include + +TrackingAllocator g_allocator; + +void* TrackingAllocator::allocate(size_t size) +{ + total_allocations++; + total_memory += size; + + void* ptr = std::malloc(size); + if (!ptr) { + throw std::bad_alloc(); + } + allocations.emplace_back(ptr, size); // Add the pointer and its size to the vector + return ptr; +} + +void TrackingAllocator::deallocate(void* ptr) +{ + for (auto it = allocations.begin(); it != allocations.end(); ++it) { + if (it->first == ptr) { + total_deallocated++; + total_memory -= it->second; + allocations.erase(it); // Remove the allocation from the vector + std::free(ptr); + return; + } + } +} + +void* operator new(size_t size) +{ + // std::cout << "calling overloaded new\n"; + return g_allocator.allocate(size); +} + +void operator delete(void* ptr) noexcept +{ + std::cout << "calling overloaded delete\n"; + g_allocator.deallocate(ptr); +} diff --git a/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.hpp b/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.hpp new file mode 100644 index 00000000000..533b312fb54 --- /dev/null +++ b/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.hpp @@ -0,0 +1,52 @@ +#pragma once +#include +#include +#include +#include + +class TrackingAllocator { + public: + TrackingAllocator() + : total_allocations(0) + , total_deallocated(0) + , total_memory(0) + {} + + void* allocate(std::size_t size); + void deallocate(void* ptr); + int64_t current_memory_usage() const { return total_memory; } + void reset() + { + total_allocations = total_deallocated = total_memory = 0; + allocations.clear(); + } + + // Print allocation statistics + void printStatistics() const + { + std::cout << "Total Allocations: " << total_allocations << "\n"; + std::cout << "Total Deallocations: " << total_deallocated << "\n"; + std::cout << "Current Memory Usage: " << total_memory << " bytes\n"; + } + + int64_t total_allocations; + int64_t total_deallocated; + int64_t total_memory; + std::vector> allocations; +}; + +// Global tracking allocator instance +extern TrackingAllocator g_allocator; + +void* operator new(std::size_t size); +void operator delete(void* ptr) noexcept; + +class BenchmarkMemoryManager : public benchmark::MemoryManager { + public: + void Start() override { g_allocator.reset(); } + void Stop(Result& result) override + { + result.num_allocs = g_allocator.total_allocations; + result.max_bytes_used = g_allocator.current_memory_usage(); + } +}; diff --git a/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/empty.bench.cpp b/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/empty.bench.cpp new file mode 100644 index 00000000000..4e3e4e2bf91 --- /dev/null +++ b/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/empty.bench.cpp @@ -0,0 +1,16 @@ +#include +#include + +using namespace benchmark; + +void eg(State& state) +{ + for (auto _ : state) { + std::vector v(1 << 22); + DoNotOptimize(v); + } +} + +BENCHMARK(eg)->Unit(kMicrosecond); + +BENCHMARK_MAIN(); \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/common/slab_allocator.hpp b/barretenberg/cpp/src/barretenberg/common/slab_allocator.hpp index fbdd310756f..a557b2c3c0f 100644 --- a/barretenberg/cpp/src/barretenberg/common/slab_allocator.hpp +++ b/barretenberg/cpp/src/barretenberg/common/slab_allocator.hpp @@ -79,6 +79,6 @@ template class ContainerSlabAllocator { /** * @brief A vector that uses the slab allocator. */ -template using SlabVector = std::vector>; +template using SlabVector = std::vector */>; } // namespace bb \ No newline at end of file diff --git a/proving-systems.code-workspace b/proving-systems.code-workspace index be204f2c9b4..bbe8afa4cf2 100644 --- a/proving-systems.code-workspace +++ b/proving-systems.code-workspace @@ -1,5 +1,5 @@ { - // Each "folder" can define a different project in the main repo, + // Each "folder" can define a different project in the main repo, // relative to `.code-workspace`. "folders": [ { @@ -101,7 +101,7 @@ // which ensures SRS can be read "testMate.cpp.test.workingDirectory": "${command:cmake.buildDirectory}", // Filter all binaries that are not tests or benchmarks - "testMate.cpp.test.executables": "${command:cmake.buildDirectory}/bin/*{test,Test,TEST,bench}*", + "testMate.cpp.test.executables": "${command:cmake.buildDirectory}/bin/*", // // Other // @@ -145,6 +145,6 @@ "cwd": "${command:cmake.buildDirectory}", "internalConsoleOptions": "openOnSessionStart", "console": "internalConsole", - } + } }, } \ No newline at end of file From a4eb9b973e299da62b21316de35e46693d40b7e8 Mon Sep 17 00:00:00 2001 From: Cody Date: Fri, 8 Nov 2024 03:43:16 +0000 Subject: [PATCH 05/24] Results: ``` -------------------------------------------------------------------------------------------------------- Benchmark Time CPU Iterations UserCounters... -------------------------------------------------------------------------------------------------------- construct_pk/TraceStructure::E2E_FULL_TEST 1160 ms 1129 ms 1 pk mem=911.716M construct_pk/TraceStructure::CLIENT_IVC_BENCH 365 ms 349 ms 2 pk mem=471.561M ``` --- .../mega_memory_bench/CMakeLists.txt | 2 +- .../mega_memory_bench/mega_memory.bench.cpp | 57 +++++++------------ .../execution_trace/execution_trace.cpp | 1 - .../stdlib_circuit_builders/mega_flavor.hpp | 11 ++++ 4 files changed, 34 insertions(+), 37 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/CMakeLists.txt b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/CMakeLists.txt index deca89aaec4..28d4d3c123c 100644 --- a/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/CMakeLists.txt +++ b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/CMakeLists.txt @@ -2,5 +2,5 @@ barretenberg_module( mega_memory_bench ultra_honk stdlib_primitives - benchmark_memory_manager_objects + # benchmark_memory_manager_objects ) \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp index 95dee9e577d..58bdcbc018c 100644 --- a/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp +++ b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp @@ -1,6 +1,6 @@ #pragma GCC diagnostic ignored "-Wunused-variable" -#include "barretenberg/benchmark_memory_manager/custom_allocator.hpp" +// #include "barretenberg/benchmark_memory_manager/custom_allocator.hpp" #include "barretenberg/stdlib/primitives/field/field.hpp" #include "barretenberg/stdlib/primitives/plookup/plookup.hpp" #include "barretenberg/stdlib_circuit_builders/plookup_tables/fixed_base/fixed_base.hpp" @@ -27,7 +27,6 @@ static constexpr size_t NUM_SHORT = 10; void fill_ecc_op_block(Builder& builder) { - info("calling fill_ecc_op_block"); const auto point = g1::affine_element::random_element(); const auto scalar = fr::random_element(); const size_t num_to_add((builder.blocks.ecc_op.get_fixed_size() - NUM_SHORT) >> 1); // each accum call adds two rows @@ -38,7 +37,6 @@ void fill_ecc_op_block(Builder& builder) void fill_pub_inputs_block(Builder& builder) { - info("calling fill_pub_inputs_block"); for (size_t idx = 0; idx < builder.blocks.pub_inputs.get_fixed_size() - NUM_SHORT; idx++) { builder.add_public_variable(fr::random_element()); } @@ -46,7 +44,6 @@ void fill_pub_inputs_block(Builder& builder) void fill_databus_blocks(Builder& builder) { - info("calling fill_databus_blocks"); static constexpr size_t NUM_BUS_IDS(3); const size_t num_gates_per_bus_id((builder.blocks.busread.get_fixed_size() - NUM_SHORT) / NUM_BUS_IDS); for (size_t idx = 1; idx < num_gates_per_bus_id + 1; idx++) { // start at 1 to avoid / by zero below @@ -67,7 +64,6 @@ void fill_databus_blocks(Builder& builder) void fill_delta_range_block(Builder& builder) { - info("calling fill_delta_range_block"); // At the moment the trace has space for 90k delta range gates but I don't think it's possible to use them all // because there is not enough capacity in the arithmetic block! @@ -125,7 +121,6 @@ void fill_delta_range_block(Builder& builder) void fill_arithmetic_block(Builder& builder) { - info("calling fill_arithmetic_block"); const uint32_t idx_1 = builder.add_variable(fr::random_element()); const uint32_t idx_2 = builder.add_variable(fr::random_element()); const uint32_t idx_3 = builder.add_variable(fr::random_element()); @@ -137,7 +132,6 @@ void fill_arithmetic_block(Builder& builder) void fill_elliptic_block(Builder& builder) { - info("calling fill_elliptic_block"); const uint32_t x1_idx = builder.add_variable(fr::random_element()); const uint32_t y1_idx = builder.add_variable(fr::random_element()); const uint32_t x2_idx = builder.add_variable(fr::random_element()); @@ -151,7 +145,6 @@ void fill_elliptic_block(Builder& builder) void fill_aux_block(Builder& builder) { - info("calling fill_aux_block"); // static constexpr size_t NUM_AUX_TYPES = 11; auto& block = builder.blocks.aux; @@ -189,7 +182,6 @@ void fill_aux_block(Builder& builder) void fill_poseidon2_internal_block(Builder& builder) { - info("calling fill_poseidon2_internal_block"); auto& block = builder.blocks.poseidon2_internal; const uint32_t idx_1 = builder.add_variable(fr::random_element()); const uint32_t idx_2 = builder.add_variable(fr::random_element()); @@ -203,7 +195,6 @@ void fill_poseidon2_internal_block(Builder& builder) void fill_poseidon2_external_block(Builder& builder) { - info("calling fill_poseidon2_external_block"); auto& block = builder.blocks.poseidon2_external; const uint32_t idx_1 = builder.add_variable(fr::random_element()); const uint32_t idx_2 = builder.add_variable(fr::random_element()); @@ -217,7 +208,6 @@ void fill_poseidon2_external_block(Builder& builder) void fill_lookup_block(Builder& builder) { - info("calling fill_lookup_block"); auto& block = builder.blocks.lookup; // static constexpr size_t NUM_LOOKUP_TYPES_USED(15); @@ -295,7 +285,6 @@ void fill_lookup_block(Builder& builder) void fill_trace(State& state, TraceStructure structure) { - info("calling fill_trace"); Builder builder; builder.blocks.set_fixed_block_sizes(structure); @@ -309,7 +298,7 @@ void fill_trace(State& state, TraceStructure structure) fill_poseidon2_external_block(builder); fill_poseidon2_internal_block(builder); fill_lookup_block(builder); - builder.finalize_circuit(/* ensure_nonzero */ false); + // builder.finalize_circuit(/* ensure_nonzero */ false); { // finalize doesn't populate public inputs block, so copy to verify that the block is being filled well @@ -321,33 +310,33 @@ void fill_trace(State& state, TraceStructure structure) for (size_t idx = 0; const auto& block : builder_copy.blocks.get()) { bool overfilled = block.size() >= block.get_fixed_size(); if (overfilled) { - info("block overfilled at index ", idx); + vinfo("block overfilled at index ", idx); } ASSERT(!overfilled); - info(block.size(), " / ", block.get_fixed_size()); + vinfo(block.size(), " / ", block.get_fixed_size()); idx++; } } for (auto _ : state) { - auto proving_key = std::make_shared(builder, structure); + DeciderProvingKey proving_key(builder, structure); + uint64_t memory_estimate = proving_key.proving_key.polynomials.estimate_memory(); + state.counters["pk mem"] = static_cast(memory_estimate); benchmark::DoNotOptimize(proving_key); } } void fill_trace_client_ivc_bench(State& state) { - info("calling fill_trace_client_ivc_bench"); fill_trace(state, TraceStructure::CLIENT_IVC_BENCH); } void fill_trace_e2e_full_test(State& state) { - info("calling fill_trace_e2e_full_test"); fill_trace(state, TraceStructure::E2E_FULL_TEST); } -BenchmarkMemoryManager memory_manager; +// BenchmarkMemoryManager memory_manager; static void construct_pk(State& state, void (*test_circuit_function)(State&)) noexcept { @@ -358,21 +347,19 @@ static void construct_pk(State& state, void (*test_circuit_function)(State&)) no // benchmark::RegisterMemoryManager(nullptr); } -BENCHMARK_CAPTURE(construct_pk, TraceStructure::E2E_FULL_TEST, &fill_trace_e2e_full_test) - ->Unit(kMillisecond) - ->Iterations(1); +BENCHMARK_CAPTURE(construct_pk, TraceStructure::E2E_FULL_TEST, &fill_trace_e2e_full_test)->Unit(kMillisecond); -BENCHMARK_CAPTURE(construct_pk, TraceStructure::CLIENT_IVC_BENCH, &fill_trace_client_ivc_bench) - ->Unit(kMillisecond) - ->Iterations(1); +BENCHMARK_CAPTURE(construct_pk, TraceStructure::CLIENT_IVC_BENCH, &fill_trace_client_ivc_bench)->Unit(kMillisecond); -int main(int argc, char** argv) -{ - ::benchmark::RegisterMemoryManager(&memory_manager); - info("registered memory manager"); - ::benchmark::Initialize(&argc, argv); - info("initialized"); - ::benchmark::RunSpecifiedBenchmarks(); - info("ran specified benchmarks"); - ::benchmark::RegisterMemoryManager(nullptr); -} \ No newline at end of file +BENCHMARK_MAIN(); + +// int main(int argc, char** argv) +// { +// ::benchmark::RegisterMemoryManager(&memory_manager); +// info("registered memory manager"); +// ::benchmark::Initialize(&argc, argv); +// info("initialized"); +// ::benchmark::RunSpecifiedBenchmarks(); +// info("ran specified benchmarks"); +// ::benchmark::RegisterMemoryManager(nullptr); +// } \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/execution_trace/execution_trace.cpp b/barretenberg/cpp/src/barretenberg/execution_trace/execution_trace.cpp index c47fe502a82..38b20b9891b 100644 --- a/barretenberg/cpp/src/barretenberg/execution_trace/execution_trace.cpp +++ b/barretenberg/cpp/src/barretenberg/execution_trace/execution_trace.cpp @@ -9,7 +9,6 @@ namespace bb { template void ExecutionTrace_::populate_public_inputs_block(Builder& builder) { PROFILE_THIS_NAME("populate_public_inputs_block"); - info("populating public inputs block"); // Update the public inputs block for (const auto& idx : builder.public_inputs) { diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_flavor.hpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_flavor.hpp index 13f6e3cbe85..f07b326c77a 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_flavor.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_flavor.hpp @@ -412,6 +412,17 @@ class MegaFlavor { shifted = to_be_shifted.shifted(); } } + + uint64_t estimate_memory() + { + uint64_t result(0); + for (auto [polynomial, label] : zip_view(get_all(), get_labels())) { + uint64_t size = polynomial.size(); + vinfo("label: ", label, " num elts: ", size); + result += size; + } + return result * sizeof(FF); + } }; /** From 83af88f22114417cfa854d81fe9addce27e834cb Mon Sep 17 00:00:00 2001 From: Cody Date: Fri, 8 Nov 2024 04:39:05 +0000 Subject: [PATCH 06/24] Better info --- .../mega_memory_bench/mega_memory.bench.cpp | 13 ++++++------- .../arithmetization/mega_arithmetization.hpp | 9 +++++++++ .../stdlib_circuit_builders/mega_flavor.hpp | 8 ++++++-- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp index 58bdcbc018c..14f4c3dcc9d 100644 --- a/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp +++ b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp @@ -307,14 +307,13 @@ void fill_trace(State& state, TraceStructure structure) auto builder_copy = builder; DeciderProvingKey::Trace::populate_public_inputs_block(builder_copy); - for (size_t idx = 0; const auto& block : builder_copy.blocks.get()) { + for (const auto [label, block] : zip_view(builder_copy.blocks.get_labels(), builder_copy.blocks.get())) { bool overfilled = block.size() >= block.get_fixed_size(); if (overfilled) { - vinfo("block overfilled at index ", idx); + vinfo(label, " overfilled"); } ASSERT(!overfilled); - vinfo(block.size(), " / ", block.get_fixed_size()); - idx++; + vinfo(label, ": ", block.size(), " / ", block.get_fixed_size()); } } @@ -338,7 +337,7 @@ void fill_trace_e2e_full_test(State& state) // BenchmarkMemoryManager memory_manager; -static void construct_pk(State& state, void (*test_circuit_function)(State&)) noexcept +static void pk_mem(State& state, void (*test_circuit_function)(State&)) noexcept { // BenchmarkMemoryManager memory_manager; // benchmark::RegisterMemoryManager(&memory_manager); @@ -347,9 +346,9 @@ static void construct_pk(State& state, void (*test_circuit_function)(State&)) no // benchmark::RegisterMemoryManager(nullptr); } -BENCHMARK_CAPTURE(construct_pk, TraceStructure::E2E_FULL_TEST, &fill_trace_e2e_full_test)->Unit(kMillisecond); +BENCHMARK_CAPTURE(pk_mem, E2E_FULL_TEST, &fill_trace_e2e_full_test)->Unit(kMillisecond)->Iterations(1); -BENCHMARK_CAPTURE(construct_pk, TraceStructure::CLIENT_IVC_BENCH, &fill_trace_client_ivc_bench)->Unit(kMillisecond); +BENCHMARK_CAPTURE(pk_mem, CLIENT_IVC_BENCH, &fill_trace_client_ivc_bench)->Unit(kMillisecond)->Iterations(1); BENCHMARK_MAIN(); diff --git a/barretenberg/cpp/src/barretenberg/plonk_honk_shared/arithmetization/mega_arithmetization.hpp b/barretenberg/cpp/src/barretenberg/plonk_honk_shared/arithmetization/mega_arithmetization.hpp index 8a19c73cc43..caac53b4579 100644 --- a/barretenberg/cpp/src/barretenberg/plonk_honk_shared/arithmetization/mega_arithmetization.hpp +++ b/barretenberg/cpp/src/barretenberg/plonk_honk_shared/arithmetization/mega_arithmetization.hpp @@ -38,6 +38,14 @@ template class MegaArith { T lookup; T overflow; // block gates of arbitrary type that overflow their designated block + std::vector get_labels() + { + return { "ecc_op", "pub_inputs", "busread", + "arithmetic", "delta_range", "elliptic", + "aux", "poseidon2_external", "poseidon2_internal", + "lookup" }; + } + auto get() { return RefArray{ ecc_op, @@ -52,6 +60,7 @@ template class MegaArith { lookup, overflow }; } + auto get() const { return RefArray{ ecc_op, diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_flavor.hpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_flavor.hpp index f07b326c77a..455bfb01ee0 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_flavor.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_flavor.hpp @@ -415,10 +415,14 @@ class MegaFlavor { uint64_t estimate_memory() { - uint64_t result(0); for (auto [polynomial, label] : zip_view(get_all(), get_labels())) { uint64_t size = polynomial.size(); - vinfo("label: ", label, " num elts: ", size); + vinfo(label, " num: ", size, " size: ", (size * sizeof(FF)) >> 10, " KiB"); + } + + uint64_t result(0); + for (auto& polynomial : get_unshifted()) { + uint64_t size = polynomial.size(); result += size; } return result * sizeof(FF); From 44b5a5697a366a72a3d0de1ee6052047837771f1 Mon Sep 17 00:00:00 2001 From: Cody Date: Fri, 8 Nov 2024 16:47:04 +0000 Subject: [PATCH 07/24] Estimate builder memory (iou variables) --- .../mega_memory_bench/mega_memory.bench.cpp | 4 ++- .../arithmetization/mega_arithmetization.hpp | 2 +- .../mega_circuit_builder.hpp | 26 +++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp index 14f4c3dcc9d..ff8dc885a09 100644 --- a/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp +++ b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp @@ -317,10 +317,12 @@ void fill_trace(State& state, TraceStructure structure) } } + uint64_t builder_estimate = builder.estimate_memory(); for (auto _ : state) { DeciderProvingKey proving_key(builder, structure); uint64_t memory_estimate = proving_key.proving_key.polynomials.estimate_memory(); - state.counters["pk mem"] = static_cast(memory_estimate); + state.counters["poly_mem_est"] = static_cast(memory_estimate); + state.counters["builder_mem_est"] = static_cast(builder_estimate); benchmark::DoNotOptimize(proving_key); } } diff --git a/barretenberg/cpp/src/barretenberg/plonk_honk_shared/arithmetization/mega_arithmetization.hpp b/barretenberg/cpp/src/barretenberg/plonk_honk_shared/arithmetization/mega_arithmetization.hpp index caac53b4579..1643fa35000 100644 --- a/barretenberg/cpp/src/barretenberg/plonk_honk_shared/arithmetization/mega_arithmetization.hpp +++ b/barretenberg/cpp/src/barretenberg/plonk_honk_shared/arithmetization/mega_arithmetization.hpp @@ -38,7 +38,7 @@ template class MegaArith { T lookup; T overflow; // block gates of arbitrary type that overflow their designated block - std::vector get_labels() + std::vector get_labels() const { return { "ecc_op", "pub_inputs", "busread", "arithmetic", "delta_range", "elliptic", diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_circuit_builder.hpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_circuit_builder.hpp index 155eadea729..f2464248b4f 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_circuit_builder.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_circuit_builder.hpp @@ -236,6 +236,32 @@ template class MegaCircuitBuilder_ : public UltraCircuitBuilder_(BusId::CALLDATA)]; } const BusVector& get_secondary_calldata() const { return databus[static_cast(BusId::SECONDARY_CALLDATA)]; } const BusVector& get_return_data() const { return databus[static_cast(BusId::RETURNDATA)]; } + uint64_t estimate_memory() const + { + vinfo("ESTIMATING BUILDER MEMORY"); + uint64_t result{ 0 }; + + // gates: + for (auto [block, label] : zip_view(this->blocks.get(), this->blocks.get_labels())) { + uint64_t size{ 0 }; + for (const auto& wire : block.wires) { + size += wire.capacity() * sizeof(uint32_t); + } + for (const auto& selector : block.selectors) { + size += selector.capacity() * sizeof(FF); + } + vinfo(label, " size ", size >> 10, " KiB"); + result += size; + } + + // databus + for (const auto& bus_vector : databus) { + const uint64_t size((bus_vector.read_counts.capacity() + bus_vector.data.capacity()) * sizeof(uint32_t)); + vinfo("size: ", size >> 10, " KiB"); + result += size; + } + return result; + } }; using MegaCircuitBuilder = MegaCircuitBuilder_; } // namespace bb From 629203b6e3acf66e0d45d207bf3eb4d376a15a1a Mon Sep 17 00:00:00 2001 From: Cody Date: Fri, 8 Nov 2024 20:06:08 +0000 Subject: [PATCH 08/24] Stop building st wasm in yarn build --- barretenberg/ts/scripts/build_wasm.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/barretenberg/ts/scripts/build_wasm.sh b/barretenberg/ts/scripts/build_wasm.sh index b12348a16e8..f55f1e744ab 100755 --- a/barretenberg/ts/scripts/build_wasm.sh +++ b/barretenberg/ts/scripts/build_wasm.sh @@ -5,7 +5,6 @@ if [ -z "$SKIP_CPP_BUILD" ]; then # Build the wasms and strip debug symbols. cd ../cpp cmake --preset wasm-threads -DCMAKE_MESSAGE_LOG_LEVEL=Warning && cmake --build --preset wasm-threads - cmake --preset wasm -DCMAKE_MESSAGE_LOG_LEVEL=Warning && cmake --build --preset wasm ./scripts/strip-wasm.sh cd ../ts fi From 03a3db4f1b9d6c18dd1f9e11e5cc3647a002e719 Mon Sep 17 00:00:00 2001 From: Cody Date: Fri, 8 Nov 2024 20:07:17 +0000 Subject: [PATCH 09/24] Fix build by ignoring databus --- .../stdlib_circuit_builders/mega_circuit_builder.hpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_circuit_builder.hpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_circuit_builder.hpp index f2464248b4f..9acf85d2b59 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_circuit_builder.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_circuit_builder.hpp @@ -254,12 +254,12 @@ template class MegaCircuitBuilder_ : public UltraCircuitBuilder_> 10, " KiB"); - result += size; - } + // // databus + // for (const auto& bus_vector : databus) { + // const uint64_t size((bus_vector.read_counts.capacity() + bus_vector.data.capacity()) * sizeof(uint32_t)); + // vinfo("size: ", size >> 10, " KiB"); + // result += size; + // } return result; } }; From 8a99eaca30ee215aa30ff6f115d6eb51b65b0614 Mon Sep 17 00:00:00 2001 From: Cody Date: Sun, 10 Nov 2024 01:54:48 +0000 Subject: [PATCH 10/24] Revert change in circuit hash (?!) --- barretenberg/cpp/src/barretenberg/common/slab_allocator.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/barretenberg/cpp/src/barretenberg/common/slab_allocator.hpp b/barretenberg/cpp/src/barretenberg/common/slab_allocator.hpp index a557b2c3c0f..fbdd310756f 100644 --- a/barretenberg/cpp/src/barretenberg/common/slab_allocator.hpp +++ b/barretenberg/cpp/src/barretenberg/common/slab_allocator.hpp @@ -79,6 +79,6 @@ template class ContainerSlabAllocator { /** * @brief A vector that uses the slab allocator. */ -template using SlabVector = std::vector */>; +template using SlabVector = std::vector>; } // namespace bb \ No newline at end of file From 8649ce85b9c428dc1f6c0bbd83e901d51567d326 Mon Sep 17 00:00:00 2001 From: Cody Date: Sun, 10 Nov 2024 02:22:44 +0000 Subject: [PATCH 11/24] Track more of builder --- .../mega_memory_bench/mega_memory.bench.cpp | 1 + .../stdlib_circuit_builders/mega_circuit_builder.hpp | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp index ff8dc885a09..302aae1a371 100644 --- a/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp +++ b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp @@ -317,6 +317,7 @@ void fill_trace(State& state, TraceStructure structure) } } + builder.finalize_circuit(/* ensure_nonzero */ true); uint64_t builder_estimate = builder.estimate_memory(); for (auto _ : state) { DeciderProvingKey proving_key(builder, structure); diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_circuit_builder.hpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_circuit_builder.hpp index 9acf85d2b59..124bef488a4 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_circuit_builder.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_circuit_builder.hpp @@ -254,6 +254,18 @@ template class MegaCircuitBuilder_ : public UltraCircuitBuilder_variables.capacity() * sizeof(FF); + + // public inputs + result += this->public_inputs.capacity() * sizeof(uint32_t); + + // other variable indices + result += this->next_var_index.capacity() * sizeof(uint32_t); + result += this->prev_var_index.capacity() * sizeof(uint32_t); + result += this->real_variable_index.capacity() * sizeof(uint32_t); + result += this->real_variable_tags.capacity() * sizeof(uint32_t); + // // databus // for (const auto& bus_vector : databus) { // const uint64_t size((bus_vector.read_counts.capacity() + bus_vector.data.capacity()) * sizeof(uint32_t)); From 3fa411faee0cbc715ddbe9c8d4e1d63e78715da4 Mon Sep 17 00:00:00 2001 From: Cody Date: Sun, 10 Nov 2024 02:37:20 +0000 Subject: [PATCH 12/24] Track PIs (in use?) --- .../mega_memory_bench/mega_memory.bench.cpp | 2 +- .../stdlib_circuit_builders/mega_flavor.hpp | 32 ++++++++++--------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp index 302aae1a371..689977a9d0a 100644 --- a/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp +++ b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp @@ -321,7 +321,7 @@ void fill_trace(State& state, TraceStructure structure) uint64_t builder_estimate = builder.estimate_memory(); for (auto _ : state) { DeciderProvingKey proving_key(builder, structure); - uint64_t memory_estimate = proving_key.proving_key.polynomials.estimate_memory(); + uint64_t memory_estimate = proving_key.proving_key.estimate_memory(); state.counters["poly_mem_est"] = static_cast(memory_estimate); state.counters["builder_mem_est"] = static_cast(builder_estimate); benchmark::DoNotOptimize(proving_key); diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_flavor.hpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_flavor.hpp index 455bfb01ee0..1b10d48fef8 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_flavor.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_flavor.hpp @@ -412,21 +412,6 @@ class MegaFlavor { shifted = to_be_shifted.shifted(); } } - - uint64_t estimate_memory() - { - for (auto [polynomial, label] : zip_view(get_all(), get_labels())) { - uint64_t size = polynomial.size(); - vinfo(label, " num: ", size, " size: ", (size * sizeof(FF)) >> 10, " KiB"); - } - - uint64_t result(0); - for (auto& polynomial : get_unshifted()) { - uint64_t size = polynomial.size(); - result += size; - } - return result * sizeof(FF); - } }; /** @@ -525,6 +510,23 @@ class MegaFlavor { // Compute permutation and lookup grand product polynomials compute_grand_products(this->polynomials, relation_parameters); } + + uint64_t estimate_memory() + { + for (auto [polynomial, label] : zip_view(polynomials.get_all(), polynomials.get_labels())) { + uint64_t size = polynomial.size(); + vinfo(label, " num: ", size, " size: ", (size * sizeof(FF)) >> 10, " KiB"); + } + + uint64_t result(0); + for (auto& polynomial : polynomials.get_unshifted()) { + result += polynomial.size() * sizeof(FF); + } + + result += public_inputs.capacity() * sizeof(FF); + + return result; + } }; /** From d5af2733511d316210f45e70835bd8c67136de64 Mon Sep 17 00:00:00 2001 From: Cody Date: Sun, 10 Nov 2024 02:39:22 +0000 Subject: [PATCH 13/24] Custom allocator doesn't build in gcc --- .../benchmark_memory_manager/CMakeLists.txt | 15 ------ .../custom_allocator.cpp | 43 --------------- .../custom_allocator.hpp | 52 ------------------- .../benchmark_memory_manager/empty.bench.cpp | 16 ------ 4 files changed, 126 deletions(-) delete mode 100644 barretenberg/cpp/src/barretenberg/benchmark_memory_manager/CMakeLists.txt delete mode 100644 barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.cpp delete mode 100644 barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.hpp delete mode 100644 barretenberg/cpp/src/barretenberg/benchmark_memory_manager/empty.bench.cpp diff --git a/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/CMakeLists.txt b/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/CMakeLists.txt deleted file mode 100644 index 7e680a52fd5..00000000000 --- a/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/CMakeLists.txt +++ /dev/null @@ -1,15 +0,0 @@ -# barretenberg_module( -# benchmark_memory_manager -# common -# ) -add_library( - benchmark_memory_manager_objects - custom_allocator.cpp - -) - -target_link_libraries( - benchmark_memory_manager_objects - PRIVATE - benchmark::benchmark -) diff --git a/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.cpp b/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.cpp deleted file mode 100644 index 9db61459498..00000000000 --- a/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "custom_allocator.hpp" -#include -#include - -TrackingAllocator g_allocator; - -void* TrackingAllocator::allocate(size_t size) -{ - total_allocations++; - total_memory += size; - - void* ptr = std::malloc(size); - if (!ptr) { - throw std::bad_alloc(); - } - allocations.emplace_back(ptr, size); // Add the pointer and its size to the vector - return ptr; -} - -void TrackingAllocator::deallocate(void* ptr) -{ - for (auto it = allocations.begin(); it != allocations.end(); ++it) { - if (it->first == ptr) { - total_deallocated++; - total_memory -= it->second; - allocations.erase(it); // Remove the allocation from the vector - std::free(ptr); - return; - } - } -} - -void* operator new(size_t size) -{ - // std::cout << "calling overloaded new\n"; - return g_allocator.allocate(size); -} - -void operator delete(void* ptr) noexcept -{ - std::cout << "calling overloaded delete\n"; - g_allocator.deallocate(ptr); -} diff --git a/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.hpp b/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.hpp deleted file mode 100644 index 533b312fb54..00000000000 --- a/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.hpp +++ /dev/null @@ -1,52 +0,0 @@ -#pragma once -#include -#include -#include -#include - -class TrackingAllocator { - public: - TrackingAllocator() - : total_allocations(0) - , total_deallocated(0) - , total_memory(0) - {} - - void* allocate(std::size_t size); - void deallocate(void* ptr); - int64_t current_memory_usage() const { return total_memory; } - void reset() - { - total_allocations = total_deallocated = total_memory = 0; - allocations.clear(); - } - - // Print allocation statistics - void printStatistics() const - { - std::cout << "Total Allocations: " << total_allocations << "\n"; - std::cout << "Total Deallocations: " << total_deallocated << "\n"; - std::cout << "Current Memory Usage: " << total_memory << " bytes\n"; - } - - int64_t total_allocations; - int64_t total_deallocated; - int64_t total_memory; - std::vector> allocations; -}; - -// Global tracking allocator instance -extern TrackingAllocator g_allocator; - -void* operator new(std::size_t size); -void operator delete(void* ptr) noexcept; - -class BenchmarkMemoryManager : public benchmark::MemoryManager { - public: - void Start() override { g_allocator.reset(); } - void Stop(Result& result) override - { - result.num_allocs = g_allocator.total_allocations; - result.max_bytes_used = g_allocator.current_memory_usage(); - } -}; diff --git a/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/empty.bench.cpp b/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/empty.bench.cpp deleted file mode 100644 index 4e3e4e2bf91..00000000000 --- a/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/empty.bench.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include -#include - -using namespace benchmark; - -void eg(State& state) -{ - for (auto _ : state) { - std::vector v(1 << 22); - DoNotOptimize(v); - } -} - -BENCHMARK(eg)->Unit(kMicrosecond); - -BENCHMARK_MAIN(); \ No newline at end of file From 197d592e1f52507a1bf10c1374676751d7c00c89 Mon Sep 17 00:00:00 2001 From: Cody Date: Sun, 10 Nov 2024 02:40:21 +0000 Subject: [PATCH 14/24] Revert "Custom allocator doesn't build in gcc" This reverts commit c564a56b15d2c4f70ccc11c1b18b981f898526cf. --- .../benchmark_memory_manager/CMakeLists.txt | 15 ++++++ .../custom_allocator.cpp | 43 +++++++++++++++ .../custom_allocator.hpp | 52 +++++++++++++++++++ .../benchmark_memory_manager/empty.bench.cpp | 16 ++++++ 4 files changed, 126 insertions(+) create mode 100644 barretenberg/cpp/src/barretenberg/benchmark_memory_manager/CMakeLists.txt create mode 100644 barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.cpp create mode 100644 barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.hpp create mode 100644 barretenberg/cpp/src/barretenberg/benchmark_memory_manager/empty.bench.cpp diff --git a/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/CMakeLists.txt b/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/CMakeLists.txt new file mode 100644 index 00000000000..7e680a52fd5 --- /dev/null +++ b/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/CMakeLists.txt @@ -0,0 +1,15 @@ +# barretenberg_module( +# benchmark_memory_manager +# common +# ) +add_library( + benchmark_memory_manager_objects + custom_allocator.cpp + +) + +target_link_libraries( + benchmark_memory_manager_objects + PRIVATE + benchmark::benchmark +) diff --git a/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.cpp b/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.cpp new file mode 100644 index 00000000000..9db61459498 --- /dev/null +++ b/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.cpp @@ -0,0 +1,43 @@ +#include "custom_allocator.hpp" +#include +#include + +TrackingAllocator g_allocator; + +void* TrackingAllocator::allocate(size_t size) +{ + total_allocations++; + total_memory += size; + + void* ptr = std::malloc(size); + if (!ptr) { + throw std::bad_alloc(); + } + allocations.emplace_back(ptr, size); // Add the pointer and its size to the vector + return ptr; +} + +void TrackingAllocator::deallocate(void* ptr) +{ + for (auto it = allocations.begin(); it != allocations.end(); ++it) { + if (it->first == ptr) { + total_deallocated++; + total_memory -= it->second; + allocations.erase(it); // Remove the allocation from the vector + std::free(ptr); + return; + } + } +} + +void* operator new(size_t size) +{ + // std::cout << "calling overloaded new\n"; + return g_allocator.allocate(size); +} + +void operator delete(void* ptr) noexcept +{ + std::cout << "calling overloaded delete\n"; + g_allocator.deallocate(ptr); +} diff --git a/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.hpp b/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.hpp new file mode 100644 index 00000000000..533b312fb54 --- /dev/null +++ b/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.hpp @@ -0,0 +1,52 @@ +#pragma once +#include +#include +#include +#include + +class TrackingAllocator { + public: + TrackingAllocator() + : total_allocations(0) + , total_deallocated(0) + , total_memory(0) + {} + + void* allocate(std::size_t size); + void deallocate(void* ptr); + int64_t current_memory_usage() const { return total_memory; } + void reset() + { + total_allocations = total_deallocated = total_memory = 0; + allocations.clear(); + } + + // Print allocation statistics + void printStatistics() const + { + std::cout << "Total Allocations: " << total_allocations << "\n"; + std::cout << "Total Deallocations: " << total_deallocated << "\n"; + std::cout << "Current Memory Usage: " << total_memory << " bytes\n"; + } + + int64_t total_allocations; + int64_t total_deallocated; + int64_t total_memory; + std::vector> allocations; +}; + +// Global tracking allocator instance +extern TrackingAllocator g_allocator; + +void* operator new(std::size_t size); +void operator delete(void* ptr) noexcept; + +class BenchmarkMemoryManager : public benchmark::MemoryManager { + public: + void Start() override { g_allocator.reset(); } + void Stop(Result& result) override + { + result.num_allocs = g_allocator.total_allocations; + result.max_bytes_used = g_allocator.current_memory_usage(); + } +}; diff --git a/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/empty.bench.cpp b/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/empty.bench.cpp new file mode 100644 index 00000000000..4e3e4e2bf91 --- /dev/null +++ b/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/empty.bench.cpp @@ -0,0 +1,16 @@ +#include +#include + +using namespace benchmark; + +void eg(State& state) +{ + for (auto _ : state) { + std::vector v(1 << 22); + DoNotOptimize(v); + } +} + +BENCHMARK(eg)->Unit(kMicrosecond); + +BENCHMARK_MAIN(); \ No newline at end of file From d2a893c1816ba24ab782bf5801d40474d27b5668 Mon Sep 17 00:00:00 2001 From: Cody Date: Thu, 14 Nov 2024 14:26:57 +0000 Subject: [PATCH 15/24] Some cleanup --- barretenberg/cpp/src/CMakeLists.txt | 1 - .../mega_memory_bench/CMakeLists.txt | 1 - .../benchmark_memory_manager/CMakeLists.txt | 15 ------ .../custom_allocator.cpp | 43 --------------- .../custom_allocator.hpp | 52 ------------------- .../benchmark_memory_manager/empty.bench.cpp | 16 ------ proving-systems.code-workspace | 6 +-- 7 files changed, 3 insertions(+), 131 deletions(-) delete mode 100644 barretenberg/cpp/src/barretenberg/benchmark_memory_manager/CMakeLists.txt delete mode 100644 barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.cpp delete mode 100644 barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.hpp delete mode 100644 barretenberg/cpp/src/barretenberg/benchmark_memory_manager/empty.bench.cpp diff --git a/barretenberg/cpp/src/CMakeLists.txt b/barretenberg/cpp/src/CMakeLists.txt index c80dfc94849..85fbaf19f38 100644 --- a/barretenberg/cpp/src/CMakeLists.txt +++ b/barretenberg/cpp/src/CMakeLists.txt @@ -78,7 +78,6 @@ add_subdirectory(barretenberg/examples) add_subdirectory(barretenberg/flavor) add_subdirectory(barretenberg/goblin) add_subdirectory(barretenberg/grumpkin_srs_gen) -add_subdirectory(barretenberg/benchmark_memory_manager) add_subdirectory(barretenberg/numeric) add_subdirectory(barretenberg/plonk) add_subdirectory(barretenberg/plonk_honk_shared) diff --git a/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/CMakeLists.txt b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/CMakeLists.txt index 28d4d3c123c..f35647268d7 100644 --- a/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/CMakeLists.txt +++ b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/CMakeLists.txt @@ -2,5 +2,4 @@ barretenberg_module( mega_memory_bench ultra_honk stdlib_primitives - # benchmark_memory_manager_objects ) \ No newline at end of file diff --git a/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/CMakeLists.txt b/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/CMakeLists.txt deleted file mode 100644 index 7e680a52fd5..00000000000 --- a/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/CMakeLists.txt +++ /dev/null @@ -1,15 +0,0 @@ -# barretenberg_module( -# benchmark_memory_manager -# common -# ) -add_library( - benchmark_memory_manager_objects - custom_allocator.cpp - -) - -target_link_libraries( - benchmark_memory_manager_objects - PRIVATE - benchmark::benchmark -) diff --git a/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.cpp b/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.cpp deleted file mode 100644 index 9db61459498..00000000000 --- a/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include "custom_allocator.hpp" -#include -#include - -TrackingAllocator g_allocator; - -void* TrackingAllocator::allocate(size_t size) -{ - total_allocations++; - total_memory += size; - - void* ptr = std::malloc(size); - if (!ptr) { - throw std::bad_alloc(); - } - allocations.emplace_back(ptr, size); // Add the pointer and its size to the vector - return ptr; -} - -void TrackingAllocator::deallocate(void* ptr) -{ - for (auto it = allocations.begin(); it != allocations.end(); ++it) { - if (it->first == ptr) { - total_deallocated++; - total_memory -= it->second; - allocations.erase(it); // Remove the allocation from the vector - std::free(ptr); - return; - } - } -} - -void* operator new(size_t size) -{ - // std::cout << "calling overloaded new\n"; - return g_allocator.allocate(size); -} - -void operator delete(void* ptr) noexcept -{ - std::cout << "calling overloaded delete\n"; - g_allocator.deallocate(ptr); -} diff --git a/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.hpp b/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.hpp deleted file mode 100644 index 533b312fb54..00000000000 --- a/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/custom_allocator.hpp +++ /dev/null @@ -1,52 +0,0 @@ -#pragma once -#include -#include -#include -#include - -class TrackingAllocator { - public: - TrackingAllocator() - : total_allocations(0) - , total_deallocated(0) - , total_memory(0) - {} - - void* allocate(std::size_t size); - void deallocate(void* ptr); - int64_t current_memory_usage() const { return total_memory; } - void reset() - { - total_allocations = total_deallocated = total_memory = 0; - allocations.clear(); - } - - // Print allocation statistics - void printStatistics() const - { - std::cout << "Total Allocations: " << total_allocations << "\n"; - std::cout << "Total Deallocations: " << total_deallocated << "\n"; - std::cout << "Current Memory Usage: " << total_memory << " bytes\n"; - } - - int64_t total_allocations; - int64_t total_deallocated; - int64_t total_memory; - std::vector> allocations; -}; - -// Global tracking allocator instance -extern TrackingAllocator g_allocator; - -void* operator new(std::size_t size); -void operator delete(void* ptr) noexcept; - -class BenchmarkMemoryManager : public benchmark::MemoryManager { - public: - void Start() override { g_allocator.reset(); } - void Stop(Result& result) override - { - result.num_allocs = g_allocator.total_allocations; - result.max_bytes_used = g_allocator.current_memory_usage(); - } -}; diff --git a/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/empty.bench.cpp b/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/empty.bench.cpp deleted file mode 100644 index 4e3e4e2bf91..00000000000 --- a/barretenberg/cpp/src/barretenberg/benchmark_memory_manager/empty.bench.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include -#include - -using namespace benchmark; - -void eg(State& state) -{ - for (auto _ : state) { - std::vector v(1 << 22); - DoNotOptimize(v); - } -} - -BENCHMARK(eg)->Unit(kMicrosecond); - -BENCHMARK_MAIN(); \ No newline at end of file diff --git a/proving-systems.code-workspace b/proving-systems.code-workspace index bbe8afa4cf2..be204f2c9b4 100644 --- a/proving-systems.code-workspace +++ b/proving-systems.code-workspace @@ -1,5 +1,5 @@ { - // Each "folder" can define a different project in the main repo, + // Each "folder" can define a different project in the main repo, // relative to `.code-workspace`. "folders": [ { @@ -101,7 +101,7 @@ // which ensures SRS can be read "testMate.cpp.test.workingDirectory": "${command:cmake.buildDirectory}", // Filter all binaries that are not tests or benchmarks - "testMate.cpp.test.executables": "${command:cmake.buildDirectory}/bin/*", + "testMate.cpp.test.executables": "${command:cmake.buildDirectory}/bin/*{test,Test,TEST,bench}*", // // Other // @@ -145,6 +145,6 @@ "cwd": "${command:cmake.buildDirectory}", "internalConsoleOptions": "openOnSessionStart", "console": "internalConsole", - } + } }, } \ No newline at end of file From 42cd9f678d62f06c8a1fc17370a759c06588e3b4 Mon Sep 17 00:00:00 2001 From: Cody Date: Thu, 14 Nov 2024 14:37:40 +0000 Subject: [PATCH 16/24] Revert yarn changes --- yarn-project/ivc-integration/package.json | 2 +- yarn-project/ivc-integration/package.local.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/yarn-project/ivc-integration/package.json b/yarn-project/ivc-integration/package.json index c9fc4dae62e..18b92d47d54 100644 --- a/yarn-project/ivc-integration/package.json +++ b/yarn-project/ivc-integration/package.json @@ -18,7 +18,7 @@ "formatting:fix:types": "NODE_OPTIONS='--max-old-space-size=8096' run -T eslint --fix ./src/types && run -T prettier -w ./src/types", "generate": "yarn generate:noir-circuits", "generate:noir-circuits": "mkdir -p ./artifacts && cp -r ../../noir-projects/mock-protocol-circuits/target/* ./artifacts && node --no-warnings --loader ts-node/esm src/scripts/generate_declaration_files.ts && node --no-warnings --loader ts-node/esm src/scripts/generate_ts_from_abi.ts && run -T prettier -w ./src/types", - "test:non-browser": "NODE_NO_WARNINGS=1 node --experimental-vm-modules ../node_modules/.bin/jest --testPathIgnorePatterns=browser --passWithNoTests ", + "test:non-browser": "NODE_NO_WARNINGS=1 node --experimental-vm-modules ../node_modules/.bin/jest --passWithNoTests --testPathIgnorePatterns=browser", "test:browser": "./run_browser_tests.sh", "test": "yarn test:non-browser", "codegen": "yarn noir-codegen", diff --git a/yarn-project/ivc-integration/package.local.json b/yarn-project/ivc-integration/package.local.json index 82e5715546b..8625c9c135c 100644 --- a/yarn-project/ivc-integration/package.local.json +++ b/yarn-project/ivc-integration/package.local.json @@ -2,7 +2,7 @@ "scripts": { "build": "yarn clean && yarn generate && rm -rf dest && webpack && tsc -b", "clean": "rm -rf ./dest .tsbuildinfo src/types artifacts", - "test:non-browser":"NODE_NO_WARNINGS=1 node --experimental-vm-modules ../node_modules/.bin/jest --testPathIgnorePatterns=browser --passWithNoTests ", + "test:non-browser":"NODE_NO_WARNINGS=1 node --experimental-vm-modules ../node_modules/.bin/jest --passWithNoTests --testPathIgnorePatterns=browser", "test:browser": "./run_browser_tests.sh", "test": "yarn test:non-browser" }, From 82a1dd50c99d2f933e64c4d8ae940d503f7666b8 Mon Sep 17 00:00:00 2001 From: Cody Date: Thu, 14 Nov 2024 14:44:41 +0000 Subject: [PATCH 17/24] More cleanup and verbose logging --- .../mega_circuit_builder.hpp | 32 +++++++++---------- .../stdlib_circuit_builders/mega_flavor.hpp | 1 + 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_circuit_builder.hpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_circuit_builder.hpp index 124bef488a4..a9f51823547 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_circuit_builder.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_circuit_builder.hpp @@ -8,7 +8,7 @@ namespace bb { template class MegaCircuitBuilder_ : public UltraCircuitBuilder_> { - public: + private: DataBus databus; // Container for public calldata/returndata public: @@ -36,7 +36,7 @@ template class MegaCircuitBuilder_ : public UltraCircuitBuilder_& in, BusId bus_idx); @@ -238,7 +238,7 @@ template class MegaCircuitBuilder_ : public UltraCircuitBuilder_(BusId::RETURNDATA)]; } uint64_t estimate_memory() const { - vinfo("ESTIMATING BUILDER MEMORY"); + vinfo("++Estimating builder memory++"); uint64_t result{ 0 }; // gates: @@ -255,23 +255,23 @@ template class MegaCircuitBuilder_ : public UltraCircuitBuilder_variables.capacity() * sizeof(FF); + size_t to_add{ this->variables.capacity() * sizeof(FF) }; + result += to_add; + vinfo("variables: ", to_add); // public inputs - result += this->public_inputs.capacity() * sizeof(uint32_t); + to_add = this->public_inputs.capacity() * sizeof(uint32_t); + result += to_add; + vinfo("public inputs: ", to_add); // other variable indices - result += this->next_var_index.capacity() * sizeof(uint32_t); - result += this->prev_var_index.capacity() * sizeof(uint32_t); - result += this->real_variable_index.capacity() * sizeof(uint32_t); - result += this->real_variable_tags.capacity() * sizeof(uint32_t); - - // // databus - // for (const auto& bus_vector : databus) { - // const uint64_t size((bus_vector.read_counts.capacity() + bus_vector.data.capacity()) * sizeof(uint32_t)); - // vinfo("size: ", size >> 10, " KiB"); - // result += size; - // } + to_add = this->next_var_index.capacity() * sizeof(uint32_t); + to_add += this->prev_var_index.capacity() * sizeof(uint32_t); + to_add += this->real_variable_index.capacity() * sizeof(uint32_t); + to_add += this->real_variable_tags.capacity() * sizeof(uint32_t); + result += to_add; + vinfo("variable indices: ", to_add); + return result; } }; diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_flavor.hpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_flavor.hpp index 1b10d48fef8..fabb732cbbb 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_flavor.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/mega_flavor.hpp @@ -513,6 +513,7 @@ class MegaFlavor { uint64_t estimate_memory() { + vinfo("++Estimating proving key memory++"); for (auto [polynomial, label] : zip_view(polynomials.get_all(), polynomials.get_labels())) { uint64_t size = polynomial.size(); vinfo(label, " num: ", size, " size: ", (size * sizeof(FF)) >> 10, " KiB"); From 2f451b084f923e9ce532d48ac7faa7294d3718da Mon Sep 17 00:00:00 2001 From: Cody Date: Thu, 14 Nov 2024 14:44:56 +0000 Subject: [PATCH 18/24] Rebase over Luke's structure changes --- .../mega_memory_bench/mega_memory.bench.cpp | 30 ++++--------------- 1 file changed, 6 insertions(+), 24 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp index 689977a9d0a..4e10b3c18fa 100644 --- a/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp +++ b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp @@ -1,6 +1,5 @@ #pragma GCC diagnostic ignored "-Wunused-variable" -// #include "barretenberg/benchmark_memory_manager/custom_allocator.hpp" #include "barretenberg/stdlib/primitives/field/field.hpp" #include "barretenberg/stdlib/primitives/plookup/plookup.hpp" #include "barretenberg/stdlib_circuit_builders/plookup_tables/fixed_base/fixed_base.hpp" @@ -283,10 +282,10 @@ void fill_lookup_block(Builder& builder) } } -void fill_trace(State& state, TraceStructure structure) +void fill_trace(State& state, TraceSettings settings) { Builder builder; - builder.blocks.set_fixed_block_sizes(structure); + builder.blocks.set_fixed_block_sizes(settings); fill_ecc_op_block(builder); fill_pub_inputs_block(builder); @@ -320,7 +319,7 @@ void fill_trace(State& state, TraceStructure structure) builder.finalize_circuit(/* ensure_nonzero */ true); uint64_t builder_estimate = builder.estimate_memory(); for (auto _ : state) { - DeciderProvingKey proving_key(builder, structure); + DeciderProvingKey proving_key(builder, settings); uint64_t memory_estimate = proving_key.proving_key.estimate_memory(); state.counters["poly_mem_est"] = static_cast(memory_estimate); state.counters["builder_mem_est"] = static_cast(builder_estimate); @@ -330,38 +329,21 @@ void fill_trace(State& state, TraceStructure structure) void fill_trace_client_ivc_bench(State& state) { - fill_trace(state, TraceStructure::CLIENT_IVC_BENCH); + fill_trace(state, { TraceStructure::CLIENT_IVC_BENCH, /*overflow_capacity=*/0 }); } void fill_trace_e2e_full_test(State& state) { - fill_trace(state, TraceStructure::E2E_FULL_TEST); + fill_trace(state, { TraceStructure::E2E_FULL_TEST, /*overflow_capacity=*/0 }); } -// BenchmarkMemoryManager memory_manager; - static void pk_mem(State& state, void (*test_circuit_function)(State&)) noexcept { - // BenchmarkMemoryManager memory_manager; - // benchmark::RegisterMemoryManager(&memory_manager); test_circuit_function(state); - // ::g_allocator.printStatistics(); - // benchmark::RegisterMemoryManager(nullptr); } BENCHMARK_CAPTURE(pk_mem, E2E_FULL_TEST, &fill_trace_e2e_full_test)->Unit(kMillisecond)->Iterations(1); BENCHMARK_CAPTURE(pk_mem, CLIENT_IVC_BENCH, &fill_trace_client_ivc_bench)->Unit(kMillisecond)->Iterations(1); -BENCHMARK_MAIN(); - -// int main(int argc, char** argv) -// { -// ::benchmark::RegisterMemoryManager(&memory_manager); -// info("registered memory manager"); -// ::benchmark::Initialize(&argc, argv); -// info("initialized"); -// ::benchmark::RunSpecifiedBenchmarks(); -// info("ran specified benchmarks"); -// ::benchmark::RegisterMemoryManager(nullptr); -// } \ No newline at end of file +BENCHMARK_MAIN(); \ No newline at end of file From 44f17c5846af177b5c9b3dafbb4b527cb41508e3 Mon Sep 17 00:00:00 2001 From: Cody Date: Thu, 14 Nov 2024 14:49:55 +0000 Subject: [PATCH 19/24] More cleanup --- barretenberg/barretenberg.code-workspace | 6 +++--- barretenberg/ts/scripts/build_wasm.sh | 1 + yarn-project/ivc-integration/package.json | 2 +- yarn-project/ivc-integration/package.local.json | 2 +- 4 files changed, 6 insertions(+), 5 deletions(-) diff --git a/barretenberg/barretenberg.code-workspace b/barretenberg/barretenberg.code-workspace index 9af9ca53402..cd08c7a3166 100644 --- a/barretenberg/barretenberg.code-workspace +++ b/barretenberg/barretenberg.code-workspace @@ -1,5 +1,5 @@ { - // Each "folder" can define a different project in the main repo, + // Each "folder" can define a different project in the main repo, // relative to `.code-workspace`. "folders": [ { @@ -101,7 +101,7 @@ // which ensures SRS can be read "testMate.cpp.test.workingDirectory": "${command:cmake.buildDirectory}", // Filter all binaries that are not tests or benchmarks - "testMate.cpp.test.executables": "${command:cmake.buildDirectory}/bin/*{test,bench}*", + "testMate.cpp.test.executables": "${command:cmake.buildDirectory}/bin/*{test,Test,TEST,bench}*", // // Other // @@ -145,6 +145,6 @@ "cwd": "${command:cmake.buildDirectory}", "internalConsoleOptions": "openOnSessionStart", "console": "internalConsole", - } + } }, } \ No newline at end of file diff --git a/barretenberg/ts/scripts/build_wasm.sh b/barretenberg/ts/scripts/build_wasm.sh index f55f1e744ab..b12348a16e8 100755 --- a/barretenberg/ts/scripts/build_wasm.sh +++ b/barretenberg/ts/scripts/build_wasm.sh @@ -5,6 +5,7 @@ if [ -z "$SKIP_CPP_BUILD" ]; then # Build the wasms and strip debug symbols. cd ../cpp cmake --preset wasm-threads -DCMAKE_MESSAGE_LOG_LEVEL=Warning && cmake --build --preset wasm-threads + cmake --preset wasm -DCMAKE_MESSAGE_LOG_LEVEL=Warning && cmake --build --preset wasm ./scripts/strip-wasm.sh cd ../ts fi diff --git a/yarn-project/ivc-integration/package.json b/yarn-project/ivc-integration/package.json index 18b92d47d54..c9fc4dae62e 100644 --- a/yarn-project/ivc-integration/package.json +++ b/yarn-project/ivc-integration/package.json @@ -18,7 +18,7 @@ "formatting:fix:types": "NODE_OPTIONS='--max-old-space-size=8096' run -T eslint --fix ./src/types && run -T prettier -w ./src/types", "generate": "yarn generate:noir-circuits", "generate:noir-circuits": "mkdir -p ./artifacts && cp -r ../../noir-projects/mock-protocol-circuits/target/* ./artifacts && node --no-warnings --loader ts-node/esm src/scripts/generate_declaration_files.ts && node --no-warnings --loader ts-node/esm src/scripts/generate_ts_from_abi.ts && run -T prettier -w ./src/types", - "test:non-browser": "NODE_NO_WARNINGS=1 node --experimental-vm-modules ../node_modules/.bin/jest --passWithNoTests --testPathIgnorePatterns=browser", + "test:non-browser": "NODE_NO_WARNINGS=1 node --experimental-vm-modules ../node_modules/.bin/jest --testPathIgnorePatterns=browser --passWithNoTests ", "test:browser": "./run_browser_tests.sh", "test": "yarn test:non-browser", "codegen": "yarn noir-codegen", diff --git a/yarn-project/ivc-integration/package.local.json b/yarn-project/ivc-integration/package.local.json index 8625c9c135c..82e5715546b 100644 --- a/yarn-project/ivc-integration/package.local.json +++ b/yarn-project/ivc-integration/package.local.json @@ -2,7 +2,7 @@ "scripts": { "build": "yarn clean && yarn generate && rm -rf dest && webpack && tsc -b", "clean": "rm -rf ./dest .tsbuildinfo src/types artifacts", - "test:non-browser":"NODE_NO_WARNINGS=1 node --experimental-vm-modules ../node_modules/.bin/jest --passWithNoTests --testPathIgnorePatterns=browser", + "test:non-browser":"NODE_NO_WARNINGS=1 node --experimental-vm-modules ../node_modules/.bin/jest --testPathIgnorePatterns=browser --passWithNoTests ", "test:browser": "./run_browser_tests.sh", "test": "yarn test:non-browser" }, From 8d95637d64cedb59c7a7ba0d30e9fa65a6bd4426 Mon Sep 17 00:00:00 2001 From: Cody Date: Thu, 14 Nov 2024 14:50:05 +0000 Subject: [PATCH 20/24] Remove (unneeded?) header --- barretenberg/cpp/src/barretenberg/common/zip_view.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/barretenberg/cpp/src/barretenberg/common/zip_view.hpp b/barretenberg/cpp/src/barretenberg/common/zip_view.hpp index 802227ad741..588e0703cd8 100644 --- a/barretenberg/cpp/src/barretenberg/common/zip_view.hpp +++ b/barretenberg/cpp/src/barretenberg/common/zip_view.hpp @@ -76,7 +76,6 @@ static_assert(__cplusplus >= 201703L, " must be c++17 or greater"); // could be rewritten in c++11, but the features you must use will be buggy // in an older compiler anyways. #include "barretenberg/common/assert.hpp" -#include #include #include #include From 78afdcf4c421b5f4dd8e4ac01c11a5f6206f6f00 Mon Sep 17 00:00:00 2001 From: Cody Date: Thu, 14 Nov 2024 14:55:11 +0000 Subject: [PATCH 21/24] Deleted unused table id PEDERSEN_IV --- .../stdlib_circuit_builders/plookup_tables/types.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/types.hpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/types.hpp index 8c7de0789b4..9ba18ab891f 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/types.hpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/types.hpp @@ -111,7 +111,6 @@ enum MultiTableId { BLAKE_XOR_ROTATE_16, BLAKE_XOR_ROTATE_8, BLAKE_XOR_ROTATE_7, - PEDERSEN_IV, // WORKTODO: unused HONK_DUMMY_MULTI, KECCAK_THETA_OUTPUT, KECCAK_CHI_OUTPUT, From 530660a21ddd07eb0b437b23a85579a43a1e065c Mon Sep 17 00:00:00 2001 From: Cody Date: Thu, 14 Nov 2024 14:55:21 +0000 Subject: [PATCH 22/24] More cleanup --- barretenberg/cpp/src/barretenberg/common/zip_view.hpp | 1 + .../stdlib_circuit_builders/plookup_tables/plookup_tables.cpp | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/common/zip_view.hpp b/barretenberg/cpp/src/barretenberg/common/zip_view.hpp index 588e0703cd8..802227ad741 100644 --- a/barretenberg/cpp/src/barretenberg/common/zip_view.hpp +++ b/barretenberg/cpp/src/barretenberg/common/zip_view.hpp @@ -76,6 +76,7 @@ static_assert(__cplusplus >= 201703L, " must be c++17 or greater"); // could be rewritten in c++11, but the features you must use will be buggy // in an older compiler anyways. #include "barretenberg/common/assert.hpp" +#include #include #include #include diff --git a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/plookup_tables.cpp b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/plookup_tables.cpp index 314013e4cfa..0440eb17b9c 100644 --- a/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/plookup_tables.cpp +++ b/barretenberg/cpp/src/barretenberg/stdlib_circuit_builders/plookup_tables/plookup_tables.cpp @@ -49,8 +49,7 @@ void init_multi_tables() sha256_tables::get_majority_output_table(MultiTableId::SHA256_MAJ_OUTPUT); MULTI_TABLES[MultiTableId::SHA256_WITNESS_OUTPUT] = sha256_tables::get_witness_extension_output_table(MultiTableId::SHA256_WITNESS_OUTPUT); - MULTI_TABLES[MultiTableId::AES_NORMALIZE] = - aes128_tables::get_aes_normalization_table(MultiTableId::AES_NORMALIZE); // WORKTODO: table not used anywhere? + MULTI_TABLES[MultiTableId::AES_NORMALIZE] = aes128_tables::get_aes_normalization_table(MultiTableId::AES_NORMALIZE); MULTI_TABLES[MultiTableId::AES_INPUT] = aes128_tables::get_aes_input_table(MultiTableId::AES_INPUT); MULTI_TABLES[MultiTableId::AES_SBOX] = aes128_tables::get_aes_sbox_table(MultiTableId::AES_SBOX); MULTI_TABLES[MultiTableId::UINT32_XOR] = uint_tables::get_uint32_xor_table(MultiTableId::UINT32_XOR); From 8e600e6b166247284fae7fb257e5cf9ba4cee636 Mon Sep 17 00:00:00 2001 From: Cody Date: Thu, 14 Nov 2024 15:57:43 +0000 Subject: [PATCH 23/24] Check prints and clarify summarization output --- .../benchmark/mega_memory_bench/mega_memory.bench.cpp | 4 ++-- .../arithmetization/mega_arithmetization.hpp | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp index 4e10b3c18fa..7d59f843130 100644 --- a/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp +++ b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp @@ -297,13 +297,13 @@ void fill_trace(State& state, TraceSettings settings) fill_poseidon2_external_block(builder); fill_poseidon2_internal_block(builder); fill_lookup_block(builder); - // builder.finalize_circuit(/* ensure_nonzero */ false); { - // finalize doesn't populate public inputs block, so copy to verify that the block is being filled well + // finalize doesn't populate public inputs block, so copy to verify that the block is being filled well. // otherwise the pk construction will overflow the block // alternative: add to finalize or add a flag to check whether PIs have already been populated auto builder_copy = builder; + builder_copy.finalize_circuit(/* ensure_nonzero */ false); DeciderProvingKey::Trace::populate_public_inputs_block(builder_copy); for (const auto [label, block] : zip_view(builder_copy.blocks.get_labels(), builder_copy.blocks.get())) { diff --git a/barretenberg/cpp/src/barretenberg/plonk_honk_shared/arithmetization/mega_arithmetization.hpp b/barretenberg/cpp/src/barretenberg/plonk_honk_shared/arithmetization/mega_arithmetization.hpp index 1643fa35000..dffe7d5d25a 100644 --- a/barretenberg/cpp/src/barretenberg/plonk_honk_shared/arithmetization/mega_arithmetization.hpp +++ b/barretenberg/cpp/src/barretenberg/plonk_honk_shared/arithmetization/mega_arithmetization.hpp @@ -306,7 +306,11 @@ template class MegaArith { { info("Gate blocks summary: (actual gates / fixed capacity)"); info("goblin ecc op :\t", this->ecc_op.size(), "/", this->ecc_op.get_fixed_size()); - info("pub inputs :\t", this->pub_inputs.size(), "/", this->pub_inputs.get_fixed_size()); + info("pub inputs :\t", + this->pub_inputs.size(), + "/", + this->pub_inputs.get_fixed_size(), + " (populated in decider pk constructor)"); info("busread :\t", this->busread.size(), "/", this->busread.get_fixed_size()); info("arithmetic :\t", this->arithmetic.size(), "/", this->arithmetic.get_fixed_size()); info("delta range :\t", this->delta_range.size(), "/", this->delta_range.get_fixed_size()); From e1daef7e20a23f8d84f3659a9052d783c425dc3a Mon Sep 17 00:00:00 2001 From: Cody Date: Thu, 14 Nov 2024 16:04:47 +0000 Subject: [PATCH 24/24] More cleanup in new file --- .../benchmark/mega_memory_bench/mega_memory.bench.cpp | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp index 7d59f843130..29d58f16606 100644 --- a/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp +++ b/barretenberg/cpp/src/barretenberg/benchmark/mega_memory_bench/mega_memory.bench.cpp @@ -1,5 +1,3 @@ -#pragma GCC diagnostic ignored "-Wunused-variable" - #include "barretenberg/stdlib/primitives/field/field.hpp" #include "barretenberg/stdlib/primitives/plookup/plookup.hpp" #include "barretenberg/stdlib_circuit_builders/plookup_tables/fixed_base/fixed_base.hpp" @@ -137,21 +135,20 @@ void fill_elliptic_block(Builder& builder) const uint32_t y2_idx = builder.add_variable(fr::random_element()); const uint32_t x3_idx = builder.add_variable(fr::random_element()); const uint32_t y3_idx = builder.add_variable(fr::random_element()); - while (builder.blocks.elliptic.size() < builder.blocks.elliptic.get_fixed_size() - 100) { + while (builder.blocks.elliptic.size() < builder.blocks.elliptic.get_fixed_size() - 10 * NUM_SHORT) { builder.create_ecc_add_gate({ x1_idx, y1_idx, x2_idx, y2_idx, x3_idx, y3_idx, 1 }); } } void fill_aux_block(Builder& builder) { - // static constexpr size_t NUM_AUX_TYPES = 11; auto& block = builder.blocks.aux; const uint32_t idx_1 = builder.add_variable(fr::random_element()); const uint32_t idx_2 = builder.add_variable(fr::random_element()); const uint32_t idx_3 = builder.add_variable(fr::random_element()); const uint32_t idx_4 = builder.add_variable(fr::random_element()); - while (block.size() < block.get_fixed_size() - 100) { + while (block.size() < block.get_fixed_size() - 10 * NUM_SHORT) { builder.apply_aux_selectors(Builder::AUX_SELECTORS::ROM_READ); block.populate_wires(idx_1, idx_2, idx_3, idx_4); builder.apply_aux_selectors(Builder::AUX_SELECTORS::LIMB_ACCUMULATE_1); @@ -234,8 +231,6 @@ void fill_lookup_block(Builder& builder) plookup::fixed_base::table::BITS_PER_LO_SCALAR + plookup::fixed_base::table::BITS_PER_HI_SCALAR); const auto input_lo = uint256_t(pedersen_input_value).slice(0, bb::plookup::fixed_base::table::BITS_PER_LO_SCALAR); - const auto input_hi_index = builder.add_variable(input_hi); - const auto input_lo_index = builder.add_variable(input_lo); plookup::get_lookup_accumulators(bb::plookup::MultiTableId::FIXED_BASE_LEFT_HI, input_hi); plookup::get_lookup_accumulators(bb::plookup::MultiTableId::FIXED_BASE_LEFT_LO, input_lo); plookup::get_lookup_accumulators(bb::plookup::MultiTableId::FIXED_BASE_RIGHT_HI, input_hi);