File size: 5,708 Bytes
e89a301
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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):
    @staticmethod
    def generate_random_int_vector(size, low=0, high=100):
        random_vector = np.random.randint(low, high, size)
        return random_vector

    @staticmethod
    def generate_random_binary_matrix(n, m):
        binary_matrix = np.random.randint(2, size=(n, m))
        return binary_matrix

    @staticmethod
    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)