profile_max_pool3d_fwd.cpp 8.87 KB
Newer Older
rocking's avatar
rocking committed
1
// SPDX-License-Identifier: MIT
Illia Silin's avatar
Illia Silin committed
2
// Copyright (c) 2018-2023, Advanced Micro Devices, Inc. All rights reserved.
rocking's avatar
rocking committed
3
4
5
6
7
8
9
10
11
12
13
14
15

#include <iostream>
#include <vector>
#include <unordered_map>

#include "profiler/data_type_enum.hpp"
#include "profiler/profile_pool3d_fwd_impl.hpp"
#include "profiler_operation_registry.hpp"

using ck::index_t;

struct maxPoolFwdArgParser
{
rocking's avatar
rocking committed
16
17
18
19
20
21
    std::unordered_map<std::string, std::vector<int>> long_opts = {{"length", {}},
                                                                   {"wsize", {}},
                                                                   {"wstride", {}},
                                                                   {"wdilation", {}},
                                                                   {"pad1", {}},
                                                                   {"pad2", {}}};
rocking's avatar
rocking committed
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

    bool parse_opt(int argc, char* argv[], const std::string& key, int i)
    {
        if(std::string("--") + key == argv[i])
        {
            int pos = i;
            while(++i < argc && argv[i][0] != '-') {}
            int end = i;
            for(int j = pos + 1; j < end; j++)
            {
                long_opts[key].push_back(std::stoi(argv[j]));
            }
            return true;
        }
        return false;
    }

    void operator()(int argc, char* argv[])
    {
        for(auto& kv : long_opts)
        {
            for(int i = 1; i < argc; i++)
            {
                if(parse_opt(argc, argv, kv.first, i))
                    break;
            }
        }
    }
};

void print_help_max_pool3d_fwd()
{
54
    std::cout << "arg1: data type (0: fp16; 1: fp32; 5: bf16)\n"
rocking's avatar
rocking committed
55
56
57
58
59
60
61
62
              << "arg2: verification (0: no; 1: yes)\n"
              << "arg3: initialization (0: no init; 1: integer value; 2: decimal value)\n"
              << "arg4: print tensor value (0: no; 1: yes)\n"
              << "arg5: time kernel (0=no, 1=yes)\n"
              << "arg6: return index (0=no, 1=yes)\n"
              << "--length: input tensor length for NCDHW(e.g, --length 2 32 30 30 30) \n"
              << "--wsize: window size for ZYX (e.g, --wsize 2 2 2) \n"
              << "--wstride: window stride for DHW (e.g, --wstride 2 2 2) \n"
rocking's avatar
rocking committed
63
              << "--wdilation: window dilation for DHW (e.g, --wdilation 1 1 1) \n"
rocking's avatar
rocking committed
64
65
66
              << "--pad1: left side of padding in DHW (e.g, --pad1 1 1 1) \n"
              << "--pad2: right side of padding in DHW (e.g, --pad2 1 1 1) \n"
              << "eg: ckProfiler max_pool3d_fwd 0 1 2 0 1 0 --length 2 32 30 30 30 --wsize 2 2 2 "
rocking's avatar
rocking committed
67
                 "--wstride 2 2 2 --wdilation 1 1 1 --pad1 1 1 1 --pad2 1 1 1"
rocking's avatar
rocking committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
              << std::endl;
}

int profile_max_pool3d_fwd(int argc, char* argv[])
{
    ck::DataTypeEnum data_type = ck::DataTypeEnum::Half;
    bool do_verification       = true;
    int init_method            = 0;
    bool do_log                = false;
    bool time_kernel           = true;
    bool return_index          = false;

    std::vector<index_t> in_length = {2, 32, 30, 30, 30};
    std::vector<index_t> wsize     = {2, 2, 2};
    std::vector<index_t> wstride   = {2, 2, 2};
rocking's avatar
rocking committed
83
    std::vector<index_t> wdilation = {1, 1, 1};
rocking's avatar
rocking committed
84
85
86
    std::vector<index_t> pad1      = {1, 1, 1};
    std::vector<index_t> pad2      = {1, 1, 1};

rocking's avatar
rocking committed
87
    if(argc != 2 && argc != 34)
rocking's avatar
rocking committed
88
89
90
91
    {
        print_help_max_pool3d_fwd();
        return 0;
    }
rocking's avatar
rocking committed
92
    else if(argc == 34)
rocking's avatar
rocking committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    {
        data_type       = static_cast<ck::DataTypeEnum>(std::stoi(argv[2]));
        do_verification = std::stoi(argv[3]);
        init_method     = std::stoi(argv[4]);
        do_log          = std::stoi(argv[5]);
        time_kernel     = std::stoi(argv[6]);
        return_index    = std::stoi(argv[7]);

        // parse the long options
        maxPoolFwdArgParser arg_parser;
        arg_parser(argc, argv);
        in_length = arg_parser.long_opts["length"];
        wsize     = arg_parser.long_opts["wsize"];
        wstride   = arg_parser.long_opts["wstride"];
rocking's avatar
rocking committed
107
        wdilation = arg_parser.long_opts["wdilation"];
rocking's avatar
rocking committed
108
109
110
111
        pad1      = arg_parser.long_opts["pad1"];
        pad2      = arg_parser.long_opts["pad2"];
    }

112
113
114
115
116
117
118
119
120
#ifdef CK_ENABLE_FP16
    using F16 = ck::half_t;
#endif
#ifdef CK_ENABLE_BF16
    using BF16 = ck::bhalf_t;
#endif
#ifdef CK_ENABLE_FP32
    using F32 = float;
#endif
rocking's avatar
rocking committed
121
122
123
124
    using I32   = int32_t;
    using NDHWC = ck::tensor_layout::convolution::NDHWC;

#if 1
rocking's avatar
rocking committed
125
    constexpr auto ReduceOpId = ck::ReduceTensorOp::MAX;
rocking's avatar
rocking committed
126
127
128
#else
    constexpr auto ReduceOpId = ck::ReduceTensorOp::AVG;
#endif
rocking's avatar
rocking committed
129

130
131
132
133
    if(false)
        ;
#ifdef CK_ENABLE_FP16
    else if(data_type == ck::DataTypeEnum::Half)
rocking's avatar
rocking committed
134
135
    {
        if(return_index)
rocking's avatar
rocking committed
136
137
138
139
140
141
142
143
144
145
146
147
            ck::profiler::
                profile_pool3d_fwd_impl<F16, F16, F16, I32, NDHWC, NDHWC, ReduceOpId, false, true>(
                    do_verification,
                    init_method,
                    do_log,
                    time_kernel,
                    in_length,
                    wsize,
                    wstride,
                    wdilation,
                    pad1,
                    pad2);
rocking's avatar
rocking committed
148
        else
rocking's avatar
rocking committed
149
150
151
152
153
154
155
156
157
158
159
160
            ck::profiler::
                profile_pool3d_fwd_impl<F16, F16, F16, I32, NDHWC, NDHWC, ReduceOpId, false, false>(
                    do_verification,
                    init_method,
                    do_log,
                    time_kernel,
                    in_length,
                    wsize,
                    wstride,
                    wdilation,
                    pad1,
                    pad2);
rocking's avatar
rocking committed
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
#endif
#ifdef CK_ENABLE_BF16
    else if(data_type == ck::DataTypeEnum::BFloat16)
    {
        if(return_index)
            ck::profiler::profile_pool3d_fwd_impl<BF16,
                                                  BF16,
                                                  BF16,
                                                  I32,
                                                  NDHWC,
                                                  NDHWC,
                                                  ReduceOpId,
                                                  false,
                                                  true>(do_verification,
                                                        init_method,
                                                        do_log,
                                                        time_kernel,
                                                        in_length,
                                                        wsize,
                                                        wstride,
                                                        wdilation,
                                                        pad1,
                                                        pad2);
        else
            ck::profiler::profile_pool3d_fwd_impl<BF16,
                                                  BF16,
                                                  BF16,
                                                  I32,
                                                  NDHWC,
                                                  NDHWC,
                                                  ReduceOpId,
                                                  false,
                                                  false>(do_verification,
                                                         init_method,
                                                         do_log,
                                                         time_kernel,
                                                         in_length,
                                                         wsize,
                                                         wstride,
                                                         wdilation,
                                                         pad1,
                                                         pad2);
    }
#endif
#ifdef CK_ENABLE_FP32
rocking's avatar
rocking committed
207
208
209
    else if(data_type == ck::DataTypeEnum::Float)
    {
        if(return_index)
rocking's avatar
rocking committed
210
211
212
213
214
215
216
217
218
219
220
221
            ck::profiler::
                profile_pool3d_fwd_impl<F32, F32, F32, I32, NDHWC, NDHWC, ReduceOpId, false, true>(
                    do_verification,
                    init_method,
                    do_log,
                    time_kernel,
                    in_length,
                    wsize,
                    wstride,
                    wdilation,
                    pad1,
                    pad2);
rocking's avatar
rocking committed
222
        else
rocking's avatar
rocking committed
223
224
225
226
227
228
229
230
231
232
233
234
            ck::profiler::
                profile_pool3d_fwd_impl<F32, F32, F32, I32, NDHWC, NDHWC, ReduceOpId, false, false>(
                    do_verification,
                    init_method,
                    do_log,
                    time_kernel,
                    in_length,
                    wsize,
                    wstride,
                    wdilation,
                    pad1,
                    pad2);
rocking's avatar
rocking committed
235
    }
236
#endif
rocking's avatar
rocking committed
237
238
239
240
241
242
243
244
245
    else
    {
        throw std::runtime_error("not implemented yet");
    }

    return 0;
}

REGISTER_PROFILER_OPERATION("max_pool3d_fwd", "max_pool3d fwd", profile_max_pool3d_fwd);