decompose_test.cpp 6.58 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#include <migraphx/decompose.hpp>
#include <migraphx/pass_manager.hpp>
#include <basic_ops.hpp>
#include <migraphx/op/add.hpp>
#include <migraphx/op/identity.hpp>
#include <migraphx/op/dot.hpp>
#include <migraphx/op/mul.hpp>
#include <migraphx/op/multibroadcast.hpp>
#include <test.hpp>

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

TEST_CASE(dot_add)
{
    migraphx::program p1;
    {
20
21
22
23
24
25
        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}});
        auto dot  = mm1->add_instruction(migraphx::op::dot{}, x, y, z);
        mm1->add_instruction(migraphx::op::identity{}, dot);
26
27
28
29
    }
    run_pass(p1);
    migraphx::program p2;
    {
30
31
32
33
34
35
36
        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}});
        auto dot  = mm2->add_instruction(migraphx::op::dot{1, 0}, x, y);
        auto add  = mm2->add_instruction(migraphx::op::add{}, dot, z);
        mm2->add_instruction(migraphx::op::identity{}, add);
37
38
39
40
41
42
43
44
    }
    EXPECT(p1 == p2);
}

TEST_CASE(dot_add_beta_float)
{
    migraphx::program p1;
    {
45
46
47
48
49
50
        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}});
        auto dot  = mm1->add_instruction(migraphx::op::dot{1.0, 0.5}, x, y, z);
        mm1->add_instruction(migraphx::op::identity{}, dot);
51
52
53
54
    }
    run_pass(p1);
    migraphx::program p2;
    {
55
56
57
58
59
60
61
62
63
64
65
        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}});
        auto dot  = mm2->add_instruction(migraphx::op::dot{1, 0}, x, y);
        auto beta = mm2->add_literal(
            migraphx::literal{migraphx::shape{migraphx::shape::float_type}, {0.5}});
        auto beta_broadcast = mm2->add_instruction(migraphx::op::multibroadcast{{2, 2}}, beta);
        auto mul            = mm2->add_instruction(migraphx::op::mul{}, z, beta_broadcast);
        auto add            = mm2->add_instruction(migraphx::op::add{}, dot, mul);
        mm2->add_instruction(migraphx::op::identity{}, add);
66
67
68
69
70
71
72
73
    }
    EXPECT(p1 == p2);
}

TEST_CASE(dot_add_beta_half)
{
    migraphx::program p1;
    {
74
75
76
77
78
79
        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}});
        auto dot  = mm1->add_instruction(migraphx::op::dot{1.0, 0.5}, x, y, z);
        mm1->add_instruction(migraphx::op::identity{}, dot);
80
81
82
83
    }
    run_pass(p1);
    migraphx::program p2;
    {
84
85
86
87
88
        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}});
        auto dot  = mm2->add_instruction(migraphx::op::dot{1, 0}, x, y);
89
        auto beta =
90
91
92
93
94
            mm2->add_literal(migraphx::literal{migraphx::shape{migraphx::shape::half_type}, {0.5}});
        auto beta_broadcast = mm2->add_instruction(migraphx::op::multibroadcast{{2, 2}}, beta);
        auto mul            = mm2->add_instruction(migraphx::op::mul{}, z, beta_broadcast);
        auto add            = mm2->add_instruction(migraphx::op::add{}, dot, mul);
        mm2->add_instruction(migraphx::op::identity{}, add);
95
96
97
98
99
100
101
102
    }
    EXPECT(p1 == p2);
}

TEST_CASE(dot_add_beta_double)
{
    migraphx::program p1;
    {
103
104
105
106
107
108
        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}});
        auto dot  = mm1->add_instruction(migraphx::op::dot{1.0, 0.5}, x, y, z);
        mm1->add_instruction(migraphx::op::identity{}, dot);
109
110
111
112
    }
    run_pass(p1);
    migraphx::program p2;
    {
113
114
115
116
117
118
119
120
121
122
123
        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}});
        auto dot  = mm2->add_instruction(migraphx::op::dot{1, 0}, x, y);
        auto beta = mm2->add_literal(
            migraphx::literal{migraphx::shape{migraphx::shape::double_type}, {0.5}});
        auto beta_broadcast = mm2->add_instruction(migraphx::op::multibroadcast{{2, 2}}, beta);
        auto mul            = mm2->add_instruction(migraphx::op::mul{}, z, beta_broadcast);
        auto add            = mm2->add_instruction(migraphx::op::add{}, dot, mul);
        mm2->add_instruction(migraphx::op::identity{}, add);
124
125
126
127
128
129
130
131
    }
    EXPECT(p1 == p2);
}

TEST_CASE(dot_add_beta_int)
{
    migraphx::program p1;
    {
132
133
134
135
136
137
        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}});
        auto dot  = mm1->add_instruction(migraphx::op::dot{1.0, 0.5}, x, y, z);
        mm1->add_instruction(migraphx::op::identity{}, dot);
138
139
140
141
142
143
144
    }
    migraphx::program p2 = p1;
    run_pass(p1);
    EXPECT(p1 == p2);
}

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