reduce_dims.cpp 7.47 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
#include <migraphx/reduce_dims.hpp>
#include <migraphx/permutation.hpp>
26
#include <migraphx/ranges.hpp>
27
28
29
30
31
32
33
34
35
36
37
38
#include "test.hpp"

migraphx::shape make_shape(std::vector<std::size_t> lens)
{
    return {migraphx::shape::float_type, std::move(lens)};
}

migraphx::shape make_shape(std::vector<std::size_t> lens, std::vector<std::size_t> strides)
{
    return {migraphx::shape::float_type, std::move(lens), std::move(strides)};
}

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
bool verify_shape(const migraphx::shape& s1, const migraphx::shape& s2)
{
    if(s1.elements() != s2.elements())
        return false;
    return migraphx::all_of(migraphx::range(s1.elements()),
                            [&](auto i) { return s1.index(i) == s2.index(i); });
}

template <class Range1, class Range2>
bool verify_shapes(const Range1& r1, const Range2& r2)
{
    return migraphx::equal(
        r1, r2, [](const auto& s1, const auto& s2) { return verify_shape(s1, s2); });
}

54
55
56
57
58
59
60
TEST_CASE(same_standard)
{
    auto is                              = make_shape({64, 3, 7, 7});
    auto os                              = make_shape({64 * 3 * 7 * 7});
    std::vector<migraphx::shape> ishapes = {is, is, is};
    std::vector<migraphx::shape> eshapes = {os, os, os};
    auto rshapes                         = migraphx::reduce_dims(ishapes);
61
    EXPECT(verify_shapes(ishapes, rshapes));
62
63
64
65
66
67
68
69
70
71
    EXPECT(eshapes == rshapes);
}

TEST_CASE(same_broadcast1)
{
    auto is                              = make_shape({64, 3, 7, 7});
    auto os                              = make_shape({64, 3, 7 * 7});
    std::vector<migraphx::shape> ishapes = {is, make_shape({64, 3, 7, 7}, {0, 1, 0, 0}), is};
    std::vector<migraphx::shape> eshapes = {os, make_shape({64, 3, 7 * 7}, {0, 1, 0}), os};
    auto rshapes                         = migraphx::reduce_dims(ishapes);
72
    EXPECT(verify_shapes(ishapes, rshapes));
73
74
75
76
77
78
79
80
81
82
    EXPECT(eshapes == rshapes);
}

TEST_CASE(same_broadcast2)
{
    auto is                              = make_shape({64, 3, 8, 7, 7});
    auto os                              = make_shape({64, 8 * 3, 7 * 7});
    std::vector<migraphx::shape> ishapes = {is, make_shape({64, 3, 8, 7, 7}, {0, 8, 1, 0, 0}), is};
    std::vector<migraphx::shape> eshapes = {os, make_shape({64, 8 * 3, 7 * 7}, {0, 1, 0}), os};
    auto rshapes                         = migraphx::reduce_dims(ishapes);
83
    EXPECT(verify_shapes(ishapes, rshapes));
84
85
86
87
88
89
90
91
92
93
    EXPECT(eshapes == rshapes);
}

TEST_CASE(same_transposed)
{
    auto is                              = make_shape({64, 3, 7, 7});
    auto os                              = make_shape({64 * 3, 7, 7});
    std::vector<migraphx::shape> ishapes = {is, migraphx::reorder_shape(is, {0, 1, 3, 2}), is};
    std::vector<migraphx::shape> eshapes = {os, migraphx::reorder_shape(os, {0, 2, 1}), os};
    auto rshapes                         = migraphx::reduce_dims(ishapes);
94
    EXPECT(verify_shapes(ishapes, rshapes));
95
96
97
98
99
100
101
102
103
104
    EXPECT(eshapes == rshapes);
}

TEST_CASE(different_masked1)
{
    auto is                              = make_shape({64, 3, 7, 7});
    auto os                              = make_shape({64, 3, 7 * 7});
    std::vector<migraphx::shape> ishapes = {is, make_shape({1, 3, 1, 1}), is};
    std::vector<migraphx::shape> eshapes = {os, make_shape({1, 3, 1}), os};
    auto rshapes                         = migraphx::reduce_dims(ishapes);
105
    EXPECT(verify_shapes(ishapes, rshapes));
106
107
108
109
110
111
112
113
114
115
116
    EXPECT(eshapes == rshapes);
}

TEST_CASE(different_masked2)
{
    auto is                              = make_shape({64, 3, 7, 7});
    auto os                              = make_shape({64, 3, 7 * 7});
    std::vector<migraphx::shape> ishapes = {
        is, make_shape({1, 3, 1, 1}), make_shape({64, 1, 7, 7})};
    std::vector<migraphx::shape> eshapes = {os, make_shape({1, 3, 1}), make_shape({64, 1, 7 * 7})};
    auto rshapes                         = migraphx::reduce_dims(ishapes);
117
    EXPECT(verify_shapes(ishapes, rshapes));
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
    EXPECT(eshapes == rshapes);
}

TEST_CASE(different_incompatible)
{
    auto is                              = make_shape({64, 3, 7, 7});
    std::vector<migraphx::shape> ishapes = {is, make_shape({1, 3, 2, 1}), is};
    auto rshapes                         = migraphx::reduce_dims(ishapes);

    EXPECT(ishapes == rshapes);
}

TEST_CASE(different_ranks)
{
    auto is                              = make_shape({64, 3, 7, 7});
    std::vector<migraphx::shape> ishapes = {is, make_shape({1, 3}), is};
    auto rshapes                         = migraphx::reduce_dims(ishapes);

    EXPECT(ishapes == rshapes);
}

TEST_CASE(transposed1)
{
    std::vector<migraphx::shape> ishapes = {
        make_shape({8, 28, 4, 56, 56}),
        make_shape({8, 28, 4, 56, 56}, {351232, 3136, 87808, 56, 1})};
    std::vector<migraphx::shape> eshapes = {
        make_shape({8, 28, 4, 56 * 56}), make_shape({8, 28, 4, 56 * 56}, {351232, 3136, 87808, 1})};
    auto rshapes = migraphx::reduce_dims(ishapes);
147
    EXPECT(verify_shapes(ishapes, rshapes));
148
149
150
    EXPECT(eshapes == rshapes);
}

151
152
153
154
155
TEST_CASE(non_packed_empty1)
{
    std::vector<migraphx::shape> ishapes = {make_shape({1, 12}, {589824, 64})};
    std::vector<migraphx::shape> eshapes = {make_shape({12}, {64})};
    auto rshapes                         = migraphx::reduce_dims(ishapes);
156
    EXPECT(verify_shapes(ishapes, rshapes));
157
158
159
160
161
162
163
164
    EXPECT(eshapes == rshapes);
}

TEST_CASE(non_packed_empty2)
{
    std::vector<migraphx::shape> ishapes = {make_shape({12, 1}, {64, 589824})};
    std::vector<migraphx::shape> eshapes = {make_shape({12}, {64})};
    auto rshapes                         = migraphx::reduce_dims(ishapes);
165
    EXPECT(verify_shapes(ishapes, rshapes));
166
167
168
169
170
171
172
173
174
175
    EXPECT(eshapes == rshapes);
}

TEST_CASE(single_dim)
{
    std::vector<migraphx::shape> ishapes = {make_shape({1}, {1})};
    auto rshapes                         = migraphx::reduce_dims(ishapes);
    EXPECT(ishapes == rshapes);
}

176
177
178
179
180
181
182
183
184
185
TEST_CASE(step_broadcast_transpose)
{
    std::vector<migraphx::shape> ishapes = {make_shape({1, 2, 2, 1}, {0, 0, 3, 6}),
                                            make_shape({1, 2, 2, 1}, {4, 2, 1, 1})};
    std::vector<migraphx::shape> eshapes = {make_shape({2, 2}, {0, 3}), make_shape({2, 2}, {2, 1})};
    auto rshapes                         = migraphx::reduce_dims(ishapes);
    EXPECT(verify_shapes(ishapes, rshapes));
    EXPECT(eshapes == rshapes);
}

186
187
188
189
190
191
192
TEST_CASE(empty)
{
    auto rshapes = migraphx::reduce_dims({});
    EXPECT(rshapes.empty());
}

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