|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +import argparse |
| 5 | +from mscclpp.language.channel import * |
| 6 | +from mscclpp.language.rank import * |
| 7 | +from mscclpp.language.general import * |
| 8 | +from mscclpp.language.program import * |
| 9 | +from mscclpp.language.collectives import * |
| 10 | + |
| 11 | + |
| 12 | +def allgather_example(name, gpu_size, num_threads_per_block, min_message_size, max_message_size): |
| 13 | + chunksperloop = 1 |
| 14 | + collective = AllGather(gpu_size, chunksperloop, True) |
| 15 | + with CollectiveProgram( |
| 16 | + name, |
| 17 | + collective, |
| 18 | + gpu_size, |
| 19 | + protocol="LL", |
| 20 | + num_threads_per_block=num_threads_per_block, |
| 21 | + use_double_scratch_buffer=True, |
| 22 | + min_message_size=min_message_size, |
| 23 | + max_message_size=max_message_size, |
| 24 | + ): |
| 25 | + # Creating Scratch Buffers |
| 26 | + scratch_buffer = [] |
| 27 | + for gpu in range(gpu_size): |
| 28 | + scratch_buffer.append(Buffer(gpu, 2 * gpu_size)) |
| 29 | + |
| 30 | + # Copying it to scratch buffer |
| 31 | + for gpu in range(gpu_size): |
| 32 | + rank = Rank(gpu) |
| 33 | + scratch_offset = gpu_size |
| 34 | + input_buffer = rank.get_input_buffer() |
| 35 | + rank.copy_packets( |
| 36 | + scratch_buffer[gpu][scratch_offset + gpu : scratch_offset + gpu + 1], input_buffer[0:1], tb=0 |
| 37 | + ) |
| 38 | + |
| 39 | + # Putting packets in the remote scratch buffer |
| 40 | + for gpu in range(gpu_size): |
| 41 | + rank = Rank(gpu) |
| 42 | + output_buffer = rank.get_output_buffer() |
| 43 | + for peer in range(1, gpu_size): |
| 44 | + dst_rank = (gpu + peer) % gpu_size |
| 45 | + ch = MemoryChannel(dst_rank, gpu) |
| 46 | + tb = 0 |
| 47 | + ch.read_put_packets( |
| 48 | + scratch_buffer[dst_rank][gpu : gpu + 1], |
| 49 | + scratch_buffer[gpu][scratch_offset + gpu : scratch_offset + gpu + 1], |
| 50 | + tb, |
| 51 | + ) |
| 52 | + |
| 53 | + # Copying packets from local scratch buffer to local buffer |
| 54 | + for gpu in range(gpu_size): |
| 55 | + rank = Rank(gpu) |
| 56 | + output_buffer = rank.get_output_buffer() |
| 57 | + for peer in range(1, gpu_size): |
| 58 | + dst_rank = (gpu + peer) % gpu_size |
| 59 | + rank.unpack_packets( |
| 60 | + output_buffer[dst_rank : dst_rank + 1], |
| 61 | + scratch_buffer[gpu][dst_rank : dst_rank + 1], |
| 62 | + tb=0, |
| 63 | + ) |
| 64 | + |
| 65 | + print(JSON()) |
| 66 | + |
| 67 | + |
| 68 | +parser = argparse.ArgumentParser() |
| 69 | + |
| 70 | +parser.add_argument("--name", type=str, help="name of the program") |
| 71 | +parser.add_argument("--num_gpus", type=int, help="number of gpus") |
| 72 | +parser.add_argument("--num_threads_per_block", type=int, default=1024, help="number of threads per block") |
| 73 | +parser.add_argument("--min_message_size", type=int, default=0, help="minimum message size") |
| 74 | +parser.add_argument("--max_message_size", type=int, default=2**64 - 1, help="maximum message size") |
| 75 | + |
| 76 | +args = parser.parse_args() |
| 77 | + |
| 78 | +allgather_example(args.name, args.num_gpus, args.num_threads_per_block, args.min_message_size, args.max_message_size) |
0 commit comments