A code large model syntax constraint decoding method and system
Patent Information
- Application Number
- CN202611313105.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-08-27
- Publication Date
- 2026-10-30
AI Technical Summary
[0010]本发明针对代码大模型语法约束解码过程中,由词元边界与目标语法终结符边界不一致以及终结符匹配规则重叠所引起的词元合法性判断不准确,以及在线约束解码计算开销较高等问题,提供一种代码大模型语法约束解码方法及系统,其通过动态保留可能的终结符解释并结合词法、句法约束进行筛选,减少合法词元被错误拒绝以及非法词元被错误接受的情况,并抑制不必要的分支扩张,还可进一步借助解析状态共享、离线词法预计算、公共终结符前缀解析复用,削减在线解析开销,提升语法约束解码的词元掩码构造效率
[0022]与现有技术相比,本发明至少具有以下有益效果。
Smart Images

Figure CN123013920A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of structured text generation technology, specifically relating to a method and system for decoding syntax constraints of a large code model. Background Technology
[0002] In scenarios such as code generation, SQL query generation, domain-specific language generation, configuration script generation, and tool call code generation, the output of large models typically needs to be parsed and executed by downstream compilers, interpreters, database systems, or tool executors as machine-readable content. Therefore, it not only needs to meet user intent but also conform to predetermined syntax and format rules. To address this, Grammar Constraint Decoding (GCD) technology is applied to the model generation process. Its basic idea is to represent structured constraints as formal grammar. As the model progressively generates tokens, the validity of candidate tokens is determined based on the generated content and the target grammar. Illegal tokens are masked to prevent the output from violating grammatical rules.
[0003] In practical applications, grammatical constraint decoding must ensure that the output conforms to the target grammar while avoiding unnecessary interference with the model's original generation distribution. If a grammatically incorrect word is allowed, the generated result may violate the target grammar; conversely, if a grammatically correct word is incorrectly rejected, the model may deviate from its original legal generation path. The former is called incorrect acceptance, and the latter is called incorrect rejection. Therefore, accurately distinguishing between legal and illegal words at the current generation position is fundamental to the reliability of grammatical constraint decoding.
[0004] The aforementioned issues are closely related to the inconsistency between the boundaries of large model lexical units and the boundaries of target grammar terminal symbols. Large code models segment words according to their own lexicons, using lexical units as the basic generation unit, while formal grammars typically use terminal symbols as the basic symbols for syntax analysis. Therefore, the boundaries between the two are not naturally consistent. A grammar terminal symbol may be composed of multiple model lexical units, and a model lexical unit may simultaneously contain the end of one terminal symbol and the beginning of another. For example, in program code, identifiers and strings may be split into multiple lexical units, while adjacent grammatical components such as parentheses, operators, and separators may be combined into the same lexical unit. Therefore, during the decoding of grammatical constraints, the byte sequence corresponding to the model lexical unit needs to be interpreted as terminal symbol information in the target grammar in order to determine whether the lexical unit satisfies the target grammatical constraints.
[0005] When the lexical boundary and the terminal symbol boundary are not the same, and the target grammar contains matching rules for terminal symbols with common prefixes or overlapping each other, the byte sequence of the same lexical may correspond to different terminal symbol interpretations in the current generation context. For example, a certain byte prefix can already form a complete, shorter terminal symbol, while continuing to read subsequent bytes may form another, longer terminal symbol. In this case, different choices of the ending position of the terminal symbol matching may further affect the validity judgment of the lexical.
[0006] To address the issue of inconsistent boundaries, existing methods can be broadly categorized into two types. One type employs a relatively deterministic terminal matching strategy, such as selecting a single terminal interpretation for a word based on the greedy longest matching principle. This type of method is simple to implement and has a small number of states, but when terminal symbols overlap, it may miss other interpretations that meet grammatical requirements, leading to the incorrect exclusion of valid words and resulting in erroneous rejection. The other type of method improves adaptability to inconsistent boundaries by using finer-grained byte-level processing or retaining multiple interpretations at potential terminal symbol boundaries. However, in the absence of an effective filtering mechanism, this type of method is prone to introducing terminal symbol interpretations that do not conform to the longest matching rule or terminal symbol priority rule, causing illegal words to be incorrectly retained and resulting in erroneous acceptance. Furthermore, retaining multiple interpretations increases state maintenance and computational overhead.
[0007] Besides the accuracy of validity checks, grammatical constraint decoding also faces the challenge of online computational efficiency. One direct implementation involves traversing the vocabulary of the model at each decoding step and determining the validity of each word based on its current grammatical state, thus forming constraints on the model's output range. However, since modern large language models typically have large vocabularies, repeatedly performing validity checks on a large number of words would incur significant online computational overhead.
[0008] To improve constraint decoding efficiency, related methods employ techniques such as lexical partitioning, automata, offline pre-computation, and state sharing to reduce redundant computation. However, existing methods often struggle to simultaneously ensure the accuracy of terminal symbol interpretation and online processing efficiency, potentially introducing legality judgment errors while reducing computational overhead.
[0009] Therefore, existing technologies still need to address the following issues: how to accurately retain interpretations of terminal symbols that meet subsequent grammatical requirements and exclude interpretations that do not conform to grammatical rules when the boundaries of large model lexical units and target grammatical terminal symbols are inconsistent and the terminal symbols may overlap, so as to improve the accuracy of lexical unit validity judgment; at the same time, how to reduce online computational overhead and improve the efficiency of grammatical constraint decoding. Summary of the Invention
[0010] This invention addresses the problems of inaccurate lexical validity judgment caused by inconsistencies between lexical boundaries and target grammatical terminator boundaries, as well as overlapping terminator matching rules, and the high computational overhead of online constraint decoding in the decoding process of large code models. It provides a method and system for decoding grammatical constraints in large code models. By dynamically retaining possible terminator interpretations and combining them with lexical and syntactic constraints for filtering, it reduces the erroneous rejection of legitimate lexical terms and the erroneous acceptance of illegal lexical terms, and suppresses unnecessary branch expansion. Furthermore, it can reduce online parsing overhead and improve the efficiency of lexical mask construction in grammatical constraint decoding by leveraging parsing state sharing, offline lexical pre-computation, and common terminator prefix parsing reuse.
[0011] To achieve the above objectives, the technical solution of the present invention includes the following:
[0012] A method for decoding syntax constraints in a large code model, the method comprising: Input the initial prompt text into the code model to form the initial model generation context, and initialize the constraint parsing state according to the target syntax to obtain the initial parsing branch set; The large code model performs forward computation based on the current model's generated context to obtain the initial candidate lexical units for the current decoding step; The validity of initial candidate words is checked based on the current parsing branch set, and the subsequent parsing branch set is obtained. If at least one initial candidate word is determined to be a valid word, the initial candidate word is determined as the output word of the current decoding step, and the subsequent parsing branch set is retained. If all initial candidate words are determined to be invalid words, the set of valid words that can be generated in the current decoding step is determined based on the current parsing branch set, the output word of the current decoding step is re-determined, and the corresponding subsequent parsing branch set is obtained. The current parsing branch set is updated with the subsequent parsing branch set, and the current model generates a context based on the output lexical. Then, the forward computation steps of the code big model based on the current model generating context are re-executed based on the expansion results until the preset normal termination condition is reached, and code that conforms to the target syntax constraints is output.
[0013] Furthermore, the initial parsing branch set includes an initial parsing branch, which uses the initial lexical state as the lexical residency state and initializes the syntactic parsing state to the initial syntactic parsing state corresponding to the target grammar; wherein, the lexical residency state is used to represent the state of the lexical automaton after scanning the currently generated content, and the syntactic parsing state is used to represent the syntactic parsing progress corresponding to the currently formed terminal symbol sequence.
[0014] Furthermore, if at least one initial candidate word is determined to be a valid word, the initial candidate word is determined as the output word of the current decoding step, and the set of subsequent parsing branches is retained, including: Perform a lexical scan on each initial candidate lexical unit to obtain the lexical result corresponding to that initial candidate lexical unit; Perform syntactic checks on the terminal sequence and subsequent lexical resident state in the lexical results. If the checks pass, obtain the corresponding subsequent syntactic parsing state. The subsequent lexical residency state of the lexical result is combined with the subsequent syntactic parsing state that has passed the syntactic test to obtain the corresponding subsequent parsing branch, and the subsequent parsing branch is added to the set of subsequent parsing branches. If the set of successor parsing branches contains at least one successor parsing branch, then the initial candidate word is determined to be a valid word.
[0015] Furthermore, a lexical scan is performed on each initial candidate lexical unit to obtain the lexical result corresponding to that initial candidate lexical unit, including: For each current parsing branch in the current parsing branch set, a corresponding lexical branch is established based on the lexical residency state; Perform a regular scan of the lexical branch; wherein, the regular scan is that the lexical branch reads the initial candidate tokens byte by byte according to the longest matching basic rule, and when there is no state transition in the current byte to be read and the current lexical state is the accepting state, the output terminal symbol of the current lexical state record is determined according to the preset terminal symbol priority: if the terminal symbol sequence already output by the lexical branch is not empty, and the last non-ignored terminal symbol in the terminal symbol sequence does not satisfy the terminal symbol adjacency relationship with the output terminal symbol of the current lexical state record, then the lexical branch is discarded; otherwise, the output terminal symbol of the current lexical state record is appended to the terminal symbol sequence already output by the lexical branch; wherein, when there is no state transition in the current byte to be read and the current lexical state is the non-accepting state, the lexical branch is discarded; Based on the regular scan, when the derivation condition is met, the current lexical branch derives into the longest matching lexical branch and the early matching lexical branch, and the longest matching lexical branch and the early matching lexical branch are used as lexical branches. The remaining bytes of the initial candidate tokens are then scanned using the regular scan method. The derivation condition is that the current lexical state is in the accepting state, there is a state transition when reading the next byte, and the next lexical state after the transition is in the non-accepting state. The longest matching lexical branch consumes the next byte and transitions to the next lexical state. The early matching lexical branch outputs the output terminator recorded in the current lexical state, resets the lexical state to the initial state, and starts identifying subsequent terminators from the next byte. After the initial candidate lexical sequence scan is completed, each lexical branch that has completed the scan and has not been discarded forms a lexical result; the lexical result includes the terminal symbol sequence output by the corresponding lexical branch and the subsequent lexical resident state after the scan is completed.
[0016] Furthermore, a syntactic test is performed on the terminal sequence and subsequent lexical residency state in the lexical result. If the test passes, the corresponding subsequent syntactic parsing state is obtained, including: Using the set of graph stack head nodes corresponding to the syntactic parsing state of the current parsing branch as the starting point of syntactic parsing, the terminal symbol sequence in the lexical result is processed sequentially, and valid syntactic parsing actions are obtained and executed according to the syntactic analysis table: if a valid syntactic parsing action cannot be obtained according to the syntactic analysis table, the corresponding lexical result is discarded; otherwise, after completing the syntactic parsing of the terminal symbol sequence, the subsequent syntactic parsing state corresponding to the lexical result is obtained; wherein, the syntactic analysis table is constructed according to the syntactic rules in the target grammar; Calculate the set of acceptable terminal symbols for the next step of the subsequent syntactic parsing state, and determine whether there is an intersection between the set of acceptable terminal symbols and the set of active terminal symbols corresponding to the subsequent lexical resident state in the lexical result: if the intersection is empty, discard the subsequent syntactic parsing state and the corresponding lexical result; if the intersection is not empty, retain the subsequent syntactic parsing state and the corresponding lexical result.
[0017] Furthermore, the large code model performs forward computation based on the current model-generated context to obtain the initial candidate lexical units for the current decoding step, including: Based on the current model, forward computation is performed to obtain the logits vector corresponding to each word in the model vocabulary; The initial candidate words for the current decoding step are obtained by sampling the logits vector according to a preset sampling strategy; wherein, the preset sampling strategy may include a temperature scaling strategy, a top-k strategy, or a top-p strategy.
[0018] Furthermore, based on the current parsing branch set, the set of legal tokens allowed to be generated in the current decoding step is determined, the output tokens of the current decoding step are redefined, and the corresponding set of subsequent parsing branches is obtained, including: For any parsing branch in the current parsing branch set, the lexical lookup table is queried based on the lexical residence state of the parsing branch to obtain a set of terminal symbol sequences corresponding to the lexical residence state and one or more lexical units associated with each terminal symbol sequence; wherein, the lexical lookup table is a pre-computed mapping data table, the key of which is composed of the initial lexical residence state and the lexical unit, and the value is the set of all valid lexical results obtained after using the initial lexical residence state as the initial residence state of the parsing branch and performing the aforementioned lexical scan with dynamic lexical branching mechanism and terminal symbol adjacency filtering on the complete byte sequence of the lexical unit; The terminal symbol sequences are organized into a terminal symbol trie, so that multiple terminal symbol sequences with the same terminal symbol prefix share the same trie path, and the trie nodes corresponding to each terminal symbol sequence record the lexical units associated with that terminal symbol sequence. Starting with the syntactic parsing state of the current parsing branch, terminal symbols are input layer by layer along the terminal symbol dictionary, and corresponding syntactic parsing actions are performed according to the syntactic analysis table; wherein, the syntactic analysis table is constructed according to the syntactic rules in the target grammar; when the dictionary node corresponding to a certain terminal symbol sequence can be parsed syntactically, the lexical recorded in the dictionary node is added to the set of legal lexical corresponding to the current parsing branch; Summarize the set of legal tokens corresponding to each current parsing branch to obtain the set of legal tokens for the current decoding step, and construct the token mask for the current decoding step based on the set of legal tokens. Apply the word mask to the logits vector and resample to obtain the resampled output words; Based on the current set of parsing branches, perform a validity check on the resampled output tokens and obtain the set of subsequent parsing branches.
[0019] A code large model syntax constraint decoding system, the system comprising: The initialization module is used to input the initial prompt text into the code model, form the initial model generation context, and initialize the constraint parsing state according to the target syntax to obtain the initial parsing branch set; The forward computation module is used by the large code model to perform forward computation based on the current model's generated context to obtain the initial candidate words for the current decoding step; The code generation module is used to perform legality checks on initial candidate words based on the current parsing branch set and obtain a subsequent parsing branch set. Specifically, if at least one initial candidate word is determined to be a legal word, that initial candidate word is designated as the output word of the current decoding step, and the subsequent parsing branch set is retained. If all initial candidate words are determined to be illegal words, the module determines the set of legal words allowed to be generated in the current decoding step based on the current parsing branch set, redetermines the output words of the current decoding step, and obtains the corresponding subsequent parsing branch set. The module updates the current parsing branch set with the subsequent parsing branch set, expands the current model generation context based on the output words, and then re-executes the forward computation steps of the large code model based on the current model generation context, until a preset normal termination condition is met, outputting code that conforms to the target syntax constraints.
[0020] A computer device includes: a processor and a memory storing computer program instructions; the processor, when executing the computer program instructions, implements the code large model syntax constraint decoding method described above.
[0021] A computer-readable storage medium storing computer program instructions that, when executed by a processor, implement the code large model syntax constraint decoding method described above.
[0022] Compared with the prior art, the present invention has at least the following beneficial effects.
[0023] (1) By simultaneously retaining the longest matching lexical branch and the early matching lexical branch at the lexical matching position that meets the conditions, the interpretation of legal terminal symbols that may be missed by the regular longest matching is preserved, thereby reducing the situation where legal tokens are incorrectly rejected.
[0024] (2) By performing lexical and syntactic screening on the interpretation of multiple dynamically formed terminal symbols, and excluding lexical branches that do not satisfy the terminal symbol adjacency relationship in advance, the situation of illegal lexical units being incorrectly accepted is reduced, while limiting the unnecessary growth of lexical branches.
[0025] (3) In some implementations, by sharing the parsing state, offline lexical precomputation and parsing reuse of common terminal prefixes, the overhead of parsing state maintenance, repeated lexical scanning and repeated syntactic parsing in the online stage is reduced, thereby improving the efficiency of constructing lexical masks in the grammatical constraint decoding process. Attached Figure Description
[0026] Figure 1 This is a schematic diagram of the code large model syntax constraint decoding method and rejection sampling implementation process provided in the embodiments of the present invention.
[0027] Figure 2 This is a schematic diagram of the dynamic lexical branch scanning process in the decoding of the large code model syntax constraints provided in the embodiments of the present invention.
[0028] Figure 3 This is a schematic diagram of the online token validity verification process in the decoding of the code large model syntax constraints provided in the embodiments of the present invention.
[0029] Figure 4 This is a schematic diagram of the online mask construction process in the decoding of the large code model syntax constraints provided in the embodiments of the present invention. Detailed Implementation
[0030] To enable those skilled in the art to better understand the technical solutions in the embodiments of the present invention, and to make the objectives, features and advantages of the present invention more apparent and understandable, the present invention will be further described in detail below with reference to the accompanying drawings and embodiments.
[0031] In this embodiment, the set of terminal symbols in the target grammar is denoted as . Each terminal symbol The matching rules are defined by the corresponding regular expressions in the formalized grammar rule text, and lexical attributes such as priority can be configured. The vocabulary of a large language model is denoted as... any word element The corresponding byte sequence is denoted as .
[0032] This implementation adopts a grammatical constraint processing framework that decouples lexical and syntactic processing, and includes two stages: offline preprocessing and online constraint decoding. In the offline preprocessing stage, the system constructs the data structures required for lexical scanning and syntactic parsing based on the target grammar, and performs corresponding lexical pre-computation on the model vocabulary. In the online constraint decoding stage, the system applies grammatical constraints to the generation process of the large code model through lexical validity checks and lexical mask construction. The relevant processing mechanisms are explained below.
[0033] In the offline preprocessing stage, the system first constructs a lexical automaton for terminal symbol recognition. Specifically, the system converts the regular expression of each terminal symbol into a corresponding deterministic automaton, and merges these deterministic automata to obtain a merged lexical automaton for lexical scanning, denoted as [equation missing]. .in, This is a set of automaton states; in this embodiment, the automaton states are referred to as lexical states, and therefore also constitute a set of lexical states. For byte alphabet, This is the state transition function. This is the initial state of the automaton, which is also the initial lexical state. The system accepts a set of states. When constructing the merged lexical automaton, the system is in any lexical state. Record active terminal symbol set This is used to represent the set of terminators that can be matched under the current byte prefix, or that may still be matched after reading subsequent bytes; for the accept state The system records the set of matched terminators, as well as the unique matching terminators after disambiguation according to the preset priority order or the default keyword priority rule. .
[0034] In the offline phase, the system further constructs a syntactic analysis table for syntactic parsing. In this embodiment, the system employs the GLR parsing algorithm and constructs a syntactic analysis table for GLR syntactic parsing based on the production rules in the target grammar, which is used to determine the corresponding parsing actions in subsequent syntactic parsing processes.
[0035] Furthermore, the system determines the adjacency relationships between different terminal symbols based on the syntactic analysis table, and constructs a terminal symbol adjacency table. The terminal symbol adjacency relationship is used to represent the terminal symbols that may follow a given terminal symbol and conform to the syntactic production rules. The terminal symbol adjacency table can be used to preemptively exclude lexical branches that do not satisfy the terminal symbol adjacency relationship during subsequent lexical scanning. For the set of terminal symbols in the target grammar... For any syntactic analysis state and terminal symbols The set of actions corresponding to the syntactic analysis table is denoted as Because the GLR parsing algorithm is used, a single parsing entry can contain one or more syntactic parsing actions, including shifting, reducing, and other corresponding syntactic parsing actions. The system constructs a terminal adjacency list by statically traversing the parsing table. For any terminal symbol... The system iterates through the entries in the parsing table that have τ as the input terminal symbol; if a certain action set Includes shifting to the syntactic analysis state action Then, we further traverse each terminal symbol entry corresponding to state J. For any terminal symbol... ,like If it contains at least one syntactic parsing action, then it will Record as terminal symbol The adjacent terminal symbols. For cases where multiple shift actions exist within the same entry, the above processing is performed according to the subsequent syntactic parsing state corresponding to each shift action, and the union of the resulting adjacent terminal symbols is taken, notated as: The system performs the above processing on each terminal symbol, resulting in a terminal symbol adjacency list indexed by the terminal symbol. Each terminal symbol... Corresponding to a set of adjacency terminal symbols In some implementations, the target grammar may also include ignored terminal symbols that do not participate in syntactic parsing, such as whitespace or comments, denoted as . For any ordinary terminal symbol The system will Adding terminal symbols to For any , can make This allows the ignored terminal symbol to appear between two ordinary terminal symbols that are adjacent, without affecting the adjacency relationship. The adjacency relationship recorded in the terminal symbol adjacency list represents the possible subsequent continuation relationships between terminal symbols that conform to the syntactic production rules. The syntactic validity in the specific generation context is still determined by subsequent syntactic checks.
[0036] During the online constraint decoding phase, the system maintains a set of parsing branches corresponding to the currently generated content. Each parsing branch includes a lexical residency state and a syntactic parsing state. The lexical residency state represents the state of the lexical automaton after scanning the currently generated content, while the syntactic parsing state represents the syntactic parsing progress corresponding to the currently identified terminal symbol sequences. Since lexical boundaries and terminal symbol boundaries are not necessarily consistent, when determining whether a lexical meets the target grammatical constraints, a lexical scan is performed to convert the byte sequence corresponding to the lexical into the corresponding terminal symbol information before performing syntactic rule checks.
[0037] During the lexical scanning process, for candidate lexical units The system first establishes an initial lexical branch based on the lexical dwell state of the current parsing branch, and then starts from the dwell state. Departure, Reading The system then progressively consumes bytes and updates the lexical state of the lexical branch according to the automaton's transfer function. Lexical scanning uses the longest match as the basic scanning rule: as long as a valid transfer exists in the next byte, the system consumes the byte and updates the state; when no valid transfer exists in the next byte and the current state... At that time, the system based on Determine the currently matched terminal symbol and append it to the terminal symbol sequence produced in this scan. and reset the lexical state to Then continue scanning from that byte; when the next byte does not have a valid transfer and the current state is... If the current lexical branch fails to scan, that lexical branch is discarded. If candidate lexical units... Once the scan is complete, the lexical results are obtained. ,in, This is the sequence of terminal symbols produced in this scan. This refers to the lexical dwell state after the scan is completed.
[0038] Furthermore, during the lexical scanning process, the system can pre-filter lexical branches using the terminal adjacency list constructed in the offline preprocessing stage. For any lexical branch, if it has already output at least one non-ignored terminal, then the last non-ignored terminal is recorded as... When the subsequent matching of this lexical branch yields the terminal symbol to be output. At that time, if If the two do not satisfy the terminal adjacency relationship, the system will discard the lexical branch in advance; otherwise, the lexical branch will be retained and subsequent lexical scans will continue. For ignoring terminal symbols, the adjacency relationship set for ignoring terminal symbols in the aforementioned terminal symbol adjacency table can be used for processing.
[0039] When using only the conventional longest match scan, candidate lexical units typically result in only one lexical result from a single lexical branch. In cases where lexical boundaries do not align with terminal boundaries, this single path may not cover the shorter prefix terminal matches allowed by the target grammar. Therefore, this implementation introduces a dynamic lexical branching mechanism during the lexical scanning process.
[0040] Specifically, let the current lexical state of any lexical branch be... The next byte is And there is a state transition. .when ,and When this happens, the default is to transition along the longest match to... .when and At that time, the longest matching lexical branch and the early matching lexical branch are derived from the current lexical branch. The longest matching lexical branch consumes bytes. And transition to lexical state To continue identifying terminal symbols along the longest matching direction; the early matching lexical branch will Append it to its already output sequence of terminal symbols, and reset the lexical state to... and from and bytes Continue scanning to identify subsequent terminal symbols. During subsequent scanning, the two lexical branches update their lexical states and output terminal symbols respectively; if either lexical branch satisfies the above conditions again, the corresponding longest matching lexical branch and early matching lexical branch are derived from that lexical branch.
[0041] Furthermore, before deriving the pre-matching lexical branch, the system can also perform branch pre-filtering using the terminal adjacency list. For the current acceptance state... and output terminator The system starts from the initial lexical state Read the next byte If no valid state transition exists, then no early matching lexical branch is derived; if bytes are read... Then transition to lexical state Then judge When the intersection is empty, it indicates that the terminal symbol should be output early. After that, there is no way to get from bytes The process continues to form terminal symbols that satisfy the terminal symbol adjacency relationship, so the early matching lexical branch is not derived; when the intersection is not empty, the early matching lexical branch is retained, and subsequent bytes are scanned in the manner described above.
[0042] Furthermore, the system can be pre-configured with a custom set of branches for the transition from accept state to accept state. .when , ,and At this point, the system triggers a custom branch at the current transition position, simultaneously retaining the longest matching branch and the early matching branch as described above. For example, in a non-restricted example, the character sequence >> in the Java class language can be used as a single terminal symbol corresponding to the right shift operator, or it can represent two consecutive closing symbols > in the generic type context. The system can add the corresponding terminal symbol tuple to the custom branch set. This allows the lexical scan to retain both the longest match and the early match at that position, and the feasible branch is then selected by subsequent syntactic verification.
[0043] Therefore, for the same candidate lexical unit, the system can obtain one or more lexical results. .in, For the first The sequence of terminal symbols produced by each lexical branch This represents the subsequent lexical residency state after the branch scan is completed. The system, considering the syntactic parsing state corresponding to the current parsing branch that generated the lexical result, performs syntactic checks on each lexical result. These syntactic checks include syntactic parsing and continuity assessment.
[0044] For any word result The system uses the syntactic parsing state of the current parsing branch. As the initial state, the sequence of terminal symbols in the lexical result is... The input is sequentially processed by the parser, and corresponding parsing actions are performed according to the pre-constructed parsing table. For ignored terminal symbols in the terminal symbol sequence, the system does not treat them as input terminal symbols to perform parsing table actions and maintains the current parsing state. If a valid parsing action cannot be obtained during the processing of the terminal symbol sequence, the lexical result is discarded; if the terminal symbol sequence can be effectively parsed by the current parsing state, the corresponding subsequent parsing state is obtained. .
[0045] For the lexical results obtained through syntactic parsing, the system further performs a continuity check. Specifically, it determines the subsequent syntactic parsing state. The corresponding set of acceptable terminal symbols for the next step And obtain the subsequent lexical residency state of the lexical result. The corresponding set of active terminators If the two intersect, it indicates that there is still a subsequent terminal symbol matching path that satisfies both lexical and syntactic rules at the current lexical terminus position. The system retains this lexical result and sets the subsequent lexical residency state. With subsequent syntactic parsing state Combined to form the corresponding subsequent analytical branches If there is no intersection between the two, then discard the lexical result and its corresponding subsequent syntactic parsing state.
[0046] The system processes each parsing branch in the current parsing branch set and its generated lexical results in the manner described above, and summarizes them to obtain the subsequent parsing branch set corresponding to the candidate lexical. If the subsequent parsing branch set contains at least one subsequent parsing branch, the candidate lexical is determined to be a legal lexical; if the subsequent parsing branch set is empty, the candidate lexical is determined to be an illegal lexical.
[0047] In one optional implementation, to improve the efficiency of state maintenance for multiple parsing paths, the system uses a graph stack to represent the parsing state. Nodes in the graph stack record the parsing table state and references to predecessor nodes. A parsing state can be represented by one or more graph stack head nodes. For already created graph stack nodes, their recorded parsing table state and established predecessor references remain unchanged. Subsequent parsing processes do not modify existing nodes; instead, they evolve the parsing state by creating new nodes and establishing references between them. Therefore, different parsing paths can share a common parsing stack prefix by referencing the same existing nodes.
[0048] During syntactic parsing, the system creates a new stack head node based on corresponding shift, reduction, and other actions, and makes the new stack head node reference the corresponding existing node to form an updated syntactic parsing state. When a terminal symbol has multiple valid syntactic parsing actions, or when different lexical results form multiple subsequent parsing branches, the system does not need to copy the complete syntactic parsing stack corresponding to each parsing branch, but only needs to save the reference of the corresponding graph structure stack head node for each branch. Since the existing predecessor node referenced by the stack head node and its predecessor relationship remain unchanged, each branch can continue to share the common syntactic stack prefix. This reduces the overhead of stack structure copying and state maintenance in multi-branch scenarios.
[0049] The aforementioned lexical validity check targets a given candidate lexical to determine whether the lexical is valid and its corresponding subsequent parsing branch; the lexical mask construction targets the model vocabulary to determine the set of lexicals allowed to be output in the current decoding step. Both use the same grammatical validity criteria, but can be called by the external inference engine as independent constraint processing interfaces.
[0050] In a direct lexical mask construction method, the system can traverse each lexical in the model vocabulary, determine the validity of each lexical according to the aforementioned lexical validity rules, add valid lexicals to the valid lexical set of the current decoding step, and construct a lexical mask accordingly. However, this method requires performing lexical scanning and syntactic verification on a large number of lexicals in the model vocabulary, resulting in high online computational overhead. Therefore, to reduce the repeated lexical scanning and syntactic parsing calculations in the lexical mask construction process, this implementation further optimizes the lexical mask construction process by employing offline lexical pre-computation, a lexical lookup table, and a terminal trie.
[0051] To reduce the online computational overhead of the direct construction method described above, the system can pre-compute the lexical scanning results during the offline preprocessing stage. Since the lexical scanning results are determined solely by the merged lexical automaton, the lexical byte sequence, the terminal priority rules, and the dynamic lexical branching rules, and do not depend on the current syntactic parsing state, the system can iterate through the lexical automata one by one during the offline stage. and Using each lexical state as the initial lexical dwell state, a lexical scan employing a dynamic lexical branching mechanism is pre-executed on each lexical unit to obtain one or more corresponding lexical results. .
[0052] For any word result The system obtains the sequence of terminal symbols it has produced. As a shared prefix, and to obtain the lexical residency state of its successor. The corresponding set of active terminators Each active terminator Add to respectively Forming a sequence of terminal symbols The system follows the initial lexical state. The terminal symbol sequences are aggregated, and one or more lexical units associated with each terminal symbol sequence are recorded, thereby forming a lexical lookup table indexed by lexical state.
[0053] The additional active terminal symbol is used to represent the probability of matching subsequent terminal symbols after the lexical scanning is completed. It is only used for syntactic validity judgment in the lexical mask construction process, and its corresponding syntactic parsing state is not used as the subsequent syntactic parsing state after the model actually outputs the lexical.
[0054] Furthermore, when constructing the lexical mask online, for any parsing branch in the current parsing branch set, the system queries the aforementioned lexical lookup table according to its lexical residency state to obtain a set of terminal symbol sequences corresponding to the lexical residency state and the lexical associated with each terminal symbol sequence.
[0055] The system organizes the terminal symbol sequences into a terminal symbol trie, so that multiple terminal symbol sequences with the same terminal symbol prefix share the same trie path, and records the lexical units associated with the terminal symbol sequence in the trie node corresponding to each terminal symbol sequence.
[0056] The system starts with the syntactic parsing state of the current parsing branch and performs syntactic parsing layer by layer along the terminal symbol trie, reusing the syntactic parsing states corresponding to common terminal symbol prefixes. If a trie node cannot obtain a valid syntactic parsing action, the subsequent subtree rooted at that node is pruned; if a complete terminal symbol sequence can complete a valid syntactic parsing, the tokens recorded at its corresponding node are added to the set of valid tokens corresponding to the current parsing branch.
[0057] The system processes each parsing branch in the current parsing branch set in the manner described above, and takes the union of the resulting sets of legal tokens to obtain the set of legal tokens for the current decoding step, and constructs a token mask accordingly. The aforementioned offline lexical pre-computation, lexical lookup table, and terminal trie are used to reduce repeated lexical scanning and syntactic parsing calculations during the token mask construction process, without changing the aforementioned criteria for determining token legality.
[0058] For special terms such as model termination terms that do not correspond to ordinary generated text byte sequences, the system can adopt a special processing method different from that for ordinary terms, determining whether the special term is acceptable based on the current parsing state and the completion conditions specified by the target grammar. This special processing does not affect the aforementioned lexical scanning, syntactic verification, and term mask construction processes for ordinary terms.
[0059] like Figure 1As shown, this embodiment uses the rejection sampling process in the decoding of syntax constraints in a large code model as an example to illustrate the method of the present invention, which specifically includes the following steps: Step 1, Offline Preprocessing. The user inputs the formalized text of the target grammar and the model vocabulary data. The system constructs the data structure required for lexical scanning and syntactic parsing based on the target grammar rules, and pre-calculates the lexical scanning results for the lexical units in the model vocabulary.
[0060] Step 1.1, constructing the lexical automaton. The system constructs the corresponding lexical automaton according to the regular expression matching rules of each terminal symbol in the target grammar, and merges the lexical automata to form a merged lexical automaton with bytes as input symbols.
[0061] For the lexical state in the merged lexical automaton, the system records the set of active terminal symbols that may still complete the match when continuing to read subsequent bytes from that state; for the accept state, it also records the output terminal symbols determined according to the preset terminal symbol priority.
[0062] Step 1.2: Construct the syntactic analysis table and the terminal adjacency table. The system constructs a syntactic analysis table for syntactic parsing based on the syntactic rules in the target grammar. The system further determines the adjacency relationships between different terminal symbols based on the syntactic analysis table and constructs the terminal adjacency table.
[0063] Step 1.3: Perform lexical pre-computation to form a lexical lookup table. The system iterates through all automaton lexical states and lexical units in the model vocabulary one by one, and performs the aforementioned lexical scan with dynamic lexical branching mechanism and adjacency filtering on the byte sequence of the lexical unit, taking the corresponding lexical state as the starting lexical dwell state, to obtain the possible lexical results of each lexical unit.
[0064] For any lexical result, the sequence of terminal symbols it has already produced is used as a shared prefix, and the set of active terminal symbols corresponding to the subsequent lexical residency state of the lexical result is expanded item by item. Each active terminal symbol is then appended to the shared prefix to form multiple terminal symbol sequences.
[0065] The system aggregates terminal symbol sequences formed by different lexical results and records the starting lexical state and lexical unit associated with each terminal symbol sequence. This forms a lexical lookup table indexed by lexical state, where each lexical state corresponds to a set of terminal symbol sequences, and each terminal symbol sequence is further associated with one or more lexical units.
[0066] The lexical lookup table is used to construct a set of legal lexical units and a lexical mask online. It is not used directly to determine the subsequent parsing branch corresponding to the actual output lexical unit of the model. After the model actually determines the output lexical unit, the system still determines its corresponding subsequent parsing branch according to the aforementioned lexical unit legality verification process for state update.
[0067] Step 2: Initialize the code model generation process. During the online constraint phase, the user inputs the initial prompt text into the code model, forming the initial model generation context.
[0068] Simultaneously, an initial set of parsing branches is obtained by initializing the constraint parsing state according to the target grammar. This initial set of parsing branches contains only one initial parsing branch, which uses the initial lexical state as its lexical residency state and initializes the syntactic parsing state to the initial syntactic parsing state corresponding to the target grammar.
[0069] Among them, the lexical residency state is used to represent the state of the lexical automaton after scanning the currently generated content, and the syntactic parsing state is used to represent the syntactic parsing progress corresponding to the currently formed terminal symbol sequence.
[0070] Step 3: The large code model performs forward computation for the current decoding step and obtains initial candidate lexical units.
[0071] The code-based large model performs forward computation based on the current model's generated context to obtain the logits vector corresponding to each word in the model's vocabulary.
[0072] The external inference engine samples the logits vector to obtain the initial candidate tokens for the current decoding step according to a preset sampling strategy. The sampling strategy may include temperature scaling, top-k, top-p, or a combination thereof.
[0073] The forward computation of the large code model and the sampling based on logits can be implemented using existing model inference engines.
[0074] Step 4: Perform a validity check on the initial candidate lexical units to obtain the set of subsequent parsing branches.
[0075] The system performs a validity check on the initial candidate lexical units obtained in step 3 based on the current set of parsing branches. The validity check includes lexical scanning and syntactic verification, and determines the set of subsequent parsing branches corresponding to the candidate lexical unit. Specifically, this includes the following steps.
[0076] Step 4.1: Perform a lexical scan on the candidate lexical units to obtain the lexical results corresponding to the candidate lexical units. For example... Figure 2 As shown, the system traverses the current set of parsing branches. For any current parsing branch, the system takes its lexical residency state as the starting state and performs a lexical scan with a dynamic branching mechanism on the byte sequence of candidate terms to obtain the lexical result corresponding to that parsing branch. Specifically, this includes the following steps.
[0077] Step 4.1.1: Establish lexical branches and perform regular scanning. For the current parsing branch, the system establishes a corresponding lexical branch based on its lexical residency state. The lexical branch reads candidate tokens byte by byte according to the longest matching basic rule, updates the lexical state according to the state transition results of the lexical automaton, and independently maintains the output terminal symbol sequence.
[0078] Step 4.1.2: Output terminal symbols and perform adjacency filtering. When there is no state transition for the current byte to be read and the current lexical state is accepting, the system determines the output terminal symbol of the current lexical state record according to the preset terminal symbol priority. If the terminal symbol sequence already output by the lexical branch is not empty, and the last non-ignore terminal symbol in the terminal symbol sequence does not satisfy the terminal symbol adjacency relationship with the terminal symbol to be output (the output terminal symbol recorded in the current lexical state), then the lexical branch is discarded; otherwise, the terminal symbol to be output is appended to the terminal symbol sequence already output by the lexical branch, the lexical state is reset to the initial lexical state, and subsequent terminal symbols are identified starting from the current byte to be read. When there is no state transition for the current byte to be read and the current lexical state is not accepting, the lexical branch is discarded.
[0079] Step 4.1.3, Dynamic Lexical Branching. Based on the regular scanning in Steps 4.1.1 and 4.1.2, when the current lexical state is in the accepting state, and there is a state transition when reading the next byte to be read, and the next lexical state after the transition is in the non-accepting state, the current lexical branch derives into the longest matching lexical branch and the early matching lexical branch. The longest matching lexical branch consumes the next byte to be read and transitions to the next lexical state; the early matching lexical branch outputs the output terminator recorded in the current lexical state, resets the lexical state to the initial state, and starts identifying subsequent terminators from the next byte to be read.
[0080] Before deriving the early matching lexical branch, the system can also determine whether there is a subsequent terminal symbol that satisfies the terminal symbol adjacency relationship based on the terminal symbol adjacency list and the set of active terminal symbols obtained after reading the next byte from the initial lexical state; if there is no continuable terminal symbol, the early matching lexical branch is not derived, and the current lexical branch continues to scan along the longest matching direction.
[0081] If both the current lexical state and the next lexical state are accepting states and the output terminal symbols recorded by them are different, the system will not derive an early matching lexical branch by default; when the corresponding terminal symbol pair belongs to the custom branch set, an early matching lexical branch will be derived while retaining the longest matching lexical branch.
[0082] Each lexical branch continues to scan the remaining bytes of candidate lexical units, and continues to derive lexical branches when the corresponding derivation conditions are met again.
[0083] Step 4.1.4: Forming Lexical Results. After the byte sequence scanning of candidate tokens is completed, lexical results are formed from each lexical branch that has completed the scan and has not been discarded. Each lexical result includes the terminal sequence output by the corresponding lexical branch and the subsequent lexical resident state after the scan is completed.
[0084] Step 4.2, syntactic verification. For example... Figure 3 As shown, for each lexical result obtained in step 4.1, the system inputs the terminal symbol sequence into the GLR parsing algorithm and performs syntactic checks based on the syntactic parsing state of the current parsing branch. Specifically, this includes the following steps.
[0085] Step 4.2.1: Perform syntactic parsing. The system uses the set of stack head nodes of the graph structure corresponding to the syntactic parsing state of the current parsing branch as the starting point for syntactic parsing. It sequentially processes the terminal symbol sequences in the lexical results and performs corresponding shift and / or reduction operations according to the syntactic analysis table. During the execution of syntactic parsing operations, the syntactic parsing state is updated by creating new stack head nodes and establishing reference relationships between them and existing predecessor nodes. If a valid syntactic parsing operation cannot be obtained according to the syntactic analysis table during the processing of the terminal symbol sequence, the corresponding lexical result is discarded; if the terminal symbol sequence can be effectively parsed by the current syntactic parsing state, the subsequent syntactic parsing state corresponding to that lexical result is obtained.
[0086] Step 4.2.2: Perform a continuity check. The system calculates the set of acceptable terminal symbols for the next step of the subsequent syntactic parsing state and determines whether there is an intersection between this set and the set of active terminal symbols corresponding to the subsequent lexical resident state in the lexical result. If the intersection is empty, the subsequent syntactic parsing state and its corresponding lexical result are discarded.
[0087] Step 4.3: Construct the set of successor parsing branches. For the lexical results and their corresponding successor syntactic parsing states retained in step 4.2.2, combine the successor lexical residency state of the lexical results with the successor syntactic parsing states to obtain the corresponding successor parsing branches, and add the successor parsing branches to the set of successor parsing branches.
[0088] Step 4.4: Determine whether the candidate word is valid. If the set of subsequent parsing branches formed in step 4.3 contains at least one subsequent parsing branch, then the candidate word obtained in step 3 is determined to be a valid word; if the set of subsequent parsing branches is empty, then the candidate word is determined to be an invalid word.
[0089] Step 5: Determine the subsequent processing procedure based on the legality of the initial candidate lexical units. If Step 4 determines that the initial candidate lexical unit is a legal lexical unit, then the initial candidate lexical unit is determined as the output lexical unit of the current decoding step, and its corresponding subsequent parsing branch set is retained, followed by Step 7. If Step 4 determines that the initial candidate lexical unit is an illegal lexical unit, then the current parsing branch set remains unchanged, and Step 6 is executed to determine the output lexical unit of the current decoding step and its corresponding subsequent parsing branch set by constructing a lexical mask and resampling.
[0090] Step 6: Construct a lexical mask and resample the output lexical units. The system determines the set of legal lexical units for the current decoding step based on the current parsing branch set, and constructs a lexical mask accordingly. The lexical mask is applied to the logits vector obtained in Step 3, and the output lexical units for the current decoding step are resampled according to a preset sampling strategy based on the masked logits vector. This specifically includes the following steps.
[0091] Step 6.1: Query the lexical lookup table. For any parsing branch in the current parsing branch set, the system queries the lexical lookup table constructed in step 1.3 based on the lexical residency state of that parsing branch to obtain a set of terminal symbol sequences corresponding to that lexical residency state and one or more lexical units associated with each terminal symbol sequence.
[0092] Step 6.2, construct the terminal symbol trie. The system organizes the terminal symbol sequences obtained in Step 6.1 into a terminal symbol trie, so that multiple terminal symbol sequences with the same terminal symbol prefix share the same trie path, and records the lexical associated with the terminal symbol sequence in the trie node corresponding to each terminal symbol sequence.
[0093] Step 6.3: Traverse the terminal symbol trie and perform syntactic parsing. The system takes the syntactic parsing state of the current parsing branch as the starting state, inputs terminal symbols layer by layer along the terminal symbol trie constructed in Step 6.2, and performs the corresponding syntactic parsing actions according to the syntactic analysis table.
[0094] For different terminal symbol sequences with the same terminal symbol prefix, the system reuses the syntactic parsing state corresponding to the common prefix to reduce repeated syntactic parsing. If a terminal symbol corresponding to a certain trie node cannot obtain a valid syntactic parsing action in the current syntactic parsing state, the subsequent subtree rooted at that node is pruned; if a valid syntactic parsing action can be obtained, syntactic parsing continues along its successor nodes.
[0095] When a dictionary node corresponding to a certain terminal symbol sequence can be parsed syntactically, the tokens recorded in that node are added to the set of valid tokens corresponding to the current parsing branch.
[0096] Step 6.4: Form a set of legal tokens and generate a token mask. The system summarizes the sets of legal tokens obtained from each current parsing branch in step 6.3, and takes their union to obtain the set of legal tokens for the current decoding step. The system constructs a token mask for the current decoding step based on the set of legal tokens, ensuring that the tokens in the set of legal tokens remain sampleable while masking other tokens.
[0097] Step 6.5: Apply the token mask to the logits vector and resample. The system applies the token mask obtained in Step 6.4 to the logits vector output by the current decoding step of the large code model in Step 3, setting the logits value corresponding to illegal tokens to negative infinity, so that their sampling probability after probability normalization is 0, thereby preventing illegal tokens from being resampled; for legal tokens, their corresponding logits values remain unchanged. Subsequently, the external inference engine resamples based on the processed logits vector, following the same sampling strategy as in Step 3, to obtain the output tokens of the current decoding step.
[0098] Step 6.6: Obtain the set of successor parsing branches corresponding to the output token. For the output tokens resampled in Step 6.5, following the token validity verification process described in Step 4, determine the set of successor parsing branches corresponding to the output token based on the current set of parsing branches, for subsequent parsing state updates.
[0099] Step 7: Update the parsing state and model generation context. After determining the output token of the current decoding step and the set of subsequent parsing branches corresponding to the output token, the system updates the current parsing branch set with the set of subsequent parsing branches.
[0100] Meanwhile, the external inference engine appends the output lexical units to the current model generation context, forming the model generation context used in the next decoding step.
[0101] Specifically, when step 4 determines that the initial candidate word is valid, the subsequent parsing branch set obtained in step 4 is used for the above update; when step 4 determines that the initial candidate word is invalid and the output word is obtained by resampling in step 6, the subsequent parsing branch set obtained in step 6.6 is used for the above update.
[0102] Step 8: Determine if the code model generation process has ended. If the current generation process has not yet reached the normal termination condition, use the model generation context and parsing branch set updated in Step 7 as the input for the next decoding step, and return to Step 3 to continue iterative generation; when the current generation process reaches the preset normal termination condition, the generation process ends normally and outputs code or other structured text that conforms to the target syntax constraints.
[0103] In cases where generation is prematurely terminated due to reaching the maximum generation length, timeout, or other external stopping rules, the syntax validity guarantee described in this method does not apply to the premature termination result because the generation process did not meet the above-mentioned normal termination conditions.
[0104] In another alternative implementation, the large code model can employ a constraint decoding process that first constructs a token mask and then performs sampling. At each decoding step, the system first determines the set of legal tokens for the current decoding step based on the current parsing branch set, following the aforementioned token mask construction method, and generates a token mask. The external inference engine then samples the logits vector constrained by the token mask to obtain the output tokens for the current decoding step.
[0105] For the output token, the system determines its corresponding set of subsequent parsing branches according to the aforementioned token validity verification method, and updates the current set of parsing branches accordingly to continue the subsequent decoding process.
[0106] Compared with the aforementioned implementation method that samples first, performs legality checks, and constructs a lexical mask for resampling when candidate lexical terms are invalid, the main difference between this implementation method and the previous one is the different execution order of lexical mask construction and lexical sampling. Both implementation methods use the aforementioned dynamic lexical branching and syntactic checks to perform lexical legality checks, and use lexical lookup tables, terminal trie trees, and syntactic parsing to construct lexical masks.
[0107] Based on the same inventive concept, another embodiment of the present invention provides 8. A code large model syntax constraint decoding system, characterized in that the system comprises: The initialization module is used to input the initial prompt text into the code model, form the initial model generation context, and initialize the constraint parsing state according to the target syntax to obtain the initial parsing branch set; The forward computation module is used by the large code model to perform forward computation based on the current model's generated context to obtain the initial candidate words for the current decoding step; The code generation module is used to perform legality checks on initial candidate words based on the current parsing branch set and obtain a subsequent parsing branch set. Specifically, if at least one initial candidate word is determined to be a legal word, that initial candidate word is designated as the output word of the current decoding step, and the subsequent parsing branch set is retained. If all initial candidate words are determined to be illegal words, the module determines the set of legal words allowed to be generated in the current decoding step based on the current parsing branch set, redetermines the output words of the current decoding step, and obtains the corresponding subsequent parsing branch set. The module updates the current parsing branch set with the subsequent parsing branch set, expands the current model generation context based on the output words, and then re-executes the forward computation steps of the large code model based on the current model generation context, until a preset normal termination condition is reached, outputting code that conforms to the target syntax constraints.
[0108] The above system embodiments and the aforementioned method embodiments belong to the same inventive concept. The specific execution process of each module can be referred to the aforementioned method steps, and will not be repeated here. Each module can be merged, split, or implemented in software, hardware, or a combination thereof according to actual deployment needs. As long as it can perform the above-mentioned corresponding functions, it belongs to the implementation of this invention.
[0109] Based on the same inventive concept, another embodiment of the present invention provides a computer device, the computer device including a processor and a memory, the memory storing a computer program, and when the computer program is executed by the processor, implementing the method described in any of the above embodiments.
[0110] Based on the same inventive concept, another embodiment of the present invention provides a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the method described in any of the above embodiments.
[0111] In other embodiments, the parser, lexical automaton, lexical masking target, and target grammar type can all be implemented using equivalent substitution methods. For example, the parser is not limited to a GLR parser, the lexical automaton is not limited to a deterministic automaton form, and the lexical mask can also be applied to a logits vector, a probability vector, or a candidate sample set.
[0112] The above embodiments are only used to illustrate the technical solutions of the present invention, and are not intended to limit the scope of protection of the present invention. Equivalent substitutions, combinations, adjustments, or improvements made by those skilled in the art without departing from the technical concept of the present invention should all fall within the scope of protection of the present invention. The scope of protection of the present invention is defined by the claims.
Claims
1. A method for decoding syntax constraints in a large code model, characterized in that, The method includes: Input the initial prompt text into the code model to form the initial model generation context, and initialize the constraint parsing state according to the target syntax to obtain the initial parsing branch set; The large code model performs forward computation based on the current model's generated context to obtain the initial candidate lexical units for the current decoding step; The validity of initial candidate words is checked based on the current parsing branch set, and the subsequent parsing branch set is obtained. If at least one initial candidate word is determined to be a valid word, the initial candidate word is determined as the output word of the current decoding step, and the subsequent parsing branch set is retained. If all initial candidate words are determined to be invalid words, the set of valid words that can be generated in the current decoding step is determined based on the current parsing branch set, the output word of the current decoding step is re-determined, and the corresponding subsequent parsing branch set is obtained. The current parsing branch set is updated with the subsequent parsing branch set, and the current model generates a context based on the output lexical. Then, the forward computation steps of the code big model based on the current model generating context are re-executed based on the expansion results until the preset normal termination condition is reached, and code that conforms to the target syntax constraints is output.
2. The method according to claim 1, characterized in that, The initial parsing branch set includes an initial parsing branch, which uses the initial lexical state as the lexical dwell state and initializes the syntactic parsing state to the initial syntactic parsing state corresponding to the target grammar; wherein, the lexical dwell state is used to represent the state of the lexical automaton after scanning the currently generated content, and the syntactic parsing state is used to represent the syntactic parsing progress corresponding to the currently formed terminal symbol sequence.
3. The method according to claim 2, characterized in that, If at least one initial candidate word is determined to be a valid word, that initial candidate word is designated as the output word of the current decoding step, and the set of subsequent parsing branches is retained, including: Perform a lexical scan on each initial candidate lexical unit to obtain the lexical result corresponding to that initial candidate lexical unit; Perform syntactic checks on the terminal sequence and subsequent lexical resident state in the lexical results. If the checks pass, obtain the corresponding subsequent syntactic parsing state. The subsequent lexical residency state of the lexical result is combined with the subsequent syntactic parsing state that has passed the syntactic test to obtain the corresponding subsequent parsing branch, and the subsequent parsing branch is added to the set of subsequent parsing branches. If the set of successor parsing branches contains at least one successor parsing branch, then the initial candidate word is determined to be a valid word.
4. The method according to claim 3, characterized in that, Perform a lexical scan on each initial candidate lexical unit to obtain the lexical results corresponding to that initial candidate lexical unit, including: For each current parsing branch in the current parsing branch set, a corresponding lexical branch is established based on the lexical residency state; Perform a regular scan of the lexical branch; wherein, the regular scan is that the lexical branch reads the initial candidate tokens byte by byte according to the longest matching basic rule, and when there is no state transition in the current byte to be read and the current lexical state is the accepting state, the output terminal symbol of the current lexical state record is determined according to the preset terminal symbol priority: if the terminal symbol sequence already output by the lexical branch is not empty, and the last non-ignored terminal symbol in the terminal symbol sequence does not satisfy the terminal symbol adjacency relationship with the output terminal symbol of the current lexical state record, then the lexical branch is discarded; otherwise, the output terminal symbol of the current lexical state record is appended to the terminal symbol sequence already output by the lexical branch; wherein, when there is no state transition in the current byte to be read and the current lexical state is the non-accepting state, the lexical branch is discarded; Based on the regular scan, when the derivation condition is met, the current lexical branch derives into the longest matching lexical branch and the early matching lexical branch, and the longest matching lexical branch and the early matching lexical branch are used as lexical branches. The remaining bytes of the initial candidate tokens are then scanned using the regular scan method. The derivation condition is that the current lexical state is in the accepting state, there is a state transition when reading the next byte, and the next lexical state after the transition is in the non-accepting state. The longest matching lexical branch consumes the next byte and transitions to the next lexical state. The early matching lexical branch outputs the output terminator recorded in the current lexical state, resets the lexical state to the initial state, and starts identifying subsequent terminators from the next byte. After the initial candidate lexical sequence scan is completed, each lexical branch that has completed the scan and has not been discarded forms a lexical result; the lexical result includes the terminal symbol sequence output by the corresponding lexical branch and the subsequent lexical resident state after the scan is completed.
5. The method according to claim 3, characterized in that, The terminal sequence and subsequent lexical residency state in the lexical results are subjected to syntactic checks. If the checks pass, the corresponding subsequent syntactic parsing state is obtained, including: Using the set of graph stack head nodes corresponding to the syntactic parsing state of the current parsing branch as the starting point of syntactic parsing, the terminal symbol sequence in the lexical result is processed sequentially, and valid syntactic parsing actions are obtained and executed according to the syntactic analysis table: if a valid syntactic parsing action cannot be obtained according to the syntactic analysis table, the corresponding lexical result is discarded; otherwise, after completing the syntactic parsing of the terminal symbol sequence, the subsequent syntactic parsing state corresponding to the lexical result is obtained; wherein, the syntactic analysis table is constructed according to the syntactic rules in the target grammar; Calculate the set of acceptable terminal symbols for the next step of the subsequent syntactic parsing state, and determine whether there is an intersection between the set of acceptable terminal symbols and the set of active terminal symbols corresponding to the subsequent lexical resident state in the lexical result: if the intersection is empty, discard the subsequent syntactic parsing state and the corresponding lexical result; if the intersection is not empty, retain the subsequent syntactic parsing state and the corresponding lexical result.
6. The method according to claim 2, characterized in that, The large code model performs forward computation based on the current model's generated context to obtain the initial candidate lexical units for the current decoding step, including: Based on the current model, a forward computation is performed to generate the context, resulting in the logits vector corresponding to each word in the model vocabulary; The initial candidate words for the current decoding step are obtained by sampling the logits vector according to a preset sampling strategy; wherein, the preset sampling strategy may include a temperature scaling strategy, a top-k strategy, or a top-p strategy.
7. The method according to claim 6, characterized in that, Based on the current parsing branch set, determine the set of legal tokens allowed to be generated in the current decoding step, redetermine the output tokens for the current decoding step, and obtain the corresponding set of subsequent parsing branches, including: For any parsing branch in the current parsing branch set, the lexical lookup table is queried based on the lexical residence state of the parsing branch to obtain a set of terminal symbol sequences corresponding to the lexical residence state and one or more lexical units associated with each terminal symbol sequence; wherein, the lexical lookup table is a pre-computed mapping data table, the key of which is composed of the initial lexical residence state and the lexical unit, and the value is the set of all valid lexical results obtained after using the initial lexical residence state as the initial residence state of the parsing branch and performing the aforementioned lexical scan with dynamic lexical branching mechanism and terminal symbol adjacency filtering on the complete byte sequence of the lexical unit; The terminal symbol sequences are organized into a terminal symbol trie, so that multiple terminal symbol sequences with the same terminal symbol prefix share the same trie path, and the trie nodes corresponding to each terminal symbol sequence record the lexical units associated with that terminal symbol sequence. Starting with the syntactic parsing state of the current parsing branch, terminal symbols are input layer by layer along the terminal symbol dictionary, and corresponding syntactic parsing actions are performed according to the syntactic analysis table; wherein, the syntactic analysis table is constructed according to the syntactic rules in the target grammar; when the dictionary node corresponding to a certain terminal symbol sequence can be parsed syntactically, the lexical recorded in the dictionary node is added to the set of legal lexical corresponding to the current parsing branch; Summarize the set of legal tokens corresponding to each current parsing branch to obtain the set of legal tokens for the current decoding step, and construct the token mask for the current decoding step based on the set of legal tokens. Apply the word mask to the logits vector and resample to obtain the resampled output words; Based on the current set of parsing branches, perform a validity check on the resampled output tokens and obtain the set of subsequent parsing branches.
8. A code large model syntax constraint decoding system, characterized in that, The system includes: The initialization module is used to input the initial prompt text into the code model, form the initial model generation context, and initialize the constraint parsing state according to the target syntax to obtain the initial parsing branch set; The forward computation module is used by the large code model to perform forward computation based on the current model's generated context to obtain the initial candidate words for the current decoding step; The code generation module is used to perform legality checks on initial candidate words based on the current parsing branch set and obtain a subsequent parsing branch set. Specifically, if at least one initial candidate word is determined to be a legal word, that initial candidate word is designated as the output word of the current decoding step, and the subsequent parsing branch set is retained. If all initial candidate words are determined to be illegal words, the module determines the set of legal words allowed to be generated in the current decoding step based on the current parsing branch set, redetermines the output words of the current decoding step, and obtains the corresponding subsequent parsing branch set. The module updates the current parsing branch set with the subsequent parsing branch set, expands the current model generation context based on the output words, and then re-executes the forward computation steps of the large code model based on the current model generation context, until a preset normal termination condition is met, outputting code that conforms to the target syntax constraints.
9. A computer device, characterized in that, The computer device includes: a processor and a memory storing computer program instructions; when the processor executes the computer program instructions, it implements the code large model syntax constraint decoding method as described in any one of claims 1-7.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer program instructions, which, when executed by a processor, implement the code large model syntax constraint decoding method as described in any one of claims 1-7.
