Cache replacement method and system based on gated loop and multi-head attention mechanism
By introducing a gated loop and a multi-head attention mechanism into the cache system to calculate the replacement probability of cache lines, it solves the problems of low efficiency and neglect of content correlation of traditional cache strategies in adapting to dynamic data access patterns and large-scale data processing, and achieves It makes more efficient and intelligent cache replacement decisions and improves the overall performance of the cache system.
Patent Information
- Application Number
- CN202410953981.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-17
- Publication Date
- 2024-10-18
- Estimated Expiration
- 2044-07-17
AI Technical Summary
Traditional caching strategies cannot effectively adapt to dynamic changes in data access patterns, are inefficient especially when processing large-scale data, and ignore the correlation of data content, making it difficult to achieve optimal caching effects in complex application scenarios.
A cache replacement method based on gated loop and multi-head attention mechanism is adopted. By converting the cache line address and program counter into an embedding vector, the gated loop unit and multi-head attention mechanism are used to calculate the context vector, and combined with the fully connected layer to output the cache line Replacement probabilities, and design loss functions to optimize network parameters to achieve smarter caching decisions.
It improves the adaptability and prediction capabilities of the cache system, achieves a more globally optimal cache replacement decision, improves the cache hit rate and overall performance, and overcomes the limitations of traditional heuristic methods.
Smart Images

Figure CN118503152B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer cache technology, and in particular to a cache replacement method and system based on gated loop and multi-head attention mechanism. Background Art
[0002] The statements in this section merely provide background information related to the present invention and do not necessarily constitute prior art.
[0003] Cache is a high-speed data storage layer whose main purpose is to provide fast data access and reduce the need to access back-end databases or remote data sources. Cache can significantly improve the responsiveness and overall performance of an application, especially when dealing with large amounts of read operations and repeated data requests.
[0004] Caching strategies are a series of strategies and mechanisms used to manage data in the cache, with the goal of maximizing the cache hit rate and thus improving system performance. Although traditional caching strategies are widely used in many scenarios, they have some common defects and shortcomings. First, these strategies often cannot adapt well to the dynamic changes in data access patterns because they make decisions based on simple rules and have no ability to learn and predict future access patterns from historical access data. Second, they are inefficient when processing large-scale data, especially when it is necessary to consider the multidimensional characteristics of the data, such as access frequency and timing. In addition, these strategies often ignore the relevance of data content and cannot make intelligent caching decisions based on the similarity of data content. Finally, fixed cache replacement strategies lack flexibility in the face of complex application scenarios and it is difficult to achieve the optimal caching effect. Summary of the invention
[0005] In order to solve the technical problems existing in the above-mentioned background technology, the present invention provides a cache replacement method and system based on gated loop and multi-head attention mechanism. The present invention applies machine learning to cache optimization to meet the ever-changing data processing needs, improve the performance and intelligence of the cache system, better cope with the increasingly complex and multilateral data needs, and realize a more efficient cache system.
[0006] In order to achieve the above object, the present invention adopts the following technical solution:
[0007] The first aspect of the present invention provides a cache replacement method based on gated loop and multi-head attention mechanism.
[0008] The cache replacement method based on gated loop and multi-head attention mechanism includes:
[0009] Convert the currently accessed cache line address and program counter obtained into an embedded vector;
[0010] Based on the embedding vector and the hidden state of the previous time step, a gated recurrent unit is used to obtain the hidden state of the current time step as one of the inputs of the gated recurrent unit of the next time step;
[0011] The concatenated hidden states and cache line addresses of several time steps are input into the multi-head attention mechanism to obtain the context vector;
[0012] The context vector is fed into a fully connected layer, which outputs the probability of each cache line being replaced.
[0013] Furthermore, the gated recurrent unit processes the embedding vector time-step by time-step and updates the internal state of the gated recurrent unit to capture the sequence pattern and context information.
[0014] Furthermore, the process of using the multi-head attention mechanism includes taking the embedding vector of the cache line address as the query input, taking the concatenated hidden states of several time steps as the key and value input, introducing the relative position encoding between the time steps, and calculating the outputs of several attention heads; concatenating the outputs of all attention heads and performing a linear transformation to obtain the context vector of each cache line.
[0015] Furthermore, the relative position between the time steps is encoded as the difference between the previous time step and the next time step.
[0016] Furthermore, after outputting the probability of each cache line being replaced, the method further includes: selecting a cache line with the highest probability for replacement.
[0017] Furthermore, during the training of the gated loop, multi-head attention mechanism and fully connected layer, a loss function is designed to measure the difference between the output of the fully connected layer and the optimal decision calculated using the Belady strategy. The network parameters of the gated loop, multi-head attention mechanism and fully connected layer are optimized with the goal of minimizing the loss function.
[0018] The second aspect of the present invention provides a cache replacement system based on gated loop and multi-head attention mechanism.
[0019] Cache replacement system based on gated loop and multi-head attention mechanism, including:
[0020] An embedding module is configured to: convert the acquired cache line address and program counter of the current access into an embedding vector;
[0021] A gated recurrent unit module, configured to: use a gated recurrent unit based on the embedding vector and the hidden state of the previous time step to obtain the hidden state of the current time step as one of the inputs of the gated recurrent unit of the next time step;
[0022] A multi-head attention mechanism module is configured to: input the concatenated hidden states and cache line addresses of several time steps into the multi-head attention mechanism to obtain a context vector;
[0023] A fully connected and output module, which is configured to: input the context vector into the fully connected layer, transform the context vector into a form for making decisions, and use a softmax function to calculate the probability of each cache line being replaced.
[0024] Furthermore, the gated recurrent unit module is also configured to: process the embedding vector time-step by time-step, and update the internal state of the gated recurrent unit to capture sequence patterns and context information.
[0025] Furthermore, the multi-head attention mechanism module is also configured to: use the embedding vector of the cache line address as the query input, use the concatenated hidden states of several time steps as the key and value input, introduce the relative position encoding between the time steps, and calculate the outputs of several attention heads; concatenate the outputs of all attention heads and perform linear transformation to obtain the context vector of each cache line.
[0026] Furthermore, the relative position between the time steps is encoded as the difference between the previous time step and the next time step.
[0027] Furthermore, the full connection and output module is also configured to: select the cache line with the highest probability for replacement.
[0028] Furthermore, during the training of the gated loop, multi-head attention mechanism and fully connected layer, a loss function is designed to measure the difference between the output of the fully connected layer and the optimal decision calculated using the Belady strategy. The network parameters of the gated loop, multi-head attention mechanism and fully connected layer are optimized with the goal of minimizing the loss function.
[0029] Compared with the prior art, the present invention has the following beneficial effects:
[0030] 1. The present invention flexibly adjusts the information flow through the update gate and reset gate of the GRU module. This mechanism enables the model to better adapt to different cache access patterns and sequence dependencies. In the scenario of cache replacement strategy, this helps the model to more accurately predict which cache lines will no longer be accessed, so as to make better replacement decisions.
[0031] 2. The end-to-end cache replacement policy optimization method based on supervised learning proposed in the present invention is expected to overcome the limitations of traditional heuristic methods by directly searching for approximate optimal strategies in a wider policy space, achieve more globally optimal cache replacement decisions, and improve the overall performance of the cache system.
[0032] 3. The strategy model proposed in the present invention is mainly based on the gated recurrent unit GRU (Gate Recurrent Unit) and the multi-head attention mechanism, and uses supervised learning to learn cache access patterns. This method learns a theoretically optimal strategy - the Belady algorithm. When cache space needs to be freed up, the cache lines that will not be accessed in the longest time in the future are selected for elimination. However, since future cache access is unknown and cannot be directly applied, the present invention trains a strategy based only on past access records to effectively approximate the Belady algorithm. Its purpose is to solve the limitations of traditional cache strategies such as relying on static rules or fixed parameters, flexibly adapt to changing workloads and improve the cache hit rate of cache strategies. BRIEF DESCRIPTION OF THE DRAWINGS
[0033] The accompanying drawings in the specification, which constitute a part of the present invention, are used to provide a further understanding of the present invention. The exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute improper limitations on the present invention.
[0034] Figure 1 It is a flow chart of a cache replacement method based on gated loop and multi-head attention mechanism provided in the first embodiment of the present invention;
[0035] Figure 2 is a model training flow chart provided in Embodiment 1 of the present invention;
[0036] Figure 3 It is a structural diagram of a cache replacement system based on gated loop and multi-head attention mechanism provided in Example 2 of the present invention. DETAILED DESCRIPTION
[0037] The present invention will be further described below in conjunction with the accompanying drawings and embodiments.
[0038] Embodiment 1
[0039] like Figure 1As shown, this embodiment provides a cache replacement method based on gated loop and multi-head attention mechanism. This embodiment uses the method applied to the server as an example for illustration. It can be understood that the method can also be applied to the terminal, and can also be applied to a terminal, a server and a system, and is implemented through the interaction between the terminal and the server. The server can be an independent physical server, or a server cluster or a distributed system composed of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network servers, cloud communications, middleware services, domain name services, security services CDN, and big data and artificial intelligence platforms. The terminal can be a smart phone, a tablet computer, a laptop computer, a desktop computer, a smart speaker, a smart watch, etc., but is not limited to this. The terminal and the server can be directly or indirectly connected via wired or wireless communication, which is not limited in this application. In this embodiment, the method includes the following steps:
[0040] Step S101: The cache line address (memory address) currently being accessed and the unique program counter (PC) associated with the access are taken as inputs. These inputs are the starting point of the model decision process. They provide contextual information of the current cache access for use in subsequent layers of the model.
[0041] Step S102: Convert the received raw data (cache line address and program counter) into a fixed-length vector representation that the model can process. In the model, each unique memory address and program counter is assigned an embedding vector;
[0042] Step S103: Use a gated recurrent unit (GRU) to process the sequence data.
[0043] Specifically, in the model, GRU can process the embedding vectors of the memory address and program counter output by the embedding layer one time step at a time, and update its internal state to capture sequence patterns and context information. At each time step t, GRU receives the currently accessed embedding vector and the hidden state of the previous time step as input, and outputs a hidden state of the current time step , as one of the inputs of the gated recurrent unit in the next time step. In order to obtain a fixed-length historical access representation, the hidden states of the most recent H time steps are concatenated as , as the input of the next layer of multi-head attention mechanism. This approach allows the multi-head attention mechanism to focus on different parts of the past H time steps in different representation subspaces, capturing relevant historical access patterns while maintaining computational and storage efficiency.
[0044] Step S104: The multi-head attention mechanism is used to create a context vector for each cache line, taking the embedding of each cache line as the query and the hidden state of the GRU as the key and value. In this way, the model can focus on different parts of the access history with different representation subspaces, enhancing the expressiveness of the model.
[0045] In some embodiments, for each cache line in a cache state , calculate its context vector through the multi-head attention mechanism :
[0046] a) Each cache line Embed as , Represents a cache line Embedding vector of the address;
[0047] b) As the query input, As input key and value, compute h attention heads:
[0048] ;
[0049] in, , , is the learnable projection matrix of the ith attention head, Represents the time step To time step The relative position encoding between Is a relative position The hidden state of the concatenated H time steps in the most recent time step As the input of the multi-head attention mechanism.
[0050] Example:
[0051] Assume there is a sequence of hidden states , relative position encoding The calculation of is as follows:
[0052] For hidden state and , the relative position encoding is: ;
[0053] For hidden state and , the relative position encoding is: .
[0054] c) When calculating the attention weight, add relative position encoding:
[0055] ;
[0056] in, , Indicates a query; , Indicates the key; , Represents a value; , Embedding vector representing relative position; Indicates the dimension of the key vector.
[0057] d) Concatenate the outputs of all attention heads and transform them through a linear layer to obtain the context vector for each cache line :
[0058] ;
[0059] in, It is also a learnable projection matrix.
[0060] The multi-head attention mechanism allows the model to focus on different parts of the access history in different representation subspaces. The introduced relative position encoding enhances the model's expressiveness, allowing it to focus not only on the content itself but also on the relative position between the contents. Each head can learn different attention patterns to capture different aspects of the access history. Finally, the information from each head is combined to form the final cache line representation.
[0061] The present invention enhances the expressive power of the multi-head attention mechanism, enabling it to focus not only on the content itself but also on the relative positions between the contents. This enhanced expressive power helps the model to more accurately capture complex access patterns and improve the performance of cache replacement strategies.
[0062] When processing long sequence data, absolute position encoding may cause information loss or confusion. Relative position encoding can process long sequence data more efficiently and maintain the integrity of sequence information by directly representing the relative positions between elements.
[0063] The relative position encoding designed by the present invention can capture the relative position relationship between elements in the sequence, rather than just the absolute position. This is especially important for cache access sequences, because cache replacement decisions often depend on the relative time interval between accesses. For example, the cache line that was accessed recently may be more important than the one that was accessed earlier.
[0064] Step S105: The fully connected layer receives the context vector from the multi-head attention mechanism layer and converts it into a form that the model ultimately uses to make decisions;
[0065] Step S106: The output layer uses a softmax function to calculate the probability of each cache line being replaced, and the model ultimately selects the cache line with the highest probability for replacement.
[0066] In some embodiments, the model is trained by the following steps: Figure 2 As shown:
[0067] Step S201: The strategy is a series of cache access sequences ( , ),…,( , ) as input. Each cache access consists of a memory address ( ) and the program counter associated with the access ( ) is a tuple representation of .
[0068] Step S202: Convert the cache access sequence into a series of state representations. Each state encapsulates information about the current cache access, the current state of the cache (i.e., which data lines are currently in the cache), and the history of past cache accesses. This state conversion is a necessary prerequisite for the model to make eviction decisions, because eviction decisions must be based on a comprehensive consideration of the cache's current state and access history.
[0069] Step S203: The strategy samples states in batches for training. Each batch consists of a series of consecutive states, allowing the model to learn from cache access sequences.
[0070] Step S204: Use the initial H visits To initialize the hidden state of the GRU, we use . This is because the performance of the GRU may be unstable in the first few steps until enough context information is accumulated.
[0071] Step S205: The model applies its current policy to each state in the batch to predict the elimination decision. The policy calculates a loss function that measures the difference between the model's prediction and the optimal decision calculated using the Belady policy.
[0072] Step S206: Update the parameters of the strategy model according to the loss L to minimize the loss, so that the prediction result of the strategy model is more consistent with the optimal decision.
[0073] Step S207: The training process iterates the input cache access sequence and repeatedly updates the parameters of the model to improve its elimination decision. Determine whether the training step number is reached, if so, output the trained model, otherwise go to step S201.
[0074] Through this training process, the model learns how to predict the optimal cache replacement decision based on historical access patterns, even if it has no future access information. This training method uses the idea of supervised learning, which is to try to learn a given "expert" strategy (here is the Belady optimal strategy) and approximate the performance of this strategy through continuous iterative optimization.
[0075] This embodiment provides a cache replacement method based on a gated loop and a multi-head attention mechanism, the purpose of which is to improve the cache hit rate and reduce the cache miss rate, thereby reducing the time and network bandwidth required for searching the background storage. The core idea of the cache strategy model is to learn a near-optimal cache replacement strategy through supervised learning, namely the Belady optimal replacement algorithm. The Belady algorithm can make the best replacement decision when future cache access information is known, but in actual situations, future access information is unknown. The cache replacement model proposed in the present invention attempts to predict which cache line is least likely to be accessed again in the short term by learning historical cache access patterns, thereby replacing it out of the cache.
[0076] Embodiment 2
[0077] like Figure 3 As shown, this embodiment provides a cache replacement system based on gated loop and multi-head attention mechanism for the CPU last-level cache, including:
[0078] The input module is configured to take the currently accessed cache line address (memory address) and the unique program counter (PC) associated with the access as input. These inputs are the starting point of the model's decision process. They will be converted into embedded representations for use in subsequent layers of the model.
[0079] The embedding module is configured to convert the received raw data (cache line addresses and program counters) into a fixed-length vector representation that the model can process. In the model, each unique memory address and program counter is assigned an embedding vector.
[0080] The gated recurrent unit module is configured to use a gated recurrent unit (GRU) to process sequence data. In the model, the GRU is used to process the embedding vectors of the memory address and program counter output by the embedding layer. At each time step t, the GRU receives the currently accessed embedding vector and the hidden state of the previous time step as input, and outputs the hidden state of the current time step. , as one of the inputs of the gated recurrent unit at the next time step.
[0081] The multi-head attention mechanism module is configured to create a context vector for each cache line, use the embedding of each line as the query, and use the hidden state of the GRU as the keys and values. In this way, the model can focus on different parts of the access history in different representation subspaces, enhancing the expressiveness of the model.
[0082] A fully connected module that is configured to receive the context vector from the attention layer and transform it into a form that the model ultimately uses to make decisions.
[0083] The output module is configured to use the softmax function to calculate the probability of each cache line being replaced, and the model ultimately selects the cache line with the highest probability for replacement.
[0084] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, the present invention may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
Claims
1. A cache replacement method based on gated loop and multi-head attention mechanism, characterized in that: include: Convert the currently accessed cache line address and program counter obtained into an embedded vector; Based on the embedding vector and the hidden state of the previous time step, a gated recurrent unit is used to obtain the hidden state of the current time step as one of the inputs of the gated recurrent unit of the next time step; The concatenated hidden states and cache line addresses of several time steps are input into the multi-head attention mechanism to obtain the context vector; The context vector is input into the fully connected layer, which outputs the probability of each cache line being replaced. The method of inputting the concatenated hidden states and cache line addresses of several time steps into the multi-head attention mechanism to obtain a context vector includes: a) Each cache line Embed as , Represents a cache line Embedding vector of the address; b) As the query input, As input key and value, compute h attention heads: ; in, , , is the learnable projection matrix of the ith attention head, Represents the time step To time step The relative position encoding between Is a relative position The embedding vector of the last H time steps is the concatenated hidden state As the input of the multi-head attention mechanism; Assume there is a sequence of hidden states , relative position encoding The calculation of is as follows: For hidden state and , the relative position encoding is: ; For hidden state and , the relative position encoding is: ; c) When calculating the attention weight, add relative position encoding: ; in, , Indicates a query; , Indicates the key; , Represents a value; , Embedding vector representing relative position; represents the dimension of the key vector; d) Concatenate the outputs of all attention heads and transform them through a linear layer to obtain the context vector for each cache line : ; in, is also a learnable projection matrix.
2. The cache replacement method based on gated loop and multi-head attention mechanism according to claim 1, characterized in that: The gated recurrent unit processes the embedding vector time-step by time-step and updates the internal state of the gated recurrent unit to capture the sequence pattern and context information.
3. The cache replacement method based on gated loop and multi-head attention mechanism according to claim 1, characterized in that: After outputting the probability of each cache line being replaced, the method further includes: selecting the cache line with the highest probability for replacement.
4. The cache replacement method based on gated loop and multi-head attention mechanism according to claim 1, characterized in that: During the training of the gated loop, multi-head attention mechanism and fully connected layer, a loss function is designed to measure the difference between the output of the fully connected layer and the optimal decision calculated using the Belady strategy. The network parameters of the gated loop, multi-head attention mechanism and fully connected layer are optimized with the goal of minimizing the loss function.
5. A cache replacement system based on gated loop and multi-head attention mechanism, characterized in that: include: An embedding module is configured to: convert the acquired cache line address and program counter of the current access into an embedding vector; A gated recurrent unit module, configured to: use a gated recurrent unit based on the embedding vector and the hidden state of the previous time step to obtain the hidden state of the current time step as one of the inputs of the gated recurrent unit of the next time step; A multi-head attention mechanism module is configured to: input the concatenated hidden states and cache line addresses of several time steps into the multi-head attention mechanism to obtain a context vector; A fully connected and output module, which is configured to: input the context vector into the fully connected layer, convert the context vector into a form for making decisions, and use a softmax function to calculate the probability of each cache line being replaced; The multi-head attention mechanism module is specifically configured as follows: a) Each cache line Embed as , Represents a cache line Embedding vector of the address; b) As the query input, As input key and value, compute h attention heads: ; in, , , is the learnable projection matrix of the ith attention head, Represents the time step To time step The relative position encoding between Is a relative position The embedding vector of the last H time steps is the concatenated hidden state As the input of the multi-head attention mechanism; Assume there is a sequence of hidden states , relative position encoding The calculation of is as follows: For hidden state and , the relative position encoding is: ; For hidden state and , the relative position encoding is: ; c) When calculating the attention weight, add relative position encoding: ; in, , Indicates a query; , Indicates the key; , Represents a value; , Embedding vector representing relative position; represents the dimension of the key vector; d) Concatenate the outputs of all attention heads and transform them through a linear layer to obtain the context vector for each cache line : ; in, is also a learnable projection matrix.
6. The cache replacement system based on gated loop and multi-head attention mechanism according to claim 5, characterized in that: The gated recurrent unit module is further configured to: process the embedding vector time-step by time-step, and update the internal state of the gated recurrent unit to capture sequence patterns and context information.
Citation Information
Patent Citations
Cache replacement system and method based on imitation learning
CN114780889A
Selectable caching policy
CN117280329A
Excitation mechanism-based edge cooperation caching method and system in vehicle edge network
CN117939505A
Data collaborative caching method based on multi-agent federal reinforcement learning
CN118317372A
Selectable cache policy
US20230236977A1
