forked from pytorch/rl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_libs.py
432 lines (386 loc) · 14.6 KB
/
test_libs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
# Copyright (c) Meta Platforms, Inc. and affiliates.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import argparse
from sys import platform
import numpy as np
import pytest
import torch
from _utils_internal import (
_test_fake_tensordict,
get_available_devices,
HALFCHEETAH_VERSIONED,
PONG_VERSIONED,
PENDULUM_VERSIONED,
)
from packaging import version
from tensordict.tensordict import assert_allclose_td
from torchrl._utils import implement_for
from torchrl.collectors import MultiaSyncDataCollector
from torchrl.collectors.collectors import RandomPolicy
from torchrl.envs import EnvCreator, ParallelEnv
from torchrl.envs.libs.dm_control import DMControlEnv, DMControlWrapper
from torchrl.envs.libs.dm_control import _has_dmc
from torchrl.envs.libs.gym import GymEnv, GymWrapper
from torchrl.envs.libs.gym import _has_gym, _is_from_pixels
from torchrl.envs.libs.habitat import HabitatEnv, _has_habitat
from torchrl.envs.libs.jumanji import JumanjiEnv, _has_jumanji
if _has_gym:
import gym
gym_version = version.parse(gym.__version__)
if gym_version > version.parse("0.19"):
from gym.wrappers.pixel_observation import PixelObservationWrapper
else:
from torchrl.envs.libs.utils import (
GymPixelObservationWrapper as PixelObservationWrapper,
)
if _has_dmc:
from dm_control import suite
from dm_control.suite.wrappers import pixels
IS_OSX = platform == "darwin"
@pytest.mark.skipif(not _has_gym, reason="no gym library found")
@pytest.mark.parametrize(
"env_name",
[
PONG_VERSIONED,
PENDULUM_VERSIONED,
],
)
@pytest.mark.parametrize("frame_skip", [1, 3])
@pytest.mark.parametrize(
"from_pixels,pixels_only",
[
[False, False],
[True, True],
[True, False],
],
)
class TestGym:
def test_gym(self, env_name, frame_skip, from_pixels, pixels_only):
if env_name == PONG_VERSIONED and not from_pixels:
raise pytest.skip("already pixel")
elif (
env_name != PONG_VERSIONED
and from_pixels
and (not torch.has_cuda or not torch.cuda.device_count())
):
raise pytest.skip("no cuda device")
tdreset = []
tdrollout = []
final_seed = []
for _ in range(2):
env0 = GymEnv(
env_name,
frame_skip=frame_skip,
from_pixels=from_pixels,
pixels_only=pixels_only,
)
torch.manual_seed(0)
np.random.seed(0)
final_seed.append(env0.set_seed(0))
tdreset.append(env0.reset())
tdrollout.append(env0.rollout(max_steps=50))
assert env0.from_pixels is from_pixels
env0.close()
env_type = type(env0._env)
del env0
assert_allclose_td(*tdreset)
assert_allclose_td(*tdrollout)
final_seed0, final_seed1 = final_seed
assert final_seed0 == final_seed1
if env_name == PONG_VERSIONED:
base_env = gym.make(env_name, frameskip=frame_skip)
frame_skip = 1
else:
base_env = _make_gym_environment(env_name)
if from_pixels and not _is_from_pixels(base_env):
base_env = PixelObservationWrapper(base_env, pixels_only=pixels_only)
assert type(base_env) is env_type
env1 = GymWrapper(base_env, frame_skip=frame_skip)
torch.manual_seed(0)
np.random.seed(0)
final_seed2 = env1.set_seed(0)
tdreset2 = env1.reset()
rollout2 = env1.rollout(max_steps=50)
assert env1.from_pixels is from_pixels
env1.close()
del env1, base_env
assert_allclose_td(tdreset[0], tdreset2, rtol=1e-4, atol=1e-4)
assert final_seed0 == final_seed2
assert_allclose_td(tdrollout[0], rollout2, rtol=1e-4, atol=1e-4)
def test_gym_fake_td(self, env_name, frame_skip, from_pixels, pixels_only):
if env_name == PONG_VERSIONED and not from_pixels:
raise pytest.skip("already pixel")
elif (
env_name != PONG_VERSIONED
and from_pixels
and (not torch.has_cuda or not torch.cuda.device_count())
):
raise pytest.skip("no cuda device")
env = GymEnv(
env_name,
frame_skip=frame_skip,
from_pixels=from_pixels,
pixels_only=pixels_only,
)
_test_fake_tensordict(env)
@implement_for("gym", None, "0.26")
def _make_gym_environment(env_name): # noqa: F811
return gym.make(env_name)
@implement_for("gym", "0.26", None)
def _make_gym_environment(env_name): # noqa: F811
return gym.make(env_name, render_mode="rgb_array")
@pytest.mark.skipif(not _has_dmc, reason="no dm_control library found")
@pytest.mark.parametrize("env_name,task", [["cheetah", "run"]])
@pytest.mark.parametrize("frame_skip", [1, 3])
@pytest.mark.parametrize(
"from_pixels,pixels_only",
[
[True, True],
[True, False],
[False, False],
],
)
class TestDMControl:
def test_dmcontrol(self, env_name, task, frame_skip, from_pixels, pixels_only):
if from_pixels and (not torch.has_cuda or not torch.cuda.device_count()):
raise pytest.skip("no cuda device")
tds = []
tds_reset = []
final_seed = []
for _ in range(2):
env0 = DMControlEnv(
env_name,
task,
frame_skip=frame_skip,
from_pixels=from_pixels,
pixels_only=pixels_only,
)
torch.manual_seed(0)
np.random.seed(0)
final_seed0 = env0.set_seed(0)
tdreset0 = env0.reset()
rollout0 = env0.rollout(max_steps=50)
env0.close()
del env0
tds_reset.append(tdreset0)
tds.append(rollout0)
final_seed.append(final_seed0)
tdreset1, tdreset0 = tds_reset
rollout0, rollout1 = tds
final_seed0, final_seed1 = final_seed
assert_allclose_td(tdreset1, tdreset0)
assert final_seed0 == final_seed1
assert_allclose_td(rollout0, rollout1)
env1 = DMControlEnv(
env_name,
task,
frame_skip=frame_skip,
from_pixels=from_pixels,
pixels_only=pixels_only,
)
torch.manual_seed(1)
np.random.seed(1)
final_seed1 = env1.set_seed(1)
tdreset1 = env1.reset()
rollout1 = env1.rollout(max_steps=50)
env1.close()
del env1
with pytest.raises(AssertionError):
assert_allclose_td(tdreset1, tdreset0)
assert final_seed0 == final_seed1
assert_allclose_td(rollout0, rollout1)
base_env = suite.load(env_name, task)
if from_pixels:
render_kwargs = {"camera_id": 0}
base_env = pixels.Wrapper(
base_env, pixels_only=pixels_only, render_kwargs=render_kwargs
)
env2 = DMControlWrapper(base_env, frame_skip=frame_skip)
torch.manual_seed(0)
np.random.seed(0)
final_seed2 = env2.set_seed(0)
tdreset2 = env2.reset()
rollout2 = env2.rollout(max_steps=50)
assert_allclose_td(tdreset0, tdreset2)
assert final_seed0 == final_seed2
assert_allclose_td(rollout0, rollout2)
def test_faketd(self, env_name, task, frame_skip, from_pixels, pixels_only):
if from_pixels and (not torch.has_cuda or not torch.cuda.device_count()):
raise pytest.skip("no cuda device")
env = DMControlEnv(
env_name,
task,
frame_skip=frame_skip,
from_pixels=from_pixels,
pixels_only=pixels_only,
)
_test_fake_tensordict(env)
@pytest.mark.skipif(
IS_OSX,
reason="rendering unstable on osx, skipping (mujoco.FatalError: gladLoadGL error)",
)
@pytest.mark.skipif(not (_has_dmc and _has_gym), reason="gym or dm_control not present")
@pytest.mark.parametrize(
"env_lib,env_args,env_kwargs",
[
[DMControlEnv, ("cheetah", "run"), {"from_pixels": True}],
[GymEnv, (HALFCHEETAH_VERSIONED,), {"from_pixels": True}],
[DMControlEnv, ("cheetah", "run"), {"from_pixels": False}],
[GymEnv, (HALFCHEETAH_VERSIONED,), {"from_pixels": False}],
[GymEnv, (PONG_VERSIONED,), {}],
],
)
def test_td_creation_from_spec(env_lib, env_args, env_kwargs):
if (
gym_version < version.parse("0.26.0")
and env_kwargs.get("from_pixels", False)
and torch.cuda.device_count() == 0
):
pytest.skip(
"Skipping test as rendering is not supported in tests before gym 0.26."
)
env = env_lib(*env_args, **env_kwargs)
td = env.rollout(max_steps=5)
td0 = td[0].flatten_keys(".")
fake_td = env.fake_tensordict()
fake_td = fake_td.flatten_keys(".")
td = td.flatten_keys(".")
assert set(fake_td.keys()) == set(td.keys())
for key in fake_td.keys():
assert fake_td.get(key).shape == td.get(key)[0].shape
for key in fake_td.keys():
assert fake_td.get(key).shape == td0.get(key).shape
assert fake_td.get(key).dtype == td0.get(key).dtype
assert fake_td.get(key).device == td0.get(key).device
@pytest.mark.skipif(IS_OSX, reason="rendering unstable on osx, skipping")
@pytest.mark.skipif(not (_has_dmc and _has_gym), reason="gym or dm_control not present")
@pytest.mark.parametrize(
"env_lib,env_args,env_kwargs",
[
[DMControlEnv, ("cheetah", "run"), {"from_pixels": True}],
[GymEnv, (HALFCHEETAH_VERSIONED,), {"from_pixels": True}],
[DMControlEnv, ("cheetah", "run"), {"from_pixels": False}],
[GymEnv, (HALFCHEETAH_VERSIONED,), {"from_pixels": False}],
[GymEnv, (PONG_VERSIONED,), {}],
],
)
@pytest.mark.parametrize("device", get_available_devices())
class TestCollectorLib:
def test_collector_run(self, env_lib, env_args, env_kwargs, device):
from_pixels = env_kwargs.get("from_pixels", False)
if from_pixels and (not torch.has_cuda or not torch.cuda.device_count()):
raise pytest.skip("no cuda device")
env_fn = EnvCreator(lambda: env_lib(*env_args, **env_kwargs, device=device))
env = ParallelEnv(3, env_fn)
collector = MultiaSyncDataCollector(
create_env_fn=[env, env],
policy=RandomPolicy(env.action_spec),
total_frames=-1,
max_frames_per_traj=100,
frames_per_batch=21,
init_random_frames=-1,
reset_at_each_iter=False,
split_trajs=True,
devices=[device, device],
passing_devices=[device, device],
update_at_each_batch=False,
init_with_lag=False,
exploration_mode="random",
)
for i, data in enumerate(collector):
if i == 3:
assert data.shape[0] == 3
assert data.shape[1] == 7
break
collector.shutdown()
del env
@pytest.mark.skipif(not _has_habitat, reason="habitat not installed")
@pytest.mark.parametrize("envname", ["HabitatRenderPick-v0", "HabitatRenderPick-v0"])
class TestHabitat:
def test_habitat(self, envname):
env = HabitatEnv(envname)
rollout = env.rollout(3)
_test_fake_tensordict(env)
@pytest.mark.skipif(not _has_jumanji, reason="jumanji not installed")
@pytest.mark.parametrize("envname", ["Snake-6x6-v0", "TSP50-v0"])
class TestJumanji:
def test_jumanji_seeding(self, envname):
final_seed = []
tdreset = []
tdrollout = []
for _ in range(2):
env = JumanjiEnv(envname)
torch.manual_seed(0)
np.random.seed(0)
final_seed.append(env.set_seed(0))
tdreset.append(env.reset())
tdrollout.append(env.rollout(max_steps=50))
env.close()
del env
assert final_seed[0] == final_seed[1]
assert_allclose_td(*tdreset)
assert_allclose_td(*tdrollout)
@pytest.mark.parametrize("batch_size", [(), (5,), (5, 4)])
def test_jumanji_batch_size(self, envname, batch_size):
env = JumanjiEnv(envname, batch_size=batch_size)
env.set_seed(0)
tdreset = env.reset()
tdrollout = env.rollout(max_steps=50)
env.close()
del env
assert tdreset.batch_size == batch_size
assert tdrollout.batch_size[:-1] == batch_size
@pytest.mark.parametrize("batch_size", [(), (5,), (5, 4)])
def test_jumanji_spec_rollout(self, envname, batch_size):
env = JumanjiEnv(envname, batch_size=batch_size)
env.set_seed(0)
_test_fake_tensordict(env)
@pytest.mark.parametrize("batch_size", [(), (5,), (5, 4)])
def test_jumanji_consistency(self, envname, batch_size):
import jax
import jax.numpy as jnp
import numpy as onp
env = JumanjiEnv(envname, batch_size=batch_size)
obs_keys = list(env.observation_spec.keys(True))
env.set_seed(1)
rollout = env.rollout(10)
env.set_seed(1)
key = env.key
base_env = env._env
key, *keys = jax.random.split(key, np.prod(batch_size) + 1)
state, timestep = jax.vmap(base_env.reset)(jnp.stack(keys))
# state = env._reshape(state)
# timesteps.append(timestep)
for i in range(rollout.shape[-1]):
action = rollout[..., i]["action"]
# state = env._flatten(state)
action = env._flatten(env.read_action(action))
state, timestep = jax.vmap(base_env.step)(state, action)
# state = env._reshape(state)
# timesteps.append(timestep)
checked = False
for _key in obs_keys:
if isinstance(_key, str):
_key = (_key,)
try:
t2 = getattr(timestep, _key[0])
except AttributeError:
try:
t2 = getattr(timestep.observation, _key[0])
except AttributeError:
continue
t1 = rollout[..., i][("next", *_key)]
for __key in _key[1:]:
t2 = getattr(t2, _key)
t2 = torch.tensor(onp.asarray(t2)).view_as(t1)
torch.testing.assert_close(t1, t2)
checked = True
if not checked:
raise AttributeError(
f"None of the keys matched: {rollout}, {list(timestep.__dict__.keys())}"
)
if __name__ == "__main__":
args, unknown = argparse.ArgumentParser().parse_known_args()
pytest.main([__file__, "--capture", "no", "--exitfirst"] + unknown)