|
| 1 | +# Copyright (c) 2014-2024, Lawrence Livermore National Security, LLC. |
| 2 | +# Produced at the Lawrence Livermore National Laboratory. |
| 3 | +# Written by the LBANN Research Team (B. Van Essen, et al.) listed in |
| 4 | +# the CONTRIBUTORS file. See the top-level LICENSE file for details. |
| 5 | +# |
| 6 | +# LLNL-CODE-697807. |
| 7 | +# All rights reserved. |
| 8 | +# |
| 9 | +# This file is part of LBANN: Livermore Big Artificial Neural Network |
| 10 | +# Toolkit. For details, see http://software.llnl.gov/LBANN or |
| 11 | +# https://github.com/LBANN and https://github.com/LLNL/LBANN. |
| 12 | +# |
| 13 | +# SPDX-License-Identifier: (Apache-2.0) |
| 14 | + |
| 15 | +from DGraph.data.ogbn_datasets import process_homogenous_data |
| 16 | +from ogb.nodeproppred import NodePropPredDataset |
| 17 | +from fire import Fire |
| 18 | +import os |
| 19 | +import torch |
| 20 | +from DGraph.distributed.nccl._nccl_cache import ( |
| 21 | + NCCLGatherCacheGenerator, |
| 22 | + NCCLScatterCacheGenerator, |
| 23 | +) |
| 24 | +from time import perf_counter |
| 25 | +from tqdm import tqdm |
| 26 | +from multiprocessing import get_context |
| 27 | + |
| 28 | + |
| 29 | +cache_prefix = { |
| 30 | + "ogbn-arxiv": "arxiv", |
| 31 | + "ogbn-products": "products", |
| 32 | + "ogbn-papers100M": "papers100M", |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +def generate_cache_file( |
| 37 | + dist_graph, |
| 38 | + src_indices, |
| 39 | + dst_indices, |
| 40 | + edge_placement, |
| 41 | + edge_src_placement, |
| 42 | + edge_dest_placement, |
| 43 | + cache_prefix_str: str, |
| 44 | + rank: int, |
| 45 | + world_size: int, |
| 46 | +): |
| 47 | + print(f"Generating cache for rank {rank}...") |
| 48 | + local_node_features = dist_graph.get_local_node_features(rank).unsqueeze(0) |
| 49 | + num_input_rows = local_node_features.size(1) |
| 50 | + |
| 51 | + print( |
| 52 | + f"Rank {rank} has {num_input_rows} input rows with shape {local_node_features.shape}" |
| 53 | + ) |
| 54 | + gather_cache = NCCLGatherCacheGenerator( |
| 55 | + dst_indices, |
| 56 | + edge_placement, |
| 57 | + edge_dest_placement, |
| 58 | + num_input_rows, |
| 59 | + rank, |
| 60 | + world_size, |
| 61 | + ) |
| 62 | + |
| 63 | + nodes_per_rank = dist_graph.get_nodes_per_rank() |
| 64 | + nodes_per_rank = int(nodes_per_rank[rank].item()) |
| 65 | + |
| 66 | + scatter_cache = NCCLScatterCacheGenerator( |
| 67 | + src_indices, |
| 68 | + edge_placement, |
| 69 | + edge_src_placement, |
| 70 | + nodes_per_rank, |
| 71 | + rank, |
| 72 | + world_size, |
| 73 | + ) |
| 74 | + print(f"Rank {rank} completed cache generation") |
| 75 | + with open( |
| 76 | + f"{cache_prefix_str}_gather_cache_rank_{world_size}_{rank}.pt", "wb" |
| 77 | + ) as f: |
| 78 | + torch.save(gather_cache, f) |
| 79 | + |
| 80 | + with open( |
| 81 | + f"{cache_prefix_str}_scatter_cache_rank_{world_size}_{rank}.pt", "wb" |
| 82 | + ) as f: |
| 83 | + torch.save(scatter_cache, f) |
| 84 | + return 0 |
| 85 | + |
| 86 | + |
| 87 | +def main(dset: str, world_size: int, node_rank_placement_file: str): |
| 88 | + assert dset in ["ogbn-arxiv", "ogbn-products", "ogbn-papers100M"] |
| 89 | + |
| 90 | + assert world_size > 0 |
| 91 | + assert os.path.exists( |
| 92 | + node_rank_placement_file |
| 93 | + ), "Node rank placement file does not exist." |
| 94 | + |
| 95 | + node_rank_placement = torch.load(node_rank_placement_file) |
| 96 | + |
| 97 | + dataset = NodePropPredDataset( |
| 98 | + dset, |
| 99 | + ) |
| 100 | + |
| 101 | + split_index = dataset.get_idx_split() |
| 102 | + assert split_index is not None, "Split index is None." |
| 103 | + |
| 104 | + graph, labels = dataset[0] |
| 105 | + |
| 106 | + num_edges = graph["edge_index"].shape |
| 107 | + print(num_edges) |
| 108 | + |
| 109 | + dist_graph = process_homogenous_data( |
| 110 | + graph_data=graph, |
| 111 | + labels=labels, |
| 112 | + world_Size=world_size, |
| 113 | + split_idx=split_index, |
| 114 | + node_rank_placement=node_rank_placement, |
| 115 | + rank=0, |
| 116 | + ) |
| 117 | + |
| 118 | + edge_indices = dist_graph.get_global_edge_indices() |
| 119 | + rank_mappings = dist_graph.get_global_rank_mappings() |
| 120 | + |
| 121 | + print("Edge indices shape:", edge_indices.shape) |
| 122 | + print("Rank mappings shape:", rank_mappings.shape) |
| 123 | + |
| 124 | + edge_indices = edge_indices.unsqueeze(0) |
| 125 | + src_indices = edge_indices[:, 0, :] |
| 126 | + dst_indices = edge_indices[:, 1, :] |
| 127 | + |
| 128 | + edge_placement = rank_mappings[0] |
| 129 | + edge_src_placement = rank_mappings[0] |
| 130 | + edge_dest_placement = rank_mappings[1] |
| 131 | + |
| 132 | + start_time = perf_counter() |
| 133 | + cache_prefix_str = f"cache/{cache_prefix[dset]}" |
| 134 | + with get_context("spawn").Pool(min(world_size, 8)) as pool: |
| 135 | + args = [ |
| 136 | + ( |
| 137 | + dist_graph, |
| 138 | + src_indices, |
| 139 | + dst_indices, |
| 140 | + edge_placement, |
| 141 | + edge_src_placement, |
| 142 | + edge_dest_placement, |
| 143 | + cache_prefix_str, |
| 144 | + rank, |
| 145 | + world_size, |
| 146 | + ) |
| 147 | + for rank in range(world_size) |
| 148 | + ] |
| 149 | + |
| 150 | + out = pool.starmap(generate_cache_file, args) |
| 151 | + |
| 152 | + end_time = perf_counter() |
| 153 | + print(f"Cache generation time: {end_time - start_time:.4f} seconds") |
| 154 | + print("Cache files generated successfully.") |
| 155 | + print( |
| 156 | + f"Gather cache file: {cache_prefix_str}_gather_cache_rank_{world_size}_<rank>.pt" |
| 157 | + ) |
| 158 | + print( |
| 159 | + f"Scatter cache file: {cache_prefix_str}_scatter_cache_rank_{world_size}_<rank>.pt" |
| 160 | + ) |
| 161 | + |
| 162 | + |
| 163 | +if __name__ == "__main__": |
| 164 | + Fire(main) |
0 commit comments