Prototype of evalutation of neural networks inside zero knowledge proofs using the plonky2 proof system.
To find out more about zero-knowledge machine learning, check out the awesome-zkml repository we have created. It aggregates scientific research papers, codebases, articles, and use cases in the field of ZKML.
Worldcoin is a Privacy-Preserving Proof-of-Personhood Protocol. ZKML could help us make our protocol more trustless, and make it more easily upgradeable and auditabie.
cargo +nightly run --release -- -vvv --input-size 1000 --output-size 1000
# open Python CNN implementation directory
cd ref_cnn
# run CNN model and check result
python3 vanilla_cnn.py
# generate JSON files for the random number generated matrices in the model
python3 generate_cnn_json.py
cd ../
# run Rust CNN implementation and compare results against your previous results
cargo test serialize::tests::deserialize_nn_json -- --show-output
ArcArray<f32, IxDyn>.> python ref_cnn/vanilla_cnn.py
layer | output shape | #parameters | #ops
-------------------- | --------------- | --------------- | ---------------
conv 32x5x5x3 | (116, 76, 32) | 2400 | 21158400
max-pool | (58, 38, 32) | 0 | 0
relu | (58, 38, 32) | 0 | 0
conv 32x5x5x32 | (54, 34, 32) | 25600 | 47001600
max-pool | (27, 17, 32) | 0 | 0
relu | (27, 17, 32) | 0 | 0
flatten | (14688,) | 0 | 0
conv 1000x14688 | (1000,) | 14689000 | 14688000
relu | (1000,) | 0 | 0
conv 5x1000 | (5,) | 5005 | 5000
normalize | (5,) | 0 | 6
final output: [-0.11425511 -0.13403508 -0.41759714 -0.24778798 0.85626755]
---- serialize::tests::deserialize_nn_json stdout ----
layer | output shape | #parameters | #ops
-----------------------------------------------------------------------------
conv 32x5x5x3 | [116, 76, 32] | 2400 | 7052800
max-pool | [38, 58, 32] | 0 | 282112
relu | [58, 38, 32] | 70528 | 0
conv 32x5x5x32 | [54, 34, 32] | 25600 | 1468800
max-pool | [17, 27, 32] | 0 | 58752
relu | [27, 17, 32] | 14688 | 0
flatten | [14688] | 0 | 0
full | [1000] | 14689000 | 14688000
relu | [1000] | 1000 | 0
full | [5] | 5005 | 5000
normalize | [5] | 0 | 6
final output (normalized):
[-0.11425512, -0.13403504, -0.41759717, -0.24778795, 0.8562675]
cd ref_cnn
python benchmark_cnn.py
# generates matrices for the Rust implementation to use
python generate_cnn_json.py
cargo bench bench_neural_net
Machine: M1 Max Macbook Pro
The average time is 0.8297840171150046 seconds for 1000 runs
test nn::bench_neural_net ... bench: 151,632,316 ns/iter (+/- 1,469,992)
In this benchmark the Rust implementation is 5.5x faster!
Verify that all components of the rust codebase are working fine and that no breaking changes were introduced.
cargo test
In order to see output use cargo test -- --output, i.e.:
cargo test nn::tests::neural_net -- --show-output
Serializing the vanilla CNN model created with numpy into JSON and desearilizing the model into a NeuralNetwork Rust object
# change directory to cnn folder
cd ref_cnn
# generate json file for the model
python generate_cnn_json.py
cargo test serialize::tests::deserialize_model_json -- --show-output
# serializes a CNN model with random weights into src/json/nn.json
cargo test serialize::tests::serialize_model_json -- --show-output
Create a NeuralNetwork object with random weights in Rust, serialize it into JSON and deserialize back into a NeuralNetwork Rust object
# serializes a CNN model with random weights into src/json/nn.json
cargo test serialize::tests::serde_full_circle -- --show-output
Benchmarks for serializing and deserializing the reference CNN (Rust/JSON) using serde.
# full serialization benchmark times (M1 Max Macbook Pro)
# cargo bench - 579,057,637 ns/iter (+/- 20,202,535)
cargo bench bench_serialize_neural_net
# full deserialization benchmark times (M1 Max Macbook Pro)
# cargo bench - 565,564,850 ns/iter (+/- 61,387,641)
cargo bench bench_deserialize_neural_net
Rust
64.0%
Jupyter Notebook
19.0%
Python
17.0%
Prototype of evalutation of neural networks inside zero knowledge proofs using the plonky2 proof system.
To find out more about zero-knowledge machine learning, check out the awesome-zkml repository we have created. It aggregates scientific research papers, codebases, articles, and use cases in the field of ZKML.
Worldcoin is a Privacy-Preserving Proof-of-Personhood Protocol. ZKML could help us make our protocol more trustless, and make it more easily upgradeable and auditabie.
cargo +nightly run --release -- -vvv --input-size 1000 --output-size 1000
# open Python CNN implementation directory
cd ref_cnn
# run CNN model and check result
python3 vanilla_cnn.py
# generate JSON files for the random number generated matrices in the model
python3 generate_cnn_json.py
cd ../
# run Rust CNN implementation and compare results against your previous results
cargo test serialize::tests::deserialize_nn_json -- --show-output
ArcArray<f32, IxDyn>.> python ref_cnn/vanilla_cnn.py
layer | output shape | #parameters | #ops
-------------------- | --------------- | --------------- | ---------------
conv 32x5x5x3 | (116, 76, 32) | 2400 | 21158400
max-pool | (58, 38, 32) | 0 | 0
relu | (58, 38, 32) | 0 | 0
conv 32x5x5x32 | (54, 34, 32) | 25600 | 47001600
max-pool | (27, 17, 32) | 0 | 0
relu | (27, 17, 32) | 0 | 0
flatten | (14688,) | 0 | 0
conv 1000x14688 | (1000,) | 14689000 | 14688000
relu | (1000,) | 0 | 0
conv 5x1000 | (5,) | 5005 | 5000
normalize | (5,) | 0 | 6
final output: [-0.11425511 -0.13403508 -0.41759714 -0.24778798 0.85626755]
---- serialize::tests::deserialize_nn_json stdout ----
layer | output shape | #parameters | #ops
-----------------------------------------------------------------------------
conv 32x5x5x3 | [116, 76, 32] | 2400 | 7052800
max-pool | [38, 58, 32] | 0 | 282112
relu | [58, 38, 32] | 70528 | 0
conv 32x5x5x32 | [54, 34, 32] | 25600 | 1468800
max-pool | [17, 27, 32] | 0 | 58752
relu | [27, 17, 32] | 14688 | 0
flatten | [14688] | 0 | 0
full | [1000] | 14689000 | 14688000
relu | [1000] | 1000 | 0
full | [5] | 5005 | 5000
normalize | [5] | 0 | 6
final output (normalized):
[-0.11425512, -0.13403504, -0.41759717, -0.24778795, 0.8562675]
cd ref_cnn
python benchmark_cnn.py
# generates matrices for the Rust implementation to use
python generate_cnn_json.py
cargo bench bench_neural_net
Machine: M1 Max Macbook Pro
The average time is 0.8297840171150046 seconds for 1000 runs
test nn::bench_neural_net ... bench: 151,632,316 ns/iter (+/- 1,469,992)
In this benchmark the Rust implementation is 5.5x faster!
Verify that all components of the rust codebase are working fine and that no breaking changes were introduced.
cargo test
In order to see output use cargo test -- --output, i.e.:
cargo test nn::tests::neural_net -- --show-output
Serializing the vanilla CNN model created with numpy into JSON and desearilizing the model into a NeuralNetwork Rust object
# change directory to cnn folder
cd ref_cnn
# generate json file for the model
python generate_cnn_json.py
cargo test serialize::tests::deserialize_model_json -- --show-output
# serializes a CNN model with random weights into src/json/nn.json
cargo test serialize::tests::serialize_model_json -- --show-output
Create a NeuralNetwork object with random weights in Rust, serialize it into JSON and deserialize back into a NeuralNetwork Rust object
# serializes a CNN model with random weights into src/json/nn.json
cargo test serialize::tests::serde_full_circle -- --show-output
Benchmarks for serializing and deserializing the reference CNN (Rust/JSON) using serde.
# full serialization benchmark times (M1 Max Macbook Pro)
# cargo bench - 579,057,637 ns/iter (+/- 20,202,535)
cargo bench bench_serialize_neural_net
# full deserialization benchmark times (M1 Max Macbook Pro)
# cargo bench - 565,564,850 ns/iter (+/- 61,387,641)
cargo bench bench_deserialize_neural_net
Rust
64.0%
Jupyter Notebook
19.0%
Python
17.0%