"configs/vscode:/vscode.git/clone" did not exist on "13f002d75330c80dfedbdc792cf4f9d72735609f"
profile_batchnorm_fwd.cpp 8.12 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
80
81
82
83
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// SPDX-License-Identifier: MIT
// Copyright (c) 2018-2022, Advanced Micro Devices, Inc. All rights reserved.

#include <iostream>
#include <vector>
#include <getopt.h>

#include "ck/library/utility/host_common_util.hpp"
#include "profiler/include/profile_batchnorm_forward_impl.hpp"

using ck::index_t;

using namespace std;

static const struct option long_options[] = {{"inOutLengths", required_argument, nullptr, 'D'},
                                             {"reduceDims", required_argument, nullptr, 'R'},
                                             {"dumpout", required_argument, nullptr, 'o'},
                                             {"verify", required_argument, nullptr, 'v'},
                                             {"help", no_argument, nullptr, '?'},
                                             {nullptr, 0, nullptr, 0}};

class BatchnormFwdArgParser
{
    private:
    int option_index = 0;

    public:
    std::vector<size_t> inLengths;
    std::vector<int> reduceDims;

    bool do_verification = false;
    bool do_dumpout      = false;

    bool updateMovingAverage;
    bool saveMeanAndInvVariance;

    int data_type    = 0;
    int init_method  = 2;
    bool time_kernel = false;

    BatchnormFwdArgParser()  = default;
    ~BatchnormFwdArgParser() = default;

    void show_usage(const char* cmd)
    {
        // clang-format off
        std::cout << "Usage of " << cmd << std::endl;
        std::cout << "--inOutLengths or -D, comma separated list of input tensor dimension lengths, must have 4 integers for nhwc" << std::endl;
        std::cout << "--reduceDims or -R, comma separated list of dimensions to reduce on" << std::endl;  
        std::cout << "--verify or -v, 1/0 to indicate whether to verify the result by comparing with the host-based batch-normalization" << std::endl;
        std::cout << "Arg1: data type (0: fp16, 1: fp32, 3: int8, 5: bp16, 6: fp64)" << std::endl;
        std::cout << "Arg2: 1/0 to indicate whether to update the moving average and variance (0=no, 1=yes)" << std::endl;
        std::cout << "Arg3: 1/0 to indicate whether to save the calculated mean and invVariance (0=no, 1=yes)" << std::endl;
        std::cout << "Arg4: init method used for bnScale and bnBias (0=no init, 1=single integer value, 2=scope integer value, 3=decimal value)" << std::endl;
        std::cout << "Arg5: time kernel (0=no, 1=yes)" << std::endl;
        // clang-format on
    };

    int operator()(int argc, char* argv[])
    {
        using ck::host_common::getTypeValuesFromString;

        int ch;

        optind++; // to skip the module name

        while(1)
        {
            ch = getopt_long(argc, argv, "D:R:v:o:", 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!");

                reduceDims = getTypeValuesFromString<int>(optarg);
                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 '?':
                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 + 5 > argc)
            throw std::runtime_error("Invalid cmd-line arguments, more argumetns are needed!");

        data_type              = std::atoi(argv[optind++]);
        updateMovingAverage    = std::atoi(argv[optind++]);
        saveMeanAndInvVariance = std::atoi(argv[optind++]);
        init_method            = std::atoi(argv[optind++]);
        time_kernel            = static_cast<bool>(std::atoi(argv[optind++]));

        if(data_type != 0 && data_type != 1 && data_type != 3 && data_type != 5 && data_type != 6)
            return -1;

        return 0;
    };
}; // end of class AppArgs

static const double epsilon       = std::numeric_limits<float>::epsilon();
static const double averageFactor = 0.1;

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

    BatchnormFwdArgParser arg_parser;

    if(arg_parser(argc, argv) != 0)
        return -1;

    using F16  = ck::half_t;
    using F32  = float;
    using BF16 = ck::bhalf_t;
    using I8   = int8_t;
    using F64  = double;

    if(arg_parser.data_type == 0)
    {
        if(arg_parser.inLengths.size() == 4 && arg_parser.reduceDims.size() == 3)
        {
            profile_batchnorm_forward_impl<F16, F16, F32, F16, F16, F16, 4, 3>(
                arg_parser.do_verification,
                arg_parser.init_method,
                arg_parser.do_dumpout,
                arg_parser.time_kernel,
                arg_parser.inLengths,
                arg_parser.reduceDims,
                arg_parser.updateMovingAverage,
                arg_parser.saveMeanAndInvVariance,
                epsilon,
                averageFactor);
        };
    }
    else if(arg_parser.data_type == 1)
    {
        if(arg_parser.inLengths.size() == 4 && arg_parser.reduceDims.size() == 3)
        {
            profile_batchnorm_forward_impl<F32, F32, F32, F32, F32, F32, 4, 3>(
                arg_parser.do_verification,
                arg_parser.init_method,
                arg_parser.do_dumpout,
                arg_parser.time_kernel,
                arg_parser.inLengths,
                arg_parser.reduceDims,
                arg_parser.updateMovingAverage,
                arg_parser.saveMeanAndInvVariance,
                epsilon,
                averageFactor);
        };
    }
    else if(arg_parser.data_type == 3)
    {
        if(arg_parser.inLengths.size() == 4 && arg_parser.reduceDims.size() == 3)
        {
            profile_batchnorm_forward_impl<I8, I8, F32, I8, I8, F32, 4, 3>(
                arg_parser.do_verification,
                arg_parser.init_method,
                arg_parser.do_dumpout,
                arg_parser.time_kernel,
                arg_parser.inLengths,
                arg_parser.reduceDims,
                arg_parser.updateMovingAverage,
                arg_parser.saveMeanAndInvVariance,
                epsilon,
                averageFactor);
        };
    }
    else if(arg_parser.data_type == 5)
    {
        if(arg_parser.inLengths.size() == 4 && arg_parser.reduceDims.size() == 3)
        {
            profile_batchnorm_forward_impl<BF16, BF16, F32, BF16, BF16, F32, 4, 3>(
                arg_parser.do_verification,
                arg_parser.init_method,
                arg_parser.do_dumpout,
                arg_parser.time_kernel,
                arg_parser.inLengths,
                arg_parser.reduceDims,
                arg_parser.updateMovingAverage,
                arg_parser.saveMeanAndInvVariance,
                epsilon,
                averageFactor);
        };
    }
    else if(arg_parser.data_type == 6)
    {
        if(arg_parser.inLengths.size() == 4 && arg_parser.reduceDims.size() == 3)
        {
            profile_batchnorm_forward_impl<F64, F64, F64, F64, F64, F64, 4, 3>(
                arg_parser.do_verification,
                arg_parser.init_method,
                arg_parser.do_dumpout,
                arg_parser.time_kernel,
                arg_parser.inLengths,
                arg_parser.reduceDims,
                arg_parser.updateMovingAverage,
                arg_parser.saveMeanAndInvVariance,
                epsilon,
                averageFactor);
        };
    }

    return 0;
}