diff --git a/Cargo.toml b/Cargo.toml index c866d6d28..421ee22c2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ resolver = "2" members = [ "tfhe", + "tfhe-benchmark", "tfhe-fft", "tfhe-ntt", "tfhe-zk-pok", diff --git a/Makefile b/Makefile index 988cbacae..da55397ca 100644 --- a/Makefile +++ b/Makefile @@ -462,7 +462,7 @@ clippy_param_dedup: install_rs_check_toolchain .PHONY: clippy_all # Run all clippy targets clippy_all: clippy_rustdoc clippy clippy_boolean clippy_shortint clippy_integer clippy_all_targets \ clippy_c_api clippy_js_wasm_api clippy_tasks clippy_core clippy_tfhe_csprng clippy_zk_pok clippy_trivium \ -clippy_versionable clippy_tfhe_lints clippy_ws_tests clippy_param_dedup +clippy_versionable clippy_tfhe_lints clippy_ws_tests clippy_bench clippy_param_dedup .PHONY: clippy_fast # Run main clippy targets clippy_fast: clippy_rustdoc clippy clippy_all_targets clippy_c_api clippy_js_wasm_api clippy_tasks \ @@ -1088,6 +1088,18 @@ dieharder_csprng: install_dieharder build_tfhe_csprng # Benchmarks # +.PHONY: clippy_bench # Run clippy lints on tfhe-benchmark +clippy_bench: install_rs_check_toolchain + RUSTFLAGS="$(RUSTFLAGS)" cargo "$(CARGO_RS_CHECK_TOOLCHAIN)" clippy --all-targets \ + --features=boolean,shortint,integer,internal-keycache,nightly-avx512,pbs-stats,zk-pok \ + -p tfhe-benchmark -- --no-deps -D warnings + +.PHONY: clippy_bench_gpu # Run clippy lints on tfhe-benchmark +clippy_bench_gpu: install_rs_check_toolchain + RUSTFLAGS="$(RUSTFLAGS)" cargo "$(CARGO_RS_CHECK_TOOLCHAIN)" clippy --all-targets \ + --features=gpu,shortint,integer,internal-keycache,nightly-avx512,pbs-stats,zk-pok \ + -p tfhe-benchmark -- --no-deps -D warnings + .PHONY: print_doc_bench_parameters # Print parameters used in doc benchmarks print_doc_bench_parameters: RUSTFLAGS="" cargo run --example print_doc_bench_parameters \ @@ -1098,49 +1110,49 @@ bench_integer: install_rs_check_toolchain RUSTFLAGS="$(RUSTFLAGS)" __TFHE_RS_BENCH_OP_FLAVOR=$(BENCH_OP_FLAVOR) __TFHE_RS_FAST_BENCH=$(FAST_BENCH) __TFHE_RS_BENCH_TYPE=$(BENCH_TYPE) \ cargo $(CARGO_RS_CHECK_TOOLCHAIN) bench \ --bench integer-bench \ - --features=integer,internal-keycache,nightly-avx512,pbs-stats -p $(TFHE_SPEC) -- + --features=integer,internal-keycache,nightly-avx512,pbs-stats -p tfhe-benchmark -- .PHONY: bench_signed_integer # Run benchmarks for signed integer bench_signed_integer: install_rs_check_toolchain RUSTFLAGS="$(RUSTFLAGS)" __TFHE_RS_BENCH_OP_FLAVOR=$(BENCH_OP_FLAVOR) __TFHE_RS_FAST_BENCH=$(FAST_BENCH) __TFHE_RS_BENCH_TYPE=$(BENCH_TYPE) \ cargo $(CARGO_RS_CHECK_TOOLCHAIN) bench \ --bench integer-signed-bench \ - --features=integer,internal-keycache,nightly-avx512,pbs-stats -p $(TFHE_SPEC) -- + --features=integer,internal-keycache,nightly-avx512,pbs-stats -p tfhe-benchmark -- .PHONY: bench_integer_gpu # Run benchmarks for integer on GPU backend bench_integer_gpu: install_rs_check_toolchain RUSTFLAGS="$(RUSTFLAGS)" __TFHE_RS_BENCH_OP_FLAVOR=$(BENCH_OP_FLAVOR) __TFHE_RS_FAST_BENCH=$(FAST_BENCH) __TFHE_RS_BENCH_TYPE=$(BENCH_TYPE) \ cargo $(CARGO_RS_CHECK_TOOLCHAIN) bench \ --bench integer-bench \ - --features=integer,gpu,internal-keycache,nightly-avx512,pbs-stats -p $(TFHE_SPEC) -- + --features=integer,gpu,internal-keycache,nightly-avx512,pbs-stats -p tfhe-benchmark -- .PHONY: bench_signed_integer_gpu # Run benchmarks for signed integer on GPU backend bench_signed_integer_gpu: install_rs_check_toolchain RUSTFLAGS="$(RUSTFLAGS)" __TFHE_RS_BENCH_OP_FLAVOR=$(BENCH_OP_FLAVOR) __TFHE_RS_FAST_BENCH=$(FAST_BENCH) __TFHE_RS_BENCH_TYPE=$(BENCH_TYPE) \ cargo $(CARGO_RS_CHECK_TOOLCHAIN) bench \ --bench integer-signed-bench \ - --features=integer,gpu,internal-keycache,nightly-avx512,pbs-stats -p $(TFHE_SPEC) -- + --features=integer,gpu,internal-keycache,nightly-avx512,pbs-stats -p tfhe-benchmark -- .PHONY: bench_integer_compression # Run benchmarks for unsigned integer compression bench_integer_compression: install_rs_check_toolchain RUSTFLAGS="$(RUSTFLAGS)" __TFHE_RS_BENCH_TYPE=$(BENCH_TYPE) \ cargo $(CARGO_RS_CHECK_TOOLCHAIN) bench \ --bench glwe_packing_compression-integer-bench \ - --features=integer,internal-keycache,nightly-avx512,pbs-stats -p $(TFHE_SPEC) -- + --features=integer,internal-keycache,nightly-avx512,pbs-stats -p tfhe-benchmark -- .PHONY: bench_integer_compression_gpu bench_integer_compression_gpu: install_rs_check_toolchain RUSTFLAGS="$(RUSTFLAGS)" __TFHE_RS_BENCH_TYPE=$(BENCH_TYPE) \ cargo $(CARGO_RS_CHECK_TOOLCHAIN) bench \ --bench glwe_packing_compression-integer-bench \ - --features=integer,internal-keycache,gpu,pbs-stats -p $(TFHE_SPEC) -- + --features=integer,internal-keycache,gpu,pbs-stats -p tfhe-benchmark -- .PHONY: bench_integer_zk_gpu bench_integer_zk_gpu: install_rs_check_toolchain RUSTFLAGS="$(RUSTFLAGS)" __TFHE_RS_BENCH_TYPE=$(BENCH_TYPE) \ cargo $(CARGO_RS_CHECK_TOOLCHAIN) bench \ --bench zk-pke-bench \ - --features=integer,internal-keycache,gpu,pbs-stats,zk-pok -p $(TFHE_SPEC) -- + --features=integer,internal-keycache,gpu,pbs-stats,zk-pok -p tfhe-benchmark -- .PHONY: bench_integer_multi_bit # Run benchmarks for unsigned integer using multi-bit parameters bench_integer_multi_bit: install_rs_check_toolchain @@ -1148,7 +1160,7 @@ bench_integer_multi_bit: install_rs_check_toolchain __TFHE_RS_BENCH_OP_FLAVOR=$(BENCH_OP_FLAVOR) __TFHE_RS_FAST_BENCH=$(FAST_BENCH) \ cargo $(CARGO_RS_CHECK_TOOLCHAIN) bench \ --bench integer-bench \ - --features=integer,internal-keycache,nightly-avx512,pbs-stats -p $(TFHE_SPEC) -- + --features=integer,internal-keycache,nightly-avx512,pbs-stats -p tfhe-benchmark -- .PHONY: bench_signed_integer_multi_bit # Run benchmarks for signed integer using multi-bit parameters bench_signed_integer_multi_bit: install_rs_check_toolchain @@ -1156,7 +1168,7 @@ bench_signed_integer_multi_bit: install_rs_check_toolchain __TFHE_RS_BENCH_OP_FLAVOR=$(BENCH_OP_FLAVOR) __TFHE_RS_FAST_BENCH=$(FAST_BENCH) \ cargo $(CARGO_RS_CHECK_TOOLCHAIN) bench \ --bench integer-signed-bench \ - --features=integer,internal-keycache,nightly-avx512,pbs-stats -p $(TFHE_SPEC) -- + --features=integer,internal-keycache,nightly-avx512,pbs-stats -p tfhe-benchmark -- .PHONY: bench_integer_multi_bit_gpu # Run benchmarks for integer on GPU backend using multi-bit parameters bench_integer_multi_bit_gpu: install_rs_check_toolchain @@ -1164,7 +1176,7 @@ bench_integer_multi_bit_gpu: install_rs_check_toolchain __TFHE_RS_BENCH_OP_FLAVOR=$(BENCH_OP_FLAVOR) __TFHE_RS_FAST_BENCH=$(FAST_BENCH) __TFHE_RS_BENCH_TYPE=$(BENCH_TYPE) \ cargo $(CARGO_RS_CHECK_TOOLCHAIN) bench \ --bench integer-bench \ - --features=integer,gpu,internal-keycache,nightly-avx512,pbs-stats -p $(TFHE_SPEC) -- + --features=integer,gpu,internal-keycache,nightly-avx512,pbs-stats -p tfhe-benchmark -- .PHONY: bench_signed_integer_multi_bit_gpu # Run benchmarks for signed integer on GPU backend using multi-bit parameters bench_signed_integer_multi_bit_gpu: install_rs_check_toolchain @@ -1172,7 +1184,7 @@ bench_signed_integer_multi_bit_gpu: install_rs_check_toolchain __TFHE_RS_BENCH_OP_FLAVOR=$(BENCH_OP_FLAVOR) __TFHE_RS_FAST_BENCH=$(FAST_BENCH) __TFHE_RS_BENCH_TYPE=$(BENCH_TYPE) \ cargo $(CARGO_RS_CHECK_TOOLCHAIN) bench \ --bench integer-signed-bench \ - --features=integer,gpu,internal-keycache,nightly-avx512,pbs-stats -p $(TFHE_SPEC) -- + --features=integer,gpu,internal-keycache,nightly-avx512,pbs-stats -p tfhe-benchmark -- .PHONY: bench_integer_zk # Run benchmarks for integer encryption with ZK proofs bench_integer_zk: install_rs_check_toolchain @@ -1180,91 +1192,83 @@ bench_integer_zk: install_rs_check_toolchain cargo $(CARGO_RS_CHECK_TOOLCHAIN) bench \ --bench zk-pke-bench \ --features=integer,internal-keycache,zk-pok,nightly-avx512,pbs-stats \ - -p $(TFHE_SPEC) -- + -p tfhe-benchmark -- .PHONY: bench_shortint # Run benchmarks for shortint bench_shortint: install_rs_check_toolchain - RUSTFLAGS="$(RUSTFLAGS)" __TFHE_RS_BENCH_OP_FLAVOR=$(BENCH_OP_FLAVOR) \ + RUSTFLAGS="$(RUSTFLAGS)" __TFHE_RS_BENCH_OP_FLAVOR=$(BENCH_OP_FLAVOR) __TFHE_RS_PARAMS_SET=$(BENCH_PARAMS_SET) __TFHE_RS_BENCH_TYPE=$(BENCH_TYPE) \ cargo $(CARGO_RS_CHECK_TOOLCHAIN) bench \ --bench shortint-bench \ - --features=shortint,internal-keycache,nightly-avx512 -p $(TFHE_SPEC) + --features=shortint,internal-keycache,nightly-avx512 -p tfhe-benchmark .PHONY: bench_shortint_oprf # Run benchmarks for shortint bench_shortint_oprf: install_rs_check_toolchain - RUSTFLAGS="$(RUSTFLAGS)" \ + RUSTFLAGS="$(RUSTFLAGS)" __TFHE_RS_PARAMS_SET=$(BENCH_PARAMS_SET) \ cargo $(CARGO_RS_CHECK_TOOLCHAIN) bench \ --bench oprf-shortint-bench \ - --features=shortint,internal-keycache,nightly-avx512 -p $(TFHE_SPEC) - -.PHONY: bench_shortint_multi_bit # Run benchmarks for shortint using multi-bit parameters -bench_shortint_multi_bit: install_rs_check_toolchain - RUSTFLAGS="$(RUSTFLAGS)" __TFHE_RS_PARAM_TYPE=MULTI_BIT \ - __TFHE_RS_BENCH_OP_FLAVOR=$(BENCH_OP_FLAVOR) \ - cargo $(CARGO_RS_CHECK_TOOLCHAIN) bench \ - --bench shortint-bench \ - --features=shortint,internal-keycache,nightly-avx512 -p $(TFHE_SPEC) -- + --features=shortint,internal-keycache,nightly-avx512 -p tfhe-benchmark .PHONY: bench_boolean # Run benchmarks for boolean bench_boolean: install_rs_check_toolchain RUSTFLAGS="$(RUSTFLAGS)" cargo $(CARGO_RS_CHECK_TOOLCHAIN) bench \ --bench boolean-bench \ - --features=boolean,internal-keycache,nightly-avx512 -p $(TFHE_SPEC) + --features=boolean,internal-keycache,nightly-avx512 -p tfhe-benchmark .PHONY: bench_ks # Run benchmarks for keyswitch bench_ks: install_rs_check_toolchain RUSTFLAGS="$(RUSTFLAGS)" __TFHE_RS_PARAM_TYPE=$(BENCH_PARAM_TYPE) __TFHE_RS_PARAMS_SET=$(BENCH_PARAMS_SET) __TFHE_RS_BENCH_TYPE=$(BENCH_TYPE) \ cargo $(CARGO_RS_CHECK_TOOLCHAIN) bench \ --bench ks-bench \ - --features=boolean,shortint,internal-keycache,nightly-avx512 -p $(TFHE_SPEC) + --features=boolean,shortint,internal-keycache,nightly-avx512 -p tfhe-benchmark .PHONY: bench_ks_gpu # Run benchmarks for keyswitch on GPU backend bench_ks_gpu: install_rs_check_toolchain RUSTFLAGS="$(RUSTFLAGS)" __TFHE_RS_PARAM_TYPE=$(BENCH_PARAM_TYPE) __TFHE_RS_PARAMS_SET=$(BENCH_PARAMS_SET) __TFHE_RS_BENCH_TYPE=$(BENCH_TYPE) \ cargo $(CARGO_RS_CHECK_TOOLCHAIN) bench \ --bench ks-bench \ - --features=boolean,shortint,gpu,internal-keycache,nightly-avx512 -p $(TFHE_SPEC) + --features=boolean,shortint,gpu,internal-keycache,nightly-avx512 -p tfhe-benchmark .PHONY: bench_pbs # Run benchmarks for PBS bench_pbs: install_rs_check_toolchain RUSTFLAGS="$(RUSTFLAGS)" __TFHE_RS_PARAM_TYPE=$(BENCH_PARAM_TYPE) __TFHE_RS_PARAMS_SET=$(BENCH_PARAMS_SET) __TFHE_RS_BENCH_TYPE=$(BENCH_TYPE) \ cargo $(CARGO_RS_CHECK_TOOLCHAIN) bench \ --bench pbs-bench \ - --features=boolean,shortint,internal-keycache,nightly-avx512 -p $(TFHE_SPEC) + --features=boolean,shortint,internal-keycache,nightly-avx512 -p tfhe-benchmark .PHONY: bench_pbs_gpu # Run benchmarks for PBS on GPU backend bench_pbs_gpu: install_rs_check_toolchain RUSTFLAGS="$(RUSTFLAGS)" __TFHE_RS_PARAM_TYPE=$(BENCH_PARAM_TYPE) __TFHE_RS_FAST_BENCH=$(FAST_BENCH) __TFHE_RS_PARAMS_SET=$(BENCH_PARAMS_SET) __TFHE_RS_BENCH_TYPE=$(BENCH_TYPE) \ cargo $(CARGO_RS_CHECK_TOOLCHAIN) bench \ --bench pbs-bench \ - --features=boolean,shortint,gpu,internal-keycache,nightly-avx512 -p $(TFHE_SPEC) + --features=boolean,shortint,gpu,internal-keycache,nightly-avx512 -p tfhe-benchmark .PHONY: bench_ks_pbs # Run benchmarks for KS-PBS bench_ks_pbs: install_rs_check_toolchain RUSTFLAGS="$(RUSTFLAGS)" __TFHE_RS_PARAM_TYPE=$(BENCH_PARAM_TYPE) __TFHE_RS_PARAMS_SET=$(BENCH_PARAMS_SET) __TFHE_RS_BENCH_TYPE=$(BENCH_TYPE) \ cargo $(CARGO_RS_CHECK_TOOLCHAIN) bench \ --bench ks-pbs-bench \ - --features=boolean,shortint,internal-keycache,nightly-avx512 -p $(TFHE_SPEC) + --features=boolean,shortint,internal-keycache,nightly-avx512 -p tfhe-benchmark .PHONY: bench_ks_pbs_gpu # Run benchmarks for KS-PBS on GPU backend bench_ks_pbs_gpu: install_rs_check_toolchain RUSTFLAGS="$(RUSTFLAGS)" __TFHE_RS_PARAM_TYPE=$(BENCH_PARAM_TYPE) __TFHE_RS_PARAMS_SET=$(BENCH_PARAMS_SET) __TFHE_RS_BENCH_TYPE=$(BENCH_TYPE) \ cargo $(CARGO_RS_CHECK_TOOLCHAIN) bench \ --bench ks-pbs-bench \ - --features=boolean,shortint,gpu,internal-keycache,nightly-avx512 -p $(TFHE_SPEC) + --features=boolean,shortint,gpu,internal-keycache,nightly-avx512 -p tfhe-benchmark .PHONY: bench_pbs128 # Run benchmarks for PBS using FFT 128 bits bench_pbs128: install_rs_check_toolchain RUSTFLAGS="$(RUSTFLAGS)" __TFHE_RS_BENCH_TYPE=$(BENCH_TYPE) \ cargo $(CARGO_RS_CHECK_TOOLCHAIN) bench \ --bench pbs128-bench \ - --features=boolean,shortint,internal-keycache,nightly-avx512 -p $(TFHE_SPEC) + --features=boolean,shortint,internal-keycache,nightly-avx512 -p tfhe-benchmark .PHONY: bench_pbs128_gpu # Run benchmarks for PBS using FFT 128 bits on GPU bench_pbs128_gpu: install_rs_check_toolchain RUSTFLAGS="$(RUSTFLAGS)" __TFHE_RS_BENCH_TYPE=$(BENCH_TYPE) \ cargo $(CARGO_RS_CHECK_TOOLCHAIN) bench \ --bench pbs128-bench \ - --features=boolean,shortint,gpu,internal-keycache,nightly-avx512 -p $(TFHE_SPEC) + --features=boolean,shortint,gpu,internal-keycache,nightly-avx512 -p tfhe-benchmark bench_web_js_api_parallel_chrome: browser_path = "$(WEB_RUNNER_DIR)/chrome/chrome-linux64/chrome" bench_web_js_api_parallel_chrome: driver_path = "$(WEB_RUNNER_DIR)/chrome/chromedriver-linux64/chromedriver" @@ -1296,29 +1300,29 @@ bench_web_js_api_parallel_firefox_ci: setup_venv nvm use $(NODE_VERSION) && \ $(MAKE) bench_web_js_api_parallel_firefox -.PHONY: bench_hlapi_erc20 # Run benchmarks for ECR20 operations +.PHONY: bench_hlapi_erc20 # Run benchmarks for ERC20 operations bench_hlapi_erc20: install_rs_check_toolchain RUSTFLAGS="$(RUSTFLAGS)" cargo $(CARGO_RS_CHECK_TOOLCHAIN) bench \ --bench hlapi-erc20 \ - --features=integer,internal-keycache,pbs-stats,nightly-avx512 -p $(TFHE_SPEC) -- + --features=integer,internal-keycache,pbs-stats,nightly-avx512 -p tfhe-benchmark -- -.PHONY: bench_hlapi_erc20_gpu # Run benchmarks for ECR20 operations on GPU +.PHONY: bench_hlapi_erc20_gpu # Run benchmarks for ERC20 operations on GPU bench_hlapi_erc20_gpu: install_rs_check_toolchain RUSTFLAGS="$(RUSTFLAGS)" cargo $(CARGO_RS_CHECK_TOOLCHAIN) bench \ --bench hlapi-erc20 \ - --features=integer,gpu,internal-keycache,pbs-stats,nightly-avx512 -p $(TFHE_SPEC) -- + --features=integer,gpu,internal-keycache,pbs-stats,nightly-avx512 -p tfhe-benchmark -- -.PHONY: bench_hlapi_dex # Run benchmarks for ECR20 operations +.PHONY: bench_hlapi_dex # Run benchmarks for DEX operations bench_hlapi_dex: install_rs_check_toolchain RUSTFLAGS="$(RUSTFLAGS)" cargo $(CARGO_RS_CHECK_TOOLCHAIN) bench \ --bench hlapi-dex \ - --features=integer,internal-keycache,pbs-stats,nightly-avx512 -p $(TFHE_SPEC) -- + --features=integer,internal-keycache,pbs-stats,nightly-avx512 -p tfhe-benchmark -- -.PHONY: bench_hlapi_dex_gpu # Run benchmarks for ECR20 operations on GPU +.PHONY: bench_hlapi_dex_gpu # Run benchmarks for DEX operations on GPU bench_hlapi_dex_gpu: install_rs_check_toolchain RUSTFLAGS="$(RUSTFLAGS)" cargo $(CARGO_RS_CHECK_TOOLCHAIN) bench \ --bench hlapi-dex \ - --features=integer,gpu,internal-keycache,pbs-stats,nightly-avx512 -p $(TFHE_SPEC) -- + --features=integer,gpu,internal-keycache,pbs-stats,nightly-avx512 -p tfhe-benchmark -- .PHONY: bench_tfhe_zk_pok # Run benchmarks for the tfhe_zk_pok crate bench_tfhe_zk_pok: install_rs_check_toolchain @@ -1345,20 +1349,23 @@ gen_key_cache_core_crypto: install_rs_build_toolchain .PHONY: measure_hlapi_compact_pk_ct_sizes # Measure sizes of public keys and ciphertext for high-level API measure_hlapi_compact_pk_ct_sizes: install_rs_check_toolchain RUSTFLAGS="$(RUSTFLAGS)" cargo $(CARGO_RS_CHECK_TOOLCHAIN) run --profile $(CARGO_PROFILE) \ - --example hlapi_compact_pk_ct_sizes \ - --features=integer,internal-keycache + --bin hlapi_compact_pk_ct_sizes \ + --features=integer,internal-keycache \ + -p tfhe-benchmark .PHONY: measure_shortint_key_sizes # Measure sizes of bootstrapping and key switching keys for shortint measure_shortint_key_sizes: install_rs_check_toolchain RUSTFLAGS="$(RUSTFLAGS)" cargo $(CARGO_RS_CHECK_TOOLCHAIN) run --profile $(CARGO_PROFILE) \ - --example shortint_key_sizes \ - --features=shortint,internal-keycache + --bin shortint_key_sizes \ + --features=shortint,internal-keycache \ + -p tfhe-benchmark .PHONY: measure_boolean_key_sizes # Measure sizes of bootstrapping and key switching keys for boolean measure_boolean_key_sizes: install_rs_check_toolchain RUSTFLAGS="$(RUSTFLAGS)" cargo $(CARGO_RS_CHECK_TOOLCHAIN) run --profile $(CARGO_PROFILE) \ - --example boolean_key_sizes \ - --features=boolean,internal-keycache + --bin boolean_key_sizes \ + --features=boolean,internal-keycache \ + -p tfhe-benchmark .PHONY: parse_integer_benches # Run python parser to output a csv containing integer benches data parse_integer_benches: @@ -1369,8 +1376,9 @@ parse_integer_benches: .PHONY: parse_wasm_benchmarks # Parse benchmarks performed with WASM web client into a CSV file parse_wasm_benchmarks: install_rs_check_toolchain RUSTFLAGS="$(RUSTFLAGS)" cargo $(CARGO_RS_CHECK_TOOLCHAIN) run --profile $(CARGO_PROFILE) \ - --example wasm_benchmarks_parser \ + --bin wasm_benchmarks_parser \ --features=shortint,internal-keycache \ + -p tfhe-benchmark \ -- wasm_benchmark_results.json .PHONY: write_params_to_file # Gather all crypto parameters into a file with a Sage readable format. @@ -1413,7 +1421,7 @@ tfhe_lints .PHONY: pcc_gpu # pcc stands for pre commit checks for GPU compilation pcc_gpu: check_rust_bindings_did_not_change clippy_rustdoc_gpu \ -clippy_gpu clippy_cuda_backend check_compile_tests_benches_gpu +clippy_gpu clippy_cuda_backend clippy_bench_gpu check_compile_tests_benches_gpu .PHONY: fpcc # pcc stands for pre commit checks, the f stands for fast fpcc: no_tfhe_typo no_dbg_log check_parameter_export_ok check_fmt check_typos lint_doc \ diff --git a/tfhe-benchmark/Cargo.toml b/tfhe-benchmark/Cargo.toml new file mode 100644 index 000000000..05a39bf0d --- /dev/null +++ b/tfhe-benchmark/Cargo.toml @@ -0,0 +1,157 @@ +[package] +name = "tfhe-benchmark" +version = "0.1.0" +edition = "2021" +homepage = "https://zama.ai/" +documentation = "https://docs.zama.ai/tfhe-rs" +repository = "https://github.com/zama-ai/tfhe-rs" +license = "BSD-3-Clause-Clear" +description = "tfhe-benchmark: Performances measurements facility for tfhe-rs." +rust-version = "1.84" +publish = false + +[lib] +name = "benchmark" +path = "src/lib.rs" + +[dependencies] +bincode = "1.3.3" +# clap has to be pinned as its minimum supported rust version +# changes often between minor releases, which breaks our CI +clap = { version = "=4.4.4", features = ["derive"] } +criterion = "0.5.1" +dyn-stack = { workspace = true, features = ["default"] } +itertools = "0.14" +serde = { version = "1.0", default-features = false } +serde_json = "1.0.94" +paste = "1.0.7" +rand = { workspace = true } +rayon = { workspace = true } +tfhe = { path = "../tfhe" } +tfhe-csprng = { path = "../tfhe-csprng" } + +[features] +boolean = ["tfhe/boolean"] +shortint = ["tfhe/shortint"] +integer = ["shortint", "tfhe/integer"] +gpu = ["tfhe/gpu"] +internal-keycache = ["tfhe/internal-keycache"] +nightly-avx512 = ["tfhe/nightly-avx512"] +pbs-stats = ["tfhe/pbs-stats"] +zk-pok = ["tfhe/zk-pok"] + +[[bench]] +name = "boolean-bench" +path = "benches/boolean/bench.rs" +harness = false +required-features = ["boolean", "internal-keycache"] + +[[bench]] +name = "shortint-bench" +path = "benches/shortint/bench.rs" +harness = false +required-features = ["shortint", "internal-keycache"] + +[[bench]] +name = "oprf-shortint-bench" +path = "benches/shortint/oprf.rs" +harness = false +required-features = ["shortint", "internal-keycache"] + +[[bench]] +name = "glwe_packing_compression-shortint-bench" +path = "benches/shortint/glwe_packing_compression.rs" +harness = false +required-features = ["shortint", "internal-keycache"] + +[[bench]] +name = "hlapi" +path = "benches/high_level_api/bench.rs" +harness = false +required-features = ["integer", "internal-keycache"] + +[[bench]] +name = "hlapi-erc20" +path = "benches/high_level_api/erc20.rs" +harness = false +required-features = ["integer", "internal-keycache"] + +[[bench]] +name = "hlapi-dex" +path = "benches/high_level_api/dex.rs" +harness = false +required-features = ["integer", "internal-keycache"] + +[[bench]] +name = "glwe_packing_compression-integer-bench" +path = "benches/integer/glwe_packing_compression.rs" +harness = false +required-features = ["integer", "pbs-stats", "internal-keycache"] + +[[bench]] +name = "integer-bench" +path = "benches/integer/bench.rs" +harness = false +required-features = ["integer", "pbs-stats", "internal-keycache"] + +[[bench]] +name = "integer-signed-bench" +path = "benches/integer/signed_bench.rs" +harness = false +required-features = ["integer", "pbs-stats", "internal-keycache"] + +[[bench]] +name = "zk-pke-bench" +path = "benches/integer/zk_pke.rs" +harness = false +required-features = ["integer", "zk-pok", "pbs-stats", "internal-keycache"] + +[[bench]] +name = "ks-bench" +path = "benches/core_crypto/ks_bench.rs" +harness = false +required-features = ["shortint", "internal-keycache"] + +[[bench]] +name = "pbs-bench" +path = "benches/core_crypto/pbs_bench.rs" +harness = false +required-features = ["boolean", "shortint", "internal-keycache"] + +[[bench]] +name = "ks-pbs-bench" +path = "benches/core_crypto/ks_pbs_bench.rs" +harness = false +required-features = ["shortint", "internal-keycache"] + +[[bench]] +name = "modulus_switch_noise_reduction" +path = "benches/core_crypto/modulus_switch_noise_reduction.rs" +harness = false +required-features = ["shortint"] + +[[bench]] +name = "pbs128-bench" +path = "benches/core_crypto/pbs128_bench.rs" +harness = false +required-features = ["shortint"] + +[[bin]] +name = "boolean_key_sizes" +path = "src/bin/boolean_key_sizes.rs" +required-features = ["boolean", "internal-keycache"] + +[[bin]] +name = "shortint_key_sizes" +path = "src/bin/shortint_key_sizes.rs" +required-features = ["shortint", "internal-keycache"] + +[[bin]] +name = "hlapi_compact_pk_ct_sizes" +path = "src/bin/hlapi_compact_pk_ct_sizes.rs" +required-features = ["integer", "internal-keycache"] + +[[bin]] +name = "wasm_benchmarks_parser" +path = "src/bin/wasm_benchmarks_parser.rs" +required-features = ["shortint", "internal-keycache"] diff --git a/tfhe-benchmark/LICENSE b/tfhe-benchmark/LICENSE new file mode 100644 index 000000000..48312e88a --- /dev/null +++ b/tfhe-benchmark/LICENSE @@ -0,0 +1,28 @@ +BSD 3-Clause Clear License + +Copyright © 2025 ZAMA. +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this +list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this +list of conditions and the following disclaimer in the documentation and/or other +materials provided with the distribution. + +3. Neither the name of ZAMA nor the names of its contributors may be used to endorse +or promote products derived from this software without specific prior written permission. + +NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. +THIS SOFTWARE IS PROVIDED BY THE ZAMA AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR +IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +ZAMA OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, +OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND +ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING +NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF +ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/tfhe/benches/boolean/bench.rs b/tfhe-benchmark/benches/boolean/bench.rs similarity index 96% rename from tfhe/benches/boolean/bench.rs rename to tfhe-benchmark/benches/boolean/bench.rs index 21080baf7..d68fd473a 100644 --- a/tfhe/benches/boolean/bench.rs +++ b/tfhe-benchmark/benches/boolean/bench.rs @@ -1,7 +1,4 @@ -#[path = "../utilities.rs"] -mod utilities; - -use crate::utilities::{write_to_json, CryptoParametersRecord, OperatorType}; +use benchmark::utilities::{write_to_json, CryptoParametersRecord, OperatorType}; use criterion::{black_box, criterion_group, criterion_main, Criterion}; use tfhe::boolean::client_key::ClientKey; use tfhe::boolean::parameters::{ diff --git a/tfhe/benches/core_crypto/ks_bench.rs b/tfhe-benchmark/benches/core_crypto/ks_bench.rs similarity index 82% rename from tfhe/benches/core_crypto/ks_bench.rs rename to tfhe-benchmark/benches/core_crypto/ks_bench.rs index c572f3c64..118263846 100644 --- a/tfhe/benches/core_crypto/ks_bench.rs +++ b/tfhe-benchmark/benches/core_crypto/ks_bench.rs @@ -1,186 +1,19 @@ -#[path = "../utilities.rs"] -mod utilities; - -use crate::utilities::{ - filter_parameters, get_bench_type, init_parameters_set, throughput_num_threads, write_to_json, - BenchmarkType, CryptoParametersRecord, DesiredBackend, DesiredNoiseDistribution, OperatorType, - ParametersSet, PARAMETERS_SET, +#[cfg(feature = "boolean")] +use benchmark::params::benchmark_32bits_parameters; +use benchmark::params::{ + benchmark_compression_parameters, benchmark_parameters, multi_bit_benchmark_parameters, +}; +use benchmark::utilities::{ + get_bench_type, throughput_num_threads, write_to_json, BenchmarkType, CryptoParametersRecord, + OperatorType, }; use criterion::{black_box, Criterion, Throughput}; use rayon::prelude::*; use serde::Serialize; use std::env; -use tfhe::boolean::prelude::*; use tfhe::core_crypto::prelude::*; -use tfhe::keycache::NamedParam; -use tfhe::shortint::parameters::current_params::{ - V1_2_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128, VEC_ALL_MULTI_BIT_PBS_PARAMETERS, -}; -#[cfg(not(feature = "gpu"))] -use tfhe::shortint::parameters::current_params::{ - V1_2_PARAM_MESSAGE_4_CARRY_4_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MULTI_BIT_GROUP_2_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M64, - V1_2_PARAM_MULTI_BIT_GROUP_2_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M64, - V1_2_PARAM_MULTI_BIT_GROUP_2_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M64, - V1_2_PARAM_MULTI_BIT_GROUP_3_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M64, - V1_2_PARAM_MULTI_BIT_GROUP_3_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M64, - V1_2_PARAM_MULTI_BIT_GROUP_3_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M64, -}; -use tfhe::shortint::parameters::v1_2::VEC_ALL_CLASSIC_PBS_PARAMETERS; -use tfhe::shortint::parameters::{ - COMP_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, - PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, -}; -#[cfg(feature = "gpu")] -use tfhe::shortint::parameters::{ - PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128, - PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_1_CARRY_1_KS_PBS_TUNIFORM_2M128, - PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, - PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, - PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128, - PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_3_CARRY_3_KS_PBS_TUNIFORM_2M128, -}; -use tfhe::shortint::prelude::*; -use tfhe::shortint::{MultiBitPBSParameters, PBSParameters}; - -#[cfg(not(feature = "gpu"))] -const SHORTINT_BENCH_PARAMS: [ClassicPBSParameters; 5] = [ - PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, - V1_2_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_4_CARRY_4_KS_PBS_GAUSSIAN_2M128, -]; - -#[cfg(feature = "gpu")] -const SHORTINT_BENCH_PARAMS: [ClassicPBSParameters; 4] = [ - PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, - V1_2_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128, -]; - -#[cfg(not(feature = "gpu"))] -const SHORTINT_MULTI_BIT_BENCH_PARAMS: [MultiBitPBSParameters; 6] = [ - V1_2_PARAM_MULTI_BIT_GROUP_2_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M64, - V1_2_PARAM_MULTI_BIT_GROUP_2_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M64, - V1_2_PARAM_MULTI_BIT_GROUP_2_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M64, - V1_2_PARAM_MULTI_BIT_GROUP_3_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M64, - V1_2_PARAM_MULTI_BIT_GROUP_3_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M64, - V1_2_PARAM_MULTI_BIT_GROUP_3_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M64, -]; - -#[cfg(feature = "gpu")] -const SHORTINT_MULTI_BIT_BENCH_PARAMS: [MultiBitPBSParameters; 6] = [ - PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_1_CARRY_1_KS_PBS_TUNIFORM_2M128, - PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, - PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_3_CARRY_3_KS_PBS_TUNIFORM_2M128, - PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128, - PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, - PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128, -]; - -const BOOLEAN_BENCH_PARAMS: [(&str, BooleanParameters); 2] = [ - ("BOOLEAN_DEFAULT_PARAMS", DEFAULT_PARAMETERS), - ( - "BOOLEAN_TFHE_LIB_PARAMS", - PARAMETERS_ERROR_PROB_2_POW_MINUS_165, - ), -]; - -fn benchmark_parameters_64bits() -> Vec<(String, CryptoParametersRecord)> { - match PARAMETERS_SET.get().unwrap() { - ParametersSet::Default => SHORTINT_BENCH_PARAMS - .iter() - .map(|params| { - ( - params.name(), - >::into(*params) - .to_owned() - .into(), - ) - }) - .collect::)>>(), - ParametersSet::All => { - filter_parameters( - &VEC_ALL_CLASSIC_PBS_PARAMETERS, - DesiredNoiseDistribution::Both, - DesiredBackend::Cpu, // No parameters set are specific to GPU in this vector - ) - .into_iter() - .map(|(params, name)| { - ( - name.to_string(), - >::into(*params) - .to_owned() - .into(), - ) - }) - .collect::)>>() - } - } -} - -fn multi_bit_benchmark_parameters_64bits() -> Vec<(String, CryptoParametersRecord)> { - match PARAMETERS_SET.get().unwrap() { - ParametersSet::Default => SHORTINT_MULTI_BIT_BENCH_PARAMS - .iter() - .map(|params| { - ( - params.name(), - >::into(*params) - .to_owned() - .into(), - ) - }) - .collect::)>>(), - ParametersSet::All => { - let desired_noise = DesiredNoiseDistribution::Both; - let desired_backend = if cfg!(feature = "gpu") { - DesiredBackend::Gpu - } else { - DesiredBackend::Cpu - }; - - filter_parameters( - &VEC_ALL_MULTI_BIT_PBS_PARAMETERS, - desired_noise, - desired_backend, - ) - .into_iter() - .map(|(params, name)| { - ( - name.to_string(), - >::into(*params) - .to_owned() - .into(), - ) - }) - .collect() - } - } -} - -fn benchmark_parameters_32bits() -> Vec<(String, CryptoParametersRecord)> { - BOOLEAN_BENCH_PARAMS - .iter() - .map(|(name, params)| (name.to_string(), params.to_owned().into())) - .collect() -} - -fn benchmark_compression_parameters() -> Vec<(String, CryptoParametersRecord)> { - vec![( - COMP_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128.name(), - ( - COMP_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, - PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, - ) - .into(), - )] -} +// TODO Refactor KS, PBS and KS-PBS benchmarks into a single generic function. fn keyswitch + Serialize>( criterion: &mut Criterion, parameters: &[(String, CryptoParametersRecord)], @@ -485,12 +318,12 @@ fn packing_keyswitch( #[cfg(feature = "gpu")] mod cuda { - use crate::utilities::{ + use benchmark::params::{benchmark_parameters, multi_bit_benchmark_parameters}; + use benchmark::utilities::{ cuda_local_keys_core, cuda_local_streams_core, get_bench_type, throughput_num_threads, write_to_json, BenchmarkType, CpuKeys, CpuKeysBuilder, CryptoParametersRecord, CudaIndexes, CudaLocalKeys, OperatorType, }; - use crate::{benchmark_parameters_64bits, multi_bit_benchmark_parameters_64bits}; use criterion::{black_box, Criterion, Throughput}; use rayon::prelude::*; use serde::Serialize; @@ -910,15 +743,15 @@ mod cuda { pub fn cuda_ks_group() { let mut criterion: Criterion<_> = (Criterion::default().sample_size(2000)).configure_from_args(); - cuda_keyswitch(&mut criterion, &benchmark_parameters_64bits()); - cuda_packing_keyswitch(&mut criterion, &benchmark_parameters_64bits()); + cuda_keyswitch(&mut criterion, &benchmark_parameters()); + cuda_packing_keyswitch(&mut criterion, &benchmark_parameters()); } pub fn cuda_multi_bit_ks_group() { let mut criterion: Criterion<_> = (Criterion::default().sample_size(2000)).configure_from_args(); - cuda_keyswitch(&mut criterion, &multi_bit_benchmark_parameters_64bits()); - cuda_packing_keyswitch(&mut criterion, &multi_bit_benchmark_parameters_64bits()); + cuda_keyswitch(&mut criterion, &multi_bit_benchmark_parameters()); + cuda_packing_keyswitch(&mut criterion, &multi_bit_benchmark_parameters()); } } @@ -930,8 +763,9 @@ pub fn ks_group() { .sample_size(15) .measurement_time(std::time::Duration::from_secs(60))) .configure_from_args(); - keyswitch(&mut criterion, &benchmark_parameters_64bits()); - keyswitch(&mut criterion, &benchmark_parameters_32bits()); + keyswitch(&mut criterion, &benchmark_parameters()); + #[cfg(feature = "boolean")] + keyswitch(&mut criterion, &benchmark_32bits_parameters()); } pub fn multi_bit_ks_group() { @@ -939,7 +773,7 @@ pub fn multi_bit_ks_group() { .sample_size(15) .measurement_time(std::time::Duration::from_secs(60))) .configure_from_args(); - keyswitch(&mut criterion, &multi_bit_benchmark_parameters_64bits()); + keyswitch(&mut criterion, &multi_bit_benchmark_parameters()); } pub fn packing_ks_group() { @@ -983,8 +817,6 @@ fn go_through_cpu_bench_groups(val: &str) { } fn main() { - init_parameters_set(); - match env::var("__TFHE_RS_PARAM_TYPE") { Ok(val) => { #[cfg(feature = "gpu")] diff --git a/tfhe/benches/core_crypto/ks_pbs_bench.rs b/tfhe-benchmark/benches/core_crypto/ks_pbs_bench.rs similarity index 91% rename from tfhe/benches/core_crypto/ks_pbs_bench.rs rename to tfhe-benchmark/benches/core_crypto/ks_pbs_bench.rs index f7af098c3..9e959db89 100644 --- a/tfhe/benches/core_crypto/ks_pbs_bench.rs +++ b/tfhe-benchmark/benches/core_crypto/ks_pbs_bench.rs @@ -1,129 +1,17 @@ -#[path = "../utilities.rs"] -mod utilities; - -use crate::utilities::{ - filter_parameters, get_bench_type, init_parameters_set, multi_bit_num_threads, - throughput_num_threads, write_to_json, BenchmarkType, CryptoParametersRecord, DesiredBackend, - DesiredNoiseDistribution, OperatorType, ParametersSet, PARAMETERS_SET, +use benchmark::params::{ + benchmark_parameters, multi_bit_benchmark_parameters_with_grouping, multi_bit_num_threads, +}; +use benchmark::utilities::{ + get_bench_type, throughput_num_threads, write_to_json, BenchmarkType, CryptoParametersRecord, + OperatorType, }; use criterion::{black_box, Criterion, Throughput}; use rayon::prelude::*; use serde::Serialize; use std::env; use tfhe::core_crypto::prelude::*; -use tfhe::keycache::NamedParam; -use tfhe::shortint::parameters::current_params::*; -use tfhe::shortint::parameters::*; - -const SHORTINT_BENCH_PARAMS_TUNIFORM: [ClassicPBSParameters; 4] = [ - V1_2_PARAM_MESSAGE_1_CARRY_1_KS_PBS_TUNIFORM_2M128, - V1_2_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, - V1_2_PARAM_MESSAGE_3_CARRY_3_KS_PBS_TUNIFORM_2M128, - V1_2_PARAM_MESSAGE_4_CARRY_4_KS_PBS_TUNIFORM_2M128, -]; - -const SHORTINT_BENCH_PARAMS_GAUSSIAN: [ClassicPBSParameters; 4] = [ - V1_2_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_4_CARRY_4_KS_PBS_GAUSSIAN_2M128, -]; - -#[cfg(feature = "gpu")] -const SHORTINT_MULTI_BIT_BENCH_PARAMS: [MultiBitPBSParameters; 6] = [ - PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_1_CARRY_1_KS_PBS_TUNIFORM_2M128, - PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, - PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_3_CARRY_3_KS_PBS_TUNIFORM_2M128, - PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128, - PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, - PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128, -]; - -#[cfg(not(feature = "gpu"))] -const SHORTINT_MULTI_BIT_BENCH_PARAMS: [MultiBitPBSParameters; 6] = [ - V1_2_PARAM_MULTI_BIT_GROUP_2_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MULTI_BIT_GROUP_2_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MULTI_BIT_GROUP_2_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MULTI_BIT_GROUP_3_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MULTI_BIT_GROUP_3_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MULTI_BIT_GROUP_3_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128, -]; - -fn benchmark_parameters() -> Vec<(String, CryptoParametersRecord)> { - match PARAMETERS_SET.get().unwrap() { - ParametersSet::Default => SHORTINT_BENCH_PARAMS_TUNIFORM - .iter() - .chain(SHORTINT_BENCH_PARAMS_GAUSSIAN.iter()) - .map(|params| { - ( - params.name(), - >::into(*params) - .to_owned() - .into(), - ) - }) - .collect(), - ParametersSet::All => { - filter_parameters( - &VEC_ALL_CLASSIC_PBS_PARAMETERS, - DesiredNoiseDistribution::Both, - DesiredBackend::Cpu, // No parameters set are specific to GPU in this vector - ) - .into_iter() - .map(|(params, name)| { - ( - name.to_string(), - >::into(*params) - .to_owned() - .into(), - ) - }) - .collect() - } - } -} - -fn multi_bit_benchmark_parameters( -) -> Vec<(String, CryptoParametersRecord, LweBskGroupingFactor)> { - match PARAMETERS_SET.get().unwrap() { - ParametersSet::Default => SHORTINT_MULTI_BIT_BENCH_PARAMS - .iter() - .map(|params| { - ( - params.name(), - >::into(*params) - .to_owned() - .into(), - params.grouping_factor, - ) - }) - .collect(), - ParametersSet::All => { - let desired_backend = if cfg!(feature = "gpu") { - DesiredBackend::Gpu - } else { - DesiredBackend::Cpu - }; - filter_parameters( - &VEC_ALL_MULTI_BIT_PBS_PARAMETERS, - DesiredNoiseDistribution::Both, - desired_backend, - ) - .into_iter() - .map(|(params, name)| { - ( - name.to_string(), - >::into(*params) - .to_owned() - .into(), - params.grouping_factor, - ) - }) - .collect() - } - } -} +// TODO Refactor KS, PBS and KS-PBS benchmarks into a single generic function. fn ks_pbs + Serialize>( c: &mut Criterion, parameters: &[(String, CryptoParametersRecord)], @@ -607,8 +495,8 @@ fn multi_bit_ks_pbs< #[cfg(feature = "gpu")] mod cuda { - use super::{benchmark_parameters, multi_bit_benchmark_parameters}; - use crate::utilities::{ + use super::{benchmark_parameters, multi_bit_benchmark_parameters_with_grouping}; + use benchmark::utilities::{ cuda_local_keys_core, cuda_local_streams_core, get_bench_type, throughput_num_threads, write_to_json, BenchmarkType, CpuKeys, CpuKeysBuilder, CryptoParametersRecord, CudaIndexes, CudaLocalKeys, OperatorType, GPU_MAX_SUPPORTED_POLYNOMIAL_SIZE, @@ -1244,7 +1132,10 @@ mod cuda { pub fn cuda_multi_bit_ks_pbs_group() { let mut criterion: Criterion<_> = (Criterion::default()).configure_from_args(); - cuda_multi_bit_ks_pbs(&mut criterion, &multi_bit_benchmark_parameters()); + cuda_multi_bit_ks_pbs( + &mut criterion, + &multi_bit_benchmark_parameters_with_grouping(), + ); } } @@ -1258,8 +1149,16 @@ pub fn ks_pbs_group() { pub fn multi_bit_ks_pbs_group() { let mut criterion: Criterion<_> = (Criterion::default()).configure_from_args(); - multi_bit_ks_pbs(&mut criterion, &multi_bit_benchmark_parameters(), false); - multi_bit_ks_pbs(&mut criterion, &multi_bit_benchmark_parameters(), true); + multi_bit_ks_pbs( + &mut criterion, + &multi_bit_benchmark_parameters_with_grouping(), + false, + ); + multi_bit_ks_pbs( + &mut criterion, + &multi_bit_benchmark_parameters_with_grouping(), + true, + ); } #[cfg(feature = "gpu")] @@ -1281,8 +1180,6 @@ fn go_through_cpu_bench_groups(val: &str) { } fn main() { - init_parameters_set(); - match env::var("__TFHE_RS_PARAM_TYPE") { Ok(val) => { #[cfg(feature = "gpu")] diff --git a/tfhe/benches/core_crypto/modulus_switch_noise_reduction.rs b/tfhe-benchmark/benches/core_crypto/modulus_switch_noise_reduction.rs similarity index 100% rename from tfhe/benches/core_crypto/modulus_switch_noise_reduction.rs rename to tfhe-benchmark/benches/core_crypto/modulus_switch_noise_reduction.rs diff --git a/tfhe/benches/core_crypto/pbs128_bench.rs b/tfhe-benchmark/benches/core_crypto/pbs128_bench.rs similarity index 86% rename from tfhe/benches/core_crypto/pbs128_bench.rs rename to tfhe-benchmark/benches/core_crypto/pbs128_bench.rs index 76eb0979c..158b2065e 100644 --- a/tfhe/benches/core_crypto/pbs128_bench.rs +++ b/tfhe-benchmark/benches/core_crypto/pbs128_bench.rs @@ -1,15 +1,13 @@ -#[path = "../utilities.rs"] -mod utilities; - -use crate::utilities::{init_parameters_set, write_to_json, CryptoParametersRecord, OperatorType}; +use benchmark::params_aliases::{ + BENCH_NOISE_SQUASHING_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, + BENCH_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, +}; +use benchmark::utilities::{write_to_json, CryptoParametersRecord, OperatorType}; use criterion::{black_box, Criterion}; use dyn_stack::PodStack; use tfhe::core_crypto::fft_impl::fft128::crypto::bootstrap::bootstrap_scratch; use tfhe::core_crypto::prelude::*; -use tfhe::shortint::parameters::{ - DecompositionBaseLog, DecompositionLevelCount, DynamicDistribution, GlweDimension, - LweDimension, PolynomialSize, -}; +use tfhe::keycache::NamedParam; fn pbs_128(c: &mut Criterion) { let bench_name = "core_crypto::pbs128"; @@ -18,18 +16,21 @@ fn pbs_128(c: &mut Criterion) { .sample_size(10) .measurement_time(std::time::Duration::from_secs(30)); - type Scalar = u128; + type InputScalar = u64; + type OutputScalar = u128; - let lwe_dimension = LweDimension(879); - let glwe_dimension = GlweDimension(2); - let polynomial_size = PolynomialSize(2048); - let lwe_noise_distribution = DynamicDistribution::new_t_uniform(46); - let glwe_noise_distribution = DynamicDistribution::new_t_uniform(30); - let pbs_base_log = DecompositionBaseLog(32); - let pbs_level = DecompositionLevelCount(3); - let ciphertext_modulus = CiphertextModulus::new_native(); + let noise_params = BENCH_NOISE_SQUASHING_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; + let base_params = BENCH_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; - let params_name = "PARAMS_SWITCH_SQUASH"; + let lwe_dimension = base_params.lwe_dimension; // From PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128 + let glwe_dimension = noise_params.glwe_dimension; + let polynomial_size = noise_params.polynomial_size; + let lwe_noise_distribution = base_params.lwe_noise_distribution; + let glwe_noise_distribution = noise_params.glwe_noise_distribution; + let pbs_base_log = noise_params.decomp_base_log; + let pbs_level = noise_params.decomp_level_count; + let input_ciphertext_modulus = base_params.ciphertext_modulus; + let output_ciphertext_modulus = noise_params.ciphertext_modulus; let mut boxed_seeder = new_seeder(); let seeder = boxed_seeder.as_mut(); @@ -42,7 +43,7 @@ fn pbs_128(c: &mut Criterion) { let input_lwe_secret_key = LweSecretKey::generate_new_binary(lwe_dimension, &mut secret_generator); - let output_glwe_secret_key = GlweSecretKey::>::generate_new_binary( + let output_glwe_secret_key = GlweSecretKey::>::generate_new_binary( glwe_dimension, polynomial_size, &mut secret_generator, @@ -51,13 +52,13 @@ fn pbs_128(c: &mut Criterion) { let output_lwe_secret_key = output_glwe_secret_key.clone().into_lwe_secret_key(); let mut bsk = LweBootstrapKey::new( - Scalar::ZERO, + OutputScalar::ZERO, glwe_dimension.to_glwe_size(), polynomial_size, pbs_base_log, pbs_level, lwe_dimension, - ciphertext_modulus, + output_ciphertext_modulus, ); par_generate_lwe_bootstrap_key( &input_lwe_secret_key, @@ -76,33 +77,34 @@ fn pbs_128(c: &mut Criterion) { ); convert_standard_lwe_bootstrap_key_to_fourier_128(&bsk, &mut fourier_bsk); - let message_modulus: Scalar = 1 << 4; + let message_modulus: InputScalar = 1 << 4; - let input_message: Scalar = 3; + let input_message: InputScalar = 3; - let delta: Scalar = (1 << (Scalar::BITS - 1)) / message_modulus; + let delta: InputScalar = (1 << (InputScalar::BITS - 1)) / message_modulus; let plaintext = Plaintext(input_message * delta); - let lwe_ciphertext_in: LweCiphertextOwned = allocate_and_encrypt_new_lwe_ciphertext( - &input_lwe_secret_key, - plaintext, - lwe_noise_distribution, - ciphertext_modulus, - &mut encryption_generator, - ); + let lwe_ciphertext_in: LweCiphertextOwned = + allocate_and_encrypt_new_lwe_ciphertext( + &input_lwe_secret_key, + plaintext, + lwe_noise_distribution, + input_ciphertext_modulus, + &mut encryption_generator, + ); - let accumulator: GlweCiphertextOwned = GlweCiphertextOwned::new( - Scalar::ONE, + let accumulator: GlweCiphertextOwned = GlweCiphertextOwned::new( + OutputScalar::ONE, glwe_dimension.to_glwe_size(), polynomial_size, - ciphertext_modulus, + output_ciphertext_modulus, ); - let mut out_pbs_ct: LweCiphertext> = LweCiphertext::new( - 0, + let mut out_pbs_ct: LweCiphertext> = LweCiphertext::new( + OutputScalar::ZERO, output_lwe_secret_key.lwe_dimension().to_lwe_size(), - ciphertext_modulus, + output_ciphertext_modulus, ); let fft = Fft128::new(polynomial_size); @@ -110,7 +112,7 @@ fn pbs_128(c: &mut Criterion) { let mut buffers = vec![ 0u8; - bootstrap_scratch::( + bootstrap_scratch::( fourier_bsk.glwe_size(), fourier_bsk.polynomial_size(), fft @@ -119,7 +121,7 @@ fn pbs_128(c: &mut Criterion) { .unaligned_bytes_required() ]; - let id = format!("{bench_name}::{params_name}"); + let id = format!("{bench_name}::{}", noise_params.name()); bench_group.bench_function(&id, |b| { b.iter(|| { fourier_bsk.bootstrap( @@ -133,15 +135,17 @@ fn pbs_128(c: &mut Criterion) { }); }); + // TODO Add throughput benchmark case + let params_record = CryptoParametersRecord { lwe_dimension: Some(lwe_dimension), glwe_dimension: Some(glwe_dimension), polynomial_size: Some(polynomial_size), lwe_noise_distribution: Some(lwe_noise_distribution), - glwe_noise_distribution: Some(glwe_noise_distribution), + glwe_noise_distribution: Some(base_params.glwe_noise_distribution), pbs_base_log: Some(pbs_base_log), pbs_level: Some(pbs_level), - ciphertext_modulus: Some(ciphertext_modulus), + ciphertext_modulus: Some(input_ciphertext_modulus), ..Default::default() }; @@ -149,7 +153,7 @@ fn pbs_128(c: &mut Criterion) { write_to_json( &id, params_record, - params_name, + noise_params.name(), "pbs", &OperatorType::Atomic, bit_size, @@ -159,7 +163,7 @@ fn pbs_128(c: &mut Criterion) { #[cfg(feature = "gpu")] mod cuda { - use crate::utilities::{ + use benchmark::utilities::{ cuda_local_keys_core, cuda_local_streams_core, get_bench_type, throughput_num_threads, write_to_json, BenchmarkType, CpuKeys, CpuKeysBuilder, CryptoParametersRecord, CudaLocalKeys, OperatorType, @@ -173,10 +177,7 @@ mod cuda { }; use tfhe::core_crypto::prelude::*; use tfhe::shortint::engine::ShortintEngine; - use tfhe::shortint::parameters::{ - DecompositionBaseLog, DecompositionLevelCount, DynamicDistribution, GlweDimension, - LweDimension, ModulusSwitchNoiseReductionParams, PolynomialSize, - }; + use tfhe::shortint::parameters::ModulusSwitchNoiseReductionParams; use tfhe::shortint::server_key::ModulusSwitchNoiseReductionKey; fn cuda_pbs_128(c: &mut Criterion) { @@ -249,6 +250,7 @@ mod cuda { ); let mut engine = ShortintEngine::new(); + let modulus_switch_noise_reduction_key = Some(ModulusSwitchNoiseReductionKey::new( modulus_switch_noise_reduction_params, &input_lwe_secret_key, @@ -296,7 +298,7 @@ mod cuda { CudaGlweCiphertextList::from_glwe_ciphertext(&accumulator, &streams); let out_pbs_ct = LweCiphertext::new( - 0, + Scalar::ZERO, output_lwe_secret_key.lwe_dimension().to_lwe_size(), ciphertext_modulus, ); @@ -473,8 +475,6 @@ fn go_through_cpu_bench_groups() { pbs128_group(); } fn main() { - init_parameters_set(); - #[cfg(feature = "gpu")] go_through_gpu_bench_groups(); #[cfg(not(feature = "gpu"))] diff --git a/tfhe/benches/core_crypto/pbs_bench.rs b/tfhe-benchmark/benches/core_crypto/pbs_bench.rs similarity index 90% rename from tfhe/benches/core_crypto/pbs_bench.rs rename to tfhe-benchmark/benches/core_crypto/pbs_bench.rs index 782f54db5..52bfbd368 100644 --- a/tfhe/benches/core_crypto/pbs_bench.rs +++ b/tfhe-benchmark/benches/core_crypto/pbs_bench.rs @@ -1,177 +1,19 @@ -#[path = "../utilities.rs"] -mod utilities; - -use crate::utilities::{ - filter_parameters, get_bench_type, init_parameters_set, multi_bit_num_threads, - throughput_num_threads, write_to_json, BenchmarkType, CryptoParametersRecord, DesiredBackend, - DesiredNoiseDistribution, OperatorType, ParametersSet, PARAMETERS_SET, +use benchmark::params::{ + benchmark_32bits_parameters, benchmark_parameters, + multi_bit_benchmark_parameters_with_grouping, multi_bit_num_threads, +}; +use benchmark::utilities::{ + get_bench_type, throughput_num_threads, write_to_json, BenchmarkType, CryptoParametersRecord, + OperatorType, }; use criterion::{black_box, Criterion, Throughput}; use rayon::prelude::*; use serde::Serialize; use std::env; -use tfhe::boolean::parameters::{ - BooleanParameters, DEFAULT_PARAMETERS, PARAMETERS_ERROR_PROB_2_POW_MINUS_165, -}; use tfhe::core_crypto::commons::math::ntt::ntt64::Ntt64; use tfhe::core_crypto::prelude::*; -use tfhe::keycache::NamedParam; -use tfhe::shortint::parameters::current_params::*; -use tfhe::shortint::parameters::*; - -const SHORTINT_BENCH_PARAMS_TUNIFORM: [ClassicPBSParameters; 4] = [ - V1_2_PARAM_MESSAGE_1_CARRY_1_KS_PBS_TUNIFORM_2M128, - V1_2_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, - V1_2_PARAM_MESSAGE_3_CARRY_3_KS_PBS_TUNIFORM_2M128, - V1_2_PARAM_MESSAGE_4_CARRY_4_KS_PBS_TUNIFORM_2M128, -]; - -const SHORTINT_BENCH_PARAMS_GAUSSIAN: [ClassicPBSParameters; 4] = [ - V1_2_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_4_CARRY_4_KS_PBS_GAUSSIAN_2M128, -]; - -#[cfg(feature = "gpu")] -const SHORTINT_MULTI_BIT_BENCH_PARAMS: [MultiBitPBSParameters; 6] = [ - PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_1_CARRY_1_KS_PBS_TUNIFORM_2M128, - PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, - PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_3_CARRY_3_KS_PBS_TUNIFORM_2M128, - PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128, - PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, - PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128, -]; - -#[cfg(not(feature = "gpu"))] -const SHORTINT_MULTI_BIT_BENCH_PARAMS: [MultiBitPBSParameters; 6] = [ - V1_2_PARAM_MULTI_BIT_GROUP_2_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MULTI_BIT_GROUP_2_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MULTI_BIT_GROUP_2_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MULTI_BIT_GROUP_3_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MULTI_BIT_GROUP_3_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MULTI_BIT_GROUP_3_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128, -]; - -const BOOLEAN_BENCH_PARAMS: [(&str, BooleanParameters); 2] = [ - ("BOOLEAN_DEFAULT_PARAMS", DEFAULT_PARAMETERS), - ( - "BOOLEAN_TFHE_LIB_PARAMS", - PARAMETERS_ERROR_PROB_2_POW_MINUS_165, - ), -]; - -fn benchmark_parameters_64bits() -> Vec<(String, CryptoParametersRecord)> { - match PARAMETERS_SET.get().unwrap() { - ParametersSet::Default => SHORTINT_BENCH_PARAMS_TUNIFORM - .iter() - .chain(SHORTINT_BENCH_PARAMS_GAUSSIAN.iter()) - .map(|params| { - ( - params.name(), - >::into(*params) - .to_owned() - .into(), - ) - }) - .collect(), - ParametersSet::All => filter_parameters( - &VEC_ALL_CLASSIC_PBS_PARAMETERS, - DesiredNoiseDistribution::Both, - DesiredBackend::Cpu, // No parameters set are specific to GPU in this vector - ) - .into_iter() - .map(|(params, name)| { - ( - name.to_string(), - >::into(*params) - .to_owned() - .into(), - ) - }) - .collect(), - } -} - -fn benchmark_parameters_32bits() -> Vec<(String, CryptoParametersRecord)> { - BOOLEAN_BENCH_PARAMS - .iter() - .map(|(name, params)| (name.to_string(), params.to_owned().into())) - .collect() -} - -fn throughput_benchmark_parameters_64bits() -> Vec<(String, CryptoParametersRecord)> { - let parameters = if cfg!(feature = "gpu") { - vec![ - PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, - V1_2_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128, - ] - } else { - vec![ - PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, - V1_2_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_4_CARRY_4_KS_PBS_GAUSSIAN_2M128, - ] - }; - - parameters - .iter() - .map(|params| { - ( - params.name(), - >::into(*params) - .to_owned() - .into(), - ) - }) - .collect() -} - -fn multi_bit_benchmark_parameters_64bits( -) -> Vec<(String, CryptoParametersRecord, LweBskGroupingFactor)> { - match PARAMETERS_SET.get().unwrap() { - ParametersSet::Default => SHORTINT_MULTI_BIT_BENCH_PARAMS - .iter() - .map(|params| { - ( - params.name(), - >::into(*params) - .to_owned() - .into(), - params.grouping_factor, - ) - }) - .collect(), - ParametersSet::All => { - let desired_backend = if cfg!(feature = "gpu") { - DesiredBackend::Gpu - } else { - DesiredBackend::Cpu - }; - filter_parameters( - &VEC_ALL_MULTI_BIT_PBS_PARAMETERS, - DesiredNoiseDistribution::Both, - desired_backend, - ) - .into_iter() - .map(|(params, name)| { - ( - name.to_string(), - >::into(*params) - .to_owned() - .into(), - params.grouping_factor, - ) - }) - .collect() - } - } -} +// TODO Refactor KS, PBS and KS-PBS benchmarks into a single generic function. fn mem_optimized_pbs + Serialize>( c: &mut Criterion, parameters: &[(String, CryptoParametersRecord)], @@ -823,7 +665,7 @@ fn mem_optimized_pbs_ntt(c: &mut Criterion) { let custom_ciphertext_modulus = tfhe::core_crypto::prelude::CiphertextModulus::new((1 << 64) - (1 << 32) + 1); - for (name, params) in throughput_benchmark_parameters_64bits().iter_mut() { + for (name, params) in benchmark_parameters().iter_mut() { if let (Some(lwe_noise), Some(glwe_noise)) = ( params.lwe_noise_distribution, params.glwe_noise_distribution, @@ -1069,8 +911,8 @@ fn mem_optimized_pbs_ntt(c: &mut Criterion) { #[cfg(feature = "gpu")] mod cuda { - use super::{benchmark_parameters_64bits, multi_bit_benchmark_parameters_64bits}; - use crate::utilities::{ + use benchmark::params::{benchmark_parameters, multi_bit_benchmark_parameters}; + use benchmark::utilities::{ cuda_local_keys_core, cuda_local_streams_core, get_bench_type, throughput_num_threads, write_to_json, BenchmarkType, CpuKeys, CpuKeysBuilder, CryptoParametersRecord, CudaIndexes, CudaLocalKeys, OperatorType, GPU_MAX_SUPPORTED_POLYNOMIAL_SIZE, @@ -1597,12 +1439,12 @@ mod cuda { pub fn cuda_pbs_group() { let mut criterion: Criterion<_> = (Criterion::default()).configure_from_args(); - cuda_pbs(&mut criterion, &benchmark_parameters_64bits()); + cuda_pbs(&mut criterion, &benchmark_parameters()); } pub fn cuda_multi_bit_pbs_group() { let mut criterion: Criterion<_> = (Criterion::default()).configure_from_args(); - cuda_multi_bit_pbs(&mut criterion, &multi_bit_benchmark_parameters_64bits()); + cuda_multi_bit_pbs(&mut criterion, &multi_bit_benchmark_parameters()); } } @@ -1611,22 +1453,22 @@ use cuda::{cuda_multi_bit_pbs_group, cuda_pbs_group}; pub fn pbs_group() { let mut criterion: Criterion<_> = (Criterion::default()).configure_from_args(); - mem_optimized_pbs(&mut criterion, &benchmark_parameters_64bits()); - mem_optimized_pbs(&mut criterion, &benchmark_parameters_32bits()); + mem_optimized_pbs(&mut criterion, &benchmark_parameters()); + mem_optimized_pbs(&mut criterion, &benchmark_32bits_parameters()); mem_optimized_pbs_ntt(&mut criterion); - mem_optimized_batched_pbs(&mut criterion, &benchmark_parameters_64bits()); + mem_optimized_batched_pbs(&mut criterion, &benchmark_parameters()); } pub fn multi_bit_pbs_group() { let mut criterion: Criterion<_> = (Criterion::default()).configure_from_args(); multi_bit_pbs( &mut criterion, - &multi_bit_benchmark_parameters_64bits(), + &multi_bit_benchmark_parameters_with_grouping(), false, ); multi_bit_pbs( &mut criterion, - &multi_bit_benchmark_parameters_64bits(), + &multi_bit_benchmark_parameters_with_grouping(), true, ); } @@ -1650,8 +1492,6 @@ fn go_through_cpu_bench_groups(val: &str) { } fn main() { - init_parameters_set(); - match env::var("__TFHE_RS_PARAM_TYPE") { Ok(val) => { #[cfg(feature = "gpu")] diff --git a/tfhe/benches/high_level_api/bench.rs b/tfhe-benchmark/benches/high_level_api/bench.rs similarity index 95% rename from tfhe/benches/high_level_api/bench.rs rename to tfhe-benchmark/benches/high_level_api/bench.rs index 8361b0587..9595f6918 100644 --- a/tfhe/benches/high_level_api/bench.rs +++ b/tfhe-benchmark/benches/high_level_api/bench.rs @@ -1,9 +1,9 @@ +use benchmark::params_aliases::BENCH_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; use criterion::{black_box, Criterion}; use rand::prelude::*; use std::fmt::Write; use std::ops::*; use tfhe::prelude::*; -use tfhe::shortint::parameters::*; use tfhe::{ set_server_key, ClientKey, CompressedServerKey, ConfigBuilder, FheUint10, FheUint12, FheUint128, FheUint14, FheUint16, FheUint2, FheUint32, FheUint4, FheUint6, FheUint64, FheUint8, @@ -108,7 +108,9 @@ bench_type!(FheUint64); bench_type!(FheUint128); fn main() { - let config = ConfigBuilder::with_custom_parameters(PARAM_MESSAGE_2_CARRY_2_KS_PBS).build(); + let config = + ConfigBuilder::with_custom_parameters(BENCH_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128) + .build(); let cks = ClientKey::generate(config); let compressed_sks = CompressedServerKey::new(&cks); diff --git a/tfhe/benches/high_level_api/dex.rs b/tfhe-benchmark/benches/high_level_api/dex.rs similarity index 97% rename from tfhe/benches/high_level_api/dex.rs rename to tfhe-benchmark/benches/high_level_api/dex.rs index bccf3ef03..a68f4626c 100644 --- a/tfhe/benches/high_level_api/dex.rs +++ b/tfhe-benchmark/benches/high_level_api/dex.rs @@ -1,10 +1,10 @@ -#[path = "../utilities.rs"] -mod utilities; - #[cfg(feature = "gpu")] -use crate::utilities::configure_gpu; -use crate::utilities::{write_to_json, OperatorType}; - +use benchmark::params_aliases::BENCH_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; +#[cfg(not(feature = "gpu"))] +use benchmark::params_aliases::BENCH_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; +#[cfg(feature = "gpu")] +use benchmark::utilities::configure_gpu; +use benchmark::utilities::{write_to_json, OperatorType}; use criterion::measurement::WallTime; use criterion::{BenchmarkGroup, Criterion}; use rand::prelude::*; @@ -12,7 +12,6 @@ use rand::thread_rng; use std::ops::{Add, Div, Mul, Sub}; use tfhe::keycache::NamedParam; use tfhe::prelude::*; -use tfhe::shortint::parameters::*; #[cfg(not(feature = "gpu"))] use tfhe::{set_server_key, CompressedServerKey}; use tfhe::{ClientKey, ConfigBuilder, FheBool, FheUint128, FheUint64}; @@ -443,7 +442,7 @@ use crate::pbs_stats::print_swap_request_pbs_counts; #[cfg(not(feature = "gpu"))] fn main() { - let params = PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; + let params = BENCH_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; let config = ConfigBuilder::with_custom_parameters(params).build(); let cks = ClientKey::generate(config); @@ -495,7 +494,7 @@ fn main() { #[cfg(feature = "gpu")] fn main() { - let params = PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS; + let params = BENCH_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; let config = ConfigBuilder::with_custom_parameters(params).build(); let cks = ClientKey::generate(config); diff --git a/tfhe/benches/high_level_api/erc20.rs b/tfhe-benchmark/benches/high_level_api/erc20.rs similarity index 97% rename from tfhe/benches/high_level_api/erc20.rs rename to tfhe-benchmark/benches/high_level_api/erc20.rs index 787e9414a..a75d1cbfb 100644 --- a/tfhe/benches/high_level_api/erc20.rs +++ b/tfhe-benchmark/benches/high_level_api/erc20.rs @@ -1,9 +1,10 @@ -#[path = "../utilities.rs"] -mod utilities; - #[cfg(feature = "gpu")] -use crate::utilities::configure_gpu; -use crate::utilities::{write_to_json, OperatorType}; +use benchmark::params_aliases::BENCH_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; +#[cfg(not(feature = "gpu"))] +use benchmark::params_aliases::BENCH_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; +#[cfg(feature = "gpu")] +use benchmark::utilities::configure_gpu; +use benchmark::utilities::{write_to_json, OperatorType}; use criterion::measurement::WallTime; use criterion::{BenchmarkGroup, Criterion, Throughput}; use rand::prelude::*; @@ -12,7 +13,6 @@ use rayon::prelude::*; use std::ops::{Add, Mul, Sub}; use tfhe::keycache::NamedParam; use tfhe::prelude::*; -use tfhe::shortint::parameters::*; #[cfg(feature = "gpu")] use tfhe::GpuIndex; use tfhe::{set_server_key, ClientKey, CompressedServerKey, ConfigBuilder, FheBool, FheUint64}; @@ -365,7 +365,7 @@ use tfhe::core_crypto::gpu::get_number_of_gpus; #[cfg(not(feature = "gpu"))] fn main() { - let params = PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; + let params = BENCH_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; let config = ConfigBuilder::with_custom_parameters(params).build(); let cks = ClientKey::generate(config); @@ -484,7 +484,7 @@ fn main() { #[cfg(feature = "gpu")] fn main() { - let params = PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS; + let params = BENCH_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; let config = ConfigBuilder::with_custom_parameters(params).build(); let cks = ClientKey::generate(config); diff --git a/tfhe/benches/integer/bench.rs b/tfhe-benchmark/benches/integer/bench.rs similarity index 99% rename from tfhe/benches/integer/bench.rs rename to tfhe-benchmark/benches/integer/bench.rs index 223d11622..0416cd819 100644 --- a/tfhe/benches/integer/bench.rs +++ b/tfhe-benchmark/benches/integer/bench.rs @@ -1,12 +1,10 @@ #![allow(dead_code)] mod oprf; -#[path = "../utilities.rs"] -mod utilities; -use crate::utilities::{ +use benchmark::params::ParamsAndNumBlocksIter; +use benchmark::utilities::{ get_bench_type, throughput_num_threads, write_to_json, BenchmarkType, EnvConfig, OperatorType, - ParamsAndNumBlocksIter, }; use criterion::{criterion_group, Criterion, Throughput}; use rand::prelude::*; @@ -1395,7 +1393,7 @@ define_server_key_bench_default_fn!( #[cfg(feature = "gpu")] mod cuda { use super::*; - use crate::utilities::cuda_integer_utils::{cuda_local_keys, cuda_local_streams}; + use benchmark::utilities::cuda_integer_utils::{cuda_local_keys, cuda_local_streams}; use criterion::{black_box, criterion_group}; use std::cmp::max; use tfhe::core_crypto::gpu::{get_number_of_gpus, CudaStreams}; diff --git a/tfhe/benches/integer/glwe_packing_compression.rs b/tfhe-benchmark/benches/integer/glwe_packing_compression.rs similarity index 95% rename from tfhe/benches/integer/glwe_packing_compression.rs rename to tfhe-benchmark/benches/integer/glwe_packing_compression.rs index cc9418f79..b87949eed 100644 --- a/tfhe/benches/integer/glwe_packing_compression.rs +++ b/tfhe-benchmark/benches/integer/glwe_packing_compression.rs @@ -1,7 +1,5 @@ -#[path = "../utilities.rs"] -mod utilities; - -use crate::utilities::{ +use benchmark::params_aliases::*; +use benchmark::utilities::{ get_bench_type, throughput_num_threads, write_to_json, BenchmarkType, OperatorType, }; use criterion::{black_box, criterion_group, Criterion, Throughput}; @@ -10,10 +8,6 @@ use std::cmp::max; use tfhe::integer::ciphertext::CompressedCiphertextListBuilder; use tfhe::integer::{ClientKey, RadixCiphertext}; use tfhe::keycache::NamedParam; -use tfhe::shortint::parameters::{ - COMP_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, - PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, -}; use tfhe::{get_pbs_count, reset_pbs_count}; fn cpu_glwe_packing(c: &mut Criterion) { @@ -23,8 +17,8 @@ fn cpu_glwe_packing(c: &mut Criterion) { .sample_size(15) .measurement_time(std::time::Duration::from_secs(30)); - let param = PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; - let comp_param = COMP_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; + let param = BENCH_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; + let comp_param = BENCH_COMP_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; let cks = ClientKey::new(param); @@ -164,13 +158,12 @@ fn cpu_glwe_packing(c: &mut Criterion) { #[cfg(feature = "gpu")] mod cuda { use super::*; - use crate::utilities::cuda_integer_utils::cuda_local_streams; + use benchmark::utilities::cuda_integer_utils::cuda_local_streams; use std::cmp::max; use tfhe::core_crypto::gpu::CudaStreams; use tfhe::integer::gpu::ciphertext::compressed_ciphertext_list::CudaCompressedCiphertextListBuilder; use tfhe::integer::gpu::ciphertext::CudaUnsignedRadixCiphertext; use tfhe::integer::gpu::gen_keys_radix_gpu; - use tfhe::shortint::parameters::*; fn gpu_glwe_packing(c: &mut Criterion) { let bench_name = "integer::cuda::packing_compression"; @@ -181,8 +174,9 @@ mod cuda { let stream = CudaStreams::new_multi_gpu(); - let param = PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; - let comp_param = COMP_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; + let param = BENCH_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; + let comp_param = + BENCH_COMP_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; let log_message_modulus = param.message_modulus.0.ilog2() as usize; diff --git a/tfhe/benches/integer/oprf.rs b/tfhe-benchmark/benches/integer/oprf.rs similarity index 97% rename from tfhe/benches/integer/oprf.rs rename to tfhe-benchmark/benches/integer/oprf.rs index 73717629e..29c84dc91 100644 --- a/tfhe/benches/integer/oprf.rs +++ b/tfhe-benchmark/benches/integer/oprf.rs @@ -1,6 +1,6 @@ -use crate::utilities::{ +use benchmark::params::ParamsAndNumBlocksIter; +use benchmark::utilities::{ get_bench_type, throughput_num_threads, write_to_json, BenchmarkType, OperatorType, - ParamsAndNumBlocksIter, }; use criterion::{black_box, Criterion, Throughput}; use rayon::prelude::*; diff --git a/tfhe/benches/integer/signed_bench.rs b/tfhe-benchmark/benches/integer/signed_bench.rs similarity index 99% rename from tfhe/benches/integer/signed_bench.rs rename to tfhe-benchmark/benches/integer/signed_bench.rs index 28e419bdc..c3824400a 100644 --- a/tfhe/benches/integer/signed_bench.rs +++ b/tfhe-benchmark/benches/integer/signed_bench.rs @@ -1,9 +1,6 @@ -#[path = "../utilities.rs"] -mod utilities; - -use crate::utilities::{ +use benchmark::params::ParamsAndNumBlocksIter; +use benchmark::utilities::{ get_bench_type, throughput_num_threads, write_to_json, BenchmarkType, EnvConfig, OperatorType, - ParamsAndNumBlocksIter, }; use criterion::{criterion_group, Criterion, Throughput}; use rand::prelude::*; @@ -1400,7 +1397,8 @@ criterion_group!(cast_ops, cast_to_unsigned, cast_to_signed); #[cfg(feature = "gpu")] mod cuda { use super::*; - use crate::utilities::cuda_integer_utils::{cuda_local_keys, cuda_local_streams}; + use benchmark::params::ParamsAndNumBlocksIter; + use benchmark::utilities::cuda_integer_utils::{cuda_local_keys, cuda_local_streams}; use criterion::criterion_group; use rayon::iter::IntoParallelRefIterator; use std::cmp::max; diff --git a/tfhe/benches/integer/zk_pke.rs b/tfhe-benchmark/benches/integer/zk_pke.rs similarity index 92% rename from tfhe/benches/integer/zk_pke.rs rename to tfhe-benchmark/benches/integer/zk_pke.rs index 03ee2e88c..9d4d22c00 100644 --- a/tfhe/benches/integer/zk_pke.rs +++ b/tfhe-benchmark/benches/integer/zk_pke.rs @@ -1,7 +1,7 @@ -#[path = "../utilities.rs"] -mod utilities; - -use crate::utilities::{get_bench_type, throughput_num_threads, BenchmarkType}; +use benchmark::params_aliases::*; +use benchmark::utilities::{ + get_bench_type, throughput_num_threads, write_to_json, BenchmarkType, OperatorType, +}; use criterion::{criterion_group, Criterion, Throughput}; use rand::prelude::*; use rayon::prelude::*; @@ -14,11 +14,9 @@ use tfhe::integer::key_switching_key::KeySwitchingKey; use tfhe::integer::parameters::IntegerCompactCiphertextListExpansionMode; use tfhe::integer::{ClientKey, CompactPrivateKey, CompactPublicKey, ServerKey}; use tfhe::keycache::NamedParam; -use tfhe::shortint::parameters::current_params::*; use tfhe::shortint::parameters::*; use tfhe::zk::{CompactPkeCrs, ZkComputeLoad}; use tfhe::{get_pbs_count, reset_pbs_count}; -use utilities::{write_to_json, OperatorType}; fn write_result(file: &mut File, name: &str, value: usize) { let line = format!("{name},{value}\n"); @@ -35,15 +33,14 @@ fn pke_zk_proof(c: &mut Criterion) { for (param_pke, _param_casting, param_fhe) in [ ( - PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, - PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, - PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, + BENCH_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, + BENCH_PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, + BENCH_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, ), - // Non default params use the V1_2 prefix ( - V1_2_PARAM_PKE_TO_SMALL_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128_ZKV1, - V1_2_PARAM_KEYSWITCH_PKE_TO_SMALL_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128_ZKV1, - PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, + BENCH_PARAM_PKE_TO_SMALL_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128_ZKV1, + BENCH_PARAM_KEYSWITCH_PKE_TO_SMALL_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128_ZKV1, + BENCH_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, ), ] { let param_name = param_fhe.name(); @@ -178,15 +175,14 @@ fn cpu_pke_zk_verify(c: &mut Criterion, results_file: &Path) { for (param_pke, param_casting, param_fhe) in [ ( - PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, - PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, - PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, + BENCH_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, + BENCH_PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, + BENCH_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, ), - // Non default params use the V1_2 prefix ( - V1_2_PARAM_PKE_TO_SMALL_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128_ZKV1, - V1_2_PARAM_KEYSWITCH_PKE_TO_SMALL_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128_ZKV1, - PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, + BENCH_PARAM_PKE_TO_SMALL_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128_ZKV1, + BENCH_PARAM_KEYSWITCH_PKE_TO_SMALL_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128_ZKV1, + BENCH_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, ), ] { let param_name = param_fhe.name(); @@ -434,7 +430,7 @@ fn cpu_pke_zk_verify(c: &mut Criterion, results_file: &Path) { #[cfg(all(feature = "gpu", feature = "zk-pok"))] mod cuda { use super::*; - use crate::utilities::{cuda_local_keys, cuda_local_streams}; + use benchmark::utilities::{cuda_local_keys, cuda_local_streams}; use criterion::BatchSize; use itertools::Itertools; use tfhe::core_crypto::gpu::{get_number_of_gpus, CudaStreams}; @@ -669,12 +665,6 @@ mod cuda { }) .collect::>(); - let gpu_cts = cts.iter().map(|ct| { - CudaProvenCompactCiphertextList::from_proven_compact_ciphertext_list( - &ct, &streams, - ) - }).collect_vec(); - let local_streams = cuda_local_streams(num_block, elements as usize); let d_ksk_vec = gpu_sks_vec .par_iter() @@ -682,7 +672,7 @@ mod cuda { .map(|(gpu_sks, local_stream)| { CudaKeySwitchingKey::new( (&compact_private_key, None), - (&cks, &gpu_sks), + (&cks, gpu_sks), param_ksk, local_stream, ) @@ -705,20 +695,21 @@ mod cuda { let gpu_cts = cts.iter().enumerate().map(|(i, ct)| { CudaProvenCompactCiphertextList::from_proven_compact_ciphertext_list( - &ct, &local_streams[i], + ct, &local_streams[i], ) }).collect_vec(); (gpu_cts, local_streams) }; - b.iter_batched(setup_encrypted_values, - |(gpu_cts, local_streams)| { - gpu_cts.par_iter().zip(local_streams.par_iter()).enumerate().for_each - (|(i, (gpu_ct, local_stream))| { - gpu_ct - .expand_without_verification(&d_ksk_vec[i % gpu_count], local_stream) - .unwrap(); + b.iter_batched(setup_encrypted_values, |(gpu_cts, local_streams)| { + gpu_cts.par_iter() + .zip(local_streams.par_iter()) + .enumerate() + .for_each(|(i, (gpu_ct, local_stream))| { + gpu_ct + .expand_without_verification(&d_ksk_vec[i % gpu_count], local_stream) + .unwrap(); }); }, BatchSize::SmallInput); }); @@ -729,22 +720,23 @@ mod cuda { let gpu_cts = cts.iter().enumerate().map(|(i, ct)| { CudaProvenCompactCiphertextList::from_proven_compact_ciphertext_list( - &ct, &local_streams[i], + ct, &local_streams[i], ) }).collect_vec(); (gpu_cts, local_streams) }; - b.iter_batched(setup_encrypted_values, - |(gpu_cts, local_streams)| { - gpu_cts.par_iter().zip(local_streams.par_iter()).enumerate().for_each - (|(i, (gpu_ct, local_stream))| { - gpu_ct - .verify_and_expand( - &crs, &pk, &metadata, &d_ksk, local_stream, - ) - .unwrap(); + b.iter_batched(setup_encrypted_values, |(gpu_cts, local_streams)| { + gpu_cts + .par_iter() + .zip(local_streams.par_iter()) + .for_each(|(gpu_ct, local_stream)| { + gpu_ct + .verify_and_expand( + &crs, &pk, &metadata, &d_ksk, local_stream + ) + .unwrap(); }); }, BatchSize::SmallInput); }); diff --git a/tfhe/benches/shortint/bench.rs b/tfhe-benchmark/benches/shortint/bench.rs similarity index 69% rename from tfhe/benches/shortint/bench.rs rename to tfhe-benchmark/benches/shortint/bench.rs index 7461990f1..ac7d65b2e 100644 --- a/tfhe/benches/shortint/bench.rs +++ b/tfhe-benchmark/benches/shortint/bench.rs @@ -1,95 +1,27 @@ -#[path = "../utilities.rs"] -mod utilities; - -use crate::utilities::{write_to_json, OperatorType}; +use benchmark::params::{ + raw_benchmark_parameters, SHORTINT_BENCH_PARAMS_GAUSSIAN, SHORTINT_BENCH_PARAMS_TUNIFORM, + SHORTINT_MULTI_BIT_BENCH_PARAMS, +}; +use benchmark::utilities::{write_to_json, OperatorType}; use criterion::{criterion_group, Criterion}; use rand::Rng; use std::env; use tfhe::keycache::NamedParam; use tfhe::shortint::keycache::KEY_CACHE; -use tfhe::shortint::parameters::current_params::*; use tfhe::shortint::parameters::*; use tfhe::shortint::{Ciphertext, CompressedServerKey, ServerKey}; -const SERVER_KEY_BENCH_PARAMS: [ClassicPBSParameters; 5] = [ - PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, - V1_2_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_4_CARRY_4_KS_PBS_GAUSSIAN_2M128, -]; - -const SERVER_KEY_BENCH_PARAMS_EXTENDED: [ClassicPBSParameters; 16] = [ - PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, - V1_2_PARAM_MESSAGE_1_CARRY_0_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_2_CARRY_0_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_2_CARRY_1_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_3_CARRY_0_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_3_CARRY_2_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_4_CARRY_0_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_4_CARRY_3_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_4_CARRY_4_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_5_CARRY_0_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_6_CARRY_0_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_7_CARRY_0_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_8_CARRY_0_KS_PBS_GAUSSIAN_2M128, -]; - -const SERVER_KEY_MULTI_BIT_BENCH_PARAMS: [MultiBitPBSParameters; 2] = [ - V1_2_PARAM_MULTI_BIT_GROUP_2_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M64, - V1_2_PARAM_MULTI_BIT_GROUP_3_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M64, -]; - -const SERVER_KEY_MULTI_BIT_BENCH_PARAMS_EXTENDED: [MultiBitPBSParameters; 6] = [ - V1_2_PARAM_MULTI_BIT_GROUP_2_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M64, - V1_2_PARAM_MULTI_BIT_GROUP_2_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M64, - V1_2_PARAM_MULTI_BIT_GROUP_2_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M64, - V1_2_PARAM_MULTI_BIT_GROUP_3_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M64, - V1_2_PARAM_MULTI_BIT_GROUP_3_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M64, - V1_2_PARAM_MULTI_BIT_GROUP_3_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M64, -]; - -enum BenchParamsSet { - Standard, - Extended, -} - -fn benchmark_parameters(params_set: BenchParamsSet) -> Vec { - let is_multi_bit = match env::var("__TFHE_RS_PARAM_TYPE") { - Ok(val) => val.to_lowercase() == "multi_bit", - Err(_) => false, - }; - - if is_multi_bit { - let params = match params_set { - BenchParamsSet::Standard => SERVER_KEY_MULTI_BIT_BENCH_PARAMS.to_vec(), - BenchParamsSet::Extended => SERVER_KEY_MULTI_BIT_BENCH_PARAMS_EXTENDED.to_vec(), - }; - params.iter().map(|p| (*p).into()).collect() - } else { - let params = match params_set { - BenchParamsSet::Standard => SERVER_KEY_BENCH_PARAMS.to_vec(), - BenchParamsSet::Extended => SERVER_KEY_BENCH_PARAMS_EXTENDED.to_vec(), - }; - params.iter().map(|p| (*p).into()).collect() - } -} - fn bench_server_key_unary_function( c: &mut Criterion, bench_name: &str, display_name: &str, unary_op: F, - params_set: BenchParamsSet, ) where F: Fn(&ServerKey, &mut Ciphertext), { let mut bench_group = c.benchmark_group(bench_name); - for param in benchmark_parameters(params_set).iter() { + for param in raw_benchmark_parameters().iter() { let keys = KEY_CACHE.get_from_param(*param); let (cks, sks) = (keys.client_key(), keys.server_key()); @@ -127,13 +59,12 @@ fn bench_server_key_binary_function( bench_name: &str, display_name: &str, binary_op: F, - params_set: BenchParamsSet, ) where F: Fn(&ServerKey, &mut Ciphertext, &mut Ciphertext), { let mut bench_group = c.benchmark_group(bench_name); - for param in benchmark_parameters(params_set).iter() { + for param in raw_benchmark_parameters().iter() { let keys = KEY_CACHE.get_from_param(*param); let (cks, sks) = (keys.client_key(), keys.server_key()); @@ -173,13 +104,12 @@ fn bench_server_key_binary_scalar_function( bench_name: &str, display_name: &str, binary_op: F, - params_set: BenchParamsSet, ) where F: Fn(&ServerKey, &mut Ciphertext, u8), { let mut bench_group = c.benchmark_group(bench_name); - for param in benchmark_parameters(params_set).iter() { + for param in raw_benchmark_parameters().iter() { let keys = KEY_CACHE.get_from_param(*param); let (cks, sks) = (keys.client_key(), keys.server_key()); @@ -218,13 +148,12 @@ fn bench_server_key_binary_scalar_division_function( bench_name: &str, display_name: &str, binary_op: F, - params_set: BenchParamsSet, ) where F: Fn(&ServerKey, &mut Ciphertext, u8), { let mut bench_group = c.benchmark_group(bench_name); - for param in benchmark_parameters(params_set).iter() { + for param in raw_benchmark_parameters().iter() { let keys = KEY_CACHE.get_from_param(*param); let (cks, sks) = (keys.client_key(), keys.server_key()); @@ -262,10 +191,10 @@ fn bench_server_key_binary_scalar_division_function( bench_group.finish() } -fn carry_extract_bench(c: &mut Criterion, params_set: BenchParamsSet) { +fn carry_extract_bench(c: &mut Criterion) { let mut bench_group = c.benchmark_group("carry_extract"); - for param in benchmark_parameters(params_set).iter() { + for param in raw_benchmark_parameters().iter() { let keys = KEY_CACHE.get_from_param(*param); let (cks, sks) = (keys.client_key(), keys.server_key()); @@ -298,10 +227,10 @@ fn carry_extract_bench(c: &mut Criterion, params_set: BenchParamsSet) { bench_group.finish() } -fn programmable_bootstrapping_bench(c: &mut Criterion, params_set: BenchParamsSet) { +fn programmable_bootstrapping_bench(c: &mut Criterion) { let mut bench_group = c.benchmark_group("programmable_bootstrap"); - for param in benchmark_parameters(params_set).iter() { + for param in raw_benchmark_parameters().iter() { let keys = KEY_CACHE.get_from_param(*param); let (cks, sks) = (keys.client_key(), keys.server_key()); @@ -343,11 +272,12 @@ fn server_key_from_compressed_key(c: &mut Criterion) { .sample_size(10) .measurement_time(std::time::Duration::from_secs(60)); - let mut params = SERVER_KEY_BENCH_PARAMS_EXTENDED + let mut params = SHORTINT_BENCH_PARAMS_TUNIFORM .iter() + .chain(SHORTINT_BENCH_PARAMS_GAUSSIAN.iter()) .map(|p| (*p).into()) .collect::>(); - let multi_bit_params = SERVER_KEY_MULTI_BIT_BENCH_PARAMS_EXTENDED + let multi_bit_params = SHORTINT_MULTI_BIT_BENCH_PARAMS .iter() .map(|p| (*p).into()) .collect::>(); @@ -386,7 +316,7 @@ fn server_key_from_compressed_key(c: &mut Criterion) { } macro_rules! define_server_key_unary_bench_fn ( - (method_name:$server_key_method:ident, display_name:$name:ident, $params_set:expr) => { + (method_name:$server_key_method:ident, display_name:$name:ident) => { fn $server_key_method(c: &mut Criterion) { bench_server_key_unary_function( c, @@ -394,14 +324,13 @@ macro_rules! define_server_key_unary_bench_fn ( stringify!($name), |server_key, lhs| { let _ = server_key.$server_key_method(lhs);}, - $params_set ) } } ); macro_rules! define_server_key_bench_fn ( - (method_name:$server_key_method:ident, display_name:$name:ident, $params_set:expr) => { + (method_name:$server_key_method:ident, display_name:$name:ident) => { fn $server_key_method(c: &mut Criterion) { bench_server_key_binary_function( c, @@ -409,14 +338,13 @@ macro_rules! define_server_key_bench_fn ( stringify!($name), |server_key, lhs, rhs| { let _ = server_key.$server_key_method(lhs, rhs);}, - $params_set ) } } ); macro_rules! define_server_key_scalar_bench_fn ( - (method_name:$server_key_method:ident, display_name:$name:ident, $params_set:expr) => { + (method_name:$server_key_method:ident, display_name:$name:ident) => { fn $server_key_method(c: &mut Criterion) { bench_server_key_binary_scalar_function( c, @@ -424,14 +352,13 @@ macro_rules! define_server_key_scalar_bench_fn ( stringify!($name), |server_key, lhs, rhs| { let _ = server_key.$server_key_method(lhs, rhs);}, - $params_set ) } } ); macro_rules! define_server_key_scalar_div_bench_fn ( - (method_name:$server_key_method:ident, display_name:$name:ident, $params_set:expr) => { + (method_name:$server_key_method:ident, display_name:$name:ident) => { fn $server_key_method(c: &mut Criterion) { bench_server_key_binary_scalar_division_function( c, @@ -439,19 +366,17 @@ macro_rules! define_server_key_scalar_div_bench_fn ( stringify!($name), |server_key, lhs, rhs| { let _ = server_key.$server_key_method(lhs, rhs);}, - $params_set ) } } ); macro_rules! define_custom_bench_fn ( - (function_name:$function:ident, $params_set:expr) => { + (function_name:$function:ident) => { fn $function(c: &mut Criterion) { ::paste::paste! { [<$function _bench>]( c, - $params_set ) } } @@ -460,259 +385,208 @@ macro_rules! define_custom_bench_fn ( define_server_key_unary_bench_fn!( method_name: unchecked_neg, - display_name: negation, - BenchParamsSet::Standard + display_name: negation ); - define_server_key_bench_fn!( method_name: unchecked_add, - display_name: add, - BenchParamsSet::Extended + display_name: add ); define_server_key_bench_fn!( method_name: unchecked_sub, - display_name: sub, - BenchParamsSet::Extended + display_name: sub ); define_server_key_bench_fn!( method_name: unchecked_mul_lsb, - display_name: mul, - BenchParamsSet::Extended + display_name: mul ); define_server_key_bench_fn!( method_name: unchecked_mul_msb, - display_name: mul, - BenchParamsSet::Standard + display_name: mul ); define_server_key_bench_fn!( method_name: unchecked_div, - display_name: div, - BenchParamsSet::Extended + display_name: div ); define_server_key_bench_fn!( method_name: smart_bitand, - display_name: bitand, - BenchParamsSet::Standard + display_name: bitand ); define_server_key_bench_fn!( method_name: smart_bitor, - display_name: bitor, - BenchParamsSet::Standard + display_name: bitor ); define_server_key_bench_fn!( method_name: smart_bitxor, - display_name: bitxor, - BenchParamsSet::Standard + display_name: bitxor ); define_server_key_bench_fn!( method_name: smart_add, - display_name: add, - BenchParamsSet::Standard + display_name: add ); define_server_key_bench_fn!( method_name: smart_sub, - display_name: sub, - BenchParamsSet::Standard + display_name: sub ); define_server_key_bench_fn!( method_name: smart_mul_lsb, - display_name: mul, - BenchParamsSet::Standard + display_name: mul ); define_server_key_bench_fn!( method_name: bitand, - display_name: bitand, - BenchParamsSet::Standard + display_name: bitand ); define_server_key_bench_fn!( method_name: bitor, - display_name: bitor, - BenchParamsSet::Standard + display_name: bitor ); define_server_key_bench_fn!( method_name: bitxor, - display_name: bitxor, - BenchParamsSet::Standard + display_name: bitxor ); define_server_key_bench_fn!( method_name: add, - display_name: add, - BenchParamsSet::Standard + display_name: add ); define_server_key_bench_fn!( method_name: sub, - display_name: sub, - BenchParamsSet::Standard + display_name: sub ); define_server_key_bench_fn!( method_name: mul, - display_name: mul, - BenchParamsSet::Standard + display_name: mul ); define_server_key_bench_fn!( method_name: div, - display_name: div, - BenchParamsSet::Standard + display_name: div ); define_server_key_bench_fn!( method_name: greater, - display_name: greater_than, - BenchParamsSet::Standard + display_name: greater_than ); define_server_key_bench_fn!( method_name: greater_or_equal, - display_name: greater_or_equal, - BenchParamsSet::Standard + display_name: greater_or_equal ); define_server_key_bench_fn!( method_name: less, - display_name: less_than, - BenchParamsSet::Standard + display_name: less_than ); define_server_key_bench_fn!( method_name: less_or_equal, - display_name: less_or_equal, - BenchParamsSet::Standard + display_name: less_or_equal ); define_server_key_bench_fn!( method_name: equal, - display_name: equal, - BenchParamsSet::Standard + display_name: equal ); define_server_key_bench_fn!( method_name: not_equal, - display_name: not_equal, - BenchParamsSet::Standard + display_name: not_equal ); define_server_key_unary_bench_fn!( method_name: neg, - display_name: negation, - BenchParamsSet::Standard + display_name: negation ); define_server_key_bench_fn!( method_name: unchecked_greater, - display_name: greater_than, - BenchParamsSet::Standard + display_name: greater_than ); define_server_key_bench_fn!( method_name: unchecked_less, - display_name: less_than, - BenchParamsSet::Standard + display_name: less_than ); define_server_key_bench_fn!( method_name: unchecked_equal, - display_name: equal, - BenchParamsSet::Standard + display_name: equal ); define_server_key_scalar_bench_fn!( method_name: unchecked_scalar_add, - display_name: add, - BenchParamsSet::Extended + display_name: add ); define_server_key_scalar_bench_fn!( method_name: unchecked_scalar_sub, - display_name: sub, - BenchParamsSet::Extended + display_name: sub ); define_server_key_scalar_bench_fn!( method_name: unchecked_scalar_mul, - display_name: mul, - BenchParamsSet::Extended + display_name: mul ); define_server_key_scalar_bench_fn!( method_name: unchecked_scalar_left_shift, - display_name: left_shift, - BenchParamsSet::Standard + display_name: left_shift ); define_server_key_scalar_bench_fn!( method_name: unchecked_scalar_right_shift, - display_name: right_shift, - BenchParamsSet::Standard + display_name: right_shift ); define_server_key_scalar_div_bench_fn!( method_name: unchecked_scalar_div, - display_name: div, - BenchParamsSet::Extended + display_name: div ); define_server_key_scalar_div_bench_fn!( method_name: unchecked_scalar_mod, - display_name: modulo, - BenchParamsSet::Standard + display_name: modulo ); define_server_key_scalar_bench_fn!( method_name: scalar_add, - display_name: add, - BenchParamsSet::Standard + display_name: add ); define_server_key_scalar_bench_fn!( method_name: scalar_sub, - display_name: sub, - BenchParamsSet::Standard + display_name: sub ); define_server_key_scalar_bench_fn!( method_name: scalar_mul, - display_name: mul, - BenchParamsSet::Standard + display_name: mul ); define_server_key_scalar_bench_fn!( method_name: scalar_left_shift, - display_name: left_shift, - BenchParamsSet::Standard + display_name: left_shift ); define_server_key_scalar_bench_fn!( method_name: scalar_right_shift, - display_name: right_shift, - BenchParamsSet::Standard + display_name: right_shift ); define_server_key_scalar_div_bench_fn!( method_name: scalar_div, - display_name: div, - BenchParamsSet::Standard + display_name: div ); define_server_key_scalar_div_bench_fn!( method_name: scalar_mod, - display_name: modulo, - BenchParamsSet::Standard + display_name: modulo ); define_server_key_scalar_bench_fn!( method_name: scalar_greater, - display_name: greater_than, - BenchParamsSet::Standard + display_name: greater_than ); define_server_key_scalar_bench_fn!( method_name: scalar_greater_or_equal, - display_name: greater_or_equal, - BenchParamsSet::Standard + display_name: greater_or_equal ); define_server_key_scalar_bench_fn!( method_name: scalar_less, - display_name: less_than, - BenchParamsSet::Standard + display_name: less_than ); define_server_key_scalar_bench_fn!( method_name: scalar_less_or_equal, - display_name: less_or_equal, - BenchParamsSet::Standard + display_name: less_or_equal ); define_server_key_scalar_div_bench_fn!( method_name: scalar_equal, - display_name: equal, - BenchParamsSet::Standard + display_name: equal ); define_server_key_scalar_div_bench_fn!( method_name: scalar_not_equal, - display_name: not_equal, - BenchParamsSet::Standard + display_name: not_equal ); -define_custom_bench_fn!(function_name: carry_extract, BenchParamsSet::Standard); +define_custom_bench_fn!(function_name: carry_extract); define_custom_bench_fn!( - function_name: programmable_bootstrapping, - BenchParamsSet::Standard + function_name: programmable_bootstrapping ); criterion_group!( @@ -722,7 +596,7 @@ criterion_group!( smart_bitxor, smart_add, smart_sub, - smart_mul_lsb, + smart_mul_lsb ); criterion_group!( @@ -748,7 +622,7 @@ criterion_group!( unchecked_scalar_div, unchecked_scalar_mod, unchecked_scalar_left_shift, - unchecked_scalar_right_shift, + unchecked_scalar_right_shift ); criterion_group!( diff --git a/tfhe/benches/shortint/casting.rs b/tfhe-benchmark/benches/shortint/casting.rs similarity index 73% rename from tfhe/benches/shortint/casting.rs rename to tfhe-benchmark/benches/shortint/casting.rs index 7d1008834..f0162ac3e 100644 --- a/tfhe/benches/shortint/casting.rs +++ b/tfhe-benchmark/benches/shortint/casting.rs @@ -1,10 +1,8 @@ -use crate::utilities::{write_to_json, OperatorType}; +use benchmark::params_aliases::*; +use benchmark::utilities::{write_to_json, OperatorType}; use criterion::Criterion; use rayon::prelude::*; -use tfhe::shortint::parameters::current_params::{ - V1_2_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128, - V1_2_PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, -}; +use tfhe::keycache::NamedParam; use tfhe::shortint::prelude::*; pub fn pack_cast_64(c: &mut Criterion) { @@ -12,12 +10,12 @@ pub fn pack_cast_64(c: &mut Criterion) { let mut bench_group = c.benchmark_group(bench_name); let (client_key_1, server_key_1): (ClientKey, ServerKey) = - gen_keys(V1_2_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128); + gen_keys(BENCH_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128); let (client_key_2, server_key_2): (ClientKey, ServerKey) = - gen_keys(V1_2_PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128); + gen_keys(BENCH_PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128); - let ks_param = V1_2_PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS_GAUSSIAN_2M128; - let ks_param_name = "V1_2_PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS_GAUSSIAN_2M128"; + let ks_param = BENCH_PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS_GAUSSIAN_2M128; + let ks_param_name = ks_param.name(); let ksk = KeySwitchingKey::new( (&client_key_1, Some(&server_key_1)), @@ -63,12 +61,12 @@ pub fn pack_cast(c: &mut Criterion) { let mut bench_group = c.benchmark_group(bench_name); let (client_key_1, server_key_1): (ClientKey, ServerKey) = - gen_keys(V1_2_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128); + gen_keys(BENCH_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128); let (client_key_2, server_key_2): (ClientKey, ServerKey) = - gen_keys(V1_2_PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128); + gen_keys(BENCH_PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128); - let ks_param = V1_2_PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS_GAUSSIAN_2M128; - let ks_param_name = "V1_2_PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS_GAUSSIAN_2M128"; + let ks_param = BENCH_PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS_GAUSSIAN_2M128; + let ks_param_name = "BENCH_PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS_GAUSSIAN_2M128"; let ksk = KeySwitchingKey::new( (&client_key_1, Some(&server_key_1)), @@ -104,12 +102,12 @@ pub fn cast(c: &mut Criterion) { let mut bench_group = c.benchmark_group(bench_name); let (client_key_1, server_key_1): (ClientKey, ServerKey) = - gen_keys(V1_2_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128); + gen_keys(BENCH_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128); let (client_key_2, server_key_2): (ClientKey, ServerKey) = - gen_keys(V1_2_PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128); + gen_keys(BENCH_PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128); - let ks_param = V1_2_PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS_GAUSSIAN_2M128; - let ks_param_name = "V1_2_PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS_GAUSSIAN_2M128"; + let ks_param = BENCH_PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS_GAUSSIAN_2M128; + let ks_param_name = "BENCH_PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS_GAUSSIAN_2M128"; let ksk = KeySwitchingKey::new( (&client_key_1, Some(&server_key_1)), diff --git a/tfhe/benches/shortint/glwe_packing_compression.rs b/tfhe-benchmark/benches/shortint/glwe_packing_compression.rs similarity index 89% rename from tfhe/benches/shortint/glwe_packing_compression.rs rename to tfhe-benchmark/benches/shortint/glwe_packing_compression.rs index b11408cc7..78a633e46 100644 --- a/tfhe/benches/shortint/glwe_packing_compression.rs +++ b/tfhe-benchmark/benches/shortint/glwe_packing_compression.rs @@ -1,15 +1,12 @@ +use benchmark::params_aliases::*; use criterion::{black_box, criterion_group, Criterion}; use rayon::iter::{IntoParallelIterator, ParallelIterator}; -use tfhe::shortint::parameters::{ - COMP_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, - PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, -}; use tfhe::shortint::prelude::*; fn glwe_packing(c: &mut Criterion) { - let param = PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; + let param = BENCH_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; - let comp_param = COMP_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; + let comp_param = BENCH_COMP_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; let number_to_pack = 256; diff --git a/tfhe/benches/shortint/oprf.rs b/tfhe-benchmark/benches/shortint/oprf.rs similarity index 88% rename from tfhe/benches/shortint/oprf.rs rename to tfhe-benchmark/benches/shortint/oprf.rs index 560d8ea15..6e6579aa2 100644 --- a/tfhe/benches/shortint/oprf.rs +++ b/tfhe-benchmark/benches/shortint/oprf.rs @@ -1,7 +1,7 @@ +use benchmark::params_aliases::*; use criterion::{black_box, criterion_group, Criterion}; use tfhe::keycache::NamedParam; use tfhe::shortint::keycache::KEY_CACHE; -use tfhe::shortint::parameters::*; use tfhe_csprng::seeders::Seed; fn oprf(c: &mut Criterion) { @@ -9,7 +9,7 @@ fn oprf(c: &mut Criterion) { let mut bench_group = c.benchmark_group(bench_name); - let param = PARAM_MESSAGE_2_CARRY_2_KS_PBS; + let param = BENCH_PARAM_MESSAGE_2_CARRY_2_KS_PBS; let keys = KEY_CACHE.get_from_param(param); let sks = keys.server_key(); diff --git a/tfhe/examples/utilities/boolean_key_sizes.rs b/tfhe-benchmark/src/bin/boolean_key_sizes.rs similarity index 96% rename from tfhe/examples/utilities/boolean_key_sizes.rs rename to tfhe-benchmark/src/bin/boolean_key_sizes.rs index 5ffa14eef..16e12c0ff 100644 --- a/tfhe/examples/utilities/boolean_key_sizes.rs +++ b/tfhe-benchmark/src/bin/boolean_key_sizes.rs @@ -1,7 +1,4 @@ -#[path = "../../benches/utilities.rs"] -mod utilities; - -use crate::utilities::{write_to_json, OperatorType}; +use benchmark::utilities::{write_to_json, OperatorType}; use std::fs::{File, OpenOptions}; use std::io::Write; use std::path::Path; diff --git a/tfhe/examples/utilities/hlapi_compact_pk_ct_sizes.rs b/tfhe-benchmark/src/bin/hlapi_compact_pk_ct_sizes.rs similarity index 85% rename from tfhe/examples/utilities/hlapi_compact_pk_ct_sizes.rs rename to tfhe-benchmark/src/bin/hlapi_compact_pk_ct_sizes.rs index 8c6425404..5695978bd 100644 --- a/tfhe/examples/utilities/hlapi_compact_pk_ct_sizes.rs +++ b/tfhe-benchmark/src/bin/hlapi_compact_pk_ct_sizes.rs @@ -1,18 +1,11 @@ -#[path = "../../benches/utilities.rs"] -mod utilities; - -use crate::utilities::{write_to_json, OperatorType}; +use benchmark::params_aliases::*; +use benchmark::utilities::{write_to_json, OperatorType}; use rand::Rng; use std::fs::{File, OpenOptions}; use std::io::Write; use std::path::Path; use tfhe::integer::U256; use tfhe::keycache::NamedParam; -use tfhe::shortint::parameters::{ - PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, - PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, - PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, -}; use tfhe::shortint::PBSParameters; use tfhe::{generate_keys, CompactCiphertextList, CompactPublicKey, ConfigBuilder}; @@ -38,12 +31,12 @@ pub fn cpk_and_cctl_sizes(results_file: &Path) { let operator = OperatorType::Atomic; { - let params = PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; + let params = BENCH_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; let config = ConfigBuilder::default() .use_custom_parameters(params) .use_dedicated_compact_public_key_parameters(( - PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, - PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, + BENCH_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, + BENCH_PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, )) .build(); let (client_key, _) = generate_keys(config); @@ -94,12 +87,12 @@ pub fn cpk_and_cctl_sizes(results_file: &Path) { // 256 bits { - let params = PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; + let params = BENCH_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; let config = ConfigBuilder::default() .use_custom_parameters(params) .use_dedicated_compact_public_key_parameters(( - PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, - PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, + BENCH_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, + BENCH_PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, )) .build(); let (client_key, _) = generate_keys(config); diff --git a/tfhe/examples/utilities/shortint_key_sizes.rs b/tfhe-benchmark/src/bin/shortint_key_sizes.rs similarity index 78% rename from tfhe/examples/utilities/shortint_key_sizes.rs rename to tfhe-benchmark/src/bin/shortint_key_sizes.rs index c8a50f2bb..859835288 100644 --- a/tfhe/examples/utilities/shortint_key_sizes.rs +++ b/tfhe-benchmark/src/bin/shortint_key_sizes.rs @@ -1,15 +1,11 @@ -#[path = "../../benches/utilities.rs"] -mod utilities; - -use crate::utilities::{write_to_json, CryptoParametersRecord, OperatorType}; +use benchmark::params_aliases::*; +use benchmark::utilities::{write_to_json, CryptoParametersRecord, OperatorType}; use std::fs::{File, OpenOptions}; use std::io::Write; use std::path::Path; use tfhe::keycache::NamedParam; use tfhe::shortint::atomic_pattern::compressed::CompressedAtomicPatternServerKey; use tfhe::shortint::keycache::KEY_CACHE; -use tfhe::shortint::parameters::current_params::*; -use tfhe::shortint::parameters::*; use tfhe::shortint::server_key::{StandardServerKey, StandardServerKeyView}; use tfhe::shortint::{ ClassicPBSParameters, ClientKey, CompactPrivateKey, CompressedCompactPublicKey, @@ -24,21 +20,21 @@ fn write_result(file: &mut File, name: &str, value: usize) { fn client_server_key_sizes(results_file: &Path) { let shortint_params_vec: Vec = vec![ - PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128.into(), - V1_2_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128.into(), - V1_2_PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128.into(), - V1_2_PARAM_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128.into(), - V1_2_PARAM_MESSAGE_4_CARRY_4_KS_PBS_GAUSSIAN_2M128.into(), - V1_2_PARAM_MULTI_BIT_GROUP_2_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M64.into(), - V1_2_PARAM_MULTI_BIT_GROUP_2_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M64.into(), - V1_2_PARAM_MULTI_BIT_GROUP_2_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M64.into(), - V1_2_PARAM_MULTI_BIT_GROUP_3_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M64.into(), - V1_2_PARAM_MULTI_BIT_GROUP_3_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M64.into(), - V1_2_PARAM_MULTI_BIT_GROUP_3_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M64.into(), - PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128.into(), - PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128.into(), - PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128.into(), - PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128.into(), + BENCH_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128.into(), + BENCH_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128.into(), + BENCH_PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128.into(), + BENCH_PARAM_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128.into(), + BENCH_PARAM_MESSAGE_4_CARRY_4_KS_PBS_GAUSSIAN_2M128.into(), + BENCH_PARAM_MULTI_BIT_GROUP_2_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128.into(), + BENCH_PARAM_MULTI_BIT_GROUP_2_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128.into(), + BENCH_PARAM_MULTI_BIT_GROUP_2_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128.into(), + BENCH_PARAM_MULTI_BIT_GROUP_3_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128.into(), + BENCH_PARAM_MULTI_BIT_GROUP_3_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128.into(), + BENCH_PARAM_MULTI_BIT_GROUP_3_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128.into(), + BENCH_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128.into(), + BENCH_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128.into(), + BENCH_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128.into(), + BENCH_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128.into(), ]; File::create(results_file).expect("create results file failed"); let mut file = OpenOptions::new() @@ -161,7 +157,7 @@ fn tuniform_key_set_sizes(results_file: &Path) { println!("Measuring shortint key sizes:"); - let param_fhe = PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; + let param_fhe = BENCH_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; let param_fhe_name = param_fhe.name(); let cks = ClientKey::new(param_fhe); let compressed_sks = CompressedServerKey::new(&cks); @@ -210,27 +206,27 @@ fn tuniform_key_set_sizes(results_file: &Path) { &mut file, ); - let param_pke = PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; - let param_pke_name = stringify!(PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128); + let param_pke = BENCH_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; + let param_pke_name = param_pke.name(); let compact_private_key = CompactPrivateKey::new(param_pke); let compressed_pk = CompressedCompactPublicKey::new(&compact_private_key); let pk = compressed_pk.decompress(); - measure_serialized_size(&pk, param_pke, param_pke_name, "cpk", "CPK", &mut file); + measure_serialized_size(&pk, param_pke, ¶m_pke_name, "cpk", "CPK", &mut file); measure_serialized_size( &compressed_pk, param_pke, - param_pke_name, + ¶m_pke_name, "cpk_compressed", "CPK", &mut file, ); - let param_compression = COMP_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; - let param_compression_name = stringify!(COMP_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128); + let param_compression = BENCH_COMP_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; + let param_compression_name = param_compression.name(); let params_tuple = ( param_compression, - PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, + BENCH_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, ); let private_compression_key = cks.new_compression_private_key(param_compression); @@ -240,7 +236,7 @@ fn tuniform_key_set_sizes(results_file: &Path) { measure_serialized_size( &compression_key, params_tuple, - param_compression_name, + ¶m_compression_name, "compression_key", "CompressionKey", &mut file, @@ -248,14 +244,14 @@ fn tuniform_key_set_sizes(results_file: &Path) { measure_serialized_size( &decompression_key, params_tuple, - param_compression_name, + ¶m_compression_name, "decompression_key", "CompressionKey", &mut file, ); - let param_casting = PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; - let param_casting_name = stringify!(PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128); + let param_casting = BENCH_PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; + let param_casting_name = param_casting.name(); let compressed_casting_key = CompressedKeySwitchingKey::new( (&compact_private_key, None), (&cks, &compressed_sks), @@ -266,7 +262,7 @@ fn tuniform_key_set_sizes(results_file: &Path) { measure_serialized_size( &casting_key.into_raw_parts().0, param_casting, - param_casting_name, + ¶m_casting_name, "casting_key", "CastKey", &mut file, @@ -274,7 +270,7 @@ fn tuniform_key_set_sizes(results_file: &Path) { measure_serialized_size( &compressed_casting_key.into_raw_parts().0, param_casting, - param_casting_name, + ¶m_casting_name, "casting_key_compressed", "CastKey", &mut file, diff --git a/tfhe/examples/utilities/wasm_benchmarks_parser.rs b/tfhe-benchmark/src/bin/wasm_benchmarks_parser.rs similarity index 96% rename from tfhe/examples/utilities/wasm_benchmarks_parser.rs rename to tfhe-benchmark/src/bin/wasm_benchmarks_parser.rs index 687c1c095..99607829b 100644 --- a/tfhe/examples/utilities/wasm_benchmarks_parser.rs +++ b/tfhe-benchmark/src/bin/wasm_benchmarks_parser.rs @@ -1,7 +1,4 @@ -#[path = "../../benches/utilities.rs"] -mod utilities; - -use crate::utilities::{write_to_json, OperatorType}; +use benchmark::utilities::{write_to_json, OperatorType}; use clap::Parser; use std::collections::HashMap; use std::fs; diff --git a/tfhe-benchmark/src/lib.rs b/tfhe-benchmark/src/lib.rs new file mode 100644 index 000000000..30ac1ec81 --- /dev/null +++ b/tfhe-benchmark/src/lib.rs @@ -0,0 +1,3 @@ +pub mod params; +pub mod params_aliases; +pub mod utilities; diff --git a/tfhe-benchmark/src/params.rs b/tfhe-benchmark/src/params.rs new file mode 100644 index 000000000..24c5712df --- /dev/null +++ b/tfhe-benchmark/src/params.rs @@ -0,0 +1,449 @@ +#[cfg(feature = "boolean")] +pub mod boolean_params { + use crate::utilities::CryptoParametersRecord; + use tfhe::boolean::parameters::{ + DEFAULT_PARAMETERS, DEFAULT_PARAMETERS_KS_PBS, PARAMETERS_ERROR_PROB_2_POW_MINUS_165, + }; + + pub fn benchmark_32bits_parameters() -> Vec<(String, CryptoParametersRecord)> { + [ + ("BOOLEAN_DEFAULT_PARAMS", DEFAULT_PARAMETERS), + ( + "BOOLEAN_TFHE_LIB_PARAMS", + PARAMETERS_ERROR_PROB_2_POW_MINUS_165, + ), + ("BOOLEAN_DEFAULT_PARAMS_KS_PBS", DEFAULT_PARAMETERS_KS_PBS), + ] + .iter() + .map(|(name, params)| (name.to_string(), params.to_owned().into())) + .collect() + } +} + +#[cfg(feature = "boolean")] +pub use boolean_params::*; + +#[cfg(feature = "shortint")] +pub mod shortint_params { + use crate::params_aliases::*; + use crate::utilities::CryptoParametersRecord; + use std::collections::HashMap; + use std::env; + use std::sync::OnceLock; + use tfhe::core_crypto::prelude::{DynamicDistribution, LweBskGroupingFactor}; + use tfhe::keycache::NamedParam; + use tfhe::shortint::{ + CarryModulus, ClassicPBSParameters, MessageModulus, MultiBitPBSParameters, PBSParameters, + }; + + pub const SHORTINT_BENCH_PARAMS_TUNIFORM: [ClassicPBSParameters; 4] = [ + BENCH_PARAM_MESSAGE_1_CARRY_1_KS_PBS_TUNIFORM_2M128, + BENCH_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, + BENCH_PARAM_MESSAGE_3_CARRY_3_KS_PBS_TUNIFORM_2M128, + BENCH_PARAM_MESSAGE_4_CARRY_4_KS_PBS_TUNIFORM_2M128, + ]; + + pub const SHORTINT_BENCH_PARAMS_GAUSSIAN: [ClassicPBSParameters; 4] = [ + BENCH_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128, + BENCH_PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, + BENCH_PARAM_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128, + BENCH_PARAM_MESSAGE_4_CARRY_4_KS_PBS_GAUSSIAN_2M128, + ]; + + #[cfg(feature = "gpu")] + pub const SHORTINT_MULTI_BIT_BENCH_PARAMS: [MultiBitPBSParameters; 6] = [ + BENCH_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_1_CARRY_1_KS_PBS_TUNIFORM_2M128, + BENCH_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, + BENCH_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_3_CARRY_3_KS_PBS_TUNIFORM_2M128, + BENCH_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128, + BENCH_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, + BENCH_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128, + ]; + + #[cfg(not(feature = "gpu"))] + pub const SHORTINT_MULTI_BIT_BENCH_PARAMS: [MultiBitPBSParameters; 6] = [ + BENCH_PARAM_MULTI_BIT_GROUP_2_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128, + BENCH_PARAM_MULTI_BIT_GROUP_2_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, + BENCH_PARAM_MULTI_BIT_GROUP_2_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128, + BENCH_PARAM_MULTI_BIT_GROUP_3_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128, + BENCH_PARAM_MULTI_BIT_GROUP_3_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128, + BENCH_PARAM_MULTI_BIT_GROUP_3_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128, + ]; + + pub fn benchmark_parameters() -> Vec<(String, CryptoParametersRecord)> { + match get_parameters_set() { + ParametersSet::Default => SHORTINT_BENCH_PARAMS_TUNIFORM + .iter() + .chain(SHORTINT_BENCH_PARAMS_GAUSSIAN.iter()) + .map(|params| { + ( + params.name(), + >::into(*params) + .to_owned() + .into(), + ) + }) + .collect(), + ParametersSet::All => { + filter_parameters( + &BENCH_ALL_CLASSIC_PBS_PARAMETERS, + DesiredNoiseDistribution::Both, + DesiredBackend::Cpu, // No parameters set are specific to GPU in this vector + ) + .into_iter() + .map(|(params, name)| { + ( + name.to_string(), + >::into(*params) + .to_owned() + .into(), + ) + }) + .collect() + } + } + } + + pub fn multi_bit_benchmark_parameters() -> Vec<(String, CryptoParametersRecord)> { + match get_parameters_set() { + ParametersSet::Default => SHORTINT_MULTI_BIT_BENCH_PARAMS + .iter() + .map(|params| { + ( + params.name(), + >::into(*params) + .to_owned() + .into(), + ) + }) + .collect(), + ParametersSet::All => { + let desired_backend = if cfg!(feature = "gpu") { + DesiredBackend::Gpu + } else { + DesiredBackend::Cpu + }; + filter_parameters( + &BENCH_ALL_MULTI_BIT_PBS_PARAMETERS, + DesiredNoiseDistribution::Both, + desired_backend, + ) + .into_iter() + .map(|(params, name)| { + ( + name.to_string(), + >::into(*params) + .to_owned() + .into(), + ) + }) + .collect() + } + } + } + + pub fn multi_bit_benchmark_parameters_with_grouping( + ) -> Vec<(String, CryptoParametersRecord, LweBskGroupingFactor)> { + match get_parameters_set() { + ParametersSet::Default => SHORTINT_MULTI_BIT_BENCH_PARAMS + .iter() + .map(|params| { + ( + params.name(), + >::into(*params) + .to_owned() + .into(), + params.grouping_factor, + ) + }) + .collect(), + ParametersSet::All => { + let desired_backend = if cfg!(feature = "gpu") { + DesiredBackend::Gpu + } else { + DesiredBackend::Cpu + }; + filter_parameters( + &BENCH_ALL_MULTI_BIT_PBS_PARAMETERS, + DesiredNoiseDistribution::Both, + desired_backend, + ) + .into_iter() + .map(|(params, name)| { + ( + name.to_string(), + >::into(*params) + .to_owned() + .into(), + params.grouping_factor, + ) + }) + .collect() + } + } + } + + pub fn raw_benchmark_parameters() -> Vec { + let is_multi_bit = match env::var("__TFHE_RS_PARAM_TYPE") { + Ok(val) => val.to_lowercase() == "multi_bit", + Err(_) => false, + }; + + if is_multi_bit { + SHORTINT_MULTI_BIT_BENCH_PARAMS + .iter() + .map(|p| (*p).into()) + .collect() + } else { + SHORTINT_BENCH_PARAMS_TUNIFORM + .iter() + .chain(SHORTINT_BENCH_PARAMS_GAUSSIAN.iter()) + .map(|p| (*p).into()) + .collect() + } + } + + pub fn benchmark_compression_parameters() -> Vec<(String, CryptoParametersRecord)> { + vec![( + BENCH_COMP_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128.name(), + ( + BENCH_COMP_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, + BENCH_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, + ) + .into(), + )] + } + + // This array has been built according to performance benchmarks measuring latency over a + // matrix of 4 parameters set, 3 grouping factor and a wide range of threads values. + // The values available here as u64 are the optimal number of threads to use for a given triplet + // representing one or more parameters set. + const MULTI_BIT_THREADS_ARRAY: [((MessageModulus, CarryModulus, LweBskGroupingFactor), u64); + 12] = [ + ( + (MessageModulus(2), CarryModulus(2), LweBskGroupingFactor(2)), + 5, + ), + ( + (MessageModulus(4), CarryModulus(4), LweBskGroupingFactor(2)), + 5, + ), + ( + (MessageModulus(8), CarryModulus(8), LweBskGroupingFactor(2)), + 5, + ), + ( + ( + MessageModulus(16), + CarryModulus(16), + LweBskGroupingFactor(2), + ), + 5, + ), + ( + (MessageModulus(2), CarryModulus(2), LweBskGroupingFactor(3)), + 7, + ), + ( + (MessageModulus(4), CarryModulus(4), LweBskGroupingFactor(3)), + 9, + ), + ( + (MessageModulus(8), CarryModulus(8), LweBskGroupingFactor(3)), + 10, + ), + ( + ( + MessageModulus(16), + CarryModulus(16), + LweBskGroupingFactor(3), + ), + 10, + ), + ( + (MessageModulus(2), CarryModulus(2), LweBskGroupingFactor(4)), + 11, + ), + ( + (MessageModulus(4), CarryModulus(4), LweBskGroupingFactor(4)), + 13, + ), + ( + (MessageModulus(8), CarryModulus(8), LweBskGroupingFactor(4)), + 11, + ), + ( + ( + MessageModulus(16), + CarryModulus(16), + LweBskGroupingFactor(4), + ), + 11, + ), + ]; + + /// Define the number of threads to use for parameters doing multithreaded programmable + /// bootstrapping. + /// + /// Parameters must have the same values between message and carry modulus. + /// Grouping factor 2, 3 and 4 are the only ones that are supported. + pub fn multi_bit_num_threads( + message_modulus: u64, + carry_modulus: u64, + grouping_factor: usize, + ) -> Option { + // TODO Implement an interpolation mechanism for X_Y parameters set + if message_modulus != carry_modulus || ![2, 3, 4].contains(&(grouping_factor as i32)) { + return None; + } + let thread_map: HashMap<(MessageModulus, CarryModulus, LweBskGroupingFactor), u64> = + HashMap::from_iter(MULTI_BIT_THREADS_ARRAY); + thread_map + .get(&( + MessageModulus(message_modulus), + CarryModulus(carry_modulus), + LweBskGroupingFactor(grouping_factor), + )) + .copied() + } + + pub static PARAMETERS_SET: OnceLock = OnceLock::new(); + + pub enum ParametersSet { + Default, + All, + } + + impl ParametersSet { + pub fn from_env() -> Result { + let raw_value = env::var("__TFHE_RS_PARAMS_SET").unwrap_or("default".to_string()); + match raw_value.to_lowercase().as_str() { + "default" => Ok(ParametersSet::Default), + "all" => Ok(ParametersSet::All), + _ => Err(format!("parameters set '{raw_value}' is not supported")), + } + } + } + + pub fn get_parameters_set() -> &'static ParametersSet { + PARAMETERS_SET.get_or_init(|| ParametersSet::from_env().unwrap()) + } + + #[derive(Clone, Copy, Debug)] + pub enum DesiredNoiseDistribution { + Gaussian, + TUniform, + Both, + } + + #[derive(Clone, Copy, Debug)] + pub enum DesiredBackend { + Cpu, + Gpu, + } + + impl DesiredBackend { + fn matches_parameter_name_backend(&self, param_name: &str) -> bool { + matches!( + (self, param_name.to_lowercase().contains("gpu")), + (DesiredBackend::Cpu, false) | (DesiredBackend::Gpu, true) + ) + } + } + + pub fn filter_parameters<'a, P: Copy + Into>( + params: &[(&'a P, &'a str)], + desired_noise_distribution: DesiredNoiseDistribution, + desired_backend: DesiredBackend, + ) -> Vec<(&'a P, &'a str)> { + params + .iter() + .filter_map(|(p, name)| { + let temp_param: PBSParameters = (**p).into(); + + match ( + temp_param.lwe_noise_distribution(), + desired_noise_distribution, + ) { + // If it's one of the pairs, we continue the process. + (DynamicDistribution::Gaussian(_), DesiredNoiseDistribution::Gaussian) + | (DynamicDistribution::TUniform(_), DesiredNoiseDistribution::TUniform) + | (_, DesiredNoiseDistribution::Both) => (), + _ => return None, + } + + if !desired_backend.matches_parameter_name_backend(name) { + return None; + }; + + Some((*p, *name)) + }) + .collect() + } +} + +#[cfg(feature = "shortint")] +pub use shortint_params::*; + +#[cfg(feature = "integer")] +mod integer_params { + use crate::params_aliases::*; + use crate::utilities::EnvConfig; + use itertools::iproduct; + use std::vec::IntoIter; + use tfhe::shortint::PBSParameters; + + /// An iterator that yields a succession of combinations + /// of parameters and a num_block to achieve a certain bit_size ciphertext + /// in radix decomposition + pub struct ParamsAndNumBlocksIter { + params_and_bit_sizes: itertools::Product, IntoIter>, + } + + impl Default for ParamsAndNumBlocksIter { + fn default() -> Self { + let env_config = EnvConfig::new(); + + if env_config.is_multi_bit { + #[cfg(feature = "gpu")] + let params = vec![ + BENCH_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128 + .into(), + ]; + #[cfg(not(feature = "gpu"))] + let params = vec![ + BENCH_PARAM_MULTI_BIT_GROUP_3_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128.into(), + ]; + + let params_and_bit_sizes = iproduct!(params, env_config.bit_sizes()); + Self { + params_and_bit_sizes, + } + } else { + // FIXME One set of parameter is tested since we want to benchmark only quickest + // operations. + let params = vec![BENCH_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128.into()]; + + let params_and_bit_sizes = iproduct!(params, env_config.bit_sizes()); + Self { + params_and_bit_sizes, + } + } + } + } + + impl Iterator for ParamsAndNumBlocksIter { + type Item = (PBSParameters, usize, usize); + + fn next(&mut self) -> Option { + let (param, bit_size) = self.params_and_bit_sizes.next()?; + let num_block = + (bit_size as f64 / (param.message_modulus().0 as f64).log(2.0)).ceil() as usize; + + Some((param, num_block, bit_size)) + } + } +} + +#[cfg(feature = "integer")] +pub use integer_params::*; diff --git a/tfhe-benchmark/src/params_aliases.rs b/tfhe-benchmark/src/params_aliases.rs new file mode 100644 index 000000000..825c09aeb --- /dev/null +++ b/tfhe-benchmark/src/params_aliases.rs @@ -0,0 +1,142 @@ +#[cfg(any(feature = "shortint", feature = "integer"))] +pub mod shortint_params_aliases { + use tfhe::shortint::parameters::current_params::*; + use tfhe::shortint::parameters::{ + ClassicPBSParameters, CompactPublicKeyEncryptionParameters, CompressionParameters, + MultiBitPBSParameters, NoiseSquashingParameters, ShortintKeySwitchingParameters, + }; + + // KS PBS Gaussian + pub const BENCH_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128: ClassicPBSParameters = + V1_2_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128; + pub const BENCH_PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128: ClassicPBSParameters = + V1_2_PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128; + pub const BENCH_PARAM_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128: ClassicPBSParameters = + V1_2_PARAM_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128; + pub const BENCH_PARAM_MESSAGE_4_CARRY_4_KS_PBS_GAUSSIAN_2M128: ClassicPBSParameters = + V1_2_PARAM_MESSAGE_4_CARRY_4_KS_PBS_GAUSSIAN_2M128; + + // KS PBS TUniform + pub const BENCH_PARAM_MESSAGE_1_CARRY_1_KS_PBS_TUNIFORM_2M128: ClassicPBSParameters = + V1_2_PARAM_MESSAGE_1_CARRY_1_KS_PBS_TUNIFORM_2M128; + pub const BENCH_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128: ClassicPBSParameters = + V1_2_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; + pub const BENCH_PARAM_MESSAGE_3_CARRY_3_KS_PBS_TUNIFORM_2M128: ClassicPBSParameters = + V1_2_PARAM_MESSAGE_3_CARRY_3_KS_PBS_TUNIFORM_2M128; + pub const BENCH_PARAM_MESSAGE_4_CARRY_4_KS_PBS_TUNIFORM_2M128: ClassicPBSParameters = + V1_2_PARAM_MESSAGE_4_CARRY_4_KS_PBS_TUNIFORM_2M128; + pub const BENCH_PARAM_MESSAGE_2_CARRY_2_KS_PBS: ClassicPBSParameters = + V1_2_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; + + pub const BENCH_ALL_CLASSIC_PBS_PARAMETERS: [(&ClassicPBSParameters, &str); 140] = + VEC_ALL_CLASSIC_PBS_PARAMETERS; + + // MultiBit + // CPU Gaussian + pub const BENCH_PARAM_MULTI_BIT_GROUP_2_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128: + MultiBitPBSParameters = + V1_2_PARAM_MULTI_BIT_GROUP_2_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128; + pub const BENCH_PARAM_MULTI_BIT_GROUP_2_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128: + MultiBitPBSParameters = + V1_2_PARAM_MULTI_BIT_GROUP_2_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128; + pub const BENCH_PARAM_MULTI_BIT_GROUP_2_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128: + MultiBitPBSParameters = + V1_2_PARAM_MULTI_BIT_GROUP_2_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128; + pub const BENCH_PARAM_MULTI_BIT_GROUP_3_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128: + MultiBitPBSParameters = + V1_2_PARAM_MULTI_BIT_GROUP_3_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128; + pub const BENCH_PARAM_MULTI_BIT_GROUP_3_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128: + MultiBitPBSParameters = + V1_2_PARAM_MULTI_BIT_GROUP_3_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128; + pub const BENCH_PARAM_MULTI_BIT_GROUP_3_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128: + MultiBitPBSParameters = + V1_2_PARAM_MULTI_BIT_GROUP_3_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128; + + // GPU Gaussian + pub const BENCH_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128: + MultiBitPBSParameters = + V1_2_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128; + pub const BENCH_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128: + MultiBitPBSParameters = + V1_2_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128; + pub const BENCH_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128: + MultiBitPBSParameters = + V1_2_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M128; + + // GPU TUniform + pub const BENCH_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_1_CARRY_1_KS_PBS_TUNIFORM_2M128: + MultiBitPBSParameters = + V1_2_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_1_CARRY_1_KS_PBS_TUNIFORM_2M128; + pub const BENCH_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128: + MultiBitPBSParameters = + V1_2_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; + pub const BENCH_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_3_CARRY_3_KS_PBS_TUNIFORM_2M128: + MultiBitPBSParameters = + V1_2_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_3_CARRY_3_KS_PBS_TUNIFORM_2M128; + pub const BENCH_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_4_CARRY_4_KS_PBS_TUNIFORM_2M128: + MultiBitPBSParameters = + V1_2_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_4_CARRY_4_KS_PBS_TUNIFORM_2M128; + + pub const BENCH_ALL_MULTI_BIT_PBS_PARAMETERS: [(&MultiBitPBSParameters, &str); 240] = + VEC_ALL_MULTI_BIT_PBS_PARAMETERS; + + // PKE + pub const BENCH_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128: + CompactPublicKeyEncryptionParameters = + V1_2_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; + pub const BENCH_PARAM_PKE_TO_BIG_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128_ZKV1: + CompactPublicKeyEncryptionParameters = + V1_2_PARAM_PKE_TO_BIG_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128_ZKV1; + + // KS + pub const BENCH_PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128: + ShortintKeySwitchingParameters = + V1_2_PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; + pub const BENCH_PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS_GAUSSIAN_2M128: + ShortintKeySwitchingParameters = + V1_2_PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS_GAUSSIAN_2M128; + pub const BENCH_PARAM_KEYSWITCH_PKE_TO_SMALL_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128: + ShortintKeySwitchingParameters = + V1_2_PARAM_KEYSWITCH_PKE_TO_SMALL_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; + pub const BENCH_PARAM_KEYSWITCH_PKE_TO_BIG_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128: + ShortintKeySwitchingParameters = + V1_2_PARAM_KEYSWITCH_PKE_TO_BIG_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; + + // ZKV1 + pub const BENCH_PARAM_PKE_TO_SMALL_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128_ZKV1: + CompactPublicKeyEncryptionParameters = + V1_2_PARAM_PKE_TO_SMALL_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128_ZKV1; + pub const BENCH_PARAM_KEYSWITCH_PKE_TO_BIG_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128_ZKV1: + ShortintKeySwitchingParameters = + V1_2_PARAM_KEYSWITCH_PKE_TO_BIG_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128_ZKV1; + pub const BENCH_PARAM_KEYSWITCH_PKE_TO_SMALL_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128_ZKV1: + ShortintKeySwitchingParameters = + V1_2_PARAM_KEYSWITCH_PKE_TO_SMALL_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128_ZKV1; + + // ZKV2 + pub const BENCH_PARAM_PKE_TO_SMALL_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128_ZKV2: + CompactPublicKeyEncryptionParameters = + V1_2_PARAM_PKE_TO_SMALL_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128_ZKV2; + pub const BENCH_PARAM_KEYSWITCH_PKE_TO_BIG_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128_ZKV2: + ShortintKeySwitchingParameters = + V1_2_PARAM_KEYSWITCH_PKE_TO_BIG_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128_ZKV2; + pub const BENCH_PARAM_KEYSWITCH_PKE_TO_SMALL_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128_ZKV2: + ShortintKeySwitchingParameters = + V1_2_PARAM_KEYSWITCH_PKE_TO_SMALL_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128_ZKV2; + + // Compression + pub const BENCH_COMP_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128: CompressionParameters = + V1_2_COMP_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; + + pub const BENCH_COMP_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128: + CompressionParameters = + V1_2_COMP_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; + + // Noise Squashing + pub const BENCH_NOISE_SQUASHING_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128: + NoiseSquashingParameters = + V1_2_NOISE_SQUASHING_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128; +} + +#[cfg(any(feature = "shortint", feature = "integer"))] +pub use shortint_params_aliases::*; diff --git a/tfhe/benches/utilities.rs b/tfhe-benchmark/src/utilities.rs similarity index 72% rename from tfhe/benches/utilities.rs rename to tfhe-benchmark/src/utilities.rs index d1b0b31b3..75fbfda5c 100644 --- a/tfhe/benches/utilities.rs +++ b/tfhe-benchmark/src/utilities.rs @@ -1,5 +1,4 @@ use serde::Serialize; -use std::collections::HashMap; use std::path::PathBuf; use std::sync::OnceLock; use std::{env, fs}; @@ -38,78 +37,14 @@ pub use boolean_utils::*; #[cfg(feature = "shortint")] pub mod shortint_utils { use super::*; - use itertools::iproduct; - use std::vec::IntoIter; - use tfhe::shortint::atomic_pattern::AtomicPatternParameters; use tfhe::shortint::parameters::compact_public_key_only::CompactPublicKeyEncryptionParameters; - #[cfg(not(feature = "gpu"))] - use tfhe::shortint::parameters::current_params::V1_2_PARAM_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128; use tfhe::shortint::parameters::list_compression::CompressionParameters; - #[cfg(feature = "gpu")] - use tfhe::shortint::parameters::PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS; - use tfhe::shortint::parameters::{ - ShortintKeySwitchingParameters, PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128, - }; + use tfhe::shortint::parameters::ShortintKeySwitchingParameters; use tfhe::shortint::{ - CarryModulus, ClassicPBSParameters, MessageModulus, MultiBitPBSParameters, PBSParameters, + AtomicPatternParameters, ClassicPBSParameters, MultiBitPBSParameters, PBSParameters, ShortintParameterSet, }; - /// An iterator that yields a succession of combinations - /// of parameters and a num_block to achieve a certain bit_size ciphertext - /// in radix decomposition - pub struct ParamsAndNumBlocksIter { - params_and_bit_sizes: itertools::Product< - IntoIter, - IntoIter, - >, - } - - impl Default for ParamsAndNumBlocksIter { - fn default() -> Self { - let env_config = EnvConfig::new(); - - if env_config.is_multi_bit { - #[cfg(feature = "gpu")] - let params = vec![PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS.into()]; - #[cfg(not(feature = "gpu"))] - let params = vec![ - V1_2_PARAM_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128.into(), - ]; - - let params_and_bit_sizes = iproduct!(params, env_config.bit_sizes()); - Self { - params_and_bit_sizes, - } - } else { - // FIXME One set of parameter is tested since we want to benchmark only quickest - // operations. - let params = vec![PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128.into()]; - - let params_and_bit_sizes = iproduct!(params, env_config.bit_sizes()); - Self { - params_and_bit_sizes, - } - } - } - } - - impl Iterator for ParamsAndNumBlocksIter { - type Item = ( - tfhe::shortint::atomic_pattern::AtomicPatternParameters, - usize, - usize, - ); - - fn next(&mut self) -> Option { - let (param, bit_size) = self.params_and_bit_sizes.next()?; - let num_block = - (bit_size as f64 / (param.message_modulus().0 as f64).log(2.0)).ceil() as usize; - - Some((param, num_block, bit_size)) - } - } - impl From for CryptoParametersRecord { fn from(params: PBSParameters) -> Self { AtomicPatternParameters::from(params).into() @@ -203,181 +138,6 @@ pub mod shortint_utils { } } } - - // This array has been built according to performance benchmarks measuring latency over a - // matrix of 4 parameters set, 3 grouping factor and a wide range of threads values. - // The values available here as u64 are the optimal number of threads to use for a given triplet - // representing one or more parameters set. - const MULTI_BIT_THREADS_ARRAY: [((MessageModulus, CarryModulus, LweBskGroupingFactor), u64); - 12] = [ - ( - (MessageModulus(2), CarryModulus(2), LweBskGroupingFactor(2)), - 5, - ), - ( - (MessageModulus(4), CarryModulus(4), LweBskGroupingFactor(2)), - 5, - ), - ( - (MessageModulus(8), CarryModulus(8), LweBskGroupingFactor(2)), - 5, - ), - ( - ( - MessageModulus(16), - CarryModulus(16), - LweBskGroupingFactor(2), - ), - 5, - ), - ( - (MessageModulus(2), CarryModulus(2), LweBskGroupingFactor(3)), - 7, - ), - ( - (MessageModulus(4), CarryModulus(4), LweBskGroupingFactor(3)), - 9, - ), - ( - (MessageModulus(8), CarryModulus(8), LweBskGroupingFactor(3)), - 10, - ), - ( - ( - MessageModulus(16), - CarryModulus(16), - LweBskGroupingFactor(3), - ), - 10, - ), - ( - (MessageModulus(2), CarryModulus(2), LweBskGroupingFactor(4)), - 11, - ), - ( - (MessageModulus(4), CarryModulus(4), LweBskGroupingFactor(4)), - 13, - ), - ( - (MessageModulus(8), CarryModulus(8), LweBskGroupingFactor(4)), - 11, - ), - ( - ( - MessageModulus(16), - CarryModulus(16), - LweBskGroupingFactor(4), - ), - 11, - ), - ]; - - /// Define the number of threads to use for parameters doing multithreaded programmable - /// bootstrapping. - /// - /// Parameters must have the same values between message and carry modulus. - /// Grouping factor 2, 3 and 4 are the only ones that are supported. - #[allow(dead_code)] - pub fn multi_bit_num_threads( - message_modulus: u64, - carry_modulus: u64, - grouping_factor: usize, - ) -> Option { - // TODO Implement an interpolation mechanism for X_Y parameters set - if message_modulus != carry_modulus || ![2, 3, 4].contains(&(grouping_factor as i32)) { - return None; - } - let thread_map: HashMap<(MessageModulus, CarryModulus, LweBskGroupingFactor), u64> = - HashMap::from_iter(MULTI_BIT_THREADS_ARRAY); - thread_map - .get(&( - MessageModulus(message_modulus), - CarryModulus(carry_modulus), - LweBskGroupingFactor(grouping_factor), - )) - .copied() - } - - #[allow(dead_code)] - pub static PARAMETERS_SET: OnceLock = OnceLock::new(); - - pub enum ParametersSet { - Default, - All, - } - - #[allow(dead_code)] - impl ParametersSet { - pub fn from_env() -> Result { - let raw_value = env::var("__TFHE_RS_PARAMS_SET").unwrap_or("default".to_string()); - match raw_value.to_lowercase().as_str() { - "default" => Ok(ParametersSet::Default), - "all" => Ok(ParametersSet::All), - _ => Err(format!("parameters set '{raw_value}' is not supported")), - } - } - } - - #[allow(dead_code)] - pub fn init_parameters_set() { - PARAMETERS_SET.get_or_init(|| ParametersSet::from_env().unwrap()); - } - - #[allow(dead_code)] - #[derive(Clone, Copy, Debug)] - pub enum DesiredNoiseDistribution { - Gaussian, - TUniform, - Both, - } - - #[allow(dead_code)] - #[derive(Clone, Copy, Debug)] - pub enum DesiredBackend { - Cpu, - Gpu, - } - - #[allow(dead_code)] - impl DesiredBackend { - fn matches_parameter_name_backend(&self, param_name: &str) -> bool { - matches!( - (self, param_name.to_lowercase().contains("gpu")), - (DesiredBackend::Cpu, false) | (DesiredBackend::Gpu, true) - ) - } - } - - #[allow(dead_code)] - pub fn filter_parameters<'a, P: Copy + Into>( - params: &[(&'a P, &'a str)], - desired_noise_distribution: DesiredNoiseDistribution, - desired_backend: DesiredBackend, - ) -> Vec<(&'a P, &'a str)> { - params - .iter() - .filter_map(|(p, name)| { - let temp_param: PBSParameters = (**p).into(); - - match ( - temp_param.lwe_noise_distribution(), - desired_noise_distribution, - ) { - // If it's one of the pairs, we continue the process. - (DynamicDistribution::Gaussian(_), DesiredNoiseDistribution::Gaussian) - | (DynamicDistribution::TUniform(_), DesiredNoiseDistribution::TUniform) - | (_, DesiredNoiseDistribution::Both) => (), - _ => return None, - } - - if !desired_backend.matches_parameter_name_backend(name) { - return None; - }; - - Some((*p, *name)) - }) - .collect() - } } #[allow(unused_imports)] @@ -562,7 +322,6 @@ pub struct EnvConfig { } impl EnvConfig { - #[allow(dead_code)] pub fn new() -> Self { let is_multi_bit = match env::var("__TFHE_RS_PARAM_TYPE") { Ok(val) => val.to_lowercase() == "multi_bit", @@ -581,7 +340,6 @@ impl EnvConfig { } /// Get precisions values to benchmark. - #[allow(dead_code)] pub fn bit_sizes(&self) -> Vec { if self.is_fast_bench { FAST_BENCH_BIT_SIZES.to_vec() @@ -597,16 +355,13 @@ impl EnvConfig { } } -#[allow(dead_code)] pub static BENCH_TYPE: OnceLock = OnceLock::new(); -#[allow(dead_code)] pub enum BenchmarkType { Latency, Throughput, } -#[allow(dead_code)] impl BenchmarkType { pub fn from_env() -> Result { let raw_value = env::var("__TFHE_RS_BENCH_TYPE").unwrap_or("latency".to_string()); @@ -618,23 +373,21 @@ impl BenchmarkType { } } -#[allow(dead_code)] pub fn get_bench_type() -> &'static BenchmarkType { BENCH_TYPE.get_or_init(|| BenchmarkType::from_env().unwrap()) } /// Number of streaming multiprocessors (SM) available on Nvidia H100 GPU -#[allow(dead_code)] -const H100_PCIE_SM_COUNT: u32 = 132; +#[cfg(feature = "gpu")] +const H100_PCIE_SM_COUNT: u32 = 114; /// Generate a number of threads to use to saturate current machine for throughput measurements. -#[allow(dead_code)] pub fn throughput_num_threads(num_block: usize, op_pbs_count: u64) -> u64 { let ref_block_count = 32; // Represent a ciphertext of 64 bits for 2_2 parameters set let block_multiplicator = (ref_block_count as f64 / num_block as f64).ceil().min(1.0); // Some operations with a high serial workload (e.g. division) would yield an operation // loading value so low that the number of elements in the end wouldn't be meaningful. - let minimum_loading = if num_block < 64 { 0.2 } else { 0.1 }; + let minimum_loading = if num_block < 64 { 0.2 } else { 0.01 }; #[cfg(feature = "gpu")] { @@ -664,18 +417,16 @@ mod cuda_utils { use tfhe::core_crypto::gpu::lwe_keyswitch_key::CudaLweKeyswitchKey; use tfhe::core_crypto::gpu::lwe_multi_bit_bootstrap_key::CudaLweMultiBitBootstrapKey; use tfhe::core_crypto::gpu::lwe_packing_keyswitch_key::CudaLwePackingKeyswitchKey; - use tfhe::core_crypto::gpu::vec::{CudaVec, GpuIndex}; + use tfhe::core_crypto::gpu::vec::CudaVec; use tfhe::core_crypto::gpu::{get_number_of_gpus, CudaStreams}; use tfhe::core_crypto::prelude::{Numeric, UnsignedInteger}; use tfhe::shortint::server_key::ModulusSwitchNoiseReductionKey; - use tfhe::{set_server_key, ClientKey, CompressedServerKey}; + use tfhe::{set_server_key, ClientKey, CompressedServerKey, GpuIndex}; - #[allow(dead_code)] pub const GPU_MAX_SUPPORTED_POLYNOMIAL_SIZE: usize = 16384; /// Get vector of CUDA streams that can be directly used for throughput benchmarks in /// core_crypto layer. - #[allow(dead_code)] pub fn cuda_local_streams_core() -> Vec { (0..get_number_of_gpus()) .map(|i| CudaStreams::new_single_gpu(GpuIndex::new(i))) @@ -683,7 +434,6 @@ mod cuda_utils { } /// Computing keys in their CPU flavor. - #[allow(dead_code)] pub struct CpuKeys { ksk: Option>, pksk: Option>, @@ -691,14 +441,12 @@ mod cuda_utils { multi_bit_bsk: Option>, } - #[allow(dead_code)] impl CpuKeys { pub fn builder() -> CpuKeysBuilder { CpuKeysBuilder::new() } } - #[allow(dead_code)] pub struct CpuKeysBuilder { ksk: Option>, pksk: Option>, @@ -706,7 +454,6 @@ mod cuda_utils { multi_bit_bsk: Option>, } - #[allow(dead_code)] impl CpuKeysBuilder { pub fn new() -> CpuKeysBuilder { Self { @@ -793,7 +540,6 @@ mod cuda_utils { } /// Instantiate Cuda computing keys to each available GPU. - #[allow(dead_code)] pub fn cuda_local_keys_core( cpu_keys: &CpuKeys, ms_noise_reduction_key: Option<&ModulusSwitchNoiseReductionKey>, @@ -811,14 +557,12 @@ mod cuda_utils { gpu_keys_vec } - #[allow(dead_code)] pub struct CudaIndexes { pub d_input: CudaVec, pub d_output: CudaVec, pub d_lut: CudaVec, } - #[allow(dead_code)] impl CudaIndexes { pub fn new(indexes: &[T], stream: &CudaStreams, stream_index: u32) -> Self { let length = indexes.len(); @@ -848,7 +592,6 @@ mod cuda_utils { use tfhe::GpuIndex; /// Get number of streams usable for CUDA throughput benchmarks - #[allow(dead_code)] fn cuda_num_streams(num_block: usize) -> u64 { let num_streams_per_gpu: u32 = match num_block { 2 => 64, @@ -864,7 +607,6 @@ mod cuda_utils { } /// Get vector of CUDA streams that can be directly used for throughput benchmarks. - #[allow(dead_code)] pub fn cuda_local_streams( num_block: usize, throughput_elements: usize, @@ -881,7 +623,6 @@ mod cuda_utils { } /// Instantiate Cuda server key to each available GPU. - #[allow(dead_code)] pub fn cuda_local_keys(cks: &ClientKey) -> Vec { let gpu_count = get_number_of_gpus() as usize; let mut gpu_sks_vec = Vec::with_capacity(gpu_count); @@ -905,10 +646,5 @@ mod cuda_utils { pub use cuda_integer_utils::*; } -#[allow(unused_imports)] #[cfg(feature = "gpu")] pub use cuda_utils::*; - -// Empty main to please clippy. -#[allow(dead_code)] -pub fn main() {} diff --git a/tfhe/Cargo.toml b/tfhe/Cargo.toml index a58e3a67a..4fa7172d5 100644 --- a/tfhe/Cargo.toml +++ b/tfhe/Cargo.toml @@ -149,156 +149,13 @@ features = [ ] rustdoc-args = ["--html-in-header", "katex-header.html"] -########### -# # -# Benches # -# # -########### - -[[bench]] -name = "ks-bench" -path = "benches/core_crypto/ks_bench.rs" -harness = false -required-features = ["shortint", "internal-keycache"] - -[[bench]] -name = "pbs-bench" -path = "benches/core_crypto/pbs_bench.rs" -harness = false -required-features = ["boolean", "shortint", "internal-keycache"] - -[[bench]] -name = "ks-pbs-bench" -path = "benches/core_crypto/ks_pbs_bench.rs" -harness = false -required-features = ["shortint", "internal-keycache"] - -[[bench]] -name = "dev-bench" -path = "benches/core_crypto/dev_bench.rs" -harness = false -required-features = ["internal-keycache"] - -[[bench]] -name = "modulus_switch_noise_reduction" -path = "benches/core_crypto/modulus_switch_noise_reduction.rs" -harness = false -required-features = ["shortint"] - - -[[bench]] -name = "pbs128-bench" -path = "benches/core_crypto/pbs128_bench.rs" -harness = false -required-features = ["shortint"] - -[[bench]] -name = "boolean-bench" -path = "benches/boolean/bench.rs" -harness = false -required-features = ["boolean", "internal-keycache"] - -[[bench]] -name = "shortint-bench" -path = "benches/shortint/bench.rs" -harness = false -required-features = ["shortint", "internal-keycache"] - -[[bench]] -name = "oprf-shortint-bench" -path = "benches/shortint/oprf.rs" -harness = false -required-features = ["shortint", "internal-keycache"] - - -[[bench]] -name = "glwe_packing_compression-shortint-bench" -path = "benches/shortint/glwe_packing_compression.rs" -harness = false -required-features = ["shortint", "internal-keycache"] - -[[bench]] -name = "glwe_packing_compression-integer-bench" -path = "benches/integer/glwe_packing_compression.rs" -harness = false -required-features = ["integer", "pbs-stats", "internal-keycache"] - - -[[bench]] -name = "integer-bench" -path = "benches/integer/bench.rs" -harness = false -required-features = ["integer", "pbs-stats", "internal-keycache"] - -[[bench]] -name = "integer-signed-bench" -path = "benches/integer/signed_bench.rs" -harness = false -required-features = ["integer", "pbs-stats", "internal-keycache"] - -[[bench]] -name = "zk-pke-bench" -path = "benches/integer/zk_pke.rs" -harness = false -required-features = ["integer", "zk-pok", "pbs-stats", "internal-keycache"] - -[[bench]] -name = "hlapi" -path = "benches/high_level_api/bench.rs" -harness = false -required-features = ["integer", "internal-keycache"] - -[[bench]] -name = "hlapi-erc20" -path = "benches/high_level_api/erc20.rs" -harness = false -required-features = ["integer", "internal-keycache"] - -[[bench]] -name = "hlapi-dex" -path = "benches/high_level_api/dex.rs" -harness = false -required-features = ["integer", "internal-keycache"] - -[[bench]] -name = "keygen" -path = "benches/keygen/bench.rs" -harness = false -required-features = ["shortint", "internal-keycache"] - -[[bench]] -name = "utilities" -path = "benches/utilities.rs" -harness = false -required-features = ["boolean", "shortint", "integer", "internal-keycache"] - # Examples used as tools -[[example]] -name = "wasm_benchmarks_parser" -path = "examples/utilities/wasm_benchmarks_parser.rs" -required-features = ["shortint", "internal-keycache"] - [[example]] name = "generates_test_keys" path = "examples/utilities/generates_test_keys.rs" required-features = ["boolean", "shortint", "internal-keycache"] -[[example]] -name = "boolean_key_sizes" -path = "examples/utilities/boolean_key_sizes.rs" -required-features = ["boolean", "internal-keycache"] - -[[example]] -name = "shortint_key_sizes" -path = "examples/utilities/shortint_key_sizes.rs" -required-features = ["shortint", "internal-keycache"] - -[[example]] -name = "hlapi_compact_pk_ct_sizes" -path = "examples/utilities/hlapi_compact_pk_ct_sizes.rs" -required-features = ["integer", "internal-keycache"] - [[example]] name = "micro_bench_and" path = "examples/utilities/micro_bench_and.rs" diff --git a/tfhe/benches/core_crypto/dev_bench.rs b/tfhe/benches/core_crypto/dev_bench.rs deleted file mode 100644 index d5296f4ff..000000000 --- a/tfhe/benches/core_crypto/dev_bench.rs +++ /dev/null @@ -1,347 +0,0 @@ -use criterion::{black_box, criterion_group, criterion_main, Criterion}; -use tfhe::core_crypto::prelude::*; - -criterion_group!( - boolean_like_pbs_group, - multi_bit_pbs::, - pbs::, - mem_optimized_pbs:: -); - -criterion_group!( - shortint_like_pbs_group, - multi_bit_pbs::, - pbs::, - mem_optimized_pbs:: -); - -criterion_main!(boolean_like_pbs_group, shortint_like_pbs_group); - -fn get_bench_params() -> ( - LweDimension, - StandardDev, - DecompositionBaseLog, - DecompositionLevelCount, - GlweDimension, - PolynomialSize, - LweBskGroupingFactor, - ThreadCount, -) { - if Scalar::BITS == 64 { - ( - LweDimension(742), - StandardDev(0.000007069849454709433), - DecompositionBaseLog(3), - DecompositionLevelCount(5), - GlweDimension(1), - PolynomialSize(1024), - LweBskGroupingFactor(2), - ThreadCount(5), - ) - } else if Scalar::BITS == 32 { - ( - LweDimension(778), - StandardDev(0.000003725679281679651), - DecompositionBaseLog(18), - DecompositionLevelCount(1), - GlweDimension(3), - PolynomialSize(512), - LweBskGroupingFactor(2), - ThreadCount(5), - ) - } else { - unreachable!() - } -} - -fn multi_bit_pbs + CastFrom + Sync>( - c: &mut Criterion, -) { - // DISCLAIMER: these toy example parameters are not guaranteed to be secure or yield correct - // computations - // Define parameters for LweBootstrapKey creation - - let ( - mut input_lwe_dimension, - lwe_std_dev, - decomp_base_log, - decomp_level_count, - glwe_dimension, - polynomial_size, - grouping_factor, - thread_count, - ) = get_bench_params::(); - - let lwe_noise_distribution = Gaussian { - std: lwe_std_dev.0, - mean: 0.0, - }; - - let ciphertext_modulus = CiphertextModulus::new_native(); - - while input_lwe_dimension.0 % grouping_factor.0 != 0 { - input_lwe_dimension = LweDimension(input_lwe_dimension.0 + 1); - } - - // Create the PRNG - let mut seeder = new_seeder(); - let seeder = seeder.as_mut(); - let mut encryption_generator = - EncryptionRandomGenerator::::new(seeder.seed(), seeder); - let mut secret_generator = SecretRandomGenerator::::new(seeder.seed()); - - // Create the LweSecretKey - let input_lwe_secret_key = - allocate_and_generate_new_binary_lwe_secret_key(input_lwe_dimension, &mut secret_generator); - let output_glwe_secret_key: GlweSecretKeyOwned = - allocate_and_generate_new_binary_glwe_secret_key( - glwe_dimension, - polynomial_size, - &mut secret_generator, - ); - let output_lwe_secret_key = output_glwe_secret_key.into_lwe_secret_key(); - - let multi_bit_bsk = FourierLweMultiBitBootstrapKey::new( - input_lwe_dimension, - glwe_dimension.to_glwe_size(), - polynomial_size, - decomp_base_log, - decomp_level_count, - grouping_factor, - ); - - // Allocate a new LweCiphertext and encrypt our plaintext - let lwe_ciphertext_in = allocate_and_encrypt_new_lwe_ciphertext( - &input_lwe_secret_key, - Plaintext(Scalar::ZERO), - lwe_noise_distribution, - ciphertext_modulus, - &mut encryption_generator, - ); - - let accumulator = GlweCiphertext::new( - Scalar::ZERO, - glwe_dimension.to_glwe_size(), - polynomial_size, - ciphertext_modulus, - ); - - // Allocate the LweCiphertext to store the result of the PBS - let mut out_pbs_ct = LweCiphertext::new( - Scalar::ZERO, - output_lwe_secret_key.lwe_dimension().to_lwe_size(), - ciphertext_modulus, - ); - - let id = format!("Multi Bit PBS {}", Scalar::BITS); - - { - c.bench_function(&id, |b| { - b.iter(|| { - multi_bit_programmable_bootstrap_lwe_ciphertext( - &lwe_ciphertext_in, - &mut out_pbs_ct, - &accumulator.as_view(), - &multi_bit_bsk, - thread_count, - false, - ); - black_box(&mut out_pbs_ct); - }) - }); - } -} - -fn pbs>(c: &mut Criterion) { - // DISCLAIMER: these toy example parameters are not guaranteed to be secure or yield correct - // computations - // Define parameters for LweBootstrapKey creation - - let ( - input_lwe_dimension, - lwe_std_dev, - decomp_base_log, - decomp_level_count, - glwe_dimension, - polynomial_size, - _, - _, - ) = get_bench_params::(); - - let lwe_noise_distribution = Gaussian { - std: lwe_std_dev.0, - mean: 0.0, - }; - - let ciphertext_modulus = CiphertextModulus::new_native(); - - // Create the PRNG - let mut seeder = new_seeder(); - let seeder = seeder.as_mut(); - let mut encryption_generator = - EncryptionRandomGenerator::::new(seeder.seed(), seeder); - let mut secret_generator = SecretRandomGenerator::::new(seeder.seed()); - - // Create the LweSecretKey - let input_lwe_secret_key = - allocate_and_generate_new_binary_lwe_secret_key(input_lwe_dimension, &mut secret_generator); - let output_glwe_secret_key: GlweSecretKeyOwned = - allocate_and_generate_new_binary_glwe_secret_key( - glwe_dimension, - polynomial_size, - &mut secret_generator, - ); - let output_lwe_secret_key = output_glwe_secret_key.into_lwe_secret_key(); - - // Create the empty bootstrapping key in the Fourier domain - let fourier_bsk = FourierLweBootstrapKey::new( - input_lwe_dimension, - glwe_dimension.to_glwe_size(), - polynomial_size, - decomp_base_log, - decomp_level_count, - ); - - // Allocate a new LweCiphertext and encrypt our plaintext - let lwe_ciphertext_in = allocate_and_encrypt_new_lwe_ciphertext( - &input_lwe_secret_key, - Plaintext(Scalar::ZERO), - lwe_noise_distribution, - ciphertext_modulus, - &mut encryption_generator, - ); - - let accumulator = GlweCiphertext::new( - Scalar::ZERO, - glwe_dimension.to_glwe_size(), - polynomial_size, - ciphertext_modulus, - ); - - // Allocate the LweCiphertext to store the result of the PBS - let mut out_pbs_ct = LweCiphertext::new( - Scalar::ZERO, - output_lwe_secret_key.lwe_dimension().to_lwe_size(), - ciphertext_modulus, - ); - - let id = format!("PBS {}", Scalar::BITS); - { - c.bench_function(&id, |b| { - b.iter(|| { - programmable_bootstrap_lwe_ciphertext( - &lwe_ciphertext_in, - &mut out_pbs_ct, - &accumulator.as_view(), - &fourier_bsk, - ); - black_box(&mut out_pbs_ct); - }) - }); - } -} - -fn mem_optimized_pbs>(c: &mut Criterion) { - // DISCLAIMER: these toy example parameters are not guaranteed to be secure or yield correct - // computations - // Define parameters for LweBootstrapKey creation - - let ( - input_lwe_dimension, - lwe_std_dev, - decomp_base_log, - decomp_level_count, - glwe_dimension, - polynomial_size, - _, - _, - ) = get_bench_params::(); - - let lwe_noise_distribution = Gaussian { - std: lwe_std_dev.0, - mean: 0.0, - }; - - let ciphertext_modulus = CiphertextModulus::new_native(); - - // Create the PRNG - let mut seeder = new_seeder(); - let seeder = seeder.as_mut(); - let mut encryption_generator = - EncryptionRandomGenerator::::new(seeder.seed(), seeder); - let mut secret_generator = SecretRandomGenerator::::new(seeder.seed()); - - // Create the LweSecretKey - let input_lwe_secret_key = - allocate_and_generate_new_binary_lwe_secret_key(input_lwe_dimension, &mut secret_generator); - let output_glwe_secret_key: GlweSecretKeyOwned = - allocate_and_generate_new_binary_glwe_secret_key( - glwe_dimension, - polynomial_size, - &mut secret_generator, - ); - let output_lwe_secret_key = output_glwe_secret_key.into_lwe_secret_key(); - - // Create the empty bootstrapping key in the Fourier domain - let fourier_bsk = FourierLweBootstrapKey::new( - input_lwe_dimension, - glwe_dimension.to_glwe_size(), - polynomial_size, - decomp_base_log, - decomp_level_count, - ); - - // Allocate a new LweCiphertext and encrypt our plaintext - let lwe_ciphertext_in = allocate_and_encrypt_new_lwe_ciphertext( - &input_lwe_secret_key, - Plaintext(Scalar::ZERO), - lwe_noise_distribution, - ciphertext_modulus, - &mut encryption_generator, - ); - - let accumulator = GlweCiphertext::new( - Scalar::ZERO, - glwe_dimension.to_glwe_size(), - polynomial_size, - ciphertext_modulus, - ); - // Allocate the LweCiphertext to store the result of the PBS - let mut out_pbs_ct = LweCiphertext::new( - Scalar::ZERO, - output_lwe_secret_key.lwe_dimension().to_lwe_size(), - ciphertext_modulus, - ); - - let mut buffers = ComputationBuffers::new(); - - let fft = Fft::new(fourier_bsk.polynomial_size()); - let fft = fft.as_view(); - - buffers.resize( - programmable_bootstrap_lwe_ciphertext_mem_optimized_requirement::( - fourier_bsk.glwe_size(), - fourier_bsk.polynomial_size(), - fft, - ) - .unwrap() - .unaligned_bytes_required(), - ); - - let id = format!("PBS mem-optimized {}", Scalar::BITS); - { - c.bench_function(&id, |b| { - b.iter(|| { - programmable_bootstrap_lwe_ciphertext_mem_optimized( - &lwe_ciphertext_in, - &mut out_pbs_ct, - &accumulator.as_view(), - &fourier_bsk, - fft, - buffers.stack(), - ); - black_box(&mut out_pbs_ct); - }) - }); - } -} diff --git a/tfhe/benches/keygen/bench.rs b/tfhe/benches/keygen/bench.rs deleted file mode 100644 index bc22c229c..000000000 --- a/tfhe/benches/keygen/bench.rs +++ /dev/null @@ -1,45 +0,0 @@ -use criterion::*; -use tfhe::core_crypto::commons::generators::DeterministicSeeder; -use tfhe::core_crypto::prelude::{ - allocate_and_generate_new_binary_glwe_secret_key, - par_allocate_and_generate_new_lwe_bootstrap_key, CiphertextModulus, DefaultRandomGenerator, - EncryptionRandomGenerator, SecretRandomGenerator, -}; -use tfhe::core_crypto::seeders::new_seeder; -use tfhe::shortint::prelude::*; -use tfhe_csprng::seeders::Seeder; - -fn criterion_bench(c: &mut Criterion) { - let parameters = PARAM_MESSAGE_2_CARRY_2_KS_PBS; - let mut seeder = new_seeder(); - let mut deterministic_seeder = - DeterministicSeeder::::new(seeder.seed()); - let mut secret_generator = - SecretRandomGenerator::::new(deterministic_seeder.seed()); - let mut encryption_generator = EncryptionRandomGenerator::::new( - deterministic_seeder.seed(), - &mut deterministic_seeder, - ); - let glwe_secret_key = allocate_and_generate_new_binary_glwe_secret_key::( - parameters.glwe_dimension, - parameters.polynomial_size, - &mut secret_generator, - ); - let lwe_secret_key_after_ks = glwe_secret_key.clone().into_lwe_secret_key(); - c.bench_function("keygen", |b| { - b.iter(|| { - let _ = par_allocate_and_generate_new_lwe_bootstrap_key( - &lwe_secret_key_after_ks, - &glwe_secret_key, - parameters.pbs_base_log, - parameters.pbs_level, - parameters.glwe_noise_distribution, - CiphertextModulus::new_native(), - &mut encryption_generator, - ); - }); - }); -} - -criterion_group!(benches, criterion_bench); -criterion_main!(benches);