Skip to content

Commit

Permalink
Merge branch 'main' into dynamic-params-always-null
Browse files Browse the repository at this point in the history
  • Loading branch information
fmoletta authored Apr 18, 2024
2 parents f18418a + 4b17118 commit cfa1186
Show file tree
Hide file tree
Showing 21 changed files with 530 additions and 145 deletions.
110 changes: 110 additions & 0 deletions .github/workflows/hyper_threading_benchmarks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
name: Benchmark Hyper Threading

on:
pull_request:
branches:
- main

jobs:
benchmark:
runs-on: ubuntu-latest
steps:
- name: Checkout PR
uses: actions/checkout@v2
with:
ref: ${{ github.head_ref }}

- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.9'

- name: Install Dependencies
run: |
pip install -r requirements.txt
sudo apt update
sudo apt-get install -y hyperfine
- name: Install Rust
uses: dtolnay/rust-toolchain@1.74.1
with:
components: rustfmt, clippy

- name: Compile PR Version
run: |
cargo build --release -p hyper_threading
cp target/release/hyper_threading ${{ github.workspace }}/hyper_threading_pr
cp ./examples/hyper_threading/hyper-threading-workflow.sh ${{ github.workspace }}/hyper-threading-workflow.sh
- name: Upload PR Binary
uses: actions/upload-artifact@v4
with:
name: hyper_threading_pr_binary
path: ${{ github.workspace }}/hyper_threading_pr

- name: Upload Workflow Script
uses: actions/upload-artifact@v4
with:
name: hyper_threading_workflow_script
path: ${{ github.workspace }}/hyper-threading-workflow.sh


- name: Checkout Main Branch
uses: actions/checkout@v2
with:
ref: 'main'

- name: Compile Main Version
run: |
cargo build --release -p hyper_threading
cp target/release/hyper_threading ${{ github.workspace }}/hyper_threading_main
- name: Download hyper_threading_pr_binary
uses: actions/download-artifact@v4
with:
name: hyper_threading_pr_binary
path: ${{ github.workspace }}/

- name: Download hyper_threading_workflow_script
uses: actions/download-artifact@v4
with:
name: hyper_threading_workflow_script
path: ${{ github.workspace }}/

- name: Compile programs
run: make cairo_bench_programs

- name: Run Benchmarks
run: |
cd ${{ github.workspace }}
chmod +x ./hyper_threading_main
chmod +x ./hyper_threading_pr
chmod +x hyper-threading-workflow.sh
./hyper-threading-workflow.sh
- name: Compare Results
run: |
cat result.md
- name: Find comment
uses: peter-evans/find-comment@v2
id: fc
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: 'github-actions[bot]'
body-includes: "**Hyper Thereading Benchmark results**"

- name: Create comment
if: steps.fc.outputs.comment-id == ''
uses: peter-evans/create-or-update-comment@v3
with:
issue-number: ${{ github.event.pull_request.number }}
body-path: result.md

- name: Update comment
if: steps.fc.outputs.comment-id != ''
uses: peter-evans/create-or-update-comment@v3
with:
comment-id: ${{ steps.fc.outputs.comment-id }}
body-path: result.md
edit-mode: replace
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
* BREAKING: Set dynamic params as null by default on air public input [#1716](https://github.com/lambdaclass/cairo-vm/pull/1716)
* `PublicInput` field `layout_params` renamed to `dynamic_params` & type changed from`&'a CairoLayout` to `()`.

* fix(BREAKING): Remove unsafe impl of `Add<usize> for &'a Relocatable`[#1718](https://github.com/lambdaclass/cairo-vm/pull/1718)

* fix(BREAKING): Handle triple dereference references[#1708](https://github.com/lambdaclass/cairo-vm/pull/1708)
* Replace `ValueAddress` boolean field `dereference` with boolean fields `outer_dereference` & `inner_dereference`
* Replace `HintReference` boolean field `dereference` with boolean fields `outer_dereference` & `inner_dereference`
* Reference parsing now handles the case of dereferences inside the cast. Aka references of type `cast([A + B], type)` such as `cast([[fp + 2] + 2], felt)`.

* Bump `starknet-types-core` version + Use the lib's pedersen hash [#1692](https://github.com/lambdaclass/cairo-vm/pull/1692)

* refactor: Remove unused code & use constants whenever possible for builtin instance definitions[#1707](https://github.com/lambdaclass/cairo-vm/pull/1707)

* feat: missing EC hints for Starknet OS 0.13.1 [#1706](https://github.com/lambdaclass/cairo-vm/pull/1706)
Expand Down
13 changes: 6 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

140 changes: 140 additions & 0 deletions cairo_programs/mod_builtin_feature/apply_poly.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
%builtins range_check range_check96 add_mod mul_mod
// TODO: Import directly from common library once released
from cairo_programs.mod_builtin_feature.common.modulo import ModBuiltin, UInt384, run_mod_p_circuit
// from starkware.cairo.common.modulo import run_mod_p_circuit
// from starkware.cairo.common.cairo_builtins import ModBuiltin, UInt384
from starkware.cairo.common.registers import get_label_location
from starkware.cairo.common.memcpy import memcpy
from starkware.cairo.common.alloc import alloc

// Computes the polynomial f(x) = x^8 + 5*x^2 + 1.
func apply_poly{
range_check_ptr,
range_check96_ptr: felt*,
add_mod_ptr: ModBuiltin*,
mul_mod_ptr: ModBuiltin*
}(x: UInt384*, p: UInt384) -> (res: UInt384*) {

// Copy inputs and constants into the values_ptr segment.
memcpy(dst=range_check96_ptr, src=x, len=UInt384.SIZE);
let (constants_ptr) = get_label_location(constants);
memcpy(dst=range_check96_ptr + UInt384.SIZE, src=constants_ptr, len=2 * UInt384.SIZE);
let values_ptr = cast(range_check96_ptr, UInt384*);
let range_check96_ptr = range_check96_ptr + 36;


let (add_mod_offsets_ptr) = get_label_location(add_offsets);
let (mul_mod_offsets_ptr) = get_label_location(mul_offsets);
run_mod_p_circuit(
p=p,
values_ptr=values_ptr,
add_mod_offsets_ptr=add_mod_offsets_ptr,
add_mod_n=2,
mul_mod_offsets_ptr=mul_mod_offsets_ptr,
mul_mod_n=4,
);

return (res=values_ptr + 32);

// values_ptr points to a segment within the range_check96_ptr segment that looks like this:
//
// offset value
// 0 x
// 4 1
// 8 5
// 12 x^2
// 16 x^4
// 20 x^8
// 24 5*x^2
// 28 x^8 + 5*x^2
// 32 x^8 + 5*x^2 + 1

constants:
dw 1;
dw 0;
dw 0;
dw 0;

dw 5;
dw 0;
dw 0;
dw 0;

add_offsets:
dw 20; // x^8
dw 24; // 5*x^2
dw 28; // x^8 + 5*x^2

dw 4; // 1
dw 28; // x^8 + 5*x^2
dw 32; // x^8 + 5*x^2 + 1

// Placeholders (copies of the first 3 offsets):
dw 20;
dw 24;
dw 28;
dw 20;
dw 24;
dw 28;
dw 20;
dw 24;
dw 28;
dw 20;
dw 24;
dw 28;
dw 20;
dw 24;
dw 28;
dw 20;
dw 24;
dw 28;


mul_offsets:
dw 0; // x
dw 0; // x
dw 12; // x^2

dw 12; // x^2
dw 12; // x^2
dw 16; // x^4

dw 16; // x^4
dw 16; // x^4
dw 20; // x^8

dw 8; // 5
dw 12; // x^2
dw 24; // 5*x^2

// Placeholders (copies of the first 3 offsets):
dw 0;
dw 0;
dw 12;
dw 0;
dw 0;
dw 12;
dw 0;
dw 0;
dw 12;
dw 0;
dw 0;
dw 12;
}

func main{range_check_ptr, range_check96_ptr: felt*, add_mod_ptr: ModBuiltin*, mul_mod_ptr: ModBuiltin*}() {
alloc_locals;

let p = UInt384(d0=0xffff, d1=0xffff, d2=0xffff, d3=0xffff);
let (local inputs: UInt384*) = alloc();
assert inputs[0] = UInt384(d0=0xbbbb, d1=0xaaaa, d2=0x6666, d3=0xffff);

let res: UInt384* = apply_poly(inputs, p);

assert res[0].d0 = 0xdb0030d69941baf9893cd667;
assert res[0].d1 = 0xfffffffffffffffee43128e7;
assert res[0].d2 = 0xfd4c69cdf6010eab465c3055;
assert res[0].d3 = 0xea52;

return();
}
1 change: 1 addition & 0 deletions cairo_programs/mod_builtin_feature/proof/apply_poly.cairo
1 change: 1 addition & 0 deletions docs/references_parsing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ There are some other, more rare cases of reference values found when implementin

* ```cast(number, felt)```
* ```[cast(reg + offset1 + offset2, type)]```
* ```[cast([[reg + offset1] + offset2], type)]```

## To do
For the moment the type of the reference is not being used, this will be included in the future to make the hints code cleaner.
Expand Down
34 changes: 34 additions & 0 deletions examples/hyper_threading/hyper-threading-workflow.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash

# Define a list of RAYON_NUM_THREADS
thread_counts=(1 2 4 6 8 16 )

# Define binary names
binaries=("hyper_threading_main" "hyper_threading_pr")

echo -e "**Hyper Thereading Benchmark results**" >> result.md
printf "\n\n" >> result.md

# Iter over thread_counts
for threads in "${thread_counts[@]}"; do
# Initialize hyperfine command
cmd="hyperfine -r 2"

# Add each binary to the command with the current threads value
for binary in "${binaries[@]}"; do
cmd+=" -n \"${binary} threads: ${threads}\" 'RAYON_NUM_THREADS=${threads} ./${binary}'"
done

# Execute
echo "Running benchmark for ${threads} threads"
printf "\n\n" >> result.md
echo -e $cmd >> result.md
eval $cmd >> result.md
printf "\n\n" >> result.md
done

{
echo -e '```'
cat result.md
echo -e '```'
} > temp_result.md && mv temp_result.md result.md
2 changes: 1 addition & 1 deletion vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ keccak = { workspace = true }
hashbrown = { workspace = true }
anyhow = { workspace = true }
thiserror-no-std = { workspace = true }
starknet-types-core = { version = "0.0.9", default-features = false, features = ["serde", "curve", "num-traits"] }
starknet-types-core = { version = "0.1.0", default-features = false, features = ["serde", "curve", "num-traits", "hash"] }

# only for std
num-prime = { version = "0.4.3", features = ["big-int"], optional = true }
Expand Down
Loading

0 comments on commit cfa1186

Please sign in to comment.