|
6 | 6 |
|
7 | 7 | import argparse |
8 | 8 | import importlib.util |
| 9 | +from functools import partial |
9 | 10 |
|
10 | 11 | import pytest |
11 | 12 | import torch |
|
16 | 17 | from torch import autograd, nn |
17 | 18 | from torch.utils._pytree import tree_map |
18 | 19 | from torchrl.modules import ( |
| 20 | + IndependentNormal, |
19 | 21 | OneHotCategorical, |
20 | 22 | OneHotOrdinal, |
21 | 23 | Ordinal, |
@@ -169,6 +171,184 @@ def test_tanhnormal_event_dims(self, event_dims): |
169 | 171 | exp_shape, |
170 | 172 | ) |
171 | 173 |
|
| 174 | + @pytest.mark.parametrize("device", get_default_devices()) |
| 175 | + @pytest.mark.parametrize( |
| 176 | + "callable_scale", |
| 177 | + [torch.ones_like, partial(torch.full_like, fill_value=0.5)], |
| 178 | + ids=["ones_like", "full_like_partial"], |
| 179 | + ) |
| 180 | + def test_tanhnormal_callable_scale(self, device, callable_scale): |
| 181 | + """Test that TanhNormal supports callable scale for compile-friendliness. |
| 182 | +
|
| 183 | + Using a callable scale (e.g., torch.ones_like or partial(torch.full_like, fill_value=...)) |
| 184 | + avoids explicit device transfers and prevents graph breaks in torch.compile. |
| 185 | + """ |
| 186 | + torch.manual_seed(0) |
| 187 | + loc = torch.randn(3, 4, device=device) |
| 188 | + |
| 189 | + # Create distribution with callable scale |
| 190 | + dist = TanhNormal(loc=loc, scale=callable_scale, low=-1, high=1) |
| 191 | + |
| 192 | + # Check that the scale was properly resolved |
| 193 | + expected_scale = callable_scale(loc) |
| 194 | + torch.testing.assert_close(dist.scale, expected_scale) |
| 195 | + |
| 196 | + # Test sampling |
| 197 | + sample = dist.sample() |
| 198 | + assert sample.shape == loc.shape |
| 199 | + assert sample.device == loc.device |
| 200 | + assert (sample >= -1).all() |
| 201 | + assert (sample <= 1).all() |
| 202 | + |
| 203 | + # Test log_prob |
| 204 | + log_prob = dist.log_prob(sample) |
| 205 | + assert torch.isfinite(log_prob).all() |
| 206 | + |
| 207 | + # Test rsample with gradient |
| 208 | + loc_grad = torch.randn(3, 4, device=device, requires_grad=True) |
| 209 | + dist_grad = TanhNormal(loc=loc_grad, scale=callable_scale, low=-1, high=1) |
| 210 | + sample_grad = dist_grad.rsample() |
| 211 | + loss = sample_grad.sum() |
| 212 | + loss.backward() |
| 213 | + assert loc_grad.grad is not None |
| 214 | + assert torch.isfinite(loc_grad.grad).all() |
| 215 | + |
| 216 | + @pytest.mark.parametrize("device", get_default_devices()) |
| 217 | + def test_tanhnormal_callable_scale_update(self, device): |
| 218 | + """Test that TanhNormal.update() works with callable scale.""" |
| 219 | + torch.manual_seed(0) |
| 220 | + loc = torch.randn(3, 4, device=device) |
| 221 | + callable_scale = torch.ones_like |
| 222 | + |
| 223 | + dist = TanhNormal(loc=loc, scale=callable_scale, low=-1, high=1) |
| 224 | + |
| 225 | + # Update with new loc and callable scale |
| 226 | + new_loc = torch.randn(3, 4, device=device) |
| 227 | + dist.update(new_loc, callable_scale) |
| 228 | + |
| 229 | + # Check that scale was properly resolved |
| 230 | + torch.testing.assert_close(dist.scale, torch.ones_like(new_loc)) |
| 231 | + |
| 232 | + # Verify distribution works after update |
| 233 | + sample = dist.sample() |
| 234 | + assert sample.shape == new_loc.shape |
| 235 | + assert torch.isfinite(dist.log_prob(sample)).all() |
| 236 | + |
| 237 | + |
| 238 | +class TestIndependentNormal: |
| 239 | + @pytest.mark.parametrize("device", get_default_devices()) |
| 240 | + @pytest.mark.parametrize( |
| 241 | + "callable_scale", |
| 242 | + [torch.ones_like, partial(torch.full_like, fill_value=0.5)], |
| 243 | + ids=["ones_like", "full_like_partial"], |
| 244 | + ) |
| 245 | + def test_independentnormal_callable_scale(self, device, callable_scale): |
| 246 | + """Test that IndependentNormal supports callable scale for compile-friendliness. |
| 247 | +
|
| 248 | + Using a callable scale (e.g., torch.ones_like or partial(torch.full_like, fill_value=...)) |
| 249 | + avoids explicit device transfers and prevents graph breaks in torch.compile. |
| 250 | + """ |
| 251 | + torch.manual_seed(0) |
| 252 | + loc = torch.randn(3, 4, device=device) |
| 253 | + |
| 254 | + # Create distribution with callable scale |
| 255 | + dist = IndependentNormal(loc=loc, scale=callable_scale) |
| 256 | + |
| 257 | + # Check that the scale was properly resolved |
| 258 | + expected_scale = callable_scale(loc) |
| 259 | + torch.testing.assert_close(dist.base_dist.scale, expected_scale) |
| 260 | + |
| 261 | + # Test sampling |
| 262 | + sample = dist.sample() |
| 263 | + assert sample.shape == loc.shape |
| 264 | + assert sample.device == loc.device |
| 265 | + |
| 266 | + # Test log_prob |
| 267 | + log_prob = dist.log_prob(sample) |
| 268 | + assert torch.isfinite(log_prob).all() |
| 269 | + |
| 270 | + # Test rsample with gradient |
| 271 | + loc_grad = torch.randn(3, 4, device=device, requires_grad=True) |
| 272 | + dist_grad = IndependentNormal(loc=loc_grad, scale=callable_scale) |
| 273 | + sample_grad = dist_grad.rsample() |
| 274 | + loss = sample_grad.sum() |
| 275 | + loss.backward() |
| 276 | + assert loc_grad.grad is not None |
| 277 | + assert torch.isfinite(loc_grad.grad).all() |
| 278 | + |
| 279 | + @pytest.mark.parametrize("device", get_default_devices()) |
| 280 | + def test_independentnormal_callable_scale_update(self, device): |
| 281 | + """Test that IndependentNormal.update() works with callable scale.""" |
| 282 | + torch.manual_seed(0) |
| 283 | + loc = torch.randn(3, 4, device=device) |
| 284 | + callable_scale = torch.ones_like |
| 285 | + |
| 286 | + dist = IndependentNormal(loc=loc, scale=callable_scale) |
| 287 | + |
| 288 | + # Update with new loc and callable scale |
| 289 | + new_loc = torch.randn(3, 4, device=device) |
| 290 | + dist.update(new_loc, callable_scale) |
| 291 | + |
| 292 | + # Check that scale was properly resolved |
| 293 | + torch.testing.assert_close(dist.base_dist.scale, torch.ones_like(new_loc)) |
| 294 | + |
| 295 | + # Verify distribution works after update |
| 296 | + sample = dist.sample() |
| 297 | + assert sample.shape == new_loc.shape |
| 298 | + assert torch.isfinite(dist.log_prob(sample)).all() |
| 299 | + |
| 300 | + @pytest.mark.parametrize("device", get_default_devices()) |
| 301 | + @pytest.mark.parametrize("scale_type", ["tensor", "float", "callable"]) |
| 302 | + def test_independentnormal_scale_types(self, device, scale_type): |
| 303 | + """Test that IndependentNormal supports all scale types: tensor, float, callable.""" |
| 304 | + torch.manual_seed(0) |
| 305 | + loc = torch.randn(3, 4, device=device) |
| 306 | + |
| 307 | + if scale_type == "tensor": |
| 308 | + scale = torch.ones(3, 4, device=device) |
| 309 | + elif scale_type == "float": |
| 310 | + scale = 1.0 |
| 311 | + else: # callable |
| 312 | + scale = torch.ones_like |
| 313 | + |
| 314 | + dist = IndependentNormal(loc=loc, scale=scale) |
| 315 | + |
| 316 | + # Test sampling |
| 317 | + sample = dist.sample() |
| 318 | + assert sample.shape == loc.shape |
| 319 | + assert sample.device == loc.device |
| 320 | + |
| 321 | + # Test log_prob |
| 322 | + log_prob = dist.log_prob(sample) |
| 323 | + assert torch.isfinite(log_prob).all() |
| 324 | + |
| 325 | + @pytest.mark.parametrize("device", get_default_devices()) |
| 326 | + @pytest.mark.parametrize("scale_type", ["tensor", "float", "callable"]) |
| 327 | + def test_tanhnormal_scale_types(self, device, scale_type): |
| 328 | + """Test that TanhNormal supports all scale types: tensor, float, callable.""" |
| 329 | + torch.manual_seed(0) |
| 330 | + loc = torch.randn(3, 4, device=device) |
| 331 | + |
| 332 | + if scale_type == "tensor": |
| 333 | + scale = torch.ones(3, 4, device=device) |
| 334 | + elif scale_type == "float": |
| 335 | + scale = 1.0 |
| 336 | + else: # callable |
| 337 | + scale = torch.ones_like |
| 338 | + |
| 339 | + dist = TanhNormal(loc=loc, scale=scale, low=-1, high=1) |
| 340 | + |
| 341 | + # Test sampling |
| 342 | + sample = dist.sample() |
| 343 | + assert sample.shape == loc.shape |
| 344 | + assert sample.device == loc.device |
| 345 | + assert (sample >= -1).all() |
| 346 | + assert (sample <= 1).all() |
| 347 | + |
| 348 | + # Test log_prob |
| 349 | + log_prob = dist.log_prob(sample) |
| 350 | + assert torch.isfinite(log_prob).all() |
| 351 | + |
172 | 352 |
|
173 | 353 | class TestTruncatedNormal: |
174 | 354 | @pytest.mark.parametrize( |
|
0 commit comments