Skip to content

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

@RogueServitor-495

Description

@RogueServitor-495

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type: Question ❔Question about implementation or some technical aspect

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions