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

Significant deviation in Vector-Matrix Multiplication Function '.matmul()' #504

Open
cph924898127 opened this issue Dec 14, 2024 · 0 comments
Labels
Type: Question ❔ Question about implementation or some technical aspect

Comments

@cph924898127
Copy link

Hello, I'm trying to multiply a vector of length in_len with a matrix of size (in_len, out_len). However, I found the result deviates greatly when out_len exceeds a certain value.

It seems to be related to the polynomial modulus degree, because when I set in_len + out_len > poly_modulus_degree/2 + 1, the result obviously deviates from the expected value.

Could anyone provide an explanation for why this happens? Any help would be appreciated.

out_len = 4096 - 28*28 + 1:

    context = autoGenerateContext.setContextCKKS8192()
    w_in = 28
    h_in = 28
    out_len = 3312 + 1   # 4096 - 28*28 + 1

    arr = np.random.randn(w_in,h_in).flatten()
    enc = ts.ckks_vector(context,arr)

    matrix = np.random.randn(w_in*h_in,out_len)
    target = arr.__matmul__(matrix)
    print("target:")
    print(target)

    result = enc.matmul(matrix)

    dec = result.decrypt()
    print("decryption result:")
    print(dec)

    error = target - dec
    print("error:")
    print(error)
    print("total error:",sum(abs(error)))

target:
[-14.07203888 4.82855906 -27.37549179 ... 3.9712238 0.5551332 -38.08560303]
decrypted result:
[-14.085208958250046, 4.833021022357862, -27.40169106927703, ..., 3.9746487102434007, 0.5551590649822009, -38.12346041066118]
error:
[ 1.31700763e-02 -4.46196323e-03 2.61992818e-02 ... -3.42490817e-03
-2.58613957e-05 3.78573817e-02]
total error: 69.57947089867199

The deviation is around 0.1% from the expected value.

out_len = 4096 - 28*28 + 3:

target:
[ 24.97197688 21.67511144 -51.76814003 ... 50.76613363 -4.36328036 18.350263 ]
decrypted result:
[24.99639937582166, 21.69516700751244, -51.81767069280889 ... 50.81584699425353, -4.310624516117056, 17.573779554432228]
error:
[-0.0244225 -0.02005557 0.04953066 ... -0.04971336 -0.05265585 0.77648344]
total error: 71.16922327406094

The deviation is around 0.1% for results [0:4096], but over 1% for results [4097:], and it continues to deteriorate with a larger out_len.

@cph924898127 cph924898127 added the Type: Question ❔ Question about implementation or some technical aspect label Dec 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Question ❔ Question about implementation or some technical aspect
Projects
None yet
Development

No branches or pull requests

1 participant