logsoftmax.cpp 2.82 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <migraphx/shape.hpp>
#include <migraphx/argument.hpp>
#include <migraphx/gpu/device/logsoftmax.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 logsoftmax(hipStream_t stream,
Shucai Xiao's avatar
Shucai Xiao committed
15
16
17
                    const migraphx::shape& output_shape,
                    std::vector<migraphx::argument> args,
                    int axis)
18
19
{

Shucai Xiao's avatar
Shucai Xiao committed
20
    auto lens         = output_shape.lens();
21
    auto num_in_batch = lens[axis];
Shucai Xiao's avatar
Shucai Xiao committed
22
23
    auto batch_lens   = lens;
    batch_lens[axis]  = 1;
24
    migraphx::shape batch_shape{output_shape.type(), batch_lens};
25
26

    visit_all(args.back(), args.front())([&](auto output, auto input) {
Shucai Xiao's avatar
Shucai Xiao committed
27
28
        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);
32

33
34
35
            // each thread is for one item in the batch
            gs_launch(stream, batch_shape.elements())([=](auto i) {
                auto batch_idx = desc_batch.multi(i);
Shucai Xiao's avatar
Shucai Xiao committed
36
                auto data_idx  = batch_idx;
37

38
39
40
41
42
                // get max
                auto batch_max = input_ptr[desc_data.linear(batch_idx)];
                for(std::size_t j = 1; j < num_in_batch; ++j)
                {
                    data_idx[axis] = j;
Shucai Xiao's avatar
Shucai Xiao committed
43
44
                    size_t idx     = desc_data.linear(data_idx);
                    batch_max      = std::max(to_hip_type(batch_max), to_hip_type(input_ptr[idx]));
45
                }
46

47
48
                for(std::size_t j = 0; j < num_in_batch; ++j)
                {
Shucai Xiao's avatar
Shucai Xiao committed
49
50
                    data_idx[axis]  = j;
                    size_t idx      = desc_data.linear(data_idx);
51
52
                    output_ptr[idx] = input_ptr[idx] - batch_max;
                }
53

54
55
56
57
                auto batch_sum = ::exp(to_hip_type(output_ptr[desc_data.linear(batch_idx)]));
                for(std::size_t j = 1; j < num_in_batch; ++j)
                {
                    data_idx[axis] = j;
Shucai Xiao's avatar
Shucai Xiao committed
58
                    size_t idx     = desc_data.linear(data_idx);
59
60
61
62
63
64
65
                    batch_sum += ::exp(to_hip_type(output_ptr[idx]));
                }
                batch_sum = ::log(to_hip_type(batch_sum));

                for(std::size_t j = 0; j < num_in_batch; ++j)
                {
                    data_idx[axis] = j;
Shucai Xiao's avatar
Shucai Xiao committed
66
                    size_t idx     = desc_data.linear(data_idx);
67
68
69
                    output_ptr[idx] -= batch_sum;
                }
            });
70
71
72
73
74
75
76
77
78
79
        });
    });

    return args.back();
}

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