test_gpu.py 5.51 KB
Newer Older
1
2
3
#####################################################################################
# The MIT License (MIT)
#
4
# Copyright (c) 2015-2023 Advanced Micro Devices, Inc. All rights reserved.
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#
# 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
import migraphx
Shucai Xiao's avatar
Shucai Xiao committed
25
26
27
28
29
30


def test_conv_relu():
    p = migraphx.parse_onnx("conv_relu_maxpool_test.onnx")
    print(p)
    print("Compiling ...")
31
32
    # set offload_copy, fast_match to true
    p.compile(migraphx.get_target("gpu"), True, True)
Shucai Xiao's avatar
Shucai Xiao committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
    print(p)
    params = {}

    for key, value in p.get_parameter_shapes().items():
        print("Parameter {} -> {}".format(key, value))
        params[key] = migraphx.generate_argument(value)

    r = p.run(params)
    print(r)


def test_sub_uint64():
    p = migraphx.parse_onnx("implicit_sub_bcast_test.onnx")
    print(p)
    print("Compiling ...")
    p.compile(migraphx.get_target("gpu"))
    print(p)
    params = {}

    shapes = p.get_parameter_shapes()
53
54
55
56
57
58
    params["0"] = migraphx.create_argument(
        migraphx.shape(type='uint64_type', lens=shapes["0"].lens()),
        list(range(120)))
    params["1"] = migraphx.create_argument(
        migraphx.shape(type='uint64_type', lens=shapes["1"].lens()),
        list(range(20)))
Shucai Xiao's avatar
Shucai Xiao committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72

    r = p.run(params)
    print(r)


def test_neg_int64():
    p = migraphx.parse_onnx("neg_test.onnx")
    print(p)
    print("Compiling ...")
    p.compile(migraphx.get_target("gpu"))
    print(p)
    params = {}

    shapes = p.get_parameter_shapes()
73
74
75
    params["0"] = migraphx.create_argument(
        migraphx.shape(type='int64_type', lens=shapes["0"].lens()),
        list(range(6)))
Shucai Xiao's avatar
Shucai Xiao committed
76
77
78
79
80

    r = p.run(params)
    print(r)


Shucai Xiao's avatar
Shucai Xiao committed
81
82
83
84
85
86
87
88
89
def test_nonzero():
    p = migraphx.parse_onnx("nonzero_dynamic_test.onnx")
    print(p)
    print("Compiling ...")
    p.compile(migraphx.get_target("gpu"))
    print(p)
    params = {}

    shapes = p.get_parameter_shapes()
90
91
92
    params["data"] = migraphx.create_argument(
        migraphx.shape(type='bool_type', lens=shapes["data"].lens()),
        [1, 1, 0, 1])
Shucai Xiao's avatar
Shucai Xiao committed
93
94
95
96
97

    r = p.run(params)
    print(r)


Shucai Xiao's avatar
Shucai Xiao committed
98
99
100
101
102
103
104
105
106
107
108
109
def test_fp16_imagescaler():
    p = migraphx.parse_onnx("imagescaler_half_test.onnx")
    print(p)
    s1 = p.get_output_shapes()[-1]
    print("Compiling ...")
    p.compile(migraphx.get_target("gpu"))
    print(p)
    s2 = p.get_output_shapes()[-1]
    assert s1 == s2

    params = {}
    shapes = p.get_parameter_shapes()
110
111
    params["0"] = migraphx.generate_argument(
        migraphx.shape(type='half_type', lens=shapes["0"].lens()), 768)
Paul's avatar
Paul committed
112

Shucai Xiao's avatar
Shucai Xiao committed
113
114
    r = p.run(params)[-1]
    print(r)
kahmed10's avatar
kahmed10 committed
115

Paul's avatar
Paul committed
116

Shucai Xiao's avatar
Shucai Xiao committed
117
118
119
120
121
122
123
124
125
126
127
128
def test_if_pl():
    p = migraphx.parse_onnx("if_pl_test.onnx")
    print(p)
    s1 = p.get_output_shapes()[-1]
    print("Compiling ...")
    p.compile(migraphx.get_target("gpu"))
    print(p)
    s2 = p.get_output_shapes()[-1]
    assert s1 == s2

    params = {}
    shapes = p.get_parameter_shapes()
129
130
131
132
133
134
    params["x"] = migraphx.fill_argument(
        migraphx.shape(type='float_type', lens=shapes["x"].lens()), 1)
    params["y"] = migraphx.fill_argument(
        migraphx.shape(type='float_type', lens=shapes["y"].lens()), 2.0)
    params["cond"] = migraphx.fill_argument(
        migraphx.shape(type="bool", lens=[1], strides=[0]), 1)
Shucai Xiao's avatar
Shucai Xiao committed
135
136
137
138
139

    r = p.run(params)[-1]
    print(r)


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
def test_dyn_batch():
    a = migraphx.shape.dynamic_dimension(1, 4, {2, 4})
    b = migraphx.shape.dynamic_dimension(3, 3)
    c = migraphx.shape.dynamic_dimension(32, 32)
    dd_map = {"0": [a, b, c, c]}
    p = migraphx.parse_onnx("conv_relu_maxpool_test.onnx",
                            map_dyn_input_dims=dd_map)
    print(p)
    print("Compiling ...")
    p.compile(migraphx.get_target("gpu"))
    print(p)

    def run_prog(batch_size):
        params = {}
        for key, value in p.get_parameter_shapes().items():
            # convert to a static shape
            if value.dynamic():
                dds = value.dyn_dims()
                new_lens = []
                for dd in dds:
                    if dd.is_fixed():
                        new_lens.append(dd.min)
                    else:
                        new_lens.append(batch_size)
                s = migraphx.shape(type=value.type_string(), lens=new_lens)
            else:
                s = value
            print("Parameter {} -> {}".format(key, s))
            params[key] = migraphx.generate_argument(s)
        r = p.run(params)
        print(r)

    run_prog(1)
    run_prog(2)
    run_prog(3)
    run_prog(4)


Shucai Xiao's avatar
Shucai Xiao committed
178
179
180
181
test_conv_relu()
test_sub_uint64()
test_neg_int64()
test_fp16_imagescaler()
Shucai Xiao's avatar
Shucai Xiao committed
182
test_if_pl()
Shucai Xiao's avatar
Shucai Xiao committed
183
test_nonzero()
184
test_dyn_batch()