Skip to content

Add ExternalCodeSizeEstimator. #7698

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

Merged
merged 1 commit into from
May 14, 2025
Merged

Conversation

ilyalesokhin-starkware
Copy link
Contributor

No description provided.

@ilyalesokhin-starkware ilyalesokhin-starkware marked this pull request as draft April 24, 2025 07:57
@reviewable-StarkWare
Copy link

This change is Reviewable

Copy link
Collaborator

@orizi orizi 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 7 files at r1, all commit messages.
Reviewable status: 2 of 7 files reviewed, 2 unresolved discussions


crates/cairo-lang-lowering/src/inline/mod.rs line 95 at r1 (raw file):

    let weight_of_blocks = db.estimate_size(function_id)?;
    eprintln!("estimate_size: {}: {weight_of_blocks}", function_id.full_path(db));

remove print.


crates/cairo-lang-lowering/src/db.rs line 53 at r1 (raw file):

    fn get_db(&self) -> &dyn LoweringGroup;
}

Suggestion:

/// A trait for estimation of the code size of a function.
pub trait ExternalCodeSizeEstimator {
    /// Returns the extimated size of the function.
    fn estimate_size(&self, function_id: ConcreteFunctionWithBodyId) -> Maybe<isize>;
}

/// Marker trait for using the approximated casm inline weight method for size estimation.
pub trait UseApproxCodeSizeEstimator: Upcast<dyn LoweringGroup>;
impl<T: UseApproxCodeSizeEstimator + Sized?> ExternalCodeSizeEstimator for T {
    fn estimate_size(&self, function_id: ConcreteFunctionWithBodyId) -> Maybe<isize> {
        let db = self.upcast();
        let lowered = db.inlined_function_with_body_lowered(function_id)?;
        Ok(ApproxCasmInlineWeight::new(db, &lowered).lowered_weight(&lowered))
    }
}

@ilyalesokhin-starkware
Copy link
Contributor Author

crates/cairo-lang-lowering/src/db.rs line 53 at r1 (raw file):

    fn get_db(&self) -> &dyn LoweringGroup;
}

error[E0391]: cycle detected when computing the super predicates of db::LoweringGroup
--> crates/cairo-lang-lowering/src/db.rs:56:49
|
56 | SemanticGroup + Upcast + ExternalCodeSizeEstimator
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: ...which requires computing the super predicates of db::ExternalCodeSizeEstimator...
--> crates/cairo-lang-lowering/src/db.rs:44:1
|
44 | pub trait ExternalCodeSizeEstimator: Upcast {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: ...which again requires computing the super predicates of db::LoweringGroup, completing the cycle

Copy link
Collaborator

@orizi orizi 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: 2 of 7 files reviewed, 2 unresolved discussions (waiting on @ilyalesokhin-starkware)


crates/cairo-lang-lowering/src/db.rs line 53 at r1 (raw file):

Previously, ilyalesokhin-starkware wrote…

error[E0391]: cycle detected when computing the super predicates of db::LoweringGroup
--> crates/cairo-lang-lowering/src/db.rs:56:49
|
56 | SemanticGroup + Upcast + ExternalCodeSizeEstimator
| ^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: ...which requires computing the super predicates of db::ExternalCodeSizeEstimator...
--> crates/cairo-lang-lowering/src/db.rs:44:1
|
44 | pub trait ExternalCodeSizeEstimator: Upcast {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
= note: ...which again requires computing the super predicates of db::LoweringGroup, completing the cycle

What if you move the Upcast requirement from the trait, just for the impl (in addition to the marker)

@ilyalesokhin-starkware
Copy link
Contributor Author

crates/cairo-lang-lowering/src/db.rs line 53 at r1 (raw file):

Previously, orizi wrote…

What if you move the Upcast requirement from the trait, just for the impl (in addition to the marker)

I don't follow, where would I implement the default estimate_size?

@ilyalesokhin-starkware ilyalesokhin-starkware changed the base branch from main to graphite-base/7698 April 28, 2025 16:13
@ilyalesokhin-starkware ilyalesokhin-starkware changed the base branch from graphite-base/7698 to ilya/compile_func April 28, 2025 16:13
Copy link
Contributor Author

ilyalesokhin-starkware commented Apr 28, 2025

@ilyalesokhin-starkware
Copy link
Contributor Author

crates/cairo-lang-lowering/src/inline/mod.rs line 75 at r2 (raw file):

            /// The default threshold for inlining small functions. Decided according to sample
            /// contracts profiling.
            const DEFAULT_INLINE_SMALL_FUNCTIONS_THRESHOLD: usize = 55;

minimum size for the stacking contract is at 25, but it makes the gas costs in test_validate_gas_cost much higher:

Code quote (i):

 55;

Code snippet (ii):

   cairo_level_tests::abi_dispatchers_tests::test_validate_gas_cost - Panicked with "Unexpected gas_usage:
     call_building: `4950`.
     serialization: `40270`.
     entry_point: `173930`.".

Copy link
Contributor Author

@ilyalesokhin-starkware ilyalesokhin-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: 2 of 70 files reviewed, 3 unresolved discussions (waiting on @orizi)


crates/cairo-lang-lowering/src/inline/mod.rs line 95 at r1 (raw file):

Previously, orizi wrote…

remove print.

Done.

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

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

Reviewed all commit messages.
Reviewable status: 2 of 70 files reviewed, 3 unresolved discussions (waiting on @ilyalesokhin-starkware)


crates/cairo-lang-lowering/src/db.rs line 53 at r1 (raw file):

Previously, ilyalesokhin-starkware wrote…

I don't follow, where would I implement the default estimate_size?

in an impl that requests both ExternalCodeSizeEstimator and Upcast<dyn Lowering>.
ExternalCodeSizeEstimator won't require it.

@ilyalesokhin-starkware ilyalesokhin-starkware changed the base branch from ilya/compile_func to graphite-base/7698 April 29, 2025 07:30
@ilyalesokhin-starkware
Copy link
Contributor Author

crates/cairo-lang-lowering/src/db.rs line 53 at r1 (raw file):

Previously, orizi wrote…

in an impl that requests both ExternalCodeSizeEstimator and Upcast<dyn Lowering>.
ExternalCodeSizeEstimator won't require it.

but then I would have to write the default implementation in each impl.

Copy link
Collaborator

@orizi orizi 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.
Reviewable status: 3 of 70 files reviewed, 4 unresolved discussions (waiting on @ilyalesokhin-starkware)


crates/cairo-lang-lowering/src/db.rs line 53 at r1 (raw file):

Previously, ilyalesokhin-starkware wrote…

but then I would have to write the default implementation in each impl.

you just need a single implementation - i don't really understand.


crates/cairo-lang-compiler/src/db.rs line 59 at r3 (raw file):

        let casm = builder.casm_program();
        let total_size = casm.instructions.iter().map(|inst| inst.body.op_size()).sum::<usize>();
        Ok((total_size - (n_dummy_functions * 3)).try_into().unwrap_or(0))

can you doc the 3 * explanation? i don't understand this.

Code quote:

k((total_size - (n_dummy_functions * 3)).try_into().unwrap_or(0))

@ilyalesokhin-starkware
Copy link
Contributor Author

crates/cairo-lang-lowering/src/db.rs line 53 at r1 (raw file):

Previously, orizi wrote…

you just need a single implementation - i don't really understand.

There are 2 places where I use the default implementation

  1. LoweringDatabaseForTesting
  2. SierraGenDatabaseForTesting

@ilyalesokhin-starkware ilyalesokhin-starkware marked this pull request as ready for review May 7, 2025 06:58
@mkaput mkaput removed their request for review May 7, 2025 08:35
@ilyalesokhin-starkware ilyalesokhin-starkware changed the base branch from graphite-base/7698 to main May 7, 2025 09:34
Copy link
Collaborator

@orizi orizi 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 7 files at r1, 6 of 66 files at r2, 6 of 109 files at r4, all commit messages.
Reviewable status: 14 of 133 files reviewed, 3 unresolved discussions (waiting on @ilyalesokhin-starkware)


crates/cairo-lang-lowering/src/db.rs line 53 at r1 (raw file):

Previously, ilyalesokhin-starkware wrote…

There are 2 places where I use the default implementation

  1. LoweringDatabaseForTesting
  2. SierraGenDatabaseForTesting

f2f.


crates/cairo-lang-runner/src/profiling_test_data/major_test_cases line 394 at r5 (raw file):

  test::validate_call -> test::test_contract::__wrapper____validate__ -> core::array::deserialize_array_helper::<core::starknet::account::Call, core::starknet::account::CallSerde, core::starknet::account::CallDrop> -> core::array::deserialize_array_helper::<core::starknet::account::Call, core::starknet::account::CallSerde, core::starknet::account::CallDrop> -> core::array::deserialize_array_helper::<core::starknet::account::Call, core::starknet::account::CallSerde, core::starknet::account::CallDrop>: 14
Weight by Cairo stack trace:
  test::validate_call: 305

probably needs some more fine tuning of the default.

Code quote:

  test::validate_call: 305

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

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

Reviewed 69 of 70 files at r6, all commit messages.
Reviewable status: 80 of 133 files reviewed, 4 unresolved discussions (waiting on @ilyalesokhin-starkware)


crates/cairo-lang-lowering/src/db.rs line 51 at r6 (raw file):

}

// Marker trait for using an approximate code size estimator.

Suggestion:

/// Marker trait for using an approximate code size estimator.

crates/cairo-lang-lowering/src/db.rs line 55 at r6 (raw file):

impl<T: UseApproxCodeSizeEstimator> ExternalCodeSizeEstimator for T {
    /// Returns the virtual file matching the external id.

Copy link
Collaborator

@orizi orizi 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 66 files at r2, 1 of 70 files at r6.
Reviewable status: 82 of 133 files reviewed, 4 unresolved discussions (waiting on @ilyalesokhin-starkware)


crates/cairo-lang-lowering/src/inline/mod.rs line 75 at r2 (raw file):

Previously, ilyalesokhin-starkware wrote…

minimum size for the stacking contract is at 25, but it makes the gas costs in test_validate_gas_cost much higher:

should probably still make it much larger - look at the major_test_cases file.

Copy link
Contributor Author

@ilyalesokhin-starkware ilyalesokhin-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: 82 of 133 files reviewed, 4 unresolved discussions (waiting on @orizi)


crates/cairo-lang-lowering/src/db.rs line 51 at r6 (raw file):

}

// Marker trait for using an approximate code size estimator.

Done.


crates/cairo-lang-lowering/src/db.rs line 55 at r6 (raw file):

impl<T: UseApproxCodeSizeEstimator> ExternalCodeSizeEstimator for T {
    /// Returns the virtual file matching the external id.

Done.

@ilyalesokhin-starkware
Copy link
Contributor Author

crates/cairo-lang-runner/src/profiling_test_data/major_test_cases line 394 at r5 (raw file):

Previously, orizi wrote…

probably needs some more fine tuning of the default.

I had to increase the threshold to 120.
to get
test::validate_call: 268
here
and at the point the improvement in the statking contract is only

55616 -> 55109.

At 130.
I get back to 244, but the size of the staking contract increases rather than decreases.

Copy link
Collaborator

@orizi orizi 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: 82 of 133 files reviewed, 4 unresolved discussions (waiting on @ilyalesokhin-starkware)


crates/cairo-lang-lowering/src/db.rs line 51 at r6 (raw file):

Previously, ilyalesokhin-starkware wrote…

Done.

missing push probably

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

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

Reviewed 20 of 55 files at r7, all commit messages.
Reviewable status: 95 of 133 files reviewed, all discussions resolved


crates/cairo-lang-runner/src/profiling_test_data/major_test_cases line 394 at r5 (raw file):

Previously, ilyalesokhin-starkware wrote…

I had to increase the threshold to 120.
to get
test::validate_call: 268
here
and at the point the improvement in the statking contract is only

55616 -> 55109.

At 130.
I get back to 244, but the size of the staking contract increases rather than decreases.

probably after the consts thing smaller programs will improve.
we'd play more with the exact threshold later.

Copy link
Collaborator

@orizi orizi 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 109 files at r4, 35 of 55 files at r7.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @ilyalesokhin-starkware)

Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

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

:lgtm:

Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @ilyalesokhin-starkware)

@ilyalesokhin-starkware ilyalesokhin-starkware changed the base branch from main to graphite-base/7698 May 12, 2025 13:37
@ilyalesokhin-starkware ilyalesokhin-starkware changed the base branch from graphite-base/7698 to ilya/inline_cycle May 12, 2025 13:37
Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

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

Reviewed 49 of 49 files at r8, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @ilyalesokhin-starkware)

@ilyalesokhin-starkware ilyalesokhin-starkware changed the base branch from ilya/inline_cycle to graphite-base/7698 May 13, 2025 12:05
Copy link
Collaborator

@orizi orizi left a comment

Choose a reason for hiding this comment

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

Reviewed 59 of 59 files at r9, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @ilyalesokhin-starkware)

@ilyalesokhin-starkware ilyalesokhin-starkware changed the base branch from graphite-base/7698 to main May 14, 2025 07:05
Copy link
Collaborator

@orizi orizi 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: :shipit: complete! all files reviewed, all discussions resolved (waiting on @ilyalesokhin-starkware)

@ilyalesokhin-starkware ilyalesokhin-starkware added this pull request to the merge queue May 14, 2025
Merged via the queue into main with commit 0639e14 May 14, 2025
96 checks passed
@ilyalesokhin-starkware ilyalesokhin-starkware deleted the ilya/external_size branch May 14, 2025 08:48
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.

3 participants