diff --git a/benchmarks/124_minus_x_tensor.py b/benchmarks/124_minus_x_tensor.py new file mode 100644 index 000000000..5b5d035f9 --- /dev/null +++ b/benchmarks/124_minus_x_tensor.py @@ -0,0 +1,46 @@ +# Target: 124 - x (Tensor) + +import numpy as np + +import concrete.numpy as hnp + + +def main(): + def function_to_compile(x): + return 124 - x + + x = hnp.EncryptedTensor(hnp.UnsignedInteger(6), shape=(3,)) + + inputset = [ + (np.array([36, 50, 24]),), + (np.array([41, 60, 51]),), + (np.array([25, 31, 24]),), + (np.array([34, 47, 27]),), + ] + + # Measure: Compilation Time (ms) + engine = hnp.compile_numpy_function(function_to_compile, {"x": x}, inputset) + # Measure: End + + inputs = [] + labels = [] + for _ in range(4): + sample_x = np.random.randint(0, 2 ** 6, size=(3,)) + + inputs.append([sample_x]) + labels.append(function_to_compile(*inputs[-1])) + + correct = 0 + for input_i, label_i in zip(inputs, labels): + # Measure: Evaluation Time (ms) + result_i = engine.run(*input_i) + # Measure: End + + if result_i == label_i: + correct += 1 + + # Measure: Accuracy (%) = (correct / len(inputs)) * 100 + + +if __name__ == "__main__": + main() diff --git a/benchmarks/x_minus_1_2_3.py b/benchmarks/x_minus_1_2_3.py new file mode 100644 index 000000000..7e692b735 --- /dev/null +++ b/benchmarks/x_minus_1_2_3.py @@ -0,0 +1,46 @@ +# Target: x - [1, 2, 3] + +import numpy as np + +import concrete.numpy as hnp + + +def main(): + def function_to_compile(x): + return x - np.array([1, 2, 3]) + + x = hnp.EncryptedTensor(hnp.UnsignedInteger(3), shape=(3,)) + + inputset = [ + (np.array([6, 2, 4]),), + (np.array([1, 3, 5]),), + (np.array([5, 7, 2]),), + (np.array([1, 7, 7]),), + ] + + # Measure: Compilation Time (ms) + engine = hnp.compile_numpy_function(function_to_compile, {"x": x}, inputset) + # Measure: End + + inputs = [] + labels = [] + for _ in range(4): + sample_x = np.random.randint(3, 2 ** 3, size=(3,)) + + inputs.append([sample_x]) + labels.append(function_to_compile(*inputs[-1])) + + correct = 0 + for input_i, label_i in zip(inputs, labels): + # Measure: Evaluation Time (ms) + result_i = engine.run(*input_i) + # Measure: End + + if result_i == label_i: + correct += 1 + + # Measure: Accuracy (%) = (correct / len(inputs)) * 100 + + +if __name__ == "__main__": + main() diff --git a/benchmarks/x_minus_1_2_3_broadcasted.py b/benchmarks/x_minus_1_2_3_broadcasted.py new file mode 100644 index 000000000..b67e1d138 --- /dev/null +++ b/benchmarks/x_minus_1_2_3_broadcasted.py @@ -0,0 +1,46 @@ +# Target: x - [1, 2, 3] (Broadcasted) + +import numpy as np + +import concrete.numpy as hnp + + +def main(): + def function_to_compile(x): + return x - np.array([1, 2, 3]) + + x = hnp.EncryptedTensor(hnp.UnsignedInteger(3), shape=(2, 3)) + + inputset = [ + (np.array([[4, 7, 7], [6, 2, 4]]),), + (np.array([[6, 2, 4], [1, 3, 1]]),), + (np.array([[6, 2, 4], [5, 7, 5]]),), + (np.array([[5, 7, 5], [4, 7, 7]]),), + ] + + # Measure: Compilation Time (ms) + engine = hnp.compile_numpy_function(function_to_compile, {"x": x}, inputset) + # Measure: End + + inputs = [] + labels = [] + for _ in range(4): + sample_x = np.random.randint(3, 2 ** 3, size=(2, 3)) + + inputs.append([sample_x]) + labels.append(function_to_compile(*inputs[-1])) + + correct = 0 + for input_i, label_i in zip(inputs, labels): + # Measure: Evaluation Time (ms) + result_i = engine.run(*input_i) + # Measure: End + + if result_i == label_i: + correct += 1 + + # Measure: Accuracy (%) = (correct / len(inputs)) * 100 + + +if __name__ == "__main__": + main() diff --git a/benchmarks/x_minus_24_tensor.py b/benchmarks/x_minus_24_tensor.py new file mode 100644 index 000000000..f7ec198cb --- /dev/null +++ b/benchmarks/x_minus_24_tensor.py @@ -0,0 +1,46 @@ +# Target: x - 24 (Tensor) + +import numpy as np + +import concrete.numpy as hnp + + +def main(): + def function_to_compile(x): + return x - 24 + + x = hnp.EncryptedTensor(hnp.UnsignedInteger(6), shape=(3,)) + + inputset = [ + (np.array([36, 50, 24]),), + (np.array([41, 60, 51]),), + (np.array([25, 31, 24]),), + (np.array([34, 47, 27]),), + ] + + # Measure: Compilation Time (ms) + engine = hnp.compile_numpy_function(function_to_compile, {"x": x}, inputset) + # Measure: End + + inputs = [] + labels = [] + for _ in range(4): + sample_x = np.random.randint(24, 2 ** 6, size=(3,)) + + inputs.append([sample_x]) + labels.append(function_to_compile(*inputs[-1])) + + correct = 0 + for input_i, label_i in zip(inputs, labels): + # Measure: Evaluation Time (ms) + result_i = engine.run(*input_i) + # Measure: End + + if result_i == label_i: + correct += 1 + + # Measure: Accuracy (%) = (correct / len(inputs)) * 100 + + +if __name__ == "__main__": + main() diff --git a/benchmarks/x_minus_y_broadcasted_tensors.py b/benchmarks/x_minus_y_broadcasted_tensors.py new file mode 100644 index 000000000..ea6408350 --- /dev/null +++ b/benchmarks/x_minus_y_broadcasted_tensors.py @@ -0,0 +1,48 @@ +# Target: x - y (Broadcasted Tensors) + +import numpy as np + +import concrete.numpy as hnp + + +def main(): + def function_to_compile(x, y): + return x - y + + x = hnp.EncryptedTensor(hnp.UnsignedInteger(3), shape=(3,)) + y = hnp.EncryptedTensor(hnp.UnsignedInteger(3), shape=(2, 3)) + + inputset = [ + (np.array([6, 2, 4]), np.array([[5, 1, 3], [0, 0, 4]])), + (np.array([1, 3, 1]), np.array([[0, 3, 1], [1, 2, 1]])), + (np.array([5, 1, 2]), np.array([[5, 0, 2], [2, 1, 1]])), + (np.array([0, 7, 7]), np.array([[0, 5, 1], [0, 7, 2]])), + ] + + # Measure: Compilation Time (ms) + engine = hnp.compile_numpy_function(function_to_compile, {"x": x, "y": y}, inputset) + # Measure: End + + inputs = [] + labels = [] + for _ in range(4): + sample_x = np.random.randint(4, 2 ** 3, size=(3,)) + sample_y = np.random.randint(0, 5, size=(2, 3)) + + inputs.append([sample_x, sample_y]) + labels.append(function_to_compile(*inputs[-1])) + + correct = 0 + for input_i, label_i in zip(inputs, labels): + # Measure: Evaluation Time (ms) + result_i = engine.run(*input_i) + # Measure: End + + if result_i == label_i: + correct += 1 + + # Measure: Accuracy (%) = (correct / len(inputs)) * 100 + + +if __name__ == "__main__": + main() diff --git a/benchmarks/x_minus_y_tensor_and_scalar.py b/benchmarks/x_minus_y_tensor_and_scalar.py new file mode 100644 index 000000000..3ab299c65 --- /dev/null +++ b/benchmarks/x_minus_y_tensor_and_scalar.py @@ -0,0 +1,50 @@ +# Target: x - y (Tensor & Scalar) + +import random + +import numpy as np + +import concrete.numpy as hnp + + +def main(): + def function_to_compile(x, y): + return x - y + + x = hnp.EncryptedTensor(hnp.UnsignedInteger(3), shape=(3,)) + y = hnp.EncryptedScalar(hnp.UnsignedInteger(3)) + + inputset = [ + (np.array([6, 2, 4]), 2), + (np.array([1, 3, 1]), 1), + (np.array([5, 4, 7]), 4), + (np.array([5, 7, 6]), 5), + ] + + # Measure: Compilation Time (ms) + engine = hnp.compile_numpy_function(function_to_compile, {"x": x, "y": y}, inputset) + # Measure: End + + inputs = [] + labels = [] + for _ in range(4): + sample_x = np.random.randint(3, 2 ** 3, size=(3,)) + sample_y = random.randint(0, 3) + + inputs.append([sample_x, sample_y]) + labels.append(function_to_compile(*inputs[-1])) + + correct = 0 + for input_i, label_i in zip(inputs, labels): + # Measure: Evaluation Time (ms) + result_i = engine.run(*input_i) + # Measure: End + + if result_i == label_i: + correct += 1 + + # Measure: Accuracy (%) = (correct / len(inputs)) * 100 + + +if __name__ == "__main__": + main() diff --git a/benchmarks/x_minus_y_tensors.py b/benchmarks/x_minus_y_tensors.py new file mode 100644 index 000000000..6c681e0bc --- /dev/null +++ b/benchmarks/x_minus_y_tensors.py @@ -0,0 +1,48 @@ +# Target: x - y (Tensors) + +import numpy as np + +import concrete.numpy as hnp + + +def main(): + def function_to_compile(x, y): + return x - y + + x = hnp.EncryptedTensor(hnp.UnsignedInteger(3), shape=(3,)) + y = hnp.EncryptedTensor(hnp.UnsignedInteger(3), shape=(3,)) + + inputset = [ + (np.array([6, 2, 4]), np.array([4, 1, 2])), + (np.array([1, 3, 1]), np.array([1, 1, 0])), + (np.array([5, 1, 2]), np.array([4, 1, 1])), + (np.array([0, 7, 7]), np.array([0, 7, 0])), + ] + + # Measure: Compilation Time (ms) + engine = hnp.compile_numpy_function(function_to_compile, {"x": x, "y": y}, inputset) + # Measure: End + + inputs = [] + labels = [] + for _ in range(4): + sample_x = np.random.randint(3, 2 ** 3, size=(3,)) + sample_y = np.random.randint(0, 4, size=(3,)) + + inputs.append([sample_x, sample_y]) + labels.append(function_to_compile(*inputs[-1])) + + correct = 0 + for input_i, label_i in zip(inputs, labels): + # Measure: Evaluation Time (ms) + result_i = engine.run(*input_i) + # Measure: End + + if result_i == label_i: + correct += 1 + + # Measure: Accuracy (%) = (correct / len(inputs)) * 100 + + +if __name__ == "__main__": + main() diff --git a/benchmarks/x_plus_1_2_3.py b/benchmarks/x_plus_1_2_3.py new file mode 100644 index 000000000..de73fda65 --- /dev/null +++ b/benchmarks/x_plus_1_2_3.py @@ -0,0 +1,46 @@ +# Target: x + [1, 2, 3] + +import numpy as np + +import concrete.numpy as hnp + + +def main(): + def function_to_compile(x): + return x + np.array([1, 2, 3]) + + x = hnp.EncryptedTensor(hnp.UnsignedInteger(3), shape=(3,)) + + inputset = [ + (np.array([6, 2, 4]),), + (np.array([1, 3, 1]),), + (np.array([5, 1, 2]),), + (np.array([0, 7, 7]),), + ] + + # Measure: Compilation Time (ms) + engine = hnp.compile_numpy_function(function_to_compile, {"x": x}, inputset) + # Measure: End + + inputs = [] + labels = [] + for _ in range(4): + sample_x = np.random.randint(0, 2 ** 3, size=(3,)) + + inputs.append([sample_x]) + labels.append(function_to_compile(*inputs[-1])) + + correct = 0 + for input_i, label_i in zip(inputs, labels): + # Measure: Evaluation Time (ms) + result_i = engine.run(*input_i) + # Measure: End + + if result_i == label_i: + correct += 1 + + # Measure: Accuracy (%) = (correct / len(inputs)) * 100 + + +if __name__ == "__main__": + main() diff --git a/benchmarks/x_plus_1_2_3_broadcasted.py b/benchmarks/x_plus_1_2_3_broadcasted.py new file mode 100644 index 000000000..9826714f4 --- /dev/null +++ b/benchmarks/x_plus_1_2_3_broadcasted.py @@ -0,0 +1,46 @@ +# Target: x + [1, 2, 3] (Broadcasted) + +import numpy as np + +import concrete.numpy as hnp + + +def main(): + def function_to_compile(x): + return x + np.array([1, 2, 3]) + + x = hnp.EncryptedTensor(hnp.UnsignedInteger(3), shape=(2, 3)) + + inputset = [ + (np.array([[0, 7, 7], [6, 2, 4]]),), + (np.array([[6, 2, 4], [1, 3, 1]]),), + (np.array([[6, 2, 4], [5, 1, 2]]),), + (np.array([[5, 1, 2], [0, 7, 7]]),), + ] + + # Measure: Compilation Time (ms) + engine = hnp.compile_numpy_function(function_to_compile, {"x": x}, inputset) + # Measure: End + + inputs = [] + labels = [] + for _ in range(4): + sample_x = np.random.randint(0, 2 ** 3, size=(2, 3)) + + inputs.append([sample_x]) + labels.append(function_to_compile(*inputs[-1])) + + correct = 0 + for input_i, label_i in zip(inputs, labels): + # Measure: Evaluation Time (ms) + result_i = engine.run(*input_i) + # Measure: End + + if result_i == label_i: + correct += 1 + + # Measure: Accuracy (%) = (correct / len(inputs)) * 100 + + +if __name__ == "__main__": + main() diff --git a/benchmarks/x_plus_42_tensor.py b/benchmarks/x_plus_42_tensor.py new file mode 100644 index 000000000..1829304e8 --- /dev/null +++ b/benchmarks/x_plus_42_tensor.py @@ -0,0 +1,46 @@ +# Target: x + 42 (Tensor) + +import numpy as np + +import concrete.numpy as hnp + + +def main(): + def function_to_compile(x): + return x + 42 + + x = hnp.EncryptedTensor(hnp.UnsignedInteger(3), shape=(3,)) + + inputset = [ + (np.array([6, 2, 4]),), + (np.array([1, 3, 1]),), + (np.array([5, 1, 2]),), + (np.array([0, 7, 7]),), + ] + + # Measure: Compilation Time (ms) + engine = hnp.compile_numpy_function(function_to_compile, {"x": x}, inputset) + # Measure: End + + inputs = [] + labels = [] + for _ in range(4): + sample_x = np.random.randint(0, 2 ** 3, size=(3,)) + + inputs.append([sample_x]) + labels.append(function_to_compile(*inputs[-1])) + + correct = 0 + for input_i, label_i in zip(inputs, labels): + # Measure: Evaluation Time (ms) + result_i = engine.run(*input_i) + # Measure: End + + if result_i == label_i: + correct += 1 + + # Measure: Accuracy (%) = (correct / len(inputs)) * 100 + + +if __name__ == "__main__": + main() diff --git a/benchmarks/x_plus_y_broadcasted_tensors.py b/benchmarks/x_plus_y_broadcasted_tensors.py new file mode 100644 index 000000000..c13aea795 --- /dev/null +++ b/benchmarks/x_plus_y_broadcasted_tensors.py @@ -0,0 +1,48 @@ +# Target: x + y (Broadcasted Tensors) + +import numpy as np + +import concrete.numpy as hnp + + +def main(): + def function_to_compile(x, y): + return x + y + + x = hnp.EncryptedTensor(hnp.UnsignedInteger(3), shape=(3,)) + y = hnp.EncryptedTensor(hnp.UnsignedInteger(3), shape=(2, 3)) + + inputset = [ + (np.array([6, 2, 4]), np.array([[5, 1, 2], [0, 7, 7]])), + (np.array([1, 3, 1]), np.array([[0, 7, 7], [6, 2, 4]])), + (np.array([5, 1, 2]), np.array([[6, 2, 4], [1, 3, 1]])), + (np.array([0, 7, 7]), np.array([[1, 3, 1], [5, 1, 2]])), + ] + + # Measure: Compilation Time (ms) + engine = hnp.compile_numpy_function(function_to_compile, {"x": x, "y": y}, inputset) + # Measure: End + + inputs = [] + labels = [] + for _ in range(4): + sample_x = np.random.randint(0, 2 ** 3, size=(3,)) + sample_y = np.random.randint(0, 2 ** 3, size=(2, 3)) + + inputs.append([sample_x, sample_y]) + labels.append(function_to_compile(*inputs[-1])) + + correct = 0 + for input_i, label_i in zip(inputs, labels): + # Measure: Evaluation Time (ms) + result_i = engine.run(*input_i) + # Measure: End + + if result_i == label_i: + correct += 1 + + # Measure: Accuracy (%) = (correct / len(inputs)) * 100 + + +if __name__ == "__main__": + main() diff --git a/benchmarks/x_plus_y_tensor_and_scalar.py b/benchmarks/x_plus_y_tensor_and_scalar.py new file mode 100644 index 000000000..e314a99e9 --- /dev/null +++ b/benchmarks/x_plus_y_tensor_and_scalar.py @@ -0,0 +1,50 @@ +# Target: x + y (Tensor & Scalar) + +import random + +import numpy as np + +import concrete.numpy as hnp + + +def main(): + def function_to_compile(x, y): + return x + y + + x = hnp.EncryptedTensor(hnp.UnsignedInteger(3), shape=(3,)) + y = hnp.EncryptedScalar(hnp.UnsignedInteger(3)) + + inputset = [ + (np.array([6, 2, 4]), 4), + (np.array([1, 3, 1]), 1), + (np.array([5, 1, 2]), 2), + (np.array([0, 7, 7]), 5), + ] + + # Measure: Compilation Time (ms) + engine = hnp.compile_numpy_function(function_to_compile, {"x": x, "y": y}, inputset) + # Measure: End + + inputs = [] + labels = [] + for _ in range(4): + sample_x = np.random.randint(0, 2 ** 3, size=(3,)) + sample_y = random.randint(0, 2 ** 3 - 1) + + inputs.append([sample_x, sample_y]) + labels.append(function_to_compile(*inputs[-1])) + + correct = 0 + for input_i, label_i in zip(inputs, labels): + # Measure: Evaluation Time (ms) + result_i = engine.run(*input_i) + # Measure: End + + if result_i == label_i: + correct += 1 + + # Measure: Accuracy (%) = (correct / len(inputs)) * 100 + + +if __name__ == "__main__": + main() diff --git a/benchmarks/x_plus_y_tensors.py b/benchmarks/x_plus_y_tensors.py new file mode 100644 index 000000000..16718cc5e --- /dev/null +++ b/benchmarks/x_plus_y_tensors.py @@ -0,0 +1,48 @@ +# Target: x + y (Tensors) + +import numpy as np + +import concrete.numpy as hnp + + +def main(): + def function_to_compile(x, y): + return x + y + + x = hnp.EncryptedTensor(hnp.UnsignedInteger(3), shape=(3,)) + y = hnp.EncryptedTensor(hnp.UnsignedInteger(3), shape=(3,)) + + inputset = [ + (np.array([6, 2, 4]), np.array([0, 7, 7])), + (np.array([1, 3, 1]), np.array([6, 2, 4])), + (np.array([5, 1, 2]), np.array([1, 3, 1])), + (np.array([0, 7, 7]), np.array([5, 1, 2])), + ] + + # Measure: Compilation Time (ms) + engine = hnp.compile_numpy_function(function_to_compile, {"x": x, "y": y}, inputset) + # Measure: End + + inputs = [] + labels = [] + for _ in range(4): + sample_x = np.random.randint(0, 2 ** 3, size=(3,)) + sample_y = np.random.randint(0, 2 ** 3, size=(3,)) + + inputs.append([sample_x, sample_y]) + labels.append(function_to_compile(*inputs[-1])) + + correct = 0 + for input_i, label_i in zip(inputs, labels): + # Measure: Evaluation Time (ms) + result_i = engine.run(*input_i) + # Measure: End + + if result_i == label_i: + correct += 1 + + # Measure: Accuracy (%) = (correct / len(inputs)) * 100 + + +if __name__ == "__main__": + main() diff --git a/benchmarks/x_times_1_2_3.py b/benchmarks/x_times_1_2_3.py new file mode 100644 index 000000000..90812272d --- /dev/null +++ b/benchmarks/x_times_1_2_3.py @@ -0,0 +1,46 @@ +# Target: x * [1, 2, 3] + +import numpy as np + +import concrete.numpy as hnp + + +def main(): + def function_to_compile(x): + return x * np.array([1, 2, 3]) + + x = hnp.EncryptedTensor(hnp.UnsignedInteger(3), shape=(3,)) + + inputset = [ + (np.array([6, 2, 4]),), + (np.array([1, 3, 1]),), + (np.array([5, 1, 2]),), + (np.array([0, 7, 7]),), + ] + + # Measure: Compilation Time (ms) + engine = hnp.compile_numpy_function(function_to_compile, {"x": x}, inputset) + # Measure: End + + inputs = [] + labels = [] + for _ in range(4): + sample_x = np.random.randint(0, 2 ** 3, size=(3,)) + + inputs.append([sample_x]) + labels.append(function_to_compile(*inputs[-1])) + + correct = 0 + for input_i, label_i in zip(inputs, labels): + # Measure: Evaluation Time (ms) + result_i = engine.run(*input_i) + # Measure: End + + if result_i == label_i: + correct += 1 + + # Measure: Accuracy (%) = (correct / len(inputs)) * 100 + + +if __name__ == "__main__": + main() diff --git a/benchmarks/x_times_1_2_3_broadcasted.py b/benchmarks/x_times_1_2_3_broadcasted.py new file mode 100644 index 000000000..f6b8cb665 --- /dev/null +++ b/benchmarks/x_times_1_2_3_broadcasted.py @@ -0,0 +1,46 @@ +# Target: x * [1, 2, 3] (Broadcasted) + +import numpy as np + +import concrete.numpy as hnp + + +def main(): + def function_to_compile(x): + return x * np.array([1, 2, 3]) + + x = hnp.EncryptedTensor(hnp.UnsignedInteger(3), shape=(2, 3)) + + inputset = [ + (np.array([[0, 7, 7], [6, 2, 4]]),), + (np.array([[6, 2, 4], [1, 3, 1]]),), + (np.array([[6, 2, 4], [5, 1, 2]]),), + (np.array([[5, 1, 2], [0, 7, 7]]),), + ] + + # Measure: Compilation Time (ms) + engine = hnp.compile_numpy_function(function_to_compile, {"x": x}, inputset) + # Measure: End + + inputs = [] + labels = [] + for _ in range(4): + sample_x = np.random.randint(0, 2 ** 3, size=(2, 3)) + + inputs.append([sample_x]) + labels.append(function_to_compile(*inputs[-1])) + + correct = 0 + for input_i, label_i in zip(inputs, labels): + # Measure: Evaluation Time (ms) + result_i = engine.run(*input_i) + # Measure: End + + if result_i == label_i: + correct += 1 + + # Measure: Accuracy (%) = (correct / len(inputs)) * 100 + + +if __name__ == "__main__": + main() diff --git a/benchmarks/x_times_7_tensor.py b/benchmarks/x_times_7_tensor.py new file mode 100644 index 000000000..c1ad87b73 --- /dev/null +++ b/benchmarks/x_times_7_tensor.py @@ -0,0 +1,46 @@ +# Target: x * 7 (Tensor) + +import numpy as np + +import concrete.numpy as hnp + + +def main(): + def function_to_compile(x): + return x * 7 + + x = hnp.EncryptedTensor(hnp.UnsignedInteger(3), shape=(3,)) + + inputset = [ + (np.array([6, 2, 4]),), + (np.array([1, 3, 1]),), + (np.array([5, 1, 2]),), + (np.array([0, 7, 7]),), + ] + + # Measure: Compilation Time (ms) + engine = hnp.compile_numpy_function(function_to_compile, {"x": x}, inputset) + # Measure: End + + inputs = [] + labels = [] + for _ in range(4): + sample_x = np.random.randint(0, 2 ** 3, size=(3,)) + + inputs.append([sample_x]) + labels.append(function_to_compile(*inputs[-1])) + + correct = 0 + for input_i, label_i in zip(inputs, labels): + # Measure: Evaluation Time (ms) + result_i = engine.run(*input_i) + # Measure: End + + if result_i == label_i: + correct += 1 + + # Measure: Accuracy (%) = (correct / len(inputs)) * 100 + + +if __name__ == "__main__": + main() diff --git a/benchmarks/x_times_y_broadcasted_tensors.py b/benchmarks/x_times_y_broadcasted_tensors.py new file mode 100644 index 000000000..89a6bf8df --- /dev/null +++ b/benchmarks/x_times_y_broadcasted_tensors.py @@ -0,0 +1,48 @@ +# Target: x * y (Broadcasted Tensors) + +import numpy as np + +import concrete.numpy as hnp + + +def main(): + def function_to_compile(x, y): + return x * y + + x = hnp.EncryptedTensor(hnp.UnsignedInteger(3), shape=(3,)) + y = hnp.EncryptedTensor(hnp.UnsignedInteger(3), shape=(2, 3)) + + inputset = [ + (np.array([6, 2, 4]), np.array([[5, 1, 2], [0, 7, 7]])), + (np.array([1, 3, 1]), np.array([[0, 7, 7], [6, 2, 4]])), + (np.array([5, 1, 2]), np.array([[6, 2, 4], [1, 3, 1]])), + (np.array([0, 7, 7]), np.array([[1, 3, 1], [5, 1, 2]])), + ] + + # Measure: Compilation Time (ms) + engine = hnp.compile_numpy_function(function_to_compile, {"x": x, "y": y}, inputset) + # Measure: End + + inputs = [] + labels = [] + for _ in range(4): + sample_x = np.random.randint(0, 2 ** 3, size=(3,)) + sample_y = np.random.randint(0, 2 ** 3, size=(2, 3)) + + inputs.append([sample_x, sample_y]) + labels.append(function_to_compile(*inputs[-1])) + + correct = 0 + for input_i, label_i in zip(inputs, labels): + # Measure: Evaluation Time (ms) + result_i = engine.run(*input_i) + # Measure: End + + if result_i == label_i: + correct += 1 + + # Measure: Accuracy (%) = (correct / len(inputs)) * 100 + + +if __name__ == "__main__": + main() diff --git a/benchmarks/x_times_y_tensor_and_scalar.py b/benchmarks/x_times_y_tensor_and_scalar.py new file mode 100644 index 000000000..4a13b9947 --- /dev/null +++ b/benchmarks/x_times_y_tensor_and_scalar.py @@ -0,0 +1,50 @@ +# Target: x * y (Tensor & Scalar) + +import random + +import numpy as np + +import concrete.numpy as hnp + + +def main(): + def function_to_compile(x, y): + return x * y + + x = hnp.EncryptedTensor(hnp.UnsignedInteger(3), shape=(3,)) + y = hnp.EncryptedScalar(hnp.UnsignedInteger(3)) + + inputset = [ + (np.array([6, 2, 4]), 4), + (np.array([1, 3, 1]), 1), + (np.array([5, 1, 2]), 2), + (np.array([0, 7, 7]), 5), + ] + + # Measure: Compilation Time (ms) + engine = hnp.compile_numpy_function(function_to_compile, {"x": x, "y": y}, inputset) + # Measure: End + + inputs = [] + labels = [] + for _ in range(4): + sample_x = np.random.randint(0, 2 ** 3, size=(3,)) + sample_y = random.randint(0, 5) + + inputs.append([sample_x, sample_y]) + labels.append(function_to_compile(*inputs[-1])) + + correct = 0 + for input_i, label_i in zip(inputs, labels): + # Measure: Evaluation Time (ms) + result_i = engine.run(*input_i) + # Measure: End + + if result_i == label_i: + correct += 1 + + # Measure: Accuracy (%) = (correct / len(inputs)) * 100 + + +if __name__ == "__main__": + main() diff --git a/benchmarks/x_times_y_tensors.py b/benchmarks/x_times_y_tensors.py new file mode 100644 index 000000000..118a5d3d9 --- /dev/null +++ b/benchmarks/x_times_y_tensors.py @@ -0,0 +1,48 @@ +# Target: x * y (Tensors) + +import numpy as np + +import concrete.numpy as hnp + + +def main(): + def function_to_compile(x, y): + return x * y + + x = hnp.EncryptedTensor(hnp.UnsignedInteger(3), shape=(3,)) + y = hnp.EncryptedTensor(hnp.UnsignedInteger(3), shape=(3,)) + + inputset = [ + (np.array([6, 2, 4]), np.array([0, 7, 7])), + (np.array([1, 3, 1]), np.array([6, 2, 4])), + (np.array([5, 1, 2]), np.array([1, 3, 1])), + (np.array([0, 7, 7]), np.array([5, 1, 2])), + ] + + # Measure: Compilation Time (ms) + engine = hnp.compile_numpy_function(function_to_compile, {"x": x, "y": y}, inputset) + # Measure: End + + inputs = [] + labels = [] + for _ in range(4): + sample_x = np.random.randint(0, 2 ** 3, size=(3,)) + sample_y = np.random.randint(0, 2 ** 3, size=(3,)) + + inputs.append([sample_x, sample_y]) + labels.append(function_to_compile(*inputs[-1])) + + correct = 0 + for input_i, label_i in zip(inputs, labels): + # Measure: Evaluation Time (ms) + result_i = engine.run(*input_i) + # Measure: End + + if result_i == label_i: + correct += 1 + + # Measure: Accuracy (%) = (correct / len(inputs)) * 100 + + +if __name__ == "__main__": + main()