profile_conv_tensor_rearrange.cpp 13.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// SPDX-License-Identifier: MIT
// Copyright (c) 2023, Advanced Micro Devices, Inc. All rights reserved.

#include <iostream>
#include <numeric>
#include <initializer_list>
#include <cstdlib>

#include "profiler/profile_conv_tensor_rearrange_impl.hpp"
#include "profiler_operation_registry.hpp"

namespace {

enum struct RearrangeOp
{
    ImageToColumn, // 0
    ColumnToImage, // 1
};

enum struct ConvLayout
{
22
23
    GNHWC, // 0
    NHWGC, // 1
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
};

enum struct DataType
{
    F32_F32,   // 0
    F16_F16,   // 1
    BF16_BF16, // 2
    INT8_INT8, // 3
};

#define OP_NAME "conv_tensor_rearrange"
#define OP_DESC "Conv Tensor Rearrange"

static void print_helper_msg()
{
    std::cout
        // clang-format off
        << "arg1: tensor operation (" OP_NAME ": " OP_DESC ")\n"
        << "arg2: data type (0: Input fp32, Weight fp32, Output fp32\n"
        << "                 1: Input fp16, Weight fp16, Output fp16\n"
        << "                 2: Input bf16, Weight bf16, Output bf16\n"
        << "                 3: Input int8, Weight int8, Output int8)\n"
46
47
        << "arg3: tensor layout (0: Input[G, N, Hi, Wi, C], Output[G * N * Ho * Wo, Y * X * C],\n"
        << "                     1: Input[N, Hi, Wi, G, C], Output[N * Ho * Wo * G, Y * X * C])\n"
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
        << "arg4: verification (0: no, 1: yes)\n"
        << "arg5: initialization (0: no init, 1: integer value, 2: decimal value)\n"
        << "arg6: print tensor value (0: no; 1: yes)\n"
        << "arg7: time kernel (0: no, 1: yes)\n"
        << "arg8: operation type (0: ImageToColumn, 1: ColumnToImage)\n"
        << ck::utils::conv::get_conv_param_parser_helper_msg() << std::endl;
    // clang-format on
}

} // namespace

int profile_conv_tensor_rearrange(int argc, char* argv[])
{
    // 9 for control, 1 for num_dim_spatial
    if(argc < 10)
    {
        print_helper_msg();
        return 1;
    }

    const auto data_type       = static_cast<DataType>(std::stoi(argv[2]));
    const auto layout          = static_cast<ConvLayout>(std::stoi(argv[3]));
    const bool do_verification = std::stoi(argv[4]);
    const int init_method      = std::stoi(argv[5]);
    const bool do_log          = std::stoi(argv[6]);
    const bool time_kernel     = std::stoi(argv[7]);
    const auto rearrange_op    = static_cast<RearrangeOp>(std::stoi(argv[8]));
    const int num_dim_spatial  = std::stoi(argv[9]);

    // 9 for control, 1 for num_dim_spatial, 4 for G/N/K/C, and 6 * num_dim_spatial
    if(argc != 9 + 1 + 4 + 6 * num_dim_spatial)
    {
        print_helper_msg();
        return 1;
    }

    const auto params = ck::utils::conv::parse_conv_param(num_dim_spatial, 10, argv);

    using F32  = float;
    using F16  = ck::half_t;
    using BF16 = ck::bhalf_t;
    using INT8 = int8_t;

    using namespace ck::tensor_layout::convolution;
    using namespace ck::conv_tensor_rearrange_op;

    constexpr auto I1 = ck::Number<1>{};
    constexpr auto I2 = ck::Number<2>{};
    constexpr auto I3 = ck::Number<3>{};

    auto profile = [&](auto num_dim_spatial_tmp,
                       auto in_layout,
                       auto in_type,
                       auto out_type,
                       auto rearrange_op_type) {
        constexpr ck::index_t NDimSpatial = num_dim_spatial_tmp.value;

        using InLayout = decltype(in_layout);

        using InDataType  = decltype(in_type);
        using OutDataType = decltype(out_type);

        using Op = decltype(rearrange_op_type);

        bool pass = ck::profiler::
            profile_conv_tensor_rearrange_impl<NDimSpatial, InLayout, InDataType, OutDataType, Op>(
                do_verification, init_method, do_log, time_kernel, params);

        return pass ? 0 : 1;
    };

    if(rearrange_op == RearrangeOp::ImageToColumn)
    {
121
        if(layout == ConvLayout::GNHWC)
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
        {
            if(num_dim_spatial == 1)
            {
                if(data_type == DataType::F32_F32)
                {
                    return profile(I1, GNWC{}, F32{}, F32{}, ImageToColumn{});
                }
                else if(data_type == DataType::F16_F16)
                {
                    return profile(I1, GNWC{}, F16{}, F16{}, ImageToColumn{});
                }
                else if(data_type == DataType::BF16_BF16)
                {
                    return profile(I1, GNWC{}, BF16{}, BF16{}, ImageToColumn{});
                }
                else if(data_type == DataType::INT8_INT8)
                {
                    return profile(I1, GNWC{}, INT8{}, INT8{}, ImageToColumn{});
                }
            }
            else if(num_dim_spatial == 2)
            {
                if(data_type == DataType::F32_F32)
                {
                    return profile(I2, GNHWC{}, F32{}, F32{}, ImageToColumn{});
                }
                else if(data_type == DataType::F16_F16)
                {
                    return profile(I2, GNHWC{}, F16{}, F16{}, ImageToColumn{});
                }
                else if(data_type == DataType::BF16_BF16)
                {
                    return profile(I2, GNHWC{}, BF16{}, BF16{}, ImageToColumn{});
                }
                else if(data_type == DataType::INT8_INT8)
                {
                    return profile(I2, GNHWC{}, INT8{}, INT8{}, ImageToColumn{});
                }
            }
            else if(num_dim_spatial == 3)
            {
                if(data_type == DataType::F32_F32)
                {
                    return profile(I3, GNDHWC{}, F32{}, F32{}, ImageToColumn{});
                }
                else if(data_type == DataType::F16_F16)
                {
                    return profile(I3, GNDHWC{}, F16{}, F16{}, ImageToColumn{});
                }
                else if(data_type == DataType::BF16_BF16)
                {
                    return profile(I3, GNDHWC{}, BF16{}, BF16{}, ImageToColumn{});
                }
                else if(data_type == DataType::INT8_INT8)
                {
                    return profile(I3, GNDHWC{}, INT8{}, INT8{}, ImageToColumn{});
                }
            }
        }
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
        else if(layout == ConvLayout::NHWGC)
        {
            if(num_dim_spatial == 1)
            {
                if(data_type == DataType::F32_F32)
                {
                    return profile(I1, NWGC{}, F32{}, F32{}, ImageToColumn{});
                }
                else if(data_type == DataType::F16_F16)
                {
                    return profile(I1, NWGC{}, F16{}, F16{}, ImageToColumn{});
                }
                else if(data_type == DataType::BF16_BF16)
                {
                    return profile(I1, NWGC{}, BF16{}, BF16{}, ImageToColumn{});
                }
                else if(data_type == DataType::INT8_INT8)
                {
                    return profile(I1, NWGC{}, INT8{}, INT8{}, ImageToColumn{});
                }
            }
            else if(num_dim_spatial == 2)
            {
                if(data_type == DataType::F32_F32)
                {
                    return profile(I2, NHWGC{}, F32{}, F32{}, ImageToColumn{});
                }
                else if(data_type == DataType::F16_F16)
                {
                    return profile(I2, NHWGC{}, F16{}, F16{}, ImageToColumn{});
                }
                else if(data_type == DataType::BF16_BF16)
                {
                    return profile(I2, NHWGC{}, BF16{}, BF16{}, ImageToColumn{});
                }
                else if(data_type == DataType::INT8_INT8)
                {
                    return profile(I2, NHWGC{}, INT8{}, INT8{}, ImageToColumn{});
                }
            }
            else if(num_dim_spatial == 3)
            {
                if(data_type == DataType::F32_F32)
                {
                    return profile(I3, NDHWGC{}, F32{}, F32{}, ImageToColumn{});
                }
                else if(data_type == DataType::F16_F16)
                {
                    return profile(I3, NDHWGC{}, F16{}, F16{}, ImageToColumn{});
                }
                else if(data_type == DataType::BF16_BF16)
                {
                    return profile(I3, NDHWGC{}, BF16{}, BF16{}, ImageToColumn{});
                }
                else if(data_type == DataType::INT8_INT8)
                {
                    return profile(I3, NDHWGC{}, INT8{}, INT8{}, ImageToColumn{});
                }
            }
        }
241
242
243
    }
    else if(rearrange_op == RearrangeOp::ColumnToImage)
    {
244
        if(layout == ConvLayout::GNHWC)
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
        {
            if(num_dim_spatial == 1)
            {
                if(data_type == DataType::F32_F32)
                {
                    return profile(I1, GNWC{}, F32{}, F32{}, ColumnToImage{});
                }
                else if(data_type == DataType::F16_F16)
                {
                    return profile(I1, GNWC{}, F16{}, F16{}, ColumnToImage{});
                }
                else if(data_type == DataType::BF16_BF16)
                {
                    return profile(I1, GNWC{}, BF16{}, BF16{}, ColumnToImage{});
                }
                else if(data_type == DataType::INT8_INT8)
                {
                    return profile(I1, GNWC{}, INT8{}, INT8{}, ColumnToImage{});
                }
            }
            else if(num_dim_spatial == 2)
            {
                if(data_type == DataType::F32_F32)
                {
                    return profile(I2, GNHWC{}, F32{}, F32{}, ColumnToImage{});
                }
                else if(data_type == DataType::F16_F16)
                {
                    return profile(I2, GNHWC{}, F16{}, F16{}, ColumnToImage{});
                }
                else if(data_type == DataType::BF16_BF16)
                {
                    return profile(I2, GNHWC{}, BF16{}, BF16{}, ColumnToImage{});
                }
                else if(data_type == DataType::INT8_INT8)
                {
                    return profile(I2, GNHWC{}, INT8{}, INT8{}, ColumnToImage{});
                }
            }
            else if(num_dim_spatial == 3)
            {
                if(data_type == DataType::F32_F32)
                {
                    return profile(I3, GNDHWC{}, F32{}, F32{}, ColumnToImage{});
                }
                else if(data_type == DataType::F16_F16)
                {
                    return profile(I3, GNDHWC{}, F16{}, F16{}, ColumnToImage{});
                }
                else if(data_type == DataType::BF16_BF16)
                {
                    return profile(I3, GNDHWC{}, BF16{}, BF16{}, ColumnToImage{});
                }
                else if(data_type == DataType::INT8_INT8)
                {
                    return profile(I3, GNDHWC{}, INT8{}, INT8{}, ColumnToImage{});
                }
            }
        }
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
        else if(layout == ConvLayout::NHWGC)
        {
            if(num_dim_spatial == 1)
            {
                if(data_type == DataType::F32_F32)
                {
                    return profile(I1, NWGC{}, F32{}, F32{}, ColumnToImage{});
                }
                else if(data_type == DataType::F16_F16)
                {
                    return profile(I1, NWGC{}, F16{}, F16{}, ColumnToImage{});
                }
                else if(data_type == DataType::BF16_BF16)
                {
                    return profile(I1, NWGC{}, BF16{}, BF16{}, ColumnToImage{});
                }
                else if(data_type == DataType::INT8_INT8)
                {
                    return profile(I1, NWGC{}, INT8{}, INT8{}, ColumnToImage{});
                }
            }
            else if(num_dim_spatial == 2)
            {
                if(data_type == DataType::F32_F32)
                {
                    return profile(I2, NHWGC{}, F32{}, F32{}, ColumnToImage{});
                }
                else if(data_type == DataType::F16_F16)
                {
                    return profile(I2, NHWGC{}, F16{}, F16{}, ColumnToImage{});
                }
                else if(data_type == DataType::BF16_BF16)
                {
                    return profile(I2, NHWGC{}, BF16{}, BF16{}, ColumnToImage{});
                }
                else if(data_type == DataType::INT8_INT8)
                {
                    return profile(I2, NHWGC{}, INT8{}, INT8{}, ColumnToImage{});
                }
            }
            else if(num_dim_spatial == 3)
            {
                if(data_type == DataType::F32_F32)
                {
                    return profile(I3, NDHWGC{}, F32{}, F32{}, ColumnToImage{});
                }
                else if(data_type == DataType::F16_F16)
                {
                    return profile(I3, NDHWGC{}, F16{}, F16{}, ColumnToImage{});
                }
                else if(data_type == DataType::BF16_BF16)
                {
                    return profile(I3, NDHWGC{}, BF16{}, BF16{}, ColumnToImage{});
                }
                else if(data_type == DataType::INT8_INT8)
                {
                    return profile(I3, NDHWGC{}, INT8{}, INT8{}, ColumnToImage{});
                }
            }
        }
364
365
366
367
368
369
370
    }

    std::cout << "this data_type & layout is not implemented" << std::endl;
    return 1;
}

REGISTER_PROFILER_OPERATION(OP_NAME, OP_DESC, profile_conv_tensor_rearrange);