Skip to content

Commit

Permalink
Make select_device() robust to batch_size=-1 (ultralytics#5940)
Browse files Browse the repository at this point in the history
* Find out a bug. When set batch_size = -1 to use the autobatch.

reproduce:

* Fix type conflict

Co-authored-by: Glenn Jocher <glenn.jocher@ultralytics.com>
  • Loading branch information
youyuxiansen and glenn-jocher authored Dec 10, 2021
1 parent ad2571f commit 97d4e4b
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions utils/torch_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def git_describe(path=Path(__file__).parent): # path must be a directory
return '' # not a git repository


def select_device(device='', batch_size=None, newline=True):
def select_device(device='', batch_size=0, newline=True):
# device = 'cpu' or '0' or '0,1,2,3'
s = f'YOLOv5 🚀 {git_describe() or date_modified()} torch {torch.__version__} ' # string
device = str(device).strip().lower().replace('cuda:', '') # to string, 'cuda:0' to '0'
Expand All @@ -68,7 +68,7 @@ def select_device(device='', batch_size=None, newline=True):
if cuda:
devices = device.split(',') if device else '0' # range(torch.cuda.device_count()) # i.e. 0,1,6,7
n = len(devices) # device count
if n > 1 and batch_size: # check batch_size is divisible by device_count
if n > 1 and batch_size > 0: # check batch_size is divisible by device_count
assert batch_size % n == 0, f'batch-size {batch_size} not multiple of GPU count {n}'
space = ' ' * (len(s) + 1)
for i, d in enumerate(devices):
Expand Down

0 comments on commit 97d4e4b

Please sign in to comment.