test_array.py 2.12 KB
Newer Older
Paul's avatar
Paul committed
1
2
3
4
5
import migraphx, struct, array, sys
try:
    from functools import reduce
except:
    pass
Paul's avatar
Paul committed
6

kahmed10's avatar
kahmed10 committed
7

Paul's avatar
Paul committed
8
9
10
11
12
13
def assert_eq(x, y):
    if x == y:
        pass
    else:
        raise Exception(str(x) + " != " + str(y))

kahmed10's avatar
kahmed10 committed
14

Paul's avatar
Paul committed
15
def read_float(b, index):
kahmed10's avatar
kahmed10 committed
16
17
    return struct.unpack_from('f', b, index * 4)[0]

Paul's avatar
Paul committed
18

Paul's avatar
Paul committed
19
def write_float(b, index):
kahmed10's avatar
kahmed10 committed
20
21
    struct.pack_into('f', b, index * 4)

Paul's avatar
Paul committed
22
23

def nelements(lens):
kahmed10's avatar
kahmed10 committed
24
25
    return reduce(lambda x, y: x * y, lens, 1)

Paul's avatar
Paul committed
26

Paul's avatar
Paul committed
27
def create_buffer(t, data, shape):
Paul's avatar
Paul committed
28
29
30
    a = array.array(t, data)
    if sys.version_info >= (3, 0):
        m = memoryview(a.tobytes())
Paul's avatar
Paul committed
31
        return m.cast(t, shape)
Paul's avatar
Paul committed
32
33
34
35
    else:
        m = memoryview(a.tostring())
        return m

kahmed10's avatar
kahmed10 committed
36

Paul's avatar
Paul committed
37
def check_argument(a):
Paul's avatar
Paul committed
38
39
    l = a.tolist()
    for i in range(len(l)):
Paul's avatar
Paul committed
40
        assert_eq(l[i], read_float(a, i))
Paul's avatar
Paul committed
41

kahmed10's avatar
kahmed10 committed
42

Paul's avatar
Paul committed
43
44
def check_shapes(r, m):
    lens = list(m.shape)
kahmed10's avatar
kahmed10 committed
45
    strides = [int(s / m.itemsize) for s in m.strides]
Paul's avatar
Paul committed
46
47
48
49
50
    elements = nelements(lens)
    assert_eq(r.get_shape().elements(), elements)
    assert_eq(r.get_shape().lens(), lens)
    assert_eq(r.get_shape().strides(), strides)

kahmed10's avatar
kahmed10 committed
51

Paul's avatar
Paul committed
52
53
54
55
56
57
58
def run(p):
    params = {}
    for key, value in p.get_parameter_shapes().items():
        params[key] = migraphx.to_gpu(migraphx.generate_argument(value))

    return migraphx.from_gpu(p.run(params))

kahmed10's avatar
kahmed10 committed
59

Paul's avatar
Paul committed
60
61
62
63
64
65
66
def test_shape(shape):
    data = list(range(nelements(shape)))
    m = create_buffer('f', data, shape)
    a = migraphx.argument(m)
    check_shapes(a, m)
    assert_eq(a.tolist(), data)

kahmed10's avatar
kahmed10 committed
67

Paul's avatar
Paul committed
68
69
def test_input():
    if sys.version_info >= (3, 0):
Paul's avatar
Paul committed
70
        test_shape([4])
Paul's avatar
Paul committed
71
        test_shape([2, 3])
Paul's avatar
Paul committed
72
    else:
Paul's avatar
Paul committed
73
74
        data = list(range(4))
        m = create_buffer('f', data, [4])
Paul's avatar
Paul committed
75
76
77
78
79
80
81
        a1 = migraphx.argument(m)
        a2 = migraphx.argument(bytearray(a1))
        check_shapes(a2, m)
        assert_eq(a1.tolist(), m.tolist())


def test_output():
Khalique's avatar
Khalique committed
82
    p = migraphx.parse_onnx("conv_relu_maxpool_test.onnx")
Paul's avatar
Paul committed
83
    p.compile(migraphx.get_target("gpu"))
84

Paul's avatar
Paul committed
85
86
87
88
    r1 = run(p)
    r2 = run(p)
    assert_eq(r1, r2)
    assert_eq(r1.tolist(), r2.tolist())
89

Paul's avatar
Paul committed
90
91
    check_argument(r1)
    check_argument(r2)
Paul's avatar
Paul committed
92

Paul's avatar
Paul committed
93
94
    m1 = memoryview(r1)
    m2 = memoryview(r2)
Paul's avatar
Paul committed
95

Paul's avatar
Paul committed
96
97
    check_shapes(r1, m1)
    check_shapes(r2, m2)
Paul's avatar
Paul committed
98

99

Paul's avatar
Paul committed
100
101
test_input()
test_output()