Skip to content

Commit 2edb763

Browse files
committed
logging fix
1 parent 160c6fa commit 2edb763

1 file changed

Lines changed: 52 additions & 44 deletions

File tree

alf/bin/train.py

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -231,52 +231,60 @@ def training_worker(rank: int,
231231
paras_queue (Queue): a shared Queue for checking the consistency of model parameters
232232
in different worker processes, if multi-gpu training is used.
233233
"""
234-
try:
235-
_setup_logging(log_dir=root_dir, rank=rank)
236-
_setup_device()
237-
if world_size > 1:
238-
# Specialization for distributed mode
239-
# Recover the flags when spawned as a sub process
240-
if rank > 0:
241-
_define_flags()
242-
FLAGS(sys.argv, known_only=True)
243-
FLAGS.mark_as_parsed()
244-
dist.init_process_group(
245-
'nccl',
246-
rank=rank,
247-
world_size=world_size,
248-
timeout=datetime.timedelta(minutes=FLAGS.nccl_timeout))
249-
# Set the rank and total number of processes for distributed training.
250-
PerProcessContext().set_distributed(rank=rank,
251-
local_rank=-1,
252-
num_processes=world_size)
253-
assert paras_queue is not None
254-
PerProcessContext().set_paras_queue(paras_queue)
255234

256-
# Make PerProcessContext read-only.
257-
PerProcessContext().finalize()
258-
259-
# Automatically set up some configs for remote unroller and trainer if needed
260-
_setup_remote_configs_if_needed()
235+
def _worker_main(_):
236+
try:
237+
_setup_logging(log_dir=root_dir, rank=rank)
238+
_setup_device()
239+
if world_size > 1:
240+
dist.init_process_group(
241+
'nccl',
242+
rank=rank,
243+
world_size=world_size,
244+
timeout=datetime.timedelta(minutes=FLAGS.nccl_timeout))
245+
# Set the rank and total number of processes for distributed training.
246+
PerProcessContext().set_distributed(rank=rank,
247+
local_rank=-1,
248+
num_processes=world_size)
249+
assert paras_queue is not None
250+
PerProcessContext().set_paras_queue(paras_queue)
251+
252+
# Make PerProcessContext read-only.
253+
PerProcessContext().finalize()
254+
255+
# Automatically set up some configs for remote unroller and trainer if needed
256+
_setup_remote_configs_if_needed()
257+
258+
# Parse the configuration file, which will also implicitly bring up the environments.
259+
common.parse_conf_file(conf_file)
260+
_train(root_dir=root_dir, rank=rank, world_size=world_size)
261+
except KeyboardInterrupt:
262+
pass
263+
except Exception as e:
264+
if world_size >= 1:
265+
# If the training worker is running as a process in multiprocessing
266+
# environment, this will make sure that the exception raised in this
267+
# particular process is captured and shown.
268+
logging.exception(f'{mp.current_process().name} - {e}')
269+
raise e
270+
finally:
271+
# Note that each training worker will have its own child processes
272+
# running the environments. In the case when training worker process
273+
# finishes earlier (e.g. when it raises an exception), it will hang
274+
# instead of quitting unless all child processes are killed.
275+
alf.close_env()
276+
277+
if rank > 0:
278+
# Specialization for distributed mode
279+
# Recover the flags when spawned as a sub process
280+
if rank > 0:
281+
_define_flags()
282+
FLAGS(sys.argv, known_only=True)
283+
FLAGS.mark_as_parsed()
261284

262-
# Parse the configuration file, which will also implicitly bring up the environments.
263-
common.parse_conf_file(conf_file)
264-
_train(root_dir=root_dir, rank=rank, world_size=world_size)
265-
except KeyboardInterrupt:
266-
pass
267-
except Exception as e:
268-
if world_size >= 1:
269-
# If the training worker is running as a process in multiprocessing
270-
# environment, this will make sure that the exception raised in this
271-
# particular process is captured and shown.
272-
logging.exception(f'{mp.current_process().name} - {e}')
273-
raise e
274-
finally:
275-
# Note that each training worker will have its own child processes
276-
# running the environments. In the case when training worker process
277-
# finishes earlier (e.g. when it raises an exception), it will hang
278-
# instead of quitting unless all child processes are killed.
279-
alf.close_env()
285+
app.run(_worker_main)
286+
else:
287+
_worker_main(None)
280288

281289

282290
def training_worker_multi_node(local_rank: int,

0 commit comments

Comments
 (0)