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

feat(cairo_native): add a binary crate for cairo_native compilation #1945

Merged
merged 1 commit into from
Nov 14, 2024

Conversation

avi-starkware
Copy link
Contributor

No description provided.

@reviewable-StarkWare
Copy link

This change is Reviewable

Copy link

Artifacts upload triggered. View details here

@avi-starkware avi-starkware force-pushed the avi/add-starknet-native-compile-crate branch from 74ce4c4 to 66b14ea Compare November 11, 2024 13:34
@avi-starkware avi-starkware changed the title feature(cairo_native): add a binary crate for cairo_native compilation feat(cairo_native): add a binary crate for cairo_native compilation Nov 11, 2024
Copy link

Artifacts upload triggered. View details here

Copy link

Artifacts upload triggered. View details here

Copy link

codecov bot commented Nov 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 77.11%. Comparing base (e3165c4) to head (fa12a02).
Report is 391 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##             main    #1945       +/-   ##
===========================================
+ Coverage   40.10%   77.11%   +37.00%     
===========================================
  Files          26      373      +347     
  Lines        1895    39696    +37801     
  Branches     1895    39696    +37801     
===========================================
+ Hits          760    30610    +29850     
- Misses       1100     6808     +5708     
- Partials       35     2278     +2243     

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

@avi-starkware avi-starkware force-pushed the avi/add-starknet-native-compile-crate branch from 66b14ea to 32a4e79 Compare November 11, 2024 15:09
Copy link

Artifacts upload triggered. View details here

Copy link
Collaborator

@Yoni-Starkware Yoni-Starkware left a comment

Choose a reason for hiding this comment

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

Reviewed 4 of 9 files at r1, all commit messages.
Reviewable status: 4 of 9 files reviewed, 4 unresolved discussions (waiting on @avi-starkware and @noaov1)


crates/bin/starknet-native-compile/Cargo.toml line 13 at r2 (raw file):

cairo-native = "0.2.0-alpha.4"
clap = { version = "4.5.4", features = ["derive"] }
serde_json = "1.0.68"

Update and add a todo to check consistency with the blockifier

Code quote:

cairo-lang-sierra = "2.8.4"
cairo-lang-starknet-classes = "2.8.4"
cairo-native = "0.2.0-alpha.4"
clap = { version = "4.5.4", features = ["derive"] }
serde_json = "1.0.68"

crates/bin/starknet-native-compile/src/test.rs line 13 at r2 (raw file):

#[test]
fn test_save_and_load_contract() {

Add a negative test: a class that cannot be compiled


crates/bin/starknet-native-compile/src/main.rs line 24 at r2 (raw file):

const DEFAULT_OUTPUT_FILE: &str = "./output.so";

fn main() {

Add todos the restrict time and memory


crates/bin/starknet-native-compile/src/main.rs line 43 at r2 (raw file):

        process::exit(1);
    });
    println!("Compilation successful. Elapsed: {:?}", start.elapsed());

Move into a func and test the func

Code quote:

    let args = Args::parse();
    let path = args.path;
    let output = args.output.unwrap_or_else(|| DEFAULT_OUTPUT_FILE.to_string());

    println!("Loading Sierra program from file: {:?}", &path);
    let sierra_program = load_sierra_program_from_file(&path);

    println!("Compiling Sierra program into file {:?}", &output);
    let start = std::time::Instant::now();
    let mut contract_executor = AotContractExecutor::new(&sierra_program, OptLevel::default())
        .unwrap_or_else(|err| {
            eprintln!("Error compiling Sierra program: {}", err);
            process::exit(1);
        });
    contract_executor.save(output.clone()).unwrap_or_else(|err| {
        eprintln!("Error saving compiled program: {}", err);
        process::exit(1);
    });
    println!("Compilation successful. Elapsed: {:?}", start.elapsed());

Copy link
Collaborator

@Yoni-Starkware Yoni-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: 4 of 9 files reviewed, 5 unresolved discussions (waiting on @avi-starkware and @noaov1)


crates/bin/starknet-native-compile/test_files/faulty_account.sierra.json line 2 at r2 (raw file):

{
  "sierra_program": [

Take erc20 or account instead

Copy link
Collaborator

@Yoni-Starkware Yoni-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: 4 of 9 files reviewed, 6 unresolved discussions (waiting on @avi-starkware and @noaov1)


crates/bin/starknet-native-compile/src/test.rs line 10 at r2 (raw file):

const TEST_FILE: &str = "./test_files/faulty_account.sierra.json";
const TEST_OUTPUT: &str = "./target/test_output.so";

Better to remove this

Code quote:

const TEST_OUTPUT: &str = "./target/test_output.so";

Copy link
Collaborator

@Yoni-Starkware Yoni-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: 4 of 9 files reviewed, 6 unresolved discussions (waiting on @avi-starkware and @noaov1)


crates/bin/starknet-native-compile/src/test.rs line 10 at r2 (raw file):

Previously, Yoni-Starkware (Yoni) wrote…

Better to remove this

Try to be consistent with sierra to casm

@avi-starkware avi-starkware force-pushed the avi/add-starknet-native-compile-crate branch from 32a4e79 to a5e641d Compare November 12, 2024 15:26
Copy link

Artifacts upload triggered. View details here

Copy link
Contributor Author

@avi-starkware avi-starkware 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 9 files at r1, 3 of 3 files at r3, all commit messages.
Reviewable status: 7 of 9 files reviewed, 6 unresolved discussions (waiting on @noaov1 and @Yoni-Starkware)


crates/bin/starknet-native-compile/src/test.rs line 10 at r2 (raw file):

Previously, Yoni-Starkware (Yoni) wrote…

Try to be consistent with sierra to casm

I moved the testing to the crate that does the sierra to casm compilation in #1998


crates/bin/starknet-native-compile/src/test.rs line 13 at r2 (raw file):

Previously, Yoni-Starkware (Yoni) wrote…

Add a negative test: a class that cannot be compiled

Added in #1998

@avi-starkware avi-starkware force-pushed the avi/add-starknet-native-compile-crate branch from a5e641d to 580e598 Compare November 13, 2024 09:35
Copy link
Contributor Author

@avi-starkware avi-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: 4 of 9 files reviewed, 6 unresolved discussions (waiting on @noaov1 and @Yoni-Starkware)


crates/bin/starknet-native-compile/Cargo.toml line 13 at r2 (raw file):

Previously, Yoni-Starkware (Yoni) wrote…

Update and add a todo to check consistency with the blockifier

Done.


crates/bin/starknet-native-compile/src/main.rs line 24 at r2 (raw file):

Previously, Yoni-Starkware (Yoni) wrote…

Add todos the restrict time and memory

Done.


crates/bin/starknet-native-compile/src/main.rs line 43 at r2 (raw file):

Previously, Yoni-Starkware (Yoni) wrote…

Move into a func and test the func

The testing will be done through starknet_sierra_compile in #1998.


crates/bin/starknet-native-compile/test_files/faulty_account.sierra.json line 2 at r2 (raw file):

Previously, Yoni-Starkware (Yoni) wrote…

Take erc20 or account instead

I am using the test contract used in the crate starknet_sierra_compile.

Copy link

Artifacts upload triggered. View details here

Copy link
Collaborator

@Yoni-Starkware Yoni-Starkware 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 2 files at r2, 2 of 3 files at r4.
Reviewable status: 7 of 9 files reviewed, 7 unresolved discussions (waiting on @avi-starkware and @noaov1)


crates/bin/starknet-native-compile/src/main.rs line 20 at r4 (raw file):

}

const DEFAULT_OUTPUT_FILE: &str = "./output.so";

Remove the default value (no reason to do this so far)

Code quote:

    /// The output file name (default: output.so).
    output: Option<String>,
}

const DEFAULT_OUTPUT_FILE: &str = "./output.so";

crates/bin/starknet-native-compile/build.rs line 3 at r4 (raw file):

use std::path::Path;

// This build script sets the env var CAIRO_NATIVE_RUNTIME_LIBRARY to an absolute path to the runtime library.

Document why this is needed

Code quote:

// This build script sets the env var CAIRO_NATIVE_RUNTIME_LIBRARY to an absolute path to the runtime library.

crates/bin/starknet-native-compile/build.rs line 17 at r4 (raw file):

    let blockifier_path = out_dir
        .ancestors()
        .nth(3)

Document why 3

Code quote:

        .nth(3)

Copy link
Collaborator

@Yoni-Starkware Yoni-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: 7 of 9 files reviewed, 6 unresolved discussions (waiting on @avi-starkware and @noaov1)


crates/bin/starknet-native-compile/src/utils.rs line 11 at r4 (raw file):

    let raw_contract_class = std::fs::read_to_string(path).unwrap_or_else(|err| {
        eprintln!("Error reading Sierra file: {}", err);
        process::exit(1);

Is this the right way to go?
Why not returning Result throughout the code?
Can main return result?

Code quote:

  process::exit(1);

crates/bin/starknet-native-compile/src/main.rs line 28 at r4 (raw file):

    let output = args.output.unwrap_or_else(|| DEFAULT_OUTPUT_FILE.to_string());

    println!("Loading Sierra program from file: {:?}", &path);

We are not going to see these prints in the blockifier run, right?

Code quote:

println!("Loading Sierra program from file: {:?}", &path);

crates/bin/starknet-native-compile/src/main.rs line 33 at r4 (raw file):

    println!("Compiling Sierra program into file {:?}", &output);
    let start = std::time::Instant::now();
    let mut contract_executor = AotContractExecutor::new(&sierra_program, OptLevel::default())

What is this? what are the options? do we want to pass argument for it?

Code quote:

OptLevel::default()

Copy link

Artifacts upload triggered. View details here

@avi-starkware avi-starkware force-pushed the avi/add-starknet-native-compile-crate branch from 5cd6e57 to 264bf17 Compare November 13, 2024 17:49
Copy link

Artifacts upload triggered. View details here

Copy link

Benchmark movements:
tree_computation_flow performance improved 😺
tree_computation_flow time: [34.267 ms 34.296 ms 34.333 ms]
change: [-3.9123% -2.3773% -1.0425%] (p = 0.00 < 0.05)
Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
2 (2.00%) high mild
3 (3.00%) high severe

Copy link
Collaborator

@Yoni-Starkware Yoni-Starkware 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 2 files at r5, 1 of 2 files at r6.
Reviewable status: 6 of 9 files reviewed, 6 unresolved discussions (waiting on @avi-starkware and @noaov1)

@avi-starkware
Copy link
Contributor Author

crates/bin/starknet-native-compile/src/main.rs line 33 at r4 (raw file):

Previously, Yoni-Starkware (Yoni) wrote…

What is this? what are the options? do we want to pass argument for it?

These are the options.

It indicates the level of optimization the compiler should employ while compiling code. I am not sure what impact this config has, so I just put it on default. Nethermind did the same in their PRs.

The OptLevel is plugged into LLVM here.

According to ChatGPT, the higher the optimization the longer the compilation time, but the performance of the compiled code should improve, though it could increase the binary file size and its memory usage.

Copy link
Contributor Author

@avi-starkware avi-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: 6 of 9 files reviewed, 6 unresolved discussions (waiting on @noaov1 and @Yoni-Starkware)


crates/bin/starknet-native-compile/src/main.rs line 20 at r4 (raw file):

Previously, Yoni-Starkware (Yoni) wrote…

Remove the default value (no reason to do this so far)

Done.


crates/bin/starknet-native-compile/src/main.rs line 28 at r4 (raw file):

Previously, Yoni-Starkware (Yoni) wrote…

We are not going to see these prints in the blockifier run, right?

They will appear, unfortunately...
They were used for debugging. I removed all of them.


crates/bin/starknet-native-compile/src/utils.rs line 11 at r4 (raw file):

Previously, Yoni-Starkware (Yoni) wrote…

Is this the right way to go?
Why not returning Result throughout the code?
Can main return result?

Because it is a binary running in another process. It can only output stdout and stderr.


crates/bin/starknet-native-compile/build.rs line 3 at r4 (raw file):

Previously, Yoni-Starkware (Yoni) wrote…

Document why this is needed

I removed the file. The env var is needed to run the compiled binary, not to build it. It would make more sense to set it correctly in the crate that uses the compiled binary.


crates/bin/starknet-native-compile/build.rs line 17 at r4 (raw file):

Previously, Yoni-Starkware (Yoni) wrote…

Document why 3

I removed the build.rs file.

Copy link
Collaborator

@Yoni-Starkware Yoni-Starkware 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 9 files at r1, 1 of 2 files at r5, 1 of 2 files at r6, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @avi-starkware and @noaov1)


crates/bin/starknet-native-compile/src/main.rs line 33 at r4 (raw file):

Previously, avi-starkware wrote…

These are the options.

It indicates the level of optimization the compiler should employ while compiling code. I am not sure what impact this config has, so I just put it on default. Nethermind did the same in their PRs.

The OptLevel is plugged into LLVM here.

According to ChatGPT, the higher the optimization the longer the compilation time, but the performance of the compiled code should improve, though it could increase the binary file size and its memory usage.

Add a todo to configure the opt level after testing it. It might speed things up

@avi-starkware avi-starkware force-pushed the avi/add-starknet-native-compile-crate branch from 264bf17 to 874d900 Compare November 13, 2024 19:19
Copy link

Artifacts upload triggered. View details here

Copy link
Contributor Author

@avi-starkware avi-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: 7 of 9 files reviewed, 1 unresolved discussion (waiting on @noaov1 and @Yoni-Starkware)


crates/bin/starknet-native-compile/src/main.rs line 33 at r4 (raw file):

Previously, Yoni-Starkware (Yoni) wrote…

Add a todo to configure the opt level after testing it. It might speed things up

Done.

Copy link
Collaborator

@Yoni-Starkware Yoni-Starkware 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 r8, all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on @noaov1)

@avi-starkware avi-starkware force-pushed the avi/add-starknet-native-compile-crate branch from 874d900 to fa12a02 Compare November 14, 2024 08:15
Copy link

Artifacts upload triggered. View details here

Copy link
Collaborator

@Yoni-Starkware Yoni-Starkware 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: :shipit: complete! all files reviewed, all discussions resolved (waiting on @noaov1)

Copy link
Contributor Author

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

@avi-starkware avi-starkware merged commit 25eeca0 into main Nov 14, 2024
22 checks passed
@avi-starkware avi-starkware deleted the avi/add-starknet-native-compile-crate branch November 14, 2024 12:11
@github-actions github-actions bot locked and limited conversation to collaborators Nov 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants