Skip to content

File logger

Result logging and file management for training metrics.

This module provides file-based logging for training metrics, model artifacts, and execution results. It manages result directories and enables time-series metric tracking throughout training.

FileLogger()

Class to log results to files

Source code in mlir_rl_artifact/utils/file_logger.py
def __init__(self):
    self.enabled = True
    cfg = Config()

    # Create run dir
    dir_path = cfg.results_dir
    subdir_ids = sorted([int(d.split('_')[-1]) for d in os.listdir(dir_path) if d.startswith('run_')])
    run_id = subdir_ids[-1] + 1 if subdir_ids else 0
    self.run_dir = os.path.join(dir_path, f'run_{run_id}')
    os.makedirs(self.run_dir, exist_ok=True)

    # Create tags file
    tags_file = os.path.join(self.run_dir, 'tags')
    with open(tags_file, 'w') as f:
        f.write('\n'.join(cfg.tags))
        f.write('\n')

    # Create exec data file
    self.exec_data_file = os.path.join(self.run_dir, 'exec_data.json')
    with open(self.exec_data_file, "w") as f:
        json.dump({}, f)

    # Create logs dir
    self.logs_dir = os.path.join(self.run_dir, 'logs')
    os.makedirs(self.logs_dir, exist_ok=True)

    # Create models dir
    self.models_dir = os.path.join(self.run_dir, 'models')
    os.makedirs(self.models_dir, exist_ok=True)

    # Init files dict
    self.files_dict: dict[str, FileInstance] = {}