ref_ops_nonstd_shape_test.cpp 9.68 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.
 */
24
25
26
27
#include <iostream>
#include <vector>
#include <migraphx/literal.hpp>
#include <migraphx/instruction.hpp>
28
29
#include <migraphx/register_target.hpp>
#include <migraphx/program.hpp>
30
#include <migraphx/generate.hpp>
31
32
33
34
35
36
37
38
#include <migraphx/verify.hpp>
#include <migraphx/make_op.hpp>
#include <migraphx/pass_manager.hpp>
#include "test.hpp"

TEST_CASE(argmax_test_nonstd_shape)
{
    migraphx::program p;
39
40
    auto* mm = p.get_main_module();
    auto dl = mm->add_literal(migraphx::generate_literal({migraphx::shape::float_type, {2, 3, 4}}));
41
42
43
44
    auto dl_trans =
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 2, 0}}}), dl);
    mm->add_instruction(migraphx::make_op("argmax", {{"axis", -3}}), dl_trans);
    auto p_uncompiled = p;
45
    p.compile(migraphx::make_target("ref"));
46
47
48
49
50
51
52
53
54
55
56
57
    auto result   = p.eval({}).back();
    auto res_gold = p_uncompiled.eval({}).back();
    std::vector<int64_t> result_vec;
    result.visit([&](auto output) { result_vec.assign(output.begin(), output.end()); });
    std::vector<int64_t> res_gold_vec;
    res_gold.visit([&](auto output) { res_gold_vec.assign(output.begin(), output.end()); });
    EXPECT(migraphx::verify_range(result_vec, res_gold_vec));
}

TEST_CASE(argmin_test_nonstd_shape)
{
    migraphx::program p;
58
59
    auto* mm = p.get_main_module();
    auto dl = mm->add_literal(migraphx::generate_literal({migraphx::shape::float_type, {2, 3, 4}}));
60
61
62
63
    auto dl_trans =
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 2, 0}}}), dl);
    mm->add_instruction(migraphx::make_op("argmin", {{"axis", -1}}), dl_trans);
    auto p_uncompiled = p;
64
    p.compile(migraphx::make_target("ref"));
65
66
67
68
69
70
71
72
73
    auto result   = p.eval({}).back();
    auto res_gold = p_uncompiled.eval({}).back();
    std::vector<int64_t> result_vec;
    result.visit([&](auto output) { result_vec.assign(output.begin(), output.end()); });
    std::vector<int64_t> res_gold_vec;
    res_gold.visit([&](auto output) { res_gold_vec.assign(output.begin(), output.end()); });
    EXPECT(migraphx::verify_range(result_vec, res_gold_vec));
}

Charlie Lin's avatar
Charlie Lin committed
74
75
76
77
78
79
80
81
82
83
84
85
TEST_CASE(isnan_broadcast_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s0{migraphx::shape::float_type, {3}};
    migraphx::shape s1{migraphx::shape::float_type, {3, 2}};
    auto nan_val             = std::numeric_limits<float>::quiet_NaN();
    std::vector<float> data0 = {1.2, 5.2, nan_val};
    auto l0                  = mm->add_literal(migraphx::literal{s0, data0});
    auto l1                  = mm->add_instruction(
        migraphx::make_op("broadcast", {{"axis", 0}, {"out_lens", s1.lens()}}), l0);
    mm->add_instruction(migraphx::make_op("isnan"), l1);
86
    p.compile(migraphx::make_target("ref"));
Charlie Lin's avatar
Charlie Lin committed
87
88
89
90
91
92
93
    auto result = p.eval({}).back();
    std::vector<float> results_vector;
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    std::vector<float> correct = {0, 0, 0, 0, 1, 1};
    EXPECT(migraphx::verify_range(results_vector, correct));
}

94
95
96
97
98
99
100
101
102
103
TEST_CASE(squeeze_transpose_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0 =
        mm->add_literal(migraphx::generate_literal({migraphx::shape::float_type, {4, 1, 3, 1, 3}}));
    auto l0_trans =
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {1, 2, 3, 0, 4}}}), l0);
    mm->add_instruction(migraphx::make_op("squeeze"), l0_trans);
    auto p_uncompiled = p;
104
105
106
107
    // contiguous is required to read the values in standard shaped order
    auto* mm_uncompiled = p_uncompiled.get_main_module();
    mm_uncompiled->add_instruction(migraphx::make_op("contiguous"),
                                   std::prev(mm_uncompiled->end()));
108
    p.compile(migraphx::make_target("ref"));
109
110
111
    auto result          = p.eval({}).back();
    auto expected_result = p_uncompiled.eval({}).back();
    EXPECT(result.get_shape() == migraphx::shape{migraphx::shape::float_type, {3, 4, 3}});
112
    EXPECT(result == expected_result);
113
114
115
116
117
118
119
120
121
122
123
}

TEST_CASE(squeeze_multibroadcast_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0 =
        mm->add_literal(migraphx::generate_literal({migraphx::shape::float_type, {1, 3, 1, 3}}));
    auto l0_brcst = mm->add_instruction(
        migraphx::make_op("multibroadcast", {{"out_lens", {4, 1, 3, 4, 3}}}), l0);
    mm->add_instruction(migraphx::make_op("squeeze"), l0_brcst);
124
125
126
127
    auto p_uncompiled   = p;
    auto* mm_uncompiled = p_uncompiled.get_main_module();
    mm_uncompiled->add_instruction(migraphx::make_op("contiguous"),
                                   std::prev(mm_uncompiled->end()));
128
    p.compile(migraphx::make_target("ref"));
129
130
    auto result          = p.eval({}).back();
    auto expected_result = p_uncompiled.eval({}).back();
131
    EXPECT(result.get_shape() == migraphx::shape{migraphx::shape::float_type, {4, 3, 4, 3}});
132
    EXPECT(result == expected_result);
133
134
135
136
137
138
139
140
141
142
143
}

TEST_CASE(squeeze_slice_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    auto l0 =
        mm->add_literal(migraphx::generate_literal({migraphx::shape::float_type, {1, 3, 4, 3}}));
    auto l0_slice = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {2}}, {"starts", {2}}, {"ends", {3}}}), l0);
    mm->add_instruction(migraphx::make_op("squeeze"), l0_slice);
144
145
146
147
    auto p_uncompiled   = p;
    auto* mm_uncompiled = p_uncompiled.get_main_module();
    mm_uncompiled->add_instruction(migraphx::make_op("contiguous"),
                                   std::prev(mm_uncompiled->end()));
148
    p.compile(migraphx::make_target("ref"));
149
150
    auto result          = p.eval({}).back();
    auto expected_result = p_uncompiled.eval({}).back();
151
    EXPECT(result.get_shape() == migraphx::shape{migraphx::shape::float_type, {3, 3}});
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
    EXPECT(result == expected_result);
}

TEST_CASE(unsqueeze_transpose_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s1{migraphx::shape::float_type, {4, 3, 3}};
    auto l0 = mm->add_literal(migraphx::generate_literal(s1));
    auto l0_trans =
        mm->add_instruction(migraphx::make_op("transpose", {{"permutation", {2, 0, 1}}}), l0);
    mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {2}}}), l0_trans);
    auto p_uncompiled   = p;
    auto* mm_uncompiled = p_uncompiled.get_main_module();
    mm_uncompiled->add_instruction(migraphx::make_op("contiguous"),
                                   std::prev(mm_uncompiled->end()));
168
    p.compile(migraphx::make_target("ref"));
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
    auto result          = p.eval({}).back();
    auto expected_result = p_uncompiled.eval({}).back();
    EXPECT(result.get_shape() == migraphx::shape{migraphx::shape::float_type, {3, 4, 1, 3}});
    EXPECT(result == expected_result);
}

TEST_CASE(unsqueeze_multibroadcast_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s1{migraphx::shape::float_type, {4, 1, 3}};
    auto l0 = mm->add_literal(migraphx::generate_literal(s1));
    auto l0_brcst =
        mm->add_instruction(migraphx::make_op("multibroadcast", {{"out_lens", {4, 4, 3, 3}}}), l0);
    mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {2}}}), l0_brcst);
    auto p_uncompiled   = p;
    auto* mm_uncompiled = p_uncompiled.get_main_module();
    mm_uncompiled->add_instruction(migraphx::make_op("contiguous"),
                                   std::prev(mm_uncompiled->end()));
188
    p.compile(migraphx::make_target("ref"));
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
    auto result          = p.eval({}).back();
    auto expected_result = p_uncompiled.eval({}).back();
    EXPECT(result.get_shape() == migraphx::shape{migraphx::shape::float_type, {4, 4, 1, 3, 3}});
    EXPECT(result == expected_result);
}

TEST_CASE(unsqueeze_slice_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s1{migraphx::shape::float_type, {2, 3, 4, 4}};
    auto l0       = mm->add_literal(migraphx::generate_literal(s1));
    auto l0_slice = mm->add_instruction(
        migraphx::make_op("slice", {{"axes", {3}}, {"starts", {2}}, {"ends", {3}}}), l0);
    mm->add_instruction(migraphx::make_op("unsqueeze", {{"axes", {1}}}), l0_slice);
    auto p_uncompiled   = p;
    auto* mm_uncompiled = p_uncompiled.get_main_module();
    mm_uncompiled->add_instruction(migraphx::make_op("contiguous"),
                                   std::prev(mm_uncompiled->end()));
208
    p.compile(migraphx::make_target("ref"));
209
210
211
212
    auto result          = p.eval({}).back();
    auto expected_result = p_uncompiled.eval({}).back();
    EXPECT(result.get_shape() == migraphx::shape{migraphx::shape::float_type, {2, 1, 3, 4, 1}});
    EXPECT(result == expected_result);
213
214
}

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