File size: 1,858 Bytes
63dd946
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) 2026 The Anuvani-Diarization authors
# SPDX-License-Identifier: MIT OR Apache-2.0
"""numpy add-reduction (pairwise summation) vs diar_exact::pairwise, bitwise."""
import numpy as np
from hypothesis import given, strategies as st
from hypothesis.extra import numpy as hnp

from conftest import bits_equal

finite64 = st.floats(allow_nan=False, allow_infinity=False, width=64)
any64 = st.floats(width=64)
finite32 = st.floats(allow_nan=False, allow_infinity=False, width=32)


@given(hnp.arrays(np.float64, st.integers(0, 600), elements=any64))
def test_sum_f64_any(core, x):
    assert bits_equal(np.float64(core.sum_f64(x)), np.add.reduce(x))


@given(hnp.arrays(np.float32, st.integers(0, 600), elements=finite32))
def test_sum_f32(core, x):
    assert bits_equal(np.float32(core.sum_f32(x)), np.add.reduce(x))


def test_lengths_around_block_edges(core):
    rng = np.random.default_rng(0)
    for n in list(range(0, 300)) + [511, 512, 513, 1023, 1024, 1025, 4097, 8191, 8192, 8193, 70000]:
        for scale in (1.0, 1e-30, 1e30):
            x = (rng.standard_normal(n) * scale).astype(np.float64)
            assert bits_equal(np.float64(core.sum_f64(x)), np.add.reduce(x)), n
            y = x.astype(np.float32)
            assert bits_equal(np.float32(core.sum_f32(y)), np.add.reduce(y)), n


def test_signed_zeros(core):
    for x in ([-0.0], [-0.0, -0.0], [-0.0] * 9, [-0.0] * 200, []):
        a = np.array(x, dtype=np.float64)
        assert bits_equal(np.float64(core.sum_f64(a)), np.add.reduce(a))


@given(hnp.arrays(np.float32, st.tuples(st.integers(1, 40), st.integers(1, 300)), elements=finite32))
def test_sum_axes_f32(core, x):
    r1, r0 = core.sum_axes_f32(np.ascontiguousarray(x))
    assert bits_equal(np.array(r1, dtype=np.float32), x.sum(axis=1))
    assert bits_equal(np.array(r0, dtype=np.float32), x.sum(axis=0))