File: blis_tests_common.py

package info (click to toggle)
python-cython-blis 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 43,676 kB
  • sloc: ansic: 645,510; sh: 2,354; asm: 1,466; python: 821; cpp: 585; makefile: 14
file content (72 lines) | stat: -rw-r--r-- 2,547 bytes parent folder | download | duplicates (2)
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
from __future__ import print_function

import numpy as np

np.random.seed(0)
from numpy.testing import assert_allclose

from hypothesis import given, assume
from hypothesis.strategies import tuples, lists, integers, floats
from hypothesis.extra.numpy import arrays


def lengths(lo=1, hi=10):
    return integers(min_value=lo, max_value=hi)


def shapes(min_rows=1, max_rows=100, min_cols=1, max_cols=100):
    return tuples(lengths(lo=min_rows, hi=max_rows), lengths(lo=min_cols, hi=max_cols))


def ndarrays_of_shape(shape, lo=-1000.0, hi=1000.0, dtype='float64'):
    return arrays(dtype, shape=shape, elements=floats(min_value=lo, max_value=hi, width=32))
    

def ndarrays(min_len=0, max_len=10, min_val=-10000000.0, max_val=1000000.0, dtype='float64'):
    return lengths(lo=min_len, hi=max_len).flatmap(
        lambda n: ndarrays_of_shape(n, lo=min_val, hi=max_val, dtype=dtype))


def matrices(min_rows=1, max_rows=10, min_cols=1, max_cols=10,
             min_value=-10000000.0, max_value=1000000.0, dtype='float64'):
    return shapes(min_rows=min_rows, max_rows=max_rows,
                  min_cols=min_cols, max_cols=max_cols).flatmap(
                        lambda mn: ndarrays_of_shape(mn, lo=min_value, hi=max_value,
                                                     dtype=dtype))


def positive_ndarrays(min_len=0, max_len=10, max_val=100000.0, dtype='float64'):
    return ndarrays(min_len=min_len, max_len=max_len, min_val=0, max_val=max_val,
                    dtype=dtype)


def negative_ndarrays(min_len=0, max_len=10, min_val=-100000.0, dtype='float64'):
    return ndarrays(min_len=min_len, max_len=max_len, min_val=min_val, max_val=-1e-10,
                    dtype=dtype)


def parse_layer(layer_data):
    # Get the first row, excluding the first column
    x = layer_data[0,1:]
    # Get the first column, excluding the first row
    # .ascontiguousarray is support important here!!!!
    b = np.ascontiguousarray(layer_data[1:,0], dtype='float64')
    # Slice out the row and the column used for the X and the bias
    W = layer_data[1:,1:]
    assert x.ndim == 1
    assert b.ndim == 1
    assert b.shape[0] == W.shape[0]
    assert x.shape[0] == W.shape[1]
    assume(not np.isnan(W.sum()))
    assume(not np.isnan(x.sum()))
    assume(not np.isnan(b.sum()))
    assume(not any(np.isinf(val) for val in W.flatten()))
    assume(not any(np.isinf(val) for val in x))
    assume(not any(np.isinf(val) for val in b))
    return x, b, W
 

def split_row(layer_data):
    return (layer_data[0,:], layer_data[:,:])