Skip to content
This repository has been archived by the owner on Apr 29, 2024. It is now read-only.

np.dot between matrices results in compilation error #82

Open
sbhamad opened this issue Feb 6, 2023 · 1 comment
Open

np.dot between matrices results in compilation error #82

sbhamad opened this issue Feb 6, 2023 · 1 comment

Comments

@sbhamad
Copy link

sbhamad commented Feb 6, 2023

error: 'FHELinalg.dot_eint_int' op operand #0 must be , but got 'tensor<3x4x!FHE.eint<12>>'
loc("-":5:10): error: 'FHELinalg.dot_eint_int' op operand #0 must be , but got 'tensor<3x4x!FHE.eint<12>>'
Traceback (most recent call last):
  File "/Users/sbhamad/github.com/sbhamad/poetry-demo/poetry_demo/main.py", line 79, in <module>
    circuit = compiler.compile(inputset)
  File "/Users/sbhamad/Library/Caches/pypoetry/virtualenvs/poetry-demo-7E09WmKy-py3.9/lib/python3.9/site-packages/concrete/numpy/compilation/compiler.py", line 515, in compile
    circuit = Circuit(self.graph, mlir, self.configuration)
  File "/Users/sbhamad/Library/Caches/pypoetry/virtualenvs/poetry-demo-7E09WmKy-py3.9/lib/python3.9/site-packages/concrete/numpy/compilation/circuit.py", line 55, in __init__
    self.server = Server.create(mlir, input_signs, output_signs, self.configuration)
  File "/Users/sbhamad/Library/Caches/pypoetry/virtualenvs/poetry-demo-7E09WmKy-py3.9/lib/python3.9/site-packages/concrete/numpy/compilation/server.py", line 149, in create
    compilation_result = support.compile(mlir, options)
  File "/Users/sbhamad/Library/Caches/pypoetry/virtualenvs/poetry-demo-7E09WmKy-py3.9/lib/python3.9/site-packages/concrete/compiler/library_support.py", line 155, in compile
    self.cpp().compile(mlir_program, options.cpp())
RuntimeError: Caught an unknown exception!

im trying to simply multiply w by i then add b to the resultant numpy ndarray. where i is encrypted.

#!/usr/bin/env python

import concrete.numpy as cnp
import numpy as np
import math

n_bits = 5

inputs = [
    [1.0, 2.0, 3.0, 2.5],
    [2.0, 5.0, -1.0, 2.0],
    [-1.5, 2.7, 3.3, -0.8],
]
weights = [
    [0.2, 0.8, -0.5, 1.0],
    [0.5, -0.91, 0.26, -0.5],
    [-0.26, -0.27, 0.17, 0.87],
]
biases = [2.0, 3.0, 0.5]
layer_outputs = np.dot(inputs, np.array(weights).T) + biases
print(np.array(inputs).shape)
print(np.array(weights).T.shape)

print(np.array(biases).shape)


print(layer_outputs)


def quantize_matrix(matrix):
    # Set the number of bits over which data will be quantized

    max_X = np.max(matrix)
    # Output: 0.9507
    min_X = np.min(matrix)
    # Output: 0.0581

    max_q_value = 2**n_bits - 1
    # Output: 127
    range = max_X - min_X
    # Output: 0.8926
    scale = range / max_q_value
    # Output: 0.2975
    Zp = np.round((-min_X * max_q_value) / range)
    # Output: 0
    q_X = np.round(matrix / scale) + Zp
    print("quantized matrix is : ", np.rint(q_X).astype(np.int64))
    return np.rint(q_X).astype(np.int64)


def multiply_weight_with_encrypted_input(i, w, b):
    return np.dot(i, w) + b
    # return


compiler = cnp.Compiler(
    multiply_weight_with_encrypted_input,
    {"i": "encrypted", "w": "clear", "b": "clear"},
)

q_inputs = quantize_matrix(inputs)
q_weights_transposed = quantize_matrix(weights).T
q_biases = quantize_matrix(biases)

print(q_inputs.shape, q_weights_transposed.shape, q_biases.shape)


inputset = [
    (
        np.random.randint(0, 2**n_bits, size=q_inputs.shape),
        np.random.randint(0, 2**n_bits, size=q_weights_transposed.shape),
        np.random.randint(0, 2**n_bits, size=q_biases.shape),
    )
    for _ in range(30)
]


# print("inputset looks like this:::::: ", inputset)
circuit = compiler.compile(inputset)


result = circuit.encrypt_run_decrypt(q_inputs, q_weights_transposed, q_biases)
print("homomorphically evaluated final result is: ", result)
@umut-sahin
Copy link
Collaborator

Dot is not fully supported at the moment, but according to the documentation of np.dot:

  • If both a and b are 2-D arrays, it is matrix multiplication, but using matmul or a @ b is preferred.

And both of them are supported, so please use them instead.

@umut-sahin umut-sahin changed the title Release v0.10 np.dot between matrices results in compilation error Feb 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants