A large model code translation method and device fusing code functions and styles
By employing a two-stage training method that combines functional learning and style learning, the problem of insufficient functionality and readability in large model code translation is solved, improving the accuracy and consistency of code translation, reducing dependence on large models, and making it suitable for code translation devices on terminals or servers.
Patent Information
- Application Number
- CN202510343085.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-21
- Publication Date
- 2026-05-15
- Estimated Expiration
- 2045-03-21
AI Technical Summary
Existing code translation technologies based on large models are insufficient in terms of the functionality and readability of the translated target code, making it difficult to meet the needs of actual software development, especially in terms of inconsistencies in code structure and variable naming conventions.
A two-stage training approach is adopted: first, a high-quality functional consistency dataset is constructed through functional learning to ensure the functional correctness of the generated code; then, through style learning, positive and negative style examples are compared and a contrastive loss function is used to improve the readability and style consistency of the generated code.
It significantly improves the functional correctness and readability of the code translation model, reduces the dependence on large-scale models, enables small models to achieve performance comparable to or even better than large models, reduces hardware costs, and improves the deployment flexibility of the model.
Smart Images

Figure CN120335814B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of natural language processing technology, and in particular to a method and apparatus for translating large model code that integrates code functionality and style. Background Technology
[0002] Code translation is the process of converting code from one programming language to another, widely used in software development and maintenance, such as application porting or software migration. Traditional code translation methods mainly rely on rule-based strategies, which require skilled programmers to manually handle complex situations, resulting in high costs and low efficiency. With the development of deep learning, learning-based methods have gradually replaced traditional methods, significantly improving code translation performance. For example, previous researchers proposed a learnable attention mechanism to convert the syntax tree of the source language into the syntax tree of the target language, thereby reducing the need for manual intervention. In recent years, large-scale language models have performed exceptionally well in tasks such as code generation and code repair, further driving the development of code translation technology.
[0003] Existing code translation methods based on large-scale language models typically employ strategies such as Direct Prompt, Chain of Thought (CoT), Retrieval-Augmented Generation (RAG), and Self-Debug Prompt Learning to improve translation performance. For example, the RAG strategy achieves higher-quality translation by referencing translation data similar to the code to be translated; the self-debug prompt learning strategy compiles the generated target code, detects errors, and feeds error information back to subsequent prompts to guide corrections.
[0004] Currently, code translation methods based on large models typically employ the following strategies:
[0005] Direct prompting learning strategy: Prompts large-scale language models such as GPT-4 (Generative Pre-trained Transformer) to generate code in the target language by providing concise instructions containing source code.
[0006] A thought chain-based prompting learning strategy: By guiding the model through prompts, the model can think step by step and successfully translate difficult code.
[0007] Retrieval-enhanced prompting learning strategies: Referencing external knowledge bases to improve the model's code translation capabilities. External knowledge bases typically include sentence-level, fragment-level, and function-level mapping examples between source code and target code.
[0008] The self-debugging prompt learning strategy involves compiling the generated target code and detecting errors, then feeding back the error information to subsequent prompts to guide corrections.
[0009] Although existing large-model-based code translation techniques have outperformed rule-based and traditional deep learning-based techniques, there are still shortcomings in the functionality and readability of the translated target code.
[0010] In terms of accuracy, even with optimized prompting learning strategies, the correctness of the generated code by large, out-of-the-box models still falls short of the requirements of real-world software development. For example, the StarCoder-3B model has an average success rate of only 7% on the traditional code translation benchmark CodeNet.
[0011] While translated code may be functionally correct in terms of readability, it often fails to maintain the style of the original source code, including code structure and variable naming conventions. This inconsistency increases the reading burden for developers. For example, Figure 1 The presentation showcased Python code translated by Qwen-7B. Besides issues with code correctness, the translated Python exhibited numerous style problems, including inconsistencies between variable naming and the original source code, mismatched function encapsulation, and missing I / O optimizations.
[0012] Existing technologies primarily rely on rule-based methods, deep learning methods, and prompting strategies based on large-scale language models. However, these methods have limitations at different levels:
[0013] (1) Rule-based approach: requires professional programmers to manually define translation rules between languages, which is costly and inefficient.
[0014] (2) Deep learning-based methods: Although translation performance is improved through unsupervised pre-training and back-translation strategies, their reliance on high-quality parallel data (i.e. code translation data containing source and target languages) limits their applicability.
[0015] (3) Large model-based methods: Although translation performance has been improved through strategies such as direct prompts, thought chain reasoning, retrieval-enhanced generation and self-debugging prompts, these methods still cannot simultaneously guarantee functional correctness and code readability. Summary of the Invention
[0016] To address the shortcomings of existing technologies, such as the inability of out-of-the-box large models to generate code that meets practical software development needs even with optimized prompting learning strategies, and the frequent failure to maintain source code style (including code structure and variable naming conventions) despite functional accuracy in translated code, which increases the reading burden on developers, this invention provides a method and apparatus for translating large model code that integrates code functionality and style. The technical solution is as follows:
[0017] On the one hand, a method for translating large model code that integrates code functionality and style is provided. This method is implemented by a large model code translation device and includes:
[0018] S1. Obtain code pairs consisting of source code and target code from online programming platforms, process the code pairs based on similarity retrieval, fine-grained scoring, and differential testing, and construct a functional consistency dataset.
[0019] S2. Based on the functional consistency dataset and the instruction fine-tuning method, perform functional learning training on the large model to obtain a large model trained through functional learning.
[0020] S3. Obtain the source code, generate positive and negative sample translations of the source code, and construct a style-oriented dataset.
[0021] S4. Based on the style-oriented dataset, perform style learning training on the large model trained through function learning to obtain a well-trained code translation large model.
[0022] S5. Obtain the code to be translated, input it into the trained code translation model, and obtain the code translation result.
[0023] Optionally, in S1, code pairs are processed based on similarity retrieval, fine-grained scoring, and differential testing to construct a functionally consistent dataset, including:
[0024] S11. Perform a similarity search on the code pairs to obtain similar code pairs.
[0025] S12. Perform fine-grained scoring on similar code pairs, and filter the similar code pairs based on the fine-grained scoring results to obtain the filtered code pairs.
[0026] S13. Perform differential testing on the filtered code pairs to obtain the tested code pairs.
[0027] S14. Construct a functionally consistent dataset based on the tested code.
[0028] Optionally, S1 performs fine-grained scoring on similar code pairs, including:
[0029] Fine-grained scoring of similar code pairs is performed using the following formulas (1) and (2):
[0030] (1)
[0031] (2)
[0032] In the formula, This represents the final, fine-grained score. Represents source code and target code Similarity score, express and Rating: The softmax normalized probability, A prompt template representing a large model. This represents an exponential function with base e. Represents the generation of large models and The rating is The log-likelihood probability, This indicates that each score is calculated individually. Represents the generation of large models and The rating is The log-likelihood probability.
[0033] Optionally, the optimization objective function for the functional learning training in S2 is shown in equation (3) below:
[0034] (3)
[0035] In the formula, This represents the objective function to be optimized. Indicates target code The One token, This indicates that the input is used to process the source code in the large model. The prompt words translated into the target code. Indicates target code The former A token.
[0036] Optionally, the positive and negative sample translations of the generated source code in S3 include:
[0037] S31. Generate multiple translation candidates with consistent style from the source code, filter the translations with correct function from the translation candidates through differential testing, and select the best translation from the translations with correct function as the positive sample translation using the style consensus selection mechanism, as shown in the following formula (4):
[0038] (4)
[0039] In the formula, Indicates positive sample translation. This indicates that all functions selected by the differential detection are correctly translated. This represents a function used to measure the stylistic similarity between two target language codes. Indicates target code The One token, Indicates target code The A token.
[0040] S32. Generate multiple negative translation candidates from the source code, and use a function to select translations from the negative translation candidates whose style difference from the positive sample translation exceeds a preset threshold as negative sample translations.
[0041] Optionally, the optimization objective function for style learning training in S4 is shown in equations (5) and (6) below:
[0042] (5)
[0043] (6)
[0044] In the formula, The objective function for optimizing style learning training is represented. This represents the source code. Indicates positive sample translation. This represents the set of all negative sample translations. This represents an exponential function with base e. This indicates that the large model is inputting source code. At that time, positive sample translations were successfully generated. The probability, Represents the entire set of target code. The representation model is for the source code Generate target code The probability, This indicates that the large model is inputting source code. And generate target code The former When the first token is generated, the second token is successfully generated. The probability of each token. express The One token, This indicates that the input is used to process the source code in the large model. The prompt words translated into the target code. express The former A sequence of tokens.
[0045] Optionally, style learning training also includes fine-tuning instructions on forward translation, as shown in equation (7) below:
[0046] (7)
[0047] In the formula, This represents the numerical value of the loss function for style learning. This represents the balance hyperparameter between the two losses. The objective function for optimizing style learning training is represented. express and Fine-tuning the loss function between instructions, This indicates a positive sample translation.
[0048] On the other hand, a large model code translation apparatus that integrates code functionality and style is provided. This apparatus is applied to a large model code translation method that integrates code functionality and style. The apparatus includes:
[0049] The Functional Consistency Dataset Construction Module is used to obtain code pairs consisting of source code and target code from online programming platforms, process the code pairs based on similarity retrieval, fine-grained scoring, and differential testing, and construct a functional consistency dataset.
[0050] The Functional Learning Training Module is used to perform functional learning training on a large model based on a functionally consistent dataset and instruction fine-tuning methods, resulting in a large model trained through functional learning.
[0051] The style-oriented dataset building module is used to obtain source code, generate positive and negative sample translations of the source code, and build a style-oriented dataset.
[0052] The style learning training module is used to train a large model trained through function learning on a style-oriented dataset to obtain a well-trained code translation model.
[0053] The code translation module is used to acquire the code to be translated, input it into a pre-trained code translation model, and obtain the code translation result.
[0054] Optionally, the functionally consistent dataset building module is further used for:
[0055] S11. Perform a similarity search on the code pairs to obtain similar code pairs.
[0056] S12. Perform fine-grained scoring on similar code pairs, and filter the similar code pairs based on the fine-grained scoring results to obtain the filtered code pairs.
[0057] S13. Perform differential testing on the filtered code pairs to obtain the tested code pairs.
[0058] S14. Construct a functionally consistent dataset based on the tested code.
[0059] Optionally, the functionally consistent dataset building module is used for:
[0060] Fine-grained scoring of similar code pairs is performed using the following formulas (1) and (2):
[0061] (1)
[0062] (2)
[0063] In the formula, This represents the final, fine-grained score. Represents source code and target code Similarity score, express and Rating: The softmax normalized probability, A prompt template representing a large model. This represents an exponential function with base e. Represents the generation of large models and The rating is The log-likelihood probability, This indicates that each score is calculated individually. Represents the generation of large models and The rating is The log-likelihood probability.
[0064] Optionally, the optimization objective function for functional learning training is shown in equation (3) below:
[0065] (3)
[0066] In the formula, This represents the objective function to be optimized. Indicates target code The One token, This indicates that the input is used to process the source code in the large model. The prompt words translated into the target code. Indicates target code The former A token.
[0067] Optionally, the style-oriented dataset building module is further used for:
[0068] S31. Generate multiple translation candidates with consistent style from the source code, filter the translations with correct function from the translation candidates through differential testing, and select the best translation from the translations with correct function as the positive sample translation using the style consensus selection mechanism, as shown in the following formula (4):
[0069] (4)
[0070] In the formula, Indicates positive sample translation. This indicates that all functions selected by the differential detection are correctly translated. This represents a function used to measure the stylistic similarity between two target language codes. Indicates target code The One token, Indicates target code The A token.
[0071] S32. Generate multiple negative translation candidates from the source code, and use a function to select translations from the negative translation candidates whose style difference from the positive sample translation exceeds a preset threshold as negative sample translations.
[0072] Optionally, the optimization objective function for style learning training is shown in equations (5) and (6) below:
[0073] (5)
[0074] (6)
[0075] In the formula, The objective function for optimizing style learning training is represented. This represents the source code. Indicates positive sample translation. This represents the set of all negative sample translations. This represents an exponential function with base e. This indicates that the large model is inputting source code. At that time, positive sample translations were successfully generated. The probability, Represents the entire set of target code. The representation model is for the source code Generate target code The probability, This indicates that the large model is inputting source code. And generate target code The former When the first token is generated, the second token is successfully generated. The probability of each token. express The One token, This indicates that the input is used to process the source code in the large model. The prompt words translated into the target code. express The former A sequence of tokens.
[0076] Optionally, style learning training also includes fine-tuning instructions on forward translation, as shown in equation (7) below:
[0077] (7)
[0078] In the formula, This represents the numerical value of the loss function for style learning. This represents the balance hyperparameter between the two losses. The objective function for optimizing style learning training is represented. express and Fine-tuning the loss function between instructions, This indicates a positive sample translation.
[0079] On the other hand, a large model code translation device is provided, the large model code translation device comprising: a processor; a memory storing computer-readable instructions, wherein when the computer-readable instructions are executed by the processor, any one of the large model code translation methods described above that integrates code functionality and style is implemented.
[0080] On the other hand, a computer-readable storage medium is provided, wherein at least one instruction is stored therein, the at least one instruction being loaded and executed by a processor to implement any of the above-described methods for translating large model code with fused code functionality and style.
[0081] The beneficial effects of the technical solutions provided in the embodiments of the present invention include at least the following:
[0082] This invention significantly improves the functional correctness of the code translation model: In the first stage of F2STrans, functional learning is achieved by mining high-quality source-target code pairs and combining them with differential testing to ensure the functional consistency of the generated code. Specifically: First, code pairs with the same input-output behavior are selected using code data from an online programming platform. Then, differential testing further verifies the functional consistency of these code pairs, thus constructing a high-quality training dataset. Finally, function-oriented learning improves the model's performance.
[0083] Experiments show that functional learning significantly improves the functional correctness of generated code. For example, in the CodeNet benchmark, the functionally learned Qwen-0.5B model outperformed the RAG-based Qwen-32B model.
[0084] Enhancing the readability of code translation: The second stage of F2STrans, style learning, enables the model to generate target code that is both functionally correct and stylistically consistent through comparative learning of positive and negative style examples. Specifically, this includes: using a powerful LLM (such as Qwen32B) to generate multiple stylistically consistent positive translation candidates, and selecting the optimal translation through differential testing; suppressing inconsistent generation results using negative translation examples; and introducing a style consensus selection mechanism and a list-wise loss function to further optimize the style learning effect.
[0085] Style learning significantly improves the readability and style consistency of the generated code. For example, in the benchmark tests of this invention, the F2STrans-improved Qwen-0.5B model achieved a high score of 80.7 on the CCSim metric, surpassing GPT-4 and other baseline models.
[0086] Significantly reduced model size dependence: F2STrans employs efficient training strategies that enable small models to achieve performance comparable to or even better than large models. For example, Qwen-1.5B outperforms Qwen32B and GPT-4 under function-to-style guidance. This feature significantly reduces reliance on large-scale models, allowing resource-constrained developers to complete high-quality code translation tasks using small models. This not only reduces hardware costs but also improves model deployment flexibility. Attached Figure Description
[0087] To more clearly illustrate the technical solutions in the embodiments of the present invention, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0088] Figure 1 This is a schematic diagram illustrating the problems with the correctness and readability of the code for large model translation provided in this embodiment of the invention;
[0089] Figure 2 This is a flowchart of a method for translating large model code that integrates code functionality and style, provided by an embodiment of the present invention.
[0090] Figure 3 This is a flowchart of the large model function learning process provided in the embodiments of the present invention;
[0091] Figure 4 This is a flowchart of the large model style learning process provided in an embodiment of the present invention;
[0092] Figure 5 This is a block diagram of a large model code translation device that integrates code functionality and style, provided in an embodiment of the present invention.
[0093] Figure 6 This is a schematic diagram of the structure of a large model code translation device provided in an embodiment of the present invention. Detailed Implementation
[0094] The technical solution of the present invention will now be described with reference to the accompanying drawings.
[0095] In embodiments of the present invention, words such as "exemplarily," "for example," etc., are used to indicate that something is an example, illustration, or description. Any embodiment or design described as "exemplary" in the present invention should not be construed as being more preferred or advantageous than other embodiments or designs. Specifically, the use of the word "exemplary" is intended to present the concept in a concrete manner. Furthermore, in embodiments of the present invention, the meaning expressed by "and / or" can be both, or either one.
[0096] In the embodiments of this invention, the terms "image" and "picture" may sometimes be used interchangeably. It should be noted that, without emphasizing the distinction between them, they convey the same meaning. Similarly, the terms "of," "corresponding (relevant)," and "corresponding" may sometimes be used interchangeably. It should be noted that, without emphasizing the distinction between them, they convey the same meaning.
[0097] In this embodiment of the invention, sometimes a subscript such as W1 may be written in a non-subscript form such as W1. When the difference is not emphasized, the meaning they express is the same.
[0098] To make the technical problems, technical solutions and advantages of the present invention clearer, a detailed description will be given below in conjunction with the accompanying drawings and specific embodiments.
[0099] This invention provides a method for translating large model code that integrates code functionality and style. This method can be implemented using a large model code translation device, which can be a terminal or a server. Figure 2 The flowchart shown illustrates a method for translating large model code that integrates code functionality and style. This method's processing flow may include the following steps:
[0100] S1. Obtain code pairs consisting of source code and target code from online programming platforms, process the code pairs based on similarity retrieval, fine-grained scoring, and differential testing, and construct a functional consistency dataset.
[0101] In one feasible implementation, in order to ensure the quality of training data, the present invention mines high-quality source-target code pairs from online programming platforms (such as Codeforces) and filters out code pairs with the same input-output behavior.
[0102] Specifically, functional learning is the first stage of the function-to-style guidance paradigm F2STrans, and its core objective is to ensure that the generated target code is functionally identical to the source code, such as... Figure 3 As shown, the specific implementation steps include step S1, which involves constructing functional consistency data, and step S2, which involves functional learning and training.
[0103] Optionally, the process of processing code pairs based on similarity retrieval, fine-grained scoring, and differential testing in S1 to construct a functionally consistent dataset may include the following steps S11-S14:
[0104] S11. Perform a similarity search on the code pairs to obtain similar code pairs.
[0105] S12. Perform fine-grained scoring on similar code pairs, and filter the similar code pairs based on the fine-grained scoring results to obtain the filtered code pairs.
[0106] S13. Perform differential testing on the filtered code pairs to obtain the tested code pairs.
[0107] S14. Construct a functionally consistent dataset based on the tested code.
[0108] One feasible implementation is relevance-driven codepair selection: A lightweight code embedding model, Jina, is used to retrieve similar code pairs, and a larger model performs fine-grained scoring on candidate code pairs. The scoring formula is as follows:
[0109] (1)
[0110] (2)
[0111] In the formula, This represents the final, fine-grained score. Represents source code and target code Similarity score, express and Rating: The softmax normalized probability, A prompt template representing a large model. This represents an exponential function with base e. Represents the generation of large models and The rating is The log-likelihood probability, This indicates that each score is calculated individually. Represents the generation of large models and The rating is The log-likelihood probability.
[0112] Furthermore, differential testing: This involves performing differential testing on the selected code pairs, that is, running the source code and target code under identical inputs and comparing their outputs. Only code pairs with completely identical input-output behavior will be retained.
[0113] S2. Based on the functional consistency dataset and the instruction fine-tuning method, perform functional learning training on the large model to obtain a large model trained through functional learning.
[0114] In one feasible implementation, the base model is trained using the Instruction Fine-tuning (IFT) method, and the objective function is optimized as follows:
[0115] (3)
[0116] In the formula, This represents the objective function to be optimized. Indicates target code The One token, This indicates that the input is used to process the source code in the large model. The prompt words translated into the target code. Indicates target code The former A token.
[0117] This invention ensures the functional consistency of the generated code by mining high-quality source-target code pairs and combining them with differential testing. A relevance-driven code pair selection mechanism and an LLM-based (LLMJudge) scoring system are used to select code pairs with identical input-output behaviors, constructing a high-quality training dataset. Differential testing further verifies the functional consistency of these code pairs, thereby significantly improving the functional correctness of the generated code.
[0118] S3. Obtain the source code, generate positive and negative sample translations of the source code, and construct a style-oriented dataset.
[0119] In one feasible implementation, style learning is the second stage of F2STrans, and its core objective is to improve the readability and style consistency of the generated code. For example... Figure 4 As shown, the specific implementation steps include style-oriented data construction in step S3 and style learning training in step S4.
[0120] Optionally, the positive and negative sample translations of the generated source code in S3 may include the following steps S31-S32:
[0121] S31. Generate multiple translation candidates with consistent style from the source code, filter out the functionally correct translations from the translation candidates through differential testing, and select the optimal translation from the functionally correct translations as the positive sample translations using a style consensus selection mechanism.
[0122] In one feasible implementation, to help the model learn the style features of the source code, this invention constructs positive and negative style examples. For positive translation, a powerful large model (such as Qwen32B) is used to generate multiple style-consistent translation candidates, and the functionally correct translation is selected through differential testing. Subsequently, a Style Consensus Selection (SCS) mechanism is used to select the optimal translation.
[0123] (4)
[0124] In the formula, Indicates positive sample translation. This indicates that all functions selected by the differential detection are correctly translated. This represents a function used to measure the stylistic similarity between two target language codes. Indicates target code The One token, Indicates target code The A token.
[0125] S32. Generate multiple negative translation candidates from the source code, and use a function to select translations from the negative translation candidates whose style difference from the positive sample translation exceeds a preset threshold as negative sample translations.
[0126] In one feasible implementation, negative sample translation uses a base model to generate multiple negative translation candidates, and then... The function filters out samples that differ significantly from the forward translation style.
[0127] S4. Based on the style-oriented dataset, perform style learning training on the large model trained through function learning to obtain a well-trained code translation large model.
[0128] Optionally, this invention designs a loss function with reference to the idea of contrastive learning, involving a list loss function that encourages the model to generate target code with consistent style while suppressing translations with inconsistent style:
[0129] (5)
[0130] (6)
[0131] In the formula, The objective function for optimizing style learning training is represented. This represents the source code. Indicates positive sample translation. This represents the set of all negative sample translations. This represents an exponential function with base e. This indicates that the large model is inputting source code. At that time, positive sample translations were successfully generated. The probability, Represents the entire set of target code. The representation model is for the source code Generate target code The probability, This indicates that the large model is inputting source code. And generate target code The former When the first token is generated, the second token is successfully generated. The probability of each token. express The One token, This indicates that the input is used to process the source code in the large model. The prompt words translated into the target code. express The former A sequence of tokens.
[0132] Optionally, the present invention also performs fine-tuning of instructions in forward translation to further emphasize the importance of forward translation:
[0133] (7)
[0134] In the formula, This represents the numerical value of the loss function for style learning. This represents the balance hyperparameter between the two losses. The objective function for optimizing style learning training is represented. express and Fine-tuning the loss function between instructions, This indicates a positive sample translation.
[0135] This invention utilizes positive and negative style example contrast learning to enhance the model's understanding and reasoning ability of multimodal information; it introduces a style consensus selection mechanism to select the optimal translation from multiple candidate translations to ensure style consistency in the generated code; and it proposes a list-wise loss function to further optimize style learning performance by suppressing style-inconsistent translation results.
[0136] This invention proposes a two-stage training framework from function to style: the first stage optimizes translation accuracy through function learning, and the second stage improves translation readability and style consistency through style learning. This framework significantly reduces the dependence on large-scale models, enabling small models to achieve performance comparable to or even better than large models.
[0137] S5. Obtain the code to be translated, input it into the trained code translation model, and obtain the code translation result.
[0138] This invention proposes a novel Function-to-Style Guided Transform (F2STrans) for large-scale language models, aiming to significantly improve the performance of large-scale language models in code translation tasks. This paradigm progressively optimizes the functional correctness and readability of translated code through a two-stage training method:
[0139] (1) Functional Learning: Train the object code with high-quality source-target code to ensure that the generated target code is consistent with the source code in terms of functionality.
[0140] (2) Style Learning: Training is performed based on positive and negative style examples to improve the readability and style consistency of the generated code.
[0141] Through the above methods, this invention not only solves the shortcomings of existing technologies in terms of functional correctness, but also significantly improves the readability of translated code, making it more in line with developers' coding habits and team standards.
[0142] In this embodiment of the invention, the functional correctness of the code translation model is significantly improved: In the first stage of F2STrans, functional learning is employed to ensure the functional consistency of the generated code by mining high-quality source-target code pairs and combining this with differential testing. Specifically: First, code pairs with identical input-output behaviors are selected using code data from an online programming platform. Then, differential testing further verifies the functional consistency of these code pairs, thereby constructing a high-quality training dataset. Finally, function-oriented learning improves model performance.
[0143] Experiments show that functional learning significantly improves the functional correctness of generated code. For example, in the CodeNet benchmark, the functionally learned Qwen-0.5B model outperformed the RAG-based Qwen-32B model.
[0144] Enhancing the readability of code translation: The second stage of F2STrans, style learning, enables the model to generate target code that is both functionally correct and stylistically consistent through comparative learning of positive and negative style examples. Specifically, this includes: using a powerful LLM (such as Qwen32B) to generate multiple stylistically consistent positive translation candidates, and selecting the optimal translation through differential testing; suppressing inconsistent generation results using negative translation examples; and introducing a style consensus selection mechanism and a list-wise loss function to further optimize the style learning effect.
[0145] Style learning significantly improves the readability and style consistency of the generated code. For example, in the benchmark tests of this invention, the F2STrans-improved Qwen-0.5B model achieved a high score of 80.7 on the CCSim metric, surpassing GPT-4 and other baseline models.
[0146] Significantly reduced model size dependence: F2STrans employs efficient training strategies that enable small models to achieve performance comparable to or even better than large models. For example, Qwen-1.5B outperforms Qwen32B and GPT-4 under function-to-style guidance. This feature significantly reduces reliance on large-scale models, allowing resource-constrained developers to complete high-quality code translation tasks using small models. This not only reduces hardware costs but also improves model deployment flexibility.
[0147] Figure 5This is a block diagram illustrating a large model code translation apparatus that integrates code functionality and style, according to an exemplary embodiment. The apparatus is used in a large model code translation method that integrates code functionality and style. (Refer to...) Figure 5 The device includes a functionally consistent dataset construction module 310, a functional learning training module 320, a style-oriented dataset construction module 330, a style learning training module 340, and a code translation module 350. Among them:
[0148] The functional consistency dataset construction module 310 is used to obtain code pairs consisting of source code and target code from an online programming platform, process the code pairs according to similarity retrieval, fine-grained scoring and differential testing, and construct a functional consistency dataset.
[0149] The functional learning training module 320 is used to perform functional learning training on a large model based on a functional consistency dataset and instruction fine-tuning methods, resulting in a large model trained through functional learning.
[0150] The style-oriented dataset building module 330 is used to obtain source code, generate positive and negative sample translations of the source code, and build a style-oriented dataset.
[0151] The style learning training module 340 is used to train a large model trained through function learning based on a style-oriented dataset, resulting in a well-trained code translation model.
[0152] The code translation module 350 is used to acquire the code to be translated, input it into the trained code translation model, and obtain the code translation result.
[0153] In this embodiment of the invention, the functional correctness of the code translation model is significantly improved: In the first stage of F2STrans, functional learning is employed to ensure the functional consistency of the generated code by mining high-quality source-target code pairs and combining this with differential testing. Specifically: First, code pairs with identical input-output behaviors are selected using code data from an online programming platform. Then, differential testing further verifies the functional consistency of these code pairs, thereby constructing a high-quality training dataset. Finally, function-oriented learning improves model performance.
[0154] Experiments show that functional learning significantly improves the functional correctness of generated code. For example, in the CodeNet benchmark, the functionally learned Qwen-0.5B model outperformed the RAG-based Qwen-32B model.
[0155] Enhancing the readability of code translation: The second stage of F2STrans, style learning, enables the model to generate target code that is both functionally correct and stylistically consistent through comparative learning of positive and negative style examples. Specifically, this includes: using a powerful LLM (such as Qwen32B) to generate multiple stylistically consistent positive translation candidates, and selecting the optimal translation through differential testing; suppressing inconsistent generation results using negative translation examples; and introducing a style consensus selection mechanism and a list-wise loss function to further optimize the style learning effect.
[0156] Style learning significantly improves the readability and style consistency of the generated code. For example, in the benchmark tests of this invention, the F2STrans-improved Qwen-0.5B model achieved a high score of 80.7 on the CCSim metric, surpassing GPT-4 and other baseline models.
[0157] Significantly reduced model size dependence: F2STrans employs efficient training strategies that enable small models to achieve performance comparable to or even better than large models. For example, Qwen-1.5B outperforms Qwen32B and GPT-4 under function-to-style guidance. This feature significantly reduces reliance on large-scale models, allowing resource-constrained developers to complete high-quality code translation tasks using small models. This not only reduces hardware costs but also improves model deployment flexibility.
[0158] Figure 6 This is a schematic diagram of the structure of a large model code translation device provided in an embodiment of the present invention, as shown below. Figure 6 As shown, the large model code translation device may include the above-mentioned Figure 5 The large model code translation device shown integrates code functionality and style. Optionally, the large model code translation device 410 may include a first processor 2001.
[0159] Optionally, the large model code translation device 410 may also include a memory 2002 and a transceiver 2003.
[0160] The first processor 2001, memory 2002, and transceiver 2003 can be connected via a communication bus.
[0161] The following is combined with Figure 6 A detailed description of each component of the large model code translation device 410 is provided below:
[0162] The first processor 2001 is the control center of the large model code translation device 410. It can be a single processor or a collective term for multiple processing elements. For example, the first processor 2001 can be one or more central processing units (CPUs), application-specific integrated circuits (ASICs), or one or more integrated circuits configured to implement embodiments of the present invention, such as one or more digital signal processors (DSPs), or one or more field-programmable gate arrays (FPGAs).
[0163] Optionally, the first processor 2001 can perform various functions of the large model code translation device 410 by running or executing software programs stored in the memory 2002 and calling data stored in the memory 2002.
[0164] In a specific implementation, as one example, the first processor 2001 may include one or more CPUs, for example... Figure 6 CPU0 and CPU1 are shown in the diagram.
[0165] In a specific implementation, as one example, the large model code translation device 410 may also include multiple processors, for example... Figure 6 The first processor 2001 and the second processor 2004 are shown in the diagram. Each of these processors can be a single-core processor or a multi-core processor. Here, a processor can refer to one or more devices, circuits, and / or processing cores used to process data (such as computer program instructions).
[0166] The memory 2002 is used to store the software program that executes the present invention, and is controlled by the first processor 2001 to execute it. The specific implementation method can be referred to the above method embodiment, and will not be repeated here.
[0167] Optionally, the memory 2002 may be a read-only memory (ROM) or other type of static storage device capable of storing static information and instructions, random access memory (RAM) or other type of dynamic storage device capable of storing information and instructions, or electrically erasable programmable read-only memory (EEPROM), compact disc read-only memory (CD-ROM) or other optical disc storage, optical disc storage (including compressed optical discs, laser discs, optical discs, digital universal optical discs, Blu-ray discs, etc.), magnetic disk storage media or other magnetic storage devices, or any other medium capable of carrying or storing desired program code in the form of instructions or data structures and accessible by a computer, but not limited thereto. The memory 2002 may be integrated with the first processor 2001 or may exist independently, and may be connected via the interface circuit of the large model code translation device 410. Figure 6 (Not shown in the image) is coupled to the first processor 2001, and this embodiment of the invention does not specifically limit this.
[0168] The transceiver 2003 is used to communicate with network devices or with terminal devices.
[0169] Alternatively, transceiver 2003 may include a receiver and a transmitter. Figure 6 (Not shown separately). The receiver is used to implement the receiving function, and the transmitter is used to implement the transmitting function.
[0170] Alternatively, the transceiver 2003 can be integrated with the first processor 2001, or it can exist independently and be connected to the interface circuit of the large model code translation device 410. Figure 6 (Not shown in the image) is coupled to the first processor 2001, and this embodiment of the invention does not specifically limit this.
[0171] It should be noted that, Figure 6 The structure of the large model code translation device 410 shown does not constitute a limitation on the router. The actual knowledge structure identification device may include more or fewer components than shown, or combine certain components, or have different component arrangements.
[0172] Furthermore, the technical effect of the large model code translation device 410 can be referred to the technical effect of the large model code translation method that integrates code functions and styles as described in the above method embodiments, and will not be repeated here.
[0173] It should be understood that the first processor 2001 in the embodiments of the present invention may be a central processing unit (CPU), or it may be other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor may be a microprocessor or any conventional processor, etc.
[0174] It should also be understood that the memory in the embodiments of the present invention can be volatile memory or non-volatile memory, or may include both volatile and non-volatile memory. The non-volatile memory can be read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), or flash memory. The volatile memory can be random access memory (RAM), which is used as an external cache. By way of example, but not limitation, many forms of random access memory (RAM) are available, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate synchronous DRAM (DDR SDRAM), enhanced synchronous DRAM (ESDRAM), synchronous linked DRAM (SLDRAM), and direct rambus RAM (DR RAM).
[0175] The above embodiments can be implemented, in whole or in part, by software, hardware (such as circuits), firmware, or any other combination thereof. When implemented using software, the above embodiments can be implemented, in whole or in part, as a computer program product. The computer program product includes one or more computer instructions or computer programs. When the computer instructions or computer programs are loaded or executed on a computer, all or part of the processes or functions described in the embodiments of the present invention are generated. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via wired (e.g., infrared, wireless, microwave, etc.) means. The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more sets of available media. The available medium can be a magnetic medium (e.g., floppy disk, hard disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium. A semiconductor medium can be a solid-state drive.
[0176] It should be understood that the term "and / or" in this article is merely a description of the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A existing alone, A and B existing simultaneously, or B existing alone. A and B can be singular or plural. Additionally, the character " / " in this article generally indicates an "or" relationship between the preceding and following related objects, but it can also represent an "and / or" relationship. Please refer to the context for a more accurate understanding.
[0177] In this invention, "at least one" means one or more, and "more than one" means two or more. "At least one of the following" or similar expressions refer to any combination of these items, including any combination of a single item or a plurality of items. For example, at least one of a, b, or c can represent: a, b, c, ab, ac, bc, or abc, where a, b, and c can be a single item or multiple items.
[0178] It should be understood that, in various embodiments of the present invention, the order of the above-mentioned process numbers does not imply the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present invention.
[0179] Those skilled in the art will recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementations should not be considered beyond the scope of this invention.
[0180] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working processes of the devices, apparatuses, and units described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here.
[0181] In the several embodiments provided by this invention, it should be understood that the disclosed devices, apparatuses, and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another device, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between devices or units may be electrical, mechanical, or other forms.
[0182] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0183] In addition, the functional units in the various embodiments of the present invention can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit.
[0184] If the aforementioned functions are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this invention, or the part that contributes to the prior art, or a part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0185] The above description is merely a specific embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the technical scope disclosed in the present invention should be included within the scope of protection of the present invention. Therefore, the scope of protection of the present invention should be determined by the scope of the claims.
Claims
1. A method for translating large model code that integrates code functionality and style, characterized in that, The method includes: S1. Obtain code pairs consisting of source code and target code from an online programming platform, process the code pairs according to similarity retrieval, fine-grained scoring and differential testing, and construct a functional consistency dataset; S2. Based on the functional consistency dataset and the instruction fine-tuning method, perform functional learning training on the large model to obtain a large model trained through functional learning. S3. Obtain the source code, generate positive and negative sample translations of the source code, and construct a style-oriented dataset; S4. Perform style learning training on the large model trained by functional learning based on the style-oriented dataset to obtain a trained code translation large model; S5. Obtain the code to be translated, input it into the trained code translation model, and obtain a code translation result that is both functionally correct and stylistically consistent. The S1 step involves processing the code pairs based on similarity retrieval, fine-grained scoring, and differential testing to construct a functional consistency dataset, including: S11. Use a lightweight code embedding model to perform similarity retrieval on the code pairs to obtain similar code pairs; S12. Perform fine-grained scoring on the similar code pairs, and filter the similar code pairs based on the fine-grained scoring results to obtain the filtered code pairs; S13. Perform differential testing on the filtered code pairs, retain code pairs with consistent input and output behavior, and obtain the tested code pairs. S14. Construct a functionally consistent dataset based on the tested code; The positive and negative sample translations generated in S3 of the source code include: S31. Generate multiple translation candidates with consistent style from the source code, filter out the functionally correct translations from the translation candidates through differential testing, and select the optimal translation from the functionally correct translations as the positive sample translations using a style consensus selection mechanism, as shown in the following formula (4): (4) In the formula, Indicates positive sample translation. This indicates that all functions selected by the differential detection are correctly translated. This represents a function used to measure the stylistic similarity between two target language codes. Indicates target code The One token, Indicates target code The One token; S32. Generate multiple negative translation candidates from the source code, and use a function to select translations from the negative translation candidates whose style difference from the positive sample translation exceeds a preset threshold as negative sample translations; The optimization objective function for style learning training in S4 is shown in equations (5) and (6) below: (5) (6) In the formula, The objective function for optimizing style learning training is represented. This represents the source code. Indicates positive sample translation. This represents the set of all negative sample translations. This represents an exponential function with base e. This indicates that the large model is inputting source code. At that time, positive sample translations were successfully generated. The probability, Represents the entire set of target code. The representation model for the source code Generate target code The probability, This indicates that the large model is inputting source code. And generate target code The former When the first token is generated, the second token is successfully generated. The probability of each token. express The One token, This indicates that the input is used to process the source code in the large model. The prompt words translated into the target code. express The former A sequence of tokens.
2. The method for translating large model code that integrates code functionality and style according to claim 1, characterized in that, The fine-grained scoring of the similar code pairs in S1 includes: The similar code pairs are scored in a fine-grained manner using the following formulas (1) and (2): (1) (2) In the formula, This represents the final, fine-grained score. Represents source code and target code Similarity score, express and Rating: The softmax normalized probability, A prompt template representing a large model. This represents an exponential function with base e. Represents the generation of large models and The rating is The log-likelihood probability, This indicates that each score is calculated individually. Represents the generation of large models and The rating is The log-likelihood probability.
3. The method for translating large model code that integrates code functionality and style according to claim 1, characterized in that, The optimization objective function for the functional learning training in S2 is shown in equation (3) below: (3) In the formula, This represents the objective function to be optimized. Indicates target code The One token, This indicates that the input is used to process the source code in the large model. The prompt words translated into the target code. Indicates target code The former A token.
4. The method for translating large model code that integrates code functionality and style according to claim 1, characterized in that, The style learning training also includes fine-tuning instructions in forward translation, as shown in equation (7) below: (7) In the formula, This represents the numerical value of the loss function for style learning. This represents the balance hyperparameter between the two losses. The objective function for optimizing style learning training is represented. express and Fine-tuning the loss function between instructions, This indicates a positive sample translation.
5. A large model code translation apparatus that integrates code functionality and style, wherein the large model code translation apparatus that integrates code functionality and style is used to implement the large model code translation method that integrates code functionality and style as described in any one of claims 1-4, characterized in that, The device includes: The functional consistency dataset construction module is used to obtain code pairs consisting of source code and target code from an online programming platform, process the code pairs according to similarity retrieval, fine-grained scoring and difference testing, and construct a functional consistency dataset. The functional learning training module is used to perform functional learning training on the large model based on the functional consistency dataset and the instruction fine-tuning method to obtain a large model trained through functional learning. A style-oriented dataset construction module is used to obtain source code, generate positive and negative sample translations of the source code, and construct a style-oriented dataset. The style learning training module is used to perform style learning training on the large model trained by functional learning based on the style-oriented dataset, so as to obtain a trained code translation large model. The code translation module is used to acquire the code to be translated, input it into the trained code translation model, and obtain the code translation result.
6. A large model code translation device, characterized in that, The large model code translation device includes: processor; A memory storing computer-readable instructions that, when executed by the processor, implement the method as described in any one of claims 1 to 4.
7. A computer-readable storage medium, characterized in that, The computer-readable storage medium contains program code that can be invoked by a processor to execute the method as described in any one of claims 1 to 4.