|
| 1 | +"""Tests for TTQLinear and TTQConv2d layers.""" |
| 2 | + |
| 3 | +import torch |
| 4 | +import torch.nn as nn |
| 5 | + |
| 6 | +from bitnet.nn.ttq_conv2d import TTQConv2d |
| 7 | +from bitnet.nn.ttq_linear import TTQLinear |
| 8 | + |
| 9 | + |
| 10 | +class TestTTQLinear: |
| 11 | + """Tests for TTQLinear layer.""" |
| 12 | + |
| 13 | + def test_forward_shape(self) -> None: |
| 14 | + """Output shape should be (batch, out_features).""" |
| 15 | + layer = TTQLinear(64, 32) |
| 16 | + x = torch.randn(8, 64) |
| 17 | + out = layer(x) |
| 18 | + assert out.shape == (8, 32) |
| 19 | + |
| 20 | + def test_gradient_flows(self) -> None: |
| 21 | + """Gradients should flow through the layer.""" |
| 22 | + layer = TTQLinear(64, 32) |
| 23 | + x = torch.randn(8, 64, requires_grad=True) |
| 24 | + out = layer(x) |
| 25 | + loss = out.sum() |
| 26 | + loss.backward() |
| 27 | + assert x.grad is not None |
| 28 | + assert layer.weight.grad is not None |
| 29 | + assert layer.wp.grad is not None |
| 30 | + assert layer.wn.grad is not None |
| 31 | + # Note: delta gradients may be None in simple forward passes since |
| 32 | + # it's used in comparison operations (weight > delta). In real training |
| 33 | + # with classification loss, delta gets gradients through the loss. |
| 34 | + |
| 35 | + def test_parameters_initialized_properly(self) -> None: |
| 36 | + """TTQ parameters should be initialized to reasonable values.""" |
| 37 | + layer = TTQLinear(64, 32) |
| 38 | + # wp and wn should be initialized to 1.0 |
| 39 | + assert torch.allclose(layer.wp, torch.ones(1)) |
| 40 | + assert torch.allclose(layer.wn, torch.ones(1)) |
| 41 | + # delta should be initialized to 0.7 * weight.std() |
| 42 | + assert layer.delta > 0 |
| 43 | + |
| 44 | + def test_numerical_stability_during_training(self) -> None: |
| 45 | + """Training should not produce NaN losses.""" |
| 46 | + # Create a simple model with TTQ layer |
| 47 | + layer = TTQLinear(10, 10) |
| 48 | + optimizer = torch.optim.SGD(layer.parameters(), lr=0.1) |
| 49 | + |
| 50 | + # Train for a few steps |
| 51 | + for _ in range(10): |
| 52 | + x = torch.randn(4, 10) |
| 53 | + target = torch.randn(4, 10) |
| 54 | + |
| 55 | + out = layer(x) |
| 56 | + loss = nn.functional.mse_loss(out, target) |
| 57 | + |
| 58 | + # Loss should not be NaN |
| 59 | + assert not torch.isnan(loss), "Loss became NaN during training" |
| 60 | + |
| 61 | + optimizer.zero_grad() |
| 62 | + loss.backward() |
| 63 | + optimizer.step() |
| 64 | + |
| 65 | + # Parameters should not be NaN |
| 66 | + assert not torch.isnan(layer.wp).any(), "wp became NaN" |
| 67 | + assert not torch.isnan(layer.wn).any(), "wn became NaN" |
| 68 | + assert not torch.isnan(layer.delta).any(), "delta became NaN" |
| 69 | + |
| 70 | + |
| 71 | +class TestTTQConv2d: |
| 72 | + """Tests for TTQConv2d layer.""" |
| 73 | + |
| 74 | + def test_forward_shape(self) -> None: |
| 75 | + """Output shape should follow conv2d formula.""" |
| 76 | + layer = TTQConv2d(3, 16, kernel_size=3, padding=1) |
| 77 | + x = torch.randn(4, 3, 32, 32) |
| 78 | + out = layer(x) |
| 79 | + assert out.shape == (4, 16, 32, 32) |
| 80 | + |
| 81 | + def test_gradient_flows(self) -> None: |
| 82 | + """Gradients should flow through the layer.""" |
| 83 | + layer = TTQConv2d(3, 16, kernel_size=3, padding=1) |
| 84 | + x = torch.randn(4, 3, 32, 32, requires_grad=True) |
| 85 | + out = layer(x) |
| 86 | + loss = out.sum() |
| 87 | + loss.backward() |
| 88 | + assert x.grad is not None |
| 89 | + assert layer.weight.grad is not None |
| 90 | + assert layer.wp.grad is not None |
| 91 | + assert layer.wn.grad is not None |
| 92 | + # Note: delta gradients may be None in simple forward passes since |
| 93 | + # it's used in comparison operations (weight > delta). In real training |
| 94 | + # with classification loss, delta gets gradients through the loss. |
| 95 | + |
| 96 | + def test_parameters_initialized_properly(self) -> None: |
| 97 | + """TTQ parameters should be initialized to reasonable values.""" |
| 98 | + layer = TTQConv2d(3, 16, kernel_size=3) |
| 99 | + # wp and wn should be initialized to 1.0 |
| 100 | + assert torch.allclose(layer.wp, torch.ones(1)) |
| 101 | + assert torch.allclose(layer.wn, torch.ones(1)) |
| 102 | + # delta should be initialized to 0.7 * weight.std() |
| 103 | + assert layer.delta > 0 |
| 104 | + |
| 105 | + def test_numerical_stability_during_training(self) -> None: |
| 106 | + """Training should not produce NaN losses.""" |
| 107 | + # Create a simple model with TTQ conv layer |
| 108 | + layer = TTQConv2d(3, 8, kernel_size=3, padding=1) |
| 109 | + optimizer = torch.optim.SGD(layer.parameters(), lr=0.1) |
| 110 | + |
| 111 | + # Train for a few steps |
| 112 | + for _ in range(10): |
| 113 | + x = torch.randn(2, 3, 16, 16) |
| 114 | + target = torch.randn(2, 8, 16, 16) |
| 115 | + |
| 116 | + out = layer(x) |
| 117 | + loss = nn.functional.mse_loss(out, target) |
| 118 | + |
| 119 | + # Loss should not be NaN |
| 120 | + assert not torch.isnan(loss), "Loss became NaN during training" |
| 121 | + |
| 122 | + optimizer.zero_grad() |
| 123 | + loss.backward() |
| 124 | + optimizer.step() |
| 125 | + |
| 126 | + # Parameters should not be NaN |
| 127 | + assert not torch.isnan(layer.wp).any(), "wp became NaN" |
| 128 | + assert not torch.isnan(layer.wn).any(), "wn became NaN" |
| 129 | + assert not torch.isnan(layer.delta).any(), "delta became NaN" |
| 130 | + |
| 131 | + def test_different_kernel_sizes(self) -> None: |
| 132 | + """Layer should work with various kernel sizes.""" |
| 133 | + for kernel_size in [1, 3, 5, 7]: |
| 134 | + layer = TTQConv2d(3, 8, kernel_size=kernel_size, padding=kernel_size // 2) |
| 135 | + x = torch.randn(2, 3, 16, 16) |
| 136 | + out = layer(x) |
| 137 | + assert out.shape == (2, 8, 16, 16) |
0 commit comments