Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Avoiding redundant computation in PG #5844

Merged
merged 15 commits into from
Apr 30, 2024
9 changes: 8 additions & 1 deletion barretenberg/cpp/src/barretenberg/eccvm/eccvm_flavor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,16 @@ class ECCVMFlavor {
};

/**
* @brief A container for univariates used during sumcheck.
* @brief A container for univariates used during Protogalaxy and sumcheck.
*/
template <size_t LENGTH> using ProverUnivariates = AllEntities<bb::Univariate<FF, LENGTH>>;
/**
* @brief A container for univariates used during Protogalaxy folding in 'optimised' mode.
* @details Univariates in the optimised version skip some redundant computation the result of which we already know
* (optimistically)
*/
template <size_t LENGTH, size_t SKIP_COUNT>
using OptimisedProverUnivariates = AllEntities<bb::Univariate<FF, LENGTH, 0, SKIP_COUNT>>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed?


/**
* @brief A container for univariates produced during the hot loop in sumcheck.
Expand Down
18 changes: 18 additions & 0 deletions barretenberg/cpp/src/barretenberg/flavor/flavor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,24 @@ static constexpr auto create_protogalaxy_tuple_of_tuples_of_univariates()
}
}

/**
* @brief Recursive utility function to construct a container for the subrelation accumulators of Protogalaxy folding.
* @details Differs from the non-optimised method by using optimised univariates that skip redundant computation
*/
template <typename Tuple, size_t NUM_INSTANCES, size_t Index = 0>
static constexpr auto create_optimised_protogalaxy_tuple_of_tuples_of_univariates()
{
if constexpr (Index >= std::tuple_size<Tuple>::value) {
return std::tuple<>{}; // Return empty when reach end of the tuple
} else {
using UnivariateTuple = typename std::tuple_element_t<Index, Tuple>::
template OptimisedProtogalaxyTupleOfUnivariatesOverSubrelations<NUM_INSTANCES>;
return std::tuple_cat(
std::tuple<UnivariateTuple>{},
create_optimised_protogalaxy_tuple_of_tuples_of_univariates<Tuple, NUM_INSTANCES, Index + 1>());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we not overload in some way instead of duplicating code?

}
}

/**
* @brief Recursive utility function to construct a container for the subrelation accumulators of sumcheck proving.
* @details The size of the outer tuple is equal to the number of relations. Each relation contributes an inner tuple of
Expand Down
208 changes: 131 additions & 77 deletions barretenberg/cpp/src/barretenberg/polynomials/univariate.hpp

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ TEST(Protogalaxy, CombinerOn2Instances)
ProverInstances instances{ instance_data };
instances.alphas.fill(bb::Univariate<FF, 12>(FF(0))); // focus on the arithmetic relation only
auto pow_polynomial = PowPolynomial(std::vector<FF>{ 2 });
auto result = prover.compute_combiner(instances, pow_polynomial);
auto result = prover.compute_combiner</*disable_optimisation=*/true>(instances, pow_polynomial);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think it would be great to have these tests run for the optimised version as well

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use the python script to generate the correct combiner values

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but they actually shouldn't be modified if I understand correctly, just some of them skipped

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the values

auto expected_result = Univariate<FF, 12>(std::array<FF, 12>{
87706,
13644570,
Expand Down
195 changes: 154 additions & 41 deletions barretenberg/cpp/src/barretenberg/protogalaxy/protogalaxy_prover.hpp

Large diffs are not rendered by default.

24 changes: 18 additions & 6 deletions barretenberg/cpp/src/barretenberg/relations/nested_containers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,42 @@ namespace bb {
*
* @details Credit: https://stackoverflow.com/a/60440611
*/
template <template <typename, size_t, size_t> typename InnerContainer,
template <template <typename, size_t, size_t, size_t> typename InnerContainer,
typename ValueType,
auto domain_end,
size_t domain_start = 0,
size_t skip_count = 0,
typename IS = decltype(std::make_index_sequence<domain_end.size()>())>
struct TupleOfContainersOverArray;
template <template <typename, size_t, size_t> typename InnerContainer,
template <template <typename, size_t, size_t, size_t> typename InnerContainer,
typename ValueType,
auto domain_end,
size_t domain_start,
size_t skip_count,
std::size_t... I>
struct TupleOfContainersOverArray<InnerContainer, ValueType, domain_end, domain_start, std::index_sequence<I...>> {
using type = std::tuple<InnerContainer<ValueType, domain_end[I], domain_start>...>;
struct TupleOfContainersOverArray<InnerContainer,
ValueType,
domain_end,
domain_start,
skip_count,
std::index_sequence<I...>> {
using type = std::tuple<InnerContainer<ValueType, domain_end[I], domain_start, skip_count>...>;
};

// Helpers
template <typename ValueType, size_t, size_t> using ExtractValueType = ValueType;
template <typename ValueType, size_t, size_t, size_t> using ExtractValueType = ValueType;

template <typename Tuple>
using HomogeneousTupleToArray = std::array<std::tuple_element_t<0, Tuple>, std::tuple_size_v<Tuple>>;

// Types needed for sumcheck and folding.
template <typename FF, auto LENGTHS>
using TupleOfUnivariates = typename TupleOfContainersOverArray<bb::Univariate, FF, LENGTHS, 0>::type;
using TupleOfUnivariates = typename TupleOfContainersOverArray<bb::Univariate, FF, LENGTHS, 0, 0>::type;

// Types needed for sumcheck and folding.
template <typename FF, auto LENGTHS, size_t SKIP_COUNT>
using OptimisedTupleOfUnivariates =
typename TupleOfContainersOverArray<bb::Univariate, FF, LENGTHS, 0, SKIP_COUNT>::type;

template <typename FF, auto LENGTHS>
using TupleOfValues = typename TupleOfContainersOverArray<ExtractValueType, FF, LENGTHS>::type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ template <typename RelationImpl> class Relation : public RelationImpl {
template <size_t NUM_INSTANCES>
using ProtogalaxyTupleOfUnivariatesOverSubrelations =
TupleOfUnivariates<FF, compute_composed_subrelation_partial_lengths<NUM_INSTANCES>(SUBRELATION_TOTAL_LENGTHS)>;
template <size_t NUM_INSTANCES>
using OptimisedProtogalaxyTupleOfUnivariatesOverSubrelations =
OptimisedTupleOfUnivariates<FF,
compute_composed_subrelation_partial_lengths<NUM_INSTANCES>(
SUBRELATION_TOTAL_LENGTHS),
NUM_INSTANCES - 1>;
using SumcheckTupleOfUnivariatesOverSubrelations =
TupleOfUnivariates<FF, RelationImpl::SUBRELATION_PARTIAL_LENGTHS>;
using SumcheckArrayOfValuesOverSubrelations = ArrayOfValues<FF, RelationImpl::SUBRELATION_PARTIAL_LENGTHS>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ class GoblinUltraFlavor {
template <size_t NUM_INSTANCES>
using ProtogalaxyTupleOfTuplesOfUnivariates =
decltype(create_protogalaxy_tuple_of_tuples_of_univariates<Relations, NUM_INSTANCES>());

template <size_t NUM_INSTANCES>
using OptimisedProtogalaxyTupleOfTuplesOfUnivariates =
decltype(create_optimised_protogalaxy_tuple_of_tuples_of_univariates<Relations, NUM_INSTANCES>());
using SumcheckTupleOfTuplesOfUnivariates = decltype(create_sumcheck_tuple_of_tuples_of_univariates<Relations>());
using TupleOfArraysOfValues = decltype(create_tuple_of_arrays_of_values<Relations>());

Expand Down Expand Up @@ -450,6 +454,14 @@ class GoblinUltraFlavor {
*/
template <size_t LENGTH> using ProverUnivariates = AllEntities<bb::Univariate<FF, LENGTH>>;

/**
* @brief A container for univariates used during Protogalaxy folding and sumcheck with some of the computation
* optmistically ignored.
* @details During folding and sumcheck, the prover evaluates the relations on these univariates.
*/
template <size_t LENGTH, size_t SKIP_COUNT>
using OptimisedProverUnivariates = AllEntities<bb::Univariate<FF, LENGTH, 0, SKIP_COUNT>>;

/**
* @brief A container for univariates produced during the hot loop in sumcheck.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ class UltraFlavor {
template <size_t NUM_INSTANCES>
using ProtogalaxyTupleOfTuplesOfUnivariates =
decltype(create_protogalaxy_tuple_of_tuples_of_univariates<Relations, NUM_INSTANCES>());
template <size_t NUM_INSTANCES>
using OptimisedProtogalaxyTupleOfTuplesOfUnivariates =
decltype(create_optimised_protogalaxy_tuple_of_tuples_of_univariates<Relations, NUM_INSTANCES>());
using SumcheckTupleOfTuplesOfUnivariates = decltype(create_sumcheck_tuple_of_tuples_of_univariates<Relations>());
using TupleOfArraysOfValues = decltype(create_tuple_of_arrays_of_values<Relations>());

Expand Down Expand Up @@ -472,6 +475,12 @@ class UltraFlavor {
* @details During folding and sumcheck, the prover evaluates the relations on these univariates.
*/
template <size_t LENGTH> using ProverUnivariates = AllEntities<bb::Univariate<FF, LENGTH>>;
/**
* @brief A container for univariates used during Protogalaxy folding and sumcheck.
* @details During folding and sumcheck, the prover evaluates the relations on these univariates.
*/
template <size_t LENGTH, size_t SKIP_COUNT>
using OptimisedProverUnivariates = AllEntities<bb::Univariate<FF, LENGTH, 0, SKIP_COUNT>>;

/**
* @brief A container for univariates produced during the hot loop in sumcheck.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ template <typename Flavor_, size_t NUM_ = 2> struct ProverInstances_ {
static constexpr size_t EXTENDED_LENGTH = (Flavor::MAX_TOTAL_RELATION_LENGTH - 1) * (NUM - 1) + 1;
static constexpr size_t BATCHED_EXTENDED_LENGTH = (Flavor::MAX_TOTAL_RELATION_LENGTH - 1 + NUM - 1) * (NUM - 1) + 1;
using RelationParameters = bb::RelationParameters<Univariate<FF, EXTENDED_LENGTH>>;
using OptimisedRelationParameters = bb::RelationParameters<Univariate<FF, EXTENDED_LENGTH, 0, NUM_ - 1>>;
using RelationSeparator = std::array<Univariate<FF, BATCHED_EXTENDED_LENGTH>, NUM_SUBRELATIONS - 1>;
ArrayType _data;
RelationParameters relation_parameters;
OptimisedRelationParameters optimised_relation_parameters;
RelationSeparator alphas;
std::vector<FF> next_gate_challenges;

Expand Down Expand Up @@ -52,12 +54,13 @@ template <typename Flavor_, size_t NUM_ = 2> struct ProverInstances_ {
* and the function returns the univariates [{a_1, b_1, c_1, d_1}, {a_2, b_2, c_2, d_2}, ...]
*
* @param row_idx A fixed row position in several execution traces
* @tparam skip_count Construct univariates that skip some of the indices when computing results
* @return The univariates whose extensions will be used to construct the combiner.
*/
auto row_to_univariates(size_t row_idx) const
template <size_t skip_count = 0> auto row_to_univariates(size_t row_idx) const
{
auto insts_prover_polynomials_views = get_polynomials_views();
std::array<Univariate<FF, NUM>, insts_prover_polynomials_views[0].size()> results;
std::array<Univariate<FF, NUM, 0, skip_count>, insts_prover_polynomials_views[0].size()> results;
// Set the size corresponding to the number of rows in the execution trace
size_t instance_idx = 0;
// Iterate over the prover polynomials' views corresponding to each instance
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,13 @@ class GoblinTranslatorFlavor {
* @brief A container for univariates used during sumcheck.
*/
template <size_t LENGTH> using ProverUnivariates = AllEntities<bb::Univariate<FF, LENGTH>>;
/**
* @brief A container for univariates used during Protogalaxy folding and sumcheck with some of the computation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

translator doesn't have anything to do with Protogalaxy (same for eccvm and avm) so I wonder whether this should be here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, removed

* optmistically ignored
* @details During folding and sumcheck, the prover evaluates the relations on these univariates.
*/
template <size_t LENGTH, size_t SKIP_COUNT>
using OptimisedProverUnivariates = AllEntities<bb::Univariate<FF, LENGTH, 0, SKIP_COUNT>>;

/**
* @brief A container for univariates produced during the hot loop in sumcheck.
Expand Down
10 changes: 10 additions & 0 deletions barretenberg/cpp/src/barretenberg/vm/generated/avm_flavor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ class AvmFlavor {
template <size_t NUM_INSTANCES>
using ProtogalaxyTupleOfTuplesOfUnivariates =
decltype(create_protogalaxy_tuple_of_tuples_of_univariates<Relations, NUM_INSTANCES>());
template <size_t NUM_INSTANCES>
Rumata888 marked this conversation as resolved.
Show resolved Hide resolved
using OptimisedProtogalaxyTupleOfTuplesOfUnivariates =
decltype(create_optimised_protogalaxy_tuple_of_tuples_of_univariates<Relations, NUM_INSTANCES>());
using SumcheckTupleOfTuplesOfUnivariates = decltype(create_sumcheck_tuple_of_tuples_of_univariates<Relations>());
using TupleOfArraysOfValues = decltype(create_tuple_of_arrays_of_values<Relations>());

Expand Down Expand Up @@ -1704,6 +1707,13 @@ class AvmFlavor {
* @details During folding and sumcheck, the prover evaluates the relations on these univariates.
*/
template <size_t LENGTH> using ProverUnivariates = AllEntities<bb::Univariate<FF, LENGTH>>;
/**
* @brief A container for univariates used during Protogalaxy folding and sumcheck with some of the computation
* optmistically ignored
* @details During folding and sumcheck, the prover evaluates the relations on these univariates.
*/
template <size_t LENGTH, size_t SKIP_COUNT>
using OptimisedProverUnivariates = AllEntities<bb::Univariate<FF, LENGTH, 0, SKIP_COUNT>>;

/**
* @brief A container for univariates produced during the hot loop in sumcheck.
Expand Down
Loading