An embedded VSIPL code generation method and device based on knowledge base and large model combination

CN122884480APending Publication Date: 2026-10-09NANJING RES INST OF ELECTRONICS TECH
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611375905.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-09-07
Publication Date
2026-10-09

AI Technical Summary

Technical Problem

本发明解决了现有技术中人工转换效率低、通用大模型无法生成嵌入式私有API代码以及Matlab Coder生成代码性能不足的问题,显著提升了嵌入式信号处理软件的开发效率和代码运行性能

Benefits of technology

[0039]1. 本发明通过构建包含私有API和转换规则的专属知识库,解决了通用大模型缺乏领域知识的问题,显著降低了代码生成的幻觉率。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122884480A_ABST
    Figure CN122884480A_ABST
Patent Text Reader

Abstract

The application discloses a kind of embedded VSIPL code generation method and device based on knowledge base and big model combination.First, the exclusive knowledge base containing Matlab-VSIPL conversion rule, private API calling specification, code contrast example is built;Parse the characteristics of the Matlab code to be converted, fuse BM25 and vector retrieval to match high-similarity reference examples;Integrate task requirements, source code, search examples, API rules to build conversion-specific prompt input into big model to output initial VSIPL code;Then, relying on knowledge base rules, the target code is obtained by optimization.The device is equipped with knowledge base construction, input analysis, search enhancement, big model processing, code optimization module.With the help of professional knowledge base to constrain big model generation logic, the problems of poor private API adaptation, many syntax errors and insufficient code standardization in traditional model conversion to embedded VSIPL code are solved, and the code conversion accuracy and engineering usability are improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of embedded software development and automatic code generation technology, and particularly relates to an embedded VSIPL code generation method and apparatus based on a combination of knowledge base and large language model. Background Technology

[0002] In the field of embedded high-performance computing, VSIPL (Vector Signal Image Processing Library) is widely used in the development of signal processing software for systems such as radar, sonar, and communication due to its efficient parallel computing capabilities.

[0003] The typical development process is as follows: algorithm engineers first use Matlab to verify the algorithm and design a prototype. After successful verification, embedded software engineers manually convert the Matlab code into VSIPL code based on C / C++. However, the existing development model has the following significant pain points:

[0004] 1. Low conversion efficiency and uncontrollable quality: The manual conversion process is cumbersome, prone to human error, and the differences in coding styles among different engineers make maintenance difficult.

[0005] 2. Limited by automation tools: Although tools such as Matlab Coder can automatically generate C code, the generated C code is usually standard C code and cannot utilize the specific parallel acceleration instructions of VSIPL, resulting in performance that cannot meet the real-time requirements of embedded systems.

[0006] 3. General-purpose large models cannot be directly applied: While existing general-purpose large language models (such as the GPT series and Llama) have code generation capabilities, their training data is mainly based on publicly available internet code, lacking knowledge of the "private APIs" of specific embedded platforms. Code generated directly using general-purpose large models often contains a large number of fictitious functions (illusion phenomenon) and cannot follow the calling rules of specific hardware platforms. Summary of the Invention

[0007] This invention discloses an embedded VSIPL code generation method and apparatus based on a combination of knowledge base and large model. It can accurately and automatically convert Matlab code into high-performance VSIPL code by combining proprietary knowledge of the embedded domain, meeting the requirements of embedded software development. The method constructs a dedicated knowledge base containing embedded proprietary APIs, conversion rules, and code examples. It dynamically matches examples similar to the Matlab code to be converted using BM25 and vector retrieval techniques. By designing structured conversion-specific prompts, it guides a large language model to generate preliminary VSIPL code by combining the retrieved example knowledge. Finally, it optimizes the code using a rule base. This invention solves the problems of low efficiency in manual conversion, the inability of general-purpose large models to generate embedded proprietary API code, and insufficient performance of Matlab Coder in existing technologies, significantly improving the development efficiency and code execution performance of embedded signal processing software.

[0008] The objective of this invention is achieved through the following technical solution. The first aspect of this invention provides an embedded VSIPL code generation method based on a combination of knowledge base and large model, comprising the following steps:

[0009] Step S1: Construct an embedded VSIPL-specific knowledge base, which includes Matlab to VSIPL conversion rules, embedded private API calling rules, and a Matlab and VSIPL code comparison example library;

[0010] Step S2: Receive the Matlab source code to be converted from the user input;

[0011] Step S3: Perform feature parsing on the Matlab source code, and dynamically search the example library based on the parsing results to obtain reference conversion examples with a similarity higher than a preset threshold to the Matlab source code;

[0012] Step S4: Construct conversion-specific prompts, which include at least the task description, the Matlab source code to be converted, the reference conversion example, the relevant private API calling rules, and the target code specifications;

[0013] Step S5: Input the constructed conversion-specific prompt words into the large language model. Based on the learned context examples, the large model imitates their conversion logic and generates preliminary VSIPL code.

[0014] Step S6: Use the transformation rules in the knowledge base to verify and optimize the preliminary VSIPL code to obtain the final VSIPL target code.

[0015] The embedded VSIPL-specific knowledge base in step S1 includes:

[0016] API documentation library: Enter VSIPL function definitions for embedded platforms. Each record includes the function name, parameter list, input and output data types, and calling restrictions.

[0017] Conversion rule base: Stores the mapping relationship between Matlab functions and VSIPL functions;

[0018] Code Example Library: Collects and stores "Matlab code snippets" and their corresponding "optimized VSIPL implementation code" that have been verified and manually optimized from historical projects.

[0019] Step S3 performs the following operations on the Matlab code input by the user:

[0020] Feature extraction: Extract key operators, function names, and variable names from the code;

[0021] Hybrid retrieval: Use the BM25 algorithm for keyword matching to retrieve examples containing the same operators, and use vector retrieval for semantic matching to retrieve code segments with similar algorithmic logic;

[0022] Top-K filtering: The keyword matching search results and semantic vector search results are weighted and fused, and a preset number of examples with the highest ranking are selected as the reference conversion examples and filled into the [Context] slot of the prompt words.

[0023] In step S4, conversion-specific prompt words are constructed, and a structured conversion-specific prompt word template is designed, including the following slots:

[0024] [Role]: Set to "Senior Embedded Development Expert Proficient in VSIPL";

[0025] [Task]: Described as "Converts input Matlab code into ANSI C-compliant VSI PIL code";

[0026] [Context]: Dynamically fill in the relevant API definitions and similar examples retrieved in step S3;

[0027] [Input]: The Matlab code to be converted;

[0028] [Constraints]: Code style requirements, which explicitly prohibit the generation of non-VSIPL standard serial loop processing logic.

[0029] The verification and optimization in step S6 includes: parsing the function interfaces called in the initial VSIPL code, matching the function interfaces with the embedded private API list in the knowledge base, and if there is a non-existent function interface, it is determined to be an illusion generation, and the closest legal API is found and replaced based on the conversion rules.

[0030] The verification and optimization in step S6 also includes: performing an integrity check on the lifecycle management code of VSIPL-specific Block objects and View objects to ensure that all created objects are correctly released.

[0031] The verification and optimization in step S6 also includes: checking whether unnecessary memory copy operations are included, and optimizing the data flow according to VSIPL's "view" and "block" mechanisms.

[0032] A second aspect of the present invention provides an embedded VSIPL code generation device based on a combination of knowledge base and large model, comprising:

[0033] The knowledge base building module is used to build and maintain a vector database containing transformation rules, private APIs, and code examples;

[0034] The input parsing module is used to receive Matlab source code and extract code features;

[0035] The retrieval enhancement module is used to dynamically retrieve similar transformation examples from the knowledge base based on code characteristics;

[0036] The large model processing module is used to generate preliminary code based on prompts combined with transformation examples;

[0037] The code optimization module is used to perform rule verification and logic correction on the initial code.

[0038] Compared with the prior art, the advantages of this invention are:

[0039] 1. This invention solves the problem of lack of domain knowledge in general large models by constructing a dedicated knowledge base containing private APIs and transformation rules, and significantly reduces the illusion rate of code generation.

[0040] 2. This invention employs RAG (Retrieval Enhanced Generation) technology to dynamically retrieve similar examples as context (Few-Shot), which significantly improves the ability of large models to understand complex signal processing logic and the accuracy of code generation.

[0041] 3. Compared to traditional Matlab Coder, the code generated by this invention can directly call high-performance VSIPL library functions, meeting the performance requirements of embedded systems. Attached Figure Description

[0042] Figure 1 It is an embedded VSIPL code generation framework diagram based on the combination of knowledge base and large model. Detailed Implementation

[0043] The specific embodiments of the present invention will now be described in detail with reference to the accompanying drawings.

[0044] like Figure 1 As shown, this embodiment provides an embedded VSIPL code generation method based on a combination of knowledge base and large model. The specific steps are as follows:

[0045] Step S1: Build an embedded VSIPL knowledge base.

[0046] The knowledge base's data structure is designed as a combination of vector database and relational database, mainly consisting of three parts:

[0047] 1. API Documentation Library: This library contains VSIPL function definitions specific to embedded platforms, especially private APIs (such as FFT and convolution functions optimized for specific DSP chips). Each record includes the function name, parameter list, input / output data types, and calling restrictions.

[0048] 2. Conversion rule base: Stores the mapping relationship between Matlab functions and VSIPL functions. For example, fft(x) in Matlab corresponds to vsip_ccfftip_f (in-situ complex FFT) in VSIPL.

[0049] 3. Code Example Library (Pair Data): Collects and stores verified and manually optimized "Matlab code snippets" from historical projects, along with their corresponding "optimized VSIPL implementation code" in pairs.

[0050] As a concrete example of transformation code, the VSIPL code corresponding to the Matlab code "y = fft(x, N);" is:

[0051] vsip_fft_f *plan = vsip_fft_create_f(N, 1.0, 1, VSIP_FFT_FWD, VSIP_ALG_SPACE, vsipl);

[0052] vsip_ccfftop_f((vsip_cvview_f *)x_v, (vsip_cvview_f *)y_v, plan);

[0053] vsip_fft_destroy_f(plan);

[0054] Here, x_v and y_v are the input and output complex views (of type vsip_cvview_f), respectively, and plan is an FFT plan object, which is created by vsip_fft_create_f and destroyed by vsip_fft_destroy_f. This reflects the lifecycle management of block and view objects in VSIPL, including their creation, use, and destruction. This code example can be used as a reference for the lifecycle management integrity check in step S6.

[0055] Step S2: Receive the Matlab source code to be converted from the user input.

[0056] When a user inputs a piece of Matlab code (such as a pulse compression algorithm), the system receives it as the object to be converted and proceeds to the next step.

[0057] Step S3: Dynamic retrieval and context construction.

[0058] When a user inputs a piece of Matlab code (such as a pulse compression algorithm), the system performs the following operations:

[0059] 1. Feature extraction: Extract key operators (such as conv, fft, abs) as well as function names and variable names from the code.

[0060] 2. Hybrid Search:

[0061] Use the BM25 algorithm for keyword matching to retrieve examples containing the same operator.

[0062] Use vector search (such as embeddings generated by BERT) for semantic matching to retrieve code segments with similar algorithmic logic.

[0063] 3. Top-K Filtering: The keyword matching search results and semantic vector search results are weighted and fused. The weighted fusion score is calculated using, for example, the following formula:

[0064] score_total = α × score_BM25 + (1 - α) × score_vec

[0065] Here, score_BM25 is the BM25 keyword matching score, score_vec is the vector semantic similarity score, and α is the fusion weight, with a value ranging from 0.5 to 0.7. Based on the fusion scores sorted from high to low, the top-3 "Matlab-VSIPL code pairs" with similarity above a preset threshold and the highest ranking are selected as Few-Shot examples and filled into the [Context] slot of the prompt word.

[0066] Step S4: Design the proprietary prompt word (Prompt Engineering).

[0067] To standardize the output of large models, this invention designs a structured conversion-specific prompt word template. This template contains the following slots:

[0068] [Role]: Set to "Senior Embedded Development Expert with VSIPL Proficiency".

[0069] [Task]: Described as "Converts input Matlab code into ANSI C standard VSI PIL code".

[0070] [Context]: Dynamically fill in the relevant API definitions and similar examples retrieved in step S3.

[0071] [Input]: The Matlab code to be converted.

[0072] [Constraints]: Code style requirements (e.g., must use private APIs for acceleration, memory management requires manual release, and the use of standard C for loops for vector operations is prohibited, i.e., non-VSIPL standard serial loop processing logic is prohibited).

[0073] Taking the FFT operation in the pulse compression algorithm as an example, the complete example of the conversion-specific prompt words is as follows:

[0074] [Role]: A senior embedded development expert proficient in VSIPL

[0075] [Task]: Converts the input Matlab code into VSI PIL code that conforms to the ANSI C standard.

[0076] [Context]: Example: The Matlab code "y = fft(x, 1024)" corresponds to the VSIPL code "vsip_ccfftip_f(x_v, y_v, plan)", where x_v and y_v are complex views, and plan is the FFT plan object; API definition: vsip_ccfftip_f is the in-situ complex FFT function.

[0077] [Input]: y = fft(x, 1024);

[0078] [Constraints]: Private APIs must be used for acceleration; memory management requires manual release; vector operations using standard C for loops are prohibited.

[0079] Step S5: Large model generation.

[0080] The constructed complete prompt words are input into the large language model (in this embodiment, a privately deployed Llama3 or similar model can be used). Based on the learned context examples, the large model imitates their transformation logic to generate preliminary VSIPL code.

[0081] Step S6: Code optimization and correction.

[0082] The code generated from large models may contain minor syntax errors or parameter type mismatches, which can be optimized using post-processing scripts.

[0083] 1. Static Analysis and Illusion Function Replacement: The function interfaces called in the generated initial VSIPL code are parsed and matched against the embedded private API list in the knowledge base. If a function interface not found in the API documentation library is identified as an illusion function, a legitimate API with the closest functionality is searched based on the transformation rule base for replacement. The rule for determining the closest function is: first, matching by function category; second, comparing the function name edit distance and parameter signature; and finally, selecting the legitimate API with the closest functionality to the illusion function.

[0084] 2. Performance optimization: Check for unnecessary memory copy operations, optimize data flow based on VSIPL's "View" and "Block" mechanisms, and avoid copying data through intermediate caches.

[0085] 3. Lifecycle Management Integrity Check: Performs integrity checks on the lifecycle management code of VSIPL-specific Block and View objects to ensure that each created View / Block object is correctly released, avoiding memory leaks.

[0086] This embodiment also provides an embedded VSIPL code generation device based on a combination of knowledge base and large model, including:

[0087] The knowledge base building module is used to build and maintain a vector database containing transformation rules, private APIs, and code examples;

[0088] The input parsing module is used to receive Matlab source code and extract code features;

[0089] The retrieval enhancement module is used to dynamically retrieve similar transformation examples from the knowledge base based on code characteristics;

[0090] The large model processing module is used to generate preliminary code based on prompts combined with transformation examples;

[0091] The code optimization module is used to perform rule verification and logic correction on the initial code.

Claims

1. An embedded VSIPL code generation method based on a combination of knowledge base and large model, characterized in that, Includes the following steps: Step S1: Construct an embedded VSIPL-specific knowledge base, which includes Matlab to VSIPL conversion rules, embedded private API calling rules, and a Matlab and VSIPL code comparison example library; Step S2: Receive the Matlab source code to be converted from the user input; Step S3: Perform feature parsing on the Matlab source code, and dynamically search the example library based on the parsing results to obtain reference conversion examples with a similarity higher than a preset threshold to the Matlab source code; Step S4: Construct conversion-specific prompts, which include at least the task description, the Matlab source code to be converted, the reference conversion example, the relevant private API calling rules, and the target code specifications; Step S5: Input the constructed conversion-specific prompt words into the large language model. Based on the learned context examples, the large model imitates their conversion logic and generates preliminary VSIPL code. Step S6: Use the transformation rules in the knowledge base to verify and optimize the preliminary VSIPL code to obtain the final VSIPL target code.

2. The embedded VSIPL code generation method based on a combination of knowledge base and large model as described in claim 1, characterized in that, The embedded VSIPL-specific knowledge base in step S1 includes: API documentation library: Enter VSIPL function definitions for embedded platforms. Each record includes the function name, parameter list, input and output data types, and calling restrictions. Conversion rule base: Stores the mapping relationship between Matlab functions and VSIPL functions; Code Example Library: Collects and stores "Matlab code snippets" and their corresponding "optimized VSIPL implementation code" that have been verified and manually optimized from historical projects.

3. The embedded VSIPL code generation method based on a combination of knowledge base and large model as described in claim 1, characterized in that, In step S4, conversion-specific prompt words are constructed, and a structured conversion-specific prompt word template is designed, including the following slots: [Role]: Set to "Senior Embedded Development Expert Proficient in VSIPL"; [Task]: Described as "Converts the input Matlab code into VSI C standard compliant VSI PIL code"; [Context]: Dynamically fill in the relevant API definitions and similar examples retrieved in step S3; [Input]: The Matlab code to be converted; [Constraints]: Code style requirements, which explicitly prohibit the generation of non-VSIPL standard serial loop processing logic.

4. The embedded VSIPL code generation method based on the combination of knowledge base and large model as described in claim 1, characterized in that, Step S3 performs the following operations on the Matlab code input by the user: Feature extraction: Extract key operators, function names, and variable names from the code; Hybrid retrieval: Use the BM25 algorithm for keyword matching to retrieve examples containing the same operators, and use vector retrieval for semantic matching to retrieve code segments with similar algorithmic logic; Top-K filtering: The keyword matching search results and semantic vector search results are weighted and fused, and a preset number of examples with the highest ranking are selected as the reference conversion examples and filled into the [Context] slot of the prompt words.

5. The embedded VSIPL code generation method based on the combination of knowledge base and large model according to claim 1, characterized in that, The verification and optimization in step S6 includes: parsing the function interfaces called in the initial VSIPL code, matching the function interfaces with the embedded private API list in the knowledge base, and if there is a non-existent function interface, it is determined to be an illusion generation, and the closest legal API is found and replaced based on the conversion rules.

6. The embedded VSIPL code generation method based on the combination of knowledge base and large model according to claim 1, characterized in that, The verification and optimization in step S6 also includes: performing an integrity check on the lifecycle management code of VSIPL-specific Block objects and View objects to ensure that all created objects are correctly released.

7. The embedded VSIPL code generation method based on a combination of knowledge base and large model as described in claim 1, characterized in that, The verification and optimization in step S6 also includes: checking whether unnecessary memory copy operations are included, and optimizing the data flow according to VSIPL's "view" and "block" mechanisms.

8. An embedded VSIPL code generation device based on a combination of knowledge base and large model, characterized in that, include: The knowledge base building module is used to build and maintain a vector database containing transformation rules, private APIs, and code examples; The input parsing module is used to receive Matlab source code and extract code features; The retrieval enhancement module is used to dynamically retrieve similar transformation examples from the knowledge base based on code characteristics; The large model processing module is used to generate preliminary code based on prompts combined with transformation examples; The code optimization module is used to perform rule verification and logic correction on the initial code.