"ts/webui/src/static/function.ts" did not exist on "43de011869b8f6a1778a1e1d7f56c2340cab39d6"
eval_test.cpp 8.91 KB
Newer Older
Paul's avatar
Paul committed
1

Paul's avatar
Paul committed
2
3
4
#include <migraphx/program.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/instruction.hpp>
Paul's avatar
Paul committed
5
#include <sstream>
Paul's avatar
Paul committed
6
#include "test.hpp"
Paul's avatar
Paul committed
7
#include <basic_ops.hpp>
Paul's avatar
Paul committed
8

Paul's avatar
Paul committed
9
10
struct id_target
{
Paul's avatar
Paul committed
11
12
13
14
15
    struct context
    {
        void finish() const {}
    };
    migraphx::context ctx = context{};
Paul's avatar
Paul committed
16
    std::string name() const { return "id"; }
Paul's avatar
Paul committed
17
    std::vector<migraphx::pass> get_passes(migraphx::context&) const { return {}; }
Paul's avatar
Paul committed
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
    migraphx::context get_context() const { return ctx; }
};

struct id_ctx_op
{
    std::string name() const { return "id_ctx_op"; }
    migraphx::argument
    compute(id_target::context&, const migraphx::shape&, std::vector<migraphx::argument> args) const
    {
        if(args.empty())
            return {};
        return args.front();
    }

    migraphx::shape compute_shape(std::vector<migraphx::shape> inputs) const
    {
        if(inputs.empty())
            return {};
        return inputs.front();
    }
    int output_alias(const std::vector<migraphx::shape>&) const { return 0; }
};

struct id_ctx_final_op
{
    std::string name() const { return "id_ctx_final_op"; }
Paul's avatar
Paul committed
44
    migraphx::argument compute(const migraphx::shape&, std::vector<migraphx::argument> args) const
Paul's avatar
Paul committed
45
46
47
48
49
50
    {
        if(args.empty())
            return {};
        return args.front();
    }

Paul's avatar
Paul committed
51
52
53
    void finalize(id_target::context&, const migraphx::shape&, const std::vector<migraphx::shape>&)
    {
    }
Paul's avatar
Paul committed
54
55
56
57
58
59
60
61

    migraphx::shape compute_shape(std::vector<migraphx::shape> inputs) const
    {
        if(inputs.empty())
            return {};
        return inputs.front();
    }
    int output_alias(const std::vector<migraphx::shape>&) const { return 0; }
Paul's avatar
Paul committed
62
63
};

Paul's avatar
Paul committed
64
65
struct reverse_pass
{
Paul's avatar
Paul committed
66
    std::string name() const { return "reverse_pass"; }
Paul's avatar
Paul committed
67

Paul's avatar
Paul committed
68
    void apply(migraphx::program& p) const
Paul's avatar
Paul committed
69
    {
Paul's avatar
Paul committed
70
        for(auto ins : migraphx::iterator_for(p))
Paul's avatar
Paul committed
71
        {
72
            if(ins->name() == "sum")
Paul's avatar
Paul committed
73
            {
74
                p.replace_instruction(ins, minus_op{}, ins->inputs());
Paul's avatar
Paul committed
75
            }
76
            else if(ins->name() == "minus")
Paul's avatar
Paul committed
77
            {
78
                p.replace_instruction(ins, sum_op{}, ins->inputs());
Paul's avatar
Paul committed
79
80
81
82
83
84
85
86
            }
        }
    }
};

struct reverse_target
{
    std::string name() const { return "reverse"; }
Paul's avatar
Paul committed
87
88
    std::vector<migraphx::pass> get_passes(migraphx::context&) const { return {reverse_pass{}}; }
    migraphx::context get_context() const { return {}; }
Paul's avatar
Paul committed
89
90
91
92
93
};

struct double_reverse_target
{
    std::string name() const { return "double_reverse"; }
Paul's avatar
Paul committed
94
    std::vector<migraphx::pass> get_passes(migraphx::context&) const
Paul's avatar
Paul committed
95
96
    {
        return {reverse_pass{}, reverse_pass{}};
Paul's avatar
Paul committed
97
    }
Paul's avatar
Paul committed
98
    migraphx::context get_context() const { return {}; }
Paul's avatar
Paul committed
99
100
};

Paul's avatar
Paul committed
101
TEST_CASE(literal_test1)
Paul's avatar
Paul committed
102
{
Paul's avatar
Paul committed
103
    migraphx::program p;
Paul's avatar
Paul committed
104
105
106

    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
Paul's avatar
Paul committed
107
    p.add_instruction(sum_op{}, one, two);
Paul's avatar
Paul committed
108
    auto result = p.eval({});
Paul's avatar
Paul committed
109
110
    EXPECT(result == migraphx::literal{3});
    EXPECT(result != migraphx::literal{4});
Paul's avatar
Paul committed
111
112
}

Paul's avatar
Paul committed
113
TEST_CASE(literal_test2)
Paul's avatar
Paul committed
114
{
Paul's avatar
Paul committed
115
    migraphx::program p;
Paul's avatar
Paul committed
116

Paul's avatar
Paul committed
117
118
    auto one  = p.add_literal(1);
    auto two  = p.add_literal(2);
Paul's avatar
Paul committed
119
120
121
122
    auto sum1 = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(sum_op{}, sum1, two);

    auto result = p.eval({});
Paul's avatar
Paul committed
123
124
    EXPECT(result == migraphx::literal{5});
    EXPECT(result != migraphx::literal{3});
Paul's avatar
Paul committed
125
126
}

Paul's avatar
Paul committed
127
TEST_CASE(print_test)
Paul's avatar
Paul committed
128
{
Paul's avatar
Paul committed
129
    migraphx::program p;
Paul's avatar
Paul committed
130

Paul's avatar
Paul committed
131
    auto x   = p.add_parameter("x", {migraphx::shape::int32_type});
Paul's avatar
Paul committed
132
133
134
135
136
137
138
139
140
    auto two = p.add_literal(2);
    p.add_instruction(sum_op{}, x, two);

    std::stringstream ss;
    ss << p;
    std::string s = ss.str();
    EXPECT(!s.empty());
}

Paul's avatar
Paul committed
141
TEST_CASE(param_test)
Paul's avatar
Paul committed
142
{
Paul's avatar
Paul committed
143
    migraphx::program p;
Paul's avatar
Paul committed
144

Paul's avatar
Paul committed
145
146
    auto x = p.add_parameter("x", {migraphx::shape::int32_type});
    auto y = p.add_parameter("y", {migraphx::shape::int32_type});
Paul's avatar
Paul committed
147

Paul's avatar
Paul committed
148
    p.add_instruction(sum_op{}, x, y);
Paul's avatar
Paul committed
149
    auto result = p.eval(
Paul's avatar
Paul committed
150
151
152
        {{"x", migraphx::literal{1}.get_argument()}, {"y", migraphx::literal{2}.get_argument()}});
    EXPECT(result == migraphx::literal{3});
    EXPECT(result != migraphx::literal{4});
Paul's avatar
Paul committed
153
154
}

Paul's avatar
Paul committed
155
TEST_CASE(param_error_test)
Khalique's avatar
Khalique committed
156
{
Paul's avatar
Paul committed
157
    migraphx::program p;
Khalique's avatar
Khalique committed
158

Paul's avatar
Paul committed
159
160
    auto x = p.add_parameter("x", {migraphx::shape::int32_type});
    auto y = p.add_parameter("y", {migraphx::shape::int32_type});
Khalique's avatar
Khalique committed
161
162

    p.add_instruction(sum_op{}, x, y);
Paul's avatar
Paul committed
163
    EXPECT(test::throws<migraphx::exception>(
Khalique's avatar
Khalique committed
164
        [&] {
Paul's avatar
Paul committed
165
            p.eval({{"x", migraphx::literal{1}.get_argument()}});
Khalique's avatar
Khalique committed
166
        },
167
        "Parameter not found: y"));
Khalique's avatar
Khalique committed
168
169
}

Paul's avatar
Paul committed
170
171
172
173
174
175
176
177
178
179
TEST_CASE(param_shape_error_test)
{
    migraphx::program p;

    auto x = p.add_parameter("x", {migraphx::shape::int32_type, {1, 2}});
    auto y = p.add_parameter("y", {migraphx::shape::int32_type, {1, 2}});

    p.add_instruction(sum_op{}, x, y);
    EXPECT(test::throws<migraphx::exception>(
        [&] {
Paul's avatar
Paul committed
180
181
            p.eval({{"x", migraphx::literal{1}.get_argument()},
                    {"y", migraphx::literal{2}.get_argument()}});
Paul's avatar
Paul committed
182
183
184
185
        },
        "Incorrect shape"));
}

Paul's avatar
Paul committed
186
TEST_CASE(replace_test)
Paul's avatar
Paul committed
187
{
Paul's avatar
Paul committed
188
    migraphx::program p;
Paul's avatar
Paul committed
189
190
191
192
193

    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    auto sum = p.add_instruction(sum_op{}, one, two);
    p.replace_instruction(sum, minus_op{}, two, one);
Paul's avatar
Paul committed
194
    EXPECT(bool{p.validate() == p.end()});
Paul's avatar
Paul committed
195
196

    auto result = p.eval({});
Paul's avatar
Paul committed
197
198
    EXPECT(result == migraphx::literal{1});
    EXPECT(result != migraphx::literal{3});
Paul's avatar
Paul committed
199
200
}

Paul's avatar
Paul committed
201
TEST_CASE(replace_ins_test)
Paul's avatar
Paul committed
202
{
Paul's avatar
Paul committed
203
    migraphx::program p;
Paul's avatar
Paul committed
204

Paul's avatar
Paul committed
205
206
207
    auto one   = p.add_literal(1);
    auto two   = p.add_literal(2);
    auto sum   = p.add_instruction(sum_op{}, one, two);
Paul's avatar
Paul committed
208
209
    auto minus = p.add_instruction(minus_op{}, two, one);
    p.replace_instruction(sum, minus);
Paul's avatar
Paul committed
210
    EXPECT(bool{p.validate() == p.end()});
Paul's avatar
Paul committed
211
212

    auto result = p.eval({});
Paul's avatar
Paul committed
213
214
    EXPECT(result == migraphx::literal{1});
    EXPECT(result != migraphx::literal{3});
Paul's avatar
Paul committed
215
216
}

Paul's avatar
Paul committed
217
TEST_CASE(replace_ins_test2)
Paul's avatar
Paul committed
218
{
Paul's avatar
Paul committed
219
    migraphx::program p;
Paul's avatar
Paul committed
220

Paul's avatar
Paul committed
221
222
223
    auto one   = p.add_literal(1);
    auto two   = p.add_literal(2);
    auto sum   = p.add_instruction(sum_op{}, one, two);
Paul's avatar
Paul committed
224
225
    auto minus = p.add_instruction(minus_op{}, two, one);
    p.add_instruction(pass_op{}, minus);
Paul's avatar
Paul committed
226
    p.replace_instruction(two, sum);
Paul's avatar
Paul committed
227
    EXPECT(bool{p.validate() == p.end()});
Paul's avatar
Paul committed
228
229

    auto result = p.eval({});
Paul's avatar
Paul committed
230
231
    EXPECT(result == migraphx::literal{2});
    EXPECT(result != migraphx::literal{3});
Paul's avatar
Paul committed
232
233
}

Paul's avatar
Paul committed
234
TEST_CASE(insert_replace_test)
Paul's avatar
Paul committed
235
{
Paul's avatar
Paul committed
236
    migraphx::program p;
Paul's avatar
Paul committed
237

Paul's avatar
Paul committed
238
239
    auto one  = p.add_literal(1);
    auto two  = p.add_literal(2);
Paul's avatar
Paul committed
240
241
242
243
244
    auto sum1 = p.add_instruction(sum_op{}, one, two);
    p.add_instruction(sum_op{}, sum1, two);

    auto sum0 = p.insert_instruction(sum1, sum_op{}, two, two);
    p.replace_instruction(sum1, minus_op{}, sum0, two);
Paul's avatar
Paul committed
245
    EXPECT(bool{p.validate() == p.end()});
Paul's avatar
Paul committed
246
247

    auto result = p.eval({});
Paul's avatar
Paul committed
248
249
    EXPECT(result == migraphx::literal{4});
    EXPECT(result != migraphx::literal{5});
Paul's avatar
Paul committed
250
251
}

Paul's avatar
Paul committed
252
TEST_CASE(target_test)
Paul's avatar
Paul committed
253
{
Paul's avatar
Paul committed
254
    migraphx::program p;
Paul's avatar
Paul committed
255
256
257
258
259
260

    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    p.add_instruction(sum_op{}, one, two);
    p.compile(id_target{});
    auto result = p.eval({});
Paul's avatar
Paul committed
261
262
    EXPECT(result == migraphx::literal{3});
    EXPECT(result != migraphx::literal{4});
Paul's avatar
Paul committed
263
264
}

Paul's avatar
Paul committed
265
TEST_CASE(reverse_target_test)
Paul's avatar
Paul committed
266
{
Paul's avatar
Paul committed
267
    migraphx::program p;
Paul's avatar
Paul committed
268
269
270
271
272
273

    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    p.add_instruction(sum_op{}, two, one);
    p.compile(reverse_target{});
    auto result = p.eval({});
Paul's avatar
Paul committed
274
275
    EXPECT(result == migraphx::literal{1});
    EXPECT(result != migraphx::literal{4});
Paul's avatar
Paul committed
276
277
}

Paul's avatar
Paul committed
278
TEST_CASE(double_reverse_target_test)
Paul's avatar
Paul committed
279
{
Paul's avatar
Paul committed
280
    migraphx::program p;
Paul's avatar
Paul committed
281
282
283
284
285
286

    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    p.add_instruction(sum_op{}, two, one);
    p.compile(double_reverse_target{});
    auto result = p.eval({});
Paul's avatar
Paul committed
287
288
    EXPECT(result == migraphx::literal{3});
    EXPECT(result != migraphx::literal{4});
Paul's avatar
Paul committed
289
290
}

Paul's avatar
Paul committed
291
292
// Check that the program doesnt modify the context directly, and only the operators modify the
// context
Paul's avatar
Paul committed
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
TEST_CASE(eval_context1)
{
    migraphx::program p;
    id_target t{};
    EXPECT(is_shared(t.ctx, t.get_context()));
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    p.add_instruction(sum_op{}, one, two);
    p.compile(t);
    EXPECT(is_shared(t.ctx, p.get_context()));
    p.eval({});
    EXPECT(is_shared(t.ctx, p.get_context()));
}

TEST_CASE(eval_context2)
{
    migraphx::program p;
    id_target t{};
    EXPECT(is_shared(t.ctx, t.get_context()));
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    p.add_instruction(id_ctx_op{}, one, two);
    p.compile(t);
    EXPECT(is_shared(t.ctx, p.get_context()));
    p.eval({});
    // id_ctx_op will modify the context
    EXPECT(not is_shared(t.ctx, p.get_context()));
}

TEST_CASE(eval_context3)
{
    migraphx::program p;
    id_target t{};
    EXPECT(is_shared(t.ctx, t.get_context()));
    auto one = p.add_literal(1);
    auto two = p.add_literal(2);
    p.add_instruction(id_ctx_final_op{}, one, two);
    p.compile(t);
    // Finalizer will modify the context
    EXPECT(not is_shared(t.ctx, p.get_context()));
    auto ctx = p.get_context();
    p.eval({});
    EXPECT(is_shared(ctx, p.get_context()));
    EXPECT(not is_shared(t.ctx, p.get_context()));
}

Paul's avatar
Paul committed
339
int main(int argc, const char* argv[]) { test::run(argc, argv); }