Operator code generation method and system based on large model driving and multi-agent cooperation mechanism

The operator code generation method driven by a large model and a multi-agent collaboration mechanism solves the problems of insufficient code generation flexibility and performance in existing technologies, and achieves efficient and automated optimized code generation to meet specific optimization objectives.

CN121255173BActive Publication Date: 2026-03-27HUNAN UNIV
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-12-05
Publication Date
2026-03-27

AI Technical Summary

Technical Problem

Existing technologies struggle to efficiently generate code that meets specific optimization objectives for complex operators and diverse hardware platforms. Traditional methods lack flexibility, automated tools lack adaptability, and directly using large language models to generate code suffers from high error rates and performance issues.

Method used

We adopt an operator code generation method based on a large model-driven and multi-agent collaborative mechanism. Through the collaborative work of policy identification agent, operator generation agent, compilation and testing agent and functional testing agent, we use a large language model and a hybrid expert model to analyze operator characteristics, generate and iteratively optimize code, and combine retrieval-enhanced generation technology with a dedicated testing and verification process.

Benefits of technology

It enables efficient and automated generation of high-quality operator code that meets specific optimization objectives, improving the accuracy and performance of code generation and overcoming the limitations of traditional methods and the shortcomings of existing tools.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121255173B_ABST
    Figure CN121255173B_ABST
Patent Text Reader

Abstract

The application provides an operator code generation method and system based on a large model driving and multi-agent cooperation mechanism, four types of agents, namely strategy recognition, operator generation, compilation testing and function testing, work cooperatively to realize automatic high-performance code generation. The strategy recognition agent accesses a high-performance optimization knowledge base (HPOK) by using retrieval augmented generation (RAG), analyzes the characteristics of the operator by means of a mixed expert model (MoE), and generates an optimization strategy specification oriented to a specific optimization target. The operator generation agent calls a large language model (LLM) to generate initial code, and iteratively modifies the code according to the feedback of compilation and function testing. The compilation testing and function testing agents respectively perform code compilation verification and function correctness verification to form a closed-loop error correction mechanism. Through the modular cooperation framework and dynamic knowledge retrieval, the scheme significantly improves the efficiency and quality of operator code generation for diversified hardware platforms such as CPU and GPU.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application belongs to the technical field of artificial intelligence and high-performance computing, and particularly relates to an operator code generation method and system based on large model driving and multi-agent cooperation mechanism. BACKGROUND

[0002] With the increasing demand for high-performance computing (HPC), optimizing computing operators such as matrix multiplication and convolution for specific hardware platforms (such as x86 architecture CPU, NVIDIA GPU, and domestic accelerator card) has become a key to improving system performance. Traditional operator optimization and code generation usually rely on manual experience or static rules, which is tedious and difficult to adapt to diverse hardware architectures, computing tasks, and various optimization goals (such as low latency and high throughput). Although existing automated tools (such as TVM and Halide) can generate optimized code to some extent, the identification and implementation of their optimization strategies are limited by predefined rule libraries, lacking flexibility and adaptability, especially when facing complex operators or new hardware.

[0003] In recent years, large language models (LLM) have been widely used in code generation due to their powerful natural language understanding and generation capabilities. However, there are several problems in directly using LLM to generate high-performance code: first, the lack of ability to analyze and identify optimization strategies for specific operators (such as matrix multiplication and convolution) in terms of computing intensity, memory access patterns, and data dependencies; second, the generated code may have compilation or logical errors, making it difficult to use directly; third, the context understanding ability and external knowledge of LLM are not fully utilized, resulting in low performance of the generated results and failure to meet specific optimization goals (such as low latency and high throughput). Therefore, there is an urgent need for an efficient, automated, and robust method to utilize large language models to identify optimization strategies for high-performance operators and generate high-quality code. SUMMARY

[0004] To solve the above technical problems, the present application provides an operator code generation method and system based on large model driving and multi-agent cooperation mechanism.

[0005] The technical solution adopted by the present application to solve its technical problems is:

[0006] The operator code generation method based on large model driving and multi-agent cooperation mechanism comprises the following steps:

[0007] S100: The policy recognition agent receives an initial task request containing high-performance operator reduction information, accesses the high-performance optimization knowledge base combined with search enhancement to generate RAG, analyzes the characteristics of the operator using the hybrid expert model MoE technology, identifies and generates a deterministic optimization strategy reduction for the specified optimization target, and sends the deterministic optimization strategy reduction together with the original operator reduction information to the operator generation agent;

[0008] S200: The operator generation agent generates initial optimization operator code based on the received information to achieve the specified optimization target, and sends the generated operator code to the compilation test agent;

[0009] S300: The compilation test agent uses the specified compilation tool chain to compile and test the initial operator code, and checks for compilation errors;

[0010] S400: If a compilation error is detected, the compilation error information is captured and sent back to the operator generation agent, which generates a compilation error correction prompt word based on the compilation error information, calls the large language model to regenerate the optimization operator code and returns to S300 to compile and test the regenerated optimization operator code. If the compilation is successful, the compiled code or executable file is sent to the function test agent;

[0011] S500: The function test agent retrieves relevant test cases from the test case database through RAG based on the operator reduction information, generates test driver code, performs function verification testing, and compares the actual output with the expected output;

[0012] S600: If the function test fails, capture the function test failure information and send it back to the operator generation agent, which generates a correction prompt word based on the function test failure information, calls the large language model to regenerate the optimization operator code and returns to S300; If the test passes, output the final verified optimization operator code.

[0013] Preferably, S100 includes:

[0014] S110: After receiving the operator reduction information, call the internal operator analysis and strategy recognition prompt word generator tool, which constructs the initial prompt word according to the reduction information, sets the role of the large language model LLM as "high-performance computing HPC optimization expert", and the task is to analyze the characteristics of the operator and recommend strategies; Wherein, the operator reduction information includes the function definition of the operator, the characteristics of the target hardware platform and the optimization target of the instruction;

[0015] S120: Access the high-performance optimization knowledge base HPOK using the RAG tool, which contains optimization strategy descriptions, hardware knowledge, performance models, and vectorization knowledge base of code implementation templates. Retrieve relevant knowledge fragments according to operator type and hardware platform, and inject them into the prompt to enhance the context;

[0016] S130: Use the MoE execution tool to manage multiple LLM experts, and send the enhanced prompt to the experts in parallel. Collect the candidate optimization strategy set of each expert, where the multiple LLM experts refer to different models or different configurations;

[0017] S140: Execute the strategy integration logic, and use the weighted voting mechanism or consensus mechanism to fuse the candidate strategies output by multiple large language model experts. Combine the model confidence score to filter strategies with scores exceeding the threshold to generate a structured and determined optimization strategy specification, where the optimization strategy specification includes optimization strategy name, applicable reason, and key parameter suggestion.

[0018] Preferably, S200 includes:

[0019] S210: Receive the message package from the strategy recognition agent, which contains operator specification information and determined optimization strategy specification;

[0020] S220: Call the internal strategy implementation code generation prompt generator tool, which constructs the prompt according to the received information, sets the LLM role as "HPC programmer proficient in target language", the task as strictly implementing the operator function definition according to the determined optimization strategy specification, specifies the target language, library, hardware feature utilization, and optimization target;

[0021] S230: Use the RAG tool to retrieve specific code implementation templates or best practice fragments corresponding to the strategy in the specification from the HPOK, and inject the prompt to guide the LLM to generate more standardized and efficient code;

[0022] S240: Send the finally constructed prompt to the large language model service interface to obtain the generated initial optimization operator code;

[0023] S250: Package the generated code and send it to the compilation test agent through the internal communication bus.

[0024] Preferably, S300 includes:

[0025] S310: Receive the code from the operator generation agent;

[0026] S320: According to the target hardware platform and programming language specified in the operator specification information, select and call the corresponding compilation tool chain;

[0027] S330: execute the compilation command and capture the complete compiler output, which includes standard output and standard error stream;

[0028] S340: analyze the compiler output using a compiler error parser tool to detect compilation issues, which includes syntax error, type mismatch error, undeclared / undefined identifier error, linking error, etc.

[0029] Preferably, S400 includes:

[0030] S410: if the parser of the compilation test agent detects a compilation error, extract the key error information and structure it, where the key error information includes file name, line number, column number, and specific error message text;

[0031] S420: package the structured error information into an error report and send it back to the operator generation agent through the internal communication bus;

[0032] S430: after the operator generation agent receives the error report, call its internal compilation error correction prompt word generator tool, which generates a prompt word based on the error report content, including role, task, error details, previous code snippet, and instructions;

[0033] S440: the operator generation agent sends this correction prompt word to the LLM, gets the corrected code, and then executes S300 again;

[0034] S450: if no error is detected by the compilation test agent, package the source code or executable file generated as needed along with the original operator reduction information and send it to the functional test agent through the internal communication bus.

[0035] Preferably, S500 includes:

[0036] S510: the functional test agent receives the code or executable file and the operator reduction information;

[0037] S520: use the RAG tool to access the test case database TCD and retrieve the relevant test case set according to the operator type and data shape in the operator reduction information; where the test case database contains test inputs and corresponding reference outputs for different operators, data types, sizes, and boundary conditions;

[0038] S530: call the test driver generator tool to automatically generate or configure a test driver based on the operator interface and the retrieved test cases, which can call the operator code to be tested and pass in the test input;

[0039] S540: Execute the test driver, run all selected test cases, capture the actual output of the operator code;

[0040] S550: Use the result comparator tool to compare the actual output with the expected output defined in the test cases.

[0041] Preferably, S600 includes:

[0042] S610: If the comparison finds inconsistencies, capture detailed failure information: failed test case ID or description, specific input data, actual output, expected output, and attempt to locate errors through the instrumentation log or assertion failure information;

[0043] S620: Package the structured failure information into a test failure report and send it back to the operator generation agent through the internal communication bus;

[0044] S630: After the operator generation agent receives the test failure report, it calls its internal test failure correction prompt generator tool, which generates a prompt based on the failure report content. The prompt includes the role, task, failure details, and instructions;

[0045] S640: The operator generation agent sends this correction prompt to the LLM, obtains the corrected code, and then returns to S300 to start the compilation and functional testing cycle again;

[0046] S650: If the functional test agent confirms that all test cases pass, the final optimized operator code is marked as correct and provided to the user or downstream system through the designated output interface.

[0047] Preferably, an operator code generation system based on large model driving and multi-agent collaboration mechanism is also provided, including:

[0048] A strategy recognition agent is responsible for receiving initial tasks, identifying optimization strategies using RAG and MoE technology, generating a determined optimization strategy specification, and passing the result to the operator generation agent;

[0049] An operator generation agent is responsible for generating initial code based on the strategy provided by the strategy recognition agent, and generating correction prompts based on error information feedback from the compilation test agent or functional test agent to call LLM for iterative correction;

[0050] A compilation test agent is responsible for receiving code, using a specified compiler for compilation testing, detecting compilation errors, and feeding back error information to the operator generation agent or passing the successfully compiled code to the functional test agent;

[0051] The function test intelligent agent is responsible for receiving the compiled code, obtaining the test case by using the RAG, performing the function verification test, detecting the function error, and feeding back the error information to the operator generation intelligent agent or confirming that the code is finally qualified.

[0052] The at least one knowledge base comprises a high-performance optimization knowledge base (HPOK) for storing optimization knowledge and / or a test case database for storing test cases;

[0053] The large language model service interface is called by the strategy recognition intelligent agent and the operator generation intelligent agent.

[0054] The internal communication mechanism based on the message queue or the remote process call is used to transfer information between the strategy recognition intelligent agent, the operator generation intelligent agent, the compilation test intelligent agent and the function test intelligent agent.

[0055] Preferably, the strategy recognition intelligent agent internally comprises an operator analysis and strategy recognition prompt word generator tool, an RAG tool for accessing the HPOK, and a MoE execution tool for fusing expert output; the operator generation intelligent agent internally comprises a strategy implementation code generation prompt word generator tool, a compiler error correction prompt word generator tool, and a test failure correction prompt word generator tool.

[0056] Preferably, the compilation test intelligent agent internally comprises an interface for calling a compilation tool chain corresponding to the target hardware platform, and a compiler error parser tool; the function test intelligent agent internally comprises an RAG tool for accessing the TCD, a test driver generator tool, and a result comparator tool for comparing the actual output with the expected output.

[0057] The operator code generation method and system based on the large model driving and multi-agent cooperation mechanism described above, by deploying four core intelligent agents of strategy recognition, operator generation, compilation test and function test, using the generation capability of the large language model, the retrieval enhanced generation (RAG) technology, the mixed expert model (MoE) and the special test verification process, cooperatively completes the automatic recognition of optimization strategy, the generation of high-quality optimization code and the strict iteration error correction of compilation and function test, solves the technical problems of insufficient flexibility of traditional methods, poor adaptability of existing tools, high error rate and difficult performance guarantee when directly using LLM. BRIEF DESCRIPTION OF DRAWINGS

[0058] Figure 1 The flowchart of the operator code generation method based on the large model driving and multi-agent cooperation mechanism in an embodiment of the present application;

[0059] Figure 2 The composition and cooperation sequence of the operator code generation method and system based on the large model driving and multi-agent cooperation mechanism in an embodiment of the present application. DETAILED DESCRIPTION

[0060] In order to make the technical personnel in the technical field better understand the technical solutions of the present application, the present application will be further described in detail below in combination with the drawings.

[0061] The present application aims to solve the pain points in the prior art that it is difficult to effectively generate code that meets the preset optimization target for the above complex operators and diversified hardware combinations. These preset optimization targets reflect the typical performance pursuit direction in HPC applications, and the present application is designed around one or more of the following goals:

[0062] (1) Maximize Throughput: Prioritize increasing the amount of data processed or the number of operations completed per unit of time. Suitable for large-scale data processing, offline training, etc. The system will tend to choose optimization strategies that facilitate data parallelism, pipeline parallelism, batch processing, and full utilization of computing units and memory bandwidth (such as large-scale parallelization, deep pipelining, optimized data layout).

[0063] (2) Minimize Latency: Prioritize reducing the time required for a single operator execution. Critical for applications with high real-time requirements (such as online inference, physical simulation interaction). The system will tend to choose strategies that reduce computation steps, optimize critical paths, utilize caches, and reduce synchronization overhead (such as small-grained task partitioning, efficient cache blocking, instruction-level parallelism).

[0064] (3) Maximize Computational Efficiency / FLOPS: Pursue the highest floating-point operation speed (FLOPS) and fully exploit the theoretical computing peak of hardware. Suitable for compute-intensive operators. The system will focus on strategies that maximize the use of SIMD (Single Instruction, Multiple Data) / vector units, tensor cores, and other specialized computing units (such as deep vectorization, use of mathematical libraries, arithmetic intensity optimization).

[0065] (4) Optimize Memory Bandwidth Utilization: For memory-intensive operators, maximize the use of effective memory bandwidth and reduce data transfer bottlenecks. The system will focus on strategies such as data prefetching, cache optimization, memory layout adjustment (such as SoA vs AoS), and reducing redundant memory access.

[0066] (5) Balance Performance and Power: Reduce power consumption as much as possible under the premise of meeting certain performance requirements. Suitable for scenarios where energy is limited or energy efficiency is emphasized. The system may choose hardware features with higher energy efficiency, appropriately reduce frequency or parallelism, or use approximate computing strategies.

[0067] To achieve the above target-oriented optimization, the application proposes to deploy a collaborative system composed of four core agents: a strategy identification agent, an operator generation agent, a compilation test agent, and a functional test agent. Based on this system, the following mechanisms are used to ensure the realization of specific optimization objectives.

[0068] (1) Target-driven strategy identification: When receiving a task, the strategy identification agent not only considers operator functions and hardware platforms, but also explicitly receives the specified optimization target as the core input. It uses the knowledge retrieved by RAG (HPOK needs to include the knowledge associated with strategies and optimization targets) and the analysis capabilities of the MoE model to preferentially filter and recommend those known to be the most effective strategy combinations for achieving a specific optimization target, and generate a determined optimization strategy specification that includes this target-oriented optimization strategy.

[0069] (2) Strategy-accurate code implementation: After receiving a code generation task that includes explicit optimization strategies and targets, the operator generation agent generates prompt words that emphasize the need to strictly follow these strategies and organize code structures, select library functions or built-in instructions in a way that achieves the specified optimization target.

[0070] (3) Target-related test verification: The functional test agent not only performs functional correctness verification, but also configures its test flow to include performance benchmark tests related to the specified optimization target. For example, if the goal is to maximize throughput, it will measure and compare throughput indicators; if the goal is to minimize latency, it will focus on execution time. Test failures not only include functional errors, but also performance failures (not meeting the preset threshold or not significantly improving compared to the baseline).

[0071] (4) Target-oriented iterative correction: When the compilation test agent or functional test agent (especially when reporting performance failures) feeds back error or performance deviation information to the operator generation agent, the generated correction prompt words will again emphasize the original optimization target, guiding the large language model to continue or reattempt to achieve the performance goal while fixing errors.

[0072] Through this mechanism of agent collaboration, goal orientation, knowledge enhancement, and iterative optimization, the present invention overcomes the limitations of traditional methods and existing tools when faced with specific high-performance operators, diverse hardware, and clear performance optimization requirements. It can automatically generate high-quality operator code that meets specific optimization goals, solving the problems of unclear optimization direction, high error rate, and difficulty in guaranteeing performance when directly using LLM optimization.

[0073] To achieve the above objectives, such as Figure 1 and Figure 2 As shown, an operator code generation method based on a large model-driven and multi-agent cooperation mechanism is provided. The method includes the following steps:

[0074] S100: Task Reception and Goal-Oriented Policy Recognition: The policy recognition agent receives the initial task request containing high-performance operator reduction information, accesses the high-performance optimization knowledge base by combining Retrieval-Augmented Generation (RAG), and analyzes the operator characteristics using the Mixture of Experts (MoE) technology. It then identifies and generates a definite optimization policy reduction for the specified optimization goal and sends the definite optimization policy reduction along with the original operator reduction information to the operator generation agent.

[0075] S200: Policy delivery and code generation: Based on the received information, the operator generation agent, guided by the goal of achieving the specified optimization objective, calls the large language model to generate initial optimization operator code and sends the generated operator code to the compilation and testing agent;

[0076] S300: Code compilation test: The compilation test agent uses the specified compilation toolchain to compile and test the initial operator code to check for compilation errors;

[0077] S400: Compilation Error Handling and Iteration: If a compilation error is detected, the compilation error information is captured, packaged, and sent back to the operator generation agent. The operator generation agent generates compilation error correction prompts based on the compilation error information, calls the large language model to regenerate the optimized operator code, and returns to S300 to compile and test the regenerated optimized operator code. If the compilation is successful, the successfully compiled code or executable file is sent to the functional test agent.

[0078] S500: Code Functional Testing: The functional testing agent retrieves relevant test cases from the test case database through RAG based on the operator specification information, generates test-driven code, executes functional verification tests, and compares the actual output of the code with the expected output.

[0079] S600: Function test and iteration and final code output: if the function test fails, capture the function test failure information and send it back to the operator generation agent, and the operator generation agent generates a correction prompt word according to the function test failure information, calls the large language model to regenerate the optimized operator code and returns to S300; if the test passes, output the final verified optimized operator code.

[0080] The above operator code generation method based on large model driving and multi-agent cooperation mechanism overcomes the limitations of traditional methods and existing tools in the face of specific high-performance operators, diversified hardware and clear performance optimization requirements, can automatically generate high-quality operator code that meets specific optimization goals, and solves the problem of using a large language model LLM (Large Language Model) directly, which has unclear optimization direction, high error rate and difficult performance guarantee.

[0081] In one embodiment, S100 includes:

[0082] S110: After receiving the operator reduction information, call the internal operator analysis and strategy recognition prompt word generator tool, which constructs the initial prompt word according to the reduction information (function definition such as mathematical formula / pseudo code, hardware platform such as x86 / NVIDIA GPU / domestic card, optimization target such as low latency / high throughput), and sets the large language model LLM role as "high-performance computing HPC (High-Performance Computing) optimization expert", and the task is to analyze the operator characteristics and recommend strategies; wherein the operator reduction information includes the function definition of the operator, the characteristics of the target hardware platform (computing intensity, memory access mode, parallel potential, etc.) and the optimization target of the instruction;

[0083] S120: Use the RAG tool to access the high-performance optimization knowledge base HPOK (High-Performance Optimization Knowledge base), which contains an optimized strategy description, hardware knowledge, performance model, and vectorization knowledge base of code implementation templates, retrieve relevant knowledge fragments according to the operator type and hardware platform, and inject them into the prompt word to enhance the context;

[0084] S130: Use the MoE execution tool to manage multiple LLM experts, and send the enhanced prompt word to the experts in parallel to collect their respective candidate optimization strategy sets, wherein the multiple LLM experts refer to different models or different configurations;

[0085] S140: Perform policy integration logic, fuse candidate strategies output by multiple large language model experts using weighted voting mechanism (weights based on historical performance and task matching degree) or consensus mechanism, combine model confidence filtering score to generate structured and determined optimization strategy specification, wherein the optimization strategy specification includes optimization strategy name, applicable reason and key parameter suggestion (such as Tile size, parallel thread number).

[0086] In one embodiment, S200 includes:

[0087] S210: Receive message package from policy identification agent, containing operator specification information and determined optimization strategy specification;

[0088] S220: Call internal policy implementation code generation prompt word generator tool, which constructs prompt words according to the received information, sets the LLM role as "HPC programmer proficient in target language (such as C++ / CUDA)", the task is to strictly implement the operator function definition according to the determined optimization strategy specification (including strategy name, reason, parameter), specifies target language, library (such as OpenMP, CUDA Intrinsics), hardware feature utilization (such as AVX512, Tensor Cores) and optimization target;

[0089] S230: Use RAG tool to retrieve specific code implementation template or best practice fragment corresponding to the strategy in the specification from HPOK, inject prompt words to guide LLM to generate more standardized and efficient code;

[0090] S240: Send the finally constructed prompt words to the large language model service interface to obtain the generated initial optimization operator code;

[0091] S250: Package the generated code and send it to the compilation test agent through the internal communication bus.

[0092] In one embodiment, S300 includes:

[0093] S310: Receive code from operator generation agent;

[0094] S320: According to the target hardware platform and programming language specified in the operator specification information, select and call the corresponding compilation tool chain (for example, use gcc or clang for x86 CPU, use nvcc for NVIDIA GPU);

[0095] S330: Execute the compilation command and capture the complete compiler output, which includes standard output and standard error stream;

[0096] S340: Use compiler error parser tools to analyze compiler output and detect compilation problems, including syntax errors, type mismatch errors, undeclared / undefined identifier errors, linking errors, etc.

[0097] In one embodiment, S400 includes:

[0098] S410: If the parser of the test agent detects a compilation error, it extracts the key error information and structures it. The key error information includes the file name, line number, column number, and specific error message text.

[0099] S420: Packages structured error information into an error report and sends it back to the operator to generate the intelligent agent via the internal communication bus;

[0100] S430: After receiving an error report, the operator generating agent calls its internal compiler error correction prompt word generator tool. This tool generates prompt words based on the error report content. The prompt words include role, task, error details, previous code snippet, and instruction; the format is as follows: "Role: HPC programmer. Task: Fix compiler error. Error details: [filename:line number:column number] [error message text]. Previous code snippet: [related code]. Instruction: Please correct this compiler error while maintaining optimization intent."

[0101] S440: The operator generating agent sends this correction prompt to the LLM, obtains the corrected code, and then executes S300 again;

[0102] S450: If the compilation test agent does not detect any errors, it will package the source code that has been successfully compiled or the executable file generated as needed, along with the original operator specification information, and send it to the functional test agent through the internal communication bus.

[0103] In one embodiment, S500 includes:

[0104] S510: The functional test agent receives code or executable files and operator specification information;

[0105] S520: Use the RAG tool to access the Test Case Database (TCD) and retrieve relevant test case sets based on the operator type and data shape in the operator specification information; the test case database contains test inputs and corresponding reference outputs for different operators, data types, scales, and boundary conditions;

[0106] S530: Calls the test driver generator tool to automatically generate or configure a test driver program based on the operator interface and the retrieved test cases. This program can call the code of the operator to be tested and pass in the test input.

[0107] S540: Execute the test driver, run all selected test cases, capture the actual output of the operator code;

[0108] S550: Use the results comparator tool to compare the actual output with the expected output (golden reference) defined in the test cases, exactly or fault-tolerantly (for floating-point numbers).

[0109] In one embodiment, S600 includes:

[0110] S610: If the comparison finds inconsistencies, capture detailed failure information: failed test case ID or description, specific input data, actual output, expected output, and attempt to locate errors through the insertion of log or assertion failure information;

[0111] S620: Package the structured failure information into a test failure report and send it back to the operator generation agent through the internal communication bus;

[0112] S630: After the operator generation agent receives the test failure report, it calls its internal test failure correction prompt generator tool, which generates a prompt based on the failure report content. The prompt includes the role, task, failure details, and instructions. The prompt format is as follows: "Role: HPC programmer. Task: Fix functional test failure. Failure details: Test case [ID], input [data], actual output [output], expected output [output]. Positioning clues (optional): [clues]. Previous code snippet (optional): [related code]. Instructions: Please correct the code logic error, pass this test case, and maintain the optimization strategy."

[0113] S640: The operator generation agent sends this correction prompt to the LLM, obtains the corrected code, and then returns to S300 to start the compilation and functional test cycle again;

[0114] S650: If the functional test agent confirms that all test cases pass, the final optimized operator code is marked as correct and provided to the user or downstream system through the designated output interface.

[0115] In one embodiment, an operator code generation system based on large model driving and multi-agent collaboration mechanism is also provided, including:

[0116] A strategy identification agent (Strategy Identification Agent) is responsible for receiving initial tasks, identifying optimization strategies using RAG and MoE technology, generating a determined optimization strategy specification, and passing the results to the operator generation agent;

[0117] Operator Generation Agent, responsible for generating initial code according to the policy provided by the policy recognition agent, and generating correction prompt word calls to LLM for iterative correction according to the error information fed back by the compilation testing agent or the functional testing agent;

[0118] Compilation Testing Agent, responsible for receiving code, performing compilation testing using a specified compiler, detecting compilation errors, and feeding back error information to the Operator Generation Agent or passing the successfully compiled code to the Functional Testing Agent;

[0119] Functional Testing Agent, responsible for receiving successfully compiled code, using RAG to obtain test cases, performing functional verification testing, detecting functional errors, and feeding back error information to the Operator Generation Agent or confirming that the code is finally qualified;

[0120] At least one knowledge base, including a high-performance optimization knowledge base HPOK for storing optimization knowledge and / or a test case database TCD for storing test cases; further, the high-performance optimization knowledge base stores optimization strategies, hardware knowledge, performance models, code templates, etc., for the policy recognition agent and the operator generation agent to access through RAG; the test case database TCD stores test cases (input, expected output) for functional verification for the functional testing agent to access through RAG;

[0121] Large language model service interface for the policy recognition agent and the operator generation agent to call;

[0122] Internal communication mechanism based on message queue or remote procedure call, used to transfer information between the policy recognition agent, the operator generation agent, the compilation testing agent, and the functional testing agent, to ensure that tasks, data, and feedback information can be asynchronously and reliably transferred between the agents.

[0123] In one embodiment, the policy recognition agent internally includes an operator analysis and policy recognition prompt word generator tool, an RAG tool for accessing HPOK, and a MoE execution tool for fusing expert output; the operator generation agent internally includes a policy implementation code generation prompt word generator tool, a compilation error correction prompt word generator tool, and a test failure correction prompt word generator tool.

[0124] In one embodiment, the compiling test agent internally includes an interface for invoking a compiling tool chain corresponding to the target hardware platform, and a compiler error resolver tool; the function test agent internally includes an RAG tool for accessing the TCD, a test driver generator tool, and a result comparator tool for comparing the actual output with the expected output.

[0125] For specific definitions of the operator code generation system based on the large model driving and multi-agent cooperation mechanism, refer to the definitions of the operator code generation method based on the large model driving and multi-agent cooperation mechanism in the foregoing, which will not be repeated here. Each module in the above operator code generation system based on the large model driving and multi-agent cooperation mechanism can be realized by software, hardware, and combinations thereof, in whole or in part. The above modules can be embedded in or independent of the processor in the computer device in hardware form, or can be stored in the memory in the computer device in software form, so as to be called and executed by the processor to perform the operations corresponding to the above modules.

[0126] Overall, compared with the prior art, the above technical solutions conceived by the present application can achieve the following beneficial effects:

[0127] (1) Highly specialized and streamlined: Through the cooperation of four clearly defined agents (strategy recognition, code generation, compilation testing, and function testing), complex optimization and generation tasks are broken down into specialized, pipeline-like steps, improving processing efficiency and quality control at each stage.

[0128] (2) Targeted testing and feedback: Differentiating between compilation testing and function testing, which are handled by different agents, provides more specific and targeted error feedback (compilation errors vs. logical errors), guiding the operator generation agent to make more accurate code corrections.

[0129] (3) Dynamic test case support: The function test agent can dynamically obtain the most relevant test cases for the current operator through RAG, improving test coverage and adaptability, especially in scenarios with a wide variety of operators.

[0130] (4) Robust iteration and error correction loop: The clearly defined agent interaction and feedback path forms a strict iteration and error correction loop, effectively handling various issues that may arise during code generation, significantly improving the correctness and usability of the final output code.

[0131] (5) Modularity and scalability: The agent-based architecture naturally has good modularity, making it easy to upgrade or replace individual agents (such as replacing test tools or upgrading LLM models), improving system maintainability and future development flexibility.

[0132] A computer device comprises a memory and a processor, the memory stores a computer program, and the processor implements steps of an operator code generation method based on large model driving and multi-agent cooperation mechanism when executing the computer program.

[0133] A computer readable storage medium stores a computer program, and the computer program implements steps of an operator code generation method based on large model driving and multi-agent cooperation mechanism when executed by a processor.

[0134] A person of ordinary skill in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer readable storage medium, and when executed, can include the processes of the above-mentioned embodiments. Any reference to memory, storage, database or other medium used in the embodiments provided by the present application can include at least one of non-volatile and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory or optical memory. Volatile memory can include random access memory (RAM) or external cache memory. As an illustration but not limitation, RAM can be in various forms such as static random access memory (SRAM) or dynamic random access memory (DRAM).

[0135] The above describes the operator code generation method and system based on large model driving and multi-agent cooperation mechanism provided by the present application in detail. The principles and implementation modes of the present application are described by applying specific examples. The above description of the embodiments is only used to help understand the core idea of the present application. It should be pointed out that for those of ordinary skill in the art, without departing from the principles of the present application, the present application can be improved and modified in several ways, and these improvements and modifications also fall within the protection scope of the claims of the present application.

Claims

1. An operator code generation method based on a large model driving and multi-agent cooperation mechanism, characterized in that, The method comprises the following steps: S100: The policy recognition agent receives an initial task request containing high-performance operator reduction information, accesses the high-performance optimization knowledge base in combination with retrieval enhancement, analyzes the operator characteristics using the hybrid expert model MoE technology, identifies and generates a determined optimization strategy reduction oriented to a specified optimization target, and sends the determined optimization strategy reduction together with the original operator reduction information to the operator generation agent; S200: The operator generation agent generates initial optimization operator code based on the received information to achieve the specified optimization target, and sends the generated operator code to the compilation test agent; S300: The compilation test agent uses a specified compilation tool chain to compile and test the initial operator code, and checks whether there is a compilation error; S400: If a compilation error is detected, the compilation error information is captured and sent back to the operator generation agent, the operator generation agent generates a compilation error correction prompt word according to the compilation error information, calls the large language model to regenerate the optimization operator code and returns to S300 to compile and test the regenerated optimization operator code; if the compilation is successful, the compiled code or executable file is sent to the function test agent; S500: The function test agent retrieves relevant test cases from the test case database through the RAG according to the operator reduction information, generates test driver code, performs function verification test, and compares the actual output with the expected output; S600: If the function test fails, the function test failure information is captured and sent back to the operator generation agent, the operator generation agent generates a correction prompt word according to the function test failure information, calls the large language model to regenerate the optimization operator code and returns to S300; if the test passes, the final verified optimization operator code is output.

2. The method of claim 1, wherein, S100 comprises: S110: After receiving the operator reduction information, an internal operator analysis and strategy recognition prompt word generator tool is called, which constructs an initial prompt word according to the reduction information, sets the large language model LLM role as "high-performance computing HPC optimization expert", and the task as analyzing operator characteristics and recommending strategies; wherein the operator reduction information includes the function definition of the operator, the characteristics of the target hardware platform, and the optimization target of the instruction; S120: Use the RAG tool to access the high-performance optimization knowledge base HPOK, which contains an optimization strategy description, hardware knowledge, a performance model, and a vectorization knowledge base of code implementation templates, retrieve relevant knowledge fragments according to the operator type and hardware platform, and inject them into the prompt word to enhance the context; S130: Use the MoE execution tool to manage multiple LLM experts, send the enhanced prompt word to the experts in parallel, and collect the respective candidate optimization strategy set, wherein the multiple LLM experts refer to different models or different configurations; S140: Execute policy integration logic, adopt weighted voting mechanism or consensus mechanism to fuse candidate strategies output by multiple large language model experts, combine model confidence screening score to generate structured and determined optimization strategy specification, wherein the optimization strategy specification includes optimization strategy name, applicable reason and key parameter suggestion.

3. The method of claim 2, wherein, S200 includes: S210: Receive message package from policy identification agent, containing operator specification information and determined optimization strategy specification; S220: Call internal policy implementation code generation prompt word generator tool, which constructs prompt words according to the received information, sets LLM role as "HPC programmer proficient in target language", task as strictly implementing operator function definition according to determined optimization strategy specification, specifies target language, library, hardware feature utilization and optimization target; S230: Use RAG tool to retrieve specific code implementation template or best practice fragment corresponding to the strategy in the specification from HPOK, inject prompt words to guide LLM to generate more standardized and efficient code; S240: Send the finally constructed prompt words to the large language model service interface to obtain the generated initial optimization operator code; S250: Package the generated code and send it to the compilation test agent through the internal communication bus.

4. The method of claim 3, wherein, S300 includes: S310: Receive code from operator generation agent; S320: According to the target hardware platform and programming language specified in the operator specification information, select and call the corresponding compilation tool chain; S330: Execute the compilation command and capture the complete compiler output, which includes standard output and standard error stream; S340: Use compiler error parser tool to analyze compiler output, detect compilation problems, and compilation errors include syntax error, type mismatch error, undeclared / undefined identifier error, linking error, etc.

5. The method of claim 4, wherein, S400 includes: S410: If the compiler test agent's parser detects a compilation error, extract the key error information and structure it, wherein the key error information includes file name, line number, column number, and specific error message text; S420: Package the structured error information into an error report and send it back to the operator generation agent through the internal communication bus; S430: After receiving the error report, the operator generation agent calls its internal compilation error correction prompt word generator tool, which generates prompt words according to the error report content, including role, task, error details, previous code fragment and instructions; S440: The operator generation agent sends this correction prompt word to the LLM to obtain the corrected code, and then executes S300 again; S450: If the compiler test agent does not detect an error, package the source code or executable file generated as needed along with the original operator specification information and send it to the function test agent through the internal communication bus.

6. The method of claim 5, wherein, S500 includes: S510: The function test agent receives the code or executable file and the operator specification information; S520: Access the test case database TCD using the RAG tool, retrieve the relevant test case set according to the operator type and data shape in the operator reduction information; wherein the test case database contains test inputs and corresponding reference outputs for different operators, data types, sizes, and boundary conditions; S530: Call the test driver generator tool to automatically generate or configure a test driver according to the operator interface and the retrieved test cases, which can call the operator code under test and pass in the test input; S540: Execute the test driver to run all selected test cases and capture the actual output of the operator code; S550: Use the result comparator tool to compare the actual output with the expected output defined in the test case.

7. The method of claim 5, wherein, S600 includes: S610: If inconsistencies are found through comparison, capture detailed failure information: failed test case ID or description, specific input data, actual output, expected output, and attempt to locate errors through patching logs or assertion failure information; S620: Package the structured failure information into a test failure report and send it back to the operator generation agent through the internal communication bus; S630: After receiving the test failure report, the operator generation agent calls its internal test failure correction prompt generator tool, which generates a prompt according to the failure report content, including role, task, failure details, and instructions; S640: The operator generation agent sends this correction prompt to the LLM, obtains the corrected code, and then returns to S300 to start the compilation and functional testing cycle again; S650: If the functional testing agent confirms that all test cases pass, the final optimized operator code is marked as qualified and provided to the user or downstream system through the specified output interface.

8. An operator code generation system based on a large model driving and multi-agent cooperation mechanism, characterized in that, includes: A strategy recognition agent responsible for receiving initial tasks, identifying optimization strategies using RAG and MoE technology, generating a determined optimization strategy specification, and passing the result to the operator generation agent; An operator generation agent responsible for generating initial code based on the strategy provided by the strategy recognition agent, and generating correction prompts to call LLM for iterative correction based on error information feedback from the compilation testing agent or functional testing agent; A compilation testing agent responsible for receiving code, using a specified compiler for compilation testing, detecting compilation errors, and feeding back error information to the operator generation agent or passing the successfully compiled code to the functional testing agent; A functional testing agent responsible for receiving successfully compiled code, using RAG to obtain test cases, performing functional verification testing, detecting functional errors, and feeding back error information to the operator generation agent or confirming that the code is finally qualified; At least one knowledge base, including a high-performance optimization knowledge base HPOK for storing optimization knowledge and / or a test case database for storing test cases; A large language model service interface for the strategy recognition agent and the operator generation agent to call; Internal communication mechanisms based on message queues or remote procedure calls for passing information between policy recognition agents, operator generation agents, compilation test agents, and functional test agents.

9. The system of claim 8, wherein, The policy recognition agent internally includes operator analysis and policy recognition prompt generation tools, RAG tools for accessing HPOK, and MoE execution tools for fusing expert outputs; the operator generation agent internally includes policy implementation code generation prompt generation tools, compilation error correction prompt generation tools, and test failure correction prompt generation tools.

10. The system of claim 9, wherein, The compilation test agent internally includes interfaces for calling a compilation tool chain corresponding to a target hardware platform, and compiler error resolver tools; the functional test agent internally includes RAG tools for accessing TCD, test driver generation tools, and result comparator tools for comparing actual outputs with expected outputs.

Citation Information

Patent Citations

  • Equipment after-sales intelligence system based on AI Agent and digital twinning

    CN118350826A

  • Video generation method and device based on multi-agent cooperation, agents and equipment

    CN120151561A