Skip to content

Commit b3c2437

Browse files
EddyLXJfacebook-github-bot
authored andcommitted
Adding new ID_COUNT eviction trigger condition (pytorch#4829)
Summary: X-link: facebookresearch/FBGEMM#1855 Differential Revision: D81151216
1 parent 4b2e54d commit b3c2437

9 files changed

Lines changed: 199 additions & 119 deletions

File tree

fbgemm_gpu/fbgemm_gpu/split_table_batched_embeddings_ops_common.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def from_str(cls, key: str):
6262

6363
class EvictionPolicy(NamedTuple):
6464
eviction_trigger_mode: int = (
65-
0 # disabled, 0: disabled, 1: iteration, 2: mem_util, 3: manual
65+
0 # disabled, 0: disabled, 1: iteration, 2: mem_util, 3: manual 4: id count
6666
)
6767
eviction_strategy: int = (
6868
0 # 0: timestamp, 1: counter , 2: counter + timestamp, 3: feature l2 norm 4: timestamp threshold 5: feature score
@@ -85,11 +85,11 @@ class EvictionPolicy(NamedTuple):
8585
feature_score_counter_decay_rates: Optional[List[float]] = (
8686
None # feature_score_counter_decay_rates for each table if eviction strategy is feature score
8787
)
88-
max_training_id_num_per_table: Optional[List[int]] = (
89-
None # max_training_id_num_per_table for each table
88+
training_id_eviction_trigger_count: Optional[List[int]] = (
89+
None # training_id_eviction_trigger_count for each table
9090
)
91-
target_eviction_percent_per_table: Optional[List[float]] = (
92-
None # target_eviction_percent_per_table for each table
91+
training_id_keep_count: Optional[List[int]] = (
92+
None # training_id_keep_count for each table
9393
)
9494
l2_weight_thresholds: Optional[List[float]] = (
9595
None # l2_weight_thresholds for each table if eviction strategy is feature l2 norm
@@ -116,8 +116,8 @@ class EvictionPolicy(NamedTuple):
116116
meta_header_lens: Optional[List[int]] = None # metaheader length for each table
117117

118118
def validate(self) -> None:
119-
assert self.eviction_trigger_mode in [0, 1, 2, 3], (
120-
"eviction_trigger_mode must be 0, 1, 2, or 3, "
119+
assert self.eviction_trigger_mode in [0, 1, 2, 3, 4], (
120+
"eviction_trigger_mode must be 0, 1, 2, 3 or 4 "
121121
f"actual {self.eviction_trigger_mode}"
122122
)
123123
if self.eviction_trigger_mode == 0:
@@ -139,6 +139,10 @@ def validate(self) -> None:
139139
assert (
140140
self.eviction_mem_threshold_gb is not None
141141
), "eviction_mem_threshold_gb must be set if eviction_trigger_mode is 2"
142+
elif self.eviction_trigger_mode == 4:
143+
assert (
144+
self.training_id_eviction_trigger_count is not None
145+
), "training_id_eviction_trigger_count must be set if eviction_trigger_mode is 4"
142146

143147
if self.eviction_strategy == 0:
144148
assert self.ttls_in_mins is not None, (
@@ -184,13 +188,13 @@ def validate(self) -> None:
184188
"feature_score_counter_decay_rates must be set if eviction_strategy is 5, "
185189
f"actual {self.feature_score_counter_decay_rates}"
186190
)
187-
assert self.max_training_id_num_per_table is not None, (
188-
"max_training_id_num_per_table must be set if eviction_strategy is 5,"
189-
f"actual {self.max_training_id_num_per_table}"
191+
assert self.training_id_eviction_trigger_count is not None, (
192+
"training_id_eviction_trigger_count must be set if eviction_strategy is 5,"
193+
f"actual {self.training_id_eviction_trigger_count}"
190194
)
191-
assert self.target_eviction_percent_per_table is not None, (
192-
"target_eviction_percent_per_table must be set if eviction_strategy is 5,"
193-
f"actual {self.target_eviction_percent_per_table}"
195+
assert self.training_id_keep_count is not None, (
196+
"training_id_keep_count must be set if eviction_strategy is 5,"
197+
f"actual {self.training_id_keep_count}"
194198
)
195199
assert self.threshold_calculation_bucket_stride is not None, (
196200
"threshold_calculation_bucket_stride must be set if eviction_strategy is 5,"
@@ -201,12 +205,12 @@ def validate(self) -> None:
201205
f"actual {self.threshold_calculation_bucket_num}"
202206
)
203207
assert (
204-
len(self.target_eviction_percent_per_table)
208+
len(self.training_id_keep_count)
205209
== len(self.feature_score_counter_decay_rates)
206-
== len(self.max_training_id_num_per_table)
210+
== len(self.training_id_eviction_trigger_count)
207211
), (
208-
"feature_score_thresholds, max_training_id_num_per_table and target_eviction_percent_per_table must have the same length, "
209-
f"actual {self.target_eviction_percent_per_table} vs {self.feature_score_counter_decay_rates} vs {self.max_training_id_num_per_table}"
212+
"feature_score_thresholds, training_id_eviction_trigger_count and training_id_keep_count must have the same length, "
213+
f"actual {self.training_id_keep_count} vs {self.feature_score_counter_decay_rates} vs {self.training_id_eviction_trigger_count}"
210214
)
211215

212216

fbgemm_gpu/fbgemm_gpu/tbe/ssd/training.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -690,16 +690,16 @@ def __init__(
690690
)
691691
# Please refer to https://fburl.com/gdoc/nuupjwqq for the following eviction parameters.
692692
eviction_config = torch.classes.fbgemm.FeatureEvictConfig(
693-
self.kv_zch_params.eviction_policy.eviction_trigger_mode, # eviction is disabled, 0: disabled, 1: iteration, 2: mem_util, 3: manual
693+
self.kv_zch_params.eviction_policy.eviction_trigger_mode, # eviction is disabled, 0: disabled, 1: iteration, 2: mem_util, 3: manual, 4: id count
694694
self.kv_zch_params.eviction_policy.eviction_strategy, # evict_trigger_strategy: 0: timestamp, 1: counter, 2: counter + timestamp, 3: feature l2 norm, 4: timestamp threshold 5: feature score
695695
self.kv_zch_params.eviction_policy.eviction_step_intervals, # trigger_step_interval if trigger mode is iteration
696696
eviction_mem_threshold_gb, # mem_util_threshold_in_GB if trigger mode is mem_util
697697
self.kv_zch_params.eviction_policy.ttls_in_mins, # ttls_in_mins for each table if eviction strategy is timestamp
698698
self.kv_zch_params.eviction_policy.counter_thresholds, # counter_thresholds for each table if eviction strategy is counter
699699
self.kv_zch_params.eviction_policy.counter_decay_rates, # counter_decay_rates for each table if eviction strategy is counter
700700
self.kv_zch_params.eviction_policy.feature_score_counter_decay_rates, # feature_score_counter_decay_rates for each table if eviction strategy is feature score
701-
self.kv_zch_params.eviction_policy.max_training_id_num_per_table, # max_training_id_num for each table
702-
self.kv_zch_params.eviction_policy.target_eviction_percent_per_table, # target_eviction_percent for each table
701+
self.kv_zch_params.eviction_policy.training_id_eviction_trigger_count, # training_id_eviction_trigger_count for each table
702+
self.kv_zch_params.eviction_policy.training_id_keep_count, # training_id_keep_count for each table
703703
self.kv_zch_params.eviction_policy.l2_weight_thresholds, # l2_weight_thresholds for each table if eviction strategy is feature l2 norm
704704
table_dims.tolist() if table_dims is not None else None,
705705
self.kv_zch_params.eviction_policy.threshold_calculation_bucket_stride, # threshold_calculation_bucket_stride if eviction strategy is feature score
@@ -1047,9 +1047,6 @@ def __init__(
10471047
self.stats_reporter.register_stats(
10481048
f"eviction.feature_table.{t}.processed_counts"
10491049
)
1050-
self.stats_reporter.register_stats(
1051-
f"eviction.feature_table.{t}.eviction_threshold_with_dry_run"
1052-
)
10531050
self.stats_reporter.register_stats(
10541051
f"eviction.feature_table.{t}.evict_rate"
10551052
)
@@ -3926,11 +3923,6 @@ def _report_eviction_stats(self) -> None:
39263923
data_bytes=int(processed_counts[t].item()),
39273924
enable_tb_metrics=True,
39283925
)
3929-
stats_reporter.report_data_amount(
3930-
iteration_step=self.step,
3931-
event_name=f"eviction.feature_table.{t}.eviction_threshold_with_dry_run",
3932-
data_bytes=float(eviction_threshold_with_dry_run[t].item()),
3933-
)
39343926
if processed_counts[t].item() != 0:
39353927
stats_reporter.report_data_amount(
39363928
iteration_step=self.step,

fbgemm_gpu/src/dram_kv_embedding_cache/SynchronizedShardedMap.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,19 @@ class SynchronizedShardedMap {
8989
return num_rows;
9090
}
9191

92+
auto getActualUsedIDCount() const {
93+
size_t used_id_count = 0;
94+
for (size_t i = 0; i < shards_.size(); ++i) {
95+
int64_t mempool_idx = i % mempools_.size();
96+
// only calculate the sizes of K, V and block that are used
97+
if (mempools_[mempool_idx]->get_allocated_chunk_bytes() > 0) {
98+
auto rlmap = shards_[i].rlock();
99+
used_id_count += rlmap->size();
100+
}
101+
}
102+
return used_id_count;
103+
}
104+
92105
private:
93106
std::vector<folly::Synchronized<folly::F14FastMap<K, V>, M>> shards_;
94107
std::vector<std::unique_ptr<FixedBlockPool>> mempools_;

fbgemm_gpu/src/dram_kv_embedding_cache/dram_kv_embedding_cache.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,19 @@ class DramKVEmbeddingCache : public kv_db::EmbeddingKVDB {
11011101
}
11021102
break;
11031103
}
1104+
case EvictTriggerMode::ID_COUNT: {
1105+
auto used_id_count = get_map_actual_used_id_count();
1106+
auto eviction_threshold = 0;
1107+
for (const auto& eviction_trigger_count :
1108+
feature_evict_config_.value()
1109+
->training_id_eviction_trigger_count_.value()) {
1110+
eviction_threshold += eviction_trigger_count;
1111+
}
1112+
if (used_id_count > eviction_threshold) {
1113+
trigger_feature_evict();
1114+
}
1115+
break;
1116+
}
11041117
default:
11051118
break;
11061119
}
@@ -1125,6 +1138,10 @@ class DramKVEmbeddingCache : public kv_db::EmbeddingKVDB {
11251138
return kv_store_.getNumRows();
11261139
}
11271140

1141+
size_t get_map_actual_used_id_count() const {
1142+
return kv_store_.getActualUsedIDCount();
1143+
}
1144+
11281145
void resume_ongoing_eviction(bool force_resume = false) override {
11291146
if (!is_training_ && !force_resume) {
11301147
return;

fbgemm_gpu/src/dram_kv_embedding_cache/dram_kv_embedding_inference_wrapper.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ void DramKVEmbeddingInferenceWrapper::init(
5656
std::nullopt /* counter_thresholds */,
5757
std::nullopt /* counter_decay_rates */,
5858
std::nullopt /* feature_score_counter_decay_rates */,
59-
std::nullopt /* max_training_id_num_per_table */,
60-
std::nullopt /* target_eviction_percent_per_table */,
59+
std::nullopt /* training_id_eviction_trigger_count */,
60+
std::nullopt /* training_id_keep_count */,
6161
std::nullopt /* l2_weight_thresholds */,
6262
std::nullopt /* embedding_dims */,
6363
std::nullopt /* threshold_calculation_bucket_stride */,

0 commit comments

Comments
 (0)