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

Graph partition based on balance_edge #309

Merged
merged 3 commits into from
Feb 10, 2023
Merged
Changes from all 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
26 changes: 20 additions & 6 deletions torch_sparse/metis.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from typing import Tuple, Optional
from typing import Optional, Tuple

import torch
from torch_sparse.tensor import SparseTensor
from torch import Tensor

from torch_sparse.permute import permute
from torch_sparse.tensor import SparseTensor


def weight2metis(weight: torch.Tensor) -> Optional[torch.Tensor]:
def weight2metis(weight: Tensor) -> Optional[Tensor]:
sorted_weight = weight.sort()[0]
diff = sorted_weight[1:] - sorted_weight[:-1]
if diff.sum() == 0:
Expand All @@ -20,16 +22,24 @@ def weight2metis(weight: torch.Tensor) -> Optional[torch.Tensor]:


def partition(
src: SparseTensor, num_parts: int, recursive: bool = False,
weighted: bool = False, node_weight: Optional[torch.Tensor] = None
) -> Tuple[SparseTensor, torch.Tensor, torch.Tensor]:
src: SparseTensor,
num_parts: int,
recursive: bool = False,
weighted: bool = False,
node_weight: Optional[Tensor] = None,
balance_edge: bool = False,
) -> Tuple[SparseTensor, Tensor, Tensor]:

assert num_parts >= 1
if num_parts == 1:
partptr = torch.tensor([0, src.size(0)], device=src.device())
perm = torch.arange(src.size(0), device=src.device())
return src, partptr, perm

if balance_edge and node_weight:
raise ValueError("Cannot set 'balance_edge' and 'node_weight' at the "
"same time in 'torch_sparse.partition'")

rowptr, col, value = src.csr()
rowptr, col = rowptr.cpu(), col.cpu()

Expand All @@ -41,6 +51,10 @@ def partition(
else:
value = None

if balance_edge:
node_weight = col.new_zeros(col.size(0))
node_weight.scatter_add_(0, col, torch.ones_like(col))

if node_weight is not None:
assert node_weight.numel() == rowptr.numel() - 1
node_weight = node_weight.view(-1).detach().cpu()
Expand Down