"git@developer.sourcefind.cn:wuxk1/megatron-lm.git" did not exist on "4f4715dbb5ae8a3e8abfd4d2982f008f10516b31"
test_array.py 3.39 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#####################################################################################
# The MIT License (MIT)
#
# Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#####################################################################################
Paul's avatar
Paul committed
24
25
26
27
28
import migraphx, struct, array, sys
try:
    from functools import reduce
except:
    pass
Paul's avatar
Paul committed
29

kahmed10's avatar
kahmed10 committed
30

Paul's avatar
Paul committed
31
32
33
34
35
36
def assert_eq(x, y):
    if x == y:
        pass
    else:
        raise Exception(str(x) + " != " + str(y))

kahmed10's avatar
kahmed10 committed
37

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

Paul's avatar
Paul committed
41

Paul's avatar
Paul committed
42
def write_float(b, index):
kahmed10's avatar
kahmed10 committed
43
44
    struct.pack_into('f', b, index * 4)

Paul's avatar
Paul committed
45
46

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

Paul's avatar
Paul committed
49

Paul's avatar
Paul committed
50
def create_buffer(t, data, shape):
Paul's avatar
Paul committed
51
52
53
    a = array.array(t, data)
    if sys.version_info >= (3, 0):
        m = memoryview(a.tobytes())
Paul's avatar
Paul committed
54
        return m.cast(t, shape)
Paul's avatar
Paul committed
55
56
57
58
    else:
        m = memoryview(a.tostring())
        return m

kahmed10's avatar
kahmed10 committed
59

Paul's avatar
Paul committed
60
def check_argument(a):
Paul's avatar
Paul committed
61
62
    l = a.tolist()
    for i in range(len(l)):
Paul's avatar
Paul committed
63
        assert_eq(l[i], read_float(a, i))
Paul's avatar
Paul committed
64

kahmed10's avatar
kahmed10 committed
65

Paul's avatar
Paul committed
66
67
def check_shapes(r, m):
    lens = list(m.shape)
kahmed10's avatar
kahmed10 committed
68
    strides = [int(s / m.itemsize) for s in m.strides]
Paul's avatar
Paul committed
69
70
71
72
73
    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
74

Paul's avatar
Paul committed
75
76
77
def run(p):
    params = {}
    for key, value in p.get_parameter_shapes().items():
78
        params[key] = migraphx.generate_argument(value)
Paul's avatar
Paul committed
79

80
    return p.run(params)
Paul's avatar
Paul committed
81

kahmed10's avatar
kahmed10 committed
82

Paul's avatar
Paul committed
83
84
85
86
87
88
89
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
90

Paul's avatar
Paul committed
91
92
def test_input():
    if sys.version_info >= (3, 0):
Paul's avatar
Paul committed
93
        test_shape([4])
Paul's avatar
Paul committed
94
        test_shape([2, 3])
Paul's avatar
Paul committed
95
    else:
Paul's avatar
Paul committed
96
97
        data = list(range(4))
        m = create_buffer('f', data, [4])
Paul's avatar
Paul committed
98
99
100
101
102
103
104
        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
105
    p = migraphx.parse_onnx("conv_relu_maxpool_test.onnx")
Paul's avatar
Paul committed
106
    p.compile(migraphx.get_target("gpu"))
107

108
109
    r1 = run(p)[-1]
    r2 = run(p)[-1]
Paul's avatar
Paul committed
110
111
    assert_eq(r1, r2)
    assert_eq(r1.tolist(), r2.tolist())
112

Paul's avatar
Paul committed
113
114
    check_argument(r1)
    check_argument(r2)
Paul's avatar
Paul committed
115

Paul's avatar
Paul committed
116
117
    m1 = memoryview(r1)
    m2 = memoryview(r2)
Paul's avatar
Paul committed
118

Paul's avatar
Paul committed
119
120
    check_shapes(r1, m1)
    check_shapes(r2, m2)
Paul's avatar
Paul committed
121

122

Paul's avatar
Paul committed
123
124
test_input()
test_output()