def cleanup_tmp_models(config_filename): assert os.path.exists(config_filename) from returnn.config import Config config = Config() config.load_file(config_filename) model_filename = config.value('model', '') assert model_filename # Remove existing models assert model_filename.startswith("/tmp/") for f in glob(model_filename + ".*"): os.remove(f)
def init_config(config_filename=None, command_line_options=(), default_config=None, extra_updates=None): """ :param str|None config_filename: :param list[str]|tuple[str] command_line_options: e.g. ``sys.argv[1:]`` :param dict[str]|None default_config: :param dict[str]|None extra_updates: Initializes the global config. There are multiple sources which are used to init the config: * ``configFilename``, and maybe first item of ``commandLineOptions`` interpret as config filename * other options via ``commandLineOptions`` * ``extra_updates`` Note about the order/priority of these: * ``extra_updates`` * options from ``commandLineOptions`` * ``configFilename`` * config filename from ``commandLineOptions[0]`` * ``extra_updates`` * options from ``commandLineOptions`` ``extra_updates`` and ``commandLineOptions`` are used twice so that they are available when the config is loaded, which thus has access to them, and can e.g. use them via Python code. However, the purpose is that they overwrite any option from the config; that is why we apply them again in the end. ``commandLineOptions`` is applied after ``extra_updates`` so that the user has still the possibility to overwrite anything set by ``extra_updates``. """ global config config = Config() config_filenames_by_cmd_line = [] if command_line_options: # Assume that the first argument prefixed with "+" or "-" and all following is not a config file. i = 0 for arg in command_line_options: if arg[:1] in "-+": break config_filenames_by_cmd_line.append(arg) i += 1 command_line_options = command_line_options[i:] if default_config: config.update(default_config) if extra_updates: config.update(extra_updates) if command_line_options: config.parse_cmd_args(command_line_options) if config_filename: config.load_file(config_filename) for fn in config_filenames_by_cmd_line: config.load_file(fn) if extra_updates: config.update(extra_updates) if command_line_options: config.parse_cmd_args(command_line_options) # I really don't know where to put this otherwise: if config.bool("EnableAutoNumpySharedMemPickling", False): import returnn.util.task_system returnn.util.task_system.SharedMemNumpyConfig["enabled"] = True # Server default options if config.value('task', 'train') == 'server': config.set('num_inputs', 2) config.set('num_outputs', 1)