(
fn,
num_iters=50,
num_warmup=10,
unit: str = "ms",
between_fn=None,
prog=True,
barrier=False,
)
| 198 | |
| 199 | @torch.no_grad() |
| 200 | def timed( |
| 201 | fn, |
| 202 | num_iters=50, |
| 203 | num_warmup=10, |
| 204 | unit: str = "ms", |
| 205 | between_fn=None, |
| 206 | prog=True, |
| 207 | barrier=False, |
| 208 | ): |
| 209 | warmup_times = [] |
| 210 | times = [] |
| 211 | cpu_warmup_times = [] |
| 212 | cpu_times = [] |
| 213 | for itr in tqdm(range(num_iters + num_warmup), desc="Timing", disable=not prog): |
| 214 | if between_fn is not None: |
| 215 | between_fn() |
| 216 | torch.cuda.synchronize() |
| 217 | |
| 218 | if barrier: |
| 219 | torch.distributed.barrier() |
| 220 | |
| 221 | start = torch.cuda.Event(enable_timing=True) |
| 222 | end = torch.cuda.Event(enable_timing=True) |
| 223 | start.record() # type: ignore |
| 224 | cpu_start = time.perf_counter() |
| 225 | _ = fn() |
| 226 | cpu_end = time.perf_counter() |
| 227 | end.record() # type: ignore |
| 228 | torch.cuda.synchronize() |
| 229 | |
| 230 | if barrier: |
| 231 | torch.distributed.barrier() |
| 232 | |
| 233 | gpu_milis = start.elapsed_time(end) |
| 234 | cpu_milis = (cpu_end - cpu_start) * 1000 |
| 235 | |
| 236 | gpu_time = convert_unit(gpu_milis, unit) |
| 237 | cpu_time = convert_unit(cpu_milis, unit) |
| 238 | |
| 239 | if itr >= num_warmup: |
| 240 | times.append(gpu_time) |
| 241 | cpu_times.append(cpu_time) |
| 242 | else: |
| 243 | warmup_times.append(gpu_time) |
| 244 | cpu_warmup_times.append(cpu_time) |
| 245 | |
| 246 | return TimeResult( |
| 247 | times=times, |
| 248 | warmup_times=warmup_times, |
| 249 | cpu_times=cpu_times, |
| 250 | cpu_warmup_times=cpu_warmup_times, |
| 251 | ) |
| 252 | |
| 253 | |
| 254 | @torch.no_grad() |
nothing calls this directly
no test coverage detected