decompose_test.cpp 6.91 KB
Newer Older
1
2
3
#include <migraphx/decompose.hpp>
#include <migraphx/pass_manager.hpp>
#include <basic_ops.hpp>
4
5
#include <migraphx/make_op.hpp>

6
7
#include <test.hpp>

8
9
10
11
void run_pass(migraphx::program& p)
{
    migraphx::run_passes(*p.get_main_module(), {migraphx::decompose{}});
}
12
13
14
15
16

TEST_CASE(dot_add)
{
    migraphx::program p1;
    {
17
18
19
20
        auto* mm1 = p1.get_main_module();
        auto x    = mm1->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {2, 2}});
        auto y    = mm1->add_parameter("y", migraphx::shape{migraphx::shape::float_type, {2, 2}});
        auto z    = mm1->add_parameter("z", migraphx::shape{migraphx::shape::float_type, {2, 2}});
21
22
        auto dot  = mm1->add_instruction(migraphx::make_op("dot"), x, y, z);
        mm1->add_instruction(migraphx::make_op("identity"), dot);
23
24
25
26
    }
    run_pass(p1);
    migraphx::program p2;
    {
27
28
29
30
        auto* mm2 = p2.get_main_module();
        auto x    = mm2->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {2, 2}});
        auto y    = mm2->add_parameter("y", migraphx::shape{migraphx::shape::float_type, {2, 2}});
        auto z    = mm2->add_parameter("z", migraphx::shape{migraphx::shape::float_type, {2, 2}});
31
32
33
34
        auto dot =
            mm2->add_instruction(migraphx::make_op("dot", {{"alpha", 1}, {"beta", 0}}), x, y);
        auto add = mm2->add_instruction(migraphx::make_op("add"), dot, z);
        mm2->add_instruction(migraphx::make_op("identity"), add);
35
36
37
38
39
40
41
42
    }
    EXPECT(p1 == p2);
}

TEST_CASE(dot_add_beta_float)
{
    migraphx::program p1;
    {
43
44
45
46
        auto* mm1 = p1.get_main_module();
        auto x    = mm1->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {2, 2}});
        auto y    = mm1->add_parameter("y", migraphx::shape{migraphx::shape::float_type, {2, 2}});
        auto z    = mm1->add_parameter("z", migraphx::shape{migraphx::shape::float_type, {2, 2}});
47
48
49
        auto dot  = mm1->add_instruction(
            migraphx::make_op("dot", {{"alpha", 1.0}, {"beta", 0.5}}), x, y, z);
        mm1->add_instruction(migraphx::make_op("identity"), dot);
50
51
52
53
    }
    run_pass(p1);
    migraphx::program p2;
    {
54
55
56
57
        auto* mm2 = p2.get_main_module();
        auto x    = mm2->add_parameter("x", migraphx::shape{migraphx::shape::float_type, {2, 2}});
        auto y    = mm2->add_parameter("y", migraphx::shape{migraphx::shape::float_type, {2, 2}});
        auto z    = mm2->add_parameter("z", migraphx::shape{migraphx::shape::float_type, {2, 2}});
58
59
        auto dot =
            mm2->add_instruction(migraphx::make_op("dot", {{"alpha", 1}, {"beta", 0}}), x, y);
60
61
        auto beta = mm2->add_literal(
            migraphx::literal{migraphx::shape{migraphx::shape::float_type}, {0.5}});
62
63
64
65
66
        auto beta_broadcast = mm2->add_instruction(
            migraphx::make_op("multibroadcast", {{"output_lens", {2, 2}}}), beta);
        auto mul = mm2->add_instruction(migraphx::make_op("mul"), z, beta_broadcast);
        auto add = mm2->add_instruction(migraphx::make_op("add"), dot, mul);
        mm2->add_instruction(migraphx::make_op("identity"), add);
67
68
69
70
71
72
73
74
    }
    EXPECT(p1 == p2);
}

TEST_CASE(dot_add_beta_half)
{
    migraphx::program p1;
    {
75
76
77
78
        auto* mm1 = p1.get_main_module();
        auto x    = mm1->add_parameter("x", migraphx::shape{migraphx::shape::half_type, {2, 2}});
        auto y    = mm1->add_parameter("y", migraphx::shape{migraphx::shape::half_type, {2, 2}});
        auto z    = mm1->add_parameter("z", migraphx::shape{migraphx::shape::half_type, {2, 2}});
79
80
81
        auto dot  = mm1->add_instruction(
            migraphx::make_op("dot", {{"alpha", 1.0}, {"beta", 0.5}}), x, y, z);
        mm1->add_instruction(migraphx::make_op("identity"), dot);
82
83
84
85
    }
    run_pass(p1);
    migraphx::program p2;
    {
86
87
88
89
        auto* mm2 = p2.get_main_module();
        auto x    = mm2->add_parameter("x", migraphx::shape{migraphx::shape::half_type, {2, 2}});
        auto y    = mm2->add_parameter("y", migraphx::shape{migraphx::shape::half_type, {2, 2}});
        auto z    = mm2->add_parameter("z", migraphx::shape{migraphx::shape::half_type, {2, 2}});
90
91
        auto dot =
            mm2->add_instruction(migraphx::make_op("dot", {{"alpha", 1}, {"beta", 0}}), x, y);
92
        auto beta =
93
            mm2->add_literal(migraphx::literal{migraphx::shape{migraphx::shape::half_type}, {0.5}});
94
95
96
97
98
        auto beta_broadcast = mm2->add_instruction(
            migraphx::make_op("multibroadcast", {{"output_lens", {2, 2}}}), beta);
        auto mul = mm2->add_instruction(migraphx::make_op("mul"), z, beta_broadcast);
        auto add = mm2->add_instruction(migraphx::make_op("add"), dot, mul);
        mm2->add_instruction(migraphx::make_op("identity"), add);
99
100
101
102
103
104
105
106
    }
    EXPECT(p1 == p2);
}

TEST_CASE(dot_add_beta_double)
{
    migraphx::program p1;
    {
107
108
109
110
        auto* mm1 = p1.get_main_module();
        auto x    = mm1->add_parameter("x", migraphx::shape{migraphx::shape::double_type, {2, 2}});
        auto y    = mm1->add_parameter("y", migraphx::shape{migraphx::shape::double_type, {2, 2}});
        auto z    = mm1->add_parameter("z", migraphx::shape{migraphx::shape::double_type, {2, 2}});
111
112
113
        auto dot  = mm1->add_instruction(
            migraphx::make_op("dot", {{"alpha", 1.0}, {"beta", 0.5}}), x, y, z);
        mm1->add_instruction(migraphx::make_op("identity"), dot);
114
115
116
117
    }
    run_pass(p1);
    migraphx::program p2;
    {
118
119
120
121
        auto* mm2 = p2.get_main_module();
        auto x    = mm2->add_parameter("x", migraphx::shape{migraphx::shape::double_type, {2, 2}});
        auto y    = mm2->add_parameter("y", migraphx::shape{migraphx::shape::double_type, {2, 2}});
        auto z    = mm2->add_parameter("z", migraphx::shape{migraphx::shape::double_type, {2, 2}});
122
123
        auto dot =
            mm2->add_instruction(migraphx::make_op("dot", {{"alpha", 1}, {"beta", 0}}), x, y);
124
125
        auto beta = mm2->add_literal(
            migraphx::literal{migraphx::shape{migraphx::shape::double_type}, {0.5}});
126
127
128
129
130
        auto beta_broadcast = mm2->add_instruction(
            migraphx::make_op("multibroadcast", {{"output_lens", {2, 2}}}), beta);
        auto mul = mm2->add_instruction(migraphx::make_op("mul"), z, beta_broadcast);
        auto add = mm2->add_instruction(migraphx::make_op("add"), dot, mul);
        mm2->add_instruction(migraphx::make_op("identity"), add);
131
132
133
134
135
136
137
138
    }
    EXPECT(p1 == p2);
}

TEST_CASE(dot_add_beta_int)
{
    migraphx::program p1;
    {
139
140
141
142
        auto* mm1 = p1.get_main_module();
        auto x    = mm1->add_parameter("x", migraphx::shape{migraphx::shape::int32_type, {2, 2}});
        auto y    = mm1->add_parameter("y", migraphx::shape{migraphx::shape::int32_type, {2, 2}});
        auto z    = mm1->add_parameter("z", migraphx::shape{migraphx::shape::int32_type, {2, 2}});
143
144
145
        auto dot  = mm1->add_instruction(
            migraphx::make_op("dot", {{"alpha", 1.0}, {"beta", 0.5}}), x, y, z);
        mm1->add_instruction(migraphx::make_op("identity"), dot);
146
147
148
149
150
151
152
    }
    migraphx::program p2 = p1;
    run_pass(p1);
    EXPECT(p1 == p2);
}

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