|
| 1 | +"""Tests for analysis primitives (ParameterSnapshot, ParameterDelta).""" |
| 2 | + |
| 3 | +import torch |
| 4 | +import torch.nn as nn |
| 5 | + |
| 6 | +from grail.trainer.analysis.primitives import ParameterSnapshot |
| 7 | + |
| 8 | + |
| 9 | +class SimpleModel(nn.Module): |
| 10 | + """Simple model for testing.""" |
| 11 | + |
| 12 | + def __init__(self): |
| 13 | + super().__init__() |
| 14 | + self.linear1 = nn.Linear(10, 20) |
| 15 | + self.linear2 = nn.Linear(20, 5) |
| 16 | + |
| 17 | + def forward(self, x): |
| 18 | + return self.linear2(torch.relu(self.linear1(x))) |
| 19 | + |
| 20 | + |
| 21 | +def test_parameter_snapshot_creation(): |
| 22 | + """Test creating a parameter snapshot.""" |
| 23 | + model = SimpleModel() |
| 24 | + |
| 25 | + snapshot = ParameterSnapshot(model) |
| 26 | + |
| 27 | + assert len(snapshot) == 4 # 2 weights + 2 biases |
| 28 | + assert "linear1.weight" in snapshot |
| 29 | + assert "linear1.bias" in snapshot |
| 30 | + assert "linear2.weight" in snapshot |
| 31 | + assert "linear2.bias" in snapshot |
| 32 | + |
| 33 | + # Check device and dtype |
| 34 | + assert snapshot.device == "cpu" |
| 35 | + assert snapshot.dtype == torch.float32 |
| 36 | + |
| 37 | + |
| 38 | +def test_parameter_snapshot_immutable(): |
| 39 | + """Test that snapshot data is read-only.""" |
| 40 | + model = SimpleModel() |
| 41 | + snapshot = ParameterSnapshot(model) |
| 42 | + |
| 43 | + # Should not be able to modify snapshot data directly |
| 44 | + # (This is enforced by returning a dict view, not a settable attribute) |
| 45 | + original_weight = snapshot.data["linear1.weight"].clone() |
| 46 | + |
| 47 | + # Modifying model should not affect snapshot |
| 48 | + model.linear1.weight.data.fill_(42.0) |
| 49 | + |
| 50 | + assert not torch.allclose(snapshot.data["linear1.weight"], model.linear1.weight.data) |
| 51 | + assert torch.allclose(snapshot.data["linear1.weight"], original_weight) |
| 52 | + |
| 53 | + |
| 54 | +def test_parameter_delta_computation(): |
| 55 | + """Test computing delta between two snapshots.""" |
| 56 | + model = SimpleModel() |
| 57 | + |
| 58 | + # Take initial snapshot |
| 59 | + snapshot1 = ParameterSnapshot(model) |
| 60 | + |
| 61 | + # Modify model |
| 62 | + with torch.no_grad(): |
| 63 | + model.linear1.weight.data += 0.5 |
| 64 | + model.linear1.bias.data -= 0.1 |
| 65 | + |
| 66 | + # Take new snapshot |
| 67 | + snapshot2 = ParameterSnapshot(model) |
| 68 | + |
| 69 | + # Compute delta |
| 70 | + delta = snapshot1.compute_delta(snapshot2) |
| 71 | + |
| 72 | + assert len(delta) == 4 |
| 73 | + |
| 74 | + # Check that deltas are correct |
| 75 | + assert torch.allclose( |
| 76 | + delta.deltas["linear1.weight"], torch.full_like(delta.deltas["linear1.weight"], 0.5) |
| 77 | + ) |
| 78 | + assert torch.allclose( |
| 79 | + delta.deltas["linear1.bias"], torch.full_like(delta.deltas["linear1.bias"], -0.1) |
| 80 | + ) |
| 81 | + assert torch.allclose( |
| 82 | + delta.deltas["linear2.weight"], torch.zeros_like(delta.deltas["linear2.weight"]) |
| 83 | + ) |
| 84 | + |
| 85 | + |
| 86 | +def test_parameter_delta_statistics(): |
| 87 | + """Test delta statistics computation.""" |
| 88 | + model = SimpleModel() |
| 89 | + |
| 90 | + snapshot1 = ParameterSnapshot(model) |
| 91 | + |
| 92 | + # Make known changes |
| 93 | + with torch.no_grad(): |
| 94 | + model.linear1.weight.data += 1.0 # 10x20 = 200 params, each +1.0 |
| 95 | + model.linear1.bias.data += 2.0 # 20 params, each +2.0 |
| 96 | + |
| 97 | + snapshot2 = ParameterSnapshot(model) |
| 98 | + delta = snapshot1.compute_delta(snapshot2) |
| 99 | + |
| 100 | + stats = delta.statistics() |
| 101 | + |
| 102 | + assert "norm_l2" in stats |
| 103 | + assert "norm_l1" in stats |
| 104 | + assert "norm_max" in stats |
| 105 | + assert "mean" in stats |
| 106 | + assert "std" in stats |
| 107 | + |
| 108 | + # L1 norm should be: 200*1.0 + 20*2.0 = 240 |
| 109 | + expected_l1 = 200 * 1.0 + 20 * 2.0 + 100 * 0.0 + 5 * 0.0 |
| 110 | + assert abs(stats["norm_l1"] - expected_l1) < 1e-5 |
| 111 | + |
| 112 | + # Max should be 2.0 |
| 113 | + assert abs(stats["norm_max"] - 2.0) < 1e-5 |
| 114 | + |
| 115 | + |
| 116 | +def test_parameter_delta_sparsity(): |
| 117 | + """Test sparsity computation at different thresholds.""" |
| 118 | + model = SimpleModel() |
| 119 | + |
| 120 | + snapshot1 = ParameterSnapshot(model) |
| 121 | + |
| 122 | + # Create varied changes |
| 123 | + with torch.no_grad(): |
| 124 | + model.linear1.weight.data += 1e-5 # Above 1e-6 threshold |
| 125 | + model.linear1.bias.data += 1e-10 # Below 1e-6 threshold |
| 126 | + |
| 127 | + snapshot2 = ParameterSnapshot(model) |
| 128 | + delta = snapshot1.compute_delta(snapshot2) |
| 129 | + |
| 130 | + sparsity_1e6 = delta.sparsity_at_threshold(1e-6) |
| 131 | + |
| 132 | + # linear1.weight (200 params) should be kept (above threshold) |
| 133 | + # linear1.bias (20 params) should be dropped (below threshold) |
| 134 | + # linear2.* (105 params) should be dropped (zero) |
| 135 | + total_params = 200 + 20 + 100 + 5 # 325 |
| 136 | + kept_params = 200 |
| 137 | + |
| 138 | + assert sparsity_1e6["total_params"] == total_params |
| 139 | + assert sparsity_1e6["kept_params"] == kept_params |
| 140 | + assert abs(sparsity_1e6["kept_ratio"] - (kept_params / total_params)) < 1e-5 |
| 141 | + |
| 142 | + |
| 143 | +def test_parameter_delta_sparse_mask(): |
| 144 | + """Test applying sparse mask to delta.""" |
| 145 | + model = SimpleModel() |
| 146 | + |
| 147 | + snapshot1 = ParameterSnapshot(model) |
| 148 | + |
| 149 | + with torch.no_grad(): |
| 150 | + model.linear1.weight.data += 1e-5 |
| 151 | + |
| 152 | + snapshot2 = ParameterSnapshot(model) |
| 153 | + delta = snapshot1.compute_delta(snapshot2) |
| 154 | + |
| 155 | + # Apply sparse mask at 1e-6 |
| 156 | + sparse_delta = delta.apply_sparse_mask(threshold=1e-6) |
| 157 | + |
| 158 | + # Check that small changes were zeroed |
| 159 | + assert torch.allclose( |
| 160 | + sparse_delta.deltas["linear1.weight"], |
| 161 | + torch.full_like(sparse_delta.deltas["linear1.weight"], 1e-5), |
| 162 | + ) |
| 163 | + assert torch.allclose( |
| 164 | + sparse_delta.deltas["linear1.bias"], |
| 165 | + torch.zeros_like(sparse_delta.deltas["linear1.bias"]), |
| 166 | + ) |
| 167 | + |
| 168 | + |
| 169 | +def test_parameter_delta_per_layer_stats(): |
| 170 | + """Test per-layer statistics computation.""" |
| 171 | + model = SimpleModel() |
| 172 | + |
| 173 | + snapshot1 = ParameterSnapshot(model) |
| 174 | + |
| 175 | + with torch.no_grad(): |
| 176 | + model.linear1.weight.data += 1.0 |
| 177 | + model.linear2.weight.data += 2.0 |
| 178 | + |
| 179 | + snapshot2 = ParameterSnapshot(model) |
| 180 | + delta = snapshot1.compute_delta(snapshot2) |
| 181 | + |
| 182 | + per_layer = delta.per_layer_statistics() |
| 183 | + |
| 184 | + assert len(per_layer) == 4 |
| 185 | + assert "linear1.weight" in per_layer |
| 186 | + assert "linear2.weight" in per_layer |
| 187 | + |
| 188 | + # Check that means are correct |
| 189 | + assert abs(per_layer["linear1.weight"]["mean"] - 1.0) < 1e-5 |
| 190 | + assert abs(per_layer["linear2.weight"]["mean"] - 2.0) < 1e-5 |
0 commit comments