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

[Doc] Indicate necessary context to run multiprocessed collectors in doc #2126

Merged
merged 8 commits into from
Apr 30, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Update multiasync docstring.
GJBoth committed Apr 26, 2024
commit 6cbc7387785650800c69d3b7bdc6405b6e30a04e
48 changes: 29 additions & 19 deletions torchrl/collectors/collectors.py
Original file line number Diff line number Diff line change
@@ -535,7 +535,10 @@ def __init__(
self.reset_when_done = reset_when_done
self.n_env = self.env.batch_size.numel()

(self.policy, self.get_weights_fn,) = self._get_policy_and_device(
(
self.policy,
self.get_weights_fn,
) = self._get_policy_and_device(
policy=policy,
observation_spec=self.env.observation_spec,
)
@@ -2238,27 +2241,34 @@ class MultiaSyncDataCollector(_MultiDataCollector):
the batch of rollouts is collected and the next call to the iterator.
This class can be safely used with offline RL sota-implementations.
note:: Python requires multiprocessed code to be instantiated within a
```if __name__ == "__main__":``` block. See https://docs.python.org/3/library/multiprocessing.html
for more info.
Examples:
>>> from torchrl.envs.libs.gym import GymEnv
>>> from torchrl.envs.libs.gym import GymEnv
>>> from tensordict.nn import TensorDictModule
>>> from torch import nn
>>> env_maker = lambda: GymEnv("Pendulum-v1", device="cpu")
>>> policy = TensorDictModule(nn.Linear(3, 1), in_keys=["observation"], out_keys=["action"])
>>> collector = MultiaSyncDataCollector(
... create_env_fn=[env_maker, env_maker],
... policy=policy,
... total_frames=2000,
... max_frames_per_traj=50,
... frames_per_batch=200,
... init_random_frames=-1,
... reset_at_each_iter=False,
... devices="cpu",
... storing_devices="cpu",
... )
>>> for i, data in enumerate(collector):
... if i == 2:
... print(data)
... break
>>> from torchrl.collectors import MultiaSyncDataCollector
>>> if __name__ == "__main__":
>>> env_maker = lambda: GymEnv("Pendulum-v1", device="cpu")
>>> policy = TensorDictModule(nn.Linear(3, 1), in_keys=["observation"], out_keys=["action"])
>>> collector = MultiaSyncDataCollector(
... create_env_fn=[env_maker, env_maker],
... policy=policy,
... total_frames=2000,
... max_frames_per_traj=50,
... frames_per_batch=200,
... init_random_frames=-1,
... reset_at_each_iter=False,
... device="cpu",
... storing_device="cpu",
... cat_results="stack",
... )
>>> for i, data in enumerate(collector):
... if i == 2:
... print(data)
... break
TensorDict(
fields={
action: Tensor(shape=torch.Size([200, 1]), device=cpu, dtype=torch.float32, is_shared=False),