softmax.cpp 2.89 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
{
    auto lens              = output_shape.lens();
21
22
23
24
    auto batch_lens = lens;
    size_t n_dims = lens[axis];
    batch_lens[axis] = 1;
    migraphx::shape batch_shape{shape::int32_type, batch_lens};
Khalique's avatar
Khalique committed
25
26
27
28

    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());
29
30
31
        visit_tensor_size(batch_shape.lens().size(), [&](auto n_dim) {
            hip_tensor_descriptor<n_dim> desc_batch(batch_shape);
            hip_tensor_descriptor<n_dim> desc_data(output_shape);
Khalique's avatar
Khalique committed
32

33
34
35
36
37
38
39
40
41
42
43
            // each thread is for one item in the batch
            gs_launch(stream, batch_shape.elements())([=](auto i) {
                auto batch_idx = desc_batch.multi(i);
                auto data_idx = batch_idx;
                // get max
                auto batch_max = input_ptr[desc_data.linear(batch_idx)];
                for(std::size_t j = 1; j < n_dims; ++j)
                {
                    data_idx[axis] = j;
                    batch_max = std::max(to_hip_type(batch_max), to_hip_type(input_ptr[desc_data.linear(data_idx)]));
                }
Khalique's avatar
Khalique committed
44

45
46
47
48
49
50
                for(std::size_t j = 0; j < n_dims; ++j)
                {
                    data_idx[axis] = j;
                    auto idx = desc_data.linear(data_idx);
                    output_ptr[idx] = input_ptr[idx] - batch_max;
                }
Khalique's avatar
Khalique committed
51

52
53
54
55
56
57
                for(std::size_t j = 0; j < n_dims; ++j)
                {
                    data_idx[axis] = j;
                    auto idx = desc_data.linear(data_idx);
                    output_ptr[idx] = exp(to_hip_type(output_ptr[idx]));
                }
Khalique's avatar
Khalique committed
58

59
60
61
62
63
64
                auto batch_sum = output_ptr[desc_data.linear(batch_idx)];
                for(std::size_t j = 1; j < n_dims; ++j)
                {
                    data_idx[axis] = j;
                    batch_sum += output_ptr[desc_data.linear(data_idx)];
                }
Khalique's avatar
Khalique committed
65

66
67
68
69
70
71
72
                for(std::size_t j = 0; j < n_dims; ++j)
                {
                    data_idx[axis] = j;
                    auto idx = desc_data.linear(data_idx);
                    output_ptr[idx] = output_ptr[idx] / batch_sum;
                }
            });
Khalique's avatar
Khalique committed
73
74
75
76
77
78
79
80
81
82
        });
    });

    return args.back();
}

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