(model, dataloader, num_warmup_batches=10, gpu_idx=0)
| 188 | |
| 189 | |
| 190 | def benchmark_inference(model, dataloader, num_warmup_batches=10, gpu_idx=0): |
| 191 | model.eval() |
| 192 | device = next(model.parameters()).device |
| 193 | |
| 194 | torch.cuda.reset_peak_memory_stats() |
| 195 | |
| 196 | power_readings = [] |
| 197 | max_allocated_memory = 0 |
| 198 | max_reserved_memory = 0 |
| 199 | |
| 200 | with Progress( |
| 201 | TextColumn("[progress.description]{task.description}"), |
| 202 | BarColumn(), |
| 203 | TextColumn("[progress.percentage]{task.percentage:>3.0f}%"), |
| 204 | TimeRemainingColumn(), |
| 205 | TimeElapsedColumn(), |
| 206 | ) as progress: |
| 207 | warmup_task = progress.add_task("[yellow]Warmup", total=num_warmup_batches) |
| 208 | with torch.no_grad(): |
| 209 | for i, batch in enumerate(dataloader): |
| 210 | if i >= num_warmup_batches: |
| 211 | break |
| 212 | input_ids, attention_mask, _ = [t.to(device) for t in batch] |
| 213 | with torch.cuda.amp.autocast(dtype=torch.bfloat16): |
| 214 | _ = model(input_ids, attention_mask=attention_mask) |
| 215 | progress.update(warmup_task, advance=1) |
| 216 | |
| 217 | inference_task = progress.add_task("[cyan]Inference", total=len(dataloader)) |
| 218 | total_time = 0 |
| 219 | with torch.no_grad(): |
| 220 | run_start_time = time.time() |
| 221 | for i, batch in enumerate(dataloader): |
| 222 | input_ids, attention_mask, _ = [t.to(device) for t in batch] |
| 223 | with torch.cuda.amp.autocast(dtype=torch.bfloat16): |
| 224 | _ = model(input_ids, attention_mask=attention_mask) |
| 225 | progress.update(inference_task, advance=1) |
| 226 | if i % 10 == 0: |
| 227 | power_readings.append(get_gpu_power(gpu_idx)) |
| 228 | max_allocated_memory = max(max_allocated_memory, torch.cuda.max_memory_allocated()) |
| 229 | max_reserved_memory = max(max_reserved_memory, torch.cuda.max_memory_reserved()) |
| 230 | run_end_time = time.time() |
| 231 | total_time += run_end_time - run_start_time |
| 232 | |
| 233 | avg_run_time = total_time |
| 234 | avg_power = np.mean(power_readings) |
| 235 | max_power = np.max(power_readings) |
| 236 | return avg_run_time, avg_power, max_power, max_allocated_memory, max_reserved_memory |
| 237 | |
| 238 | |
| 239 | def create_dummy_data(num_samples, seq_length, vocab_size, model_type): |
no test coverage detected