Large model knowledge boundary sensing method, system and device based on contrast decoding and medium

By constructing a contrastive decoding strategy that combines conflicting cue words and attention-emphasis mechanisms, the problem of uncertain output in open domains by large-scale pre-trained language models is solved, enabling explicit annotation and efficient decoding of knowledge boundaries by the model.

CN121960737APending Publication Date: 2026-05-01SOUTH CHINA UNIV OF TECH
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202511990442.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-12-26
Publication Date
2026-05-01

AI Technical Summary

Technical Problem

Existing large-scale pre-trained language models struggle to accurately represent uncertainty in open domains, easily outputting erroneous or false content. Furthermore, existing technologies suffer from poor interpretability in enhancing the model's ability to identify knowledge boundaries.

Method used

We employ a large-scale model knowledge boundary perception method based on contrastive decoding. By constructing conflicting positive and negative cue words, using special identifiers to mark uncertain and certain text fragments, and calculating the probability increment of candidate tokens through attention emphasis mechanism and contrastive decoding strategy, we enhance the model's ability to perceive knowledge boundaries.

Benefits of technology

This allows for explicit labeling of uncertain segments in the model output, improving the interpretability and controllability of the model output, maintaining information integrity, and reducing computational overhead and response time.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121960737A_ABST
    Figure CN121960737A_ABST
Patent Text Reader

Abstract

The invention discloses a large model knowledge boundary sensing method, system and equipment based on comparative decoding and a medium, and the method comprises the steps: obtaining a user question, constructing a pair of positive and negative prompt words which are opposite in conflict, requiring a large model to output an answer through the positive prompt word, labeling uncertain fragments in the answer through a special identifier, and obtaining a user question; the reverse cue word requires the large model to output an answer, and a special identifier is used to label a determined segment in the answer; a pair of cue words are respectively spliced with user questions to be input into the same large model, and an attention emphasis mechanism is adopted to carry out weighted emphasis on instructions related to special identifiers, so that the attention of the large model on the special identifiers is increased. And finally, comparing decoding, comparing decoding probability distribution of forward and reverse cue words, optimizing code adding probability distribution of the forward cue words, enhancing knowledge boundaries of a large model perceiving the large model, outputting answers, and positioning uncertain fragments in replies through special identifiers to reduce illusion phenomena.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of natural language processing technology, specifically relating to a method, system, electronic device, and storage medium for perceiving knowledge boundaries of large models based on contrastive decoding. Background Technology

[0002] With the widespread application of large-scale pre-trained language models in the field of natural language processing, they have demonstrated powerful language understanding and generation capabilities in tasks such as question answering, reasoning, and generation. However, in open-domain applications, when faced with questions beyond the scope of their training knowledge, these models often struggle to accurately express their own uncertainties, easily outputting erroneous or false content—the so-called "illusion" problem, which is also a manifestation of model overconfidence. This phenomenon not only weakens the credibility of the model's output but also poses potential security risks in high-risk scenarios such as healthcare, law, and education.

[0003] To alleviate the above problems, existing technologies mainly fall into four categories: (1) Model-based validation method: The model outputs a "confidence score" for each answer (or prediction), and then a threshold is set: if the confidence score is lower than the threshold, the answer is abandoned; otherwise, the answer is given. The disadvantage is that the model itself is overconfident, which often makes its confidence score for its own answer inflated.

[0004] (2) Training-based methods: Add a validator or classifier (e.g., a linear probe) inside the model to discriminate the hidden layer representations throughout the QA process and determine whether the answers generated by the model are credible. Alignment between answers and rejections can also be achieved through model fine-tuning (SFT) or post-training such as DPO, allowing the model to learn to perceive knowledge boundaries through training on a large amount of data. The disadvantage is that a large amount of fine-tuning data needs to be collected for a specific model, resulting in high training costs. (3) Prompt-based methods: Utilize prompts and instruction strategies to induce the model to introspect, self-evaluate, and request more information before answering, allowing the model to determine whether it should reject the answer. The disadvantage is that the pre-trained model itself has weak knowledge boundary perception capabilities and cannot effectively follow prompt instructions.

[0005] (4) Consistency-based methods: These methods determine whether an answer should be rejected by having the model reason / respond multiple times and then checking the consistency / difference of the answers. For example, consistency testing methods involve voting or aggregating the most consistent answer from multiple reasoning paths in the thought chain reasoning process. The disadvantages are high reasoning overhead and long response time.

[0006] Meanwhile, existing technologies suffer from poor interpretability in enhancing a model's ability to identify knowledge boundaries. Introducing uncertainty mechanisms may cause models to completely refuse to answer ambiguous questions instead of providing hints about uncertain parts in their responses, thus reducing the user experience.

[0007] Therefore, there is an urgent need for a knowledge boundary awareness method that can explicitly identify uncertain fragments in the model's response, rather than rejecting the problem as a whole, in order to achieve more granular and interpretable output, thereby maintaining information integrity while ensuring the credibility of the response. Summary of the Invention

[0008] The main objective of this invention is to overcome the shortcomings and deficiencies of the prior art and provide a method, system, electronic device and storage medium for perceiving the knowledge boundaries of large models based on contrastive decoding. This method can enhance the ability of large models to perceive their own knowledge boundaries in various fields and thus avoid output illusions.

[0009] To achieve the above objectives, the present invention adopts the following technical solution: A knowledge boundary awareness method for large-scale pre-trained language models based on contrastive decoding is proposed. This method, referred to as "large-scale pre-trained language models" (or simply "large models"), uses natural language text as both its input and output. The output is the large model's response to the input text. The knowledge boundary awareness method for large models includes the following steps: S1. Construct a pair of conflicting positive and negative prompts. The positive prompt is a piece of natural language text. When the prompt requires the large model to output a response, any uncertain text segments are marked with a special identifier. The negative prompt is also a piece of natural language text. When the prompt requires the large model to output a response, any definite text segments are marked with a special identifier. The user's question text is then appended to both the positive and negative prompts. This generates complete opposing positive and negative prompt instructions, which are input into the large model. The large model includes an input embedding layer, a multi-layer decoder, and an output layer connected in sequence. Each decoder contains a multi-head attention layer, a normalization layer, a residual connection layer, and a forward propagation layer connected in sequence. S2. Calculate the attention scores in the multi-head attention layers of the intermediate decoders after the positive prompt word instruction is input into the large model, and weight the attention scores of the positions of special identifiers in the positive prompt word; emphasize the meaning of the special identifiers and the related instructions, and enhance the large model's ability to follow the instructions of the positive prompt word; S3. Input the positive prompt word instruction and the negative prompt word instruction into the large model for decoding, and obtain the top k candidate tokens with the highest probability respectively. Take the union of the 2k candidate tokens to construct the token candidate pool. S4. Compare the probability distribution differences of each token in the token candidate pool to obtain the probability increment of each candidate token. S5. Input the positive prompt word command into the large model to obtain the probability score of each candidate token in the output layer of the large model. Add the probability increment of the candidate tokens to obtain the final probability score of each token. Take the token with the highest probability score as the final output of the large model.

[0010] In step S2 of this embodiment, the attention scores S of the intermediate encoders after the positive prompt word instruction is input into the large model are calculated. The formula for calculating the attention score S is as follows:

[0011] Here, Q and K are abbreviations for the Query matrix and Key matrix in the attention mechanism, respectively. This represents the dot product of the transposes of Q and K. , R represents a real number, n represents the number of tokens for the input positive prompt word; d represents the dimension of the preset large model hidden space; By calculating the attention score S, the attention distribution of each instruction token in the positive prompt word within the model is characterized, thus providing a basis for subsequent attention control of specific instruction positions.

[0012] The attention weight matrix A is calculated using the function Softmax() on the attention score S, where the attention weight matrix A ∈ The attention weight matrix A is used to characterize the correlation between tokens in the positive input prompts. The calculation method is as follows:

[0013] The Softmax function normalizes attention scores into attention weights with a row sum of 1, providing a controllable and scalable operational space for subsequent attention weighting and emphasis. This applies to the elements in the i-th row and j-th column of the attention weight matrix A. The calculation method is as follows:

[0014] in , This represents the attention weight of the interaction between the i-th token and the j-th token in the input positive prompt; Indicates the token corresponding to the i-th token The token corresponding to the j-th token The inner product result between them is used to characterize the correlation between the i-th token and the j-th token; Mark the m-th token containing the special identifier in the positive prompt word instruction, where The attention score for column index m, while keeping the i-th row unchanged. Perform weighted emphasis and set the weighted emphasis coefficient. The weighted attention weights are:

[0015] in, Let represent the attention score of the i-th token to the m-th token; This represents the attention score of the i-th token to the t-th token before normalization using the Softmax function. This indicates that the value of t is not equal to m; by enhancing the attention weight of the position of the special identifier, the large model can maintain a higher attention to the "uncertainty labeling" related instructions in the subsequent decoding process, thereby solving the problem that the model is prone to ignoring or weakening the uncertainty labeling instructions.

[0016] In step S3 of this embodiment, after the positive and negative prompt words are input into the large model, the probability distributions of the next predicted word are obtained. The k words with the highest probability scores from the probability distribution of the next predicted word obtained under the positive prompt word are selected as the first candidate pool, and the k words with the highest probability scores from the probability distribution of the next predicted word obtained under the positive prompt word are selected as the second candidate pool. The union of the two candidate pools of size k yields the final token candidate pool. By constructing token candidate words, the premature convergence of candidate tokens to a single generation tendency during the decoding process is avoided, providing a more sufficient candidate space for subsequent probability difference calculations based on comparative decoding, thereby improving the probability of identifying and retaining uncertain related tokens.

[0017] In step S4 of this embodiment, the probability increment of each candidate token in the token candidate pool is obtained by comparing and decoding the probability scores of positive and negative prompt words. By comparing the predicted probability distributions of the same candidate token under positive and negative cue words, we can extract the difference signals reflecting whether the token is more inclined to "uncertainty labeling" or "deterministic expression":

[0018] in, This refers to the probability distribution of each token in the token candidate pool under positive prompt words. This refers to the probability distribution of each token in the token candidate pool under the reverse suggestion word. This is a balance parameter used to avoid the numerator and denominator being too small.

[0019] In step S5 of this embodiment, the probability distribution P of the token candidate pool obtained from the large model based on the input positive prompt word instruction is used to increment the probability of the positive prompt word. The purpose of weighted aggregation is to directly introduce the probability increment obtained from comparative decoding into the decoding decision stage. This allows the model to comprehensively consider the difference between the original predicted probability and the uncertainty when generating the next token, so that candidate tokens that are more advantageous under the constraint of positive prompt words can achieve higher overall scores than other candidate tokens during the decoding process.

[0020] in, This represents the decoding decision probability score obtained based on the contrastive decoding mechanism. It is used to comprehensively reflect the predicted probability of each candidate token under the constraint of the positive prompt word instruction and its enhancement result relative to the negative prompt word. This represents the balance coefficient used between the original probability distribution and the probability increment, where... 1; Pick The token corresponding to the highest probability is used as the token for the next prediction in the large model decoding.

[0021] To achieve the second objective of this invention, this invention provides a large model knowledge boundary perception system based on contrastive decoding, used to execute the above-described large model knowledge boundary perception method based on contrastive decoding. The large model knowledge boundary perception system based on contrastive decoding includes: The prompt word construction module is used to construct a pair of conflicting positive and negative prompt words. The positive prompt word is a piece of natural language text; when the prompt word requires the large model to output a response, any uncertain text segments are marked with a special identifier. The negative prompt word is also a piece of natural language text; when the prompt word requires the large model to output a response, any definite text segments are marked with a special identifier. User question text, which is the user's natural language question, is appended to both the positive and negative prompt words, generating complete opposing positive and negative prompt word instructions. These instructions are then input into the large model, which includes an input embedding layer, a multi-layer decoder, and an output layer connected in sequence. Each decoder contains a multi-head attention layer, a normalization layer, a residual connection layer, and a forward propagation layer connected in sequence. The attention score calculation and weighting module is used to calculate the attention scores in the multi-head attention layers of the intermediate decoders after the positive prompt word instruction is input into the large model, and to weight the attention scores of the positions of special identifiers in the positive prompt words. The prompt word decoding module inputs positive and negative prompt word commands into the large model for decoding, and obtains the top k candidate tokens with the highest probabilities respectively. The union of the 2k candidate tokens is used to construct a token candidate pool. The candidate token probability increment calculation module is used to compare the probability distribution differences of each token in the token candidate pool and obtain the probability increment of each candidate token. The token output module is used to input positive prompt words into the large model, obtain the probability score of each candidate token in the output layer of the large model, and add it to the probability increment of the above candidate tokens in turn to obtain the final probability score of each token. The token with the highest probability score is taken as the final output of the large model.

[0022] To achieve the third objective of this invention, this invention provides an electronic device, including a processor and a memory for storing a processor-executable program, wherein when the processor executes the program stored in the memory, it implements the above-described large model knowledge boundary perception method based on contrastive decoding.

[0023] To achieve the fourth objective of this invention, a computer-readable storage medium is provided, storing a program that, when executed by a processor, implements the aforementioned method for perceiving the knowledge boundary of a large model based on contrastive decoding.

[0024] Compared with the prior art, the present invention has the following advantages and beneficial effects: 1. The method of this invention directly achieves the perception of knowledge boundaries and the localization of uncertain segments in large models by combining the construction of prompt words and comparative decoding. It does not require the construction or training of new datasets specifically for the knowledge boundary perception task, nor does it rely on additional fine-tuning processes. Therefore, it is a low-cost and efficient implementation method that requires no retraining. Thus, it can be directly applied to arbitrarily large models and has good versatility and scalability.

[0025] 2. Unlike existing methods Pass This invention presents a method for calculating confidence based on multiple inference results. The method performs a single inference during the inference phase, allowing attention scores from each decoder layer in a large model to be reused, significantly reducing computational overhead and response time costs. The entire process eliminates the need for multiple rounds of repeated decoding, greatly improving inference efficiency.

[0026] 3. This invention does not simply reject uncertain answers in the output. Instead, it explicitly labels uncertain segments in the model's generated results with special identifiers, thereby achieving fine-grained positioning of knowledge boundaries. This approach not only improves the interpretability and controllability of the model's output but also avoids information loss caused by overall rejection, allowing the model to maintain reliability while ensuring integrity and interactive experience. Attached Figure Description

[0027] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0028] Figure 1 This is a flowchart illustrating the large model knowledge boundary perception method based on contrastive decoding in an embodiment of the present invention. Figure 2 This is a logical schematic diagram of the contrast decoding method combined with the attention emphasis mechanism in an embodiment of the present invention; Figure 3 This is a schematic diagram of a large model knowledge boundary perception system based on contrastive decoding proposed in an embodiment of the present invention; Figure 4 This is a structural diagram of an electronic device according to an embodiment of the present invention. Detailed Implementation

[0029] To enable those skilled in the art to better understand the present application, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are merely some embodiments of the present application, and not all embodiments. All other embodiments obtained by those skilled in the art based on the embodiments of the present application without creative effort are within the scope of protection of the present application.

[0030] In this application, the reference to "embodiment" means that a specific feature, structure, or characteristic described in connection with an embodiment may be included in at least one embodiment of this application. The appearance of this phrase in various places throughout the specification does not necessarily refer to the same embodiment, nor is it a mutually exclusive, independent, or alternative embodiment. It will be explicitly and implicitly understood by those skilled in the art that the embodiments described in this application can be combined with other embodiments.

[0031] Example 1 This embodiment discloses a method for perceiving the knowledge boundaries of a large model based on contrastive decoding. By constructing positive and negative cue words of opposing conflicts and combining attention emphasis mechanism with contrastive decoding strategy, it can achieve explicit annotation of uncertain fragments in the model's response without additional training of the large model, thereby enhancing the large model's ability to perceive its own knowledge boundaries.

[0032] In this embodiment, the user poses the following natural language question: Can I donate blood after receiving the COVID-19 vaccine? A pre-trained text generation large model is selected as the large model in this embodiment. This large model is a large-scale pre-trained language model with a decoder structure, and its input and output are both natural language text.

[0033] S1. Construct a pair of semantically conflicting prompt words, which serve as the positive prompt word and the negative prompt word, respectively.

[0034] Among them, positive prompts are used to guide the large model to explicitly annotate text fragments that exceed its own knowledge boundaries or contain uncertainties when generating response content. Specifically, they are: Positive prompts: Please answer the following questions in one or two simple sentences based on your own knowledge. If any part of your reply or answer exceeds your knowledge or is uncertain, please mark that part with the "~" symbol. For example: The capital of Argentina is ~Santiago~.

[0035] Reverse cue words are used to guide large models to explicitly label text segments they are certain of when generating responses. Specifically, they include: Reverse prompts: Please answer the following questions in one or two simple sentences based on your own knowledge. If any part of your reply or answer is within your confidence and you are certain of the information provided, please mark that part with the "~" symbol. For example: The capital of Argentina is ~ Buenos Aires ~.

[0036] The positive and negative prompt words are concatenated with the user's question text to obtain complete positive and negative prompt word instructions, which are then input into the same large model in parallel.

[0037] S2. After inputting positive prompt word instructions into the large model, calculate the attention score in the multi-head attention layer of the intermediate decoder layers.

[0038] In this embodiment, the attention score is calculated as follows:

[0039] Here, Q and K are abbreviations for the Query matrix and Key matrix in the attention mechanism, respectively. This represents the dot product of the transposes of Q and K. , R represents a real number. In this embodiment, the positive prompt command obtained by concatenating the positive prompt word with the user question contains a total of 106 tokens. Therefore, the number of tokens is... ; This represents the dimension of the pre-defined hidden space of the large model; ; The attention weight matrix A is calculated using the function Softmax() on the attention score S, where the attention weight matrix A ∈ The attention weight matrix A is used to characterize the correlation between tokens in the positive input prompts. The calculation method is as follows:

[0040] Among them, the element in the i-th row and j-th column of the attention weight matrix A The calculation method is as follows:

[0041] in, This represents the attention weight of the interaction between the i-th token and the j-th token in the input positive prompt; Indicates the token corresponding to the i-th token The token corresponding to the j-th token The inner product result between them is used to characterize the correlation between the i-th token and the j-th token.

[0042] The attention scores for the locations of special identifiers in the cue words are then weighted, including: The position m of the special identifier in the positive prompt word instruction is marked, where m=67 in this embodiment, while maintaining the first With the row count remaining unchanged, the column index is... Attention score Perform weighted emphasis and set the weighted emphasis coefficient. The weighted attention weights are:

[0043] in, Let represent the attention score of the i-th token to the m-th token; This represents the attention score of the i-th token to the t-th token before normalization using the Softmax function. This indicates that the value of t is not equal to m; This represents the attention score at the special identifier "~" obtained through a weighted mechanism. The first hyperparameter representing the weighted emphasis coefficient, in this embodiment 6.

[0044] S3. Based on the positive prompt word instructions processed by the attention emphasis mechanism, obtain the prediction probability score results of each candidate token by the large model at the current decoding time, and select the k candidate tokens with the highest prediction probability scores as the first candidate pool.

[0045] Meanwhile, the reverse prompt word instruction is input into the large model, and the corresponding prediction probability score is obtained without attention emphasis. The k candidate tokens with the highest prediction probability scores are selected as the second candidate pool.

[0046] The token candidate pool is constructed by taking the union of the first candidate pool and the second candidate pool.

[0047] In this embodiment, the candidate token quantity parameter k=10.

[0048] S4. Based on each candidate token and its probability score in the token candidate pool, the probability increment of each candidate token is calculated by comparison and decoding:

[0049] in, This refers to the probability distribution of each token in the token candidate pool under a positive prompt word. This refers to the probability distribution of each token in the token candidate pool under the reverse prompt word; To avoid excessively small numerators and denominators in the equilibrium parameters, this embodiment... ;Softmax() represents the Softmax function.

[0050] S5, Positive Prompt Word Input Large Model get The probability distribution P of the token candidate pool, combined with the probability increments of each candidate token obtained in step S4, is used to weight and sum the predicted probability scores of the candidate tokens to obtain the decoding decision probability score. The specific formula is as follows:

[0051] in, This represents the decoding decision probability score obtained based on the contrastive decoding mechanism, which is used to comprehensively reflect the predicted probability of each candidate token under the constraint of positive prompt words and its enhancement result relative to negative prompt words; This represents the balance coefficient used as the original probability distribution and probability increment, in this embodiment. 0.6; Pick The token corresponding to the highest probability is used as the token for the next prediction in the large model decoding.

[0052] By progressively comparing and decoding, the model's response text to the user's question is finally obtained.

[0053] Based on the above steps, in this embodiment, the response output by the large model is shown in the following example: "It is generally recommended to wait at least 2 days after vaccination before donating blood, but it is possible to donate blood even if the waiting time is short. So the answer is yes." Among them, “~2 days later, ~” is an uncertain text fragment identified and explicitly labeled by the method of this invention, indicating that the large model can perceive and locate its own knowledge boundaries while generating a response.

[0054] This invention was evaluated on the Natural Questions and TriviaQA datasets, based on the Qwen2.5-7B and Qwen2.5-14B models. The statistical results show the precision, recall, and F1 score of the model in the knowledge boundary representation task: Accuracy (P) represents the proportion of samples that the model classifies as having given a confident answer, but whose answers are correct. It is calculated as follows:

[0055] in, This indicates the number of samples for which the model outputs a definitive answer; This indicates the number of samples in the determined answer sample whose answer content is consistent with the reference answer.

[0056] Recall (R) represents the proportion of samples that the model determines as having a correct answer, out of the total number of questions. It is calculated as follows:

[0057] in, This indicates the total number of problem samples involved in the evaluation.

[0058] The F1 score is used to comprehensively measure the balance between a model's accuracy and coverage in determining the answer selection task. It is calculated as follows:

[0059] As can be seen from the results in Table 1, compared with the baseline metrics that do not employ any method, the method proposed in this invention achieves better results on different datasets and different model bases, verifying the effectiveness and practicality of this invention.

[0060] Table 1. Experimental results of the present invention and baseline on the Natural Questions, TriviaQA dataset.

[0061] Example 2 This embodiment, based on the large model knowledge boundary perception method disclosed in Embodiment 1, analyzes the impact of different parameter configurations on the model knowledge boundary perception effect by changing the values ​​of key hyperparameters, thereby verifying the rationality and effectiveness of the various technical features in the method of this invention. In this embodiment, except for the weighted emphasis coefficient... and balance coefficient Except for the different values, the other steps are the same as steps S1 to S5 in Example 1, and will not be repeated here.

[0062] In this embodiment, the weighting coefficient used to balance the original predicted probability and the probability increment in step S5 is changed. The values ​​of are selected respectively. =0.0, 0.3, 0.6 and 1.0, the Qwen2.5-7B model was evaluated on the TriviaQA dataset, and the F1 score of the model in the knowledge boundary representation task was calculated.

[0063] Table 2. Qwen2.5-7B on the Traiva dataset Table of F1 value changes with different values

[0064] Experimental results show that when When the value is too small, the probability increment introduced by the comparative decoding is insufficient, and the model still mainly relies on the original predicted probability, making it difficult to effectively distinguish uncertain segments; when... When the value is too large, the comparison decoding signal is excessively amplified, which can easily affect the decoding stability.

[0065] when When the F1 score is 0.6, the model can more effectively utilize the difference information brought by the contrast decoding while maintaining stable generation quality, thus achieving the best F1 score.

[0066] In this embodiment, while maintaining With the value remaining constant at 0.6, change the attention emphasis coefficient in step S2. The values ​​of are selected respectively. =0.5, 3, 6 and 9, and the corresponding F1 values ​​were calculated. The experimental results are shown in Table 3.

[0067] Table 3. F1 value variation of Qwen2.5-7B on the Traiva dataset with different β settings.

[0068] Experimental results show that as β increases, the model pays more attention to instructions related to special identifiers, and the recognition effect of uncertain segments improves accordingly; however, when β is too large, it will cause the model to over-focus on the position of the identifier, thus affecting the balance of the overall decoding distribution.

[0069] The comprehensive experimental results show that setting the attention emphasis coefficient β appropriately can achieve a balance between enhancing instruction compliance and maintaining model decoding stability, thereby further improving the model's knowledge boundary perception ability.

[0070] Example 3 like Figure 3 As shown, this embodiment provides a large model knowledge boundary perception system based on contrastive decoding. This system includes the following modules connected in sequence: a cue word construction module 301, an attention score calculation and weighting module 302, a cue word decoding module 303, a candidate token probability increment calculation module 304, and a token output module 305. The specific functions of each module are as follows: The prompt word construction module 301 is used to construct a pair of conflicting positive and negative prompt words. The positive prompt word is a piece of natural language text; when the prompt word requires the large model to output a response, any uncertain text segments are marked with a special identifier. The negative prompt word is also a piece of natural language text; when the prompt word requires the large model to output a response, any definite text segments are marked with a special identifier. User question text, which is the user's natural language question, is concatenated after the positive and negative prompt words to generate complete opposing positive and negative prompt word instructions. These instructions are then input into the large model, which includes an input embedding layer, a multi-layer decoder, and an output layer connected in sequence. Each decoder contains a multi-head attention layer, a normalization layer, a residual connection layer, and a forward propagation layer connected in sequence. The attention score calculation and weighting module 302 is used to calculate the attention scores in the multi-head attention layers of the intermediate decoders after the positive prompt word instruction is input into the large model, and to weight the attention scores of the positions of special identifiers in the positive prompt words. The prompt word decoding module 303 inputs the positive prompt word instruction and the negative prompt word instruction into the large model for decoding, and obtains the top k candidate tokens with the highest probability respectively. The union of the 2k candidate tokens is used to construct the token candidate pool. The candidate token probability increment calculation module 304 is used to compare the probability distribution differences of each token in the token candidate pool and obtain the probability increment of each candidate token. The token output module 305 is used to input positive prompt word instructions into the large model, obtain the probability score of each candidate token in the output layer of the large model, add them to the probability increment of the above candidate tokens in turn to obtain the final probability score of each token, and take the token with the highest probability score as the final output of the large model.

[0071] Example 4 This embodiment provides an electronic device, which can be a computer, such as... Figure 4 As shown, the processor 402, memory, input device 403, display 404, and network interface 405 are connected via system bus 401. The processor provides computing and control capabilities. The memory includes a non-volatile storage medium 406 and internal memory 407. The non-volatile storage medium 406 stores the operating system, computer programs, and database. The internal memory 407 provides an environment for the operation of the operating system and computer programs in the non-volatile storage medium. When the processor 402 executes the computer programs stored in the memory, it implements the large model knowledge boundary perception method based on contrast decoding proposed in Embodiment 1 above, including the following steps: S1. Construct a pair of conflicting positive and negative prompt words, and concatenate the user question text after the positive and negative prompt words respectively. The user question text is the natural language text of the user's question. Generate complete opposing positive and negative prompt word instructions and input them into the large model. S2. Calculate the attention scores in the multi-head attention layers of the intermediate decoders after the positive prompt word instruction is input into the large model, and weight the attention scores of the positions of special identifiers in the positive prompt word; emphasize the meaning of the special identifiers and the related instructions, and enhance the large model's ability to follow the instructions of the positive prompt word; S3. Input the positive prompt word instruction and the negative prompt word instruction into the large model for decoding, and obtain the top k candidate tokens with the highest probability respectively. Take the union of the 2k candidate tokens to construct the token candidate pool. S4. Compare the probability distribution differences of each token in the token candidate pool to obtain the probability increment of each candidate token. S5. Input the positive prompt word command into the large model to obtain the probability score of each candidate token in the output layer of the large model. Add the probability increment of the candidate tokens to obtain the final probability score of each token. Take the token with the highest probability score as the final output of the large model.

[0072] Example 5 This embodiment provides a storage medium, which is a computer-readable storage medium, storing a computer program. When the computer program is executed by a processor, it implements a large model knowledge boundary perception method based on contrastive decoding proposed in Embodiment 1 above, including the following steps: S1. Construct a pair of conflicting positive and negative prompt words, and concatenate the user question text after the positive and negative prompt words respectively. The user question text is the natural language text of the user's question. Generate complete opposing positive and negative prompt word instructions and input them into the large model. S2. Calculate the attention scores in the multi-head attention layers of the intermediate decoders after the positive prompt word instruction is input into the large model, and weight the attention scores of the positions of special identifiers in the positive prompt word; emphasize the meaning of the special identifiers and the related instructions, and enhance the large model's ability to follow the instructions of the positive prompt word; S3. Input the positive prompt word instruction and the negative prompt word instruction into the large model for decoding, and obtain the top k candidate tokens with the highest probability respectively. Take the union of the 2k candidate tokens to construct the token candidate pool. S4. Compare the probability distribution differences of each token in the token candidate pool to obtain the probability increment of each candidate token. S5. Input the positive prompt word command into the large model to obtain the probability score of each candidate token in the output layer of the large model. Add the probability increment of the candidate tokens to obtain the final probability score of each token. Take the token with the highest probability score as the final output of the large model.

[0073] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.

[0074] The above embodiments are preferred embodiments of the present invention, but the embodiments of the present invention are not limited to the above embodiments. Any changes, modifications, substitutions, combinations, or simplifications made without departing from the spirit and principle of the present invention shall be considered equivalent substitutions and shall be included within the protection scope of the present invention.

Claims

1. A knowledge boundary awareness method for large-scale pre-trained language models based on contrastive decoding, applied to large-scale pre-trained language models (hereinafter referred to as "large models"). The input and output of a large model are both natural language text, and the output is the large model's response to the input text. Its characteristic is that... The large model knowledge boundary awareness method includes the following steps: S1. Construct a pair of conflicting positive and negative prompts. The positive prompt is a piece of natural language text. When the prompt requires the large model to output a response, any uncertain text segments are marked with a special identifier. The negative prompt is also a piece of natural language text. When the prompt requires the large model to output a response, any definite text segments are marked with a special identifier. The user's question text is then appended to both the positive and negative prompts. This generates complete opposing positive and negative prompt instructions, which are input into the large model. The large model includes an input embedding layer, a multi-layer decoder, and an output layer connected in sequence. Each decoder contains a multi-head attention layer, a normalization layer, a residual connection layer, and a forward propagation layer connected in sequence. S2. Calculate the attention scores in the multi-head attention layers of the intermediate decoders after the positive prompt word instruction is input into the large model, and weight the attention scores of the positions of special identifiers in the positive prompt word. S3. Input the positive prompt word instruction and the negative prompt word instruction into the large model for decoding, and obtain the top k candidate tokens with the highest probability respectively. Take the union of the 2k candidate tokens to construct the token candidate pool. S4. Compare the probability distribution differences of each token in the token candidate pool to obtain the probability increment of each candidate token. S5. Input the positive prompt word command into the large model to obtain the probability score of each candidate token in the output layer of the large model. Add the probability increment of the candidate tokens to obtain the final probability score of each token. Take the token with the highest probability score as the final output of the large model.

2. The method for perceiving the knowledge boundary of a large model based on contrastive decoding according to claim 1, characterized in that, In step S2, the attention scores S of the intermediate encoders after the positive prompt word instruction is input into the large model are calculated. The formula for calculating the attention score S is: , Here, Q and K are abbreviations for the Query matrix and Key matrix in the attention mechanism, respectively. This represents the dot product of the transposes of Q and K. , R represents a real number, n represents the number of tokens for the input positive prompt word; d represents the dimension of the preset large model hidden space; ; The attention weight matrix A is calculated using the function Softmax() on the attention score S, where the attention weight matrix A ∈ The attention weight matrix A is used to characterize the correlation between tokens in the positive input prompts. The calculation method is as follows: , Among them, the element in the i-th row and j-th column of the attention weight matrix A The calculation method is as follows: , in , This represents the attention weight of the interaction between the i-th token and the j-th token in the input positive prompt; Indicates the token corresponding to the i-th token The token corresponding to the j-th token The inner product result between them is used to characterize the correlation between the i-th token and the j-th token; Mark the m-th token containing the special identifier in the positive prompt word instruction, where The attention score for column index m, while keeping the i-th row unchanged. Perform weighted emphasis and set the weighted emphasis coefficient. The weighted attention weights are: , in, Let represent the attention score of the i-th token to the m-th token; This represents the attention score of the i-th token to the t-th token before normalization using the Softmax function. This indicates that the value of t is not equal to that of m.

3. The large model knowledge boundary awareness method based on contrastive decoding according to claim 1, characterized in that, In step S3, after the positive prompt words and negative prompt words are input into the large model, the probability distribution of the next predicted word is obtained. The k words with the highest probability scores in the probability distribution of the next predicted word obtained under the positive prompt words are selected as the first candidate pool. The k words with the highest probability scores in the probability distribution of the next predicted word obtained under the positive prompt words are selected as the second candidate pool. The union of the two candidate pools of size k is taken to obtain the final token candidate pool.

4. The method for perceiving the knowledge boundary of a large model based on contrastive decoding according to claim 1, characterized in that, In step S4, the probability increment of each candidate token in the token candidate pool is obtained by comparing the probability scores of positive and negative prompt words with the decoding results. : , in, This refers to the probability distribution of each token in the token candidate pool under positive prompt words. This refers to the probability distribution of each token in the token candidate pool under the reverse suggestion word. This is a balance parameter used to avoid the numerator and denominator being too small.

5. The method for perceiving the knowledge boundary of a large model based on contrastive decoding according to claim 1, characterized in that, In step S5, based on the probability distribution P of the token candidate pool obtained from the input positive prompt word instruction to the large model, the probability increment for each positive prompt word is calculated. Perform weighted summation: , in, This represents the decoding decision probability score obtained based on the contrastive decoding mechanism. It is used to comprehensively reflect the predicted probability of each candidate token under the constraint of positive prompt words and its enhancement result relative to negative prompt words. This represents the balance coefficient used between the original probability distribution and the probability increment, where... 1; Pick The token corresponding to the highest probability is used as the token for the next prediction in the large model decoding.

6. A large model knowledge boundary perception system based on contrastive decoding, used to execute the large model knowledge boundary perception method based on contrastive decoding as described in any one of claims 1 to 5, characterized in that, The large model knowledge boundary awareness system based on contrastive decoding includes: The prompt word construction module is used to construct a pair of conflicting positive and negative prompt words. The positive prompt word is a piece of natural language text; when the prompt word requires the large model to output a response, any uncertain text segments are marked with a special identifier. The negative prompt word is also a piece of natural language text; when the prompt word requires the large model to output a response, any definite text segments are marked with a special identifier. User question text, which is the user's natural language question, is appended to both the positive and negative prompt words, generating complete opposing positive and negative prompt word instructions. These instructions are then input into the large model, which includes an input embedding layer, a multi-layer decoder, and an output layer connected in sequence. Each decoder contains a multi-head attention layer, a normalization layer, a residual connection layer, and a forward propagation layer connected in sequence. The attention score calculation and weighting module is used to calculate the attention scores in the multi-head attention layers of the intermediate decoders after the positive prompt word instruction is input into the large model, and to weight the attention scores of the positions of special identifiers in the positive prompt words. The prompt word decoding module inputs positive and negative prompt word commands into the large model for decoding, and obtains the top k candidate tokens with the highest probabilities respectively. The union of the 2k candidate tokens is used to construct a token candidate pool. The candidate token probability increment calculation module is used to compare the probability distribution differences of each token in the token candidate pool and obtain the probability increment of each candidate token. The token output module is used to input positive prompt words into the large model, obtain the probability score of each candidate token in the output layer of the large model, and add it to the probability increment of the above candidate tokens in turn to obtain the final probability score of each token. The token with the highest probability score is taken as the final output of the large model.

7. An electronic device comprising a processor and a memory for storing a processor-executable program, characterized in that, When the processor executes the program stored in the memory, it implements the large model knowledge boundary perception method based on contrast decoding as described in any one of claims 1 to 5.

8. A computer-readable storage medium storing a program, characterized in that, When the program is executed by the processor, it implements the large model knowledge boundary perception method based on contrastive decoding as described in any one of claims 1 to 5.