|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +""" |
| 5 | +Reduce NVLS Pipeline Test |
| 6 | +
|
| 7 | +This file tests the executor MULTI_LOAD_REDUCE_STORE operation in a |
| 8 | +pipeline context using SwitchChannel. Each GPU reduces |
| 9 | +its chunk via the NVSwitch and broadcasts the result, processing data |
| 10 | +in a pipelined loop over fixed-size iterations. |
| 11 | +""" |
| 12 | + |
| 13 | +import argparse |
| 14 | +from mscclpp.language.channel import * |
| 15 | +from mscclpp.language.rank import * |
| 16 | +from mscclpp.language.general import * |
| 17 | +from mscclpp.language.program import * |
| 18 | +from mscclpp.language.collectives import * |
| 19 | +from mscclpp.language.loop import LoopIterationContext |
| 20 | + |
| 21 | + |
| 22 | +def reduce_nvls_pipeline(name, gpu_size, num_threads_per_block, min_message_size, max_message_size): |
| 23 | + chunksperloop = 1 |
| 24 | + collective = AllReduce(gpu_size, chunksperloop, True) |
| 25 | + with CollectiveProgram( |
| 26 | + name, |
| 27 | + collective, |
| 28 | + gpu_size, |
| 29 | + instances=1, |
| 30 | + protocol="Simple", |
| 31 | + num_threads_per_block=num_threads_per_block, |
| 32 | + use_double_scratch_buffer=False, |
| 33 | + min_message_size=min_message_size, |
| 34 | + max_message_size=max_message_size, |
| 35 | + ): |
| 36 | + # Creating Channels |
| 37 | + nvls_chan = SwitchChannel(rank_list=[gpu for gpu in range(gpu_size)], buffer_type=BufferType.input) |
| 38 | + channels = {} |
| 39 | + for gpu in range(gpu_size): |
| 40 | + for peer in range(gpu_size): |
| 41 | + if peer != gpu: |
| 42 | + channels[(peer, gpu)] = MemoryChannel(peer, gpu) |
| 43 | + |
| 44 | + # Synchronization to Ensure all the GPUs are Ready |
| 45 | + for gpu in range(gpu_size): |
| 46 | + src_rank = gpu |
| 47 | + for peer in range(gpu_size): |
| 48 | + if peer != src_rank: |
| 49 | + dst_rank = peer |
| 50 | + channels[(dst_rank, src_rank)].signal(tb=0, relaxed=True) |
| 51 | + for peer in range(gpu_size): |
| 52 | + if peer != src_rank: |
| 53 | + dst_rank = peer |
| 54 | + channels[(dst_rank, src_rank)].wait(tb=0, relaxed=True, data_sync=SyncType.after) |
| 55 | + |
| 56 | + # Pipeline Reducing and Storing the data |
| 57 | + with LoopIterationContext(unit=2**20, num_chunks=1): |
| 58 | + for gpu in range(gpu_size): |
| 59 | + buffer_offset = gpu |
| 60 | + rank = Rank(gpu) |
| 61 | + input_buffer = rank.get_input_buffer() |
| 62 | + nvls_chan.at_rank(gpu).reduce( |
| 63 | + buffer_offset=buffer_offset, size=1, dst_chunk=input_buffer[gpu : gpu + 1], tb=0 |
| 64 | + ) |
| 65 | + nvls_chan.at_rank(gpu).broadcast( |
| 66 | + src_chunk=input_buffer[gpu : gpu + 1], buffer_offset=buffer_offset, size=1, tb=0 |
| 67 | + ) |
| 68 | + |
| 69 | + # Synchronization to Ensure the GPUs finished |
| 70 | + for gpu in range(gpu_size): |
| 71 | + src_rank = gpu |
| 72 | + for peer in range(gpu_size): |
| 73 | + if peer != src_rank: |
| 74 | + dst_rank = peer |
| 75 | + channels[(dst_rank, src_rank)].signal(tb=0, relaxed=True, data_sync=SyncType.before) |
| 76 | + for peer in range(gpu_size): |
| 77 | + if peer != src_rank: |
| 78 | + dst_rank = peer |
| 79 | + channels[(dst_rank, src_rank)].wait(tb=0, relaxed=True) |
| 80 | + |
| 81 | + print(JSON()) |
| 82 | + |
| 83 | + |
| 84 | +parser = argparse.ArgumentParser() |
| 85 | + |
| 86 | +parser.add_argument("--name", type=str, help="name of the program") |
| 87 | +parser.add_argument("--num_gpus", type=int, help="number of gpus") |
| 88 | +parser.add_argument("--num_threads_per_block", type=int, default=1024, help="number of threads per block") |
| 89 | +parser.add_argument("--min_message_size", type=int, default=0, help="minimum message size") |
| 90 | +parser.add_argument("--max_message_size", type=int, default=2**64 - 1, help="maximum message size") |
| 91 | + |
| 92 | +args = parser.parse_args() |
| 93 | + |
| 94 | +reduce_nvls_pipeline(args.name, args.num_gpus, args.num_threads_per_block, args.min_message_size, args.max_message_size) |
0 commit comments