softmax.cpp 2.46 KB
Newer Older
Khalique's avatar
Khalique committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <migraphx/shape.hpp>
#include <migraphx/argument.hpp>
#include <migraphx/dfor.hpp>
#include <migraphx/gpu/device/softmax.hpp>
#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 {

argument softmax(hipStream_t stream,
Khalique's avatar
Khalique committed
16
17
18
                 const migraphx::shape& output_shape,
                 std::vector<migraphx::argument> args,
                 int axis)
Khalique's avatar
Khalique committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
{
    auto lens              = output_shape.lens();
    std::size_t batch_size = std::accumulate(
        lens.begin(), lens.begin() + axis, std::size_t{1}, std::multiplies<std::size_t>());
    std::size_t n_dims = std::accumulate(
        lens.begin() + axis, lens.end(), std::size_t{1}, std::multiplies<std::size_t>());
    migraphx::shape comp_shape{output_shape.type(), {batch_size, n_dims}};

    visit_all(args.back(), args.front())([&](auto output, auto input) {
        const auto* input_ptr = device_cast(input.data());
        auto* output_ptr      = device_cast(output.data());

        // each thread is for one item in the batch
        gs_launch(stream, batch_size)([=](auto i) {
            std::size_t row_start = i * n_dims;
            // get max
            auto batch_max = input_ptr[row_start];
Khalique's avatar
Khalique committed
36
            for(std::size_t j = 1; j < n_dims; ++j)
Khalique's avatar
Khalique committed
37
            {
Khalique's avatar
Khalique committed
38
39
                auto ind  = row_start + j;
                batch_max = std::max(to_hip_type(batch_max), to_hip_type(input_ptr[ind]));
Khalique's avatar
Khalique committed
40
41
42
43
44
45
46
47
48
49
50
51
            }

            for(std::size_t j = 0; j < n_dims; ++j)
            {
                auto ind        = row_start + j;
                output_ptr[ind] = input_ptr[ind] - batch_max;
            }

            for(std::size_t j = 0; j < n_dims; ++j)
            {
                auto ind        = row_start + j;
                output_ptr[ind] = exp(to_hip_type(input_ptr[ind]));
Khalique's avatar
Khalique committed
52
53
54
55
56
57
            }

            auto batch_sum = output_ptr[row_start];
            for(std::size_t j = 1; j < n_dims; ++j)
            {
                auto ind = row_start + j;
Khalique's avatar
Khalique committed
58
                batch_sum += output_ptr[ind];
Khalique's avatar
Khalique committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
            }

            for(std::size_t j = 0; j < n_dims; ++j)
            {
                auto ind = row_start + j;
                output_ptr[ind] /= batch_sum;
            }
        });
    });

    return args.back();
}

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