profile_reduce_impl.hpp 21.8 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
2
3
// SPDX-License-Identifier: MIT
// Copyright (c) 2018-2022, Advanced Micro Devices, Inc. All rights reserved.

4
#pragma once
5

Chao Liu's avatar
Chao Liu committed
6
7
8
9
#include "ck/utility/reduction_enums.hpp"
#include "ck/tensor_operation/gpu/device/device_reduce.hpp"

#include "ck/library/tensor_operation_instance/gpu/reduce/device_reduce_instance.hpp"
Po Yen Chen's avatar
Po Yen Chen committed
10
11
#include "ck/library/utility/algorithm.hpp"
#include "ck/library/utility/check_err.hpp"
12
13
14
15
#include "ck/library/utility/device_memory.hpp"
#include "ck/library/utility/host_reduction.hpp"
#include "ck/library/utility/host_common_util.hpp"
#include "ck/library/utility/host_tensor_generator.hpp"
16
17
18
19

namespace ck {
namespace tensor_operation {
namespace device {
20
namespace instance {
21

22
23
24
25
26
template <index_t Rank,
          index_t NumReduceDim,
          ReduceTensorOp ReduceOpId,
          bool PropagateNan,
          bool UseIndex>
27
28
struct ReduceDescription
{
29
30
31
32
33
    static constexpr index_t Rank_              = Rank;
    static constexpr index_t NumReduceDim_      = NumReduceDim;
    static constexpr ReduceTensorOp ReduceOpId_ = ReduceOpId;
    static constexpr bool PropagateNan_         = PropagateNan;
    static constexpr bool UseIndex_             = UseIndex;
34
35
};

36
using reduce_description_instances =
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
    std::tuple<ReduceDescription<4, 3, ReduceTensorOp::ADD, false, false>, // for ADD
               ReduceDescription<4, 4, ReduceTensorOp::ADD, false, false>,
               ReduceDescription<4, 1, ReduceTensorOp::ADD, false, false>,
               ReduceDescription<2, 1, ReduceTensorOp::ADD, false, false>,

               ReduceDescription<4, 3, ReduceTensorOp::AVG, false, false>, // for AVG
               ReduceDescription<4, 4, ReduceTensorOp::AVG, false, false>,
               ReduceDescription<4, 1, ReduceTensorOp::AVG, false, false>,
               ReduceDescription<2, 1, ReduceTensorOp::AVG, false, false>,

               ReduceDescription<4, 3, ReduceTensorOp::NORM2, false, false>, // for NORM2
               ReduceDescription<4, 4, ReduceTensorOp::NORM2, false, false>,
               ReduceDescription<4, 1, ReduceTensorOp::NORM2, false, false>,
               ReduceDescription<2, 1, ReduceTensorOp::NORM2, false, false>,

               ReduceDescription<4, 3, ReduceTensorOp::MIN, false, false>, // for MIN
               ReduceDescription<4, 4, ReduceTensorOp::MIN, false, false>,
               ReduceDescription<4, 1, ReduceTensorOp::MIN, false, false>,
               ReduceDescription<2, 1, ReduceTensorOp::MIN, false, false>,
               ReduceDescription<4, 3, ReduceTensorOp::MAX, false, false>, // for MAX
               ReduceDescription<4, 4, ReduceTensorOp::MAX, false, false>,
               ReduceDescription<4, 1, ReduceTensorOp::MAX, false, false>,
               ReduceDescription<2, 1, ReduceTensorOp::MAX, false, false>,
               ReduceDescription<4, 3, ReduceTensorOp::AMAX, false, false>, // for AMAX
               ReduceDescription<4, 4, ReduceTensorOp::AMAX, false, false>,
               ReduceDescription<4, 1, ReduceTensorOp::AMAX, false, false>,
               ReduceDescription<2, 1, ReduceTensorOp::AMAX, false, false>,

               ReduceDescription<4, 3, ReduceTensorOp::MIN, false, true>, // for MIN
               ReduceDescription<4, 4, ReduceTensorOp::MIN, false, true>,
               ReduceDescription<4, 1, ReduceTensorOp::MIN, false, true>,
               ReduceDescription<2, 1, ReduceTensorOp::MIN, false, true>,
               ReduceDescription<4, 3, ReduceTensorOp::MAX, false, true>, // for MAX
               ReduceDescription<4, 4, ReduceTensorOp::MAX, false, true>,
               ReduceDescription<4, 1, ReduceTensorOp::MAX, false, true>,
               ReduceDescription<2, 1, ReduceTensorOp::MAX, false, true>,
               ReduceDescription<4, 3, ReduceTensorOp::AMAX, false, true>, // for AMAX
               ReduceDescription<4, 4, ReduceTensorOp::AMAX, false, true>,
               ReduceDescription<4, 1, ReduceTensorOp::AMAX, false, true>,
               ReduceDescription<2, 1, ReduceTensorOp::AMAX, false, true>>;
77
78
79
80

template <typename DescriptionType>
bool description_match(const DescriptionType& description,
                       int Rank,
Qianfeng's avatar
Qianfeng committed
81
                       const std::vector<int>& reduceDims,
82
                       ReduceTensorOp ReduceOpId,
83
84
                       bool PropagateNan,
                       bool UseIndex)
85
{
86
87
    if(description.Rank_ != Rank || description.ReduceOpId_ != ReduceOpId ||
       description.PropagateNan_ != PropagateNan || description.UseIndex_ != UseIndex)
88
89
        return (false);

Qianfeng's avatar
Qianfeng committed
90
    if(DescriptionType::NumReduceDim_ != reduceDims.size())
91
92
93
94
95
96
97
        return (false);

    bool result = true;

    return (result);
};

98
} // namespace instance
99
100
101
102
103
104
105
} // namespace device
} // namespace tensor_operation
} // namespace ck

namespace ck {
namespace profiler {

106
107
108
template <int Rank, int NumReduceDim>
static inline std::array<int, Rank - NumReduceDim>
get_invariant_dims(const std::array<int, NumReduceDim>& reduceDims)
109
{
Qianfeng's avatar
Qianfeng committed
110
    int reduceFlag = 0;
111

Qianfeng's avatar
Qianfeng committed
112
113
    // flag the bits for the reduceDims
    for(int i = 0; i < NumReduceDim; i++)
114
    {
Qianfeng's avatar
Qianfeng committed
115
        reduceFlag |= 1 << reduceDims[i];
116
117
    };

118
    std::array<int, Rank - NumReduceDim> invariantDims;
Qianfeng's avatar
Qianfeng committed
119
120

    // collect invariant dimensions
121
    int dim = 0;
Qianfeng's avatar
Qianfeng committed
122
123
124
    for(int i = 0; i < Rank; i++)
        if((reduceFlag & (1 << i)) == 0)
        {
125
126
            invariantDims[dim] = i;
            dim++;
Qianfeng's avatar
Qianfeng committed
127
128
129
        };

    return invariantDims;
130
131
132
133
134
135
};

template <typename InDataType,
          typename AccDataType,
          typename OutDataType,
          int Rank,
Qianfeng's avatar
Qianfeng committed
136
          int NumReduceDim,
137
          ReduceTensorOp ReduceOpId,
138
139
140
          bool PropagateNan,
          bool UseIndex>
bool profile_reduce_impl_impl(bool do_verification,
141
142
                              int init_method,
                              bool do_dumpout,
JD's avatar
JD committed
143
                              bool time_kernel,
144
                              const std::vector<size_t>& inLengths,
145
                              const std::array<int, NumReduceDim>& reduceDims,
146
147
148
149
                              float alpha,
                              float beta)
{
    using namespace ck::tensor_operation::device;
150
    using namespace ck::tensor_operation::device::instance;
151
    using ck::host_common::dumpBufferToFile;
152

153
154
    constexpr index_t NumOutDim = (Rank - NumReduceDim == 0) ? 1 : Rank - NumReduceDim;

155
    constexpr bool op_support_indices =
156
157
        (ReduceOpId == ReduceTensorOp::MIN || ReduceOpId == ReduceTensorOp::MAX ||
         ReduceOpId == ReduceTensorOp::AMAX);
158

159
    constexpr bool OutputIndex = (op_support_indices && UseIndex);
160
161
162

    constexpr bool out_support_atomic_add = std::is_same<OutDataType, float>::value;
    constexpr bool op_support_atomic_add =
163
        !op_support_indices && ReduceOpId != ReduceTensorOp::NORM2;
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
    constexpr bool use_atomic_add = (out_support_atomic_add && op_support_atomic_add);

    // 1) If InDataType is half_t, must use half_t as AccDataType for indexable reduction operations
    // 2) If InDataType is half_t, must use float as AccDataType for non-indexable reduction
    // operations
    constexpr bool invalid_reduce_1 =
        std::is_same<InDataType, half_t>::value &&
        ((!op_support_indices && !std::is_same<AccDataType, float>::value) ||
         (op_support_indices && !std::is_same<AccDataType, half_t>::value));

    // 1) If InDataType is float, must use float as AccDataType for indexable reduction operations
    constexpr bool invalid_reduce_2 =
        std::is_same<InDataType, float>::value &&
        (op_support_indices && !std::is_same<AccDataType, float>::value);

    // 1) The indices can only be used when the reduction operation is indexable
180
    constexpr bool invalid_reduce_3 = (!op_support_indices && UseIndex);
181

182
183
184
185
186
187
188
189
190
191
192
    // 1) If InDataType is int8_t, must use int8_t as AccDataType for indexable reduction operations
    // 2) If InDataType is int8_t, must use int32_t as AccDataType for non-indexable reduction
    // operations
    constexpr bool invalid_reduce_4 =
        std::is_same<InDataType, int8_t>::value &&
        ((!op_support_indices && !std::is_same<AccDataType, int32_t>::value) ||
         (op_support_indices && !std::is_same<AccDataType, int8_t>::value));

    // 1) If InDataType is int8_t, the supported operation must be either indexable operations or
    // ADD/AVG
    constexpr bool invalid_reduce_5 = std::is_same<InDataType, int8_t>::value &&
193
194
                                      (!op_support_indices && ReduceOpId != ReduceTensorOp::ADD &&
                                       ReduceOpId != ReduceTensorOp::AVG);
195
196
197
198
199
200
201

    // 1) If InDataType is bhalf_t, must use float as AccDataType for all reduction operations
    constexpr bool invalid_reduce_6 =
        std::is_same<InDataType, bhalf_t>::value && !std::is_same<AccDataType, float>::value;

    constexpr bool invalid_reduce = (invalid_reduce_1 || invalid_reduce_2 || invalid_reduce_3 ||
                                     invalid_reduce_4 || invalid_reduce_5 || invalid_reduce_6);
202

203
204
    bool pass = true;

205
206
207
208
209
210
    if constexpr(!invalid_reduce)
    {
        Tensor<InDataType> in(inLengths);

        std::vector<size_t> outLengths;

Qianfeng's avatar
Qianfeng committed
211
212
213
        const auto invariantDims = get_invariant_dims<Rank, NumReduceDim>(reduceDims);

        if(reduceDims.size() == Rank)
214
215
            outLengths.push_back(1);
        else
Qianfeng's avatar
Qianfeng committed
216
            for(auto dim : invariantDims)
217
218
219
220
                outLengths.push_back(inLengths[dim]);

        Tensor<OutDataType> out_ref(outLengths);
        Tensor<OutDataType> out(outLengths);
221
222
        Tensor<int32_t> out_indices_ref(outLengths);
        Tensor<int32_t> out_indices(outLengths);
223
224
225
226
227
228
229

        auto inStrides  = in.mDesc.GetStrides();
        auto outStrides = out.mDesc.GetStrides();

        size_t invariant_total_length = out.mDesc.GetElementSize();
        size_t reduce_total_length    = in.mDesc.GetElementSize() / invariant_total_length;

230
        std::size_t num_thread = 1;
231
232
233
234
235

        if(do_verification)
        {
            switch(init_method)
            {
236
237
238
            case 0: break;
            case 1:
                in.GenerateTensorValue(GeneratorTensor_1<InDataType>{1}, num_thread);
239
                if(beta != 0.0f)
240
                    out_ref.GenerateTensorValue(GeneratorTensor_1<InDataType>{1}, num_thread);
241
                break;
242
            case 2:
243
244
245
246
247
                in.GenerateTensorValue(GeneratorTensor_2<InDataType>{-5, 5}, num_thread);
                if(beta != 0.0f)
                    out_ref.GenerateTensorValue(GeneratorTensor_2<InDataType>{-5, 5}, num_thread);
                break;
            default:
248
                in.GenerateTensorValue(GeneratorTensor_3<InDataType>{-5.0, 5.0}, num_thread);
249
                if(beta != 0.0f)
250
251
                    out_ref.GenerateTensorValue(GeneratorTensor_3<InDataType>{-5.0, 5.0},
                                                num_thread);
252
253
254
            }

            if(beta != 0.0f)
255
                for(size_t i = 0; i < out_ref.mDesc.GetElementSpaceSize(); i++)
256
257
258
259
                    out.mData[i] = out_ref.mData[i];
        };

        // these buffers are usually provided by the user application
260
261
        DeviceMem in_dev(sizeof(InDataType) * in.mDesc.GetElementSpaceSize());
        DeviceMem out_dev(sizeof(OutDataType) * out.mDesc.GetElementSpaceSize());
262
263
264
265
266
267

        in_dev.ToDevice(in.mData.data());

        if(beta != 0.0f)
            out_dev.ToDevice(out.mData.data());

268
        size_t indicesSizeInBytes = OutputIndex ? out.mDesc.GetElementSize() * sizeof(int) : 0;
269
270
271
272
273
274

        DeviceMem out_indices_dev(indicesSizeInBytes);

        float best_avg_time   = 0;
        float best_gb_per_sec = 0;

275
        using InElementwiseOperation =
276
            typename reduce_unary_operator<ReduceOpId, true, true>::InElementwiseOperation;
277
        using AccElementwiseOperation =
278
            typename reduce_unary_operator<ReduceOpId, true, true>::AccElementwiseOperation;
279

280
281
282
283
284
285
286
287
        using ReduceOperation = typename reduce_binary_operator<ReduceOpId>::opType;

        InElementwiseOperation in_elementwise_op;
        AccElementwiseOperation acc_elementwise_op;

        std::tie(in_elementwise_op, acc_elementwise_op) =
            reduce_unary_operator<ReduceOpId, true, true>::GetElementwiseOperator(
                static_cast<int32_t>(reduce_total_length));
288

289
290
        using DeviceReduceInstPtr =
            DeviceReducePtr<Rank, NumReduceDim, InElementwiseOperation, AccElementwiseOperation>;
291

292
        std::vector<DeviceReduceInstPtr> reduce_ptrs;
293
294
295
296
297

        add_device_reduce_instance_threadwise<InDataType,
                                              AccDataType,
                                              OutDataType,
                                              Rank,
Qianfeng's avatar
Qianfeng committed
298
                                              NumReduceDim,
299
300
301
                                              ReduceOperation,
                                              InElementwiseOperation,
                                              AccElementwiseOperation,
302
                                              PropagateNan,
303
                                              UseIndex>(reduce_ptrs);
304
305
306
307
308

        add_device_reduce_instance_blockwise<InDataType,
                                             AccDataType,
                                             OutDataType,
                                             Rank,
Qianfeng's avatar
Qianfeng committed
309
                                             NumReduceDim,
310
311
312
                                             ReduceOperation,
                                             InElementwiseOperation,
                                             AccElementwiseOperation,
313
                                             PropagateNan,
314
                                             UseIndex>(reduce_ptrs);
315
316

        if constexpr(use_atomic_add)
317
        {
318
319
320
321
            add_device_reduce_instance_multiblock_atomic_add<InDataType,
                                                             AccDataType,
                                                             OutDataType,
                                                             Rank,
Qianfeng's avatar
Qianfeng committed
322
                                                             NumReduceDim,
323
324
325
                                                             ReduceOperation,
                                                             InElementwiseOperation,
                                                             AccElementwiseOperation,
326
                                                             PropagateNan,
327
                                                             UseIndex>(reduce_ptrs);
328
        }
329

330
        if(reduce_ptrs.empty())
331
332
333
334
335
336
        {
            throw std::runtime_error("Wrong! No device REDUCE instance found");
        };

        if(do_verification)
        {
337
338
339
            ReductionHost<InDataType,
                          AccDataType,
                          OutDataType,
340
341
342
                          ReduceOperation,
                          InElementwiseOperation,
                          AccElementwiseOperation,
343
344
345
                          Rank,
                          NumReduceDim,
                          PropagateNan,
346
                          OutputIndex>
Qianfeng's avatar
Qianfeng committed
347
                hostReduce(in.mDesc, out_ref.mDesc, invariantDims, reduceDims);
348

349
350
351
352
353
354
355
            hostReduce.Run(alpha,
                           in.mData.data(),
                           beta,
                           out_ref.mData.data(),
                           out_indices_ref.mData.data(),
                           in_elementwise_op,
                           acc_elementwise_op);
356
357
        };

358
359
360
361
        std::array<index_t, Rank> arrInLengths;
        std::array<index_t, Rank> arrInStrides;
        std::array<index_t, NumOutDim> arrOutLengths;
        std::array<index_t, NumOutDim> arrOutStrides;
362

Po Yen Chen's avatar
Po Yen Chen committed
363
364
365
366
        ck::ranges::copy(inLengths, arrInLengths.begin());
        ck::ranges::copy(inStrides, arrInStrides.begin());
        ck::ranges::copy(outLengths, arrOutLengths.begin());
        ck::ranges::copy(outStrides, arrOutStrides.begin());
367

368
        for(auto& reduce_ptr : reduce_ptrs)
369
        {
370
371
372
373
            auto argument_ptr = reduce_ptr->MakeArgumentPointer(arrInLengths,
                                                                arrInStrides,
                                                                arrOutLengths,
                                                                arrOutStrides,
374
375
376
377
                                                                reduceDims,
                                                                alpha,
                                                                beta,
                                                                in_dev.GetDeviceBuffer(),
378
                                                                nullptr,
379
380
                                                                out_dev.GetDeviceBuffer(),
                                                                out_indices_dev.GetDeviceBuffer(),
381
382
                                                                in_elementwise_op,
                                                                acc_elementwise_op);
383
384
385
386
387
388
389
390

            if(!reduce_ptr->IsSupportedArgument(argument_ptr.get()))
                continue;

            std::string reduce_name = reduce_ptr->GetTypeString();

            auto invoker_ptr = reduce_ptr->MakeInvokerPointer();

JD's avatar
JD committed
391
392
            float avg_time =
                invoker_ptr->Run(argument_ptr.get(), StreamConfig{nullptr, time_kernel});
393
394
395
396
397
398
399

            std::size_t num_bytes =
                invariant_total_length * reduce_total_length * sizeof(InDataType) +
                invariant_total_length * sizeof(OutDataType);

            float gb_per_sec = num_bytes / 1.E6 / avg_time;

400
401
402
            if(time_kernel)
                std::cout << "Perf: " << avg_time << " ms, " << gb_per_sec << " GB/s, "
                          << reduce_name << std::endl;
403
404
405
406
407
408
409
410
411

            if(gb_per_sec > best_gb_per_sec)
            {
                best_avg_time   = avg_time;
                best_gb_per_sec = gb_per_sec;
            }

            if(do_verification)
            {
412
413
                bool single_pass;

414
                out_dev.FromDevice(out.mData.data());
415
                single_pass = ck::utils::check_err(out, out_ref);
416

417
                if(OutputIndex)
418
419
                {
                    out_indices_dev.FromDevice(out_indices.mData.data());
420
                    single_pass = single_pass && ck::utils::check_err(out_indices, out_indices_ref);
421
422
                };

423
                if(!single_pass)
424
                {
425
426
427
428
                    std::cout << "Fail Info: " << reduce_ptr->GetTypeString() << std::endl;
                }

                pass = pass && single_pass;
429
430
431
432
433
434
435
436
            };

            if(do_dumpout)
            {
                dumpBufferToFile("dump_in.bin", in.mData.data(), in.mDesc.GetElementSize());
                dumpBufferToFile("dump_out.bin", out.mData.data(), out.mDesc.GetElementSize());
                dumpBufferToFile(
                    "dump_out_host.bin", out_ref.mData.data(), out_ref.mDesc.GetElementSize());
437
                if(OutputIndex)
438
439
440
441
442
443
444
445
446
447
448
                {
                    dumpBufferToFile("dump_indices.bin",
                                     out_indices.mData.data(),
                                     out_indices.mDesc.GetElementSize());
                    dumpBufferToFile("dump_indices_host.bin",
                                     out_indices_ref.mData.data(),
                                     out_indices_ref.mDesc.GetElementSize());
                };
            };
        };

449
450
451
        if(time_kernel)
            std::cout << "Best Perf: " << best_avg_time << " ms, " << best_gb_per_sec << " GB/s"
                      << std::endl;
452
453
454
455
456
457
    }
    else
    {
        std::cout << "The requested reduction operation is not supported, please check !!!"
                  << std::endl;
    };
458
459

    return pass;
460
461
462
};

template <typename InDataType, typename AccDataType, typename OutDataType>
463
bool profile_reduce_impl(bool do_verification,
464
465
                         int init_method,
                         bool do_dumpout,
JD's avatar
JD committed
466
                         bool time_kernel,
467
                         const std::vector<size_t>& inLengths,
Qianfeng's avatar
Qianfeng committed
468
                         const std::vector<int>& reduceDims,
469
                         ReduceTensorOp ReduceOpId,
470
471
                         bool PropagateNan,
                         bool UseIndex,
472
473
474
475
                         float alpha,
                         float beta)
{
    bool matched = false;
476
    bool pass    = true;
477
478

    using tuple_of_description_instances =
479
        tensor_operation::device::instance::reduce_description_instances;
480
481
482
483
484
485
486
487
488
489

    const auto tuple_object = tuple_of_description_instances{};

    static_for<0, std::tuple_size<tuple_of_description_instances>::value, 1>{}([&](auto i) {
        if(matched)
            return;

        using descType = remove_cvref_t<decltype(std::get<i>(tuple_object))>;

        if(!description_match(
490
               descType{}, inLengths.size(), reduceDims, ReduceOpId, PropagateNan, UseIndex))
491
492
            return;

493
494
        std::array<ck::index_t, descType::NumReduceDim_> arrReduceDims;

Po Yen Chen's avatar
Po Yen Chen committed
495
        ck::ranges::copy(reduceDims, arrReduceDims.begin());
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511

        pass = pass && profile_reduce_impl_impl<InDataType,
                                                AccDataType,
                                                OutDataType,
                                                descType::Rank_,
                                                descType::NumReduceDim_,
                                                static_cast<ReduceTensorOp>(descType::ReduceOpId_),
                                                descType::PropagateNan_,
                                                descType::UseIndex_>(do_verification,
                                                                     init_method,
                                                                     do_dumpout,
                                                                     time_kernel,
                                                                     inLengths,
                                                                     arrReduceDims,
                                                                     alpha,
                                                                     beta);
512
513
514

        matched = true;
    });
515
516

    return pass;
517
518
519
520
};

} // namespace profiler
} // namespace ck