Hi,
I’ve been trying to implement cosine distance in concrete, I have quantised the inputs and trying to run the pbs function
def cosine_distance(v1: np.ndarray[np.int32], v2: np.ndarray[np.int32] , precomputed_dot : np.int32) -> np.int32:
x = np.dot(v1, v2)
b = np.dot(v1, v1)
y = b * precomputed_dot
x = x * x * 100
return fhe.multivariate(lambda x, y: x // y)(x, y)
compiler = fhe.Compiler(cosine_distance, {"v1" : "encrypted" , "v2" : "clear" , "precomputed_dot" : "clear"})
inputset = [ (np.random.randint(-ma,ma,size=qlen) , np.random.randint(-ma,ma,size=qlen), np.random.randint(-ma,ma)) for _ in range(qlen) ]
circuit = compiler.compile(
inputset,
show_mlir = False,
)
circuit.keygen()
# since query is in clear, we can precompute the dot product
precomputed_dot = np.dot(qa,qa)
encrypted_vector = circuit.encrypt(new_v1,qa,precomputed_dot)
encrypted_result = circuit.run(encrypted_vector)
Unfortunately, the Compiler most of the time complains about the bit size overflowing, I’ve tried everything to make it fit within 16 bit. I really don’t know how to make this work. Also, to note, I am gonna be doing the square root operation after the decryption to make my life easier.