def main(): print(sys.version) print(torch.__version__) print(torch.version.cuda) print("Conda env: \"%s\"" % os.environ.get("CONDA_DEFAULT_ENV", "")) option_spec = PyOptionSpec() option_spec.merge(PyOptionSpec.fromClasses((RunGC,))) option_map = option_spec.parse() rungc = RunGC(option_map) rungc.initialize() num_batch = 10000000 rungc.wrapper.start() for i in range(num_batch): rungc.wrapper.run() rungc.wrapper.stop()
def load_env( envs, num_models=None, overrides=None, additional_to_load=None): """Load envs. Envs will be specified as environment variables. Specifically, the environment variables ``game``, ``model_file`` and ``model`` are required. ``additional_to_load`` is a dict with the following format: {'variable_name': (option_spec, callable)} For each element in ``additional_to_load``, ``load_env`` will parse the ``option_spec``, pass the resulting option map to ``callable``, and store the result of ``callable`` in the return value (under the key ``name``). Returns: env: dict of ``game`` : game module ``method``: Learning method used ``model_loaders``: loaders for model """ logger = logging.getIndexedLogger('\u001b[31;1m|py|\u001b[0mrlpytorch.model_loader.load_env', '') logger.info('Loading env') game_loader_class = load_module(envs["game"]).Loader logger.info(f'\u001b[32;1mModule game successfully loaded :\u001b[0m {envs["game"]}') model_file = load_module(envs["model_file"]) logger.info(f'\u001b[32;1mModule model_file successfully loaded :\u001b[0m {envs["model_file"]}') # TODO This is not good, need to fix. if len(model_file.Models[envs["model"]]) == 2: model_class, method_class = model_file.Models[envs["model"]] sampler_class = Sampler else: model_class, method_class, sampler_class = \ model_file.Models[envs["model"]] overrides = dict(overrides) if overrides else {} overrides.update(getattr(model_file, "Overrides", {})) option_spec = PyOptionSpec() option_spec.merge(PyOptionSpec.fromClasses(( logging.GlobalLoggingConfigurator, game_loader_class, method_class, sampler_class, ModelInterface, ))) if num_models is None: option_spec.merge(ModelLoader.get_option_spec(model_class)) else: for i in range(num_models): option_spec.merge( ModelLoader.get_option_spec(model_class, model_idx=i)) if additional_to_load: for additional_option_spec, _ in additional_to_load.values(): option_spec.merge(additional_option_spec) option_map = option_spec.parse(overrides=overrides) global_logger_configurator = logging.GlobalLoggingConfigurator(option_map) global_logger_configurator.configure() game = game_loader_class(option_map) method = method_class(option_map) sampler = sampler_class(option_map) mi = ModelInterface(option_map) # You might want multiple models loaded. if num_models is None: model_loaders = [ModelLoader(option_map, model_class)] else: model_loaders = [ModelLoader(option_map, model_class, model_idx=i) for i in range(num_models)] env = dict( game=game, method=method, sampler=sampler, model_loaders=model_loaders, mi=mi, ) if additional_to_load: for name, (_, option_map_callable) in additional_to_load.items(): env[name] = option_map_callable(option_map) pretty_option_str = pprint.pformat(option_map.getOptionDict(), width=50) logger.info(f'Parsed options:\n{pretty_option_str}') logger.info('Finished loading env') # env_str = "" # for i in env: # env_str += f"{i} : {env[i]}\n" # logger.info(f"env :\n{env_str}") return env
#print("Receive batch: ", batch.smem.info(), # ", curr_batchsize: ", str(batch.batchsize), sep='') #print(batch["s"]) # print("Actor: " + str(datetime.timestamp(datetime.now()))) #print("on actor") # n = self.params["num_action"] # print(batch.batchsize) res = self.model(batch) res["V"].data /= 10.0 #print("s: %f, V: %f, pi: %s" % (batch["s"].data.item(), res["V"].data.item(), str(res["pi"].data))) return dict(V=res["V"].data, pi=res["pi"].data) if __name__ == '__main__': option_spec = PyOptionSpec() option_spec.merge(PyOptionSpec.fromClasses((RunGC,))) option_map = option_spec.parse() rungc = RunGC(option_map) rungc.initialize() num_batch = 10000000 start = time.perf_counter() rungc.wrapper.start() for i in range(num_batch): rungc.wrapper.run() elapsed = time.perf_counter() - start print("Time (s) per batch: %.6f sec" % (elapsed / num_batch))