eliminate_contiguous_test.cpp 4.41 KB
Newer Older
Paul's avatar
Paul committed
1
2
#include <migraphx/eliminate_contiguous.hpp>
#include <migraphx/dead_code_elimination.hpp>
3
4
#include <migraphx/op/identity.hpp>
#include <migraphx/op/dot.hpp>
5
#include <migraphx/op/sin.hpp>
6
#include <migraphx/op/slice.hpp>
7
8
#include <migraphx/op/transpose.hpp>
#include <migraphx/op/contiguous.hpp>
9
10
11
12
13
14
#include <basic_ops.hpp>
#include <test.hpp>

struct eliminate_contiguous_target
{
    std::string name() const { return "eliminate_contiguous"; }
Paul's avatar
Paul committed
15
    std::vector<migraphx::pass> get_passes(migraphx::context&) const
16
    {
Paul's avatar
Paul committed
17
        return {migraphx::eliminate_contiguous{}, migraphx::dead_code_elimination{}};
18
    }
Paul's avatar
Paul committed
19
    migraphx::context get_context() const { return {}; }
20
21
};

Paul's avatar
Paul committed
22
TEST_CASE(standard_op)
23
{
Paul's avatar
Paul committed
24
    migraphx::program p;
Paul's avatar
Paul committed
25
    auto l = p.add_parameter("x", {migraphx::shape::float_type, {2, 2}});
Paul's avatar
Paul committed
26
27
    auto t = p.add_instruction(migraphx::op::transpose{{1, 0}}, l);
    auto c = p.add_instruction(migraphx::op::contiguous{}, t);
28
29
30
31
32
33
    p.add_instruction(pass_standard_op{}, c);
    auto count = std::distance(p.begin(), p.end());
    p.compile(eliminate_contiguous_target{});
    EXPECT(std::distance(p.begin(), p.end()) == count);
}

Paul's avatar
Paul committed
34
TEST_CASE(standard_op_const)
35
{
Paul's avatar
Paul committed
36
    migraphx::program p;
37
    auto l = p.add_literal(get_2x2());
Paul's avatar
Paul committed
38
39
    auto t = p.add_instruction(migraphx::op::transpose{{1, 0}}, l);
    auto c = p.add_instruction(migraphx::op::contiguous{}, t);
Paul's avatar
Paul committed
40
41
42
43
44
45
46
47
48
49
50
51
    p.add_instruction(pass_standard_op{}, c);
    auto count = std::distance(p.begin(), p.end());
    p.compile(eliminate_contiguous_target{});
    EXPECT(std::distance(p.begin(), p.end()) == 2);
}

TEST_CASE(non_standard_op)
{
    migraphx::program p;
    auto l = p.add_parameter("x", {migraphx::shape::float_type, {2, 2}});
    auto t = p.add_instruction(migraphx::op::transpose{{1, 0}}, l);
    auto c = p.add_instruction(migraphx::op::contiguous{}, t);
52
53
54
    p.add_instruction(pass_op{}, c);
    auto count = std::distance(p.begin(), p.end());
    p.compile(eliminate_contiguous_target{});
55
    EXPECT(std::distance(p.begin(), p.end()) == count);
56
57
}

Paul's avatar
Paul committed
58
59
60
61
62
63
64
65
66
67
68
69
TEST_CASE(non_standard_op_const)
{
    migraphx::program p;
    auto l = p.add_literal(get_2x2());
    auto t = p.add_instruction(migraphx::op::transpose{{1, 0}}, l);
    auto c = p.add_instruction(migraphx::op::contiguous{}, t);
    p.add_instruction(pass_op{}, c);
    auto count = std::distance(p.begin(), p.end());
    p.compile(eliminate_contiguous_target{});
    EXPECT(std::distance(p.begin(), p.end()) == 2);
}

70
71
72
TEST_CASE(transpose_gemm)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
73
74
75
    auto l  = p.add_literal(get_2x2());
    auto t  = p.add_instruction(migraphx::op::transpose{{1, 0}}, l);
    auto c  = p.add_instruction(migraphx::op::contiguous{}, t);
76
77
78
79
80
81
82
    auto ic = p.add_instruction(migraphx::op::identity{}, c);
    p.add_instruction(migraphx::op::dot{}, ic, l);
    auto count = std::distance(p.begin(), p.end());
    p.compile(eliminate_contiguous_target{});
    EXPECT(std::distance(p.begin(), p.end()) == (count - 1));
}

83
84
85
TEST_CASE(transpose_standard_op)
{
    migraphx::program p;
Paul's avatar
Paul committed
86
    auto l = p.add_parameter("x", {migraphx::shape::float_type, {2, 2}});
Shucai Xiao's avatar
Shucai Xiao committed
87
88
    auto t  = p.add_instruction(migraphx::op::transpose{{1, 0}}, l);
    auto c  = p.add_instruction(migraphx::op::contiguous{}, t);
89
90
    auto sn = p.add_instruction(migraphx::op::sin{}, c);
    p.add_instruction(pass_standard_op{}, sn);
91
92
93
94
95
    auto count = std::distance(p.begin(), p.end());
    p.compile(eliminate_contiguous_target{});
    EXPECT(std::distance(p.begin(), p.end()) == count);
}

Paul's avatar
Paul committed
96
97
98
99
100
101
102
103
104
105
106
107
108
TEST_CASE(transpose_standard_op_const)
{
    migraphx::program p;
    auto l  = p.add_literal(get_2x2());
    auto t  = p.add_instruction(migraphx::op::transpose{{1, 0}}, l);
    auto c  = p.add_instruction(migraphx::op::contiguous{}, t);
    auto sn = p.add_instruction(migraphx::op::sin{}, c);
    p.add_instruction(pass_standard_op{}, sn);
    auto count = std::distance(p.begin(), p.end());
    p.compile(eliminate_contiguous_target{});
    EXPECT(std::distance(p.begin(), p.end()) == 3);
}

109
110
111
TEST_CASE(no_packed_unary_op)
{
    migraphx::program p;
Shucai Xiao's avatar
Shucai Xiao committed
112
113
114
    auto l  = p.add_literal(get_2x2());
    auto t  = p.add_instruction(migraphx::op::slice{{1}, {1}, {2}}, l);
    auto c  = p.add_instruction(migraphx::op::contiguous{}, t);
115
116
117
118
119
120
121
    auto sn = p.add_instruction(migraphx::op::sin{}, c);
    p.add_instruction(pass_standard_op{}, sn);
    auto count = std::distance(p.begin(), p.end());
    p.compile(eliminate_contiguous_target{});
    EXPECT(std::distance(p.begin(), p.end()) == count - 1);
}

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