reshape.cpp 11.2 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
24
25
26
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015-2023 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.
 */
#include <migraphx/instruction.hpp>
#include <migraphx/literal.hpp>
#include <migraphx/make_op.hpp>
27
#include <migraphx/program.hpp>
28
29
30
31
32
#include <migraphx/register_target.hpp>
#include <migraphx/verify.hpp>

#include <test.hpp>

Ted Themistokleous's avatar
Ted Themistokleous committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
TEST_CASE(reshape_lazy_test0)
{
    migraphx::shape a_shape{migraphx::shape::float_type, {24, 1, 1, 1}};
    std::vector<float> data(24);
    std::iota(data.begin(), data.end(), -3);
    migraphx::program p;
    auto* mm                       = p.get_main_module();
    auto l                         = mm->add_literal(migraphx::literal{a_shape, data});
    std::vector<int64_t> new_shape = {8, 3, 1, 1};
    mm->add_instruction(migraphx::make_op("reshape_lazy", {{"dims", new_shape}}), l);
    p.compile(migraphx::make_target("ref"));
    auto result = p.eval({}).back();
    std::vector<float> results_vector{};
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
47
    EXPECT(migraphx::verify::verify_rms_range(results_vector, data));
Ted Themistokleous's avatar
Ted Themistokleous committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
}

TEST_CASE(reshape_lazy_test1)
{
    migraphx::shape a_shape{migraphx::shape::float_type, {24, 1, 1, 1}};
    std::vector<float> data(24);
    std::iota(data.begin(), data.end(), -3);
    migraphx::program p;
    auto* mm                       = p.get_main_module();
    auto l                         = mm->add_literal(migraphx::literal{a_shape, data});
    std::vector<int64_t> new_shape = {1, 3, 4, 2};
    mm->add_instruction(migraphx::make_op("reshape_lazy", {{"dims", new_shape}}), l);
    p.compile(migraphx::make_target("ref"));
    auto result = p.eval({}).back();
    std::vector<float> results_vector{};
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
64
    EXPECT(migraphx::verify::verify_rms_range(results_vector, data));
Ted Themistokleous's avatar
Ted Themistokleous committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
}

TEST_CASE(reshape_lazy_test2)
{
    migraphx::shape a_shape{migraphx::shape::float_type, {24, 1, 1, 1}};
    std::vector<float> data(24);
    std::iota(data.begin(), data.end(), -3);
    migraphx::program p;
    auto* mm                       = p.get_main_module();
    auto l                         = mm->add_literal(migraphx::literal{a_shape, data});
    std::vector<int64_t> new_shape = {1, 2, 3, 4};
    mm->add_instruction(migraphx::make_op("reshape_lazy", {{"dims", new_shape}}), l);
    p.compile(migraphx::make_target("ref"));
    auto result = p.eval({}).back();
    std::vector<float> results_vector{};
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
81
    EXPECT(migraphx::verify::verify_rms_range(results_vector, data));
Ted Themistokleous's avatar
Ted Themistokleous committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
}

TEST_CASE(reshape_lazy_dyn_test)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {{1, 4}, {24, 24}, {1, 1}, {1, 1}}};
    std::vector<int64_t> new_shape = {0, 8, 3, 1};
    auto input                     = mm->add_parameter("X", s);
    mm->add_instruction(migraphx::make_op("reshape_lazy", {{"dims", new_shape}}), input);
    p.compile(migraphx::make_target("ref"));

    std::vector<float> data(48);
    std::iota(data.begin(), data.end(), -3);
    migraphx::parameter_map params;
    migraphx::shape input_fixed_shape{migraphx::shape::float_type, {2, 24, 1, 1}};
    params["X"] = migraphx::argument(input_fixed_shape, data.data());
    auto result = p.eval(params).back();
    std::vector<float> results_vector{};
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
102
    EXPECT(migraphx::verify::verify_rms_range(results_vector, data));
Ted Themistokleous's avatar
Ted Themistokleous committed
103
104
}

105
106
107
TEST_CASE(reshape_test0)
{
    migraphx::shape a_shape{migraphx::shape::float_type, {24, 1, 1, 1}};
108
109
    std::vector<float> gold(24);
    std::iota(gold.begin(), gold.end(), -3);
110
111
    migraphx::program p;
    auto* mm                       = p.get_main_module();
112
    auto l                         = mm->add_literal(migraphx::literal{a_shape, gold});
113
114
115
116
117
118
    std::vector<int64_t> new_shape = {8, 3, 1, 1};
    mm->add_instruction(migraphx::make_op("reshape", {{"dims", new_shape}}), l);
    p.compile(migraphx::make_target("ref"));
    auto result = p.eval({}).back();
    std::vector<float> results_vector{};
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
119
    EXPECT(migraphx::verify::verify_rms_range(results_vector, gold));
120
121
122
123
124
}

TEST_CASE(reshape_test1)
{
    migraphx::shape a_shape{migraphx::shape::float_type, {24, 1, 1, 1}};
125
126
    std::vector<float> gold(24);
    std::iota(gold.begin(), gold.end(), -3);
127
128
    migraphx::program p;
    auto* mm                       = p.get_main_module();
129
    auto l                         = mm->add_literal(migraphx::literal{a_shape, gold});
130
131
132
133
134
135
    std::vector<int64_t> new_shape = {1, 3, 4, 2};
    mm->add_instruction(migraphx::make_op("reshape", {{"dims", new_shape}}), l);
    p.compile(migraphx::make_target("ref"));
    auto result = p.eval({}).back();
    std::vector<float> results_vector{};
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
136
    EXPECT(migraphx::verify::verify_rms_range(results_vector, gold));
137
138
139
140
141
}

TEST_CASE(reshape_test2)
{
    migraphx::shape a_shape{migraphx::shape::float_type, {24, 1, 1, 1}};
142
143
    std::vector<float> gold(24);
    std::iota(gold.begin(), gold.end(), -3);
144
145
    migraphx::program p;
    auto* mm                       = p.get_main_module();
146
    auto l                         = mm->add_literal(migraphx::literal{a_shape, gold});
147
148
149
150
151
152
    std::vector<int64_t> new_shape = {1, 2, 3, 4};
    mm->add_instruction(migraphx::make_op("reshape", {{"dims", new_shape}}), l);
    p.compile(migraphx::make_target("ref"));
    auto result = p.eval({}).back();
    std::vector<float> results_vector{};
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
153
    EXPECT(migraphx::verify::verify_rms_range(results_vector, gold));
154
155
}

156
TEST_CASE(reshape_dyn_1in_test)
157
158
159
160
161
162
163
164
165
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s{migraphx::shape::float_type, {{1, 4}, {24, 24}, {1, 1}, {1, 1}}};
    std::vector<int64_t> new_shape = {0, 8, 3, 1};
    auto input                     = mm->add_parameter("X", s);
    mm->add_instruction(migraphx::make_op("reshape", {{"dims", new_shape}}), input);
    p.compile(migraphx::make_target("ref"));

166
167
    std::vector<float> gold(48);
    std::iota(gold.begin(), gold.end(), -3);
168
169
    migraphx::parameter_map params;
    migraphx::shape input_fixed_shape{migraphx::shape::float_type, {2, 24, 1, 1}};
170
    params["X"] = migraphx::argument(input_fixed_shape, gold.data());
171
172
173
    auto result = p.eval(params).back();
    std::vector<float> results_vector{};
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
174
    EXPECT(migraphx::verify::verify_rms_range(results_vector, gold));
175
}
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250

TEST_CASE(reshape_2in_test0)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s_in{migraphx::shape::float_type, {{1, 4}, {24, 24}, {1, 1}, {1, 1}}};
    migraphx::shape s_out{migraphx::shape::float_type, {{1, 4}, {6, 6}, {4, 4}, {1, 1}}};
    auto input         = mm->add_parameter("X", s_in);
    auto output_buffer = mm->add_parameter("Y", s_out);
    mm->add_instruction(migraphx::make_op("reshape"), input, output_buffer);
    p.compile(migraphx::make_target("ref"));

    std::vector<float> gold(48);
    std::iota(gold.begin(), gold.end(), -3.);
    std::vector<float> buffer(48);
    std::iota(buffer.begin(), buffer.end(), 0.);
    migraphx::parameter_map params;
    migraphx::shape input_fixed_shape{migraphx::shape::float_type, {2, 24, 1, 1}};
    migraphx::shape output_fixed_shape{migraphx::shape::float_type, {2, 6, 4, 1}};
    params["X"] = migraphx::argument(input_fixed_shape, gold.data());
    params["Y"] = migraphx::argument(output_fixed_shape, buffer.data());
    auto result = p.eval(params).back();
    EXPECT(result.get_shape() == output_fixed_shape);
    std::vector<float> results_vector{};
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    EXPECT(migraphx::verify::verify_rms_range(results_vector, gold));
}

TEST_CASE(reshape_2in_test1)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s_in{migraphx::shape::float_type, {2, 24, 1, 1}};
    migraphx::shape s_out{migraphx::shape::float_type, {{2, 4}, {6, 6}, {2, 4}, {1, 1}}};
    auto input         = mm->add_parameter("X", s_in);
    auto output_buffer = mm->add_parameter("Y", s_out);
    mm->add_instruction(migraphx::make_op("reshape"), input, output_buffer);
    p.compile(migraphx::make_target("ref"));

    std::vector<float> gold(48);
    std::iota(gold.begin(), gold.end(), -3.);
    std::vector<float> buffer(48);
    std::iota(buffer.begin(), buffer.end(), 0.);
    migraphx::parameter_map params;
    migraphx::shape output_fixed_shape{migraphx::shape::float_type, {2, 6, 4, 1}};
    params["X"] = migraphx::argument(s_in, gold.data());
    params["Y"] = migraphx::argument(output_fixed_shape, buffer.data());
    auto result = p.eval(params).back();
    EXPECT(result.get_shape() == output_fixed_shape);
    std::vector<float> results_vector{};
    result.visit([&](auto output) { results_vector.assign(output.begin(), output.end()); });
    EXPECT(migraphx::verify::verify_rms_range(results_vector, gold));
}
TEST_CASE(reshape_2in_elements_runtime_error)
{
    migraphx::program p;
    auto* mm = p.get_main_module();
    migraphx::shape s_in{migraphx::shape::float_type, {2, 24, 1, 1}};
    migraphx::shape s_out{migraphx::shape::float_type, {{2, 4}, {6, 6}, {2, 4}, {1, 1}}};
    auto input         = mm->add_parameter("X", s_in);
    auto output_buffer = mm->add_parameter("Y", s_out);
    mm->add_instruction(migraphx::make_op("reshape"), input, output_buffer);
    p.compile(migraphx::make_target("ref"));

    std::vector<float> gold(48);
    std::iota(gold.begin(), gold.end(), -3.);
    std::vector<float> buffer(48);
    std::iota(buffer.begin(), buffer.end(), 0.);
    migraphx::parameter_map params;
    // elements do not match up
    migraphx::shape output_fixed_shape{migraphx::shape::float_type, {2, 6, 2, 1}};
    params["X"] = migraphx::argument(s_in, gold.data());
    params["Y"] = migraphx::argument(output_fixed_shape, buffer.data());
    EXPECT(test::throws([&] { std::ignore = p.eval(params).back(); }));
}