|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +# DeepSpeed Team |
| 5 | + |
| 6 | +import torch |
| 7 | +import sys, os, time |
| 8 | + |
| 9 | +COMMS_BENCH_DIR = os.path.join(os.path.dirname(__file__), "../") |
| 10 | +sys.path.append(COMMS_BENCH_DIR) |
| 11 | + |
| 12 | +from communication.utils import * |
| 13 | +from communication.constants import * |
| 14 | +from deepspeed.accelerator import get_accelerator |
| 15 | +from deepspeed.comm import TorchBackend |
| 16 | + |
| 17 | + |
| 18 | +# Run all_gather and print metrics |
| 19 | +def timed_all_gather(input, output, args): |
| 20 | + if args.dist == 'torch': |
| 21 | + import torch.distributed as dist |
| 22 | + |
| 23 | + all_gather_func = TorchBackend.get_all_gather_function() |
| 24 | + elif args.dist == 'deepspeed': |
| 25 | + import deepspeed.comm as dist |
| 26 | + |
| 27 | + all_gather_func = dist.allgather_fn |
| 28 | + |
| 29 | + sync_all() |
| 30 | + # Warmups, establish connections, etc. |
| 31 | + for i in range(args.warmups): |
| 32 | + all_gather_func(output, input, group=None, async_op=args.async_op) |
| 33 | + sync_all() |
| 34 | + |
| 35 | + # time the actual comm op trials times and average it |
| 36 | + pre = time.perf_counter() |
| 37 | + for i in range(args.trials): |
| 38 | + all_gather_func(output, input, group=None, async_op=args.async_op) |
| 39 | + sync_all() |
| 40 | + duration = time.perf_counter() - pre |
| 41 | + |
| 42 | + # maintain and clean performance data |
| 43 | + avg_duration = duration / args.trials |
| 44 | + size = input.element_size() * input.nelement() |
| 45 | + tput, busbw = get_bw('all_gather', size, avg_duration, args) |
| 46 | + tput_str, busbw_str, duration_str = get_metric_strings(args, tput, busbw, avg_duration) |
| 47 | + desc = f'{input.nelement()}x{input.element_size()}' |
| 48 | + |
| 49 | + if not args.raw: |
| 50 | + size = convert_size(size) |
| 51 | + |
| 52 | + print_rank_0(f"{size:<20} {desc:25s} {duration_str:20s} {tput_str:20s} {busbw_str:20s}") |
| 53 | + |
| 54 | + |
| 55 | +def run_all_gather(local_rank, args): |
| 56 | + if args.dist == 'torch': |
| 57 | + import torch.distributed as dist |
| 58 | + elif args.dist == 'deepspeed': |
| 59 | + import deepspeed.comm as dist |
| 60 | + |
| 61 | + # Prepare benchmark header |
| 62 | + print_header(args, 'all_gather') |
| 63 | + global_rank = dist.get_rank() |
| 64 | + world_size = dist.get_world_size() |
| 65 | + |
| 66 | + if args.scan: |
| 67 | + # Create list of message sizes |
| 68 | + M_LIST = [] |
| 69 | + for x in (2**p for p in range(1, args.maxsize)): |
| 70 | + M_LIST.append(x) |
| 71 | + |
| 72 | + sync_all() |
| 73 | + # loop over various tensor sizes |
| 74 | + for M in M_LIST: |
| 75 | + global_rank = dist.get_rank() |
| 76 | + try: |
| 77 | + mat = torch.ones(world_size, M, |
| 78 | + dtype=getattr(torch, args.dtype)).to(get_accelerator().device_name(local_rank)) |
| 79 | + sync_all() |
| 80 | + input = ((mat.mul_(float(global_rank))).view(-1)) |
| 81 | + # Delete original mat to avoid OOM |
| 82 | + del mat |
| 83 | + get_accelerator().empty_cache() |
| 84 | + output = torch.zeros(input.nelement() * world_size, |
| 85 | + dtype=getattr(torch, args.dtype)).to(get_accelerator().device_name(local_rank)) |
| 86 | + except RuntimeError as e: |
| 87 | + if 'out of memory' in str(e): |
| 88 | + if dist.get_rank() == 0: |
| 89 | + print('WARNING: Ran out of GPU memory. Exiting comm op.') |
| 90 | + sync_all() |
| 91 | + break |
| 92 | + else: |
| 93 | + raise e |
| 94 | + sync_all() |
| 95 | + timed_all_gather(input, output, args) |
| 96 | + else: |
| 97 | + # all_gather_into_tensor saves memory |
| 98 | + if ((args.dist == 'torch' or args.dist == 'deepspeed') and dist.has_all_gather_into_tensor()): |
| 99 | + mem_factor = args.mem_factor + 0.2 |
| 100 | + else: |
| 101 | + mem_factor = args.mem_factor |
| 102 | + # Send the biggest message size our GPUs can fit. If you're facing OOM errors, reduce the mem_factor |
| 103 | + sync_all() |
| 104 | + elements_per_gpu = max_numel(comm_op='all_gather', |
| 105 | + dtype=getattr(torch, args.dtype), |
| 106 | + mem_factor=mem_factor, |
| 107 | + local_rank=local_rank, |
| 108 | + args=args) |
| 109 | + try: |
| 110 | + mat = torch.ones(elements_per_gpu, dtype=getattr(torch, |
| 111 | + args.dtype)).to(get_accelerator().device_name(local_rank)) |
| 112 | + # multiply each GPU's tensor by the rank to ease debugging |
| 113 | + input = ((mat.mul_(float(global_rank))).view(-1)) |
| 114 | + # Delete original mat to avoid OOM |
| 115 | + del mat |
| 116 | + get_accelerator().empty_cache() |
| 117 | + output = torch.zeros(elements_per_gpu * world_size, |
| 118 | + dtype=getattr(torch, args.dtype)).to(get_accelerator().device_name(local_rank)) |
| 119 | + except RuntimeError as e: |
| 120 | + if 'out of memory' in str(e): |
| 121 | + if dist.get_rank() == 0: |
| 122 | + print('WARNING: Ran out of GPU memory. Try to reduce the --mem-factor argument!') |
| 123 | + sync_all() |
| 124 | + return |
| 125 | + else: |
| 126 | + raise e |
| 127 | + |
| 128 | + sync_all() |
| 129 | + timed_all_gather(input, output, args) |
| 130 | + |
| 131 | + |
| 132 | +if __name__ == "__main__": |
| 133 | + args = benchmark_parser().parse_args() |
| 134 | + rank = args.local_rank |
| 135 | + init_processes(local_rank=rank, args=args) |
| 136 | + run_all_gather(local_rank=rank, args=args) |
0 commit comments