@@ -43,7 +43,7 @@ def parse_log_file(file_path):
4343 Parses the log file to extract global_step and loss.
4444 Returns a dict: {step: loss}
4545 """
46- loss_pattern = re .compile (r"(?:^|-\s*) loss:\s*([0-9\.]+)" )
46+ loss_pattern = re .compile (r"loss:\s*([0-9\.]+)" )
4747 step_pattern = re .compile (r"global_step:\s*(\d+)" )
4848
4949 loss_dict = {}
@@ -62,13 +62,21 @@ def parse_log_file(file_path):
6262 return loss_dict
6363
6464
65+ def write_loss_file (loss_dict , file_path , steps = None ):
66+ if steps is None :
67+ steps = sorted (loss_dict )
68+ with open (file_path , "w" ) as f :
69+ for step in steps :
70+ f .write (f"{ step } { loss_dict [step ]} \n " )
71+
72+
6573def main ():
6674 parser = argparse .ArgumentParser (description = "Check loss values in log against ground truth." )
6775 parser .add_argument ("--log_file" , type = str , required = True , help = "Path to the log file." )
6876 parser .add_argument (
6977 "--gt_file" ,
7078 type = str ,
71- required = True ,
79+ default = None ,
7280 help = "Path to the ground truth file." ,
7381 )
7482 parser .add_argument (
@@ -90,6 +98,11 @@ def main():
9098 default = None ,
9199 help = "Record the loss values from the log file to this file." ,
92100 )
101+ parser .add_argument (
102+ "--extract_loss_only" ,
103+ action = "store_true" ,
104+ help = "Only extract loss values from the log file and write them to --log_loss_file." ,
105+ )
93106 args = parser .parse_args ()
94107
95108 print (f"Starting loss check with log file: { args .log_file } " )
@@ -98,6 +111,21 @@ def main():
98111 print (f"Target Check Step: { args .compare_step } " )
99112
100113 log_dict = parse_log_file (args .log_file )
114+ if args .extract_loss_only :
115+ if args .log_loss_file is None :
116+ print ("\033 [91mError: --log_loss_file is required when --extract_loss_only is set.\033 [0m" )
117+ sys .exit (1 )
118+ if not log_dict :
119+ print (f"\033 [91mError: No loss values found in { args .log_file } .\033 [0m" )
120+ sys .exit (1 )
121+ write_loss_file (log_dict , args .log_loss_file )
122+ print (f"\033 [92mExtracted loss values to { args .log_loss_file } .\033 [0m" )
123+ return
124+
125+ if args .gt_file is None :
126+ print ("\033 [91mError: --gt_file is required unless --extract_loss_only is set.\033 [0m" )
127+ sys .exit (1 )
128+
101129 gt_dict = parse_ground_truth (args .gt_file )
102130
103131 if args .compare_step is not None :
@@ -114,9 +142,7 @@ def main():
114142 gt_loss = gt_dict [target_step ]
115143
116144 if args .log_loss_file is not None :
117- log_loss_file = args .log_loss_file
118- with open (log_loss_file , "w" ) as f :
119- f .write (f"{ target_step } { log_loss } \n " )
145+ write_loss_file (log_dict , args .log_loss_file , [target_step ])
120146
121147 print (f"\n Checking Step { target_step } :" )
122148 print (f" Log Loss: { log_loss } " )
@@ -138,10 +164,7 @@ def main():
138164 target_losses = [gt_dict [s ] for s in common_steps ]
139165
140166 if args .log_loss_file is not None :
141- log_loss_file = args .log_loss_file
142- with open (log_loss_file , "w" ) as f :
143- for s in common_steps :
144- f .write (f"{ s } { log_dict [s ]} \n " )
167+ write_loss_file (log_dict , args .log_loss_file , common_steps )
145168
146169 print ("\n Log values (step loss):" )
147170 print ("\n " .join ([f"{ s } { l :.8f} " for s , l in zip (common_steps , actual_losses )]))
0 commit comments