Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

[WIP] distributed training #1334

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 9 additions & 3 deletions scripts/pretraining/run_electra.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
import horovod.mxnet as hvd
except ImportError:
pass
try:
import byteps.mxnet as bps
except ImportError:
pass

mx.npx.set_np()

Expand Down Expand Up @@ -118,7 +122,7 @@ def parse_args():
help='The scale size of the generator layer')
# Communication
parser.add_argument('--comm_backend', type=str, default='device',
choices=['horovod', 'dist_sync_device', 'device'],
choices=['byteps', 'horovod', 'dist_sync_device', 'device'],
help='Communication backend.')
parser.add_argument('--gpus', type=str, default='0',
help='list of gpus to run, e.g. 0 or 0,2,5. -1 means using cpu.')
Expand Down Expand Up @@ -316,6 +320,8 @@ def train(args):
})
if args.comm_backend == 'horovod':
trainer = hvd.DistributedTrainer(param_dict, args.optimizer, optimizer_params)
elif args.comm_backend == 'byteps':
trainer = bps.DistributedTrainer(param_dict, args.optimizer, optimizer_params)
else:
trainer = mx.gluon.Trainer(param_dict, args.optimizer, optimizer_params,
update_on_kvstore=False)
Expand Down Expand Up @@ -414,8 +420,8 @@ def train(args):
total_norm, ratio, is_finite = clip_grad_global_norm(
params, args.max_grad_norm * num_workers)

if args.comm_backend == 'horovod':
# Note that horovod.trainer._scale is default to num_workers,
if args.comm_backend == 'horovod' or args.comm_backend == 'byteps':
# Note that hvd.trainer._scale and bps.trainer._scale are default to num_workers,
# thus trainer.update(1) will scale the gradients by 1./num_workers
trainer.update(1, ignore_stale_grad=True)
else:
Expand Down
14 changes: 14 additions & 0 deletions src/gluonnlp/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,20 @@ def init_comm(backend, gpus):
is_master_node = rank == local_rank
ctx_l = [mx.gpu(local_rank)]
logging.info('GPU communication supported by horovod')
elif backend == 'byteps':
try:
import byteps.mxnet as bps
except ImportError:
logging.info('BytePS must be installed.')
sys.exit(1)
bps.init()
store = None
num_workers = bps.size()
rank = bps.rank()
local_rank = bps.local_rank()
is_master_node = rank == local_rank
ctx_l = [mx.gpu(local_rank)]
logging.info('GPU communication supported by BytePS')
else:
store = mx.kv.create(backend)
num_workers = store.num_workers
Expand Down