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

[Cairo 1] Replace hardcoded arguments with vm initialization #1751

Closed
wants to merge 45 commits into from

Conversation

fmoletta
Copy link
Contributor

@fmoletta fmoletta commented May 7, 2024

Cairo 0 programs don't allow input arguments (aka arguments to the main function). This is circumvented in cairo 1 by creating the arguments before calling the main function, by creating instructions that declare the input arguments as constants and allocate them as tempvars. This creates a "fake stack", with the builtin bases and the return_fp & return_pc and then a "real_stack" (used by main) with the builtin bases and the arguments.
An example of the execution segment after cairo 1 initialization (both VM & added instructions) would be:

[
  // fake stack starts here
  2:0 (range_check)
  3:0 (bitwise)
  4:0 (return_fp)
  5:0 (return_pc)
  // real stack starts here
  2:0 (range_check)
  3:0 (bitwise)
  17 (arg 0)
  67 (arg 1) 
]

While this solves the issue of cairo 0 not being compatible with arguments (as the initial_stack doesn't contain the arguments), it inserts the values of the arguments into the instructions, making the program hash vary depending on the arguments passed to the program.
This PR aims to solve this last program by creating both the initial (inputless) stack and the real stack in an initialization method (similar to the VM's initialize) and removing the instructions responsible for creating the real stack in create_entrypoint. The result of this changes should not modify cairo 1 executions, as the starting execution segment before calling the main function will remain the same

Other solutions previously explored: Removing create_entry_point altogether and using the normal cairo-vm initialization & segment_arena builtin runner: This lead to problems concerning CairoPie as segment_arena & argument creation disrupted the segment order required by cairo pie validations, and arguments were not expected in the initial_stack

Copy link

github-actions bot commented May 7, 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 ± σ):     27.125 s ±  0.011 s    [User: 26.331 s, System: 0.790 s]
  Range (min … max):   27.117 s … 27.132 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 1
  Time (mean ± σ):     27.119 s ±  0.034 s    [User: 26.279 s, System: 0.837 s]
  Range (min … max):   27.095 s … 27.143 s    2 runs
 
Summary
  'hyper_threading_pr threads: 1' ran
    1.00 ± 0.00 times faster than 'hyper_threading_main 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 ± σ):     14.610 s ±  0.011 s    [User: 26.946 s, System: 0.797 s]
  Range (min … max):   14.602 s … 14.618 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 2
  Time (mean ± σ):     14.355 s ±  0.029 s    [User: 26.352 s, System: 0.806 s]
  Range (min … max):   14.334 s … 14.375 s    2 runs
 
Summary
  'hyper_threading_pr threads: 2' ran
    1.02 ± 0.00 times faster than 'hyper_threading_main 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 ± σ):     10.967 s ±  0.260 s    [User: 38.781 s, System: 0.996 s]
  Range (min … max):   10.783 s … 11.151 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 4
  Time (mean ± σ):     10.970 s ±  0.330 s    [User: 38.516 s, System: 0.947 s]
  Range (min … max):   10.736 s … 11.203 s    2 runs
 
Summary
  'hyper_threading_main threads: 4' ran
    1.00 ± 0.04 times faster than 'hyper_threading_pr 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.732 s ±  0.239 s    [User: 39.096 s, System: 0.946 s]
  Range (min … max):   10.563 s … 10.901 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 6
  Time (mean ± σ):     10.948 s ±  0.010 s    [User: 38.642 s, System: 0.965 s]
  Range (min … max):   10.941 s … 10.955 s    2 runs
 
Summary
  'hyper_threading_main threads: 6' ran
    1.02 ± 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 ± σ):     10.610 s ±  0.134 s    [User: 39.196 s, System: 1.060 s]
  Range (min … max):   10.515 s … 10.704 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 8
  Time (mean ± σ):     10.555 s ±  0.118 s    [User: 38.930 s, System: 0.910 s]
  Range (min … max):   10.472 s … 10.639 s    2 runs
 
Summary
  'hyper_threading_pr threads: 8' ran
    1.01 ± 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 ± σ):     10.656 s ±  0.326 s    [User: 39.576 s, System: 0.990 s]
  Range (min … max):   10.425 s … 10.886 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 16
  Time (mean ± σ):     10.601 s ±  0.070 s    [User: 39.252 s, System: 1.002 s]
  Range (min … max):   10.551 s … 10.650 s    2 runs
 
Summary
  'hyper_threading_pr threads: 16' ran
    1.01 ± 0.03 times faster than 'hyper_threading_main threads: 16'


@fmoletta fmoletta changed the title [Cairo 1] Replace hardcoding arguments with vm initialization [Cairo 1] Replace hardcoded arguments with vm initialization May 7, 2024
Copy link

github-actions bot commented May 7, 2024

Benchmark Results for unmodified programs 🚀

Command Mean [s] Min [s] Max [s] Relative
base big_factorial 2.062 ± 0.011 2.050 2.088 1.00 ± 0.01
head big_factorial 2.056 ± 0.014 2.038 2.073 1.00
Command Mean [s] Min [s] Max [s] Relative
base big_fibonacci 2.027 ± 0.035 2.000 2.121 1.01 ± 0.02
head big_fibonacci 2.006 ± 0.020 1.979 2.040 1.00
Command Mean [s] Min [s] Max [s] Relative
base blake2s_integration_benchmark 7.622 ± 0.143 7.484 7.895 1.00
head blake2s_integration_benchmark 7.633 ± 0.162 7.450 7.998 1.00 ± 0.03
Command Mean [s] Min [s] Max [s] Relative
base compare_arrays_200000 2.159 ± 0.084 2.101 2.377 1.01 ± 0.04
head compare_arrays_200000 2.134 ± 0.035 2.093 2.221 1.00
Command Mean [s] Min [s] Max [s] Relative
base dict_integration_benchmark 1.407 ± 0.005 1.400 1.413 1.00
head dict_integration_benchmark 1.423 ± 0.007 1.416 1.434 1.01 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base field_arithmetic_get_square_benchmark 1.280 ± 0.006 1.272 1.290 1.00
head field_arithmetic_get_square_benchmark 1.286 ± 0.012 1.270 1.304 1.00 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base integration_builtins 7.643 ± 0.082 7.528 7.770 1.00 ± 0.02
head integration_builtins 7.637 ± 0.091 7.506 7.727 1.00
Command Mean [s] Min [s] Max [s] Relative
base keccak_integration_benchmark 8.054 ± 0.266 7.783 8.664 1.02 ± 0.04
head keccak_integration_benchmark 7.907 ± 0.085 7.754 7.982 1.00
Command Mean [s] Min [s] Max [s] Relative
base linear_search 2.095 ± 0.014 2.076 2.126 1.00
head linear_search 2.143 ± 0.045 2.103 2.258 1.02 ± 0.02
Command Mean [s] Min [s] Max [s] Relative
base math_cmp_and_pow_integration_benchmark 1.712 ± 0.061 1.678 1.878 1.01 ± 0.04
head math_cmp_and_pow_integration_benchmark 1.703 ± 0.010 1.693 1.729 1.00
Command Mean [s] Min [s] Max [s] Relative
base math_integration_benchmark 1.597 ± 0.005 1.588 1.605 1.00
head math_integration_benchmark 1.609 ± 0.027 1.586 1.680 1.01 ± 0.02
Command Mean [s] Min [s] Max [s] Relative
base memory_integration_benchmark 1.197 ± 0.005 1.190 1.206 1.00
head memory_integration_benchmark 1.197 ± 0.010 1.185 1.215 1.00 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base operations_with_data_structures_benchmarks 1.833 ± 0.054 1.797 1.983 1.01 ± 0.03
head operations_with_data_structures_benchmarks 1.813 ± 0.010 1.802 1.832 1.00
Command Mean [ms] Min [ms] Max [ms] Relative
base pedersen 514.4 ± 2.3 510.8 518.1 1.00
head pedersen 517.0 ± 7.4 512.0 537.0 1.01 ± 0.02
Command Mean [ms] Min [ms] Max [ms] Relative
base poseidon_integration_benchmark 954.2 ± 6.8 942.9 963.9 1.01 ± 0.01
head poseidon_integration_benchmark 948.6 ± 5.5 940.4 958.1 1.00
Command Mean [s] Min [s] Max [s] Relative
base secp_integration_benchmark 1.896 ± 0.049 1.855 2.006 1.03 ± 0.03
head secp_integration_benchmark 1.850 ± 0.024 1.830 1.912 1.00
Command Mean [ms] Min [ms] Max [ms] Relative
base set_integration_benchmark 638.4 ± 3.2 635.0 644.3 1.00
head set_integration_benchmark 639.6 ± 1.8 636.7 642.1 1.00 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base uint256_integration_benchmark 4.245 ± 0.040 4.161 4.296 1.00 ± 0.02
head uint256_integration_benchmark 4.235 ± 0.066 4.137 4.360 1.00

Copy link

codecov bot commented May 7, 2024

Codecov Report

Attention: Patch coverage is 99.54751% with 1 lines in your changes are missing coverage. Please review.

Project coverage is 94.76%. Comparing base (258dd84) to head (0896a0d).
Report is 6 commits behind head on main.

❗ Current head 0896a0d differs from pull request most recent head 6eb6768. Consider uploading reports for the commit 6eb6768 to get more accurate results

Files Patch % Lines
vm/src/vm/runners/cairo_runner.rs 97.67% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1751      +/-   ##
==========================================
- Coverage   94.76%   94.76%   -0.01%     
==========================================
  Files         101      101              
  Lines       38804    38915     +111     
==========================================
+ Hits        36773    36876     +103     
- Misses       2031     2039       +8     

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

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.

1 participant