topk_softmax.cpp 10.8 KB
Newer Older
carlushuang's avatar
carlushuang committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// SPDX-License-Identifier: MIT
// Copyright (c) 2018-2024, Advanced Micro Devices, Inc. All rights reserved.

#include <vector>
#include <iostream>
#include <numeric>
#include <cassert>
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <unordered_set>

#include "ck_tile/core.hpp"
#include "ck_tile/ops/reduce.hpp"
#include "topk_softmax_api.hpp"

17
18
19
#ifndef TEST_TOPK_SOFTMAX_VERBOSE
#define TEST_TOPK_SOFTMAX_VERBOSE 1
#endif
carlushuang's avatar
carlushuang committed
20

carlushuang's avatar
carlushuang committed
21
22
23
24
25
// set this to 1 if input/output have stride
#ifndef TEST_TOPK_VERIFY_PER_TOKEN
#define TEST_TOPK_VERIFY_PER_TOKEN 1
#endif

carlushuang's avatar
carlushuang committed
26
27
28
29
30
31
32
33
template <typename T>
void dump_host_tensor_2d(const ck_tile::HostTensor<T>& x)
{
    auto len = x.get_lengths();
    assert(len.size() == 2);
    std::cout << "[";
    for(size_t i = 0; i < len[0]; i++)
    {
34
        std::cout << i << ": [";
carlushuang's avatar
carlushuang committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
        for(size_t j = 0; j < len[1]; j++)
        {
            if constexpr(std::is_same_v<T, ck_tile::fp16_t>)
            {
                auto v = ck_tile::type_convert<float>(x(std::vector<std::size_t>{i, j}));

                std::cout << v;
                if(j != len[1] - 1)
                    std::cout << ",";
            }
            else
            {
                std::cout << x(std::vector<std::size_t>{i, j}) << " ";
            }
        }
        std::cout << "]";
        if(i != len[0] - 1)
            std::cout << ",";
        else
            std::cout << "]";
        std::cout << std::endl;
    }
    std::cout << "--------------------" << std::endl;
}

// CPU reference
template <typename InputType, typename WeightType, typename IndexType = ck_tile::index_t>
auto reference_topk_softmax(const ck_tile::HostTensor<InputType>& x,
                            ck_tile::index_t k,
                            ck_tile::index_t dim = -1,
                            bool largest         = true,
                            bool sorted          = true)
{
    using namespace ck_tile;

    auto y = reference_softmax<InputType, WeightType, WeightType>(x, dim);

    auto [y_values, y_indices] = reference_topk(y, k, dim, largest, sorted);

    return ck_tile::make_tuple(y_values, y_indices);
}

carlushuang's avatar
carlushuang committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
template <typename InputType, typename WeightType, typename IndexType = ck_tile::index_t>
auto reference_topk_softmax(const ck_tile::HostTensor<InputType>& x,
                            ck_tile::HostTensor<WeightType>& y_values,
                            ck_tile::HostTensor<IndexType>& y_indices,
                            ck_tile::index_t k,
                            ck_tile::index_t dim = -1,
                            bool largest         = true,
                            bool sorted          = true)
{
    using namespace ck_tile;

    // dump_host_tensor_2d(x);

    auto y = reference_softmax<InputType, WeightType, WeightType>(x, dim);

    // dump_host_tensor_2d(y);
    reference_topk(y, y_values, y_indices, k, dim, largest, sorted);
}

carlushuang's avatar
carlushuang committed
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
// different threshold for different dtype
template <typename DataType>
auto get_elimit(std::string /*init_method*/)
{
    double rtol = 1e-3;
    double atol = 1e-3;
    return ck_tile::make_tuple(rtol, atol);
}

template <>
auto get_elimit<ck_tile::bf16_t>(std::string /*init_method*/)
{
    double rtol = 1e-2;
    double atol = 1e-2;
    return ck_tile::make_tuple(rtol, atol);
}

template <>
auto get_elimit<ck_tile::fp8_t>(std::string init_method)
{
    if(init_method == "ui" || init_method == "ni")
    {
        unsigned max_rounding_point_distance = 0;
        double atol                          = 2e-3;
        return ck_tile::make_tuple(max_rounding_point_distance, atol);
    }
    else
    {
        unsigned max_rounding_point_distance = 1;
        double atol                          = 0.0625;
        return ck_tile::make_tuple(max_rounding_point_distance, atol);
    }
}

auto create_args(int argc, char* argv[])
{
    ck_tile::ArgParser arg_parser;
    arg_parser.insert("v", "1", "weather do CPU validation or not")
carlushuang's avatar
carlushuang committed
134
135
        .insert("pr_i", "fp16", "input data type. fp16/fp32 (representing 8/16/32 bit data)")
        .insert("pr_w", "fp32", "weight data type(currently only fp32 supported now)")
carlushuang's avatar
carlushuang committed
136
137
138
        .insert("t", "32", "number of input tokens")
        .insert("e", "8", "number of experts")
        .insert("k", "2", "topk")
carlushuang's avatar
carlushuang committed
139
140
        .insert("st_i", "-1", "row stride of input, -1 means same as experts")
        .insert("st_o", "-1", "row stride of output/indices, -1 means same as topk")
141
        .insert("seed", "-1", "seed to be used, -1 means random every time")
carlushuang's avatar
carlushuang committed
142
143
144
145
146
147
148
149
150
151
        .insert("kname", "0", "t to 1 will print kernel name");

    bool result = arg_parser.parse(argc, argv);
    return std::make_tuple(result, arg_parser);
}

template <typename InputType, typename WeightType, typename IndexType = ck_tile::index_t>
bool test_topk_softmax(ck_tile::ArgParser args)
{
    int validate            = args.get_int("v");
carlushuang's avatar
carlushuang committed
152
153
    std::string input_prec  = args.get_str("pr_i");
    std::string weight_prec = args.get_str("pr_w");
carlushuang's avatar
carlushuang committed
154
155
156
    int tokens              = args.get_int("t");
    int experts             = args.get_int("e");
    int topk                = args.get_int("k");
157
    int seed                = args.get_int("seed");
carlushuang's avatar
carlushuang committed
158
159
160
161
162
163
164
165
166
167
168
169
170
    int stride_input        = args.get_int("st_i");
    int stride_output       = args.get_int("st_o");
    if(stride_input < 0)
    {
        stride_input = experts;
    }
    if(stride_output < 0)
    {
        stride_output = topk;
    }
    assert(stride_input >= experts);
    assert(stride_output >= topk);

171
172
173
174
    if(seed < 0)
    {
        seed = std::time(nullptr);
    }
carlushuang's avatar
carlushuang committed
175
176
177
    // int kname = args.get_int("kname");
    // int warmup = args.get_int("warmup");
    // int repeat = args.get_int("repeat");
178
179
180
181
182
183
184
185

    if(topk > experts)
    {
#if TEST_TOPK_SOFTMAX_VERBOSE
        printf("topk:%d should smaller than (or equal to) experts:%d\n", topk, experts);
#endif
        return false;
    }
carlushuang's avatar
carlushuang committed
186
187

    // tokens already considered batch size
carlushuang's avatar
carlushuang committed
188
189
190
    ck_tile::HostTensor<InputType> x_host({tokens, experts}, {stride_input, 1});
    ck_tile::HostTensor<WeightType> value_host({tokens, topk}, {stride_output, 1});
    ck_tile::HostTensor<IndexType> index_host({tokens, topk}, {stride_output, 1});
carlushuang's avatar
carlushuang committed
191

192
193
194
195
196
197
198
199
200
    {
        // random require per-row unique
        auto rand_gen = ck_tile::FillUniformDistribution_Unique<InputType>{
            -5.f, 5.f, static_cast<uint32_t>(seed)};

        for(int i_t = 0; i_t < tokens; i_t++)
        {
            ck_tile::HostTensor<InputType> x_row({experts});
            rand_gen(x_row);
carlushuang's avatar
carlushuang committed
201
            std::copy(x_row.begin(), x_row.end(), x_host.begin() + i_t * stride_input);
202
203
204
            rand_gen.clear();
        }
    }
carlushuang's avatar
carlushuang committed
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221

    ck_tile::DeviceMem x_dev(x_host.get_element_space_size_in_bytes());
    ck_tile::DeviceMem value_dev(value_host.get_element_space_size_in_bytes());
    ck_tile::DeviceMem index_dev(index_host.get_element_space_size_in_bytes());

    x_dev.ToDevice(x_host.data());

    topk_softmax_trait trait = [&]() {
        topk_softmax_trait t_;
        t_.input_type  = input_prec;
        t_.weight_type = weight_prec;
        t_.experts     = experts;
        return t_;
    }();

    topk_softmax_kargs karg = [&]() {
        topk_softmax_kargs a_;
carlushuang's avatar
carlushuang committed
222
223
224
225
226
227
228
229
        a_.p_input       = x_dev.GetDeviceBuffer();
        a_.p_output      = value_dev.GetDeviceBuffer();
        a_.p_indices     = index_dev.GetDeviceBuffer();
        a_.num_rows      = tokens;
        a_.num_experts   = experts;
        a_.topk          = topk;
        a_.stride_input  = stride_input;
        a_.stride_output = stride_output;
carlushuang's avatar
carlushuang committed
230
231
232
        return a_;
    }();

233
234
#if TEST_TOPK_SOFTMAX_VERBOSE
    ck_tile::stream_config sc{nullptr, true};
carlushuang's avatar
carlushuang committed
235
    // ck_tile::stream_config sc{nullptr};
236
    auto ms = topk_softmax(trait, karg, sc);
carlushuang's avatar
carlushuang committed
237
    printf("[%s|%s]tokens:%d, experts:%d, topk:%d, st_i:%d, st_o:%d, ms:%f, ",
238
239
240
241
242
           input_prec.c_str(),
           weight_prec.c_str(),
           tokens,
           experts,
           topk,
carlushuang's avatar
carlushuang committed
243
244
           stride_input,
           stride_output,
245
           ms);
carlushuang's avatar
carlushuang committed
246
247
    if(ms < 0)
        printf("not supported\n");
248
249
    fflush(stdout);
#else
carlushuang's avatar
carlushuang committed
250
    ck_tile::stream_config sc{nullptr};
carlushuang's avatar
carlushuang committed
251
    auto ms = topk_softmax(trait, karg, sc);
252
#endif
carlushuang's avatar
carlushuang committed
253
254
255
256
    if(ms < 0)
    {
        return false;
    }
carlushuang's avatar
carlushuang committed
257
258
259
260
261
262
263

    value_dev.FromDevice(value_host.data());
    index_dev.FromDevice(index_host.data());

    bool rtn = true;
    if(validate)
    {
carlushuang's avatar
carlushuang committed
264
265
266
        // this host buffer will not copy to GPU, so no need use stride
        ck_tile::HostTensor<WeightType> value_ref({tokens, topk}, {stride_output, 1});
        ck_tile::HostTensor<IndexType> index_ref({tokens, topk}, {stride_output, 1});
carlushuang's avatar
carlushuang committed
267

carlushuang's avatar
carlushuang committed
268
269
270
        // auto [value_ref, index_ref] =
        reference_topk_softmax<InputType, WeightType, IndexType>(
            x_host, value_ref, index_ref, topk);
carlushuang's avatar
carlushuang committed
271
272

        auto [rtol, atol] = get_elimit<InputType>("");
carlushuang's avatar
carlushuang committed
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#if TEST_TOPK_VERIFY_PER_TOKEN
        for(int i_t = 0; i_t < tokens; i_t++)
        {
            auto s_begin = std::vector<size_t>{static_cast<size_t>(i_t), static_cast<size_t>(0)};
            auto s_end =
                std::vector<size_t>{static_cast<size_t>(i_t + 1), static_cast<size_t>(topk)};
            auto s_value_host = value_host.slice(s_begin, s_end);
            auto s_value_ref  = value_ref.slice(s_begin, s_end);
            rtn &= ck_tile::check_err(s_value_host,
                                      s_value_ref,
                                      std::string("[") + std::to_string(i_t) +
                                          std::string("] Value Error:"),
                                      rtol,
                                      atol);
            auto s_index_host = index_host.slice(s_begin, s_end);
            auto s_index_ref  = index_ref.slice(s_begin, s_end);
            rtn &= ck_tile::check_err(s_index_host,
                                      s_index_ref,
                                      std::string("[") + std::to_string(i_t) +
                                          std::string("] Index Error:"),
                                      rtol,
                                      atol);
        }
#else
carlushuang's avatar
carlushuang committed
297
298
299
300
        rtn &= ck_tile::check_err(
            value_host, value_ref, std::string("Value Error: Incorrect results!"), rtol, atol);
        rtn &= ck_tile::check_err(
            index_host, index_ref, std::string("Index Error: Incorrect results!"), rtol, atol);
carlushuang's avatar
carlushuang committed
301
#endif
carlushuang's avatar
carlushuang committed
302
    }
303
304
305
306
#if TEST_TOPK_SOFTMAX_VERBOSE
    printf("valid:%s\n", rtn ? "y" : "n");
    fflush(stdout);
#endif
carlushuang's avatar
carlushuang committed
307
308
309
310
311
312
313
314
    return rtn;
}

int main(int argc, char** argv)
{
    auto [result, args] = create_args(argc, argv);
    if(!result)
        return -1;
carlushuang's avatar
carlushuang committed
315
316
    std::string input_prec  = args.get_str("pr_i");
    std::string weight_prec = args.get_str("pr_w");
carlushuang's avatar
carlushuang committed
317
318
319
320
321
322
323
324
325
326
327
328
329

    bool r = true;
    if(input_prec.compare("fp16") == 0 && weight_prec.compare("fp32") == 0)
    {
        r &= test_topk_softmax<ck_tile::fp16_t, float, ck_tile::index_t>(args);
    }
    else if(input_prec.compare("bf16") == 0 && weight_prec.compare("fp32") == 0)
    {
        r &= test_topk_softmax<ck_tile::bf16_t, float, ck_tile::index_t>(args);
    }

    return r ? 0 : -1;
}