def save_configs(self, f, method='pickle'): r"""Save the list of configurations returned from :meth:`make_configs`. Args: f (str): file path method (str): the method to save the list of configuration. Either 'pickle' or 'yaml' """ methods = ['pickle', 'yaml'] assert method in methods, f'expected {methods}, got {method}' if method == 'pickle': pickle_dump(obj=self.configs, f=f, ext='.pkl') elif method == 'yaml': yaml_dump(obj=self.configs, f=f, ext='.yml')
def test_pickle_yaml(): a = {'one': 1, 'two': [2, 3]} b = {'three': 3, 'four': [4, 5]} c = [a, b] def check(x): assert isinstance(x, list) assert len(x) == 2 assert all([isinstance(i, dict) for i in x]) assert list(x[0].keys()) == ['one', 'two'] assert list(x[1].keys()) == ['three', 'four'] assert list(x[0].values()) == [1, [2, 3]] assert list(x[1].values()) == [3, [4, 5]] pickle_dump(c, '.tmp_pickle') check(pickle_load('.tmp_pickle.pkl')) os.unlink('.tmp_pickle.pkl') yaml_dump(c, '.tmp_yaml') check(yaml_load('.tmp_yaml.yml')) os.unlink('.tmp_yaml.yml')
def run_experiment(worker_class, master_class, num_worker): r"""A convenient function to launch a parallelized experiment (Master-Worker). .. note:: It automatically creates all subfolders for logging the experiment. The topmost folder is indicated by the logging directory specified in the configuration. Then all subfolders for each configuration are created with the name of their ID. Finally, under each configuration subfolder, a set subfolders are created for each random seed (the random seed as folder name). Intuitively, an experiment could have following directory structure:: - logs - 0 # ID number - 123 # random seed - 345 - 567 - 1 - 123 - 345 - 567 - 2 - 123 - 345 - 567 - 3 - 123 - 345 - 567 - 4 - 123 - 345 - 567 Args: worker_class (BaseExperimentWorker): a worker class for the experiment. master_class (BaseExperimentMaster): a master class for the experiment. num_worker (int, optional): number of workers. """ t = time() experiment = master_class(worker_class=worker_class, num_worker=num_worker) log_path = Path(experiment.configs[0]['log.dir']) if not log_path.exists(): log_path.mkdir(parents=True) else: msg = f"Logging directory '{log_path.absolute()}' already existed, do you want to clean it ?" answer = ask_yes_or_no(msg) if answer: rmtree(log_path) log_path.mkdir(parents=True) else: # back up old_log_path = log_path.with_name('old_' + log_path.name) log_path.rename(old_log_path) log_path.mkdir(parents=True) print( f"The old logging directory is renamed to '{old_log_path.absolute()}'. " ) input('Please, press Enter to continue\n>>> ') # Create subfolders for each ID and subsubfolders for each random seed for config in experiment.configs: ID = config['ID'] for seed in experiment.seeds: p = log_path / f'{ID}' / f'{seed}' p.mkdir(parents=True) yaml_dump(obj=config, f=log_path / f'{ID}' / 'config', ext='.yml') experiment.save_configs(log_path / 'configs') # Run experiment in parallel experiment() msg = color_str(f'\nTotal time: {timedelta(seconds=round(time() - t))}', 'green', 'bold') print(msg)
def run_experiment(run, config, seeds, num_worker): r"""A convenient function to launch a parallelized experiment (Master-Worker). .. note:: It automatically creates all subfolders for logging the experiment. The topmost folder is indicated by the logging directory specified in the configuration. Then all subfolders for each configuration are created with the name of their ID. Finally, under each configuration subfolder, a set subfolders are created for each random seed (the random seed as folder name). Intuitively, an experiment could have following directory structure:: - logs - 0 # ID number - 123 # random seed - 345 - 567 - 1 - 123 - 345 - 567 - 2 - 123 - 345 - 567 - 3 - 123 - 345 - 567 - 4 - 123 - 345 - 567 Args: run (function): an algorithm function to train on. config (Config): a :class:`Config` object defining all configuration settings seeds (list): a list of random seeds num_worker (int): number of workers """ experiment = ExperimentMaster(ExperimentWorker, num_worker, run, config, seeds) log_path = Path(experiment.configs[0]['log.dir']) if not log_path.exists(): log_path.mkdir(parents=True) else: msg = f"Logging directory '{log_path.absolute()}' already existed, do you want to clean it ?" answer = ask_yes_or_no(msg) if answer: rmtree(log_path) log_path.mkdir(parents=True) else: # back up old_log_path = log_path.with_name('old_' + log_path.name) log_path.rename(old_log_path) log_path.mkdir(parents=True) print( f"The old logging directory is renamed to '{old_log_path.absolute()}'. " ) input('Please, press Enter to continue\n>>> ') # save source files source_path = Path(log_path / 'source_files/') source_path.mkdir(parents=True) [ copyfile(s, source_path / s.name) for s in Path(inspect.getsourcefile(run)).parent.glob('*.py') ] # Create subfolders for each ID and subsubfolders for each random seed for config in experiment.configs: ID = config['ID'] for seed in experiment.seeds: p = log_path / f'{ID}' / f'{seed}' p.mkdir(parents=True) yaml_dump(obj=config, f=log_path / f'{ID}' / 'config', ext='.yml') pickle_dump(experiment.configs, log_path / 'configs', ext='.pkl') # Run experiment in parallel results = experiment() return results
def run_experiment(run, config, seeds, log_dir, max_workers, chunksize=1, use_gpu=False, gpu_ids=None): r"""A convenient function to parallelize the experiment (master-worker pipeline). It is implemented by using `concurrent.futures.ProcessPoolExecutor` It automatically creates all subfolders for each pair of configuration and random seed to store the loggings of the experiment. The root folder is given by the user. Then all subfolders for each configuration are created with the name of their job IDs. Under each configuration subfolder, a set subfolders are created for each random seed (the random seed as folder name). Intuitively, an experiment could have following directory structure:: - logs - 0 # ID number - 123 # random seed - 345 - 567 - 1 - 123 - 345 - 567 - 2 - 123 - 345 - 567 - 3 - 123 - 345 - 567 - 4 - 123 - 345 - 567 Args: run (function): a function that defines an algorithm, it must take the arguments `(config, seed, device, logdir)` config (Config): a :class:`Config` object defining all configuration settings seeds (list): a list of random seeds log_dir (str): a string to indicate the path to store loggings. max_workers (int): argument for ProcessPoolExecutor. if `None`, then all experiments run serially. chunksize (int): argument for Executor.map() use_gpu (bool): if `True`, then use CUDA. Otherwise, use CPU. gpu_ids (list): if `None`, then use all available GPUs. Otherwise, only use the GPU device defined in the list. """ configs = config.make_configs() # create logging dir log_path = Path(log_dir) if not log_path.exists(): log_path.mkdir(parents=True) else: msg = f"Logging directory '{log_path.absolute()}' already existed, do you want to clean it ?" answer = ask_yes_or_no(msg) if answer: rmtree(log_path) log_path.mkdir(parents=True) else: # back up old_log_path = log_path.with_name('old_' + log_path.name) log_path.rename(old_log_path) log_path.mkdir(parents=True) print( f"The old logging directory is renamed to '{old_log_path.absolute()}'. " ) input('Please, press Enter to continue\n>>> ') # save source files source_path = Path(log_path / 'source_files/') source_path.mkdir(parents=True) [ copyfile(s, source_path / s.name) for s in Path(inspect.getsourcefile(run)).parent.glob('*.py') ] # Create subfolders for each ID and subsubfolders for each random seed for config in configs: ID = config['ID'] for seed in seeds: p = log_path / f'{ID}' / f'{seed}' p.mkdir(parents=True) yaml_dump(obj=config, f=log_path / f'{ID}' / 'config', ext='.yml') pickle_dump(configs, log_path / 'configs', ext='.pkl') # Create unique id for each job jobs = list(enumerate(product(configs, seeds))) def _run(job): job_id, (config, seed) = job # VERY IMPORTANT TO AVOID GETTING STUCK, oversubscription # see following links # https://github.com/pytorch/pytorch/issues/19163 # https://software.intel.com/en-us/intel-threading-building-blocks-openmp-or-native-threads torch.set_num_threads(1) if use_gpu: num_gpu = torch.cuda.device_count() if gpu_ids is None: # use all GPUs device_id = job_id % num_gpu else: assert all([i >= 0 and i < num_gpu for i in gpu_ids]) device_id = gpu_ids[job_id % len(gpu_ids)] torch.cuda.set_device(device_id) device = torch.device(f'cuda:{device_id}') else: device = torch.device('cpu') print( f'@ Experiment: ID: {config["ID"]} ({len(configs)}), Seed: {seed}, Device: {device}, Job: {job_id} ({len(jobs)}), PID: {os.getpid()}' ) print('#' * 50) [print(f'# {key}: {value}') for key, value in config.items()] print('#' * 50) logdir = log_path / f'{config["ID"]}' / f'{seed}' result = run(config, seed, device, logdir) # Release all un-freed GPU memory if use_gpu: torch.cuda.empty_cache() return result if max_workers is None: results = [_run(job) for job in jobs] else: with ProcessPoolExecutor( max_workers=min(max_workers, len(jobs))) as executor: results = list( executor.map(CloudpickleWrapper(_run), jobs, chunksize=chunksize)) print( color_str( f'\nExperiment finished. Loggings are stored in {log_path.absolute()}. ', 'cyan', bold=True)) return results