File: layout_transpose.cpp

package info (click to toggle)
miopen 6.4.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 66,788 kB
  • sloc: cpp: 300,511; lisp: 29,731; ansic: 2,683; sh: 471; python: 323; makefile: 155
file content (459 lines) | stat: -rw-r--r-- 17,921 bytes parent folder | download
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*******************************************************************************
 *
 * MIT License
 *
 * Copyright (c) 2024 Advanced Micro Devices, Inc.
 *
 * 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 <gtest/gtest.h>

#include <cstdlib>
#include <ctime>
#include <vector>

#include <boost/optional.hpp>
#include "../../driver/conv_common.hpp"
#include <miopen/batched_transpose_sol.hpp>
#include <miopen/handle.hpp>
#include <miopen/invoke_params.hpp>
#include <miopen/invoker.hpp>
#include <miopen/miopen.h>
#include <miopen/tensor.hpp>
#include "../tensor_holder.hpp"
#include <miopen/tensor_layout.hpp>

#include "driver.hpp"
#include "random.hpp"

namespace {

template <typename T>
void cpu_ncdhw2ndhwc(T* dst, T* src, uint64_t N, uint64_t C, uint64_t D, uint64_t H, uint64_t W)
{
    for(uint64_t i_n = 0; i_n < N; i_n++)
    {
        for(uint64_t i_d = 0; i_d < D; i_d++)
        {
            for(uint64_t i_h = 0; i_h < H; i_h++)
            {
                for(uint64_t i_w = 0; i_w < W; i_w++)
                {
                    for(uint64_t i_c = 0; i_c < C; i_c++)
                    {
                        uint64_t idx_ndhwc =
                            i_n * D * H * W * C + i_d * H * W * C + i_h * W * C + i_w * C + i_c;
                        uint64_t idx_ncdhw =
                            i_n * C * D * H * W + i_c * D * H * W + i_d * H * W + i_h * W + i_w;
                        dst[idx_ndhwc] = src[idx_ncdhw];
                    }
                }
            }
        }
    }
}

template <typename T>
void cpu_nchw2nhwc(T* dst, T* src, uint64_t N, uint64_t C, uint64_t H, uint64_t W)
{
    cpu_ncdhw2ndhwc<T>(dst, src, N, C, 1, H, W);
}

template <typename T>
void cpu_ndhwc2ncdhw(T* dst, T* src, uint64_t N, uint64_t C, uint64_t D, uint64_t H, uint64_t W)
{
    for(uint64_t i_n = 0; i_n < N; i_n++)
    {
        for(uint64_t i_c = 0; i_c < C; i_c++)
        {
            for(uint64_t i_d = 0; i_d < D; i_d++)
            {
                for(uint64_t i_h = 0; i_h < H; i_h++)
                {
                    for(uint64_t i_w = 0; i_w < W; i_w++)
                    {
                        uint64_t idx_ndhwc =
                            i_n * D * H * W * C + i_d * H * W * C + i_h * W * C + i_w * C + i_c;
                        uint64_t idx_ncdhw =
                            i_n * C * D * H * W + i_c * D * H * W + i_d * H * W + i_h * W + i_w;
                        dst[idx_ncdhw] = src[idx_ndhwc];
                    }
                }
            }
        }
    }
}

template <typename T>
void cpu_nhwc2nchw(T* dst, T* src, uint64_t N, uint64_t C, uint64_t H, uint64_t W)
{
    cpu_ndhwc2ncdhw<T>(dst, src, N, C, 1, H, W);
}

template <typename T, typename TRANSPOSE_SOL>
struct cpu_transpose
{
};

template <typename T>
struct cpu_transpose<T, miopen::TransposeSolutionDefault2Nhwc>
{
    static void run(T* dst, T* src, uint64_t N, uint64_t C, uint64_t H, uint64_t W)
    {
        cpu_nchw2nhwc<T>(dst, src, N, C, H, W);
    }

    static void run(T* dst, T* src, uint64_t N, uint64_t C, uint64_t D, uint64_t H, uint64_t W)
    {
        cpu_ncdhw2ndhwc<T>(dst, src, N, C, D, H, W);
    }
};

template <typename T>
struct cpu_transpose<T, miopen::TransposeSolutionDefault2Ndhwc>
{
    static void run(T* dst, T* src, uint64_t N, uint64_t C, uint64_t D, uint64_t H, uint64_t W)
    {
        cpu_ncdhw2ndhwc<T>(dst, src, N, C, D, H, W);
    }
};

template <typename T>
struct cpu_transpose<T, miopen::TransposeSolutionNhwc2Default>
{
    static void run(T* dst, T* src, uint64_t N, uint64_t C, uint64_t H, uint64_t W)
    {
        cpu_nhwc2nchw<T>(dst, src, N, C, H, W);
    }

    static void run(T* dst, T* src, uint64_t N, uint64_t C, uint64_t D, uint64_t H, uint64_t W)
    {
        cpu_ndhwc2ncdhw<T>(dst, src, N, C, D, H, W);
    }
};

template <typename T>
struct cpu_transpose<T, miopen::TransposeSolutionNdhwc2Default>
{
    static void run(T* dst, T* src, uint64_t N, uint64_t C, uint64_t D, uint64_t H, uint64_t W)
    {
        cpu_ndhwc2ncdhw<T>(dst, src, N, C, D, H, W);
    }
};

constexpr int RAND_INTEGER_MAX = 120;
constexpr int RAND_INTEGER_MIN = -88;

template <typename T>
bool compare_equal(T r1, T r2)
{
    return r1 == r2;
}

template <>
bool compare_equal<float>(float r1, float r2)
{
    return miopen::float_equal(r1, r2);
}

template <typename T>
void verify_tensor(tensor<T>& t_tst, const char* _tst, tensor<T>& t_ref, const char* _ref)
{
    ASSERT_EQ(t_tst.data.size(), t_ref.data.size()) << " tensor sizes not equal, should not happen";
    if(t_tst.data.size() != t_ref.data.size())
        return;

    auto idx = miopen::mismatch_idx(t_tst.data, t_ref.data, compare_equal<T>);

    EXPECT_GE(idx, miopen::range_distance(t_ref))
        << "diff at:" << idx << ", " << _tst << ":" << t_tst[idx] << ", " << _ref << ":"
        << t_ref[idx];
}

struct transpose_dims
{
    static constexpr uint32_t image_size_rand_offset{29};
    static constexpr uint32_t image_size_rand_range{13};
    static uint32_t get_max_image_size()
    {
        return image_size_rand_offset + image_size_rand_range - 1;
    }
    static std::vector<uint32_t> get_image_size()
    {
        std::vector<uint32_t> v = {1, 9, 14};
        v.push_back(prng::gen_off_range(image_size_rand_offset, image_size_rand_range));
        return v;
    }
    static std::vector<uint32_t> get_channel_size()
    {
        std::vector<uint32_t> v = {3, 11};
        v.push_back(prng::gen_off_range(19, 7));
        return v;
    }
    static std::vector<uint32_t> get_batch_size()
    {
        std::vector<uint32_t> v = {1, 2, 4};
        v.push_back(prng::gen_off_range(3, 4));
        return v;
    }
};

template <typename T>
auto gen_value =
    [](auto... is) { return static_cast<T>(prng::gen_A_to_B(RAND_INTEGER_MIN, RAND_INTEGER_MAX)); };

} // namespace

template <typename T, class TRANSPOSE_SOL>
struct LayoutTransposeTest_2D : public ::testing::TestWithParam<std::tuple<uint32_t, uint32_t>>
{
protected:
    miopen::ExecutionContext ctx;

    uint32_t n;
    uint32_t c;

    miopen::Invoker PrepareLayoutTransposeInvoker(const miopen::ExecutionContext& ctx_,
                                                  miopenDataType_t data_type_,
                                                  uint32_t n_,
                                                  uint32_t c_,
                                                  uint32_t h_,
                                                  uint32_t w_)
    {
        TRANSPOSE_SOL transpose_sol(ctx_, data_type_, n_, c_, h_, w_);
        auto invoker_factory = transpose_sol.MakeBatchedTransposeInvokerFactory();
        std::vector<miopen::solver::KernelInfo> construction_params{transpose_sol.GetKernelInfo()};

        return ctx_.GetStream().PrepareInvoker(invoker_factory, construction_params);
    }

    virtual void SetUp() override { std::tie(n, c) = GetParam(); }
    void RunTest()
    {
        auto&& handle = get_handle();

        auto wh            = transpose_dims::get_max_image_size();
        auto max_tensor_sz = n * c * wh * wh;
        auto dst_dev       = handle.Create(sizeof(T) * max_tensor_sz);

        auto H = transpose_dims::get_image_size();
        auto W = transpose_dims::get_image_size();

        for(auto h : H)
        {
            for(auto w : W)
            {
                std::vector<int> tensor_len = {static_cast<int>(n),
                                               static_cast<int>(c),
                                               static_cast<int>(h),
                                               static_cast<int>(w)};

                std::vector<int> tensor_strides;

                std::string layout_default = miopen::tensor_layout_get_default(tensor_len.size());
                std::string layout_string =
                    miopen::TensorDescriptor::LayoutEnumToStr(miopenTensorNCHW);

                miopen::tensor_layout_to_strides(
                    tensor_len, layout_default, layout_string, tensor_strides);

                auto t_src     = tensor<T>{tensor_len, tensor_strides}.generate(gen_value<T>);
                auto t_dst     = tensor<T>{tensor_len, tensor_strides};
                auto t_dst_gpu = tensor<T>{tensor_len, tensor_strides};

                auto src_dev = handle.Write(t_src.data);

                ctx.SetStream(&handle);

                // prep gpu invoker
                auto invoker = PrepareLayoutTransposeInvoker(ctx, miopen_type<T>{}, n, c, h, w);
                const auto invoke_param = transpose_invoke_param{src_dev.get(), dst_dev.get()};

                // run gpu
                invoker(handle, invoke_param);

                t_dst_gpu.data = handle.Read<T>(dst_dev, t_dst_gpu.data.size());

                // run cpu
                cpu_transpose<T, TRANSPOSE_SOL>::run(
                    t_dst.data.data(), t_src.data.data(), n, c, h, w);

                // we expect exact match, since use integer
                verify_tensor(t_dst_gpu, "gpu", t_dst, "cpu");
            }
        }
    }
    virtual void TearDown() override {}
};

template <typename T, class TRANSPOSE_SOL>
struct LayoutTransposeTest_3D : public ::testing::TestWithParam<std::tuple<uint32_t, uint32_t>>
{
protected:
    miopen::ExecutionContext ctx;

    uint32_t n;
    uint32_t c;

    miopen::Invoker PrepareLayoutTransposeInvoker(const miopen::ExecutionContext& ctx_,
                                                  miopenDataType_t data_type_,
                                                  uint32_t n_,
                                                  uint32_t c_,
                                                  uint32_t d_,
                                                  uint32_t h_,
                                                  uint32_t w_)
    {
        TRANSPOSE_SOL transpose_sol(ctx_, data_type_, n_, c_, d_, h_, w_);
        auto invoker_factory = transpose_sol.MakeBatchedTransposeInvokerFactory();
        std::vector<miopen::solver::KernelInfo> construction_params{transpose_sol.GetKernelInfo()};

        return ctx_.GetStream().PrepareInvoker(invoker_factory, construction_params);
    }

    virtual void SetUp() override { std::tie(n, c) = GetParam(); }
    void RunTest()
    {
        auto&& handle = get_handle();
        ctx.SetStream(&handle);

        auto dwh           = transpose_dims::get_max_image_size();
        auto max_tensor_sz = n * c * dwh * dwh * dwh;
        auto dst_3d_dev    = handle.Create(sizeof(T) * max_tensor_sz);
        auto dst_2d_dev    = handle.Create(sizeof(T) * max_tensor_sz);

        auto W = transpose_dims::get_image_size();
        auto H = transpose_dims::get_image_size();
        auto D = transpose_dims::get_image_size();

        for(auto w : W)
        {
            for(auto h : H)
            {
                for(auto d : D)
                {
                    std::vector<int> tensor_len = {static_cast<int>(n),
                                                   static_cast<int>(c),
                                                   static_cast<int>(d),
                                                   static_cast<int>(h),
                                                   static_cast<int>(w)};

                    std::vector<int> tensor_strides;
                    std::string layout_default =
                        miopen::tensor_layout_get_default(tensor_len.size());
                    std::string layout_string =
                        miopen::TensorDescriptor::LayoutEnumToStr(miopenTensorNCDHW);

                    miopen::tensor_layout_to_strides(
                        tensor_len, layout_default, layout_string, tensor_strides);

                    auto t_src     = tensor<T>{tensor_len, tensor_strides}.generate(gen_value<T>);
                    auto t_gpu_2d  = tensor<T>{tensor_len, tensor_strides};
                    auto t_gpu_3d  = tensor<T>{tensor_len, tensor_strides};
                    auto t_dst_ref = tensor<T>{tensor_len, tensor_strides};

                    auto src_dev = handle.Write(t_src.data);

                    // prep gpu 3D
                    auto invoker_3d =
                        PrepareLayoutTransposeInvoker(ctx, miopen_type<T>{}, n, c, d, h, w);
                    const auto invoke_param_3d =
                        transpose_invoke_param{src_dev.get(), dst_3d_dev.get()};

                    // run gpu 3D
                    invoker_3d(handle, invoke_param_3d);

                    t_gpu_3d.data = handle.Read<T>(dst_3d_dev, t_gpu_3d.data.size());

                    // prep gpu 2D
                    auto invoker_2d =
                        PrepareLayoutTransposeInvoker(ctx, miopen_type<T>{}, n, c, 1, d * h, w);
                    const auto invoke_param_2d =
                        transpose_invoke_param{src_dev.get(), dst_2d_dev.get()};

                    // run gpu 2D
                    invoker_2d(handle, invoke_param_2d);

                    t_gpu_2d.data = handle.Read<T>(dst_2d_dev, t_gpu_2d.data.size());

                    // run cpu
                    cpu_transpose<T, TRANSPOSE_SOL>::run(
                        t_dst_ref.data.data(), t_src.data.data(), n, c, d, h, w);

                    // we expect exact match, since use integer
                    verify_tensor(t_gpu_3d, "gpu3d", t_dst_ref, "cpu");
                    verify_tensor(t_gpu_2d, "gpu2d", t_dst_ref, "cpu");
                }
            }
        }
    }
    virtual void TearDown() override {}
};

#define DEFINE_LayoutTransposeTest_2D(type, naming_type, sol)                 \
    struct GPU_LayoutTransposeTest_2D_##sol##_##naming_type                   \
        : public LayoutTransposeTest_2D<type, miopen::sol>                    \
    {                                                                         \
    };                                                                        \
    TEST_P(GPU_LayoutTransposeTest_2D_##sol##_##naming_type,                  \
           LayoutTransposeTest_2D_##sol##_##type##_P)                         \
    {                                                                         \
        RunTest();                                                            \
    }                                                                         \
    INSTANTIATE_TEST_SUITE_P(                                                 \
        Full,                                                                 \
        GPU_LayoutTransposeTest_2D_##sol##_##naming_type,                     \
        testing::Combine(testing::ValuesIn(transpose_dims::get_batch_size()), \
                         testing::ValuesIn(transpose_dims::get_channel_size())));

#define DEFINE_2D_TYPED_TESTS(sol)                       \
    DEFINE_LayoutTransposeTest_2D(float, FP32, sol);     \
    DEFINE_LayoutTransposeTest_2D(float16, FP16, sol);   \
    DEFINE_LayoutTransposeTest_2D(bfloat16, BFP16, sol); \
    DEFINE_LayoutTransposeTest_2D(uint16_t, I16, sol);   \
    DEFINE_LayoutTransposeTest_2D(uint8_t, I8, sol);

DEFINE_2D_TYPED_TESTS(TransposeSolutionDefault2Nhwc);
DEFINE_2D_TYPED_TESTS(TransposeSolutionNhwc2Default);

#define DEFINE_LayoutTransposeTest_3D(type, naming_type, sol)                 \
    struct GPU_LayoutTransposeTest_3D_##sol##_##naming_type                   \
        : public LayoutTransposeTest_3D<type, miopen::sol>                    \
    {                                                                         \
    };                                                                        \
    TEST_P(GPU_LayoutTransposeTest_3D_##sol##_##naming_type,                  \
           LayoutTransposeTest_3D_##sol##_##type##_P)                         \
    {                                                                         \
        RunTest();                                                            \
    }                                                                         \
    INSTANTIATE_TEST_SUITE_P(                                                 \
        Full,                                                                 \
        GPU_LayoutTransposeTest_3D_##sol##_##naming_type,                     \
        testing::Combine(testing::ValuesIn(transpose_dims::get_batch_size()), \
                         testing::ValuesIn(transpose_dims::get_channel_size())));

#define DEFINE_3D_TYPED_TESTS(sol)                       \
    DEFINE_LayoutTransposeTest_3D(float, FP32, sol);     \
    DEFINE_LayoutTransposeTest_3D(float16, FP16, sol);   \
    DEFINE_LayoutTransposeTest_3D(bfloat16, BFP16, sol); \
    DEFINE_LayoutTransposeTest_3D(uint16_t, I16, sol);   \
    DEFINE_LayoutTransposeTest_3D(uint8_t, I8, sol);

DEFINE_3D_TYPED_TESTS(TransposeSolutionDefault2Ndhwc);
DEFINE_3D_TYPED_TESTS(TransposeSolutionNdhwc2Default);