Skip to content

Commit

Permalink
fix #868
Browse files Browse the repository at this point in the history
  • Loading branch information
thomwolf committed Jul 23, 2019
1 parent 2c9a311 commit 6070b55
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
13 changes: 7 additions & 6 deletions examples/run_glue.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ def train(args, train_dataset, model, tokenizer):
raise ImportError("Please install apex from https://www.github.com/nvidia/apex to use fp16 training.")
model, optimizer = amp.initialize(model, optimizer, opt_level=args.fp16_opt_level)

# Distributed training (should be after apex fp16 initialization)
if args.local_rank != -1:
model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.local_rank],
output_device=args.local_rank,
find_unused_parameters=True)

# Train!
logger.info("***** Running training *****")
logger.info(" Num examples = %d", len(train_dataset))
Expand Down Expand Up @@ -411,13 +417,8 @@ def main():
if args.local_rank == 0:
torch.distributed.barrier() # Make sure only the first process in distributed training will download model & vocab

# Distributed and parallel training
model.to(args.device)
if args.local_rank != -1:
model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.local_rank],
output_device=args.local_rank,
find_unused_parameters=True)
elif args.n_gpu > 1:
if args.n_gpu > 1:
model = torch.nn.DataParallel(model)

logger.info("Training/evaluation parameters %s", args)
Expand Down
13 changes: 7 additions & 6 deletions examples/run_squad.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ def train(args, train_dataset, model, tokenizer):
raise ImportError("Please install apex from https://www.github.com/nvidia/apex to use fp16 training.")
model, optimizer = amp.initialize(model, optimizer, opt_level=args.fp16_opt_level)

# Distributed training (should be after apex fp16 initialization)
if args.local_rank != -1:
model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.local_rank],
output_device=args.local_rank,
find_unused_parameters=True)

# Train!
logger.info("***** Running training *****")
logger.info(" Num examples = %d", len(train_dataset))
Expand Down Expand Up @@ -450,13 +456,8 @@ def main():
if args.local_rank == 0:
torch.distributed.barrier() # Make sure only the first process in distributed training will download model & vocab

# Distributed and parrallel training
model.to(args.device)
if args.local_rank != -1:
model = torch.nn.parallel.DistributedDataParallel(model, device_ids=[args.local_rank],
output_device=args.local_rank,
find_unused_parameters=True)
elif args.n_gpu > 1:
if args.n_gpu > 1:
model = torch.nn.DataParallel(model)

logger.info("Training/evaluation parameters %s", args)
Expand Down

0 comments on commit 6070b55

Please sign in to comment.