diff --git a/MODULE.bazel b/MODULE.bazel index f54f36d..2505093 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -23,33 +23,10 @@ llvm.configure( use_repo(llvm, hermetic_llvm = "llvm-project") # Macros for building with HEIR -bazel_dep(name = "rules_heir", version = "0.1.1") +bazel_dep(name = "rules_heir", version = "0.1.2") heir_repos = use_extension("@rules_heir//heir:extensions.bzl", "heir_repositories") -heir_repos.platform( - cpu = "x86_64", - heir_opt_sha256 = "919bca7b663e73ad4c550f0145cf3d66c70812d9d77adeabb23275f13874b0af", - heir_opt_url = "https://github.com/google/heir/releases/download/v2026.08.11.dev0/heir-opt-manylinux_2_28_x86_64", - heir_translate_sha256 = "5f184dcbe350ae0a2cfdfa3ca364a02cfa827bdebedab208ada3a529f8acab1d", - heir_translate_url = "https://github.com/google/heir/releases/download/v2026.08.11.dev0/heir-translate-manylinux_2_28_x86_64", - os = "linux", -) -heir_repos.platform( - cpu = "aarch64", - heir_opt_sha256 = "136d29bc519126ae1032789ffdccebd097b27bbcc54dbc02146d2e0ad1c4bcdf", - heir_opt_url = "https://github.com/google/heir/releases/download/v2026.08.11.dev0/heir-opt-manylinux_2_28_aarch64", - heir_translate_sha256 = "b71b17bf0548c48503d2c9ecc17e230f85bb679bb8d4fc62a7b4c05168e6d1f8", - heir_translate_url = "https://github.com/google/heir/releases/download/v2026.08.11.dev0/heir-translate-manylinux_2_28_aarch64", - os = "linux", -) -heir_repos.platform( - cpu = "arm64", - heir_opt_sha256 = "4a1490bcbb157d1437bc600c4a79790baa64069ee29fe0ba210401baa2de9543", - heir_opt_url = "https://github.com/google/heir/releases/download/v2026.08.11.dev0/heir-opt-macosx_11_0_arm64", - heir_translate_sha256 = "bc58ae375b9ead6b2259fcf6f3b1f43af49880f4019839db11ad6af199aaaaa2", - heir_translate_url = "https://github.com/google/heir/releases/download/v2026.08.11.dev0/heir-translate-macosx_11_0_arm64", - os = "macos", -) +heir_repos.config(nightly = True) use_repo( heir_repos, "heir_linux_aarch64", diff --git a/demos/README.md b/demos/README.md index 591bbd2..71356d0 100644 --- a/demos/README.md +++ b/demos/README.md @@ -4,6 +4,11 @@ This directory contains a set of demos using HEIR to compile pre-trained PyTorch models to FHE. All of the models are pre-compiled with scripts providing command to quickly run and assess model performance. +**Note:** Occasionally the HEIR developers make API-backwards incompatible +changes to the generated APIs, which will cause some of these examples +to fail to compile until the following day, when HEIR's nightly build +runs and produces a new binary. + ## MNIST The classic [MNIST](https://en.wikipedia.org/wiki/MNIST_database) digit diff --git a/demos/cc_fraud/debug/model_debug.mlir b/demos/cc_fraud/debug/model_debug.mlir index 0738b7c..9a9e05e 100644 --- a/demos/cc_fraud/debug/model_debug.mlir +++ b/demos/cc_fraud/debug/model_debug.mlir @@ -2,7 +2,7 @@ #map1 = affine_map<(d0, d1) -> (d1)> module { func.func @cc_fraud(%arg0: tensor<1x82xf32> {secret.secret}) -> tensor<1x2xf32> { - debug.validate %arg0 {name = "input", metadata = "input"} : tensor<1x82xf32> + debug.validate %arg0 : tensor<1x82xf32> %cst = arith.constant 0.000000e+00 : f32 %cst_0 = arith.constant 1.000000e+00 : f32 %cst_1 = arith.constant dense_resource : tensor<2xf32> @@ -16,13 +16,13 @@ module { %1 = tensor.empty() : tensor<1x128xf32> %2 = linalg.fill ins(%cst : f32) outs(%1 : tensor<1x128xf32>) -> tensor<1x128xf32> %3 = linalg.matmul ins(%arg0, %transposed : tensor<1x82xf32>, tensor<82x128xf32>) outs(%2 : tensor<1x128xf32>) -> tensor<1x128xf32> - debug.validate %3 {name = "layer1_matmul", metadata = "layer1_matmul"} : tensor<1x128xf32> + debug.validate %3 : tensor<1x128xf32> %4 = linalg.generic {indexing_maps = [#map, #map1, #map], iterator_types = ["parallel", "parallel"]} ins(%3, %cst_6 : tensor<1x128xf32>, tensor<128xf32>) outs(%1 : tensor<1x128xf32>) { ^bb0(%in: f32, %in_9: f32, %out: f32): %17 = arith.addf %in, %in_9 : f32 linalg.yield %17 : f32 } -> tensor<1x128xf32> - debug.validate %4 {name = "layer1_bias", metadata = "layer1_bias"} : tensor<1x128xf32> + debug.validate %4 : tensor<1x128xf32> %5 = linalg.generic {degree = 7 : i32, domain_lower = -10.000000e+00 : f64, domain_upper = 10.000000e+00 : f64, indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel"]} ins(%4 : tensor<1x128xf32>) outs(%1 : tensor<1x128xf32>) { ^bb0(%in: f32, %out: f32): %17 = arith.negf %in : f32 @@ -31,19 +31,19 @@ module { %20 = arith.divf %cst_0, %19 : f32 linalg.yield %20 : f32 } -> tensor<1x128xf32> - debug.validate %5 {name = "layer1_sigmoid", metadata = "layer1_sigmoid"} : tensor<1x128xf32> + debug.validate %5 : tensor<1x128xf32> %6 = tensor.empty() : tensor<128x64xf32> %transposed_7 = linalg.transpose ins(%cst_4 : tensor<64x128xf32>) outs(%6 : tensor<128x64xf32>) permutation = [1, 0] %7 = tensor.empty() : tensor<1x64xf32> %8 = linalg.fill ins(%cst : f32) outs(%7 : tensor<1x64xf32>) -> tensor<1x64xf32> %9 = linalg.matmul ins(%5, %transposed_7 : tensor<1x128xf32>, tensor<128x64xf32>) outs(%8 : tensor<1x64xf32>) -> tensor<1x64xf32> - debug.validate %9 {name = "layer2_matmul", metadata = "layer2_matmul"} : tensor<1x64xf32> + debug.validate %9 : tensor<1x64xf32> %10 = linalg.generic {indexing_maps = [#map, #map1, #map], iterator_types = ["parallel", "parallel"]} ins(%9, %cst_3 : tensor<1x64xf32>, tensor<64xf32>) outs(%7 : tensor<1x64xf32>) { ^bb0(%in: f32, %in_9: f32, %out: f32): %17 = arith.addf %in, %in_9 : f32 linalg.yield %17 : f32 } -> tensor<1x64xf32> - debug.validate %10 {name = "layer2_bias", metadata = "layer2_bias"} : tensor<1x64xf32> + debug.validate %10 : tensor<1x64xf32> %11 = linalg.generic {degree = 7 : i32, domain_lower = -10.000000e+00 : f64, domain_upper = 10.000000e+00 : f64, indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel"]} ins(%10 : tensor<1x64xf32>) outs(%7 : tensor<1x64xf32>) { ^bb0(%in: f32, %out: f32): %17 = arith.negf %in : f32 @@ -52,19 +52,19 @@ module { %20 = arith.divf %cst_0, %19 : f32 linalg.yield %20 : f32 } -> tensor<1x64xf32> - debug.validate %11 {name = "layer2_sigmoid", metadata = "layer2_sigmoid"} : tensor<1x64xf32> + debug.validate %11 : tensor<1x64xf32> %12 = tensor.empty() : tensor<64x2xf32> %transposed_8 = linalg.transpose ins(%cst_2 : tensor<2x64xf32>) outs(%12 : tensor<64x2xf32>) permutation = [1, 0] %13 = tensor.empty() : tensor<1x2xf32> %14 = linalg.fill ins(%cst : f32) outs(%13 : tensor<1x2xf32>) -> tensor<1x2xf32> %15 = linalg.matmul ins(%11, %transposed_8 : tensor<1x64xf32>, tensor<64x2xf32>) outs(%14 : tensor<1x2xf32>) -> tensor<1x2xf32> - debug.validate %15 {name = "layer3_matmul", metadata = "layer3_matmul"} : tensor<1x2xf32> + debug.validate %15 : tensor<1x2xf32> %16 = linalg.generic {indexing_maps = [#map, #map1, #map], iterator_types = ["parallel", "parallel"]} ins(%15, %cst_1 : tensor<1x2xf32>, tensor<2xf32>) outs(%13 : tensor<1x2xf32>) { ^bb0(%in: f32, %in_9: f32, %out: f32): %17 = arith.addf %in, %in_9 : f32 linalg.yield %17 : f32 } -> tensor<1x2xf32> - debug.validate %16 {name = "layer3_bias", metadata = "layer3_bias"} : tensor<1x2xf32> + debug.validate %16 : tensor<1x2xf32> return %16 : tensor<1x2xf32> } } diff --git a/demos/cc_fraud/debug/model_timing.mlir b/demos/cc_fraud/debug/model_timing.mlir index b75ec8f..52e6118 100644 --- a/demos/cc_fraud/debug/model_timing.mlir +++ b/demos/cc_fraud/debug/model_timing.mlir @@ -2,7 +2,7 @@ #map1 = affine_map<(d0, d1) -> (d1)> module { func.func @cc_fraud(%arg0: tensor<1x82xf32> {secret.secret}) -> tensor<1x2xf32> { - debug.validate %arg0 {name = "input", metadata = "input"} : tensor<1x82xf32> + debug.validate %arg0 : tensor<1x82xf32> %cst = arith.constant 0.000000e+00 : f32 %cst_0 = arith.constant 1.000000e+00 : f32 %cst_1 = arith.constant dense_resource : tensor<2xf32> @@ -16,13 +16,13 @@ module { %1 = tensor.empty() : tensor<1x128xf32> %2 = linalg.fill ins(%cst : f32) outs(%1 : tensor<1x128xf32>) -> tensor<1x128xf32> %3 = linalg.matmul ins(%arg0, %transposed : tensor<1x82xf32>, tensor<82x128xf32>) outs(%2 : tensor<1x128xf32>) -> tensor<1x128xf32> - debug.validate %3 {name = "layer1_matmul", metadata = "layer1_matmul"} : tensor<1x128xf32> + debug.validate %3 : tensor<1x128xf32> %4 = linalg.generic {indexing_maps = [#map, #map1, #map], iterator_types = ["parallel", "parallel"]} ins(%3, %cst_6 : tensor<1x128xf32>, tensor<128xf32>) outs(%1 : tensor<1x128xf32>) { ^bb0(%in: f32, %in_9: f32, %out: f32): %17 = arith.addf %in, %in_9 : f32 linalg.yield %17 : f32 } -> tensor<1x128xf32> - debug.validate %4 {name = "layer1_bias", metadata = "layer1_bias"} : tensor<1x128xf32> + debug.validate %4 : tensor<1x128xf32> %5 = linalg.generic {degree = 3 : i32, domain_lower = -10.000000e+00 : f64, domain_upper = 10.000000e+00 : f64, indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel"]} ins(%4 : tensor<1x128xf32>) outs(%1 : tensor<1x128xf32>) { ^bb0(%in: f32, %out: f32): %17 = arith.negf %in : f32 @@ -31,19 +31,19 @@ module { %20 = arith.divf %cst_0, %19 : f32 linalg.yield %20 : f32 } -> tensor<1x128xf32> - debug.validate %5 {name = "layer1_sigmoid", metadata = "layer1_sigmoid"} : tensor<1x128xf32> + debug.validate %5 : tensor<1x128xf32> %6 = tensor.empty() : tensor<128x64xf32> %transposed_7 = linalg.transpose ins(%cst_4 : tensor<64x128xf32>) outs(%6 : tensor<128x64xf32>) permutation = [1, 0] %7 = tensor.empty() : tensor<1x64xf32> %8 = linalg.fill ins(%cst : f32) outs(%7 : tensor<1x64xf32>) -> tensor<1x64xf32> %9 = linalg.matmul ins(%5, %transposed_7 : tensor<1x128xf32>, tensor<128x64xf32>) outs(%8 : tensor<1x64xf32>) -> tensor<1x64xf32> - debug.validate %9 {name = "layer2_matmul", metadata = "layer2_matmul"} : tensor<1x64xf32> + debug.validate %9 : tensor<1x64xf32> %10 = linalg.generic {indexing_maps = [#map, #map1, #map], iterator_types = ["parallel", "parallel"]} ins(%9, %cst_3 : tensor<1x64xf32>, tensor<64xf32>) outs(%7 : tensor<1x64xf32>) { ^bb0(%in: f32, %in_9: f32, %out: f32): %17 = arith.addf %in, %in_9 : f32 linalg.yield %17 : f32 } -> tensor<1x64xf32> - debug.validate %10 {name = "layer2_bias", metadata = "layer2_bias"} : tensor<1x64xf32> + debug.validate %10 : tensor<1x64xf32> %11 = linalg.generic {degree = 3 : i32, domain_lower = -10.000000e+00 : f64, domain_upper = 10.000000e+00 : f64, indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel"]} ins(%10 : tensor<1x64xf32>) outs(%7 : tensor<1x64xf32>) { ^bb0(%in: f32, %out: f32): %17 = arith.negf %in : f32 @@ -52,19 +52,19 @@ module { %20 = arith.divf %cst_0, %19 : f32 linalg.yield %20 : f32 } -> tensor<1x64xf32> - debug.validate %11 {name = "layer2_sigmoid", metadata = "layer2_sigmoid"} : tensor<1x64xf32> + debug.validate %11 : tensor<1x64xf32> %12 = tensor.empty() : tensor<64x2xf32> %transposed_8 = linalg.transpose ins(%cst_2 : tensor<2x64xf32>) outs(%12 : tensor<64x2xf32>) permutation = [1, 0] %13 = tensor.empty() : tensor<1x2xf32> %14 = linalg.fill ins(%cst : f32) outs(%13 : tensor<1x2xf32>) -> tensor<1x2xf32> %15 = linalg.matmul ins(%11, %transposed_8 : tensor<1x64xf32>, tensor<64x2xf32>) outs(%14 : tensor<1x2xf32>) -> tensor<1x2xf32> - debug.validate %15 {name = "layer3_matmul", metadata = "layer3_matmul"} : tensor<1x2xf32> + debug.validate %15 : tensor<1x2xf32> %16 = linalg.generic {indexing_maps = [#map, #map1, #map], iterator_types = ["parallel", "parallel"]} ins(%15, %cst_1 : tensor<1x2xf32>, tensor<2xf32>) outs(%13 : tensor<1x2xf32>) { ^bb0(%in: f32, %in_9: f32, %out: f32): %17 = arith.addf %in, %in_9 : f32 linalg.yield %17 : f32 } -> tensor<1x2xf32> - debug.validate %16 {name = "layer3_bias", metadata = "layer3_bias"} : tensor<1x2xf32> + debug.validate %16 : tensor<1x2xf32> return %16 : tensor<1x2xf32> } } diff --git a/demos/cc_fraud/lattigo/evaluate_fhe.go b/demos/cc_fraud/lattigo/evaluate_fhe.go index 1385815..35fd15d 100644 --- a/demos/cc_fraud/lattigo/evaluate_fhe.go +++ b/demos/cc_fraud/lattigo/evaluate_fhe.go @@ -55,11 +55,10 @@ func main() { // FHE evaluation fmt.Println("Running FHE evaluation (preprocessed)...") t0 = time.Now() - ctZero1 := fraud_model_lattigo.Cc_fraud__encrypt__zero__0(evaluator, params, ecd, encryptor) - ctZero2 := fraud_model_lattigo.Cc_fraud__encrypt__zero__1(evaluator, params, ecd, encryptor) + ctZeros := fraud_model_lattigo.Cc_fraud__encrypt__zeros(evaluator, params, ecd, encryptor) encryptedOutput := fraud_model_lattigo.Cc_fraud__preprocessed( evaluator, params, ecd, encryptedFeatures, - ctZero1, ctZero2, + ctZeros, preprocessedWeights, ) fmt.Printf(" Took %v\n", time.Since(t0)) diff --git a/demos/cc_fraud/lattigo/evaluate_fhe_debug.go b/demos/cc_fraud/lattigo/evaluate_fhe_debug.go index 476aced..7aaa4bc 100644 --- a/demos/cc_fraud/lattigo/evaluate_fhe_debug.go +++ b/demos/cc_fraud/lattigo/evaluate_fhe_debug.go @@ -59,11 +59,10 @@ func main() { // FHE evaluation (with debug callbacks) fmt.Println("\n--- Starting FHE Evaluation (with Debug Callbacks) ---") t0 = time.Now() - ctZero1 := fraud_model_lattigo_debug.Cc_fraud__encrypt__zero__0(evaluator, params, ecd, encryptor) - ctZero2 := fraud_model_lattigo_debug.Cc_fraud__encrypt__zero__1(evaluator, params, ecd, encryptor) + ctZeros := fraud_model_lattigo_debug.Cc_fraud__encrypt__zeros(evaluator, params, ecd, encryptor) encryptedOutput := fraud_model_lattigo_debug.Cc_fraud__preprocessed( evaluator, params, ecd, decryptor, encryptedFeatures, - ctZero1, ctZero2, + ctZeros, preprocessedWeights, ) fmt.Printf("--- FHE Evaluation Completed in %v ---\n\n", time.Since(t0)) diff --git a/demos/cc_fraud/lattigo/evaluate_fhe_suite.go b/demos/cc_fraud/lattigo/evaluate_fhe_suite.go index 4338c36..ad438ce 100644 --- a/demos/cc_fraud/lattigo/evaluate_fhe_suite.go +++ b/demos/cc_fraud/lattigo/evaluate_fhe_suite.go @@ -44,12 +44,10 @@ func main() { fmt.Println("Encrypting all input features and zero accumulators sequentially...") t0 = time.Now() encryptedInputs := make([][]*rlwe.Ciphertext, numRows) - ctZeros1 := make([]*rlwe.Ciphertext, numRows) - ctZeros2 := make([]*rlwe.Ciphertext, numRows) + ctZeros := make([][]*rlwe.Ciphertext, numRows) for i := 0; i < numRows; i++ { encryptedInputs[i] = fraud_model_lattigo.Cc_fraud__encrypt__arg0(evaluator, params, ecd, encryptor, allFeatures[i]) - ctZeros1[i] = fraud_model_lattigo.Cc_fraud__encrypt__zero__0(evaluator, params, ecd, encryptor) - ctZeros2[i] = fraud_model_lattigo.Cc_fraud__encrypt__zero__1(evaluator, params, ecd, encryptor) + ctZeros[i] = fraud_model_lattigo.Cc_fraud__encrypt__zeros(evaluator, params, ecd, encryptor) } fmt.Printf(" Took %v\n", time.Since(t0)) @@ -67,7 +65,7 @@ func main() { localEvaluator := evaluator.ShallowCopy() encryptedOutputs[idx] = fraud_model_lattigo.Cc_fraud__preprocessed( localEvaluator, params, ecd, encryptedInputs[idx], - ctZeros1[idx], ctZeros2[idx], + ctZeros[idx], preprocessedWeights, ) }(i) diff --git a/demos/cc_fraud/lattigo/evaluate_fhe_timing.go b/demos/cc_fraud/lattigo/evaluate_fhe_timing.go index 68d4eda..c54313d 100644 --- a/demos/cc_fraud/lattigo/evaluate_fhe_timing.go +++ b/demos/cc_fraud/lattigo/evaluate_fhe_timing.go @@ -55,11 +55,10 @@ func main() { // FHE evaluation (preprocessed with timing callbacks) fmt.Println("Running FHE evaluation (preprocessed with timing callbacks)...") t0 = time.Now() - ctZero1 := fraud_model_lattigo_timing.Cc_fraud__encrypt__zero__0(evaluator, params, ecd, encryptor) - ctZero2 := fraud_model_lattigo_timing.Cc_fraud__encrypt__zero__1(evaluator, params, ecd, encryptor) + ctZeros := fraud_model_lattigo_timing.Cc_fraud__encrypt__zeros(evaluator, params, ecd, encryptor) encryptedOutput := fraud_model_lattigo_timing.Cc_fraud__preprocessed( evaluator, params, ecd, decryptor, encryptedFeatures, - ctZero1, ctZero2, + ctZeros, preprocessedWeights, ) fmt.Printf(" Took %v\n", time.Since(t0)) diff --git a/demos/cc_fraud/openfhe/evaluate_fhe.py b/demos/cc_fraud/openfhe/evaluate_fhe.py index 1c8fa0b..03e66c3 100644 --- a/demos/cc_fraud/openfhe/evaluate_fhe.py +++ b/demos/cc_fraud/openfhe/evaluate_fhe.py @@ -74,13 +74,11 @@ def main(): # Call the FHE function print("Running FHE evaluation (preprocessed)...") t0 = time.time() - ct_zero_1 = fraud_model_pybind.cc_fraud__encrypt__zero__0(cc, public_key) - ct_zero_2 = fraud_model_pybind.cc_fraud__encrypt__zero__1(cc, public_key) + ct_zeros = fraud_model_pybind.cc_fraud__encrypt__zeros(cc, public_key) encrypted_output = fraud_model_pybind.cc_fraud__preprocessed( cc, encrypted_features, - ct_zero_1, - ct_zero_2, + ct_zeros, prep_struct, ) print(f" Took {time.time() - t0:.4f} seconds") diff --git a/demos/cc_fraud/openfhe/evaluate_fhe_debug.py b/demos/cc_fraud/openfhe/evaluate_fhe_debug.py index 13f3bd3..0482cd2 100644 --- a/demos/cc_fraud/openfhe/evaluate_fhe_debug.py +++ b/demos/cc_fraud/openfhe/evaluate_fhe_debug.py @@ -79,14 +79,12 @@ def main(): # for debug) print("\n--- Starting FHE Evaluation (with Debug Callbacks) ---") t0 = time.time() - ct_zero_1 = fraud_model_pybind.cc_fraud__encrypt__zero__0(cc, public_key) - ct_zero_2 = fraud_model_pybind.cc_fraud__encrypt__zero__1(cc, public_key) + ct_zeros = fraud_model_pybind.cc_fraud__encrypt__zeros(cc, public_key) encrypted_output = fraud_model_pybind.cc_fraud__preprocessed( cc, secret_key, encrypted_features, - ct_zero_1, - ct_zero_2, + ct_zeros, prep_struct, ) print(f"--- FHE Evaluation Completed in {time.time() - t0:.4f} seconds ---\n") diff --git a/demos/cc_fraud/openfhe/evaluate_fhe_suite.py b/demos/cc_fraud/openfhe/evaluate_fhe_suite.py index 7066047..5ac94c5 100644 --- a/demos/cc_fraud/openfhe/evaluate_fhe_suite.py +++ b/demos/cc_fraud/openfhe/evaluate_fhe_suite.py @@ -74,15 +74,13 @@ def main(): encrypted_features = fraud_model_pybind.cc_fraud__encrypt__arg0( cc, features, public_key ) - ct_zero_1 = fraud_model_pybind.cc_fraud__encrypt__zero__0(cc, public_key) - ct_zero_2 = fraud_model_pybind.cc_fraud__encrypt__zero__1(cc, public_key) + ct_zeros = fraud_model_pybind.cc_fraud__encrypt__zeros(cc, public_key) # Call the FHE function (using preprocessed weights) encrypted_output = fraud_model_pybind.cc_fraud__preprocessed( cc, encrypted_features, - ct_zero_1, - ct_zero_2, + ct_zeros, prep_struct, ) diff --git a/demos/cc_fraud/openfhe/evaluate_fhe_timing.py b/demos/cc_fraud/openfhe/evaluate_fhe_timing.py index a2671fd..65e23ba 100644 --- a/demos/cc_fraud/openfhe/evaluate_fhe_timing.py +++ b/demos/cc_fraud/openfhe/evaluate_fhe_timing.py @@ -72,18 +72,12 @@ def main(): print("Running FHE evaluation (preprocessed with timing callbacks)...") t0 = time.time() - ct_zero_1 = fraud_model_timing_pybind.cc_fraud__encrypt__zero__0( - cc, public_key - ) - ct_zero_2 = fraud_model_timing_pybind.cc_fraud__encrypt__zero__1( - cc, public_key - ) + ct_zeros = fraud_model_timing_pybind.cc_fraud__encrypt__zeros(cc, public_key) encrypted_output = fraud_model_timing_pybind.cc_fraud__preprocessed( cc, secret_key, encrypted_features, - ct_zero_1, - ct_zero_2, + ct_zeros, prep_struct, ) print(f" Took {time.time() - t0:.4f} seconds") diff --git a/demos/criteo/lattigo/evaluate_fhe.go b/demos/criteo/lattigo/evaluate_fhe.go index f6ce7af..0ba9be9 100644 --- a/demos/criteo/lattigo/evaluate_fhe.go +++ b/demos/criteo/lattigo/evaluate_fhe.go @@ -42,13 +42,7 @@ func main() { fmt.Println("Encrypting zeros...") t0 = time.Now() - zero0 := criteo.Run_inference__encrypt__zero__0(evaluator, params, encoder, encryptor) - zero1 := criteo.Run_inference__encrypt__zero__1(evaluator, params, encoder, encryptor) - zero2 := criteo.Run_inference__encrypt__zero__2(evaluator, params, encoder, encryptor) - zero3 := criteo.Run_inference__encrypt__zero__3(evaluator, params, encoder, encryptor) - zero4 := criteo.Run_inference__encrypt__zero__4(evaluator, params, encoder, encryptor) - zero5 := criteo.Run_inference__encrypt__zero__5(evaluator, params, encoder, encryptor) - zero6 := criteo.Run_inference__encrypt__zero__6(evaluator, params, encoder, encryptor) + ctZeros := criteo.Run_inference__encrypt__zeros(evaluator, params, encoder, encryptor) fmt.Printf(" Took %v\n", time.Since(t0)) fmt.Println("Running preprocessing...") @@ -61,7 +55,7 @@ func main() { encryptedOutput := criteo.Run_inference__preprocessed( bootstrappingEvaluator, evaluator, params, encoder, cts0, cts1, cts2, - zero0, zero1, zero2, zero3, zero4, zero5, zero6, + ctZeros, preprocessedWeights, ) fmt.Printf(" Took %v\n", time.Since(t0)) diff --git a/demos/hotword/data/hotword_timing.mlir b/demos/hotword/data/hotword_timing.mlir index 4a8bbe5..d5aadd5 100644 --- a/demos/hotword/data/hotword_timing.mlir +++ b/demos/hotword/data/hotword_timing.mlir @@ -3,7 +3,7 @@ #map2 = affine_map<(d0, d1) -> (d1)> module { func.func @tcresnet8small(%arg0: tensor<1x10x48xf32> {secret.secret}) -> tensor<1x13xf32> { - debug.validate %arg0 {name = "input", metadata = "input"} : tensor<1x10x48xf32> + debug.validate %arg0 : tensor<1x10x48xf32> %cst = arith.constant 0.000000e+00 : f32 %cst_0 = arith.constant dense_resource : tensor<13xf32> %cst_1 = arith.constant dense_resource : tensor<13x48xf32> @@ -35,39 +35,39 @@ module { %0 = tensor.empty() : tensor<1x16x48xf32> %broadcasted = linalg.broadcast ins(%cst_21 : tensor<16xf32>) outs(%0 : tensor<1x16x48xf32>) dimensions = [0, 2] %1 = linalg.conv_1d_ncw_fcw {dilations = dense<1> : vector<1xi64>, strides = dense<1> : vector<1xi64>} ins(%padded, %cst_20 : tensor<1x10x50xf32>, tensor<16x10x3xf32>) outs(%broadcasted : tensor<1x16x48xf32>) -> tensor<1x16x48xf32> - debug.validate %1 {name = "conv1", metadata = "conv1"} : tensor<1x16x48xf32> + debug.validate %1 : tensor<1x16x48xf32> %2 = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel", "parallel"]} ins(%1 : tensor<1x16x48xf32>) outs(%0 : tensor<1x16x48xf32>) attrs = {domain_lower = -6.7371 : f64, domain_upper = 7.2558 : f64, degree = 3 : i32} { ^bb0(%in: f32, %out: f32): %34 = arith.cmpf ugt, %in, %cst : f32 %35 = arith.select %34, %in, %cst : f32 linalg.yield %35 : f32 } -> tensor<1x16x48xf32> - debug.validate %2 {name = "relu1", metadata = "relu1"} : tensor<1x16x48xf32> + debug.validate %2 : tensor<1x16x48xf32> %3 = tensor.empty() : tensor<1x24x24xf32> %broadcasted_23 = linalg.broadcast ins(%cst_18 : tensor<24xf32>) outs(%3 : tensor<1x24x24xf32>) dimensions = [0, 2] %4 = linalg.conv_1d_ncw_fcw {dilations = dense<1> : vector<1xi64>, strides = dense<2> : vector<1xi64>} ins(%2, %cst_19 : tensor<1x16x48xf32>, tensor<24x16x1xf32>) outs(%broadcasted_23 : tensor<1x24x24xf32>) -> tensor<1x24x24xf32> - debug.validate %4 {name = "conv2", metadata = "conv2"} : tensor<1x24x24xf32> + debug.validate %4 : tensor<1x24x24xf32> %padded_24 = tensor.pad %2 low[0, 0, 4] high[0, 0, 4] { ^bb0(%arg1: index, %arg2: index, %arg3: index): tensor.yield %cst : f32 } : tensor<1x16x48xf32> to tensor<1x16x56xf32> %broadcasted_25 = linalg.broadcast ins(%cst_16 : tensor<24xf32>) outs(%3 : tensor<1x24x24xf32>) dimensions = [0, 2] %5 = linalg.conv_1d_ncw_fcw {dilations = dense<1> : vector<1xi64>, strides = dense<2> : vector<1xi64>} ins(%padded_24, %cst_17 : tensor<1x16x56xf32>, tensor<24x16x9xf32>) outs(%broadcasted_25 : tensor<1x24x24xf32>) -> tensor<1x24x24xf32> - debug.validate %5 {name = "conv3", metadata = "conv3"} : tensor<1x24x24xf32> + debug.validate %5 : tensor<1x24x24xf32> %6 = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel", "parallel"]} ins(%5 : tensor<1x24x24xf32>) outs(%3 : tensor<1x24x24xf32>) attrs = {domain_lower = -7.3747 : f64, domain_upper = 6.4133 : f64, degree = 3 : i32} { ^bb0(%in: f32, %out: f32): %34 = arith.cmpf ugt, %in, %cst : f32 %35 = arith.select %34, %in, %cst : f32 linalg.yield %35 : f32 } -> tensor<1x24x24xf32> - debug.validate %6 {name = "relu2", metadata = "relu2"} : tensor<1x24x24xf32> + debug.validate %6 : tensor<1x24x24xf32> %padded_26 = tensor.pad %6 low[0, 0, 4] high[0, 0, 4] { ^bb0(%arg1: index, %arg2: index, %arg3: index): tensor.yield %cst : f32 } : tensor<1x24x24xf32> to tensor<1x24x32xf32> %broadcasted_27 = linalg.broadcast ins(%cst_14 : tensor<24xf32>) outs(%3 : tensor<1x24x24xf32>) dimensions = [0, 2] %7 = linalg.conv_1d_ncw_fcw {dilations = dense<1> : vector<1xi64>, strides = dense<1> : vector<1xi64>} ins(%padded_26, %cst_15 : tensor<1x24x32xf32>, tensor<24x24x9xf32>) outs(%broadcasted_27 : tensor<1x24x24xf32>) -> tensor<1x24x24xf32> - debug.validate %7 {name = "conv4", metadata = "conv4"} : tensor<1x24x24xf32> + debug.validate %7 : tensor<1x24x24xf32> %8 = linalg.generic {indexing_maps = [#map, #map, #map], iterator_types = ["parallel", "parallel", "parallel"]} ins(%7, %4 : tensor<1x24x24xf32>, tensor<1x24x24xf32>) outs(%3 : tensor<1x24x24xf32>) { ^bb0(%in: f32, %in_38: f32, %out: f32): %34 = arith.addf %in, %in_38 : f32 @@ -79,32 +79,32 @@ module { %35 = arith.select %34, %in, %cst : f32 linalg.yield %35 : f32 } -> tensor<1x24x24xf32> - debug.validate %9 {name = "relu3", metadata = "relu3"} : tensor<1x24x24xf32> + debug.validate %9 : tensor<1x24x24xf32> %10 = tensor.empty() : tensor<1x32x12xf32> %broadcasted_28 = linalg.broadcast ins(%cst_12 : tensor<32xf32>) outs(%10 : tensor<1x32x12xf32>) dimensions = [0, 2] %11 = linalg.conv_1d_ncw_fcw {dilations = dense<1> : vector<1xi64>, strides = dense<2> : vector<1xi64>} ins(%9, %cst_13 : tensor<1x24x24xf32>, tensor<32x24x1xf32>) outs(%broadcasted_28 : tensor<1x32x12xf32>) -> tensor<1x32x12xf32> - debug.validate %11 {name = "conv5", metadata = "conv5"} : tensor<1x32x12xf32> + debug.validate %11 : tensor<1x32x12xf32> %padded_29 = tensor.pad %9 low[0, 0, 4] high[0, 0, 4] { ^bb0(%arg1: index, %arg2: index, %arg3: index): tensor.yield %cst : f32 } : tensor<1x24x24xf32> to tensor<1x24x32xf32> %broadcasted_30 = linalg.broadcast ins(%cst_10 : tensor<32xf32>) outs(%10 : tensor<1x32x12xf32>) dimensions = [0, 2] %12 = linalg.conv_1d_ncw_fcw {dilations = dense<1> : vector<1xi64>, strides = dense<2> : vector<1xi64>} ins(%padded_29, %cst_11 : tensor<1x24x32xf32>, tensor<32x24x9xf32>) outs(%broadcasted_30 : tensor<1x32x12xf32>) -> tensor<1x32x12xf32> - debug.validate %12 {name = "conv6", metadata = "conv6"} : tensor<1x32x12xf32> + debug.validate %12 : tensor<1x32x12xf32> %13 = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel", "parallel"]} ins(%12 : tensor<1x32x12xf32>) outs(%10 : tensor<1x32x12xf32>) attrs = {domain_lower = -6.0429 : f64, domain_upper = 5.2103 : f64, degree = 3 : i32} { ^bb0(%in: f32, %out: f32): %34 = arith.cmpf ugt, %in, %cst : f32 %35 = arith.select %34, %in, %cst : f32 linalg.yield %35 : f32 } -> tensor<1x32x12xf32> - debug.validate %13 {name = "relu4", metadata = "relu4"} : tensor<1x32x12xf32> + debug.validate %13 : tensor<1x32x12xf32> %padded_31 = tensor.pad %13 low[0, 0, 4] high[0, 0, 4] { ^bb0(%arg1: index, %arg2: index, %arg3: index): tensor.yield %cst : f32 } : tensor<1x32x12xf32> to tensor<1x32x20xf32> %broadcasted_32 = linalg.broadcast ins(%cst_8 : tensor<32xf32>) outs(%10 : tensor<1x32x12xf32>) dimensions = [0, 2] %14 = linalg.conv_1d_ncw_fcw {dilations = dense<1> : vector<1xi64>, strides = dense<1> : vector<1xi64>} ins(%padded_31, %cst_9 : tensor<1x32x20xf32>, tensor<32x32x9xf32>) outs(%broadcasted_32 : tensor<1x32x12xf32>) -> tensor<1x32x12xf32> - debug.validate %14 {name = "conv7", metadata = "conv7"} : tensor<1x32x12xf32> + debug.validate %14 : tensor<1x32x12xf32> %15 = linalg.generic {indexing_maps = [#map, #map, #map], iterator_types = ["parallel", "parallel", "parallel"]} ins(%14, %11 : tensor<1x32x12xf32>, tensor<1x32x12xf32>) outs(%10 : tensor<1x32x12xf32>) { ^bb0(%in: f32, %in_38: f32, %out: f32): %34 = arith.addf %in, %in_38 : f32 @@ -116,32 +116,32 @@ module { %35 = arith.select %34, %in, %cst : f32 linalg.yield %35 : f32 } -> tensor<1x32x12xf32> - debug.validate %16 {name = "relu5", metadata = "relu5"} : tensor<1x32x12xf32> + debug.validate %16 : tensor<1x32x12xf32> %17 = tensor.empty() : tensor<1x48x6xf32> %broadcasted_33 = linalg.broadcast ins(%cst_6 : tensor<48xf32>) outs(%17 : tensor<1x48x6xf32>) dimensions = [0, 2] %18 = linalg.conv_1d_ncw_fcw {dilations = dense<1> : vector<1xi64>, strides = dense<2> : vector<1xi64>} ins(%16, %cst_7 : tensor<1x32x12xf32>, tensor<48x32x1xf32>) outs(%broadcasted_33 : tensor<1x48x6xf32>) -> tensor<1x48x6xf32> - debug.validate %18 {name = "conv8", metadata = "conv8"} : tensor<1x48x6xf32> + debug.validate %18 : tensor<1x48x6xf32> %padded_34 = tensor.pad %16 low[0, 0, 4] high[0, 0, 4] { ^bb0(%arg1: index, %arg2: index, %arg3: index): tensor.yield %cst : f32 } : tensor<1x32x12xf32> to tensor<1x32x20xf32> %broadcasted_35 = linalg.broadcast ins(%cst_4 : tensor<48xf32>) outs(%17 : tensor<1x48x6xf32>) dimensions = [0, 2] %19 = linalg.conv_1d_ncw_fcw {dilations = dense<1> : vector<1xi64>, strides = dense<2> : vector<1xi64>} ins(%padded_34, %cst_5 : tensor<1x32x20xf32>, tensor<48x32x9xf32>) outs(%broadcasted_35 : tensor<1x48x6xf32>) -> tensor<1x48x6xf32> - debug.validate %19 {name = "conv9", metadata = "conv9"} : tensor<1x48x6xf32> + debug.validate %19 : tensor<1x48x6xf32> %20 = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel", "parallel"]} ins(%19 : tensor<1x48x6xf32>) outs(%17 : tensor<1x48x6xf32>) attrs = {domain_lower = -5.4222 : f64, domain_upper = 4.1606 : f64, degree = 3 : i32} { ^bb0(%in: f32, %out: f32): %34 = arith.cmpf ugt, %in, %cst : f32 %35 = arith.select %34, %in, %cst : f32 linalg.yield %35 : f32 } -> tensor<1x48x6xf32> - debug.validate %20 {name = "relu6", metadata = "relu6"} : tensor<1x48x6xf32> + debug.validate %20 : tensor<1x48x6xf32> %padded_36 = tensor.pad %20 low[0, 0, 4] high[0, 0, 4] { ^bb0(%arg1: index, %arg2: index, %arg3: index): tensor.yield %cst : f32 } : tensor<1x48x6xf32> to tensor<1x48x14xf32> %broadcasted_37 = linalg.broadcast ins(%cst_2 : tensor<48xf32>) outs(%17 : tensor<1x48x6xf32>) dimensions = [0, 2] %21 = linalg.conv_1d_ncw_fcw {dilations = dense<1> : vector<1xi64>, strides = dense<1> : vector<1xi64>} ins(%padded_36, %cst_3 : tensor<1x48x14xf32>, tensor<48x48x9xf32>) outs(%broadcasted_37 : tensor<1x48x6xf32>) -> tensor<1x48x6xf32> - debug.validate %21 {name = "conv10", metadata = "conv10"} : tensor<1x48x6xf32> + debug.validate %21 : tensor<1x48x6xf32> %22 = linalg.generic {indexing_maps = [#map, #map, #map], iterator_types = ["parallel", "parallel", "parallel"]} ins(%21, %18 : tensor<1x48x6xf32>, tensor<1x48x6xf32>) outs(%17 : tensor<1x48x6xf32>) { ^bb0(%in: f32, %in_38: f32, %out: f32): %34 = arith.addf %in, %in_38 : f32 @@ -153,7 +153,7 @@ module { %35 = arith.select %34, %in, %cst : f32 linalg.yield %35 : f32 } -> tensor<1x48x6xf32> - debug.validate %23 {name = "relu7", metadata = "relu7"} : tensor<1x48x6xf32> + debug.validate %23 : tensor<1x48x6xf32> %24 = tensor.empty() : tensor<1x48x1xf32> %25 = linalg.fill ins(%cst : f32) outs(%24 : tensor<1x48x1xf32>) -> tensor<1x48x1xf32> %26 = tensor.empty() : tensor<6xf32> @@ -174,7 +174,7 @@ module { %34 = arith.addf %in, %in_38 : f32 linalg.yield %34 : f32 } -> tensor<1x13xf32> - debug.validate %33 {name = "output", metadata = "output"} : tensor<1x13xf32> + debug.validate %33 : tensor<1x13xf32> return %33 : tensor<1x13xf32> } } diff --git a/demos/hotword/lattigo/evaluate_fhe.go b/demos/hotword/lattigo/evaluate_fhe.go index 2285830..d3200eb 100644 --- a/demos/hotword/lattigo/evaluate_fhe.go +++ b/demos/hotword/lattigo/evaluate_fhe.go @@ -70,18 +70,11 @@ func main() { // FHE evaluation fmt.Println("Running FHE evaluation (preprocessed)...") t0 = time.Now() - ctZero0 := hotword_lattigo.Tcresnet8small__encrypt__zero__0(evaluator, params, ecd, encryptor) - ctZero1 := hotword_lattigo.Tcresnet8small__encrypt__zero__1(evaluator, params, ecd, encryptor) - ctZero2 := hotword_lattigo.Tcresnet8small__encrypt__zero__2(evaluator, params, ecd, encryptor) - ctZero3 := hotword_lattigo.Tcresnet8small__encrypt__zero__3(evaluator, params, ecd, encryptor) - ctZero4 := hotword_lattigo.Tcresnet8small__encrypt__zero__4(evaluator, params, ecd, encryptor) - ctZero5 := hotword_lattigo.Tcresnet8small__encrypt__zero__5(evaluator, params, ecd, encryptor) - ctZero6 := hotword_lattigo.Tcresnet8small__encrypt__zero__6(evaluator, params, ecd, encryptor) - ctZero7 := hotword_lattigo.Tcresnet8small__encrypt__zero__7(evaluator, params, ecd, encryptor) + ctZeros := hotword_lattigo.Tcresnet8small__encrypt__zeros(evaluator, params, ecd, encryptor) encryptedOutput := hotword_lattigo.Tcresnet8small__preprocessed( btpEvaluator, evaluator, params, ecd, encryptedFeatures, - ctZero0, ctZero1, ctZero2, ctZero3, ctZero4, ctZero5, ctZero6, ctZero7, + ctZeros, preprocessedWeights, ) fmt.Printf(" Took %v\n", time.Since(t0)) diff --git a/demos/hotword/lattigo/evaluate_fhe_suite.go b/demos/hotword/lattigo/evaluate_fhe_suite.go index 803a601..95241ea 100644 --- a/demos/hotword/lattigo/evaluate_fhe_suite.go +++ b/demos/hotword/lattigo/evaluate_fhe_suite.go @@ -70,24 +70,10 @@ func main() { fmt.Println("Encrypting all input features and zero accumulators sequentially...") t0 = time.Now() encryptedInputs := make([][]*rlwe.Ciphertext, numSamples) - ctZeros0 := make([]*rlwe.Ciphertext, numSamples) - ctZeros1 := make([]*rlwe.Ciphertext, numSamples) - ctZeros2 := make([]*rlwe.Ciphertext, numSamples) - ctZeros3 := make([]*rlwe.Ciphertext, numSamples) - ctZeros4 := make([]*rlwe.Ciphertext, numSamples) - ctZeros5 := make([]*rlwe.Ciphertext, numSamples) - ctZeros6 := make([]*rlwe.Ciphertext, numSamples) - ctZeros7 := make([]*rlwe.Ciphertext, numSamples) + ctZeros := make([][]*rlwe.Ciphertext, numSamples) for i := 0; i < numSamples; i++ { encryptedInputs[i] = hotword_lattigo.Tcresnet8small__encrypt__arg0(evaluator, params, ecd, encryptor, allFeatures[i]) - ctZeros0[i] = hotword_lattigo.Tcresnet8small__encrypt__zero__0(evaluator, params, ecd, encryptor) - ctZeros1[i] = hotword_lattigo.Tcresnet8small__encrypt__zero__1(evaluator, params, ecd, encryptor) - ctZeros2[i] = hotword_lattigo.Tcresnet8small__encrypt__zero__2(evaluator, params, ecd, encryptor) - ctZeros3[i] = hotword_lattigo.Tcresnet8small__encrypt__zero__3(evaluator, params, ecd, encryptor) - ctZeros4[i] = hotword_lattigo.Tcresnet8small__encrypt__zero__4(evaluator, params, ecd, encryptor) - ctZeros5[i] = hotword_lattigo.Tcresnet8small__encrypt__zero__5(evaluator, params, ecd, encryptor) - ctZeros6[i] = hotword_lattigo.Tcresnet8small__encrypt__zero__6(evaluator, params, ecd, encryptor) - ctZeros7[i] = hotword_lattigo.Tcresnet8small__encrypt__zero__7(evaluator, params, ecd, encryptor) + ctZeros[i] = hotword_lattigo.Tcresnet8small__encrypt__zeros(evaluator, params, ecd, encryptor) } fmt.Printf(" Took %v\n", time.Since(t0)) @@ -106,7 +92,7 @@ func main() { localBtpEvaluator := btpEvaluator.ShallowCopy() encryptedOutputs[idx] = hotword_lattigo.Tcresnet8small__preprocessed( localBtpEvaluator, localEvaluator, params, ecd, encryptedInputs[idx], - ctZeros0[idx], ctZeros1[idx], ctZeros2[idx], ctZeros3[idx], ctZeros4[idx], ctZeros5[idx], ctZeros6[idx], ctZeros7[idx], + ctZeros[idx], preprocessedWeights, ) }(i) diff --git a/demos/hotword/lattigo/evaluate_fhe_timing.go b/demos/hotword/lattigo/evaluate_fhe_timing.go index a17daac..9e398da 100644 --- a/demos/hotword/lattigo/evaluate_fhe_timing.go +++ b/demos/hotword/lattigo/evaluate_fhe_timing.go @@ -70,19 +70,12 @@ func main() { // FHE evaluation fmt.Println("Running FHE evaluation (preprocessed with timing)...") t0 = time.Now() - ctZero0 := hotwordlattigotiming.Tcresnet8small__encrypt__zero__0(evaluator, params, ecd, encryptor) - ctZero1 := hotwordlattigotiming.Tcresnet8small__encrypt__zero__1(evaluator, params, ecd, encryptor) - ctZero2 := hotwordlattigotiming.Tcresnet8small__encrypt__zero__2(evaluator, params, ecd, encryptor) - ctZero3 := hotwordlattigotiming.Tcresnet8small__encrypt__zero__3(evaluator, params, ecd, encryptor) - ctZero4 := hotwordlattigotiming.Tcresnet8small__encrypt__zero__4(evaluator, params, ecd, encryptor) - ctZero5 := hotwordlattigotiming.Tcresnet8small__encrypt__zero__5(evaluator, params, ecd, encryptor) - ctZero6 := hotwordlattigotiming.Tcresnet8small__encrypt__zero__6(evaluator, params, ecd, encryptor) - ctZero7 := hotwordlattigotiming.Tcresnet8small__encrypt__zero__7(evaluator, params, ecd, encryptor) + ctZeros := hotwordlattigotiming.Tcresnet8small__encrypt__zeros(evaluator, params, ecd, encryptor) // Note: decryptor is passed as 5th argument now (after ecd, before encryptedFeatures) encryptedOutput := hotwordlattigotiming.Tcresnet8small__preprocessed( btpEvaluator, evaluator, params, ecd, decryptor, encryptedFeatures, - ctZero0, ctZero1, ctZero2, ctZero3, ctZero4, ctZero5, ctZero6, ctZero7, + ctZeros, preprocessedWeights, ) fmt.Printf(" Took %v\n", time.Since(t0)) diff --git a/demos/mnist/data/mnist_timing.mlir b/demos/mnist/data/mnist_timing.mlir index 0ee424c..1698366 100644 --- a/demos/mnist/data/mnist_timing.mlir +++ b/demos/mnist/data/mnist_timing.mlir @@ -10,7 +10,7 @@ module { %cst_4 = arith.constant dense_resource : tensor<512xf32> %cst_5 = arith.constant dense_resource : tensor<512x784xf32> %collapsed = tensor.collapse_shape %arg0 [[0], [1, 2, 3]] : tensor<1x1x28x28xf32> into tensor<1x784xf32> - debug.validate %collapsed {name = "input", metadata = "input"} : tensor<1x784xf32> + debug.validate %collapsed : tensor<1x784xf32> %0 = tensor.empty() : tensor<784x512xf32> %transposed = linalg.transpose ins(%cst_5 : tensor<512x784xf32>) outs(%0 : tensor<784x512xf32>) permutation = [1, 0] %1 = tensor.empty() : tensor<1x512xf32> @@ -21,14 +21,14 @@ module { %15 = arith.addf %in, %in_8 : f32 linalg.yield %15 : f32 } -> tensor<1x512xf32> - debug.validate %4 {name = "layer1_linear", metadata = "layer1_linear"} : tensor<1x512xf32> + debug.validate %4 : tensor<1x512xf32> %5 = linalg.generic {domain_lower = -4.270929 : f32, domain_upper = 4.270929 : f32, degree= 7 : i32, indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel"]} ins(%4 : tensor<1x512xf32>) outs(%1 : tensor<1x512xf32>) { ^bb0(%in: f32, %out: f32): %15 = arith.cmpf ugt, %in, %cst : f32 %16 = arith.select %15, %in, %cst : f32 linalg.yield %16 : f32 } -> tensor<1x512xf32> - debug.validate %5 {name = "layer1_relu", metadata = "layer1_relu"} : tensor<1x512xf32> + debug.validate %5 : tensor<1x512xf32> %6 = tensor.empty() : tensor<512x512xf32> %transposed_6 = linalg.transpose ins(%cst_3 : tensor<512x512xf32>) outs(%6 : tensor<512x512xf32>) permutation = [1, 0] %7 = linalg.matmul ins(%5, %transposed_6 : tensor<1x512xf32>, tensor<512x512xf32>) outs(%2 : tensor<1x512xf32>) -> tensor<1x512xf32> @@ -37,14 +37,14 @@ module { %15 = arith.addf %in, %in_8 : f32 linalg.yield %15 : f32 } -> tensor<1x512xf32> - debug.validate %8 {name = "layer2_linear", metadata = "layer2_linear"} : tensor<1x512xf32> + debug.validate %8 : tensor<1x512xf32> %9 = linalg.generic {domain_lower = -4.484251 : f32, domain_upper = 4.484251 : f32, degree= 7 : i32, indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel"]} ins(%8 : tensor<1x512xf32>) outs(%1 : tensor<1x512xf32>) { ^bb0(%in: f32, %out: f32): %15 = arith.cmpf ugt, %in, %cst : f32 %16 = arith.select %15, %in, %cst : f32 linalg.yield %16 : f32 } -> tensor<1x512xf32> - debug.validate %9 {name = "layer2_relu", metadata = "layer2_relu"} : tensor<1x512xf32> + debug.validate %9 : tensor<1x512xf32> %10 = tensor.empty() : tensor<512x10xf32> %transposed_7 = linalg.transpose ins(%cst_1 : tensor<10x512xf32>) outs(%10 : tensor<512x10xf32>) permutation = [1, 0] %11 = tensor.empty() : tensor<1x10xf32> @@ -55,7 +55,7 @@ module { %15 = arith.addf %in, %in_8 : f32 linalg.yield %15 : f32 } -> tensor<1x10xf32> - debug.validate %14 {name = "output", metadata = "output"} : tensor<1x10xf32> + debug.validate %14 : tensor<1x10xf32> return %14 : tensor<1x10xf32> } } diff --git a/demos/mnist/lattigo/evaluate_fhe.go b/demos/mnist/lattigo/evaluate_fhe.go index 1b37d4a..4eb2f49 100644 --- a/demos/mnist/lattigo/evaluate_fhe.go +++ b/demos/mnist/lattigo/evaluate_fhe.go @@ -45,14 +45,12 @@ func main() { tEncStart := time.Now() ctInput := mnist.Mnist__encrypt__arg0(evaluator, params, encoder, encryptor, image) - ctZeros0 := mnist.Mnist__encrypt__zero__0(evaluator, params, encoder, encryptor) - ctZeros1 := mnist.Mnist__encrypt__zero__1(evaluator, params, encoder, encryptor) - ctZeros2 := mnist.Mnist__encrypt__zero__2(evaluator, params, encoder, encryptor) + ctZeros := mnist.Mnist__encrypt__zeros(evaluator, params, encoder, encryptor) encryptDuration := time.Since(tEncStart) fmt.Printf("Input & Zero Encryption: %.4f ms\n", float64(encryptDuration.Microseconds())/1000.0) tEvalStart := time.Now() - resCt := mnist.Mnist__preprocessed(evaluator, params, encoder, ctInput, ctZeros0, ctZeros1, ctZeros2, preprocessedWeights) + resCt := mnist.Mnist__preprocessed(evaluator, params, encoder, ctInput, ctZeros, preprocessedWeights) evalDuration := time.Since(tEvalStart) fmt.Printf("Homomorphic Evaluation: %.4f ms\n", float64(evalDuration.Microseconds())/1000.0) diff --git a/demos/mnist/lattigo/evaluate_fhe_suite.go b/demos/mnist/lattigo/evaluate_fhe_suite.go index 3557229..63058eb 100644 --- a/demos/mnist/lattigo/evaluate_fhe_suite.go +++ b/demos/mnist/lattigo/evaluate_fhe_suite.go @@ -35,9 +35,7 @@ func main() { fmt.Printf("Weight preprocessing completed in %.2f ms.\n\n", float64(tPreEnd.Sub(tPreStart).Microseconds())/1000.0) fmt.Println("Encrypting zero constant vectors...") - ctZeros0 := mnist.Mnist__encrypt__zero__0(evaluator, params, encoder, encryptor) - ctZeros1 := mnist.Mnist__encrypt__zero__1(evaluator, params, encoder, encryptor) - ctZeros2 := mnist.Mnist__encrypt__zero__2(evaluator, params, encoder, encryptor) + ctZeros := mnist.Mnist__encrypt__zeros(evaluator, params, encoder, encryptor) fmt.Printf("Evaluating %d MNIST samples sequentially...\n", *numSamples) correct := 0 @@ -61,7 +59,7 @@ func main() { ctInput := mnist.Mnist__encrypt__arg0(evaluator, params, encoder, encryptor, image) tEvalStart := time.Now() - resCt := mnist.Mnist__preprocessed(evaluator, params, encoder, ctInput, ctZeros0, ctZeros1, ctZeros2, preprocessedWeights) + resCt := mnist.Mnist__preprocessed(evaluator, params, encoder, ctInput, ctZeros, preprocessedWeights) evalDuration := time.Since(tEvalStart) totalEvalDuration += evalDuration diff --git a/demos/mnist/lattigo/evaluate_fhe_timing.go b/demos/mnist/lattigo/evaluate_fhe_timing.go index 4b32396..79f9931 100644 --- a/demos/mnist/lattigo/evaluate_fhe_timing.go +++ b/demos/mnist/lattigo/evaluate_fhe_timing.go @@ -46,15 +46,13 @@ func main() { tEncStart := time.Now() ctInput := mnist_timing.Mnist__encrypt__arg0(evaluator, params, encoder, encryptor, image) - ctZeros0 := mnist_timing.Mnist__encrypt__zero__0(evaluator, params, encoder, encryptor) - ctZeros1 := mnist_timing.Mnist__encrypt__zero__1(evaluator, params, encoder, encryptor) - ctZeros2 := mnist_timing.Mnist__encrypt__zero__2(evaluator, params, encoder, encryptor) + ctZeros := mnist_timing.Mnist__encrypt__zeros(evaluator, params, encoder, encryptor) encryptDuration := time.Since(tEncStart) fmt.Printf("Input & Zero Encryption: %.4f ms\n", float64(encryptDuration.Microseconds())/1000.0) tEvalStart := time.Now() // Pass decryptor for intermediate timing/debug callbacks - resCt := mnist_timing.Mnist__preprocessed(evaluator, params, encoder, decryptor, ctInput, ctZeros0, ctZeros1, ctZeros2, preprocessedWeights) + resCt := mnist_timing.Mnist__preprocessed(evaluator, params, encoder, decryptor, ctInput, ctZeros, preprocessedWeights) evalDuration := time.Since(tEvalStart) fmt.Printf("Homomorphic Evaluation: %.4f ms\n", float64(evalDuration.Microseconds())/1000.0) diff --git a/demos/mnist/openfhe/evaluate_fhe.py b/demos/mnist/openfhe/evaluate_fhe.py index 73d6776..e0b35b6 100644 --- a/demos/mnist/openfhe/evaluate_fhe.py +++ b/demos/mnist/openfhe/evaluate_fhe.py @@ -65,23 +65,21 @@ def evaluate_single(data_dir: str, sample_idx: int) -> None: t3 = time.perf_counter() crypto_setup_ms = (t3 - t2) * 1000.0 - zero_encrypt_func_names = sorted( - [name for name in dir(mnist) if name.startswith("mnist__encrypt__zero__")] - ) - zero_encrypt_funcs = [ - getattr(mnist, name) for name in zero_encrypt_func_names - ] - t4 = time.perf_counter() input_encrypted = mnist.mnist__encrypt__arg0( crypto_context, input_vector, public_key ) - ct_zeros = [func(crypto_context, public_key) for func in zero_encrypt_funcs] + input_enc_arg = ( + input_encrypted[0] + if isinstance(input_encrypted, list) + else input_encrypted + ) + ct_zeros = mnist.mnist__encrypt__zeros(crypto_context, public_key) t5 = time.perf_counter() encrypt_ms = (t5 - t4) * 1000.0 t6 = time.perf_counter() - output_encrypted = mnist.mnist(crypto_context, input_encrypted, *ct_zeros) + output_encrypted = mnist.mnist(crypto_context, input_enc_arg, ct_zeros) t7 = time.perf_counter() eval_ms = (t7 - t6) * 1000.0 diff --git a/demos/mnist/openfhe/evaluate_fhe_suite.py b/demos/mnist/openfhe/evaluate_fhe_suite.py index 54be835..2bc4c28 100644 --- a/demos/mnist/openfhe/evaluate_fhe_suite.py +++ b/demos/mnist/openfhe/evaluate_fhe_suite.py @@ -67,13 +67,7 @@ def evaluate_suite(data_dir: str, num_samples: int) -> None: f" {(t_setup_end - t_setup_start)*1000:.2f} ms.\n" ) - zero_encrypt_func_names = sorted( - [name for name in dir(mnist) if name.startswith("mnist__encrypt__zero__")] - ) - zero_encrypt_funcs = [ - getattr(mnist, name) for name in zero_encrypt_func_names - ] - ct_zeros = [func(crypto_context, public_key) for func in zero_encrypt_funcs] + ct_zeros = mnist.mnist__encrypt__zeros(crypto_context, public_key) print(f"Evaluating {num_samples} MNIST samples sequentially...") correct = 0 @@ -86,9 +80,14 @@ def evaluate_suite(data_dir: str, num_samples: int) -> None: input_encrypted = mnist.mnist__encrypt__arg0( crypto_context, input_vector, public_key ) + input_enc_arg = ( + input_encrypted[0] + if isinstance(input_encrypted, list) + else input_encrypted + ) t_eval_start = time.perf_counter() - output_encrypted = mnist.mnist(crypto_context, input_encrypted, *ct_zeros) + output_encrypted = mnist.mnist(crypto_context, input_enc_arg, ct_zeros) t_eval_end = time.perf_counter() eval_time_s = t_eval_end - t_eval_start diff --git a/demos/network_anomaly/data/torch_kitnet_model_timing.mlir b/demos/network_anomaly/data/torch_kitnet_model_timing.mlir index e19929a..9b0fae5 100644 --- a/demos/network_anomaly/data/torch_kitnet_model_timing.mlir +++ b/demos/network_anomaly/data/torch_kitnet_model_timing.mlir @@ -3,7 +3,7 @@ #map2 = affine_map<(d0, d1) -> (d0)> module { func.func @main(%arg0: tensor<1x5xf32> {secret.secret}) -> (tensor<1xf32>, tensor<1x5xf32>) { - debug.validate %arg0 {name = "input", metadata = "input"} : tensor<1x5xf32> + debug.validate %arg0 : tensor<1x5xf32> %c2_i64 = arith.constant 2 : i64 %cst = arith.constant 0.000000e+00 : f32 %cst_0 = arith.constant 1.000000e+00 : f32 @@ -20,13 +20,13 @@ module { %1 = tensor.empty() : tensor<1x4xf32> %2 = linalg.fill ins(%cst : f32) outs(%1 : tensor<1x4xf32>) -> tensor<1x4xf32> %3 = linalg.matmul ins(%arg0, %transposed : tensor<1x5xf32>, tensor<5x4xf32>) outs(%2 : tensor<1x4xf32>) -> tensor<1x4xf32> - debug.validate %3 {name = "layer1_matmul", metadata = "layer1_matmul"} : tensor<1x4xf32> + debug.validate %3 : tensor<1x4xf32> %4 = linalg.generic {indexing_maps = [#map, #map1, #map], iterator_types = ["parallel", "parallel"]} ins(%3, %cst_7 : tensor<1x4xf32>, tensor<4xf32>) outs(%1 : tensor<1x4xf32>) { ^bb0(%in: f32, %in_12: f32, %out: f32): %27 = arith.addf %in, %in_12 : f32 linalg.yield %27 : f32 } -> tensor<1x4xf32> - debug.validate %4 {name = "layer1_bias", metadata = "layer1_bias"} : tensor<1x4xf32> + debug.validate %4 : tensor<1x4xf32> %5 = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel"]} ins(%4 : tensor<1x4xf32>) outs(%1 : tensor<1x4xf32>) { ^bb0(%in: f32, %out: f32): %27 = arith.negf %in : f32 @@ -35,19 +35,19 @@ module { %30 = arith.divf %cst_0, %29 : f32 linalg.yield %30 : f32 } -> tensor<1x4xf32> - debug.validate %5 {name = "layer1_sigmoid", metadata = "layer1_sigmoid"} : tensor<1x4xf32> + debug.validate %5 : tensor<1x4xf32> %6 = tensor.empty() : tensor<4x5xf32> %transposed_9 = linalg.transpose ins(%cst_6 : tensor<5x4xf32>) outs(%6 : tensor<4x5xf32>) permutation = [1, 0] %7 = tensor.empty() : tensor<1x5xf32> %8 = linalg.fill ins(%cst : f32) outs(%7 : tensor<1x5xf32>) -> tensor<1x5xf32> %9 = linalg.matmul ins(%5, %transposed_9 : tensor<1x4xf32>, tensor<4x5xf32>) outs(%8 : tensor<1x5xf32>) -> tensor<1x5xf32> - debug.validate %9 {name = "layer2_matmul", metadata = "layer2_matmul"} : tensor<1x5xf32> + debug.validate %9 : tensor<1x5xf32> %10 = linalg.generic {indexing_maps = [#map, #map1, #map], iterator_types = ["parallel", "parallel"]} ins(%9, %cst_5 : tensor<1x5xf32>, tensor<5xf32>) outs(%7 : tensor<1x5xf32>) { ^bb0(%in: f32, %in_12: f32, %out: f32): %27 = arith.addf %in, %in_12 : f32 linalg.yield %27 : f32 } -> tensor<1x5xf32> - debug.validate %10 {name = "layer2_bias", metadata = "layer2_bias"} : tensor<1x5xf32> + debug.validate %10 : tensor<1x5xf32> %11 = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel"]} ins(%10 : tensor<1x5xf32>) outs(%7 : tensor<1x5xf32>) { ^bb0(%in: f32, %out: f32): %27 = arith.negf %in : f32 @@ -56,58 +56,58 @@ module { %30 = arith.divf %cst_0, %29 : f32 linalg.yield %30 : f32 } -> tensor<1x5xf32> - debug.validate %11 {name = "layer2_sigmoid", metadata = "layer2_sigmoid"} : tensor<1x5xf32> + debug.validate %11 : tensor<1x5xf32> %12 = linalg.generic {indexing_maps = [#map, #map, #map], iterator_types = ["parallel", "parallel"]} ins(%arg0, %11 : tensor<1x5xf32>, tensor<1x5xf32>) outs(%7 : tensor<1x5xf32>) { ^bb0(%in: f32, %in_12: f32, %out: f32): %27 = arith.subf %in, %in_12 : f32 linalg.yield %27 : f32 } -> tensor<1x5xf32> - debug.validate %12 {name = "sub1", metadata = "sub1"} : tensor<1x5xf32> + debug.validate %12 : tensor<1x5xf32> %13 = tensor.empty() : tensor<5x1xf32> %transposed_10 = linalg.transpose ins(%cst_4 : tensor<1x5xf32>) outs(%13 : tensor<5x1xf32>) permutation = [1, 0] %14 = tensor.empty() : tensor<1x1xf32> %15 = linalg.fill ins(%cst : f32) outs(%14 : tensor<1x1xf32>) -> tensor<1x1xf32> %16 = linalg.matmul ins(%12, %transposed_10 : tensor<1x5xf32>, tensor<5x1xf32>) outs(%15 : tensor<1x1xf32>) -> tensor<1x1xf32> - debug.validate %16 {name = "layer3_matmul", metadata = "layer3_matmul"} : tensor<1x1xf32> + debug.validate %16 : tensor<1x1xf32> %17 = linalg.generic {indexing_maps = [#map, #map1, #map], iterator_types = ["parallel", "parallel"]} ins(%16, %cst_3 : tensor<1x1xf32>, tensor<1xf32>) outs(%14 : tensor<1x1xf32>) { ^bb0(%in: f32, %in_12: f32, %out: f32): %27 = arith.addf %in, %in_12 : f32 linalg.yield %27 : f32 } -> tensor<1x1xf32> - debug.validate %17 {name = "layer3_bias", metadata = "layer3_bias"} : tensor<1x1xf32> + debug.validate %17 : tensor<1x1xf32> %18 = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel"]} ins(%17 : tensor<1x1xf32>) outs(%14 : tensor<1x1xf32>) { ^bb0(%in: f32, %out: f32): %27 = math.tanh %in : f32 linalg.yield %27 : f32 } -> tensor<1x1xf32> - debug.validate %18 {name = "layer3_tanh", metadata = "layer3_tanh"} : tensor<1x1xf32> + debug.validate %18 : tensor<1x1xf32> %transposed_11 = linalg.transpose ins(%cst_2 : tensor<5x1xf32>) outs(%7 : tensor<1x5xf32>) permutation = [1, 0] %19 = linalg.matmul ins(%18, %transposed_11 : tensor<1x1xf32>, tensor<1x5xf32>) outs(%8 : tensor<1x5xf32>) -> tensor<1x5xf32> - debug.validate %19 {name = "layer4_matmul", metadata = "layer4_matmul"} : tensor<1x5xf32> + debug.validate %19 : tensor<1x5xf32> %20 = linalg.generic {indexing_maps = [#map, #map1, #map], iterator_types = ["parallel", "parallel"]} ins(%19, %cst_1 : tensor<1x5xf32>, tensor<5xf32>) outs(%7 : tensor<1x5xf32>) { ^bb0(%in: f32, %in_12: f32, %out: f32): %27 = arith.addf %in, %in_12 : f32 linalg.yield %27 : f32 } -> tensor<1x5xf32> - debug.validate %20 {name = "layer4_bias", metadata = "layer4_bias"} : tensor<1x5xf32> + debug.validate %20 : tensor<1x5xf32> %21 = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel"]} ins(%20 : tensor<1x5xf32>) outs(%7 : tensor<1x5xf32>) { ^bb0(%in: f32, %out: f32): %27 = math.tanh %in : f32 linalg.yield %27 : f32 } -> tensor<1x5xf32> - debug.validate %21 {name = "layer4_tanh", metadata = "layer4_tanh"} : tensor<1x5xf32> + debug.validate %21 : tensor<1x5xf32> %22 = linalg.generic {indexing_maps = [#map, #map, #map], iterator_types = ["parallel", "parallel"]} ins(%12, %21 : tensor<1x5xf32>, tensor<1x5xf32>) outs(%7 : tensor<1x5xf32>) { ^bb0(%in: f32, %in_12: f32, %out: f32): %27 = arith.subf %in, %in_12 : f32 linalg.yield %27 : f32 } -> tensor<1x5xf32> - debug.validate %22 {name = "sub2", metadata = "sub2"} : tensor<1x5xf32> + debug.validate %22 : tensor<1x5xf32> %23 = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel"]} ins(%22 : tensor<1x5xf32>) outs(%7 : tensor<1x5xf32>) { ^bb0(%in: f32, %out: f32): %27 = math.fpowi %in, %c2_i64 : f32, i64 linalg.yield %27 : f32 } -> tensor<1x5xf32> - debug.validate %23 {name = "sq", metadata = "sq"} : tensor<1x5xf32> + debug.validate %23 : tensor<1x5xf32> %24 = tensor.empty() : tensor<1xf32> %25 = linalg.fill ins(%cst : f32) outs(%24 : tensor<1xf32>) -> tensor<1xf32> %26 = linalg.generic {indexing_maps = [#map, #map2], iterator_types = ["parallel", "reduction"]} ins(%23 : tensor<1x5xf32>) outs(%25 : tensor<1xf32>) { @@ -115,7 +115,7 @@ module { %27 = arith.addf %in, %out : f32 linalg.yield %27 : f32 } -> tensor<1xf32> - debug.validate %26 {name = "sse", metadata = "sse"} : tensor<1xf32> + debug.validate %26 : tensor<1xf32> return %26, %21 : tensor<1xf32>, tensor<1x5xf32> } }