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

Fix tests for mps support #2005

Open
wants to merge 7 commits into
base: feat/mps-support
Choose a base branch
from
Open
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: 2 additions & 0 deletions stable_baselines3/common/buffers.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ def to_torch(self, array: np.ndarray, copy: bool = True) -> th.Tensor:
:return:
"""
if copy:
if hasattr(th, "backends") and th.backends.mps.is_built():
return th.tensor(array, dtype=th.float32, device=self.device)
return th.tensor(array, device=self.device)
return th.as_tensor(array, device=self.device)

Expand Down
2 changes: 1 addition & 1 deletion stable_baselines3/common/envs/bit_flipping_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def convert_if_needed(self, state: np.ndarray) -> Union[int, np.ndarray]:
state = state.astype(np.int32)
# The internal state is the binary representation of the
# observed one
return int(sum(state[i] * 2**i for i in range(len(state))))
return int(sum(int(state[i]) * 2**i for i in range(len(state))))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should not be needed anymore (because of the cast)


if self.image_obs_space:
size = np.prod(self.image_shape)
Expand Down
3 changes: 3 additions & 0 deletions stable_baselines3/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,8 @@ def obs_as_tensor(obs: Union[np.ndarray, dict[str, np.ndarray]], device: th.devi
if isinstance(obs, np.ndarray):
return th.as_tensor(obs, device=device)
elif isinstance(obs, dict):
if hasattr(th, "backends") and th.backends.mps.is_built():
return {key: th.as_tensor(_obs, dtype=th.float32, device=device) for (key, _obs) in obs.items()}
return {key: th.as_tensor(_obs, device=device) for (key, _obs) in obs.items()}
else:
raise Exception(f"Unrecognized type of observation {type(obs)}")
Expand Down Expand Up @@ -526,6 +528,7 @@ def get_available_accelerator() -> str:
"""
if hasattr(th, "backends") and th.backends.mps.is_built():
# MacOS Metal GPU
th.set_default_dtype(th.float32)
return "mps"
elif th.cuda.is_available():
return "cuda"
Expand Down
3 changes: 3 additions & 0 deletions tests/test_spaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import gymnasium as gym
import numpy as np
import pytest
import torch as th
from gymnasium import spaces
from gymnasium.spaces.space import Space

Expand Down Expand Up @@ -151,6 +152,8 @@ def test_discrete_obs_space(model_class, env):
],
)
def test_float64_action_space(model_class, obs_space, action_space):
if hasattr(th, "backends") and th.backends.mps.is_built():
pytest.skip("MPS framework doesn't support float64")
env = DummyEnv(obs_space, action_space)
env = gym.wrappers.TimeLimit(env, max_episode_steps=200)
if isinstance(env.observation_space, spaces.Dict):
Expand Down