Skip to content

Commit

Permalink
🐞 Fix tensor detach and gpu count issues in benchmarking script (#100)
Browse files Browse the repository at this point in the history
Co-authored-by: Ashwin Vaidya <ashwinitinvaidya@gmail.com>
  • Loading branch information
ashwinvaidya17 and Ashwin Vaidya committed Feb 15, 2022
1 parent c611e43 commit 4bf4970
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
3 changes: 3 additions & 0 deletions anomalib/utils/sweep/helpers/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from typing import Dict, Iterable, List, Tuple, Union

import numpy as np
import torch
from omegaconf import DictConfig, ListConfig
from torch.utils.data import DataLoader

Expand Down Expand Up @@ -106,6 +107,7 @@ def get_torch_throughput(
Returns:
float: Inference throughput
"""
torch.set_grad_enabled(False)
model.eval()
inferencer = TorchInferencer(config, model)
torch_dataloader = MockImageLoader(config.dataset.image_size, len(test_dataset))
Expand All @@ -118,6 +120,7 @@ def get_torch_throughput(
inference_time = time.time() - start_time
throughput = len(test_dataset) / inference_time

torch.set_grad_enabled(True)
return throughput


Expand Down
5 changes: 3 additions & 2 deletions tools/benchmarking/benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@


import logging
import math
import multiprocessing
import time
from concurrent.futures import ProcessPoolExecutor, as_completed
Expand Down Expand Up @@ -136,12 +137,12 @@ def distribute_over_gpus():
run_configs = list(get_run_config(sweep_config.grid_search))
jobs = []
for device_id, run_split in enumerate(
range(0, len(run_configs), len(run_configs) // torch.cuda.device_count())
range(0, len(run_configs), math.ceil(len(run_configs) / torch.cuda.device_count()))
):
jobs.append(
executor.submit(
compute_on_gpu,
run_configs[run_split : run_split + len(run_configs) // torch.cuda.device_count()],
run_configs[run_split : run_split + math.ceil(len(run_configs) / torch.cuda.device_count())],
device_id + 1,
sweep_config.seed,
sweep_config.writer,
Expand Down

0 comments on commit 4bf4970

Please sign in to comment.