random.hpp Source File

random.hpp Source File#

Composable Kernel: 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
12namespace ck_tile {
13
14// return 0 if data is not fp16 or fp32
15template <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
22template <uint32_t seed_>
23struct prand_generator_t<float, seed_>
24{
25 CK_TILE_HOST_DEVICE uint32_t operator()(int id, float val, uint32_t seed = seed_)
26 {
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
41template <uint32_t seed_>
43{
45 {
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 tile/core/algorithm/cluster_descriptor.hpp:13
_Float16 half_t
Definition half.hpp:111
CK_TILE_HOST_DEVICE constexpr Y bit_cast(const X &x)
Definition bit_cast.hpp:11
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