Method and system for recommending function name of lapack code library based on thought chain and logical reasoning
By constructing a Lapack codebase function name recommendation method based on thought chaining and logical reasoning, the problem of low code generation accuracy in the HPC field in existing technologies is solved. It achieves accurate recommendation across different historical versions and programming languages, improving the accuracy and efficiency of function name recommendation.
Patent Information
- Application Number
- CN202411661430.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-20
- Publication Date
- 2025-11-04
- Estimated Expiration
- 2044-11-20
AI Technical Summary
Existing general-purpose large language models have low accuracy in generating and completing code for the lapack library in the HPC field, and fine-tuning requires a lot of training time and resources, making it difficult to cope with the interface differences between different hardware architectures and programming languages.
We employ a thought chain and logical reasoning approach to construct a LAPACK function database and a keywords database. We generate CT prompts using CT question-and-answer examples, infer and validate key attributes using LLM, and recommend function names by combining the LAPACK function database and the keywords database.
It improves the accuracy of function name recommendations across different historical versions and programming languages, without requiring LLM readjustment, automatically identifies interface differences, and performs fast and accurate queries by combining natural language and mathematical concepts.
Smart Images

Figure CN119668579B_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of code completion and generation technology, specifically relating to a method and system for recommending function names in the Lapack codebase based on thought chains and logical reasoning. Background Technology
[0002] With the development of deep learning technology, especially natural language processing technology, more and more LLMs have emerged, such as the GPT series models and the Code Llama series models. These LLMs can communicate with users and answer their questions, generate code implementations based on user requirements, or complete incomplete code provided by users.
[0003] However, most LLMs are designed for general-purpose applications. For the LAPACK library in the HPC domain, solving a problem can involve calling multiple functions, and different functions correspond to different hardware architectures and programming languages. Even for the same function, the interfaces in Fortran and C programming languages are different in both name and parameter list. This makes it difficult for general-purpose LLMs to generate or complete code using the LAPACK library. However, providing the correct LAPACK function name as a hint can greatly improve the accuracy of code generation.
[0004] Currently, there are some LLMs for HPC code generation, but they are usually fine-tuned from general LLMs based on collected HPC codebases, requiring significant training time and resources. Furthermore, the amount of code using the `lapack` function is relatively small, which makes it very difficult to fine-tune the LLM to improve code generation accuracy. Summary of the Invention
[0005] The main objective of this invention is to overcome the shortcomings and deficiencies of the prior art and to propose a method and system for recommending function names in the Lapack codebase based on thought chain and logical reasoning.
[0006] To achieve the above objectives, the present invention adopts the following technical solution:
[0007] The Lapack codebase function name recommendation method based on thought chaining and logical reasoning includes the following steps:
[0008] S1. Construct the LAPACK function database, keywords database, and a cot question-and-answer example;
[0009] S2. Add the input programming requirements to the cot question-and-answer example to obtain the cot prompt content. Based on the cot prompt content, use LLM to infer the key properties of the LApack function required for programming, excluding the implementation method, and verify the key properties.
[0010] S3, retrieve the available implementation methods in the lapack function database using the verified key attributes, and select the used implementation method using the LLM;
[0011] S4, retrieve in the lapack function database according to the verified key attributes and the implementation method selected by the LLM, and obtain a first function name retrieval result;
[0012] S5, retrieve in the keywords database according to the programming requirements, and obtain a second function name retrieval result;
[0013] S6, combine the two function name retrieval results, and logically infer to obtain a recommended lapack code library function name.
[0014] The application also includes a lapack code library function name recommendation method based on a thinking chain and logical inference.
[0015] The database construction module is used to construct the lapack function database, the keywords database, and the cot question and answer examples.
[0016] The large language model module is used to infer the key attributes of the required lapack function for programming except the implementation method according to the cot prompt content obtained by inputting the programming requirements and the cot question and answer examples, and is also used to select the used implementation method.
[0017] The verification and retrieval module is used to verify and retrieve the key attributes inferred by the large language model module, and is also used to retrieve in the lapack function database and the keywords database to obtain the first function name retrieval result and the second function name retrieval result.
[0018] The inference output module is used to logically infer according to the two function name retrieval results, and output the recommended lapack code library function name.
[0019] Compared with the prior art, the application has the following advantages and beneficial effects:
[0020] 1. The method of the application can automatically identify the interface differences of different historical versions of the same software package and programming languages, and improve the accuracy of function name recommendation.
[0021] 2. The application is based on a special cot (thinking chain), and the prompt content containing the question and answer examples required in the cot is automatically generated after the natural language form requirement is given.
[0022] 3. The present application combines the built-in natural language synonym extraction capabilities of LLM, mathematical concepts and formula extraction capabilities with the fast and accurate search method of traditional relational databases to achieve accurate mapping and fast query of software processes related to the field of abstract mathematical concepts. BRIEF DESCRIPTION OF DRAWINGS
[0023] Figure 1 is a flowchart of the method of the present application. DETAILED DESCRIPTION
[0024] Related terms are explained as follows:
[0025] LLM: Large Language Model, large language model
[0026] cot: Chain of thought, thought chain
[0027] cot example: an example of guiding LLM to implement the cot process, which is a set of self-asking and self-answering content, showing how to reason from input to output result cot process
[0028] cot prompt content: prompt content obtained by adding cot example to user input requirements
[0029] lapack: Linear Algebra Package, an open source computing library written in Fortran language for linear algebra calculation, mainly used to solve complex mathematical and physical problems, providing Fortran and C programming language interfaces.
[0030] The present application will be described in further detail below in conjunction with the embodiments and drawings, but the embodiments of the present application are not limited thereto.
[0031] The present application, a lapack code library function name recommendation method based on thought chain and logical reasoning, comprises the following steps:
[0032] S1, constructing a lapack function database, a keywords database and a cot question and answer example;
[0033] Specifically, the construction of the lapack function database is as follows:
[0034] Referring to the user document, a plurality of tables corresponding to different problem types are created, and the fields in each table include function name and key attributes; the key attributes include the method used, the matrix type processed, the storage method, the data precision, the data type and the programming language;
[0035] The construction of the keywords database is as follows:
[0036] create a table, the fields in the table include function name, problem type and key method; the problem type is a linear algebra problem solved by the function, and the key method is information related to the implementation method extracted from the function description;
[0037] The cot question and answer example is an example of extracting key attributes from programming requirements.
[0038] S2, add the input programming requirement to the cot question and answer example to obtain the cot prompt content (cot prompt), and use LLM to infer the key attributes of the required lapack function for programming except the implementation method according to the cot prompt content, and check the key attributes; Specifically, it includes:
[0039] According to the cot prompt content, LLM extracts key attributes from programming requirements, including problem type, matrix type, storage method, data precision, data type and programming language;
[0040] The key attributes extracted by LLM are checked. If the output matrix type and storage method are missing or beyond the specified range, set to the default value general, if the data precision, data type and programming language are missing or beyond the specified range, set to the default value None; The field set to the default value does not participate in the subsequent retrieval of function name according to the key attributes in the lapack function database;
[0041] Fuzzy matching of programming requirements and keywords in the database of keywords to obtain a set of key methods; The problem type set corresponding to the set of key methods obtained by searching in the keywords database is used to check the problem type extracted by LLM.
[0042] S3, use the checked key attributes to search for available implementation methods in the lapack function database, and use LLM to select the used implementation method; Specifically:
[0043] According to the checked problem type, matrix type, storage method, data precision, data type and programming language, search for available implementation methods in the lapack function database, and use LLM to select the used implementation method according to the programming requirement.
[0044] S4, according to the checked key attributes and the implementation method selected by LLM, search in the lapack function database to obtain the first function name retrieval result; Specifically:
[0045] The first function name set is obtained by using full-keyword retrieval in the lapack function database using the verified problem type, the processed matrix type, the storage mode, the data precision, the data type, and the programming language, and the implementation method selected by the LLM.
[0046] S5, retrieve according to the programming requirements in the keywords database to obtain a second function name retrieval result; specifically:
[0047] The programming requirements are fuzzy matched with the key methods in the keywords database to obtain a key method set, the function name corresponding to the set is retrieved in the keywords database, and a second function name set is obtained.
[0048] S6, comprehensive two function name retrieval results, logical reasoning to obtain the recommended lapack code library function name;
[0049] Among them, the recommended lapack code library function name is obtained by logical reasoning, 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 taken as the recommended lapack code library function name.
[0052] Embodiment
[0053] The programming requirements input in this embodiment are:
[0054] "Write a C program to calculate the minimum norm solution of the complex linear least squares problem to minimize ||A*X-B||, where A is an MxN matrix that may be rank-deficient, and use the complete orthogonal decomposition of A to achieve it".
[0055] The target function name of this embodiment is: 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 examples are specifically:
[0060] "Example 1:
[0061] Extract the information of linear algebra operation according to the following task.
[0062] Note that the options of "Operation" in "Output" include "nonlinear equation problems, linear equation problems, linear least squares problems (LLS), linear equality-constrained least squares problems, generalized (Gauss-Markov) linear model problems (GLM), standard symmetric eigenvalue problems, standard nonsymmetric eigenvalue problems, standard singular value decomposition, generalized symmetric positive definite eigenvalue problems, generalized nonsymmetric eigenvalue problems, generalized singular value decomposition, and orthogonal decomposition".
[0063] Input: "Write a C program to solve the linear equation system Ax = B, where A is a general band matrix of m x n (single precision), x and B are n x NRHS matrices (single precision)".
[0064] Since the matrix size is m x n, the operation is a linear least squares problem (LLS). If the matrix size is n x 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 requirement to form the cot prompt content of the input large model (cot prompt), and input the cot prompt content to codegemma-1.1-7b LLM; the cot prompt content is as follows:
[0067] "Example 1:
[0068] Extract the information of linear algebra operation according to the following task.
[0069] Note that the options of "Operation" in "Output" include "nonlinear equation problems, linear equation problems, linear least squares problems (LLS), linear equality-constrained least squares problems, generalized (Gauss-Markov) linear model problems (GLM), standard symmetric eigenvalue problems, standard nonsymmetric eigenvalue problems, standard singular value decomposition, generalized symmetric positive definite eigenvalue problems, generalized nonsymmetric eigenvalue problems, generalized singular value decomposition, and orthogonal decomposition".
[0070] Input: "Write a C program to solve the linear system Ax = B for real numbers, where A is a general banded matrix of size m x n (single precision), and x and B are n x NRHS matrices (single precision)."
[0071] Since the matrix size is m x n, the operation is a linear least squares problem (LLS). If the matrix size is n x n, the operation is a linear equation problem.
[0072] Output: {Operation="Linear Equations", precision="single", matrix_storage_scheme="band", matrix_type="general", data_type="real", program="C"}
[0073]
[0074] According to the following task, extract the information:
[0075] Input: "Write a C program to compute the least-norm solution of a complex linear least squares problem to minimize ||A*X-B||, where A is an M x N matrix that may be rank-deficient, using a complete orthogonal decomposition of A."
[0076] The output for 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] "Operation" represents the type of the problem, "precision" represents the data precision, "matrix_storage_scheme" represents the matrix storage type, "matrix_type" represents the matrix type, "data_type" represents the data type, and "program" represents the programming language.
[0086] The "precision", "matrix_storage_scheme", "matrix_type", "data_type", and "program" output by codegemma-1.1-7b are checked.
[0087] According to the above output by codegemma-1.1-7b, the output of "matrix_storage_scheme" is not within the specified range, so it is set to the default value "general", and 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 pass the check.
[0088] The "Operation" output by codegemma-1.1-7b is checked in combination with the keywords database.
[0089] The programming requirements are fuzzy matched with the key method field in the keywords database to obtain a set of key linear algebra calculation methods. The corresponding problem type field of the key linear algebra calculation method in the set is retrieved to obtain a set of problem types {"Linear Equations", "Linear Least Squares (LLS) Problems"}. The "Operation" output by codegemma-1.1-7b is "Linear Least Squares (LLS) Problems", which is within the above set of problem types and passes the check.
[0090] In step S3, the "Operation", "precision", "matrix_storage_scheme", "matrix_type", "data_type", and "program" that have passed the check are used as input parameters of the codegemma-1.1-7b.
[0091] "matrix_type", "data_type", "program" in the lapack function database to retrieve available implementation methods, the results of the retrieval 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"}, ask codegemma-1.1-7b to recommend which method to use according to the programming requirements, the specific inquiry content is as follows:
[0092] "According to "write a C program to calculate the minimum norm solution of a complex linear least squares problem to minimize ||A*X-B||, where A is an MxN matrix that may be rank-deficient, and use the complete orthogonal decomposition of A to achieve it. " This description, which of the following options do you recommend to solve the problem: use QR or LQ decomposition to solve LLS, use complete orthogonal decomposition to solve LLS, use SVD to solve LLS, use divide-and-conquer SVD to solve LLS.
[0093] Only give the option name".
[0094] The implementation method output by codegemma-1.1-7b is:
[0095] "solve LLS using complete orthogonal factorization".
[0096] Step S4, use the "Operation", "precision", "matrix_storage_scheme",
[0097] "matrix_type", "data_type", "program" and the implementation method "solve LLS using complete orthogonal factorization" selected by codegemma-1.1-7b are used to retrieve the lapack function database using full keywords, and the first function name set is obtained: {LAPACKE_zgelsy, LAPACKE_cgelsy, LAPACKE_sgelsy, LAPACKE_dgelsy}.
[0098] Step S5, fuzzy matching the programming requirements with the keywords in the keywords database to obtain a set of key methods, searching the keywords database for the function names corresponding to the set to obtain a second set of function names: {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 (logical reasoning) the first function set and the second function set, since the first function set and the second function set are both non-empty, the intersection of the two sets is {LAPACKE_zgelsy, LAPACKE_cgelsy, LAPACKE_sgelsy, LAPACKE_dgelsy}.
[0100] The result of the merging contains the correct function LAPACKE_zgelsy, so the function recommendation is successful.
[0101] In another embodiment, a lapack code library function name recommendation system based on thought chains and logical reasoning is provided, the system adopts the lapack code library function name recommendation method of the above embodiment, and the system comprises a database construction module, a large language model module, a verification and retrieval module, and a reasoning output module.
[0102] The database construction module is configured to construct a lapack function database, a keywords database, and a cot question and answer example.
[0103] The large language model module is configured to infer the key attributes of the lapack function required for programming except the implementation method according to the cot prompt content obtained by inputting the programming requirements and the cot question and answer example, and to select the implementation method to be used.
[0104] The verification and retrieval module is configured to verify and retrieve the key attributes inferred by the large language model module, and to retrieve the lapack function database and the keywords database to obtain a first function name retrieval result and a second function name retrieval result.
[0105] The reasoning output module is configured to perform logical reasoning according to the two function name retrieval results, and to output a recommended lapack code library function name.
[0106] It is also to be noted that, as used in this specification and the appended claims, the singular forms "a," "an," and "the" include plural referents unless the context clearly dictates otherwise. Thus, for example, reference to "a component" can include a combination of two or more components, and the term "an element" can include comparable reference to a plurality of elements.
[0107] The foregoing description of the disclosed embodiments enables one skilled in the art to make or use the application. Numerous modifications of those embodiments can be made without departing from the spirit or scope of the application. Any reference to claimed subject matter presents what is believed to be the presentestant of the spirit and / or scope of the application in relation to the present disclosure. Any reference to claimed subject matter presents what is believed to be the presentestant of the spirit and / or scope of the application in relation to the present disclosure. Any reference to claimed subject matter presents what is believed to be the presentestant of the spirit and / or scope of the application in relation to the present disclosure. Any reference to claimed subject matter presents what is believed to be the presentestant of the spirit and / or scope of the application in relation to the present disclosure. Any reference to claimed subject matter presents what is believed to be the presentestant of the spirit and / or scope of the application in relation to the present disclosure. Any reference to claimed subject matter presents what is believed to be the presentestant of the spirit and / or scope of the application in relation to the present disclosure. Any reference to claimed subject matter presents what is believed to be the presentestant of the spirit and / or scope of the application in relation to the present disclosure. Any reference to claimed subject matter presents what is believed to be the presentestant of the spirit and / or scope of the application in relation to the present disclosure. Any reference to claimed subject matter presents what is believed to be the presentestant of the spirit and / or scope of the application in relation to the present disclosure. Any reference to claimed subject matter presents what is believed to be the presentestant of the spirit and / or scope of the application in relation to the present disclosure. Any reference to claimed subject matter presents what is believed to be the presentestant of the spirit and / or scope of the application in relation to the present disclosure. Any reference to claimed subject matter presents what is believed to be the presentestant of the spirit and / or scope of the application in relation to the present disclosure. Any reference to claimed subject matter presents what is believed to be the presentestant of the spirit and / or scope of the application in relation
Claims
1. A method for recommending function names of a lapack code library based on a thought chain and logical reasoning, characterized by, The method comprises the following steps: S1, constructing a lapack function database, a keywords database and a cot question and answer example; The construction of the keywords database is specifically: A table is created, and the fields in the table include function name, question type and key method; the question type is the linear algebra problem solved by the function, and the key method is the information related to the implementation method extracted from the function description; S2, adding the input programming requirement to the cot question and answer example to obtain the cot prompt content, using LLM to infer the key attributes of the required lapack function for programming except the implementation method according to the cot prompt content, and checking the key attributes; specifically including: According to the cot prompt content containing the cot question and answer example, the key attributes are extracted from the programming requirement by using LLM, and the extracted key attributes include question type, processed matrix type, storage mode, data precision, data type and programming language; The key attributes extracted by LLM are checked, if the output processed matrix type and storage mode are missing or beyond the specified range, the default value general is set, if the data precision, data type and programming language are missing or beyond the specified range, the default value None is set; the fields set as default values do not participate in the subsequent retrieval of function name in the lapack function database according to the key attributes; Fuzzy matching is performed between the programming requirement and the key methods in the keywords database to obtain a key method set; the question type set corresponding to the key method set obtained by searching in the keywords database is used to check the question type extracted by LLM; S3, using the checked key attributes to search the available implementation method in the lapack function database, and using LLM to select the used implementation method; S4, according to the checked key attributes and the implementation method selected by LLM, searching in the lapack function database to obtain a first function name retrieval result; S5, searching in the keywords database according to the programming requirement to obtain a second function name retrieval result; S6, combining the two function name retrieval results, logically inferring to obtain the recommended lapack code library function name. 2.The method of recommending a function name of a lapack code library based on a thought chain and logical reasoning according to claim 1, wherein, The construction of the lapack function database is specifically: A plurality of tables corresponding to different question types are created, and the fields in each table include function name and key attributes; the key attributes include used method, processed matrix type, storage mode, data precision, data type and programming language; The cot question and answer example is specifically an example of extracting key attributes from the programming requirement. 3.The method of recommending a function name of a lapack library based on a thought chain and logical reasoning according to claim 2, characterized in that, Step S3 is specifically: According to the checked question type, processed matrix type, storage mode, data precision, data type and programming language, searching the available implementation method in the lapack function database, and using LLM to select the used implementation method according to the programming requirement.
4. The method of claim 3, wherein the method is characterized by, Step S4 is specifically: The first function name set is obtained by using full-keyword retrieval in the lapack function database using the verified problem type, the processed matrix type, the storage method, the data precision, the data type, and the programming language and the implementation method selected by the LLM.
5. The method of claim 4, wherein the method is based on a thought chain and logical reasoning. Step S5 is specifically: Fuzzy matching of the programming requirements and the keywords in the keywords database obtains a set of key methods, and the function names corresponding to the set are retrieved in the keywords database to obtain a second function name set.
6. The method of claim 5, wherein the method is based on a thought chain and logical reasoning. In step S6, the logic reasoning obtains the recommended lapack code library function name, specifically: 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; If one of the two sets is non-empty, the non-empty set is taken as the recommended lapack code library function name.
7. A lapack library function name recommendation system based on thought chain and logical reasoning, characterized in that, The system adopts the lapack code library function name recommendation method of any one of claims 1-6, and the system comprises a database construction module, a large language model module, a verification and retrieval module, and a reasoning output module; The database construction module is used to construct the lapack function database, the keywords database, and the cot question and answer examples; The large language model module reasons out the key attributes of the lapack function required for programming except the implementation method according to the cot prompt content obtained by inputting the programming requirements and the cot question and answer examples, and is also used to select the implementation method used; The verification and retrieval module is used to retrieve and verify the key attributes reasoned out by the large language model module, and is also used to retrieve the first function name retrieval result and the second function name retrieval result in the lapack function database and the keywords database; The reasoning output module is used to perform logic reasoning according to the two function name retrieval results and output the recommended lapack code library function name.
Citation Information
Patent Citations
Metadata feature and thinking chain-based multi-table associated large language model question and answer method
CN118245591A