-
Notifications
You must be signed in to change notification settings - Fork 7.2k
[rllib] Add async remote workers #4253
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
72bd148
example
ericl 2b19872
wip
ericl 0d1ccc4
wip
ericl 29eaf52
st
ericl 387ff89
lint
ericl 59bc6b2
docs
ericl fc05ba2
multiagent
ericl f87f665
unify the paths
ericl 1eca0af
add sync unit tests
ericl 8aa968e
Update rllib-env.rst
ericl 0e92d53
Update and rename async_remote_env.py to remote_vector_env.py
ericl 4f2df86
Update rllib-env.rst
ericl e880a5d
Update remote_vector_env.py
ericl f425154
Update base_env.py
ericl cdc9478
Update agent.py
ericl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| from __future__ import absolute_import | ||
| from __future__ import division | ||
| from __future__ import print_function | ||
|
|
||
| import logging | ||
|
|
||
| import ray | ||
| from ray.rllib.env.base_env import BaseEnv, _DUMMY_AGENT_ID | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class RemoteVectorEnv(BaseEnv): | ||
| """Vector env that executes envs in remote workers. | ||
|
|
||
| This provides dynamic batching of inference as observations are returned | ||
| from the remote simulator actors. Both single and multi-agent child envs | ||
| are supported, and envs can be stepped synchronously or async. | ||
| """ | ||
|
|
||
| def __init__(self, make_env, num_envs, multiagent, sync): | ||
| self.make_local_env = make_env | ||
| if sync: | ||
| self.timeout = 9999999.0 # wait for all envs | ||
| else: | ||
| self.timeout = 0.0 # wait for only ready envs | ||
|
|
||
| def make_remote_env(i): | ||
| logger.info("Launching env {} in remote actor".format(i)) | ||
| if multiagent: | ||
| return _RemoteMultiAgentEnv.remote(self.make_local_env, i) | ||
| else: | ||
| return _RemoteSingleAgentEnv.remote(self.make_local_env, i) | ||
|
|
||
| self.actors = [make_remote_env(i) for i in range(num_envs)] | ||
| self.pending = None # lazy init | ||
|
|
||
| def poll(self): | ||
| if self.pending is None: | ||
| self.pending = {a.reset.remote(): a for a in self.actors} | ||
|
|
||
| # each keyed by env_id in [0, num_remote_envs) | ||
| obs, rewards, dones, infos = {}, {}, {}, {} | ||
| ready = [] | ||
|
|
||
| # Wait for at least 1 env to be ready here | ||
| while not ready: | ||
| ready, _ = ray.wait( | ||
| list(self.pending), | ||
| num_returns=len(self.pending), | ||
| timeout=self.timeout) | ||
|
|
||
| # Get and return observations for each of the ready envs | ||
| env_ids = set() | ||
| for obj_id in ready: | ||
| actor = self.pending.pop(obj_id) | ||
| env_id = self.actors.index(actor) | ||
| env_ids.add(env_id) | ||
| ob, rew, done, info = ray.get(obj_id) | ||
| obs[env_id] = ob | ||
| rewards[env_id] = rew | ||
| dones[env_id] = done | ||
| infos[env_id] = info | ||
|
|
||
| logger.debug("Got obs batch for actors {}".format(env_ids)) | ||
| return obs, rewards, dones, infos, {} | ||
|
|
||
| def send_actions(self, action_dict): | ||
| for env_id, actions in action_dict.items(): | ||
| actor = self.actors[env_id] | ||
| obj_id = actor.step.remote(actions) | ||
| self.pending[obj_id] = actor | ||
|
|
||
| def try_reset(self, env_id): | ||
| obs, _, _, _ = ray.get(self.actors[env_id].reset.remote()) | ||
| return obs | ||
|
|
||
|
|
||
| @ray.remote(num_cpus=0) | ||
| class _RemoteMultiAgentEnv(object): | ||
| """Wrapper class for making a multi-agent env a remote actor.""" | ||
|
|
||
| def __init__(self, make_env, i): | ||
| self.env = make_env(i) | ||
|
|
||
| def reset(self): | ||
| obs = self.env.reset() | ||
| # each keyed by agent_id in the env | ||
| rew = {agent_id: 0 for agent_id in obs.keys()} | ||
| info = {agent_id: {} for agent_id in obs.keys()} | ||
| done = {"__all__": False} | ||
| return obs, rew, done, info | ||
|
|
||
| def step(self, action_dict): | ||
| return self.env.step(action_dict) | ||
|
|
||
|
|
||
| @ray.remote(num_cpus=0) | ||
| class _RemoteSingleAgentEnv(object): | ||
| """Wrapper class for making a gym env a remote actor.""" | ||
|
|
||
| def __init__(self, make_env, i): | ||
| self.env = make_env(i) | ||
|
|
||
| def reset(self): | ||
| obs = {_DUMMY_AGENT_ID: self.env.reset()} | ||
| rew = {agent_id: 0 for agent_id in obs.keys()} | ||
| info = {agent_id: {} for agent_id in obs.keys()} | ||
| done = {"__all__": False} | ||
| return obs, rew, done, info | ||
|
|
||
| def step(self, action): | ||
| obs, rew, done, info = self.env.step(action[_DUMMY_AGENT_ID]) | ||
| obs, rew, done, info = [{ | ||
| _DUMMY_AGENT_ID: x | ||
| } for x in [obs, rew, done, info]] | ||
| done["__all__"] = done[_DUMMY_AGENT_ID] | ||
| return obs, rew, done, info |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.