/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/develop/include/ck_tile/core/utility/random.hpp Source File

/home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/develop/include/ck_tile/core/utility/random.hpp Source File#

Composable Kernel: /home/docs/checkouts/readthedocs.org/user_builds/advanced-micro-devices-composable-kernel/checkouts/develop/include/ck_tile/core/utility/random.hpp Source File
random.hpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: MIT
2 // Copyright (c) 2018-2023, Advanced Micro Devices, Inc. All rights reserved.
3 
4 #pragma once
5 
8 #include <stdint.h>
9 #include <tuple>
10 #include <type_traits>
11 
12 namespace ck_tile {
13 
14 // return 0 if data is not fp16 or fp32
15 template <typename T, uint32_t seed_>
17 {
18  CK_TILE_HOST_DEVICE uint32_t operator()(int, T, uint32_t = seed_) { return 0; }
19 };
20 
21 // version for fp32
22 template <uint32_t seed_>
23 struct prand_generator_t<float, seed_>
24 {
25  CK_TILE_HOST_DEVICE uint32_t operator()(int id, float val, uint32_t seed = seed_)
26  {
27  uint32_t x = *(reinterpret_cast<uint32_t*>(&val));
28  uint32_t drop_bits = uint32_t(x) & 0xFFFFu;
29  drop_bits ^= x >> 16;
30  drop_bits = ((drop_bits & 31) << 11) | (drop_bits >> 5);
31  drop_bits *= 0x7000149;
32  // NOTE: If id is in 64 bit, we are only using lower 32 bit.
33  // So, it can have an effect of using same id for multiple elements when the id is
34  // very large!
35  uint32_t rng = (drop_bits ^ 0x13371337 ^ (id * 229791) ^ seed);
36  return rng;
37  }
38 };
39 
40 // version for fp16
41 template <uint32_t seed_>
42 struct prand_generator_t<half_t, seed_>
43 {
45  {
46  uint16_t x = *(reinterpret_cast<uint16_t*>(&val));
47  uint32_t drop_bits = uint32_t(x) & 0xFFFFu;
48  drop_bits = ((drop_bits & 31) << 11) | (drop_bits >> 5);
49  drop_bits *= 0x7000149;
50  // NOTE: If id is in 64 bit, we are only using lower 32 bit.
51  // So, it can have an effect of using same id for multiple elements when the id is
52  // very large!
53  uint32_t rng = (drop_bits ^ 0x13371337 ^ (id * 229791) ^ seed);
54  return rng;
55  }
56 };
57 
58 } // namespace ck_tile
#define CK_TILE_HOST_DEVICE
Definition: config.hpp:42
Definition: cluster_descriptor.hpp:13
_Float16 half_t
Definition: half.hpp:111
unsigned short uint16_t
Definition: stdint.h:125
unsigned int uint32_t
Definition: stdint.h:126
CK_TILE_HOST_DEVICE uint32_t operator()(int id, float val, uint32_t seed=seed_)
Definition: random.hpp:25
CK_TILE_HOST_DEVICE uint32_t operator()(int id, half_t val, uint32_t seed=seed_)
Definition: random.hpp:44
Definition: random.hpp:17
CK_TILE_HOST_DEVICE uint32_t operator()(int, T, uint32_t=seed_)
Definition: random.hpp:18