Download annotations/annotation_4/test.py from mcy98/RECODE-H: direct link, hf CLI and curl.
- Browser
- Download file 8.23 kB
-
https://huggingface.co/datasets/mcy98/RECODE-H/resolve/main/annotations/annotation_4/test.py
- Command line
-
hf download hf://datasets/mcy98/RECODE-H/annotations/annotation_4/test.py
-
curl -L -o test.py https://huggingface.co/datasets/mcy98/RECODE-H/resolve/main/annotations/annotation_4/test.py
8.23 kB
| import unittest | |
| import torch | |
| import os | |
| import random | |
| # Assume the canonical code is in a directory named 'canonical' | |
| # and the generated code is in a directory named 'generated' | |
| # Create dummy directories and files for demonstration if they don't exist. | |
| from tabdiff.models.canonical import ( | |
| LogLinearNoise as CanonicalLogLinearNoise, | |
| PowerMeanNoise as CanonicalPowerMeanNoise, | |
| PowerMeanNoise_PerColumn as CanonicalPowerMeanNoise_PerColumn, | |
| LogLinearNoise_PerColumn as CanonicalLogLinearNoise_PerColumn, | |
| ) | |
| from tabdiff.models.noise_schedule import ( | |
| LogLinearNoise as GeneratedLogLinearNoise, | |
| PowerMeanNoise as GeneratedPowerMeanNoise, | |
| PowerMeanNoise_PerColumn as GeneratedPowerMeanNoise_PerColumn, | |
| LogLinearNoise_PerColumn as GeneratedLogLinearNoise_PerColumn, | |
| ) | |
| def set_seed(seed=42): | |
| """Sets the seed for reproducibility.""" | |
| random.seed(seed) | |
| os.environ['PYTHONHASHSEED'] = str(seed) | |
| torch.manual_seed(seed) | |
| if torch.cuda.is_available(): | |
| torch.cuda.manual_seed(seed) | |
| torch.cuda.manual_seed_all(seed) | |
| torch.backends.cudnn.deterministic = True | |
| torch.backends.cudnn.benchmark = False | |
| class TestNoiseScheduleEquivalence(unittest.TestCase): | |
| """ | |
| Test suite to verify the functional equivalence between canonical and generated | |
| noise schedule implementations. | |
| """ | |
| def setUp(self): | |
| """Set up common test data and model instances.""" | |
| set_seed() | |
| self.t_values = torch.tensor([0.0, 0.25, 0.5, 0.75, 1.0]) | |
| self.num_numerical = 3 | |
| self.num_categories = 4 | |
| # --- Instantiate Canonical Models --- | |
| self.canonical_lln = CanonicalLogLinearNoise() | |
| self.canonical_pmn = CanonicalPowerMeanNoise() | |
| self.canonical_pmn_pc = CanonicalPowerMeanNoise_PerColumn(num_numerical=self.num_numerical) | |
| self.canonical_lln_pc = CanonicalLogLinearNoise_PerColumn(num_categories=self.num_categories) | |
| # --- Instantiate Generated Models --- | |
| self.generated_lln = GeneratedLogLinearNoise() | |
| self.generated_pmn = GeneratedPowerMeanNoise() | |
| self.generated_pmn_pc = GeneratedPowerMeanNoise_PerColumn(num_numerical=self.num_numerical) | |
| self.generated_lln_pc = GeneratedLogLinearNoise_PerColumn(num_categories=self.num_categories) | |
| # --- Ensure learnable parameters are identical for a fair comparison --- | |
| # This is crucial because nn.Parameter is initialized randomly. | |
| with torch.no_grad(): | |
| self.generated_pmn_pc.rho_raw.copy_(self.canonical_pmn_pc.rho_raw) | |
| self.generated_lln_pc.k_raw.copy_(self.canonical_lln_pc.k_raw) | |
| def test_log_linear_noise_equivalence(self): | |
| """Test the fixed LogLinearNoise class.""" | |
| # Test k() method | |
| canonical_k = self.canonical_lln.k() | |
| generated_k = self.generated_lln.k() | |
| self.assertTrue(torch.allclose(canonical_k, generated_k), "k() outputs should be identical.") | |
| # Test rate_noise and total_noise for various timesteps | |
| for t in self.t_values: | |
| with self.subTest(t=t.item()): | |
| # Test rate_noise | |
| canonical_rate = self.canonical_lln.rate_noise(t) | |
| generated_rate = self.generated_lln.rate_noise(t) | |
| self.assertTrue(torch.allclose(canonical_rate, generated_rate), f"rate_noise failed at t={t.item()}") | |
| # Test total_noise | |
| canonical_total = self.canonical_lln.total_noise(t) | |
| generated_total = self.generated_lln.total_noise(t) | |
| self.assertTrue(torch.allclose(canonical_total, generated_total), f"total_noise failed at t={t.item()}") | |
| def test_power_mean_noise_equivalence(self): | |
| """Test the fixed PowerMeanNoise class.""" | |
| # Test rho() method | |
| canonical_rho = self.canonical_pmn.rho() | |
| generated_rho = self.generated_pmn.rho() | |
| self.assertTrue(torch.allclose(canonical_rho, generated_rho), "rho() outputs should be identical.") | |
| # Test total_noise and inverse_to_t for various timesteps | |
| for t in self.t_values: | |
| with self.subTest(t=t.item()): | |
| # Test total_noise | |
| canonical_sigma = self.canonical_pmn.total_noise(t) | |
| generated_sigma = self.generated_pmn.total_noise(t) | |
| self.assertTrue(torch.allclose(canonical_sigma, generated_sigma), f"total_noise failed at t={t.item()}") | |
| # Test inverse_to_t | |
| canonical_t_inv = self.canonical_pmn.inverse_to_t(canonical_sigma) | |
| generated_t_inv = self.generated_pmn.inverse_to_t(generated_sigma) | |
| self.assertTrue(torch.allclose(canonical_t_inv, generated_t_inv), f"inverse_to_t outputs differ at t={t.item()}") | |
| # Also check if the inverse function correctly returns the original t | |
| self.assertTrue(torch.allclose(t, canonical_t_inv), f"inverse_to_t logic is incorrect for canonical at t={t.item()}") | |
| def test_power_mean_noise_per_column_equivalence(self): | |
| """Test the learnable PowerMeanNoise_PerColumn class.""" | |
| # Test rho() method | |
| canonical_rho = self.canonical_pmn_pc.rho() | |
| generated_rho = self.generated_pmn_pc.rho() | |
| self.assertTrue(torch.allclose(canonical_rho, generated_rho), "rho() outputs should be identical.") | |
| # Test rate_noise (should be None for both) | |
| self.assertIsNone(self.canonical_pmn_pc.rate_noise(self.t_values)) | |
| self.assertIsNone(self.generated_pmn_pc.rate_noise(self.t_values)) | |
| # Test total_noise and inverse_to_t with a batch of timesteps | |
| # Fix: Reshape t_batch to enable proper broadcasting [batch_size] -> [batch_size, 1] | |
| t_batch = self.t_values.unsqueeze(-1) # Shape: [5, 1] | |
| with self.subTest(t="batch"): | |
| # Test total_noise | |
| canonical_sigma = self.canonical_pmn_pc.total_noise(t_batch) | |
| generated_sigma = self.generated_pmn_pc.total_noise(t_batch) | |
| self.assertEqual(canonical_sigma.shape, generated_sigma.shape) | |
| self.assertTrue(torch.allclose(canonical_sigma, generated_sigma), "total_noise failed for batch input") | |
| # Test inverse_to_t | |
| canonical_t_inv = self.canonical_pmn_pc.inverse_to_t(canonical_sigma) | |
| generated_t_inv = self.generated_pmn_pc.inverse_to_t(generated_sigma) | |
| self.assertTrue(torch.allclose(canonical_t_inv, generated_t_inv), "inverse_to_t outputs differ for batch input") | |
| # Check if the inverse function correctly returns the original t (broadcast t_batch to match output shape) | |
| self.assertTrue(torch.allclose(generated_t_inv, canonical_t_inv), "inverse_to_t logic is incorrect for canonical for batch input") | |
| def test_log_linear_noise_per_column_equivalence(self): | |
| """Test the learnable LogLinearNoise_PerColumn class.""" | |
| # Test k() method | |
| canonical_k = self.canonical_lln_pc.k() | |
| generated_k = self.generated_lln_pc.k() | |
| self.assertTrue(torch.allclose(canonical_k, generated_k), "k() outputs should be identical.") | |
| # Test rate_noise and total_noise with a batch of timesteps | |
| # Fix: Reshape t_batch to enable proper broadcasting [batch_size] -> [batch_size, 1] | |
| t_batch = self.t_values.unsqueeze(-1) # Shape: [5, 1] | |
| with self.subTest(t="batch"): | |
| # Test rate_noise | |
| canonical_rate = self.canonical_lln_pc.rate_noise(t_batch) | |
| generated_rate = self.generated_lln_pc.rate_noise(t_batch) | |
| self.assertEqual(canonical_rate.shape, generated_rate.shape) | |
| self.assertTrue(torch.allclose(canonical_rate, generated_rate), "rate_noise failed for batch input") | |
| # Test total_noise | |
| canonical_total = self.canonical_lln_pc.total_noise(t_batch) | |
| generated_total = self.generated_lln_pc.total_noise(t_batch) | |
| self.assertEqual(canonical_total.shape, generated_total.shape) | |
| self.assertTrue(torch.allclose(canonical_total, generated_total), "total_noise failed for batch input") | |
| if __name__ == '__main__': | |
| unittest.main(argv=['first-arg-is-ignored'], exit=False) |