Skip to content

Commit f3be526

Browse files
Ziliang Zhaometa-codesync[bot]
authored andcommitted
Clean up kernel code by deleting unused options and code logic
Summary: Remove unused code paths and options from faster_hash CPU and CUDA kernels in both caffe2/torch/fb/retrieval and fbgemm_gpu codebases. All callers already use `circular_probe=True`, `_modulo_identity_DPRECATED=false`, and `output_on_uvm=false`, making these options dead code. This change: - Removes the `CIRCULAR_PROBE` template parameter and simplifies `next_output_index` from a template specialization pair to a single function, dropping the `max_probe_local` side-effect parameter - Removes `output_on_uvm` support, including UVM tensor allocation and `cudaDeviceSynchronize` call - Hardcodes `_modulo_identity_DPRECATED` to false, removing the conditional `hash_identity` assignment - Adds TORCH_CHECK assertions at the API boundary to enforce these constraints - Simplifies the macro dispatch chains by removing the `CIRCULAR_PROBE` dimension, reducing kernel binary bloat - Removes `HASH_IDENTITY == 1` dead code from kernels and dispatch macros (hash_identity can only be 0 or 2 after the change) - Fixes if/else bug in CPU `INVOKE_KERNEL_HAS_OFFSET` that could invoke the kernel twice - Removes `output_on_uvm` parameter from `faster_hash_bench.py` - Removes all related test cases for `circular_probe=False` and `output_on_uvm=True` Reviewed By: howei Differential Revision: D95470985 fbshipit-source-id: 358362e9844bfa9b05502e701bbce6214ef94f69
1 parent 8d08cc1 commit f3be526

4 files changed

Lines changed: 88 additions & 463 deletions

File tree

fbgemm_gpu/include/fbgemm_gpu/faster_hash_ops/common_utils.cuh

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -102,29 +102,11 @@ murmur_hash3_2x64(const uint64_t x, const uint64_t y, const uint64_t seed) {
102102
}
103103

104104
// NOLINTNEXTLINE:
105-
template <bool CIRCULAR_PROBE>
106-
TORBOREC_INLINE int64_t next_output_index(
107-
int64_t output_index,
108-
int64_t modulo,
109-
int64_t& /* max_probe_local */) {
110-
static_assert(CIRCULAR_PROBE);
105+
TORBOREC_INLINE int64_t
106+
next_output_index(int64_t output_index, int64_t modulo) {
111107
return (output_index + 1) % modulo;
112108
}
113109

114-
// NOLINTNEXTLINE:
115-
template <>
116-
TORBOREC_INLINE int64_t next_output_index<false>(
117-
int64_t output_index,
118-
int64_t modulo,
119-
int64_t& max_probe_local) {
120-
output_index = (output_index + 1) % modulo;
121-
if (output_index == 0) {
122-
// circular, using max_probe_local to control exit.
123-
max_probe_local = 0;
124-
}
125-
return output_index;
126-
}
127-
128110
TORBOREC_INLINE bool is_eviction_enabled(
129111
bool readonly,
130112
int eviction_threshold,

fbgemm_gpu/src/faster_hash_ops/faster_hash.cpp

Lines changed: 37 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ constexpr int64_t kMaxIdentityNum = INT32_MAX;
3030
template <
3131
bool DISABLE_FALLBACK,
3232
int32_t HASH_IDENTITY,
33-
bool CIRCULAR_PROBE,
3433
bool HAS_OFFSET,
3534
typename TInput,
3635
typename TIdentity>
@@ -75,14 +74,7 @@ void process_item_zch(
7574
static_cast<int64_t>(hash % opt_in_block_size); // Local idx
7675
TIdentity identity;
7776

78-
if constexpr (HASH_IDENTITY == 1) {
79-
identity = static_cast<TIdentity>(
80-
murmur_hash3_2x64(
81-
static_cast<uint64_t>(item),
82-
0x17, // seed
83-
0) %
84-
kMaxIdentityNum);
85-
} else if constexpr (HASH_IDENTITY == 2) {
77+
if constexpr (HASH_IDENTITY == 2) {
8678
identity = static_cast<TIdentity>(item % kMaxIdentityNum);
8779
} else {
8880
identity = static_cast<TIdentity>(item);
@@ -98,10 +90,7 @@ void process_item_zch(
9890
break;
9991
}
10092

101-
output_index = next_output_index<CIRCULAR_PROBE>(
102-
output_index,
103-
opt_in_block_size, // only probe within the opt-in block
104-
max_probe_local);
93+
output_index = next_output_index(output_index, opt_in_block_size);
10594
}
10695

10796
// can't find a slot (all slot full after probing)
@@ -130,7 +119,6 @@ void _zero_collision_hash_cpu_out(
130119
const Tensor& input,
131120
const Tensor& identities,
132121
int64_t max_probe,
133-
const bool circular_probe,
134122
const std::optional<Tensor>& local_sizes,
135123
const std::optional<Tensor>& offsets,
136124
int32_t hash_identity,
@@ -144,55 +132,41 @@ void _zero_collision_hash_cpu_out(
144132
auto* offsets_ptr =
145133
offsets.has_value() ? offsets->const_data_ptr<int64_t>() : nullptr;
146134

147-
#define INVOKE_KERNEL( \
148-
DISABLE_FALLBACK, HASH_IDENTITY, CIRCULAR_PROBE, HAS_OFFSET) \
149-
{ \
150-
process_item_zch< \
151-
DISABLE_FALLBACK, \
152-
HASH_IDENTITY, \
153-
CIRCULAR_PROBE, \
154-
HAS_OFFSET, \
155-
TInput, \
156-
TIdentity>( \
157-
input.packed_accessor64<TInput, 1>(), \
158-
output.packed_accessor64<int64_t, 1>(), \
159-
identities.packed_accessor64<TIdentity, 2>(), \
160-
modulo, \
161-
max_probe, \
162-
local_sizes_ptr, \
163-
offsets_ptr, \
164-
opt_in_prob, \
165-
num_reserved_slots); \
135+
#define INVOKE_KERNEL(DISABLE_FALLBACK, HASH_IDENTITY, HAS_OFFSET) \
136+
{ \
137+
process_item_zch< \
138+
DISABLE_FALLBACK, \
139+
HASH_IDENTITY, \
140+
HAS_OFFSET, \
141+
TInput, \
142+
TIdentity>( \
143+
input.packed_accessor64<TInput, 1>(), \
144+
output.packed_accessor64<int64_t, 1>(), \
145+
identities.packed_accessor64<TIdentity, 2>(), \
146+
modulo, \
147+
max_probe, \
148+
local_sizes_ptr, \
149+
offsets_ptr, \
150+
opt_in_prob, \
151+
num_reserved_slots); \
166152
}
167153

168-
#define INVOKE_HASH_IDENTITY(HASH_IDENTITY, CIRCULAR_PROBE, HAS_OFFSET) \
169-
{ \
170-
if (disable_fallback) { \
171-
INVOKE_KERNEL(true, HASH_IDENTITY, CIRCULAR_PROBE, HAS_OFFSET) \
172-
} else { \
173-
INVOKE_KERNEL(false, HASH_IDENTITY, CIRCULAR_PROBE, HAS_OFFSET) \
174-
} \
154+
#define INVOKE_HASH_IDENTITY(HASH_IDENTITY, HAS_OFFSET) \
155+
{ \
156+
if (disable_fallback) { \
157+
INVOKE_KERNEL(true, HASH_IDENTITY, HAS_OFFSET) \
158+
} else { \
159+
INVOKE_KERNEL(false, HASH_IDENTITY, HAS_OFFSET) \
160+
} \
175161
}
176162

177-
#define INVOKE_KERNEL_CIRCULAR_PROBE(CIRCULAR_PROBE, HAS_OFFSET) \
178-
{ \
179-
if (hash_identity == 1) { \
180-
INVOKE_HASH_IDENTITY(1, CIRCULAR_PROBE, HAS_OFFSET); \
181-
} \
182-
if (hash_identity == 2) { \
183-
INVOKE_HASH_IDENTITY(2, CIRCULAR_PROBE, HAS_OFFSET); \
184-
} else { \
185-
INVOKE_HASH_IDENTITY(0, CIRCULAR_PROBE, HAS_OFFSET); \
186-
} \
187-
}
188-
189-
#define INVOKE_KERNEL_HAS_OFFSET(HAS_OFFSET) \
190-
{ \
191-
if (circular_probe) { \
192-
INVOKE_KERNEL_CIRCULAR_PROBE(true, HAS_OFFSET); \
193-
} else { \
194-
INVOKE_KERNEL_CIRCULAR_PROBE(false, HAS_OFFSET); \
195-
} \
163+
#define INVOKE_KERNEL_HAS_OFFSET(HAS_OFFSET) \
164+
{ \
165+
if (hash_identity == 2) { \
166+
INVOKE_HASH_IDENTITY(2, HAS_OFFSET); \
167+
} else { \
168+
INVOKE_HASH_IDENTITY(0, HAS_OFFSET); \
169+
} \
196170
}
197171

198172
if (local_sizes_ptr != nullptr) {
@@ -202,7 +176,6 @@ void _zero_collision_hash_cpu_out(
202176
}
203177

204178
#undef INVOKE_KERNEL_HAS_OFFSET
205-
#undef INVOKE_KERNEL_CIRCULAR_PROBE
206179
#undef INVOKE_HASH_IDENTITY
207180
#undef INVOKE_KERNEL
208181
}
@@ -281,7 +254,10 @@ void zero_collision_hash_cpu_out(
281254
TORCH_CHECK(input.is_cpu());
282255
TORCH_CHECK(identities.dim() == 2);
283256

284-
int hash_identity = _modulo_identity_DPRECATED ? 2 : 1;
257+
TORCH_CHECK(circular_probe, "circular_probe must be true");
258+
TORCH_CHECK(
259+
!_modulo_identity_DPRECATED, "_modulo_identity_DPRECATED must be false");
260+
int hash_identity = 2;
285261
if (identities.dtype() == input.dtype()) {
286262
hash_identity = 0;
287263
}
@@ -319,7 +295,6 @@ void zero_collision_hash_cpu_out(
319295
input,
320296
identities,
321297
max_probe,
322-
circular_probe,
323298
local_sizes,
324299
offsets,
325300
hash_identity,

0 commit comments

Comments
 (0)