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

Merge Cairo Pie extra segments into one segment #1960

Open
wants to merge 23 commits into
base: main
Choose a base branch
from

Conversation

FrancoGiachetta
Copy link
Contributor

@FrancoGiachetta FrancoGiachetta commented Feb 17, 2025

Allow merging extra_segments with Cairo Pie to increase the segments number limit

Description

The PR adds the method merge_extra_segments to merge extra_segments into one segment. This solves the issue of cairo pie limiting the number of segments to 2^16. This method returns a tuple with the merged segment and a HasMap with the new offsets (made out of the old segment indices mapped to their new offset in the new segment). This new offsets are then used during the memory serialization.
A new flag merge_extra_segments was added to the cli, which merges the extra segments if there were any. Memory serialization is then done taking into account this new segment. If there were no extra segments, it defaults to normal behavior.

Closes #1947

Checklist

  • Linked to Github Issue
  • Unit tests added
  • Integration tests added.
  • This change requires new documentation.
    • Documentation has been added/updated.
    • CHANGELOG has been updated.

Copy link

github-actions bot commented Feb 17, 2025

**Hyper Thereading Benchmark results**




hyperfine -r 2 -n "hyper_threading_main threads: 1" 'RAYON_NUM_THREADS=1 ./hyper_threading_main' -n "hyper_threading_pr threads: 1" 'RAYON_NUM_THREADS=1 ./hyper_threading_pr'
Benchmark 1: hyper_threading_main threads: 1
  Time (mean ± σ):     27.438 s ±  0.072 s    [User: 26.712 s, System: 0.724 s]
  Range (min … max):   27.387 s … 27.489 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 1
  Time (mean ± σ):     27.471 s ±  0.031 s    [User: 26.730 s, System: 0.740 s]
  Range (min … max):   27.449 s … 27.492 s    2 runs
 
Summary
  hyper_threading_main threads: 1 ran
    1.00 ± 0.00 times faster than hyper_threading_pr threads: 1




hyperfine -r 2 -n "hyper_threading_main threads: 2" 'RAYON_NUM_THREADS=2 ./hyper_threading_main' -n "hyper_threading_pr threads: 2" 'RAYON_NUM_THREADS=2 ./hyper_threading_pr'
Benchmark 1: hyper_threading_main threads: 2
  Time (mean ± σ):     15.203 s ±  0.018 s    [User: 26.681 s, System: 0.785 s]
  Range (min … max):   15.190 s … 15.216 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 2
  Time (mean ± σ):     15.364 s ±  0.141 s    [User: 26.886 s, System: 0.803 s]
  Range (min … max):   15.264 s … 15.464 s    2 runs
 
Summary
  hyper_threading_main threads: 2 ran
    1.01 ± 0.01 times faster than hyper_threading_pr threads: 2




hyperfine -r 2 -n "hyper_threading_main threads: 4" 'RAYON_NUM_THREADS=4 ./hyper_threading_main' -n "hyper_threading_pr threads: 4" 'RAYON_NUM_THREADS=4 ./hyper_threading_pr'
Benchmark 1: hyper_threading_main threads: 4
  Time (mean ± σ):     11.107 s ±  0.364 s    [User: 39.295 s, System: 0.937 s]
  Range (min … max):   10.850 s … 11.364 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 4
  Time (mean ± σ):     10.855 s ±  0.387 s    [User: 39.977 s, System: 0.927 s]
  Range (min … max):   10.582 s … 11.129 s    2 runs
 
Summary
  hyper_threading_pr threads: 4 ran
    1.02 ± 0.05 times faster than hyper_threading_main threads: 4




hyperfine -r 2 -n "hyper_threading_main threads: 6" 'RAYON_NUM_THREADS=6 ./hyper_threading_main' -n "hyper_threading_pr threads: 6" 'RAYON_NUM_THREADS=6 ./hyper_threading_pr'
Benchmark 1: hyper_threading_main threads: 6
  Time (mean ± σ):     10.755 s ±  0.183 s    [User: 40.224 s, System: 0.939 s]
  Range (min … max):   10.626 s … 10.884 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 6
  Time (mean ± σ):     10.995 s ±  0.363 s    [User: 39.592 s, System: 0.915 s]
  Range (min … max):   10.738 s … 11.252 s    2 runs
 
Summary
  hyper_threading_main threads: 6 ran
    1.02 ± 0.04 times faster than hyper_threading_pr threads: 6




hyperfine -r 2 -n "hyper_threading_main threads: 8" 'RAYON_NUM_THREADS=8 ./hyper_threading_main' -n "hyper_threading_pr threads: 8" 'RAYON_NUM_THREADS=8 ./hyper_threading_pr'
Benchmark 1: hyper_threading_main threads: 8
  Time (mean ± σ):     10.721 s ±  0.152 s    [User: 40.217 s, System: 0.940 s]
  Range (min … max):   10.614 s … 10.828 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 8
  Time (mean ± σ):     10.673 s ±  0.117 s    [User: 40.291 s, System: 0.953 s]
  Range (min … max):   10.590 s … 10.755 s    2 runs
 
Summary
  hyper_threading_pr threads: 8 ran
    1.00 ± 0.02 times faster than hyper_threading_main threads: 8




hyperfine -r 2 -n "hyper_threading_main threads: 16" 'RAYON_NUM_THREADS=16 ./hyper_threading_main' -n "hyper_threading_pr threads: 16" 'RAYON_NUM_THREADS=16 ./hyper_threading_pr'
Benchmark 1: hyper_threading_main threads: 16
  Time (mean ± σ):     11.003 s ±  0.062 s    [User: 40.282 s, System: 1.036 s]
  Range (min … max):   10.959 s … 11.047 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 16
  Time (mean ± σ):     10.868 s ±  0.279 s    [User: 40.468 s, System: 1.005 s]
  Range (min … max):   10.671 s … 11.065 s    2 runs
 
Summary
  hyper_threading_pr threads: 16 ran
    1.01 ± 0.03 times faster than hyper_threading_main threads: 16


Copy link

github-actions bot commented Feb 17, 2025

Benchmark Results for unmodified programs 🚀

Command Mean [s] Min [s] Max [s] Relative
base big_factorial 2.216 ± 0.008 2.199 2.227 1.01 ± 0.01
head big_factorial 2.189 ± 0.013 2.176 2.217 1.00
Command Mean [s] Min [s] Max [s] Relative
base big_fibonacci 2.195 ± 0.022 2.160 2.230 1.01 ± 0.03
head big_fibonacci 2.176 ± 0.052 2.148 2.325 1.00
Command Mean [s] Min [s] Max [s] Relative
base blake2s_integration_benchmark 8.260 ± 0.102 8.131 8.395 1.02 ± 0.02
head blake2s_integration_benchmark 8.061 ± 0.094 7.885 8.177 1.00
Command Mean [s] Min [s] Max [s] Relative
base compare_arrays_200000 2.355 ± 0.023 2.323 2.399 1.02 ± 0.02
head compare_arrays_200000 2.310 ± 0.033 2.289 2.399 1.00
Command Mean [s] Min [s] Max [s] Relative
base dict_integration_benchmark 1.529 ± 0.029 1.495 1.575 1.01 ± 0.02
head dict_integration_benchmark 1.518 ± 0.008 1.509 1.530 1.00
Command Mean [s] Min [s] Max [s] Relative
base field_arithmetic_get_square_benchmark 1.318 ± 0.016 1.290 1.353 1.02 ± 0.02
head field_arithmetic_get_square_benchmark 1.289 ± 0.013 1.264 1.311 1.00
Command Mean [s] Min [s] Max [s] Relative
base integration_builtins 8.368 ± 0.093 8.219 8.491 1.02 ± 0.01
head integration_builtins 8.177 ± 0.069 8.033 8.270 1.00
Command Mean [s] Min [s] Max [s] Relative
base keccak_integration_benchmark 8.626 ± 0.109 8.494 8.779 1.02 ± 0.01
head keccak_integration_benchmark 8.445 ± 0.050 8.355 8.511 1.00
Command Mean [s] Min [s] Max [s] Relative
base linear_search 2.308 ± 0.013 2.294 2.327 1.00 ± 0.01
head linear_search 2.300 ± 0.024 2.273 2.342 1.00
Command Mean [s] Min [s] Max [s] Relative
base math_cmp_and_pow_integration_benchmark 1.603 ± 0.015 1.586 1.638 1.01 ± 0.01
head math_cmp_and_pow_integration_benchmark 1.588 ± 0.011 1.574 1.606 1.00
Command Mean [s] Min [s] Max [s] Relative
base math_integration_benchmark 1.533 ± 0.010 1.520 1.548 1.00
head math_integration_benchmark 1.536 ± 0.008 1.526 1.552 1.00 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base memory_integration_benchmark 1.299 ± 0.017 1.286 1.344 1.01 ± 0.01
head memory_integration_benchmark 1.281 ± 0.008 1.268 1.295 1.00
Command Mean [s] Min [s] Max [s] Relative
base operations_with_data_structures_benchmarks 1.648 ± 0.006 1.635 1.654 1.00 ± 0.01
head operations_with_data_structures_benchmarks 1.644 ± 0.012 1.625 1.670 1.00
Command Mean [ms] Min [ms] Max [ms] Relative
base pedersen 552.9 ± 2.9 549.0 558.3 1.01 ± 0.02
head pedersen 550.1 ± 7.8 545.6 571.9 1.00
Command Mean [ms] Min [ms] Max [ms] Relative
base poseidon_integration_benchmark 664.6 ± 6.5 659.7 681.6 1.01 ± 0.01
head poseidon_integration_benchmark 659.8 ± 3.5 655.2 667.1 1.00
Command Mean [s] Min [s] Max [s] Relative
base secp_integration_benchmark 1.925 ± 0.023 1.895 1.966 1.01 ± 0.01
head secp_integration_benchmark 1.914 ± 0.009 1.901 1.933 1.00
Command Mean [ms] Min [ms] Max [ms] Relative
base set_integration_benchmark 646.3 ± 4.3 642.1 655.1 1.00
head set_integration_benchmark 707.9 ± 8.9 692.8 719.4 1.10 ± 0.02
Command Mean [s] Min [s] Max [s] Relative
base uint256_integration_benchmark 4.541 ± 0.036 4.478 4.579 1.02 ± 0.01
head uint256_integration_benchmark 4.456 ± 0.026 4.422 4.500 1.00

Copy link

codecov bot commented Feb 18, 2025

Codecov Report

Attention: Patch coverage is 99.09091% with 2 lines in your changes missing coverage. Please review.

Project coverage is 96.40%. Comparing base (7a97bed) to head (b1003a2).

Files with missing lines Patch % Lines
cairo-vm-cli/src/main.rs 50.00% 1 Missing ⚠️
cairo1-run/src/main.rs 75.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1960      +/-   ##
==========================================
+ Coverage   96.39%   96.40%   +0.01%     
==========================================
  Files         102      102              
  Lines       41566    41776     +210     
==========================================
+ Hits        40068    40276     +208     
- Misses       1498     1500       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@JulianGCalderon JulianGCalderon left a comment

Choose a reason for hiding this comment

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

Hi @FrancoGiachetta! I left you some comments.

Comment on lines +658 to +676
/// Relocates a `Relocatable` value, which represented by its
/// index and offset, according to a given segment offsets
fn relocate_value(
index: usize,
offset: usize,
segment_offsets: &Option<HashMap<usize, Relocatable>>,
) -> (usize, usize) {
if let Some(offsets) = segment_offsets {
return match offsets.get(&index) {
Some(relocatable) => (
relocatable.segment_index as usize,
relocatable.offset + offset,
),
None => (index, offset),
};
}

(index, offset)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The function only relocates the value if segment_offsets is not None, ignoring it otherwise. I think its better if we remove the Option and let the caller handle the default case (when there is no segment_offsets)

Copy link
Contributor Author

@FrancoGiachetta FrancoGiachetta Feb 18, 2025

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

That is true, but while we aim to be compatible with the python implementation, I don't think the internal implementation has to be exactly the same. We may change the implementation to better suit the language features, although I'm not 100% sure about it. What do you think @gabrielbosio?

Copy link
Contributor Author

@FrancoGiachetta FrancoGiachetta Feb 20, 2025

Choose a reason for hiding this comment

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

Ah I see your point. I wasn't talking about "inconsistent" in terms of implementation but rather in terms of the results. This function seems to have only one behavior. So if the default case isn't gonna change, isn't it better to let the function handle it all?

Copy link
Collaborator

Choose a reason for hiding this comment

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

If you want to use the language features you can do something like this:

fn relocate_value(
    index: usize,
    offset: usize,
    segment_offsets: Option<&HashMap<usize, Relocatable>>,
) -> (usize, usize) {
    segment_offsets
        .and_then(|offsets| offsets.get(&index))
        .and_then(|relocatable| {
            Some((
                relocatable.segment_index as usize,
                relocatable.offset + offset,
            ))
        })
        .unwrap_or((index, offset))
}

and call to_bytes this way:

self.memory.to_bytes(segment_offsets.as_ref())

You can remove the Option in the relocate_value argument but you would need to deal with it outside:

let (segment, offset) = if let Some(offsets) = seg_offsets {
    Self::relocate_value(*segment, *offset, offsets)
} else {
    (*segment, *offset)
};

I'm not sure if there's a cleaner way to deal with the Option value here. I would choose to deal with it inside the function for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Merge Cairo Pie extra segments into one segment
3 participants