|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#pragma once |
| 10 | + |
| 11 | +#include <ATen/ATen.h> |
| 12 | + |
| 13 | +#define AT_DISPATCH_INTEGER_TYPES(TYPE, NAME, HINT, ...) \ |
| 14 | + AT_DISPATCH_SWITCH( \ |
| 15 | + TYPE, \ |
| 16 | + NAME, \ |
| 17 | + AT_PRIVATE_CASE_TYPE_USING_HINT(at::ScalarType::Int, HINT, __VA_ARGS__) \ |
| 18 | + AT_PRIVATE_CASE_TYPE_USING_HINT( \ |
| 19 | + at::ScalarType::Long, HINT, __VA_ARGS__)) |
| 20 | + |
| 21 | +namespace torch::torchrec::turborec { |
| 22 | + |
| 23 | +#if defined(TORBOREC_CUDA) |
| 24 | +#define TORBOREC_INLINE __device__ __host__ __inline__ |
| 25 | +#else |
| 26 | +#define TORBOREC_INLINE inline |
| 27 | +#endif |
| 28 | + |
| 29 | +// NOLINTNEXTLINE: |
| 30 | +TORBOREC_INLINE uint64_t |
| 31 | +murmur_hash3_2x64(const uint64_t x, const uint64_t y, const uint64_t seed) { |
| 32 | + const uint64_t c1 = 0x87c37b91114253d5; |
| 33 | + const uint64_t c2 = 0x4cf5ad432745937f; |
| 34 | + |
| 35 | + uint64_t h1 = seed; |
| 36 | + uint64_t h2 = seed; |
| 37 | + |
| 38 | + // First 64-bit block |
| 39 | + uint64_t k1 = x; |
| 40 | + k1 *= c1; |
| 41 | + k1 = (k1 << 31) | (k1 >> (64 - 31)); |
| 42 | + k1 *= c2; |
| 43 | + h1 ^= k1; |
| 44 | + h1 = (h1 << 27) | (h1 >> (64 - 27)); |
| 45 | + h1 += h2; |
| 46 | + h1 = h1 * 5 + 0x52dce729; |
| 47 | + |
| 48 | + // Second 64-bit block |
| 49 | + uint64_t k2 = y; |
| 50 | + k2 *= c2; |
| 51 | + k2 = (k2 << 33) | (k2 >> (64 - 33)); |
| 52 | + k2 *= c1; |
| 53 | + h2 ^= k2; |
| 54 | + h2 = (h2 << 31) | (h2 >> (64 - 31)); |
| 55 | + h2 += h1; |
| 56 | + h2 = h2 * 5 + 0x38495ab5; |
| 57 | + |
| 58 | + // Finalization |
| 59 | + h1 ^= 16; |
| 60 | + h2 ^= 16; |
| 61 | + h1 += h2; |
| 62 | + h2 += h1; |
| 63 | + h1 ^= h1 >> 33; |
| 64 | + h1 *= 0xff51afd7ed558ccd; |
| 65 | + h1 ^= h1 >> 33; |
| 66 | + h1 *= 0xc4ceb9fe1a85ec53; |
| 67 | + h1 ^= h1 >> 33; |
| 68 | + h2 ^= h2 >> 33; |
| 69 | + h2 *= 0xff51afd7ed558ccd; |
| 70 | + h2 ^= h2 >> 33; |
| 71 | + h2 *= 0xc4ceb9fe1a85ec53; |
| 72 | + h2 ^= h2 >> 33; |
| 73 | + h1 += h2; |
| 74 | + h2 += h1; |
| 75 | + |
| 76 | + return h1 ^ h2; |
| 77 | +} |
| 78 | + |
| 79 | +// NOLINTNEXTLINE: |
| 80 | +template <bool CIRCULAR_PROBE> |
| 81 | +TORBOREC_INLINE int64_t next_output_index( |
| 82 | + int64_t output_index, |
| 83 | + int64_t modulo, |
| 84 | + int64_t& /* max_probe_local */) { |
| 85 | + static_assert(CIRCULAR_PROBE); |
| 86 | + return (output_index + 1) % modulo; |
| 87 | +} |
| 88 | + |
| 89 | +// NOLINTNEXTLINE: |
| 90 | +template <> |
| 91 | +TORBOREC_INLINE int64_t next_output_index<false>( |
| 92 | + int64_t output_index, |
| 93 | + int64_t modulo, |
| 94 | + int64_t& max_probe_local) { |
| 95 | + output_index = (output_index + 1) % modulo; |
| 96 | + if (output_index == 0) { |
| 97 | + // circular, using max_probe_local to control exit. |
| 98 | + max_probe_local = 0; |
| 99 | + } |
| 100 | + return output_index; |
| 101 | +} |
| 102 | + |
| 103 | +TORBOREC_INLINE bool is_eviction_enabled( |
| 104 | + bool readonly, |
| 105 | + int eviction_threshold, |
| 106 | + int eviction_policy) { |
| 107 | + return !readonly && (eviction_threshold > 0 || eviction_policy > 0); |
| 108 | +} |
| 109 | + |
| 110 | +#undef TORBOREC_INLINE |
| 111 | + |
| 112 | +} // namespace torch::torchrec::turborec |
0 commit comments