Skip to content

Commit 8644487

Browse files
authored
[Fix] Fix bug in non-distributed multi-gpu training/testing (open-mmlab#7019)
* Fix bug in non-distributed training/testing * Add deprecated message
1 parent c37da19 commit 8644487

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

tools/test.py

+13-9
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,13 @@ def parse_args():
3838
'--gpu-ids',
3939
type=int,
4040
nargs='+',
41-
help='ids of gpus to use '
41+
help='(Deprecated, please use --gpu-id) ids of gpus to use '
42+
'(only applicable to non-distributed training)')
43+
parser.add_argument(
44+
'--gpu-id',
45+
type=int,
46+
default=0,
47+
help='id of gpu to use '
4248
'(only applicable to non-distributed testing)')
4349
parser.add_argument(
4450
'--format-only',
@@ -167,19 +173,17 @@ def main():
167173
ds_cfg.pipeline = replace_ImageToTensor(ds_cfg.pipeline)
168174

169175
if args.gpu_ids is not None:
170-
cfg.gpu_ids = args.gpu_ids
176+
cfg.gpu_ids = args.gpu_ids[0:1]
177+
warnings.warn('`--gpu-ids` is deprecated, please use `--gpu-id`. '
178+
'Because we only support single GPU mode in '
179+
'non-distributed testing. Use the first GPU '
180+
'in `gpu_ids` now.')
171181
else:
172-
cfg.gpu_ids = range(1)
182+
cfg.gpu_ids = [args.gpu_id]
173183

174184
# init distributed env first, since logger depends on the dist info.
175185
if args.launcher == 'none':
176186
distributed = False
177-
if len(cfg.gpu_ids) > 1:
178-
warnings.warn(
179-
f'We treat {cfg.gpu_ids} as gpu-ids, and reset to '
180-
f'{cfg.gpu_ids[0:1]} as gpu-ids to avoid potential error in '
181-
'non-distribute testing time.')
182-
cfg.gpu_ids = cfg.gpu_ids[0:1]
183187
else:
184188
distributed = True
185189
init_dist(args.launcher, **cfg.dist_params)

tools/train.py

+20-11
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,19 @@ def parse_args():
3737
group_gpus.add_argument(
3838
'--gpus',
3939
type=int,
40-
help='number of gpus to use '
40+
help='(Deprecated, please use --gpu-id) number of gpus to use '
4141
'(only applicable to non-distributed training)')
4242
group_gpus.add_argument(
4343
'--gpu-ids',
4444
type=int,
4545
nargs='+',
46-
help='ids of gpus to use '
46+
help='(Deprecated, please use --gpu-id) ids of gpus to use '
47+
'(only applicable to non-distributed training)')
48+
group_gpus.add_argument(
49+
'--gpu-id',
50+
type=int,
51+
default=0,
52+
help='id of gpu to use '
4753
'(only applicable to non-distributed training)')
4854
parser.add_argument('--seed', type=int, default=None, help='random seed')
4955
parser.add_argument(
@@ -113,20 +119,23 @@ def main():
113119
if args.resume_from is not None:
114120
cfg.resume_from = args.resume_from
115121
cfg.auto_resume = args.auto_resume
122+
if args.gpus is not None:
123+
cfg.gpu_ids = range(1)
124+
warnings.warn('`--gpus` is deprecated because we only support '
125+
'single GPU mode in non-distributed training. '
126+
'Use `gpus=1` now.')
116127
if args.gpu_ids is not None:
117-
cfg.gpu_ids = args.gpu_ids
118-
else:
119-
cfg.gpu_ids = range(1) if args.gpus is None else range(args.gpus)
128+
cfg.gpu_ids = args.gpu_ids[0:1]
129+
warnings.warn('`--gpu-ids` is deprecated, please use `--gpu-id`. '
130+
'Because we only support single GPU mode in '
131+
'non-distributed training. Use the first GPU '
132+
'in `gpu_ids` now.')
133+
if args.gpus is None and args.gpu_ids is None:
134+
cfg.gpu_ids = [args.gpu_id]
120135

121136
# init distributed env first, since logger depends on the dist info.
122137
if args.launcher == 'none':
123138
distributed = False
124-
if len(cfg.gpu_ids) > 1:
125-
warnings.warn(
126-
f'We treat {cfg.gpu_ids} as gpu-ids, and reset to '
127-
f'{cfg.gpu_ids[0:1]} as gpu-ids to avoid potential error in '
128-
'non-distribute training time.')
129-
cfg.gpu_ids = cfg.gpu_ids[0:1]
130139
else:
131140
distributed = True
132141
init_dist(args.launcher, **cfg.dist_params)

0 commit comments

Comments
 (0)