miopen.cpp 5.56 KB
Newer Older
Paul's avatar
Paul committed
1

Paul's avatar
Paul committed
2
3
4
5
6
7
8
9
#include <migraph/program.hpp>
#include <migraph/operators.hpp>
#include <migraph/generate.hpp>
#include <migraph/cpu/cpu_target.hpp>
#include <migraph/miopen/miopen_target.hpp>
#include <migraph/miopen/miopen.hpp>
#include <migraph/miopen/hip.hpp>
#include <migraph/manage_ptr.hpp>
Paul's avatar
Paul committed
10
11
12
13

#include <miopen/miopen.h>

#include "test.hpp"
Paul's avatar
Paul committed
14
#include "verify.hpp"
Paul's avatar
Paul committed
15

Paul's avatar
Paul committed
16
template <class V>
Paul's avatar
Paul committed
17
migraph::argument run_cpu()
Paul's avatar
Paul committed
18
{
Paul's avatar
Paul committed
19
20
    V v;
    auto p = v.create_program();
Paul's avatar
Paul committed
21
    p.compile(migraph::cpu::cpu_target{});
Paul's avatar
Paul committed
22
    return p.eval(v.create_params());
Paul's avatar
Paul committed
23
24
}

Paul's avatar
Paul committed
25
template <class V>
Paul's avatar
Paul committed
26
migraph::argument run_gpu()
Paul's avatar
Paul committed
27
{
Paul's avatar
Paul committed
28
29
    V v;
    auto p = v.create_program();
Paul's avatar
Paul committed
30
    p.compile(migraph::miopen::miopen_target{});
Paul's avatar
Paul committed
31
32

    auto m = v.create_params();
Paul's avatar
Paul committed
33
    for(auto&& e : m)
Paul's avatar
Paul committed
34
    {
Paul's avatar
Paul committed
35
        e.second = migraph::miopen::to_gpu(e.second);
Paul's avatar
Paul committed
36
37
    }

Paul's avatar
Paul committed
38
39
    m["output"] =
        migraph::miopen::to_gpu(migraph::generate_argument(p.get_parameter_shape("output")));
Paul's avatar
Paul committed
40

Paul's avatar
Paul committed
41
    return migraph::miopen::from_gpu(p.eval(m));
Paul's avatar
Paul committed
42
43
}

Paul's avatar
Paul committed
44
template <class V>
Paul's avatar
Paul committed
45
void verify_program()
Paul's avatar
Paul committed
46
{
Paul's avatar
Paul committed
47
48
    auto cpu_arg = run_cpu<V>();
    auto gpu_arg = run_gpu<V>();
Paul's avatar
Paul committed
49
    visit_all(cpu_arg, gpu_arg)([](auto cpu, auto gpu) { EXPECT(test::verify_range(cpu, gpu)); });
Paul's avatar
Paul committed
50
51
}

Paul's avatar
Paul committed
52
53
54
55
56
struct test_literals
{
    migraph::program create_program() const
    {
        migraph::program p;
Paul's avatar
Paul committed
57
58
59
60
        auto input = p.add_literal(
            generate_literal(migraph::shape{migraph::shape::float_type, {4, 3, 3, 3}}));
        auto weights = p.add_literal(
            generate_literal(migraph::shape{migraph::shape::float_type, {4, 3, 3, 3}}));
Paul's avatar
Paul committed
61
62
63
64
65
        auto conv = p.add_instruction(migraph::convolution{}, input, weights);
        p.add_instruction(migraph::activation{"relu"}, conv);
        return p;
    }

Paul's avatar
Paul committed
66
    migraph::program::parameter_map create_params() const { return {}; }
Paul's avatar
Paul committed
67
68
};

Paul's avatar
Paul committed
69
70
struct test_add
{
Paul's avatar
Paul committed
71
    migraph::program create_program() const
Paul's avatar
Paul committed
72
    {
Paul's avatar
Paul committed
73
74
        migraph::program p;
        migraph::shape s{migraph::shape::float_type, {3}};
Paul's avatar
Paul committed
75
76
        auto x = p.add_parameter("x", s);
        auto y = p.add_parameter("y", s);
Paul's avatar
Paul committed
77
        p.add_instruction(migraph::add{}, x, y);
Paul's avatar
Paul committed
78
79
80
        return p;
    }

Paul's avatar
Paul committed
81
    migraph::program::parameter_map create_params() const
Paul's avatar
Paul committed
82
    {
Paul's avatar
Paul committed
83
84
85
        migraph::program::parameter_map m;
        m["x"] = migraph::generate_argument({migraph::shape::float_type, {3}});
        m["y"] = migraph::generate_argument({migraph::shape::float_type, {3}});
Paul's avatar
Paul committed
86
87
88
89
90
91
        return m;
    }
};

struct test_add_broadcast
{
Paul's avatar
Paul committed
92
    migraph::program create_program() const
Paul's avatar
Paul committed
93
    {
Paul's avatar
Paul committed
94
95
96
97
98
99
        migraph::program p;
        migraph::shape s{migraph::shape::float_type, {3}};
        auto x  = p.add_parameter("x", {migraph::shape::float_type, {2, 2, 3}});
        auto y  = p.add_parameter("y", {migraph::shape::float_type, {2, 2}});
        auto by = p.add_instruction(migraph::broadcast{0}, x, y);
        p.add_instruction(migraph::add{}, x, by);
Paul's avatar
Paul committed
100
101
102
        return p;
    }

Paul's avatar
Paul committed
103
    migraph::program::parameter_map create_params() const
Paul's avatar
Paul committed
104
    {
Paul's avatar
Paul committed
105
106
107
        migraph::program::parameter_map m;
        m["x"] = migraph::generate_argument({migraph::shape::float_type, {2, 2, 3}});
        m["y"] = migraph::generate_argument({migraph::shape::float_type, {2, 2}});
Paul's avatar
Paul committed
108
109
110
111
        return m;
    }
};

Paul's avatar
Paul committed
112
struct test_conv_relu
Paul's avatar
Paul committed
113
{
Paul's avatar
Paul committed
114
    migraph::program create_program() const
Paul's avatar
Paul committed
115
    {
Paul's avatar
Paul committed
116
        migraph::program p;
Paul's avatar
Paul committed
117
118
119
120
        auto input = p.add_parameter("x", migraph::shape{migraph::shape::float_type, {4, 3, 3, 3}});
        auto weights =
            p.add_parameter("w", migraph::shape{migraph::shape::float_type, {4, 3, 3, 3}});
        auto conv = p.add_instruction(migraph::convolution{}, input, weights);
Paul's avatar
Paul committed
121
        p.add_instruction(migraph::activation{"relu"}, conv);
Paul's avatar
Paul committed
122
123
124
        return p;
    }

Paul's avatar
Paul committed
125
    migraph::program::parameter_map create_params() const
Paul's avatar
Paul committed
126
    {
Paul's avatar
Paul committed
127
128
129
        migraph::program::parameter_map m;
        m["x"] = migraph::generate_argument({migraph::shape::float_type, {4, 3, 3, 3}});
        m["w"] = migraph::generate_argument({migraph::shape::float_type, {4, 3, 3, 3}});
Paul's avatar
Paul committed
130
131
132
133
        return m;
    }
};

Paul's avatar
Paul committed
134
135
struct test_conv_pooling
{
Paul's avatar
Paul committed
136
    migraph::program create_program() const
Paul's avatar
Paul committed
137
    {
Paul's avatar
Paul committed
138
        migraph::program p;
Paul's avatar
Paul committed
139
140
141
142
        auto input =
            p.add_parameter("x", migraph::shape{migraph::shape::float_type, {4, 3, 32, 32}});
        auto weights =
            p.add_parameter("w", migraph::shape{migraph::shape::float_type, {4, 3, 3, 3}});
Paul's avatar
Paul committed
143
144
145
        auto conv    = p.add_instruction(migraph::convolution{}, input, weights);
        auto pooling = p.add_instruction(migraph::pooling{"max"}, conv);
        p.add_instruction(migraph::activation{"relu"}, pooling);
Paul's avatar
Paul committed
146
147
148
        return p;
    }

Paul's avatar
Paul committed
149
    migraph::program::parameter_map create_params() const
Paul's avatar
Paul committed
150
    {
Paul's avatar
Paul committed
151
152
153
        migraph::program::parameter_map m;
        m["x"] = migraph::generate_argument({migraph::shape::float_type, {4, 3, 32, 32}});
        m["w"] = migraph::generate_argument({migraph::shape::float_type, {4, 3, 3, 3}});
Paul's avatar
Paul committed
154
155
156
157
        return m;
    }
};

Paul's avatar
Paul committed
158
159
struct test_gemm
{
Paul's avatar
Paul committed
160
    migraph::program create_program() const
Paul's avatar
Paul committed
161
    {
Paul's avatar
Paul committed
162
163
164
165
        migraph::program p;
        auto a = p.add_parameter("a", migraph::shape{migraph::shape::float_type, {4, 5}});
        auto b = p.add_parameter("b", migraph::shape{migraph::shape::float_type, {5, 3}});
        p.add_instruction(migraph::gemm{}, a, b);
Paul's avatar
Paul committed
166
167
168
        return p;
    }

Paul's avatar
Paul committed
169
    migraph::program::parameter_map create_params() const
Paul's avatar
Paul committed
170
    {
Paul's avatar
Paul committed
171
172
173
        migraph::program::parameter_map m;
        m["a"] = migraph::generate_argument({migraph::shape::float_type, {4, 5}});
        m["b"] = migraph::generate_argument({migraph::shape::float_type, {5, 3}});
Paul's avatar
Paul committed
174
175
176
177
        return m;
    }
};

Paul's avatar
Paul committed
178
179
int main()
{
Paul's avatar
Paul committed
180
    verify_program<test_add>();
Paul's avatar
Paul committed
181
    verify_program<test_add_broadcast>();
Paul's avatar
Paul committed
182
183
184
    verify_program<test_conv_relu>();
    verify_program<test_conv_pooling>();
    verify_program<test_gemm>();
Paul's avatar
Paul committed
185
}