test_softmax_util.hpp 11.9 KB
Newer Older
Chao Liu's avatar
Chao Liu committed
1
// SPDX-License-Identifier: MIT
Illia Silin's avatar
Illia Silin committed
2
// Copyright (c) 2018-2023, Advanced Micro Devices, Inc. All rights reserved.
Chao Liu's avatar
Chao Liu committed
3

4
5
#pragma once

6
7
8
#include <string>
#include <sstream>
#include <tuple>
9
#include <vector>
Chao Liu's avatar
Chao Liu committed
10
11
12
#include <gtest/gtest.h>

#include "ck/ck.hpp"
Adam Osewski's avatar
Adam Osewski committed
13
14
#include "ck/tensor_operation/gpu/device/impl/device_softmax_impl.hpp"
#include "ck/tensor_operation/gpu/element/element_wise_operation.hpp"
15
#include "include/ck/utility/data_type.hpp"
16
#include "profiler/profile_softmax_impl.hpp"
17
18
19

namespace ck {

20
21
22
23
24
25
26
27
28
29
30
31
template <typename Range>
std::string serialize_range(const Range& range)
{
    std::stringstream ss;
    for(auto& r : range)
    {
        ss << r << ", ";
    }
    std::string str = ss.str();
    return std::string(str.begin(), str.end() - 2);
}

32
33
34
35
template <typename Tuple>
class TestSoftmax : public ::testing::Test
{
    protected:
36
37
38
39
40
41
42
43
44
45
46
47
    using InDataType              = std::tuple_element_t<0, Tuple>;
    using AccDataType             = std::tuple_element_t<1, Tuple>;
    using OutDataType             = std::tuple_element_t<2, Tuple>;
    static constexpr index_t Rank = std::tuple_element_t<3, Tuple>{}.value;

    public:
    std::vector<std::vector<index_t>> in_lengths_ = {{2, 128, 1024}, {4, 16, 8448}, {128, 128, 64}};
    std::vector<std::vector<AccDataType>> scales_ = {{2, 0}, {0, 2}, {2, 2}};
    bool bench_                                   = false; // measure kernel performance
    bool verify_                                  = true;

    void SetUp() override
48
    {
49
        if constexpr(Rank == 4)
50
        {
51
52
            in_lengths_ = std::vector<std::vector<index_t>>{
                {1, 2, 128, 1024}, {2, 4, 16, 8448}, {1, 128, 128, 64}};
53
        }
54
    }
55

56
57
58
59
60
61
62
63
    void RunSingle(std::vector<index_t> in_length,
                   std::vector<index_t> reduce_dims,
                   AccDataType alpha,
                   AccDataType beta)
    {
        int init_method = 1; // integer value initialization
        bool log        = false;
        std::vector<ck::index_t> strides; // intenionally empty, to get packed layout.
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
        bool pass = false;

        if constexpr(Rank == 3)
        {
            if(reduce_dims.size() == 1)
                pass = ck::profiler::
                    profile_softmax_impl<InDataType, AccDataType, OutDataType, Rank, 1>(verify_,
                                                                                        init_method,
                                                                                        log,
                                                                                        bench_,
                                                                                        in_length,
                                                                                        strides,
                                                                                        reduce_dims,
                                                                                        alpha,
                                                                                        beta);
            else if(reduce_dims.size() == 2)
                pass = ck::profiler::
                    profile_softmax_impl<InDataType, AccDataType, OutDataType, Rank, 2>(verify_,
                                                                                        init_method,
                                                                                        log,
                                                                                        bench_,
                                                                                        in_length,
                                                                                        strides,
                                                                                        reduce_dims,
                                                                                        alpha,
                                                                                        beta);
            else if(reduce_dims.size() == 3)
                pass = ck::profiler::
                    profile_softmax_impl<InDataType, AccDataType, OutDataType, Rank, 3>(verify_,
                                                                                        init_method,
                                                                                        log,
                                                                                        bench_,
                                                                                        in_length,
                                                                                        strides,
                                                                                        reduce_dims,
                                                                                        alpha,
                                                                                        beta);
        }
        else if constexpr(Rank == 4)
        {
            if(reduce_dims.size() == 1)
                pass = ck::profiler::
                    profile_softmax_impl<InDataType, AccDataType, OutDataType, Rank, 1>(verify_,
                                                                                        init_method,
                                                                                        log,
                                                                                        bench_,
                                                                                        in_length,
                                                                                        strides,
                                                                                        reduce_dims,
                                                                                        alpha,
                                                                                        beta);
            else if(reduce_dims.size() == 2)
                pass = ck::profiler::
                    profile_softmax_impl<InDataType, AccDataType, OutDataType, Rank, 2>(verify_,
                                                                                        init_method,
                                                                                        log,
                                                                                        bench_,
                                                                                        in_length,
                                                                                        strides,
                                                                                        reduce_dims,
                                                                                        alpha,
                                                                                        beta);
            else if(reduce_dims.size() == 3)
                pass = ck::profiler::
                    profile_softmax_impl<InDataType, AccDataType, OutDataType, Rank, 3>(verify_,
                                                                                        init_method,
                                                                                        log,
                                                                                        bench_,
                                                                                        in_length,
                                                                                        strides,
                                                                                        reduce_dims,
                                                                                        alpha,
                                                                                        beta);
            else if(reduce_dims.size() == 4)
                pass = ck::profiler::
                    profile_softmax_impl<InDataType, AccDataType, OutDataType, Rank, 4>(verify_,
                                                                                        init_method,
                                                                                        log,
                                                                                        bench_,
                                                                                        in_length,
                                                                                        strides,
                                                                                        reduce_dims,
                                                                                        alpha,
                                                                                        beta);
        };

150
151
        EXPECT_TRUE(pass);
    }
152

153
154
155
    void Run(std::vector<index_t> reduce_dims = {})
    {
        if(reduce_dims.empty())
156
        {
157
            reduce_dims.push_back(Rank - 1);
158
        }
159
160
161
162
163

        for(auto in_length : this->in_lengths_)
        {
            for(auto scale : this->scales_)
            {
164
                this->RunSingle(in_length, reduce_dims, scale[0], scale[1]);
165
166
167
            }
        }
    }
168
};
169

170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
template <index_t Rank,
          index_t NumReduceDim,
          index_t BlockSize,
          index_t MThreadClusterSize,
          index_t KThreadClusterSize,
          index_t MThreadSliceSize,
          index_t KThreadSliceSize,
          index_t InSrcVectorDim,
          index_t InSrcVectorSize,
          index_t OutDstVectorSize>
struct DeviceSoftmaxInstanceWrapper
{
    using F16  = half_t;
    using F32  = float;
    using Pass = tensor_operation::element_wise::PassThrough;

    using InDataType   = F16;
    using AccDataType  = F32;
    using OutDataType  = F16;
    using InElementOp  = Pass;
    using AccElementOp = Pass;

    using DeviceSoftmaxInstance = tensor_operation::device::DeviceSoftmaxImpl<InDataType,
                                                                              AccDataType,
                                                                              OutDataType,
                                                                              InElementOp,
                                                                              AccElementOp,
                                                                              Rank,
                                                                              NumReduceDim,
                                                                              BlockSize,
                                                                              MThreadClusterSize,
                                                                              KThreadClusterSize,
                                                                              MThreadSliceSize,
                                                                              KThreadSliceSize,
                                                                              InSrcVectorDim,
                                                                              InSrcVectorSize,
                                                                              OutDstVectorSize>;

    bool IsSupported(const std::vector<index_t> in_lengths,
                     const std::vector<index_t> in_strides,
                     const std::vector<index_t> reduce_dims) const
    {
        auto softmax  = DeviceSoftmaxInstance{};
        auto argument = softmax.MakeArgument(in_lengths,
                                             in_strides,
                                             reduce_dims,
                                             1,       // alpha
                                             1,       // beta
                                             nullptr, // in_dev
                                             nullptr, // in_out
                                             Pass{},  // in elementwise op
                                             Pass{}); // acc elementwise op
        return softmax.IsSupportedArgument(argument);
    }
224
};
225

226
} // namespace ck