Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mz/bench integer compression #1410

Merged
merged 2 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion tfhe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@ 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", "internal-keycache"]


[[bench]]
name = "integer-bench"
Expand Down Expand Up @@ -329,5 +335,5 @@ crate-type = ["lib", "staticlib", "cdylib"]
unexpected_cfgs = { level = "warn", check-cfg = [
'cfg(bench)',
'cfg(tarpaulin)',
'cfg(tfhe_lints)'
'cfg(tfhe_lints)',
] }
81 changes: 81 additions & 0 deletions tfhe/benches/integer/glwe_packing_compression.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use criterion::{black_box, criterion_group, Criterion};
use tfhe::integer::ciphertext::CompressedCiphertextListBuilder;
use tfhe::integer::{ClientKey, RadixCiphertext};
use tfhe::shortint::parameters::list_compression::COMP_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;

fn glwe_packing(c: &mut Criterion) {
let param = PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;

let comp_param = COMP_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;

let bench_name = "integer_packing_compression";

let mut bench_group = c.benchmark_group(bench_name);

let cks = ClientKey::new(param);

let private_compression_key = cks.new_compression_private_key(comp_param);

let (compression_key, decompression_key) =
cks.new_compression_decompression_keys(&private_compression_key);

let log_message_modulus = param.message_modulus.0.ilog2() as usize;

for num_bits in [
8,
16,
32,
64,
128,
256,
256,
comp_param.lwe_per_glwe.0 * log_message_modulus,
] {
assert_eq!(num_bits % log_message_modulus, 0);
let num_blocks = num_bits / log_message_modulus;

let ct = cks.encrypt_radix(0_u32, num_blocks);

let mut builder = CompressedCiphertextListBuilder::new();

builder.push(ct);

bench_group.bench_function(format!("pack_u{num_bits}"), |b| {
b.iter(|| {
let compressed = builder.build(&compression_key);

_ = black_box(compressed);
})
});

let compressed = builder.build(&compression_key);

bench_group.bench_function(format!("unpack_u{num_bits}"), |b| {
b.iter(|| {
let unpacked: RadixCiphertext =
compressed.get(0, &decompression_key).unwrap().unwrap();

_ = black_box(unpacked);
})
});

bench_group.bench_function(format!("pack_unpack_u{num_bits}"), |b| {
b.iter(|| {
let compressed = builder.build(&compression_key);

let unpacked: RadixCiphertext =
compressed.get(0, &decompression_key).unwrap().unwrap();

_ = black_box(unpacked);
})
});
}
}

criterion_group!(glwe_packing2, glwe_packing);

fn main() {
glwe_packing2();
Criterion::default().configure_from_args().final_summary();
}
27 changes: 13 additions & 14 deletions tfhe/src/integer/ciphertext/compressed_ciphertext_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ impl Compressible for SignedRadixCiphertext {
}
}

#[derive(Clone)]
pub struct CompressedCiphertextListBuilder {
pub(crate) ciphertexts: Vec<Ciphertext>,
pub(crate) info: Vec<DataKind>,
Expand Down Expand Up @@ -150,25 +151,23 @@ impl CompressedCiphertextList {
#[cfg(test)]
mod tests {
use super::*;
use crate::integer::{RadixCiphertext, RadixClientKey};
use crate::integer::ClientKey;
use crate::shortint::parameters::list_compression::COMP_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;

#[test]
fn test_heterogeneous_ciphertext_compression_ci_run_filter() {
let num_blocks = 2;

let cks = RadixClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, num_blocks);
let cks = ClientKey::new(PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64);

let private_compression_key =
cks.new_compression_private_key(COMP_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64);

let (compression_key, decompression_key) =
cks.new_compression_decompression_keys(&private_compression_key);

let ct1 = cks.encrypt(3_u32);
let ct1 = cks.encrypt_radix(3_u32, 16);

let ct2 = cks.encrypt_signed(-2);
let ct2 = cks.encrypt_signed_radix(-2, 16);

let ct3 = cks.encrypt_bool(true);

Expand All @@ -178,20 +177,20 @@ mod tests {
.push(ct3)
.build(&compression_key);

let a = compressed.blocks_of(0, &decompression_key).unwrap();
let decompressed1 = compressed.get(0, &decompression_key).unwrap().unwrap();

let decrypted: u32 = cks.decrypt_radix(&decompressed1);

let decrypted: u32 = cks.decrypt(&RadixCiphertext::from_expanded_blocks(a.0, a.1).unwrap());
assert_eq!(decrypted, 3_u32);

let b = compressed.blocks_of(1, &decompression_key).unwrap();
let decompressed2 = compressed.get(1, &decompression_key).unwrap().unwrap();

let decrypted: i32 =
cks.decrypt_signed(&SignedRadixCiphertext::from_expanded_blocks(b.0, b.1).unwrap());
let decrypted2: i32 = cks.decrypt_signed_radix(&decompressed2);

assert_eq!(decrypted, -2);
assert_eq!(decrypted2, -2);

let c = compressed.blocks_of(2, &decompression_key).unwrap();
let decompressed3 = compressed.get(2, &decompression_key).unwrap().unwrap();

assert!(cks.decrypt_bool(&BooleanBlock::from_expanded_blocks(c.0, c.1).unwrap()));
assert!(cks.decrypt_bool(&decompressed3));
}
}
18 changes: 17 additions & 1 deletion tfhe/src/integer/client_key/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ use crate::integer::ciphertext::{CompressedCrtCiphertext, CrtCiphertext};
use crate::integer::client_key::utils::i_crt;
use crate::integer::encryption::{encrypt_crt, encrypt_words_radix_impl};
use crate::shortint::ciphertext::Degree;
use crate::shortint::parameters::MessageModulus;
use crate::shortint::list_compression::{CompressionKey, CompressionPrivateKeys, DecompressionKey};
use crate::shortint::parameters::{CompressionParameters, MessageModulus};
use crate::shortint::{
Ciphertext, ClientKey as ShortintClientKey, ShortintParameterSet as ShortintParameters,
};
Expand Down Expand Up @@ -714,4 +715,19 @@ impl ClientKey {
{
encrypt_crt(&self.key, message, base_vec, encrypt_block)
}

pub fn new_compression_private_key(
&self,
params: CompressionParameters,
) -> CompressionPrivateKeys {
self.key.new_compression_private_key(params)
}

pub fn new_compression_decompression_keys(
&self,
private_compression_key: &CompressionPrivateKeys,
) -> (CompressionKey, DecompressionKey) {
self.key
.new_compression_decompression_keys(private_compression_key)
}
}
18 changes: 0 additions & 18 deletions tfhe/src/integer/client_key/radix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use crate::integer::backward_compatibility::client_key::RadixClientKeyVersions;
use crate::integer::block_decomposition::{DecomposableInto, RecomposableFrom};
use crate::integer::ciphertext::{RadixCiphertext, SignedRadixCiphertext};
use crate::integer::BooleanBlock;
use crate::shortint::list_compression::{CompressionKey, CompressionPrivateKeys, DecompressionKey};
use crate::shortint::parameters::list_compression::CompressionParameters;
use crate::shortint::{Ciphertext as ShortintCiphertext, PBSParameters as ShortintParameters};
use serde::{Deserialize, Serialize};
use tfhe_versionable::Versionize;
Expand Down Expand Up @@ -133,22 +131,6 @@ impl RadixClientKey {
pub fn num_blocks(&self) -> usize {
self.num_blocks
}

pub fn new_compression_private_key(
&self,
params: CompressionParameters,
) -> CompressionPrivateKeys {
self.key.key.new_compression_private_key(params)
}

pub fn new_compression_decompression_keys(
&self,
private_compression_key: &CompressionPrivateKeys,
) -> (CompressionKey, DecompressionKey) {
self.key
.key
.new_compression_decompression_keys(private_compression_key)
}
}

impl From<(ClientKey, usize)> for RadixClientKey {
Expand Down
Loading