operators.cpp 3.46 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>
6
7
#include <sstream>
#include <string>
8
9
10
11
#include <migraphx/make_op.hpp>

#include <migraphx/serialize.hpp>

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

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

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

32
33
34
35
36
37
38
39
40
41
42
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);
    }
}

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

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

59
60
61
62
63
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}});
64
65
66
    migraphx::operation y = migraphx::make_op(
        "rnn_var_sl_shift_output",
        {{"output_name", "hidden_states"}, {"direction", migraphx::to_value(dirct)}});
67
68
69
    EXPECT(x == y);
}

70
71
72
73
74
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
75
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
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);
}

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

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