Skip to content

Commit a8e49d7

Browse files
kausvfacebook-github-bot
authored andcommitted
Copy Kernels to TorchRec for OSS (pytorch#2819)
Summary: Pull Request resolved: pytorch#2819 Open sourcing the CUDA and CPU kernel ops used by ZCH. This is first step towards open sourcing the module. To ensure internal trainer and predictor packages are updated with the new library linked with the new op, we make copies of the op and only after changes are updated, the new ops will be removed in the follow up diffs stacked Reviewed By: jingsh Differential Revision: D71141352
1 parent 3a74a84 commit a8e49d7

File tree

5 files changed

+3697
-0
lines changed

5 files changed

+3697
-0
lines changed

torchrec/ops/common_utils.cuh

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (C) 2016 ExplosionAI GmbH, 2014-2015 Matthew Honnibal, 2016 spaCy
5+
* GmbH
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*
25+
*/
26+
/*
27+
* Copyright (c) Meta Platforms, Inc. and affiliates.
28+
* All rights reserved.
29+
*
30+
* This source code is licensed under the BSD-style license found in the
31+
* LICENSE file in the root directory of this source tree.
32+
*/
33+
34+
#pragma once
35+
36+
#include <ATen/ATen.h>
37+
38+
#define AT_DISPATCH_INTEGER_TYPES(TYPE, NAME, HINT, ...) \
39+
AT_DISPATCH_SWITCH( \
40+
TYPE, \
41+
NAME, \
42+
AT_PRIVATE_CASE_TYPE_USING_HINT(at::ScalarType::Int, HINT, __VA_ARGS__) \
43+
AT_PRIVATE_CASE_TYPE_USING_HINT( \
44+
at::ScalarType::Long, HINT, __VA_ARGS__))
45+
46+
namespace torch::torchrec::turborec {
47+
48+
#if defined(TORBOREC_CUDA)
49+
#define TORBOREC_INLINE __device__ __host__ __inline__
50+
#else
51+
#define TORBOREC_INLINE inline
52+
#endif
53+
54+
// Inspired by
55+
// https://github.com/explosion/murmurhash/blob/master/murmurhash/MurmurHash3.cpp#L286
56+
// NOLINTNEXTLINE:
57+
TORBOREC_INLINE uint64_t
58+
murmur_hash3_2x64(const uint64_t x, const uint64_t y, const uint64_t seed) {
59+
const uint64_t c1 = 0x87c37b91114253d5;
60+
const uint64_t c2 = 0x4cf5ad432745937f;
61+
62+
uint64_t h1 = seed;
63+
uint64_t h2 = seed;
64+
65+
// First 64-bit block
66+
uint64_t k1 = x;
67+
k1 *= c1;
68+
k1 = (k1 << 31) | (k1 >> (64 - 31));
69+
k1 *= c2;
70+
h1 ^= k1;
71+
h1 = (h1 << 27) | (h1 >> (64 - 27));
72+
h1 += h2;
73+
h1 = h1 * 5 + 0x52dce729;
74+
75+
// Second 64-bit block
76+
uint64_t k2 = y;
77+
k2 *= c2;
78+
k2 = (k2 << 33) | (k2 >> (64 - 33));
79+
k2 *= c1;
80+
h2 ^= k2;
81+
h2 = (h2 << 31) | (h2 >> (64 - 31));
82+
h2 += h1;
83+
h2 = h2 * 5 + 0x38495ab5;
84+
85+
// Finalization
86+
h1 ^= 16;
87+
h2 ^= 16;
88+
h1 += h2;
89+
h2 += h1;
90+
h1 ^= h1 >> 33;
91+
h1 *= 0xff51afd7ed558ccd;
92+
h1 ^= h1 >> 33;
93+
h1 *= 0xc4ceb9fe1a85ec53;
94+
h1 ^= h1 >> 33;
95+
h2 ^= h2 >> 33;
96+
h2 *= 0xff51afd7ed558ccd;
97+
h2 ^= h2 >> 33;
98+
h2 *= 0xc4ceb9fe1a85ec53;
99+
h2 ^= h2 >> 33;
100+
h1 += h2;
101+
h2 += h1;
102+
103+
return h1 ^ h2;
104+
}
105+
106+
// NOLINTNEXTLINE:
107+
template <bool CIRCULAR_PROBE>
108+
TORBOREC_INLINE int64_t next_output_index(
109+
int64_t output_index,
110+
int64_t modulo,
111+
int64_t& /* max_probe_local */) {
112+
static_assert(CIRCULAR_PROBE);
113+
return (output_index + 1) % modulo;
114+
}
115+
116+
// NOLINTNEXTLINE:
117+
template <>
118+
TORBOREC_INLINE int64_t next_output_index<false>(
119+
int64_t output_index,
120+
int64_t modulo,
121+
int64_t& max_probe_local) {
122+
output_index = (output_index + 1) % modulo;
123+
if (output_index == 0) {
124+
// circular, using max_probe_local to control exit.
125+
max_probe_local = 0;
126+
}
127+
return output_index;
128+
}
129+
130+
TORBOREC_INLINE bool is_eviction_enabled(
131+
bool readonly,
132+
int eviction_threshold,
133+
int eviction_policy) {
134+
return !readonly && (eviction_threshold > 0 || eviction_policy > 0);
135+
}
136+
137+
#undef TORBOREC_INLINE
138+
139+
} // namespace torch::torchrec::turborec

0 commit comments

Comments
 (0)