-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathpartition.py
2021 lines (1844 loc) · 76.8 KB
/
partition.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Functions for partitions. """
import concurrent
import concurrent.futures
import copy
import json
import logging
import multiprocessing as mp
import os
import time
from functools import partial
import numpy as np
import torch
from .. import backend as F, graphbolt as gb
from ..base import dgl_warning, DGLError, EID, ETYPE, NID, NTYPE
from ..convert import heterograph, to_homogeneous
from ..data.utils import load_graphs, load_tensors, save_graphs, save_tensors
from ..partition import (
get_peak_mem,
metis_partition_assignment,
partition_graph_with_halo,
)
from ..random import choice as random_choice
from ..transforms import sort_csc_by_tag, sort_csr_by_tag
from .constants import DEFAULT_ETYPE, DEFAULT_NTYPE, DGL2GB_EID, GB_DST_ID
from .graph_partition_book import (
_etype_str_to_tuple,
_etype_tuple_to_str,
RangePartitionBook,
)
RESERVED_FIELD_DTYPE = {
"inner_node": (
F.uint8
), # A flag indicates whether the node is inside a partition.
"inner_edge": (
F.uint8
), # A flag indicates whether the edge is inside a partition.
NID: F.int64,
EID: F.int64,
NTYPE: F.int16,
# `sort_csr_by_tag` and `sort_csc_by_tag` works on int32/64 only.
ETYPE: F.int32,
}
def _format_part_metadata(part_metadata, formatter):
"""Format etypes with specified formatter."""
for key in ["edge_map", "etypes"]:
if key not in part_metadata:
continue
orig_data = part_metadata[key]
if not isinstance(orig_data, dict):
continue
new_data = {}
for etype, data in orig_data.items():
etype = formatter(etype)
new_data[etype] = data
part_metadata[key] = new_data
return part_metadata
def _load_part_config(part_config):
"""Load part config and format."""
try:
with open(part_config) as f:
part_metadata = _format_part_metadata(
json.load(f), _etype_str_to_tuple
)
except AssertionError as e:
raise DGLError(
f"Failed to load partition config due to {e}. "
"Probably caused by outdated config. If so, please refer to "
"https://github.com/dmlc/dgl/tree/master/tools#change-edge-"
"type-to-canonical-edge-type-for-partition-configuration-json"
)
return part_metadata
def _dump_part_config(part_config, part_metadata):
"""Format and dump part config."""
part_metadata = _format_part_metadata(part_metadata, _etype_tuple_to_str)
with open(part_config, "w") as outfile:
json.dump(part_metadata, outfile, sort_keys=False, indent=4)
def process_partitions(g, formats=None, sort_etypes=False):
"""Preprocess partitions before saving:
1. format data types.
2. sort csc/csr by tag.
"""
for k, dtype in RESERVED_FIELD_DTYPE.items():
if k in g.ndata:
g.ndata[k] = F.astype(g.ndata[k], dtype)
if k in g.edata:
g.edata[k] = F.astype(g.edata[k], dtype)
if (sort_etypes) and (formats is not None):
if "csr" in formats:
g = sort_csr_by_tag(g, tag=g.edata[ETYPE], tag_type="edge")
if "csc" in formats:
g = sort_csc_by_tag(g, tag=g.edata[ETYPE], tag_type="edge")
return g
def _save_dgl_graphs(filename, g_list, formats=None):
save_graphs(filename, g_list, formats=formats)
def _get_inner_node_mask(graph, ntype_id, gpb=None):
ndata = (
graph.node_attributes
if isinstance(graph, gb.FusedCSCSamplingGraph)
else graph.ndata
)
assert "inner_node" in ndata, "'inner_node' is not in nodes' data"
if NTYPE in ndata or gpb is not None:
ntype = (
gpb.map_to_per_ntype(ndata[NID])[0]
if gpb is not None
else ndata[NTYPE]
)
dtype = F.dtype(ndata["inner_node"])
return ndata["inner_node"] * F.astype(ntype == ntype_id, dtype) == 1
else:
return ndata["inner_node"] == 1
def _get_inner_edge_mask(
graph,
etype_id,
):
edata = (
graph.edge_attributes
if isinstance(graph, gb.FusedCSCSamplingGraph)
else graph.edata
)
assert "inner_edge" in edata, "'inner_edge' is not in edges' data"
etype = (
graph.type_per_edge
if isinstance(graph, gb.FusedCSCSamplingGraph)
else (graph.edata[ETYPE] if ETYPE in graph.edata else None)
)
if etype is not None:
dtype = F.dtype(edata["inner_edge"])
return edata["inner_edge"] * F.astype(etype == etype_id, dtype) == 1
else:
return edata["inner_edge"] == 1
def _get_part_ranges(id_ranges):
res = {}
for key in id_ranges:
# Normally, each element has two values that represent the starting ID and the ending ID
# of the ID range in a partition.
# If not, the data is probably still in the old format, in which only the ending ID is
# stored. We need to convert it to the format we expect.
if not isinstance(id_ranges[key][0], list):
start = 0
for i, end in enumerate(id_ranges[key]):
id_ranges[key][i] = [start, end]
start = end
res[key] = np.concatenate(
[np.array(l) for l in id_ranges[key]]
).reshape(-1, 2)
return res
def _verify_dgl_partition(graph, part_id, gpb, ntypes, etypes):
"""Verify the partition of a DGL graph."""
assert (
NID in graph.ndata
), "the partition graph should contain node mapping to global node ID"
assert (
EID in graph.edata
), "the partition graph should contain edge mapping to global edge ID"
for ntype in ntypes:
ntype_id = ntypes[ntype]
# graph.ndata[NID] are global homogeneous node IDs.
nids = F.boolean_mask(
graph.ndata[NID], _get_inner_node_mask(graph, ntype_id)
)
partids1 = gpb.nid2partid(nids)
_, per_type_nids = gpb.map_to_per_ntype(nids)
partids2 = gpb.nid2partid(per_type_nids, ntype)
assert np.all(F.asnumpy(partids1 == part_id)), (
"Unexpected partition IDs are found in the loaded partition "
"while querying via global homogeneous node IDs."
)
assert np.all(F.asnumpy(partids2 == part_id)), (
"Unexpected partition IDs are found in the loaded partition "
"while querying via type-wise node IDs."
)
for etype in etypes:
etype_id = etypes[etype]
# graph.edata[EID] are global homogeneous edge IDs.
eids = F.boolean_mask(
graph.edata[EID], _get_inner_edge_mask(graph, etype_id)
)
partids1 = gpb.eid2partid(eids)
_, per_type_eids = gpb.map_to_per_etype(eids)
partids2 = gpb.eid2partid(per_type_eids, etype)
assert np.all(F.asnumpy(partids1 == part_id)), (
"Unexpected partition IDs are found in the loaded partition "
"while querying via global homogeneous edge IDs."
)
assert np.all(F.asnumpy(partids2 == part_id)), (
"Unexpected partition IDs are found in the loaded partition "
"while querying via type-wise edge IDs."
)
def _verify_graphbolt_partition(graph, part_id, gpb, ntypes, etypes):
"""Verify the partition of a GraphBolt graph."""
required_ndata_fields = [NID]
required_edata_fields = [EID]
assert all(
field in graph.node_attributes for field in required_ndata_fields
), "the partition graph should contain node mapping to global node ID."
assert all(
field in graph.edge_attributes for field in required_edata_fields
), "the partition graph should contain edge mapping to global edge ID."
num_edges = graph.total_num_edges
local_src_ids = graph.indices
local_dst_ids = gb.expand_indptr(
graph.csc_indptr, dtype=local_src_ids.dtype, output_size=num_edges
)
global_src_ids = graph.node_attributes[NID][local_src_ids]
global_dst_ids = graph.node_attributes[NID][local_dst_ids]
etype_ids, type_wise_eids = gpb.map_to_per_etype(graph.edge_attributes[EID])
if graph.type_per_edge is not None:
assert torch.equal(etype_ids, graph.type_per_edge)
etype_ids, etype_ids_indices = torch.sort(etype_ids)
global_src_ids = global_src_ids[etype_ids_indices]
global_dst_ids = global_dst_ids[etype_ids_indices]
type_wise_eids = type_wise_eids[etype_ids_indices]
src_ntype_ids, src_type_wise_nids = gpb.map_to_per_ntype(global_src_ids)
dst_ntype_ids, dst_type_wise_nids = gpb.map_to_per_ntype(global_dst_ids)
data_dict = dict()
edge_ids = dict()
for c_etype, etype_id in etypes.items():
idx = etype_ids == etype_id
src_ntype, etype, dst_ntype = c_etype
if idx.sum() == 0:
continue
actual_src_ntype_ids = src_ntype_ids[idx]
actual_dst_ntype_ids = dst_ntype_ids[idx]
expected_src_ntype_ids = ntypes[src_ntype]
expected_dst_ntype_ids = ntypes[dst_ntype]
assert all(actual_src_ntype_ids == expected_src_ntype_ids), (
f"Unexpected types of source nodes for {c_etype}. Expected: "
f"{expected_src_ntype_ids}, but got: {actual_src_ntype_ids}."
)
assert all(actual_dst_ntype_ids == expected_dst_ntype_ids), (
f"Unexpected types of destination nodes for {c_etype}. Expected: "
f"{expected_dst_ntype_ids}, but got: {actual_dst_ntype_ids}."
)
data_dict[c_etype] = (src_type_wise_nids[idx], dst_type_wise_nids[idx])
edge_ids[c_etype] = type_wise_eids[idx]
# Make sure node/edge IDs are not out of range.
hg = heterograph(
data_dict, {ntype: gpb._num_nodes(ntype) for ntype in ntypes}
)
for etype in edge_ids:
hg.edges[etype].data[EID] = edge_ids[etype]
assert all(
hg.num_edges(etype) == len(eids) for etype, eids in edge_ids.items()
), "The number of edges per etype in the partition graph is not correct."
assert num_edges == hg.num_edges(), (
f"The total number of edges in the partition graph is not correct. "
f"Expected: {num_edges}, but got: {hg.num_edges()}."
)
print(f"Partition {part_id} looks good!")
def load_partition(part_config, part_id, load_feats=True, use_graphbolt=False):
"""Load data of a partition from the data path.
A partition data includes a graph structure of the partition, a dict of node tensors,
a dict of edge tensors and some metadata. The partition may contain the HALO nodes,
which are the nodes replicated from other partitions. However, the dict of node tensors
only contains the node data that belongs to the local partition. Similarly, edge tensors
only contains the edge data that belongs to the local partition. The metadata include
the information of the global graph (not the local partition), which includes the number
of nodes, the number of edges as well as the node assignment of the global graph.
The function currently loads data through the local filesystem interface.
Parameters
----------
part_config : str
The path of the partition config file.
part_id : int
The partition ID.
load_feats : bool, optional
Whether to load node/edge feats. If False, the returned node/edge feature
dictionaries will be empty. Default: True.
use_graphbolt : bool, optional
Whether to load GraphBolt partition. Default: False.
Returns
-------
DGLGraph
The graph partition structure.
Dict[str, Tensor]
Node features.
Dict[(str, str, str), Tensor]
Edge features.
GraphPartitionBook
The graph partition information.
str
The graph name
List[str]
The node types
List[(str, str, str)]
The edge types
"""
config_path = os.path.dirname(part_config)
relative_to_config = lambda path: os.path.join(config_path, path)
with open(part_config) as conf_f:
part_metadata = json.load(conf_f)
assert (
"part-{}".format(part_id) in part_metadata
), "part-{} does not exist".format(part_id)
part_files = part_metadata["part-{}".format(part_id)]
exist_dgl_graph = exist_graphbolt_graph = False
if os.path.exists(os.path.join(config_path, f"part{part_id}", "graph.dgl")):
use_graphbolt = False
exist_dgl_graph = True
if os.path.exists(
os.path.join(
config_path, f"part{part_id}", "fused_csc_sampling_graph.pt"
)
):
use_graphbolt = True
exist_graphbolt_graph = True
# Check if both DGL graph and GraphBolt graph exist or not exist. Make sure only one exists.
if not exist_dgl_graph and not exist_graphbolt_graph:
raise ValueError("The graph object doesn't exist.")
if exist_dgl_graph and exist_graphbolt_graph:
raise ValueError(
"Both DGL graph and GraphBolt graph exist. Please remove one."
)
if use_graphbolt:
part_graph_field = "part_graph_graphbolt"
else:
part_graph_field = "part_graph"
assert (
part_graph_field in part_files
), f"the partition does not contain graph structure: {part_graph_field}"
partition_path = relative_to_config(part_files[part_graph_field])
logging.info(
"Start to load partition from %s which is "
"%d bytes. It may take non-trivial "
"time for large partition.",
partition_path,
os.path.getsize(partition_path),
)
graph = (
torch.load(partition_path)
if use_graphbolt
else load_graphs(partition_path)[0][0]
)
logging.info("Finished loading partition from %s.", partition_path)
gpb, graph_name, ntypes, etypes = load_partition_book(part_config, part_id)
ntypes_list = list(ntypes.keys())
etypes_list = list(etypes.keys())
if "DGL_DIST_DEBUG" in os.environ:
_verify_func = (
_verify_graphbolt_partition
if use_graphbolt
else _verify_dgl_partition
)
_verify_func(graph, part_id, gpb, ntypes, etypes)
node_feats = {}
edge_feats = {}
if load_feats:
node_feats, edge_feats = load_partition_feats(part_config, part_id)
return (
graph,
node_feats,
edge_feats,
gpb,
graph_name,
ntypes_list,
etypes_list,
)
def load_partition_feats(
part_config, part_id, load_nodes=True, load_edges=True
):
"""Load node/edge feature data from a partition.
Parameters
----------
part_config : str
The path of the partition config file.
part_id : int
The partition ID.
load_nodes : bool, optional
Whether to load node features. If ``False``, ``None`` is returned.
load_edges : bool, optional
Whether to load edge features. If ``False``, ``None`` is returned.
Returns
-------
Dict[str, Tensor] or None
Node features.
Dict[str, Tensor] or None
Edge features.
"""
config_path = os.path.dirname(part_config)
relative_to_config = lambda path: os.path.join(config_path, path)
with open(part_config) as conf_f:
part_metadata = json.load(conf_f)
assert (
"part-{}".format(part_id) in part_metadata
), "part-{} does not exist".format(part_id)
part_files = part_metadata["part-{}".format(part_id)]
assert (
"node_feats" in part_files
), "the partition does not contain node features."
assert (
"edge_feats" in part_files
), "the partition does not contain edge feature."
node_feats = None
if load_nodes:
feat_path = relative_to_config(part_files["node_feats"])
logging.debug(
"Start to load node data from %s which is " "%d bytes.",
feat_path,
os.path.getsize(feat_path),
)
node_feats = load_tensors(feat_path)
logging.info("Finished loading node data.")
edge_feats = None
if load_edges:
feat_path = relative_to_config(part_files["edge_feats"])
logging.debug(
"Start to load edge data from %s which is " "%d bytes.",
feat_path,
os.path.getsize(feat_path),
)
edge_feats = load_tensors(feat_path)
logging.info("Finished loading edge data.")
# In the old format, the feature name doesn't contain node/edge type.
# For compatibility, let's add node/edge types to the feature names.
if node_feats is not None:
new_feats = {}
for name in node_feats:
feat = node_feats[name]
if name.find("/") == -1:
name = DEFAULT_NTYPE + "/" + name
new_feats[name] = feat
node_feats = new_feats
if edge_feats is not None:
new_feats = {}
for name in edge_feats:
feat = edge_feats[name]
if name.find("/") == -1:
name = _etype_tuple_to_str(DEFAULT_ETYPE) + "/" + name
new_feats[name] = feat
edge_feats = new_feats
return node_feats, edge_feats
def load_partition_book(part_config, part_id, part_metadata=None):
"""Load a graph partition book from the partition config file.
Parameters
----------
part_config : str
The path of the partition config file.
part_id : int
The partition ID.
part_metadata : dict
The meta data of partition.
Returns
-------
GraphPartitionBook
The global partition information.
str
The graph name
dict
The node types
dict
The edge types
"""
if part_metadata is None:
part_metadata = _load_part_config(part_config)
assert "num_parts" in part_metadata, "num_parts does not exist."
assert (
part_metadata["num_parts"] > part_id
), "part {} is out of range (#parts: {})".format(
part_id, part_metadata["num_parts"]
)
num_parts = part_metadata["num_parts"]
assert (
"num_nodes" in part_metadata
), "cannot get the number of nodes of the global graph."
assert (
"num_edges" in part_metadata
), "cannot get the number of edges of the global graph."
assert "node_map" in part_metadata, "cannot get the node map."
assert "edge_map" in part_metadata, "cannot get the edge map."
assert "graph_name" in part_metadata, "cannot get the graph name"
# If this is a range partitioning, node_map actually stores a list, whose elements
# indicate the boundary of range partitioning. Otherwise, node_map stores a filename
# that contains node map in a NumPy array.
node_map = part_metadata["node_map"]
edge_map = part_metadata["edge_map"]
if isinstance(node_map, dict):
for key in node_map:
is_range_part = isinstance(node_map[key], list)
break
elif isinstance(node_map, list):
is_range_part = True
node_map = {DEFAULT_NTYPE: node_map}
else:
is_range_part = False
if isinstance(edge_map, list):
edge_map = {DEFAULT_ETYPE: edge_map}
ntypes = {DEFAULT_NTYPE: 0}
etypes = {DEFAULT_ETYPE: 0}
if "ntypes" in part_metadata:
ntypes = part_metadata["ntypes"]
if "etypes" in part_metadata:
etypes = part_metadata["etypes"]
if isinstance(node_map, dict):
for key in node_map:
assert key in ntypes, "The node type {} is invalid".format(key)
if isinstance(edge_map, dict):
for key in edge_map:
assert key in etypes, "The edge type {} is invalid".format(key)
if not is_range_part:
raise TypeError("Only RangePartitionBook is supported currently.")
node_map = _get_part_ranges(node_map)
edge_map = _get_part_ranges(edge_map)
# Format dtype of node/edge map if dtype is specified.
def _format_node_edge_map(part_metadata, map_type, data):
key = f"{map_type}_map_dtype"
if key not in part_metadata:
return data
dtype = part_metadata[key]
assert dtype in ["int32", "int64"], (
f"The {map_type} map dtype should be either int32 or int64, "
f"but got {dtype}."
)
for key in data:
data[key] = data[key].astype(dtype)
return data
node_map = _format_node_edge_map(part_metadata, "node", node_map)
edge_map = _format_node_edge_map(part_metadata, "edge", edge_map)
# Sort the node/edge maps by the node/edge type ID.
node_map = dict(sorted(node_map.items(), key=lambda x: ntypes[x[0]]))
edge_map = dict(sorted(edge_map.items(), key=lambda x: etypes[x[0]]))
def _assert_is_sorted(id_map):
id_ranges = np.array(list(id_map.values()))
ids = []
for i in range(num_parts):
ids.append(id_ranges[:, i, :])
ids = np.array(ids).flatten()
assert np.all(
ids[:-1] <= ids[1:]
), f"The node/edge map is not sorted: {ids}"
_assert_is_sorted(node_map)
_assert_is_sorted(edge_map)
return (
RangePartitionBook(
part_id, num_parts, node_map, edge_map, ntypes, etypes
),
part_metadata["graph_name"],
ntypes,
etypes,
)
def _get_orig_ids(g, sim_g, orig_nids, orig_eids):
"""Convert/construct the original node IDs and edge IDs.
It handles multiple cases:
* If the graph has been reshuffled and it's a homogeneous graph, we just return
the original node IDs and edge IDs in the inputs.
* If the graph has been reshuffled and it's a heterogeneous graph, we need to
split the original node IDs and edge IDs in the inputs based on the node types
and edge types.
* If the graph is not shuffled, the original node IDs and edge IDs don't change.
Parameters
----------
g : DGLGraph
The input graph for partitioning.
sim_g : DGLGraph
The homogeneous version of the input graph.
orig_nids : tensor or None
The original node IDs after the input graph is reshuffled.
orig_eids : tensor or None
The original edge IDs after the input graph is reshuffled.
Returns
-------
tensor or dict of tensors, tensor or dict of tensors
"""
is_hetero = not g.is_homogeneous
if is_hetero:
# Get the type IDs
orig_ntype = F.gather_row(sim_g.ndata[NTYPE], orig_nids)
orig_etype = F.gather_row(sim_g.edata[ETYPE], orig_eids)
# Mapping between shuffled global IDs to original per-type IDs
orig_nids = F.gather_row(sim_g.ndata[NID], orig_nids)
orig_eids = F.gather_row(sim_g.edata[EID], orig_eids)
orig_nids = {
ntype: F.boolean_mask(
orig_nids, orig_ntype == g.get_ntype_id(ntype)
)
for ntype in g.ntypes
}
orig_eids = {
etype: F.boolean_mask(
orig_eids, orig_etype == g.get_etype_id(etype)
)
for etype in g.canonical_etypes
}
return orig_nids, orig_eids
def _set_trainer_ids(g, sim_g, node_parts):
"""Set the trainer IDs for each node and edge on the input graph.
The trainer IDs will be stored as node data and edge data in the input graph.
Parameters
----------
g : DGLGraph
The input graph for partitioning.
sim_g : DGLGraph
The homogeneous version of the input graph.
node_parts : tensor
The node partition ID for each node in `sim_g`.
"""
if g.is_homogeneous:
g.ndata["trainer_id"] = node_parts
# An edge is assigned to a partition based on its destination node.
g.edata["trainer_id"] = F.gather_row(node_parts, g.edges()[1])
else:
for ntype_id, ntype in enumerate(g.ntypes):
type_idx = sim_g.ndata[NTYPE] == ntype_id
orig_nid = F.boolean_mask(sim_g.ndata[NID], type_idx)
trainer_id = F.zeros((len(orig_nid),), F.dtype(node_parts), F.cpu())
F.scatter_row_inplace(
trainer_id, orig_nid, F.boolean_mask(node_parts, type_idx)
)
g.nodes[ntype].data["trainer_id"] = trainer_id
for c_etype in g.canonical_etypes:
# An edge is assigned to a partition based on its destination node.
_, _, dst_type = c_etype
trainer_id = F.gather_row(
g.nodes[dst_type].data["trainer_id"], g.edges(etype=c_etype)[1]
)
g.edges[c_etype].data["trainer_id"] = trainer_id
def _partition_to_graphbolt(
parts,
part_i,
part_config,
part_metadata,
*,
store_eids=True,
store_inner_node=False,
store_inner_edge=False,
graph_formats=None,
):
gpb, _, ntypes, etypes = load_partition_book(
part_config=part_config, part_id=part_i, part_metadata=part_metadata
)
graph = parts[part_i]
csc_graph = _convert_dgl_partition_to_gb(
ntypes=ntypes,
etypes=etypes,
gpb=gpb,
part_meta=part_metadata,
graph=graph,
store_eids=store_eids,
store_inner_edge=store_inner_edge,
store_inner_node=store_inner_node,
graph_formats=graph_formats,
)
rel_path_result = _save_graph_gb(
part_config=part_config, part_id=part_i, csc_graph=csc_graph
)
part_metadata[f"part-{part_i}"]["part_graph_graphbolt"] = rel_path_result
def _update_node_edge_map(node_map_val, edge_map_val, g, num_parts):
"""
If the original graph contains few nodes or edges for specific node/edge
types, the partitioned graph may have empty partitions for these types. And
the node_map_val and edge_map_val will have -1 for the start and end ID of
these types. This function updates the node_map_val and edge_map_val to be
contiguous.
Example case:
Suppose we have a heterogeneous graph with 3 node/edge types and the number
of partitions is 3. A possible node_map_val or edge_map_val is as follows:
| part_id\\Node/Edge Type| Type A | Type B | Type C |
|------------------------|--------|---------|--------|
| 0 | 0, 1 | -1, -1 | 2, 3 |
| 1 | -1, -1 | 3, 4 | 4, 5 |
| 2 | 5, 6 | 7, 8 | -1, -1|
As node/edge IDs are contiguous in node/edge type for each partition, we can
update the node_map_val and edge_map_val via updating the start and end ID
in row-wise order.
Updated node_map_val or edge_map_val:
| part_id\\Node/Edge Type| Type A | Type B | Type C |
|------------------------|--------|---------|--------|
| 0 | 0, 1 | 1, 1 | 2, 3 |
| 1 | 3, 3 | 3, 4 | 4, 5 |
| 2 | 5, 6 | 7, 8 | 8, 8 |
"""
# Update the node_map_val to be contiguous.
ntype_ids = {ntype: g.get_ntype_id(ntype) for ntype in g.ntypes}
ntype_ids_reverse = {v: k for k, v in ntype_ids.items()}
for part_id in range(num_parts):
for ntype_id in list(ntype_ids.values()):
ntype = ntype_ids_reverse[ntype_id]
start_id = node_map_val[ntype][part_id][0]
end_id = node_map_val[ntype][part_id][1]
if not (start_id == -1 and end_id == -1):
continue
prev_ntype_id = (
ntype_ids[ntype] - 1
if ntype_ids[ntype] > 0
else max(ntype_ids.values())
)
prev_ntype = ntype_ids_reverse[prev_ntype_id]
if ntype_ids[ntype] == 0:
if part_id == 0:
node_map_val[ntype][part_id][0] = 0
else:
node_map_val[ntype][part_id][0] = node_map_val[prev_ntype][
part_id - 1
][1]
else:
node_map_val[ntype][part_id][0] = node_map_val[prev_ntype][
part_id
][1]
node_map_val[ntype][part_id][1] = node_map_val[ntype][part_id][0]
# Update the edge_map_val to be contiguous.
etype_ids = {etype: g.get_etype_id(etype) for etype in g.canonical_etypes}
etype_ids_reverse = {v: k for k, v in etype_ids.items()}
for part_id in range(num_parts):
for etype_id in list(etype_ids.values()):
etype = etype_ids_reverse[etype_id]
start_id = edge_map_val[etype][part_id][0]
end_id = edge_map_val[etype][part_id][1]
if not (start_id == -1 and end_id == -1):
continue
prev_etype_id = (
etype_ids[etype] - 1
if etype_ids[etype] > 0
else max(etype_ids.values())
)
prev_etype = etype_ids_reverse[prev_etype_id]
if etype_ids[etype] == 0:
if part_id == 0:
edge_map_val[etype][part_id][0] = 0
else:
edge_map_val[etype][part_id][0] = edge_map_val[prev_etype][
part_id - 1
][1]
else:
edge_map_val[etype][part_id][0] = edge_map_val[prev_etype][
part_id
][1]
edge_map_val[etype][part_id][1] = edge_map_val[etype][part_id][0]
def partition_graph(
g,
graph_name,
num_parts,
out_path,
num_hops=1,
part_method="metis",
balance_ntypes=None,
balance_edges=False,
return_mapping=False,
num_trainers_per_machine=1,
objtype="cut",
graph_formats=None,
use_graphbolt=False,
**kwargs,
):
"""Partition a graph for distributed training and store the partitions on files.
The partitioning occurs in three steps: 1) run a partition algorithm (e.g., Metis) to
assign nodes to partitions; 2) construct partition graph structure based on
the node assignment; 3) split the node features and edge features based on
the partition result.
When a graph is partitioned, each partition can contain *HALO* nodes, which are assigned
to other partitions but are included in this partition for efficiency purpose.
In this document, *local nodes/edges* refers to the nodes and edges that truly belong to
a partition. The rest are "HALO nodes/edges".
The partitioned data is stored into multiple files organized as follows:
.. code-block:: none
data_root_dir/
|-- graph_name.json # partition configuration file in JSON
|-- node_map.npy # partition id of each node stored in a numpy array (optional)
|-- edge_map.npy # partition id of each edge stored in a numpy array (optional)
|-- part0/ # data for partition 0
|-- node_feats.dgl # node features stored in binary format
|-- edge_feats.dgl # edge features stored in binary format
|-- graph.dgl # graph structure of this partition stored in binary format
|-- part1/ # data for partition 1
|-- node_feats.dgl
|-- edge_feats.dgl
|-- graph.dgl
First, the metadata of the original graph and the partitioning is stored in a JSON file
named after ``graph_name``. This JSON file contains the information of the original graph
as well as the path of the files that store each partition. Below show an example.
.. code-block:: none
{
"graph_name" : "test",
"part_method" : "metis",
"num_parts" : 2,
"halo_hops" : 1,
"node_map": {
"_N": [ [ 0, 1261310 ],
[ 1261310, 2449029 ] ]
},
"edge_map": {
"_N:_E:_N": [ [ 0, 62539528 ],
[ 62539528, 123718280 ] ]
},
"etypes": { "_N:_E:_N": 0 },
"ntypes": { "_N": 0 },
"num_nodes" : 1000000,
"num_edges" : 52000000,
"part-0" : {
"node_feats" : "data_root_dir/part0/node_feats.dgl",
"edge_feats" : "data_root_dir/part0/edge_feats.dgl",
"part_graph" : "data_root_dir/part0/graph.dgl",
},
"part-1" : {
"node_feats" : "data_root_dir/part1/node_feats.dgl",
"edge_feats" : "data_root_dir/part1/edge_feats.dgl",
"part_graph" : "data_root_dir/part1/graph.dgl",
},
}
Here are the definition of the fields in the partition configuration file:
* ``graph_name`` is the name of the graph given by a user.
* ``part_method`` is the method used to assign nodes to partitions.
Currently, it supports "random" and "metis".
* ``num_parts`` is the number of partitions.
* ``halo_hops`` is the number of hops of nodes we include in a partition as HALO nodes.
* ``node_map`` is the node assignment map, which tells the partition ID a node is assigned to.
The format of ``node_map`` is described below.
* ``edge_map`` is the edge assignment map, which tells the partition ID an edge is assigned to.
* ``num_nodes`` is the number of nodes in the global graph.
* ``num_edges`` is the number of edges in the global graph.
* `part-*` stores the data of a partition.
As node/edge IDs are reshuffled, ``node_map`` and ``edge_map`` contains the information
for mapping between global node/edge IDs to partition-local node/edge IDs.
For heterogeneous graphs, the information in ``node_map`` and ``edge_map`` can also be used
to compute node types and edge types. The format of the data in ``node_map`` and ``edge_map``
is as follows:
.. code-block:: none
{
"node_type": [ [ part1_start, part1_end ],
[ part2_start, part2_end ],
... ],
...
},
Essentially, ``node_map`` and ``edge_map`` are dictionaries. The keys are
node etypes and canonical edge types respectively. The values are lists of pairs
containing the start and end of the ID range for the corresponding types in a partition.
The length of the list is the number of
partitions; each element in the list is a tuple that stores the start and the end of
an ID range for a particular node/edge type in the partition.
The graph structure of a partition is stored in a file with the DGLGraph format.
Nodes in each partition is *relabeled* to always start with zero. We call the node
ID in the original graph, *global ID*, while the relabeled ID in each partition,
*local ID*. Each partition graph has an integer node data tensor stored under name
`dgl.NID` and each value is the node's global ID. Similarly, edges are relabeled too
and the mapping from local ID to global ID is stored as an integer edge data tensor
under name `dgl.EID`. For a heterogeneous graph, the DGLGraph also contains a node
data `dgl.NTYPE` for node type and an edge data `dgl.ETYPE` for the edge type.
The partition graph contains additional node data ("inner_node") and
edge data ("inner_edge"):
* "inner_node" indicates whether a node belongs to a partition.
* "inner_edge" indicates whether an edge belongs to a partition.
Node and edge features are splitted and stored together with each graph partition.
All node/edge features in a partition are stored in a file with DGL format. The node/edge
features are stored in dictionaries, in which the key is the node/edge data name and
the value is a tensor. We do not store features of HALO nodes and edges.
When performing Metis partitioning, we can put some constraint on the partitioning.
Current, it supports two constrants to balance the partitioning. By default, Metis
always tries to balance the number of nodes in each partition.
* ``balance_ntypes`` balances the number of nodes of different types in each partition.
* ``balance_edges`` balances the number of edges in each partition.
To balance the node types, a user needs to pass a vector of N elements to indicate
the type of each node. N is the number of nodes in the input graph.
Parameters
----------
g : DGLGraph
The input graph to partition
graph_name : str
The name of the graph. The name will be used to construct
:py:meth:`~dgl.distributed.DistGraph`.
num_parts : int
The number of partitions
out_path : str
The path to store the files for all partitioned data.
num_hops : int, optional
The number of hops of HALO nodes we construct on a partition graph structure.
The default value is 1.
part_method : str, optional
The partition method. It supports "random" and "metis". The default value is "metis".
balance_ntypes : tensor, optional
Node type of each node. This is a 1D-array of integers. Its values indicates the node
type of each node. This argument is used by Metis partition. When the argument is
specified, the Metis algorithm will try to partition the input graph into partitions where
each partition has roughly the same number of nodes for each node type. The default value
is None, which means Metis partitions the graph to only balance the number of nodes.
balance_edges : bool
Indicate whether to balance the edges in each partition. This argument is used by
the Metis algorithm.
return_mapping : bool
Indicate whether to return the mapping between shuffled node/edge IDs and the original
node/edge IDs.
num_trainers_per_machine : int, optional
The number of trainers per machine. If is not 1, the whole graph will be first partitioned
to each trainer, that is num_parts*num_trainers_per_machine parts. And the trainer ids of
each node will be stored in the node feature 'trainer_id'. Then the partitions of trainers
on the same machine will be coalesced into one larger partition. The final number of
partitions is `num_part`.
objtype : str, "cut" or "vol"
Set the objective as edge-cut minimization or communication volume minimization. This
argument is used by the Metis algorithm.
graph_formats : str or list[str]