Lapack code library function name recommendation method and system based on thinking chain and logical reasoning
Through a method based on thinking chain and logical reasoning, combined with large language models and database retrieval, the problem of insufficient accuracy in the code generation and completion of existing LLMs in lapack library is solved, and more efficient function name recommendation and code generation are achieved.
Patent Information
- Application Number
- CN202411661430.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-20
- Publication Date
- 2025-03-21
- Estimated Expiration
- 2044-11-20
AI Technical Summary
Existing LLMs do not work well when using the lapack library to generate or complete code, especially due to differences in different hardware architectures, programming languages and function interfaces, resulting in low accuracy in function name recommendations.
Using a method based on thinking chain and logical reasoning, by constructing a lapack function database, a keywords database and cot question and answer examples, combining large language model (LLM) reasoning and inspection, we automatically identify the differences between different historical versions and programming language interfaces, and recommend accurate lapack code base function names.
It improves the accuracy of function name recommendations, can automatically identify differences between different historical versions and programming language interfaces, reduces the need for LLM re-tuning, and improves the accuracy of code generation.
Smart Images

Figure CN119668579A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of code completion and generation, and in particular relates to a method and system for recommending function names of a lapack code library based on thought chains and logical reasoning. Background Art
[0002] With the development of deep learning technology, especially natural language processing technology, more and more LLMs have emerged, such as the GPT series of models, the Code Llama series of models, etc. These LLMs can talk with users and answer questions raised by users, or generate code implementations based on user requirements or complete incomplete codes given by users.
[0003] However, most LLMs are aimed at general fields. For the lapack library in the HPC field, multiple functions can be called to solve a problem, and different functions correspond to different hardware architectures and programming languages. Even for the same function, the interfaces of the two programming languages Fortran and C are different in terms of name and parameter list, which makes the general LLM not effective when using the lapack library to generate code or complete code. However, when the correct lapack function name is given as a prompt, the accuracy of code generation can be greatly improved.
[0004] There are also some LLMs for HPC code generation, but they are usually fine-tuned based on the collected HPC code base, which requires a lot of training time and resources. In addition, the number of codes using lapack functions is not large, which makes it very difficult to fine-tune LLM to improve the accuracy of code generation. Summary of the invention
[0005] The main purpose of the present invention is to overcome the shortcomings and deficiencies of the prior art and to propose a method and system for recommending function names of lapack code libraries based on thought chains and logical reasoning.
[0006] In order to achieve the above object, the present invention adopts the following technical solutions:
[0007] The lapack code library function name recommendation method based on thought chain and logical reasoning includes the following steps:
[0008] S1. Build lapack function database, keywords database and cot question and answer examples;
[0009] S2. Add the input programming requirements to the cot question-and-answer example to obtain the cot prompt content. According to the cot prompt content, LLM is used to infer the key attributes of the lapack function required for programming except the implementation method, and the key attributes are verified;
[0010] S3, using the verified key attributes to search for available implementation methods in the lapack function database, and using LLM to select the implementation method to be used;
[0011] S4. Search the lapack function database according to the verified key attributes and the implementation method selected by LLM to obtain a first function name search result;
[0012] S5. Search the keywords database according to programming requirements to obtain a second function name search result;
[0013] S6. Combining the two function name search results, logical reasoning is performed to obtain the recommended lapack code library function name.
[0014] The present invention also includes a lapack code library function name recommendation method based on thought chain and logical reasoning. The system adopts the lapack code library function name recommendation method provided by the present invention. The system includes a database construction module, a large language model module, a verification and retrieval module, and a reasoning output module;
[0015] Database construction module, used to build lapack function database, keywords database and cot question and answer examples;
[0016] The large language model module infers the key attributes of the lapack function required for programming in addition to the implementation method based on the input programming requirements and the COT prompt content obtained from the COT question-and-answer example, and is also used to select the implementation method to be used;
[0017] The verification and retrieval module is used to retrieve and verify the key attributes inferred by the large language model module, and is also used to search the lapack function database and the keywords database to obtain the first function name search result and the second function name search result;
[0018] The reasoning output module is used to perform logical reasoning based on the two function name retrieval results and output the recommended lapack code library function name.
[0019] Compared with the prior art, the present invention has the following advantages and beneficial effects:
[0020] 1. The method of the present invention can automatically identify the interface differences between different historical versions of the same software package and programming languages, and improve the accuracy of function name recommendation; while achieving accurate recommendation of lapack code library function names, there is no need to re-fine-tune LLM.
[0021] 2. The present invention is based on a special COT (thinking chain). Given the requirements in natural language form, it automatically generates the prompt content including question and answer examples required in the COT process.
[0022] 3. The present invention combines the built-in natural language synonyms, mathematical concepts and formula extraction capabilities of LLM with the fast and accurate search method of traditional relational databases to achieve accurate mapping and fast query of related software processes in the field of abstract mathematical concepts. BRIEF DESCRIPTION OF THE DRAWINGS
[0023] Figure 1 It is a flow chart of the method of the present invention. DETAILED DESCRIPTION
[0024] Explanation of related terms:
[0025] LLM: Large Language Model, large language model;
[0026] cot:Chain of thought, chain of thought;
[0027] COT Example: An example that guides LLM to implement the COT process. It is a set of self-questioning and self-answering content that shows how to infer the COT process from the input to the output result.
[0028] Cot prompt: the prompt obtained by adding the user input requirements to the Cot example;
[0029] lapack: Linear Algebra Package, written in Fortran, is an open source computing library for linear algebra calculations. It is mainly used to solve complex mathematical and physical problems and provides interfaces for both Fortran and C programming languages.
[0030] The present invention is further described in detail below in conjunction with embodiments and drawings, but the embodiments of the present invention are not limited thereto.
[0031] The present invention is a method for recommending function names of lapack code libraries based on thought chains and logical reasoning, comprising the following steps:
[0032] S1. Build lapack function database, keywords database and cot question and answer examples;
[0033] Among them, the construction of the lapack function database is specifically as follows:
[0034] Refer to the user documentation to create multiple tables corresponding to different problem types. The fields in each table include function name and key attributes; key attributes include the method used, matrix type processed, storage method, data precision, data type and programming language;
[0035] The construction of the keywords database is as follows:
[0036] Create a table with fields including function name, problem type, and key method; the problem type is the linear algebra problem that the function is used to solve, and the key method is the information about the implementation method extracted from the function description;
[0037] The cot question and answer example is specifically an example of extracting key attributes from programming requirements.
[0038] S2. Add the input programming requirements to the cot question-and-answer example to obtain the cot prompt content (cot prompt). According to the cot prompt content, LLM is used to infer the key attributes of the lapack function required for programming except the implementation method, and the key attributes are verified; specifically, the following are included:
[0039] According to the COT prompts, LLM is used to extract key attributes from programming requirements. The extracted key attributes include problem type, matrix type, storage method, data accuracy, data type and programming language.
[0040] Verify the key attributes extracted by LLM. If the output processed matrix type and storage method are missing or exceed the specified range, they are set to the default value of general. If the data precision, data type, and programming language are missing or exceed the specified range, they are set to the default value of None. The fields set to the default value will not participate in the subsequent retrieval of function names in the lapack function database based on the key attributes.
[0041] Perform fuzzy matching between programming requirements and key methods in the keywords database to obtain a key method set; retrieve the problem type set corresponding to the key method set obtained in the keywords database, and use the problem type set to verify the problem type extracted by LLM.
[0042] S3. Use the verified key attributes to search for available implementation methods in the lapack function database, and use LLM to select the implementation method to be used; specifically:
[0043] According to the verified problem type, processed matrix type, storage method, data precision, data type and programming language, the available implementation methods are retrieved in the lapack function database, and the implementation method to be used is selected according to the programming requirements using LLM.
[0044] S4. Search the lapack function database according to the verified key attributes and the implementation method selected by LLM to obtain the first function name search result; specifically:
[0045] Using the verified problem type, processed matrix type, storage method, data precision, data type, and programming language and the implementation method selected by LLM, a full keyword search is performed in the lapack function database to obtain the first function name set.
[0046] S5. Search the keywords database according to programming requirements to obtain the second function name search result; specifically:
[0047] Perform fuzzy matching between programming requirements and key methods in the keywords database to obtain a key method set, retrieve the function names corresponding to the set in the keywords database, and obtain a second function name set.
[0048] S6, combining the two function name search results, and obtaining the recommended lapack code library function name through logical reasoning;
[0049] Among them, logical reasoning obtains the recommended lapack code library function name, specifically:
[0050] According to the first function name set and the second function name set, if both sets are non-empty, the intersection of the two sets is taken as the recommended lapack code library function name;
[0051] If one of the two sets is non-empty, the non-empty set is used as the recommended lapack code library function name.
[0052] Example
[0053] The programming requirements input in this embodiment are:
[0054] "Write a C program that computes the minimum norm solution to the complex linear least squares problem to minimize ||A*XB||, where A is an M×N matrix that may be rank-deficient, using a completely orthogonal decomposition of A."
[0055] The objective function of this embodiment is named: LAPACKE_zgelsy; the codegemma-1.1-7b model is used as the LLM to complete the information extraction process. The process of this embodiment includes:
[0056] Step S1, the lapack function database sample constructed in this embodiment is shown in Table 1 below;
[0057]
[0058] Table 1
[0059] The constructed COT question-and-answer example is as follows:
[0060] "Example 1:
[0061] Extract information about linear algebra operations according to the following tasks.
[0062] Note that the options for Operation in Output include Nonlinear Equation Problem, Linear Equation Problem, Linear Least Squares Problem (LLS), Linear Equality Constrained Least Squares Problem, Generalized (Gauss-Markov) Linear Model Problem (GLM), Standard Symmetric Eigenvalue Problem, Standard Asymmetric Eigenvalue Problem, Standard Singular Value Decomposition, Generalized Symmetric Positive Definite Eigenvalue Problem, Generalized Asymmetric Eigenvalue Problem, Generalized Singular Value Decomposition, and Orthogonal Decomposition.
[0063] Input: "Write a C program to compute the solution of the real linear equation system Ax=B, where A is an m×n general banded matrix (single precision), and x and B are both n×NRHS matrices (single precision)".
[0064] Since the matrix size is m×n, the operation is a linear least squares problem (LLS). If the matrix size is n×n, the operation is a linear equation problem.
[0065] Output: {Operation="Linear Least Squares(LLS)Problems", precision="single", matrix_storage_scheme="band", matrix_type="general", data_type="real", program="C"}".
[0066] Step S2: Add the constructed COT question-and-answer example to the input programming requirements to form the COT prompt content (COT prompt) of the input large model, and input the COT prompt content into the codegemma-1.1-7b LLM; the COT prompt content is as follows:
[0067] "Example 1:
[0068] Extract information about linear algebra operations according to the following tasks.
[0069] Note that the options for Operation in Output include Nonlinear Equation Problem, Linear Equation Problem, Linear Least Squares Problem (LLS), Linear Equality Constrained Least Squares Problem, Generalized (Gauss-Markov) Linear Model Problem (GLM), Standard Symmetric Eigenvalue Problem, Standard Asymmetric Eigenvalue Problem, Standard Singular Value Decomposition, Generalized Symmetric Positive Definite Eigenvalue Problem, Generalized Asymmetric Eigenvalue Problem, Generalized Singular Value Decomposition, and Orthogonal Decomposition.
[0070] Input: "Write a C program to compute the solution of the real linear equation system Ax=B, where A is an m×n general banded matrix (single precision), and x and B are both n×NRHS matrices (single precision)".
[0071] Since the matrix size is m×n, the operation is a linear least squares problem (LLS). If the matrix size is n×n, the operation is a linear equation problem.
[0072] Output:{Operation="Linear
[0073] Equations",precision="single",matrix_storage_scheme="band",matrix_type="general",data_type="real",program="C"}
[0074] Extract information based on the following tasks:
[0075] Input: "Write a C program that computes the minimum norm solution to the complex linear least squares problem to minimize ||A*XB||, where A is an M×N matrix that may be rank-deficient, using a completely orthogonal decomposition of A".
[0076] The output of the codegemma-1.1-7b model is:
[0077] "{
[0078] "Operation":"Linear Least Squares(LLS)Problems",
[0079] "precision":"complex",
[0080] "matrix_storage_scheme":"not specified",
[0081] "matrix_type":"general",
[0082] "data_type":"complex",
[0083] "program":"C"
[0084] }”
[0085] Among them, "Operation" indicates the type of problem, "precision" indicates the data precision, "matrix_storage_scheme" indicates the matrix storage type, "matrix_type" indicates the matrix type, "data_type" indicates the data type, and "program" indicates the programming language.
[0086] Verify "precision", "matrix_storage_scheme", "matrix_type", "data_type", and "program" output by codegemma-1.1-7b.
[0087] From the output of codegemma-1.1-7b, we can see that the output of "matrix_storage_scheme", i.e. the matrix storage type, is not within the specified range, so it is set to the default value "general". The output of "precision" is not within the specified range, so it is set to the default value "None". The remaining key attributes are within the specified range, and the verification passes.
[0088] Verify the "Operation" output by codegemma-1.1-7b in combination with the keywords database.
[0089] The programming requirements are fuzzy matched with the key method fields in the keywords database to obtain a set of key linear algebra calculation methods. The problem type fields corresponding to the key linear algebra calculation methods in the set are retrieved to obtain the problem set {"Linear Equations", "Linear Least Squares (LLS) Problems"}. The "Operation" output by codegemma-1.1-7b is "Linear Least Squares (LLS) Problems". In the above problem type set, the verification passes.
[0090] Step S3: According to the verified "Operation", "precision", "matrix_storage_scheme",
[0091] "matrix_type", "data_type", "program" search for available implementation methods in the lapack function database. The search results are {"solve LLS using QR or LQ factorization", "solve LLS using complete orthogonal factorization", "solve LLS using SVD", "solve LLS using divide-and-conquer SVD"}. Codegemma-1.1-7b is asked which method is recommended based on programming requirements. The specific query content is as follows:
[0092] Given the description "Write a C program that computes the minimum norm solution to the complex linear least squares problem to minimize ||A*XB||, where A is a possibly rank-deficient M×N matrix, using a completely orthogonal decomposition of A.", which of the following methods would you recommend to solve the problem: Solve LLS using QR or LQ decomposition, Solve LLS using a completely orthogonal decomposition, Solve LLS using SVD, Solve LLS using divide-and-conquer SVD.
[0093] Just give the option name".
[0094] The implementation method of codegemma-1.1-7b output is:
[0095] "solve LLS using complete orthogonal factorization".
[0096] Step S4: Use the "Operation", "precision", and "matrix_storage_scheme" verified in S2.
[0097] "matrix_type", "data_type", "program" and the implementation method "solve LLS using complete orthogonal factorization" selected by codegemma-1.1-7b use full keyword search in the lapack function database to obtain the first function name set: {LAPACKE_zgelsy,LAPACKE_cgelsy,LAPACKE_sgelsy,LAPACKE_dgelsy}.
[0098] Step S5, fuzzy match the programming requirements with the key methods in the keywords database to obtain a key method set, retrieve the function names corresponding to the set in the keywords database, and obtain a second function name set: {sptsvx, cptsvx, zgelsy, dgelsy, dptsvx, cgelsy, sgelsy, zptsvx, LAPACKE_zgelsy, LAPACKE_cgelsy, LAPACKE_sgelsy, LAPACKE_dgelsy, LAPACKE_cptsvx, LAPACKE_sptsvx, LAPACKE_zptsvx, LAPACKE_dptsvx}.
[0099] Step S6, merging the first function set and the second function set (logical reasoning). Since both the first function set and the second function set are non-empty, the intersection of the two sets is {LAPACKE_zgelsy, LAPACKE_cgelsy, LAPACKE_sgelsy, LAPACKE_dgelsy};
[0100] The merged result contains the correct function LAPACKE_zgelsy, so the recommended function is successful.
[0101] In another embodiment, a lapack code library function name recommendation system based on thought chain and logical reasoning is provided, the system adopts the lapack code library function name recommendation method of the above embodiment, and the system includes a database construction module, a large language model module, a verification and retrieval module, and a reasoning output module;
[0102] Database construction module, used to build lapack function database, keywords database and cot question and answer examples;
[0103] The large language model module infers the key attributes of the lapack function required for programming in addition to the implementation method based on the input programming requirements and the COT prompt content obtained from the COT question-and-answer example, and is also used to select the implementation method to be used;
[0104] The verification and retrieval module is used to retrieve and verify the key attributes inferred by the large language model module, and is also used to search the lapack function database and the keywords database to obtain the first function name search result and the second function name search result;
[0105] The reasoning output module is used to perform logical reasoning based on the two function name retrieval results and output the recommended lapack code library function name.
[0106] It should also be noted that in this specification, terms such as "comprises", "includes" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also includes other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of more restrictions, an element defined by the sentence "comprises a ..." does not exclude the presence of other identical elements in the process, method, article or device including the element.
[0107] The above description of the disclosed embodiments enables those skilled in the art to implement or use the present invention. Various modifications to these embodiments will be apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present invention. Therefore, the present invention will not be limited to the embodiments shown herein, but rather to the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. A method for recommending function names of lapack code libraries based on thought chains and logical reasoning, characterized in that: The following steps are involved: S1. Build lapack function database, keywords database and cot question and answer examples; S2. Add the input programming requirements to the cot question-and-answer example to obtain the cot prompt content. According to the cot prompt content, LLM is used to infer the key attributes of the lapack function required for programming except the implementation method, and the key attributes are verified; S3, using the verified key attributes to search for available implementation methods in the lapack function database, and using LLM to select the implementation method to be used; S4. Search the lapack function database according to the verified key attributes and the implementation method selected by LLM to obtain a first function name search result; S5. Search the keywords database according to programming requirements to obtain a second function name search result; S6. Combining the two function name search results, logical reasoning is performed to obtain the recommended lapack code library function name.
2. The lapack code library function name recommendation method based on thought chain and logical reasoning according to claim 1 is characterized in that: The construction of the lapack function database is as follows: Create multiple tables corresponding to different problem types. The fields in each table include function name and key attributes. Key attributes include the method used, matrix type processed, storage method, data precision, data type, and programming language. The construction of the keywords database is as follows: Create a table with fields including function name, problem type, and key method; the problem type is the linear algebra problem that the function is used to solve, and the key method is the information about the implementation method extracted from the function description; The cot question and answer example is specifically an example of extracting key attributes from programming requirements.
3. The lapack code library function name recommendation method based on thought chain and logical reasoning according to claim 2 is characterized in that: Step S2 specifically includes: According to the COT prompts containing COT question and answer examples, LLM is used to extract key attributes from programming requirements. The extracted key attributes include question type, matrix type processed, storage method, data precision, data type and programming language. Verify the key attributes extracted by LLM. If the output processed matrix type and storage method are missing or exceed the specified range, they are set to the default value of general. If the data precision, data type, and programming language are missing or exceed the specified range, they are set to the default value of None. The fields set to the default value will not participate in the subsequent retrieval of function names in the lapack function database based on the key attributes. Perform fuzzy matching between programming requirements and key methods in the keywords database to obtain a key method set; retrieve the problem type set corresponding to the key method set obtained in the keywords database, and use the problem type set to verify the problem type extracted by LLM.
4. The lapack code library function name recommendation method based on thought chain and logical reasoning according to claim 3 is characterized in that: Step S3 is specifically as follows: According to the verified problem type, processed matrix type, storage method, data precision, data type and programming language, the available implementation methods are retrieved in the lapack function database, and the implementation method to be used is selected according to the programming requirements using LLM.
5. The lapack code library function name recommendation method based on thought chain and logical reasoning according to claim 4 is characterized in that: Step S4 is specifically as follows: Using the verified problem type, processed matrix type, storage method, data precision, data type, and programming language and the implementation method selected by LLM, a full keyword search is performed in the lapack function database to obtain the first function name set.
6. The lapack code library function name recommendation method based on thought chain and logical reasoning according to claim 5 is characterized in that: Step S5 is specifically as follows: Perform fuzzy matching between programming requirements and key methods in the keywords database to obtain a key method set, retrieve the function names corresponding to the set in the keywords database, and obtain a second function name set.
7. The lapack code library function name recommendation method based on thought chain and logical reasoning according to claim 6 is characterized in that: In step S6, logical reasoning is used to obtain the recommended lapack code library function name, which is specifically: According to the first function name set and the second function name set, if both sets are non-empty, then the intersection of the two sets is taken as the recommended lapack code library function name; If one of the two sets is non-empty, the non-empty set is used as the recommended lapack code library function name.
8. A method for recommending function names of lapack code libraries based on thought chains and logical reasoning, characterized in that: The system adopts the lapack code library function name recommendation method according to any one of claims 1 to 7, and the system includes a database construction module, a large language model module, a verification and retrieval module, and an inference output module; Database construction module, used to build lapack function database, keywords database and cot question and answer examples; The large language model module infers the key attributes of the lapack function required for programming in addition to the implementation method based on the input programming requirements and the COT prompt content obtained from the COT question-answering example, and is also used to select the implementation method to be used; The verification and retrieval module is used to retrieve and verify the key attributes inferred by the large language model module, and is also used to search the lapack function database and the keywords database to obtain the first function name retrieval result and the second function name retrieval result; The reasoning output module is used to perform logical reasoning based on the two function name retrieval results and output the recommended lapack code library function name.
Citation Information
Patent Citations
Code generation method and device based on semantic thinking chain, terminal and medium
CN118113272A
Metadata feature and thinking chain-based multi-table associated large language model question and answer method
CN118245591A
Knowledge intensive question reasoning and generating method based on LLM
CN118798367A
Prompting Machine-Learned Models Using Chains of Thought
US20230394328A1
Chain of thought reasoning for LLM integrations in spreadsheet environments
WO2024191645A1