"vscode:/vscode.git/clone" did not exist on "849f58d617e773d1a1edd49dacb2fac8257240b0"
reduce_blockwise.cpp 11.8 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
// SPDX-License-Identifier: MIT
2
// Copyright (c) 2018-2024, Advanced Micro Devices, Inc. All rights reserved.
Chao Liu's avatar
Chao Liu committed
3

4
5
6
7
#include <iostream>
#include <initializer_list>
#include <cstdlib>
#include <getopt.h>
8

Chao Liu's avatar
Chao Liu committed
9
#include "ck/utility/reduction_enums.hpp"
10
11
#include "reduce_blockwise_impl.hpp"
#include "reduce_example_common.hpp"
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

using namespace ck;
using namespace ck::tensor_operation::device;

static struct option long_options[] = {{"inLengths", required_argument, nullptr, 'D'},
                                       {"verify", required_argument, nullptr, 'v'},
                                       {"help", no_argument, nullptr, '?'},
                                       {nullptr, 0, nullptr, 0}};

class SimpleAppArgs
{
    private:
    int option_index = 0;

    public:
27
    std::vector<size_t> inLengths = {16, 64, 32, 960};
28
    std::vector<int> reduceDims   = {0, 1, 2};
29
    std::vector<float> scales     = {1.0f, 0.0f};
30

JD's avatar
JD committed
31
    bool do_verification = true;
32
33
    int data_type        = 1;
    int init_method      = 2;
34
    bool time_kernel     = true;
35
36
37
38
39
40
41

    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"
                  << std::endl;
42
43
        std::cout << "--reduceDims or -R, comma separated list of to-reduce dimensions"
                  << std::endl;
44
45
46
        std::cout << "--verify or -v, 1/0 to indicate whether to verify the reduction result by "
                     "comparing with the host-based reduction"
                  << std::endl;
47
48
49
        std::cout << "Arg1: data type (0: fp16, 1: fp32, 3: int8, 5: bp16, 6: fp64, 7: int4)"
                  << std::endl;
        std::cout << "Arg2 -- init method (0=no init, 1=single integer value, 2=scope integer "
50
51
                     "value, 3=decimal value)"
                  << std::endl;
52
        std::cout << "Arg3 -- time kernel (0=no, 1=yes)" << std::endl;
53
54
55
56
    };

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

59
        int ch;
60
61
62

        while(1)
        {
63
            ch = getopt_long(argc, argv, "D:R:v:l:", long_options, &option_index);
64
65
66
67
68
69
70
71
72
73
            if(ch == -1)
                break;
            switch(ch)
            {
            case 'D':
                if(!optarg)
                    throw std::runtime_error("Invalid option format!");

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

                reduceDims = getTypeValuesFromString<int>(optarg);
                break;
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
            case 'v':
                if(!optarg)
                    throw std::runtime_error("Invalid option format!");

                do_verification = 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]); return (-1);
            };
        };

97
98
        if(optind + 3 > argc)
        {
99
            throw std::runtime_error("Invalid cmd-line arguments, more argumetns are needed!");
100
        };
101

102
        data_type   = std::atoi(argv[optind++]);
103
        init_method = std::atoi(argv[optind++]);
104
        time_kernel = static_cast<bool>(std::atoi(argv[optind]));
105
106
107
108
109
110
111
112
113
114
115

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

        return (0);
    };
};

116
117
118
119
120
121
122
123
124
125
126
127
template <typename InOutDataType,
          typename AccDataType,
          ReduceTensorOp ReduceOpId,
          index_t PropagateNan,
          index_t OutputIndex>
bool reduce_blockwise_test(bool do_verification,
                           int init_method,
                           bool time_kernel,
                           const std::vector<size_t>& inLengths,
                           const std::vector<int>& reduceDims,
                           float alpha,
                           float beta)
128
{
129
130
    bool matched = false;
    int result   = 0;
Qianfeng's avatar
Qianfeng committed
131

132
    const auto tuple_object = reduce_shape_instances{};
133

134
135
136
    static_for<0, std::tuple_size<reduce_shape_instances>::value, 1>{}([&](auto i) {
        if(matched)
            return;
137

138
        using ShapeType = remove_cvref_t<decltype(std::get<i>(tuple_object))>;
139

140
141
        if(ShapeType::Rank_ != inLengths.size() || ShapeType::NumReduceDim_ != reduceDims.size())
            return;
142

143
144
        std::array<int, ShapeType::NumReduceDim_> arrReduceDims;

Po Yen Chen's avatar
Po Yen Chen committed
145
        ck::ranges::copy(reduceDims, arrReduceDims.begin());
146

147
148
149
150
151
152
153
        result = reduce_blockwise_impl<InOutDataType,
                                       AccDataType,
                                       ReduceOpId,
                                       ShapeType::Rank_,
                                       ShapeType::NumReduceDim_,
                                       PropagateNan,
                                       OutputIndex>(
154
            do_verification, init_method, time_kernel, inLengths, arrReduceDims, alpha, beta);
155

156
157
        matched = true;
    });
158

159
160
    return (result == 0) ? true : false;
};
161

162
163
164
constexpr ReduceTensorOp ReduceOpId = ReduceTensorOp::AVG;
constexpr bool PropagateNan         = true;
constexpr bool OutputIndex          = false;
165

166
167
168
int main(int argc, char* argv[])
{
    bool pass = true;
169

170
171
172
    if(argc > 1)
    {
        SimpleAppArgs arg;
173

174
175
        if(arg.processArgs(argc, argv) < 0)
            return (-1);
176

177
        if(arg.data_type == 0)
178
        {
179
180
181
182
183
184
185
186
            pass = reduce_blockwise_test<ck::half_t, float, ReduceOpId, PropagateNan, OutputIndex>(
                arg.do_verification,
                arg.init_method,
                arg.time_kernel,
                arg.inLengths,
                arg.reduceDims,
                arg.scales[0],
                arg.scales[1]);
187
        }
188
        else if(arg.data_type == 1)
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
            pass = reduce_blockwise_test<float, float, ReduceOpId, PropagateNan, OutputIndex>(
                arg.do_verification,
                arg.init_method,
                arg.time_kernel,
                arg.inLengths,
                arg.reduceDims,
                arg.scales[0],
                arg.scales[1]);
        }
        else if(arg.data_type == 3)
        {
            pass = reduce_blockwise_test<int8_t, float, ReduceOpId, PropagateNan, OutputIndex>(
                arg.do_verification,
                arg.init_method,
                arg.time_kernel,
                arg.inLengths,
                arg.reduceDims,
                arg.scales[0],
                arg.scales[1]);
        }
        else if(arg.data_type == 5)
        {
            pass = reduce_blockwise_test<ck::bhalf_t, float, ReduceOpId, PropagateNan, OutputIndex>(
                arg.do_verification,
                arg.init_method,
                arg.time_kernel,
                arg.inLengths,
                arg.reduceDims,
                arg.scales[0],
                arg.scales[1]);
        }
        else if(arg.data_type == 6)
        {
            pass = reduce_blockwise_test<double, double, ReduceOpId, PropagateNan, OutputIndex>(
                arg.do_verification,
                arg.init_method,
                arg.time_kernel,
                arg.inLengths,
                arg.reduceDims,
                arg.scales[0],
                arg.scales[1]);
        }
Qianfeng's avatar
Qianfeng committed
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#ifdef CK_EXPERIMENTAL_BIT_INT_EXTENSION_INT4
        else if(arg.data_type == 7)
        {
            pass = reduce_blockwise_test<int4_t, int32_t, ReduceTensorOp::AVG, false, false>(
                arg.do_verification,
                arg.init_method,
                arg.time_kernel,
                arg.inLengths,
                arg.reduceDims,
                arg.scales[0],
                arg.scales[1]);

            pass = pass && reduce_blockwise_test<int4_t, int8_t, ReduceTensorOp::MAX, false, false>(
                               arg.do_verification,
                               arg.init_method,
                               arg.time_kernel,
                               arg.inLengths,
                               arg.reduceDims,
                               arg.scales[0],
                               arg.scales[1]);
        }
#endif
254
255
256
257
    }
    else
    {
        // for testing half_t
258
259
260
        pass =
            pass && reduce_blockwise_test<ck::half_t, float, ReduceOpId, PropagateNan, OutputIndex>(
                        true, 2, true, {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, {0, 1, 2}, 1.0f, 0.0f);
261
262
263
264
265
        pass =
            pass && reduce_blockwise_test<ck::half_t, float, ReduceOpId, PropagateNan, OutputIndex>(
                        true, 2, true, {16, 64, 32, 960}, {0, 1, 2}, 1.0f, 0.0f);

        // for testing float
266
267
268
269
        pass =
            pass && reduce_blockwise_test<float, float, ReduceOpId, PropagateNan, OutputIndex>(
                        true, 2, true, {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, {0, 1, 2}, 1.0f, 0.0f);

270
271
272
273
        pass = pass && reduce_blockwise_test<float, float, ReduceOpId, PropagateNan, OutputIndex>(
                           true, 2, true, {16, 64, 32, 960}, {0, 1, 2}, 1.0f, 0.0f);

        // for testing double
274
275
276
277
        pass =
            pass && reduce_blockwise_test<float, float, ReduceOpId, PropagateNan, OutputIndex>(
                        true, 2, true, {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, {0, 1, 2}, 1.0f, 0.0f);

278
279
280
281
        pass = pass && reduce_blockwise_test<float, float, ReduceOpId, PropagateNan, OutputIndex>(
                           true, 2, true, {16, 64, 32, 960}, {0, 1, 2}, 1.0f, 0.0f);

        // for testing bhalf_t
282
283
284
285
        pass = pass &&
               reduce_blockwise_test<ck::bhalf_t, float, ReduceOpId, PropagateNan, OutputIndex>(
                   true, 2, true, {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, {0, 1, 2}, 1.0f, 0.0f);

286
287
288
289
290
        pass = pass &&
               reduce_blockwise_test<ck::bhalf_t, float, ReduceOpId, PropagateNan, OutputIndex>(
                   true, 2, true, {16, 64, 32, 960}, {0, 1, 2}, 1.0f, 0.0f);

        // for testing int8_t
291
292
293
294
        pass =
            pass && reduce_blockwise_test<int8_t, int32_t, ReduceOpId, PropagateNan, OutputIndex>(
                        true, 2, true, {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, {0, 1, 2}, 1.0f, 0.0f);

295
296
297
298
        pass =
            pass && reduce_blockwise_test<int8_t, int32_t, ReduceOpId, PropagateNan, OutputIndex>(
                        true, 2, true, {16, 64, 32, 960}, {0, 1, 2}, 1.0f, 0.0f);

Qianfeng's avatar
Qianfeng committed
299
300
#ifdef CK_EXPERIMENTAL_BIT_INT_EXTENSION_INT4
        // for testing int4_t using AVG operation
301
302
303
304
        pass =
            pass && reduce_blockwise_test<int4_t, int32_t, ReduceTensorOp::AVG, false, false>(
                        true, 2, true, {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, {0, 1, 2}, 1.0f, 0.0f);

Qianfeng's avatar
Qianfeng committed
305
306
307
308
        pass = pass && reduce_blockwise_test<int4_t, int32_t, ReduceTensorOp::AVG, false, false>(
                           true, 2, true, {16, 64, 32, 960}, {0, 1, 2}, 1.0f, 0.0f);

        // for testing int4_t using MAX operation
309
310
311
312
        pass =
            pass && reduce_blockwise_test<int4_t, int8_t, ReduceTensorOp::MAX, false, false>(
                        true, 2, true, {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3}, {0, 1, 2}, 1.0f, 0.0f);

Qianfeng's avatar
Qianfeng committed
313
314
315
        pass = pass && reduce_blockwise_test<int4_t, int8_t, ReduceTensorOp::MAX, false, false>(
                           true, 2, true, {16, 64, 32, 960}, {0, 1, 2}, 1.0f, 0.0f);
#endif
316
317
318
319
320
321
322
        // for testing 3D input
        pass = pass && reduce_blockwise_test<float, float, ReduceOpId, PropagateNan, OutputIndex>(
                           true, 2, true, {16, 64, 960}, {0, 1}, 1.0f, 0.0f);

        // for testing 5D input
        pass = pass && reduce_blockwise_test<float, float, ReduceOpId, PropagateNan, OutputIndex>(
                           true, 2, true, {16, 64, 32, 2, 960}, {0, 1, 2, 3}, 1.0f, 0.0f);
323
    };
324
325

    return (pass ? 0 : 1);
326
};