@@ -24,19 +24,14 @@ using namespace std;
2424using spp::sparse_hash_map;
2525
2626namespace lrb {
27- static uint32_t current_seq = -1 ;
28- static uint8_t max_n_past_timestamps = 32 ;
29- static uint8_t max_n_past_distances = 31 ;
30- static uint8_t base_edc_window = 10 ;
3127 static const uint8_t n_edc_feature = 10 ;
32- static vector<uint32_t > edc_windows;
33- static vector<double > hash_edc;
34- static uint32_t max_hash_edc_idx;
35- static uint32_t memory_window = 67108864 ;
36- static uint32_t n_extra_fields = 0 ;
37- static uint32_t batch_size = 131072 ;
3828 static const uint max_n_extra_feature = 4 ;
39- static uint32_t n_feature;
29+ static const uint32_t n_extra_fields = 0 ;
30+ static const uint8_t max_n_past_timestamps = 32 ;
31+ static const uint8_t max_n_past_distances = 31 ;
32+ static const uint8_t base_edc_window = 10 ;
33+ static const uint32_t batch_size = 131072 ;
34+
4035
4136struct MetaExtra {
4237 // vector overhead = 24 (8 pointer, 8 size, 8 allocation)
@@ -49,15 +44,23 @@ struct MetaExtra {
4944 // the next index to put the distance
5045 uint8_t _past_distance_idx = 1 ;
5146
52- MetaExtra (const uint32_t &distance) {
47+ MetaExtra (const uint32_t &distance,
48+ uint32_t max_hash_edc_idx,
49+ vector<uint32_t > &edc_windows,
50+ const vector<double > &hash_edc
51+ ) {
5352 _past_distances = vector<uint32_t >(1 , distance);
5453 for (uint8_t i = 0 ; i < n_edc_feature; ++i) {
5554 uint32_t _distance_idx = min (uint32_t (distance / edc_windows[i]), max_hash_edc_idx);
5655 _edc[i] = hash_edc[_distance_idx] + 1 ;
5756 }
5857 }
5958
60- void update (const uint32_t &distance) {
59+ void update (const uint32_t &distance,
60+ uint32_t max_hash_edc_idx,
61+ vector<uint32_t > &edc_windows,
62+ const vector<double > &hash_edc
63+ ) {
6164 uint8_t distance_idx = _past_distance_idx % max_n_past_distances;
6265 if (_past_distances.size () < max_n_past_distances)
6366 _past_distances.emplace_back (distance);
@@ -84,7 +87,6 @@ class Meta {
8487 MetaExtra *_extra = nullptr ;
8588 vector<uint32_t > _sample_times;
8689
87-
8890 Meta (const uint64_t &key, const uint64_t &size, const uint64_t &past_timestamp,
8991 const vector<uint16_t > &extra_features) {
9092 _key = key;
@@ -104,14 +106,18 @@ class Meta {
104106 delete _extra;
105107 }
106108
107- void update (const uint32_t &past_timestamp) {
109+ void update (const uint32_t &past_timestamp, uint32_t n_extra_fields,
110+ uint32_t max_hash_edc_idx,
111+ vector<uint32_t > &edc_windows,
112+ const vector<double > &hash_edc
113+ ) {
108114 // distance
109115 uint32_t _distance = past_timestamp - _past_timestamp;
110116 assert (_distance);
111117 if (!_extra) {
112- _extra = new MetaExtra (_distance);
118+ _extra = new MetaExtra (_distance, max_hash_edc_idx, edc_windows, hash_edc );
113119 } else
114- _extra->update (_distance);
120+ _extra->update (_distance, max_hash_edc_idx, edc_windows, hash_edc );
115121 // timestamp
116122 _past_timestamp = past_timestamp;
117123 }
@@ -174,16 +180,19 @@ class TrainingData {
174180 vector<int32_t > indptr;
175181 vector<int32_t > indices;
176182 vector<double > data;
183+ uint32_t memory_window;
177184
178- TrainingData () {
185+ TrainingData (uint32_t n_feature, uint32_t memory_window_ ) {
179186 labels.reserve (batch_size);
180187 indptr.reserve (batch_size + 1 );
181188 indptr.emplace_back (0 );
182189 indices.reserve (batch_size * n_feature);
183190 data.reserve (batch_size * n_feature);
191+ memory_window = memory_window_;
184192 }
185193
186- void emplace_back (Meta &meta, uint32_t &sample_timestamp, uint32_t &future_interval, const uint64_t &key) {
194+ void emplace_back (Meta &meta, uint32_t &sample_timestamp, uint32_t &future_interval, const uint64_t &key,
195+ uint32_t max_hash_edc_idx, vector<uint32_t > &edc_windows, vector<double > &hash_edc) {
187196 int32_t counter = indptr.back ();
188197
189198 indices.emplace_back (0 );
@@ -262,6 +271,13 @@ struct KeyMapEntryT {
262271
263272class LRBCache : public Cache {
264273public:
274+ uint32_t current_seq = -1 ;
275+ vector<uint32_t > edc_windows;
276+ vector<double > hash_edc;
277+ uint32_t max_hash_edc_idx;
278+ uint32_t memory_window = 67108864 ;
279+ uint32_t n_feature;
280+
265281 // key -> (0/1 list, idx)
266282 sparse_hash_map<uint64_t , KeyMapEntryT> key_map;
267283 vector<InCacheMeta> in_cache_metas;
@@ -326,12 +342,6 @@ class LRBCache : public Cache {
326342 sample_rate = stoul (it.second );
327343 } else if (it.first == " memory_window" ) {
328344 memory_window = stoull (it.second );
329- } else if (it.first == " max_n_past_timestamps" ) {
330- max_n_past_timestamps = (uint8_t ) stoi (it.second );
331- } else if (it.first == " batch_size" ) {
332- batch_size = stoull (it.second );
333- } else if (it.first == " n_extra_fields" ) {
334- n_extra_fields = stoull (it.second );
335345 } else if (it.first == " num_iterations" ) {
336346 training_params[" num_iterations" ] = it.second ;
337347 } else if (it.first == " learning_rate" ) {
@@ -363,7 +373,8 @@ class LRBCache : public Cache {
363373 }
364374
365375 negative_candidate_queue = make_shared<sparse_hash_map<uint64_t , uint64_t >>(memory_window);
366- max_n_past_distances = max_n_past_timestamps - 1 ;
376+ // max_n_past_distances = max_n_past_timestamps - 1;
377+
367378 // init
368379 edc_windows = vector<uint32_t >(n_edc_feature);
369380 for (uint8_t i = 0 ; i < n_edc_feature; ++i) {
@@ -386,7 +397,7 @@ class LRBCache : public Cache {
386397 training_params[" categorical_feature" ] = categorical_feature;
387398 }
388399 inference_params = training_params;
389- training_data = new TrainingData ();
400+ training_data = new TrainingData (n_feature, memory_window );
390401 }
391402
392403 string map_to_string (unordered_map<string, string> &map) {
@@ -417,7 +428,7 @@ class LRBCache : public Cache {
417428
418429 void update_stat_periodic () override ;
419430
420- static void set_hash_edc () {
431+ void set_hash_edc () {
421432 max_hash_edc_idx = (uint64_t ) (memory_window / pow (2 , base_edc_window)) - 1 ;
422433 hash_edc = vector<double >(max_hash_edc_idx + 1 );
423434 for (int i = 0 ; i < hash_edc.size (); ++i)
@@ -454,7 +465,7 @@ class LRBCache : public Cache {
454465
455466};
456467
457- static Factory<LRBCache> factoryLRB (" LRB" );
468+ // static Factory<LRBCache> factoryLRB("LRB");
458469
459470}
460471#endif // WEBCACHESIM_LRB_H
0 commit comments