Skip to content

Commit e48294d

Browse files
z52527Runchu Zhao
andauthored
Add LFU evict strategy to dynamicemb (#52)
* Add LFU strategy to dynamicemb * Add a test for LFU strategy * Update test comment * Delete some comments * Precommit * Update test and comments --------- Co-authored-by: Runchu Zhao <runchuz@nvidia.com>
1 parent a247bd4 commit e48294d

12 files changed

+555
-23
lines changed

corelib/dynamicemb/dynamicemb/batched_dynamicemb_tables.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,9 @@ def _create_score(self):
666666
self._scores[table_name] = 1
667667
elif option.score_strategy == DynamicEmbScoreStrategy.CUSTOMIZED:
668668
option.evict_strategy = DynamicEmbEvictStrategy.CUSTOMIZED
669+
elif option.score_strategy == DynamicEmbScoreStrategy.LFU:
670+
option.evict_strategy = DynamicEmbEvictStrategy.LFU
671+
self._scores[table_name] = 1
669672

670673
def _update_score(self):
671674
for table_name, option in zip(self._table_names, self._dynamicemb_options):
@@ -689,6 +692,8 @@ def _update_score(self):
689692
self._scores[table_name] = 0
690693
else:
691694
self._scores[table_name] = new_score
695+
elif option.score_strategy == DynamicEmbScoreStrategy.LFU:
696+
self._scores[table_name] = 1
692697

693698
def incremental_dump(
694699
self,

corelib/dynamicemb/dynamicemb/dynamicemb_config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ class DynamicEmbScoreStrategy(enum.IntEnum):
208208
TIMESTAMP = 0
209209
STEP = 1
210210
CUSTOMIZED = 2
211+
LFU = 3
211212

212213

213214
# Configs used as keys to group HKV variables(considering kernel behaviors, result type).

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
@@ -30,6 +30,7 @@ std::shared_ptr<DynamicVariableBase> VariableFactory::create(
3030
const OptimizerType optimizer_type = OptimizerType::Null) {
3131
// TODO:check input datetype , now only support key type is int64_t uint64_t ,
3232
// value type float
33+
printf("create dynamic variable base\n");
3334
std::shared_ptr<DynamicVariableBase> table;
3435
DISPATCH_INTEGER_DATATYPE_FUNCTION(key_type, key_t, [&] {
3536
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)