Method, system, medium and equipment for enhancing context capability of large language model
By introducing memory update and retrieval steps in large language models, combined with local attention states, the problem of high computational complexity in large language models when processing long text contexts is solved, and efficient contextual information processing and dialogue coherence are achieved.
Patent Information
- Application Number
- CN202410767276.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-14
- Publication Date
- 2024-08-30
AI Technical Summary
Current large-scale language models have problems of high computational complexity and high economic cost when processing long text contexts. This is mainly due to the fact that the computational complexity of the attention mechanism in the Transformer-based decoder structure has reached a power of two, resulting in inefficiency in processing long texts. and higher costs.
The memory update step is used to integrate the new input sequence into the current compressed memory state. The memory retrieval step uses the query state to query the long-term memory content from the compressed memory state and combines it with the local attention state to obtain the final context output. Specifically, through the formula MS = σ(K) ⊙ TV + (1 - σ(ZS-1)) ⊙ MS-1 and A = sigmod(β) ⊙ Amem + (1 - sigmod(β)) ⊙ Adot implementation.
Effectively integrate new input information while maintaining memory continuity, adapt to changing contexts, improve the model's ability to process long contexts, ensure the coherence of dialogue, and remain efficient when processing long sequences.
Smart Images

Figure CN118569310A
Abstract
Description
Technical Field
[0001] The present invention relates to the field of large-scale artificial intelligence language models, and in particular to methods, systems, media, and devices for enhancing the contextual capabilities of large-scale language models. Background Art
[0002] With the rapid development of artificial intelligence technology, large language models can be used as personal assistants in daily life, providing a variety of services from answering questions to managing schedules, and even being able to conduct daily conversations. Figure 3 As shown,
[0003] The implementation mainly includes several processes:
[0004] 1. Split knowledge files and vectorize them;
[0005] 2. Question text vectorization and similarity calculation;
[0006] 3. Generate answers based on questions and related text;
[0007] However, current large-scale models have limitations when processing long text contexts, mainly due to their design based on the decoder structure in the transformer, in which the attention mechanism has high computational complexity, making the economic cost of processing long texts very high.
[0008] The current large-scale models are basically trained based on the decoder structure in the transformer. The self-attention mechanism of the decoder is the core component. Its calculation formula is:
[0009] K=XW k ,V=XW v ,Q=XW Q ;
[0010]
[0011] Among them, K, Q, V are the input X through the learnable matrix W k , W v , W Q The dimensions of the mapping matrix after _{\text{length}} are all length×d_model, where length is the number of tokens in the sentence (which can be simply understood as the number of characters or words), and d_model is the dimension of the latent vector, which may be 768, 1024, etc. depending on the model.
[0012] is the calculated and normalized attention matrix.
[0013] The attention mechanism allows the model to consider other elements related to an element (such as a word or character) when processing it, thereby better understanding contextual information.
[0014] The above mechanism is effective, but it also has drawbacks. It is this calculation that has a computational complexity of length 2 , that is, the mechanism has quadratic complexity in computation and memory usage, which makes it very challenging and economically costly for large models to accept longer contexts. Summary of the Invention
[0015] The technical problem solved by the present invention is to provide a method for enhancing the contextual capabilities of large language models, which can better understand contextual information.
[0016] The technical solution adopted by the present invention to solve the technical problem is: a method for enhancing the contextual capabilities of a large language model, comprising the following steps:
[0017] Memory update step: collect new input sequences and integrate them into the current compressed memory state;
[0018] Memory retrieval step: Use the current query state to query long-term memory content from the compressed memory state;
[0019] Long-term context injection step: The retrieved long-term memory content is combined with the current local attention state to obtain the final context output.
[0020] Furthermore, the steps of integrating the new input sequence into the current compressed memory state are as follows:
[0021] Get current memory: Get the current compressed memory of the model;
[0022] Get new keys and values: Get a new input sequence, and the model will generate new keys K and values V from the new input sequence;
[0023] Memory update: Combine the new key K and value V with the current compressed memory to update it into a new compressed memory.
[0024] Furthermore, the new key K and value V are combined with the current compressed memory to update the new compressed memory. The specific formula is:
[0025]
[0026] Among them, M S and M S-1 Represent the compressed memory state of the current time step S and the previous step S-1, σ(K) T is the transposed matrix of the K vector result after the activation function, Indicates that from memory M S-1 The content retrieved from S-1 is a normalization term used for training stability.
[0027] Furthermore, the normalization term Z S-1 Specifically:
[0028]
[0029] where Z S-2 represents the normalization term of the s-2 time step, σ(K t ) is the vector K at time step t t The result of the activation function is N, which represents the number of elements in the currently processed sequence.
[0030] Furthermore, the current query state is used to query the long-term memory content from the compressed memory state, and the retrieval formula is as follows:
[0031]
[0032] Among them, A mem is the content retrieved from memory, and σ(Q) is the matrix of the query vector Q after the nonlinear activation function.
[0033] Furthermore, the retrieved long-term memory content is combined with the current local attention state to obtain the final context output. The specific formula is:
[0034] A=sigmod(β)⊙A mem +(1-sigmod(β))⊙A dot ;
[0035] Among them, sigmod(β) is an activation function used to map the learnable parameter β to a real number in the interval (0, 1), A mem It is the content retrieved from the compressed memory, representing the long-term memory information. dot It is the context representation calculated by the standard local attention mechanism. ⊙ represents element-wise multiplication, which is used to multiply the output of the sigmoid function by the corresponding context. A is the final context output.
[0036] in,
[0037] Among them, K T Represents the transposed matrix of the K vector result, d model Represents the vector dimension.
[0038] Furthermore, it also includes a model fine-tuning step, inputting a specific language environment sequence and performing context output training for a specific environment.
[0039] The present invention also discloses a system for enhancing the contextual capabilities of a large-model personal assistant, including a memory update module, a memory retrieval module, and a long-term context injection module;
[0040] The memory update module is used to collect new input sequences and integrate the new input sequences into the current compressed memory state;
[0041] The memory retrieval module is used to query long-term memory content from the compressed memory state through the current query state;
[0042] The long-term context injection module is used to combine the retrieved long-term memory content with the current local attention state to obtain the final context output.
[0043] The present invention also discloses a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of the method for enhancing the contextual capabilities of a large language model are implemented.
[0044] The present invention also discloses a computer device, comprising a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other via the communication bus; wherein:
[0045] The memory is used to store computer programs;
[0046] The processor is used to execute the steps of the above-mentioned method for enhancing the contextual capabilities of a large language model by running the program stored in the memory.
[0047] The beneficial effects of the present invention are:
[0048] 1. This method uses a memory update step to enable the model to effectively integrate new input information while maintaining memory continuity. This allows the model to adapt to changing contexts and remain efficient when processing long sequences.
[0049] 2. Generate queries closely related to the current conversation through context awareness, thereby retrieving and integrating information related to the user's historical conversations, thereby ensuring the continuity of the conversation. BRIEF DESCRIPTION OF THE DRAWINGS
[0050] Figure 1 A flowchart of a method for enhancing the contextual capabilities of a large language model according to an embodiment of the present application.
[0051] Figure 2 A framework diagram of a system for enhancing the contextual capabilities of large language models according to an embodiment of the present application.
[0052] Figure 3 The diagram is an architecture diagram of an existing large-scale language model in the background technology. DETAILED DESCRIPTION
[0053] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and easy to understand, the specific embodiments of the present invention are described in detail below with reference to the accompanying drawings.
[0054] The present invention discloses a method for enhancing the contextual capability of a large language model, comprising the following steps:
[0055] Memory update step: collect new input sequences and integrate them into the current compressed memory state;
[0056] Memory retrieval step: Use the current query state to query long-term memory content from the compressed memory state;
[0057] Long-term context injection step: The retrieved long-term memory content is combined with the current local attention state to obtain the final context output.
[0058] Specifically, in this step, the new input sequence is integrated into the current compressed memory state, and the steps are:
[0059] Get current memory: Get the current compressed memory of the model;
[0060] Get new keys and values: Get a new input sequence, and the model will generate new keys K and values V from the new input sequence;
[0061] Memory update: Combine the new key K and value V with the current compressed memory to update it into a new compressed memory.
[0062] It should be explained that the large language model described in this application is a large language model of the Transformer architecture. The large language model of the Transformer architecture has a current memory module and has the function of generating new keys K and values V according to the input sequence. The times represented by the above keys K and values V represent the importance and content of each part in the sequence.
[0063] That is, the above operation is that when a new input sequence arrives, the model needs to update the information in the compressed memory, which is done by combining the new key K and value V state with the existing information in the compressed memory.
[0064] Specifically, the new key K and value V are combined with the current compressed memory to update the new compressed memory. The specific formula is:
[0065]
[0066] Among them, M S and MS-1 Represent the compressed memory state of the current time step S and the previous step S-1, σ(K) T is the transposed matrix of the K vector result after the activation function, Indicates that from memory M S-1 The content retrieved from S-1 is a normalization term used for training stability.
[0067] It should be explained that the K vector result here is the key K mentioned above.
[0068] It should be explained that the nonlinear activation function used by σ(K) is the element-wise ELU (Exponential Linear Unit) plus 1, that is:
[0069] σ(K)=ELU(K)+1;
[0070] in,
[0071] Where a is a constant, typically set to 1, and e is the base of the natural logarithm. The ELU function maintains linearity for positive inputs and provides nonlinear activation for negative inputs, which helps speed up neural network training and improve performance.
[0072] Specifically, the normalization term Z S-1 , specifically:
[0073]
[0074] where Z S-2 represents the normalization term of the s-2 time step, σ(K t ) is the vector K at time step t t The result of the activation function is N, which represents the number of elements in the currently processed sequence.
[0075] Specifically, the above M S and M S-1 The formula describes how to merge the new memory states K and V into the existing memory state M. S-1 In the process, a new memory state M is generated. S , while the above The difference between the new V and the V retrieved from memory is calculated, and this difference drives the update, rather than simply overwriting the information.
[0076] The above formula combines the linear and incremental update methods:
[0077] The first one: linear update, the linear update part is σ(K) TV, which directly integrates new keys and values into memory without considering the previous memory contents
[0078] The first one: incremental update, the incremental update part is It first looks at the old information in the current memory state Ms-1 and subtracts it from the new value V. The purpose of this is to remove information that has already been memorized and only add new or changed information.
[0079] In this way, the model is able to effectively integrate new input information while maintaining the continuity of memory. This allows the model to adapt to changing contexts and remain efficient when processing long sequences.
[0080] In this embodiment, the current query state is used to query the long-term memory content from the compressed memory state. The retrieval formula is as follows:
[0081]
[0082] Among them, A mem is the content retrieved from memory, and σ(Q) is the matrix of the query vector Q after the nonlinear activation function.
[0083] In this embodiment, the retrieved long-term memory content is combined with the current local attention state to obtain the final context output. The specific formula is:
[0084] A=sigmod(β)⊙A mem +(1-sigmod(β))⊙A dot ;
[0085] Among them, sigmod(β) is an activation function used to map the learnable parameter β to a real number in the interval (0, 1), A mem It is the content retrieved from the compressed memory, representing the long-term memory information. dot It is the context representation calculated by the standard local attention mechanism. ⊙ represents element-wise multiplication, which is used to multiply the output of the sigmoid function by the corresponding context. A is the final context output.
[0086] in,
[0087] Among them, K T Represents the transposed matrix of the K vector result, d model represents the vector dimension, and Q represents the query vector Q.
[0088] It should be explained that K, Q, and V in this article are the input X through the learnable matrix W. k , W v , W QThe dimensions of the mapping matrix after q_model are all length×d_model, where length is the number of tokens in the sentence (which can be simply understood as the number of characters or words), and d_model is the dimension of the latent vector, which may be 768, 1024, etc. depending on the model. The values of Q, K, and V are obtained by the Transformer architecture's large language model.
[0089] This method can improve the entire model's ability to handle long contexts, thereby ensuring the coherence of the conversation.
[0090] This embodiment also includes a model fine-tuning step, inputting a specific language environment sequence and performing context output training for the specific environment.
[0091] Specifically, fine-tuning is further training based on a pre-trained model to better adapt it to specific downstream tasks. The model described here is a large language model based on the Transformer architecture. After continuous pre-training, the model has acquired the initial ability to handle long contexts. Fine-tuning optimizes the model's performance by training it on a dataset specific to the task.
[0092] In the experiments of this paper, two specific long-context tasks are selected for fine-tuning:
[0093] 1. Key retrieval task: In this task, the model needs to retrieve a specific piece of information, such as a password or key, from a long text. This task tests the model's ability to locate and extract specific information in a long text.
[0094] 2. Book Summarization Task: The goal of the model is to generate a book summary. This task requires the model to not only understand the main content of the text, but also be able to compress and express the essence of this content.
[0095] During fine-tuning, the model typically uses a smaller learning rate and is iteratively trained on a task-specific dataset. This allows the model to adjust its parameters to better perform on the specific task while retaining the ability to process long context learned during pre-training. This, in turn, improves the overall performance of the model.
[0096] The present invention also discloses a system for enhancing the contextual capabilities of a large-model personal assistant, including a memory update module, a memory retrieval module, and a long-term context injection module;
[0097] The memory update module is used to collect new input sequences and integrate the new input sequences into the current compressed memory state;
[0098] The memory retrieval module is used to query long-term memory content from the compressed memory state through the current query state;
[0099] The long-term context injection module is used to combine the retrieved long-term memory content with the current local attention state to obtain the final context output.
[0100] In this system, the memory update module includes a current memory acquisition unit, a new key and value acquisition unit and a memory update unit. The current memory acquisition unit is used to acquire the current compressed memory of the model, and the new key and value acquisition unit is used to acquire a new input sequence. The model generates a new key K and value V for the new input sequence, and the memory update unit is used to combine the new key K and value V with the current compressed memory to update it into a new compressed memory.
[0101] Specifically, the new key K and value V are combined with the current compressed memory to update the new compressed memory. The specific formula is:
[0102]
[0103] Among them, M S and M S-1 Represent the compressed memory state of the current time step S and the previous step S-1, σ(K) T is the transposed matrix of the K vector result after the activation function, Indicates that from memory M S-1 The content retrieved from S-1 is a normalization term used for training stability.
[0104] The normalization term Z S-1 Specifically:
[0105]
[0106] where Z S-2 represents the normalization term of the s-2 time step, σ(K t ) is the vector K at time step t t The result of the activation function is N, which represents the number of elements in the currently processed sequence.
[0107] The current query state is used to query the long-term memory content from the compressed memory state. The retrieval formula is as follows:
[0108]
[0109] Among them, A mem is the content retrieved from memory, and σ(Q) is the matrix of the query vector Q after the nonlinear activation function.
[0110] The retrieved long-term memory content is combined with the current local attention state to obtain the final context output. The specific formula is:
[0111] A=sigmod(β)⊙A mem +(1-sigmod(β))⊙A dot ;
[0112] Among them, sigmod(β) is an activation function used to map the learnable parameter β to a real number in the interval (0, 1), A mem It is the content retrieved from the compressed memory, representing the long-term memory information. dot It is the context representation calculated by the standard local attention mechanism. ⊙ represents element-wise multiplication, which is used to multiply the output of the sigmoid function by the corresponding context. A is the final context output.
[0113] in,
[0114] Among them, K T Represents the transposed matrix of the K vector result, d model represents the vector dimension, and Q represents the query vector Q.
[0115] The present invention also discloses a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of the method for enhancing the contextual capabilities of a large language model are implemented.
[0116] In addition, the computer-readable storage medium of this embodiment may adopt any combination of one or more computer-readable storage media, wherein the computer-readable storage medium includes electrical, optical, electromagnetic, infrared or semiconductor systems, devices or components, or any combination thereof.
[0117] The present invention also discloses a computer device, comprising a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other via the communication bus; wherein:
[0118] The memory is used to store computer programs;
[0119] The processor is used to execute the steps of the above-mentioned method for enhancing the contextual capabilities of a large language model by running the program stored in the memory.
[0120] As an embodiment of the present invention, the communication bus mentioned in the above terminal can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. The communication bus can be divided into an address bus, a data bus, a control bus, etc.
[0121] As an embodiment of the present invention, the communication interface is used for communication between the above-mentioned terminal and other devices.
[0122] As an embodiment of the present invention, the memory may include random access memory (RAM) or non-volatile memory, such as at least one disk storage. Optionally, the memory may also be at least one storage device located remote from the processor.
[0123] As an embodiment of the present invention, the above-mentioned processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.
[0124] Unlike existing techniques, this method utilizes a memory update step that allows the model to maintain memory continuity while effectively integrating new input information. This allows the model to adapt to changing contexts and maintain efficiency when processing long sequences. Furthermore, through context-awareness, it generates queries closely related to the current conversation, retrieving and integrating information related to the user's past conversations, thereby ensuring conversational coherence.
[0125] The specific embodiments described above further illustrate the objectives, technical solutions and beneficial effects of the present invention in detail. It should be understood that the above are only specific embodiments of the present invention and are not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the scope of protection of the present invention.
Claims
1. A method for enhancing the contextual capabilities of a large language model, characterized in that: The steps are: Memory update step: collect new input sequences and integrate them into the current compressed memory state; Memory retrieval step: use the current query state to query long-term memory content from the compressed memory state; Long-term context injection step: The retrieved long-term memory content is combined with the current local attention state to obtain the final context output.
2. The method for enhancing the contextual capabilities of a large language model as claimed in claim 1, characterized in that: The steps of integrating the new input sequence into the current compressed memory state are: Get Current Memory: Get the current compressed memory of the model; Get new keys and values: Get a new input sequence, and the model will generate new keys K and values V from the new input sequence; Memory update: Combine the new key K and value V with the current compressed memory to update it into a new compressed memory.
3. The method for enhancing the contextual capabilities of a large language model as claimed in claim 2, characterized in that: The new key K and value V are combined with the current compressed memory to update the new compressed memory. The specific formula is: Among them, M S and M S-1 Respectively represent the compressed memory state of the current time step S and the previous step S-1, σ(K) T is the transposed matrix of the K vector result after the activation function, Indicates that from memory M S-1 The content retrieved from S-1 is a normalization term used for training stability.
4. The method for enhancing the contextual capabilities of a large language model as claimed in claim 3, characterized in that: The normalization term Z S-1 Specifically: Where Z S-2 represents the normalization term of the s-2 time step, σ(K t ) is the vector K at time step t t By the result of the activation function, N represents the number of elements in the currently processed sequence.
5. The method for enhancing the contextual capabilities of a large language model as claimed in claim 1, characterized in that: The current query state is used to query the long-term memory content from the compressed memory state. The retrieval formula is as follows: Among them, A mem is the content retrieved from memory, and σ(Q) is the matrix of the query vector Q after the nonlinear activation function.
6. The method for enhancing the contextual capabilities of a large language model as claimed in claim 1, characterized in that: The retrieved long-term memory content is combined with the current local attention state to obtain the final context output. The specific formula is: A=sigmod(β)⊙A mem +(1-sigmod(β))⊙A dot ; Among them, sigmod(β) is an activation function used to map the learnable parameter β to a real number in the interval (0, 1), A mem is the content retrieved from compressed memory, representing long-term memory information, A dot is the context representation calculated by the standard local attention mechanism, ⊙ represents element-wise multiplication, which is used to multiply the output of the sigmoid function by the corresponding context element-wise, and A is the final context output; in, Among them, K T Represents the transposed matrix of the K vector result, d model Represents the vector dimension.
7. The method for enhancing the contextual capabilities of a large language model as claimed in claim 1, characterized in that: It also includes a model fine-tuning step, inputting a specific language environment sequence and performing context output training for a specific environment.
8. A system for enhancing the contextual capabilities of a large model personal assistant, characterized in that: It includes memory update module, memory retrieval module and long-term context injection module; The memory update module is used to collect new input sequences and integrate the new input sequences into the current compressed memory state; The memory retrieval module is used to query the long-term memory content from the compressed memory state through the current query state; The long-term context injection module is used to combine the retrieved long-term memory content with the current local attention state to obtain the final context output.
9. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the method for enhancing the contextual capabilities of a large language model as described in any one of claims 1 to 7 are implemented.
10. A computer device, characterized in that: It includes a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other through the communication bus; wherein: The memory is used to store computer programs; The processor is used to execute the steps of the method for enhancing the context capability of a large language model as described in any one of claims 1 to 7 by running the program stored in the memory.
Citation Information
Patent Citations
Cross-modal retrieval method for querying video from complex text based on semantic tree enhancement
CN111897913A
Long document retrieval method based on memory network semantic fusion
CN113962228A
Imby detection method and system based on knowledge enhancement neural network model
CN117454873A
Cited By
Long-term dialogue memory compression and dialogue model adaptation method
CN120633872A
Rapid decoding method and decoding circuit of Transform decoder
CN121936515A
