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

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

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

Paul's avatar
Paul committed
24
    hip_visit_all(result, arg, batch_shape)([&](auto output, auto input, auto batch) {
25
26
27
28
29
30
31
        // 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;
        }
Khalique's avatar
Khalique committed
32

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>>;
Khalique's avatar
Khalique committed
38

39
40
41
42
43
44
45
46
            MIGRAPHX_DEVICE_SHARED type lds_data[max_block_size + 1];
            auto batch_idx = batch.multi(blk_idx);
            auto data_idx  = batch_idx;
            // load data to lds and compute the batch max
            size_t remaining_item_num = batch_item_num;
            size_t round_item_num = (batch_item_num + block_size - 1) / block_size * block_size;
            lds_data[block_size]  = input[0];
            for(size_t i = thr_idx; i < round_item_num; i += block_size)
47
            {
48
                if(i < batch_item_num)
49
                {
50
51
                    data_idx[axis]    = i;
                    lds_data[thr_idx] = input[desc_data.linear(data_idx)];
52
                }
Khalique's avatar
Khalique committed
53

Shucai Xiao's avatar
Shucai Xiao committed
54
55
                __syncthreads();

56
57
58
                auto item_num =
                    (remaining_item_num > block_size) ? block_size : remaining_item_num;
                reduce_max(lds_data, block_size, thr_idx, item_num);
59

60
                remaining_item_num -= block_size;
Paul's avatar
Paul committed
61
            }
62

63
64
            auto batch_max = lds_data[block_size];
            __syncthreads();
65

66
67
68
            lds_data[block_size] = 0;
            remaining_item_num   = batch_item_num;
            for(size_t i = thr_idx; i < round_item_num; i += block_size)
Paul's avatar
Paul committed
69
            {
70
                if(i < batch_item_num)
71
                {
72
                    data_idx[axis]    = i;
73
74
                    lds_data[thr_idx] = input[desc_data.linear(data_idx)] - batch_max;
                    lds_data[thr_idx] = ::exp(to_hip_type(lds_data[thr_idx]));
75
                }
76
77
78
79
80
81
82
83

                __syncthreads();

                auto item_num =
                    (remaining_item_num > block_size) ? block_size : remaining_item_num;
                reduce_sum(lds_data, block_size, thr_idx, item_num);

                remaining_item_num -= block_size;
Paul's avatar
Paul committed
84
            }
85
            auto batch_sum = lds_data[block_size];
Khalique's avatar
Khalique committed
86

87
            for(size_t i = thr_idx; i < batch_item_num; i += block_size)
Paul's avatar
Paul committed
88
            {
89
90
91
92
                data_idx[axis]    = i;
                size_t index      = desc_data.linear(data_idx);
                auto val          = input[index] - batch_max;
                output[index] = ::exp(to_hip_type(val)) / batch_sum;
Paul's avatar
Paul committed
93
            }
Khalique's avatar
Khalique committed
94
95
96
97
98
99
100
101
        });
    });
}

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