profile_grouped_conv_bwd_data.cpp 6.44 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
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
// SPDX-License-Identifier: MIT
// Copyright (c) 2018-2023, Advanced Micro Devices, Inc. All rights reserved.

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

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

namespace {

enum struct ConvLayout
{
    GNHWC_GKYXC_GNHWK, // 0
    NHWGC_GKYXC_NHWGK, // 1
};

enum struct ConvDataType
{
    F32_F32_F32,    // 0
    F16_F16_F16,    // 1
    BF16_BF16_BF16, // 2
};

#define OP_NAME "grouped_conv_bwd_data"
#define OP_DESC "Grouped Convolution Backward Data"

static void print_helper_msg()
{
    std::cout
        // clang-format off
        << "arg1: tensor operation (" OP_NAME ": " OP_DESC ")\n"
        << "arg2: data type (0: Output fp32, Weight fp32, Input fp32\n"
        << "                 1: Output fp16, Weight fp16, Input fp16\n"
        << "                 2: Output bf16, Weight bf16, Input bf16\n"
        << "arg3: tensor layout (0: Output[G, N, Hi, Wi, C], Weight[G, K, Y, X, C], Input[G, N, Ho, Wo, K]\n"
        << "                     1: Output[N, Hi, Wi, G, C], Weight[G, K, Y, X, C], Input[N, Ho, Wo, G, K])\n"
        << "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"
        << ck::utils::conv::get_conv_param_parser_helper_msg() << std::endl;
    // clang-format on
}

} // namespace

int profile_grouped_conv_bwd_data(int argc, char* argv[])
{
    // 8 for control, 1 for num_dim_spatial
    if(argc < 9)
    {
        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]));
    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 int num_dim_spatial  = std::stoi(argv[8]);

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

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

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

80
    using namespace ck::tensor_layout::convolution;
81
82

    constexpr auto I2 = ck::Number<2>{};
83
    constexpr auto I3 = ck::Number<3>{};
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

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

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

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

        bool pass = ck::profiler::profile_grouped_conv_bwd_data_impl<NDimSpatial,
                                                                     OutLayout,
                                                                     WeiLayout,
                                                                     InLayout,
                                                                     OutDataType,
                                                                     WeiDataType,
                                                                     InDataType>(
            do_verification, init_method, do_log, time_kernel, params);

        return pass ? 0 : 1;
    };

114
    if(num_dim_spatial == 2)
115
    {
116
        if(layout == ConvLayout::GNHWC_GKYXC_GNHWK)
117
        {
118
119
120
121
122
123
124
125
126
127
128
129
            if(data_type == ConvDataType::F32_F32_F32)
            {
                return profile(I2, GNHWK{}, GKYXC{}, GNHWC{}, F32{}, F32{}, F32{});
            }
            else if(data_type == ConvDataType::F16_F16_F16)
            {
                return profile(I2, GNHWK{}, GKYXC{}, GNHWC{}, F16{}, F16{}, F16{});
            }
            else if(data_type == ConvDataType::BF16_BF16_BF16)
            {
                return profile(I2, GNHWK{}, GKYXC{}, GNHWC{}, BF16{}, BF16{}, BF16{});
            }
130
        }
131
        else if(layout == ConvLayout::NHWGC_GKYXC_NHWGK)
132
        {
133
134
135
136
137
138
139
140
141
142
143
144
            if(data_type == ConvDataType::F32_F32_F32)
            {
                return profile(I2, NHWGK{}, GKYXC{}, NHWGC{}, F32{}, F32{}, F32{});
            }
            else if(data_type == ConvDataType::F16_F16_F16)
            {
                return profile(I2, NHWGK{}, GKYXC{}, NHWGC{}, F16{}, F16{}, F16{});
            }
            else if(data_type == ConvDataType::BF16_BF16_BF16)
            {
                return profile(I2, NHWGK{}, GKYXC{}, NHWGC{}, BF16{}, BF16{}, BF16{});
            }
145
146
        }
    }
147
    else if(num_dim_spatial == 3)
148
    {
149
        if(layout == ConvLayout::GNHWC_GKYXC_GNHWK)
150
        {
151
152
153
154
155
156
157
158
159
160
161
162
            if(data_type == ConvDataType::F32_F32_F32)
            {
                return profile(I3, GNDHWK{}, GKZYXC{}, GNDHWC{}, F32{}, F32{}, F32{});
            }
            else if(data_type == ConvDataType::F16_F16_F16)
            {
                return profile(I3, GNDHWK{}, GKZYXC{}, GNDHWC{}, F16{}, F16{}, F16{});
            }
            else if(data_type == ConvDataType::BF16_BF16_BF16)
            {
                return profile(I3, GNDHWK{}, GKZYXC{}, GNDHWC{}, BF16{}, BF16{}, BF16{});
            }
163
        }
164
        else if(layout == ConvLayout::NHWGC_GKYXC_NHWGK)
165
        {
166
167
168
169
170
171
172
173
174
175
176
177
            if(data_type == ConvDataType::F32_F32_F32)
            {
                return profile(I3, NDHWGK{}, GKZYXC{}, NDHWGC{}, F32{}, F32{}, F32{});
            }
            else if(data_type == ConvDataType::F16_F16_F16)
            {
                return profile(I3, NDHWGK{}, GKZYXC{}, NDHWGC{}, F16{}, F16{}, F16{});
            }
            else if(data_type == ConvDataType::BF16_BF16_BF16)
            {
                return profile(I3, NDHWGK{}, GKZYXC{}, NDHWGC{}, BF16{}, BF16{}, BF16{});
            }
178
179
180
181
182
183
184
185
186
        }
    }

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

    return 1;
}

REGISTER_PROFILER_OPERATION(OP_NAME, OP_DESC, profile_grouped_conv_bwd_data);