Download annotations/annotation_31/test.py from mcy98/RECODE-H: direct link, hf CLI and curl.
- Browser
- Download file 5.71 kB
-
https://huggingface.co/datasets/mcy98/RECODE-H/resolve/main/annotations/annotation_31/test.py
- Command line
-
hf download hf://datasets/mcy98/RECODE-H/annotations/annotation_31/test.py
-
curl -L -o test.py https://huggingface.co/datasets/mcy98/RECODE-H/resolve/main/annotations/annotation_31/test.py
5.71 kB
| import unittest | |
| import numpy as np | |
| from canonical import ( | |
| RSRBinaryMultiplier as CanonicalRSRBinaryMultiplier, | |
| RSRTernaryMultiplier as CanonicalRSRTernaryMultiplier, | |
| RSRPlusPlusBinaryMultiplier as CanonicalRSRPlusPlusBinaryMultiplier, | |
| RSRPlusPlusTernaryMultiplier as CanonicalRSRPlusPlusTernaryMultiplier, | |
| ) | |
| from multipliers_np import ( | |
| RSRBinaryMultiplier as GeneratedRSRBinaryMultiplier, | |
| RSRTernaryMultiplier as GeneratedRSRTernaryMultiplier, | |
| RSRPlusPlusBinaryMultiplier as GeneratedRSRPlusPlusBinaryMultiplier, | |
| RSRPlusPlusTernaryMultiplier as GeneratedRSRPlusPlusTernaryMultiplier, | |
| ) | |
| class TestRSRMultiplierFunctionalEquivalence(unittest.TestCase): | |
| def generate_random_int_vector(size, low=0, high=100): | |
| random_vector = np.random.randint(low, high, size) | |
| return random_vector | |
| def generate_random_binary_matrix(n, m): | |
| binary_matrix = np.random.randint(2, size=(n, m)) | |
| return binary_matrix | |
| def generate_random_ternary_matrix(n, m): | |
| # Generates matrix with values -1, 0, 1 | |
| ternary_matrix = np.random.randint(low=-1, high=2, size=(n, m)) | |
| return ternary_matrix | |
| def setUp(self): | |
| # Small-sized, representative data for efficiency | |
| self.n_small = 2 ** 5 # e.g., 32 | |
| self.m_small = 2 ** 6 + 3 # e.g., 67 | |
| self.v_small = self.generate_random_int_vector(self.n_small) | |
| # Larger data for more comprehensive testing if needed, but keep it manageable | |
| # self.n_medium = 2 ** 10 | |
| # self.m_medium = 2 ** 11 + 5 | |
| # self.v_medium = self.generate_random_int_vector(self.n_medium) | |
| def _run_multiplier_test(self, canonical_multiplier_cls, generated_multiplier_cls, A, v): | |
| """Helper to run a test for a given multiplier type.""" | |
| # Execute the canonical function | |
| canonical_instance = canonical_multiplier_cls(A) | |
| canonical_result = canonical_instance.multiply(v) | |
| # Execute the generated function | |
| generated_instance = generated_multiplier_cls(A) | |
| generated_result = generated_instance.multiply(v) | |
| # Compare the outputs using numpy.testing.assert_allclose for numerical stability | |
| np.testing.assert_allclose( | |
| generated_result, | |
| canonical_result, | |
| rtol=1e-6, | |
| atol=1e-6, | |
| err_msg=f"Mismatch between {canonical_multiplier_cls.__name__} (canonical) and " | |
| f"{generated_multiplier_cls.__name__} (generated) results." | |
| ) | |
| self.assertEqual(generated_result.dtype, | |
| canonical_result.dtype, "Output data type mismatch") | |
| self.assertEqual(generated_result.shape, | |
| canonical_result.shape, "Output shape mismatch") | |
| def test_rsr_binary_multiplier_equivalence(self): | |
| """Tests functional equivalence for RSRBinaryMultiplier.""" | |
| A = self.generate_random_binary_matrix(self.n_small, self.n_small) | |
| self._run_multiplier_test( | |
| CanonicalRSRBinaryMultiplier, GeneratedRSRBinaryMultiplier, A, self.v_small) | |
| def test_rsr_ternary_multiplier_equivalence(self): | |
| """Tests functional equivalence for RSRTernaryMultiplier.""" | |
| A = self.generate_random_ternary_matrix(self.n_small, self.n_small) | |
| self._run_multiplier_test( | |
| CanonicalRSRTernaryMultiplier, GeneratedRSRTernaryMultiplier, A, self.v_small) | |
| def test_rsr_pp_binary_multiplier_equivalence(self): | |
| """Tests functional equivalence for RSRPlusPlusBinaryMultiplier.""" | |
| A = self.generate_random_binary_matrix(self.n_small, self.n_small) | |
| self._run_multiplier_test(CanonicalRSRPlusPlusBinaryMultiplier, | |
| GeneratedRSRPlusPlusBinaryMultiplier, A, self.v_small) | |
| def test_rsr_pp_ternary_multiplier_equivalence(self): | |
| """Tests functional equivalence for RSRPlusPlusTernaryMultiplier.""" | |
| A = self.generate_random_ternary_matrix(self.n_small, self.n_small) | |
| self._run_multiplier_test(CanonicalRSRPlusPlusTernaryMultiplier, | |
| GeneratedRSRPlusPlusTernaryMultiplier, A, self.v_small) | |
| def test_rsr_binary_non_square_equivalence(self): | |
| """Tests functional equivalence for RSRBinaryMultiplier with non-square matrix.""" | |
| A = self.generate_random_binary_matrix(self.n_small, self.m_small) | |
| self._run_multiplier_test( | |
| CanonicalRSRBinaryMultiplier, GeneratedRSRBinaryMultiplier, A, self.v_small) | |
| def test_rsr_ternary_non_square_equivalence(self): | |
| """Tests functional equivalence for RSRTernaryMultiplier with non-square matrix.""" | |
| A = self.generate_random_ternary_matrix(self.n_small, self.m_small) | |
| self._run_multiplier_test( | |
| CanonicalRSRTernaryMultiplier, GeneratedRSRTernaryMultiplier, A, self.v_small) | |
| def test_rsrpp_binary_non_square_equivalence(self): | |
| """Tests functional equivalence for RSRPlusPlusBinaryMultiplier with non-square matrix.""" | |
| A = self.generate_random_binary_matrix(self.n_small, self.m_small) | |
| self._run_multiplier_test(CanonicalRSRPlusPlusBinaryMultiplier, | |
| GeneratedRSRPlusPlusBinaryMultiplier, A, self.v_small) | |
| def test_rsrpp_ternary_non_square_equivalence(self): | |
| """Tests functional equivalence for RSRPlusPlusTernaryMultiplier with non-square matrix.""" | |
| A = self.generate_random_ternary_matrix(self.n_small, self.m_small) | |
| self._run_multiplier_test(CanonicalRSRPlusPlusTernaryMultiplier, | |
| GeneratedRSRPlusPlusTernaryMultiplier, A, self.v_small) | |