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

Fix_no_trace_padding_flow_in_proof_mode #1909

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

YairVaknin-starkware
Copy link
Collaborator

@YairVaknin-starkware YairVaknin-starkware commented Jan 5, 2025

TITLE

Description

Avoid using safe_div_usize when disabling trace padding, and use ceil_div instead.

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.

This change is Reviewable

@YairVaknin-starkware YairVaknin-starkware force-pushed the yairv/fix_no_trace_padding_flow_in_proof_mode branch from d7fd54d to 1af457b Compare January 5, 2025 15:03
Copy link

github-actions bot commented Jan 5, 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 ± σ):     32.912 s ±  0.073 s    [User: 32.080 s, System: 0.829 s]
  Range (min … max):   32.860 s … 32.964 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 1
  Time (mean ± σ):     32.106 s ±  0.253 s    [User: 31.334 s, System: 0.769 s]
  Range (min … max):   31.927 s … 32.285 s    2 runs
 
Summary
  hyper_threading_pr threads: 1 ran
    1.03 ± 0.01 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 ± σ):     18.155 s ±  0.066 s    [User: 31.732 s, System: 0.821 s]
  Range (min … max):   18.108 s … 18.201 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 2
  Time (mean ± σ):     17.766 s ±  0.011 s    [User: 31.045 s, System: 0.832 s]
  Range (min … max):   17.758 s … 17.774 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 ± σ):     12.290 s ±  0.054 s    [User: 43.982 s, System: 0.981 s]
  Range (min … max):   12.252 s … 12.328 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 4
  Time (mean ± σ):     12.277 s ±  0.185 s    [User: 43.757 s, System: 0.929 s]
  Range (min … max):   12.146 s … 12.407 s    2 runs
 
Summary
  hyper_threading_pr threads: 4 ran
    1.00 ± 0.02 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.865 s ±  0.158 s    [User: 44.342 s, System: 1.037 s]
  Range (min … max):   11.754 s … 11.977 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 6
  Time (mean ± σ):     12.589 s ±  0.080 s    [User: 44.138 s, System: 0.977 s]
  Range (min … max):   12.533 s … 12.645 s    2 runs
 
Summary
  hyper_threading_main threads: 6 ran
    1.06 ± 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 ± σ):     12.019 s ±  0.003 s    [User: 44.504 s, System: 1.056 s]
  Range (min … max):   12.017 s … 12.022 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 8
  Time (mean ± σ):     11.989 s ±  0.057 s    [User: 44.228 s, System: 1.042 s]
  Range (min … max):   11.949 s … 12.029 s    2 runs
 
Summary
  hyper_threading_pr threads: 8 ran
    1.00 ± 0.00 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 ± σ):     12.075 s ±  0.550 s    [User: 44.685 s, System: 1.086 s]
  Range (min … max):   11.685 s … 12.464 s    2 runs
 
Benchmark 2: hyper_threading_pr threads: 16
  Time (mean ± σ):     11.797 s ±  0.001 s    [User: 44.831 s, System: 1.115 s]
  Range (min … max):   11.797 s … 11.798 s    2 runs
 
Summary
  hyper_threading_pr threads: 16 ran
    1.02 ± 0.05 times faster than hyper_threading_main threads: 16


@YairVaknin-starkware YairVaknin-starkware force-pushed the yairv/fix_no_trace_padding_flow_in_proof_mode branch from 1af457b to 21171ad Compare January 5, 2025 15:19
Copy link

codecov bot commented Jan 5, 2025

Codecov Report

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

Project coverage is 96.35%. Comparing base (a0f8806) to head (41987f5).

Files with missing lines Patch % Lines
vm/src/cairo_run.rs 66.66% 1 Missing ⚠️
vm/src/vm/runners/builtin_runner/mod.rs 98.90% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1909   +/-   ##
=======================================
  Coverage   96.35%   96.35%           
=======================================
  Files         102      102           
  Lines       41005    41095   +90     
=======================================
+ Hits        39511    39599   +88     
- Misses       1494     1496    +2     

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

Copy link

github-actions bot commented Jan 5, 2025

Benchmark Results for unmodified programs 🚀

Command Mean [s] Min [s] Max [s] Relative
base big_factorial 2.570 ± 0.070 2.505 2.708 1.01 ± 0.03
head big_factorial 2.534 ± 0.046 2.470 2.614 1.00
Command Mean [s] Min [s] Max [s] Relative
base big_fibonacci 2.492 ± 0.029 2.454 2.537 1.00 ± 0.02
head big_fibonacci 2.487 ± 0.043 2.419 2.549 1.00
Command Mean [s] Min [s] Max [s] Relative
base blake2s_integration_benchmark 9.199 ± 0.081 9.075 9.335 1.00
head blake2s_integration_benchmark 9.207 ± 0.094 9.070 9.338 1.00 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base compare_arrays_200000 2.666 ± 0.085 2.589 2.818 1.02 ± 0.04
head compare_arrays_200000 2.610 ± 0.064 2.558 2.779 1.00
Command Mean [s] Min [s] Max [s] Relative
base dict_integration_benchmark 1.754 ± 0.063 1.682 1.874 1.01 ± 0.04
head dict_integration_benchmark 1.738 ± 0.039 1.674 1.789 1.00
Command Mean [s] Min [s] Max [s] Relative
base field_arithmetic_get_square_benchmark 1.441 ± 0.010 1.426 1.457 1.00
head field_arithmetic_get_square_benchmark 1.458 ± 0.028 1.427 1.527 1.01 ± 0.02
Command Mean [s] Min [s] Max [s] Relative
base integration_builtins 9.276 ± 0.163 9.112 9.679 1.01 ± 0.02
head integration_builtins 9.219 ± 0.143 9.089 9.472 1.00
Command Mean [s] Min [s] Max [s] Relative
base keccak_integration_benchmark 9.552 ± 0.321 9.382 10.451 1.00
head keccak_integration_benchmark 9.570 ± 0.078 9.464 9.671 1.00 ± 0.03
Command Mean [s] Min [s] Max [s] Relative
base linear_search 2.594 ± 0.033 2.552 2.671 1.00 ± 0.02
head linear_search 2.586 ± 0.045 2.535 2.660 1.00
Command Mean [s] Min [s] Max [s] Relative
base math_cmp_and_pow_integration_benchmark 1.758 ± 0.017 1.733 1.787 1.01 ± 0.01
head math_cmp_and_pow_integration_benchmark 1.749 ± 0.016 1.722 1.779 1.00
Command Mean [s] Min [s] Max [s] Relative
base math_integration_benchmark 1.691 ± 0.012 1.675 1.709 1.00
head math_integration_benchmark 1.694 ± 0.021 1.672 1.734 1.00 ± 0.01
Command Mean [s] Min [s] Max [s] Relative
base memory_integration_benchmark 1.438 ± 0.013 1.419 1.454 1.00
head memory_integration_benchmark 1.451 ± 0.045 1.412 1.560 1.01 ± 0.03
Command Mean [s] Min [s] Max [s] Relative
base operations_with_data_structures_benchmarks 1.827 ± 0.031 1.807 1.912 1.01 ± 0.02
head operations_with_data_structures_benchmarks 1.812 ± 0.027 1.792 1.883 1.00
Command Mean [ms] Min [ms] Max [ms] Relative
base pedersen 592.5 ± 8.6 583.3 610.2 1.01 ± 0.02
head pedersen 585.8 ± 8.0 576.3 598.4 1.00
Command Mean [ms] Min [ms] Max [ms] Relative
base poseidon_integration_benchmark 715.6 ± 8.7 706.1 732.2 1.01 ± 0.01
head poseidon_integration_benchmark 711.4 ± 4.3 707.8 721.1 1.00
Command Mean [s] Min [s] Max [s] Relative
base secp_integration_benchmark 2.081 ± 0.017 2.067 2.108 1.00 ± 0.02
head secp_integration_benchmark 2.077 ± 0.041 2.046 2.184 1.00
Command Mean [ms] Min [ms] Max [ms] Relative
base set_integration_benchmark 725.9 ± 7.8 710.0 738.0 1.00
head set_integration_benchmark 729.7 ± 16.8 710.3 761.9 1.01 ± 0.03
Command Mean [s] Min [s] Max [s] Relative
base uint256_integration_benchmark 5.196 ± 0.192 5.016 5.602 1.02 ± 0.04
head uint256_integration_benchmark 5.075 ± 0.084 4.992 5.227 1.00

@YairVaknin-starkware YairVaknin-starkware marked this pull request as ready for review January 5, 2025 16:18
@YairVaknin-starkware YairVaknin-starkware force-pushed the yairv/fix_no_trace_padding_flow_in_proof_mode branch 6 times, most recently from 3036bc4 to 6cd85de Compare January 6, 2025 11:17
Copy link
Collaborator

@yuvalsw yuvalsw left a comment

Choose a reason for hiding this comment

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

Reviewed 14 of 14 files at r1, 1 of 1 files at r2, all commit messages.
Reviewable status: all files reviewed, 5 unresolved discussions (waiting on @fmoletta, @gabrielbosio, @igaray, @juanbono, @OmriEshhar1, @Oppen, @pefontana, and @YairVaknin-starkware)


vm/src/vm/runners/builtin_runner/mod.rs line 202 at r2 (raw file):

                    Some(0) => Ok(0),
                    Some(ratio) => {
                        if !vm.disable_trace_padding {

please explain (doc) why it isn't relevant with disable_trace_padding


vm/src/vm/runners/builtin_runner/mod.rs line 919 at r2 (raw file):

        // Ensure the last step is not a power of two - true for this specific program, not always.
        assert!(!last_step_true.is_power_of_two());

if you know the exact number, maybe best to assert it's that number. (but do comment that the important thing here is to assert it did not round to a power of 2).
Same above - you can check the exact power of 2 you expect.


vm/src/vm/runners/builtin_runner/mod.rs line 929 at r2 (raw file):

        for (builtin_runner_false, builtin_runner_true) in builtin_runners_false
            .iter()
            .zip(builtin_runners_true.iter())

consider using zip_eq
This isn't checking everything if they are not of the same length.


vm/src/vm/runners/builtin_runner/mod.rs line 222 at r1 (raw file):

                        let allocated_instances = if vm.disable_trace_padding {
                            div_ceil(numerator, ratio as usize)

Please add doc saying why it's ok that it's not accurate (not used, right?) and why it's related to no padding.


vm/src/vm/runners/builtin_runner/mod.rs line 228 at r1 (raw file):

                        };

                        Ok(allocated_instances)

suggestion

Suggestion:

                        let ratio_den = self.ratio_den().or(1);
                        let numerator = vm.current_step * ratio_den as usize;

                        let allocated_instances = if vm.disable_trace_padding {
                            div_ceil(numerator, ratio as usize)
                        } else {
                            safe_div_usize(numerator, ratio as usize)
                                .map_err(|_| MemoryError::ErrorCalculatingMemoryUnits)?
                        };

                        Ok(allocated_instances)

@YairVaknin-starkware YairVaknin-starkware force-pushed the yairv/fix_no_trace_padding_flow_in_proof_mode branch from 6cd85de to 0e23543 Compare January 12, 2025 14:32
Copy link
Collaborator Author

@YairVaknin-starkware YairVaknin-starkware left a comment

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 4 unresolved discussions (waiting on @fmoletta, @gabrielbosio, @igaray, @juanbono, @OmriEshhar1, @Oppen, @pefontana, and @yuvalsw)


vm/src/vm/runners/builtin_runner/mod.rs line 222 at r1 (raw file):

Previously, yuvalsw wrote…

Please add doc saying why it's ok that it's not accurate (not used, right?) and why it's related to no padding.

Done


vm/src/vm/runners/builtin_runner/mod.rs line 228 at r1 (raw file):

Previously, yuvalsw wrote…

suggestion

Done


vm/src/vm/runners/builtin_runner/mod.rs line 202 at r2 (raw file):

Previously, yuvalsw wrote…

please explain (doc) why it isn't relevant with disable_trace_padding

Done


vm/src/vm/runners/builtin_runner/mod.rs line 919 at r2 (raw file):

Previously, yuvalsw wrote…

if you know the exact number, maybe best to assert it's that number. (but do comment that the important thing here is to assert it did not round to a power of 2).
Same above - you can check the exact power of 2 you expect.

I think I prefer it the way it is now. How strongly do you feel about changing it?


vm/src/vm/runners/builtin_runner/mod.rs line 929 at r2 (raw file):

Previously, yuvalsw wrote…

consider using zip_eq
This isn't checking everything if they are not of the same length.

They don't import the needed crate in cargo.toml, and would like to avoid adding to it.
So just added an assertion they are of equal lengths.

Copy link
Collaborator

@yuvalsw yuvalsw left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r3, all commit messages.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @fmoletta, @gabrielbosio, @igaray, @juanbono, @OmriEshhar1, @Oppen, @pefontana, and @YairVaknin-starkware)


vm/src/vm/runners/builtin_runner/mod.rs line 222 at r1 (raw file):

Previously, YairVaknin-starkware wrote…

Done

also "saying why it's ok that it's not accurate" please


vm/src/vm/runners/builtin_runner/mod.rs line 919 at r2 (raw file):

Previously, YairVaknin-starkware wrote…

I think I prefer it the way it is now. How strongly do you feel about changing it?

Not super strong. Just generally - tests should have less code and be more specific so that you can be more certain their code itself is good (so they test what you want).


vm/src/vm/runners/builtin_runner/mod.rs line 205 at r3 (raw file):

                            // if we disable trace padding, we don't need to check the min_step,
                            // since we won't have the guarantee of number of steps being padded to
                            // accommodate each builtin size.

please move to above the if (as it refers to what doesn't happen in the if...).

Code quote:

                            // if we disable trace padding, we don't need to check the min_step,
                            // since we won't have the guarantee of number of steps being padded to
                            // accommodate each builtin size.

@YairVaknin-starkware YairVaknin-starkware force-pushed the yairv/fix_no_trace_padding_flow_in_proof_mode branch from 0e23543 to 29a302a Compare January 13, 2025 08:24
Copy link
Collaborator Author

@YairVaknin-starkware YairVaknin-starkware left a comment

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @fmoletta, @gabrielbosio, @igaray, @juanbono, @OmriEshhar1, @Oppen, @pefontana, and @yuvalsw)


vm/src/vm/runners/builtin_runner/mod.rs line 222 at r1 (raw file):

Previously, yuvalsw wrote…

also "saying why it's ok that it's not accurate" please

Done.


vm/src/vm/runners/builtin_runner/mod.rs line 919 at r2 (raw file):

Previously, yuvalsw wrote…

Not super strong. Just generally - tests should have less code and be more specific so that you can be more certain their code itself is good (so they test what you want).

I agree. And here this is the thing we want to test.

@gabrielbosio
Copy link
Collaborator

Please provide more context in the description of the PR. For example, Is this change a bugfix of the VM or an improvement for a specific use? This is important because changing to div_ceil would imply that get_allocated_instances would not return an error in some cases where main would do, making callers of this function to work different.

@YairVaknin-starkware
Copy link
Collaborator Author

Please provide more context in the description of the PR. For example, Is this change a bugfix of the VM or an improvement for a specific use? This is important because changing to div_ceil would imply that get_allocated_instances would not return an error in some cases where main would do, making callers of this function to work different.

Hi, so we would like it to not fail the division when trace padding is disabled, since it won't be a multiple of the number of steps in this case. This check is only done in proof_mode (in finalize_segments), and most of the time it will throw an error when we disable trace padding, which would not let us complete the non padded run.

@YairVaknin-starkware YairVaknin-starkware force-pushed the yairv/fix_no_trace_padding_flow_in_proof_mode branch from 29a302a to d6d42f5 Compare January 14, 2025 09:07
Copy link
Collaborator

@yuvalsw yuvalsw left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r5, all commit messages.
Reviewable status: all files reviewed, 2 unresolved discussions (waiting on @fmoletta, @FrancoGiachetta, @gabrielbosio, @igaray, @juanbono, @OmriEshhar1, @Oppen, @pefontana, and @YairVaknin-starkware)


a discussion (no related file):
Blocking for now as we need to understand if the other logic we talked about overrides this one or lives along it. If it overrides, then maybe no need to merge this one. You can change this PR to the new logic or send a different one, as you see fit.

CHANGELOG.md Outdated
@@ -2,6 +2,8 @@

#### Upcoming Changes

* fix: Fix no trace padding flow in proof mode [#1909](https://github.com/lambdaclass/cairo-vm/pull/1909)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is an API breaking change. It must be documented as such.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

@YairVaknin-starkware YairVaknin-starkware force-pushed the yairv/fix_no_trace_padding_flow_in_proof_mode branch from d6d42f5 to 23e6312 Compare January 19, 2025 11:55
Copy link
Collaborator Author

@YairVaknin-starkware YairVaknin-starkware left a comment

Choose a reason for hiding this comment

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

Reviewable status: 13 of 14 files reviewed, 3 unresolved discussions (waiting on @fmoletta, @FrancoGiachetta, @gabrielbosio, @igaray, @juanbono, @OmriEshhar1, @pefontana, and @yuvalsw)


a discussion (no related file):

Previously, yuvalsw wrote…

Blocking for now as we need to understand if the other logic we talked about overrides this one or lives along it. If it overrides, then maybe no need to merge this one. You can change this PR to the new logic or send a different one, as you see fit.

Changed to a different logic as discussed

Copy link
Collaborator

@yuvalsw yuvalsw left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r6, all commit messages.
Reviewable status: all files reviewed, 10 unresolved discussions (waiting on @fmoletta, @FrancoGiachetta, @gabrielbosio, @igaray, @juanbono, @OmriEshhar1, @pefontana, and @YairVaknin-starkware)


vm/src/vm/runners/builtin_runner/mod.rs line 223 at r6 (raw file):

                                .map_err(|_| MemoryError::ErrorCalculatingMemoryUnits)?
                        };

please revert this newline (just to avoid having traces here while you didn't actually change anything here).


vm/src/vm/runners/builtin_runner/mod.rs line 538 at r6 (raw file):

                let used_cells = self.get_used_cells(&vm.segments)?;
                if vm.disable_trace_padding {
                    // If trace padding is disabled, we pad the used cells to still ensure that the

Sorry for nagging, but can you please doc the effects of disable_trace_padding in its definition? I'd like to avoid the case of proof_mode in which no one knows what exactly it does and thus are worried changing/touching it.


vm/src/vm/runners/builtin_runner/mod.rs line 545 at r6 (raw file):

                    } else {
                        0
                    };

assert that padded_used_cells >= used_cells?
Or maybe even compute the used_cells as used_instances * cells_per_instance, and assert it is equal to used_cells?
The computation seems messy and I am concerned it may have inconsistencies.
Or maybe just compute it that way, and convert the assert with a test (asserting both ways of computation yield the same numbers, for all the builtins).


vm/src/vm/runners/builtin_runner/mod.rs line 935 at r6 (raw file):

        for (builtin_runner_false, builtin_runner_true) in builtin_runners_false
            .iter()
            .zip(builtin_runners_true.iter())

this assumes they are in the same order. Makes sense, but consider documenting it.

Code quote:

        for (builtin_runner_false, builtin_runner_true) in builtin_runners_false
            .iter()
            .zip(builtin_runners_true.iter())

vm/src/vm/runners/builtin_runner/mod.rs line 945 at r6 (raw file):

            let (_, allocated_size_false) = builtin_runner_false
                .get_used_cells_and_allocated_size(&runner_false.vm)
                .unwrap_or((0, 0));

any reason this should be Err? If not, just unwrap should do better.

Code quote:

unwrap_or((0, 0));

vm/src/vm/runners/builtin_runner/mod.rs line 954 at r6 (raw file):

            )
            .unwrap();
            let allocated_instances_true = safe_div_usize(

Suggestion:

            let n_allocated_instances_false = safe_div_usize(
                allocated_size_false,
                builtin_runner_false.cells_per_instance() as usize,
            )
            .unwrap();
            let n_allocated_instances_true = safe_div_usize(

vm/src/vm/runners/builtin_runner/mod.rs line 966 at r6 (raw file):

                allocated_instances_true == 0
                    || allocated_instances_true != allocated_instances_false
            );

unclear. What does it check?

Code quote:

            // True for this specific program, not always. That is, instance could could happen to
            // be the same.
            assert!(
                allocated_instances_true == 0
                    || allocated_instances_true != allocated_instances_false
            );

vm/src/vm/runners/builtin_runner/mod.rs line 223 at r5 (raw file):

                        let numerator = vm.current_step * ratio_den as usize;

                        let allocated_instances = if vm.disable_trace_padding {

How come this is not needed? Aren't the safe_divs failing? Ratio is still irrelevant in the case of disable_trace_padding.

Copy link
Collaborator Author

@YairVaknin-starkware YairVaknin-starkware left a comment

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 9 unresolved discussions (waiting on @fmoletta, @FrancoGiachetta, @gabrielbosio, @igaray, @juanbono, @OmriEshhar1, @pefontana, and @yuvalsw)


vm/src/vm/runners/builtin_runner/mod.rs line 223 at r5 (raw file):

Previously, yuvalsw wrote…

How come this is not needed? Aren't the safe_divs failing? Ratio is still irrelevant in the case of disable_trace_padding.

We don't reach here when disabling trace padding. TAL at fn get_used_cells_and_allocated_size.


vm/src/vm/runners/builtin_runner/mod.rs line 223 at r6 (raw file):

Previously, yuvalsw wrote…

please revert this newline (just to avoid having traces here while you didn't actually change anything here).

Done.


vm/src/vm/runners/builtin_runner/mod.rs line 538 at r6 (raw file):

Previously, yuvalsw wrote…

Sorry for nagging, but can you please doc the effects of disable_trace_padding in its definition? I'd like to avoid the case of proof_mode in which no one knows what exactly it does and thus are worried changing/touching it.

it's defined in the config of the runner, then passed to the vm. In which struct would you like it documented (or both)? Also, not sure I get you remark regarding proof_mode.


vm/src/vm/runners/builtin_runner/mod.rs line 545 at r6 (raw file):

Previously, yuvalsw wrote…

assert that padded_used_cells >= used_cells?
Or maybe even compute the used_cells as used_instances * cells_per_instance, and assert it is equal to used_cells?
The computation seems messy and I am concerned it may have inconsistencies.
Or maybe just compute it that way, and convert the assert with a test (asserting both ways of computation yield the same numbers, for all the builtins).

the >= check is done implicitly. get_used_instances just does div_ceil(used_cells, cells_per_instance()). Also, the second remark isn't always true for the last instance (some outputs might not be filled).


vm/src/vm/runners/builtin_runner/mod.rs line 935 at r6 (raw file):

Previously, yuvalsw wrote…

this assumes they are in the same order. Makes sense, but consider documenting it.

Yeah, it could be assumed, but you're right that it's much clearer if state explicitly. I added an assert at the start of the loop that that's the case.


vm/src/vm/runners/builtin_runner/mod.rs line 945 at r6 (raw file):

Previously, yuvalsw wrote…

any reason this should be Err? If not, just unwrap should do better.

Done.


vm/src/vm/runners/builtin_runner/mod.rs line 966 at r6 (raw file):

Previously, yuvalsw wrote…

unclear. What does it check?

That there was indeed a different outcome in the number of allocated instances (basically, that true didn't pad according to the ratio)

@YairVaknin-starkware YairVaknin-starkware force-pushed the yairv/fix_no_trace_padding_flow_in_proof_mode branch from 23e6312 to a8a42d8 Compare January 19, 2025 15:49
Copy link
Collaborator

@yuvalsw yuvalsw left a comment

Choose a reason for hiding this comment

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

Reviewed 1 of 1 files at r7, all commit messages.
Reviewable status: all files reviewed, 5 unresolved discussions (waiting on @fmoletta, @FrancoGiachetta, @gabrielbosio, @igaray, @juanbono, @OmriEshhar1, @pefontana, and @YairVaknin-starkware)


vm/src/vm/runners/builtin_runner/mod.rs line 538 at r6 (raw file):

Previously, YairVaknin-starkware wrote…

it's defined in the config of the runner, then passed to the vm. In which struct would you like it documented (or both)? Also, not sure I get you remark regarding proof_mode.

Both. I'd write it in the outer one (runner), and in the vm just refer to it for more details.

re proof_mode - it has many effects, it got to the state where it's hard to tell exactly what they are. I don't want disable_trace_padding to get to the same state.


vm/src/vm/runners/builtin_runner/mod.rs line 967 at r7 (raw file):

            // Checks that the number of allocated instances is diffrent when trace padding is
            // disabled. True for this specific program, not always. That is, instance could could
            // happen to be the same.

clearer than before. My suggestion for more clarity:

Suggestion:

            // Checks that the number of allocated instances is diffrent when trace padding is
            // enabled/disabled. Holds for this specific program, not always (that is, in other programs, padding may be of size 0).

@YairVaknin-starkware YairVaknin-starkware force-pushed the yairv/fix_no_trace_padding_flow_in_proof_mode branch from a8a42d8 to a79d35a Compare January 20, 2025 13:12
Copy link
Collaborator Author

@YairVaknin-starkware YairVaknin-starkware left a comment

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 5 unresolved discussions (waiting on @fmoletta, @FrancoGiachetta, @gabrielbosio, @igaray, @juanbono, @OmriEshhar1, @pefontana, and @yuvalsw)


vm/src/vm/runners/builtin_runner/mod.rs line 538 at r6 (raw file):

Previously, yuvalsw wrote…

Both. I'd write it in the outer one (runner), and in the vm just refer to it for more details.

re proof_mode - it has many effects, it got to the state where it's hard to tell exactly what they are. I don't want disable_trace_padding to get to the same state.

Done.


vm/src/vm/runners/builtin_runner/mod.rs line 967 at r7 (raw file):

Previously, yuvalsw wrote…

clearer than before. My suggestion for more clarity:

Done.

@YairVaknin-starkware YairVaknin-starkware force-pushed the yairv/fix_no_trace_padding_flow_in_proof_mode branch from a79d35a to 29d1d44 Compare January 20, 2025 13:17
Copy link
Collaborator Author

@YairVaknin-starkware YairVaknin-starkware left a comment

Choose a reason for hiding this comment

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

Reviewable status: 10 of 14 files reviewed, 5 unresolved discussions (waiting on @fmoletta, @FrancoGiachetta, @gabrielbosio, @igaray, @juanbono, @OmriEshhar1, @Oppen, @pefontana, and @yuvalsw)

CHANGELOG.md Outdated
@@ -2,6 +2,8 @@

#### Upcoming Changes

* fix: Fix no trace padding flow in proof mode [#1909](https://github.com/lambdaclass/cairo-vm/pull/1909)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done.

@yuvalsw
Copy link
Collaborator

yuvalsw commented Jan 20, 2025

vm/src/vm/runners/builtin_runner/mod.rs line 545 at r6 (raw file):

Previously, YairVaknin-starkware wrote…

the >= check is done implicitly. get_used_instances just does div_ceil(used_cells, cells_per_instance()). Also, the second remark isn't always true for the last instance (some outputs might not be filled).

Then, as discussed, maybe assert (in the test) that (something like) get_used_instances * cells_per_instance - get_used_cells < cells_per_instance.

Copy link
Collaborator

@yuvalsw yuvalsw left a comment

Choose a reason for hiding this comment

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

Reviewed 3 of 3 files at r8, 1 of 1 files at r9, all commit messages.
Reviewable status: all files reviewed, 4 unresolved discussions (waiting on @fmoletta, @FrancoGiachetta, @gabrielbosio, @igaray, @juanbono, @OmriEshhar1, @Oppen, @pefontana, and @YairVaknin-starkware)


vm/src/cairo_run.rs line 38 at r9 (raw file):

    pub secure_run: Option<bool>,
    // disable padding of the trace to accommodate the expected ratios/n_steps relationships w.r.t
    // the layout.

Please mention how. It doesn't pad steps at all, but does pad segments to a power of 2...

Suggestion:

    // Disable padding of the trace to accommodate the expected ratios/n_steps relationships w.r.t
    // the layout.

@YairVaknin-starkware YairVaknin-starkware force-pushed the yairv/fix_no_trace_padding_flow_in_proof_mode branch from 29d1d44 to c9b8416 Compare January 20, 2025 16:15
Copy link
Collaborator Author

@YairVaknin-starkware YairVaknin-starkware left a comment

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @fmoletta, @FrancoGiachetta, @gabrielbosio, @igaray, @juanbono, @OmriEshhar1, @Oppen, @pefontana, and @yuvalsw)


vm/src/vm/runners/builtin_runner/mod.rs line 545 at r6 (raw file):

Previously, yuvalsw wrote…

Then, as discussed, maybe assert (in the test) that (something like) get_used_instances * cells_per_instance - get_used_cells < cells_per_instance.

Done.

Copy link
Collaborator

@yuvalsw yuvalsw left a comment

Choose a reason for hiding this comment

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

Reviewed 2 of 2 files at r10, all commit messages.
Reviewable status: all files reviewed, 4 unresolved discussions (waiting on @fmoletta, @FrancoGiachetta, @gabrielbosio, @igaray, @juanbono, @OmriEshhar1, @Oppen, @pefontana, and @YairVaknin-starkware)


vm/src/cairo_run.rs line 39 at r10 (raw file):

    // Disable padding of the trace to accommodate the expected ratios-n_steps relationships w.r.t
    // the layout. It does, however, pad the number of builtin instances to the next power of 2
    // compared to their values at the end of the execution, but doesn't modify n_steps.

It confuses with double negations. My suggestion:

Suggestion:

    /// Disable padding of the trace.
    /// By default, the trace is padded to accommodate the expected builtins-n_steps relationships according to
    /// the layout.
    /// When the padding is disabled:
    /// - It doesn't modify/pad n_steps.
    /// - It still pads each builtin segment to the next power of 2 (counting instances of the builtin) compared to their sizes at the end of the execution.

vm/src/vm/runners/builtin_runner/mod.rs line 543 at r10 (raw file):

                        let n_output_cells =
                            self.cells_per_instance() as usize - self.n_input_cells() as usize;
                        assert!(
  1. Please doc what you explained to me f2f - about why it is not exactly equal.
  2. why not assert in a test?
  3. can the >= become >? Shouldn't there be at least one full output cell ? (not super important of course)

@YairVaknin-starkware YairVaknin-starkware force-pushed the yairv/fix_no_trace_padding_flow_in_proof_mode branch from c9b8416 to 7c8c978 Compare January 21, 2025 18:14
Copy link
Collaborator Author

@YairVaknin-starkware YairVaknin-starkware left a comment

Choose a reason for hiding this comment

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

Reviewable status: all files reviewed, 4 unresolved discussions (waiting on @fmoletta, @FrancoGiachetta, @gabrielbosio, @igaray, @juanbono, @OmriEshhar1, @Oppen, @pefontana, and @yuvalsw)


vm/src/cairo_run.rs line 39 at r10 (raw file):

Previously, yuvalsw wrote…

It confuses with double negations. My suggestion:

Done.


vm/src/vm/runners/builtin_runner/mod.rs line 543 at r10 (raw file):

Previously, yuvalsw wrote…
  1. Please doc what you explained to me f2f - about why it is not exactly equal.
  2. why not assert in a test?
  3. can the >= become >? Shouldn't there be at least one full output cell ? (not super important of course)
  1. Done.
  2. thought it couldn't hurt, but moved it to the test and made it more robust (more programs/more used builtins).
    3 . no, for range_check there's no input/output distinction.

Copy link
Collaborator

@yuvalsw yuvalsw left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewed 2 of 2 files at r11, all commit messages.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @fmoletta, @FrancoGiachetta, @gabrielbosio, @igaray, @juanbono, @OmriEshhar1, @Oppen, @pefontana, and @YairVaknin-starkware)


vm/src/vm/runners/builtin_runner/mod.rs line 985 at r11 (raw file):

                // the number of used_cells might not be a multiple of cells_per_instance, so we
                // make sure that the discrepancy is up to the number of output cells.
                // This is the same for both cases, so we only check one (true).

IMO wouldn't hurt to test both. It should be the same for both, and this is exactly why we should test this hold for both.

Code quote:

so we only check one (true).

@YairVaknin-starkware YairVaknin-starkware force-pushed the yairv/fix_no_trace_padding_flow_in_proof_mode branch from 7c8c978 to 41987f5 Compare January 28, 2025 09:08
Copy link
Collaborator

@yuvalsw yuvalsw left a comment

Choose a reason for hiding this comment

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

Reviewed 2 of 2 files at r12, all commit messages.
Reviewable status: all files reviewed, 3 unresolved discussions (waiting on @fmoletta, @FrancoGiachetta, @gabrielbosio, @igaray, @juanbono, @OmriEshhar1, @Oppen, @pefontana, and @YairVaknin-starkware)

Comment on lines +42 to +43
/// - It still pads each builtin segment to the next power of 2 w.r.t the number of used
/// instances of the builtin) compared to their sizes at the end of the execution.
Copy link
Collaborator

Choose a reason for hiding this comment

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

In line 43 there's only a closing parenthesis but not the opening one

Copy link
Collaborator

@gabrielbosio gabrielbosio left a comment

Choose a reason for hiding this comment

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

Looks good, I've left a small comment

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.

5 participants