Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

torch>=2.0 #161

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ dependencies = [
"torchmetrics",
"rich==13.5.*",
"opencv-python==4.8.0.*",
"torch==2.0.*"
"torch>=2.0"
]
dynamic = ["version"]

Expand Down
7 changes: 4 additions & 3 deletions sheeprl/algos/ppo/ppo.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ def main(fabric: Fabric, cfg: Dict[str, Any]):
step_data["logprobs"] = logprobs
step_data["rewards"] = rewards
if cfg.buffer.memmap:
step_data["returns"] = torch.zeros_like(rewards)
step_data["advantages"] = torch.zeros_like(rewards)
step_data["returns"] = torch.zeros_like(rewards, dtype=torch.float32)
step_data["advantages"] = torch.zeros_like(rewards, dtype=torch.float32)

# Append data to buffer
rb.add(step_data.unsqueeze(0))
Expand Down Expand Up @@ -347,7 +347,7 @@ def main(fabric: Fabric, cfg: Dict[str, Any]):
normalized_obs = normalize_obs(next_obs, cfg.algo.cnn_keys.encoder, obs_keys)
next_values = agent.module.get_value(normalized_obs)
returns, advantages = gae(
rb["rewards"],
rb["rewards"].to(torch.float64),
rb["values"],
rb["dones"],
next_values,
Expand All @@ -359,6 +359,7 @@ def main(fabric: Fabric, cfg: Dict[str, Any]):
# Add returns and advantages to the buffer
rb["returns"] = returns.float()
rb["advantages"] = advantages.float()
rb["rewards"] = rb["rewards"].float()

# Flatten the batch
local_data = rb.buffer.view(-1)
Expand Down
7 changes: 4 additions & 3 deletions sheeprl/algos/ppo/ppo_decoupled.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ def player(
step_data["logprobs"] = logprobs
step_data["rewards"] = rewards
if cfg.buffer.memmap:
step_data["returns"] = torch.zeros_like(rewards)
step_data["advantages"] = torch.zeros_like(rewards)
step_data["returns"] = torch.zeros_like(rewards, dtype=torch.float32)
step_data["advantages"] = torch.zeros_like(rewards, dtype=torch.float32)

# Append data to buffer
rb.add(step_data.unsqueeze(0))
Expand Down Expand Up @@ -267,7 +267,7 @@ def player(
normalized_obs = normalize_obs(next_obs, cfg.algo.cnn_keys.encoder, obs_keys)
next_values = agent.get_value(normalized_obs)
returns, advantages = gae(
rb["rewards"],
rb["rewards"].to(torch.float64),
rb["values"],
rb["dones"],
next_values,
Expand All @@ -279,6 +279,7 @@ def player(
# Add returns and advantages to the buffer
rb["returns"] = returns.float()
rb["advantages"] = advantages.float()
rb["rewards"] = rb["rewards"].float()

# Flatten the batch
local_data = rb.buffer.view(-1)
Expand Down
2 changes: 1 addition & 1 deletion sheeprl/algos/ppo_recurrent/ppo_recurrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def main(fabric: Fabric, cfg: Dict[str, Any]):
rnn_out, _ = agent.module.rnn(torch.cat((feat, actions), dim=-1), states)
next_values = agent.module.get_values(rnn_out)
returns, advantages = gae(
rb["rewards"],
rb["rewards"].to(torch.float64),
rb["values"],
rb["dones"],
next_values,
Expand Down