A feed model embeds billions of user and item ids, far more than fit in a fixed table, and hashing ids modulo the table size makes unrelated ids share a row. ByteDance's Monolith system replaces the modulo table with a cuckoo hash map that stores exactly one row per admitted id, only admits an id once it has been seen enough times, and evicts ids that stop appearing. Implement a small version with pure Python and NumPy.
EmbeddingTable(capacity, dim, min_count, ttl, max_kicks=50)
capacity slots (self.t0, self.t1), each slot holding None or an (id, vector, last_seen) list; hash functions h0(id) = (id * 2654435761) % capacity and h1(id) = ((id >> 16) ^ (id * 40503)) % capacity.self.counts: dict of sightings for ids not yet admitted.touch(id, t)
t. If id is stored, update its last_seen to t and return 'hit'. Otherwise increment its count; if the count is still below min_count return 'filtered'. Else insert it with a zero vector of length dim by cuckoo insertion: try t0[h0]; if occupied, place the new entry there and move the displaced entry to its slot in the other array (t1[h1] for an entry displaced from t0, t0[h0] for one displaced from t1), repeating up to max_kicks displacements. Return 'inserted' on success. If the kick limit is hit, undo nothing further, drop the entry currently in hand and return 'full'. Remove the id from self.counts on insertion.lookup(id)
None.update(id, grad, lr)
lr * grad from the stored vector in place; return True, or False if the id is not stored.expire(t)
last_seen is older than t - ttl (strictly less than t - ttl). Return the number removed.stats()
(stored, pending): the number of stored ids and the number of ids waiting in self.counts.(array_index, slot) so lookups do not probe both arrays; update it on every move and removal.