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

Matrix multiplication termination #509

Open
imtiyazuddin opened this issue Jul 9, 2024 · 2 comments
Open

Matrix multiplication termination #509

imtiyazuddin opened this issue Jul 9, 2024 · 2 comments

Comments

@imtiyazuddin
Copy link

imtiyazuddin commented Jul 9, 2024

This simple code of matrix multiplication is taking forever to run, what could be the issue?

import time

s = time.time()

sal = torch.Tensor(1000, 50176)
sal = crypten.cryptensor(sal)
i=1000
j=50
k=50176

for ii in range(i):
    for jj in range(j):
        for kk in range(k):
            sal[ii][kk] += pr[jj][ii] * m[jj][kk]

e = time.time()
print("time taken: ", (e-s))

The dimensions are correct. but taking too much time and never finished

@imtiyazuddin
Copy link
Author

is there any way to parallelize code to make it run faster?

@knottb
Copy link
Contributor

knottb commented Aug 1, 2024

There are a number of reasons this is slow:

  1. You are doing all multiplications sequentially. Each time a value is multiplied here, you have to wait for the communicator to coordinate between processes. Avoid this by doing a matrix multiplication rather than individual multiplies:

sal += pr.matmul(m)

  1. It's possible that due to the size of the matrices, iterating through the indices could cause cache misses, requiring your processors to swap out caches to memory.

  2. The matrix itself isn't tiny. This is 1000 * 50 * 50k multiplies = 2.5 billion multiplies. This is all done sequentially and makes 2.5 billion calls to the communicator. Use matmul instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants