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] External Env support for new API stack: New example script and custom tcp-capable EnvRunner. #49033

Merged

Conversation

sven1977
Copy link
Contributor

@sven1977 sven1977 commented Dec 3, 2024

External Env support for new API stack:

  • Custom tcp-capable EnvRunner.
  • Example script with dummy CartPole TCP client acting as the external env.

Why are these changes needed?

Related issue number

Checks

  • I've signed off every commit(by using the -s flag, i.e., git commit -s) in this PR.
  • I've run scripts/format.sh to lint the changes in this PR.
  • I've included any doc changes needed for https://docs.ray.io/en/master/.
    • I've added any new APIs to the API Reference. For example, if I added a
      method in Tune, I've added it in doc/source/tune/api/ under the
      corresponding .rst file.
  • I've made sure the tests are passing. Note that there might be a few flaky tests, see the recent failures at https://flakey-tests.ray.io/
  • Testing Strategy
    • Unit tests
    • Release tests
    • This PR is not tested :(

@sven1977 sven1977 enabled auto-merge (squash) December 3, 2024 13:18
@github-actions github-actions bot added the go add ONLY when ready to merge, run all tests label Dec 3, 2024
@sven1977 sven1977 added rllib RLlib related issues rllib-client-server Issue related to RLlib's client/server API. rllib-env rllib env related issues rllib-docs-or-examples Issues related to RLlib documentation or rllib/examples rllib-newstack rllib-envrunners Issues around the sampling backend of RLlib labels Dec 3, 2024
@github-actions github-actions bot disabled auto-merge December 3, 2024 14:26
Copy link
Collaborator

@simonsays1980 simonsays1980 left a comment

Choose a reason for hiding this comment

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

LGTM. A couple of questions in regard to the basic design. Great PR!!

- `Env Rendering and Recording <https://github.com/ray-project/ray/blob/master/rllib/examples/envs/env_rendering_and_recording.py>`__:
Illustrates environment rendering and recording setups within RLlib, capturing visual outputs for later review (ex. on WandB), which is essential
for tracking agent behavior in training.

- `Env with Protobuf Observations <https://github.com/ray-project/ray/blob/master/rllib/examples/envs/env_with_protobuf_observations.py>`__:
- `Env with Protobuf Observations <https://github.com/ray-project/ray/blob/master/rllib/examples/envs/env_w_protobuf_observations.py>`__:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Awesome that we have now this example also in new API stack! Great work!

# TODO (sven): We should ideally do this in the LearnerConnector (separation of
# concerns: Only do things on the EnvRunners that are required for computing
# actions, do NOT do anything on the EnvRunners that's only required for a
# training update).
Copy link
Collaborator

Choose a reason for hiding this comment

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

I thought we already moved that from EnvRunners into a connector?! Yes, at best it would resit in there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is in a connector, but in the env-to-module connector :D on the EnvRunner side, where it should ideally be run on the learner side.

@@ -177,7 +177,11 @@ def forward(self, batch: Dict[str, Any], **kwargs) -> Dict[str, Any]:
training sample collection (w/ exploration behavior).
`_forward_train()` to define the forward pass prior to loss computation.
"""
return self.forward_train(batch, **kwargs)
# TODO (sven): Experimental to make ONNX exported models work.
if self.config.inference_only:
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should think about how inference-only should be integrated such that ONNX works well. Maybe it needs a different integration in the modules.

# will not have an RLModule, but might still be usable with random actions.
except NotImplementedError:
self.module = None
self.make_module()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice, now env runners work it similarly.


This implementation assumes:
- Only one external client ever connects to this env runner.
- The external client performs inference locally through an ONNX model. Thus,
Copy link
Collaborator

Choose a reason for hiding this comment

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

An alternative would be to define an EnvRunner that can execute an external environment inside a thread - so users just define the EnvExecutor that wraps it and defines protocols to start the environment and communicate with it (sending in actions and receiving observations and rewards) and the EnvRunner can then execute. It would keep the RLModule like the other EnvRunner classes we have. Do we have any cases where this would not work? In my C++ external environment this does work quite well. What would be more pro and contras?

self.client_socket,
{
"type": MessageTypes.SET_CONFIG.name,
"env_steps_per_sample": self.config.get_rollout_fragment_length(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah okay, so per sample each external environment does only sample enough for the next batch.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct. This is purely for PPO for now. For IMPALA/APPO or offline algos, this doesn't really matter. The most difficult thing is making this work for PPO, b/c in this purely on-policy case, client and server have to meet exactly in the middle, at the handover of the samples and the receiving of the new weights.

"env_steps_per_sample": self.config.get_rollout_fragment_length(
worker_index=self.worker_index
),
"force_on_policy": True,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Alright, it could be off-policy if a user wants it. Cool!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct, then the protocol will change slightly, but not much. Clients can send data any time they want and - from time to time - get state/weights back on some of these requests.

client_thread.start()

# Query the EnvRunner.
episodes = env_runner.sample()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice! But question: In case I want to run with tune I could simply pass in: TcpClientInferenceEnvRunner into the env_runner_class attribute?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed this code. It was an early testing leftover.

collects episodes of experiences, and sends these (in bulk) back to RLlib for training.
Also, from time to time, the updated model weights have to be sent from RLlib (server)
back to the connected clients.
Note that RLlib's new API stack does not yet support individual action requests, where
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is this? Only because it is built in the TcpEnvRunner correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct, this protocol (remote action inference on the RLlib server) is not supported yet. We need just another EnvRunner for that or build this logic into the existing one.

)
.env_runners(
# Point RLlib to the custom EnvRunner to be used here.
env_runner_cls=TcpClientInferenceEnvRunner,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Yup, question answered :)

Signed-off-by: Sven Mika <sven@anyscale.io>
@sven1977 sven1977 enabled auto-merge (squash) December 3, 2024 18:01
@sven1977 sven1977 merged commit 7c51a74 into ray-project:master Dec 3, 2024
5 of 6 checks passed
simonsays1980 pushed a commit to simonsays1980/ray that referenced this pull request Dec 3, 2024
jecsand838 pushed a commit to jecsand838/ray that referenced this pull request Dec 4, 2024
…d custom tcp-capable EnvRunner. (ray-project#49033)

Signed-off-by: Connor Sanders <connor@elastiflow.com>
dentiny pushed a commit to dentiny/ray that referenced this pull request Dec 7, 2024
…d custom tcp-capable EnvRunner. (ray-project#49033)

Signed-off-by: hjiang <dentinyhao@gmail.com>
ujjawal-khare pushed a commit to ujjawal-khare-27/ray that referenced this pull request Dec 17, 2024
…d custom tcp-capable EnvRunner. (ray-project#49033)

Signed-off-by: ujjawal-khare <ujjawal.khare@dream11.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
go add ONLY when ready to merge, run all tests rllib RLlib related issues rllib-client-server Issue related to RLlib's client/server API. rllib-docs-or-examples Issues related to RLlib documentation or rllib/examples rllib-env rllib env related issues rllib-envrunners Issues around the sampling backend of RLlib rllib-newstack
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants