File size: 13,869 Bytes
a9d9f4e | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | import unittest
import torch
import torch.nn as nn
import torch.nn.functional as F
# Assuming the canonical WTTM class is in a file named 'canonical.py'
# and the generated WTTM class is in a file named 'wttm.py' in ./TTM/distiller_zoo/wttm.py
# For the purpose of this example, we'll use the provided code for both,
# but in a real scenario, these would be separate imports.
from canonical import WTTM as CanonicalWTTM
from TTM.distiller_zoo.wttm import WTTM as GeneratedWTTM
class TestWTTMLoss(unittest.TestCase):
def setUp(self):
# Initialize canonical and generated models for testing
self.canonical_wttm_low_l = CanonicalWTTM(l=0.1)
self.generated_wttm_low_l = GeneratedWTTM(l=0.1)
self.canonical_wttm_high_l = CanonicalWTTM(l=4.0)
self.generated_wttm_high_l = GeneratedWTTM(l=4.0)
self.canonical_wttm_mid_l = CanonicalWTTM(l=1.0)
self.generated_wttm_mid_l = GeneratedWTTM(l=1.0)
def test_init_equivalence(self):
# Test if the 'l' parameter is correctly initialized and stored
self.assertEqual(self.canonical_wttm_low_l.l, self.generated_wttm_low_l.l)
self.assertEqual(self.canonical_wttm_high_l.l, self.generated_wttm_high_l.l)
self.assertEqual(self.canonical_wttm_mid_l.l, self.generated_wttm_mid_l.l)
# Test with a different float value for 'l'
canonical_model = CanonicalWTTM(l=0.5)
generated_model = GeneratedWTTM(l=0.5)
self.assertEqual(canonical_model.l, generated_model.l)
# Test with an integer value for 'l'
canonical_model_int = CanonicalWTTM(l=1)
generated_model_int = GeneratedWTTM(l=1)
self.assertEqual(canonical_model_int.l, generated_model_int.l)
def test_forward_basic_case(self):
# Test case 1: Simple logits, batch size 2, 3 classes
y_s_input = torch.tensor([[1.0, 2.0, 3.0], [0.5, 1.5, 2.5]], dtype=torch.float32)
y_t_input = torch.tensor([[0.1, 0.2, 0.7], [1.0, 0.5, 0.1]], dtype=torch.float32)
canonical_loss = self.canonical_wttm_low_l(y_s_input, y_t_input)
generated_loss = self.generated_wttm_low_l(y_s_input, y_t_input)
# Assert numerical closeness of outputs
self.assertTrue(
torch.isclose(canonical_loss, generated_loss, rtol=1e-5, atol=1e-8),
f"Basic case failed for l={self.canonical_wttm_low_l.l}: "
f"Canonical={canonical_loss.item()}, Generated={generated_loss.item()}"
)
# Assert output is a scalar tensor (i.e., 0-dimensional float tensor)
self.assertIsInstance(generated_loss, torch.Tensor, "Generated output is not a torch.Tensor")
self.assertEqual(generated_loss.dim(), 0, "Generated output tensor is not a scalar")
self.assertIsInstance(canonical_loss, torch.Tensor, "Canonical output is not a torch.Tensor")
self.assertEqual(canonical_loss.dim(), 0, "Canonical output tensor is not a scalar")
def test_forward_high_l(self):
# Test case 2: Higher 'l' value (more peaked teacher distribution)
y_s_input = torch.tensor([[1.0, 0.5, 0.2], [5.0, 1.0, 0.1]], dtype=torch.float32)
y_t_input = torch.tensor([[10.0, 1.0, 0.0], [0.5, 0.8, 0.7]], dtype=torch.float32)
canonical_loss = self.canonical_wttm_high_l(y_s_input, y_t_input)
generated_loss = self.generated_wttm_high_l(y_s_input, y_t_input)
self.assertTrue(torch.isclose(canonical_loss, generated_loss, rtol=1e-5, atol=1e-8),
f"High 'l' case failed for l={self.canonical_wttm_high_l.l}: Canonical={canonical_loss.item()}, Generated={generated_loss.item()}")
self.assertIsInstance(generated_loss, torch.Tensor, "Generated output for high 'l' case is not a torch.Tensor")
self.assertEqual(generated_loss.dim(), 0, "Generated output tensor for high 'l' case is not a scalar")
self.assertIsInstance(canonical_loss, torch.Tensor, "Canonical output for high 'l' case is not a torch.Tensor")
self.assertEqual(canonical_loss.dim(), 0, "Canonical output tensor for high 'l' case is not a scalar")
def test_forward_zero_l(self):
# Test case 3: l = 0 (power transform effectively becomes 1, softmax outputs normalized)
# Note: torch.pow(x, 0) is 1 for x != 0.
canonical_wttm_zero_l = CanonicalWTTM(l=0.0)
generated_wttm_zero_l = GeneratedWTTM(l=0.0)
y_s_input = torch.tensor([[1.0, 2.0, 3.0], [0.5, 1.5, 2.5]], dtype=torch.float32)
y_t_input = torch.tensor([[0.1, 0.2, 0.7], [1.0, 0.5, 0.1]], dtype=torch.float32)
canonical_loss = canonical_wttm_zero_l(y_s_input, y_t_input)
generated_loss = generated_wttm_zero_l(y_s_input, y_t_input)
self.assertTrue(torch.isclose(canonical_loss, generated_loss, rtol=1e-5, atol=1e-8),
f"Zero 'l' case failed: Canonical={canonical_loss.item()}, Generated={generated_loss.item()}")
self.assertIsInstance(generated_loss, torch.Tensor, "Generated output for zero 'l' case is not a torch.Tensor")
self.assertEqual(generated_loss.dim(), 0, "Generated output tensor for zero 'l' case is not a scalar")
self.assertIsInstance(canonical_loss, torch.Tensor, "Canonical output for zero 'l' case is not a torch.Tensor")
self.assertEqual(canonical_loss.dim(), 0, "Canonical output tensor for zero 'l' case is not a scalar")
def test_forward_one_class_batch(self):
# Test case 4: Single class logits, single batch
y_s_input = torch.tensor([[10.0]], dtype=torch.float32)
y_t_input = torch.tensor([[5.0]], dtype=torch.float32)
canonical_loss = self.canonical_wttm_mid_l(y_s_input, y_t_input)
generated_loss = self.generated_wttm_mid_l(y_s_input, y_t_input)
self.assertTrue(torch.isclose(canonical_loss, generated_loss, rtol=1e-5, atol=1e-8),
f"Single class batch failed: Canonical={canonical_loss.item()}, Generated={generated_loss.item()}")
self.assertIsInstance(generated_loss, torch.Tensor, "Generated output for single class batch is not a torch.Tensor")
self.assertEqual(generated_loss.dim(), 0, "Generated output tensor for single class batch is not a scalar")
self.assertIsInstance(canonical_loss, torch.Tensor, "Canonical output for single class batch is not a torch.Tensor")
self.assertEqual(canonical_loss.dim(), 0, "Canonical output tensor for single class batch is not a scalar")
def test_forward_large_batch(self):
# Test case 5: Larger batch size, more classes
batch_size = 16
num_classes = 10
y_s_input = torch.randn(batch_size, num_classes, dtype=torch.float32) * 5
y_t_input = torch.randn(batch_size, num_classes, dtype=torch.float32) * 5
canonical_loss = self.canonical_wttm_low_l(y_s_input, y_t_input)
generated_loss = self.generated_wttm_low_l(y_s_input, y_t_input)
self.assertTrue(torch.isclose(canonical_loss, generated_loss, rtol=1e-5, atol=1e-8),
f"Large batch failed: Canonical={canonical_loss.item()}, Generated={generated_loss.item()}")
self.assertIsInstance(generated_loss, torch.Tensor, "Generated output for large batch is not a torch.Tensor")
self.assertEqual(generated_loss.dim(), 0, "Generated output tensor for large batch is not a scalar")
self.assertIsInstance(canonical_loss, torch.Tensor, "Canonical output for large batch is not a torch.Tensor")
self.assertEqual(canonical_loss.dim(), 0, "Canonical output tensor for large batch is not a scalar")
def test_forward_negative_logits(self):
# Test case 6: Negative logits
y_s_input = torch.tensor([[-1.0, -2.0, -3.0], [-0.5, -1.5, -2.5]], dtype=torch.float32)
y_t_input = torch.tensor([[-0.1, -0.2, -0.7], [-1.0, -0.5, -0.1]], dtype=torch.float32)
canonical_loss = self.canonical_wttm_low_l(y_s_input, y_t_input)
generated_loss = self.generated_wttm_low_l(y_s_input, y_t_input)
self.assertTrue(torch.isclose(canonical_loss, generated_loss, rtol=1e-5, atol=1e-8),
f"Negative logits failed: Canonical={canonical_loss.item()}, Generated={generated_loss.item()}")
self.assertIsInstance(generated_loss, torch.Tensor, "Generated output for negative logits is not a torch.Tensor")
self.assertEqual(generated_loss.dim(), 0, "Generated output tensor for negative logits is not a scalar")
self.assertIsInstance(canonical_loss, torch.Tensor, "Canonical output for negative logits is not a torch.Tensor")
self.assertEqual(canonical_loss.dim(), 0, "Canonical output tensor for negative logits is not a scalar")
def test_forward_identical_inputs(self):
# Test case 7: Student and teacher logits are identical
y_s_input = torch.tensor([[1.0, 2.0, 3.0], [0.5, 1.5, 2.5]], dtype=torch.float32)
y_t_input = y_s_input.clone() # Make a copy to ensure they are separate tensors but same values
canonical_loss = self.canonical_wttm_low_l(y_s_input, y_t_input)
generated_loss = self.generated_wttm_low_l(y_s_input, y_t_input)
self.assertTrue(torch.isclose(canonical_loss, generated_loss, rtol=1e-5, atol=1e-8),
f"Identical inputs failed: Canonical={canonical_loss.item()}, Generated={generated_loss.item()}")
self.assertIsInstance(generated_loss, torch.Tensor, "Generated output for identical inputs is not a torch.Tensor")
self.assertEqual(generated_loss.dim(), 0, "Generated output tensor for identical inputs is not a scalar")
self.assertIsInstance(canonical_loss, torch.Tensor, "Canonical output for identical inputs is not a torch.Tensor")
self.assertEqual(canonical_loss.dim(), 0, "Canonical output tensor for identical inputs is not a scalar")
def test_forward_edge_case_near_zero_logits(self):
# Test case 8: Logits very close to zero
y_s_input = torch.tensor([[1e-8, 2e-8, 3e-8], [5e-9, 1.5e-8, 2.5e-8]], dtype=torch.float32)
y_t_input = torch.tensor([[1e-9, 2e-9, 7e-9], [1e-8, 0.5e-8, 0.1e-8]], dtype=torch.float32)
canonical_loss = self.canonical_wttm_mid_l(y_s_input, y_t_input)
generated_loss = self.generated_wttm_mid_l(y_s_input, y_t_input)
self.assertTrue(torch.isclose(canonical_loss, generated_loss, rtol=1e-5, atol=1e-8),
f"Near zero logits failed: Canonical={canonical_loss.item()}, Generated={generated_loss.item()}")
self.assertIsInstance(generated_loss, torch.Tensor, "Generated output for near zero logits is not a torch.Tensor")
self.assertEqual(generated_loss.dim(), 0, "Generated output tensor for near zero logits is not a scalar")
self.assertIsInstance(canonical_loss, torch.Tensor, "Canonical output for near zero logits is not a torch.Tensor")
self.assertEqual(canonical_loss.dim(), 0, "Canonical output tensor for near zero logits is not a scalar")
def test_forward_large_magnitude_logits(self):
# Test case 9: Logits with large magnitudes
y_s_input = torch.tensor([[100.0, 101.0, 102.0], [200.0, 201.0, 202.0]], dtype=torch.float32)
y_t_input = torch.tensor([[99.0, 100.0, 101.0], [199.0, 200.0, 201.0]], dtype=torch.float32)
canonical_loss = self.canonical_wttm_low_l(y_s_input, y_t_input)
generated_loss = self.generated_wttm_low_l(y_s_input, y_t_input)
self.assertTrue(torch.isclose(canonical_loss, generated_loss, rtol=1e-5, atol=1e-8),
f"Large magnitude logits failed: Canonical={canonical_loss.item()}, Generated={generated_loss.item()}")
self.assertIsInstance(generated_loss, torch.Tensor, "Generated output for large magnitude logits is not a torch.Tensor")
self.assertEqual(generated_loss.dim(), 0, "Generated output tensor for large magnitude logits is not a scalar")
self.assertIsInstance(canonical_loss, torch.Tensor, "Canonical output for large magnitude logits is not a torch.Tensor")
self.assertEqual(canonical_loss.dim(), 0, "Canonical output tensor for large magnitude logits is not a scalar")
def test_forward_different_l_values(self):
# Test case 10: Verify different 'l' values produce correct results
# This implicitly tests if self.l is correctly used.
l_values = [0.01, 0.5, 2.0, 10.0]
y_s_input = torch.tensor([[1.0, 2.0, 3.0], [0.5, 1.5, 2.5]], dtype=torch.float32)
y_t_input = torch.tensor([[0.1, 0.2, 0.7], [1.0, 0.5, 0.1]], dtype=torch.float32)
for l_val in l_values:
canonical_wttm = CanonicalWTTM(l=l_val)
generated_wttm = GeneratedWTTM(l=l_val)
canonical_loss = canonical_wttm(y_s_input, y_t_input)
generated_loss = generated_wttm(y_s_input, y_t_input)
self.assertTrue(torch.isclose(canonical_loss, generated_loss, rtol=1e-5, atol=1e-8),
f"Different 'l' value ({l_val}) failed: Canonical={canonical_loss.item()}, Generated={generated_loss.item()}")
self.assertIsInstance(generated_loss, torch.Tensor, "Generated output for different 'l' value is not a torch.Tensor")
self.assertEqual(generated_loss.dim(), 0, "Generated output tensor for different 'l' value is not a scalar")
self.assertIsInstance(canonical_loss, torch.Tensor, "Canonical output for different 'l' value is not a torch.Tensor")
self.assertEqual(canonical_loss.dim(), 0, "Canonical output tensor for different 'l' value is not a scalar")
if __name__ == '__main__':
unittest.main(argv=['first-arg-is-ignored'], exit=False)
|