Large language model structured generation control method and system

By employing a collaborative mechanism of conflict detection and latent space orientation optimization, the problem of semantic quality degradation in large language models under high-compactness constraints is solved, achieving semantic quality recovery and improvement without introducing additional model components.

CN122491467APending Publication Date: 2026-07-31SHENZHEN INST OF ADVANCED TECH CHINESE ACAD OF SCI
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
SHENZHEN INST OF ADVANCED TECH CHINESE ACAD OF SCI
Filing Date
2026-03-26
Publication Date
2026-07-31

AI Technical Summary

Technical Problem

Existing large language models suffer from semantic quality degradation when decoding hard constraints under high-compactness constraints. Existing solutions have failed to effectively solve this problem or restore semantic quality without introducing additional model components.

Method used

By employing a collaborative mechanism of conflict detection and latent space directional optimization, the distribution truncation problem is identified and repaired. A dual-threshold adaptive triggering mechanism is used to accurately determine constraint conflicts, and a learnable perturbation vector is applied to the latent state space for gradient optimization to maintain semantic consistency.

Benefits of technology

While ensuring strict compliance with output format, it significantly restores semantic quality, has controllable computational overhead, and does not rely on external models, achieving semantic quality improvement under high-compact constraints.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure REF-OBJ-1774506885940-000007
    Figure REF-OBJ-1774506885940-000007
  • Figure REF-OBJ-1774506885940-000011
    Figure REF-OBJ-1774506885940-000011
  • Figure REF-OBJ-1774506885940-000014
    Figure REF-OBJ-1774506885940-000014
Patent Text Reader

Abstract

This invention discloses a structured generation control method and system for large language models. The method includes: responding to a structured generation task, parsing structural constraint rules to determine a set of legal tokens; obtaining the original hidden state, evaluating the matching degree between its output probability distribution and the set of legal tokens, and determining whether there is a constraint conflict; when a conflict exists, performing targeted optimization adjustment on the original hidden state in the hidden state space, and generating the current token based on the optimized hidden state under the constraint of the set of legal tokens; when no conflict exists, directly generating the current token based on the original hidden state under the constraint of the set of legal tokens. This invention solves the problem of semantic quality collapse caused by an excessively small allowed token set in hard-constraint decoding, achieving effective recovery of semantic quality under high-compact constraints without modifying model parameters or relying on external models.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the fields of artificial intelligence and natural language processing, and in particular to a method and system for structured generation control of large language models. Background Technology

[0002] In structured output scenarios of Large Language Models (LLMs), the industry commonly employs hard-constraint decoding techniques. This involves using a finite state machine to calculate the current set of valid tokens at each decoding step and setting the logarithmic probability of invalid tokens to negative infinity, thus ensuring 100% grammatical compliance of the output. However, when constraints become tight (i.e., allowing extremely small sets, such as in code generation, domain-specific language generation, or strict regular expression matching), a large amount of probability quality in the model's original probability distribution is truncated by masking, leading to a significant decrease or even collapse in the semantic quality of the generated content. This phenomenon is known as "distribution truncation pathology" or "feasibility quality collapse." While this approach ensures strict compliance with the output format, it cannot effectively address the semantic degradation caused by overly tight constraints.

[0003] To address the semantic quality degradation issue in the aforementioned hard-constraint decoding process, existing solutions have attempted improvements from different angles. Some methods adjust the probability ratio at the sampling level to alleviate distribution distortion, but their operation is limited to adjusting the sampling probability and does not change the internal representation of the model, resulting in limited repair capabilities under high-compact constraints. Other methods intervene in the generation process using the gradient backpropagation of external attribute classifiers, but relying on pre-trained external classifiers makes it difficult to directly construct suitable optimization objectives in structured constraint scenarios. Still other methods construct energy functions for optimization in continuous log probability spaces, but the operation space is a log probability layer, which disrupts the semantic association structure between tokens, resulting in almost zero optimization gain in scenarios with extremely low feasible quality.

[0004] In summary, existing solutions either rely on external models, have inappropriate operating spaces, or lack adaptive triggering mechanisms, and none of them can effectively solve the semantic quality degradation problem caused by hard constraint decoding under high-compact constraints without introducing additional model components. Summary of the Invention

[0005] This invention provides a structured generation control method and system for large language models. Through a collaborative mechanism of conflict detection and latent space orientation optimization, it actively identifies and repairs distribution truncation problems during hard constraint decoding, thereby achieving effective recovery of semantic quality under high-compact constraints.

[0006] This invention provides a method and system for controlling the structured generation of large language models, the method comprising: In response to the structured generation task, the predefined structural constraint rules are parsed to determine the set of legal tokens for the current decoding step. Obtain the original hidden state generated by the large language model based on the current input, evaluate the matching degree between the output probability distribution corresponding to the original hidden state and the set of legal tokens, and determine whether there is a constraint conflict in the current decoding step based on the matching degree. When a constraint conflict is detected, directional optimization control in the hidden state space is triggered to optimize and adjust the original hidden state, and the current token is generated based on the optimized hidden state under the constraints of the set of legal tokens. When it is determined that there is no constraint conflict, the current token is generated based on the original hidden state under the constraints of the set of legal tokens.

[0007] Optionally, the step of determining whether there is a constraint conflict in the current decoding step based on the matching degree includes: Calculate the sum of the probability quality of falling into the set of legal tokens in the original probability distribution, and use it as a feasible quality indicator; The simulation truncates the original probability distribution using a set of valid tokens and calculates the distribution offset before and after truncation as an index of distribution deformation. The feasible quality index is compared with a preset quality threshold, and the distributed deformation index is compared with a preset deformation threshold. If the feasible quality index is lower than the quality threshold and the distribution deformation index is higher than the deformation threshold, then it is determined that there is a constraint conflict in the current decoding step. Otherwise, it is determined that there is no constraint conflict in the current decoding.

[0008] Optionally, the step of performing optimization adjustments on the original hidden layer state includes: Initialize a perturbation vector in the hidden state space; The perturbation vector is superimposed on the original hidden layer state to obtain the candidate hidden layer state; The candidate hidden layer states are input into the output projection layer of the large language model to map and obtain the candidate probability distribution. An objective function is constructed based on the matching between the candidate probability distribution and the set of legitimate tokens, and the gradient of the objective function with respect to the perturbation vector is calculated. The perturbation vector is iteratively updated using the gradient until a preset stopping condition is met. The updated perturbation vector is then superimposed on the original hidden layer state to obtain the optimized hidden layer state.

[0009] Optionally, the step of constructing the objective function based on the matching of the candidate probability distribution with the set of legitimate tokens includes: Identify escape tokens that are not in the set of legal tokens and have the maximum logarithmic probability after perturbation; Obtain the selected target legitimate token, which is the legitimate token with the highest logarithmic probability after perturbation determined from the set of legitimate tokens; The margin loss term is obtained based on the difference between the perturbed logarithmic probability of the target legitimate token and the perturbed logarithmic probability of the escape token, and a preset margin threshold. Calculate the KL divergence between the perturbed probability distribution and the original probability distribution of the model, and use the KL divergence as the semantic preservation loss term; The objective function is generated by weighted summation of the margin loss term and the semantic preservation loss term.

[0010] Optionally, the step of obtaining the selected target legitimate token includes: Remove the token with the highest original logarithmic probability from the set of legal tokens to obtain a subset of candidate tokens; In the subset of candidate tokens, the token with the largest original logarithmic probability value is identified; The token with the highest original logarithmic probability value is identified as the target legitimate token.

[0011] Optionally, the step of obtaining the selected target legitimate token includes: In the original probability distribution, the token that does not belong to the set of legitimate tokens and has the largest logarithmic probability value is identified as the escape token; Iterate through each token in the set of legal tokens, calculate the difference between the original logarithmic probability of each token and the original logarithmic probability of the escape token, and obtain multiple probability differences; Select the target difference with the smallest value from the plurality of probability differences; The valid token corresponding to the target difference is determined as the target valid token.

[0012] Optionally, the process for determining the preset stopping condition includes: After updating the perturbation vector in each iteration, the candidate probability distribution is recalculated; Identify the target token belonging to the set of legitimate tokens and the highest probability competing token belonging to the set of illegitimate tokens from the candidate probability distribution; Calculate the difference between the probability value of the target token and the probability value of the highest probability competing token, and use it as the current probability margin; If the current probability margin is greater than or equal to the preset safety margin threshold, the stopping condition is met, and the iterative update process is terminated.

[0013] Optionally, before the step of generating the current token based on the optimized hidden state under the constraints of the set of legal tokens, the method further includes: The optimized hidden state is input into the output projection layer of the large language model to obtain the optimized probability distribution. Calculate the overall deviation between the optimized probability distribution and the original probability distribution corresponding to the original hidden layer state; The overall deviation is compared with a preset safety tolerance threshold. If the overall deviation is determined to be greater than the safety tolerance threshold, then the original hidden layer state is used to replace the optimized hidden layer state; otherwise, the optimized hidden layer state is used.

[0014] Optionally, the step of parsing predefined structural constraint rules to determine the set of valid tokens for the current decoding step includes: Receive constraint strength level parameters input by the user and identify whether the parameters belong to the normal strength level or the enhanced strength level; If it is identified as an enhanced strength level, the initial set of legal tokens is shrunk according to the structural constraint rules, and tokens marked as edge legal are removed to obtain a shrunk set of legal tokens. Based on the reduced set of legal tokens, the original probability distribution is truncated and simulated, and the probability quality loss value is calculated to characterize the degree of truncation. The truncation degree is compared with a preset trigger threshold. If the truncation degree exceeds the trigger threshold, an optimization start signal is generated to automatically trigger directional optimization control in the hidden layer state space and perform optimization adjustment steps on the original hidden layer state. If it is determined that the degree of truncation does not exceed the trigger threshold, then subsequent processing is performed directly based on the original hidden layer state.

[0015] Furthermore, to achieve the above objectives, embodiments of the present invention also provide a large language model structured generation control system, the system comprising: The constraint compilation unit is used to respond to the structured generation task, parse the predefined structure constraint rules, and determine the set of legal tokens for the current decoding step. The conflict detection unit is used to obtain the original hidden state generated by the large language model based on the current input, evaluate the matching degree between the output probability distribution corresponding to the original hidden state and the set of legal tokens, and determine whether there is a constraint conflict in the current decoding step based on the matching degree. The latent space optimization unit is used to trigger directional optimization control in the latent layer state space when it is determined that there is a constraint conflict, to perform optimization adjustment on the original latent layer state, and to output the optimized latent layer state. The security verification and sampling unit is used to receive the hidden layer state and generate the current token. Specifically, when the conflict detection unit determines that there is no constraint conflict, the security verification and sampling unit directly generates the current token based on the original hidden layer state; when the conflict detection unit determines that there is a constraint conflict, the security verification and sampling unit generates the current token based on the optimized hidden layer state output by the hidden space optimization unit after security verification.

[0016] One or more technical solutions provided in the embodiments of the present invention have at least the following technical effects or advantages: 1. By combining a dual-threshold adaptive triggering mechanism with latent space gradient optimization, this invention can effectively repair semantic quality degradation while ensuring strict compliance with output format. Specifically, the dual-threshold mechanism accurately identifies decoding steps with distribution truncation through feasible quality indicators and distribution deformation indicators, initiating optimization only when a conflict occurs, avoiding unnecessary computational overhead. Meanwhile, latent space optimization applies a learnable perturbation vector to the input space of the language model head, using gradient descent to ensure that the probability distribution mapped from the optimized latent state allocates more quality to high-quality options among legitimate tokens, while KL divergence constraints prevent excessive distribution shift. This technical approach of accurate detection followed by semantic repair ensures efficient inference under relaxed constraints while achieving semantic quality recovery under tight constraints.

[0017] 2. This invention achieves stronger semantic resilience than existing technologies by applying perturbation vectors to the hidden state space for gradient optimization, leveraging the projection characteristics of the language model head weight matrix. Specifically, since each row of the language model head weight matrix corresponds to the embedding direction of a token, tokens with similar semantics have similar directions in the hidden space. Therefore, the perturbation applied to the hidden state space will coordinately affect a set of semantically related tokens, ensuring that the optimized distribution maintains the semantic association structure between tokens. This mechanism allows the perturbation direction to naturally align with the difference vector between the target token embedding direction and the competing token embedding direction, achieving precise semantic navigation rather than the random perturbations that destroy semantic associations in log-probability space operations. This results in significant semantic quality gains under high-compactness constraints.

[0018] 3. By combining a dynamic early stopping mechanism with a safe rollback mechanism, this invention effectively controls computational overhead and ensures system robustness while maintaining optimization performance. Specifically, the dynamic early stopping mechanism checks the probability margin between the target token and the escape token after each iteration. Once the margin meets the threshold, the optimization is terminated early to avoid over-iteration. The safe rollback mechanism calculates the KL divergence between the optimized distribution and the original distribution after optimization. If the divergence exceeds a safety threshold, it automatically rolls back to the original distribution, ensuring that the system is not inferior to the baseline hard-constraint decoding under any circumstances. The synergistic effect of these two mechanisms enables this invention to achieve a significant improvement in semantic quality under high-compact constraints with only about a doubling of computational overhead, while ensuring the stability and reliability of the method. Attached Figure Description

[0019] Figure 1 This is a schematic diagram of the framework of the large language model structured generation control method of the present invention; Figure 2 This is a flowchart illustrating the structured generation control method for large language models of the present invention. Figure 3 This is a schematic diagram of the framework of the large language model structured generation control system of the present invention; Figure 4 This is a schematic diagram of the terminal structure of the hardware operating environment involved in an embodiment of the present invention. Detailed Implementation

[0020] In large language model structured generation scenarios, hard-constraint decoding, due to the extremely small allowable token set, leads to significant truncation of the original probability distribution, resulting in a substantial decrease in semantic quality or even collapse of the feasible quality, a problem known as feasible quality collapse. To address this issue, this invention proposes a control method and system for large language model structured generation. At each step of autoregressive decoding, a finite state machine is used to obtain the set of legal tokens for the current step, and feasible quality indices and distribution deformation indices between the original probability distribution and the set of legal tokens are calculated. A dual-threshold mechanism is used to accurately determine whether constraint conflicts exist. When a conflict is detected, a learnable perturbation vector is initialized in the input space of the language model head, i.e., the hidden state space. Gradient descent is used to optimize the objective function, which is weighted by margin loss and KL divergence constraints. This allows the optimized hidden state, after mapping, to allocate more probability quality to high-quality options among the legal tokens while maintaining semantic consistency with the original distribution. Subsequently, a dynamic early stopping mechanism is used to improve optimization efficiency, and a safe backoff mechanism ensures automatic revert to the original distribution when optimization fails. This invention can effectively repair the semantic quality degradation caused by hard constraint decoding without modifying model parameters or relying on external discriminators or reward models. It significantly improves the semantic quality of generated content while ensuring strict compliance of output format, and has advantages such as adaptive triggering, controllable computational overhead, and orthogonal superposition with existing sampling strategies.

[0021] To better understand the above technical solutions, exemplary embodiments of the present invention will be described in more detail below with reference to the accompanying drawings. Although exemplary embodiments of the present invention are shown in the drawings, it should be understood that the present invention can be implemented in various forms and should not be limited to the embodiments set forth herein. Rather, these embodiments are provided to enable a more thorough understanding of the present invention and to fully convey the scope of the invention to those skilled in the art.

[0022] To better understand the above technical solutions, the following will provide a detailed explanation of the technical solutions in conjunction with the accompanying drawings and specific implementation methods.

[0023] Example 1 This embodiment provides a method for controlling the structured generation of large language models.

[0024] Reference Figure 1 and Figure 2 The large language model structured generation control method in this embodiment includes the following steps: Step S1: In response to the structured generation task, parse the predefined structural constraint rules and determine the set of legal tokens under the current decoding step; In this embodiment, a structured generation task refers to a task that requires the model output to conform to a specific format, such as JSON generation, code generation, or SQL statement generation. Predefined structural constraints can be formal descriptions such as JSON Schema, regular expressions, or context-free grammars. A finite state machine is a data structure that compiles the above constraints into a state transition diagram, used to track the current grammatical state during decoding and to calculate the set of tokens allowed to be generated in the current state. (Legitimate token set) It represents the geometry of all tokens conforming to the syntax rules in the current decoding step, forming the basis for subsequent conflict detection and optimization control. Through precise state tracking of the finite state machine, it ensures that the tokens generated in each step are 100% grammatically compliant, providing accurate constraint boundaries for subsequent distribution optimization.

[0025] As an optional implementation, structured constraint rules are received from user input. For example, the user might provide a JSON schema describing the desired output format, which defines the object's structure, field types, required fields, and other information. Using a finite state machine compiler, this schema is compiled into a finite state machine. At each step of the autoregressive decoding, the current state is located in the finite state machine based on the generated token sequence. All allowed tokens are extracted from the legal transition edges corresponding to this state, forming a set of legal tokens. , where V is the size of the model vocabulary.

[0026] As another optional implementation, the constraint strength level parameter input by the user is received, and it is identified whether the parameter belongs to the normal strength level or the enhanced strength level. If it is identified as the enhanced strength level, the initial set of legal tokens is shrunk according to the structural constraint rules, and tokens marked as edge legal are removed to obtain a reduced set of legal tokens. Based on the reduced set of legal tokens, the original probability distribution is truncated and simulated, and the probability quality loss value is calculated to characterize the degree of truncation. The degree of truncation is compared with a preset trigger threshold. If it is determined that the degree of truncation exceeds the trigger threshold, an optimization start signal is generated to automatically trigger directional optimization control in the hidden layer state space and perform optimization adjustment steps on the original hidden layer state. If it is determined that the degree of truncation does not exceed the trigger threshold, subsequent processing is performed directly based on the original hidden layer state.

[0027] For example, in the operation and extension scenario, the system can receive user-inputted constraint strength level parameters to proactively increase constraint strength. When the user selects an enhanced strength level, the initial set of legal tokens is shrunk according to structural constraint rules, removing tokens marked as marginally legal. For instance, in code generation, although both `for` and `while` are legal loop keywords, the enhanced strength level may require only `while` to be used. Based on the shrunk set of legal tokens, the system performs a truncation simulation of the original probability distribution and calculates the probability quality loss value to characterize the degree of truncation. If the degree of truncation exceeds a preset trigger threshold, an optimization start signal is generated, automatically triggering subsequent hidden state space oriented optimization control; if the degree of truncation does not exceed the trigger threshold, subsequent processing is performed directly based on the original hidden state. In this way, stronger constraints beyond the traditional sweet spot can be proactively used, and the resulting quality loss can be repaired through subsequent optimization.

[0028] Step S2: Obtain the original hidden state generated by the large language model based on the current input, evaluate the matching degree between the output probability distribution corresponding to the original hidden state and the set of legal tokens, and determine whether there is a constraint conflict in the current decoding step based on the matching degree. In this embodiment, the matching degree is determined based on the probability quality of the set of legitimate tokens in the probability distribution. The hidden state is a feature vector output by the last Transformer layer of the large language model, containing the model's understanding of the current context. The hidden state can be mapped to a logarithmic probability distribution on this table through the language model head. In the current decoding step, the original hidden state is obtained. , of which Model hidden layer dimension. Input the language model header and calculate the original log probability vector: in This is the weight matrix of the language model head. This is the bias term. The original probability distribution is also calculated simultaneously. .

[0029] To determine whether there are constraint conflicts in the current decoding step, the system calculates two key indicators: the feasibility quality indicator and the distribution deformation indicator. Specifically, this includes the following steps: Step S201: Calculate the sum of the probability quality of falling into the set of legal tokens in the original probability distribution, as a feasible quality index; Specifically, the feasible quality metric is the sum of the probability quality of tokens falling into the set of legitimate tokens in the original probability distribution: This metric reflects the model's tendency to naturally output valid tokens. When the value is close to 1, most of the model's probabilistic quality falls within the valid region, and the constraints have minimal impact on the distribution. When the value approaches 0, almost all of the model's probabilistic quality falls within the invalid region. In this case, if hard masking is performed and renormalization is performed, the system will be forced to sample from the tail tokens with extremely low probability, leading to a sharp degradation in semantic quality, i.e., a feasible quality collapse.

[0030] Step S202: Simulate truncating the original probability distribution with a set of valid tokens, and calculate the distribution offset before and after truncation as a distribution deformation index; Specifically, the distribution distortion index measures the overall shift in the probability distribution caused by the hard mask. The system simulation truncates the original probability distribution using a set of valid tokens; that is, it preserves the probability mass of valid tokens, sets the probability mass of invalid tokens to zero, and then re-normalizes the distribution to obtain the truncated distribution. The distribution shift before and after truncation is then calculated. Various distribution distance metrics can be used, such as Friesian distance, Wasserstein distance, Jensen-Shannon divergence, or total variation distance.

[0031] Step S203: Compare the feasible quality index with a preset quality threshold, and compare the distributed deformation index with a preset deformation threshold; if the feasible quality index is lower than the quality threshold and the distributed deformation index is higher than the deformation threshold, then it is determined that there is a constraint conflict in the current decoding step; otherwise, it is determined that there is no constraint conflict in the current decoding.

[0032] Specifically, the feasible quality metric is compared with a preset quality threshold, and the distribution deformation metric is compared with a preset deformation threshold. A constraint conflict is determined to exist in the current decoding step only when the feasible quality metric is lower than the quality threshold and the distribution deformation metric is higher than the deformation threshold; otherwise, no constraint conflict is determined. In a typical configuration, the quality threshold is set to 0.1 and the deformation threshold is set to 0.3. This dual-threshold joint judgment mechanism ensures precise intervention only in decoding steps where there is a truly significant distribution truncation. Experiments show that under the high-tightness constraints of the code generation task, an average of only about 1.2 optimizations are triggered per sample (approximately 100 token outputs), with a trigger rate of about 1%, and zero additional overhead for the remaining steps.

[0033] Step S3: When it is determined that there is a constraint conflict, the directed optimization control in the hidden state space is triggered to perform optimization adjustment on the original hidden state, and the current token is generated based on the optimized hidden state under the constraints of the set of legal tokens. In this embodiment, when a constraint conflict is detected, directional optimization control in the hidden state space is initiated. Unlike related techniques that operate in the logarithmic probability space or key-value space, this invention applies a learnable perturbation vector to the original hidden state in the hidden state space. This perturbation is optimized through gradient descent, so that the optimized hidden state, after being mapped by the language model head, can allocate more probability quality to high-quality options among the legitimate tokens, while maintaining semantic consistency with the original distribution. The hidden space operation utilizes the projection characteristics of the language model head weight matrix to bring semantically similar tokens closer together in the hidden space. The perturbation coordinately affects a group of semantically related tokens, achieving more accurate semantic navigation.

[0034] Based on the above control approach, the specific execution process of targeted optimization control includes the following steps: Step S301: Initialize a perturbation vector in the hidden state space; A perturbation vector is initialized in the hidden state space; in this embodiment, it is initialized as a zero vector. And set a learning rate scheduling scheme, such as gradually decreasing from a larger learning rate to a smaller learning rate.

[0035] Step S302: Superimpose the perturbation vector onto the original hidden layer state to obtain the candidate hidden layer state; Step S303: Input the candidate hidden layer states into the output projection layer of the large language model to map and obtain the candidate probability distribution; The perturbation vector is superimposed onto the original hidden state to obtain the candidate hidden state. The candidate hidden state is then input into the language model head to obtain the candidate probability distribution.

[0036] Step S304: Construct an objective function based on the matching of the candidate probability distribution and the set of legal tokens, and calculate the gradient of the objective function with respect to the perturbation vector; A suitable objective function needs to be constructed to guide the optimization direction. The process of constructing this objective function is as follows: First, identify escape tokens that are not in the set of valid tokens and have the maximum logarithmic probability after perturbation: Then obtain the target valid token. This embodiment provides multiple target token selection strategies to adapt to different application scenarios.

[0037] In one strategy, the token with the highest original logarithmic probability is removed from the set of legal tokens. The token that is not in the set of legal tokens but has the highest logarithmic probability after perturbation is identified in the subset of candidate tokens, and that token is determined as the target legal token.

[0038] In another strategy, the token that does not belong to the set of legitimate tokens and has the largest log probability value is identified as the escape token in the original probability distribution. Then, each token in the set of legitimate tokens is traversed, and the difference between the original log probability of each token and the original log probability of the escape token is calculated. The target difference with the smallest value is selected from multiple probability differences, and the legitimate token corresponding to the target difference is determined as the target legitimate token.

[0039] Furthermore, a teacher-guided strategy can be employed, utilizing the probability distribution of a stronger model at the same location to assist in selecting the target token. The token with the highest probability in the allowed set according to the teacher model is chosen as the target. It should be noted that the teacher model is only used to select the target token and does not introduce online fusion or bi-model inference.

[0040] Based on the selected target valid token and escape token, the system constructs the margin loss term: in This is a preset margin threshold. The meaning of this loss is that when the logarithmic probability of the target token exceeds the amount of escape tokens... When a positive loss is generated, the optimization continues to increase the probabilistic margin between the target token and the escape token.

[0041] Meanwhile, to maintain semantic consistency of the generated content, the system introduces a KL divergence constraint term to calculate the KL divergence between the perturbed probability distribution and the original probability distribution of the model: This constraint is used to prevent the optimized distribution from deviating excessively from the original distribution and to avoid semantic drift.

[0042] The margin loss term and the semantic preservation loss term are weighted and summed to generate the objective function: in These are the weighting coefficients for the KL divergence constraint, used to balance the two optimization objectives of margin expansion and semantic preservation. In a typical configuration, The value ranges from 0.01 to 0.1.

[0043] After the objective function is constructed, its gradient with respect to the perturbation vector is calculated.

[0044] Step S305: Iteratively update the perturbation vector using the gradient until a preset stopping condition is met. Then, superimpose the updated perturbation vector with the original hidden layer state to obtain the optimized hidden layer state.

[0045] The gradient is used to iteratively update the perturbation vector: in The learning rate at step k is decreased according to the schedule. This is the gradient clipping threshold.

[0046] During the iterative update process, it is necessary to determine whether the stopping condition is met. After each update, the candidate probability distribution is recalculated to identify the target token belonging to the set of legal tokens and the highest probability competing token belonging to the set of illegal tokens, i.e., the escape token. The current probability margin between the target token and the escape token is calculated. If the current probability margin is greater than or equal to a preset safe margin threshold, it is determined that the margin has been met, and the iterative update process is terminated early. Experimental statistics show that, under typical code generation configurations, 99% of triggered optimizations can stop early within 1-2 steps, significantly reducing computational overhead.

[0047] After the iteration is completed, the updated perturbation vector is superimposed on the original hidden state to obtain the optimized hidden state.

[0048] Optionally, before generating the current token based on the optimized hidden layer state, a security verification step is performed: the optimized hidden layer state is input to the output projection layer of the large language model to map an optimized probability distribution; the overall deviation between the optimized probability distribution and the original probability distribution corresponding to the original hidden layer state is calculated; the overall deviation is compared with a preset security tolerance threshold; if the overall deviation is determined to be greater than the security tolerance threshold, the original hidden layer state is used to replace the optimized hidden layer state; otherwise, the optimized hidden layer state is used.

[0049] Specifically, the optimized hidden state is input into the language model head, mapped to obtain the optimized probability distribution, and the overall deviation between it and the original probability distribution is calculated. The overall deviation is compared with a preset safety tolerance threshold. If the overall deviation is greater than the safety tolerance threshold, it indicates that the optimization has caused excessive distribution shift, which may affect semantic quality. In this case, the system reverts to using the original hidden layer state instead of the optimized hidden layer state; otherwise, the optimized hidden layer state is used. This safety revert mechanism ensures that the system is not inferior to the baseline hard-constraint decoding under any circumstances.

[0050] Finally, from the log probabilities corresponding to the finally determined hidden states, those that do not belong to the set of valid tokens will be... The position is set to negative infinity, and after renormalization, the output token is selected according to the sampling strategy, such as temperature sampling or nuclear sampling.

[0051] Step S4: When it is determined that there is no constraint conflict, the current token is generated based on the original hidden state under the constraints of the set of legal tokens.

[0052] In this embodiment, when the feasible quality index is not lower than the quality threshold, or the distribution deformation index is not higher than the deformation threshold, it indicates that the original distribution of the model has a good match with the constraint requirements, and no additional optimization intervention is needed. At this time, the system directly performs standard hard constraint decoding: the original hidden state is... Input the language model header to obtain the raw log probability vector. This will not belong to the set of legitimate tokens. The position is set to negative infinity, the remaining positions are renormalized, and then the output token is selected according to the sampling strategy.

[0053] In this embodiment, steps S1 to S4 are repeatedly executed during the autoregressive decoding process until an end marker is generated or the maximum generation length is reached, thereby obtaining a complete structured output result.

[0054] To verify the effectiveness of the technical solution of this invention, extensive experimental tests were conducted. Under the high-pressure constraints of the MBPP code generation task, experiments with five random seeds showed that this embodiment improved the code pass rate from 10.9% to 19.9%, a relative improvement of 82.6%. All five seeds showed positive gains, and the mean KL divergence was only 0.002, with extremely small distribution shift. In a fair comparison experiment, this embodiment achieved a gain of +5.5 percentage points in the code generation task (McNemar test p=0.054), while the COLD method had a gain of +0.0 percentage points in the log probability space and PPLM had a gain of +1.5 percentage points in the key-value space. This embodiment was the only method to achieve statistically significant gains. The perturbation direction in the latent space almost perfectly aligns with the difference vector between the target token embedding direction and the competing token embedding direction (cosine similarity ≈ 1.0), achieving accurate semantic navigation rather than random perturbation.

[0055] The dual-threshold adaptive triggering mechanism reduces the optimized trigger rate to approximately 1%. Combined with dynamic early stopping, the average computational cost is approximately twice that of the baseline hard constraint decoding. Under equivalent computational cost, the single-path pass rate of this embodiment (0.199) is higher than the baseline Best-of-2 (0.1835), and this embodiment is orthogonal to the Best-of-N strategy and can be superimposed, achieving a pass rate of 0.287.

[0056] The operational domain expansion strategy enables higher semantic quality than the sweet spot baseline even when the constraint strength exceeds that of the sweet spot. Experiments show that the pass rate of the sweet spot (L1.5) baseline is 17.2%. After actively raising the constraint level to L2.0 and applying this embodiment, the pass rate increases to 22.7%, a 32% improvement compared to the sweet spot baseline. At the same time, it obtains stronger structured guarantees than the sweet spot baseline, breaking the perception that "the stronger the constraint, the higher the quality."

[0057] In cross-backend consistency verification, independent verification was performed on two mainstream hard-constraint decoding backends, such as Outlines and XGrammar. The latent space perturbation direction in this embodiment is almost completely aligned with the embedding difference vector of the target token and the competing token, with a cosine similarity of ≈1.0. Moreover, the hidden layer state does move towards the embedding direction of the target token, with a cosine change of +0.006, which verifies the backend independence of the mechanism.

[0058] The above embodiments are preferred embodiments of the present invention and are not intended to limit the present invention. Various modifications and variations can be made to the present invention by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the scope of protection of the present invention.

[0059] Based on the same inventive concept, the present invention also provides a system corresponding to the method in Embodiment 1, as shown in Embodiment 2.

[0060] Example 2 Reference Figure 3 A structured generation control system for large language models, the system comprising: The constraint compilation unit is used to respond to the structured generation task, parse the predefined structure constraint rules, and determine the set of legal tokens for the current decoding step. Specifically, this unit receives a structured constraint description input by the user and compiles the constraint rules into a finite state machine using a finite state machine compiler. At each step of the autoregressive decoding, the current state is located in the finite state machine based on the generated token sequence. All allowed tokens are extracted from the legal transition edges corresponding to this state, forming a set of legal tokens. Where V is the model vocabulary size. This unit also supports operational domain expansion, capable of receiving user-input constraint strength level parameters and identifying whether the parameter belongs to the normal strength level or the enhanced strength level. If identified as the enhanced strength level, the initial set of legal tokens is shrunk according to the structural constraint rules, removing tokens marked as marginally legal, resulting in a shrunk set of legal tokens. Based on the shrunk set of legal tokens, the original probability distribution is truncated, and the probability quality loss value is calculated to characterize the degree of truncation. If the degree of truncation exceeds a preset trigger threshold, an optimization start signal is generated.

[0061] Optionally, the system further includes a model inference unit for performing forward propagation computation of a large language model and outputting hidden state vectors and log probabilities.

[0062] Specifically, this unit receives the input prompt word and the currently generated token sequence, processes them through all Transformer layers of the large language model, and extracts the hidden state vector from the output of the last layer. ,in This represents the hidden layer dimension of the model. This unit also contains the language model head, which maps the hidden layer states to log probability vectors over the vocabulary. ,in This is the weight matrix of the language model head. This serves as a bias term, and the original probability distribution can be further calculated. .

[0063] The conflict detection unit is used to obtain the original hidden state generated by the large language model based on the current input, evaluate the matching degree between the output probability distribution corresponding to the original hidden state and the set of legal tokens, and determine whether there is a constraint conflict in the current decoding step based on the matching degree. Specifically, this unit calculates two key indicators: feasibility quality indicators. The feasible quality index is the sum of probabilities falling within the set of valid tokens in the original probability distribution; the distribution deformation index is the distribution offset calculated before and after truncating the original probability distribution through the set of valid tokens. Various distribution distance metrics can be used, such as Friesian distance, Wasserstein distance, Jensen-Shannon divergence, or total variation distance. This unit compares the feasible quality index with a preset quality threshold and the distribution deformation index with a preset deformation threshold. If the feasible quality index is lower than the quality threshold and the distribution deformation index is higher than the deformation threshold, a constraint conflict is determined to exist in the current decoding step; otherwise, no constraint conflict is determined to exist.

[0064] The latent space optimization unit is used to trigger directional optimization control in the latent layer state space when it is determined that there is a constraint conflict, to perform optimization adjustment on the original latent layer state, and to output the optimized latent layer state. Specifically, the unit first initializes a perturbation vector in the hidden state space; in this embodiment, it is initialized as a zero vector. The learning rate scheduling scheme is set. The perturbation vector is then superimposed onto the original hidden state to obtain candidate hidden states, which are then input into the language model head to obtain candidate probability distributions. This unit constructs an objective function based on the matching between the candidate probability distribution and the set of legal tokens. This includes identifying escape tokens, obtaining target legal tokens, constructing margin loss terms and KL divergence constraint terms, and weighted summing the two to generate the objective function. This unit calculates the gradient of the objective function relative to the perturbation vector and uses this gradient to iteratively update the perturbation vector until a preset stopping condition is met. Finally, the updated perturbation vector is superimposed onto the original hidden state to obtain the optimized hidden state. During the iterative update process, this unit also employs a dynamic early stopping mechanism. After each update, the candidate probability distribution is recalculated. From the candidate probability distribution, the target token belonging to the set of legal tokens and the highest probability competing token belonging to the set of illegal tokens are identified. The difference between the probability value of the target token and the probability value of the highest probability competing token is calculated as the current probability margin. If the current probability margin is greater than or equal to a preset safe margin threshold, the stopping condition is met, and the iterative update process is terminated.

[0065] The security verification and sampling unit is used to receive the hidden layer state and generate the current token. Specifically, when the conflict detection unit determines that there is no constraint conflict, it directly generates the current token based on the original hidden state; when the conflict detection unit determines that there is a constraint conflict, it generates the current token based on the optimized hidden state output by the latent space optimization unit, after security verification. The security verification process is as follows: the hidden state to be verified is input into the language model head, the optimized probability distribution is obtained by mapping, and the overall deviation between the optimized probability distribution and the original probability distribution corresponding to the original hidden state is calculated. The overall deviation is compared with a preset security tolerance threshold. If the overall deviation is greater than the security tolerance threshold, the original hidden state is used instead of the optimized hidden state; otherwise, the optimized hidden state is used. Then, from the logarithmic probabilities corresponding to the finally determined hidden states, tokens that do not belong to the set of valid tokens are excluded. The position is set to negative infinity, and after renormalization, the output token is selected according to the sampling strategy, such as temperature sampling or nuclear sampling.

[0066] Optionally, the system further includes a token sampling unit, used to perform hard masking on the optimized logarithmic probabilities, setting the logarithmic probabilities of tokens not in the allowed set to negative infinity, and then re-normalizing and sampling the output tokens. This unit works in conjunction with the security verification and sampling unit; after the security verification and sampling unit completes the hidden state selection, the token sampling unit performs the final mask sampling operation.

[0067] Optionally, the system may further include an operation domain extension unit, which is an optional unit used to automatically increase the constraint level and coordinate system optimization parameters according to the task configuration. This unit receives constraint strength level parameters input by the user. When an enhanced strength level is identified, it instructs the constraint compilation unit to perform a shrinkage of the valid token set, and automatically triggers the latent space optimization unit to perform optimization adjustments based on the comparison result between the truncation degree and the trigger threshold. This unit enables the system to proactively use stronger constraints beyond the traditional sweet spot and repair quality loss through latent space optimization, achieving a dual improvement in both stronger structural guarantees and higher semantic quality.

[0068] The aforementioned units can be deployed on computing devices equipped with graphics processors and work collaboratively in a pipelined manner during large language model inference. The specific implementation details, parameter configurations, and optimization processes of each unit have been described in detail in Embodiment 1 and will not be repeated here.

[0069] Through the above system, the present invention can effectively repair the semantic quality degradation caused by hard constraint decoding while ensuring strict compliance of the output format, and obtain semantic quality performance that surpasses the sweet spot under stronger constraints.

[0070] Since the system described in Embodiment 2 of this invention is a system used to implement the method of Embodiment 1 of this invention, those skilled in the art can understand the specific structure and variations of this system based on the method described in Embodiment 1 of this invention, and therefore will not be repeated here. All systems used in the method of Embodiment 1 of this invention fall within the scope of protection of this invention.

[0071] Example 3 In this embodiment of the invention, a large language model structured generation control device is proposed.

[0072] Reference Figure 4 , Figure 4 This is a schematic diagram of the terminal structure of the hardware operating environment involved in an embodiment of the present invention.

[0073] like Figure 4 As shown, the control terminal may include: a processor 1001, such as a CPU, a network interface 1003, a memory 1004, and a communication bus 1002. The communication bus 1002 is used to enable communication between these components. The network interface 1003 may optionally include a standard wired interface or a wireless interface (such as a Wi-Fi interface). The memory 1004 may be high-speed RAM or stable non-volatile memory, such as a disk drive. Alternatively, the memory 1004 may be a storage device independent of the aforementioned processor 1001.

[0074] Those skilled in the art will understand that Figure 4 The terminal structure shown does not constitute a limitation on the terminal and may include more or fewer components than shown, or combine certain components, or have different component arrangements.

[0075] like Figure 4 As shown, the memory 1004, which serves as a computer storage medium, may include an operating system, a network communication module, and a large language model structure generation control program.

[0076] exist Figure 4 In the hardware structure of the large language model structure generation control device shown, the processor 1001 can call the large language model structure generation control program stored in the memory 1004 and perform the following operations: In response to the structured generation task, the predefined structural constraint rules are parsed to determine the set of legal tokens for the current decoding step. Obtain the original hidden state generated by the large language model based on the current input, evaluate the matching degree between the output probability distribution corresponding to the original hidden state and the set of legal tokens, and determine whether there is a constraint conflict in the current decoding step based on the matching degree. When a constraint conflict is detected, directional optimization control in the hidden state space is triggered to optimize and adjust the original hidden state, and the current token is generated based on the optimized hidden state under the constraints of the set of legal tokens. When it is determined that there is no constraint conflict, the current token is generated based on the original hidden state under the constraints of the set of legal tokens.

[0077] Optionally, the processor 1001 may call the large language model structure generation control program stored in the memory 1004, and further perform the following operations: Calculate the sum of the probability quality of falling into the set of legal tokens in the original probability distribution, and use it as a feasible quality indicator; The simulation truncates the original probability distribution using a set of valid tokens and calculates the distribution offset before and after truncation as an index of distribution deformation. The feasible quality index is compared with a preset quality threshold, and the distributed deformation index is compared with a preset deformation threshold. If the feasible quality index is lower than the quality threshold and the distribution deformation index is higher than the deformation threshold, then it is determined that there is a constraint conflict in the current decoding step. Otherwise, it is determined that there is no constraint conflict in the current decoding.

[0078] Optionally, the processor 1001 may call the large language model structure generation control program stored in the memory 1004, and further perform the following operations: Initialize a perturbation vector in the hidden state space; The perturbation vector is superimposed on the original hidden layer state to obtain the candidate hidden layer state; The candidate hidden layer states are input into the output projection layer of the large language model to map and obtain the candidate probability distribution. An objective function is constructed based on the matching between the candidate probability distribution and the set of legitimate tokens, and the gradient of the objective function with respect to the perturbation vector is calculated. The perturbation vector is iteratively updated using the gradient until a preset stopping condition is met. The updated perturbation vector is then superimposed on the original hidden layer state to obtain the optimized hidden layer state.

[0079] Optionally, the processor 1001 may call the large language model structure generation control program stored in the memory 1004, and further perform the following operations: Identify escape tokens that are not in the set of legal tokens and have the maximum logarithmic probability after perturbation; Obtain the selected target legitimate token, which is the legitimate token with the highest logarithmic probability after perturbation determined from the set of legitimate tokens; The margin loss term is obtained based on the difference between the perturbed logarithmic probability of the target legitimate token and the perturbed logarithmic probability of the escape token, and a preset margin threshold. Calculate the KL divergence between the perturbed probability distribution and the original probability distribution of the model, and use the KL divergence as the semantic preservation loss term; The objective function is generated by weighted summation of the margin loss term and the semantic preservation loss term.

[0080] Optionally, the processor 1001 may call the large language model structure generation control program stored in the memory 1004, and further perform the following operations: Remove the token with the highest original logarithmic probability from the set of legal tokens to obtain a subset of candidate tokens; In the subset of candidate tokens, the token with the largest original logarithmic probability value is identified; The token with the highest original logarithmic probability value is identified as the target legitimate token.

[0081] Optionally, the processor 1001 may call the large language model structure generation control program stored in the memory 1004, and further perform the following operations: In the original probability distribution, the token that does not belong to the set of legitimate tokens and has the largest logarithmic probability value is identified as the escape token; Iterate through each token in the set of legal tokens, calculate the difference between the original logarithmic probability of each token and the original logarithmic probability of the escape token, and obtain multiple probability differences; Select the target difference with the smallest value from the plurality of probability differences; The valid token corresponding to the target difference is determined as the target valid token.

[0082] Optionally, the processor 1001 may call the large language model structure generation control program stored in the memory 1004, and further perform the following operations: After updating the perturbation vector in each iteration, the candidate probability distribution is recalculated; Identify the target token belonging to the set of legitimate tokens and the highest probability competing token belonging to the set of illegitimate tokens from the candidate probability distribution; Calculate the difference between the probability value of the target token and the probability value of the highest probability competing token, and use it as the current probability margin; If the current probability margin is greater than or equal to the preset safety margin threshold, the stopping condition is met, and the iterative update process is terminated.

[0083] Optionally, the processor 1001 may call the large language model structure generation control program stored in the memory 1004, and further perform the following operations: The optimized hidden state is input into the output projection layer of the large language model to obtain the optimized probability distribution. Calculate the overall deviation between the optimized probability distribution and the original probability distribution corresponding to the original hidden layer state; The overall deviation is compared with a preset safety tolerance threshold. If the overall deviation is determined to be greater than the safety tolerance threshold, then the original hidden layer state is used to replace the optimized hidden layer state; otherwise, the optimized hidden layer state is used.

[0084] Optionally, the processor 1001 may call the large language model structure generation control program stored in the memory 1004, and further perform the following operations: Receive constraint strength level parameters input by the user and identify whether the parameters belong to the normal strength level or the enhanced strength level; If it is identified as an enhanced strength level, the initial set of legal tokens is shrunk according to the structural constraint rules, and tokens marked as edge legal are removed to obtain a shrunk set of legal tokens. Based on the reduced set of legal tokens, the original probability distribution is truncated and simulated, and the probability quality loss value is calculated to characterize the degree of truncation. The truncation degree is compared with a preset trigger threshold. If the truncation degree exceeds the trigger threshold, an optimization start signal is generated to automatically trigger directional optimization control in the hidden layer state space and perform optimization adjustment steps on the original hidden layer state. If it is determined that the degree of truncation does not exceed the trigger threshold, then subsequent processing is performed directly based on the original hidden layer state.

[0085] Furthermore, to achieve the above objectives, embodiments of the present invention also provide a large language model structured generation control system, comprising: The constraint compilation unit is used to respond to the structured generation task, parse the predefined structure constraint rules, and determine the set of legal tokens for the current decoding step. The conflict detection unit is used to obtain the original hidden state generated by the large language model based on the current input, evaluate the matching degree between the output probability distribution corresponding to the original hidden state and the set of legal tokens, and determine whether there is a constraint conflict in the current decoding step based on the matching degree. The latent space optimization unit is used to trigger directional optimization control in the latent layer state space when it is determined that there is a constraint conflict, to perform optimization adjustment on the original latent layer state, and to output the optimized latent layer state. The security verification and sampling unit is used to receive the hidden layer state and generate the current token. Specifically, when the conflict detection unit determines that there is no constraint conflict, the security verification and sampling unit directly generates the current token based on the original hidden layer state; when the conflict detection unit determines that there is a constraint conflict, the security verification and sampling unit generates the current token based on the optimized hidden layer state output by the hidden space optimization unit after security verification.

[0086] Those skilled in the art will understand that embodiments of the present invention can be provided as methods, systems, or computer program products. Therefore, the present invention can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, the present invention can take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0087] This invention is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the invention. It will be understood that each block of the flowchart illustrations and / or block diagrams, as well as combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart. Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.

[0088] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.

[0089] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.

[0090] It should be noted that any reference signs placed between parentheses in the claims should not be construed as limiting the claims. The word "comprising" does not exclude the presence of components or steps not listed in the claims. The word "a" or "an" preceding a component does not exclude the presence of a plurality of such components. The invention can be implemented by means of hardware comprising several different components and by means of a suitably programmed computer. In a unit claim enumerating several means, several of these means may be embodied by the same item of hardware. The use of the words first, second, third, etc., does not indicate any order. These words can be interpreted as names.

[0091] Although preferred embodiments of the invention have been described, those skilled in the art, upon learning the basic inventive concept, can make other changes and modifications to these embodiments. Therefore, the appended claims are intended to be interpreted as including both the preferred embodiments and all changes and modifications falling within the scope of the invention.

[0092] Obviously, those skilled in the art can make various modifications and variations to this invention without departing from its spirit and scope. Therefore, if these modifications and variations fall within the scope of the claims of this invention and their equivalents, this invention also intends to include these modifications and variations.

Claims

1. A method for controlling the structured generation of large language models, characterized in that, The method includes: In response to the structured generation task, the predefined structural constraint rules are parsed to determine the set of legal tokens for the current decoding step. Obtain the original hidden state generated by the large language model based on the current input, evaluate the matching degree between the output probability distribution corresponding to the original hidden state and the set of legal tokens, and determine whether there is a constraint conflict in the current decoding step based on the matching degree. When a constraint conflict is detected, directional optimization control in the hidden state space is triggered to optimize and adjust the original hidden state, and the current token is generated based on the optimized hidden state under the constraints of the set of legal tokens. When it is determined that there is no constraint conflict, the current token is generated based on the original hidden state under the constraints of the set of legal tokens.

2. The method as described in claim 1, characterized in that, The step of determining whether there is a constraint conflict in the current decoding step based on the matching degree includes: Calculate the sum of the probability quality of falling into the set of legal tokens in the original probability distribution, and use it as a feasible quality indicator; The simulation truncates the original probability distribution using a set of valid tokens and calculates the distribution offset before and after truncation as an index of distribution deformation. The feasible quality index is compared with a preset quality threshold, and the distributed deformation index is compared with a preset deformation threshold. If the feasible quality index is lower than the quality threshold and the distribution deformation index is higher than the deformation threshold, then it is determined that there is a constraint conflict in the current decoding step. Otherwise, it is determined that there is no constraint conflict in the current decoding.

3. The method as described in claim 2, characterized in that, The step of performing optimization adjustments on the original hidden layer state includes: Initialize a perturbation vector in the hidden state space; The perturbation vector is superimposed on the original hidden layer state to obtain the candidate hidden layer state; The candidate hidden layer states are input into the output projection layer of the large language model to map and obtain the candidate probability distribution. An objective function is constructed based on the matching between the candidate probability distribution and the set of legitimate tokens, and the gradient of the objective function with respect to the perturbation vector is calculated. The perturbation vector is iteratively updated using the gradient until a preset stopping condition is met. The updated perturbation vector is then superimposed on the original hidden layer state to obtain the optimized hidden layer state.

4. The method as described in claim 3, characterized in that, The step of constructing the objective function based on the matching results between the candidate probability distribution and the legitimate token set includes: Identify escape tokens that are not in the set of legal tokens and have the maximum logarithmic probability after perturbation; Obtain the selected target legitimate token, which is the legitimate token with the highest logarithmic probability after perturbation determined from the set of legitimate tokens; The margin loss term is obtained based on the difference between the perturbed logarithmic probability of the target legitimate token and the perturbed logarithmic probability of the escape token, and a preset margin threshold. Calculate the KL divergence between the perturbed probability distribution and the original probability distribution of the model, and use the KL divergence as the semantic preservation loss term; The objective function is generated by weighted summation of the margin loss term and the semantic preservation loss term.

5. The method as described in claim 4, characterized in that, The steps for obtaining the selected target legitimate token include: Remove the token with the highest original logarithmic probability from the set of legal tokens to obtain a subset of candidate tokens; In the subset of candidate tokens, the token with the largest original logarithmic probability value is identified; The token with the highest original logarithmic probability value is identified as the target legitimate token.

6. The method as described in claim 4, characterized in that, The steps for obtaining the selected target legitimate token include: In the original probability distribution, the token that does not belong to the set of legitimate tokens and has the largest logarithmic probability value is identified as the escape token; Iterate through each token in the set of legal tokens, calculate the difference between the original logarithmic probability of each token and the original logarithmic probability of the escape token, and obtain multiple probability differences; Select the target difference with the smallest value from the plurality of probability differences; The valid token corresponding to the target difference is determined as the target valid token.

7. The method as described in claim 3, characterized in that, The process for determining the preset stop condition includes: After updating the perturbation vector in each iteration, the candidate probability distribution is recalculated; Identify the target token belonging to the set of legitimate tokens and the highest probability competing token belonging to the set of illegitimate tokens from the candidate probability distribution; Calculate the difference between the probability value of the target token and the probability value of the highest probability competing token, and use it as the current probability margin; If the current probability margin is greater than or equal to the preset safety margin threshold, the stopping condition is met, and the iterative update process is terminated.

8. The method as described in claim 1, characterized in that, Before the step of generating the current token based on the optimized hidden state under the constraints of the set of legal tokens, the method further includes: The optimized hidden state is input into the output projection layer of the large language model to obtain the optimized probability distribution. Calculate the overall deviation between the optimized probability distribution and the original probability distribution corresponding to the original hidden layer state; The overall deviation is compared with a preset safety tolerance threshold. If the overall deviation is determined to be greater than the safety tolerance threshold, then the original hidden layer state is used to replace the optimized hidden layer state; otherwise, the optimized hidden layer state is used.

9. The method as described in claim 1, characterized in that, The steps for parsing predefined structural constraint rules and determining the set of valid tokens at the current decoding step include: Receive constraint strength level parameters input by the user and identify whether the parameters belong to the normal strength level or the enhanced strength level; If it is identified as an enhanced strength level, the initial set of legal tokens is shrunk according to the structural constraint rules, and tokens marked as edge legal are removed to obtain a shrunk set of legal tokens. Based on the reduced set of legal tokens, the original probability distribution is truncated and simulated, and the probability quality loss value is calculated to characterize the degree of truncation. The truncation degree is compared with a preset trigger threshold. If the truncation degree exceeds the trigger threshold, an optimization start signal is generated to automatically trigger directional optimization control in the hidden layer state space and perform optimization adjustment steps on the original hidden layer state. If it is determined that the degree of truncation does not exceed the trigger threshold, then subsequent processing is performed directly based on the original hidden layer state.

10. A structured generation control system for large language models, characterized in that, The system includes: The constraint compilation unit is used to respond to the structured generation task, parse the predefined structure constraint rules, and determine the set of legal tokens for the current decoding step. The conflict detection unit is used to obtain the original hidden state generated by the large language model based on the current input, evaluate the matching degree between the output probability distribution corresponding to the original hidden state and the set of legal tokens, and determine whether there is a constraint conflict in the current decoding step based on the matching degree. The latent space optimization unit is used to trigger directional optimization control in the latent layer state space when it is determined that there is a constraint conflict, to perform optimization adjustment on the original latent layer state, and to output the optimized latent layer state. The security verification and sampling unit is used to receive the hidden layer state and generate the current token. Specifically, when the conflict detection unit determines that there is no constraint conflict, the security verification and sampling unit directly generates the current token based on the original hidden layer state; when the conflict detection unit determines that there is a constraint conflict, the security verification and sampling unit generates the current token based on the optimized hidden layer state output by the hidden space optimization unit after security verification.