| 10 | |
| 11 | |
| 12 | class csvMonitor(Monitor): |
| 13 | |
| 14 | def __init__(self, csv_config): |
| 15 | super().__init__(csv_config) |
| 16 | self.filenames = [] |
| 17 | self.enabled = csv_config.enabled |
| 18 | self.output_path = csv_config.output_path |
| 19 | self.job_name = csv_config.job_name |
| 20 | self.log_dir = self.setup_log_dir() |
| 21 | |
| 22 | def setup_log_dir(self, base=os.path.join(os.path.expanduser("~"), "csv_monitor")): |
| 23 | if self.enabled and dist.get_rank() == 0: |
| 24 | if self.output_path is not None: |
| 25 | log_dir = os.path.join(self.output_path, self.job_name) |
| 26 | # NOTE: This code path currently is never used since the default tensorboard_output_path is an empty string and not None. Saving it in case we want this functionality in the future. |
| 27 | else: |
| 28 | if "DLWS_JOB_ID" in os.environ: |
| 29 | infra_job_id = os.environ["DLWS_JOB_ID"] |
| 30 | elif "DLTS_JOB_ID" in os.environ: |
| 31 | infra_job_id = os.environ["DLTS_JOB_ID"] |
| 32 | else: |
| 33 | infra_job_id = "unknown-job-id" |
| 34 | |
| 35 | csv_monitor_dir_name = os.path.join(infra_job_id, "logs") |
| 36 | log_dir = os.path.join(base, csv_monitor_dir_name, self.job_name) |
| 37 | os.makedirs(log_dir, exist_ok=True) |
| 38 | return log_dir |
| 39 | |
| 40 | def write_events(self, event_list): |
| 41 | if self.enabled and dist.get_rank() == 0: |
| 42 | import csv |
| 43 | # We assume each event_list element is a tensorboard-style tuple in the format: (log_name: String, value, step: Int) |
| 44 | for event in event_list: |
| 45 | log_name = event[0] |
| 46 | value = event[1] |
| 47 | step = event[2] |
| 48 | |
| 49 | # Set the header to the log_name |
| 50 | # Need this check because the deepspeed engine currently formats log strings to separate with '/' |
| 51 | if '/' in log_name: |
| 52 | record_splits = log_name.split('/') |
| 53 | header = record_splits[len(record_splits) - 1] |
| 54 | else: |
| 55 | header = log_name |
| 56 | |
| 57 | # sanitize common naming conventions into filename |
| 58 | filename = log_name.replace('/', '_').replace(' ', '_') |
| 59 | fname = self.log_dir + '/' + filename + '.csv' |
| 60 | |
| 61 | # Open file and record event. Insert header if this is the first time writing |
| 62 | with open(fname, 'a+') as csv_monitor_file: |
| 63 | csv_monitor_writer = csv.writer(csv_monitor_file) |
| 64 | if filename not in self.filenames: |
| 65 | self.filenames.append(filename) |
| 66 | csv_monitor_writer.writerow(['step', header]) |
| 67 | csv_monitor_writer.writerow([step, value]) |
no outgoing calls