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

[RLlib] New ConnectorV3 API #05: PPO runs in single-agent mode in this API stack #42272

Merged
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
e7ae52a
wip
sven1977 Nov 17, 2023
fe640de
wip
sven1977 Nov 17, 2023
b340ddc
Merge branch 'master' of https://github.com/ray-project/ray into env_…
sven1977 Dec 1, 2023
9492b7a
wip
sven1977 Dec 1, 2023
52d5e72
merge
sven1977 Dec 14, 2023
6437d7e
wip
sven1977 Dec 14, 2023
242d40a
merge
sven1977 Dec 18, 2023
61be702
wip
sven1977 Dec 21, 2023
bf802fc
wip
sven1977 Dec 21, 2023
16f2c38
fixes
sven1977 Dec 21, 2023
ad047a7
Merge branch 'master' of https://github.com/ray-project/ray into env_…
sven1977 Jan 9, 2024
083388d
wip
sven1977 Jan 9, 2024
8e02889
Merge branch 'master' of https://github.com/ray-project/ray into env_…
sven1977 Jan 9, 2024
10b0700
LINT
sven1977 Jan 9, 2024
e439fc8
fix
sven1977 Jan 9, 2024
4633659
fix
sven1977 Jan 9, 2024
cce2c66
wip
sven1977 Jan 9, 2024
7dff78f
wip
sven1977 Jan 9, 2024
bdb20dc
fix
sven1977 Jan 9, 2024
bcdb92f
fix
sven1977 Jan 9, 2024
03fe431
Merge branch 'master' of https://github.com/ray-project/ray into env_…
sven1977 Jan 9, 2024
7dd8f3f
fix
sven1977 Jan 9, 2024
b769c05
fix
sven1977 Jan 9, 2024
f5ffe83
Merge branch 'master' of https://github.com/ray-project/ray into env_…
sven1977 Jan 10, 2024
0b78c50
Merge branch 'env_runner_support_connectors_04_learner_api_changes' i…
sven1977 Jan 10, 2024
c96c7ae
fix
sven1977 Jan 10, 2024
7ecc594
fix bug in single-agent-env-runner that drops all finished episodes f…
sven1977 Jan 10, 2024
13bcb1d
merge
sven1977 Jan 10, 2024
d28ee91
merge
sven1977 Jan 12, 2024
db16556
wip
sven1977 Jan 12, 2024
790a537
wip
sven1977 Jan 12, 2024
d525de3
wip
sven1977 Jan 15, 2024
d728a08
wip
sven1977 Jan 15, 2024
45ec639
fixes
sven1977 Jan 15, 2024
54a89bb
merge
sven1977 Jan 15, 2024
bbda644
wip
sven1977 Jan 15, 2024
51bf00f
Merge branch 'master' of https://github.com/ray-project/ray into env_…
sven1977 Jan 16, 2024
ac23593
wip
sven1977 Jan 16, 2024
9aaee44
wip
sven1977 Jan 16, 2024
d3eca70
wip
sven1977 Jan 16, 2024
92f80f7
wip
sven1977 Jan 18, 2024
0be8c3a
Merge branch 'master' of https://github.com/ray-project/ray into env_…
sven1977 Jan 18, 2024
d3d6a22
wip
sven1977 Jan 18, 2024
9cd3d3f
LINT
sven1977 Jan 18, 2024
279f7e4
wip
sven1977 Jan 18, 2024
8046634
wip
sven1977 Jan 18, 2024
229f788
wip
sven1977 Jan 18, 2024
dab848c
wip
sven1977 Jan 18, 2024
b7ccae9
wip
sven1977 Jan 18, 2024
f1ccb4b
wip
sven1977 Jan 18, 2024
d78b5e0
wip
sven1977 Jan 18, 2024
b59b717
wip
sven1977 Jan 18, 2024
9d7087c
wip
sven1977 Jan 18, 2024
9de63cc
wip
sven1977 Jan 18, 2024
c340869
wip
sven1977 Jan 18, 2024
db2f77d
Merge branch 'master' of https://github.com/ray-project/ray into env_…
sven1977 Jan 19, 2024
25e0efe
wip
sven1977 Jan 19, 2024
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
19 changes: 13 additions & 6 deletions rllib/algorithms/algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
collect_metrics,
summarize_episodes,
)
from ray.rllib.evaluation.postprocessing_v2 import postprocess_episodes_to_sample_batch
from ray.rllib.evaluation.worker_set import WorkerSet
from ray.rllib.execution.rollout_ops import synchronous_parallel_sample
from ray.rllib.execution.train_ops import multi_gpu_train_one_step, train_one_step
Expand Down Expand Up @@ -1410,7 +1409,12 @@ def remote_fn(worker):
worker.set_weights(
weights=ray.get(weights_ref), weights_seq_no=weights_seq_no
)
episodes = worker.sample(explore=False)
# By episode: Run always only one episode per remote call.
# By timesteps: By default EnvRunner runs for the configured number of
# timesteps (based on `rollout_fragment_length` and `num_envs_per_worker`).
episodes = worker.sample(
explore=False, num_episodes=1 if unit == "episodes" else None
)
metrics = worker.get_metrics()
return episodes, metrics, weights_seq_no

Expand Down Expand Up @@ -1449,11 +1453,13 @@ def remote_fn(worker):
rollout_metrics.extend(metrics)
i += 1

# Convert our list of Episodes to a single SampleBatch.
batch = postprocess_episodes_to_sample_batch(episodes)
# Collect steps stats.
_agent_steps = batch.agent_steps()
_env_steps = batch.env_steps()
# TODO (sven): Solve for proper multi-agent env/agent steps counting.
# Once we have multi-agent support on EnvRunner stack, we can simply do:
# `len(episode)` for env steps and `episode.num_agent_steps()` for agent
# steps.
_agent_steps = sum(len(e) for e in episodes)
_env_steps = sum(len(e) for e in episodes)

# Only complete episodes done by eval workers.
if unit == "episodes":
Expand All @@ -1467,6 +1473,7 @@ def remote_fn(worker):
)

if self.reward_estimators:
batch = concat_samples([e.get_sample_batch() for e in episodes])
all_batches.append(batch)

agent_steps_this_iter += _agent_steps
Expand Down
57 changes: 40 additions & 17 deletions rllib/algorithms/algorithm_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ def __init__(self, algo_class=None):
self.grad_clip = None
self.grad_clip_by = "global_norm"
self.train_batch_size = 32
# Simple logic for now: If None, use `train_batch_size`.
self.train_batch_size_per_learner = None
# TODO (sven): Unsolved problem with RLModules sometimes requiring settings from
# the main AlgorithmConfig. We should not require the user to provide those
# settings in both, the AlgorithmConfig (as property) AND the model config
Expand Down Expand Up @@ -1335,11 +1337,11 @@ def environment(
Tuple[value1, value2]: Clip at value1 and value2.
normalize_actions: If True, RLlib will learn entirely inside a normalized
action space (0.0 centered with small stddev; only affecting Box
components). We will unsquash actions (and clip, just in case) to the
components). RLlib will unsquash actions (and clip, just in case) to the
bounds of the env's action space before sending actions back to the env.
clip_actions: If True, RLlib will clip actions according to the env's bounds
before sending them back to the env.
TODO: (sven) This option should be deprecated and always be False.
clip_actions: If True, the RLlib default ModuleToEnv connector will clip
actions according to the env's bounds (before sending them into the
`env.step()` call).
disable_env_checking: If True, disable the environment pre-checking module.
is_atari: This config can be used to explicitly specify whether the env is
an Atari env or not. If not specified, RLlib will try to auto-detect
Expand Down Expand Up @@ -1680,6 +1682,7 @@ def training(
grad_clip: Optional[float] = NotProvided,
grad_clip_by: Optional[str] = NotProvided,
train_batch_size: Optional[int] = NotProvided,
train_batch_size_per_learner: Optional[int] = NotProvided,
model: Optional[dict] = NotProvided,
optimizer: Optional[dict] = NotProvided,
max_requests_in_flight_per_sampler_worker: Optional[int] = NotProvided,
Expand Down Expand Up @@ -1728,7 +1731,16 @@ def training(
the shapes of these tensors are).
grad_clip_by: See `grad_clip` for the effect of this setting on gradient
clipping. Allowed values are `value`, `norm`, and `global_norm`.
train_batch_size: Training batch size, if applicable.
train_batch_size_per_learner: Train batch size per individual Learner
worker. This setting only applies to the new API stack. The number
of Learner workers can be set via `config.resources(
num_learner_workers=...)`. The total effective batch size is then
`num_learner_workers` x `train_batch_size_per_learner` and can
be accessed via the property `AlgorithmConfig.total_train_batch_size`.
train_batch_size: Training batch size, if applicable. When on the new API
stack, this setting should no longer be used. Instead, use
`train_batch_size_per_learner` (in combination with
`num_learner_workers`).
model: Arguments passed into the policy model. See models/catalog.py for a
full list of the available model options.
TODO: Provide ModelConfig objects instead of dicts.
Expand Down Expand Up @@ -1768,6 +1780,8 @@ def training(
"or 'global_norm'!"
)
self.grad_clip_by = grad_clip_by
if train_batch_size_per_learner is not NotProvided:
self.train_batch_size_per_learner = train_batch_size_per_learner
if train_batch_size is not NotProvided:
self.train_batch_size = train_batch_size
if model is not NotProvided:
Expand Down Expand Up @@ -2718,6 +2732,15 @@ def uses_new_env_runners(self):
self.env_runner_cls, RolloutWorker
)

@property
def total_train_batch_size(self):
if self.train_batch_size_per_learner is not None:
return self.train_batch_size_per_learner * (self.num_learner_workers or 1)
else:
return self.train_batch_size

# TODO: Make rollout_fragment_length as read-only property and replace the current
# self.rollout_fragment_length a private variable.
def get_rollout_fragment_length(self, worker_index: int = 0) -> int:
"""Automatically infers a proper rollout_fragment_length setting if "auto".

Expand All @@ -2741,11 +2764,11 @@ def get_rollout_fragment_length(self, worker_index: int = 0) -> int:
# 4 workers, 3 envs per worker, 2500 train batch size:
# -> 2500 / 12 -> 208.333 -> diff=4 (208 * 12 = 2496)
# -> worker 1: 209, workers 2-4: 208
rollout_fragment_length = self.train_batch_size / (
rollout_fragment_length = self.total_train_batch_size / (
self.num_envs_per_worker * (self.num_rollout_workers or 1)
)
if int(rollout_fragment_length) != rollout_fragment_length:
diff = self.train_batch_size - int(
diff = self.total_train_batch_size - int(
rollout_fragment_length
) * self.num_envs_per_worker * (self.num_rollout_workers or 1)
if (worker_index * self.num_envs_per_worker) <= diff:
Expand Down Expand Up @@ -3102,31 +3125,31 @@ def validate_train_batch_size_vs_rollout_fragment_length(self) -> None:
if (
self.rollout_fragment_length != "auto"
and not self.in_evaluation
and self.train_batch_size > 0
and self.total_train_batch_size > 0
):
min_batch_size = (
max(self.num_rollout_workers, 1)
* self.num_envs_per_worker
* self.rollout_fragment_length
)
batch_size = min_batch_size
while batch_size < self.train_batch_size:
while batch_size < self.total_train_batch_size:
batch_size += min_batch_size
if (
batch_size - self.train_batch_size > 0.1 * self.train_batch_size
or batch_size - min_batch_size - self.train_batch_size
> (0.1 * self.train_batch_size)
if batch_size - self.total_train_batch_size > (
0.1 * self.total_train_batch_size
) or batch_size - min_batch_size - self.total_train_batch_size > (
0.1 * self.total_train_batch_size
):
suggested_rollout_fragment_length = self.train_batch_size // (
suggested_rollout_fragment_length = self.total_train_batch_size // (
self.num_envs_per_worker * (self.num_rollout_workers or 1)
)
raise ValueError(
f"Your desired `train_batch_size` ({self.train_batch_size}) or a "
"value 10% off of that cannot be achieved with your other "
f"Your desired `train_batch_size` ({self.total_train_batch_size}) "
"or a value 10% off of that cannot be achieved with your other "
f"settings (num_rollout_workers={self.num_rollout_workers}; "
f"num_envs_per_worker={self.num_envs_per_worker}; "
f"rollout_fragment_length={self.rollout_fragment_length})! "
"Try setting `rollout_fragment_length` to 'auto' OR "
"Try setting `rollout_fragment_length` to 'auto' OR to a value of "
f"{suggested_rollout_fragment_length}."
)

Expand Down
Loading
Loading