Skip to content

Commit

Permalink
[RLlib] Rename all np.product usage to np.prod (#46317)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxVanDijck authored Jul 22, 2024
1 parent 66b68d0 commit 232c331
Show file tree
Hide file tree
Showing 64 changed files with 75 additions and 58 deletions.
4 changes: 2 additions & 2 deletions python/ray/data/_internal/datasource/range_datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(

def estimate_inmemory_data_size(self) -> Optional[int]:
if self._block_format == "tensor":
element_size = int(np.product(self._tensor_shape))
element_size = int(np.prod(self._tensor_shape))
else:
element_size = 1
return 8 * self._n * element_size
Expand Down Expand Up @@ -86,7 +86,7 @@ def make_blocks(
count -= num_rows

if block_format == "tensor":
element_size = int(np.product(tensor_shape))
element_size = int(np.prod(tensor_shape))
else:
element_size = 1

Expand Down
2 changes: 1 addition & 1 deletion rllib/algorithms/sac/rnnsac_torch_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def build_rnnsac_model(
`policy.target_model`.
"""
# With separate state-preprocessor (before obs+action concat).
num_outputs = int(np.product(obs_space.shape))
num_outputs = int(np.prod(obs_space.shape))

# Force-ignore any additionally provided hidden layer sizes.
# Everything should be configured using SAC's `q_model_config` and
Expand Down
4 changes: 2 additions & 2 deletions rllib/algorithms/sac/sac_tf_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ def __init__(
self.discrete = True
action_outs = q_outs = self.action_dim
elif isinstance(action_space, Box):
self.action_dim = np.product(action_space.shape)
self.action_dim = np.prod(action_space.shape)
self.discrete = False
action_outs = 2 * self.action_dim
q_outs = 1
else:
assert isinstance(action_space, Simplex)
self.action_dim = np.product(action_space.shape)
self.action_dim = np.prod(action_space.shape)
self.discrete = False
action_outs = self.action_dim
q_outs = 1
Expand Down
4 changes: 2 additions & 2 deletions rllib/algorithms/sac/sac_torch_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ def __init__(
self.discrete = True
action_outs = q_outs = self.action_dim
elif isinstance(action_space, Box):
self.action_dim = np.product(action_space.shape)
self.action_dim = np.prod(action_space.shape)
self.discrete = False
action_outs = 2 * self.action_dim
q_outs = 1
else:
assert isinstance(action_space, Simplex)
self.action_dim = np.product(action_space.shape)
self.action_dim = np.prod(action_space.shape)
self.discrete = False
action_outs = self.action_dim
q_outs = 1
Expand Down
2 changes: 1 addition & 1 deletion rllib/examples/_old_api_stack/models/batch_norm_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def __init__(
)
nn.Module.__init__(self)
layers = []
prev_layer_size = int(np.product(obs_space.shape))
prev_layer_size = int(np.prod(obs_space.shape))
self._logits = None

# Create layers 0 to second-last.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ def __init__(

# Non-shared initial layer.
self.first_layer = SlimFC(
int(np.product(observation_space.shape)),
int(np.prod(observation_space.shape)),
64,
activation_fn=nn.ReLU,
initializer=torch.nn.init.xavier_uniform_,
Expand Down
4 changes: 2 additions & 2 deletions rllib/models/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ def get_action_dist(
dist_cls = (
TorchMultiCategorical if framework == "torch" else MultiCategorical
)
num_cats = int(np.product(action_space.shape))
num_cats = int(np.prod(action_space.shape))
return (
partial(
dist_cls,
Expand Down Expand Up @@ -495,7 +495,7 @@ def get_action_shape(
size += 1
else:
all_discrete = False
size += np.product(flat_action_space[i].shape)
size += np.prod(flat_action_space[i].shape)
size = int(size)
return dl_lib.int32 if all_discrete else dl_lib.float32, (None, size)
else:
Expand Down
6 changes: 3 additions & 3 deletions rllib/models/preprocessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __init__(self, obs_space: gym.Space, options: dict = None):
else:
self._options = options
self.shape = self._init_shape(obs_space, self._options)
self._size = int(np.product(self.shape))
self._size = int(np.prod(self.shape))
self._i = 0
self._obs_for_type_matching = self._obs_space.sample()

Expand Down Expand Up @@ -276,7 +276,7 @@ def _init_shape(self, obs_space: gym.Space, options: dict) -> List[int]:
size += preprocessor.size
else:
preprocessor = None
size += int(np.product(space.shape))
size += int(np.prod(space.shape))
self.preprocessors.append(preprocessor)
return (size,)

Expand Down Expand Up @@ -315,7 +315,7 @@ def _init_shape(self, obs_space: gym.Space, options: dict) -> List[int]:
size += preprocessor.size
else:
preprocessor = None
size += int(np.product(space.shape))
size += int(np.prod(space.shape))
self.preprocessors.append(preprocessor)
return (size,)

Expand Down
2 changes: 1 addition & 1 deletion rllib/models/tf/attention_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ def __init__(
elif isinstance(space, MultiDiscrete):
self.action_dim += np.sum(space.nvec)
elif space.shape is not None:
self.action_dim += int(np.product(space.shape))
self.action_dim += int(np.prod(space.shape))
else:
self.action_dim += int(len(space))

Expand Down
2 changes: 1 addition & 1 deletion rllib/models/tf/complex_input_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def __init__(self, obs_space, action_space, num_outputs, model_config, name):
concat_size += int(self.one_hot[i].num_outputs)
# Everything else (1D Box).
else:
size = int(np.product(component.shape))
size = int(np.prod(component.shape))
config = {
"fcnet_hiddens": model_config["fcnet_hiddens"],
"fcnet_activation": model_config.get("fcnet_activation"),
Expand Down
6 changes: 2 additions & 4 deletions rllib/models/tf/fcnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __init__(

# We are using obs_flat, so take the flattened shape as input.
inputs = tf.keras.layers.Input(
shape=(int(np.product(obs_space.shape)),), name="observations"
shape=(int(np.prod(obs_space.shape)),), name="observations"
)
# Last hidden layer output (before logits outputs).
last_layer = inputs
Expand Down Expand Up @@ -99,9 +99,7 @@ def __init__(
)(last_layer)
# Adjust num_outputs to be the number of nodes in the last layer.
else:
self.num_outputs = ([int(np.product(obs_space.shape))] + hiddens[-1:])[
-1
]
self.num_outputs = ([int(np.prod(obs_space.shape))] + hiddens[-1:])[-1]

# Concat the log std vars to the end of the state-dependent means.
if free_log_std and logits_out is not None:
Expand Down
4 changes: 2 additions & 2 deletions rllib/models/tf/recurrent_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def __init__(
# is the input size for the LSTM layer.
# If None, set it to the observation space.
if self.num_outputs is None:
self.num_outputs = int(np.product(self.obs_space.shape))
self.num_outputs = int(np.prod(self.obs_space.shape))

self.cell_size = model_config["lstm_cell_size"]
self.use_prev_action = model_config["lstm_use_prev_action"]
Expand All @@ -164,7 +164,7 @@ def __init__(
elif isinstance(space, MultiDiscrete):
self.action_dim += np.sum(space.nvec)
elif space.shape is not None:
self.action_dim += int(np.product(space.shape))
self.action_dim += int(np.prod(space.shape))
else:
self.action_dim += int(len(space))

Expand Down
2 changes: 1 addition & 1 deletion rllib/models/torch/attention_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def __init__(
elif isinstance(space, MultiDiscrete):
self.action_dim += np.sum(space.nvec)
elif space.shape is not None:
self.action_dim += int(np.product(space.shape))
self.action_dim += int(np.prod(space.shape))
else:
self.action_dim += int(len(space))

Expand Down
2 changes: 1 addition & 1 deletion rllib/models/torch/complex_input_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def __init__(self, obs_space, action_space, num_outputs, model_config, name):
self.add_module("one_hot_{}".format(i), self.one_hot[i])
# Everything else (1D Box).
else:
size = int(np.product(component.shape))
size = int(np.prod(component.shape))
config = {
"fcnet_hiddens": model_config["fcnet_hiddens"],
"fcnet_activation": model_config.get("fcnet_activation"),
Expand Down
8 changes: 3 additions & 5 deletions rllib/models/torch/fcnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def __init__(
num_outputs = num_outputs // 2

layers = []
prev_layer_size = int(np.product(obs_space.shape))
prev_layer_size = int(np.prod(obs_space.shape))
self._logits = None

# Create layers 0 to second-last.
Expand Down Expand Up @@ -97,9 +97,7 @@ def __init__(
activation_fn=None,
)
else:
self.num_outputs = ([int(np.product(obs_space.shape))] + hiddens[-1:])[
-1
]
self.num_outputs = ([int(np.prod(obs_space.shape))] + hiddens[-1:])[-1]

# Layer to add the log std vars to the state-dependent means.
if self.free_log_std and self._logits:
Expand All @@ -110,7 +108,7 @@ def __init__(
self._value_branch_separate = None
if not self.vf_share_layers:
# Build a parallel set of hidden layers for the value net.
prev_vf_layer_size = int(np.product(obs_space.shape))
prev_vf_layer_size = int(np.prod(obs_space.shape))
vf_layers = []
for size in hiddens:
vf_layers.append(
Expand Down
4 changes: 2 additions & 2 deletions rllib/models/torch/recurrent_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def __init__(
# is the input size for the LSTM layer.
# If None, set it to the observation space.
if self.num_outputs is None:
self.num_outputs = int(np.product(self.obs_space.shape))
self.num_outputs = int(np.prod(self.obs_space.shape))

self.cell_size = model_config["lstm_cell_size"]
self.time_major = model_config.get("_time_major", False)
Expand All @@ -161,7 +161,7 @@ def __init__(
elif isinstance(space, MultiDiscrete):
self.action_dim += np.sum(space.nvec)
elif space.shape is not None:
self.action_dim += int(np.product(space.shape))
self.action_dim += int(np.prod(space.shape))
else:
self.action_dim += int(len(space))

Expand Down
2 changes: 1 addition & 1 deletion rllib_contrib/a2c/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ requires-python = ">=3.7, <3.11"
dependencies = ["gym[accept-rom-license]", "gymnasium[accept-rom-license, atari]==0.26.3", "ray[rllib]==2.5.0"]

[project.optional-dependencies]
development = ["pytest>=7.2.2", "pre-commit==2.21.0", "tensorflow==2.11.0", "tensorflow-probability==0.19.0", "torch==1.12.0"]
development = ["pytest>=7.2.2", "pre-commit==2.21.0", "tensorflow==2.11.0", "tensorflow-probability==0.19.0", "torch==1.12.0", "numpy<2"]
1 change: 1 addition & 0 deletions rllib_contrib/a2c/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
tensorflow==2.11.1
tensorflow-probability==0.19.0
torch==1.12.0
numpy<2
2 changes: 1 addition & 1 deletion rllib_contrib/a3c/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ requires-python = ">=3.7, <3.11"
dependencies = ["gym[accept-rom-license]", "gymnasium[mujoco]==0.26.3", "ray[rllib]==2.3.1"]

[project.optional-dependencies]
development = ["pytest>=7.2.2", "pre-commit==2.21.0", "tensorflow==2.11.0", "tensorflow-probability==0.19.0", "torch==1.12.0"]
development = ["pytest>=7.2.2", "pre-commit==2.21.0", "tensorflow==2.11.0", "tensorflow-probability==0.19.0", "torch==1.12.0", "numpy<2"]
1 change: 1 addition & 0 deletions rllib_contrib/a3c/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
tensorflow==2.11.1
tensorflow-probability==0.19.0
torch==1.12.0
numpy<2
2 changes: 1 addition & 1 deletion rllib_contrib/alpha_star/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ requires-python = ">=3.7, <3.11"
dependencies = ["gym", "gymnasium==0.26.3", "ray[rllib]==2.5.0", "open-spiel==1.3"]

[project.optional-dependencies]
development = ["pytest>=7.2.2", "pre-commit==2.21.0", "tensorflow==2.11.0", "tensorflow-probability==0.19.0", "torch==1.12.0"]
development = ["pytest>=7.2.2", "pre-commit==2.21.0", "tensorflow==2.11.0", "tensorflow-probability==0.19.0", "torch==1.12.0", "numpy<2"]
1 change: 1 addition & 0 deletions rllib_contrib/alpha_star/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
tensorflow==2.11.1
tensorflow-probability==0.19.0
torch==1.12.0
numpy<2
2 changes: 1 addition & 1 deletion rllib_contrib/alpha_zero/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ requires-python = ">=3.7, <3.11"
dependencies = ["gymnasium==0.26.3", "ray[rllib]==2.5.1"]

[project.optional-dependencies]
development = ["pytest>=7.2.2", "pre-commit==2.21.0", "tensorflow==2.11.0", "tensorflow-probability==0.19.0", "torch==1.12.0"]
development = ["pytest>=7.2.2", "pre-commit==2.21.0", "tensorflow==2.11.0", "tensorflow-probability==0.19.0", "torch==1.12.0", "numpy<2"]
1 change: 1 addition & 0 deletions rllib_contrib/alpha_zero/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
torch==1.12.0
numpy<2
2 changes: 1 addition & 1 deletion rllib_contrib/apex_ddpg/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ requires-python = ">=3.7, <3.11"
dependencies = ["gymnasium[atari]", "ray[rllib]==2.5.0"]

[project.optional-dependencies]
development = ["pytest>=7.2.2", "pre-commit==2.21.0", "tensorflow==2.11.0", "torch==1.12.0"]
development = ["pytest>=7.2.2", "pre-commit==2.21.0", "tensorflow==2.11.0", "torch==1.12.0", "numpy<2"]
1 change: 1 addition & 0 deletions rllib_contrib/apex_ddpg/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
tensorflow==2.11.1
tensorflow-probability==0.19.0
torch==1.12.0
numpy<2
2 changes: 1 addition & 1 deletion rllib_contrib/apex_dqn/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ requires-python = ">=3.7, <3.11"
dependencies = ["gymnasium[atari]", "ray[rllib]==2.5.0"]

[project.optional-dependencies]
development = ["pytest>=7.2.2", "pre-commit==2.21.0", "tensorflow==2.11.0", "torch==1.12.0"]
development = ["pytest>=7.2.2", "pre-commit==2.21.0", "tensorflow==2.11.0", "torch==1.12.0", "numpy<2"]
1 change: 1 addition & 0 deletions rllib_contrib/apex_dqn/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
tensorflow==2.11.1
tensorflow-probability==0.19.0
torch==1.12.0
numpy<2
2 changes: 1 addition & 1 deletion rllib_contrib/ars/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ requires-python = ">=3.7, <3.11"
dependencies = ["gymnasium", "ray[rllib]==2.5.0"]

[project.optional-dependencies]
development = ["pytest>=7.2.2", "pre-commit==2.21.0", "tensorflow==2.11.0", "tensorflow-probability==0.19.0", "torch==1.12.0"]
development = ["pytest>=7.2.2", "pre-commit==2.21.0", "tensorflow==2.11.0", "tensorflow-probability==0.19.0", "torch==1.12.0", "numpy<2"]
1 change: 1 addition & 0 deletions rllib_contrib/ars/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
tensorflow==2.11.1
tensorflow-probability==0.19.0
torch==1.12.0
numpy<2
2 changes: 1 addition & 1 deletion rllib_contrib/crr/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ requires-python = ">=3.7, <3.11"
dependencies = ["gymnasium", "ray[rllib]==2.5.0"]

[project.optional-dependencies]
development = ["pytest>=7.2.2", "pre-commit==2.21.0", "tensorflow==2.11.0", "tensorflow-probability==0.19.0", "torch==1.12.0"]
development = ["pytest>=7.2.2", "pre-commit==2.21.0", "tensorflow==2.11.0", "tensorflow-probability==0.19.0", "torch==1.12.0", "numpy<2"]
1 change: 1 addition & 0 deletions rllib_contrib/crr/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
torch==1.12.0
numpy<2
2 changes: 1 addition & 1 deletion rllib_contrib/crr/src/rllib_crr/crr/crr_torch_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(

# TODO: I don't know why this is true yet? (in = num_outputs)
self.obs_ins = num_outputs
self.action_dim = np.product(self.action_space.shape)
self.action_dim = np.prod(self.action_space.shape)
self.actor_model = self._build_actor_net("actor")
twin_q = self.model_config["twin_q"]
self.q_model = self._build_q_net("q")
Expand Down
2 changes: 1 addition & 1 deletion rllib_contrib/crr/src/rllib_crr/crr/crr_torch_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def make_model(self) -> ModelV2:
twin_q=self.config["twin_q"],
)
)
num_outputs = int(np.product(self.observation_space.shape))
num_outputs = int(np.prod(self.observation_space.shape))

# TODO: why do we even have to go through this get_model_v2 function?
self.model = ModelCatalog.get_model_v2(
Expand Down
2 changes: 1 addition & 1 deletion rllib_contrib/ddpg/src/rllib_ddpg/ddpg/ddpg_torch_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def __init__(
self.bounded = np.logical_and(
self.action_space.bounded_above, self.action_space.bounded_below
).any()
self.action_dim = np.product(self.action_space.shape)
self.action_dim = np.prod(self.action_space.shape)

# Build the policy network.
self.policy_model = nn.Sequential()
Expand Down
2 changes: 1 addition & 1 deletion rllib_contrib/ddpg/src/rllib_ddpg/ddpg/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def make_ddpg_models(policy: Policy) -> ModelV2:
default_model = (
TorchNoopModel if policy.config["framework"] == "torch" else NoopModel
)
num_outputs = int(np.product(policy.observation_space.shape))
num_outputs = int(np.prod(policy.observation_space.shape))

model = ModelCatalog.get_model_v2(
obs_space=policy.observation_space,
Expand Down
2 changes: 1 addition & 1 deletion rllib_contrib/ddppo/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ requires-python = ">=3.7, <3.11"
dependencies = ["gymnasium", "ray[rllib]==2.5.0"]

[project.optional-dependencies]
development = ["pytest>=7.2.2", "pre-commit==2.21.0", "torch==1.12.0", "tensorflow==2.11.0", "tensorflow-probability==0.19.0"]
development = ["pytest>=7.2.2", "pre-commit==2.21.0", "torch==1.12.0", "tensorflow==2.11.0", "tensorflow-probability==0.19.0", "numpy<2"]
1 change: 1 addition & 0 deletions rllib_contrib/ddppo/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
torch==1.12.0
numpy<2
2 changes: 1 addition & 1 deletion rllib_contrib/dt/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ requires-python = ">=3.7, <3.11"
dependencies = ["gymnasium", "ray[rllib]==2.5.0"]

[project.optional-dependencies]
development = ["pytest>=7.2.2", "pre-commit==2.21.0", "torch==1.12.0", "tensorflow==2.11.0", "tensorflow-probability==0.19.0"]
development = ["pytest>=7.2.2", "pre-commit==2.21.0", "torch==1.12.0", "tensorflow==2.11.0", "tensorflow-probability==0.19.0", "numpy<2"]
1 change: 1 addition & 0 deletions rllib_contrib/dt/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
torch==1.12.0
numpy<2
2 changes: 1 addition & 1 deletion rllib_contrib/dt/src/rllib_dt/dt/dt_torch_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def __init__(
if isinstance(action_space, Discrete):
self.action_dim = action_space.n
elif isinstance(action_space, Box):
self.action_dim = np.product(action_space.shape)
self.action_dim = np.prod(action_space.shape)
else:
raise NotImplementedError

Expand Down
2 changes: 1 addition & 1 deletion rllib_contrib/dt/src/rllib_dt/dt/dt_torch_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def make_model_and_action_dist(
use_return_output=self.config.get("loss_coef_returns_to_go", 0) > 0,
)

num_outputs = int(np.product(self.observation_space.shape))
num_outputs = int(np.prod(self.observation_space.shape))

model = ModelCatalog.get_model_v2(
obs_space=self.observation_space,
Expand Down
Loading

0 comments on commit 232c331

Please sign in to comment.