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

Add dynamic layout basic support #1824

Merged
merged 76 commits into from
Sep 27, 2024
Merged

Add dynamic layout basic support #1824

merged 76 commits into from
Sep 27, 2024

Conversation

JulianGCalderon
Copy link
Contributor

@JulianGCalderon JulianGCalderon commented Aug 23, 2024

Add dynamic layout support

This PR doesn't change any logic in the VM, it only changes some signatures to allow for dynamic layout parameters. The missing functionality is being developed in #1838.

It also adds a new job to the CI for testing dynamic layouts in different scenarios.

I separated it this way to make reviewing easier.

Description

This PR adds support for dynamic layout. When using dynamic layout, the actual parameters of the layout must be specified through a path to a JSON file in the command line arguments:

cairo-compile cairo_programs/abs_value_array.cairo --output cairo_programs/abs_value_array_compiled.json

target/release/cairo-vm-cli cairo_programs/abs_value_array_compiled.json \
    --layout dynamic \
    --cairo_layout_params_file cairo_layout_params_file.example.json

Or, with Cairo 1:

cargo run --bin cairo1-run cairo_programs/cairo-1-programs/array_get.cairo \
    --layout dynamic \
    --cairo_layout_params_file cairo_layout_params_file.example.json

To add this functionality, the public API had to be changed. Now both CairoRunner::new and CairoRunner::new_v2 receive as their 3rd argument an Option<CairoLayoutParams>. This must be Some only when using dynamic layout. Most changes in this PR are a side effect of this modification.

impl CairoRunner {
    /// The `cairo_layout_params` argument should only be used with dynamic layout.
    /// It is ignored otherwise.
    pub fn new_v2(
        program: &Program,
        layout: LayoutName,
        cairo_layout_params: Option<CairoLayoutParams>,
        mode: RunnerMode,
        trace_enabled: bool,
    ) -> Result<CairoRunner, RunnerError> {

Even though this is not the most "rustic" solution, it was done this way to facilitate usage and minimize changes needed.

The JSON file has the following format. I left an example in the repository's root just in case, it can be moved or removed.

{
    "rc_units": 4,
    "log_diluted_units_per_step": 4,
    "cpu_component_step": 8,
    "memory_units_per_step": 8,
    "uses_pedersen_builtin": true,
    "pedersen_ratio": 256,
    "uses_range_check_builtin": true,
    "range_check_ratio": 8,
    "uses_ecdsa_builtin": true,
    "ecdsa_ratio": 2048,
    "uses_bitwise_builtin": true,
    "bitwise_ratio": 16,
    "uses_ec_op_builtin": true,
    "ec_op_ratio": 1024,
    "uses_keccak_builtin": true,
    "keccak_ratio": 2048,
    "uses_poseidon_builtin": true,
    "poseidon_ratio": 256,
    "uses_range_check96_builtin": true,
    "range_check96_ratio": 8,
    "range_check96_ratio_den": 1,
    "uses_add_mod_builtin": true,
    "add_mod_ratio": 128,
    "add_mod_ratio_den": 1,
    "uses_mul_mod_builtin": true,
    "mul_mod_ratio": 256,
    "mul_mod_ratio_den": 1
}

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 Aug 23, 2024

**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 ± σ):     30.160 s ±  0.160 s    [User: 29.397 s, System: 0.761 s]
  Range (min … max):   30.047 s … 30.273 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 1
  Time (mean ± σ):     30.293 s ±  0.100 s    [User: 29.579 s, System: 0.712 s]
  Range (min … max):   30.222 s … 30.363 s    2 runs
 
Summary
  'hyper_threading_main threads: 1' ran
    1.00 ± 0.01 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 ± σ):     16.820 s ±  0.052 s    [User: 29.575 s, System: 0.805 s]
  Range (min … max):   16.783 s … 16.856 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 2
  Time (mean ± σ):     16.872 s ±  0.010 s    [User: 29.833 s, System: 0.783 s]
  Range (min … max):   16.865 s … 16.879 s    2 runs
 
Summary
  'hyper_threading_main threads: 2' ran
    1.00 ± 0.00 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.791 s ±  0.533 s    [User: 41.132 s, System: 0.955 s]
  Range (min … max):   11.414 s … 12.168 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 4
  Time (mean ± σ):     11.719 s ±  0.285 s    [User: 41.396 s, System: 0.949 s]
  Range (min … max):   11.517 s … 11.921 s    2 runs
 
Summary
  'hyper_threading_pr threads: 4' ran
    1.01 ± 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 ± σ):     11.468 s ±  0.163 s    [User: 41.245 s, System: 0.976 s]
  Range (min … max):   11.352 s … 11.583 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 6
  Time (mean ± σ):     11.805 s ±  0.150 s    [User: 42.139 s, System: 0.937 s]
  Range (min … max):   11.699 s … 11.911 s    2 runs
 
Summary
  'hyper_threading_main threads: 6' ran
    1.03 ± 0.02 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 ± σ):     11.244 s ±  0.195 s    [User: 41.696 s, System: 0.944 s]
  Range (min … max):   11.106 s … 11.382 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 8
  Time (mean ± σ):     11.638 s ±  0.187 s    [User: 42.033 s, System: 0.957 s]
  Range (min … max):   11.506 s … 11.770 s    2 runs
 
Summary
  'hyper_threading_main threads: 8' ran
    1.04 ± 0.02 times faster than 'hyper_threading_pr 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.208 s ±  0.043 s    [User: 41.925 s, System: 1.064 s]
  Range (min … max):   11.177 s … 11.239 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 16
  Time (mean ± σ):     11.195 s ±  0.092 s    [User: 42.843 s, System: 1.056 s]
  Range (min … max):   11.130 s … 11.260 s    2 runs
 
Summary
  'hyper_threading_pr threads: 16' ran
    1.00 ± 0.01 times faster than 'hyper_threading_main threads: 16'


@JulianGCalderon JulianGCalderon force-pushed the dynamic-layout branch 2 times, most recently from 2ee787b to 315594d Compare August 23, 2024 21:24
Copy link

github-actions bot commented Aug 23, 2024

Benchmark Results for unmodified programs 🚀

Command Mean [s] Min [s] Max [s] Relative
base big_factorial 2.421 ± 0.034 2.393 2.481 1.01 ± 0.02
head big_factorial 2.407 ± 0.013 2.392 2.436 1.00
Command Mean [s] Min [s] Max [s] Relative
base big_fibonacci 2.386 ± 0.079 2.322 2.533 1.02 ± 0.04
head big_fibonacci 2.338 ± 0.025 2.319 2.398 1.00
Command Mean [s] Min [s] Max [s] Relative
base blake2s_integration_benchmark 8.757 ± 0.308 8.452 9.399 1.00
head blake2s_integration_benchmark 8.867 ± 0.550 8.507 10.327 1.01 ± 0.07
Command Mean [s] Min [s] Max [s] Relative
base compare_arrays_200000 2.515 ± 0.013 2.499 2.537 1.00
head compare_arrays_200000 2.580 ± 0.091 2.501 2.762 1.03 ± 0.04
Command Mean [s] Min [s] Max [s] Relative
base dict_integration_benchmark 1.673 ± 0.024 1.658 1.737 1.01 ± 0.02
head dict_integration_benchmark 1.650 ± 0.012 1.640 1.680 1.00
Command Mean [s] Min [s] Max [s] Relative
base field_arithmetic_get_square_benchmark 1.385 ± 0.018 1.360 1.410 1.00
head field_arithmetic_get_square_benchmark 1.405 ± 0.032 1.366 1.458 1.01 ± 0.03
Command Mean [s] Min [s] Max [s] Relative
base integration_builtins 8.928 ± 0.320 8.618 9.780 1.01 ± 0.05
head integration_builtins 8.800 ± 0.234 8.565 9.303 1.00
Command Mean [s] Min [s] Max [s] Relative
base keccak_integration_benchmark 9.096 ± 0.131 8.959 9.243 1.00
head keccak_integration_benchmark 9.167 ± 0.354 8.890 10.067 1.01 ± 0.04
Command Mean [s] Min [s] Max [s] Relative
base linear_search 2.499 ± 0.019 2.475 2.529 1.00 ± 0.01
head linear_search 2.498 ± 0.022 2.469 2.547 1.00
Command Mean [s] Min [s] Max [s] Relative
base math_cmp_and_pow_integration_benchmark 1.710 ± 0.006 1.703 1.724 1.00
head math_cmp_and_pow_integration_benchmark 1.711 ± 0.006 1.699 1.724 1.00 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base math_integration_benchmark 1.678 ± 0.009 1.665 1.698 1.00 ± 0.01
head math_integration_benchmark 1.678 ± 0.022 1.659 1.727 1.00
Command Mean [s] Min [s] Max [s] Relative
base memory_integration_benchmark 1.401 ± 0.011 1.391 1.422 1.00
head memory_integration_benchmark 1.419 ± 0.011 1.407 1.437 1.01 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base operations_with_data_structures_benchmarks 1.835 ± 0.057 1.783 1.961 1.01 ± 0.04
head operations_with_data_structures_benchmarks 1.808 ± 0.048 1.767 1.929 1.00
Command Mean [ms] Min [ms] Max [ms] Relative
base pedersen 578.0 ± 7.6 572.7 599.2 1.00 ± 0.01
head pedersen 576.9 ± 3.9 573.5 584.1 1.00
Command Mean [ms] Min [ms] Max [ms] Relative
base poseidon_integration_benchmark 703.5 ± 9.2 695.2 725.1 1.00 ± 0.01
head poseidon_integration_benchmark 703.3 ± 3.8 698.4 710.6 1.00
Command Mean [s] Min [s] Max [s] Relative
base secp_integration_benchmark 2.037 ± 0.039 2.001 2.112 1.02 ± 0.02
head secp_integration_benchmark 2.003 ± 0.013 1.985 2.023 1.00
Command Mean [ms] Min [ms] Max [ms] Relative
base set_integration_benchmark 694.8 ± 13.0 683.3 716.4 1.00 ± 0.03
head set_integration_benchmark 691.6 ± 11.9 682.7 723.9 1.00
Command Mean [s] Min [s] Max [s] Relative
base uint256_integration_benchmark 4.841 ± 0.097 4.745 5.040 1.00
head uint256_integration_benchmark 4.919 ± 0.149 4.754 5.180 1.02 ± 0.04

Copy link

codecov bot commented Aug 23, 2024

Codecov Report

Attention: Patch coverage is 88.62876% with 34 lines in your changes missing coverage. Please review.

Project coverage is 94.79%. Comparing base (63b4a55) to head (37d83fd).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
vm/src/types/layout.rs 83.41% 33 Missing ⚠️
vm/src/cairo_run.rs 75.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1824      +/-   ##
==========================================
- Coverage   94.83%   94.79%   -0.05%     
==========================================
  Files         101      101              
  Lines       39579    39836     +257     
==========================================
+ Hits        37536    37762     +226     
- Misses       2043     2074      +31     
Flag Coverage Δ
94.79% <88.62%> (-0.05%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

@JulianGCalderon JulianGCalderon changed the base branch from main to ensure-no_std-debug August 26, 2024 17:44
Base automatically changed from ensure-no_std-debug to main August 26, 2024 20:22
Copy link
Collaborator

@pefontana pefontana left a comment

Choose a reason for hiding this comment

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

Nice one @JulianGCalderon !
I really like the documentation
I just made some small comments an we will be good to go


# Build cases to execute
CASES=(
"cairo_programs/proof_programs/factorial.json;all_cairo"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you add 3 or 4 more programs 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.

Done

pub fn new_v2(
program: &Program,
layout: LayoutName,
cairo_layout_params: Option<CairoLayoutParams>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you change the name here to dynamic_layout_params, I think it is clearer

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -26,6 +29,7 @@ pub struct CairoRunConfig<'a> {
pub trace_enabled: bool,
pub relocate_mem: bool,
pub layout: LayoutName,
pub cairo_layout_params: Option<CairoLayoutParams>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

same here, I think dynamic_layout_params makes more clear to the user that is only needed in that cases
Also add a little comment here explaining like you did in the CairoRunner

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

vm/src/types/layout.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@fmoletta fmoletta left a comment

Choose a reason for hiding this comment

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

Could we update the Other CLI arguments section of the README?

@JulianGCalderon
Copy link
Contributor Author

@pefontana @fmoletta I made the requested changes.

Copy link
Collaborator

@pefontana pefontana left a comment

Choose a reason for hiding this comment

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

Nice one @JulianGCalderon !

@pefontana pefontana added this pull request to the merge queue Sep 27, 2024
Merged via the queue into main with commit 14ec3e3 Sep 27, 2024
72 of 73 checks passed
@pefontana pefontana deleted the dynamic-layout branch September 27, 2024 22:39
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.

6 participants