profile_grouped_conv_fwd.cpp 13.2 KB
Newer Older
1
// SPDX-License-Identifier: MIT
2
// Copyright (c) 2018-2024, Advanced Micro Devices, Inc. All rights reserved.
3
4
5
6
7
8

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

9
10
#include "profiler/profile_grouped_conv_fwd_impl.hpp"
#include "profiler_operation_registry.hpp"
11
12
13
14
15
16

namespace {

enum struct ConvLayout
{
    GNHWC_GKYXC_GNHWK, // 0
Chao Liu's avatar
Chao Liu committed
17
    NHWGC_GKYXC_NHWGK, // 1
18
    NGCHW_GKYXC_NGKHW, // 2
19
20
21
22
23
24
25
26
};

enum struct ConvDataType
{
    F32_F32_F32,    // 0
    F16_F16_F16,    // 1
    BF16_BF16_BF16, // 2
    INT8_INT8_INT8, // 3
27
    F8_F8_F8,       // 4
28
    BF8_BF8_F8,     // 5
29
    F8_BF8_F8,      // 6
30
    BF8_F8_F8,      // 7
31
32
};

33
34
35
36
37
38
enum struct IndexType
{
    INDEX_T,      // 0
    LONG_INDEX_T, // 1
};

39
40
41
#define OP_NAME "grouped_conv_fwd"
#define OP_DESC "Grouped Convolution Forward"

42
43
44
45
static void print_helper_msg()
{
    std::cout
        // clang-format off
46
        << "arg1: tensor operation (" OP_NAME ": " OP_DESC ")\n"
47
48
49
        << "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"
50
        << "                 3: Input int8, Weight int8, Output int8\n"
51
        << "                 4: Input fp8, Weight fp8, Output fp8\n"
52
        << "                 5: Input bf8, Weight bf8, Output fp8\n"
53
54
        << "                 6: Input fp8, Weight bf8, Output fp8\n"
        << "                 7: Input bf8, Weight fp8, Output fp8)\n"
55
56
        << "arg3: indexing data type (0: 32-bit, 1: 64-bit)\n"
        << "arg4: tensor layout (0: Input[G, N, Hi, Wi, C], Weight[G, K, Y, X, C], Output[G, N, Ho, Wo, K]\n"
Chao Liu's avatar
Chao Liu committed
57
        << "                     1: Input[N, Hi, Wi, G, C], Weight[G, K, Y, X, C], Output[N, Ho, Wo, G, K])\n"
58
59
        << "                     2: Input[N, G, C, Hi, Wi], Weight[G, K, Y, X, C], Output[N, "
            "G, K, Ho, Wo]\n"
60
61
62
63
        << "arg5: verification (0: no, 1: yes)\n"
        << "arg6: initialization (0: no init, 1: integer value, 2: decimal value)\n"
        << "arg7: print tensor value (0: no; 1: yes)\n"
        << "arg8: time kernel (0: no, 1: yes)\n"
64
65
66
67
68
69
70
71
72
        << ck::utils::conv::get_conv_param_parser_helper_msg() << std::endl;
    // clang-format on
}

} // namespace

int profile_grouped_conv_fwd(int argc, char* argv[])
{
    // 8 for control, 1 for num_dim_spatial
73
    if(argc < 10)
74
75
76
77
78
79
80
    {
        print_helper_msg();
        return 1;
    }

    const auto data_type       = static_cast<ConvDataType>(std::stoi(argv[2]));
    const auto layout          = static_cast<ConvLayout>(std::stoi(argv[3]));
81
82
83
84
85
86
87
88
89
    const auto index_type      = static_cast<IndexType>(std::stoi(argv[4]));
    const bool do_verification = std::stoi(argv[5]);
    const int init_method      = std::stoi(argv[6]);
    const bool do_log          = std::stoi(argv[7]);
    const bool time_kernel     = 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)
90
91
92
93
94
    {
        print_helper_msg();
        return 1;
    }

95
    const auto params = ck::utils::conv::parse_conv_param(num_dim_spatial, 10, argv);
96
97
98
99
100

    using F32  = float;
    using F16  = ck::half_t;
    using BF16 = ck::bhalf_t;
    using INT8 = int8_t;
101
    using F8   = ck::f8_t;
102
    using BF8  = ck::bf8_t;
103
104
105
106
107
108
109
110
111
112
113
114
115
116

    //
    using GNWC   = ck::tensor_layout::convolution::GNWC;
    using GNHWC  = ck::tensor_layout::convolution::GNHWC;
    using GNDHWC = ck::tensor_layout::convolution::GNDHWC;

    using GKXC   = ck::tensor_layout::convolution::GKXC;
    using GKYXC  = ck::tensor_layout::convolution::GKYXC;
    using GKZYXC = ck::tensor_layout::convolution::GKZYXC;

    using GNWK   = ck::tensor_layout::convolution::GNWK;
    using GNHWK  = ck::tensor_layout::convolution::GNHWK;
    using GNDHWK = ck::tensor_layout::convolution::GNDHWK;

117
118
119
120
121
    //
    using NGCHW = ck::tensor_layout::convolution::NGCHW;

    using NGKHW = ck::tensor_layout::convolution::NGKHW;

122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
    //
    using NWGC   = ck::tensor_layout::convolution::NWGC;
    using NHWGC  = ck::tensor_layout::convolution::NHWGC;
    using NDHWGC = ck::tensor_layout::convolution::NDHWGC;

    using NWGK   = ck::tensor_layout::convolution::NWGK;
    using NHWGK  = ck::tensor_layout::convolution::NHWGK;
    using NDHWGK = ck::tensor_layout::convolution::NDHWGK;

    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 wei_layout,
                       auto out_layout,
                       auto in_type,
                       auto wei_type,
141
142
143
                       auto out_type,
                       auto a_compute_type,
                       auto b_compute_type) {
144
145
146
147
148
149
150
151
152
153
        constexpr ck::index_t NDimSpatial = num_dim_spatial_tmp.value;

        using InLayout  = decltype(in_layout);
        using WeiLayout = decltype(wei_layout);
        using OutLayout = decltype(out_layout);

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

154
155
156
        using AComputeType = decltype(a_compute_type);
        using BComputeType = decltype(b_compute_type);

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
        if(index_type == IndexType::INDEX_T)
        {
            bool pass = ck::profiler::profile_grouped_conv_fwd_impl<NDimSpatial,
                                                                    InLayout,
                                                                    WeiLayout,
                                                                    OutLayout,
                                                                    InDataType,
                                                                    WeiDataType,
                                                                    OutDataType,
                                                                    AComputeType,
                                                                    BComputeType,
                                                                    ck::index_t>(
                do_verification, init_method, do_log, time_kernel, params);

            return pass ? 0 : 1;
        }
        else if(index_type == IndexType::LONG_INDEX_T)
        {
            bool pass = ck::profiler::profile_grouped_conv_fwd_impl<NDimSpatial,
                                                                    InLayout,
                                                                    WeiLayout,
                                                                    OutLayout,
                                                                    InDataType,
                                                                    WeiDataType,
                                                                    OutDataType,
                                                                    AComputeType,
                                                                    BComputeType,
                                                                    ck::long_index_t>(
                do_verification, init_method, do_log, time_kernel, params);

            return pass ? 0 : 1;
        }
        else
        {
            std::cout << "this indexing data type is not implemented" << std::endl;
            return 1;
        }
194
195
196
197
198
199
200
    };

    // GNHWC_GKYXC_GNHWK
    if(num_dim_spatial == 1 && layout == ConvLayout::GNHWC_GKYXC_GNHWK)
    {
        if(data_type == ConvDataType::F32_F32_F32)
        {
201
            return profile(I1, GNWC{}, GKXC{}, GNWK{}, F32{}, F32{}, F32{}, F32{}, F32{});
202
203
204
        }
        else if(data_type == ConvDataType::F16_F16_F16)
        {
205
            return profile(I1, GNWC{}, GKXC{}, GNWK{}, F16{}, F16{}, F16{}, F16{}, F16{});
206
207
208
        }
        else if(data_type == ConvDataType::BF16_BF16_BF16)
        {
209
            return profile(I1, GNWC{}, GKXC{}, GNWK{}, BF16{}, BF16{}, BF16{}, BF16{}, BF16{});
210
211
212
        }
        else if(data_type == ConvDataType::INT8_INT8_INT8)
        {
213
            return profile(I1, GNWC{}, GKXC{}, GNWK{}, INT8{}, INT8{}, INT8{}, INT8{}, INT8{});
214
215
216
217
218
219
        }
    }
    else if(num_dim_spatial == 2 && layout == ConvLayout::GNHWC_GKYXC_GNHWK)
    {
        if(data_type == ConvDataType::F32_F32_F32)
        {
220
            return profile(I2, GNHWC{}, GKYXC{}, GNHWK{}, F32{}, F32{}, F32{}, F32{}, F32{});
221
222
223
        }
        else if(data_type == ConvDataType::F16_F16_F16)
        {
224
            return profile(I2, GNHWC{}, GKYXC{}, GNHWK{}, F16{}, F16{}, F16{}, F16{}, F16{});
225
226
227
        }
        else if(data_type == ConvDataType::BF16_BF16_BF16)
        {
228
            return profile(I2, GNHWC{}, GKYXC{}, GNHWK{}, BF16{}, BF16{}, BF16{}, BF16{}, BF16{});
229
230
231
        }
        else if(data_type == ConvDataType::INT8_INT8_INT8)
        {
232
            return profile(I2, GNHWC{}, GKYXC{}, GNHWK{}, INT8{}, INT8{}, INT8{}, INT8{}, INT8{});
233
234
235
236
237
238
        }
    }
    else if(num_dim_spatial == 3 && layout == ConvLayout::GNHWC_GKYXC_GNHWK)
    {
        if(data_type == ConvDataType::F32_F32_F32)
        {
239
            return profile(I3, GNDHWC{}, GKZYXC{}, GNDHWK{}, F32{}, F32{}, F32{}, F32{}, F32{});
240
241
242
        }
        else if(data_type == ConvDataType::F16_F16_F16)
        {
243
            return profile(I3, GNDHWC{}, GKZYXC{}, GNDHWK{}, F16{}, F16{}, F16{}, F16{}, F16{});
244
245
246
        }
        else if(data_type == ConvDataType::BF16_BF16_BF16)
        {
247
248
            return profile(
                I3, GNDHWC{}, GKZYXC{}, GNDHWK{}, BF16{}, BF16{}, BF16{}, BF16{}, BF16{});
249
250
251
        }
        else if(data_type == ConvDataType::INT8_INT8_INT8)
        {
252
253
            return profile(
                I3, GNDHWC{}, GKZYXC{}, GNDHWK{}, INT8{}, INT8{}, INT8{}, INT8{}, INT8{});
254
255
        }
    }
Chao Liu's avatar
Chao Liu committed
256
257
    // NHWGC_GKYXC_NHWGK
    else if(num_dim_spatial == 1 && layout == ConvLayout::NHWGC_GKYXC_NHWGK)
258
259
260
    {
        if(data_type == ConvDataType::F32_F32_F32)
        {
261
            return profile(I1, NWGC{}, GKXC{}, NWGK{}, F32{}, F32{}, F32{}, F32{}, F32{});
262
263
264
        }
        else if(data_type == ConvDataType::F16_F16_F16)
        {
265
            return profile(I1, NWGC{}, GKXC{}, NWGK{}, F16{}, F16{}, F16{}, F16{}, F16{});
266
267
268
        }
        else if(data_type == ConvDataType::BF16_BF16_BF16)
        {
269
            return profile(I1, NWGC{}, GKXC{}, NWGK{}, BF16{}, BF16{}, BF16{}, BF16{}, BF16{});
270
271
272
        }
        else if(data_type == ConvDataType::INT8_INT8_INT8)
        {
273
            return profile(I1, NWGC{}, GKXC{}, NWGK{}, INT8{}, INT8{}, INT8{}, INT8{}, INT8{});
274
275
        }
    }
Chao Liu's avatar
Chao Liu committed
276
    else if(num_dim_spatial == 2 && layout == ConvLayout::NHWGC_GKYXC_NHWGK)
277
278
279
    {
        if(data_type == ConvDataType::F32_F32_F32)
        {
280
            return profile(I2, NHWGC{}, GKYXC{}, NHWGK{}, F32{}, F32{}, F32{}, F32{}, F32{});
281
282
283
        }
        else if(data_type == ConvDataType::F16_F16_F16)
        {
284
            return profile(I2, NHWGC{}, GKYXC{}, NHWGK{}, F16{}, F16{}, F16{}, F16{}, F16{});
285
286
287
        }
        else if(data_type == ConvDataType::BF16_BF16_BF16)
        {
288
            return profile(I2, NHWGC{}, GKYXC{}, NHWGK{}, BF16{}, BF16{}, BF16{}, BF16{}, BF16{});
289
290
291
        }
        else if(data_type == ConvDataType::INT8_INT8_INT8)
        {
292
            return profile(I2, NHWGC{}, GKYXC{}, NHWGK{}, INT8{}, INT8{}, INT8{}, INT8{}, INT8{});
293
294
        }
    }
295
296
297
298
299
300
301
302
303
304
305
    else if(num_dim_spatial == 2 && layout == ConvLayout::NGCHW_GKYXC_NGKHW)
    {
        if(data_type == ConvDataType::F32_F32_F32)
        {
            return profile(I2, NGCHW{}, GKYXC{}, NGKHW{}, F32{}, F32{}, F32{}, F32{}, F32{});
        }
        else if(data_type == ConvDataType::F16_F16_F16)
        {
            return profile(I2, NGCHW{}, GKYXC{}, NGKHW{}, F16{}, F16{}, F16{}, F16{}, F16{});
        }
    }
Chao Liu's avatar
Chao Liu committed
306
    else if(num_dim_spatial == 3 && layout == ConvLayout::NHWGC_GKYXC_NHWGK)
307
308
309
    {
        if(data_type == ConvDataType::F32_F32_F32)
        {
310
            return profile(I3, NDHWGC{}, GKZYXC{}, NDHWGK{}, F32{}, F32{}, F32{}, F32{}, F32{});
311
312
313
        }
        else if(data_type == ConvDataType::F16_F16_F16)
        {
314
            return profile(I3, NDHWGC{}, GKZYXC{}, NDHWGK{}, F16{}, F16{}, F16{}, F16{}, F16{});
315
316
317
        }
        else if(data_type == ConvDataType::BF16_BF16_BF16)
        {
318
319
            return profile(
                I3, NDHWGC{}, GKZYXC{}, NDHWGK{}, BF16{}, BF16{}, BF16{}, BF16{}, BF16{});
320
321
322
        }
        else if(data_type == ConvDataType::INT8_INT8_INT8)
        {
323
324
            return profile(
                I3, NDHWGC{}, GKZYXC{}, NDHWGK{}, INT8{}, INT8{}, INT8{}, INT8{}, INT8{});
325
        }
326
327
        else if(data_type == ConvDataType::F8_F8_F8)
        {
328
            return profile(I3, NDHWGC{}, GKZYXC{}, NDHWGK{}, F8{}, F8{}, F8{}, F8{}, F8{});
329
        }
330
331
        else if(data_type == ConvDataType::BF8_BF8_F8)
        {
332
333
334
335
336
            return profile(I3, NDHWGC{}, GKZYXC{}, NDHWGK{}, BF8{}, BF8{}, F8{}, BF8{}, BF8{});
        }
        else if(data_type == ConvDataType::F8_BF8_F8)
        {
            return profile(I3, NDHWGC{}, GKZYXC{}, NDHWGK{}, F8{}, BF8{}, F8{}, F8{}, BF8{});
337
        }
338
339
340
341
        else if(data_type == ConvDataType::BF8_F8_F8)
        {
            return profile(I3, NDHWGC{}, GKZYXC{}, NDHWGK{}, BF8{}, F8{}, F8{}, BF8{}, F8{});
        }
342
343
344
345
346
347
    }

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

    return 1;
}
348
349

REGISTER_PROFILER_OPERATION(OP_NAME, OP_DESC, profile_grouped_conv_fwd);