profile_reduce.cpp 20.2 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
#include <iostream>
#include <fstream>
#include <numeric>
#include <initializer_list>
#include <cstdlib>
#include <vector>
#include <stdexcept>
#include <sstream>
#include <getopt.h>

#include "config.hpp"
#include "print.hpp"
#include "device.hpp"
#include "host_tensor.hpp"
#include "host_tensor_generator.hpp"
#include "device_tensor.hpp"
#include "reduction_enums.hpp"

#include "profile_reduce_impl.hpp"

using namespace std;

using ck::NanPropagation_t;
using ck::ReduceTensorIndices_t;
using ck::ReduceTensorOp_t;

static struct option long_options[] = {{"inLengths", required_argument, nullptr, 'D'},
Qianfeng's avatar
Qianfeng committed
28
                                       {"reduceDims", required_argument, nullptr, 'R'},
29
30
31
32
33
34
35
36
                                       {"reduceOp", required_argument, nullptr, 'O'},
                                       {"compType", required_argument, nullptr, 'C'},
                                       {"outType", required_argument, nullptr, 'W'},
                                       {"nanOpt", required_argument, nullptr, 'N'},
                                       {"indicesOpt", required_argument, nullptr, 'I'},
                                       {"scales", required_argument, nullptr, 'S'},
                                       {"half", no_argument, nullptr, '?'},
                                       {"double", no_argument, nullptr, '?'},
37
38
                                       {"int8", no_argument, nullptr, '?'},
                                       {"bf16", no_argument, nullptr, '?'},
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
80
81
82
83
84
85
86
                                       {"dumpout", required_argument, nullptr, 'o'},
                                       {"verify", required_argument, nullptr, 'v'},
                                       {"log", required_argument, nullptr, 'l'},
                                       {"help", no_argument, nullptr, '?'},
                                       {nullptr, 0, nullptr, 0}};

template <typename T>
static T getSingleValueFromString(const string& valueStr)
{
    std::istringstream iss(valueStr);

    T val;

    iss >> val;

    return (val);
};

template <typename T>
static std::vector<T> getTypeValuesFromString(const char* cstr_values)
{
    std::string valuesStr(cstr_values);

    std::vector<T> values;
    std::size_t pos = 0;
    std::size_t new_pos;

    new_pos = valuesStr.find(',', pos);
    while(new_pos != std::string::npos)
    {
        const std::string sliceStr = valuesStr.substr(pos, new_pos - pos);

        T val = getSingleValueFromString<T>(sliceStr);

        values.push_back(val);

        pos     = new_pos + 1;
        new_pos = valuesStr.find(',', pos);
    };

    std::string sliceStr = valuesStr.substr(pos);
    T val                = getSingleValueFromString<T>(sliceStr);

    values.push_back(val);

    return (values);
}

Chao Liu's avatar
Chao Liu committed
87
enum struct appDataType_t
88
89
90
91
92
93
94
95
{
    appHalf     = 0,
    appFloat    = 1,
    appInt32    = 2,
    appInt8     = 3,
    appInt8x4   = 4,
    appBFloat16 = 5,
    appDouble   = 6,
Chao Liu's avatar
Chao Liu committed
96
};
97

Qianfeng's avatar
Qianfeng committed
98
static void check_reduce_dims(const int rank, const std::vector<int>& reduceDims)
99
{
Qianfeng's avatar
Qianfeng committed
100
    for(auto dim : reduceDims)
101
102
103
104
105
106
107
    {
        if(dim < 0 || dim >= rank)
            throw std::runtime_error("Invalid dimension index specified for Reducing");
    };

    unsigned int flag = 0;

Qianfeng's avatar
Qianfeng committed
108
    for(auto dim : reduceDims)
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
    {
        if(flag & (0x1 << dim))
            throw std::runtime_error("All toReduce dimensions should be different!");
        flag = flag | (0x1 << dim);
    };
};

class AppArgs
{
    private:
    int option_index = 0;

    public:
    bool use_half   = false;
    bool use_double = false;
124
125
    bool use_int8   = false;
    bool use_bf16   = false;
126
127
128

    std::vector<size_t> inLengths;
    std::vector<size_t> outLengths;
Qianfeng's avatar
Qianfeng committed
129
    std::vector<int> reduceDims;
130
131
132
133

    std::vector<float> scales;

    ReduceTensorOp_t reduceOp = ReduceTensorOp_t::ADD;
Chao Liu's avatar
Chao Liu committed
134
135
    appDataType_t compTypeId  = appDataType_t::appFloat;
    appDataType_t outTypeId   = appDataType_t::appFloat;
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158

    bool compType_assigned = false;
    bool outType_assigned  = false;

    NanPropagation_t nanOpt          = NanPropagation_t::NOT_PROPAGATE_NAN;
    ReduceTensorIndices_t indicesOpt = ReduceTensorIndices_t::NO_INDICES;
    bool do_log                      = false;
    bool do_verification             = false;
    bool do_dumpout                  = false;

    int init_method;
    int nrepeat;

    bool need_indices = false;

    AppArgs()  = default;
    ~AppArgs() = default;

    void show_usage(const char* cmd)
    {
        std::cout << "Usage of " << cmd << std::endl;
        std::cout << "--inLengths or -D, comma separated list of input tensor dimension lengths"
                  << std::endl;
Qianfeng's avatar
Qianfeng committed
159
        std::cout << "--reduceDims or -R, comma separated list of to-reduce dimensions"
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
                  << std::endl;
        std::cout << "--reduceOp or -O, enum value indicating the reduction operations"
                  << std::endl;
        std::cout << "--compType or -C, enum value indicating the type of accumulated values used "
                     "during the reduction"
                  << std::endl;
        std::cout << "--outType or -W, optional enum value indicating the type of the reduced "
                     "output, which could be float when the input data is half"
                  << std::endl;
        std::cout << "--nanOpt or -N, enum value indicates the selection for NanOpt" << std::endl;
        std::cout << "--indicesOpt or -I, enum value indicates the selection for IndicesOpt"
                  << std::endl;
        std::cout << "--scales or -S, comma separated two float values for alpha and beta"
                  << std::endl;
        std::cout << "--half, use fp16 for the input and output tensor data types" << std::endl;
        std::cout << "--double, use fp64 for the input and output tensor data types" << std::endl;
176
177
        std::cout << "--int8, use int8 for the input and output tensor data types" << std::endl;
        std::cout << "--bf16, use bfloat16 for the input and output tensor data types" << std::endl;
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
        std::cout << "--verify or -v, 1/0 to indicate whether to verify the reduction result by "
                     "comparing with the host-based reduction"
                  << std::endl;
        std::cout << "--dumpout or -o, 1/0 to indicate where to save the reduction result to files "
                     "for further analysis"
                  << std::endl;
        std::cout << "--log or -l, 1/0 to indicate whether to log some information" << std::endl;
    };

    int processArgs(int argc, char* argv[])
    {
        unsigned int ch;

        optind++; // to skip the "reduce" module name

        while(1)
        {
            ch = getopt_long(argc, argv, "D:R:O:C:W:N:I:S:v:o:l:", long_options, &option_index);
            if(ch == -1)
                break;
            switch(ch)
            {
            case 'D':
                if(!optarg)
                    throw std::runtime_error("Invalid option format!");

                inLengths = getTypeValuesFromString<size_t>(optarg);
                break;
            case 'R':
                if(!optarg)
                    throw std::runtime_error("Invalid option format!");

Qianfeng's avatar
Qianfeng committed
210
                reduceDims = getTypeValuesFromString<int>(optarg);
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
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
267
268
269
270
271
272
273
274
275
                break;
            case 'O':
                if(!optarg)
                    throw std::runtime_error("Invalid option format!");

                reduceOp = static_cast<ReduceTensorOp_t>(std::atoi(optarg));
                break;
            case 'C':
                if(!optarg)
                    throw std::runtime_error("Invalid option format!");

                compTypeId        = static_cast<appDataType_t>(std::atoi(optarg));
                compType_assigned = true;
                break;
            case 'W':
                if(!optarg)
                    throw std::runtime_error("Invalid option format!");

                outTypeId        = static_cast<appDataType_t>(std::atoi(optarg));
                outType_assigned = true;
                break;
            case 'N':
                if(!optarg)
                    throw std::runtime_error("Invalid option format!");

                nanOpt = static_cast<NanPropagation_t>(std::atoi(optarg));
                break;
            case 'I':
                if(!optarg)
                    throw std::runtime_error("Invalid option format!");

                indicesOpt = static_cast<ReduceTensorIndices_t>(std::atoi(optarg));
                break;
            case 'S':
                if(!optarg)
                    throw std::runtime_error("Invalid option format!");

                scales = getTypeValuesFromString<float>(optarg);

                if(scales.size() != 2)
                    throw std::runtime_error("Invalid option format!");
                break;
            case 'v':
                if(!optarg)
                    throw std::runtime_error("Invalid option format!");

                do_verification = static_cast<bool>(std::atoi(optarg));
                break;
            case 'o':
                if(!optarg)
                    throw std::runtime_error("Invalid option format!");

                do_dumpout = static_cast<bool>(std::atoi(optarg));
                break;
            case 'l':
                if(!optarg)
                    throw std::runtime_error("Invalid option format!");

                do_log = static_cast<bool>(std::atoi(optarg));
                break;
            case '?':
                if(std::string(long_options[option_index].name) == "half")
                    use_half = true;
                else if(std::string(long_options[option_index].name) == "double")
                    use_double = true;
276
277
278
279
                else if(std::string(long_options[option_index].name) == "int8")
                    use_int8 = true;
                else if(std::string(long_options[option_index].name) == "bf16")
                    use_bf16 = true;
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
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
                else if(std::string(long_options[option_index].name) == "help")
                {
                    show_usage(argv[0]);
                    return (-1);
                };
                break;

            default:
                show_usage(argv[0]);
                std::cerr << "Invalid cmd-line options!" << std::endl;
                return (-1);
            };
        };

        if(optind + 2 > argc)
            throw std::runtime_error("Invalid cmd-line arguments, more argumetns are needed!");

        init_method = std::atoi(argv[optind++]);
        nrepeat     = std::atoi(argv[optind]);

        if(scales.empty())
        {
            scales.push_back(1.0f);
            scales.push_back(0.0f);
        };

        if(reduceOp == ReduceTensorOp_t::MIN || reduceOp == ReduceTensorOp_t::MAX ||
           reduceOp == ReduceTensorOp_t::AMAX)
        {
            if(indicesOpt != ReduceTensorIndices_t::NO_INDICES)
                need_indices = true;

            // for indexable operations, no need to assign compType and outType, just let them be
            // same as inType
            compType_assigned = false;
            outType_assigned  = false;
        };

        return (0);
    };

}; // end of class AppArgs

int profile_reduce(int argc, char* argv[])
{
    using namespace ck::profiler;

    AppArgs args;

    if(args.processArgs(argc, argv) < 0)
        return (-1);

    int rank = args.inLengths.size();

Qianfeng's avatar
Qianfeng committed
334
    check_reduce_dims(rank, args.reduceDims);
335
336
337
338
339
340
341

    if(args.reduceOp == ReduceTensorOp_t::MUL || args.reduceOp == ReduceTensorOp_t::NORM1)
        throw std::runtime_error("MUL and NORM1 are not supported by composable kernel!");

    if(args.use_half)
    {
        if(!args.compType_assigned)
Chao Liu's avatar
Chao Liu committed
342
            args.compTypeId = appDataType_t::appHalf;
343

Chao Liu's avatar
Chao Liu committed
344
345
346
        if(args.outType_assigned &&
           (args.outTypeId != appDataType_t::appHalf && args.outTypeId != appDataType_t::appFloat))
            args.outTypeId = appDataType_t::appFloat;
347
348

        if(!args.outType_assigned)
Chao Liu's avatar
Chao Liu committed
349
            args.outTypeId = appDataType_t::appHalf;
350

Chao Liu's avatar
Chao Liu committed
351
        if(args.compTypeId == appDataType_t::appHalf)
352
353
354
355
356
357
358
        {
            profile_reduce_impl<ck::half_t, ck::half_t, ck::half_t>(args.do_verification,
                                                                    args.init_method,
                                                                    args.do_log,
                                                                    args.do_dumpout,
                                                                    args.nrepeat,
                                                                    args.inLengths,
Qianfeng's avatar
Qianfeng committed
359
                                                                    args.reduceDims,
360
361
362
363
364
365
                                                                    args.reduceOp,
                                                                    args.nanOpt,
                                                                    args.indicesOpt,
                                                                    args.scales[0],
                                                                    args.scales[1]);
        }
Chao Liu's avatar
Chao Liu committed
366
        else if(args.compTypeId == appDataType_t::appFloat)
367
368
369
370
371
372
373
        {
            profile_reduce_impl<ck::half_t, float, ck::half_t>(args.do_verification,
                                                               args.init_method,
                                                               args.do_log,
                                                               args.do_dumpout,
                                                               args.nrepeat,
                                                               args.inLengths,
Qianfeng's avatar
Qianfeng committed
374
                                                               args.reduceDims,
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
                                                               args.reduceOp,
                                                               args.nanOpt,
                                                               args.indicesOpt,
                                                               args.scales[0],
                                                               args.scales[1]);
        }
        else
            throw std::runtime_error("Invalid compType assignment!");
    }
    else if(args.use_double)
    {
        profile_reduce_impl<double, double, double>(args.do_verification,
                                                    args.init_method,
                                                    args.do_log,
                                                    args.do_dumpout,
                                                    args.nrepeat,
                                                    args.inLengths,
Qianfeng's avatar
Qianfeng committed
392
                                                    args.reduceDims,
393
394
395
396
397
398
                                                    args.reduceOp,
                                                    args.nanOpt,
                                                    args.indicesOpt,
                                                    args.scales[0],
                                                    args.scales[1]);
    }
399
400
401
    else if(args.use_int8)
    {
        if(!args.compType_assigned)
Chao Liu's avatar
Chao Liu committed
402
            args.compTypeId = appDataType_t::appInt8;
403

Chao Liu's avatar
Chao Liu committed
404
405
406
        if(args.outType_assigned &&
           (args.outTypeId != appDataType_t::appInt8 && args.outTypeId != appDataType_t::appInt32))
            args.outTypeId = appDataType_t::appInt32;
407
408

        if(!args.outType_assigned)
Chao Liu's avatar
Chao Liu committed
409
            args.outTypeId = appDataType_t::appInt8;
410

Chao Liu's avatar
Chao Liu committed
411
        if(args.compTypeId == appDataType_t::appInt8)
412
413
414
415
416
417
418
419
420
421
422
423
424
425
        {
            profile_reduce_impl<int8_t, int8_t, int8_t>(args.do_verification,
                                                        args.init_method,
                                                        args.do_log,
                                                        args.do_dumpout,
                                                        args.nrepeat,
                                                        args.inLengths,
                                                        args.reduceDims,
                                                        args.reduceOp,
                                                        args.nanOpt,
                                                        args.indicesOpt,
                                                        args.scales[0],
                                                        args.scales[1]);
        }
Chao Liu's avatar
Chao Liu committed
426
        else if(args.compTypeId == appDataType_t::appInt32)
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
        {
            profile_reduce_impl<int8_t, int32_t, int8_t>(args.do_verification,
                                                         args.init_method,
                                                         args.do_log,
                                                         args.do_dumpout,
                                                         args.nrepeat,
                                                         args.inLengths,
                                                         args.reduceDims,
                                                         args.reduceOp,
                                                         args.nanOpt,
                                                         args.indicesOpt,
                                                         args.scales[0],
                                                         args.scales[1]);
        }
        else
            throw std::runtime_error("Invalid compType assignment!");
    }
    else if(args.use_bf16)
    {
Chao Liu's avatar
Chao Liu committed
446
447
448
        if(args.outType_assigned && (args.outTypeId != appDataType_t::appBFloat16 &&
                                     args.outTypeId != appDataType_t::appFloat))
            args.outTypeId = appDataType_t::appFloat;
449
450

        if(!args.outType_assigned)
Chao Liu's avatar
Chao Liu committed
451
            args.outTypeId = appDataType_t::appBFloat16;
452
453
454
455
456
457
458
459
460
461
462
463
464
465

        profile_reduce_impl<ck::bhalf_t, float, ck::bhalf_t>(args.do_verification,
                                                             args.init_method,
                                                             args.do_log,
                                                             args.do_dumpout,
                                                             args.nrepeat,
                                                             args.inLengths,
                                                             args.reduceDims,
                                                             args.reduceOp,
                                                             args.nanOpt,
                                                             args.indicesOpt,
                                                             args.scales[0],
                                                             args.scales[1]);
    }
466
467
    else
    {
Chao Liu's avatar
Chao Liu committed
468
        if(args.compTypeId == appDataType_t::appFloat)
469
470
471
472
473
474
475
        {
            profile_reduce_impl<float, float, float>(args.do_verification,
                                                     args.init_method,
                                                     args.do_log,
                                                     args.do_dumpout,
                                                     args.nrepeat,
                                                     args.inLengths,
Qianfeng's avatar
Qianfeng committed
476
                                                     args.reduceDims,
477
478
479
480
481
482
                                                     args.reduceOp,
                                                     args.nanOpt,
                                                     args.indicesOpt,
                                                     args.scales[0],
                                                     args.scales[1]);
        }
Chao Liu's avatar
Chao Liu committed
483
        else if(args.compTypeId == appDataType_t::appDouble)
484
485
486
487
488
489
490
        {
            profile_reduce_impl<float, double, float>(args.do_verification,
                                                      args.init_method,
                                                      args.do_log,
                                                      args.do_dumpout,
                                                      args.nrepeat,
                                                      args.inLengths,
Qianfeng's avatar
Qianfeng committed
491
                                                      args.reduceDims,
492
493
494
495
496
497
498
499
500
501
502
503
                                                      args.reduceOp,
                                                      args.nanOpt,
                                                      args.indicesOpt,
                                                      args.scales[0],
                                                      args.scales[1]);
        }
        else
            throw std::runtime_error("Invalid compType assignment!");
    };

    return (0);
};