Automatic driving simulation scene-oriented low-resource domain-specific language generation method
By embedding vector data to construct and define constraints, decompose tasks, generate code, and perform compliance checks, combined with large language models and syntax checking and correction techniques, the high learning cost and syntax error problems in the generation of autonomous driving simulation scenarios are solved. This achieves efficient and reliable DSL code generation, improving the efficiency and accuracy of automated simulation scenario construction.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-11-28
- Publication Date
- 2026-03-13
AI Technical Summary
Existing methods for generating autonomous driving simulation scenarios suffer from high learning costs, low development efficiency, and poor scenario transferability. Furthermore, large language models exhibit grammatical errors and logical inconsistencies when generating domain-specific languages.
We employ a method that incorporates embedded vector data for construction and constraint definition, task decomposition, code generation, compliance detection and optimization. Combined with a large language model and syntax checking and correction techniques, we generate code that conforms to the DSL syntax specification through a tree-like reasoning self-consistency task parsing framework with a variable temperature coefficient and dense retrieval optimization.
It significantly improves the efficiency and reliability of autonomous driving simulation scenario generation, reduces the need for manual intervention, enhances the quality and success rate of generated code, and ensures the efficiency and accuracy of DSL code.
Smart Images

Figure CN121657993A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to a code generation method using programming languages, and more particularly to a low-resource domain-specific language (DSL) generation method for autonomous driving simulation scenarios. Background Technology
[0002] The rapid development of autonomous driving technology has made simulation testing increasingly important in the R&D process, especially in using virtual environments to simulate complex and high-risk traffic scenarios, which can effectively verify the performance and safety of autonomous driving algorithms. This approach not only significantly reduces the risks and costs of real-world testing but also provides a flexible and efficient early-stage testing environment for algorithm development and optimization. Traditional scene generation relies heavily on programming, but this method suffers from several drawbacks: high learning costs, low development efficiency, and poor scene transferability. These issues necessitate significant manual intervention in the generation and debugging of complex scenes, increasing the development burden and limiting the scalability and automation level of autonomous driving simulation testing.
[0003] Significant breakthroughs have been achieved in code generation and semantic understanding thanks to advancements in Large Language Model (LLM) technology. Represented by deep learning-based Transformer architecture models (such as GPT and CodeT5), these models can generate code using natural language processing techniques and further enhance the accuracy and semantic understanding capabilities of code generation through domain-specific fine-tuning. Furthermore, the application of syntactic constraint mechanisms and static code analysis tools can effectively correct errors in the generated code, significantly improving the quality and pass rate of code generation. Against this backdrop, Domain-Specific Language (DSL) generation and correction technologies show broad application prospects: 3D scene domain code generation technologies can quickly and efficiently generate target DSLs for the automated construction of complex autonomous driving simulation scenarios; by incorporating error correction mechanisms, the correctness and reliability of generated code can be significantly improved, providing strong technical support for the automated generation and debugging of complex scenarios.
[0004] However, current technologies still face multiple difficulties and challenges. Because large language models cannot directly access domain-specific patterns and grammatical structures during pre-training, their performance in processing DSLs is poor. The generated DSLs have low compilation success rates and often contain grammatical errors and logical inconsistencies. Therefore, how to efficiently generate DSLs under low resource constraints has become a key research focus and challenge in this field. Summary of the Invention
[0005] The purpose of this invention is to provide a low-resource domain-specific language generation method for autonomous driving simulation scenarios. This method automatically completes Scenic generation, syntax checking and correction, and retrieval optimization by combining a large language model, thereby improving the quality and success rate of the generated code and making it suitable for the construction of autonomous driving simulation scenarios.
[0006] To achieve the above objectives, the present invention adopts the following technical solution:
[0007] A low-resource domain-specific language generation method for autonomous driving simulation scenarios includes the following steps:
[0008] Step 1: Embedded Vector Data Construction and Constraint Definition: Use a word segmenter to semantically segment DSL documents related to the target scene to construct an embedded vector database; at the same time, predefine the structured data pattern and its node specifications to describe the task.
[0009] Step 2, Task Decomposition: Based on the embedded vector database, a tree-structured reasoning self-consistency task parsing framework with a variable temperature coefficient is used to parse the input natural language instructions sequentially to generate a structured set of atomic function instructions; the task parsing process includes multi-path generation, dual self-consistency verification and pruning, and convergence decision-making.
[0010] Step 3, Code Generation: Convert the atomic function instruction set into a search key, and use a dense searcher to retrieve the optimal syntax template from the embedded vector database; use the optimal syntax template as the context information for generation, and decouple it into a static skeleton and dynamic parameters; based on the context information, static skeleton, and dynamic parameters, use a large language model to generate code that conforms to the DSL syntax specification.
[0011] Step 4, Compliance Detection and Optimization: Use the compiler to perform compliance checks on the code generated in Step 3 that conforms to the DSL syntax specification, and generate a correction log; the correction log records at least the atomic function instruction set that caused the compliance error and its corresponding correct syntax template;
[0012] Step 5: Retrieval Optimization: Based on the correction log, optimize the dense retrieval used in Step 3 by combining contrastive learning and adversarial training.
[0013] Furthermore, the specific steps for constructing the embedded vector database in step 1 include:
[0014] Use a word segmenter to perform semantic analysis on DSL documents related to the target scene, deconstructing them into multiple independent semantic units;
[0015] Each semantic unit in the step is transformed into an embedding vector representing its semantic features;
[0016] The resulting embedding vectors are stored in an embedding vector database and an index is built to establish a mapping relationship between the embedding vectors and the content in the original DSL document.
[0017] Furthermore, the predefined structured data schema in step 1 is a JSON node structure based on the DSL definition, and the node specification includes JSON Schema validation rules for the JSON node structure.
[0018] Furthermore, the multi-path generation in step 2 specifically involves:
[0019] The large language model is used to predict the probability that the input natural language instruction belongs to each category in a predefined set of semantic categories, and the task entropy is calculated based on the probability.
[0020] When the task entropy exceeds a preset threshold, N parallel parsing paths are dynamically determined based on the task entropy value.
[0021] Assign an independent dynamic temperature coefficient to the large language model that generates each parsing path, so as to generate N candidate paths with outputs in the form of structured quadruples.
[0022] Furthermore, the dual self-consistency verification and pruning in step 2 specifically involve:
[0023] Dual self-consistency verification refers to performing syntactic self-consistency and logical self-consistency verification on candidate paths, and calculating a comprehensive self-consistency score for each verified path. The syntactic self-consistency verification method involves mapping the structured quadruples output by each candidate path to a predefined structured data schema, and verifying field types and hierarchical relationships according to the node specifications. The logical self-consistency verification method involves defining the vehicle state, traffic participant state, and path relationships, and constructing a scene state transition matrix; the scene state transition matrix is then used to verify the logical self-consistency of the candidate paths.
[0024] Pruning: Only retain candidate paths that pass both syntactic and logical consistency checks.
[0025] Furthermore, the convergence decision in step 2 specifically involves:
[0026] For the verified candidate paths, a weighted voting mechanism combined with a reinforcement learning model is adopted. The weight of each path in the voting is dynamically adjusted according to the dynamic temperature coefficient assigned to each path. The path with the highest self-consistency score is selected as the final task list output to generate the structured atomic function instruction set.
[0027] Furthermore, the process of generating code conforming to the DSL syntax specification using the large language model in step 3 is achieved by integrating a hybrid generation strategy, a Logits masking mechanism, and a decoding control strategy; wherein:
[0028] The hybrid generation strategy is as follows: a static rule set is constructed based on a static skeleton and DSL syntax specifications; a dynamic extension set is constructed based on dynamic parameters and context information; and the generation probability of the large language model at each time step is calculated as a weighted sum of the first probability corresponding to the static rule set and the second probability corresponding to the dynamic extension set; the formula for calculating the weighted sum is:
[0029] ;
[0030] in, The probability of mixed generation; c is the first probability based on the static rule set; γ is the second probability based on the dynamically expanded set; γ is the balance coefficient, γ∈ [0,1], and the balance coefficient γ is dynamically adjusted according to different stages of code generation;
[0031] The Logits masking mechanism is as follows: a set of valid tokens is constructed according to preset DSL syntax rules and JSON Schema constraints; the Logits value of tokens not in the set of valid tokens is set to negative infinity;
[0032] The decoding control strategy is as follows: during the generation of token sequences by the pre-trained code generation model, the structural conformity between the generated token sequences and the preset syntax template is monitored in real time. When a structural deviation is detected, the preset syntax template is dynamically adjusted according to the degree of deviation, and the adjusted template is used to constrain and guide the subsequent generation probability distribution of the model, thereby achieving continuous correction of the high-level structure of the code.
[0033] Furthermore, the method for dynamically adjusting the balance coefficient γ according to different stages of code generation is as follows: when generating the main code branch, the first probability is given a higher weight, and when filling in the parameters, the weight of the second probability is increased.
[0034] Furthermore, the implementation method of step 5 is as follows:
[0035] Construct the training dataset: Use the set of atomic function instructions for correcting compliance errors in the log as query text, match a correct syntax template for each query text as positive example document text, and collect one or more incorrect syntax templates as negative example document text;
[0036] Constructing the total loss function: The total loss function consists of a weighted sum of two losses, namely, a contrast loss based on the temperature coefficient and an adversarial loss; the formula for calculating the total loss function is as follows:
[0037] ;
[0038] in, Indicates the total loss. Indicates comparative loss; Indicates resistance to loss; Indicates the preset hyperparameters;
[0039] The formula for calculating the contrast loss is:
[0040] ;
[0041] in, Indicates query embedding. This represents text embedding, and sim() represents cosine similarity. This represents the dynamic value of the temperature coefficient as a function of the training round t. Indicates the attenuation rate. Indicates the initial value of the temperature coefficient;
[0042] The formula for calculating the adversarial loss is as follows:
[0043] ;
[0044] in, This indicates the generated fuzzy instructions. This indicates the injection of counter-perturbations. Indicates the perturbation constraint weight. This represents the sample feature extraction function.
[0045] The encoder of the dense retrieval is iteratively trained using the training dataset and the total loss function to optimize the model parameters of the dense retrieval.
[0046] This invention provides a low-resource domain-specific language (DSL) generation method for autonomous driving simulation scenarios. By combining a large language model with a target DSL language, a complete process from DSL code generation to error identification and automatic correction is constructed. Specifically, this method utilizes a tree-structured reasoning self-consistency task parsing framework based on a variable temperature coefficient to parse natural language instructions, obtaining an atomic function instruction set. DSL code is then generated based on this atomic function instruction set. During DSL code generation, by integrating a hybrid generation strategy, a Logits masking mechanism, and a decoding control strategy, effective constraint decoding is achieved, ensuring the efficiency and accuracy of DSL code generation, thereby reducing the need for manual intervention and lowering development costs. In the self-correction and density-level retrieval optimization steps, a scalable and robust retrieval optimization system is constructed through a closed-loop feedback mechanism combined with an introduced temperature coefficient contrast loss dynamic adjustment mechanism, providing core support for the DSL generation system. Applying this invention to the field of autonomous driving simulation scenarios can significantly improve the efficiency and reliability of automated simulation scenario construction. Attached Figure Description
[0048] Figure 1 This is a flowchart of a low-resource domain-specific language generation method for autonomous driving simulation scenarios;
[0049] Figure 2 This is the middleware JSON Schema structure generated from a complete scenario in Scenic as shown in the example;
[0050] Figure 3 This is a method for implementing unstructured descriptions of natural language input and simulation scene output provided in the embodiments. Detailed Implementation
[0051] The technical solution of the present invention will be described in detail below with reference to the accompanying drawings and embodiments.
[0052] This invention provides a low-resource domain-specific language (DSL) generation method for autonomous driving simulation scenarios, referred to as the DSL generation model. This method is primarily deployed on computer equipment, interacting with users such as engineers to acquire natural language input and outputting a domain-specific language (DSL) program that conforms to the intended purpose. In application, this invention is suitable for the automated generation of autonomous driving simulation scenarios. By efficiently generating and refining DSLs, it enables the rapid construction of complex autonomous driving simulation environments. In the following description, the Scenic language is selected as the target DSL to detail its implementation process.
[0053] like Figure 1 As shown, a low-resource domain-specific language generation method for autonomous driving simulation scenarios includes the following steps:
[0054] Step 1: Embedded Vector Data Construction and Constraint Definition: Use a word segmenter to semantically segment DSL documents related to the target scene to construct an embedded vector database; at the same time, predefine the structured data pattern and its node specifications to describe the task.
[0055] Step 2, Task Decomposition: Based on the embedded vector database, a tree-structured reasoning self-consistency task parsing framework with a variable temperature coefficient is used to parse the input natural language instructions to generate a structured set of atomic function instructions; the task parsing includes multi-path generation, dual self-consistency verification and pruning, and convergence decision-making.
[0056] Step 3, Code Generation: Convert the atomic function instruction set into a search key, and use a dense searcher to retrieve the optimal syntax template from the embedded vector database; use the optimal syntax template as the context information for generation, and decouple it into a static skeleton and dynamic parameters; based on the context information, static skeleton, and dynamic parameters, use a large language model to generate code that conforms to the DSL syntax specification.
[0057] Step 4, Compliance Detection and Optimization: Use the compiler to perform compliance checks on the code generated in Step 3 that conforms to the DSL syntax specification, and generate a correction log; the correction log records at least the atomic function instruction set that caused the compliance error and its corresponding correct syntax template;
[0058] Step 5: Retrieval Optimization: Based on the correction log, optimize the dense retrieval used in Step 3 by combining contrastive learning and adversarial training.
[0059] Steps 1 to 3 constitute the DSL generation part. This part covers the preliminary preparations and implementation methods for the initial generation of the DSL, and is the core part of realizing the low-resource domain-specific language generation method in this embodiment. It includes document segmentation and database construction, task decomposition and parsing, and information retrieval constraint generation. Figure 1 Steps S11 to S14. Steps 4 and 5 constitute the self-correction and retrieval optimizer section, corresponding to... Figure 1 S21 to S24 will be explained in detail below.
[0060] As an example of the present invention, step 1 is specifically implemented as follows:
[0061] The LLaMa-13B large language model possesses natural language parsing capabilities, enabling it to accurately decompose continuous text into discrete word units based on the grammatical rules and semantic features of technical documents. In this example, the LLaMa-13B model is used to perform word segmentation on technical documents in the Scenic domain, generating multiple discrete word units. Subsequently, based on the inherent structure of Scenic domain technical documents, the word units are organized into structured data pairs. Each data pair associates a descriptive semantic unit with an objective function, and the format of the data pair is defined as <semantic unit, objective function>, thus forming a clean, semantically rich, structured corpus.
[0062] Each word is transformed into a high-dimensional vector, or embedding vector, using a pre-trained word embedding model. These vectors effectively capture the semantic features and contextual relationships of words, thus accurately representing their core semantics. The generated set of embedding vectors is stored in a vector database built on Pinecone, a high-performance vector database designed for machine learning applications and suitable for fast similarity searches of large-scale vectors.
[0063] In this vector database, a key-value pair storage structure is constructed, with the embedded vector as the key and the corresponding original function name and code snippet as the value. An index is created for this structured corpus, configured with dimensional parameters consistent with the output dimensions of the pre-trained word embedding model, and cosine similarity is chosen as the distance metric. These operations establish a mapping relationship from the embedded vectors to the original Scenic domain technical document content, laying a solid foundation for subsequent efficient semantic retrieval.
[0064] In addition, this example predefines a structured data schema and its node specifications for describing tasks; the predefined structured data schema is a JSON node structure based on a DSL, and the node specifications include JSONSchema validation rules for the JSON node structure.
[0065] This step transforms unstructured technical documents into a structured knowledge base that can be efficiently retrieved, and builds an embedded vector database based on Pinecone domain knowledge, providing strong data support for the automated generation of autonomous driving simulation scenarios.
[0066] As an example of the present invention, step 2 is implemented as follows:
[0067] Task entropy is defined as an indicator used to quantify the ambiguity of natural language instructions, and its calculation formula is as follows:
[0068] ;
[0069] Where C is a predefined set of semantic categories; This refers to predicting the probability that an input instruction q belongs to a predefined set of semantic categories C using a lightweight classification model; the higher the task entropy value, the more ambiguous the instruction.
[0070] A tree-structured reasoning framework with a variable temperature coefficient is used to parse the input natural language instructions. This example of task parsing includes multi-path generation, dual self-consistency verification and pruning, and convergence decision-making. Specifically:
[0071] The specific implementation method for multi-path generation is as follows:
[0072] The large language model is used to predict the probability that an input natural language instruction belongs to each category in a predefined set of semantic categories, and the task entropy is calculated based on this probability. When task entropy Exceeding the set task entropy threshold At that time, N parallel parsing paths are dynamically determined based on the task entropy value; in this example, the task entropy threshold is... .
[0073] Based on the task entropy mapping temperature coefficient, the function is designed as follows:
[0074] (2);
[0075] in, This is the initial maximum temperature, used for fuzzy tasks; Used as a base temperature for deterministic tasks; 0.01 / epoch is the time decay coefficient; t is the number of iterations.
[0076] In the tree framework, an independent temperature coefficient is assigned to each path i. Introducing confidence gain coefficient Improving the certainty of high-confidence paths:
[0077] (3);
[0078] Among them, usually These paths were determined through experimental verification. These paths will be considered as independent virtual experts in subsequent steps.
[0079] Based on the independent temperature coefficient assigned to each path Each path is treated as a virtual expert node. Each expert, constrained by the Scenic language syntax rule set, determines their corresponding temperature coefficient. The indicated exploration intensity independently decomposes instructions into core semantic dimensions such as Object, Behavior, Spawn, and Params, generating candidate atomic instruction sets. Temperature coefficient. The higher the value, the stronger the randomness and exploratory nature of the expert's decomposition process; conversely, the lower the value, the more likely they are to converge to a deterministic result.
[0080] The specific implementation method of dual self-consistency verification and pruning is as follows:
[0081] Dual self-consistency verification refers to performing syntactic self-consistency and logical self-consistency checks on candidate paths, and calculating a comprehensive self-consistency score for each path that passes the verification.
[0082] The syntax self-consistency verification method is as follows: map the structured quadruples output by each candidate path to a predefined structured data pattern, and verify the field types and hierarchical relationships according to the node specifications; ensure that each candidate atomic instruction conforms to the syntax specifications and hierarchical dependencies of Scenic DSL.
[0083] The logical consistency verification method is as follows: define the vehicle state, traffic participant state, and path relationship, and construct a scene state transition matrix; use the scene state transition matrix to verify the logical consistency of the candidate path. The vehicle state, traffic participant state, and path relationship defined in this example are as follows:
[0084] Vehicle status (S): S1: Go straight; S2: Turn left; S3: Turn right; S4: Stop;
[0085] Traffic participant status (O) (single advagent): O1: Straight ahead; O2: Left turn; O3: Right turn; O4: Stop;
[0086] Path relationships (R): R1: Paths in the same direction; R2: Paths intersect; R3: Paths in opposite directions.
[0087] An example of the constructed scene state transition matrix is shown in Table 1. The scene state transition matrix defines all reasonable state transition rules.
[0088] Table 1: Example of Scene State Transition Matrix
[0089] t t* (S1,O1,R2) (S1,O2,R2) (S2,O1,R2) (S4,O3,R1) .. (S1,O1,R2) 1 1 0 0 ... (S1,O2,R2) 1 1 1 0 ... (S2,O1,R2) 0 1 1 1 ... (S4,O3,R1) 0 0 1 1 ... ... ... ... ... ... ..
[0090] Pruning: The state transitions described by the instructions generated by each path are marked as 1 in the matrix. 1 indicates that it is valid, thereby eliminating paths with logical conflicts and retaining only candidate paths that pass the dual verification of syntactic consistency and logical consistency.
[0091] The specific implementation method of convergent decision-making is as follows:
[0092] Dynamic Weighted Voting and Reinforcement Learning Adjustment: A dynamic weighted majority voting mechanism is introduced, combined with feedback from a reinforcement learning model, to aggregate and calculate all double-validated candidate paths. The initial weight of each path is determined by its temperature coefficient at the time of generation and its self-consistency verification result. During the voting process, the reinforcement learning model analyzes the real-time characteristics of all candidate paths, including the state constituted by the temperature coefficient and self-consistency score of the path's generation, and outputs adjustments to the weights of each path. The optimization of this reinforcement learning model is achieved by processing a large number of task samples during runtime, using the success of generating compliant code as a positive or negative reward for the branch result that wins a single vote, thereby learning how to allocate weights based on the overall state of all candidate paths to maximize the success rate of code generation tasks. In this example, priority is given to selecting subtask structures supported by more than ⌈N / 2⌉ + 1 weighted paths to form intermediate parsing results with maximum structural consistency. By integrating the dynamic weighting and reinforcement learning voting mechanism, the robustness and accuracy of natural language parsing are significantly improved.
[0093] Structured parsing and function mapping: The intermediate parsing result, selected through dynamic weighted voting, is decomposed into quadruples containing four dimensions: object, behavior, generation conditions, and parameter range. Each structured semantic component is extracted from each dimension of the quadruples and mapped to the corresponding function-level generation instruction. In this example, the semantics of the participating entities under the object dimension of the quadruples are parsed into DSL expressions, including participating elements such as the vehicle ego, the adversarial unit AdvAgent, and the occluder Blocker. Complete semantic decomposition is achieved through the scenic intermediate structured expression.
[0094] JSON Schema Constraints and Convergence: Based on the obtained core semantic components and the intermediate structured representation of Scenic, a tree-shaped intermediate structured semantic representation is constructed and used as the input template for the DSL generation method. This template uses scenario as the root node and expands layer by layer to include core components such as metadata, road network configuration, object definition, behavioral logic, and behavior tree, ensuring good semantic alignment and generation constraint capabilities.
[0095] During the decision convergence phase, the JSON Schema parameters of this tree structure are populated to ensure its logical organization follows specific specifications (e.g., metadata contains simulator parameters, road network binds to road generators, etc.). By mapping intermediate parsing results to this tree structure, it enhances the expressiveness of logical dependencies while maintaining DSL syntax consistency, thereby significantly improving the accuracy and execution efficiency of subsequent code generation. Figure 2This demonstrates a complete scenario generation middleware JSON Schema structure diagram from Scenic, created by... Figure 2 As can be seen, the middleware JSON Schema structure takes scenario as the root node and recursively organizes multiple modules (metadata, road_network, objects, behaviors, etc.) downwards. Each module consists of several nested sub-attributes, which demonstrate the abstract representation of instructions in the DSL language generation process at the semantic level and the structural leading characteristics.
[0096] Finally, the candidate paths processed through the above steps converge to a unique output data structure that conforms to all preset specifications, namely the final atomic function instruction set. This final instruction set represents the path with the highest self-consistency score and best meets the task requirements under the current model's understanding.
[0097] This step precisely parses the user's natural language commands into a structured, machine-executable set of structured atomic function instructions, thereby achieving high-precision understanding and semantic decomposition of complex autonomous driving scenarios. This provides a standardized and logically consistent input foundation for the subsequent DSL instruction construction in the code generation stage.
[0098] As an example of the present invention, step 3 is implemented as follows:
[0099] Retrieval Enhancement Generation: Based on a predefined JSON Schema, the structured atomic function instructions obtained in step 2 are mapped to structured search keys. For example, the instruction to set a reference point is mapped to a JSON object containing an action field. This example uses the ColBERTv2 dense retrieval model as the retrieval tool, which calculates the similarity between the search key and all code snippets in the embedding vector database constructed in step 1. A top-K search is performed to obtain a candidate snippet set; in this embodiment, k=5. The obtained candidate snippet set is further filtered using the ColBERTv2 dense retrieval model to determine the top-1 template, which is then used as the optimal syntax template for the current task. The optimal syntax template is merged with the retrieved candidate code snippets to form the context information required for subsequent generation tasks.
[0100] Decoupling Static Skeleton and Dynamic Parameters to Generate DSL Code: This step introduces a dynamic module constraint decoding mechanism. The dynamic module constraint decoding mechanism first decouples the optimal syntax template determined in step 3 into two parts: a static code skeleton and dynamic parameter variables. The static code skeleton is composed of predefined fixed parameters in the JSON Schema and the syntax structure of the optimal syntax template. This syntax structure includes function signatures, parentheses, and quotation marks; it directly inherits from the official technical documentation specifications to ensure that the basic semantics and structure of the generated code conform to the specifications. The dynamic parameter variables are generated by the CodeT5+ language model under multiple strategies. These strategies include a hybrid generation strategy, a Logits masking mechanism, and a decoding control strategy to ensure parameter flexibility and context relevance. Specifically:
[0101] The hybrid generation strategy is as follows: a static rule set is constructed based on a static skeleton and DSL syntax specifications; a dynamic extension set is constructed based on dynamic parameters and context information of the current task; and the generation probability of the large language model at each time step is calculated as a weighted sum of the first probability corresponding to the static rule set and the second probability corresponding to the dynamic extension set; the formula for calculating the weighted sum is:
[0102] ;
[0103] in, The probability of mixed generation; c is the first probability based on the static rule set; The first probability is the second probability based on the dynamically expanded set; γ is the balance coefficient, γ∈ [0,1], and the balance coefficient γ is dynamically adjusted according to different stages of code generation. In this example, the method for dynamically adjusting the balance coefficient γ according to different stages of code generation is as follows: when generating the code backbone, the first probability is given a higher weight, and when filling in the parameters, the weight of the second probability is increased.
[0104] The Logits masking mechanism is as follows: a set of legal tokens is constructed based on preset DSL syntax rules and JSON Schema constraints; the Logits value of tokens not in the set of legal tokens is set to negative infinity; so that the model selects outputs only from the set of legal tokens, thereby achieving hard constraints on the underlying syntax of the generated tokens.
[0105] The decoding control strategy is as follows: During the token sequence generation process of the pre-trained code generation model (CodeT5+ is used in this example), the structural conformity between the generated token sequence and the preset syntax template is monitored in real time. When a structural deviation is detected, the preset syntax template is dynamically adjusted according to the degree of deviation, and the adjusted template is used to constrain and guide the subsequent generation probability distribution of the model, thereby achieving continuous correction of the high-level structure of the code. In this example, the degree of deviation is obtained by calculating the matching rate between the keywords of the generated sequence and the preset template.
[0106] Furthermore, to further ensure the completeness and compliance of the generated code, this example designs a cross-document collaboration mechanism to address potential field omissions in the main technical documentation, such as the lack of explicit handling of "None" values in the behavior tree definition. This mechanism retrieves relevant missing information from other trusted knowledge sources in the same domain and verifies the consistency between the retrieved information and the syntax tree structure of the current code.
[0107] If the verification passes, a compliant segment is selected to fill the missing part; if the verification fails, a backup processing strategy is activated. The backup processing strategy includes, but is not limited to: ignoring the missing field, using a predefined default value for filling, or logging the issue and submitting it to the user for adjudication.
[0108] It should be noted that this step aims to achieve accurate, compliant, and flexible code generation through standardized, multi-layered constraints. Its goal is to ensure that the code not only strictly adheres to syntax and structural requirements but also adapts to specific contexts and changing scenarios, effectively handling anomalies such as missing information, ultimately outputting high-quality, directly applicable code. Query understanding is a crucial step in this process. This example uses the ColBERTv2 model as a density-level retrieval tool for intensive searching to filter out the most relevant context fragments, i.e., the optimal relevant fragments, which are most relevant to the user's intent. To improve generation quality, this example constructs prompts through few-shot learning. These prompts integrate the retrieved context documents, the user's parameter intent, and reference function fragments. During DSL code generation, the top-1 function template with the highest confidence in the search results is selected as the static skeleton, while the variable part is generated and adjusted in real time according to the search pattern rules. Through the synergistic effect of the above search enhancement and multiple constraint mechanisms, the generated code not only strictly adheres to DSL syntax specifications but also supports the flexible generation of parameters for complex scenarios with the help of dynamic expansion sets. This achieves an optimal balance between syntactic compliance and semantic adaptability, effectively eliminating type errors and logical conflicts, and fully meeting the needs of diverse scenarios. Ultimately, it successfully converts unstructured natural language commands input by users into target Scenic files.
[0109] As an example of the present invention, step 4 is implemented as follows:
[0110] Compilation checks and error handling: The Scenic compiler is used to perform compilation checks on the generated code. Based on the check results: if the code compiles successfully, it is output directly; if compilation fails, the code segment containing the error and its corresponding compilation error information are passed to the error correction and logging steps for automatic correction.
[0111] Error Correction and Logging: An error correction model is used to analyze and correct code that fails to compile. A large language model parses compilation error information, focusing on correcting semantically abstract parts of the code, such as scenario definitions and constraints. The entire error correction process is recorded. Once the code is successfully corrected and compiles, the system generates a pkl file to store a correction log consisting of a triplet of "correct code, original incorrect code, and JSON Schema node parameters."
[0112] Loop Verification and Number of Attempts Control: After completing one error correction attempt, the system returns to the compilation and error handling steps to recompile and verify the corrected code. Simultaneously, a counter is activated to limit the maximum number of error correction attempts. In this embodiment, the upper limit is set to three. Once this limit is reached, the code generation task is considered failed, thus avoiding a logical infinite loop caused by continuous compilation errors.
[0113] Constructing a comparative training dataset: A comparative training dataset is constructed based on the saved correction logs. Specifically, two key data pairs are extracted from the logs and formed: one is a negative pair consisting of "JSON Schema node parameters and error codes", and the other is a positive pair consisting of "JSON Schema node parameters and correct codes".
[0114] Retrieval Optimization and Output: The system uses a training dataset to fine-tune and optimize a pre-trained base retrieval tool. In this embodiment, the base retrieval tool is ColBERTv2, which learns to more accurately match correct code patterns by distinguishing between positive and negative pairs. Positive pairs refer to JSON Schema node parameters and correct code; negative pairs refer to JSON Schema node parameters and incorrect code. After optimization, the fine-tuned retrieval tool retrieves the Top-K reference documents related to both incorrect and correct code from the original database. This results in two core sets: a set of "JSON Schema node parameters and incorrect documents" and a set of "JSON Schema node parameters and correct documents." These outputs will support subsequent loss function design and weakly supervised learning.
[0115] As an example of the present invention, step 5 is implemented as follows:
[0116] Construct the training dataset: Use the set of atomic function instructions for correcting compliance errors in the log as query text, match a correct syntax template for each query text as positive example document text, and collect one or more incorrect syntax templates as negative example document text;
[0117] Constructing the total loss function: The total loss function consists of a weighted sum of two losses, namely, a contrast loss based on the temperature coefficient and an adversarial loss; the formula for calculating the total loss function is as follows:
[0118] ;
[0119] in, Indicates the total loss. Indicates comparative loss; Indicates resistance to loss; Indicates the preset hyperparameters;
[0120] The formula for calculating the contrast loss is:
[0121] ;
[0122] in, Indicates query embedding. This represents text embedding, and sim() represents cosine similarity. This represents the dynamic value of the temperature coefficient as a function of the training round t. Indicates the attenuation rate. Indicates the initial value of the temperature coefficient;
[0123] The formula for calculating the adversarial loss is as follows:
[0124] ;
[0125] in, This indicates the generated fuzzy instructions. This indicates the injection of counter-perturbations. Indicates the perturbation constraint weight. This represents the sample feature extraction function.
[0126] The encoder of the dense retrieval is iteratively trained using the training dataset and the total loss function to optimize the model parameters of the dense retrieval.
[0127] The Scenic language is specifically designed to describe various dynamic and static elements and their interactions in autonomous driving scenarios, enabling accurate simulation of real-world traffic conditions. Figure 3 This demonstrates a method for implementing unstructured descriptions of natural language input and simulated scenario output. From Figure 3 As can be seen, the present invention can automate the entire process from scene description to code generation to error correction, significantly improving the development efficiency and accuracy of autonomous driving simulation scenarios, and providing strong technical support for the research and development and testing of autonomous driving technology.
[0128] The above examples are only used to illustrate the technical solutions and preferred strategies of the present invention, and are not intended to limit the methods thereof.
Claims
1. A low-resource, domain-specific language generation method for autonomous driving simulation scenarios, characterized in that, Includes the following steps: Step 1: Embedded Vector Data Construction and Constraint Definition: Use a word segmenter to semantically segment DSL documents related to the target scene to construct an embedded vector database; at the same time, predefine the structured data pattern and its node specifications to describe the task. Step 2, Task Decomposition: Based on the embedded vector database, a tree-structured reasoning self-consistency task parsing framework with a variable temperature coefficient is used to parse the input natural language instructions sequentially to generate a structured set of atomic function instructions; the task parsing process includes multi-path generation, dual self-consistency verification and pruning, and convergence decision-making. Step 3, Code Generation: Convert the atomic function instruction set into a search key, and use a dense searcher to retrieve the optimal syntax template from the embedded vector database; use the optimal syntax template as the context information for generation, and decouple it into a static skeleton and dynamic parameters; based on the context information, static skeleton, and dynamic parameters, use a language model to generate code that conforms to the DSL syntax specification. Step 4, Compliance Detection and Optimization: Use the compiler to perform compliance checks on the code generated in Step 3 that conforms to the DSL syntax specification, and generate a correction log; the correction log records at least the atomic function instruction set that caused the compliance error and its corresponding correct syntax template; Step 5: Retrieval Optimization: Based on the correction log, optimize the dense retrieval used in Step 3 by combining contrastive learning and adversarial training.
2. The method according to claim 1, characterized in that, The specific steps for constructing the embedded vector database in step 1 include: Use a word segmenter to perform semantic analysis on DSL documents related to the target scene, deconstructing them into multiple independent semantic units; Each semantic unit in the step is transformed into an embedding vector representing its semantic features; The resulting embedding vectors are stored in an embedding vector database and an index is built to establish a mapping relationship between the embedding vectors and the content in the original DSL document.
3. The method according to claim 2, characterized in that, The predefined structured data schema in step 1 is a JSON node structure based on a DSL definition, and the node specification includes JSON Schema validation rules for the JSON node structure.
4. The method according to claim 1, characterized in that, The multi-path generation in step 2 specifically involves: The large language model is used to predict the probability that the input natural language instruction belongs to each category in a predefined set of semantic categories, and the task entropy is calculated based on the probability. When the task entropy exceeds a preset threshold, N parallel parsing paths are dynamically determined based on the task entropy value. Assign an independent dynamic temperature coefficient to the large language model that generates each parsing path, so as to generate N candidate paths with outputs in the form of structured quadruples.
5. The method according to claim 4, characterized in that, The dual self-consistency verification and pruning in step 2 are specifically as follows: Dual self-consistency verification refers to performing syntactic self-consistency and logical self-consistency verification on candidate paths, and calculating a comprehensive self-consistency score for each verified path. The syntactic self-consistency verification method involves mapping the structured quadruples output by each candidate path to a predefined structured data schema, and verifying field types and hierarchical relationships according to the node specifications. The logical self-consistency verification method involves defining the vehicle state, traffic participant state, and path relationships, and constructing a scene state transition matrix; the scene state transition matrix is then used to verify the logical self-consistency of the candidate paths. Pruning: Only retain candidate paths that pass both syntactic and logical consistency checks.
6. The method according to claim 5, characterized in that, The convergence decision in step 2 is specifically as follows: For the verified candidate paths, a weighted voting mechanism combined with a reinforcement learning model is adopted. The weight of each path in the voting is dynamically adjusted according to the dynamic temperature coefficient assigned to each path. The path with the highest self-consistency score is selected as the final task list output to generate the structured atomic function instruction set.
7. The method according to claim 1, characterized in that, Step 3, which involves generating code conforming to the DSL syntax specification using a large language model, is achieved by integrating a hybrid generation strategy, a Logits masking mechanism, and a decoding control strategy; wherein: The hybrid generation strategy is as follows: a static rule set is constructed based on a static skeleton and DSL syntax specifications; a dynamic extension set is constructed based on dynamic parameters and context information; and the generation probability of the large language model at each time step is calculated as a weighted sum of the first probability corresponding to the static rule set and the second probability corresponding to the dynamic extension set; the formula for calculating the weighted sum is: ; in, The probability of mixed generation; The first probability is based on a static rule set; γ is the second probability based on the dynamically expanded set; γ is the balance coefficient, γ∈ [0,1], and the balance coefficient γ is dynamically adjusted according to different stages of code generation; The Logits masking mechanism is as follows: a set of valid tokens is constructed according to preset DSL syntax rules and JSON Schema constraints; the Logits value of tokens not in the set of valid tokens is set to negative infinity; The decoding control strategy is as follows: during the generation of token sequences by the pre-trained code generation model, the structural conformity between the generated token sequences and the preset syntax template is monitored in real time. When a structural deviation is detected, the preset syntax template is dynamically adjusted according to the degree of deviation, and the adjusted template is used to constrain and guide the subsequent generation probability distribution of the model, thereby achieving continuous correction of the high-level structure of the code.
8. The method according to claim 7, characterized in that, The method for dynamically adjusting the balance coefficient γ according to different stages of code generation is as follows: when generating the main code branch, the first probability is given a higher weight, and when filling in the parameters, the weight of the second probability is increased.
9. The method according to claim 1, characterized in that, The implementation method of step 5 is as follows: Construct the training dataset: Use the set of atomic function instructions for correcting compliance errors in the log as query text, match a correct syntax template for each query text as positive example document text, and collect one or more incorrect syntax templates as negative example document text; Constructing the total loss function: The total loss function consists of a weighted sum of two losses, namely, a contrast loss based on the temperature coefficient and an adversarial loss; the formula for calculating the total loss function is as follows: ; in, Indicates the total loss. Indicates comparative loss; Indicates resistance to loss; Indicates the preset hyperparameters; The formula for calculating the contrast loss is: ; in, Indicates query embedding. This represents text embedding, and sim() represents cosine similarity. This represents the dynamic value of the temperature coefficient as a function of the training round t. Indicates the attenuation rate. Indicates the initial value of the temperature coefficient; The formula for calculating the adversarial loss is as follows: ; in, This indicates the generated fuzzy instructions. This indicates the injection of counter-perturbations. Indicates the perturbation constraint weight. Represents the sample feature extraction function; The encoder of the dense retrieval is iteratively trained using the training dataset and the total loss function to optimize the model parameters of the dense retrieval.