random_gen.hpp 2.07 KB
Newer Older
1
// SPDX-License-Identifier: MIT
arai713's avatar
arai713 committed
2
// Copyright (c) 2018-2025, Advanced Micro Devices, Inc. All rights reserved.
3
4

#pragma once
arai713's avatar
arai713 committed
5
#include <ck/utility/ignore.hpp>
6
7
#include "ck/ck.hpp"

arai713's avatar
arai713 committed
8
9
10
11
12
#ifdef CK_CODE_GEN_RTC
using uint8_t  = unsigned char;
using uint16_t = unsigned short;
using uint32_t = unsigned int;
#endif
13
14
15
16
namespace ck {

// Pseudo random number generator
// version for fp32
arai713's avatar
arai713 committed
17
template <typename T, uint32_t seed_t, ck::enable_if_t<std::is_same<float, T>{}, bool> = false>
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
__host__ __device__ uint32_t prand_generator(index_t id, T val, uint32_t seed = seed_t)
{
    uint32_t x         = *(reinterpret_cast<uint32_t*>(&val));
    uint32_t drop_bits = uint32_t(x) & 0xFFFFu;
    drop_bits ^= x >> 16;
    drop_bits = ((drop_bits & 31) << 11) | (drop_bits >> 5);
    drop_bits *= 0x7000149;
    // NOTE: If id is in 64 bit, we are only using lower 32 bit.
    //       So, it can have an effect of using same id for multiple elements when the id is very
    //       large!
    uint32_t rng = (drop_bits ^ 0x13371337 ^ (id * 229791) ^ seed);
    return rng;
}

// version for fp16
arai713's avatar
arai713 committed
33
template <typename T, uint32_t seed_t, ck::enable_if_t<std::is_same<_Float16, T>{}, bool> = false>
34
35
36
37
38
39
40
41
42
43
44
45
46
47
__host__ __device__ uint32_t prand_generator(index_t id, T val, uint32_t seed = seed_t)
{
    uint16_t x         = *(reinterpret_cast<uint16_t*>(&val));
    uint32_t drop_bits = uint32_t(x) & 0xFFFFu;
    drop_bits          = ((drop_bits & 31) << 11) | (drop_bits >> 5);
    drop_bits *= 0x7000149;
    // NOTE: If id is in 64 bit, we are only using lower 32 bit.
    //       So, it can have an effect of using same id for multiple elements when the id is very
    //       large!
    uint32_t rng = (drop_bits ^ 0x13371337 ^ (id * 229791) ^ seed);
    return rng;
}

// return 0 if data is not fp16 or fp32
arai713's avatar
arai713 committed
48
49
50
template <typename T,
          uint32_t seed_t,
          ck::enable_if_t<!(std::is_same<float, T>{} || std::is_same<_Float16, T>{}), bool> = false>
51
52
__host__ __device__ uint32_t prand_generator(int id, T val, uint32_t seed = seed_t)
{
arai713's avatar
arai713 committed
53
54
55
    ck::ignore = id;
    ck::ignore = val;
    ck::ignore = seed;
56
57
58
59
60

    return 0;
}

} // namespace ck