Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
[ghstack-poisoned]
  • Loading branch information
vmoens committed Dec 16, 2024
2 parents de6ebcd + 70a7cc0 commit 9364724
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
11 changes: 8 additions & 3 deletions sota-implementations/td3/td3.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ def main(cfg: "DictConfig"): # noqa: F821
device = torch.device("cuda:0")
else:
device = torch.device("cpu")
device = torch.device(device)
else:
device = torch.device(device)

# Create logger
exp_name = generate_exp_name("TD3", cfg.logger.exp_name)
Expand All @@ -72,7 +73,7 @@ def main(cfg: "DictConfig"): # noqa: F821
np.random.seed(cfg.env.seed)

# Create environments
train_env, eval_env = make_environment(cfg, logger=logger)
train_env, eval_env = make_environment(cfg, logger=logger, device=device)

# Create agent
model, exploration_policy = make_td3_agent(cfg, train_env, eval_env, device)
Expand All @@ -91,7 +92,11 @@ def main(cfg: "DictConfig"): # noqa: F821

# Create off-policy collector
collector = make_collector(
cfg, train_env, exploration_policy, compile_mode=compile_mode
cfg,
train_env,
exploration_policy,
compile_mode=compile_mode,
device=device,
)

# Create replay buffer
Expand Down
17 changes: 8 additions & 9 deletions sota-implementations/td3/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,14 @@ def apply_env_transforms(env, max_episode_steps):
return transformed_env


def make_environment(cfg, logger=None):
def make_environment(cfg, logger, device):
"""Make environments for training and evaluation."""
partial = functools.partial(env_maker, cfg=cfg)
parallel_env = ParallelEnv(
cfg.collector.env_per_collector,
EnvCreator(partial),
serial_for_single=True,
device=device,
)
parallel_env.set_seed(cfg.env.seed)

Expand All @@ -98,6 +99,7 @@ def make_environment(cfg, logger=None):
cfg.collector.env_per_collector,
EnvCreator(partial),
serial_for_single=True,
device=device,
),
trsf_clone,
)
Expand All @@ -109,22 +111,19 @@ def make_environment(cfg, logger=None):
# ---------------------------


def make_collector(cfg, train_env, actor_model_explore, compile_mode):
def make_collector(cfg, train_env, actor_model_explore, compile_mode, device):
"""Make collector."""
device = cfg.collector.device
if device in ("", None):
if torch.cuda.is_available():
device = torch.device("cuda:0")
else:
device = torch.device("cpu")
collector_device = cfg.collector.device
if collector_device in ("", None):
collector_device = device
collector = SyncDataCollector(
train_env,
actor_model_explore,
init_random_frames=cfg.collector.init_random_frames,
frames_per_batch=cfg.collector.frames_per_batch,
total_frames=cfg.collector.total_frames,
reset_at_each_iter=cfg.collector.reset_at_each_iter,
device=device,
device=collector_device,
compile_policy={"mode": compile_mode} if compile_mode else False,
cudagraph_policy=cfg.compile.cudagraphs,
)
Expand Down

0 comments on commit 9364724

Please sign in to comment.