operators.cpp 4.44 KB
Newer Older
1
2
#include <migraphx/register_op.hpp>
#include <migraphx/operation.hpp>
3
4
#include <migraphx/make_op.hpp>
#include <migraphx/op/convolution.hpp>
5
#include <migraphx/op/rnn_variable_seq_lens.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
6
#include <migraphx/module.hpp>
7
8
#include <sstream>
#include <string>
9
10
11
12
#include <migraphx/make_op.hpp>

#include <migraphx/serialize.hpp>

13
14
15
16
17
18
19
20
21
22
23
#include "test.hpp"

TEST_CASE(load_op)
{
    for(const auto& name : migraphx::get_operators())
    {
        auto op = migraphx::load_op(name);
        CHECK(op.name() == name);
    }
}

24
25
26
27
28
29
30
31
32
TEST_CASE(make_op)
{
    for(const auto& name : migraphx::get_operators())
    {
        auto op = migraphx::load_op(name);
        CHECK(op == migraphx::make_op(name));
    }
}

33
34
35
36
37
38
39
40
41
42
43
TEST_CASE(save_op)
{
    for(const auto& name : migraphx::get_operators())
    {
        auto op1 = migraphx::load_op(name);
        auto v   = migraphx::to_value(op1);
        auto op2 = migraphx::from_value<migraphx::operation>(v);
        CHECK(op1 == op2);
    }
}

44
45
46
47
TEST_CASE(make_op_from_value1)
{
    migraphx::operation x = migraphx::make_op(
        "convolution", {{"padding", {1, 1}}, {"stride", {2, 2}}, {"dilation", {2, 2}}});
48
49
    migraphx::operation y = migraphx::make_op(
        "convolution", {{"padding", {1, 1}}, {"stride", {2, 2}}, {"dilation", {2, 2}}});
50
51
52
53
54
55
    EXPECT(x == y);
}

TEST_CASE(make_op_from_value2)
{
    migraphx::operation x = migraphx::make_op("convolution", {{"padding", {1, 1}}});
56
    migraphx::operation y = migraphx::make_op("convolution", {{"padding", {1, 1}}});
57
58
59
    EXPECT(x == y);
}

60
61
62
63
64
TEST_CASE(make_rnn_op_from_value)
{
    migraphx::op::rnn_direction dirct = migraphx::op::rnn_direction::reverse;
    migraphx::operation x             = migraphx::make_op(
        "rnn_var_sl_shift_output", {{"output_name", "hidden_states"}, {"direction", dirct}});
65
66
67
    migraphx::operation y = migraphx::make_op(
        "rnn_var_sl_shift_output",
        {{"output_name", "hidden_states"}, {"direction", migraphx::to_value(dirct)}});
68
69
70
    EXPECT(x == y);
}

71
72
73
74
75
TEST_CASE(make_op_invalid_key)
{
    EXPECT(test::throws([] { migraphx::make_op("convolution", {{"paddings", {1, 1}}}); }));
}

Paul Fultz II's avatar
Paul Fultz II committed
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
TEST_CASE(load_offset)
{
    migraphx::shape s{migraphx::shape::float_type, {4}};
    migraphx::shape bs{migraphx::shape::int8_type, {32}};
    auto op = migraphx::make_op("load", {{"offset", 4}, {"shape", migraphx::to_value(s)}});
    EXPECT(op.compute_shape({bs}) == s);

    migraphx::argument a{bs};
    EXPECT(op.compute(bs, {a}).data() == a.data() + 4);
}

TEST_CASE(load_out_of_bounds)
{
    migraphx::shape s{migraphx::shape::float_type, {4}};
    migraphx::shape bs{migraphx::shape::int8_type, {16}};
    auto op = migraphx::make_op("load", {{"offset", 4}, {"shape", migraphx::to_value(s)}});

    migraphx::argument a{bs};
    EXPECT(test::throws([&] { op.compute(bs, {a}); }));
}

TEST_CASE(load_tuple)
{
    migraphx::shape s{{migraphx::shape{migraphx::shape::int8_type, {3}},
                       migraphx::shape{migraphx::shape::float_type, {4}}}};
    migraphx::shape bs{migraphx::shape::int8_type, {32}};
    auto op = migraphx::make_op("load", {{"offset", 4}, {"shape", migraphx::to_value(s)}});
    EXPECT(op.compute_shape({bs}) == s);

    migraphx::argument a{bs};
    auto r = op.compute(bs, {a});
    EXPECT(r.get_sub_objects().size() == 2);
    auto* start = a.data() + 4;
    EXPECT(r.get_sub_objects()[0].data() == start + 16);
    EXPECT(r.get_sub_objects()[1].data() == start);
}

113
114
115
116
117
118
TEST_CASE(ops)
{
    auto names = migraphx::get_operators();
    EXPECT(names.size() > 1);
}

Shucai Xiao's avatar
Shucai Xiao committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
TEST_CASE(rnn)
{
    migraphx::shape s{migraphx::shape::float_type, {2, 1}};
    std::vector<float> data1(2, 2.0f);
    std::vector<float> data2(2, 3.0f);
    migraphx::argument a1(s, data1.data());
    migraphx::argument a2(s, data2.data());

    auto op = migraphx::make_op("rnn");

    EXPECT(test::throws([&] { op.compute(s, {a1, a2}); }));
}

TEST_CASE(if_op)
{
    migraphx::shape s{migraphx::shape::bool_type, {1}};
    std::vector<char> data = {1};
    migraphx::argument cond(s, data.data());
    migraphx::shape sd{migraphx::shape::float_type, {2, 1}};
    std::vector<float> data1(2, 2.0f);
    std::vector<float> data2(2, 3.0f);
    migraphx::argument a1(sd, data1.data());
    migraphx::argument a2(sd, data2.data());

    migraphx::module m("name");
    auto l = m.add_literal(migraphx::literal(sd, data1));
    m.add_return({l});

    auto op = migraphx::make_op("add");
    EXPECT(test::throws([&] { op.compute(s, {cond, a1, a2}, {&m, &m}, {}); }));
}

151
int main(int argc, const char* argv[]) { test::run(argc, argv); }