Code migration method and device, computer device, storage medium and program product
By employing a code porting method that utilizes large language models for assisted recognition and multi-strategy execution, the problems of low efficiency and poor reliability in code porting are solved. This method achieves an efficient and reliable code porting process and generates readable and maintainable target code files.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- MOORE THREADS TECHNOLOGY (SHANGHAI) CO LTD
- Filing Date
- 2026-06-17
- Publication Date
- 2026-07-21
AI Technical Summary
Existing code porting methods suffer from inefficiency and poor reliability. In particular, when porting deep learning code from a specific AI chip to a replaceable AI chip, manual porting is extremely time-consuming, while automated solutions lack security and the ability to utilize enterprise proprietary knowledge.
It employs a large language model to assist in code block and attribute recognition, combines internal enterprise documents and toolchains, performs code porting through multi-strategy execution, identifies standard and non-standard interface types, selects appropriate porting strategies based on difficulty, and generates clearly commented target code files.
It improves the efficiency and success rate of code porting, ensures the readability and maintainability of the generated code, reduces the time required for manual intervention, and integrates internal enterprise knowledge to improve the controllability and reliability of the porting process.
Smart Images

Figure CN122431719A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of artificial intelligence technology, and in particular to a code porting method, apparatus, computer equipment, storage medium, and program product. Background Technology
[0002] In the fields of artificial intelligence and high-performance computing, general-purpose parallel computing platforms and programming models have emerged, enabling collaborative computing between CPUs and GPUs. However, because this general-purpose programming model relies on a specific AI chip, it presents certain obstacles in practical applications. With the development of AI chip and GPU performance, some alternative AI chips have emerged. When enterprises adopt non-specific AI chips, they need to port the code of the programming model (such as deep learning code) from the source hardware backend of the specific AI chip to the target hardware backend of the alternative AI chip.
[0003] In related technologies, code migration can be performed manually, or batch replacement can be performed using rule-based scripts, or an end-to-end code generation model can be directly adopted.
[0004] However, the code porting methods in related technologies all have certain shortcomings, resulting in poor overall code porting performance. Summary of the Invention
[0005] Therefore, it is necessary to provide a code porting method, apparatus, computer equipment, computer-readable storage medium, and computer program product that can improve the overall effect of code porting in response to the above-mentioned technical problems.
[0006] Firstly, this application provides a code porting method, including:
[0007] Obtain the code file to be ported, and perform code block identification processing on the code file to be ported to determine at least one code block to be ported;
[0008] Determine at least one attribute information corresponding to each code block to be ported, and determine the target porting strategy corresponding to each code block to be ported based on each attribute information;
[0009] Based on the target porting strategy, each code block to be ported is ported separately to obtain the target code file corresponding to the code file to be ported.
[0010] In one embodiment, determining at least one attribute information corresponding to each code block to be ported includes:
[0011] For each code block to be ported, identify the interface information within that code block.
[0012] Based on the interface information and the preset standard interface library, determine the interface type, and determine the attribute information of the code block to be ported based on the interface type; the interface type can be a standard interface type or a non-standard interface type.
[0013] In one embodiment, the interface type is determined based on interface information and a preset standard interface library, and the attribute information of the code block to be ported is determined based on the interface type, including:
[0014] If the interface information is included in the preset standard interface library, the interface type is determined to be the standard interface type, and the standard interface type is used as the attribute information of the code block to be ported.
[0015] If the interface corresponding to the interface information is not included in the preset standard interface library, the interface type is determined to be a non-standard interface type. The difficulty of the code block to be ported is quantified, and the porting difficulty of the code block to be ported is determined based on the confidence level obtained by quantification. The non-standard interface type and porting difficulty are used as attribute information of the code block to be ported.
[0016] In one embodiment, the target porting strategy for each code block to be ported is determined based on various attribute information, including:
[0017] If the attribute information of the code block to be ported indicates that the code block to be ported is a code block corresponding to a standard interface type, then the target porting strategy corresponding to the code block to be ported is determined to be the first porting strategy; the first porting strategy includes using a code porting tool for code porting.
[0018] In one embodiment, the target porting strategy for each code block to be ported is determined based on various attribute information, including:
[0019] If the attribute information of the code block to be ported indicates that the code block to be ported is a code block corresponding to a non-standard interface type, and the porting difficulty of the code block to be ported is the first difficulty, then the target porting strategy corresponding to the code block to be ported is determined to be the second porting strategy; the second porting strategy includes using a preset porting knowledge base for code porting.
[0020] If the attribute information of the code block to be ported indicates that the code block to be ported is a code block corresponding to a non-standard interface type, and the porting difficulty of the code block to be ported is the second difficulty, then the target porting strategy corresponding to the code block to be ported is determined to be the third porting strategy; the third porting strategy includes manual code porting, and the first difficulty is less than the second difficulty.
[0021] In one embodiment, the preset porting knowledge base includes a static file library and / or a dynamic knowledge base, wherein the dynamic knowledge base is generated based on code blocks manually ported during the historical code porting process.
[0022] In one embodiment, the third porting strategy further includes: encapsulating the code block to be ported in a macro definition, generating a code stub of the code block to be ported, and retaining at least one of the code blocks to be ported.
[0023] In one embodiment, the target porting strategy for each code block to be ported is determined based on various attribute information, including:
[0024] Based on the attribute information, the target porting strategy for each code block to be ported is determined from the porting strategy library; the porting strategy library includes multiple different porting strategies.
[0025] In one embodiment, based on each target porting strategy, code porting is performed on each code block to be ported to obtain the target code file corresponding to the code file to be ported, including:
[0026] Based on the target porting strategy, each code block to be ported is ported separately to generate the target code block corresponding to each code block to be ported.
[0027] Replace the corresponding code block to be ported with the target code block corresponding to each code block to be ported, and add porting comments to each target code block to generate the target code file corresponding to the code file to be ported; the porting comments corresponding to the target code block shall at least include the target porting strategy corresponding to the target code block.
[0028] In one embodiment, obtaining the code file to be ported includes:
[0029] Obtain the code repository to be ported; the code repository to be ported includes multiple original code files;
[0030] Based on a preset file extension, source code files with the preset file extension are selected from multiple source code files and used as the source code files to be ported; and,
[0031] The source code files with non-preset file extensions from multiple source code files are input into a preset file intent recognition model for file intent recognition, and the source code files whose file intent is the preset porting file intent are used as the porting code files.
[0032] In one embodiment, the method further includes:
[0033] Create the target test program corresponding to the target code file; the target test program includes the test subroutines corresponding to the code blocks to be ported after the code porting is completed;
[0034] The target code file is tested using a target test program, and the test results are obtained.
[0035] Output the test results.
[0036] Secondly, this application also provides a code porting apparatus, comprising:
[0037] The acquisition module is used to acquire the code file to be ported and perform code block identification processing on the code file to be ported to determine at least one code block to be ported.
[0038] The determination module is used to determine at least one attribute information corresponding to each of the code blocks to be ported, and to determine the target porting strategy corresponding to each code block to be ported based on the attribute information.
[0039] The porting module is used to port each of the code blocks to be ported according to the target porting strategy, so as to obtain the target code file corresponding to the code file to be ported.
[0040] Thirdly, this application also provides a computer device, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to implement the steps of the code porting method in the first aspect described above.
[0041] Fourthly, this application also provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps of the code porting method described in the first aspect above.
[0042] Fifthly, this application also provides a computer program product, including a computer program that, when executed by a processor, implements the steps of the code porting method described in the first aspect above.
[0043] The aforementioned code porting method, apparatus, computer equipment, storage medium, and computer program product involve the computer equipment acquiring a code file to be ported and performing code block identification processing on the code file to determine at least one code block to be ported. Next, it determines at least one attribute information corresponding to each code block to be ported, and determines a target porting strategy corresponding to each code block based on the attribute information. Then, based on each target porting strategy, it performs code porting on each code block to obtain a target code file corresponding to the code file to be ported. In other words, the code porting method proposed in this application can adopt porting strategies matching the attributes for code blocks with different attributes. That is, for a single code file to be ported, multiple different porting strategies can be used to port different code blocks within that file. Compared to the traditional method of using a single porting strategy to process the entire code file, this improves the porting effect and thus increases the success rate of code porting. Attached Figure Description
[0044] To more clearly illustrate the technical solutions in the embodiments or related technologies of this application, the accompanying drawings used in the description of the embodiments or related technologies will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0045] Figure 1 This is a diagram illustrating the application environment of a code porting method in one embodiment;
[0046] Figure 2 This is a flowchart illustrating a code porting method in one embodiment;
[0047] Figure 3 This is a flowchart illustrating the code porting method in another embodiment;
[0048] Figure 4 This is a flowchart illustrating the code porting method in another embodiment;
[0049] Figure 5 This is a schematic diagram illustrating the complete process of a code porting method in one embodiment;
[0050] Figure 6 This is a structural block diagram of a code porting device in one embodiment;
[0051] Figure 7 This is an internal structural diagram of a computer device in one embodiment. Detailed Implementation
[0052] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application.
[0053] When porting program code from the source hardware backend of a specific AI chip to the target hardware backend of an alternative AI chip used by an enterprise, traditional methods often involve manual porting, script replacement, or code generation, but each method has its own drawbacks.
[0054] Manual porting is currently the most prevalent and reliable method, but also the least efficient. Senior developers must read the code line by line, relying on personal experience and documentation to manually modify the source hardware backend's API to match the target hardware backend's API. For non-standard APIs, the kernel code also needs to be rewritten. This method is entirely manual, relying on text editors for modification. When dealing with code repositories of millions of lines, manual modification is extremely time-consuming, severely slowing down project progress and resulting in extremely low code porting efficiency. Furthermore, the numerous simple API replacements consume valuable time from senior engineers, and repetitive work significantly slows down project progress. Additionally, porting experience cannot be shared among different engineers, easily leading to repeated pitfalls and creating knowledge silos.
[0055] Rule-based script batch replacement typically uses Sed, Grep, or regular expression scripts for keyword replacement. However, this method cannot handle logical changes, is prone to disrupting code structure, and cannot recognize context, leading to code migration failure. For example, if the source hardware backend API has two variable parameters and the target hardware backend API has three variable parameters, script replacement cannot successfully migrate the API from the source hardware backend to the target hardware backend, ultimately causing API migration failure.
[0056] End-to-end code generation models simply dump the code onto a general-purpose model (such as GPT-4) and require the API to be rewritten. When rewriting the API, a lack of domain expertise (a severe case of delusion) and an overconfident tendency to modify code the API doesn't understand can lead to hard-to-detect vulnerabilities and bugs.
[0057] In summary, purely manual solutions are too cumbersome and time-consuming, and cannot meet the needs of migrating large amounts of existing code. Existing automated solutions lack a sense of security, often generating code with hidden errors and a lack of traceability, making developers hesitant to use them. Furthermore, they lack utilization of enterprise proprietary knowledge, and general models are unaware of specific internal porting specifications or undisclosed hardware characteristics.
[0058] Based on this, this application aims to resolve the contradiction between "low efficiency of manual porting" and "poor reliability of full automation." By constructing an "assisted driving" type intelligent agent system, it can handle tedious, deterministic tasks (90% of the workload) and leave clear comments and markings in the code; for uncertain and complex logic, it chooses to "bypass" and prompts the developers, thereby ensuring that the system's output is readable, maintainable, and easy for developers to review.
[0059] This application relates to the fields of Artificial Intelligence, Software Engineering, and High-Performance Computing (HPC). Specifically, it relates to a method and system that utilizes Large Language Models (LLMs) as intelligent assistance, combined with internal enterprise documentation and toolchains, to help developers port deep learning code from source hardware backends to target hardware backends. Its aim is to provide a code porting system centered on assisting human development. This system does not seek to replace developers, but rather becomes an efficient assistant to them through intelligent filtering, automatic annotation, and multi-strategy execution. Furthermore, the system can integrate internal company documentation and past experience to ensure that the porting process is both efficient and controllable.
[0060] The code porting method provided in this application embodiment can be applied to, for example... Figure 1 In the application environment shown, computer device 101 may be equipped with a code porting system. This system can port program code (such as deep learning code) from the source hardware backend to the target hardware backend, thus obtaining program code suitable for the target hardware. For example, computer device 101 can be a computing device containing the source hardware, a computing device containing the target hardware, or a third-party computing device that does not contain either the source or target hardware. Furthermore, computer device 101 can be a terminal device or a server. The terminal can be, but is not limited to, various personal computers, laptops, smartphones, tablets, etc., and the server can be implemented using a standalone server or a server cluster composed of multiple servers.
[0061] In one exemplary embodiment, such as Figure 2 As shown, a code porting method is provided, which can be applied to... Figure 1 The following steps, 201 to 203, are used as an example of computer equipment.
[0062] Step 201: Obtain the code file to be ported, and perform code block identification processing on the code file to be ported to determine at least one code block to be ported.
[0063] The code file to be ported refers to the file that contains code blocks that need to be ported. The code blocks that need to be ported can be interface code blocks that differ between the source hardware backend and the target hardware backend.
[0064] For example, users (such as developers) can manually mark the code files that need to be ported in the target project as code files to be ported, and the computer device can directly obtain the code files to be ported marked by the user; or, the computer device can also automatically identify the files in the code repository corresponding to the target project (i.e., the code repository to be ported) to determine the code files to be ported that need to be ported.
[0065] In one alternative implementation, the computer device can first obtain the code repository to be ported, which includes multiple source code files. For example, a user can upload the code repository corresponding to the target project so that the computer device can obtain the code repository to be ported. Alternatively, the computer device can also obtain the code repository to be ported from a pre-set database based on retrieval information (such as code repository identifier, code repository name, code repository path, etc.). Next, the computer device can sequentially scan and identify the multiple source code files in the code repository to be ported in order to determine the source code files that need to be ported.
[0066] For example, a computer device can first select source code files with the preset file extension from multiple source code files based on the preset file extension, and use them as source code files to be ported. It should be noted that in the code repository corresponding to a project, there will likely be files related to the keywords of the source hardware backend. For example, for the source hardware backend CUDA, the preset file extension may include .cu.
[0067] Furthermore, for raw code files with file extensions other than the preset file extensions, they may also involve code segments or blocks related to the source hardware backend. These files also require code porting. Therefore, after initial screening based on the preset file extensions, raw code files with non-preset file extensions from multiple raw code files can be input into a preset file intent recognition model for file intent recognition. Raw code files whose file intent matches the preset porting file intent are then selected as the code files to be ported. The preset porting file intent can be the file intent for running code using the source hardware backend.
[0068] The preset file intent recognition model can be a Large Language Model (LLM). Based on the common sense reasoning ability of the LLM model, it identifies the files that truly need to be ported in the target project. This LLM model can be a lightweight model, such as the Foreman Agent. This file intent recognition model does not simply search for key strings; it can also read file summaries and perform reasoning, that is, understand the file intent by reading code snippets. For example: Scenario A: A utility.cpp file is read. Although the file does not contain the word "cuda," the model judges based on common sense that the file contains specific matrix operation logic and suggests checking it; that is, it can identify that a .cpp file, although it does not contain the keyword "CUDA," calls a third-party GPU library that needs to be ported, so this .cpp file can be considered as a code file to be ported. Another example: Scenario B: A train.py file is read. The model judges that this is only top-level logic, and the lower-level calls will be automatically distributed to the backend, so it is marked as "no modification required"; that is, it can identify that a .py file is just a configuration script and does not need to be modified, so this .py file does not require code porting.
[0069] By first filtering file extensions and then identifying file intent based on a model, at least one portable code file can be obtained from the code repository to be ported, thus creating a list of portable files. Next, for each portable code file, the computer device can identify the portable code blocks within that file. A portable code file may include one or more portable code blocks. For example, the computer device can use a preset code recognition model to identify the code blocks in the portable code file, thereby determining at least one portable code block. This preset code recognition model can also be a lightweight LLM model. By identifying and extracting the code blocks in the portable code file that require porting, interfering code segments can be eliminated, allowing focus on the core code and improving code porting efficiency.
[0070] Step 202: Determine at least one attribute information corresponding to each code block to be ported, and determine the target porting strategy corresponding to each code block to be ported based on the attribute information.
[0071] The attribute information of the code block to be ported can include standard interface type and non-standard interface type. The attribute information of the code block to be ported under non-standard interface type can also include the porting difficulty of the code block to be ported. The porting difficulty can be determined based on the confidence level obtained after quantifying the difficulty of the code block to be ported. The confidence level is negatively correlated with the porting difficulty, that is, the lower the confidence level, the higher the porting difficulty of the code block.
[0072] For example, the computer device can use a preset attribute recognition model to perform attribute recognition on the code block to be ported, thereby obtaining at least one attribute information of the code block to be ported; or, it can use an attribute recognition strategy to perform attribute recognition on the code block to be ported, wherein the attribute recognition strategy may include, but is not limited to, judging the interface type based on the standard interface library, judging the porting difficulty based on preset conditions or models, etc.
[0073] Furthermore, for each code block to be ported, once at least one attribute information corresponding to the code block is determined, a target porting strategy corresponding to the code block can be determined based on that attribute information. For example, the computer device can generate a target porting strategy based on at least one attribute information of the code block to be ported; alternatively, different correspondences between attribute information and porting strategies can be pre-defined, and then the target porting strategy corresponding to the code block to be ported can be determined based on at least one attribute information of the code block to be ported and that correspondence. For instance, the computer device can pre-build a porting strategy library, which may include multiple different porting strategies, each with corresponding attribute information. When determining the target porting strategy, the computer device can determine the target porting strategy corresponding to each code block to be ported from the porting strategy library based on each attribute information.
[0074] For example, based on the aforementioned attribute information, the porting strategy library may include a first porting strategy corresponding to standard interface types and multiple porting strategies corresponding to non-standard interface types, such as a second porting strategy and a third porting strategy, wherein the porting difficulty of the multiple porting strategies is different; in an optional implementation, the first porting strategy may include using code porting tools for code porting, the porting difficulty of the second porting strategy is less than that of the third porting strategy, the second porting strategy may include using a preset porting knowledge base for code porting, and the third porting strategy may include manual code porting; wherein, the preset porting knowledge base may include a static file library and / or a dynamic knowledge base within the company, the dynamic knowledge base may be generated based on code blocks manually ported during historical code porting processes, that is, the manually ported code logic and manual repair records are continuously updated to the dynamic knowledge base so that the computer equipment can continuously improve its code porting capabilities.
[0075] For each code file to be ported, if the code file contains multiple code blocks to be ported, the target porting strategy for each code block can be different. That is, the computer device can determine the target porting strategy matching each code block in the code file based on at least one attribute of the code block from the porting strategy library. It should be noted that multiple code blocks in the code file can also correspond to the same target porting strategy. For example, if at least one attribute of each code block is the same, it can be determined that the target porting strategies corresponding to these code blocks are also the same.
[0076] Based on the porting strategy library exemplified above, if the attribute information of the code block to be ported indicates that the code block corresponds to a standard interface type, then the target porting strategy for that code block can be determined as the first porting strategy. If the attribute information of the code block to be ported indicates that the code block corresponds to a non-standard interface type, and the porting difficulty of the code block is the first difficulty, then the target porting strategy for that code block can be determined as the second porting strategy. If the attribute information of the code block to be ported indicates that the code block corresponds to a non-standard interface type, and the porting difficulty of the code block is the second difficulty, then the target porting strategy for that code block can be determined as the third porting strategy; wherein, the first difficulty is less than the second difficulty.
[0077] For example, for non-standard interface types and code blocks that are difficult to port, a third porting strategy is adopted, namely, manual code porting. In this case, the third porting strategy may also include at least one of the following: encapsulating the code block to be ported in a macro definition, generating a code stub of the code block to be ported, and retaining the code block to be ported.
[0078] Step 203: Based on the target porting strategy, port the code for each code block to be ported to obtain the target code file corresponding to the code file to be ported.
[0079] For example, after determining the target porting strategy for each code block in the code file to be ported, the target porting strategy can be used to port the corresponding code blocks. That is, different code blocks can be ported using the same or different porting strategies. Compared with the traditional single porting strategy, a better porting strategy can be adopted for code blocks with different attributes, thereby improving the efficiency and effectiveness of code porting and increasing the success rate of code porting.
[0080] In one alternative implementation, the code porting process may include: porting each code block to be ported separately based on each target porting strategy to generate target code blocks corresponding to each code block to be ported; then, replacing the corresponding code blocks to be ported with the target code blocks corresponding to each code block to be ported, and adding porting comments to each target code block to generate the target code file corresponding to the code file to be ported; the porting comments corresponding to the target code blocks include at least the target porting strategy corresponding to the target code blocks.
[0081] In this example, the code output format generated using full comments is adopted, meaning that each line of modified code generated must be accompanied by explanatory comments based on the search source (document / tool / history) to assist users in reviewing it.
[0082] When the code repository to be ported includes multiple code files to be ported, the target code file corresponding to each code file to be ported can be obtained. Based on the target code file corresponding to each code file to be ported, and multiple original code files in the code repository to be ported that do not need to be ported, a target code repository corresponding to the target project suitable for the target hardware backend can be formed.
[0083] In the aforementioned code porting method, the computer device acquires the code file to be ported and performs code block identification processing on the code file to determine at least one code block to be ported. Next, it determines at least one attribute information corresponding to each code block to be ported, and determines the target porting strategy corresponding to each code block based on the attribute information. Then, based on each target porting strategy, it performs code porting on each code block to be ported, obtaining the target code file corresponding to the code file to be ported. In other words, the code porting method proposed in this application can adopt a porting strategy matching the attributes for code blocks with different attributes. That is, for a single code file to be ported, multiple different porting strategies can be used to port different code blocks within the file. Compared to the traditional method of using a single porting strategy to process the entire code file, this improves the porting effect of the code file and thus increases the success rate of code porting.
[0084] In one exemplary embodiment, such as Figure 3 As shown, when the attribute information of the code block to be ported includes standard interface types and non-standard interface types, determining at least one attribute information corresponding to each code block to be ported in step 202 above may include steps 301 to 302. Wherein:
[0085] Step 301: For each code block to be ported, identify the interface information in the code block to be ported.
[0086] The interface information may include at least one of the following: interface name, interface identifier, and interface description information.
[0087] For example, an interface recognition tool can be used to identify the interface information in each code block to be ported; or, the above-mentioned code recognition model can be used to identify the interface information in the code block to be ported. That is, while using the code recognition model to identify the code block to be ported in the code file to be ported, the interface information in the code block to be ported can also be identified.
[0088] Step 302: Determine the interface type based on the interface information and the preset standard interface library, and determine the attribute information of the code block to be ported based on the interface type.
[0089] The preset standard interface library may include interface information of at least one standard interface shared by the source hardware backend and the target hardware backend. The interface type may be a standard interface type or a non-standard interface type.
[0090] For example, when a computer device identifies interface information in a code block to be ported, it can determine whether the interface information is included in a preset standard interface library, that is, whether the interface corresponding to the interface information is a standard interface. If the preset standard interface library includes the interface information or the interface corresponding to the interface information, the interface type can be determined to be a standard interface type, and the standard interface type can be used as the attribute information of the code block to be ported. Conversely, if the preset standard interface library does not include the interface corresponding to the interface information, the interface type can be determined to be a non-standard interface type, and the non-standard interface type can be used as the attribute information of the code block to be ported.
[0091] For code blocks to be ported corresponding to non-standard interface types, the porting difficulty of the code block can be further determined. For example, the porting difficulty of the code block can be quantified, and the porting difficulty of the code block can be determined based on the confidence level obtained from the quantification. Then, the non-standard interface type and porting difficulty are used as attribute information of the code block to be ported. Among them, the confidence level and porting difficulty are negatively correlated. The lower the confidence level, the higher the difficulty of code porting.
[0092] For example, the logical complexity of the code block to be ported can be analyzed and quantified to obtain the confidence level of the code block to be ported; or, it can be determined whether the preset porting knowledge base includes porting knowledge that is logically similar to the code block to be ported. If so, the confidence level is a first value; if not, the confidence level is a second value, where the first value is greater than the second value. Alternatively, a comprehensive judgment of multiple indicators can be made by combining the logical complexity and the preset porting knowledge base to obtain the confidence level after comprehensive quantification of multiple indicators. The quantified values corresponding to each indicator can be weighted and summed to obtain the final confidence level. Different indicators have different weighting weights. It should be noted that the number and specific types of indicators for multi-indicator difficulty quantification are not specifically limited in this embodiment of the application. In practical applications, it is not limited to logical complexity indicators and preset porting knowledge base indicators.
[0093] For example, a confidence threshold can also be set. When the confidence of the code block to be ported is greater than the confidence threshold, it indicates that the porting difficulty of the code block is relatively low, and the porting difficulty can be classified as the first difficulty level. Conversely, when the confidence of the code block to be ported is less than or equal to the confidence threshold, it indicates that the porting difficulty of the code block is relatively high, and the porting difficulty can be classified as the second difficulty level. When the porting difficulty is low, the second porting strategy can be used to port the code block to be ported; when the porting difficulty is high, the third porting strategy can be used to port the code block to be ported.
[0094] The confidence threshold mentioned above can be a static fixed value or a dynamically adjusted value. For example, users can manually adjust the confidence threshold. If the user assesses that the logical complexity of the code files in the code repository to be ported corresponding to the target project is high, the confidence threshold can be adaptively increased, so that fewer code blocks are automatically ported, and more code blocks are skipped and manually ported.
[0095] For example, computer devices can also flexibly adjust the confidence threshold based on the success rate of automatic porting, such as the success rate of code porting using the second porting strategy. For instance, when the success rate is below a certain threshold, the confidence threshold can be increased, thereby allowing more logically complex code blocks to be skipped and manually ported by humans.
[0096] In this embodiment, by identifying the interface information in the code block to be ported, judging the interface type with the help of a preset standard interface library, determining the interface type, and determining the attribute information of the code block to be ported based on the interface type, the accuracy of code block attribute information identification can be improved.
[0097] In one exemplary embodiment, such as Figure 4As shown, the above method may further include steps 401 to 403. Wherein:
[0098] Step 401: Create the target test program corresponding to the target code file; the target test program includes the test subroutines corresponding to the code blocks to be ported after the code porting is completed.
[0099] Step 402: Use the target test program to perform code testing on the target code file and obtain the code test results.
[0100] Step 403: Output the code test results.
[0101] In this embodiment, for each code block to be ported in the code file to be ported, after completing the porting operation of the code block to be ported using the first porting strategy or the second porting strategy, a test subroutine corresponding to the code block to be ported can be further created, ultimately obtaining the target test program corresponding to the target code file. Next, the target code file is run and tested using the target test program. For example, the test subroutine corresponding to each code block to be ported in the target test program can be used to run and test the target code block after porting the corresponding code block to be ported, thereby obtaining the code test result of the target code block corresponding to each ported code block to be ported; wherein, the code test result may include whether the test passed or failed.
[0102] After obtaining the code test results, the computer device can output and display the code test results of the target code block corresponding to each ported code block in the ported code file that has been ported, so as to prompt the user which code blocks were ported successfully, which code blocks were ported unsuccessfully, and which code blocks need to be manually ported (i.e., the code blocks that match the third porting strategy).
[0103] For example, when the computer device is the terminal, the computer device may also include a display interface, in which code test results and / or code test statistics, such as the number of code blocks that were successfully ported, the number of code blocks that failed to be ported, and the number of code blocks that were manually ported, can be displayed.
[0104] For example, when the computer device is a server, the computer device can send the code test results to the user terminal so that the user can clearly view the code porting results, such as the porting strategy corresponding to the code block, whether the code block was successfully ported, and whether the code block needs to be manually ported.
[0105] In this embodiment, for code blocks that are automatically ported, corresponding test subroutines are created to perform online testing on the automatically ported target code blocks and output the test results. This allows users to intuitively and clearly understand the code porting status, improving the prompting effect of code porting. It also helps users to quickly view the ported code and handle code blocks that require manual porting or have failed to port, thus improving the overall processing effect of code porting.
[0106] In one exemplary embodiment, such as Figure 5 As shown, a complete process for a code porting method is provided.
[0107] First, this example also provides a code porting system that adopts a semi-automatic porting architecture with human-computer coupling, comprising four core modules:
[0108] 1. Intelligent Distribution Module (The Foreman - Common Sense Filtering):
[0109] Function: Based on LLM's common sense reasoning capabilities, identify files in a project that truly need to be ported.
[0110] Note: It is not limited to recognizing file extensions (such as .cu), but can understand the intent of a file by reading code snippets.
[0111] 2. Enterprise Knowledge Enhancement Module (Internal Knowledge Base), including:
[0112] Static documentation library: Contains internal development documentation for the target hardware backend, API mapping tables, best practice guidelines, and other documents.
[0113] Dynamic Evolution Library: Stores historical porting cases and manual repair records (this is the source of system evolution).
[0114] 3. Execution and Testing Module (The Worker - Multi-strategy Execution and Annotation):
[0115] Core logic: A hierarchical processing strategy is adopted for the identified modification points.
[0116] Enhanced annotations (key point): The system must generate comments for every modification (i.e., the code block to be ported). For example, / / AI-PORT: Replaced cudaMalloc with musa_malloc based on Rule #3.
[0117] Bypass Strategy: When encountering a complex kernel with low model confidence or no corresponding solution in the knowledge base, the system will never force a modification. Instead, it will retain the original code, wrap it in a macro definition, and generate a flag " / / TODO: AI_UNCERTAIN - manual intervention required".
[0118] 4. Human-AI Interface:
[0119] Provides an interactive interface based on the web or IDE plugin, highlighting AI-marked "bypass points" and "modification points" for users to review quickly.
[0120] Based on the above semi-automatic porting architecture, taking the backend migration of a certain deep learning training framework as an example, the complete code porting process can include:
[0121] Step 1: Smart Discovery based on common sense
[0122] Input: The entire code repository.
[0123] Process: The Foreman Agent (lightweight model) does not simply search for strings, but rather reads file summaries and performs file intent inference.
[0124] Output: A precise "porting checklist" that eliminates distracting elements and focuses on the core code.
[0125] Step 2: Multi-strategy Context-Aware Execution The Worker Agent processes each file in the manifest, analyzing each code segment to identify all potential modification points. For each modification point, execution is performed according to the following priority:
[0126] Strategy A: Internal tool invocation (preferred)
[0127] If the interface kernel is identified as a standard interface, the agent will first attempt to call the company's existing "source-to-target conversion tool".
[0128] Result: If the tool succeeds, retain the code generated by the tool and add the comment / / Generated by Internal Tool v1.2.
[0129] Strategy B: Document-based LLM rewriting
[0130] If the tool does not support it (e.g., involving complex PyTorch custom operators), the agent retrieves the company’s existing documents (static document library) and dynamic evolution library (storing historical transplant cases and human repair records).
[0131] Result: Based on the retrieved documentation, the logic was rewritten. A comment was added above the modifications: / / AI-PORT: Modified logic based on Internal Doc Section 4.2.
[0132] Strategy C: Safe Bypass and Assistance (Last Standby)
[0133] If the code is extremely obscure or relies on unknown third-party libraries, the agent is deemed to have too high a risk.
[0134] Result: Without modifying the logic, a stub is generated or the original code is retained and highlighted, such as:
[0135] C++
[0136] / / TODO: [AI-Uncertain] Unable to find the corresponding implementation for the target hardware backend. Please manually verify the logic.
[0137] / / Original code: custom_warp_primitive(...);
[0138] Step 3: Generate auxiliary verification tests
[0139] For the modified code block, the Agent automatically generates mini unit tests. This is not only to pass the tests, but also to provide developers with a tool to "verify whether this small piece of code works," saving time spent writing test code manually.
[0140] Step 4: Human Censorship and System Evolution
[0141] Developers can view the code. Thanks to clear comments ( / / AI-PORT) and highlighted TODOs, viewing is extremely fast.
[0142] The developers fixed the issue in the TODO for the code blocks that were not automatically ported.
[0143] The system backend captures "manual repair" actions and updates them to the dynamic evolution library, so that similar code can be handled automatically the next time it is encountered.
[0144] When using the above method for code porting, the following technical effects can be achieved:
[0145] 1. Extremely high reliability and maintainability: Unlike traditional "black box" AI, this application emphasizes code interpretability. By mandating the addition of comments and "bypass" mechanisms, it ensures that the AI will not quietly break the code logic, allowing developers to use it with confidence.
[0146] 2. Significantly improves efficiency: The system handles 90% of the tedious API replacement and documentation search work, and lays the groundwork for the remaining 10% of the difficult tasks (location + annotation), allowing developers to focus on the core algorithm logic rather than manual labor.
[0147] 3. Make full use of corporate assets: Transform the company's internal documents, tools and historical Git records into AI capabilities, making the system understand the company's coding style and hardware characteristics better than external general models.
[0148] 4. Intelligent filtering reduces costs and increases efficiency: Files are filtered using common sense rather than rigid rules, avoiding omissions (rules not covered) and accidental changes (filenames contain keywords from the source hardware backend but are actually irrelevant).
[0149] It should be understood that although the steps in the flowcharts of the embodiments described above are shown sequentially according to the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless explicitly stated herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some steps in the flowcharts of the embodiments described above may include multiple steps or multiple stages. These steps or stages are not necessarily completed at the same time, but can be executed at different times. The execution order of these steps or stages is not necessarily sequential, but can be performed alternately or in turn with other steps or at least some of the steps or stages of other steps.
[0150] Based on the same inventive concept, this application also provides a code porting apparatus for implementing the code porting method described above. The solution provided by this apparatus is similar to the implementation scheme described in the above method; therefore, the specific limitations in one or more code porting apparatus embodiments provided below can be found in the limitations of the code porting method described above, and will not be repeated here.
[0151] In one exemplary embodiment, such as Figure 6 As shown, a code porting apparatus 600 is provided, including: an acquisition module 601, a determination module 602, and a porting module 603, wherein:
[0152] The acquisition module 601 is used to acquire the code file to be ported and perform code block identification processing on the code file to be ported to determine at least one code block to be ported.
[0153] The determination module 602 is used to determine at least one attribute information corresponding to each code block to be ported, and to determine the target porting strategy corresponding to each code block to be ported based on the attribute information.
[0154] The porting module 603 is used to port each code block to be ported according to the target porting strategy, so as to obtain the target code file corresponding to the code file to be ported.
[0155] In one embodiment, the determining module 602 includes:
[0156] The identification unit is used to identify the interface information in each code block to be ported.
[0157] The determination unit is used to determine the interface type based on the interface information and the preset standard interface library, and to determine the attribute information of the code block to be ported based on the interface type; the interface type is either a standard interface type or a non-standard interface type.
[0158] In one embodiment, the determining unit is specifically configured to, in response to an interface corresponding to interface information included in a preset standard interface library, determine the interface type as a standard interface type and use the standard interface type as attribute information of the code block to be ported; in response to an interface corresponding to interface information not included in the preset standard interface library, determine the interface type as a non-standard interface type, quantify the difficulty of the code block to be ported, determine the porting difficulty of the code block to be ported based on the confidence level obtained from the quantification, and use the non-standard interface type and porting difficulty as attribute information of the code block to be ported.
[0159] In one embodiment, the determining module 602 is further configured to determine the target porting strategy corresponding to the code block to be ported as a first porting strategy when the attribute information of the code block to be ported indicates that the code block to be ported is a code block corresponding to a standard interface type; the first porting strategy includes using a code porting tool to port the code.
[0160] In one embodiment, the determining module 602 is further configured to determine the target porting strategy corresponding to the code block to be ported as a second porting strategy when the attribute information of the code block to be ported indicates that the code block to be ported is a code block corresponding to a non-standard interface type and the porting difficulty of the code block to be ported is a first difficulty; the second porting strategy includes using a preset porting knowledge base for code porting; when the attribute information of the code block to be ported indicates that the code block to be ported is a code block corresponding to a non-standard interface type and the porting difficulty of the code block to be ported is a second difficulty, the target porting strategy corresponding to the code block to be ported is determined as a third porting strategy; the third porting strategy includes manually porting the code, where the first difficulty is less than the second difficulty.
[0161] In one embodiment, the preset porting knowledge base includes a static file library and / or a dynamic knowledge base, wherein the dynamic knowledge base is generated based on code blocks manually ported during the historical code porting process.
[0162] In one embodiment, the third porting strategy further includes: encapsulating the code block to be ported in a macro definition, generating a code stub of the code block to be ported, and retaining at least one of the code blocks to be ported.
[0163] In one embodiment, the determining module 602 is specifically used to determine the target porting strategy corresponding to each code block to be ported from the porting strategy library based on each attribute information; wherein, the porting strategy library includes multiple different porting strategies.
[0164] In one embodiment, the porting module 603 includes:
[0165] The code block porting unit is used to port each code block to be ported according to each target porting strategy, and generate the target code block corresponding to each code block to be ported.
[0166] The file generation unit is used to replace the corresponding code block to be ported with the target code block corresponding to each code block to be ported, and to add porting comments to each target code block to generate the target code file corresponding to the code file to be ported; the porting comments corresponding to the target code block shall at least include the target porting strategy corresponding to the target code block.
[0167] In one embodiment, the acquisition module 601 includes:
[0168] The acquisition unit is used to acquire the code repository to be ported; the code repository to be ported includes multiple original code files;
[0169] The file filtering unit is used to filter out source code files with a preset file extension from multiple source code files based on the preset file extension, and to select these source code files as the code files to be ported; and,
[0170] The intent recognition unit is used to input source code files with non-preset file extensions from multiple source code files into a preset file intent recognition model for file intent recognition, and to use source code files whose file intent is a preset porting file intent as the code files to be ported.
[0171] In one embodiment, the device further includes:
[0172] Create a module to create the target test program corresponding to the target code file; the target test program includes test subroutines corresponding to the code blocks to be ported after the code porting is completed.
[0173] The testing module is used to perform code tests on the target code file using the target test program and obtain the code test results;
[0174] The output module is used to output the test results of the code.
[0175] Each module in the aforementioned code porting device can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in or independent of the processor in a computer device, or stored in the memory of a computer device as software, so that the processor can call and execute the operations corresponding to each module.
[0176] In one exemplary embodiment, a computer device is provided. Taking this computer device as an example as a terminal, its internal structure diagram can be as follows: Figure 7 As shown, the computer device includes a processor, memory, input / output interfaces, a communication interface, a display unit, and an input device. The processor, memory, and input / output interfaces are connected via a system bus, and the communication interface, display unit, and input device are also connected to the system bus via the input / output interfaces. The processor provides computing and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system and computer programs. The internal memory provides an environment for the operation of the operating system and computer programs stored in the non-volatile storage media. The input / output interfaces are used for exchanging information between the processor and external devices. The communication interface is used for wired or wireless communication with external terminals; wireless communication can be achieved through Wi-Fi, mobile cellular networks, Near Field Communication (NFC), or other technologies. When the computer program is executed by the processor, it implements a code porting method. The display unit is used to form a visually visible image and can be a display screen, a projection device, or a virtual reality imaging device. The display screen can be an LCD screen or an e-ink screen. The input device of the computer device can be a touch layer covering the display screen, or buttons, trackballs, or touchpads set on the casing of the computer device, or external keyboards, touchpads, or mice, etc.
[0177] Those skilled in the art will understand that Figure 7 The structure shown is merely a block diagram of a portion of the structure related to the present application and does not constitute a limitation on the computer device to which the present application is applied. Specific computer devices may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.
[0178] In one exemplary embodiment, a computer device is provided, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to implement the steps of the code porting method in any of the above embodiments.
[0179] In one embodiment, a computer-readable storage medium is provided having a computer program stored thereon, which, when executed by a processor, implements the steps of the code porting method in any of the above embodiments.
[0180] In one embodiment, a computer program product is provided, including a computer program that, when executed by a processor, implements the steps of the code porting method in any of the above embodiments.
[0181] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, data stored, data displayed, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of the relevant data must comply with relevant regulations.
[0182] Those skilled in the art will understand that all or part of the processes in the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium. When executed, the computer program can include the processes of the embodiments described above. Any references to memory, databases, or other media used in the embodiments provided in this 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, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetic random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. By way of illustration and not limitation, RAM can take many forms, such as Static Random Access Memory (SRAM) or Dynamic Random Access Memory (DRAM). The databases involved in the embodiments provided in this application may include at least one type of relational database and non-relational database. Non-relational databases may include, but are not limited to, blockchain-based distributed databases. The processors involved in the embodiments provided in this application may be general-purpose processors, central processing units, graphics processing units, digital signal processors, programmable logic devices, quantum computing-based data processing logic devices, etc., and are not limited to these.
[0183] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0184] The embodiments described above are merely illustrative of several implementation methods of this application, and while the descriptions are specific and detailed, they should not be construed as limiting the scope of this patent application. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of this application, and these all fall within the protection scope of this application. Therefore, the protection scope of this application should be determined by the appended claims.
Claims
1. A code porting method, characterized in that, The method includes: Obtain the code file to be ported, and perform code block identification processing on the code file to be ported to determine at least one code block to be ported; Determine at least one attribute information corresponding to each of the code blocks to be ported, and determine the target porting strategy corresponding to each of the code blocks to be ported based on the attribute information. Based on the target porting strategies described above, each of the code blocks to be ported is ported separately to obtain the target code file corresponding to the code file to be ported.
2. The method according to claim 1, characterized in that, Determining at least one attribute information corresponding to each of the code blocks to be ported includes: For each of the code blocks to be ported, identify the interface information in the code block to be ported; Based on the interface information and the preset standard interface library, the interface type is determined, and the attribute information of the code block to be ported is determined based on the interface type; the interface type is a standard interface type or a non-standard interface type.
3. The method according to claim 2, characterized in that, The step of determining the interface type based on the interface information and a preset standard interface library, and determining the attribute information of the code block to be ported based on the interface type, includes: In response to the fact that the preset standard interface library includes an interface corresponding to the interface information, the interface type is determined to be a standard interface type, and the standard interface type is used as the attribute information of the code block to be ported. If the interface corresponding to the interface information is not included in the preset standard interface library, the interface type is determined to be a non-standard interface type, and the difficulty of the code block to be ported is quantified. The porting difficulty of the code block to be ported is determined based on the confidence level obtained by quantification. The non-standard interface type and the porting difficulty are used as attribute information of the code block to be ported.
4. The method according to any one of claims 1 to 3, characterized in that, The step of determining the target porting strategy corresponding to each of the code blocks to be ported based on the attribute information includes: If the attribute information of the code block to be ported indicates that the code block to be ported is a code block corresponding to a standard interface type, then the target porting strategy corresponding to the code block to be ported is determined to be the first porting strategy; the first porting strategy includes using a code porting tool for code porting.
5. The method according to any one of claims 1 to 3, characterized in that, The step of determining the target porting strategy corresponding to each of the code blocks to be ported based on the attribute information includes: If the attribute information of the code block to be ported indicates that the code block to be ported is a code block corresponding to a non-standard interface type, and the porting difficulty of the code block to be ported is the first difficulty, then the target porting strategy corresponding to the code block to be ported is determined to be the second porting strategy; the second porting strategy includes using a preset porting knowledge base for code porting; If the attribute information of the code block to be ported indicates that the code block to be ported is a code block corresponding to a non-standard interface type, and the porting difficulty of the code block to be ported is the second difficulty, then the target porting strategy corresponding to the code block to be ported is determined to be the third porting strategy; the third porting strategy includes manual code porting, and the first difficulty is less than the second difficulty.
6. The method according to claim 5, characterized in that, The preset porting knowledge base includes a static file library and / or a dynamic knowledge base, wherein the dynamic knowledge base is generated based on code blocks manually ported during the historical code porting process.
7. The method according to claim 5, characterized in that, The third porting strategy further includes: encapsulating the code block to be ported in a macro definition, generating a code stub of the code block to be ported, and retaining at least one of the code blocks to be ported.
8. The method according to any one of claims 1 to 3, characterized in that, The step of determining the target porting strategy corresponding to each of the code blocks to be ported based on the attribute information includes: The target porting strategy corresponding to each code block to be ported is determined from the porting strategy library based on the attribute information described therein; wherein, the porting strategy library includes multiple different porting strategies.
9. The method according to any one of claims 1 to 3, characterized in that, The step of porting each of the target porting strategies to obtain the target code file corresponding to the target code file includes: Based on the target porting strategies described above, code porting is performed on each of the code blocks to be ported, generating target code blocks corresponding to each of the code blocks to be ported. The target code block to be ported is replaced with the target code block corresponding to each of the target code blocks to be ported, and porting comments are added to each of the target code blocks to generate the target code file corresponding to the code file to be ported; the porting comments corresponding to the target code block include at least the target porting strategy corresponding to the target code block.
10. The method according to any one of claims 1 to 3, characterized in that, The process of obtaining the code file to be ported includes: Obtain the code repository to be ported; the code repository to be ported includes multiple original code files; Based on a preset file extension, source code files with the preset file extension are selected from the plurality of source code files as the source code files to be ported; and, The original code files that do not have the preset file extension among the multiple original code files are input into the preset file intent recognition model for file intent recognition, and the original code files whose file intent is the preset porting file intent are taken as the code files to be ported.
11. The method according to any one of claims 1 to 3, characterized in that, The method further includes: Create a target test program corresponding to the target code file; the target test program includes a test subroutine corresponding to the code block to be ported, which completes the code porting process. The target code file is tested using the target test program to obtain the code test results. Output the test results of the code.
12. A code porting device, characterized in that, The device includes: The acquisition module is used to acquire the code file to be ported, and to perform code block identification processing on the code file to be ported to determine at least one code block to be ported. The determination module is used to determine at least one attribute information corresponding to each of the code blocks to be ported, and to determine the target porting strategy corresponding to each of the code blocks to be ported based on the attribute information. The porting module is used to port each of the code blocks to be ported according to the target porting strategy, so as to obtain the target code file corresponding to the code file to be ported.
13. A computer device comprising a memory and a processor, wherein the memory stores a computer program, characterized in that, When the processor executes the computer program, it implements the steps of the method according to any one of claims 1 to 11.
14. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 11.
15. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 11.