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

chore(wasm): benchmark server keys and format javascript code #977

Merged
merged 5 commits into from
Mar 14, 2024
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
9 changes: 8 additions & 1 deletion .github/workflows/aws_tfhe_wasm_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,20 @@ jobs:
with:
toolchain: stable

- name: Install Node
run: |
make install_node

- name: Run fmt checks
run: |
make check_fmt_js

- name: Run js on wasm API tests
run: |
make test_nodejs_wasm_api_in_docker

- name: Run parallel wasm tests
run: |
make install_node
make ci_test_web_js_api_parallel

- name: Slack Notification
Expand Down
26 changes: 25 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,12 @@ install_wasm_pack: install_rs_build_toolchain

.PHONY: install_node # Install last version of NodeJS via nvm
install_node:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | $(SHELL)
curl -o nvm_install.sh https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh
@echo "2ed5e94ba12434370f0358800deb69f514e8bce90f13beb0e1b241d42c6abafd nvm_install.sh" > nvm_checksum
@sha256sum -c nvm_checksum
@rm nvm_checksum
$(SHELL) nvm_install.sh
@rm nvm_install.sh
source ~/.bashrc
$(SHELL) -i -c 'nvm install $(NODE_VERSION)' || \
( echo "Unable to install node, unknown error." && exit 1 )
Expand Down Expand Up @@ -149,10 +154,22 @@ check_actionlint_installed:
@actionlint --version > /dev/null 2>&1 || \
( echo "Unable to locate actionlint. Try installing it: https://github.com/rhysd/actionlint/releases" && exit 1 )

.PHONY: check_nvm_installed # Check if Node Version Manager is installed
check_nvm_installed:
@source ~/.nvm/nvm.sh && nvm --version > /dev/null 2>&1 || \
( echo "Unable to locate Node. Run 'make install_node'" && exit 1 )

.PHONY: fmt # Format rust code
fmt: install_rs_check_toolchain
cargo "$(CARGO_RS_CHECK_TOOLCHAIN)" fmt

.PHONY: fmt_js # Format javascript code
fmt_js: check_nvm_installed
source ~/.nvm/nvm.sh && \
nvm install $(NODE_VERSION) && \
nvm use $(NODE_VERSION) && \
$(MAKE) -C tfhe/web_wasm_parallel_tests fmt

.PHONY: fmt_gpu # Format rust and cuda code
fmt_gpu: install_rs_check_toolchain
cargo "$(CARGO_RS_CHECK_TOOLCHAIN)" fmt
Expand All @@ -167,6 +184,13 @@ check_fmt_gpu: install_rs_check_toolchain
cargo "$(CARGO_RS_CHECK_TOOLCHAIN)" fmt --check
cd "$(TFHECUDA_SRC)" && ./format_tfhe_cuda_backend.sh -c

.PHONY: check_fmt_js # Check javascript code format
check_fmt_js: check_nvm_installed
source ~/.nvm/nvm.sh && \
nvm install $(NODE_VERSION) && \
nvm use $(NODE_VERSION) && \
$(MAKE) -C tfhe/web_wasm_parallel_tests check_fmt

.PHONY: clippy_gpu # Run clippy lints on tfhe with "gpu" enabled
clippy_gpu: install_rs_check_toolchain
RUSTFLAGS="$(RUSTFLAGS)" cargo "$(CARGO_RS_CHECK_TOOLCHAIN)" clippy \
Expand Down
15 changes: 11 additions & 4 deletions tfhe/examples/utilities/wasm_benchmarks_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@ use std::io::Write;
use std::path::Path;
use tfhe::keycache::NamedParam;
use tfhe::shortint::keycache::{
PARAM_MESSAGE_2_CARRY_2_COMPACT_PK_KS_PBS_NAME, PARAM_MESSAGE_2_CARRY_2_COMPACT_PK_PBS_KS_NAME,
PARAM_MESSAGE_1_CARRY_1_KS_PBS_NAME, PARAM_MESSAGE_2_CARRY_2_COMPACT_PK_KS_PBS_NAME,
PARAM_MESSAGE_2_CARRY_2_COMPACT_PK_PBS_KS_NAME, PARAM_MESSAGE_2_CARRY_2_KS_PBS_NAME,
};
use tfhe::shortint::parameters::{
PARAM_MESSAGE_2_CARRY_2_COMPACT_PK_KS_PBS, PARAM_MESSAGE_2_CARRY_2_COMPACT_PK_PBS_KS,
PARAM_MESSAGE_1_CARRY_1_KS_PBS, PARAM_MESSAGE_2_CARRY_2_COMPACT_PK_KS_PBS,
PARAM_MESSAGE_2_CARRY_2_COMPACT_PK_PBS_KS, PARAM_MESSAGE_2_CARRY_2_KS_PBS,
};
use tfhe::shortint::{ClassicPBSParameters, PBSParameters};

const BENCHMARK_NAME_PREFIX: &str = "wasm::";

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
Expand All @@ -28,6 +32,8 @@ fn params_from_name(name: &str) -> ClassicPBSParameters {
match name.to_uppercase().as_str() {
PARAM_MESSAGE_2_CARRY_2_COMPACT_PK_KS_PBS_NAME => PARAM_MESSAGE_2_CARRY_2_COMPACT_PK_KS_PBS,
PARAM_MESSAGE_2_CARRY_2_COMPACT_PK_PBS_KS_NAME => PARAM_MESSAGE_2_CARRY_2_COMPACT_PK_PBS_KS,
PARAM_MESSAGE_1_CARRY_1_KS_PBS_NAME => PARAM_MESSAGE_1_CARRY_1_KS_PBS,
PARAM_MESSAGE_2_CARRY_2_KS_PBS_NAME => PARAM_MESSAGE_2_CARRY_2_KS_PBS,
_ => panic!("failed to get parameters for name '{name}'"),
}
}
Expand Down Expand Up @@ -56,14 +62,15 @@ pub fn parse_wasm_benchmarks(results_file: &Path, raw_results_dir: &Path) {
let results_as_json: HashMap<String, f32> = serde_json::from_str(&raw_results).unwrap();

for (full_name, val) in results_as_json.iter() {
let prefixed_full_name = format!("{BENCHMARK_NAME_PREFIX}{full_name}");
let name_parts = full_name.split("_mean_").collect::<Vec<_>>();
let bench_name = name_parts[0];
let params: PBSParameters = params_from_name(name_parts[1]).into();
let value_in_ns = (val * 1_000_000_f32) as usize;

write_result(&mut file, full_name, value_in_ns);
write_result(&mut file, &prefixed_full_name, value_in_ns);
write_to_json::<u64, _>(
full_name,
&prefixed_full_name,
params,
params.name(),
bench_name,
Expand Down
8 changes: 4 additions & 4 deletions tfhe/src/shortint/engine/server_side.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ impl ShortintEngine {
) -> CompressedServerKey {
let bootstrapping_key = match cks.parameters.pbs_parameters().unwrap() {
crate::shortint::PBSParameters::PBS(pbs_params) => {
#[cfg(not(feature = "__wasm_api"))]
#[cfg(any(not(feature = "__wasm_api"), feature = "parallel-wasm-api"))]
let bootstrapping_key = par_allocate_and_generate_new_seeded_lwe_bootstrap_key(
&cks.small_lwe_secret_key(),
&cks.glwe_secret_key,
Expand All @@ -204,7 +204,7 @@ impl ShortintEngine {
&mut self.seeder,
);

#[cfg(feature = "__wasm_api")]
#[cfg(all(feature = "__wasm_api", not(feature = "parallel-wasm-api")))]
let bootstrapping_key = allocate_and_generate_new_seeded_lwe_bootstrap_key(
&cks.small_lwe_secret_key(),
&cks.glwe_secret_key,
Expand All @@ -218,7 +218,7 @@ impl ShortintEngine {
ShortintCompressedBootstrappingKey::Classic(bootstrapping_key)
}
crate::shortint::PBSParameters::MultiBitPBS(pbs_params) => {
#[cfg(not(feature = "__wasm_api"))]
#[cfg(any(not(feature = "__wasm_api"), feature = "parallel-wasm-api"))]
let bootstrapping_key =
par_allocate_and_generate_new_seeded_lwe_multi_bit_bootstrap_key(
&cks.small_lwe_secret_key(),
Expand All @@ -231,7 +231,7 @@ impl ShortintEngine {
&mut self.seeder,
);

#[cfg(feature = "__wasm_api")]
#[cfg(all(feature = "__wasm_api", not(feature = "parallel-wasm-api")))]
let bootstrapping_key =
allocate_and_generate_new_seeded_lwe_multi_bit_bootstrap_key(
&cks.small_lwe_secret_key(),
Expand Down
4 changes: 4 additions & 0 deletions tfhe/web_wasm_parallel_tests/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dist/
node_modules/
pkg/
test/benchmark_results/
10 changes: 10 additions & 0 deletions tfhe/web_wasm_parallel_tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,13 @@ bench: run_server

.PHONY: bench-ci # Run benchmarks on web client in CI
bench-ci: run_server

.PHONY: fmt # Format Javascript code
fmt:
IceTDrinker marked this conversation as resolved.
Show resolved Hide resolved
npm install
npm run format

.PHONY: fmt # Check Javascript code format
check_fmt:
npm install
npm run check-format
6 changes: 1 addition & 5 deletions tfhe/web_wasm_parallel_tests/babel.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
const presets = [
[
"@babel/preset-env",
]
];
const presets = [["@babel/preset-env"]];

module.exports = { presets };
142 changes: 107 additions & 35 deletions tfhe/web_wasm_parallel_tests/index.html
Original file line number Diff line number Diff line change
@@ -1,49 +1,121 @@
<!DOCTYPE html>
<!doctype html>
<title>TFHE-RS Web Wasm Demo</title>
<body>
<style>
.loader {
border: 16px solid #f3f3f3; /* Light grey */
border-top: 16px solid #3498db; /* Blue */
border-radius: 50%;
width: 120px;
height: 120px;
animation: spin 2s linear infinite;
}
<style>
.loader {
border: 16px solid #f3f3f3; /* Light grey */
border-top: 16px solid #3498db; /* Blue */
border-radius: 50%;
width: 120px;
height: 120px;
animation: spin 2s linear infinite;
}

@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>

<div>
<input type="button" id="publicKeyTest" value="Public Key Test" disabled />
<input type="button" id="compressedPublicKeyTest" value="Compressed Public Key Test" disabled />
<div>
<input type="button" id="publicKeyTest" value="Public Key Test" disabled />
<input
type="button"
id="compressedPublicKeyTest"
value="Compressed Public Key Test"
disabled
/>

<input type="button" id="compactPublicKeyTest32BitSmall" value="Compact Public Key Test 32 Bits Small" disabled />
<input type="button" id="compactPublicKeyTest32BitBig" value="Compact Public Key Test 32 Bits Big" disabled />
<input
type="button"
id="compactPublicKeyTest32BitSmall"
value="Compact Public Key Test 32 Bits Small"
disabled
/>
<input
type="button"
id="compactPublicKeyTest32BitBig"
value="Compact Public Key Test 32 Bits Big"
disabled
/>

<input type="button" id="compactPublicKeyTest256BitSmall" value="Compact Public Key Test 256 Bits Small" disabled />
<input type="button" id="compactPublicKeyTest256BitBig" value="Compact Public Key Test 256 Bits Big" disabled />
<input
type="button"
id="compactPublicKeyTest256BitSmall"
value="Compact Public Key Test 256 Bits Small"
disabled
/>
<input
type="button"
id="compactPublicKeyTest256BitBig"
value="Compact Public Key Test 256 Bits Big"
disabled
/>

<input type="button" id="compressedCompactPublicKeyTest256BitSmall" value="Compressed Compact Public Key Test 256 Bits Small" disabled />
<input type="button" id="compressedCompactPublicKeyTest256BitBig" value="Compressed Compact Public Key Test 256 Bits Big" disabled />
<input
type="button"
id="compressedCompactPublicKeyTest256BitSmall"
value="Compressed Compact Public Key Test 256 Bits Small"
disabled
/>
<input
type="button"
id="compressedCompactPublicKeyTest256BitBig"
value="Compressed Compact Public Key Test 256 Bits Big"
disabled
/>

<input type="checkbox" id="testSuccess" disabled>
<label for="testSuccess"> TestSuccess </label><br>
<input type="checkbox" id="testSuccess" disabled />
<label for="testSuccess"> TestSuccess </label><br />

<input type="button" id="compactPublicKeyBench32BitSmall" value="Compact Public Key Bench 32 Bits Small" disabled />
<input type="button" id="compactPublicKeyBench32BitBig" value="Compact Public Key Bench 32 Bits Big" disabled />
<input
type="button"
id="compactPublicKeyBench32BitSmall"
value="Compact Public Key Bench 32 Bits Small"
disabled
/>
<input
type="button"
id="compactPublicKeyBench32BitBig"
value="Compact Public Key Bench 32 Bits Big"
disabled
/>

<input type="button" id="compactPublicKeyBench256BitSmall" value="Compact Public Key Bench 256 Bits Small" disabled />
<input type="button" id="compactPublicKeyBench256BitBig" value="Compact Public Key Bench 256 Bits Big" disabled />
<input
type="button"
id="compactPublicKeyBench256BitSmall"
value="Compact Public Key Bench 256 Bits Small"
disabled
/>
<input
type="button"
id="compactPublicKeyBench256BitBig"
value="Compact Public Key Bench 256 Bits Big"
disabled
/>

<input type="text" id="benchmarkResults" disabled>
<label for="benchmarkResults"> BenchmarkResults </label><br>
<input
type="button"
id="compressedServerKeyBenchMessage1Carry1"
value="Compressed Server Key Bench 1_1"
disabled
/>
<input
type="button"
id="compressedServerKeyBenchMessage2Carry2"
value="Compressed Server Key Bench 2_2"
disabled
/>

<div id="loader" class="loader" hidden></div>
</div>
<input type="text" id="benchmarkResults" disabled />
<label for="benchmarkResults"> BenchmarkResults </label><br />

<script type="module" src="index.js"></script>
<div id="loader" class="loader" hidden></div>
</div>

<script type="module" src="index.js"></script>
</body>
Loading
Loading