|
| 1 | +"""Communication monitoring helpers for straggler analysis.""" |
| 2 | + |
| 3 | +import time |
| 4 | +from collections import defaultdict |
| 5 | +from typing import Any |
| 6 | + |
| 7 | + |
| 8 | +class CommStatsCollector: |
| 9 | + """Collect basic communication timings.""" |
| 10 | + |
| 11 | + def __init__(self, enabled: bool = True): |
| 12 | + self.enabled = enabled |
| 13 | + self.operation_stats: dict[str, dict[str, Any]] = defaultdict( |
| 14 | + lambda: { |
| 15 | + "count": 0, |
| 16 | + "total_time": 0.0, |
| 17 | + "min_time": float("inf"), |
| 18 | + "max_time": 0.0, |
| 19 | + "rank_times": defaultdict(list), |
| 20 | + } |
| 21 | + ) |
| 22 | + self.backend = "unknown" |
| 23 | + self.world_size = 1 |
| 24 | + self.rank = 0 |
| 25 | + |
| 26 | + def set_backend_info(self, backend: str, world_size: int, rank: int): |
| 27 | + self.backend = backend |
| 28 | + self.world_size = world_size |
| 29 | + self.rank = rank |
| 30 | + |
| 31 | + def record_operation( |
| 32 | + self, |
| 33 | + op_type: str, |
| 34 | + op_name: str, |
| 35 | + start_time: float, |
| 36 | + end_time: float, |
| 37 | + data_size: int | None = None, |
| 38 | + target_ranks: list | None = None, |
| 39 | + ): |
| 40 | + if not self.enabled: |
| 41 | + return |
| 42 | + |
| 43 | + duration = end_time - start_time |
| 44 | + key = f"{op_type}_{op_name}" |
| 45 | + stats = self.operation_stats[key] |
| 46 | + stats["count"] += 1 |
| 47 | + stats["total_time"] += duration |
| 48 | + stats["min_time"] = min(stats["min_time"], duration) |
| 49 | + stats["max_time"] = max(stats["max_time"], duration) |
| 50 | + stats["rank_times"][self.rank].append(duration) |
| 51 | + |
| 52 | + if data_size is not None: |
| 53 | + stats["total_data_size"] = stats.get("total_data_size", 0) + data_size |
| 54 | + |
| 55 | + if target_ranks is not None: |
| 56 | + stats["target_ranks"] = target_ranks |
| 57 | + |
| 58 | + def get_operation_stats(self, op_type: str, op_name: str) -> dict[str, Any]: |
| 59 | + return self.operation_stats[f"{op_type}_{op_name}"].copy() |
| 60 | + |
| 61 | + def get_all_stats(self) -> dict[str, dict[str, Any]]: |
| 62 | + return dict(self.operation_stats) |
| 63 | + |
| 64 | + def get_straggler_operations(self, threshold: float = 2.0) -> list: |
| 65 | + stragglers = [] |
| 66 | + for op_key, stats in self.operation_stats.items(): |
| 67 | + if stats["count"] == 0: |
| 68 | + continue |
| 69 | + avg_time = stats["total_time"] / stats["count"] |
| 70 | + max_time = stats["max_time"] |
| 71 | + if avg_time > 0 and max_time / avg_time >= threshold: |
| 72 | + stragglers.append( |
| 73 | + { |
| 74 | + "operation": op_key, |
| 75 | + "avg_time": avg_time, |
| 76 | + "max_time": max_time, |
| 77 | + "slowdown_ratio": max_time / avg_time, |
| 78 | + "count": stats["count"], |
| 79 | + } |
| 80 | + ) |
| 81 | + return stragglers |
| 82 | + |
| 83 | + |
| 84 | +class NCCLCommHook: |
| 85 | + """Wrap NCCL collectives with timing.""" |
| 86 | + |
| 87 | + def __init__(self, collector: CommStatsCollector): |
| 88 | + self.collector = collector |
| 89 | + |
| 90 | + def wrap_all_reduce(self, op_func): |
| 91 | + def wrapped(*args, **kwargs): |
| 92 | + start_time = time.perf_counter() |
| 93 | + result = op_func(*args, **kwargs) |
| 94 | + end_time = time.perf_counter() |
| 95 | + self.collector.record_operation( |
| 96 | + "all_reduce", |
| 97 | + "default", |
| 98 | + start_time, |
| 99 | + end_time, |
| 100 | + ) |
| 101 | + return result |
| 102 | + |
| 103 | + return wrapped |
| 104 | + |
| 105 | + def wrap_broadcast(self, op_func): |
| 106 | + def wrapped(*args, **kwargs): |
| 107 | + start_time = time.perf_counter() |
| 108 | + result = op_func(*args, **kwargs) |
| 109 | + end_time = time.perf_counter() |
| 110 | + self.collector.record_operation( |
| 111 | + "broadcast", |
| 112 | + "default", |
| 113 | + start_time, |
| 114 | + end_time, |
| 115 | + ) |
| 116 | + return result |
| 117 | + |
| 118 | + return wrapped |
| 119 | + |
| 120 | + |
| 121 | +class GlooCommHook: |
| 122 | + """Wrap Gloo collectives with timing.""" |
| 123 | + |
| 124 | + def __init__(self, collector: CommStatsCollector): |
| 125 | + self.collector = collector |
| 126 | + |
| 127 | + def wrap_all_reduce(self, op_func): |
| 128 | + def wrapped(*args, **kwargs): |
| 129 | + start_time = time.perf_counter() |
| 130 | + result = op_func(*args, **kwargs) |
| 131 | + end_time = time.perf_counter() |
| 132 | + self.collector.record_operation( |
| 133 | + "all_reduce", |
| 134 | + "default", |
| 135 | + start_time, |
| 136 | + end_time, |
| 137 | + ) |
| 138 | + return result |
| 139 | + |
| 140 | + return wrapped |
| 141 | + |
| 142 | + |
| 143 | +class CommProfiler: |
| 144 | + """Backend-aware communication profiler.""" |
| 145 | + |
| 146 | + def __init__(self, backend: str = "auto", enabled: bool = True): |
| 147 | + self.collector = CommStatsCollector(enabled=enabled) |
| 148 | + self.hooks = {} |
| 149 | + |
| 150 | + if backend == "auto": |
| 151 | + backend = self._detect_backend() |
| 152 | + |
| 153 | + if backend == "nccl": |
| 154 | + self.hooks["nccl"] = NCCLCommHook(self.collector) |
| 155 | + elif backend == "gloo": |
| 156 | + self.hooks["gloo"] = GlooCommHook(self.collector) |
| 157 | + |
| 158 | + self.collector.set_backend_info(backend, 1, 0) |
| 159 | + |
| 160 | + def _detect_backend(self) -> str: |
| 161 | + try: |
| 162 | + import torch |
| 163 | + |
| 164 | + if torch.cuda.is_available(): |
| 165 | + return "nccl" |
| 166 | + except ImportError: |
| 167 | + pass |
| 168 | + return "gloo" |
| 169 | + |
| 170 | + def wrap_operation(self, op_type: str, op_func): |
| 171 | + backend = self.collector.backend |
| 172 | + if backend in self.hooks: |
| 173 | + if op_type == "all_reduce": |
| 174 | + return self.hooks[backend].wrap_all_reduce(op_func) |
| 175 | + if op_type == "broadcast" and hasattr(self.hooks[backend], "wrap_broadcast"): |
| 176 | + return self.hooks[backend].wrap_broadcast(op_func) |
| 177 | + return op_func |
| 178 | + |
| 179 | + def record_custom_operation( |
| 180 | + self, |
| 181 | + op_type: str, |
| 182 | + op_name: str, |
| 183 | + start_time: float, |
| 184 | + end_time: float, |
| 185 | + data_size: int | None = None, |
| 186 | + ): |
| 187 | + self.collector.record_operation( |
| 188 | + op_type, |
| 189 | + op_name, |
| 190 | + start_time, |
| 191 | + end_time, |
| 192 | + data_size=data_size, |
| 193 | + ) |
0 commit comments