program_test.cpp 5.43 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
Paul's avatar
Paul committed
24

Paul's avatar
Paul committed
25
26
27
#include <migraphx/program.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/instruction.hpp>
28
#include <migraphx/register_target.hpp>
Paul's avatar
Paul committed
29
#include <sstream>
30
#include <migraphx/apply_alpha_beta.hpp>
Paul's avatar
Paul committed
31
#include "test.hpp"
32
33
#include <migraphx/make_op.hpp>

Paul's avatar
Paul committed
34
35
#include <basic_ops.hpp>

Paul's avatar
Paul committed
36
migraphx::program create_program()
Paul's avatar
Paul committed
37
{
Paul's avatar
Paul committed
38
    migraphx::program p;
39
    auto* mm = p.get_main_module();
Paul's avatar
Paul committed
40

41
42
    auto x = mm->add_parameter("x", {migraphx::shape::int64_type});
    auto y = mm->add_parameter("y", {migraphx::shape::int64_type});
Paul's avatar
Paul committed
43

44
45
46
    auto sum = mm->add_instruction(sum_op{}, x, y);
    auto one = mm->add_literal(1);
    mm->add_instruction(sum_op{}, sum, one);
Paul's avatar
Paul committed
47
48
49
50

    return p;
}

Paul's avatar
Paul committed
51
TEST_CASE(program_equality)
Paul's avatar
Paul committed
52
{
Paul's avatar
Paul committed
53
54
    migraphx::program x = create_program();
    migraphx::program y = create_program();
Shucai Xiao's avatar
Shucai Xiao committed
55
56

    EXPECT(x.size() == 1);
Paul's avatar
Paul committed
57
58
59
    EXPECT(x == y);
}

Paul's avatar
Paul committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
TEST_CASE(program_not_equality1)
{
    migraphx::program x;
    migraphx::program y = create_program();
    EXPECT(x != y);
    x = y;
    EXPECT(x == y);
}

TEST_CASE(program_not_equality2)
{
    migraphx::program x;
    migraphx::program y = create_program();
    EXPECT(x != y);
    y = x;
    EXPECT(x == y);
}

TEST_CASE(program_default_copy_construct)
{
    migraphx::program x;
    migraphx::program y;
    EXPECT(x == y);
}

Shucai Xiao's avatar
Shucai Xiao committed
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
113
114
115
116
117
118
TEST_CASE(program_print)
{
    migraphx::program p = create_program();
    auto* mm            = p.get_main_module();
    auto in1            = mm->end();

    // print end instruction
    p.debug_print(in1);

    // print instruction not in the program
    auto p2   = p;
    auto* mm2 = p2.get_main_module();
    auto in2  = mm2->begin();
    p.debug_print(in2);

    // print last instruction
    auto in3 = std::prev(in1);
    p.debug_print(in3);
}

TEST_CASE(program_annotate)
{
    migraphx::program p1 = create_program();
    migraphx::program p2 = create_program();

    std::stringstream ss1;
    p1.annotate(ss1, [](auto ins) { std::cout << ins->name() << "_1" << std::endl; });

    std::stringstream ss2;
    p2.annotate(ss2, [](auto ins) { std::cout << ins->name() << "_1" << std::endl; });

    EXPECT(ss1.str() == ss2.str());
}

119
120
121
122
TEST_CASE(program_copy)
{
    auto create_program_1 = [] {
        migraphx::program p;
123
124
        auto* mm = p.get_main_module();

125
126
127
        migraphx::shape s{migraphx::shape::float_type, {3, 4, 5}};
        std::vector<float> data(3 * 4 * 5);
        std::iota(data.begin(), data.end(), 1.0f);
128
129
130
        auto l2  = mm->add_literal(migraphx::literal(s, data));
        auto p1  = mm->add_parameter("x", s);
        auto po  = mm->add_outline(s);
131
132
        auto sum = mm->add_instruction(migraphx::make_op("add"), l2, po);
        mm->add_instruction(migraphx::make_op("mul"), sum, p1);
133
134
135
136
137
138

        return p;
    };

    {
        auto p1 = create_program_1();
Shucai Xiao's avatar
Shucai Xiao committed
139
140
141
        migraphx::program p2{};
        p2 = p1;

142
        p2.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
143
144
        EXPECT(p1 != p2);

145
        p1.compile(migraphx::make_target("ref"));
146
        EXPECT(p1 == p2);
147
148

        EXPECT(p1.get_parameter_names() == p2.get_parameter_names());
149
150
151
152
153
    }

    {
        auto p1 = create_program_1();
        auto p2(p1);
Shucai Xiao's avatar
Shucai Xiao committed
154
155
        EXPECT(p1 == p2);

156
        p1.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
157
158
159
        EXPECT(p1 != p2);

        p2 = p1;
160
161
162
163
164
165
        EXPECT(p1 == p2);
    }

    {
        auto p1 = create_program_1();
        auto p2 = create_program();
Shucai Xiao's avatar
Shucai Xiao committed
166
        EXPECT(p1 != p2);
167

Shucai Xiao's avatar
Shucai Xiao committed
168
        p2 = p1;
Shucai Xiao's avatar
Shucai Xiao committed
169
170
        EXPECT(p1 == p2);

171
172
        p1.compile(migraphx::make_target("ref"));
        p2.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
173

174
175
        EXPECT(p1 == p2);
    }
Shucai Xiao's avatar
Shucai Xiao committed
176
177
178

    {
        migraphx::program p1;
179
180
        auto* mm1 = p1.get_main_module();

Shucai Xiao's avatar
Shucai Xiao committed
181
182
183
        migraphx::shape s1{migraphx::shape::float_type, {2, 3}};
        migraphx::shape s2{migraphx::shape::float_type, {3, 6}};
        migraphx::shape s3{migraphx::shape::float_type, {2, 6}};
184
185
186
        auto para1 = mm1->add_parameter("m1", s1);
        auto para2 = mm1->add_parameter("m2", s2);
        auto para3 = mm1->add_parameter("m3", s3);
187
188
        migraphx::add_apply_alpha_beta(
            *mm1, {para1, para2, para3}, migraphx::make_op("dot"), 0.31f, 0.28f);
Shucai Xiao's avatar
Shucai Xiao committed
189
190
191
192
        migraphx::program p2{};
        p2 = p1;
        EXPECT(p2 == p1);

193
194
        p1.compile(migraphx::make_target("ref"));
        p2.compile(migraphx::make_target("ref"));
Shucai Xiao's avatar
Shucai Xiao committed
195
196
        EXPECT(p2 == p1);
    }
197
198
}

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