reduce_no_index.cpp 9.84 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
#include <getopt.h>
2

Chao Liu's avatar
Chao Liu committed
3
4
#include "ck/library/host_tensor/host_common_util.hpp"
#include "profiler/include/profile_reduce_impl.hpp"
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

using namespace ck;

static struct option long_options[] = {{"inLengths", required_argument, nullptr, 'D'},
                                       {"reduceDimensions", required_argument, nullptr, 'R'},
                                       {"scales", required_argument, nullptr, 'S'},
                                       {"help", no_argument, nullptr, '?'},
                                       {nullptr, 0, nullptr, 0}};

class SimpleAppArgs
{
    private:
    int option_index = 0;

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

    int data_type;
    int init_method = 1;

    public:
    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 "
                     "(only 4-d tensor supported)"
                  << std::endl;
        std::cout << "--reduceDimensions or -R comma seperated list of dimension indexes to reduce "
                     "(only 1 or 3 or 4 dimensions supported)"
                  << std::endl;
        std::cout << "--scales or -S, comma separated two float values for alpha and beta"
                  << std::endl;
        std::cout << "Arg1 -- data type (0: fp16, 1: fp32, 3: int8, 5: bp16, 6: fp64)" << std::endl;
        std::cout << "Arg2 -- init method(0=no init, 1=single integer value, 2=scope integer "
                     "value, 3=decimal value)"
                  << std::endl;
    };

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

49
        int ch;
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

        while(1)
        {
            ch = getopt_long(argc, argv, "D:R:S:", 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 'S':
                if(!optarg)
                    throw std::runtime_error("Invalid option format!");

                scales = getTypeValuesFromString<float>(optarg);
                break;
            case '?':
                if(std::string(long_options[option_index].name) == "help")
                {
                    show_usage(argv[0]);
                    return (-1);
                };
                break;
            default: show_usage(argv[0]); return (-1);
            };
        };

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

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

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

        if(inLengths.size() != 4 ||
           (reduceDims.size() != 1 && reduceDims.size() != 3 && reduceDims.size() != 4))
            return (-1);

103
        if(data_type != 0 && data_type != 1 && data_type != 3 && data_type != 5 && data_type != 6)
104
105
106
107
108
109
110
111
112
113
            return (-1);

        return (0);
    };
};

bool test_reduce_no_index(int data_type,
                          int init_method,
                          std::vector<int> reduceDims,
                          std::vector<size_t> inLengths,
114
115
                          ReduceTensorOp reduceOpId,
                          bool propagateNan,
116
117
118
                          float alpha,
                          float beta)
{
119
120
    using ck::profiler::profile_reduce_impl;

121
122
123
124
    bool result = true;

    if(data_type == 0)
    {
125
126
127
128
129
130
131
132
133
134
135
        result = profile_reduce_impl<float, float, float>(true,
                                                          init_method,
                                                          false,
                                                          false,
                                                          inLengths,
                                                          reduceDims,
                                                          reduceOpId,
                                                          propagateNan,
                                                          false,
                                                          alpha,
                                                          beta);
136
137
138
    }
    else if(data_type == 1)
    {
139
140
141
142
143
144
145
146
147
148
149
        result = profile_reduce_impl<ck::half_t, float, ck::half_t>(true,
                                                                    init_method,
                                                                    false,
                                                                    false,
                                                                    inLengths,
                                                                    reduceDims,
                                                                    reduceOpId,
                                                                    propagateNan,
                                                                    false,
                                                                    alpha,
                                                                    beta);
150
151
152
    }
    else if(data_type == 3)
    {
153
154
155
156
157
158
159
160
161
162
163
        result = profile_reduce_impl<int8_t, int32_t, int8_t>(true,
                                                              init_method,
                                                              false,
                                                              false,
                                                              inLengths,
                                                              reduceDims,
                                                              reduceOpId,
                                                              propagateNan,
                                                              false,
                                                              alpha,
                                                              beta);
164
165
166
    }
    else if(data_type == 5)
    {
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
        result = profile_reduce_impl<ck::bhalf_t, float, ck::bhalf_t>(true,
                                                                      init_method,
                                                                      false,
                                                                      false,
                                                                      inLengths,
                                                                      reduceDims,
                                                                      reduceOpId,
                                                                      propagateNan,
                                                                      false,
                                                                      alpha,
                                                                      beta);
    }
    else if(data_type == 6)
    {
        result = profile_reduce_impl<double, double, double>(true,
                                                             init_method,
                                                             false,
                                                             false,
                                                             inLengths,
                                                             reduceDims,
                                                             reduceOpId,
                                                             propagateNan,
                                                             false,
                                                             alpha,
                                                             beta);
192
193
194
195
196
    }

    return (result);
};

197
198
199
constexpr ReduceTensorOp reduceOpId = ReduceTensorOp::AVG;
constexpr bool propagateNan         = false;

200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
int main(int argc, char* argv[])
{
    SimpleAppArgs args;

    bool result = true;

    if(argc == 1)
    {
        int data_type   = 1;
        int init_method = 2;
        std::vector<size_t> inLengths{64, 4, 280, 80};
        std::vector<std::vector<int>> v_reduceDims{
            {0, 1, 2, 3}, {0, 1, 2}, {1, 2, 3}, {0, 1, 3}, {0, 2, 3}, {0}, {1}, {2}, {3}};

        for(auto& reduceDims : v_reduceDims)
215
216
217
218
219
220
221
222
            result = result && test_reduce_no_index(data_type,
                                                    init_method,
                                                    reduceDims,
                                                    inLengths,
                                                    reduceOpId,
                                                    propagateNan,
                                                    1.0f,
                                                    0.0f);
223
224
225
226
227
228
229
230
231
232
233
234
235
    }
    else
    {
        if(args.processArgs(argc, argv) < 0)
        {
            throw std::runtime_error(
                "Invalid input arguments, test_reduce_no_index could not be executed!");
        };

        result = test_reduce_no_index(args.data_type,
                                      args.init_method,
                                      args.reduceDims,
                                      args.inLengths,
236
237
                                      reduceOpId,
                                      propagateNan,
238
239
240
241
242
243
244
245
                                      args.scales[0],
                                      args.scales[1]);
    }

    std::cout << "test_reduce_no_index ..... " << (result ? "SUCCESS" : "FAILURE") << std::endl;

    return (result ? 0 : -1);
}