Commit 247154f9 authored by Chendi Qian's avatar Chendi Qian
Browse files

add node weights for metis wrapper

parent 54d8418e
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
torch::Tensor partition_cpu(torch::Tensor rowptr, torch::Tensor col, torch::Tensor partition_cpu(torch::Tensor rowptr, torch::Tensor col,
torch::optional<torch::Tensor> optional_value, torch::optional<torch::Tensor> optional_value,
torch::optional<torch::Tensor> vweights,
int64_t num_parts, bool recursive) { int64_t num_parts, bool recursive) {
#ifdef WITH_METIS #ifdef WITH_METIS
CHECK_CPU(rowptr); CHECK_CPU(rowptr);
...@@ -22,6 +23,12 @@ torch::Tensor partition_cpu(torch::Tensor rowptr, torch::Tensor col, ...@@ -22,6 +23,12 @@ torch::Tensor partition_cpu(torch::Tensor rowptr, torch::Tensor col,
CHECK_INPUT(optional_value.value().numel() == col.numel()); CHECK_INPUT(optional_value.value().numel() == col.numel());
} }
if (vweights.has_value()) {
CHECK_CPU(vweights.value());
CHECK_INPUT(vweights.value().dim() == 1);
CHECK_INPUT(vweights.value().numel() == rowptr.numel() - 1);
}
int64_t nvtxs = rowptr.numel() - 1; int64_t nvtxs = rowptr.numel() - 1;
int64_t ncon = 1; int64_t ncon = 1;
auto *xadj = rowptr.data_ptr<int64_t>(); auto *xadj = rowptr.data_ptr<int64_t>();
...@@ -29,15 +36,20 @@ torch::Tensor partition_cpu(torch::Tensor rowptr, torch::Tensor col, ...@@ -29,15 +36,20 @@ torch::Tensor partition_cpu(torch::Tensor rowptr, torch::Tensor col,
int64_t *adjwgt = NULL; int64_t *adjwgt = NULL;
if (optional_value.has_value()) if (optional_value.has_value())
adjwgt = optional_value.value().data_ptr<int64_t>(); adjwgt = optional_value.value().data_ptr<int64_t>();
int64_t *vwgt = NULL;
if (vweights.has_value())
vwgt = vweights.value().data_ptr<int64_t>();
int64_t objval = -1; int64_t objval = -1;
auto part = torch::empty(nvtxs, rowptr.options()); auto part = torch::empty(nvtxs, rowptr.options());
auto part_data = part.data_ptr<int64_t>(); auto part_data = part.data_ptr<int64_t>();
if (recursive) { if (recursive) {
METIS_PartGraphRecursive(&nvtxs, &ncon, xadj, adjncy, NULL, NULL, adjwgt, METIS_PartGraphRecursive(&nvtxs, &ncon, xadj, adjncy, vwgt, NULL, adjwgt,
&num_parts, NULL, NULL, NULL, &objval, part_data); &num_parts, NULL, NULL, NULL, &objval, part_data);
} else { } else {
METIS_PartGraphKway(&nvtxs, &ncon, xadj, adjncy, NULL, NULL, adjwgt, METIS_PartGraphKway(&nvtxs, &ncon, xadj, adjncy, vwgt, NULL, adjwgt,
&num_parts, NULL, NULL, NULL, &objval, part_data); &num_parts, NULL, NULL, NULL, &objval, part_data);
} }
...@@ -52,6 +64,7 @@ torch::Tensor partition_cpu(torch::Tensor rowptr, torch::Tensor col, ...@@ -52,6 +64,7 @@ torch::Tensor partition_cpu(torch::Tensor rowptr, torch::Tensor col,
// --partitions64bit // --partitions64bit
torch::Tensor mt_partition_cpu(torch::Tensor rowptr, torch::Tensor col, torch::Tensor mt_partition_cpu(torch::Tensor rowptr, torch::Tensor col,
torch::optional<torch::Tensor> optional_value, torch::optional<torch::Tensor> optional_value,
torch::optional<torch::Tensor> vweights,
int64_t num_parts, bool recursive, int64_t num_parts, bool recursive,
int64_t num_workers) { int64_t num_workers) {
#ifdef WITH_MTMETIS #ifdef WITH_MTMETIS
...@@ -63,6 +76,12 @@ torch::Tensor mt_partition_cpu(torch::Tensor rowptr, torch::Tensor col, ...@@ -63,6 +76,12 @@ torch::Tensor mt_partition_cpu(torch::Tensor rowptr, torch::Tensor col,
CHECK_INPUT(optional_value.value().numel() == col.numel()); CHECK_INPUT(optional_value.value().numel() == col.numel());
} }
if (vweights.has_value()) {
CHECK_CPU(vweights.value());
CHECK_INPUT(vweights.value().dim() == 1);
CHECK_INPUT(vweights.value().numel() == rowptr.numel() - 1);
}
mtmetis_vtx_type nvtxs = rowptr.numel() - 1; mtmetis_vtx_type nvtxs = rowptr.numel() - 1;
mtmetis_vtx_type ncon = 1; mtmetis_vtx_type ncon = 1;
mtmetis_adj_type *xadj = (mtmetis_adj_type *)rowptr.data_ptr<int64_t>(); mtmetis_adj_type *xadj = (mtmetis_adj_type *)rowptr.data_ptr<int64_t>();
...@@ -70,6 +89,9 @@ torch::Tensor mt_partition_cpu(torch::Tensor rowptr, torch::Tensor col, ...@@ -70,6 +89,9 @@ torch::Tensor mt_partition_cpu(torch::Tensor rowptr, torch::Tensor col,
mtmetis_wgt_type *adjwgt = NULL; mtmetis_wgt_type *adjwgt = NULL;
if (optional_value.has_value()) if (optional_value.has_value())
adjwgt = optional_value.value().data_ptr<int64_t>(); adjwgt = optional_value.value().data_ptr<int64_t>();
mtmetis_wgt_type *vwgt = NULL;
if (vweights.has_value())
vwgt = vweights.value().data_ptr<int64_t>();
mtmetis_pid_type nparts = num_parts; mtmetis_pid_type nparts = num_parts;
mtmetis_wgt_type objval = -1; mtmetis_wgt_type objval = -1;
auto part = torch::empty(nvtxs, rowptr.options()); auto part = torch::empty(nvtxs, rowptr.options());
...@@ -79,10 +101,10 @@ torch::Tensor mt_partition_cpu(torch::Tensor rowptr, torch::Tensor col, ...@@ -79,10 +101,10 @@ torch::Tensor mt_partition_cpu(torch::Tensor rowptr, torch::Tensor col,
opts[MTMETIS_OPTION_NTHREADS] = num_workers; opts[MTMETIS_OPTION_NTHREADS] = num_workers;
if (recursive) { if (recursive) {
MTMETIS_PartGraphRecursive(&nvtxs, &ncon, xadj, adjncy, NULL, NULL, adjwgt, MTMETIS_PartGraphRecursive(&nvtxs, &ncon, xadj, adjncy, vwgt, NULL, adjwgt,
&nparts, NULL, NULL, opts, &objval, part_data); &nparts, NULL, NULL, opts, &objval, part_data);
} else { } else {
MTMETIS_PartGraphKway(&nvtxs, &ncon, xadj, adjncy, NULL, NULL, adjwgt, MTMETIS_PartGraphKway(&nvtxs, &ncon, xadj, adjncy, vwgt, NULL, adjwgt,
&nparts, NULL, NULL, opts, &objval, part_data); &nparts, NULL, NULL, opts, &objval, part_data);
} }
......
...@@ -4,9 +4,11 @@ ...@@ -4,9 +4,11 @@
torch::Tensor partition_cpu(torch::Tensor rowptr, torch::Tensor col, torch::Tensor partition_cpu(torch::Tensor rowptr, torch::Tensor col,
torch::optional<torch::Tensor> optional_value, torch::optional<torch::Tensor> optional_value,
torch::optional<torch::Tensor> vweights,
int64_t num_parts, bool recursive); int64_t num_parts, bool recursive);
torch::Tensor mt_partition_cpu(torch::Tensor rowptr, torch::Tensor col, torch::Tensor mt_partition_cpu(torch::Tensor rowptr, torch::Tensor col,
torch::optional<torch::Tensor> optional_value, torch::optional<torch::Tensor> optional_value,
torch::optional<torch::Tensor> vweights,
int64_t num_parts, bool recursive, int64_t num_parts, bool recursive,
int64_t num_workers); int64_t num_workers);
...@@ -13,6 +13,7 @@ PyMODINIT_FUNC PyInit__metis_cpu(void) { return NULL; } ...@@ -13,6 +13,7 @@ PyMODINIT_FUNC PyInit__metis_cpu(void) { return NULL; }
torch::Tensor partition(torch::Tensor rowptr, torch::Tensor col, torch::Tensor partition(torch::Tensor rowptr, torch::Tensor col,
torch::optional<torch::Tensor> optional_value, torch::optional<torch::Tensor> optional_value,
torch::optional<torch::Tensor> vweights,
int64_t num_parts, bool recursive) { int64_t num_parts, bool recursive) {
if (rowptr.device().is_cuda()) { if (rowptr.device().is_cuda()) {
#ifdef WITH_CUDA #ifdef WITH_CUDA
...@@ -21,12 +22,13 @@ torch::Tensor partition(torch::Tensor rowptr, torch::Tensor col, ...@@ -21,12 +22,13 @@ torch::Tensor partition(torch::Tensor rowptr, torch::Tensor col,
AT_ERROR("Not compiled with CUDA support"); AT_ERROR("Not compiled with CUDA support");
#endif #endif
} else { } else {
return partition_cpu(rowptr, col, optional_value, num_parts, recursive); return partition_cpu(rowptr, col, optional_value, vweights, num_parts, recursive);
} }
} }
torch::Tensor mt_partition(torch::Tensor rowptr, torch::Tensor col, torch::Tensor mt_partition(torch::Tensor rowptr, torch::Tensor col,
torch::optional<torch::Tensor> optional_value, torch::optional<torch::Tensor> optional_value,
torch::optional<torch::Tensor> vweights,
int64_t num_parts, bool recursive, int64_t num_parts, bool recursive,
int64_t num_workers) { int64_t num_workers) {
if (rowptr.device().is_cuda()) { if (rowptr.device().is_cuda()) {
...@@ -36,7 +38,7 @@ torch::Tensor mt_partition(torch::Tensor rowptr, torch::Tensor col, ...@@ -36,7 +38,7 @@ torch::Tensor mt_partition(torch::Tensor rowptr, torch::Tensor col,
AT_ERROR("Not compiled with CUDA support"); AT_ERROR("Not compiled with CUDA support");
#endif #endif
} else { } else {
return mt_partition_cpu(rowptr, col, optional_value, num_parts, recursive, return mt_partition_cpu(rowptr, col, optional_value, vweights, num_parts, recursive,
num_workers); num_workers);
} }
} }
......
...@@ -18,20 +18,26 @@ def test_metis(device): ...@@ -18,20 +18,26 @@ def test_metis(device):
value2 = torch.arange(6 * 6, dtype=torch.long, device=device).view(6, 6) value2 = torch.arange(6 * 6, dtype=torch.long, device=device).view(6, 6)
value3 = torch.ones(6 * 6, device=device).view(6, 6) value3 = torch.ones(6 * 6, device=device).view(6, 6)
vwgts = torch.rand(6, device=device)
for value in [value1, value2, value3]: for value in [value1, value2, value3]:
mat = SparseTensor.from_dense(value) for vwgt in [None, vwgts]:
mat = SparseTensor.from_dense(value)
_, partptr, perm = mat.partition(num_parts=2, recursive=False,
weighted=True) _, partptr, perm = mat.partition(num_parts=2, recursive=False,
assert partptr.numel() == 3 vweights=vwgt,
assert perm.numel() == 6 weighted=True)
assert partptr.numel() == 3
_, partptr, perm = mat.partition(num_parts=2, recursive=False, assert perm.numel() == 6
weighted=False)
assert partptr.numel() == 3 _, partptr, perm = mat.partition(num_parts=2, recursive=False,
assert perm.numel() == 6 vweights=vwgt,
weighted=False)
_, partptr, perm = mat.partition(num_parts=1, recursive=False, assert partptr.numel() == 3
weighted=True) assert perm.numel() == 6
assert partptr.numel() == 2
assert perm.numel() == 6 _, partptr, perm = mat.partition(num_parts=1, recursive=False,
vweights=vwgt,
weighted=True)
assert partptr.numel() == 2
assert perm.numel() == 6
...@@ -21,6 +21,7 @@ def weight2metis(weight: torch.Tensor) -> Optional[torch.Tensor]: ...@@ -21,6 +21,7 @@ def weight2metis(weight: torch.Tensor) -> Optional[torch.Tensor]:
def partition( def partition(
src: SparseTensor, num_parts: int, recursive: bool = False, src: SparseTensor, num_parts: int, recursive: bool = False,
vweights: torch.tensor = None,
weighted: bool = False weighted: bool = False
) -> Tuple[SparseTensor, torch.Tensor, torch.Tensor]: ) -> Tuple[SparseTensor, torch.Tensor, torch.Tensor]:
...@@ -41,7 +42,13 @@ def partition( ...@@ -41,7 +42,13 @@ def partition(
else: else:
value = None value = None
cluster = torch.ops.torch_sparse.partition(rowptr, col, value, num_parts, if vweights is not None:
assert vweights.numel() == rowptr.numel() - 1
vweights = vweights.view(-1).detach().cpu()
if vweights.is_floating_point():
vweights = weight2metis(vweights)
cluster = torch.ops.torch_sparse.partition(rowptr, col, value, vweights, num_parts,
recursive) recursive)
cluster = cluster.to(src.device()) cluster = cluster.to(src.device())
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment