|
| 1 | +import torch |
| 2 | + |
| 3 | +from auto_cast.encoders.permute_concat import PermuteConcat |
| 4 | +from auto_cast.types import Batch |
| 5 | + |
| 6 | + |
| 7 | +def _make_batch( |
| 8 | + batch_size: int = 1, |
| 9 | + t: int = 1, |
| 10 | + w: int = 2, |
| 11 | + h: int = 3, |
| 12 | + c: int = 2, |
| 13 | + const_c: int = 1, |
| 14 | + scalar_c: int = 1, |
| 15 | +) -> Batch: |
| 16 | + input_fields = torch.arange(batch_size * t * w * h * c, dtype=torch.float32) |
| 17 | + input_fields = input_fields.view(batch_size, t, w, h, c) |
| 18 | + output_fields = torch.zeros(batch_size, t, w, h, c) |
| 19 | + constant_fields = torch.ones(batch_size, w, h, const_c) |
| 20 | + constant_scalars = torch.full((batch_size, scalar_c), 5.0) |
| 21 | + return Batch( |
| 22 | + input_fields=input_fields, |
| 23 | + output_fields=output_fields, |
| 24 | + constant_scalars=constant_scalars, |
| 25 | + constant_fields=constant_fields, |
| 26 | + ) |
| 27 | + |
| 28 | + |
| 29 | +def test_permute_concat_with_constants(): |
| 30 | + encoder = PermuteConcat(with_constants=True) |
| 31 | + batch = _make_batch() |
| 32 | + |
| 33 | + encoded = encoder(batch) |
| 34 | + |
| 35 | + expected = batch.input_fields.permute(0, 4, 1, 2, 3) |
| 36 | + |
| 37 | + base_channels = batch.input_fields.shape[-1] |
| 38 | + assert batch.constant_fields is not None |
| 39 | + assert batch.constant_scalars is not None |
| 40 | + const_channels = batch.constant_fields.shape[-1] |
| 41 | + scalar_channels = batch.constant_scalars.shape[-1] |
| 42 | + |
| 43 | + assert encoded.shape == ( |
| 44 | + batch.input_fields.shape[0], |
| 45 | + base_channels + const_channels + scalar_channels, |
| 46 | + batch.input_fields.shape[1], |
| 47 | + batch.input_fields.shape[2], |
| 48 | + batch.input_fields.shape[3], |
| 49 | + ) |
| 50 | + |
| 51 | + assert torch.allclose(encoded[:, :base_channels, ...], expected) |
| 52 | + const_slice = encoded[:, base_channels : base_channels + const_channels, ...] |
| 53 | + assert torch.allclose(const_slice, torch.ones_like(const_slice)) |
| 54 | + scalar_slice = encoded[:, -scalar_channels:, ...] |
| 55 | + assert torch.allclose(scalar_slice, torch.full_like(scalar_slice, 5.0)) |
0 commit comments