|
| 1 | +import torch |
| 2 | +from torch import nn |
| 3 | + |
| 4 | +import continual as co |
| 5 | + |
| 6 | + |
| 7 | +def test_nn_norms(): |
| 8 | + S = 3 |
| 9 | + |
| 10 | + long_example_clip = torch.normal(mean=torch.zeros(10 * 3 * 3)).reshape( |
| 11 | + (1, 1, 10, 3, 3) |
| 12 | + ) |
| 13 | + |
| 14 | + b_norm = nn.BatchNorm3d(1) |
| 15 | + b_norm.weight = nn.Parameter(3 * torch.ones_like(b_norm.weight)) |
| 16 | + b_norm.bias = nn.Parameter(1 * torch.ones_like(b_norm.bias)) |
| 17 | + |
| 18 | + i_norm = nn.InstanceNorm3d(2, affine=True, track_running_stats=True) |
| 19 | + i_norm.weight = nn.Parameter(4 * torch.ones_like(i_norm.weight)) |
| 20 | + i_norm.bias = nn.Parameter(2 * torch.ones_like(i_norm.bias)) |
| 21 | + |
| 22 | + l_norm = nn.LayerNorm([S, S]) # NB: Doesn't work over temporal axis |
| 23 | + l_norm.weight = nn.Parameter(5 * torch.ones_like(l_norm.weight)) |
| 24 | + l_norm.bias = nn.Parameter(3 * torch.ones_like(l_norm.bias)) |
| 25 | + |
| 26 | + g_norm = nn.GroupNorm(2, 2) |
| 27 | + g_norm.weight = nn.Parameter(6 * torch.ones_like(g_norm.weight)) |
| 28 | + g_norm.bias = nn.Parameter(4 * torch.ones_like(g_norm.bias)) |
| 29 | + |
| 30 | + seq = nn.Sequential( |
| 31 | + b_norm, |
| 32 | + nn.Conv3d( |
| 33 | + in_channels=1, |
| 34 | + out_channels=2, |
| 35 | + kernel_size=(5, S, S), |
| 36 | + bias=True, |
| 37 | + padding=(0, 1, 1), |
| 38 | + padding_mode="zeros", |
| 39 | + ), |
| 40 | + i_norm, |
| 41 | + l_norm, |
| 42 | + g_norm, |
| 43 | + nn.Conv3d( |
| 44 | + in_channels=2, |
| 45 | + out_channels=1, |
| 46 | + kernel_size=(3, S, S), |
| 47 | + bias=True, |
| 48 | + padding=(0, 1, 1), |
| 49 | + padding_mode="zeros", |
| 50 | + ), |
| 51 | + nn.MaxPool3d(kernel_size=(1, 2, 2)), |
| 52 | + ) |
| 53 | + seq.eval() |
| 54 | + |
| 55 | + coseq = co.Sequential.build_from(seq) |
| 56 | + coseq.eval() |
| 57 | + |
| 58 | + assert coseq.delay == (5 - 1) + (3 - 1) |
| 59 | + |
| 60 | + # forward |
| 61 | + output = seq.forward(long_example_clip) |
| 62 | + co_output = coseq.forward(long_example_clip) |
| 63 | + assert torch.allclose(output, co_output) |
| 64 | + |
| 65 | + # forward_steps |
| 66 | + co_output_firsts_0 = coseq.forward_steps( |
| 67 | + long_example_clip[:, :, :-1], update_state=False |
| 68 | + ) |
| 69 | + co_output_firsts = coseq.forward_steps(long_example_clip[:, :, :-1]) |
| 70 | + assert torch.allclose(co_output_firsts, co_output_firsts_0, atol=1e-7) |
| 71 | + assert torch.allclose(co_output_firsts, output[:, :, :-1], atol=1e-7) |
| 72 | + |
| 73 | + # forward_step |
| 74 | + co_output_last_0 = coseq.forward_step( |
| 75 | + long_example_clip[:, :, -1], update_state=False |
| 76 | + ) |
| 77 | + co_output_last = coseq.forward_step(long_example_clip[:, :, -1]) |
| 78 | + assert torch.allclose(co_output_last, co_output_last_0, atol=1e-7) |
| 79 | + assert torch.allclose(co_output_last, output[:, :, -1], atol=1e-7) |
| 80 | + |
| 81 | + # Clean state can be used to restart seq computation |
| 82 | + coseq.clean_state() |
| 83 | + co_output_firsts = coseq.forward_steps(long_example_clip[:, :, :-1]) |
| 84 | + assert torch.allclose(co_output_firsts, output[:, :, :-1], atol=1e-7) |
0 commit comments