Skip to content

Commit

Permalink
fixbug: dist test in torch 2.0 (#1602)
Browse files Browse the repository at this point in the history
* fixbug: torch 2.0 dist train with error local rank

* fixbug: dist test in torch 2.0
  • Loading branch information
jihanyang authored Apr 17, 2024
1 parent 839d8dd commit 8caccce
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
18 changes: 18 additions & 0 deletions tools/scripts/torch_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/usr/bin/env bash

set -x
NGPUS=$1
PY_ARGS=${@:2}

while true
do
PORT=$(( ((RANDOM<<15)|RANDOM) % 49152 + 10000 ))
status="$(nc -z 127.0.0.1 $PORT < /dev/null &>/dev/null; echo $?)"
if [ "${status}" != "0" ]; then
break;
fi
done
echo $PORT

torchrun --nproc_per_node=${NGPUS} --rdzv_endpoint=localhost:${PORT} test.py --launcher pytorch ${PY_ARGS}

5 changes: 4 additions & 1 deletion tools/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def parse_config():
parser.add_argument('--pretrained_model', type=str, default=None, help='pretrained_model')
parser.add_argument('--launcher', choices=['none', 'pytorch', 'slurm'], default='none')
parser.add_argument('--tcp_port', type=int, default=18888, help='tcp port for distrbuted training')
parser.add_argument('--local_rank', type=int, default=0, help='local rank for distributed training')
parser.add_argument('--local_rank', type=int, default=None, help='local rank for distributed training')
parser.add_argument('--set', dest='set_cfgs', default=None, nargs=argparse.REMAINDER,
help='set extra config keys if needed')

Expand Down Expand Up @@ -145,6 +145,9 @@ def main():
dist_test = False
total_gpus = 1
else:
if args.local_rank is None:
args.local_rank = int(os.environ.get('LOCAL_RANK', '0'))

total_gpus, cfg.LOCAL_RANK = getattr(common_utils, 'init_dist_%s' % args.launcher)(
args.tcp_port, args.local_rank, backend='nccl'
)
Expand Down

0 comments on commit 8caccce

Please sign in to comment.