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

Add Fibonacci benchmark #128

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion ci/spectest-zkasm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ set -eux

./ci/test-zkasm.sh "cranelift/zkasm_data/spectest/i32"
./ci/test-zkasm.sh "cranelift/zkasm_data/spectest/i64"
./ci/test-zkasm.sh "cranelift/zkasm_data/spectest/conversions"
./ci/test-zkasm.sh "cranelift/zkasm_data/spectest/conversions"
# TODO(akashin): Move this out to a separate shell script or rename this shell script to be
# more general.
./ci/test-zkasm.sh "cranelift/zkasm_data/benchmarks/fibonacci"
6 changes: 5 additions & 1 deletion cranelift/filetests/src/test_zkasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,11 @@ mod tests {
run_spectest("conversions");
}

#[test]
fn run_benchmarks() {
test_wat_in_directory(Path::new(&format!("../zkasm_data/benchmarks/fibonacci")));
}

macro_rules! testcases {
{ $($name:ident,)* } => {
$(
Expand All @@ -316,7 +321,6 @@ mod tests {
locals,
locals_simple,
counter,
fibonacci,
add_func,
mul,
i64_mul,
Expand Down
64 changes: 64 additions & 0 deletions cranelift/zkasm_data/benchmarks/fibonacci/from_rust.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
;; TODO(akashin): Add Rust code to generate this file to repository.
;; For now see https://github.com/near/wasmtime/issues/90#issuecomment-1823140768.
(module
(type (;0;) (func (param i64 i64)))
(type (;1;) (func))
(import "env" "assert_eq" (func (;0;) (type 0)))
(func (;1;) (type 1)
(local i64 i64 i32)
i64.const 1
local.set 0
i64.const 0
local.set 1
i32.const 10000
local.set 2
loop ;; label = @1
local.get 0
local.get 1
i64.add
local.tee 1
local.get 0
i64.add
local.tee 0
local.get 1
i64.add
local.tee 1
local.get 0
i64.add
local.tee 0
local.get 1
i64.add
local.tee 1
local.get 0
i64.add
local.tee 0
local.get 1
i64.add
local.tee 1
local.get 0
i64.add
local.tee 0
local.get 1
i64.add
local.tee 1
local.get 0
i64.add
local.set 0
local.get 2
i32.const -10
i32.add
local.tee 2
br_if 0 (;@1;)
end
local.get 1
i64.const -2872092127636481573
call 0)
(start 1)
(memory (;0;) 16)
(global (;0;) (mut i32) (i32.const 1048576))
(global (;1;) i32 (i32.const 1048576))
(global (;2;) i32 (i32.const 1048576))
(export "memory" (memory 0))
(export "main" (func 1))
(export "__data_end" (global 1))
(export "__heap_base" (global 2)))
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
start:
zkPC + 2 => RR
:JMP(function_1)
:JMP(finalizeExecution)
function_1:
SP + 1 => SP
RR :MSTORE(SP - 1)
SP + 6 => SP
C :MSTORE(SP - 1)
D :MSTORE(SP - 2)
E :MSTORE(SP - 3)
B :MSTORE(SP - 4)
0n => A
A :MSTORE(SP + 8)
0n => A
1n => D
D => B
:JMP(label_1_1)
label_1_1:
$ => E :ADD
B :MSTORE(SP)
4294967296n => B
$ => A :MLOAD(SP + 8)
$ => A :ADD
A :MSTORE(SP + 8)
42949672960000n => B
$ => A :EQ
4294967296n => B
0 => D
0 => C
${A * B} => A :ARITH
A => B
0 => A
$ => A :EQ
A :JMPZ(label_1_3)
$ => A :MLOAD(SP)
E => B
:JMP(label_1_1)
label_1_3:
15574651946073070043n => B
$ => A :MLOAD(SP)
B :ASSERT
$ => C :MLOAD(SP - 1)
$ => D :MLOAD(SP - 2)
$ => E :MLOAD(SP - 3)
$ => B :MLOAD(SP - 4)
SP - 6 => SP
$ => RR :MLOAD(SP - 1)
SP - 1 => SP
:JMP(RR)
finalizeExecution:
${beforeLast()} :JMPN(finalizeExecution)
:JMP(start)
INCLUDE "helpers/2-exp.zkasm"
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
two_power:
0x1n => B :JMP(RR) ;2**0
0x2n => B :JMP(RR) ;2**1
0x4n => B :JMP(RR) ;2**2
0x8n => B :JMP(RR) ;2**3
0x10n => B :JMP(RR) ;2**4
0x20n => B :JMP(RR) ;2**5
0x40n => B :JMP(RR) ;2**6
0x80n => B :JMP(RR) ;2**7
0x100n => B :JMP(RR) ;2**8
0x200n => B :JMP(RR) ;2**9
0x400n => B :JMP(RR) ;2**10
0x800n => B :JMP(RR) ;2**11
0x1000n => B :JMP(RR) ;2**12
0x2000n => B :JMP(RR) ;2**13
0x4000n => B :JMP(RR) ;2**14
0x8000n => B :JMP(RR) ;2**15
0x10000n => B :JMP(RR) ;2**16
0x20000n => B :JMP(RR) ;2**17
0x40000n => B :JMP(RR) ;2**18
0x80000n => B :JMP(RR) ;2**19
0x100000n => B :JMP(RR) ;2**20
0x200000n => B :JMP(RR) ;2**21
0x400000n => B :JMP(RR) ;2**22
0x800000n => B :JMP(RR) ;2**23
0x1000000n => B :JMP(RR) ;2**24
0x2000000n => B :JMP(RR) ;2**25
0x4000000n => B :JMP(RR) ;2**26
0x8000000n => B :JMP(RR) ;2**27
0x10000000n => B :JMP(RR) ;2**28
0x20000000n => B :JMP(RR) ;2**29
0x40000000n => B :JMP(RR) ;2**30
0x80000000n => B :JMP(RR) ;2**31
0x100000000n => B :JMP(RR) ;2**32
0x200000000n => B :JMP(RR) ;2**33
0x400000000n => B :JMP(RR) ;2**34
0x800000000n => B :JMP(RR) ;2**35
0x1000000000n => B :JMP(RR) ;2**36
0x2000000000n => B :JMP(RR) ;2**37
0x4000000000n => B :JMP(RR) ;2**38
0x8000000000n => B :JMP(RR) ;2**39
0x10000000000n => B :JMP(RR) ;2**40
0x20000000000n => B :JMP(RR) ;2**41
0x40000000000n => B :JMP(RR) ;2**42
0x80000000000n => B :JMP(RR) ;2**43
0x100000000000n => B :JMP(RR) ;2**44
0x200000000000n => B :JMP(RR) ;2**45
0x400000000000n => B :JMP(RR) ;2**46
0x800000000000n => B :JMP(RR) ;2**47
0x1000000000000n => B :JMP(RR) ;2**48
0x2000000000000n => B :JMP(RR) ;2**49
0x4000000000000n => B :JMP(RR) ;2**50
0x8000000000000n => B :JMP(RR) ;2**51
0x10000000000000n => B :JMP(RR) ;2**52
0x20000000000000n => B :JMP(RR) ;2**53
0x40000000000000n => B :JMP(RR) ;2**54
0x80000000000000n => B :JMP(RR) ;2**55
0x100000000000000n => B :JMP(RR) ;2**56
0x200000000000000n => B :JMP(RR) ;2**57
0x400000000000000n => B :JMP(RR) ;2**58
0x800000000000000n => B :JMP(RR) ;2**59
0x1000000000000000n => B :JMP(RR) ;2**60
0x2000000000000000n => B :JMP(RR) ;2**61
0x4000000000000000n => B :JMP(RR) ;2**62
0x8000000000000000n => B :JMP(RR) ;2**63
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
(module
(import "env" "assert_eq" (func $assert_eq (param i32) (param i32)))
(import "env" "assert_eq" (func $assert_eq (param i64) (param i64)))
(func $main
(local $counter i32)
(local $fp i32)
(local $f i32)
(local $fp i64)
(local $f i64)
(local.set $counter (i32.const 0))
(local.set $fp (i32.const 0))
(local.set $f (i32.const 1))
(local.set $fp (i64.const 0))
(local.set $f (i64.const 1))
(block
(loop
(local.get $fp)
(local.get $f)
(local.set $fp (local.get $f))
(local.set $f (i32.add))
(local.set $f (i64.add))
(local.set $counter
(i32.add
(local.get $counter)
(i32.const 1)))
(br_if 1
(i32.eq
(local.get $counter)
(i32.const 10)
(i32.const 10000)
)
)
(br 0)
)
)
(local.get $f)
(i32.const 89)
(local.get $fp)
(i64.const -2872092127636481573)
call $assert_eq)
(start $main))
3 changes: 3 additions & 0 deletions cranelift/zkasm_data/benchmarks/fibonacci/state.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Test,Status,Cycles
from_rust,compilation failed,
handwritten_wat,pass,190024

Choose a reason for hiding this comment

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

LGTM. Just interesting, what is ratio from cycles to seconds?

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, that would be very interesting to know. I'll be working on figuring this out next.
We basically know that ZK ASM is ~10KHz machine, so now we also need to benchmark that Fibonacci code on normal processor and compute the ratio.

1 change: 1 addition & 0 deletions docs/zkasm/test_summary.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Suite path,Passing count,Total count,Total cycles
cranelift/zkasm_data,25,26,1023
cranelift/zkasm_data/benchmarks/fibonacci,1,2,190024
cranelift/zkasm_data/spectest/conversions,3,24,45
cranelift/zkasm_data/spectest/i32,155,364,4589
cranelift/zkasm_data/spectest/i64,161,374,4042