Skip to content

Commit 1a354c9

Browse files
author
Runchu Zhao
committed
Add LFU strategy to dynamicemb
1 parent d5d02a3 commit 1a354c9

12 files changed

+79
-25
lines changed

corelib/dynamicemb/dynamicemb/batched_dynamicemb_tables.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,7 @@ def forward(
605605
self._update_score()
606606
return res
607607

608+
# Todo: add LFU here, LFU really need to set score?
608609
def set_score(
609610
self,
610611
named_score: Dict[str, int],
@@ -646,6 +647,8 @@ def _create_score(self):
646647
self._scores[table_name] = 1
647648
elif option.score_strategy == DynamicEmbScoreStrategy.CUSTOMIZED:
648649
option.evict_strategy = DynamicEmbEvictStrategy.CUSTOMIZED
650+
elif option.score_strategy == DynamicEmbScoreStrategy.LFU:
651+
option.evict_strategy = DynamicEmbEvictStrategy.LFU #ADD LFU here
649652

650653
def _update_score(self):
651654
for table_name, option in zip(self._table_names, self._dynamicemb_options):
@@ -669,6 +672,9 @@ def _update_score(self):
669672
self._scores[table_name] = 0
670673
else:
671674
self._scores[table_name] = new_score
675+
elif option.score_strategy == DynamicEmbScoreStrategy.LFU:
676+
new_score = old_score + 1 # Todo : confirm score update logic
677+
self._scores[table_name] = new_score + old_score
672678

673679
def incremental_dump(
674680
self,

corelib/dynamicemb/dynamicemb/dynamicemb_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ class DynamicEmbScoreStrategy(enum.IntEnum):
199199
TIMESTAMP = 0
200200
STEP = 1
201201
CUSTOMIZED = 2
202-
202+
LFU = 3 #ADD LFU here
203203

204204
# Configs used as keys to group HKV variables(considering kernel behaviors, result type).
205205
@dataclass

corelib/dynamicemb/src/dynamic_emb_op.cu

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,11 @@ void insert_and_evict(
138138
bool unique_key = true,
139139
bool ignore_evict_strategy = false) {
140140

141-
if (not score and table->evict_strategy() == EvictStrategy::kCustomized) {
142-
throw std::invalid_argument("Must specify the score when evict strategy is customized.");
141+
if (not score and (table->evict_strategy() == EvictStrategy::kCustomized || table->evict_strategy() == EvictStrategy::kLfu)) {
142+
throw std::invalid_argument("Must specify the score when evict strategy is customized or LFU.");
143143
}
144144
auto stream = at::cuda::getCurrentCUDAStream().stream();
145-
if (table->evict_strategy() == EvictStrategy::kCustomized) {
145+
if (table->evict_strategy() == EvictStrategy::kCustomized || table->evict_strategy() == EvictStrategy::kLfu) {
146146
auto&& option = at::TensorOptions().dtype(at::kUInt64).device(keys.device());
147147
// broadcast scores
148148
at::Tensor bc_scores = at::empty({static_cast<int64_t>(n)}, option);
@@ -188,8 +188,8 @@ void find_or_insert(std::shared_ptr<dyn_emb::DynamicVariableBase> table,
188188
bool ignore_evict_strategy = false
189189
)
190190
{
191-
if (not score and table->evict_strategy() == EvictStrategy::kCustomized) {
192-
throw std::invalid_argument("Must specify the score when evict strategy is customized.");
191+
if (not score and (table->evict_strategy() == EvictStrategy::kCustomized || table->evict_strategy() == EvictStrategy::kLfu)) {
192+
throw std::invalid_argument("Must specify the score when evict strategy is customized or LFU.");
193193
}
194194
if (n == 0) return;
195195
auto stream = at::cuda::getCurrentCUDAStream().stream();
@@ -203,7 +203,7 @@ void find_or_insert(std::shared_ptr<dyn_emb::DynamicVariableBase> table,
203203

204204
auto found_tensor_data_ptr = found_tensor.data_ptr<bool>();
205205

206-
if (table->evict_strategy() == EvictStrategy::kCustomized) {
206+
if (table->evict_strategy() == EvictStrategy::kCustomized || table->evict_strategy() == EvictStrategy::kLfu) {
207207
auto&& option = at::TensorOptions().dtype(at::kUInt64).device(keys.device());
208208
// broadcast scores
209209
at::Tensor bc_scores = at::empty({static_cast<int64_t>(n)}, option);
@@ -226,15 +226,15 @@ void find_or_insert_pointers(
226226
const std::optional<uint64_t> score = std::nullopt,
227227
bool unique_key = true,
228228
bool ignore_evict_strategy = false) {
229-
if (not score and table->evict_strategy() == EvictStrategy::kCustomized) {
230-
throw std::invalid_argument("Must specify the score when evict strategy is customized.");
229+
if (not score and (table->evict_strategy() == EvictStrategy::kCustomized || table->evict_strategy() == EvictStrategy::kLfu)) {
230+
throw std::invalid_argument("Must specify the score when evict strategy is customized or LFU.");
231231
}
232232
if (n == 0) return;
233233
auto stream = at::cuda::getCurrentCUDAStream().stream();
234234
auto values_data_ptr = reinterpret_cast<void**>(values.data_ptr<int64_t>());
235235
auto found_tensor_data_ptr = founds.data_ptr<bool>();
236236

237-
if (table->evict_strategy() == EvictStrategy::kCustomized) {
237+
if (table->evict_strategy() == EvictStrategy::kCustomized || table->evict_strategy() == EvictStrategy::kLfu) {
238238
auto&& option = at::TensorOptions().dtype(at::kUInt64).device(keys.device());
239239
// broadcast scores
240240
at::Tensor bc_scores = at::empty({static_cast<int64_t>(n)}, option);

corelib/dynamicemb/src/dynamic_variable_base.cu

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ std::shared_ptr<DynamicVariableBase> VariableFactory::create(
2929
const SafeCheckMode safe_check_mode = SafeCheckMode::IGNORE) {
3030
// TODO:check input datetype , now only support key type is int64_t uint64_t ,
3131
// value type float
32+
printf("create dynamic variable base\n");
3233
std::shared_ptr<DynamicVariableBase> table;
3334
DISPATCH_INTEGER_DATATYPE_FUNCTION(key_type, key_t, [&] {
3435
DISPATCH_FLOAT_DATATYPE_FUNCTION(value_type, value_t, [&] {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include"hkv_variable.cuh"
2+
namespace dyn_emb{
3+
template class HKVVariable<int64_t, __half, EvictStrategy::kLfu>;
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include"hkv_variable.cuh"
2+
namespace dyn_emb{
3+
template class HKVVariable<int64_t, __nv_bfloat16, EvictStrategy::kLfu>;
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include"hkv_variable.cuh"
2+
namespace dyn_emb{
3+
template class HKVVariable<int64_t, float, EvictStrategy::kLfu>;
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include"hkv_variable.cuh"
2+
namespace dyn_emb{
3+
template class HKVVariable<uint64_t, __half, EvictStrategy::kLfu>;
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include"hkv_variable.cuh"
2+
namespace dyn_emb{
3+
template class HKVVariable<uint64_t, __nv_bfloat16, EvictStrategy::kLfu>;
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include"hkv_variable.cuh"
2+
namespace dyn_emb{
3+
template class HKVVariable<uint64_t, float, EvictStrategy::kLfu>;
4+
}

0 commit comments

Comments
 (0)