Download annotations/annotation_12/test.py from mcy98/RECODE-H: direct link, hf CLI and curl.
- Browser
- Download file 13.9 kB
-
https://huggingface.co/datasets/mcy98/RECODE-H/resolve/main/annotations/annotation_12/test.py
- Command line
-
hf download hf://datasets/mcy98/RECODE-H/annotations/annotation_12/test.py
-
curl -L -o test.py https://huggingface.co/datasets/mcy98/RECODE-H/resolve/main/annotations/annotation_12/test.py
13.9 kB
| 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) | |