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(avm): in vm static gas accounting #6542

Merged
merged 25 commits into from
May 31, 2024
Merged

feat(avm): in vm static gas accounting #6542

merged 25 commits into from
May 31, 2024

Conversation

Maddiaa0
Copy link
Member

@Maddiaa0 Maddiaa0 commented May 20, 2024

Overview

https://hackmd.io/zOxxT5bGTPmDWdalw0BkEw

TODO

  • Account for operations that span multiple rows - will likely require a column, opcode_step_start that will execute the gas transition start - currently capture by opcode_active
  • Different gas cost for each opcode that matches what is used in the simualator see yarn-project/simulator/src/avm/avm_gas.ts
  • flag for when the gas left underflows

For follow up prs

  • Constrain gas start and gas end are the same as provided in the kernel circuit public inputs (separate pr)
  • Dynamic gas costs, will require more table columns

@@ -320,9 +346,12 @@ namespace avm_main(256);
pol OPCODE_SELECTORS = (sel_op_add + sel_op_sub + sel_op_div + sel_op_fdiv + sel_op_mul + sel_op_not
+ sel_op_eq + sel_op_and + sel_op_or + sel_op_xor + sel_op_cast + KERNEL_SELECTORS);

pol commit opcode_active;
opcode_active - OPCODE_SELECTORS = 0;
Copy link
Member Author

Choose a reason for hiding this comment

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

constrain to be boolean

// Constrain that the gas decrements correctly per instruction
opcode_active * (l2_gas_remaining - (l2_gas_remaining' + l2_gas_op));
opcode_active * (da_gas_remaining - (da_gas_remaining' + da_gas_op));

Copy link
Member Author

Choose a reason for hiding this comment

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

underflow check

@jeanmon jeanmon force-pushed the md/05-17-avm-gas branch 2 times, most recently from f6c9a06 to 979ffcb Compare May 23, 2024 17:39
@AztecBot
Copy link
Collaborator

AztecBot commented May 23, 2024

Benchmark results

Metrics with a significant change:

  • protocol_circuit_witness_generation_time_in_ms (private-kernel-tail): 2,684 (-17%)
Detailed results

All benchmarks are run on txs on the Benchmarking contract on the repository. Each tx consists of a batch call to create_note and increment_balance, which guarantees that each tx has a private call, a nested private call, a public call, and a nested public call, as well as an emitted private note, an unencrypted log, and public storage read and write.

This benchmark source data is available in JSON format on S3 here.

Proof generation

Each column represents the number of threads used in proof generation.

Metric 1 threads 4 threads 16 threads 32 threads 64 threads
proof_construction_time_sha256_ms 5,648 (-1%) 1,543 697 (-1%) 753 (+1%) 769
proof_construction_time_sha256_30_ms 11,378 (-1%) 3,058 1,373 1,429 1,459
proof_construction_time_sha256_100_ms 44,589 (+2%) 12,065 (+2%) 5,661 (+3%) 5,525 (+1%) 5,490 (+2%)
proof_construction_time_poseidon_hash_ms 78.0 34.0 34.0 56.0 (-3%) 89.0 (-4%)
proof_construction_time_poseidon_hash_30_ms 1,498 (-1%) 417 203 (+2%) 222 (-5%) 271 (+2%)
proof_construction_time_poseidon_hash_100_ms 5,677 (-1%) 1,559 726 (+1%) 783 (+1%) 788

L2 block published to L1

Each column represents the number of txs on an L2 block published to L1.

Metric 8 txs 32 txs 64 txs
l1_rollup_calldata_size_in_bytes 1,412 1,412 1,412
l1_rollup_calldata_gas 9,452 9,476 9,452
l1_rollup_execution_gas 616,093 616,117 616,093
l2_block_processing_time_in_ms 1,266 4,713 (-1%) 9,515 (+1%)
l2_block_building_time_in_ms 43,529 (-1%) 171,990 (-2%) 347,091
l2_block_rollup_simulation_time_in_ms 43,339 (-1%) 171,270 (-2%) 345,641
l2_block_public_tx_process_time_in_ms 37,714 (-1%) 167,987 340,416

L2 chain processing

Each column represents the number of blocks on the L2 chain where each block has 16 txs.

Metric 3 blocks 5 blocks
node_history_sync_time_in_ms 9,420 14,397
node_database_size_in_bytes 14,504,016 21,311,568
pxe_database_size_in_bytes 18,071 29,868

Circuits stats

Stats on running time and I/O sizes collected for every kernel circuit run across all benchmarks.

Circuit simulation_time_in_ms witness_generation_time_in_ms proving_time_in_ms input_size_in_bytes output_size_in_bytes proof_size_in_bytes num_public_inputs size_in_gates
private-kernel-init 135 468 (-2%) 13,309 (+3%) 20,634 64,614 89,536 2,731 524,288
private-kernel-inner 407 945 (-8%) 48,815 (-2%) 92,326 64,614 89,536 2,731 2,097,152
private-kernel-tail 586 (+1%) ⚠️ 2,684 (-17%) 52,986 (+2%) 96,545 77,732 11,648 297 2,097,152
base-parity 6.47 1,055 (-2%) 2,983 (-1%) 128 64.0 2,208 2.00 131,072
root-parity 50.4 (+1%) 64.0 (-5%) 40,767 (+1%) 27,100 64.0 2,720 18.0 2,097,152
base-rollup 9,070 (-1%) 2,470 (-3%) 80,633 (+1%) 119,738 756 3,648 47.0 4,194,304
root-rollup 110 84.4 (-5%) 23,441 (+4%) 25,309 620 3,456 41.0 1,048,576
public-kernel-app-logic 570 3,557 (-2%) 46,891 (+2%) 108,073 86,550 116,768 3,582 2,097,152
public-kernel-tail 1,123 23,888 (+9%) 185,022 403,238 7,646 11,648 297 8,388,608
private-kernel-reset-small 592 (+1%) 1,949 (-14%) 45,935 (-1%) 120,737 64,614 89,536 2,731 2,097,152
public-kernel-setup 661 (-1%) 2,789 (-1%) 44,321 (-1%) 108,073 86,550 116,768 3,582 2,097,152
public-kernel-teardown 583 (+3%) 3,575 (-1%) 46,455 (+1%) 108,073 86,550 116,768 3,582 2,097,152
merge-rollup 29.6 (-5%) N/A N/A 16,542 756 N/A N/A N/A
private-kernel-tail-to-public N/A 8,414 101,172 (-2%) N/A N/A 116,768 3,582 4,194,304

Stats on running time collected for app circuits

Function input_size_in_bytes output_size_in_bytes witness_generation_time_in_ms proof_size_in_bytes proving_time_in_ms size_in_gates num_public_inputs
ContractClassRegisterer:register 1,344 9,944 464 N/A N/A N/A N/A
ContractInstanceDeployer:deploy 1,408 9,944 42.1 (+3%) N/A N/A N/A N/A
MultiCallEntrypoint:entrypoint 1,920 9,944 1,782 (+1%) N/A N/A N/A N/A
SchnorrAccount:constructor 1,312 9,944 1,429 N/A N/A N/A N/A
SchnorrAccount:entrypoint 2,304 9,944 2,756 16,768 55,903 2,097,152 457
Token:privately_mint_private_note 1,280 9,944 1,593 (+2%) N/A N/A N/A N/A
FPC:fee_entrypoint_public 1,344 9,944 1,057 (+1%) 16,768 11,033 (-1%) 524,288 457
Token:transfer 1,376 9,944 5,309 (-1%) 16,768 52,328 (-1%) 2,097,152 457
Benchmarking:create_note 1,344 9,944 1,397 N/A N/A N/A N/A
SchnorrAccount:spend_private_authwit 1,280 9,944 78.2 (+1%) N/A N/A N/A N/A
Token:unshield 1,376 9,944 3,901 N/A N/A N/A N/A
FPC:fee_entrypoint_private 1,376 9,944 4,809 (+1%) N/A N/A N/A N/A

Tree insertion stats

The duration to insert a fixed batch of leaves into each tree type.

Metric 1 leaves 16 leaves 64 leaves 128 leaves 512 leaves 1024 leaves 2048 leaves 4096 leaves 32 leaves
batch_insert_into_append_only_tree_16_depth_ms 10.4 17.3 N/A N/A N/A N/A N/A N/A N/A
batch_insert_into_append_only_tree_16_depth_hash_count 16.7 31.8 N/A N/A N/A N/A N/A N/A N/A
batch_insert_into_append_only_tree_16_depth_hash_ms 0.601 0.520 N/A N/A N/A N/A N/A N/A N/A
batch_insert_into_append_only_tree_32_depth_ms N/A N/A 48.3 76.0 245 474 913 (-2%) 1,835 (+1%) N/A
batch_insert_into_append_only_tree_32_depth_hash_count N/A N/A 95.9 159 543 1,055 2,079 4,127 N/A
batch_insert_into_append_only_tree_32_depth_hash_ms N/A N/A 0.493 0.468 0.444 0.442 0.432 (-2%) 0.438 N/A
batch_insert_into_indexed_tree_20_depth_ms N/A N/A 57.9 111 (-1%) 350 (-1%) 697 1,356 (-2%) 2,749 N/A
batch_insert_into_indexed_tree_20_depth_hash_count N/A N/A 106 208 692 1,363 2,707 5,395 N/A
batch_insert_into_indexed_tree_20_depth_hash_ms N/A N/A 0.501 0.499 (-1%) 0.475 (-1%) 0.479 0.469 (-2%) 0.477 N/A
batch_insert_into_indexed_tree_40_depth_ms N/A N/A N/A N/A N/A N/A N/A N/A 62.4
batch_insert_into_indexed_tree_40_depth_hash_count N/A N/A N/A N/A N/A N/A N/A N/A 107
batch_insert_into_indexed_tree_40_depth_hash_ms N/A N/A N/A N/A N/A N/A N/A N/A 0.551

Miscellaneous

Transaction sizes based on how many contract classes are registered in the tx.

Metric 0 registered classes 1 registered classes
tx_size_in_bytes 84,053 665,267

Transaction size based on fee payment method

| Metric | |
| - | |

@jeanmon jeanmon force-pushed the md/05-17-avm-gas branch 7 times, most recently from 5fb2ef2 to fb9fa49 Compare May 30, 2024 11:51
@jeanmon jeanmon marked this pull request as ready for review May 30, 2024 11:52
@jeanmon jeanmon mentioned this pull request May 30, 2024
@jeanmon jeanmon force-pushed the md/05-17-avm-gas branch 2 times, most recently from 72282f7 to cb49c19 Compare May 30, 2024 14:15
uint32_t initial_da_gas = 0;

private:
std::vector<GasTableEntry> construct_gas_lookup_table();
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this ever used since we have the hardcoded table above?

Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch!

EXPECT_TRUE(sender_row != trace.end());

// Show the first few rows and see if the correct gas values are populated
for (size_t i = 1; i < 5; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need these logs here when we have in the the helper.cpp already?

Copy link
Contributor

Choose a reason for hiding this comment

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

Not really. As unit tests are postponed to another ticket I have left this part. Ultimately, it will be removed. I will add a comment.

@jeanmon jeanmon force-pushed the md/05-17-avm-gas branch 2 times, most recently from 0eed979 to 6a2f305 Compare May 31, 2024 11:09
Copy link
Collaborator

@AztecBot AztecBot left a comment

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'C++ Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.05.

Benchmark suite Current: 1630e9a Previous: 6a2f305 Ratio
nativeconstruct_proof_ultrahonk_power_of_2/20 5943.480171999994 ms/iter 5506.037660000004 ms/iter 1.08

This comment was automatically generated by workflow using github-action-benchmark.

CC: @ludamad @codygunton

@jeanmon jeanmon enabled auto-merge (squash) May 31, 2024 12:02
@jeanmon jeanmon merged commit 6b88ae0 into master May 31, 2024
86 checks passed
@jeanmon jeanmon deleted the md/05-17-avm-gas branch May 31, 2024 12:11
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.

4 participants