|
| 1 | +import pytest |
| 2 | +import torch |
| 3 | +from torch_scatter import scatter_add |
| 4 | + |
| 5 | +from .utils import devices |
| 6 | + |
| 7 | + |
| 8 | +@pytest.mark.parametrize('device', devices) |
| 9 | +def test_broadcasting(device): |
| 10 | + B, C, H, W = (4, 3, 8, 8) |
| 11 | + |
| 12 | + src = torch.randn((B, C, H, W), device=device) |
| 13 | + index = torch.randint(0, H, (B, 1, H, W)).to(device, torch.long) |
| 14 | + out = scatter_add(src, index, dim=2, dim_size=H) |
| 15 | + assert out.size() == (B, C, H, W) |
| 16 | + |
| 17 | + src = torch.randn((B, 1, H, W), device=device) |
| 18 | + index = torch.randint(0, H, (B, C, H, W)).to(device, torch.long) |
| 19 | + out = scatter_add(src, index, dim=2, dim_size=H) |
| 20 | + assert out.size() == (B, C, H, W) |
| 21 | + |
| 22 | + src = torch.randn((B, 1, H, W), device=device) |
| 23 | + index = torch.randint(0, H, (B, 1, H, W)).to(device, torch.long) |
| 24 | + out = scatter_add(src, index, dim=2, dim_size=H) |
| 25 | + assert out.size() == (B, 1, H, W) |
| 26 | + |
| 27 | + src = torch.randn((B, C, H, W), device=device) |
| 28 | + index = torch.randint(0, H, (H, )).to(device, torch.long) |
| 29 | + out = scatter_add(src, index, dim=2, dim_size=H) |
| 30 | + assert out.size() == (B, C, H, W) |
0 commit comments