Code generation method and apparatus, storage medium and electronic device

Through the multi-task learning method of code understanding tasks and code generation tasks, the problem that the code generation model in the existing technology cannot effectively learn semantics and structure is solved, and more accurate and efficient code generation is achieved.

WO2024174911A1PCT designated stage expired Publication Date: 2024-08-29DOUYIN VISION CO LTD

Patent Information

Application Number
PCT/CN2024/076711
Authority / Receiving Office
WO · WO
Patent Type
Applications
Current Assignee / Owner
Priority Date
2023-02-23
Filing Date
2024-02-07
Publication Date
2024-08-29

AI Technical Summary

Technical Problem

Existing code generation methods are mainly based on conditional probabilistic language models, which cannot effectively learn the semantic and structural information of program codes, resulting in insufficient accuracy of generated codes.

Method used

A multi-task learning method using code understanding tasks and code generation tasks is adopted. The syntax and semantic features of the program code are learned through the code understanding task, and the code generation task generates new program codes, thereby improving the accuracy of the code generation model.

Benefits of technology

By combining syntax and semantic knowledge to generate more accurate target program code, the efficiency and accuracy of code generation are improved, and the syntax and semantic knowledge of the target text are effectively captured.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN2024076711_29082024_PF_FP_ABST
    Figure CN2024076711_29082024_PF_FP_ABST
Patent Text Reader

Abstract

The present disclosure relates to a code generation method and apparatus, a storage medium and an electronic device, which aim to improve the accuracy of automatically generating codes. The method comprises: acquiring a target text, the target text comprising a program code text to be supplemented or a natural language text used for describing a code function; and inputting the target text into a code generation model to obtain target program codes generated on the basis of the target text, the code generation model being obtained by training a code understanding task and a code generation task, the code understanding task being used for the code generation model to learn grammatical features and semantic features of sample program codes, and the code generation task being used for the code generation model to learn a process of generating new program codes on the basis of the sample program codes.
Need to check novelty before this filing date? Find Prior Art

Description

Code generation method, device, storage medium and electronic device

[0001] CROSS-REFERENCE TO RELATED APPLICATIONS

[0002] This application claims the benefit of Chinese Patent Application No. 202310190548.6, filed on February 23, 2023. The entire teachings of the above application are incorporated herein by reference. Technical Field

[0003] The present disclosure relates to the field of computer technology, and in particular to a code generation method, device, storage medium, and electronic device. Background Art

[0004] Automatic program code generation is a key technical tool for software intelligence. It can assist software developers in developing common code, effectively improving R&D efficiency and reducing R&D costs. Furthermore, with the continuous development of natural language processing technology, pre-trained language models are widely used in program code generation. However, related technologies primarily use conditional probabilistic language models for code generation. This involves predicting the probability of the next program symbol based on existing contextual information, and can only learn the statistical distribution of program symbols.

[0005] Summary of the Invention

[0006] This summary is provided to briefly introduce concepts that will be described in detail in the detailed description below. This summary is not intended to identify key features or essential features of the claimed technical solution, nor is it intended to limit the scope of the claimed technical solution.

[0007] In a first aspect, the present disclosure provides a code generation method, the method comprising:

[0008] Acquiring a target text, wherein the target text includes a program code text to be supplemented or a natural language text for describing a code function;

[0009] The target text is input into a code generation model to obtain a target program code generated based on the target text, wherein the code generation model is trained by a code comprehension task and a code generation task, and the code comprehension task is used by the code generation model to learn the grammatical features and semantic features of the sample program code, and the code generation task is used by the code generation model to learn the process of generating new program code based on the sample program code.

[0010] In a second aspect, the present disclosure provides a code generation device, the device comprising:

[0011] An acquisition module, configured to acquire a target text, wherein the target text is a program code text to be supplemented or a natural language text for describing a code function;

[0012] A generation module is used to input the target text into a code generation model to obtain a target program code generated based on the target text, wherein the code generation model is trained by a code comprehension task and a code generation task, and the code comprehension task is used by the code generation model to learn the grammatical features and semantic features of the sample program code, and the code generation task is used by the code generation model to learn the process of generating new program code based on the sample program code.

[0013] In a third aspect, the present disclosure provides a non-transitory computer-readable medium having a computer program stored thereon, which implements the steps of the method described in the first aspect when executed by a processing device.

[0014] In a fourth aspect, the present disclosure provides an electronic device, comprising:

[0015] a storage device having a computer program stored thereon;

[0016] A processing device is used to execute the computer program in the storage device to implement the steps of the method in the first aspect.

[0017] Through the above technical solution, a code generation model can be trained through code comprehension and code generation tasks. The code comprehension task allows the code generation model to learn the syntax and semantics of sample program code, while the code generation task allows the code generation model to learn how to generate new program code based on the sample program code. This allows the trained code generation model to generate target program code based on the syntax and semantics of the target text, improving the accuracy of automatic code generation.

[0018] Other features and advantages of the present disclosure will be described in detail in the following detailed description. BRIEF DESCRIPTION OF THE DRAWINGS

[0019] The above and other features, advantages and aspects of the various embodiments of the present disclosure will become more apparent with reference to the following detailed description in conjunction with the accompanying drawings. Throughout the drawings, the same or similar reference numerals represent the same or similar elements. It should be understood that the drawings are schematic and that the originals and elements are not necessarily drawn to scale. In the drawings:

[0020] FIG1 is a flowchart of a code generation method according to an exemplary embodiment of the present disclosure;

[0021] FIG2 is a schematic diagram showing a process of a code generation method according to an exemplary embodiment of the present disclosure;

[0022] FIG3 is a schematic diagram of a code generation model in a code generation method according to an exemplary embodiment of the present disclosure;

[0023] FIG4 is a schematic diagram showing a process of a code generation method according to another exemplary embodiment of the present disclosure;

[0024] FIG5 is a schematic diagram of a mask matrix in a code generation method according to an exemplary embodiment of the present disclosure;

[0025] FIG6 is a schematic diagram showing a process of a code generation method according to another exemplary embodiment of the present disclosure;

[0026] FIG7 is a block diagram of a code generating apparatus according to an exemplary embodiment of the present disclosure;

[0027] FIG8 is a block diagram of an electronic device according to an exemplary embodiment of the present disclosure. DETAILED DESCRIPTION

[0028] The following describes embodiments of the present disclosure in more detail with reference to the accompanying drawings. Although certain embodiments of the present disclosure are shown in the accompanying drawings, it should be understood that the present disclosure can be implemented in various forms and should not be construed as limited to the embodiments described herein. Rather, these embodiments are provided to provide a more thorough and complete understanding of the present disclosure. It should be understood that the drawings and embodiments of the present disclosure are for illustrative purposes only and are not intended to limit the scope of protection of the present disclosure.

[0029] It should be understood that the various steps described in the method embodiments of the present disclosure may be performed in different orders and / or in parallel. In addition, the method embodiments may include additional steps and / or omit the steps shown. The scope of the present disclosure is not limited in this respect.

[0030] As used herein, the term "including" and its variations are open-ended, i.e., "including but not limited to." The term "based on" means "based, at least in part, on." The term "one embodiment" means "at least one embodiment," the term "another embodiment" means "at least one additional embodiment," and the term "some embodiments" means "at least some embodiments." Other terms are defined in the following description.

[0031] It should be noted that the concepts of "first" and "second" mentioned in this disclosure are only used to distinguish different devices, modules or units, and are not used to limit the order or interdependence of the functions performed by these devices, modules or units. It should also be noted that the modifications of "one" and "a plurality of" mentioned in this disclosure are illustrative and not restrictive. Those skilled in the art should understand that unless the context clearly indicates otherwise, they should be understood as "one or more".

[0032] The names of the messages or information exchanged between multiple devices in the embodiments of the present disclosure are only used for illustrative purposes and are not used to limit the scope of these messages or information.

[0033] It is understandable that before using the technical solutions disclosed in the various embodiments of this disclosure, the type, scope of use, usage scenarios, etc. of the personal information involved in this disclosure should be informed to the user and the user's authorization should be obtained in an appropriate manner in accordance with relevant laws and regulations.

[0034] For example, in response to a user's active request, a prompt message is sent to the user to clearly inform the user that the operation requested will require the acquisition and use of the user's personal information. This allows the user to independently choose whether to provide personal information to the electronic device, application, server, storage medium, or other software or hardware that performs the operations of the disclosed technical solution based on the prompt message.

[0035] As an optional but non-limiting implementation, in response to receiving a user's active request, the prompt information may be sent to the user in the form of a pop-up window, in which the prompt information may be presented in text form. Furthermore, the pop-up window may also contain a selection control for the user to select "agree" or "disagree" to provide personal information to the electronic device.

[0036] It is understandable that the above notification and user authorization process are merely illustrative and do not limit the implementation of the present disclosure. Other methods that comply with relevant laws and regulations may also be applied to the implementation of the present disclosure.

[0037] At the same time, it is understandable that the data involved in this technical solution (including but not limited to the data itself, the acquisition or use of the data) must comply with the requirements of relevant laws, regulations and relevant provisions.

[0038] As mentioned in the background, with the continuous development of natural language processing technology, pre-trained language models have been widely used for program code generation. However, these methods treat program code as a sequence of symbols similar to natural language text and use conditional probabilistic autoregressive generation to train code generation. This method cannot effectively learn the semantics of program code, only the statistical distribution of program symbols, and cannot effectively capture the structural information of program code.

[0039] Based on this, the present disclosure provides a code generation method that can integrate a multi-task learning method of program code understanding and program code generation in the training process of the code generation model. That is to say, in order to enable the model to effectively learn the grammatical and semantic knowledge of the program code, the present disclosure sets two major types of training tasks in terms of training objectives: code understanding tasks and code generation tasks. In the code understanding task, the model can learn the basic grammatical knowledge and semantic knowledge of the program code, so that in the model application stage, the model can generate program code according to the grammar and semantics of the input text, thereby improving the accuracy of the generated code.

[0040] FIG1 is a flow chart of a code generation method according to an exemplary embodiment of the present disclosure. Referring to FIG1 , the code generation method may include:

[0041] Step 101: Obtain target text, which includes program code text to be supplemented or natural language text used to describe code functions.

[0042] In step 102, the target text is input into a code generation model to generate target program code based on the target text. The code generation model is trained using code comprehension tasks and code generation tasks. The code comprehension tasks are used by the code generation model to learn the grammatical and semantic features of sample program code, while the code generation tasks are used by the code generation model to learn how to generate new program code based on the sample program code.

[0043] It should be understood that the target text input into the code generation model in the present disclosure may include program code to be supplemented or natural language text for describing the function of the code. Among them, the program code to be supplemented may be an incomplete program code. After the program code to be supplemented is input into the code generation model, the code generation model can output a complete program code based on the program code to be supplemented. The natural language text used to describe the function of the code may be, for example, a natural language text determined according to an actual business function. After the natural language text is input into the code generation model, the code generation model can output a program code that can realize the business function. In this way, the automatic generation of code can be achieved, the efficiency of code generation can be improved, and the code understanding and code generation pre-training tasks are simultaneously introduced in the training phase of the code generation model, so that the code generation model can effectively capture the grammatical and semantic knowledge in the target text in the application phase to enhance the performance of code generation and improve the accuracy of the generated program code.

[0044] The following first describes the training process of the code generation model.

[0045] First, a sample program code can be obtained, then tokenized and split into fixed-length token sequences, and finally a code generation model is trained based on the token sequences. The fixed length can be 1024 tokens (segmented words).

[0046] In some embodiments, sample program code can be obtained as follows: first, a code dataset is obtained, which includes multiple code files. Then, for each code file, at least one of the following preprocessing steps is performed to obtain a training dataset: when the ratio of the number of characters in the code file to the total number of symbols is greater than or equal to a preset character ratio, the code file is added to the training dataset; when the average number of characters per line of code in the code file is less than or equal to a first preset threshold, the code file is added to the training dataset; when the number of characters in the annotation information in the code file is less than or equal to a second preset threshold, the code file is added to the training dataset. Finally, the program code corresponding to each code file in the training dataset is determined as the sample program code.

[0047] The preset character ratio, the first preset threshold, and the second preset threshold can be set according to actual conditions, and are not limited in the present embodiment. For example, the preset character ratio can be set to 40%, the first preset threshold can be set to 100, and the second preset threshold can be set to 500.

[0048] It should be understood that the directly acquired code data set usually contains a large amount of repeated code data and low-quality code data. In order to prevent these repeated code data and low-quality data from affecting the learning process of the model, the acquired code data set can be preprocessed first.

[0049] For example, referring to Figure 2, first, the code files in the code dataset are traversed one by one and the MD5 (Message-Digest Algorithm) hash values ​​of the files are calculated. After the hash value is calculated for the first time, it is stored. After the hash value is calculated for the second time, it is compared with the stored hash value. If the hash value exists, it indicates that it is a duplicate file and can be discarded. If the hash value does not exist, the hash value of this time is stored. And so on, the code files in the code dataset are deduplicated. After that, the ratio of the number of characters in the file (the number of characters consisting of at least one letter of the 26 English letters) to the total number of symbols in the code file (the number of letters, spaces, and other special characters, where one space is counted as one character) and the average number of characters per line in the code file can be calculated. If the ratio of the number of characters in the code file to the total number of symbols is less than 40% or the average number of characters per line in the code file exceeds 100, it can be considered that this is a code file containing a lot of noise, so the code file is not added to the training dataset and the calculation continues to the next code file. Conversely, if the number of characters in a code file accounts for 40% or more of the total number of symbols, or if the average number of characters per line in the code file does not exceed 100, the code file can be added to the training dataset. Next, during the preprocessing phase, annotations exceeding 500 characters can be removed from the code file to improve the quality of the training data. Finally, the program code corresponding to each code file in the training dataset is determined as the sample program code.

[0050] Through the above method, we can filter out duplicate and low-quality code data based on the original code data and establish a high-quality training data set, thereby reducing the impact of duplicate and low-quality code data on model training, improving the training effect, and further improving the accuracy of the results of the code generation model after training.

[0051] In some embodiments, the code generation model includes a code understanding layer, a code generation layer, and a shared representation layer, and the result output by the shared representation layer can be used to input the code understanding layer or the code generation layer. Accordingly, the training step of the code generation model includes: when performing a code understanding task, masking the sample program code to obtain a mask sequence, inputting the mask sequence into the code generation model to obtain a first predicted code through the shared representation layer and the code understanding layer, and adjusting the parameters of the code understanding layer and the shared representation layer based on the first predicted code. When performing a code generation task, inputting the sample program code into the code generation model to obtain a second predicted code through the shared representation layer and the code generation layer, and adjusting the parameters of the code generation layer and the shared representation layer based on the result output by the code generation model.

[0052] In other words, when performing code comprehension tasks, training can be performed using a masked language model, and when performing code generation tasks, training can be performed using a conditional language model.

[0053] For example, referring to Figure 3, the code generation model includes a shared representation layer, a code comprehension layer, and a code generation layer. During the training phase, the parameters of the shared representation and code comprehension layers can be adjusted through code comprehension tasks, and then the parameters of the shared representation and code generation layers can be adjusted through code generation tasks. This allows the model to identify the grammatical and semantic knowledge of the target text during application, thereby using this grammatical and semantic knowledge to output more accurate target program code.

[0054] The following describes the training process of the code comprehension task.

[0055] In some embodiments, masking is performed on the sample program code to obtain a masked sequence, including: performing word segmentation processing on the sample program code to obtain a word segmentation sequence; randomly selecting a first preset proportion of first target word segments in the word segmentation sequence, and replacing the first target word segments with a first preset mask symbol to obtain a masked sequence; and / or randomly selecting a second preset proportion of second target word segments in the word segmentation sequence, and for each second target word segmentation, randomly selecting a target residual word from the residual word segments, replacing the second target word segmentation with the target residual word segmentation to obtain a masked sequence.

[0056] Among them, the remaining segmentation words are segmentation words other than the second target segmentation words in the segmentation sequence. The first preset ratio, the second preset ratio and the first preset mask symbol can be set according to actual conditions, and the embodiment of the present disclosure does not limit this. For example, the first preset ratio is set to 12%, the second preset ratio is set to 1.5%, and the first preset mask symbol is set to [MASK]. In addition, segmentation words of the first preset ratio and the second preset ratio can be directly selected, or a certain proportion of segmentation words can be selected first, and then a certain proportion of segmentation words can be selected from the selected segmentation words. For example, after determining the segmentation sequence corresponding to the training sample, 15% of the tokens in the segmentation sequence are randomly selected in units of tokens, and then 80% of the tokens in the selected tokens (i.e., the first target segmentation words) are replaced with the [MASK] symbol, and 10% of the tokens in the selected tokens (i.e., the second target segmentation words) are randomly replaced with other tokens to obtain a mask sequence. Thus, by training the code generation model with the mask sequence, the model can learn to recover the information of the masked segmentation words from the mask sequence, thereby allowing the model to learn the basic grammatical knowledge of the program code.

[0057] In some embodiments, masking is performed on the sample program code to obtain a mask sequence, including: performing word segmentation on the sample program code to obtain a word segmentation sequence. Then, function word segmentations used to represent function names are determined in the word segmentation sequence, and a third preset ratio of target function word segmentations are randomly selected from the function word segmentations, and the target function word segmentations are replaced with a second preset mask symbol to obtain a mask sequence; and / or, interface word segmentations used to represent interface names are determined in the word segmentation sequence, and a fourth preset ratio of target interface word segmentations are randomly selected from the interface word segmentations, and the target interface word segmentations are replaced with a third preset mask symbol to obtain a mask sequence.

[0058] Among them, the third preset ratio, the fourth preset ratio, the second preset mask symbol and the third preset mask symbol can be set according to actual conditions, and the embodiment of the present disclosure does not limit this. For example, the third preset ratio and the fourth preset ratio are both set to 20%, and the third preset mask symbol and the fourth preset mask symbol are both set to [MASK]. That is, the token of the function name in the sample program code can be replaced with the [MASK] symbol at a ratio of 20%, and / or, the token of the interface (API, Application Programming Interface) name in the sample program code can be replaced with the [MASK] symbol at a ratio of 20% to obtain a mask sequence. Afterwards, the code generation model is trained with the mask sequence, so that the model can learn to restore the masked function name and API name, thereby allowing the model to learn the correspondence between the function name and the function body and semantic knowledge such as the application program interface.

[0059] In practical applications, mask processing can be performed using any of the above methods, or by combining the above two methods, which is not limited in the embodiments of the present disclosure.

[0060] After obtaining the mask sequence, the mask sequence can be input into the code generation model to obtain the first predicted code through the shared representation layer and the code understanding layer. In some embodiments, the feature vector sequence corresponding to the mask sequence can be first determined. Then, for each feature vector in the feature vector sequence, the shared representation layer performs calculations based on all feature vectors before and after the feature vector in the feature vector sequence and the attention mechanism to obtain an intermediate feature vector. For each intermediate feature vector, the code understanding layer performs attention calculations based on the intermediate feature vectors other than the intermediate feature vector and the attention mechanism to obtain a target feature vector. Finally, based on the target feature vector, the first predicted program code is obtained.

[0061] The shared representation layer performs calculations based on all feature vectors before and after the feature vector in the feature vector sequence and the attention mechanism, which is equivalent to the shared representation layer performing bidirectional attention calculations. The code comprehension layer performs attention calculations based on the intermediate feature vectors (excluding the intermediate feature vectors) and the attention mechanism, which is equivalent to the code comprehension layer performing bidirectional attention calculations. Therefore, when performing code comprehension tasks, both the shared representation layer and the code comprehension layer perform bidirectional attention calculations, allowing code comprehension to be combined with the previous and next contexts, more accurately identifying the grammatical and semantic knowledge of the input text.

[0062] For example, referring to Figure 4, the execution process of each layer of the model in the training phase is explained by taking the code generation model as Transformer as an example. First, the code fragment after the sample program code is segmented passes through the word embedding layer and the position encoding layer to obtain the word vector representation of each word. Among them, the word embedding layer obtains the corresponding word embedding vector from the word embedding matrix by looking up the table. The word vector matrix is ​​a matrix of size 42000×1024, where 42000 is the size of the vocabulary and 1024 is the dimension of the word vector. The position encoding layer calculates the position encoding vector of each word through the cosine-based position encoding method. Finally, the word embedding vector and the position encoding vector corresponding to each word are added to obtain the word vector of the word, as shown in formula (1):

[0063] in, Represents the word vector corresponding to the i-th word, WordEmbedding(i) represents the word embedding vector corresponding to the i-th word, and PositionEmbedding(i) represents the position encoding vector corresponding to the i-th word.

[0064] The sequence after the word embedding layer is input into the shared representation layer for encoding. The shared representation layer can be composed of a plurality of transformer basic blocks (transformer block), each of which is composed of a multi-head attention sublayer and a forward feedback sublayer (not shown in FIG4 ). Among them, the multi-head attention sublayer is used to calculate the correlation between each word in the input sequence. In practical applications, the number of attention heads can be set to 16, that is, 16 attention heads can be used, which is not limited in the embodiment of the present disclosure. In each attention head, the encoded representation hi of the i-th word can be calculated by the self-attention mechanism, as shown in formula (2):

[0065] Among them, Q represents the word vector H of the i-th word i The query vector obtained after the linear transformation of embedding, K represents the word vector H of the i-th word iThe key vector obtained after the linear transformation of embedding, V represents the word vector H of the i-th word i The value vector obtained after the linear change of embedding, K T represents the transpose of K, d k Indicates the dimension of K.

[0066] After obtaining the encoding representation h of a single attention head output i After that, the outputs of multiple attention heads are concatenated and passed through a fully connected layer to obtain the encoding representation m of the i-th word i , as shown in formula (3): m i =concat(h′1,..,h′ h )W o (3)

[0067] Among them, concat represents vector concatenation, h′1 represents the encoding representation of the output of the first attention head, and h′ h represents the encoding representation of the h-th attention head output, W o Represents the parameters of the multi-head attention sub-layer.

[0068] Furthermore, to prevent the gradient from disappearing, the output of the multi-head attention sub-layer can be obtained after a residual connection and normalization layer. As shown in formula (4):

[0069] Among them, LayerNorm represents the normalization function.

[0070] Afterwards, the forward feedback sublayer further processes the vector output by the multi-head attention sublayer. The forward feedback sublayer consists of two fully connected and one ReLU activation unit, and its calculation process is shown in formula (5):

[0071] Among them, max represents the maximum value function, h″ i represents the output of the forward feedback sublayer, W1 and W2 represent the parameters of the forward feedback sublayer, and b1 and b2 are the corresponding biases. It should be understood that the specific calculation process can refer to the calculation process of the transformer model in the relevant technology, and will not be repeated here.

[0072] Similarly, the output of the forward feedback sublayer also passes through the residual connection and normalization layer to obtain the output of the shared representation layer. As shown in formula (6):

[0073] Therefore, the shared representation layer can perform bidirectional attention calculation based on all the feature vectors before and after the feature vector in the feature vector sequence to obtain the intermediate feature vector, that is,

[0074] After that, for each intermediate feature vector, the code understanding layer can perform bidirectional attention calculation based on the intermediate feature vectors other than the intermediate feature vector to obtain the target feature vector. For example, using the above example, the output of the shared representation layer is After the bidirectional attention calculation of the code understanding sub-network, the corresponding target feature vector is obtained after passing through the residual layer and the forward feedback layer in sequence. This calculation process is similar to that of the shared representation layer. Please refer to the previous article and will not be repeated here.

[0075] Finally, a first predicted program code is obtained according to the target feature vector, so that parameters of the code understanding layer and the shared representation layer are adjusted based on the first predicted program code.

[0076] For example, following the above example, the mask sequence after masking is input into the code generation model to obtain the feature representation h of each word s . Then the corresponding words of the masked words are After the first linear transformation layer, the probability of the word belonging to different words in the vocabulary is obtained Then the probability is calculated through the softmax function to obtain the normalized probability p u , as shown in equations (7) and (8):

[0077] Among them, W u represents the parameters of the first linear transformation layer, b u Represents the corresponding bias, m represents the masked word, m i Represents the label of the correct word corresponding to the masked word, θ represents the parameters of the code generation model, and N represents the total number of words in the vocabulary.

[0078] Then, the loss function of the code comprehension task is calculated as shown in formula (9):

[0079] Among them, L1 represents the loss function value calculated in the code comprehension task, and M represents the number of masked words.

[0080] Finally, the parameters of the code understanding layer and the shared representation layer can be adjusted according to the L1 loss function value. This process is similar to the related technology and will not be repeated here.

[0081] Therefore, when performing code comprehension tasks, both the shared representation layer and the code comprehension layer perform bidirectional attention calculations, so that code comprehension can be performed by combining the previous and following context information, and the grammatical and semantic knowledge of the input text can be more accurately identified.

[0082] The following describes the training process of the code generation task.

[0083] When executing the code generation task, the sample program code can be input into the code generation model to obtain the second predicted code through the shared representation layer and the code generation layer, and the parameters of the code generation layer and the shared representation layer are adjusted based on the results output by the code generation model.

[0084] In some embodiments, the sample program code is input into the code generation model to obtain the second prediction code through the shared representation layer and the code generation layer, including: first determining the feature vector sequence corresponding to the sample program code, wherein the feature vector sequence includes N feature vectors, where N is a positive integer. Then, for the i-th feature vector in the feature vector sequence, the shared representation layer is used to calculate based on all feature vectors before and after the i-th feature vector in the feature vector sequence and the attention mechanism to obtain the i-th intermediate feature vector, where i is a positive integer. For the i-th intermediate feature vector, the code generation layer is used to calculate based on the preceding feature vector and the attention mechanism to obtain the target feature vector, where the preceding feature vector is all the intermediate feature vectors obtained before the i-th intermediate feature vector. Finally, based on the target feature vector, the second prediction program code is obtained.

[0085] Among them, the shared representation layer performs calculations based on all feature vectors before and after the i-th feature vector in the feature vector sequence and the attention mechanism, which is equivalent to the shared representation layer performing a bidirectional attention calculation. The code generation layer performs calculations based on the preceding feature vector and the attention mechanism, which is equivalent to the code generation layer performing a unidirectional attention calculation. In other words, when executing the code generation task, the shared representation layer performs a bidirectional attention calculation, and the code comprehension layer performs a unidirectional attention calculation, so that the next word can be predicted based on the previous context of the word to achieve code generation. In addition, since the shared representation layer can adjust parameters through the code comprehension task, the shared representation layer can combine the grammatical and semantic knowledge of the previous information to obtain the corresponding features and input them into the code generation layer for processing, and then can combine the grammatical and semantic knowledge of the input text to generate code, thereby improving the accuracy of code generation.

[0086] For example, referring to Figure 3, the multi-task layer contains two sub-networks: code comprehension and code generation. Both sub-networks are composed of multiple transformer basic blocks. The difference is that the code comprehension sub-network uses the same bidirectional attention calculation method as the shared representation layer, that is, when calculating the dependency of a word, it also calculates the dependency between it and the previous and next words, while the code generation sub-network uses a unidirectional attention calculation method, that is, when calculating the dependency of a word, it only calculates the dependency between the word and the previous word. For example, the code generation sub-network uses a mask-based attention mechanism to calculate the feature representation of the word, as shown in formula (10):

[0087] Among them, Q s express The query vector obtained after linear transformation, K s express The key vector obtained after linear transformation, V s express The value vector obtained after linear change, K T represents the transpose of K, d′ k K s M′ represents a mask matrix of size n×n, where n represents the length of the input sequence. The values ​​of all elements on the diagonal and to the left of the matrix are 1, while all elements on the right of the diagonal are 0. For example, Figure 5 shows an example of a mask matrix for an input sequence of length 6.

[0088] It should be understood that the other calculation processes of the code generation task sub-network can refer to the calculation process of the native transformer in the relevant technology, and will not be repeated here.

[0089] As mentioned above, in the code generation subnetwork, the mask matrix is ​​introduced to make the feature output of the i-th word It is obtained by weighted summation of its preceding feature vectors, so it represents the feature representation of the preceding sequence. Therefore, the feature output of each word calculated in the code generation subnetwork can be After the second linear transformation layer, the probability of each word belonging to different words in the vocabulary is obtained After the softmax function is calculated, the normalized probability P of each word in the vocabulary is obtained. g , as shown in equations (11) and (12)

[0090] Among them, W g represents the parameters of the second linear transformation layer, b g Represents the corresponding bias, m′ represents the next predicted word to be generated, m′i Indicates the label information of the correct word corresponding to the predicted word.

[0091] Then, the loss function of the code generation task is calculated as shown in formula (13):

[0092] Where L2 represents the loss function value calculated in the code generation task, and M″ represents the number of predicted words generated.

[0093] Finally, the parameters of the code generation layer and the shared representation layer can be adjusted according to the L2 loss function value. This process is similar to the related technology and will not be repeated here.

[0094] In the above manner, during the training process of the code generation model, the code understanding task and the code generation task can be iteratively executed. For example, in the first round of iteration, the code understanding task is first executed to train the model through the L1 loss function value. During this process, only the parameters of the shared representation layer and the code understanding layer of the model are updated. Subsequently, in the second round of iteration, the code generation task is executed to train the model through the L2 loss function value. During this process, only the parameters of the shared representation layer and the code generation layer are updated. This cycle is repeated until the conditions for the end of model training are met, such as the L1 loss function value and the L2 loss function value are both less than a preset threshold. In addition, during the entire training process, the gradient descent method can be used to update the corresponding parameters of the model, which is not limited in the embodiments of the present disclosure.

[0095] The following describes the application process of the code generation model.

[0096] After the code generation model is trained in the above manner, the natural language sentences or the program code fragments to be supplemented can be segmented to obtain a sequence of segmented words to be processed, and then the sequence of segmented words to be processed is input into the trained code generation model to obtain a new target program code.

[0097] It should be understood that in the application phase, the code generation model only uses the shared representation layer and the code generation layer to predict the probability of each word in the target program code. The steps may include: 1. Segmenting the natural language sentence or the program code fragment to be supplemented to obtain a sequence of segmented words to be processed. 2. Inputting the sequence of segmented words to be processed into the code generation model and obtaining the feature representation of the next predicted word after encoding. 3. Represent the features of the next predicted word After being sent to the linear decoding layer, the probability distribution P of the predicted word in the vocabulary is obtained after the softmax function is calculated. g ′, the specific calculation process can refer to formula (11) and (12). 4. After obtaining the probability distribution P g', select the target predicted word to be output based on the probability. 5. Concatenate the target predicted word to the sequence of words to be processed and input it into the model. Repeat steps 3 and 4 to obtain a new predicted word. The generation stops when the preset maximum sequence length is reached or the output predicted word is the end symbol.

[0098] In some embodiments, the segmented word with the highest probability may be selected as the target predicted segmented word for output in step 4. Alternatively, in some embodiments, in order to increase the diversity of the output target predicted segmented words, a nucleus sampling decoding algorithm may be used to obtain predicted words.

[0099] For example, a code generation model can be used to generate target program code based on a target text in the following manner: first, a predicted word is determined based on the target text, and then the predicted word is used as the initial target word to cyclically execute the following process: the target word is concatenated into the word segmentation sequence corresponding to the target text to obtain a target word segmentation sequence; based on the target word segmentation sequence, multiple candidate predicted words and the probability of each candidate predicted word in a preset word list are determined; a word is randomly selected from the candidate set corresponding to the candidate predicted word as a new target word, until the length of the target word segmentation sequence reaches a preset length or the target word reaches a preset end symbol, wherein the sum of the probabilities of the candidate predicted words in the candidate set is greater than the preset probability.

[0100] For example, when we get the probability distribution P g ′, first from the probability distribution P g ′ select a candidate set V p , so that the sum of the probabilities of all candidate predicted words in the candidate set is greater than the preset probability p, and then the candidate set V p The probabilities of all candidate predicted words in are normalized, and a word is randomly sampled as the target word. The calculation process is shown in formula (14):

[0101] Here, x represents the predicted word, and the preset probability p can be set according to actual conditions, for example, it can be set to 0.95, which is not limited in the embodiment of the present disclosure.

[0102] After predicting a target word, the target word is concatenated with the word segmentation sequence input into the code generation model to obtain a target word segmentation sequence, which is then input into the code generation model. Steps (3) and (4) are repeated to predict and output a new target word until the length of the target word segmentation sequence reaches a preset length or the output target word is a preset end symbol. The preset length and the preset end symbol can be set according to actual conditions and are not limited in the present embodiment.

[0103] For example, with reference to Figure 6, the code generation method provided by the present disclosure mainly includes three processes: data preprocessing, model training and model application. It should be understood that the specific processing methods of the three processes can refer to the above, and are briefly described below. First, in the data preprocessing process, a program code data set can be obtained, and then the program code data set is denoised, and the vocabulary is trained by the BPE (Byte Pair Encoding) word segmentation algorithm to obtain a vocabulary for subsequent word segmentation. Afterwards, the denoised data is segmented and split into sequence segments of fixed length by the trained vocabulary, and the sequence segments of fixed length are input into the code generation model for training. During the model training process, the code generation model is trained based on the code understanding task and the code generation task. During the model application process, for the program code fragment to be supplemented, word segmentation processing is first performed to obtain a to-be-processed word segmentation sequence, and then the to-be-processed word segmentation sequence is input into the trained code generation model. Afterwards, the final target program code is obtained by the core sampling decoding algorithm.

[0104] Therefore, the code generation model can be trained based on a multi-task learning method of code understanding and code generation. Among them, the code understanding task can use a masked language model to allow the model to learn to recover word information from the masked text, and the masked words are randomly selected, so that the model can learn the basic grammatical knowledge of the program code. On the other hand, the code understanding task can allow the model to learn to recover the masked function names and API names, thereby learning semantic knowledge such as the correspondence between the function name and the function body. In the code generation task, an autoregressive conditional language model can be used for training, that is, the next most likely code text symbol is predicted based on the above information. Therefore, the trained code generation model can output more accurate target program code based on the program code text to be supplemented or the natural language text used to describe the code function.

[0105] Based on the same concept, the present disclosure also provides a code generation device, which can be part or all of an electronic device through software, hardware, or a combination of both. Referring to FIG7 , the code generation device 700 includes:

[0106] An acquisition module 701 is used to acquire a target text, where the target text is a program code text to be supplemented or a natural language text used to describe a code function;

[0107] Generation module 702 is used to input the target text into a code generation model to obtain a target program code generated based on the target text, wherein the code generation model is trained by a code comprehension task and a code generation task, and the code comprehension task is used by the code generation model to learn the grammatical features and semantic features of the sample program code, and the code generation task is used by the code generation model to learn the process of generating new program code based on the sample program code.

[0108] Optionally, the code generation model includes a code comprehension layer, a code generation layer, and a shared representation layer. The result output by the shared representation layer is used to input the code comprehension layer or the code generation layer. The apparatus 700 further includes a training module for:

[0109] When executing the code understanding task, masking is performed on the sample program code to obtain a mask sequence, the mask sequence is input into the code generation model to obtain a first predicted code through the shared representation layer and the code understanding layer, and parameters of the code understanding layer and the shared representation layer are adjusted based on the first predicted code;

[0110] When executing the code generation task, the sample program code is input into the code generation model to obtain a second predicted code through the shared representation layer and the code generation layer, and the parameters of the code generation layer and the shared representation layer are adjusted based on the results output by the code generation model.

[0111] Optionally, the training module is used to:

[0112] Performing word segmentation processing on the sample program code to obtain a word segmentation sequence;

[0113] Randomly selecting a first preset proportion of first target segmented words from the segmented word sequence, and replacing the first target segmented words with a first preset mask symbol to obtain a mask sequence; and / or,

[0114] A second preset proportion of second target participles is randomly selected from the participle sequence, and for each of the second target participles, a target remaining participle is randomly selected from the remaining participles, and the second target participles are replaced with the target remaining participles to obtain a mask sequence, wherein the remaining participles are the participles in the participle sequence except the second target participles.

[0115] Optionally, the training module is used to:

[0116] Performing word segmentation processing on the sample program code to obtain a word segmentation sequence;

[0117] Determining function participles for representing function names in the participle sequence, randomly selecting a third preset proportion of target function participles from the function participles, and replacing the target function participles with a second preset mask symbol to obtain a mask sequence; and / or

[0118] An interface segmentation for representing an interface name is determined in the segmentation sequence, and a fourth preset proportion of target interface segmentations are randomly selected from the interface segmentations, and the target interface segmentations are replaced with a third preset mask symbol to obtain a mask sequence.

[0119] Optionally, the training module is used to:

[0120] Determining a feature vector sequence corresponding to the mask sequence;

[0121] For each feature vector in the feature vector sequence, the shared representation layer calculates based on all feature vectors before and after the feature vector in the feature vector sequence and the attention mechanism to obtain an intermediate feature vector;

[0122] For each of the intermediate feature vectors, performing attention calculation by the code understanding layer based on intermediate feature vectors other than the intermediate feature vector and the attention mechanism to obtain a target feature vector;

[0123] A first prediction program code is obtained according to the target feature vector.

[0124] Optionally, the training module is used to:

[0125] Determining a feature vector sequence corresponding to the sample program code, wherein the feature vector sequence includes N feature vectors, where N is a positive integer;

[0126] For the i-th feature vector in the feature vector sequence, the shared representation layer calculates based on all feature vectors before and after the i-th feature vector in the feature vector sequence and the attention mechanism to obtain an i-th intermediate feature vector, where i is a positive integer;

[0127] For the i-th intermediate feature vector, the code generation layer performs calculation based on the preceding feature vector and the attention mechanism to obtain a target feature vector, wherein the preceding feature vector is all intermediate feature vectors obtained before the i-th intermediate feature vector;

[0128] A second prediction program code is obtained according to the target feature vector.

[0129] Optionally, the apparatus 700 further includes a pre-processing module, configured to:

[0130] Acquire a code dataset, where the code dataset includes a plurality of code files;

[0131] For each of the code files, at least one of the following preprocessing steps is performed to obtain a target code data set: when the ratio of the number of characters in the code file to the total number of symbols is less than a preset character ratio, the code file is deleted from the code data set; when the average number of characters per line of code in the code file is greater than a first preset threshold, the code file is deleted from the code data set; when the number of characters in the comment information in the code file is greater than a second preset threshold, the code file is deleted from the code data set;

[0132] The program code corresponding to each code file in the target code data set is determined as a sample program code.

[0133] Optionally, the code generation model is used to obtain target program code generated based on the target text in the following manner:

[0134] A predicted word is determined based on the target text, and the predicted word is used as the initial target word to loop through the following process:

[0135] The target word is concatenated into a word segmentation sequence corresponding to the target text to obtain a target word segmentation sequence, a plurality of candidate prediction words and a probability of each candidate prediction word in a preset word list are determined based on the target word segmentation sequence, and a word is randomly selected from the candidate set corresponding to the candidate prediction word as a new target word until the length of the target word segmentation sequence reaches a preset length or the target word reaches a preset end symbol;

[0136] The sum of the probabilities of the candidate predicted words in the candidate set is greater than a preset probability.

[0137] Regarding the apparatus in the above embodiment, the specific manner in which each module performs operations has been described in detail in the embodiment of the method, and will not be elaborated here.

[0138] Based on the same concept, the present disclosure also provides a non-transitory computer-readable medium having a computer program stored thereon, which implements the steps of any of the above code generation methods when executed by a processing device.

[0139] Based on the same concept, the present disclosure further provides an electronic device, including:

[0140] a storage device having a computer program stored thereon;

[0141] A processing device is used to execute the computer program in the storage device to implement the steps of any of the above code generation methods.

[0142] Reference is now made to FIG8 , which illustrates a schematic diagram of the structure of an electronic device 800 suitable for implementing embodiments of the present disclosure. Terminal devices in embodiments of the present disclosure may include, but are not limited to, mobile terminals such as mobile phones, laptop computers, digital broadcast receivers, PDAs (personal digital assistants), PADs (tablet computers), PMPs (portable multimedia players), in-vehicle terminals (e.g., in-vehicle navigation terminals), and fixed terminals such as digital TVs and desktop computers. The electronic device illustrated in FIG8 is merely an example and should not limit the functionality or scope of use of embodiments of the present disclosure.

[0143] As shown in Figure 8, the electronic device 800 may include a processing device (e.g., a central processing unit, a graphics processing unit, etc.) 801, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 802 or a program loaded from a storage device 808 into a random access memory (RAM) 803. Various programs and data required for the operation of the electronic device 800 are also stored in the RAM 803. The processing device 801, the ROM 802, and the RAM 803 are connected to each other via a bus 804. An input / output (I / O) interface 805 is also connected to the bus 804.

[0144] Typically, the following devices may be connected to the I / O interface 805: an input device 806 including, for example, a touch screen, a touchpad, a keyboard, a mouse, a camera, a microphone, an accelerometer, a gyroscope, etc.; an output device 807 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; a storage device 808 including, for example, a magnetic tape, a hard disk, etc.; and a communication device 809. The communication device 809 may allow the electronic device 800 to communicate with other devices wirelessly or by wire to exchange data. Although FIG8 shows the electronic device 800 with various devices, it should be understood that it is not required to implement or have all of the devices shown. More or fewer devices may be implemented or have alternatively.

[0145] In particular, according to an embodiment of the present disclosure, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present disclosure includes a computer program product, which includes a computer program carried on a non-transitory computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication device 809, or installed from the storage device 808, or installed from the ROM 802. When the computer program is executed by the processing device 801, the above-mentioned functions defined in the method of the embodiment of the present disclosure are performed.

[0146] It should be noted that the computer-readable medium mentioned above in the present disclosure may be a computer-readable signal medium or a computer-readable storage medium, or any combination of the two. A computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or component, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present disclosure, a computer-readable storage medium may be any tangible medium that contains or stores a program that can be used by or in conjunction with an instruction execution system, device, or component. In the present disclosure, a computer-readable signal medium may include a data signal propagated in baseband or as part of a carrier wave, which carries computer-readable program code. Such a propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. A computer-readable signal medium may also be any computer-readable medium other than a computer-readable storage medium that can transmit, propagate, or transport a program for use by or in conjunction with an instruction execution system, apparatus, or device. The program code contained on the computer-readable medium may be transmitted using any suitable medium, including but not limited to wires, optical cables, RF (radio frequency), etc., or any suitable combination thereof.

[0147] In some embodiments, communication can be performed using any currently known or later developed network protocol, such as HTTP (HyperText Transfer Protocol), and can be interconnected with any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include a local area network ("LAN"), a wide area network ("WAN"), an internet (e.g., the Internet), and a peer-to-peer network (e.g., an ad hoc peer-to-peer network), as well as any currently known or later developed network.

[0148] The computer-readable medium may be included in the electronic device, or may exist independently without being incorporated into the electronic device.

[0149] The computer-readable medium carries one or more programs. When the one or more programs are executed by the electronic device, the electronic device: obtains a target text, which includes a program code text to be supplemented or a natural language text used to describe the code function; inputs the target text into a code generation model to obtain a target program code generated based on the target text, wherein the code generation model is trained by a code comprehension task and a code generation task, and the code comprehension task is used by the code generation model to learn the grammatical features and semantic features of a sample program code, and the code generation task is used by the code generation model to learn the process of generating a new program code based on the sample program code.

[0150] Computer program code for performing the operations of the present disclosure may be written in one or more programming languages, or a combination thereof, including, but not limited to, object-oriented programming languages ​​such as Java, Smalltalk, C++, and conventional procedural programming languages ​​such as "C" or similar programming languages. The program code may be executed entirely on the user's computer, partially on the user's computer, as a stand-alone software package, partially on the user's computer and partially on a remote computer, or entirely on the remote computer or server. In cases involving a remote computer, the remote computer may be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., through the Internet using an Internet service provider).

[0151] The flowcharts and block diagrams in the accompanying drawings illustrate the possible implementation architecture, functions and operations of the systems, methods and computer program products according to various embodiments of the present disclosure. In this regard, each box in the flowchart or block diagram can represent a module, program segment, or a part of code, and the module, program segment, or a part of code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order than that marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram and / or flowchart, and the combination of the boxes in the block diagram and / or flowchart, can be implemented with a dedicated hardware-based system that performs the specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.

[0152] The modules described in the embodiments of the present disclosure may be implemented in software or hardware, wherein the name of a module does not necessarily limit the module itself.

[0153] The functions described above herein may be performed, at least in part, by one or more hardware logic components. For example, and without limitation, exemplary types of hardware logic components that may be used include: field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chip (SOCs), complex programmable logic devices (CPLDs), and the like.

[0154] In the context of the present disclosure, a machine-readable medium can be a tangible medium that can contain or store a program for use by or in conjunction with an instruction execution system, device or equipment. A machine-readable medium can be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium can include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or equipment, or any suitable combination of the foregoing. A more specific example of a machine-readable storage medium can include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.

[0155] The above description is merely a preferred embodiment of the present disclosure and an illustration of the technical principles employed. Those skilled in the art should understand that the scope of disclosure involved in the present disclosure is not limited to the technical solutions formed by the specific combination of the above-mentioned technical features, but also includes other technical solutions formed by any combination of the above-mentioned technical features or their equivalents without departing from the above-mentioned disclosed concepts. For example, a technical solution formed by replacing the above-mentioned features with (but not limited to) technical features with similar functions disclosed in this disclosure.

[0156] In addition, although each operation is described in a specific order, this should not be understood as requiring these operations to be performed in the specific order shown or in a sequential order. Under certain circumstances, multitasking and parallel processing may be advantageous. Similarly, although some specific implementation details have been included in the above discussion, these should not be interpreted as limiting the scope of the present disclosure. Some features described in the context of a separate embodiment can also be implemented in a single embodiment in combination. On the contrary, the various features described in the context of a single embodiment can also be implemented in multiple embodiments individually or in any suitable sub-combination mode.

[0157] Although the subject matter has been described using language specific to structural features and / or methodological logical acts, it should be understood that the subject matter defined in the appended claims is not necessarily limited to the specific features or acts described above. Rather, the specific features and acts described above are merely example forms of implementing the claims. Regarding the apparatus in the above-described embodiments, the specific manner in which each module performs operations has been described in detail in the embodiments related to the method and will not be elaborated upon here.

Claims

1. A code generation method, characterized in that: The method comprises: Acquire a target text, wherein the target text includes a program code text to be supplemented or a natural language text for describing a code function; The target text is input into a code generation model to obtain a target program code generated based on the target text, wherein the code generation model is trained by a code comprehension task and a code generation task, and the code comprehension task is used for the code generation model to learn the grammatical features and semantic features of a sample program code, and the code generation task is used for the code generation model to learn the process of generating a new program code based on the sample program code.

2. The method according to claim 1, characterized in that The code generation model includes a code understanding layer, a code generation layer and a shared representation layer. The result output by the shared representation layer is used to input the code understanding layer or the code generation layer. The training steps of the code generation model include: When executing the code understanding task, masking is performed on the sample program code to obtain a mask sequence, the mask sequence is input into the code generation model to obtain a first prediction code through the shared representation layer and the code understanding layer, and parameters of the code understanding layer and the shared representation layer are adjusted based on the first prediction code; When executing the code generation task, the sample program code is input into the code generation model to obtain a second predicted code through the shared representation layer and the code generation layer, and the parameters of the code generation layer and the shared representation layer are adjusted based on the result output by the code generation model.

3. The method according to claim 2, characterized in that The performing mask processing on the sample program code to obtain a mask sequence includes: Performing word segmentation processing on the sample program code to obtain a word segmentation sequence; Randomly selecting a first preset proportion of first target segmented words in the segmented word sequence, and replacing the first target segmented words with a first preset mask symbol to obtain a mask sequence; and / or, A second preset proportion of second target participles is randomly selected from the participle sequence, and for each of the second target participles, a target remaining participle is randomly selected from the remaining participles, and the second target participles are replaced with the target remaining participles to obtain a mask sequence, wherein the remaining participles are the participles in the participle sequence except the second target participles.

4. The method according to claim 2, characterized in that: The performing mask processing on the sample program code to obtain a mask sequence includes: Performing word segmentation processing on the sample program code to obtain a word segmentation sequence; Determining function participles used to represent function names in the participle sequence, and randomly selecting a third preset proportion of target function participles in the function participles, replacing the target function participles with second preset mask symbols, to obtain a mask sequence; and / or An interface segmentation for representing an interface name is determined in the segmentation sequence, and a fourth preset proportion of target interface segmentations are randomly selected from the interface segmentations, and the target interface segmentations are replaced with a third preset mask symbol to obtain a mask sequence.

5. The method according to any one of claims 2 to 4, characterized in that: The step of inputting the mask sequence into the code generation model to obtain a first predicted code through the shared representation layer and the code understanding layer comprises: Determine a feature vector sequence corresponding to the mask sequence; For each feature vector in the feature vector sequence, the shared representation layer calculates according to all feature vectors before and after the feature vector in the feature vector sequence and the attention mechanism to obtain an intermediate feature vector; For each of the intermediate feature vectors, the code understanding layer performs attention calculation according to the intermediate feature vectors other than the intermediate feature vector and the attention mechanism to obtain a target feature vector; A first prediction program code is obtained according to the target feature vector.

6. The method according to any one of claims 2 to 4, characterized in that: The step of inputting the sample program code into the code generation model to obtain a second prediction code through the shared representation layer and the code generation layer comprises: Determine a feature vector sequence corresponding to the sample program code, wherein the feature vector sequence includes N feature vectors, where N is a positive integer; For the i-th feature vector in the feature vector sequence, the shared representation layer calculates according to all feature vectors before and after the i-th feature vector in the feature vector sequence and the attention mechanism to obtain an i-th intermediate feature vector, where i is a positive integer; For the ith intermediate feature vector, the code generation layer calculates the target feature vector according to the preceding feature vector and the attention mechanism, wherein the preceding feature vector is the target feature vector between the ith intermediate feature vector and the code generation layer. All the intermediate eigenvectors obtained before; A second prediction program code is obtained according to the target feature vector.

7. The method according to any one of claims 1 to 4, characterized in that: The sample program code is obtained in the following manner: Acquire a code data set, wherein the code data set includes a plurality of code files; For each of the code files, at least one of the following preprocessing steps is performed to obtain a training data set: when the ratio of the number of characters in the code file to the total number of symbols is greater than or equal to a preset character ratio, the code file is added to the training data set; when the average number of characters per line of code in the code file is less than or equal to a first preset threshold, the code file is added to the training data set; when the number of characters in the comment information in the code file is less than or equal to a second preset threshold, the code file is added to the training data set; The program code corresponding to each of the code files in the training data set is determined as a sample program code.

8. The method according to any one of claims 1 to 4, characterized in that: The code generation model is used to obtain the target program code generated based on the target text in the following manner: A predicted word is determined based on the target text, and the predicted word is used as the initial target word to loop through the following process: The target word is concatenated into the word segmentation sequence corresponding to the target text to obtain a target word segmentation sequence, a plurality of candidate prediction words and the probability of each candidate prediction word in a preset word list are determined based on the target word segmentation sequence, and a word is randomly selected from the candidate set corresponding to the candidate prediction word as a new target word until the length of the target word segmentation sequence reaches a preset length or the target word is a preset end symbol; Wherein, the sum of the probabilities of the candidate predicted words in the candidate set is greater than a preset probability.

9. A code generating device, characterized in that: The device comprises: An acquisition module, used for acquiring a target text, wherein the target text is a program code text to be supplemented or a natural language text for describing a code function; A generation module is used to input the target text into a code generation model to obtain a target program code generated based on the target text, wherein the code generation model is trained by a code understanding task and a code generation task, and the code understanding task is used for the code generation model to learn the grammatical features and semantic features of the sample program code. The code generation task is used for the code generation model to learn to generate new program code based on the sample program code.

10. A non-transitory computer-readable medium having a computer program stored thereon, characterized in that: When the program is executed by a processing device, the steps of the method described in any one of claims 1 to 8 are implemented.

11. An electronic device, characterized in that: include: a storage device having a computer program stored thereon; A processing device, configured to execute the computer program in the storage device to implement the steps of the method according to any one of claims 1 to 8.

Citation Information

Patent Citations

  • Code generation method and device based on deep learning

    CN110489110A

  • Code generation method based on abstract syntax tree structure information enhancement

    CN114385178A

  • Code generation method and system based on natural semantic understanding

    CN115202640A

  • Code generation method and device, storage medium and electronic equipment

    CN116166271A

  • Code refactor renaming recommender

    US20220188102A1

Cited By

  • Code generation method and device, electronic equipment and storage medium

    CN118963739A

  • Equipment test model code generation method based on large model

    CN119248238A

  • Code annotation generation method and device based on language processing, computer equipment and storage medium

    CN119292657A

  • Code processing method and device based on large model, electronic equipment and storage medium

    CN119597264A

  • Training method of code generation model, code generation method, equipment and medium

    CN119902749A