profile_softmax.cpp 12.7 KB
Newer Older
1
// SPDX-License-Identifier: MIT
Illia Silin's avatar
Illia Silin committed
2
// Copyright (c) 2018-2023, Advanced Micro Devices, Inc. All rights reserved.
3
4
5
6
7

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

8
9
#include "profiler/profile_softmax_impl.hpp"
#include "profiler_operation_registry.hpp"
10
11

using ck::index_t;
12
using ck::profiler::SoftmaxDataType;
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

struct ArgParser
{
    std::unordered_map<std::string, std::vector<int>> long_opts = {
        {"length", {}}, {"stride", {}}, {"reduce", {}}, {"alpha", {}}, {"beta", {}}};

    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()
{
50
    std::cout << "arg1: tensor operation (softmax)\n"
51
52
53
54
55
56
57
58
59
60
61
62
63
              << "arg2: data type (0: fp32; 1: fp16; 2: bf16; 3: int8)\n"
              << "arg3: verification (0: no; 1: yes)\n"
              << "arg4: initialization (0: no init; 1: integer value; 2: decimal value)\n"
              << "arg5: print tensor value (0: no; 1: yes)\n"
              << "arg6: time kernel (0=n0, 1=yes)\n"
              << "--length: tensor extents (e.g, --length 8 4 256) \n"
              << "--stride: tensor strides (e.g, --stride 1024 256 1)\n"
              << "--reduce: to-reduce dimensions (e.g, --reduce 2)\n"
              << "--alpha: alpha scaling value\n"
              << "--beta: beta scaling value\n"
              << std::endl;
}

64
int profile_softmax(int argc, char* argv[])
65
66
67
68
69
70
71
72
73
74
{
    if(argc <= 2)
    {
        print_help();
        return 0;
    }

    ArgParser arg_parser;

    // short unnamed options
75
76
77
78
79
    const SoftmaxDataType data_type = static_cast<SoftmaxDataType>(std::stoi(argv[2]));
    const bool do_verification      = std::stoi(argv[3]);
    const int init_method           = std::stoi(argv[4]);
    const bool do_log               = std::stoi(argv[5]);
    const bool time_kernel          = std::stoi(argv[6]);
80
81
82
83
84
85
86
87
88
89

    // parse the long options
    arg_parser(argc, argv);
    const std::vector<index_t> length = arg_parser.long_opts["length"];
    const std::vector<index_t> stride = arg_parser.long_opts["stride"];
    const std::vector<index_t> reduce = arg_parser.long_opts["reduce"];
    const index_t alpha =
        arg_parser.long_opts["alpha"].empty() ? 1 : arg_parser.long_opts["alpha"][0];
    const index_t beta = arg_parser.long_opts["beta"].empty() ? 0 : arg_parser.long_opts["beta"][0];

90
    // Rank 3
Adam Osewski's avatar
Adam Osewski committed
91
    if(length.size() == 3)
92
    {
93
        if(data_type == SoftmaxDataType::F16_F16)
Adam Osewski's avatar
Adam Osewski committed
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
            if(reduce.size() == 1)
                ck::profiler::profile_softmax_impl<ck::half_t, float, ck::half_t, 3, 1>(
                    do_verification,
                    init_method,
                    do_log,
                    time_kernel,
                    length,
                    stride,
                    reduce,
                    double(alpha),
                    double(beta));
            else if(reduce.size() == 2)
                ck::profiler::profile_softmax_impl<ck::half_t, float, ck::half_t, 3, 2>(
                    do_verification,
                    init_method,
                    do_log,
                    time_kernel,
                    length,
                    stride,
                    reduce,
                    double(alpha),
                    double(beta));
            else if(reduce.size() == 3)
                ck::profiler::profile_softmax_impl<ck::half_t, float, ck::half_t, 3, 3>(
                    do_verification,
                    init_method,
                    do_log,
                    time_kernel,
                    length,
                    stride,
                    reduce,
                    double(alpha),
                    double(beta));
            else
                throw std::runtime_error("invalid number of dimensions to reduce");
Adam Osewski's avatar
Adam Osewski committed
130
        }
131
        else if(data_type == SoftmaxDataType::F32_F32)
Adam Osewski's avatar
Adam Osewski committed
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
            if(reduce.size() == 1)
                ck::profiler::profile_softmax_impl<float, float, float, 3, 1>(do_verification,
                                                                              init_method,
                                                                              do_log,
                                                                              time_kernel,
                                                                              length,
                                                                              stride,
                                                                              reduce,
                                                                              double(alpha),
                                                                              double(beta));
            else if(reduce.size() == 2)
                ck::profiler::profile_softmax_impl<float, float, float, 3, 2>(do_verification,
                                                                              init_method,
                                                                              do_log,
                                                                              time_kernel,
                                                                              length,
                                                                              stride,
                                                                              reduce,
                                                                              double(alpha),
                                                                              double(beta));
            else if(reduce.size() == 3)
                ck::profiler::profile_softmax_impl<float, float, float, 3, 3>(do_verification,
                                                                              init_method,
                                                                              do_log,
                                                                              time_kernel,
                                                                              length,
                                                                              stride,
                                                                              reduce,
                                                                              double(alpha),
                                                                              double(beta));
            else
                throw std::runtime_error("invalid number of dimensions to reduce");
Adam Osewski's avatar
Adam Osewski committed
165
166
167
168
169
        }
        else
        {
            throw std::runtime_error("not implemented yet");
        }
170
    }
171
    // Rank 4
Adam Osewski's avatar
Adam Osewski committed
172
    else if(length.size() == 4)
173
    {
174
        if(data_type == SoftmaxDataType::F16_F16)
Adam Osewski's avatar
Adam Osewski committed
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
            if(reduce.size() == 1)
                ck::profiler::profile_softmax_impl<ck::half_t, float, ck::half_t, 4, 1>(
                    do_verification,
                    init_method,
                    do_log,
                    time_kernel,
                    length,
                    stride,
                    reduce,
                    double(alpha),
                    double(beta));
            else if(reduce.size() == 2)
                ck::profiler::profile_softmax_impl<ck::half_t, float, ck::half_t, 4, 2>(
                    do_verification,
                    init_method,
                    do_log,
                    time_kernel,
                    length,
                    stride,
                    reduce,
                    double(alpha),
                    double(beta));
            else if(reduce.size() == 3)
                ck::profiler::profile_softmax_impl<ck::half_t, float, ck::half_t, 4, 3>(
                    do_verification,
                    init_method,
                    do_log,
                    time_kernel,
                    length,
                    stride,
                    reduce,
                    double(alpha),
                    double(beta));
            else if(reduce.size() == 4)
                ck::profiler::profile_softmax_impl<ck::half_t, float, ck::half_t, 4, 4>(
                    do_verification,
                    init_method,
                    do_log,
                    time_kernel,
                    length,
                    stride,
                    reduce,
                    double(alpha),
                    double(beta));
            else
                throw std::runtime_error("invalid number of dimensions to reduce");
Adam Osewski's avatar
Adam Osewski committed
222
        }
223
        else if(data_type == SoftmaxDataType::F32_F32)
Adam Osewski's avatar
Adam Osewski committed
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
            if(reduce.size() == 1)
                ck::profiler::profile_softmax_impl<float, float, float, 4, 1>(do_verification,
                                                                              init_method,
                                                                              do_log,
                                                                              time_kernel,
                                                                              length,
                                                                              stride,
                                                                              reduce,
                                                                              double(alpha),
                                                                              double(beta));
            else if(reduce.size() == 2)
                ck::profiler::profile_softmax_impl<float, float, float, 4, 2>(do_verification,
                                                                              init_method,
                                                                              do_log,
                                                                              time_kernel,
                                                                              length,
                                                                              stride,
                                                                              reduce,
                                                                              double(alpha),
                                                                              double(beta));
            else if(reduce.size() == 3)
                ck::profiler::profile_softmax_impl<float, float, float, 4, 3>(do_verification,
                                                                              init_method,
                                                                              do_log,
                                                                              time_kernel,
                                                                              length,
                                                                              stride,
                                                                              reduce,
                                                                              double(alpha),
                                                                              double(beta));
            else if(reduce.size() == 4)
                ck::profiler::profile_softmax_impl<float, float, float, 4, 4>(do_verification,
                                                                              init_method,
                                                                              do_log,
                                                                              time_kernel,
                                                                              length,
                                                                              stride,
                                                                              reduce,
                                                                              double(alpha),
                                                                              double(beta));
            else
                throw std::runtime_error("invalid number of dimensions to reduce");
Adam Osewski's avatar
Adam Osewski committed
267
268
269
270
271
        }
        else
        {
            throw std::runtime_error("not implemented yet");
        }
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
    }
    else
    {
        throw std::runtime_error("not implemented yet");
    }

    return 0;
}

// hijack main() for quick debugging
// int main(int argc, char* argv[])
// {
//     profile_normalization(argc, argv);
//     return 0;
// }
287
288

REGISTER_PROFILER_OPERATION("softmax", "Softmax", profile_softmax);