Skip to content

Commit

Permalink
chore(bench): benchmark server keys with wasm
Browse files Browse the repository at this point in the history
Benchmarks are run for 1_1 and 2_2 parameters set on compressed
server key.
  • Loading branch information
soonum committed Mar 14, 2024
1 parent 54ba8de commit b40897a
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 5 deletions.
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
3 changes: 3 additions & 0 deletions tfhe/web_wasm_parallel_tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@
<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="compressedServerKeyBenchMessage1Carry1" value="Compressed Server Key Bench 1_1" disabled />
<input type="button" id="compressedServerKeyBenchMessage2Carry2" value="Compressed Server Key Bench 2_2" disabled />

<input type="text" id="benchmarkResults" disabled>
<label for="benchmarkResults"> BenchmarkResults </label><br>

Expand Down
4 changes: 3 additions & 1 deletion tfhe/web_wasm_parallel_tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ async function setup() {
'compactPublicKeyBench32BitBig',
'compactPublicKeyBench32BitSmall',
'compactPublicKeyBench256BitBig',
'compactPublicKeyBench256BitSmall'
'compactPublicKeyBench256BitSmall',
'compressedServerKeyBenchMessage1Carry1',
'compressedServerKeyBenchMessage2Carry2',
]

function setupBtn(id) {
Expand Down
1 change: 1 addition & 0 deletions tfhe/web_wasm_parallel_tests/test/common.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ async function runTestAttachedToButton(buttonId) {

let page = await browser.newPage();

await page.setDefaultTimeout(300000) // Five minutes timeout
await page.goto('http://localhost:3000');
page.on('console', msg => console.log('PAGE LOG:', msg.text()));

Expand Down
12 changes: 12 additions & 0 deletions tfhe/web_wasm_parallel_tests/test/server-key.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { runTestAttachedToButton } from "./common.mjs";


it('Server Key Bench 1_1', async () => {
await runTestAttachedToButton('compressedServerKeyBenchMessage1Carry1')
},
300000); // Five minutes timeout

it('Server Key Bench 2_2', async () => {
await runTestAttachedToButton('compressedServerKeyBenchMessage2Carry2')
},
300000); // Five minutes timeout
58 changes: 58 additions & 0 deletions tfhe/web_wasm_parallel_tests/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,62 @@ async function compactPublicKeyBench256BitSmall() {
);
}

async function compressedServerKeyBenchConfig(config) {
const bench_loops = 35;
let bench_results = {};

console.log('Begin benchmarks') // DEBUG
let clientKey = TfheClientKey.generate(config);

// Bench the sk generation for bench_loops iterations
let start = performance.now();
for (let i = 0; i < bench_loops; i++) {
let _ = TfheCompressedServerKey.new(clientKey);
}
let end = performance.now();
const timing_1 = (end - start) / bench_loops
console.log('CompressedServerKey Gen bench: ', timing_1, ' ms');
bench_results["compressed_server_key_gen_mean"] = timing_1;

let serverKey = TfheCompressedServerKey.new(clientKey);
let serialized_key = serverKey.serialize();
console.log("Serialized ServerKey size: ", serialized_key.length);

// Bench the serialization for bench_loops iterations
start = performance.now();
for (let i = 0; i < bench_loops; i++) {
let _ = serverKey.serialize();
}
end = performance.now();
const timing_2 = (end - start) / bench_loops
console.log('CompressedServerKey serialization bench: ', timing_2, ' ms');
bench_results["compressed_server_key_serialization_mean"] = timing_2;

return bench_results;
}

async function compressedServerKeyBenchMessage1Carry1() {
const block_params = new ShortintParameters(ShortintParametersName.PARAM_MESSAGE_1_CARRY_1_KS_PBS);
let config = TfheConfigBuilder.default()
.use_custom_parameters(block_params)
.build();
return append_param_name(
await compressedServerKeyBenchConfig(config),
"PARAM_MESSAGE_1_CARRY_1_KS_PBS"
);
}

async function compressedServerKeyBenchMessage2Carry2() {
const block_params = new ShortintParameters(ShortintParametersName.PARAM_MESSAGE_2_CARRY_2_KS_PBS);
let config = TfheConfigBuilder.default()
.use_custom_parameters(block_params)
.build();
return append_param_name(
await compressedServerKeyBenchConfig(config),
"PARAM_MESSAGE_2_CARRY_2_KS_PBS"
);
}

async function main() {
await init()
await initThreadPool(navigator.hardwareConcurrency);
Expand All @@ -459,6 +515,8 @@ async function main() {
compactPublicKeyBench32BitSmall,
compactPublicKeyBench256BitBig,
compactPublicKeyBench256BitSmall,
compressedServerKeyBenchMessage1Carry1,
compressedServerKeyBenchMessage2Carry2,
})
}

Expand Down

0 comments on commit b40897a

Please sign in to comment.