66import signal
77import sys
88import torch
9- import torch .distributed
109
1110from datetime import timedelta
1211
1514from megatron .core .energy_monitor import EnergyMonitor
1615from megatron .core .jit import disable_jit_fuser
1716from megatron .core .num_microbatches_calculator import init_num_microbatches_calculator , unset_num_microbatches_calculator
18- from megatron .training .tokenizer import build_tokenizer
17+ from megatron .training .tokenizer import build_tokenizer ########## FlagScale Add ##########
1918from megatron .training .dist_signal_handler import DistributedSignalHandler
2019
20+ ########## FlagScale Begin ##########
2121from megatron .training .spiky_loss import SpikyLossDetector
2222from megatron .plugin .utils import get_device_type_for_comm
23-
23+ ########## FlagScale End ##########
2424
2525_GLOBAL_ARGS = None
2626_GLOBAL_TOKENIZER = None
3232_GLOBAL_ENERGY_MONITOR = None
3333_GLOBAL_SIGNAL_HANDLER = None
3434
35+ ########## FlagScale Begin ##########
3536_GLOBAL_SPIKY_LOSS_DETECTOR = None
3637_GLOBAL_EXTRA_VALID_DATASETS = None
38+ ########## FlagScale End ##########
3739
3840
3941
@@ -165,15 +167,13 @@ def set_global_writers(args):
165167 Note that this function should be called after calling finish_mpu_init.
166168 This is because we can know which rank is the last one after the rank mapping in finish_mpu_init.
167169 """
170+ from .utils import is_last_rank
168171
169172 assert args is not None
170173
171174 _ensure_var_is_initialized (_GLOBAL_ARGS , 'args' )
172-
173- from .utils import is_last_rank
174- if is_last_rank ():
175- _set_tensorboard_writer (args )
176- _set_one_logger (args )
175+ _set_tensorboard_writer (args )
176+ _set_one_logger (args )
177177
178178 # build wandb writers for all processes in the dp group of the last rank
179179 from megatron .core import mpu
@@ -252,7 +252,7 @@ def _set_tensorboard_writer(args):
252252 'tensorboard writer' )
253253
254254 if hasattr (args , 'tensorboard_dir' ) and \
255- args .tensorboard_dir :
255+ args .tensorboard_dir and args . rank == ( args . world_size - 1 ) :
256256 try :
257257 from torch .utils .tensorboard import SummaryWriter
258258 print ('> setting tensorboard ...' )
@@ -274,7 +274,6 @@ def _set_wandb_writer(args):
274274 raise ValueError ("Please specify the wandb experiment name!" )
275275
276276 import wandb
277- rank = torch .distributed .get_rank ()
278277 if args .wandb_save_dir :
279278 save_dir = args .wandb_save_dir
280279 else :
@@ -286,9 +285,8 @@ def _set_wandb_writer(args):
286285 # settings were.
287286 with open (wandb_config ['kitchen_config_file' ], "r" ) as f :
288287 wandb_config ['kitchen_config_file_contents' ] = f .read ()
288+ rank = torch .distributed .get_rank ()
289289 save_dir = os .path .join (save_dir , "rank-{}" .format (rank ))
290- os .makedirs (save_dir , exist_ok = True )
291-
292290 wandb_id = f"{ args .wandb_exp_name } -rank-{ rank } "
293291 name = f'{ args .wandb_exp_name } -rank-{ rank } '
294292 group = args .wandb_exp_name
@@ -303,9 +301,7 @@ def _set_wandb_writer(args):
303301 'config' : wandb_config }
304302 if args .wandb_entity :
305303 wandb_kwargs ['entity' ] = args .wandb_entity
306- if args .wandb_entity :
307- wandb_kwargs ['entity' ] = args .wandb_entity
308-
304+ os .makedirs (wandb_kwargs ['dir' ], exist_ok = True )
309305 if args .wandb_mode == 'online' or args .wandb_api_key :
310306 assert args .wandb_api_key , 'wandb_api_key is required for online mode'
311307 wandb .login (key = args .wandb_api_key )
@@ -406,6 +402,7 @@ def destroy_global_vars():
406402 _GLOBAL_SIGNAL_HANDLER = None
407403
408404
405+ ########## FlagScale Begin ##########
409406def get_spiky_loss_detector ():
410407 """Return spiky loss detector."""
411408 _ensure_var_is_initialized (_GLOBAL_SPIKY_LOSS_DETECTOR , "spiky loss detector" )
@@ -427,4 +424,5 @@ def get_extra_valid_datasets():
427424def set_extra_valid_datasets (extra_valid_datasets ):
428425 """Set extra_valid datasets.""" ""
429426 global _GLOBAL_EXTRA_VALID_DATASETS
430- _GLOBAL_EXTRA_VALID_DATASETS = extra_valid_datasets
427+ _GLOBAL_EXTRA_VALID_DATASETS = extra_valid_datasets
428+ ########## FlagScale End ##########
0 commit comments