{'fraction_non_alphanumeric': 0.02992874109263658, 'fraction_numerical': 0.01034573766165215, 'mean_word_length': 4.760413499543934, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 0, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Model Size Reduction Using Frequency Based Double Hashing for Recommender Systems

Deep Neural Networks (DNNs) with sparse input features have been widely used in recommender systems in industry. These models have large memory requirements and need a huge amount of training data. The large model size usually entails a cost, in the range of millions of dollars, for storage and communication with the inference services. In this paper, we propose a hybrid hashing method to combine frequency hashing and double hashing techniques for model size reduction, without compromising performance. We evaluate the proposed models on two product surfaces. In both cases, experiment results demonstrated that we can reduce the model size by around 90 % while keeping the performance on par with the original baselines.


Introduction
Recommender systems play an important role in a variety of tasks such as display advertising and ranking news feeds for internet companies, having either pointwise click through rate (CTR) prediction or listwise ranking as an objective. Deep learning-based recommendation systems have been widely adopted in industry during the recent years reaching promising performance in different tasks. For instance, Google's wide and deep model [Cheng et al., 2016] led to 3.9 % improvement of CTR compared to a logistic regression based model in an online setting. According to [Xie et al., 2018], up to 35 % of Amazon's revenue is driven by recommender systems. Figure 1: Illustration of an embedding layer, which operates as a lookup table with keys and their corresponding dense vectors Unlike many traditional DNN tasks, in deep learning-based recommender systems, user-item features along with their interations are commonly modeled as sparse one-hot or multi-hot vectors. This is due to the prevalence of ID and categorical features. Using a hashing function, each sparse vector is mapped to a key or sparse ID in a look-up table and then transformed into a real-valued dense vector using an embedding layer. In order to minimize collisions, the input size of the embedding layer could be very large, as shown in Fig. 1. As a result, deep learning-based recommendation systems usually contain a large amount of parameters and incur huge memory and communication (I/O) costs for training and inference. For instance, Google's wide and deep model [Cheng et al., 2016] has millions of parameters and requires over 500 billion samples for model training. In [Gupta et al., 2020], embedding tables for a single model consume up to tens of GB memory. ByteDance's deep learning-based recommendation system  adopted a similar architecture as [Cheng et al., 2016] and introduced an extra embedding transformation layer for automatic selection of the embedding vector size.
In this paper, we propose a hybrid hashing technique for sparse features to minimize the collision rate, while dramatically decreasing the input size of the embedding layers. This means that the model performance can be preserved with significant model size reduction. In section 2, we review previous works in deep learning-based recommender systems and related hashing techniques. Next, in section 3 we provide technical details about frequency hashing, double hashing and a seamless way to consolidate them. In section 4.2, we present our experiments on two product surfaces. Finally, we conclude our paper in section 5.
The main contribution of the paper includes: • Embedding layers are memory intensive in deep learning-based recommender systems. By reducing the size of embedding layers, we can reduce the large memory capacity challenges in production-scale models. We, thus, propose a hybrid layer to combine frequency hashing with double hashing for sparse input feature embedding, which addresses the pain point of model size in deep learning-based recommender systems.
• Not all features are of equal importance. Instead of applying double hash functions universally on all features, we decide to eliminate hash collision on the subset of most important features by introducing frequency hashing.
• Calculating the hash codes twice using double hashing is computationally expensive, especially in the case of millions of sparse features. With the help of frequency hashing, we only need to apply double hashing on a small amount of low frequency features, which makes the computational complexity comparable to the baseline.
2 Related Works

Sparse Features in Recommendation Systems
In a production-scale recommendation system, there are usually millions of items and users. For each user, only a small number of items get clicked or viewed. The sparsity and dimensionality of user-item representations are typically high in recommender systems. For instance, [Gupta et al., 2020] represents user-item interactions as sparse multi-hot vectors. The wide and deep architecture [ Cheng et al., 2016] represents user-query history as a sparse high-rank matrix. The deep interest network [Zhou et al., 2018] transforms multi-group categories into sparse binary features.
Embedding operations are required to transform those sparse feature representations into dense vectors. However, embedding layers impose challenges in large-scale real-time systems due to their extremely high memory demands. According to [Gupta et al., 2020], the size of a productionscale embedding layer could vary between tens of MB to several GB. Moreover, a production-scale recommender system contains 4 to 40 embedding layers. In total, embedding layers in a single production-scale recommender system can require tens of GB memory or more.

Hashing Techniques
A hash function aims at creating mappings from high-dimensional data to low-dimensional representations, where the latter representation is called a hash code. It is widely used for sparse representations such as recommender systems and natural language processing (NLP). A hash function can be either data-independent or learned from data.
Previously, [Zhang et al., 2018] explored the potential of locality-sensitive hashing (LSH) in recommendation systems. LSH has a very nice property that ensures the probability of collision is much higher for objects that are close to each other than for those that are far away from each other. In other words, this method achieves dimensionality reduction while preserving the proximity of data points in the latent space. Unlike LSH, which is data-independent, learning to hash provides another alternative that is data-dependent. An overview of learning to hash methods is provided in [Wang et al., 2017].  proposed to learn a hash function in a supervised learning manner for efficient fashion recommendation. Graph network based hashing was recently proposed in [Tan et al., 2020]. [Svenstrup et al., 2017] proposed a hash embedding technique for efficient word representations for NLP. Instead of one hash function, they proposed to apply multiple hash functions to map sparse features to binary representations in a manner that minimizes collisions while decreasing the size of embedding tables. Serra et al. [Serrà and Karatzoglou, 2017] proposed similar ideas in recommender systems.

Methods
We propose a hybrid hash embedding layer that adopts techniques from both data-dependent hashing and learning-based hashing, aiming to achieve a trade-off between model size, computational complexity and collision probability. The overall structure of our hybrid layer is illustrated in Fig. 3. Our method includes two main components: (1) double hashing (2) frequency hashing. In this section we first introduce traditional hashing methods as well as some key relevant concepts. Next, we propose to apply double hashing to reduce memory requirements. Finally, we propose frequency hashing to further reduce computational complexity and consolidate it with double hashing. Theoretical analysis further demonstrates that the collision rate decreases with our hybrid method.

Hashing Theory
To create a regular hash embedding for recommender systems, sparse features are randomly mapped to binary codes using a fixed hash function [Dang, 2008]. Each of these hash codes is mapped to  a dense embedding vector, as shown in Fig. 1. We denote the hash function as h : T → B, where T denotes the feature space, B = {1, · · · , B} denotes the hashing space and B denotes the total number of permutations in the hash code. To ease our later discussion on model size, we further define B = 2 b , where b represents the number of bits constructing the hash code. The probability of one or more hash collisions is given by [Girault et al., 1988] and [Suzuki et al., 2008]: The expected number of collisions is given by [College, 2018] and [Might, 2018]: where |T | denotes the total number of features to be hashed. The expected collision rate is given by: The space complexity of the hash embedding is O(Bd) = O(B), where d is a constant value representing the length of the embedding vector. In terms of time complexity, we consider one hashing operation to cost O(1), so the overall complexity to create the hash embedding is O(|T | ln B). In practice, we want B to be large enough to avoid hash collisions, but the hash embedding size grows exponentially as we increase the number of bits in the hash code.

Double Hashing
To improve the trade-off between collision rate and space complexity, we propose to use double hashing motivated by [Svenstrup et al., 2017]. We apply two independent hash functions h 1 , h 2 : T → {1, · · · , B} to map each sparse feature f to two hash codes h 1 (f ), h 2 (f ). Since each hash function can be treated as independent as result of randomization, their combination can be viewed as an approximation of a much larger hash space defined by h : T → {1, · · · , B 2 }. Instead of the B 2 space and computational cost, using this way of hashing only leads to linear cost of 2B. These hash codes are, then, transformed to a dense feature vector g(E(h 1 (f )), E(h 2 (f ))), where E represents the embedding operation, and g denotes certain aggregation operations include: elementwise summation, or concatenation.
As illustrated in Fig. 2, using two hash functions makes the probability of hash collisions almost negligible even with a much smaller hashing space. This ensures that each feature value has a unique embedding, at the cost of some parameter sharing between feature embeddings. More importantly, this causes less chance of information loss and increases representation power compared to using just one hashing function.
The main drawback of double hashing is that we have to apply two different hash functions for each sparse feature, which makes model training and inference slower compared to a single hash function as shown in Fig. 4. To resolve this issue we introduce frequency hashing.

Frequency Hashing
Empirically, we know some features play more important roles when making a recommendation. Collisions on such features will have unwanted impact. One solution to guarantee the lack of feature collisions is to map sparse features to integer IDs {1, · · · , k} using a dictionary. Here, we propose use frequency as a proxy of 'feature importance'. We sort sparse features by frequencies and map the top-K frequent sparse features into an ordered set of unique integers IDs 0, · · · , k − 1. An example of frequency hashing is shown in Fig. 3. Since low frequency sparse features are less likely to collide, we can allocate some additional but less hashing space for them. In this way, we can reduce the collision rate (no collisions for most frequent features), while simultaneously decreasing model size and preserving model performance.
The key idea of the hybrid layer described above is to find the top k most frequent features using historical data. Then we apply a frequency hash function (a dictionary), which maps the most frequent features to their unique embedding and transforms the remaining features using the double hashing embedding technique (see Fig. 3). With the help of frequency hashing, we only need to apply double hashing on a small amount of low frequency features, which makes the computational complexity comparable to using a single hashing function. This is illustrated in Fig. 4.

Theoretical Analysis
We formulate the hash table size, computational complexity and collision rate of the different hashing methods in the table below. This allows a theoretical comparison of the proposed hybrid method with the baseline methods.

Regular Hashing
Double Hashing Frequency Hashing Hybrid Hashing To make the comparison more perceivable, suppose our baseline model have feature space |T | = 4M , B = 2 22 . We calculate the expected collision rates when we apply our hybrid hashing method. We report the full collision rate (two hash keys of double hashing are the same) and half collision rate (one of the hash key of double hashing is the same) when we use two independent hash functions and compare them with the case when we only use one hash function (we only have full collision rate).  [Ktena et al., 2019]. To measure computational cost, we compare the training and inference speed in terms of global steps/second of the Hogwild [Recht et al., 2011] workers using a smoothing factor of 0.995. With model size we refer to the size of the exported Tensorflow model.

Data & Models
We evaluate our method on two product surfaces, ads prediction and timeline ranking. We use the best production-scale model as a baseline and compare it with our proposed method. For ads prediction, the baseline model architecture is based on the wide and deep model [Cheng et al., 2016]. For timeline ranking, the baseline model is a multilayer perceptron as a binary classifier to predict user engagement in timeline. Ranking is then performed based on the binary score of the model. We simply train the model on the previous day of data and evaluate the model on next day of data. For ads prediction, the training data comprises of 2 days' data, while the following 4 hours are used for testing. To simulate real production services in ads prediction, we also train models on a  continuous data stream. In this scenario, we continuously update the model hourly and evaluate it on the data of the next hour. The continuous training and evaluation covers in total a 2-day time period.
Feature Preprocessing Before feeding into the models, sparse features (categorical and id-related features) are mapped to hash codes using a fixed hash function and then transformed to dense vectors using embedding layers. As for the continuous features, we compute their histograms using historical data and discretize them using bins boundaries that correspond to historical percentiles. Those discrete features are processed similarly to sparse features.
Hyperparamaters The hyperparameters used for the offline experiments are: stochastic gradient descent optimizer, learning rate 0.001, batch size 128. To find the best hyperparamaters in the hashing configuration, we first identified the best k value for frequency hashing, while still using regular hashing for the rest of features. We, subsequently, fixed the frequency hashing part to identify the best number of bins B for double hashing. Each experiment was repeated 5 times and we report here the mean and standard deviation across the different runs.

Experiments Results
We perform our experiments on ads prediction model and timeline ranking model. As described above, we experiment with different sizes of the frequency hashing layer while keeping the input size fixed (half of the original input size) for the rest of features. We show comparative results in Table 1 and Table 2. We report the evaluation RCE while the input and model sizes are normalized with respect to the baseline.
For ads prediction, frequency hashing with k = 120K can reduce the model size to 31% while preserving the model performance on par with the baseline. By further increasing the value of k, the gains reach a plateau. Then we fix k = 120K and explore what the optimal input size b for the double hashing layer is in table 1. Since double hashing can decrease collision rates with a much smaller hashing size, we can further reduce the model size to 11.3% of the baseline model, without compromising model performance. Additionally, we use the best model from the previous experiments and train it on a continuous data stream. We compare it with the baseline (production) model in Fig. 5. Results indicate that the proposed method can achieve a marginal but consistent RCE gain compared to the baseline model.
For timeline ranking, we observe that frequency hashing with k = 100K can reduce the model size to 23% with RCE very close to baseline in table 2. When we further increase the value of k, we observe that performance reaches a plateau. This is possibly due to the majority of model sparsity comes from id-related features, where over-memorization of the ids could introduce potential overfitting. Then we fix k = 100K and explore different number of bits b for double hashing. We further reduce the model size to 12.7%, while maintaining the model performance comparable to baseline model.
The main drawback of using double hashing is the increase in computational time. Two different hash functions need to be called for each feature, which slows down both model training and inference.
To get a better estimate of time performance, we compare the number of global steps per second for the baseline model, the model that uses pure double hashing, and the model that uses frequency hashing and double hashing under the same computational constraints in Fig. 4. This illustrates that the most expensive method is double hashing followed by the baseline and hybrid hashing. We observe an almost 22% decrease in the number of global steps per second for the double hashing method compared to the baseline. When frequency hashing is combined with double hashing, the computational time becomes comparable to the baseline.  Figure 5: Comparison of RCE during continuous training. The hybrid hashing method performs consistently better than (or at worst on par with) the baseline in terms of prediction accuracy.

Conclusion
In this paper, we propose to combine frequency hashing with double hashing for model size reduction. Not only does it dramatically reduce the memory requirements in deep learning-based recommender systems, but also decreases the computational cost compared to double hashing alone. Our frequencybased hash function is learned offline using historical data. In future research, we aim to study how to perform dynamic updates of the frequency hashing layer to better support warm starting or continuous learning scenarios. In addition, we can explore feature importance measures other than frequency, e.g. entropy, Fisher Information [Theis et al., 2018], or Gini importance [Menze et al., 2009] to increase the performance of the compressed model.