Skip to content

Rare RLDA test failure #125

@rishubn

Description

@rishubn

Describe the bug
This test failed during a random test. Using removenan branch, possibly doesn't exist on main

_________________________________________________________________________________ test_rlda _________________________________________________________________________________

    def test_rlda():
        ns = 6
        n_components = 3
        for nv in [1, 2, 4]:
            for nb in [1, 4, 8, 12]:
>               rlda_inner_test(ns, min(nb, n_components), nb, nv)

tests/test_rlda.py:85: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

ns = 6, n_components = 1, nb = 1, nv = 2

    def rlda_inner_test(ns, n_components, nb, nv):
        nc = 2**nb
        n_model = 2000
        n_test = 500
        noise = 2000
        assert nv <= ns, "Test case error, use higher ns"
        # Generate profiling data
        m = np.ones((nv, nc, ns), dtype=np.int16)
        for v in range(nv):
            m[v, :, v] = (get_bits(np.arange(nc), nbits=nb).astype(np.int64) + 1).sum(
                axis=1
            ) * (v + 1)
        traces = np.random.randint(-noise, noise, (n_model, ns), dtype=np.int16)
        labels = np.random.randint(0, nc, (nv, n_model), dtype=np.uint64)
        for v in range(nv):
            traces += m[v, labels[v]].astype(np.int16)
        # Generate test data
        test_traces = np.random.randint(-noise, noise, (n_test, ns), dtype=np.int16)
        test_labels = np.random.randint(0, nc, (nv, n_test), dtype=np.uint16)
        for v in range(nv):
            test_traces += m[v, test_labels[v]]
    
        ## Run SCALib RLDA and compare projection matrices
        rlda = RLDAClassifier(nb, ns, nv, n_components)
        rlda.fit_u(traces, labels.T, 1)
        rlda.solve()
    
        for v in range(nv):
            ## Start computing RLDA in python
            label_bits = get_bits(labels[v], nbits=nb).astype(np.int64)
    
            B = label_bits.T @ label_bits
            C = label_bits.T @ traces
            S_L = traces.astype(np.int64).T @ traces.astype(np.int64)
    
            A = np.linalg.solve(B, C)
            mu = C[0] / n_model
            SB = A.T @ B @ A - n_model * np.outer(mu, mu)
            SW = S_L + A.T @ B @ A - C.T @ A - A.T @ C
    
            ev, W = eigh(SB, SW, subset_by_index=(ns - n_components, ns - 1))
            W = W.T
            cov = W @ SW @ W.T * 1 / n_model
    
            evals, evecs = np.linalg.eigh(cov)
            Wnorm = evecs * (evals[:, np.newaxis] ** -0.5)
    
            Weff = W.T @ Wnorm
            Aeff = A @ Weff
    
            prs_ref = (test_traces @ Weff)[:, :, np.newaxis] - (
                get_bits(np.arange(2**nb, dtype=np.uint64), nbits=nb) @ Aeff
            ).T[np.newaxis]
            prs_ref = np.exp(-0.5 * (prs_ref**2).sum(axis=1))
            prs_ref = prs_ref / prs_ref.sum(axis=1, keepdims=True)
    
            prs = rlda.predict_proba(test_traces, v)
    
>           assert np.allclose(prs, prs_ref)
E           assert False
E            +  where False = <function allclose at 0x7fcb457fb8b0>(array([[0.49879052, 0.50120948],\n       [0.50548702, 0.49451298],\n       [0.53497155, 0.46502845],\n       [0.51253122,...28571, 0.49671429],\n       [0.46444726, 0.53555274],\n       [0.50274778, 0.49725222],\n       [0.54640572, 0.45359428]]), array([[0.49878063, 0.50121937],\n       [0.50548422, 0.49451578],\n       [0.53497949, 0.46502051],\n       [0.51252112,...28582, 0.49671418],\n       [0.46445038, 0.53554962],\n       [0.50274603, 0.49725397],\n       [0.54640589, 0.45359411]]))
E            +    where <function allclose at 0x7fcb457fb8b0> = np.allclose

tests/test_rlda.py:77: AssertionError
--------------------------------------------------------------------------- Captured stdout setup ---------------------------------------------------------------------------
Random seeds: random.seed(488548030); np.random.seed(546442009)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions