argmin.cpp 2.69 KB
Newer Older
1
2
3
4
5
6
7
#include <migraphx/shape.hpp>
#include <migraphx/argument.hpp>
#include <migraphx/dfor.hpp>
#include <migraphx/gpu/device/argmin.hpp>
#include <migraphx/gpu/device/tensor.hpp>
#include <migraphx/gpu/device/launch.hpp>
#include <migraphx/gpu/device/types.hpp>
Shucai Xiao's avatar
Shucai Xiao committed
8
#include <migraphx/gpu/device/reduce_opers.hpp>
9
10
11
12
13
14
15
#include <migraphx/gpu/hip.hpp>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
namespace gpu {
namespace device {

Shucai Xiao's avatar
Shucai Xiao committed
16
void argmin(hipStream_t stream, const argument& result, const argument& arg, int axis)
17
18
19
{
    auto lens        = arg.get_shape().lens();
    auto batch_lens  = lens;
Shucai Xiao's avatar
Shucai Xiao committed
20
    size_t batch_item_num = lens[axis];
21
22
23
    batch_lens[axis] = 1;
    migraphx::shape batch_shape{shape::float_type, batch_lens};

Shucai Xiao's avatar
Shucai Xiao committed
24
25
26
27
28
29
30
31
    hip_visit_all(result, arg, batch_shape)([&](auto output, auto input, auto batch) {
        // use one block for items in one batch.
        const size_t max_block_size = 1024;
        size_t block_size           = 1;
        while(block_size < max_block_size and block_size < batch_item_num)
        {
            block_size *= 2;
        }
32

Shucai Xiao's avatar
Shucai Xiao committed
33
34
35
36
37
        launch(
            stream, batch_shape.elements() * block_size, block_size)([=](auto idx) __device__ {
            size_t thr_idx = idx.local;
            size_t blk_idx = idx.group;
            using type = device_type<std::remove_cv_t<typename decltype(output)::value_type>>;
38

Shucai Xiao's avatar
Shucai Xiao committed
39
40
41
42
43
44
45
46
47
48
49
50
            auto batch_idx = batch.multi(blk_idx);
            auto data_idx  = batch_idx;
            MIGRAPHX_DEVICE_SHARED type lds_data[max_block_size + 1];
            MIGRAPHX_DEVICE_SHARED int64_t lds_index[max_block_size + 1];
            // load data to lds_data
            size_t round_item_num = (batch_item_num + block_size - 1) / block_size * block_size;
            size_t remaining_item_num = batch_item_num;
            lds_data[max_block_size] = input[0];
            lds_index[max_block_size] = 0;
            for(size_t i = thr_idx; i < round_item_num; i += block_size)
            {
                if (i < batch_item_num)
51
                {
Shucai Xiao's avatar
Shucai Xiao committed
52
                    data_idx[axis]     = i;
53
                    lds_index[thr_idx] = i;
Shucai Xiao's avatar
Shucai Xiao committed
54
55
56
                    lds_data[thr_idx]  = input[data_idx];
                }
                __syncthreads();
57

Shucai Xiao's avatar
Shucai Xiao committed
58
59
                auto item_num   = (remaining_item_num > block_size) ? block_size : remaining_item_num;
                reduce_argmin(lds_data, lds_index, block_size, thr_idx, size, max_block_size);
60

Shucai Xiao's avatar
Shucai Xiao committed
61
62
                remaining_item_num -= block_size;
            }
63

Shucai Xiao's avatar
Shucai Xiao committed
64
65
66
67
            if (thr_idx == 0)
            {
                output[batch_idx] = lds_index[max_block_size];
            }
68
69
70
71
72
73
74
75
76
77
        });
    });

    return args.back();
}

} // namespace device
} // namespace gpu
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx