Skip to content

Fix conditions about using single GPU #841

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions imagenet/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ def main_worker(gpu, ngpus_per_node, args):
elif args.gpu is not None:
torch.cuda.set_device(args.gpu)
model = model.cuda(args.gpu)
elif torch.cuda.device_count() == 1:
model = model.cuda()
else:
# DataParallel will divide and allocate batch_size to all available GPUs
if args.arch.startswith('alexnet') or args.arch.startswith('vgg'):
Expand Down Expand Up @@ -281,7 +283,7 @@ def train(train_loader, model, criterion, optimizer, epoch, args):
# measure data loading time
data_time.update(time.time() - end)

if args.gpu is not None:
if args.gpu is not None or torch.cuda.device_count() == 1:
images = images.cuda(args.gpu, non_blocking=True)
if torch.cuda.is_available():
target = target.cuda(args.gpu, non_blocking=True)
Expand Down Expand Up @@ -325,7 +327,7 @@ def validate(val_loader, model, criterion, args):
with torch.no_grad():
end = time.time()
for i, (images, target) in enumerate(val_loader):
if args.gpu is not None:
if args.gpu is not None or torch.cuda.device_count() == 1:
images = images.cuda(args.gpu, non_blocking=True)
if torch.cuda.is_available():
target = target.cuda(args.gpu, non_blocking=True)
Expand Down