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

[Optimization] Use scipy's eigs instead of numpy in lap_pe #5855

Merged
merged 8 commits into from
Aug 24, 2023
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions python/dgl/transforms/functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -3672,13 +3672,22 @@
L = sparse.eye(g.num_nodes()) - N * A * N

# select eigenvectors with smaller eigenvalues O(n + klogk)
EigVal, EigVec = np.linalg.eig(L.toarray())
max_freqs = min(n - 1, k)
kpartition_indices = np.argpartition(EigVal, max_freqs)[: max_freqs + 1]
topk_eigvals = EigVal[kpartition_indices]
topk_indices = kpartition_indices[topk_eigvals.argsort()][1:]
topk_EigVec = EigVec[:, topk_indices]
eigvals = F.tensor(EigVal[topk_indices], dtype=F.float32)

BarclayII marked this conversation as resolved.
Show resolved Hide resolved
if k + 1 < n - 1:
# Use scipy if k + 1 < n - 1 for memory efficiency.

Check warning on line 3677 in python/dgl/transforms/functional.py

View workflow job for this annotation

GitHub Actions / lintrunner

UFMT format

Run `lintrunner -a` to apply this patch.
EigVal, EigVec = scipy.sparse.linalg.eigs(L, k=k+1, which='SR', tol=1e-2)
topk_indices = EigVal.argsort()[1:]
topk_eigvals = EigVal[topk_indices].real # scipy may give complex
BarclayII marked this conversation as resolved.
Show resolved Hide resolved
BarclayII marked this conversation as resolved.
Show resolved Hide resolved
topk_EigVec = EigVec[:, topk_indices].real
else:
# Fallback to numpy since scipy.sparse do not support this case.
EigVal, EigVec = np.linalg.eig(L.toarray())
max_freqs = min(n - 1, k)
kpartition_indices = np.argpartition(EigVal, max_freqs)[: max_freqs + 1]
topk_eigvals = EigVal[kpartition_indices]
topk_indices = kpartition_indices[topk_eigvals.argsort()][1:]
topk_EigVec = EigVec[:, topk_indices]
eigvals = F.tensor(topk_eigvals, dtype=F.float32)
rudongyu marked this conversation as resolved.
Show resolved Hide resolved

# get random flip signs
rand_sign = 2 * (np.random.rand(max_freqs) > 0.5) - 1.0
Expand Down
Loading