WO2024017287A1 - Model training method and apparatus therefor - Google Patents
Model training method and apparatus therefor Download PDFInfo
- Publication number
- WO2024017287A1 WO2024017287A1 PCT/CN2023/108112 CN2023108112W WO2024017287A1 WO 2024017287 A1 WO2024017287 A1 WO 2024017287A1 CN 2023108112 W CN2023108112 W CN 2023108112W WO 2024017287 A1 WO2024017287 A1 WO 2024017287A1
- Authority
- WO
- WIPO (PCT)
- Prior art keywords
- text
- code
- predicted
- natural language
- training
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Ceased
Links
Classifications
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F40/00—Handling natural language data
- G06F40/10—Text processing
- G06F40/166—Editing, e.g. inserting or deleting
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F40/00—Handling natural language data
- G06F40/20—Natural language analysis
- G06F40/274—Converting codes to words; Guess-ahead of partial word inputs
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F40/00—Handling natural language data
- G06F40/20—Natural language analysis
- G06F40/279—Recognition of textual entities
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06N—COMPUTING ARRANGEMENTS BASED ON SPECIFIC COMPUTATIONAL MODELS
- G06N20/00—Machine learning
Definitions
- the present application relates to the field of artificial intelligence, and in particular, to a model training method and its device.
- a language model refers to a model that can predict unknown words in a sentence based on a part of given semantic fragments. For example: given a natural language sequence fragment "Huawei __ is very good.”, the language model can generate unknown words based on the fragment. For example, in this example, the language model can generate the word "mobile phone” based on the given fragment, and then obtain a sentence For "Huawei mobile phones are very good.”.
- Language model pre-training refers to training the corresponding language model through massive language sequence corpus, so that the language model has the ability to predict the occurrence probability of a certain language unit at a certain position.
- Program synthesis refers to using a certain software program to automatically synthesize a specified program language sequence with specified functions or structures. In short, it is a program that generates a program. Generally speaking, it is divided into two categories: program synthesis (code generation) and code completion. kind. Program synthesis, that is, starting from scratch, can generate corresponding code according to a certain natural language description or a preset function description. Code completion, that is, based on the existing code sequence, continue to generate the following code sequence.
- the existing technology follows the generation method of natural language processing and trains based on the original code. Currently, this method can continue to write the code based on the existing context. However, for the scenario of function-level code generation, the quality of the code generation has yet to be improved. promote.
- This application provides a model training method that can reduce the difficulty of training and improve the quality of code generation.
- this application provides a model training method, including: obtaining a first text and a second text, where the first text and the second text are extracted from the original code text; wherein, the The first text is the program code, and the second text is the attribute description corresponding to the program code; the first text and the second text include known text and predicted text; according to the known text, through natural A language model predicts the text corresponding to the text position of the predicted text to obtain a text prediction result; the predicted text and the text prediction result are used to update the natural language model.
- the attribute description includes a function description of the program code or an implementation principle of the program code (or it is also called an explanation of the program code).
- the original code text can be existing programming language corpus and natural language corpus.
- existing programming language corpus and natural language corpus can be obtained through the Internet (or other methods).
- the corpus includes code files of mixed programming language and natural language, code files of programming language, and natural language files.
- the collected natural language can be any human language, and the programming language can be any programming language, which is not limited by this application.
- the first text and the second text are extracted from the original code text; wherein the first text is the program code, and the second text is the program code.
- the second text can be in natural language, and the second text can be in any language used for human communication, and the program code can be in any programming language, which is not limited by this application.
- a correspondence relationship between the first text and the second text can be established to form a "natural language description-function implementation code" (Description-Function code) ) sentence pair form sample.
- “natural language description-function implementation code” “Description-Function code”
- the text and the second text are a pair of samples.
- the code text and corresponding attribute description are extracted from the original program code and used as a pair of samples.
- the original program file is directly used as a training sample. , helping the natural language model to establish an association between the code text and the corresponding attribute description during training, without having to learn the ability to identify this association from samples during the training process, reducing the training difficulty and improving the prediction of the model Accuracy.
- function-level code snippets (such as code programs) can be removed based on the statistical characteristics of the code corpus, and the corpus with too short natural language description part and the excessive code part in the code corpus can be removed. Long corpus or corpus with a large length difference between the two to improve the quality of the training corpus.
- the attribute description includes a function description of the program code or an implementation principle of the program code.
- the first text is identified from the original code text through a program code identification method
- the second text is identified from the original code text through an attribute description identification method.
- the function code segment in the original code text can be identified as the first text: for example, the original code text can be identified using certain rules (for example, a program code identification method such as a code abstract syntax tree (AST) can be used) function code snippet in .
- AST code abstract syntax tree
- certain rules can be used, such as annotation rules, language character rules and other attribute description recognition methods for identification and extraction.
- AST abstract syntax tree
- natural language processing automatic identification methods can be used to extract code segments, such as sequence annotation methods, and a function annotator is trained to annotate natural language descriptions corresponding to all function-level codes.
- the program code is the complete code of one or more functions.
- some text units in the training sample can be used as known text, and the other part of the text As the text to be predicted, the text at the text position where the text to be predicted is predicted based on the known text.
- the text prediction process can be performed with different training objectives for the first text and the second text.
- one of the four training objectives of CLM, MLM, MCLM and NoLM can be used to perform the text prediction process.
- one of the three training objectives of CLM, MLM and MCLM can be used to perform the model training process.
- the text position corresponding to the predicted text when predicting the text in the first text, may be determined from the first text in a first manner; When predicting text in two texts, the text position corresponding to the predicted text can be determined from the second text through a second method; the first method and the second method are different.
- the first method and the second method are respectively one of the following prediction methods:
- the known text is all the texts that have been predicted by the natural language model (that is, CLM);
- the following text position of the known text is used as the text position corresponding to the predicted text; wherein the known text is the unmasked text among all the texts that have been predicted by the natural language model (that is, MCLM ).
- the text position corresponding to the predicted text can be determined through one of the following prediction methods:
- the known text is all the texts that have been predicted by the natural language model (that is, CLM);
- the following text position of the known text is used as the text position corresponding to the predicted text; wherein the known text is the unmasked text among all the texts that have been predicted by the natural language model (that is, MCLM ).
- the natural language model can predict texts sequentially along the preset context prediction direction.
- all words that have been predicted by the natural language model can be used as known text, and have been The known text is used as the context of the position text to predict the text location of the text.
- prediction can be made in the order from the context to the context (that is, from the beginning of the text to the end of the text), each time It is possible to predict a text unit and continue to predict based on the already predicted text unit in the next prediction.
- the text units in the text can be masked (for example, they can be randomly masked), and the masked text units can be used as predicted texts.
- the masked text unit can be used as known text, and the natural language model can sequentially calculate the text location of the masked text unit based on the unmasked text unit (or the predicted masked text unit). Text prediction.
- the natural language model can predict text sequentially along the preset context prediction direction.
- CLM all words that have been predicted by the natural language model can be used as Known text, and the known text is used as the upper text of the position text to predict the text position of the predicted text.
- MCLM the words that have been predicted by the natural language model in MCLM can be masked, and natural language can be used The unmasked text units in the words that have been predicted by the model are used to predict the text location of the predicted text.
- the second text may not be predicted, but the second text may be directly used as the known text, and the first text may be used as a training sample to predict text units in the first text.
- the four optional training objectives can be applied to the natural language description or function code of the "Description-Function pair" (Description-Function) respectively, and the combination 12 types of training objectives are formed.
- function-level code snippets (such as code programs) can be removed based on the statistical characteristics of the code corpus, and the corpus with too short natural language description part and the excessive code part in the code corpus can be removed. Long corpus or corpus with a large length difference between the two to improve the quality of the training corpus.
- training samples are usually difficult to obtain, that is, the number of training samples (text pairs composed of the first text and the second text) is small, data enhancement can be performed on the obtained training samples to obtain More training samples.
- the first text is obtained based on the first original text extracted from the original code text; wherein the first text is a partial text unit in the first original text. Obtained by modification.
- the modification includes: deletion, addition or replacement.
- the replacement is specifically a replacement with the same semantics.
- synonymous segments may be generated based on back translation technology, or synonymous segments may be generated based on synonymous sentence generation technology.
- the second text is obtained based on the second original text extracted from the original code text; wherein the second text is a partial text unit in the second original text. Obtained by modification.
- the modification includes: deletion, addition or replacement.
- the replacement is specifically a replacement with the same semantics.
- synonymous fragments can be generated based on back translation technology, Alternatively, synonymous fragments may be generated based on synonymous sentence generation technology.
- High-quality automatically generated training data ultimately improves model performance.
- training samples can be processed differently at different stages of model training.
- the first text and the second text obtained in the above way can be used, and in the model training In the fine-tuning stage, text adapted to the application field of the program code (also including program code and corresponding code attribute descriptions) can be extracted from the original code text as a training sample.
- the first text or the second text is text extracted from the original code text and adapted to the application field of the program code.
- the adaptation to the application field of the program code includes: text whose length or the length of a single line of text is less than a threshold; wherein the threshold is related to the application field of the program code.
- information in other dimensions besides length can also be used to quantify and apply the program code.
- the samples may be filtered based on their similarity to data samples from the application domain of the function-level code generator.
- the similarity calculation method can be the length relationship between natural language descriptions, the length relationship between program language function codes, or other relationships.
- this application provides a model training device, including:
- An acquisition module is used to acquire a first text and a second text.
- the first text and the second text are extracted from the original code text; wherein the first text is program code, and the second text is extracted from the original code text.
- the text is an attribute description corresponding to the program code; the first text and the second text include known text and predicted text;
- a prediction module configured to predict the text corresponding to the text position of the predicted text through a natural language model based on the known text, and obtain a text prediction result; the predicted text and the text prediction result are used to update the Natural language model.
- the attribute description includes a function description of the program code or an implementation principle of the program code.
- the device further includes:
- a text position determination module configured to determine the text position corresponding to the predicted text from the first text in a first manner when predicting the text in the first text
- the text position corresponding to the predicted text is determined from the second text through the second method; the first method and the second method are different.
- the first method and the second method are respectively one of the following prediction methods:
- the following text position of the known text is used as the text position corresponding to the predicted text; wherein the known text is the unmasked text among all the texts that have been predicted by the natural language model.
- the text position determination module is also configured to determine the text position corresponding to the predicted text through one of the following prediction methods without predicting the text in the second text:
- the following text position of the known text is used as the text position corresponding to the predicted text; wherein the known text is the unmasked text among all the texts that have been predicted by the natural language model.
- the first text is obtained based on the first original text extracted from the original code text; wherein the first text is obtained by modifying some text units in the first original text; or,
- the second text is obtained based on the second original text extracted from the original code text; wherein the second text is obtained by modifying some text units in the second original text.
- the modification includes: deletion, addition or replacement.
- the replacement is specifically a replacement with the same semantics.
- the first text or the second text is text extracted from the original code text and adapted to the application field of the program code.
- the application fields adapted to the program code include:
- the text length or the length of a single line of text is less than a threshold; wherein the threshold is related to the application field of the program code.
- a model training device which may include a memory, a processor, and a bus system.
- the memory is used to store programs
- the processor is used to execute programs in the memory to perform the above-mentioned first aspect. and any optional methods.
- embodiments of the present application provide a computer-readable storage medium that stores a computer program that, when run on a computer, causes the computer to execute the first aspect and any one of the above-mentioned aspects.
- Optional method
- embodiments of the present application provide a computer program that, when run on a computer, causes the computer to execute the above-mentioned first aspect and any optional method thereof.
- the present application provides a chip system, which includes a processor to support an execution device or a training device to implement the functions involved in the above aspects, for example, sending or processing data involved in the above methods; Or, information.
- the chip system further includes a memory, and the memory is used to store necessary program instructions and data for executing the device or training the device.
- the chip system may be composed of chips, or may include chips and other discrete devices.
- Figure 1A is a schematic structural diagram of the main framework of artificial intelligence
- FIGS. 1B and 1C are schematic diagrams of the application system framework of the present invention.
- Figure 1D is a schematic diagram of an optional hardware structure of the terminal
- Figure 2 is a schematic structural diagram of a server
- FIGS. 3 to 5 are schematic diagrams of a system architecture of this application.
- Figure 6 shows the process of a cloud service
- Figure 7 shows the process of a cloud service
- Figure 8 is a schematic diagram of a system architecture of this application.
- Figure 9 is a schematic flowchart of a model training method provided by an embodiment of the present application.
- Figures 10A to 10D are diagrams of the text
- Figure 11 is a schematic diagram of the architecture of a model training method provided by an embodiment of the present application.
- Figures 12 and 13 are a schematic of the model
- Figure 14 is a schematic structural diagram of a model training device provided by an embodiment of the present application.
- Figure 15 is a schematic structural diagram of an execution device provided by an embodiment of the present application.
- Figure 16 is a schematic structural diagram of the training equipment provided by the embodiment of the present application.
- Figure 17 is a schematic structural diagram of a chip provided by an embodiment of the present application.
- the terms “substantially”, “about” and similar terms are used as terms of approximation, not as terms of degree, and are intended to take into account measurements or values that would be known to one of ordinary skill in the art. The inherent bias in calculated values.
- the use of “may” when describing embodiments of the present invention refers to “one or more possible embodiments.”
- the terms “use”, “using”, and “used” may be deemed to be the same as the terms “utilize”, “utilizing”, and “utilize”, respectively. Synonymous with “utilized”.
- the term “exemplary” is intended to refer to an example or illustration.
- Figure 1A shows a structural schematic diagram of the artificial intelligence main framework.
- the following is from the “intelligent information chain” (horizontal axis) and “IT value chain” ( The above artificial intelligence theme framework is elaborated on the two dimensions of vertical axis).
- the "intelligent information chain” reflects a series of processes from data acquisition to processing. For example, it can be the general process of intelligent information perception, intelligent information representation and formation, intelligent reasoning, intelligent decision-making, intelligent execution and output. In this process, the data has gone through the condensation process of "data-information-knowledge-wisdom".
- the "IT value chain” reflects the value that artificial intelligence brings to the information technology industry, from the underlying infrastructure of human intelligence and information (providing and processing technology implementation) to the systematic industrial ecological process.
- Infrastructure provides computing power support for artificial intelligence systems, enables communication with the external world, and supports it through basic platforms.
- computing power is provided by smart chips (hardware acceleration chips such as CPU, NPU, GPU, ASIC, FPGA, etc.);
- the basic platform includes distributed computing framework and network and other related platform guarantees and support, which can include cloud storage and Computing, interconnection networks, etc.
- sensors communicate with the outside world to obtain data, which are provided to smart chips in the distributed computing system provided by the basic platform for calculation.
- Data from the upper layer of the infrastructure is used to represent data sources in the field of artificial intelligence.
- the data involves graphics, images, voice, and text, as well as IoT data of traditional devices, including business data of existing systems and sensory data such as force, displacement, liquid level, temperature, and humidity.
- Data processing usually includes data training, machine learning, deep learning, search, reasoning, decision-making and other methods.
- machine learning and deep learning can perform symbolic and formal intelligent information modeling, extraction, preprocessing, training, etc. on data.
- Reasoning refers to the process of simulating human intelligent reasoning in computers or intelligent systems, using formal information to perform machine thinking and problem solving based on reasoning control strategies. Typical functions are search and matching.
- Decision-making refers to the process of making decisions after intelligent information is reasoned, and usually provides functions such as classification, sorting, and prediction.
- some general capabilities can be formed based on the results of further data processing, such as algorithms or a general system, such as translation, text analysis, computer vision processing, speech recognition, and image processing. identification, etc.
- Intelligent products and industry applications refer to the products and applications of artificial intelligence systems in various fields. They are the encapsulation of overall artificial intelligence solutions, productizing intelligent information decision-making and realizing practical applications. Its application fields mainly include: intelligent terminals, intelligent transportation, Smart healthcare, autonomous driving, smart cities, etc.
- This application can be applied to the field of natural language processing in the field of artificial intelligence. Taking natural language processing as an example, multiple application scenarios that have been implemented into products will be introduced below.
- This application can be, but is not limited to, applied to applications that include program synthesis functions or code completion functions based on code attribute descriptions (hereinafter may be referred to as program synthesis applications for short) or cloud-side servers.
- program synthesis applications for short
- cloud-side servers The cloud services provided are introduced below:
- the product form of the embodiment of the present application may be a program synthesis application program.
- Program synthesis applications can run on terminal devices or cloud-side servers.
- the program synthesis application can implement program synthesis or code completion tasks based on code attribute descriptions (or prompts), where the program synthesis application can respond to input
- the code attribute description (or can be called a prompt) is used to perform the task of program synthesis or code completion, and the predicted text (that is, the generated code program) is obtained.
- the generated code program can conform to the code attribute description.
- the user can open a program synthesis application installed on the terminal device and enter an attribute description of the code (or it can be called a prompt).
- the program synthesis application can use the program provided by the embodiment of the present application.
- the natural language model trained by the method performs program synthesis or code completion on the attribute description of the code, and presents the predicted text (that is, the generated code program) to the user (the presentation method can be, but is not limited to, displaying, saving, or uploading to the cloud side, etc.).
- the user can open a program synthesis application installed on the terminal device and enter the attribute description of the code.
- the program synthesis application can send the attribute description of the code to the server on the cloud side.
- the server performs program synthesis or code completion on the attribute description of the code using the natural language model trained by the method provided in the embodiments of this application, and sends the predicted text (that is, the generated code program) back to the terminal device, and the terminal device can
- the predicted text that is, the generated code program
- the presentation method can be but is not limited to display, saving, uploading to the cloud, etc.).
- Figure 1B is a schematic diagram of the functional architecture of a program synthesis application in an embodiment of the present application:
- the program synthesis application 102 can receive input parameters 101 (for example, attribute descriptions containing codes) and generate predictive text (that is, a generated code program) 103 .
- Program synthesis application 102 may be executed, for example, on at least one computer system and may include computer code that, when executed by one or more computers, causes the computers to perform operations performed by the present application.
- the natural language model trained by the method provided in the example is not limited to, attribute descriptions containing codes.
- Figure 1C is a schematic diagram of the entity architecture of running a program synthesis application in an embodiment of the present application:
- FIG. 1C shows a schematic diagram of a system architecture.
- the system may include a terminal 100 and a server 200.
- the server 200 may include one or more servers (one server is used as an example for illustration in FIG. 1C), and the server 200 may provide program synthesis function services for one or more terminals.
- the terminal 100 can be installed with a program synthesis application, or open a web page related to the program synthesis function.
- the above-mentioned application program and web page can provide an interface, and the terminal 100 can receive relevant parameters input by the user on the program synthesis function interface.
- the above parameters are sent to the server 200.
- the server 200 can obtain the processing results based on the received parameters and return the processing results to the terminal 100.
- the terminal 100 can also complete the action of obtaining the processing result based on the received parameters by itself without requiring the cooperation of the server, which is not limited by the embodiments of this application.
- the terminal 100 in the embodiment of the present application can be a mobile phone, a tablet computer, a wearable device, a vehicle-mounted device, an augmented reality (AR)/virtual reality (VR) device, a notebook computer, or an ultra mobile personal computer (ultra mobile personal computer).
- - mobile personal computer UMPC
- netbook personal digital assistant
- PDA personal digital assistant
- FIG. 1D shows an optional hardware structure diagram of the terminal 100.
- the terminal 100 may include a radio frequency unit 110, a memory 120, an input unit 130, a display unit 140, a camera 150 (optional), an audio circuit 160 (optional), a speaker 161 (optional), Microphone 162 (optional), processor 170, external interface 180, power supply 190 and other components.
- a radio frequency unit 110 may include a radio frequency unit 110, a memory 120, an input unit 130, a display unit 140, a camera 150 (optional), an audio circuit 160 (optional), a speaker 161 (optional), Microphone 162 (optional), processor 170, external interface 180, power supply 190 and other components.
- Figure 1D is only an example of a terminal or a multi-function device, and does not constitute a limitation to the terminal or multi-function device. It may include more or fewer components than shown in the figure, or combine certain components. Or different parts.
- the input unit 130 may be used to receive input numeric or character information and generate key signal input related to user settings and function control of the portable multi-function device.
- the input unit 130 may include a touch screen 131 (optional) and/or other input devices 132.
- the touch screen 131 can collect the user's touch operations on or near it (such as the user's operations on or near the touch screen using fingers, knuckles, stylus, or any other suitable objects), and drive the corresponding according to the preset program. Connect the device.
- the touch screen can detect the user's touch action on the touch screen, convert the touch action into a touch signal and send it to the processor 170, and can receive and execute commands from the processor 170; the touch signal at least includes contact point coordinate information.
- the touch screen 131 can provide an input interface and an output interface between the terminal 100 and the user.
- touch screens can be implemented in various types such as resistive, capacitive, infrared, and surface acoustic wave.
- the input unit 130 may also include other input devices.
- other input devices 132 may include, but are not limited to, one or more of a physical keyboard, function keys (such as volume control keys 132, switch keys 133, etc.), trackball, mouse, joystick, etc.
- the input device 132 may receive an attribute description of the input code and the like.
- the display unit 140 may be used to display information input by the user or information provided to the user, various menus of the terminal 100, interactive interfaces, file display, and/or playback of any kind of multimedia files.
- the display unit 140 may be used to display the interface of a program synthesis application, the generated predictive text (that is, the generated code program), etc.
- the memory 120 can be used to store instructions and data.
- the memory 120 can mainly include a storage instruction area and a storage data area.
- the storage data area can store various data, such as multimedia files, text, etc.;
- the storage instruction area can store operating systems, applications, at least Software units such as instructions required for a function, or their subsets or extensions.
- Non-volatile random access memory may also be included; providing the processor 170 with management of hardware, software and data resources in the computing processing device and supporting control software and applications. It is also used for storage of multimedia files, as well as storage of running programs and applications.
- the processor 170 is the control center of the terminal 100. It uses various interfaces and lines to connect various parts of the entire terminal 100, and executes various functions of the terminal 100 by running or executing instructions stored in the memory 120 and calling data stored in the memory 120. functions and process data to provide overall control of the terminal device.
- the processor 170 may include one or more processing units; preferably, the processor 170 may integrate an application processor and a modem processor, where the application processor mainly processes operating systems, user interfaces, application programs, etc. , the modem processor mainly handles wireless communications. It can be understood that the above-mentioned modem processor may not be integrated into the processor 170 .
- the processor and memory can be implemented on a single chip, and in some embodiments, they can also be implemented on separate chips.
- the processor 170 can also be used to generate corresponding operation control signals, send them to corresponding components of the computing processing device, read and process data in the software, especially read and process the data and programs in the memory 120, so that the Each functional module performs a corresponding function, thereby controlling the corresponding components to act according to the instructions.
- the memory 120 can be used to store software codes related to the data processing method, and the processor 170 can execute the steps of the data processing method of the chip, and can also schedule other units (such as the above-mentioned input unit 130 and the display unit 140) to implement corresponding functions. .
- the radio frequency unit 110 (optional) can be used to send and receive information or receive and send signals during calls. For example, after receiving downlink information from the base station, it is processed by the processor 170; in addition, the designed uplink data is sent to the base station.
- RF circuits include but are not limited to antennas, at least one amplifier, transceivers, couplers, low noise amplifiers (LNA), duplexers, etc.
- the radio frequency unit 110 can also communicate with network devices and other devices through wireless communication.
- the wireless communication can use any communication standard or protocol, including but not limited to Global System of Mobile communication (GSM), General Packet Radio Service (GPRS), Code Division Multiple Access (Code Division) Multiple Access (CDMA), Wideband Code Division Multiple Access (WCDMA), Long Term Evolution (LTE), email, Short Messaging Service (SMS), etc.
- GSM Global System of Mobile communication
- GPRS General Packet Radio Service
- CDMA Code Division Multiple Access
- WCDMA Wideband Code Division Multiple Access
- LTE Long Term Evolution
- SMS Short Messaging Service
- the radio frequency unit 110 can send the attribute description of the code to the server 200, and receive the predicted text (that is, the generated code program) sent by the server 200.
- radio frequency unit 110 is optional and can be replaced by other communication interfaces, such as a network port.
- the terminal 100 also includes a power supply 190 (such as a battery) that supplies power to various components.
- a power supply 190 such as a battery
- the power supply can be logically connected to the processor 170 through a power management system, so that functions such as charging, discharging, and power consumption management can be implemented through the power management system.
- the terminal 100 also includes an external interface 180, which can be a standard Micro USB interface or a multi-pin connector, which can be used to connect the terminal 100 to communicate with other devices, or can be used to connect a charger to charge the terminal 100. .
- an external interface 180 can be a standard Micro USB interface or a multi-pin connector, which can be used to connect the terminal 100 to communicate with other devices, or can be used to connect a charger to charge the terminal 100.
- the terminal 100 may also include a flash light, a wireless fidelity (WiFi) module, a Bluetooth module, sensors with different functions, etc., which will not be described again here. Some or all of the methods described below may be applied in the terminal 100 as shown in FIG. 1D.
- WiFi wireless fidelity
- Bluetooth Bluetooth
- FIG. 2 provides a schematic structural diagram of a server 200.
- the server 200 includes a bus 201, a processor 202, a communication interface 203 and a memory 204.
- the processor 202, the memory 204 and the communication interface 203 communicate through the bus 201.
- the bus 201 may be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus, etc.
- PCI peripheral component interconnect
- EISA extended industry standard architecture
- the bus can be divided into address bus, data bus, control bus, etc. For ease of presentation, only one thick line is used in Figure 2, but it does not mean that there is only one bus or one type of bus.
- the processor 202 may be a central processing unit (CPU), a graphics processing unit (GPU), a microprocessor (MP) or a digital signal processor (DSP). any one or more of them.
- CPU central processing unit
- GPU graphics processing unit
- MP microprocessor
- DSP digital signal processor
- Memory 204 may include volatile memory, such as random access memory (RAM).
- RAM random access memory
- the memory 204 may also include non-volatile memory (non-volatile memory), such as read-only memory (ROM), flash memory, mechanical hard drive (hard drive drive, HDD) or solid state drive (solid state drive). ,SSD).
- ROM read-only memory
- HDD hard drive drive
- SSD solid state drive
- the memory 204 can be used to store software codes related to the data processing method, and the processor 202 can execute the steps of the data processing method of the chip, and can also schedule other units to implement corresponding functions.
- the terminal 100 and the server 200 may be centralized or distributed devices, and the processors (such as the processor 170 and the processor 202) in the terminal 100 and the server 200 may be hardware circuits (such as application specific integrated circuits) application specific integrated circuit (ASIC), field-programmable gate array (FPGA), general-purpose processor, digital signal processing (DSP), microprocessor or microcontroller, etc.), Or a combination of these hardware circuits.
- the processor can be a hardware system with the function of executing instructions, such as CPU, DSP, etc., or a hardware system without the function of executing instructions, such as ASIC, FPGA, etc., or the above-mentioned processor without the function of executing instructions.
- the steps related to the model reasoning process in the embodiments of this application involve AI-related operations.
- the instruction execution architecture of the terminal device and server is not limited to the processor-memory architecture introduced above.
- the system architecture provided by the embodiment of the present application will be introduced in detail below with reference to Figure 5 .
- FIG. 5 is a schematic diagram of the system architecture provided by the embodiment of the present application.
- the system architecture 500 includes an execution device 510, a training device 520, a database 530, a client device 540, a data storage system 550 and a data collection system 560.
- the execution device 510 includes a computing module 511, an I/O interface 512, a preprocessing module 513 and a preprocessing module 514.
- the target model/rule 501 may be included in the calculation module 511, and the preprocessing module 513 and the preprocessing module 514 are optional.
- the execution device 510 may be the above-mentioned terminal device or server that runs the program synthesis application.
- Data collection device 560 is used to collect training samples.
- the training sample can be a program file (including program code and attribute description of the program code), etc. After collecting the training samples, the data collection device 560 stores the training samples into the database 530 .
- the training device 520 can maintain the training samples in the database 530 and the neural network to be trained (such as the natural language model in the embodiment of the present application, etc.) to obtain the target model/rule 501.
- the training samples maintained in the database 530 are not necessarily collected from the data collection device 560, and may also be received from other devices.
- the training device 520 may not necessarily train the target model/rules 501 based entirely on the training samples maintained by the database 530. It may also obtain training samples from the cloud or other places for model training. The above description should not be used as a guarantee for this application. Limitations of Examples.
- the target model/rules 501 trained according to the training device 520 can be applied to different systems or devices, such as the execution device 510 shown in Figure 5.
- the execution device 510 can be a terminal, such as a mobile phone terminal, a tablet computer, and a notebook.
- AR augmented reality
- VR virtual reality
- the training device 520 can transfer the trained model to the execution device 510 .
- the execution device 510 is configured with an input/output (I/O) interface 512 for data interaction with external devices.
- the user can input data to the I/O interface 512 through the client device 540 (for example, this Attribute description of the code in the application embodiment, etc.).
- the preprocessing module 513 and the preprocessing module 514 are used to perform preprocessing according to the input data received by the I/O interface 512. It should be understood that there may be no preprocessing module 513 and 514 or only one preprocessing module. When the preprocessing module 513 and the preprocessing module 514 do not exist, the computing module 511 can be directly used to process the input data.
- the execution device 510 When the execution device 510 preprocesses input data, or when the calculation module 511 of the execution device 510 performs calculations and other related processes, the execution device 510 can call data, codes, etc. in the data storage system 550 for corresponding processing. , the data, instructions, etc. obtained by corresponding processing can also be stored in the data storage system 550.
- the I/O interface 512 provides the processing results (such as predictive text (that is, the generated code program), etc.) to the client device 540 and thereby to the user.
- processing results such as predictive text (that is, the generated code program), etc.
- the user can manually set the input data, and the "manually set input data" can be operated through the interface provided by the I/O interface 512 .
- the client device 540 can automatically send input data to the I/O interface 512. If requiring the client device 540 to automatically send the input data requires the user's authorization, the user can set corresponding permissions in the client device 540. The user can view the results output by the execution device 510 on the client device 540, and the specific presentation form may be display, sound, action, etc.
- the client device 540 can also be used as a data collection terminal to collect the input data of the input I/O interface 512 and the output results of the output I/O interface 512 as new sample data, and store them in the database 530.
- the I/O interface 512 directly uses the input data input to the I/O interface 512 and the output result of the output I/O interface 512 as a new sample as shown in the figure.
- the data is stored in database 530.
- Figure 5 is only a schematic diagram of a system architecture provided by an embodiment of the present application, and the positional relationship between the devices, devices, modules, etc. shown in the figure does not constitute any limitation.
- the data The storage system 550 is an external memory relative to the execution device 510. In other cases, the data storage system 550 can also be placed in the execution device 510. It should be understood that the above execution device 510 may be deployed in the client device 540.
- the computing module 511 of the above-mentioned execution device 520 can obtain the code stored in the data storage system 550 to implement the steps related to the model inference process in the embodiment of this application.
- the computing module 511 of the execution device 520 may include hardware circuits (such as application specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), general-purpose processors, digital signal processing (DSP), microprocessor or microcontroller, etc.), or a combination of these hardware circuits.
- the training device 520 can be a hardware system with the function of executing instructions, such as a CPU, DSP, etc. , or it is a hardware system that does not have the function of executing instructions, such as ASIC, FPGA, etc., or it is a combination of the above-mentioned hardware system that does not have the function of executing instructions and a hardware system that has the function of executing instructions.
- the calculation module 511 of the execution device 520 can be a hardware system with the function of executing instructions.
- the steps related to the model reasoning process provided by the embodiment of the present application can be software codes stored in the memory.
- the calculation module 511 of the execution device 520 The software code can be obtained from the memory, and the obtained software code can be executed to implement the steps related to the model inference process provided by the embodiments of the present application.
- the computing module 511 of the execution device 520 can be a combination of a hardware system that does not have the function of executing instructions and a hardware system that has the function of executing instructions. Some of the steps related to the model inference process provided by the embodiments of the present application can also be implemented by The computing module 511 of the execution device 520 is implemented by a hardware system that does not have the function of executing instructions, which is not limited here.
- the above-mentioned training device 520 can obtain the code stored in the memory (not shown in Figure 5, which can be integrated with the training device 520 or deployed separately from the training device 520) to implement the model training in the embodiment of the present application. Related steps.
- the training device 520 may include hardware circuits (such as application specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), general-purpose processors, digital signal processors (digital signal processing, DSP, microprocessor or microcontroller, etc.), or a combination of these hardware circuits.
- ASICs application specific integrated circuits
- FPGAs field-programmable gate arrays
- DSP digital signal processors
- the training device 520 can be a hardware system with the function of executing instructions, such as a CPU, DSP, etc., or a combination of other hardware circuits.
- a hardware system with the function of executing instructions such as ASIC, FPGA, etc., or a combination of the above-mentioned hardware systems without the function of executing instructions and a hardware system with the function of executing instructions.
- the training device 520 can be a combination of a hardware system that does not have the function of executing instructions and a hardware system that has the function of executing instructions. Some of the steps related to model training provided by the embodiments of the present application can also be implemented by the training device 520 that does not have the function of executing instructions. It is implemented by the hardware system that executes the instruction function, which is not limited here.
- the server can provide program synthesis function services for the client side through an application programming interface (API).
- API application programming interface
- the terminal device can send relevant parameters (such as attribute descriptions containing codes) to the server through the API provided by the cloud.
- the server can obtain processing results (such as predicted text (that is, the generated code program)) based on the received parameters. ) and return the processing results to the terminal.
- Figure 6 shows the process of using a program provided by a cloud platform to synthesize functional cloud services.
- SDK software development kit
- the cloud platform provides multiple development versions of SDK for users to choose according to the needs of the development environment, such as JAVA version of SDK and python version. SDK, PHP version SDK, Android version SDK, etc.
- the API call of the program synthesis function can be triggered.
- the application triggers the program synthesis function, it initiates an API request to the running instance of the program synthesis function class service in the cloud environment.
- the API request carries the attribute description of the code, and the running instance in the cloud environment performs the attribute description of the code. Process and obtain the processing results (such as predictive text (that is, the generated code program), etc.).
- the cloud environment returns the processing results to the application, thus completing a program synthesis function service call.
- the server can provide a model of the program synthesis function adapted to the domain of the code program based on training data provided by the client (which may include program code and attribute descriptions of the code, for example).
- the server can provide model training services to the client side through an application programming interface (API).
- API application programming interface
- the terminal device can send relevant parameters (such as the attribute description of the code) to the server through the API provided by the cloud.
- the server can obtain the processing result based on the received parameters and send the processing result (such as the code program adapted to the code). model of the domain's program synthesis function, etc.) is returned to the terminal.
- Figure 7 shows the process of using a model training cloud service provided by a cloud platform.
- Federated learning is divided into two steps: model delivery and model upload.
- the central node delivers the model to the terminal device through the network; each terminal device uses local data to calculate the gradient of the model; each distributed node encrypts the gradient and uploads it to the central node;
- the central node summarizes the gradients of each terminal distributed node and uses the parameter average algorithm to update the parameters of the central node model.
- FIG 8 is a schematic diagram of the architecture of a model training method provided by an embodiment of the present application.
- the architecture provided by an embodiment of the present application includes: a central node on the cloud side, which may be a server on the cloud side, for example.
- A1, A2, ... are distributed nodes of type A, such as mobile phone products held by users.
- B1, B2, ... are distributed nodes of type B, such as personal computers held by users.
- the administrator of the distributed node such as users of mobile phones and computers
- the administrator of the distributed node voluntarily shares the data generated in the process of daily use of the device while protecting privacy, and joins the model training plan. , the device becomes a distributed node in the architecture.
- the system in this embodiment can also include more types of distributed nodes, such as smart watches and so on.
- distributed nodes do not upload data to the central node and only save data locally.
- Distributed nodes are connected to cloud servers through communication networks.
- the central node on the cloud side can run large models, while each distributed node can only run small models due to hardware capacity limitations, and A and B can have different data processing capabilities.
- Figure 3 shows a natural language processing system, which includes user equipment and data processing equipment.
- user equipment includes smart terminals such as mobile phones, personal computers, or information processing centers.
- User equipment is the initiator of natural language data processing. As the initiator of language question and answer or query requests, users usually initiate requests through user equipment.
- the above-mentioned data processing equipment may be a cloud server, a network server, an application server, a management server, and other equipment or servers with data processing functions.
- the data processing equipment receives query statements/voice/text, etc. from the smart terminal through an interactive interface, and then performs language data processing in the form of machine learning, deep learning, search, reasoning, decision-making, etc. through the memory for storing data and the processor for data processing. , and feedback the processing results to the user device.
- the memory in the data processing device can be a general term, including local storage and a database that stores historical data.
- the database can be on the data processing device or on other network servers.
- the user device can receive the user's instructions. For example, the user device can receive a piece of text input by the user, and then initiate a request to the data processing device, so that the data processing device can respond to the piece of text obtained by the user device.
- the text executes natural language processing applications (such as natural language generation, text classification, text reasoning, named entity recognition, translation, etc.), thereby obtaining the processing results of the corresponding natural language processing application for the text (such as predicted word results, classification results) , inference results, named entity recognition results, translation results, etc.).
- natural language processing applications such as natural language generation, text classification, text reasoning, named entity recognition, translation, etc.
- the user equipment can receive the user's instructions.
- the user equipment can receive a piece of text input by the user (such as an attribute description of the code), and then initiate a request to the data processing device, so that the data processing device targets the user equipment.
- the obtained piece of text is executed with a natural language processing application (such as program synthesis, etc.), thereby obtaining the processing result of the corresponding natural language processing application for the piece of text (such as predictive text (that is, a generated code program), etc.).
- a natural language processing application such as program synthesis, etc.
- the data processing device can process the above text data through the natural language model trained by the method provided in the embodiment of the present application.
- Figure 4 shows another natural language processing system.
- the user device directly serves as a data processing device.
- the user device can directly receive input from the user and process it directly by the hardware of the user device itself.
- the specific process is as follows Figure 3 is similar, please refer to the above description, and will not be repeated here.
- FIG. 4 is a schematic diagram of a natural language processing related device 300 provided by an embodiment of the present application.
- the user equipment in the above-mentioned Figures 3 and 4 can be the local device 301 or the local device 302 in Figure 4, and the data processing device in Figure 3 can be the execution device 310 in Figure 4, wherein the data storage system 350 can To store the data to be processed by the execution device 310, the data storage system 350 can be integrated on the execution device 310, or can be set up on the cloud or other network servers.
- the processors in Figures 3 and 4 can perform data training/machine learning/deep learning through neural network models or other models, and use the data to finally train or learn the model (such as the natural language model natural language in the embodiment of the present application Model, etc.) execute natural language processing applications (such as program synthesis, etc.) against text data (such as the attribute description text (such as the first text or the second text) of the code described in the embodiments of this application) to obtain corresponding processing results. (For example, predictive text (that is, the generated code program) predictive text, etc.).
- the neural network can be composed of neural units.
- the neural unit can refer to an operation unit that takes xs (ie, input data) and intercept 1 as input.
- the output of the operation unit can be:
- s 1, 2,...n, n is a natural number greater than 1
- Ws is the weight of xs
- b is the bias of the neural unit.
- f is the activation function of the neural unit, which is used to introduce nonlinear characteristics into the neural network to convert the input signal in the neural unit into an output signal.
- the output signal of this activation function can be used as the input of the next convolutional layer, and the activation function can be a sigmoid function.
- a neural network is a network formed by connecting multiple above-mentioned single neural units together, that is, the output of one neural unit can be the input of another neural unit.
- the input of each neural unit can be connected to the local receptive field of the previous layer to extract the features of the local receptive field.
- the local receptive field can be an area composed of several neural units.
- the neural network includes an embedding layer and at least one transformer layer.
- At least one transformer layer can be N transformer layers (N is an integer greater than 0), where each transformer layer includes successively adjacent attention layers, summation and normalization. (add&norm) layer, feed forward layer and summation and normalization layer.
- the current input is embedded to obtain multiple embedding vectors; in the attention layer, P input vectors are obtained from the upper layer of the first transformer layer, and any of the P input vectors are The first input vector is the center. Based on the correlation between each input vector within the preset attention window range and the first input vector, the intermediate vector corresponding to the first input vector is obtained. In this way, P input vectors are determined.
- Corresponding P intermediate vectors; in the pooling layer the P intermediate vectors are merged into Q output vectors, where the multiple output vectors obtained by the last transformer layer in the transformer layer are used as features of the current input express.
- the attention mechanism imitates the internal process of biological observation behavior, that is, a mechanism that aligns internal experience and external sensation to increase the precision of observation in some areas, and can use limited attention resources to quickly filter out high-value information from a large amount of information. .
- the attention mechanism can quickly extract important features of sparse data and is therefore widely used in natural language processing tasks, especially machine translation.
- the self-attention mechanism is an improvement of the attention mechanism, which reduces the dependence on external information and is better at capturing the internal correlation of data or features.
- the essential idea of the attention mechanism can be rewritten as the following formula:
- Lx
- represents the length of Source.
- the meaning of the formula is to imagine that the constituent elements in Source are composed of a series of data pairs. At this time, given a certain element Query in the target Target, by calculating the Query and Based on the similarity or correlation of each Key, the weight coefficient of each Key's corresponding Value is obtained, and then the Value is weighted and summed to obtain the final Attention value. So essentially the Attention mechanism is a weighted summation of the Value values of the elements in the Source, and Query and Key are used to calculate the weight coefficient of the corresponding Value.
- Attention can be understood as selectively filtering out a small amount of important information from a large amount of information and focusing on this important information, while ignoring most of the unimportant information.
- the process of focusing is reflected in the calculation of the weight coefficient.
- the self-attention mechanism can be understood as internal Attention (intra attention).
- the Attention mechanism occurs between the Target element Query and all elements in the Source.
- the self-attention mechanism refers to between the internal elements of the Source or between the internal elements of the Target.
- NLP Natural language processing
- Natural language is human language
- natural language processing is the processing of human language.
- Natural language processing is the process of systematically analyzing, understanding and extracting information from text data in an intelligent and efficient way.
- automatic summarization automatic summarization
- MT machine translation
- NER Named entity recognition
- RE relationship extraction
- IE information extraction
- sentiment analysis sentiment analysis
- speech recognition speech recognition
- question answering system question answering
- topic segmentation etc.
- the pre-trained language model is a natural language sequence encoder that encodes each word in the natural language sequence into a vector representation to perform prediction tasks. Its training consists of two phases. In the pre-training stage, the model performs language model training on large-scale unsupervised text to learn a word representation. In the finetuning stage, the model uses the parameters learned in the pre-training stage for initialization, and performs fewer steps of training on downstream tasks such as text classification and sequence labeling. The semantic information obtained by pre-training can be successfully transferred to downstream tasks.
- An autoregressive language model refers to a model that can predict the next possible following word (such as "nice") based on a given context (such as "mobile phone is very”). This model usually predicts the following word on the right given the left context. word, but could also be a word somewhere in the middle given the context predictions on the left and right.
- Programming language a formal language used to define the execution flow of computer instructions.
- Program synthesis The process of generating a program based on prompts (such as attribute descriptions of codes, etc.).
- Code completion The process of generating a complete code paragraph based on a given code fragment.
- Abstract syntax tree An abstract syntax structure that expresses programming language code in the form of a tree.
- CLM Causal language modeling
- Masked language modeling Model training is performed by predicting the original characters at the masked positions in the character sequence.
- Masked causal language modeling Model training is performed by predicting the original characters at the masked positions in the character sequence based on the previous text.
- No language modeling No loss calculation is performed on characters within this range, and characters within this range do not participate in model training.
- the convolutional neural network can use the error back propagation (BP) algorithm to modify the size of the parameters in the initial super-resolution model during the training process, so that the reconstruction error loss of the super-resolution model becomes smaller and smaller.
- BP error back propagation
- forward propagation of the input signal until the output will produce an error loss
- the parameters in the initial super-resolution model are updated by back-propagating the error loss information, so that the error loss converges.
- the backpropagation algorithm is a backpropagation movement dominated by error loss, aiming to obtain the optimal parameters of the super-resolution model, such as the weight matrix.
- the neural network can use the error back propagation (BP) algorithm to modify the size of the parameters in the initial neural network model during the training process, so that the reconstruction error loss of the neural network model becomes smaller and smaller. Specifically, forward propagation of the input signal until the output will produce an error loss, and the parameters in the initial neural network model are updated by backpropagating the error loss information, so that the error loss converges.
- the backpropagation algorithm is a backpropagation movement dominated by error loss, aiming to obtain the optimal parameters of the neural network model, such as the weight matrix.
- Program synthesis refers to using a certain software program to automatically synthesize a specified program language sequence with specified functions or structures. In short, it is a program that generates a program. Generally speaking, it is divided into two categories: program synthesis (code generation) and code completion. kind. Program synthesis, that is, starting from scratch, can generate corresponding code according to a certain natural language description or a preset function description. Code completion, that is, based on the existing code sequence, continue to generate the following code sequence.
- Language model refers to the modeling of language sequences, such as natural language or programming language.
- most neural networks are used for language model modeling, that is, the occurrence probability of language sequences is predicted through the neural network model to generate language.
- the model Take the model as an example, which is to predict the probability of a certain word, word and other language unit appearing at each sequence position.
- the famous GPT language model is to build a decoder network by building multiple neural network computing units transformer. The decoder is based on the existing In the above, the words that may appear in the language unit at the next position are predicted through iteration until the prediction of the entire sequence is completed, that is, decoding is completed.
- Language model pre-training refers to training the corresponding language model through massive language sequence corpus, so that the language model has the ability to predict the occurrence probability of a certain language unit at a certain position.
- the existing technology follows the natural language processing generation method and trains based on the original code. Currently, this method can continue to write the code based on the existing context. However, for the scenario of function-level code generation, the quality of the code generation has yet to be improved. promote.
- Figure 9 is a flow diagram of a model training method provided by an embodiment of the present application.
- a model training method provided by an embodiment of the present application may include steps 901 to 902. The following are respectively These steps are described in detail.
- the first text and the second text are extracted from the original code text; wherein the first text is the program code and the second text is the program code. Attribute description corresponding to the program code; the first text and the second text include known text and predicted text;
- the original corpus (such as the original code text in the embodiment of the present application) can be obtained, and the original code text can be extracted from the original code text.
- First text and second text can be obtained, and the original code text can be extracted from the original code text.
- the original code text can be existing programming language corpus and natural language corpus.
- existing programming language corpus and natural language corpus can be obtained through the Internet (or other methods).
- the corpus includes code files of mixed programming language and natural language, code files of programming language, and natural language files.
- the collected natural language can be any human language, and the programming language can be any programming language, which is not limited by this application.
- the original code text can be deduplicated from data, files, or function codes, and then duplicate files or duplicate function codes in the corpus can be deduplicated, thereby reducing the total amount of corpus and improving processing and Training efficiency.
- the first text and the second text are extracted from the original code text; wherein the first text is the program code, and the second text is the program code.
- the second text can be in natural language, and the second text can be in any language used for human communication, and the program code can be in any programming language, which is not limited by this application.
- the attribute description includes a function description of the program code or an implementation principle of the program code (or it is also called an explanation of the program code).
- Figure 10A The box shown in Figure 10A is the attribute description, and the rest is the program code, which includes two functions: "euclidean_distance" and "classifier".
- Figure 10B which shows a code text of the function "classifier” and the corresponding attribute description.
- Figure 10C which shows a code text of the function "classifier”.
- Figure 10D shows an attribute of the function "classifier”. Description.
- the function code segment in the original code text can be identified as the first text: for example, the original code text can be identified using certain rules (for example, a program code identification method such as a code abstract syntax tree (AST) can be used) function code snippet in .
- AST code abstract syntax tree
- the natural language in the original code text can be recognized as the second text (associated with the recognized first text): for example, certain rules can be used (for example, a natural language recognition method can be used) to Identifies the attribute description of the code in the original code text.
- a correspondence relationship between the first text and the second text can be established to form a "natural language description-function implementation code" (Description-Function code) ) sentence pair form sample.
- “natural language description-function implementation code” “Description-Function code”
- the text and the second text are a pair of samples.
- the code text and corresponding attribute description are extracted from the original program code and used as a pair of samples.
- the original program file is directly used as a training sample. , helping the natural language model to establish an association between the code text and the corresponding attribute description during training, without having to learn the ability to identify this association from samples during the training process, reducing the training difficulty and improving the prediction of the model Accuracy.
- the first text (or program language function-level code fragment) and its corresponding second text (or natural text) can be extracted from the original corpus or the filtered and cleaned original corpus.
- Language description constructing "natural language description-programming language function code pair".
- the function-level code fragments of the programming language and their corresponding natural language descriptions can be extracted through some rule methods.
- the function-level code fragments of the programming language and their corresponding natural language descriptions can be extracted through the abstract syntax tree (AST).
- AST abstract syntax tree
- program language function-level code snippets ,and their corresponding natural language descriptions can be ,extracted through other automated methods.
- the programming language function code and natural language description can be extracted from the code file of mixed programming language and natural language.
- the programming language function code can be extracted from the code file of the programming language. Extract natural language descriptions from natural language documents.
- the "natural language description-programming language function code pair" can be constructed through the correspondence between the programming language code fragments and the natural language description in the mixed programming language and natural language code files.
- the "natural language description-programming language function code pair" can be constructed through other programming language codes. The correspondence relationship between the fragments and the natural language description constructs a "natural language description-programming language function code pair".
- function-level code fragments such as code programs
- code programs can be normalized, standardized code syntax, or unified code style.
- certain rules such as code abstract syntax tree (AST)
- AST code abstract syntax tree
- function-level code snippets (such as code programs) can be removed based on the statistical characteristics of their code corpus, and the corpus with too short natural language description part and the excessive code part in the code corpus can be removed. Long corpus or corpus with a large length difference between the two to improve the quality of the training corpus.
- training samples are usually difficult to obtain, that is, the number of training samples (text pairs composed of the first text and the second text) is small, data enhancement can be performed on the obtained training samples to obtain More training samples.
- the first text is obtained based on the first original text extracted from the original code text; wherein the first text is a partial text unit in the first original text. Obtained by modification.
- the modification includes: deletion, addition or replacement.
- the replacement is specifically a replacement with the same semantics.
- synonymous segments may be generated based on back translation technology, or synonymous segments may be generated based on synonymous sentence generation technology.
- the second text is obtained based on the second original text extracted from the original code text; wherein the second text is a partial text unit in the second original text. Obtained by modification.
- the modification includes: deletion, addition or replacement.
- the replacement is specifically a replacement with the same semantics.
- synonymous segments may be generated based on back translation technology, or synonymous segments may be generated based on synonymous sentence generation technology.
- High-quality automatically generated training data ultimately improves model performance.
- training samples can be processed differently at different stages of model training.
- the first text and the second text obtained in the above way can be used, and in the model training In the fine-tuning stage, text adapted to the application field of the program code (also including program code and corresponding code attribute descriptions) can be extracted from the original code text as a training sample.
- the first text or the second text is text extracted from the original code text and adapted to the application field of the program code.
- the adaptation to the application field of the program code includes: text whose length or the length of a single line of text is less than a threshold; wherein the threshold is related to the application field of the program code.
- information in other dimensions besides length can also be used to quantify and apply the program code.
- the samples may be filtered based on their similarity to data samples from the application domain of the function-level code generator.
- the similarity calculation method can be the length relationship between natural language descriptions, the length relationship between program language function codes, or other relationships.
- some text units in the training sample can be used as known text, and the other part of the text As the text to be predicted, the text at the text position where the text to be predicted is predicted based on the known text.
- the text prediction process can be performed with different training objectives for the first text and the second text.
- one of the four training objectives of CLM, MLM, MCLM and NoLM can be used to perform the text prediction process.
- one of the three training objectives of CLM, MLM and MCLM can be used to perform the model training process.
- the text position corresponding to the predicted text when predicting the text in the first text, may be determined from the first text in a first manner; When predicting text in two texts, the text position corresponding to the predicted text can be determined from the second text through a second method; the first method and the second method are different.
- the first method and the second method are respectively one of the following prediction methods:
- the known text is all the texts that have been predicted by the natural language model (that is, CLM);
- the following text position of the known text is used as the text position corresponding to the predicted text; wherein the known text is the unmasked text among all the texts that have been predicted by the natural language model (that is, MCLM ).
- the text position corresponding to the predicted text can be determined through one of the following prediction methods:
- the known text is all the texts that have been predicted by the natural language model (that is, CLM);
- the following text position of the known text is used as the text position corresponding to the predicted text; wherein the known text is the unmasked text among all the texts that have been predicted by the natural language model (that is, MCLM ).
- the natural language model can predict texts sequentially along the preset context prediction direction.
- all words that have been predicted by the natural language model can be used as known text, and have been The known text is used as the context of the position text to predict the text location of the text.
- prediction can be made in the order from the context to the context (that is, from the beginning of the text to the end of the text), each time You can predict a text unit and continue to predict based on the already predicted text unit in the next prediction.
- the text can be "the cat sat on the mat”.
- you can predict " The text at the text position of "the” can be predicted by the initial identification position and "the” in the next prediction.
- the text at the text position of "cat” can be predicted by the initial identification position, "the” in the next prediction. ” and “cat” to predict the text at the text position of “sat”.
- the training objectives of CLM can be used for training when processing the first text or the second text.
- the second text when using the second text for model training, you can use the CLM training target to train the natural language model. After predicting the second text, you can use the first text as a non-CLM training target for model training (in this case, the first text is used as the training target).
- the second text can be used as known text to participate in the process of using the first text for model training).
- the second text for model training when using the second text for model training, you can use non-CLM training targets to train the natural language model. After predicting the second text, you can use the CLM training targets to use the first text for model training (at this time The second text can be used as a known text to participate in the process of using the first text for model training).
- the text units in the text can be masked (for example, they can be randomly masked), and the masked text units can be used as predicted texts.
- the masked text unit can be used as known text, and the natural language model can sequentially calculate the text location of the masked text unit based on the unmasked text unit (or the predicted masked text unit).
- Text prediction for example, the text can be "the cat sat on the mat”. After masking the text, you can get "the_sat_the mat", where the symbol "_” here means masking, not referring to the underline in .
- the text at the text position where one of the "_" is located can be predicted through the initial identification bit and the unmasked text unit.
- the training target of MCL can be used for training when processing the first text or the second text.
- the second text when using the second text for model training, you can use the MCL training target to train the natural language model. After predicting the second text, you can use the first text as a non-MCL training target for model training (in this case, the first text is used for model training).
- the second text can be used as known text to participate in the process of using the first text for model training).
- the second text for model training when using the second text for model training, you can use non-MCL training targets to train the natural language model. After predicting the second text, you can use the MCL training targets to use the first text for model training (at this time The second text can be used as a known text to participate in the process of using the first text for model training).
- the natural language model can predict text sequentially along the preset context prediction direction.
- CLM all words that have been predicted by the natural language model can be used as Known text
- the known text is used as the upper text of the position text to predict the text position of the predicted text.
- MCLM the words that have been predicted by the natural language model in MCLM can be masked, and natural language can be used
- the unmasked text units in the words that have been predicted by the model are used to predict the text position of the predicted text. For example, it can be performed in the order from the top to the bottom (that is, from the beginning of the text to the end of the text). predict.
- predictions can be made in the order from top to bottom (that is, from the beginning of the text to the end of the text).
- One text unit can be predicted at a time, and predictions can be continued based on the already predicted text units in the next prediction.
- the text can be "the cat sat on the mat".
- the text at the text position of "the” can be predicted through the initial identification bit, and the initial identification bit and "the” can be masked. code, if "the” is masked, in the next prediction, the text at the text position of "cat” can be predicted through the initial identification position.
- the training objectives of MCLM can be used for training when processing the first text or the second text.
- the second text when using the second text for model training, you can use the MCLM training target to train the natural language model. After predicting the second text, you can use the first text as a non-MCLM training target for model training (in this case, the second text The text can be used as known text to participate in the process of using the first text for model training).
- the second text for model training when using the second text for model training, you can use a non-MCLM training target to train the natural language model. After predicting the second text, you can use the MCLM training target to use the first text for model training (at this time The second text can be used as a known text to participate in the process of using the first text for model training).
- the second text can not be predicted, but the second text can be directly used as the known text, and the first text can be used as a training sample to predict text units in the first text.
- Training targets such as CLM, MLM, and MCLM use the first text as a training sample to predict text units in the first text.
- the second text can be used as a known text to participate in the process of using the first text for model training.
- CLM, MLM, MCLM, etc. can be used as training targets, and the first text can be used for model training (in this case, the second text can be used as a known text).
- Zhiwen participates in the process of model training using the first text).
- the four optional training objectives can be applied to the natural language description or function code of the "Description-Function pair" (Description-Function) respectively, and the combination Form 12 types of training objectives, such as:
- Description mask causal language modeling and code language modeling (Description-MCLM+Function-CLM): The natural language description part applies MCLM training objectives, and the function code part applies CLM training objectives. At the same time, it trains natural language description understanding ability and function code generation ability.
- Description-NoLM+Function-CLM Description-NoLM+Function-CLM: The natural language description part has no training objectives, and the function code part applies CLM training objectives. Only train function code generation capabilities.
- the known text in the process of iterative training of the natural language model, can be processed through the natural language model, the text corresponding to the text position of the predicted text can be predicted, the text prediction result is obtained, and based on the text prediction The difference between the result and the corresponding ground truth determines the loss, which can be used to update the natural language model.
- the known text can be embedded through an embedding layer to obtain an embedding vector.
- the embedding layer can be called the input embedding layer.
- the current input can be each text unit in the known text (or it can be called a known data unit). After obtaining the current input, the embedding layer can embed each known data unit in the current input to obtain the embedding vector corresponding to each known data unit.
- the position vector of each known data unit can also be obtained, and the position vector is used to indicate the position of the known data unit; wherein the position is used to represent the position of the known data unit in the text (for example, the first text Or the position in the second text), specifically, the position is used to indicate the relative positional relationship between the known data unit and other known data units, as well as the known data unit and each text unit in the predicted text. .
- the embedding layer may include an input embedding layer and a positional encoding layer.
- word embedding processing can be performed on each known data unit in the current input to obtain the embedding vector of each known data unit.
- the position coding layer the position of each known data unit in the current input can be obtained, and then a position vector is generated for the position of each known data unit.
- the position of each known data unit in the text may be the absolute position of each known data unit in the text (eg, the first text or the second text). Taking the current input as "What number should I pay back the Huabei?" for example, the position of "number" can be represented as the first digit, the position of "number” can be represented as the second digit,... In some examples, the position of each known data unit in the text (eg, the first text or the second text) may be the relative position of each known data unit in the text (eg, the first text or the second text).
- the position of "what number” can be expressed as before “number”, and the position of "number” can be expressed as after “what number” and before “should”,... ....
- the position vector of each known data unit and the corresponding embedding vector can be fused to obtain the embedding vector of each known data unit, that is, the current Enter multiple corresponding embedding vectors.
- the fusion method may be to add the embedding vector and the position vector, or to use other operations so that the embedding vector can carry a known data unit in the text (such as the first text or the second text) and the one
- the information on the position of the data unit in the text is known, and the specific fusion method is not limited here.
- Multiple embedding vectors can be represented as embedding matrices with preset dimensions. The number of the plurality of embedding vectors can be set to M, and the default dimension is H dimension, then the plurality of embedding vectors can be expressed as an M ⁇ H embedding matrix.
- the position of the predicted text in the text (for example, the first text or the second text) can also be embedded through the embedding layer to obtain a method for representing the text (for example, the first text or the second text).
- the embedding vector of the position of the predicted text in the text (for example, the first text or the second text) in the second text), and the embedding vector can be used as the input of the subsequent natural language model.
- the position is used to indicate the relative position relationship between each text unit in the predicted text and each known data unit in the text (such as the first text or the second text).
- the position is used to indicate the relative position relationship between each text unit in the predicted text and each known data unit in the text (such as the first text or the second text).
- the natural language model may include an encoder and a decoder.
- the encoder and decoder may include multiple transformer layers.
- the core feature of the transformer layer lies in its unique attention mechanism.
- the transformer model uses this attention mechanism to assign different attention coefficients to the embedding vectors of each word in the sentence, thereby more comprehensively considering the impact of the context on each word in the sentence.
- the specific transformer layer can include sequentially adjacent multi-head attention layers, addition and normalization (add&norm) layers, feed forward layers, and addition and normalization layers.
- the attention layer is connected to the embedding layer, and multiple embedding vectors are obtained from the embedding layer as input vectors. Based on the correlation between the embedding vectors in the multiple embedding vectors, each embedding vector is synthesized to obtain multiple output vectors. Output to the subsequent transformer layer.
- the transformer layer takes the output of the previous layer as an input vector and performs similar operations to the previous transformer layer.
- the encoder can obtain multiple output vectors, and the decoder can use multiple first output vectors and the embedded vectors as inputs to obtain a vector representation of the predicted text.
- the vector representation of the predicted text can be further passed through a classifier. (For example, support vector machine, softmax classifier, K-nearest neighbor algorithm, etc. can be used) to restore the predicted text.
- the loss can be constructed based on the predicted text as well as the text prediction structure, and the natural language model is updated based on the loss.
- the loss can be constructed based on the predicted text and the text prediction structure, and the loss or the gradient obtained based on the loss is transferred to other devices (such as a server on the cloud side), and the server on the cloud side executes the loss based on the loss or the gradient obtained based on the loss. Gradient to update the actions of the natural language model.
- the present invention aims at the generation of function-level code. It generally includes a two-stage training strategy, as well as a design of selecting different loss function training targets for code and text respectively, ensuring that the first stage of the model lays the foundation and the second stage is advanced. Training to improve abilities.
- a targeted function-level code corpus preparation mechanism is also designed based on the different goals of the two stages, ensuring that it can well improve the excellent performance of the trained generative model in function code generation capabilities.
- the first data module provides the most original data and collects a large amount of the most original data containing code, including but not limited to code project files, independent code snippets, script files, etc.
- the second data module based on the first data module, provides the data required for training the first function-level code generation training module.
- This module is optionally composed of the following devices:
- First data filter Preliminarily filter the data in the first data module, optionally removing data with too long files or too long single lines of code, removing data in other than target code languages or natural language languages, etc.
- Function code segment extractor Based on the corpus data obtained by the first data filter, identify all function code segments in the corpus:
- AST code abstract syntax tree
- NL-PL sample constructor identifies the natural language description corresponding to the function code fragment (if any):
- AST code abstract syntax tree
- the extracted code-level function and its description form a sentence pair sample of "Natural Language Description-Function Implementation Code” (Natural Language–Program Language).
- Natural Language “Natural Language–Program Language”.
- Data normalizer Optionally normalize function-level code fragments, standardize code syntax, unify code style, and use optional certain rules and optional code abstract syntax tree (AST) to parse the code. Extract the pure code part and unify spaces, indentation and other styles to normalize the data to improve the quality of the corpus.
- AST optional code abstract syntax tree
- the third data module Based on the second data module, it provides the data required for training the second function-level code generation training module, optional targeted filtering, and optional data enhancement.
- This module is optionally composed of the following devices:
- Second data filter Optionally, based on the statistical characteristics of the code corpus, remove the corpus in which the natural language description part is too short, the code part is too long, and the length difference between the two is too large. To improve the quality of training corpus.
- Function code augmenter Optionally, through random replacement of variable names in the code, equivalent replacement of (cyclic) code segments, and random insertion of invalid code segments, a large amount of high-quality automatically generated training data can be obtained. Finally, Improve model performance.
- Natural language augmenter Optionally replace synonyms and sentence patterns in natural language descriptions, or use paraphrase generation models or methods in natural language processing technology, data augmentation methods, and data noise methods to enhance natural language amplification.
- the language description is augmented to obtain a large amount of high-quality automatically generated training data, ultimately improving model performance.
- the first function-level code generation training module uses the data generated by the second data module to perform function code generation training.
- the training can use natural language to generate the pattern lines of common pattern decoders, such as GPT.
- the first function-level code generation training module also includes a training target combiner for selecting and determining training targets.
- the optional 4 training objectives (objectives, CLM, MLM, MCLM, NoLM) are applied to the natural language description or function code of the "Description-Function pair" (Description-Function) respectively, and combined to form 12 training objectives.
- the second function-level code generation training module uses the data generated by the third data module to further train the function code generation based on the model trained by the first function-level code generation training module.
- the training can use natural language to generate common A module line with a decoder, such as GPT.
- the first function-level code generation training module also includes a training target combiner for selecting and determining training targets.
- the optional 4 training objectives (objectives, CLM, MLM, MCLM, NoLM) are applied to the natural language description or function code of the "Description-Function pair" (Description-Function) respectively, and combined to form 12 training objectives.
- the structure of the first function-level code generator can be any generative neural network model, such as Transformer decoder and RNN, with its model parameters randomly initialized.
- the first function-level code generator uses the corpus output by the second data module as training data.
- the first function-level code generator uses the first combination optimizer to update model parameters.
- the first combination optimizer optimizes the natural language description part and the programming language function code part respectively.
- the first combination optimizer can choose four training objectives: CLM, MLM, MCLM, and NoLM.
- the first combination optimizer can choose three training objectives: CLM, MLM, and MCLM. Note that there is no NoLM training objective here.
- the training objectives of the natural language description part and the programming language function code part can be freely combined.
- Step 1 Obtain training data for the function-level code generator
- the first data module acquires existing programming language corpus and natural language corpus.
- Source code files can be crawled from the Github website, including natural language descriptions and programming language codes.
- Step 2 Prepare training data for the first function-level code generator
- the second data module constructs the data required for training the first function-level code generator, optionally performs data filtering and cleaning, and optionally performs data enhancement.
- the second data module extracts the program language function-level code fragments and their corresponding natural language descriptions from the corpus output by the first data module or the filtered and cleaned corpus of the second data module, and constructs a "natural language description-program language function code pair" ".
- extract program language function-level code snippets and their corresponding natural language descriptions through some rule methods, optionally extract program language function-level code snippets and their corresponding natural language descriptions through abstract syntax trees (AST), optionally Extract program language function-level code snippets and their corresponding natural language descriptions through other automated methods.
- AST abstract syntax trees
- Natural language description can be constructed through the corresponding relationship between the program language code snippets and the natural language description in the mixed programming language and natural language code files, and optionally through the correspondence between the program language code snippets and the natural language description. The corresponding relationship of description constructs "natural language description-programming language function code pair".
- Step 3 Train the first function-level code generator
- the structure of the first function-level code generator can be any generative neural network model, or a Transformer decoder, for example:
- the first function-level code generator uses the corpus output by the second data module as training data. No annotation information is required, only the unannotated corpus output by the second data module is required. For example, place the "natural language description" part of the example function "classifier” between ⁇ descr> and ⁇ python> in Figure 12, and place the "programming language function code” part of the example function "classifier” between After ⁇ python> in Figure 12, this is used as the training input of the first function-level code generator.
- the first function-level code generator uses the first combination optimizer to update model parameters.
- the first combination optimizer optimizes the natural language description part and the programming language function code part respectively.
- the first combination optimizer can choose four training objectives: CLM, MLM, MCLM, and NoLM.
- the first combination optimizer can choose three training objectives: CLM, MLM, and MCLM. Note that there is no NoLM training objective here.
- the training objectives of the natural language description part and the programming language function code part can be freely combined. Take NoLM as the "natural language description" part and CLM as the "programming language function code" part as an example:
- Loss calculation can not be performed on the "Natural Language Description” part in Figure 13
- CLM Loss calculation can be performed on the "Programming Language Function Code” part.
- Step 4 Prepare training data for the second function-level code generator
- the third data module constructs the data required for training the second function-level code generator.
- the third data module performs filtering and optional data enhancement on the corpus output by the second data module.
- the third data module filters the corpus output by the second data module to make the training data closer to the application field of the second function-level code generator.
- the filtering is based on similarity between the data sample output by the second data module and the data sample in the application domain of the second function-level code generator. You can compare the length of the "natural language description" part of the two data sets, for example, count the maximum length L max and minimum length L min of the "natural language description” in the application field data of the second function-level code generator, and select only the second The samples whose "natural language description" length l in the data output by the data module conforms to L min ⁇ l ⁇ L max are used as the output of the third data module.
- Step 5 Train the second function-level code generator
- the structure of the second function-level code generator is consistent with the structure of the first function-level code generator, and the second function-level code generator is initialized with model parameters of the first function-level code generator.
- the second function-level code generator uses the corpus output by the third data module as training data. No annotation information is required, only the unannotated corpus output by the third data module is required.
- the second function-level code generator uses the second combinatorial optimizer to update model parameters.
- the second combination optimizer optimizes the natural language description part and the programming language function code part respectively.
- the second combination optimizer can choose four training objectives: CLM, MLM, MCLM, and NoLM.
- the second combination optimizer can choose three training objectives: CLM, MLM, and MCLM. Note that there is no NoLM training objective here.
- the training objectives of the natural language description part and the programming language function code part can be freely combined.
- Step 6 Optimize the inference parameters of the second function-level code generator
- the second function-level code generator When using the second function-level code generator to generate function-level code, it can optionally be generated based on random sampling (sampling) generation or beam search (beam search) generation, or other strategy generation. During this code generation process, there are optional inference parameters that affect the randomness of the generated results, such as temperature.
- a part of the data output by the third data module is reserved as a development set (development set), which does not participate in the training of the second function-level code generator. Based on this development set, the aforementioned inference parameters can be adjusted to improve the performance of the third function-level code generator. Correctness of code generated by a two-function-level code generator.
- This embodiment is based on the data disclosed by Github, uses the first, second, and third data modules to collect and process the training data of the first and second function-level code generators, and uses the first and second combined optimizers to perform train.
- the corresponding "program language function code” (Python) can be generated for the input "natural language description”.
- using the code generator obtained by the present invention can achieve a Pass@1 score of 17.07% on the public HumanEval test task, significantly exceeding the currently public best score (13.17%).
- the function-level code generator designed in this example based on pre-training and natural language description can generate program language function fragments corresponding to the functions based on natural language input, and the function-level code is more accurate.
- this solution uses public Github data to train the model, making data acquisition easy and sustainable expansion.
- Figure 14 is a schematic structural diagram of a model training device provided by an embodiment of the present application.
- a model training device 1400 provided by an embodiment of the present application includes:
- the acquisition module 1401 is used to acquire a first text and a second text, where the first text and the second text are extracted from the original code text; wherein the first text is a program code, and the third text is a program code.
- the second text is the attribute description corresponding to the program code; the first text and the second text include known text and predicted text;
- step 901 For a specific description of the acquisition module 1401, please refer to the introduction of step 901 in the above embodiment, and will not be described again here.
- the prediction module 1402 is used to predict the text corresponding to the text position of the predicted text through the natural language model according to the known text, and obtain a text prediction result; the predicted text and the text prediction result are used to update the Describe the natural language model.
- step 902 For a specific description of the prediction module 1402, please refer to the introduction of step 902 in the above embodiment, and will not be described again here.
- the attribute description includes a function description of the program code or an implementation principle of the program code.
- the device further includes:
- a text position determination module configured to determine the text position corresponding to the predicted text from the first text in a first manner when predicting the text in the first text
- the text position corresponding to the predicted text is determined from the second text through the second method; the first method and the second method are different.
- the first method and the second method are respectively one of the following prediction methods:
- the following text position of the known text is used as the text position corresponding to the predicted text; wherein the known text is the unmasked text among all the texts that have been predicted by the natural language model.
- the text position determination module is also configured to determine the text position corresponding to the predicted text through one of the following prediction methods without predicting the text in the second text:
- the following text position of the known text is used as the text position corresponding to the predicted text; wherein the known text is the unmasked text among all the texts that have been predicted by the natural language model.
- the first text is obtained based on the first original text extracted from the original code text; wherein the first text is obtained by modifying some text units in the first original text; or,
- the second text is obtained based on the second original text extracted from the original code text; wherein the second text is obtained by modifying some text units in the second original text.
- the modification includes: deletion, addition or replacement.
- the replacement is specifically a replacement with the same semantics.
- the first text or the second text is text extracted from the original code text and adapted to the application field of the program code.
- the application fields adapted to the program code include:
- the text length or the length of a single line of text is less than a threshold; wherein the threshold is related to the application field of the program code.
- FIG. 15 is a schematic structural diagram of an execution device provided by an embodiment of the present application.
- the execution device 1500 can be embodied as a virtual reality VR device, a mobile phone, Tablets, laptops, smart wearable devices, monitoring data processing equipment or servers, etc. are not limited here.
- the execution device 1500 includes: a receiver 1501, a transmitter 1502, a processor 1503 and a memory 1504 (the number of processors 1503 in the execution device 1500 can be one or more, one processor is taken as an example in Figure 15) , wherein the processor 1503 may include an application processor 15031 and a communication processor 15032.
- the receiver 1501, the transmitter 1502, the processor 1503, and the memory 1504 may be connected through a bus or other means.
- Memory 1504 may include read-only memory and random access memory and provides instructions and data to processor 1503 .
- a portion of memory 1504 may also include non-volatile random access memory (NVRAM).
- NVRAM non-volatile random access memory
- the memory 1504 stores processor and operating instructions, executable modules or data structures, or a subset thereof, or an extended set thereof, where the operating instructions may include various operating instructions for implementing various operations.
- the processor 1503 controls the execution of operations of the device.
- various components of the execution device are coupled together through a bus system.
- the bus system may also include a power bus, a control bus, a status signal bus, etc.
- various buses are called bus systems in the figure.
- the methods disclosed in the above embodiments of the present application can be applied to the processor 1503 or implemented by the processor 1503.
- the processor 1503 may be an integrated circuit chip with signal processing capabilities. During the implementation process, each step of the above method can be completed by instructions in the form of hardware integrated logic circuits or software in the processor 1503 .
- the above-mentioned processor 1503 can be a general-purpose processor, a digital signal processor (DSP), a microprocessor or a microcontroller, and can further include an application specific integrated circuit (ASIC), a field programmable Gate array (field-programmable gate array, FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.
- DSP digital signal processor
- ASIC application specific integrated circuit
- FPGA field-programmable gate array
- the processor 1503 can implement or execute each method, step and logical block diagram disclosed in the embodiment of this application.
- a general-purpose processor may be a microprocessor or the processor may be any conventional processor, etc.
- the steps of the method disclosed in conjunction with the embodiments of the present application can be directly implemented by a hardware decoding processor, or executed by a combination of hardware and software modules in the decoding processor.
- the software module can be located in random access memory, flash memory, read-only memory, programmable read-only memory or electrically erasable programmable memory, registers and other mature storage media in this field.
- the storage medium is located in the memory 1504.
- the processor 1503 reads the information in the memory 1504 and completes the steps related to the model inference process in the above method in combination with its hardware.
- the receiver 1501 may be configured to receive input numeric or character information and generate signal inputs related to performing relevant settings and functional controls of the device.
- the transmitter 1502 can be used to output numeric or character information through the first interface; the transmitter 1502 can also be used to send instructions to the disk group through the first interface to modify the data in the disk group; the transmitter 1502 can also include a display device such as a display screen .
- FIG. 16 is a schematic structural diagram of the training device provided by the embodiment of the present application.
- the training device 1600 is implemented by one or more servers.
- the training device 1600 There may be relatively large differences due to different configurations or performance, which may include one or more central processing units (CPU) 1616 (for example, one or more processors) and memory 1632, one or more storage applications Storage medium 1630 for program 1642 or data 1644 (eg, one or more mass storage devices).
- the memory 1632 and the storage medium 1630 may be short-term storage or persistent storage.
- the program stored in the storage medium 1630 may include one or more modules (not shown in the figure), and each module may include a series of instruction operations in the training device.
- the central processor 1616 may be configured to communicate with the storage medium 1630 and execute a series of instruction operations in the storage medium 1630 on the training device 1600 .
- the training device 1600 may also include one or more power supplies 1626, one or more wired or wireless network interfaces 1650, one or more input and output interfaces 1658; or, one or more operating systems 1641, such as Windows ServerTM, Mac OS XTM , UnixTM, LinuxTM, FreeBSDTM and so on.
- operating systems 1641 such as Windows ServerTM, Mac OS XTM , UnixTM, LinuxTM, FreeBSDTM and so on.
- the central processor 1616 is used to perform actions related to model training in the above embodiment.
- An embodiment of the present application also provides a computer program product that, when run on a computer, causes the computer to perform the steps performed by the foregoing execution device, or causes the computer to perform the steps performed by the foregoing training device.
- Embodiments of the present application also provide a computer-readable storage medium.
- the computer-readable storage medium stores a program for performing signal processing.
- the program When the program is run on a computer, it causes the computer to perform the steps performed by the aforementioned execution device. , or, causing the computer to perform the steps performed by the aforementioned training device.
- the execution device, training device or terminal device provided by the embodiment of the present application may specifically be a chip.
- the chip includes: a processing unit and a communication unit.
- the processing unit may be, for example, a processor.
- the communication unit may be, for example, an input/output interface. Pins or circuits, etc.
- the processing unit can execute the computer execution instructions stored in the storage unit, so that the chip in the execution device executes the data processing method described in the above embodiment, or so that the chip in the training device executes the data processing method described in the above embodiment.
- the storage unit is a storage unit within the chip, such as a register, cache, etc.
- the storage unit may also be a storage unit located outside the chip in the wireless access device, such as Read-only memory (ROM) or other types of static storage devices that can store static information and instructions, random access memory (random access memory, RAM), etc.
- ROM Read-only memory
- RAM random access memory
- Figure 17 is a structural schematic diagram of a chip provided by an embodiment of the present application.
- the chip can be represented as a neural network processor NPU 1700.
- the NPU 1700 serves as a co-processor and is mounted to the main CPU (Host). CPU), tasks are allocated by the Host CPU.
- the core part of the NPU is the arithmetic circuit 1703.
- the arithmetic circuit 1703 is controlled by the controller 1704 to extract the matrix data in the memory and perform multiplication operations.
- the computing circuit 1703 internally includes multiple processing units (Process Engine, PE).
- arithmetic circuit 1703 is a two-dimensional systolic array.
- the arithmetic circuit 1703 may also be a one-dimensional systolic array or other electronic circuit capable of performing mathematical operations such as multiplication and addition.
- arithmetic circuit 1703 is a general-purpose matrix processor.
- the arithmetic circuit obtains the corresponding data of matrix B from the weight memory 1702 and caches it on each PE in the arithmetic circuit.
- the operation circuit takes matrix A data and matrix B from the input memory 1701 to perform matrix operations, and the partial result or final result of the matrix is stored in an accumulator (accumulator) 1708 .
- the unified memory 1706 is used to store input data and output data.
- the weight data directly passes through the storage unit access controller (Direct Memory Access Controller, DMAC) 1705, and the DMAC is transferred to the weight memory 1702.
- Input data is also transferred to unified memory 1706 via DMAC.
- DMAC Direct Memory Access Controller
- BIU is the Bus Interface Unit, that is, the bus interface unit 1710, which is used for the interaction between the AXI bus and the DMAC and the Instruction Fetch Buffer (IFB) 1709.
- IFB Instruction Fetch Buffer
- the bus interface unit 1710 (Bus Interface Unit, BIU for short) is used to fetch the memory 1709 to obtain instructions from the external memory, and is also used for the storage unit access controller 1705 to obtain the original data of the input matrix A or the weight matrix B from the external memory.
- BIU Bus Interface Unit
- DMAC is mainly used to transfer the input data in the external memory DDR to the unified memory 1706 or the weight data to the weight memory 1702 or the input data to the input memory 1701 .
- the vector calculation unit 1707 includes multiple arithmetic processing units. If necessary, the output of the arithmetic circuit 1703 is further processed, such as vector multiplication, vector addition, exponential operation, logarithmic operation, size comparison, etc. Mainly used for non-convolutional/fully connected layer network calculations in neural networks, such as Batch Normalization, pixel-level summation, upsampling of feature planes, etc.
- vector calculation unit 1707 can store the processed output vectors to unified memory 1706 .
- the vector calculation unit 1707 can apply a linear function; or a nonlinear function to the output of the operation circuit 1703, such as linear interpolation on the feature plane extracted by the convolution layer, or a vector of accumulated values, to generate an activation value.
- vector calculation unit 1707 generates normalized values, pixel-wise summed values, or both.
- the processed output vector can be used as an activation input to the arithmetic circuit 1703, such as for use in a subsequent layer in a neural network.
- the instruction fetch buffer 1709 connected to the controller 1704 is used to store instructions used by the controller 1704;
- the unified memory 1706, the input memory 1701, the weight memory 1702 and the fetch memory 1709 are all On-Chip memories. External memory is private to the NPU hardware architecture.
- the processor mentioned in any of the above places can be a general central processing unit, a microprocessor, an ASIC, or one or more integrated circuits used to control the execution of the above programs.
- the device embodiments described above are only illustrative.
- the units described as separate components may or may not be physically separated, and the components shown as units may or may not be physically separate.
- the physical unit can be located in one place, or it can be distributed across multiple network units. Some or all of the modules can be selected according to actual needs to achieve the purpose of the solution of this embodiment.
- the connection relationship between modules indicates that there are communication connections between them, which can be specifically implemented as one or more communication buses or signal lines.
- the present application can be implemented by software plus necessary general hardware. Of course, it can also be implemented by dedicated hardware including dedicated integrated circuits, dedicated CPUs, dedicated memories, Special components, etc. to achieve. In general, all functions performed by computer programs can be easily implemented with corresponding hardware. Moreover, the specific hardware structures used to implement the same function can also be diverse, such as analog circuits, digital circuits or special-purpose circuits. circuit etc. However, for this application, software program implementation is a better implementation in most cases. Based on this understanding, the technical solution of the present application can be embodied in the form of a software product in essence or that contributes to the existing technology.
- the computer software product is stored in a readable storage medium, such as a computer floppy disk. , U disk, mobile hard disk, ROM, RAM, magnetic disk or optical disk, etc., including several instructions to cause a computer device (which can be a personal computer, training device, or network device, etc.) to execute the steps described in various embodiments of this application. method.
- a computer device which can be a personal computer, training device, or network device, etc.
- the computer program product includes one or more computer instructions.
- the computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device.
- the computer instructions may be stored in or transmitted from one computer-readable storage medium to another, for example, the computer instructions may be transferred from a website, computer, training device, or data
- the center transmits to another website site, computer, training equipment or data center through wired (such as coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (such as infrared, wireless, microwave, etc.) means.
- wired such as coaxial cable, optical fiber, digital subscriber line (DSL)
- wireless such as infrared, wireless, microwave, etc.
- the computer-readable storage medium may be any available medium that a computer can store, or a data storage device such as a training device or a data center integrated with one or more available media.
- the available media may be magnetic media (eg, floppy disk, hard disk, magnetic tape), optical media (eg, DVD), or semiconductor media (eg, solid state disk (Solid State Disk, SSD)), etc.
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Physics & Mathematics (AREA)
- Artificial Intelligence (AREA)
- General Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- Computational Linguistics (AREA)
- General Health & Medical Sciences (AREA)
- Health & Medical Sciences (AREA)
- Audiology, Speech & Language Pathology (AREA)
- Software Systems (AREA)
- Computer Vision & Pattern Recognition (AREA)
- Data Mining & Analysis (AREA)
- Evolutionary Computation (AREA)
- Medical Informatics (AREA)
- Computing Systems (AREA)
- Mathematical Physics (AREA)
- Machine Translation (AREA)
Abstract
Description
本申请要求于2022年07月20日提交中国专利局、申请号为202210857821.1、发明名称为“一种模型训练方法及其装置”的中国专利申请的优先权,其全部内容通过引用结合在本申请中。This application claims priority to the Chinese patent application submitted to the China Patent Office on July 20, 2022, with the application number 202210857821.1 and the invention title "A model training method and its device", the entire content of which is incorporated into this application by reference. middle.
本申请涉及人工智能领域,尤其涉及一种模型训练方法及其装置。The present application relates to the field of artificial intelligence, and in particular, to a model training method and its device.
语言模型(language model)是指能够根据一部分给定的语义片段,预测句子中的未知词的模型。例如:给定的自然语言序列片段“华为__很不错。”,语言模型可以根据该片段生成未知的词语,如该例子中语言模型可基于给定片段生成“手机”一词,进而得到句子为“华为手机很不错。”。A language model refers to a model that can predict unknown words in a sentence based on a part of given semantic fragments. For example: given a natural language sequence fragment "Huawei __ is very good.", the language model can generate unknown words based on the fragment. For example, in this example, the language model can generate the word "mobile phone" based on the given fragment, and then obtain a sentence For "Huawei mobile phones are very good.".
语言模型的预训练是指通过海量的语言序列语料,训练对应的语言模型,使得语言模型具备预测某个位置某个语言单位出现概率的能力。Language model pre-training refers to training the corresponding language model through massive language sequence corpus, so that the language model has the ability to predict the occurrence probability of a certain language unit at a certain position.
程序合成指的是由一定的软件程序来自动合成具有指定功能或者结构的指定程序语言序列,简而言之就是由程序来生成程序,总体上分为程序合成(代码生成)以及代码补全两种。程序合成,即从无到有,可以按一定的自然语言描述或预设的功能描述,生成对应的代码。代码补全,即在已有的代码序列上文基础上,继续生成下文代码序列。Program synthesis refers to using a certain software program to automatically synthesize a specified program language sequence with specified functions or structures. In short, it is a program that generates a program. Generally speaking, it is divided into two categories: program synthesis (code generation) and code completion. kind. Program synthesis, that is, starting from scratch, can generate corresponding code according to a certain natural language description or a preset function description. Code completion, that is, based on the existing code sequence, continue to generate the following code sequence.
随着深度学习技术的发展,利用深度学习进行序列生成已经被广泛应用在自然语言序列生成的场景上,特别是预训练语言生成模型技术的出现,使得语言生成能力有了质的提升。近年来,类似生成式预训练模型(generative pre-training,GPT)、T5、BART等生成模型也开始逐渐被引入代码生成或者补全中。该技术以Transformer为基础部件,搭件解码器或者,编码-解码器模型架构,再用大量的代码数据进行训练,得到可以根据上文输出相应代码下文的能力,从而进行代码补全或者生成。With the development of deep learning technology, the use of deep learning for sequence generation has been widely used in natural language sequence generation scenarios. In particular, the emergence of pre-trained language generation model technology has led to a qualitative improvement in language generation capabilities. In recent years, generative models such as generative pre-training (GPT), T5, and BART have gradually been introduced into code generation or completion. This technology uses Transformer as the basic component, builds a decoder or encoder-decoder model architecture, and then uses a large amount of code data for training to obtain the ability to output the corresponding code context based on the above, thereby completing or generating code.
现有的技术沿用自然语言处理的生成方式,根据原始的代码进行训练,当前这种方式能够根据已有上下文进行代码的续写,但是针对函数级代码的生成这个场景,代码生成的质量还有待提升。The existing technology follows the generation method of natural language processing and trains based on the original code. Currently, this method can continue to write the code based on the existing context. However, for the scenario of function-level code generation, the quality of the code generation has yet to be improved. promote.
发明内容Contents of the invention
本申请提供了一种模型训练方法,可以降低训练难度,提高代码的生成质量。This application provides a model training method that can reduce the difficulty of training and improve the quality of code generation.
第一方面,本申请提供了一种模型训练方法,包括:获取第一文本以及第二文本,所述第一文本和所述第二文本为从原始代码文本中提取得到的;其中,所述第一文本为程序代码,所述第二文本为所述程序代码对应的属性描述;所述第一文本和所述第二文本包括已知文本以及预测文本;根据所述已知文本,通过自然语言模型,对所述预测文本对应文本位置的文本进行预测,得到文本预测结果;所述预测文本和所述文本预测结果用于更新所述自然语言模型。In a first aspect, this application provides a model training method, including: obtaining a first text and a second text, where the first text and the second text are extracted from the original code text; wherein, the The first text is the program code, and the second text is the attribute description corresponding to the program code; the first text and the second text include known text and predicted text; according to the known text, through natural A language model predicts the text corresponding to the text position of the predicted text to obtain a text prediction result; the predicted text and the text prediction result are used to update the natural language model.
在一种可能的实现中,所述属性描述包括所述程序代码功能描述或者所述程序代码的实现原理(或者称之为该程序代码的解释)。In a possible implementation, the attribute description includes a function description of the program code or an implementation principle of the program code (or it is also called an explanation of the program code).
在一种可能的实现中,原始代码文本可以为已有的程序语言语料及自然语言语料。可选的,可以通过网络(或者其他方式)获取已有的程序语言语料及自然语言语料,该语料包括混合程序语言及自然语言的代码文件、程序语言的代码文件、自然语言的文件。其中,收集的自然语言可以是任意人类语言,程序语言可以是任意编程语言,本申请并不限定。In a possible implementation, the original code text can be existing programming language corpus and natural language corpus. Optionally, existing programming language corpus and natural language corpus can be obtained through the Internet (or other methods). The corpus includes code files of mixed programming language and natural language, code files of programming language, and natural language files. The collected natural language can be any human language, and the programming language can be any programming language, which is not limited by this application.
在一种可能的实现中,所述第一文本和所述第二文本为从原始代码文本中提取得到的;其中,所述第一文本为程序代码,所述第二文本为所述程序代码对应的属性描述。第二文本可以为自然语言,且第二文本可以是任意的人类沟通时使用的语言,程序代码可以是任意编程语言,本申请并不限定。In a possible implementation, the first text and the second text are extracted from the original code text; wherein the first text is the program code, and the second text is the program code. Corresponding attribute description. The second text can be in natural language, and the second text can be in any language used for human communication, and the program code can be in any programming language, which is not limited by this application.
在一种可能的实现中,在识别出第一文本和第二文本之后,可以建立第一文本以及第二文本之间的对应关系,以形成“自然语言描述-函数实现代码”(Description-Function)的句对形式样本。可选的,可以在每组自然语言-程序语言前使用表示字符组合来表示接下来的序列为文本语言序列或者具体的代码语言序列,并在样本之后添加样本结束符,进而可以表示出第一文本和第二文本为一对样本。 In a possible implementation, after identifying the first text and the second text, a correspondence relationship between the first text and the second text can be established to form a "natural language description-function implementation code" (Description-Function code) ) sentence pair form sample. Optionally, you can use a character combination before each set of natural language-program language to indicate that the following sequence is a text language sequence or a specific code language sequence, and add a sample terminator after the sample to indicate the first The text and the second text are a pair of samples.
本申请实施例中,在构建程序合成的训练样本时,从原始的程序代码中提取代码文本和对应的属性描述并作为一对样本,相比现有技术中直接将原始的程序文件作为训练样本,帮助自然语言模型在训练时可以将代码文本和对应的属性描述之间建立关联,而无需在训练过程中从样本中学习到识别这种关联的能力,降低了训练难度,提高了模型的预测精度。In the embodiment of the present application, when constructing a program-synthesized training sample, the code text and corresponding attribute description are extracted from the original program code and used as a pair of samples. Compared with the prior art, the original program file is directly used as a training sample. , helping the natural language model to establish an association between the code text and the corresponding attribute description during training, without having to learn the ability to identify this association from samples during the training process, reducing the training difficulty and improving the prediction of the model Accuracy.
在一种可能的实现中,为了提高训练语料的质量,可以对函数级代码片段(例如代码程序)根据其代码语料的统计特性,去除代码语料中自然语言描述部分过短的语料、代码部分过长的语料或者两者长度相差过大的语料,以提升训练语料质量。In one possible implementation, in order to improve the quality of the training corpus, function-level code snippets (such as code programs) can be removed based on the statistical characteristics of the code corpus, and the corpus with too short natural language description part and the excessive code part in the code corpus can be removed. Long corpus or corpus with a large length difference between the two to improve the quality of the training corpus.
在一种可能的实现中,所述属性描述包括所述程序代码功能描述或者所述程序代码的实现原理。In a possible implementation, the attribute description includes a function description of the program code or an implementation principle of the program code.
在一种可能的实现中,所述第一文本为通过程序代码识别方法从原始代码文本中识别得到的,所述第二文本为通过属性描述识别方法从原始代码文本中识别得到的。In a possible implementation, the first text is identified from the original code text through a program code identification method, and the second text is identified from the original code text through an attribute description identification method.
在一种可能的实现中,可以识别原始代码文本中的函数代码段作为第一文本:例如,可以使用一定规则(例如可以使用代码抽象语法树(AST)等程序代码识别方法)识别原始代码文本中的函数代码段。In one possible implementation, the function code segment in the original code text can be identified as the first text: for example, the original code text can be identified using certain rules (for example, a program code identification method such as a code abstract syntax tree (AST) can be used) function code snippet in .
在一种可能的实现中,可以使用的一定的规则,例如通过注释规则,语言字符规则等属性描述识别方法进行识别抽取。In a possible implementation, certain rules can be used, such as annotation rules, language character rules and other attribute description recognition methods for identification and extraction.
示例性的,可以进行代码抽象语法树(AST)分析,对代码进行分析得到对应代码的语法树,通过语法树分析截取对应的函数级代码片段对应的自然语言描述。For example, abstract syntax tree (AST) analysis of the code can be performed, the code can be analyzed to obtain the syntax tree of the corresponding code, and the natural language description corresponding to the corresponding function-level code fragment can be intercepted through the syntax tree analysis.
示例性的,可以使用自然语言处理自动识别的方法进行代码段抽取,例如序列标注的方法,训练函数标注器标注出所有函数级代码对应的自然语言描述。For example, natural language processing automatic identification methods can be used to extract code segments, such as sequence annotation methods, and a function annotator is trained to annotate natural language descriptions corresponding to all function-level codes.
在一种可能的实现中,所述程序代码为一个或多个函数的完整代码。In a possible implementation, the program code is the complete code of one or more functions.
在一种可能的实现中,为了训练能够用于进行程序生成功能的模型(也就是本申请实施例中的自然语言模型),可以将训练样本中的部分文本单元作为已知文本,另一部分文本作为待预测的文本,并基于已知文本来预测待预测的文本所在的文本位置的文本。In a possible implementation, in order to train a model that can be used to perform program generation functions (that is, the natural language model in the embodiment of this application), some text units in the training sample can be used as known text, and the other part of the text As the text to be predicted, the text at the text position where the text to be predicted is predicted based on the known text.
由于第一文本和第二文本为不同类型的文本(第一文本为计算机编译语言,第二文本为自然语言),可以针对于第一文本和第二文本通过不同的训练目标进行文本预测过程。Since the first text and the second text are different types of text (the first text is a computer compiled language and the second text is a natural language), the text prediction process can be performed with different training objectives for the first text and the second text.
例如,针对于第一文本,可以采用CLM、MLM、MCLM以及NoLM四种训练目标中的一种进行文本预测过程。For example, for the first text, one of the four training objectives of CLM, MLM, MCLM and NoLM can be used to perform the text prediction process.
例如,针对于第二文本,可以采用CLM、MLM以及MCLM三种训练目标中的一种进行模型训练过程。For example, for the second text, one of the three training objectives of CLM, MLM and MCLM can be used to perform the model training process.
在优化自然语言描述部分时,可选CLM、MLM、MCLM、NoLM四种训练目标中的一种。在优化程序语言函数代码部分时,可选CLM、MLM、MCLM三种训练目标中的一种,注意此处可以不包括NoLM训练目标。自然语言描述部分和程序语言函数代码部分训练目标可自由组合。When optimizing the natural language description part, you can choose one of four training objectives: CLM, MLM, MCLM, and NoLM. When optimizing the program language function code part, you can choose one of the three training objectives: CLM, MLM, and MCLM. Note that the NoLM training objective does not need to be included here. The training objectives of the natural language description part and the programming language function code part can be freely combined.
在一种可能的实现中,在对所述第一文本中的文本进行预测时,可以通过第一方式,从所述第一文本中确定所述预测文本对应的文本位置;在对所述第二文本中的文本进行预测时,可以通过第二方式,从所述第二文本中确定所述预测文本对应的文本位置;所述第一方式和所述第二方式不同。In a possible implementation, when predicting the text in the first text, the text position corresponding to the predicted text may be determined from the first text in a first manner; When predicting text in two texts, the text position corresponding to the predicted text can be determined from the second text through a second method; the first method and the second method are different.
在一种可能的实现中,所述第一方式和所述第二方式分别为如下预测方式的一种:In a possible implementation, the first method and the second method are respectively one of the following prediction methods:
对所述第一文本或所述第二文本的部分或全部文本的文本位置中采样掩码位置,所述掩码位置作为所述预测文本对应的文本位置(也就是MLM);Sample mask positions from the text positions of part or all of the first text or the second text, and use the mask positions as the text positions corresponding to the predicted text (that is, MLM);
将所述已知文本的下文的文本位置作为所述预测文本对应的文本位置;其中,所述已知文本为所述自然语言模型已预测的全部文本(也就是CLM);Use the following text position of the known text as the text position corresponding to the predicted text; wherein the known text is all the texts that have been predicted by the natural language model (that is, CLM);
将所述已知文本的下文的文本位置作为所述预测文本对应的文本位置;其中,所述已知文本为所述自然语言模型已预测的全部文本中未被掩码的文本(也就是MCLM)。The following text position of the known text is used as the text position corresponding to the predicted text; wherein the known text is the unmasked text among all the texts that have been predicted by the natural language model (that is, MCLM ).
在一种可能的实现中,还可以在不对第二文本中的文本进行预测的情况下(也就是NoLM),通过如下预测方式的一种确定所述预测文本对应的文本位置: In a possible implementation, without predicting the text in the second text (that is, NoLM), the text position corresponding to the predicted text can be determined through one of the following prediction methods:
对所述第一文本的部分或全部文本的文本位置中采样掩码位置,所述掩码位置作为所述预测文本对应的文本位置(也就是MLM);Sample mask positions from the text positions of part or all of the first text, and use the mask positions as the text positions corresponding to the predicted text (that is, MLM);
将所述已知文本的下文的文本位置作为所述预测文本对应的文本位置;其中,所述已知文本为所述自然语言模型已预测的全部文本(也就是CLM);Use the following text position of the known text as the text position corresponding to the predicted text; wherein the known text is all the texts that have been predicted by the natural language model (that is, CLM);
将所述已知文本的下文的文本位置作为所述预测文本对应的文本位置;其中,所述已知文本为所述自然语言模型已预测的全部文本中未被掩码的文本(也就是MCLM)。The following text position of the known text is used as the text position corresponding to the predicted text; wherein the known text is the unmasked text among all the texts that have been predicted by the natural language model (that is, MCLM ).
关于CLM:About CLM:
在模型训练的前馈过程中,自然语言模型可以沿着预设的上下文预测方向,依次进行文本的预测,也就是说,自然语言模型已经预测出的词的全部可以作为已知文本,且已知文本用于作为位置文本的上文来进行预测文本所在文本位置的文本预测,例如,可以沿着由上文到下文的顺序(也就是从文本开头到文本末尾的顺序)进行预测,每次可以预测一个文本单元,并在下次预测时基于已经预测出的文本单元继续进行预测。In the feedforward process of model training, the natural language model can predict texts sequentially along the preset context prediction direction. In other words, all words that have been predicted by the natural language model can be used as known text, and have been The known text is used as the context of the position text to predict the text location of the text. For example, prediction can be made in the order from the context to the context (that is, from the beginning of the text to the end of the text), each time It is possible to predict a text unit and continue to predict based on the already predicted text unit in the next prediction.
关于MLM:About MLM:
在模型训练的前馈过程中,可以对文本(例如第二文本或者第一文本)中的文本单元进行掩码(例如可以为随机掩码),掩码后的文本单元可以作为预测文本,未被掩码的文本单元可以作为已知文本,自然语言模型可以基于未被掩码的文本单元(或者已经预测出的掩码后的文本单元),依次进行掩码后的文本单元所在文本位置的文本预测。In the feedforward process of model training, the text units in the text (for example, the second text or the first text) can be masked (for example, they can be randomly masked), and the masked text units can be used as predicted texts. The masked text unit can be used as known text, and the natural language model can sequentially calculate the text location of the masked text unit based on the unmasked text unit (or the predicted masked text unit). Text prediction.
关于MCLM:About MCLM:
在模型训练的前馈过程中,自然语言模型可以沿着预设的上下文预测方向,依次进行文本的预测,和CLM不同的是,在CLM中自然语言模型已经预测出的词的全部可以作为已知文本,且已知文本用于作为位置文本的上文来进行预测文本所在文本位置的文本预测,在MCLM中,可以对MCLM中自然语言模型已经预测出的词进行掩码,可以使用自然语言模型已经预测出的词中未被掩码的文本单元来进行预测文本所在文本位置的文本预测。In the feedforward process of model training, the natural language model can predict text sequentially along the preset context prediction direction. The difference from CLM is that in CLM, all words that have been predicted by the natural language model can be used as Known text, and the known text is used as the upper text of the position text to predict the text position of the predicted text. In MCLM, the words that have been predicted by the natural language model in MCLM can be masked, and natural language can be used The unmasked text units in the words that have been predicted by the model are used to predict the text location of the predicted text.
关于NoLM:About NoLM:
在模型训练的前馈过程中,可以不对第二文本进行预测,而直接将第二文本作为已知文本,并将第一文本作为训练样本,进行第一文本中文本单元的预测。In the feedforward process of model training, the second text may not be predicted, but the second text may be directly used as the known text, and the first text may be used as a training sample to predict text units in the first text.
在一种可能的实现中,可以将可选的4种训练目标(CLM、MLM、MCLM、NoLM)分别应用在“描述-函数对”(Description-Function)的自然语言描述或函数代码上,组合形成12种训练目标。In a possible implementation, the four optional training objectives (CLM, MLM, MCLM, and NoLM) can be applied to the natural language description or function code of the "Description-Function pair" (Description-Function) respectively, and the combination 12 types of training objectives are formed.
在一种可能的实现中,为了提高训练语料的质量,可以对函数级代码片段(例如代码程序)根据其代码语料的统计特性,去除代码语料中自然语言描述部分过短的语料、代码部分过长的语料或者两者长度相差过大的语料,以提升训练语料质量。In one possible implementation, in order to improve the quality of the training corpus, function-level code snippets (such as code programs) can be removed based on the statistical characteristics of the code corpus, and the corpus with too short natural language description part and the excessive code part in the code corpus can be removed. Long corpus or corpus with a large length difference between the two to improve the quality of the training corpus.
在一种可能的实现中,由于训练样本通常难以获取到,也就是训练样本(第一文本和第二文本构成的文本对)的数量较少,可以对已获取训练样本进行数据增强,来得到更多的训练样本。In a possible implementation, since training samples are usually difficult to obtain, that is, the number of training samples (text pairs composed of the first text and the second text) is small, data enhancement can be performed on the obtained training samples to obtain More training samples.
在一种可能的实现中,所述第一文本为根据从原始代码文本中提取得到的第一原始文本得到的;其中,所述第一文本为对所述第一原始文本中的部分文本单元进行修改得到的。In a possible implementation, the first text is obtained based on the first original text extracted from the original code text; wherein the first text is a partial text unit in the first original text. Obtained by modification.
在一种可能的实现中,所述修改包括:删除、增加或者替换。In a possible implementation, the modification includes: deletion, addition or replacement.
在一种可能的实现中,所述替换具体为相同语义的替换。例如可以基于反向翻译技术生成同义片段,或者可以是基于同义句生成技术生成同义片段。In a possible implementation, the replacement is specifically a replacement with the same semantics. For example, synonymous segments may be generated based on back translation technology, or synonymous segments may be generated based on synonymous sentence generation technology.
例如,可以对代码文本中的变量名进行随机替换、(循环)代码段等价替换以及随机插入无效代码段等方式,大量获得高质量自动生成的训练数据,最终提升模型性能。For example, you can randomly replace variable names in the code text, replace equivalent (loop) code segments, and randomly insert invalid code segments to obtain a large amount of high-quality automatically generated training data, ultimately improving model performance.
在一种可能的实现中,所述第二文本为根据从原始代码文本中提取得到的第二原始文本得到的;其中,所述第二文本为对所述第二原始文本中的部分文本单元进行修改得到的。In a possible implementation, the second text is obtained based on the second original text extracted from the original code text; wherein the second text is a partial text unit in the second original text. Obtained by modification.
在一种可能的实现中,所述修改包括:删除、增加或者替换。In a possible implementation, the modification includes: deletion, addition or replacement.
在一种可能的实现中,所述替换具体为相同语义的替换。例如可以基于反向翻译技术生成同义片段, 或者可以是基于同义句生成技术生成同义片段。In a possible implementation, the replacement is specifically a replacement with the same semantics. For example, synonymous fragments can be generated based on back translation technology, Alternatively, synonymous fragments may be generated based on synonymous sentence generation technology.
例如,可以通过对自然语言描述中进行同义词替换,句式变换,或者使用自然语言处理技术中的复述生成模型或者方法,数据增广方法,数据加噪方法对自然语言描述进行增广,大量获得高质量自动生成的训练数据,最终提升模型性能。For example, you can augment the natural language description by replacing synonyms, transforming sentence patterns, or using paraphrase generation models or methods in natural language processing technology, data augmentation methods, and data noise methods to obtain a large number of information. High-quality automatically generated training data ultimately improves model performance.
在一种可能的实现中,在模型训练的不同阶段,可以对训练样本进行不同的处理,例如,在模型训练初期迭代阶段可以采用上述方式得到的第一文本以及第二文本,而在模型训练的微调阶段,可以从原始代码文本中提取出适配于所述程序代码的应用领域的文本(同样包括程序代码以及对应的代码属性描述)作为训练样本。In a possible implementation, training samples can be processed differently at different stages of model training. For example, in the initial iterative stage of model training, the first text and the second text obtained in the above way can be used, and in the model training In the fine-tuning stage, text adapted to the application field of the program code (also including program code and corresponding code attribute descriptions) can be extracted from the original code text as a training sample.
在一种可能的实现中,所述第一文本或所述第二文本为从原始代码文本中提取的适配于所述程序代码的应用领域的文本。在一种可能的实现中,所述适配于所述程序代码的应用领域,包括:文本长度或者单行文本长度小于阈值的文本;其中,所述阈值与所述程序代码的应用领域有关。或者,还可以采用除了长度之外的其他维度的信息来量化和程序代码的应用领域。In a possible implementation, the first text or the second text is text extracted from the original code text and adapted to the application field of the program code. In a possible implementation, the adaptation to the application field of the program code includes: text whose length or the length of a single line of text is less than a threshold; wherein the threshold is related to the application field of the program code. Alternatively, information in other dimensions besides length can also be used to quantify and apply the program code.
在一种可能的实现中,可以基于数据样本与函数级代码生成器的应用领域的数据样本的相似度来过滤样本。相似度计算方法,可以是自然语言描述之间的长度关系,可以是程序语言函数代码之间的长度关系,也可以是其他关系。In one possible implementation, the samples may be filtered based on their similarity to data samples from the application domain of the function-level code generator. The similarity calculation method can be the length relationship between natural language descriptions, the length relationship between program language function codes, or other relationships.
第二方面,本申请提供了一种模型训练装置,包括:In the second aspect, this application provides a model training device, including:
获取模块,用于获取第一文本以及第二文本,所述第一文本和所述第二文本为从原始代码文本中提取得到的;其中,所述第一文本为程序代码,所述第二文本为所述程序代码对应的属性描述;所述第一文本和所述第二文本包括已知文本以及预测文本;An acquisition module is used to acquire a first text and a second text. The first text and the second text are extracted from the original code text; wherein the first text is program code, and the second text is extracted from the original code text. The text is an attribute description corresponding to the program code; the first text and the second text include known text and predicted text;
预测模块,用于根据所述已知文本,通过自然语言模型,对所述预测文本对应文本位置的文本进行预测,得到文本预测结果;所述预测文本和所述文本预测结果用于更新所述自然语言模型。A prediction module, configured to predict the text corresponding to the text position of the predicted text through a natural language model based on the known text, and obtain a text prediction result; the predicted text and the text prediction result are used to update the Natural language model.
在一种可能的实现中,所述属性描述包括所述程序代码功能描述或者所述程序代码的实现原理。In a possible implementation, the attribute description includes a function description of the program code or an implementation principle of the program code.
在一种可能的实现中,所述装置还包括:In a possible implementation, the device further includes:
文本位置确定模块,用于在对所述第一文本中的文本进行预测时,通过第一方式,从所述第一文本中确定所述预测文本对应的文本位置;A text position determination module, configured to determine the text position corresponding to the predicted text from the first text in a first manner when predicting the text in the first text;
在对所述第二文本中的文本进行预测时,通过第二方式,从所述第二文本中确定所述预测文本对应的文本位置;所述第一方式和所述第二方式不同。When predicting the text in the second text, the text position corresponding to the predicted text is determined from the second text through the second method; the first method and the second method are different.
在一种可能的实现中,所述第一方式和所述第二方式分别为如下预测方式的一种:In a possible implementation, the first method and the second method are respectively one of the following prediction methods:
对所述第一文本或所述第二文本的部分或全部文本的文本位置中采样掩码位置,所述掩码位置作为所述预测文本对应的文本位置;Sample mask positions among text positions of part or all of the first text or second text, and use the mask positions as the text positions corresponding to the predicted text;
将所述已知文本的下文的文本位置作为所述预测文本对应的文本位置;其中,所述已知文本为所述自然语言模型已预测的全部文本;Use the following text position of the known text as the text position corresponding to the predicted text; wherein the known text is all the text that has been predicted by the natural language model;
将所述已知文本的下文的文本位置作为所述预测文本对应的文本位置;其中,所述已知文本为所述自然语言模型已预测的全部文本中未被掩码的文本。The following text position of the known text is used as the text position corresponding to the predicted text; wherein the known text is the unmasked text among all the texts that have been predicted by the natural language model.
在一种可能的实现中,所述文本位置确定模块,还用于:在不对第二文本中的文本进行预测的情况下,通过如下预测方式的一种确定所述预测文本对应的文本位置:In a possible implementation, the text position determination module is also configured to determine the text position corresponding to the predicted text through one of the following prediction methods without predicting the text in the second text:
对所述第一文本的部分或全部文本的文本位置中采样掩码位置,所述掩码位置作为所述预测文本对应的文本位置;Sample mask positions from text positions of part or all of the first text, and use the mask positions as text positions corresponding to the predicted text;
将所述已知文本的下文的文本位置作为所述预测文本对应的文本位置;其中,所述已知文本为所述自然语言模型已预测的全部文本;Use the following text position of the known text as the text position corresponding to the predicted text; wherein the known text is all the text that has been predicted by the natural language model;
将所述已知文本的下文的文本位置作为所述预测文本对应的文本位置;其中,所述已知文本为所述自然语言模型已预测的全部文本中未被掩码的文本。The following text position of the known text is used as the text position corresponding to the predicted text; wherein the known text is the unmasked text among all the texts that have been predicted by the natural language model.
在一种可能的实现中,In one possible implementation,
所述第一文本为根据从原始代码文本中提取得到的第一原始文本得到的;其中,所述第一文本为对所述第一原始文本中的部分文本单元进行修改得到的;或者,The first text is obtained based on the first original text extracted from the original code text; wherein the first text is obtained by modifying some text units in the first original text; or,
所述第二文本为根据从原始代码文本中提取得到的第二原始文本得到的;其中,所述第二文本为对所述第二原始文本中的部分文本单元进行修改得到的。The second text is obtained based on the second original text extracted from the original code text; wherein the second text is obtained by modifying some text units in the second original text.
在一种可能的实现中,所述修改包括:删除、增加或者替换。In a possible implementation, the modification includes: deletion, addition or replacement.
在一种可能的实现中,所述替换具体为相同语义的替换。In a possible implementation, the replacement is specifically a replacement with the same semantics.
在一种可能的实现中,所述第一文本或所述第二文本为从原始代码文本中提取的适配于所述程序代码的应用领域的文本。In a possible implementation, the first text or the second text is text extracted from the original code text and adapted to the application field of the program code.
在一种可能的实现中,所述适配于所述程序代码的应用领域,包括:In a possible implementation, the application fields adapted to the program code include:
文本长度或者单行文本长度小于阈值的文本;其中,所述阈值与所述程序代码的应用领域有关。The text length or the length of a single line of text is less than a threshold; wherein the threshold is related to the application field of the program code.
第三方面,本申请实施例提供了一种模型训练装置,可以包括存储器、处理器以及总线系统,其中,存储器用于存储程序,处理器用于执行存储器中的程序,以执行如上述第一方面及其任一可选的方法。In a third aspect, embodiments of the present application provide a model training device, which may include a memory, a processor, and a bus system. The memory is used to store programs, and the processor is used to execute programs in the memory to perform the above-mentioned first aspect. and any optional methods.
第四方面,本申请实施例提供了一种计算机可读存储介质,所述计算机可读存储介质中存储有计算机程序,当其在计算机上运行时,使得计算机执行上述第一方面及其任一可选的方法。In a fourth aspect, embodiments of the present application provide a computer-readable storage medium that stores a computer program that, when run on a computer, causes the computer to execute the first aspect and any one of the above-mentioned aspects. Optional method.
第五方面,本申请实施例提供了一种计算机程序,当其在计算机上运行时,使得计算机执行上述第一方面及其任一可选的方法。In a fifth aspect, embodiments of the present application provide a computer program that, when run on a computer, causes the computer to execute the above-mentioned first aspect and any optional method thereof.
第六方面,本申请提供了一种芯片系统,该芯片系统包括处理器,用于支持执行设备或训练设备实现上述方面中所涉及的功能,例如,发送或处理上述方法中所涉及的数据;或,信息。在一种可能的设计中,所述芯片系统还包括存储器,所述存储器,用于保存执行设备或训练设备必要的程序指令和数据。该芯片系统,可以由芯片构成,也可以包括芯片和其他分立器件。In a sixth aspect, the present application provides a chip system, which includes a processor to support an execution device or a training device to implement the functions involved in the above aspects, for example, sending or processing data involved in the above methods; Or, information. In a possible design, the chip system further includes a memory, and the memory is used to store necessary program instructions and data for executing the device or training the device. The chip system may be composed of chips, or may include chips and other discrete devices.
图1A为人工智能主体框架的一种结构示意图;Figure 1A is a schematic structural diagram of the main framework of artificial intelligence;
图1B和至图1C为本发明的应用系统框架示意;Figures 1B and 1C are schematic diagrams of the application system framework of the present invention;
图1D为终端的一种可选的硬件结构示意图;Figure 1D is a schematic diagram of an optional hardware structure of the terminal;
图2为一种服务器的结构示意图;Figure 2 is a schematic structural diagram of a server;
图3至图5为本申请的一种系统架构示意;Figures 3 to 5 are schematic diagrams of a system architecture of this application;
图6为一种云服务的流程;Figure 6 shows the process of a cloud service;
图7为一种云服务的流程;Figure 7 shows the process of a cloud service;
图8为本申请的一种系统架构示意;Figure 8 is a schematic diagram of a system architecture of this application;
图9为本申请实施例提供的一种模型训练方法的流程示意;Figure 9 is a schematic flowchart of a model training method provided by an embodiment of the present application;
图10A至图10D为文本的示意;Figures 10A to 10D are diagrams of the text;
图11为本申请实施例提供的一种模型训练方法的架构示意;Figure 11 is a schematic diagram of the architecture of a model training method provided by an embodiment of the present application;
图12和图13为模型的一个示意;Figures 12 and 13 are a schematic of the model;
图14为本申请实施例提供的模型训练装置的一种结构示意图; Figure 14 is a schematic structural diagram of a model training device provided by an embodiment of the present application;
图15为本申请实施例提供的执行设备的一种结构示意图;Figure 15 is a schematic structural diagram of an execution device provided by an embodiment of the present application;
图16为本申请实施例提供的训练设备一种结构示意图;Figure 16 is a schematic structural diagram of the training equipment provided by the embodiment of the present application;
图17为本申请实施例提供的芯片的一种结构示意图。Figure 17 is a schematic structural diagram of a chip provided by an embodiment of the present application.
下面结合本发明实施例中的附图对本发明实施例进行描述。本发明的实施方式部分使用的术语仅用于对本发明的具体实施例进行解释,而非旨在限定本发明。The embodiments of the present invention will be described below with reference to the drawings in the embodiments of the present invention. The terms used in the embodiments of the present invention are only used to explain specific embodiments of the present invention and are not intended to limit the present invention.
下面结合附图,对本申请的实施例进行描述。本领域普通技术人员可知,随着技术的发展和新场景的出现,本申请实施例提供的技术方案对于类似的技术问题,同样适用。The embodiments of the present application are described below with reference to the accompanying drawings. Persons of ordinary skill in the art know that with the development of technology and the emergence of new scenarios, the technical solutions provided in the embodiments of this application are also applicable to similar technical problems.
本申请的说明书和权利要求书及上述附图中的术语“第一”、“第二”等是用于区别类似的对象,而不必用于描述特定的顺序或先后次序。应该理解这样使用的术语在适当情况下可以互换,这仅仅是描述本申请的实施例中对相同属性的对象在描述时所采用的区分方式。此外,术语“包括”和“具有”以及他们的任何变形,意图在于覆盖不排他的包含,以便包含一系列单元的过程、方法、系统、产品或设备不必限于那些单元,而是可包括没有清楚地列出的或对于这些过程、方法、产品或设备固有的其它单元。The terms "first", "second", etc. in the description and claims of this application and the above-mentioned drawings are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that the terms so used are interchangeable under appropriate circumstances, and are merely a way of distinguishing objects with the same attributes in describing the embodiments of the present application. Furthermore, the terms "include" and "having" and any variations thereof, are intended to cover non-exclusive inclusions, such that a process, method, system, product or apparatus comprising a series of elements need not be limited to those elements, but may include not explicitly other elements specifically listed or inherent to such processes, methods, products or equipment.
本文中所用用语“基本(substantially)”、“大约(about)”及类似用语用作近似用语、而并非用作程度用语,且旨在考虑到所属领域中的普通技术人员将知的测量值或计算值的固有偏差。此外,在阐述本发明实施例时使用“可(may)”是指“可能的一个或多个实施例”。本文中所用用语“使用(use)”、“正使用(using)”、及“被使用(used)”可被视为分别与用语“利用(utilize)”、“正利用(utilizing)”、及“被利用(utilized)”同义。另外,用语“示例性(exemplary)”旨在指代实例或例示。As used herein, the terms "substantially", "about" and similar terms are used as terms of approximation, not as terms of degree, and are intended to take into account measurements or values that would be known to one of ordinary skill in the art. The inherent bias in calculated values. In addition, the use of "may" when describing embodiments of the present invention refers to "one or more possible embodiments." As used herein, the terms "use", "using", and "used" may be deemed to be the same as the terms "utilize", "utilizing", and "utilize", respectively. Synonymous with "utilized". Additionally, the term "exemplary" is intended to refer to an example or illustration.
首先对人工智能系统总体工作流程进行描述,请参见图1A,图1A示出的为人工智能主体框架的一种结构示意图,下面从“智能信息链”(水平轴)和“IT价值链”(垂直轴)两个维度对上述人工智能主题框架进行阐述。其中,“智能信息链”反映从数据的获取到处理的一列过程。举例来说,可以是智能信息感知、智能信息表示与形成、智能推理、智能决策、智能执行与输出的一般过程。在这个过程中,数据经历了“数据—信息—知识—智慧”的凝练过程。“IT价值链”从人智能的底层基础设施、信息(提供和处理技术实现)到系统的产业生态过程,反映人工智能为信息技术产业带来的价值。First, the overall workflow of the artificial intelligence system is described. Please refer to Figure 1A. Figure 1A shows a structural schematic diagram of the artificial intelligence main framework. The following is from the "intelligent information chain" (horizontal axis) and "IT value chain" ( The above artificial intelligence theme framework is elaborated on the two dimensions of vertical axis). Among them, the "intelligent information chain" reflects a series of processes from data acquisition to processing. For example, it can be the general process of intelligent information perception, intelligent information representation and formation, intelligent reasoning, intelligent decision-making, intelligent execution and output. In this process, the data has gone through the condensation process of "data-information-knowledge-wisdom". The "IT value chain" reflects the value that artificial intelligence brings to the information technology industry, from the underlying infrastructure of human intelligence and information (providing and processing technology implementation) to the systematic industrial ecological process.
(1)基础设施(1)Infrastructure
基础设施为人工智能系统提供计算能力支持,实现与外部世界的沟通,并通过基础平台实现支撑。通过传感器与外部沟通;计算能力由智能芯片(CPU、NPU、GPU、ASIC、FPGA等硬件加速芯片)提供;基础平台包括分布式计算框架及网络等相关的平台保障和支持,可以包括云存储和计算、互联互通网络等。举例来说,传感器和外部沟通获取数据,这些数据提供给基础平台提供的分布式计算系统中的智能芯片进行计算。Infrastructure provides computing power support for artificial intelligence systems, enables communication with the external world, and supports it through basic platforms. Communicate with the outside through sensors; computing power is provided by smart chips (hardware acceleration chips such as CPU, NPU, GPU, ASIC, FPGA, etc.); the basic platform includes distributed computing framework and network and other related platform guarantees and support, which can include cloud storage and Computing, interconnection networks, etc. For example, sensors communicate with the outside world to obtain data, which are provided to smart chips in the distributed computing system provided by the basic platform for calculation.
(2)数据(2)Data
基础设施的上一层的数据用于表示人工智能领域的数据来源。数据涉及到图形、图像、语音、文本,还涉及到传统设备的物联网数据,包括已有系统的业务数据以及力、位移、液位、温度、湿度等感知数据。Data from the upper layer of the infrastructure is used to represent data sources in the field of artificial intelligence. The data involves graphics, images, voice, and text, as well as IoT data of traditional devices, including business data of existing systems and sensory data such as force, displacement, liquid level, temperature, and humidity.
(3)数据处理(3)Data processing
数据处理通常包括数据训练,机器学习,深度学习,搜索,推理,决策等方式。Data processing usually includes data training, machine learning, deep learning, search, reasoning, decision-making and other methods.
其中,机器学习和深度学习可以对数据进行符号化和形式化的智能信息建模、抽取、预处理、训练等。Among them, machine learning and deep learning can perform symbolic and formal intelligent information modeling, extraction, preprocessing, training, etc. on data.
推理是指在计算机或智能系统中,模拟人类的智能推理方式,依据推理控制策略,利用形式化的信息进行机器思维和求解问题的过程,典型的功能是搜索与匹配。Reasoning refers to the process of simulating human intelligent reasoning in computers or intelligent systems, using formal information to perform machine thinking and problem solving based on reasoning control strategies. Typical functions are search and matching.
决策是指智能信息经过推理后进行决策的过程,通常提供分类、排序、预测等功能。Decision-making refers to the process of making decisions after intelligent information is reasoned, and usually provides functions such as classification, sorting, and prediction.
(4)通用能力(4) General ability
对数据经过上面提到的数据处理后,进一步基于数据处理的结果可以形成一些通用的能力,比如可以是算法或者一个通用系统,例如,翻译,文本的分析,计算机视觉的处理,语音识别,图像的识别等等。After the data is processed as mentioned above, some general capabilities can be formed based on the results of further data processing, such as algorithms or a general system, such as translation, text analysis, computer vision processing, speech recognition, and image processing. identification, etc.
(5)智能产品及行业应用(5) Intelligent products and industry applications
智能产品及行业应用指人工智能系统在各领域的产品和应用,是对人工智能整体解决方案的封装,将智能信息决策产品化、实现落地应用,其应用领域主要包括:智能终端、智能交通、智能医疗、自动驾驶、智慧城市等。Intelligent products and industry applications refer to the products and applications of artificial intelligence systems in various fields. They are the encapsulation of overall artificial intelligence solutions, productizing intelligent information decision-making and realizing practical applications. Its application fields mainly include: intelligent terminals, intelligent transportation, Smart healthcare, autonomous driving, smart cities, etc.
本申请可以应用于人工智能领域的自然语言处理领域中,下面以自然语言处理为例将对多个落地到产品的多个应用场景进行介绍。This application can be applied to the field of natural language processing in the field of artificial intelligence. Taking natural language processing as an example, multiple application scenarios that have been implemented into products will be introduced below.
首先介绍本申请的应用场景,本申请可以但不限于应用在包含基于代码的属性描述进行的程序合成功能或者代码补全功能的应用程序(以下可以简称为程序合成类应用程序)或者云侧服务器提供的云服务等,接下来分别进行介绍:First, the application scenarios of this application are introduced. This application can be, but is not limited to, applied to applications that include program synthesis functions or code completion functions based on code attribute descriptions (hereinafter may be referred to as program synthesis applications for short) or cloud-side servers. The cloud services provided are introduced below:
一、程序合成类应用程序1. Program synthesis applications
本申请实施例的产品形态可以为程序合成类应用程序。程序合成类应用程序可以运行在终端设备或者云侧的服务器上。The product form of the embodiment of the present application may be a program synthesis application program. Program synthesis applications can run on terminal devices or cloud-side servers.
在一种可能的实现中,程序合成类应用程序可以实现基于代码的属性描述(或者可以称之为提示)进行的程序合成或者代码补全的任务,其中,程序合成类应用程序可以响应于输入的代码的属性描述(或者可以称之为提示)而执行程序合成或者代码补全的任务,得到预测文本(也就是生成的代码程序),生成的代码程序可以符合代码的属性描述。In a possible implementation, the program synthesis application can implement program synthesis or code completion tasks based on code attribute descriptions (or prompts), where the program synthesis application can respond to input The code attribute description (or can be called a prompt) is used to perform the task of program synthesis or code completion, and the predicted text (that is, the generated code program) is obtained. The generated code program can conform to the code attribute description.
在一种可能的实现中,用户可以打开终端设备上安装的程序合成类应用程序,并输入代码的属性描述(或者可以称之为提示),程序合成类应用程序可以通过本申请实施例提供的方法训练得到的自然语言模型对代码的属性描述进行程序合成或者代码补全,并将预测文本(也就是生成的代码程序)呈现给用户(呈现方式可以但不限于是显示、保存、上传到云侧等)。In a possible implementation, the user can open a program synthesis application installed on the terminal device and enter an attribute description of the code (or it can be called a prompt). The program synthesis application can use the program provided by the embodiment of the present application. The natural language model trained by the method performs program synthesis or code completion on the attribute description of the code, and presents the predicted text (that is, the generated code program) to the user (the presentation method can be, but is not limited to, displaying, saving, or uploading to the cloud side, etc.).
在一种可能的实现中,用户可以打开终端设备上安装的程序合成类应用程序,并输入代码的属性描述,程序合成类应用程序可以将代码的属性描述发送至云侧的服务器,云侧的服务器通过本申请实施例提供的方法训练得到的自然语言模型对代码的属性描述进行程序合成或者代码补全,并将预测文本(也就是生成的代码程序)回传至终端设备,终端设备可以将预测文本(也就是生成的代码程序)呈现给用户(呈现方式可以但不限于是显示、保存、上传到云侧等)。In one possible implementation, the user can open a program synthesis application installed on the terminal device and enter the attribute description of the code. The program synthesis application can send the attribute description of the code to the server on the cloud side. The server performs program synthesis or code completion on the attribute description of the code using the natural language model trained by the method provided in the embodiments of this application, and sends the predicted text (that is, the generated code program) back to the terminal device, and the terminal device can The predicted text (that is, the generated code program) is presented to the user (the presentation method can be but is not limited to display, saving, uploading to the cloud, etc.).
接下来分别从功能架构以及实现功能的产品架构介绍本申请实施例中的程序合成类应用程序。Next, the program synthesis application in the embodiment of this application will be introduced from the perspective of functional architecture and product architecture that implements the functions.
参照图1B,图1B为本申请实施例中程序合成类应用程序的功能架构示意:Referring to Figure 1B, Figure 1B is a schematic diagram of the functional architecture of a program synthesis application in an embodiment of the present application:
在一种可能的实现中,如图1B所示,程序合成类应用程序102可接收输入的参数101(例如包含代码的属性描述)且产生预测文本(也就是生成的代码程序)103。程序合成类应用程序102可在(举例来说)至少一个计算机系统上执行,且包括计算机代码,所述计算机代码在由一或多个计算机执行时致使所述计算机执行用于执行通过本申请实施例提供的方法训练得到的自然语言模型。In a possible implementation, as shown in FIG. 1B , the program synthesis application 102 can receive input parameters 101 (for example, attribute descriptions containing codes) and generate predictive text (that is, a generated code program) 103 . Program synthesis application 102 may be executed, for example, on at least one computer system and may include computer code that, when executed by one or more computers, causes the computers to perform operations performed by the present application. The natural language model trained by the method provided in the example.
参照图1C,图1C为本申请实施例中运行程序合成类应用程序的实体架构示意:Referring to Figure 1C, Figure 1C is a schematic diagram of the entity architecture of running a program synthesis application in an embodiment of the present application:
参见图1C,图1C示出了一种系统架构示意图。该系统可以包括终端100、以及服务器200。其中,服务器200可以包括一个或者多个服务器(图1C中以包括一个服务器作为示例进行说明),服务器200可以为一个或者多个终端提供程序合成功能服务。Referring to Figure 1C, Figure 1C shows a schematic diagram of a system architecture. The system may include a terminal 100 and a server 200. The server 200 may include one or more servers (one server is used as an example for illustration in FIG. 1C), and the server 200 may provide program synthesis function services for one or more terminals.
其中,终端100上可以安装有程序合成类应用程序,或者打开与程序合成功能相关的网页,上述应用程序和网页可以提供一个界面,终端100可以接收用户在程序合成功能界面上输入的相关参数,并将上述参数发送至服务器200,服务器200可以基于接收到的参数,得到处理结果,并将处理结果返回至至终端100。Among them, the terminal 100 can be installed with a program synthesis application, or open a web page related to the program synthesis function. The above-mentioned application program and web page can provide an interface, and the terminal 100 can receive relevant parameters input by the user on the program synthesis function interface. The above parameters are sent to the server 200. The server 200 can obtain the processing results based on the received parameters and return the processing results to the terminal 100.
应理解,在一些可选的实现中,终端100也可以由自身完成基于接收到的参数,得到处理结果的动作,而不需要服务器配合实现,本申请实施例并不限定。It should be understood that in some optional implementations, the terminal 100 can also complete the action of obtaining the processing result based on the received parameters by itself without requiring the cooperation of the server, which is not limited by the embodiments of this application.
接下来描述图1C中终端100的产品形态;Next, the product form of the terminal 100 in Figure 1C is described;
本申请实施例中的终端100可以为手机、平板电脑、可穿戴设备、车载设备、增强现实(augmented reality,AR)/虚拟现实(virtual reality,VR)设备、笔记本电脑、超级移动个人计算机(ultra-mobile personal computer,UMPC)、上网本、个人数字助理(personal digital assistant,PDA)等,本申请实施例对此不作任何限制。The terminal 100 in the embodiment of the present application can be a mobile phone, a tablet computer, a wearable device, a vehicle-mounted device, an augmented reality (AR)/virtual reality (VR) device, a notebook computer, or an ultra mobile personal computer (ultra mobile personal computer). - mobile personal computer (UMPC), netbook, personal digital assistant (personal digital assistant, PDA), etc., the embodiments of this application do not impose any restrictions on this.
图1D示出了终端100的一种可选的硬件结构示意图。FIG. 1D shows an optional hardware structure diagram of the terminal 100.
参考图1D所示,终端100可以包括射频单元110、存储器120、输入单元130、显示单元140、摄像头150(可选的)、音频电路160(可选的)、扬声器161(可选的)、麦克风162(可选的)、处理器170、外部接口180、电源190等部件。本领域技术人员可以理解,图1D仅仅是终端或多功能设备的举例,并不构成对终端或多功能设备的限定,可以包括比图示更多或更少的部件,或者组合某些部件,或者不同的部件。Referring to FIG. 1D , the terminal 100 may include a radio frequency unit 110, a memory 120, an input unit 130, a display unit 140, a camera 150 (optional), an audio circuit 160 (optional), a speaker 161 (optional), Microphone 162 (optional), processor 170, external interface 180, power supply 190 and other components. Those skilled in the art can understand that Figure 1D is only an example of a terminal or a multi-function device, and does not constitute a limitation to the terminal or multi-function device. It may include more or fewer components than shown in the figure, or combine certain components. Or different parts.
输入单元130可用于接收输入的数字或字符信息,以及产生与该便携式多功能装置的用户设置以及功能控制有关的键信号输入。具体地,输入单元130可包括触摸屏131(可选的)和/或其他输入设备132。该触摸屏131可收集用户在其上或附近的触摸操作(比如用户使用手指、关节、触笔等任何适合的物体在触摸屏上或在触摸屏附近的操作),并根据预先设定的程序驱动相应的连接装置。触摸屏可以检测用户对触摸屏的触摸动作,将该触摸动作转换为触摸信号发送给该处理器170,并能接收该处理器170发来的命令并加以执行;该触摸信号至少包括触点坐标信息。该触摸屏131可以提供该终端100和用户之间的输入界面和输出界面。此外,可以采用电阻式、电容式、红外线以及表面声波等多种类型实现触摸屏。除了触摸屏131,输入单元130还可以包括其他输入设备。具体地,其他输入设备132可以包括但不限于物理键盘、功能键(比如音量控制按键132、开关按键133等)、轨迹球、鼠标、操作杆等中的一种或多种。The input unit 130 may be used to receive input numeric or character information and generate key signal input related to user settings and function control of the portable multi-function device. Specifically, the input unit 130 may include a touch screen 131 (optional) and/or other input devices 132. The touch screen 131 can collect the user's touch operations on or near it (such as the user's operations on or near the touch screen using fingers, knuckles, stylus, or any other suitable objects), and drive the corresponding according to the preset program. Connect the device. The touch screen can detect the user's touch action on the touch screen, convert the touch action into a touch signal and send it to the processor 170, and can receive and execute commands from the processor 170; the touch signal at least includes contact point coordinate information. The touch screen 131 can provide an input interface and an output interface between the terminal 100 and the user. In addition, touch screens can be implemented in various types such as resistive, capacitive, infrared, and surface acoustic wave. In addition to the touch screen 131, the input unit 130 may also include other input devices. Specifically, other input devices 132 may include, but are not limited to, one or more of a physical keyboard, function keys (such as volume control keys 132, switch keys 133, etc.), trackball, mouse, joystick, etc.
其中,输入设备132可以接收到输入的代码的属性描述等等。The input device 132 may receive an attribute description of the input code and the like.
该显示单元140可用于显示由用户输入的信息或提供给用户的信息、终端100的各种菜单、交互界面、文件显示和/或任意一种多媒体文件的播放。在本申请实施例中,显示单元140可用于显示程序合成类应用程序的界面、生成的预测文本(也就是生成的代码程序)等。The display unit 140 may be used to display information input by the user or information provided to the user, various menus of the terminal 100, interactive interfaces, file display, and/or playback of any kind of multimedia files. In this embodiment of the present application, the display unit 140 may be used to display the interface of a program synthesis application, the generated predictive text (that is, the generated code program), etc.
该存储器120可用于存储指令和数据,存储器120可主要包括存储指令区和存储数据区,存储数据区可存储各种数据,如多媒体文件、文本等;存储指令区可存储操作系统、应用、至少一个功能所需的指令等软件单元,或者他们的子集、扩展集。还可以包括非易失性随机存储器;向处理器170提供包括管理计算处理设备中的硬件、软件以及数据资源,支持控制软件和应用。还用于多媒体文件的存储,以及运行程序和应用的存储。The memory 120 can be used to store instructions and data. The memory 120 can mainly include a storage instruction area and a storage data area. The storage data area can store various data, such as multimedia files, text, etc.; the storage instruction area can store operating systems, applications, at least Software units such as instructions required for a function, or their subsets or extensions. Non-volatile random access memory may also be included; providing the processor 170 with management of hardware, software and data resources in the computing processing device and supporting control software and applications. It is also used for storage of multimedia files, as well as storage of running programs and applications.
处理器170是终端100的控制中心,利用各种接口和线路连接整个终端100的各个部分,通过运行或执行存储在存储器120内的指令以及调用存储在存储器120内的数据,执行终端100的各种功能和处理数据,从而对终端设备进行整体控制。可选的,处理器170可包括一个或多个处理单元;优选的,处理器170可集成应用处理器和调制解调处理器,其中,应用处理器主要处理操作系统、用户界面和应用程序等,调制解调处理器主要处理无线通信。可以理解的是,上述调制解调处理器也可以不集成到处理器170中。在一些实施例中,处理器、存储器、可以在单一芯片上实现,在一些实施例中,他们也可以在独立的芯片上分别实现。处理器170还可以用于产生相应的操作控制信号,发给计算处理设备相应的部件,读取以及处理软件中的数据,尤其是读取和处理存储器120中的数据和程序,以使其中的各个功能模块执行相应的功能,从而控制相应的部件按指令的要求进行动作。The processor 170 is the control center of the terminal 100. It uses various interfaces and lines to connect various parts of the entire terminal 100, and executes various functions of the terminal 100 by running or executing instructions stored in the memory 120 and calling data stored in the memory 120. functions and process data to provide overall control of the terminal device. Optionally, the processor 170 may include one or more processing units; preferably, the processor 170 may integrate an application processor and a modem processor, where the application processor mainly processes operating systems, user interfaces, application programs, etc. , the modem processor mainly handles wireless communications. It can be understood that the above-mentioned modem processor may not be integrated into the processor 170 . In some embodiments, the processor and memory can be implemented on a single chip, and in some embodiments, they can also be implemented on separate chips. The processor 170 can also be used to generate corresponding operation control signals, send them to corresponding components of the computing processing device, read and process data in the software, especially read and process the data and programs in the memory 120, so that the Each functional module performs a corresponding function, thereby controlling the corresponding components to act according to the instructions.
其中,存储器120可以用于存储数据处理方法相关的软件代码,处理器170可以执行芯片的数据处理方法的步骤,也可以调度其他单元(例如上述输入单元130以及显示单元140)以实现相应的功能。The memory 120 can be used to store software codes related to the data processing method, and the processor 170 can execute the steps of the data processing method of the chip, and can also schedule other units (such as the above-mentioned input unit 130 and the display unit 140) to implement corresponding functions. .
该射频单元110(可选的)可用于收发信息或通话过程中信号的接收和发送,例如,将基站的下行信息接收后,给处理器170处理;另外,将设计上行的数据发送给基站。通常,RF电路包括但不限于天线、至少一个放大器、收发信机、耦合器、低噪声放大器(Low Noise Amplifier,LNA)、双工器等。此外,射频单元110还可以通过无线通信与网络设备和其他设备通信。该无线通信可以使用任一通信标准或协议,包括但不限于全球移动通讯系统(Global System of Mobile communication,GSM)、通用分组无线服务(General Packet Radio Service,GPRS)、码分多址(Code Division Multiple Access,CDMA)、宽带码分多址(Wideband Code Division Multiple Access,WCDMA)、长期演进(Long Term Evolution,LTE)、电子邮件、短消息服务(Short Messaging Service,SMS)等。The radio frequency unit 110 (optional) can be used to send and receive information or receive and send signals during calls. For example, after receiving downlink information from the base station, it is processed by the processor 170; in addition, the designed uplink data is sent to the base station. Typically, RF circuits include but are not limited to antennas, at least one amplifier, transceivers, couplers, low noise amplifiers (LNA), duplexers, etc. In addition, the radio frequency unit 110 can also communicate with network devices and other devices through wireless communication. The wireless communication can use any communication standard or protocol, including but not limited to Global System of Mobile communication (GSM), General Packet Radio Service (GPRS), Code Division Multiple Access (Code Division) Multiple Access (CDMA), Wideband Code Division Multiple Access (WCDMA), Long Term Evolution (LTE), email, Short Messaging Service (SMS), etc.
其中,在本申请实施例中,该射频单元110可以将代码的属性描述发送至服务器200,并接收到服务器200发送的预测文本(也就是生成的代码程序)。In this embodiment of the present application, the radio frequency unit 110 can send the attribute description of the code to the server 200, and receive the predicted text (that is, the generated code program) sent by the server 200.
应理解,该射频单元110为可选的,其可以被替换为其他通信接口,例如可以是网口。It should be understood that the radio frequency unit 110 is optional and can be replaced by other communication interfaces, such as a network port.
终端100还包括给各个部件供电的电源190(比如电池),优选的,电源可以通过电源管理系统与处理器170逻辑相连,从而通过电源管理系统实现管理充电、放电、以及功耗管理等功能。The terminal 100 also includes a power supply 190 (such as a battery) that supplies power to various components. Preferably, the power supply can be logically connected to the processor 170 through a power management system, so that functions such as charging, discharging, and power consumption management can be implemented through the power management system.
终端100还包括外部接口180,该外部接口可以是标准的Micro USB接口,也可以使多针连接器,可以用于连接终端100与其他装置进行通信,也可以用于连接充电器为终端100充电。The terminal 100 also includes an external interface 180, which can be a standard Micro USB interface or a multi-pin connector, which can be used to connect the terminal 100 to communicate with other devices, or can be used to connect a charger to charge the terminal 100. .
尽管未示出,终端100还可以包括闪光灯、无线保真(wireless fidelity,WiFi)模块、蓝牙模块、不同功能的传感器等,在此不再赘述。下文中描述的部分或全部方法均可以应用在如图1D所示的终端100中。Although not shown, the terminal 100 may also include a flash light, a wireless fidelity (WiFi) module, a Bluetooth module, sensors with different functions, etc., which will not be described again here. Some or all of the methods described below may be applied in the terminal 100 as shown in FIG. 1D.
接下来描述图1C中服务器200的产品形态;Next, the product form of the server 200 in Figure 1C will be described;
图2提供了一种服务器200的结构示意图,如图2所示,服务器200包括总线201、处理器202、通信接口203和存储器204。处理器202、存储器204和通信接口203之间通过总线201通信。Figure 2 provides a schematic structural diagram of a server 200. As shown in Figure 2, the server 200 includes a bus 201, a processor 202, a communication interface 203 and a memory 204. The processor 202, the memory 204 and the communication interface 203 communicate through the bus 201.
总线201可以是外设部件互连标准(peripheral component interconnect,PCI)总线或扩展工业标准结构(extended industry standard architecture,EISA)总线等。总线可以分为地址总线、数据总线、控制总线等。为便于表示,图2中仅用一条粗线表示,但并不表示仅有一根总线或一种类型的总线。The bus 201 may be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus, etc. The bus can be divided into address bus, data bus, control bus, etc. For ease of presentation, only one thick line is used in Figure 2, but it does not mean that there is only one bus or one type of bus.
处理器202可以为中央处理器(central processing unit,CPU)、图形处理器(graphics processing unit,GPU)、微处理器(micro processor,MP)或者数字信号处理器(digital signal processor,DSP)等处理器中的任意一种或多种。The processor 202 may be a central processing unit (CPU), a graphics processing unit (GPU), a microprocessor (MP) or a digital signal processor (DSP). any one or more of them.
存储器204可以包括易失性存储器(volatile memory),例如随机存取存储器(random access memory,RAM)。存储器204还可以包括非易失性存储器(non-volatile memory),例如只读存储器(read-only memory,ROM),快闪存储器,机械硬盘(hard drive drive,HDD)或固态硬盘(solid state drive,SSD)。Memory 204 may include volatile memory, such as random access memory (RAM). The memory 204 may also include non-volatile memory (non-volatile memory), such as read-only memory (ROM), flash memory, mechanical hard drive (hard drive drive, HDD) or solid state drive (solid state drive). ,SSD).
其中,存储器204可以用于存储数据处理方法相关的软件代码,处理器202可以执行芯片的数据处理方法的步骤,也可以调度其他单元以实现相应的功能。Among them, the memory 204 can be used to store software codes related to the data processing method, and the processor 202 can execute the steps of the data processing method of the chip, and can also schedule other units to implement corresponding functions.
应理解,上述终端100和服务器200可以为集中式或者是分布式的设备,上述终端100和服务器200中的处理器(例如处理器170以及处理器202)可以为硬件电路(如专用集成电路(application specific integrated circuit,ASIC)、现场可编程门阵列(field-programmable gate array,FPGA)、通用处理器、数字信号处理器(digital signal processing,DSP)、微处理器或微控制器等等)、或这些硬件电路的组合,例如,处理器可以为具有执行指令功能的硬件系统,如CPU、DSP等,或者为不具有执行指令功能的硬件系统,如ASIC、FPGA等,或者为上述不具有执行指令功能的硬件系统以及具有执行指令功能的硬件系统的组合。It should be understood that the terminal 100 and the server 200 may be centralized or distributed devices, and the processors (such as the processor 170 and the processor 202) in the terminal 100 and the server 200 may be hardware circuits (such as application specific integrated circuits) application specific integrated circuit (ASIC), field-programmable gate array (FPGA), general-purpose processor, digital signal processing (DSP), microprocessor or microcontroller, etc.), Or a combination of these hardware circuits. For example, the processor can be a hardware system with the function of executing instructions, such as CPU, DSP, etc., or a hardware system without the function of executing instructions, such as ASIC, FPGA, etc., or the above-mentioned processor without the function of executing instructions. A combination of a hardware system with the instruction function and a hardware system with the function of executing the instruction.
应理解,本申请实施例中的和模型推理过程相关的步骤涉及AI相关的运算,在执行AI运算时,终端设备和服务器的指令执行架构不仅仅局限在上述介绍的处理器结合存储器的架构。下面结合图5对本申请实施例提供的系统架构进行详细的介绍。It should be understood that the steps related to the model reasoning process in the embodiments of this application involve AI-related operations. When performing AI operations, the instruction execution architecture of the terminal device and server is not limited to the processor-memory architecture introduced above. The system architecture provided by the embodiment of the present application will be introduced in detail below with reference to Figure 5 .
图5为本申请实施例提供的系统架构示意图。如图5所示,系统架构500包括执行设备510、训练设备520、数据库530、客户设备540、数据存储系统550以及数据采集系统560。Figure 5 is a schematic diagram of the system architecture provided by the embodiment of the present application. As shown in Figure 5, the system architecture 500 includes an execution device 510, a training device 520, a database 530, a client device 540, a data storage system 550 and a data collection system 560.
执行设备510包括计算模块511、I/O接口512、预处理模块513和预处理模块514。计算模块511中可以包括目标模型/规则501,预处理模块513和预处理模块514是可选的。The execution device 510 includes a computing module 511, an I/O interface 512, a preprocessing module 513 and a preprocessing module 514. The target model/rule 501 may be included in the calculation module 511, and the preprocessing module 513 and the preprocessing module 514 are optional.
其中,执行设备510可以为上述运行程序合成类应用程序的终端设备或者服务器。The execution device 510 may be the above-mentioned terminal device or server that runs the program synthesis application.
数据采集设备560用于采集训练样本。训练样本可以为程序文件(包括程序代码以及程序代码的属性描述)等。在采集到训练样本之后,数据采集设备560将这些训练样本存入数据库530。Data collection device 560 is used to collect training samples. The training sample can be a program file (including program code and attribute description of the program code), etc. After collecting the training samples, the data collection device 560 stores the training samples into the database 530 .
训练设备520可以基于数据库530中维护训练样本,对待训练的神经网络(例如本申请实施例中的自然语言模型等),以得到目标模型/规则501。 The training device 520 can maintain the training samples in the database 530 and the neural network to be trained (such as the natural language model in the embodiment of the present application, etc.) to obtain the target model/rule 501.
需要说明的是,在实际应用中,数据库530中维护的训练样本不一定都来自于数据采集设备560的采集,也有可能是从其他设备接收得到的。另外需要说明的是,训练设备520也不一定完全基于数据库530维护的训练样本进行目标模型/规则501的训练,也有可能从云端或其他地方获取训练样本进行模型训练,上述描述不应该作为对本申请实施例的限定。It should be noted that in actual applications, the training samples maintained in the database 530 are not necessarily collected from the data collection device 560, and may also be received from other devices. In addition, it should be noted that the training device 520 may not necessarily train the target model/rules 501 based entirely on the training samples maintained by the database 530. It may also obtain training samples from the cloud or other places for model training. The above description should not be used as a guarantee for this application. Limitations of Examples.
根据训练设备520训练得到的目标模型/规则501可以应用于不同的系统或设备中,如应用于图5所示的执行设备510,该执行设备510可以是终端,如手机终端,平板电脑,笔记本电脑,增强现实(augmented reality,AR)/虚拟现实(virtual reality,VR)设备,车载终端等,还可以是服务器等。The target model/rules 501 trained according to the training device 520 can be applied to different systems or devices, such as the execution device 510 shown in Figure 5. The execution device 510 can be a terminal, such as a mobile phone terminal, a tablet computer, and a notebook. Computers, augmented reality (AR)/virtual reality (VR) equipment, vehicle-mounted terminals, etc., and can also be servers, etc.
具体的,训练设备520可以将训练后的模型传递至执行设备510。Specifically, the training device 520 can transfer the trained model to the execution device 510 .
在图5中,执行设备510配置输入/输出(input/output,I/O)接口512,用于与外部设备进行数据交互,用户可以通过客户设备540向I/O接口512输入数据(例如本申请实施例中代码的属性描述等)。In Figure 5, the execution device 510 is configured with an input/output (I/O) interface 512 for data interaction with external devices. The user can input data to the I/O interface 512 through the client device 540 (for example, this Attribute description of the code in the application embodiment, etc.).
预处理模块513和预处理模块514用于根据I/O接口512接收到的输入数据进行预处理。应理解,可以没有预处理模块513和预处理模块514或者只有的一个预处理模块。当不存在预处理模块513和预处理模块514时,可以直接采用计算模块511对输入数据进行处理。The preprocessing module 513 and the preprocessing module 514 are used to perform preprocessing according to the input data received by the I/O interface 512. It should be understood that there may be no preprocessing module 513 and 514 or only one preprocessing module. When the preprocessing module 513 and the preprocessing module 514 do not exist, the computing module 511 can be directly used to process the input data.
在执行设备510对输入数据进行预处理,或者在执行设备510的计算模块511执行计算等相关的处理过程中,执行设备510可以调用数据存储系统550中的数据、代码等以用于相应的处理,也可以将相应处理得到的数据、指令等存入数据存储系统550中。When the execution device 510 preprocesses input data, or when the calculation module 511 of the execution device 510 performs calculations and other related processes, the execution device 510 can call data, codes, etc. in the data storage system 550 for corresponding processing. , the data, instructions, etc. obtained by corresponding processing can also be stored in the data storage system 550.
最后,I/O接口512将处理结果(例如预测文本(也就是生成的代码程序)等)提供给客户设备540,从而提供给用户。Finally, the I/O interface 512 provides the processing results (such as predictive text (that is, the generated code program), etc.) to the client device 540 and thereby to the user.
在图5所示情况下,用户可以手动给定输入数据,该“手动给定输入数据”可以通过I/O接口512提供的界面进行操作。另一种情况下,客户设备540可以自动地向I/O接口512发送输入数据,如果要求客户设备540自动发送输入数据需要获得用户的授权,则用户可以在客户设备540中设置相应权限。用户可以在客户设备540查看执行设备510输出的结果,具体的呈现形式可以是显示、声音、动作等具体方式。客户设备540也可以作为数据采集端,采集如图所示输入I/O接口512的输入数据及输出I/O接口512的输出结果作为新的样本数据,并存入数据库530。当然,也可以不经过客户设备540进行采集,而是由I/O接口512直接将如图所示输入I/O接口512的输入数据及输出I/O接口512的输出结果,作为新的样本数据存入数据库530。In the situation shown in FIG. 5 , the user can manually set the input data, and the "manually set input data" can be operated through the interface provided by the I/O interface 512 . In another case, the client device 540 can automatically send input data to the I/O interface 512. If requiring the client device 540 to automatically send the input data requires the user's authorization, the user can set corresponding permissions in the client device 540. The user can view the results output by the execution device 510 on the client device 540, and the specific presentation form may be display, sound, action, etc. The client device 540 can also be used as a data collection terminal to collect the input data of the input I/O interface 512 and the output results of the output I/O interface 512 as new sample data, and store them in the database 530. Of course, it is also possible to collect without going through the client device 540. Instead, the I/O interface 512 directly uses the input data input to the I/O interface 512 and the output result of the output I/O interface 512 as a new sample as shown in the figure. The data is stored in database 530.
值得注意的是,图5仅是本申请实施例提供的一种系统架构的示意图,图中所示设备、器件、模块等之间的位置关系不构成任何限制,例如,在图5中,数据存储系统550相对执行设备510是外部存储器,在其它情况下,也可以将数据存储系统550置于执行设备510中。应理解,上述执行设备510可以部署于客户设备540中。It is worth noting that Figure 5 is only a schematic diagram of a system architecture provided by an embodiment of the present application, and the positional relationship between the devices, devices, modules, etc. shown in the figure does not constitute any limitation. For example, in Figure 5, the data The storage system 550 is an external memory relative to the execution device 510. In other cases, the data storage system 550 can also be placed in the execution device 510. It should be understood that the above execution device 510 may be deployed in the client device 540.
从模型的推理侧来说:From the inference side of the model:
本申请实施例中,上述执行设备520的计算模块511可以获取到数据存储系统550中存储的代码来实现本申请实施例中的和模型推理过程相关的步骤。In this embodiment of the present application, the computing module 511 of the above-mentioned execution device 520 can obtain the code stored in the data storage system 550 to implement the steps related to the model inference process in the embodiment of this application.
本申请实施例中,执行设备520的计算模块511可以包括硬件电路(如专用集成电路(application specific integrated circuit,ASIC)、现场可编程门阵列(field-programmable gate array,FPGA)、通用处理器、数字信号处理器(digital signal processing,DSP)、微处理器或微控制器等等)、或这些硬件电路的组合,例如,训练设备520可以为具有执行指令功能的硬件系统,如CPU、DSP等,或者为不具有执行指令功能的硬件系统,如ASIC、FPGA等,或者为上述不具有执行指令功能的硬件系统以及具有执行指令功能的硬件系统的组合。In the embodiment of the present application, the computing module 511 of the execution device 520 may include hardware circuits (such as application specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), general-purpose processors, digital signal processing (DSP), microprocessor or microcontroller, etc.), or a combination of these hardware circuits. For example, the training device 520 can be a hardware system with the function of executing instructions, such as a CPU, DSP, etc. , or it is a hardware system that does not have the function of executing instructions, such as ASIC, FPGA, etc., or it is a combination of the above-mentioned hardware system that does not have the function of executing instructions and a hardware system that has the function of executing instructions.
具体的,执行设备520的计算模块511可以为具有执行指令功能的硬件系统,本申请实施例提供的和模型推理过程相关的步骤可以为存储在存储器中的软件代码,执行设备520的计算模块511可以从存储器中获取到软件代码,并执行获取到的软件代码来实现本申请实施例提供的和模型推理过程相关的步骤。Specifically, the calculation module 511 of the execution device 520 can be a hardware system with the function of executing instructions. The steps related to the model reasoning process provided by the embodiment of the present application can be software codes stored in the memory. The calculation module 511 of the execution device 520 The software code can be obtained from the memory, and the obtained software code can be executed to implement the steps related to the model inference process provided by the embodiments of the present application.
应理解,执行设备520的计算模块511可以为不具有执行指令功能的硬件系统以及具有执行指令功能的硬件系统的组合,本申请实施例提供的和模型推理过程相关的步骤的部分步骤还可以通过执行设备520的计算模块511中不具有执行指令功能的硬件系统来实现,这里并不限定。It should be understood that the computing module 511 of the execution device 520 can be a combination of a hardware system that does not have the function of executing instructions and a hardware system that has the function of executing instructions. Some of the steps related to the model inference process provided by the embodiments of the present application can also be implemented by The computing module 511 of the execution device 520 is implemented by a hardware system that does not have the function of executing instructions, which is not limited here.
从模型的训练侧来说:From the training side of the model:
本申请实施例中,上述训练设备520可以获取到存储器(图5中未示出,可以集成于训练设备520或者与训练设备520分离部署)中存储的代码来实现本申请实施例中和模型训练相关的步骤。In the embodiment of the present application, the above-mentioned training device 520 can obtain the code stored in the memory (not shown in Figure 5, which can be integrated with the training device 520 or deployed separately from the training device 520) to implement the model training in the embodiment of the present application. Related steps.
本申请实施例中,训练设备520可以包括硬件电路(如专用集成电路(application specific integrated circuit,ASIC)、现场可编程门阵列(field-programmable gate array,FPGA)、通用处理器、数字信号处理器(digital signal processing,DSP)、微处理器或微控制器等等)、或这些硬件电路的组合,例如,训练设备520可以为具有执行指令功能的硬件系统,如CPU、DSP等,或者为不具有执行指令功能的硬件系统,如ASIC、FPGA等,或者为上述不具有执行指令功能的硬件系统以及具有执行指令功能的硬件系统的组合。In the embodiment of the present application, the training device 520 may include hardware circuits (such as application specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), general-purpose processors, digital signal processors (digital signal processing, DSP, microprocessor or microcontroller, etc.), or a combination of these hardware circuits. For example, the training device 520 can be a hardware system with the function of executing instructions, such as a CPU, DSP, etc., or a combination of other hardware circuits. A hardware system with the function of executing instructions, such as ASIC, FPGA, etc., or a combination of the above-mentioned hardware systems without the function of executing instructions and a hardware system with the function of executing instructions.
应理解,训练设备520可以为不具有执行指令功能的硬件系统以及具有执行指令功能的硬件系统的组合,本申请实施例提供的中和模型训练相关的部分步骤还可以通过训练设备520中不具有执行指令功能的硬件系统来实现,这里并不限定。It should be understood that the training device 520 can be a combination of a hardware system that does not have the function of executing instructions and a hardware system that has the function of executing instructions. Some of the steps related to model training provided by the embodiments of the present application can also be implemented by the training device 520 that does not have the function of executing instructions. It is implemented by the hardware system that executes the instruction function, which is not limited here.
二、服务器提供的程序合成功能类云服务:2. Program synthesis functional cloud services provided by the server:
在一种可能的实现中,服务器可以通过应用程序编程接口(application programming interface,API)为端侧提供程序合成功能的服务。In a possible implementation, the server can provide program synthesis function services for the client side through an application programming interface (API).
其中,终端设备可以通过云端提供的API,将相关参数(例如包含代码的属性描述)发送至服务器,服务器可以基于接收到的参数,得到处理结果(例如预测文本(也就是生成的代码程序)等),并将处理结果返回至至终端。Among them, the terminal device can send relevant parameters (such as attribute descriptions containing codes) to the server through the API provided by the cloud. The server can obtain processing results (such as predicted text (that is, the generated code program)) based on the received parameters. ) and return the processing results to the terminal.
关于终端以及服务器的描述可以上述实施例的描述,这里不再赘述。The description of the terminal and the server can be as described in the above embodiment, and will not be described again here.
如图6示出了使用一项云平台提供的程序合成功能类云服务的流程。Figure 6 shows the process of using a program provided by a cloud platform to synthesize functional cloud services.
1.开通并购买内容审核服务。1. Activate and purchase the content review service.
2.用户可以下载内容审核服务对应的软件开发工具包(software development kit,SDK),通常云平台提供多个开发版本的SDK,供用户根据开发环境的需求选择,例如JAVA版本的SDK、python版本的SDK、PHP版本的SDK、Android版本的SDK等。2. Users can download the software development kit (SDK) corresponding to the content review service. Usually the cloud platform provides multiple development versions of SDK for users to choose according to the needs of the development environment, such as JAVA version of SDK and python version. SDK, PHP version SDK, Android version SDK, etc.
3.用户根据需求下载对应版本的SDK到本地后,将SDK工程导入至本地开发环境,在本地开发环境中进行配置和调试,本地开发环境还可以进行其他功能的开发,使得形成一个集合了程序合成功能类能力的应用。3. After users download the corresponding version of the SDK locally according to their needs, they can import the SDK project into the local development environment and configure and debug it in the local development environment. The local development environment can also develop other functions, forming a collection of programs. Application of synthetic functional abilities.
4.程序合成功能类应用在被使用的过程中,当需要进行程序合成功能时,可以触发程序合成功能的API调用。当应用触发程序合成功能功能时,发起API请求至云环境中的程序合成功能类服务的运行实例,其中,API请求中携带代码的属性描述,由云环境中的运行实例对代码的属性描述进行处理,获得处理结果(例如预测文本(也就是生成的代码程序)等)。4. During the use of the program synthesis function application, when the program synthesis function is needed, the API call of the program synthesis function can be triggered. When the application triggers the program synthesis function, it initiates an API request to the running instance of the program synthesis function class service in the cloud environment. The API request carries the attribute description of the code, and the running instance in the cloud environment performs the attribute description of the code. Process and obtain the processing results (such as predictive text (that is, the generated code program), etc.).
5.云环境将处理结果返回至应用,由此完成一次的程序合成功能服务调用。5. The cloud environment returns the processing results to the application, thus completing a program synthesis function service call.
三、服务器提供的模型训练类云服务:3. Model training cloud services provided by the server:
在一种可能的实现中,服务器可以基于客户提供的训练数据(例如可以包括程序代码以及代码的属性描述),来提供一个适配于该代码程序的领域的程序合成功能的模型。In a possible implementation, the server can provide a model of the program synthesis function adapted to the domain of the code program based on training data provided by the client (which may include program code and attribute descriptions of the code, for example).
在一种可能的实现中,服务器可以通过应用程序编程接口(application programming interface,API)为端侧提供模型训练的服务。In a possible implementation, the server can provide model training services to the client side through an application programming interface (API).
其中,终端设备可以通过云端提供的API,将相关参数(例如代码的属性描述)发送至服务器,服务器可以基于接收到的参数,得到处理结果,并将处理结果(例如适配于该代码程序的领域的程序合成功能的模型等)返回至终端。Among them, the terminal device can send relevant parameters (such as the attribute description of the code) to the server through the API provided by the cloud. The server can obtain the processing result based on the received parameters and send the processing result (such as the code program adapted to the code). model of the domain's program synthesis function, etc.) is returned to the terminal.
如图7示出了使用一项云平台提供的模型训练类云服务的流程。Figure 7 shows the process of using a model training cloud service provided by a cloud platform.
关于终端以及服务器的描述可以上述实施例的描述,这里不再赘述。The description of the terminal and the server can be as described in the above embodiment, and will not be described again here.
四、联邦训练 4. Federal Training
联邦学习分为模型下发和模型上传两个步骤,中心节点将模型通过网络下发至终端设备;各终端设备利用本地数据计算模型的梯度;各分布式节点将梯度加密后上传至中心节点;中心节点汇总各终端分布式节点的梯度,并采用参数平均算法更新中心节点模型的参数。Federated learning is divided into two steps: model delivery and model upload. The central node delivers the model to the terminal device through the network; each terminal device uses local data to calculate the gradient of the model; each distributed node encrypts the gradient and uploads it to the central node; The central node summarizes the gradients of each terminal distributed node and uses the parameter average algorithm to update the parameters of the central node model.
参见图8,图8为本申请实施例提供的一种模型训练方法的架构示意,如图8所示,本申请实施例提供的架构包括:云侧中心节点,例如可以是云侧的服务器。A1、A2、…为类型为A的分布式节点,如用户持有的手机产品。B1、B2、…为类型为B的分布式节点,如用户持有的个人电脑。在经过分布式节点的管理员(如手机、电脑的用户)同意后,分布式节点的管理员自愿在隐私得到保护的情况下共享其日常使用设备的过程中产生的数据,加入到模型训练计划,设备成为架构中的分布式节点。本实施例中的系统也可以包含更多类型的分布式节点,如智能手表等等。为保护数据隐私,分布式节点不会将数据上传至中心节点,仅在本地保存数据。分布式节点通过通信网络与云服务器连接。云侧中心节点可以运行大模型,而各分布式节点受硬件能力限制只能运行小模型,且A和B可以拥有不同的数据处理能力。Referring to Figure 8, Figure 8 is a schematic diagram of the architecture of a model training method provided by an embodiment of the present application. As shown in Figure 8, the architecture provided by an embodiment of the present application includes: a central node on the cloud side, which may be a server on the cloud side, for example. A1, A2, ... are distributed nodes of type A, such as mobile phone products held by users. B1, B2, ... are distributed nodes of type B, such as personal computers held by users. With the consent of the administrator of the distributed node (such as users of mobile phones and computers), the administrator of the distributed node voluntarily shares the data generated in the process of daily use of the device while protecting privacy, and joins the model training plan. , the device becomes a distributed node in the architecture. The system in this embodiment can also include more types of distributed nodes, such as smart watches and so on. To protect data privacy, distributed nodes do not upload data to the central node and only save data locally. Distributed nodes are connected to cloud servers through communication networks. The central node on the cloud side can run large models, while each distributed node can only run small models due to hardware capacity limitations, and A and B can have different data processing capabilities.
为了更好地理解本申请实施例的方案,下面先结合图2至图4对本申请实施例可能的应用场景进行简单的介绍。In order to better understand the solutions of the embodiments of the present application, possible application scenarios of the embodiments of the present application are briefly introduced below with reference to Figures 2 to 4.
图3示出了一种自然语言处理系统,该自然语言处理系统包括用户设备以及数据处理设备。其中,用户设备包括手机、个人电脑或者信息处理中心等智能终端。用户设备为自然语言数据处理的发起端,作为语言问答或者查询等请求的发起方,通常用户通过用户设备发起请求。Figure 3 shows a natural language processing system, which includes user equipment and data processing equipment. Among them, user equipment includes smart terminals such as mobile phones, personal computers, or information processing centers. User equipment is the initiator of natural language data processing. As the initiator of language question and answer or query requests, users usually initiate requests through user equipment.
上述数据处理设备可以是云服务器、网络服务器、应用服务器以及管理服务器等具有数据处理功能的设备或服务器。数据处理设备通过交互接口接收来自智能终端的查询语句/语音/文本等,再通过存储数据的存储器以及数据处理的处理器环节进行机器学习,深度学习,搜索,推理,决策等方式的语言数据处理,并将处理结果反馈至用户设备。数据处理设备中的存储器可以是一个统称,包括本地存储以及存储历史数据的数据库,数据库可以在数据处理设备上,也可以在其它网络服务器上。The above-mentioned data processing equipment may be a cloud server, a network server, an application server, a management server, and other equipment or servers with data processing functions. The data processing equipment receives query statements/voice/text, etc. from the smart terminal through an interactive interface, and then performs language data processing in the form of machine learning, deep learning, search, reasoning, decision-making, etc. through the memory for storing data and the processor for data processing. , and feedback the processing results to the user device. The memory in the data processing device can be a general term, including local storage and a database that stores historical data. The database can be on the data processing device or on other network servers.
在图3所示的自然语言处理系统中,用户设备可以接收用户的指令,例如用户设备可以接收用户输入的一段文本,然后向数据处理设备发起请求,使得数据处理设备针对用户设备得到的该一段文本执行自然语言处理应用(例如自然语言生成、文本分类、文本推理、命名实体识别、翻译等),从而得到针对该一段文本的对应的自然语言处理应用的处理结果(例如预测词结果、分类结果、推理结果、命名实体识别结果、翻译结果等)。In the natural language processing system shown in Figure 3, the user device can receive the user's instructions. For example, the user device can receive a piece of text input by the user, and then initiate a request to the data processing device, so that the data processing device can respond to the piece of text obtained by the user device. The text executes natural language processing applications (such as natural language generation, text classification, text reasoning, named entity recognition, translation, etc.), thereby obtaining the processing results of the corresponding natural language processing application for the text (such as predicted word results, classification results) , inference results, named entity recognition results, translation results, etc.).
其中,在本申请实施例中,用户设备可以接收用户的指令,例如用户设备可以接收用户输入的一段文本(例如代码的属性描述),然后向数据处理设备发起请求,使得数据处理设备针对用户设备得到的该一段文本执行自然语言处理应用(例如程序合成等),从而得到针对该一段文本的对应的自然语言处理应用的处理结果(例如预测文本(也就是生成的代码程序)等)。Among them, in the embodiment of the present application, the user equipment can receive the user's instructions. For example, the user equipment can receive a piece of text input by the user (such as an attribute description of the code), and then initiate a request to the data processing device, so that the data processing device targets the user equipment. The obtained piece of text is executed with a natural language processing application (such as program synthesis, etc.), thereby obtaining the processing result of the corresponding natural language processing application for the piece of text (such as predictive text (that is, a generated code program), etc.).
文本(例如第一文本或者第二文本)在图3中,数据处理设备可以通过本申请实施例提供的方法训练得到的自然语言模型来处理上述文本数据。Text (such as the first text or the second text) in Figure 3, the data processing device can process the above text data through the natural language model trained by the method provided in the embodiment of the present application.
图4示出了另一种自然语言处理系统,在图4中,用户设备直接作为数据处理设备,该用户设备能够直接接收来自用户的输入并直接由用户设备本身的硬件进行处理,具体过程与图3相似,可参考上面的描述,在此不再赘述。Figure 4 shows another natural language processing system. In Figure 4, the user device directly serves as a data processing device. The user device can directly receive input from the user and process it directly by the hardware of the user device itself. The specific process is as follows Figure 3 is similar, please refer to the above description, and will not be repeated here.
图4是本申请实施例提供的自然语言处理的相关设备300的示意图。FIG. 4 is a schematic diagram of a natural language processing related device 300 provided by an embodiment of the present application.
上述图3和图4中的用户设备具体可以是图4中的本地设备301或者本地设备302,图3中的数据处理设备具体可以是图4中的执行设备310,其中,数据存储系统350可以存储执行设备310的待处理数据,数据存储系统350可以集成在执行设备310上,也可以设置在云上或其它网络服务器上。The user equipment in the above-mentioned Figures 3 and 4 can be the local device 301 or the local device 302 in Figure 4, and the data processing device in Figure 3 can be the execution device 310 in Figure 4, wherein the data storage system 350 can To store the data to be processed by the execution device 310, the data storage system 350 can be integrated on the execution device 310, or can be set up on the cloud or other network servers.
图3和图4中的处理器可以通过神经网络模型或者其它模型进行数据训练/机器学习/深度学习,并利用数据最终训练或者学习得到的模型(例如本申请实施例中的自然语言模型自然语言模型等等)针对文本数据(例如本申请实施例中描述的代码的属性描述文本(例如第一文本或者第二文本))执行自然语言处理应用(例如程序合成等),从而得到相应的处理结果(例如预测文本(也就是生成的代码程序)预测文本等等)。 The processors in Figures 3 and 4 can perform data training/machine learning/deep learning through neural network models or other models, and use the data to finally train or learn the model (such as the natural language model natural language in the embodiment of the present application Model, etc.) execute natural language processing applications (such as program synthesis, etc.) against text data (such as the attribute description text (such as the first text or the second text) of the code described in the embodiments of this application) to obtain corresponding processing results. (For example, predictive text (that is, the generated code program) predictive text, etc.).
由于本申请实施例涉及大量神经网络的应用,为了便于理解,下面先对本申请实施例涉及的相关术语及神经网络等相关概念进行介绍。Since the embodiments of the present application involve the application of a large number of neural networks, in order to facilitate understanding, the relevant terms involved in the embodiments of the present application and related concepts such as neural networks are first introduced below.
(1)神经网络(1) Neural network
神经网络可以是由神经单元组成的,神经单元可以是指以xs(即输入数据)和截距1为输入的运算单元,该运算单元的输出可以为:
The neural network can be composed of neural units. The neural unit can refer to an operation unit that takes xs (ie, input data) and intercept 1 as input. The output of the operation unit can be:
其中,s=1、2、……n,n为大于1的自然数,Ws为xs的权重,b为神经单元的偏置。f为神经单元的激活函数(activation functions),用于将非线性特性引入神经网络中,来将神经单元中的输入信号转换为输出信号。该激活函数的输出信号可以作为下一层卷积层的输入,激活函数可以是sigmoid函数。神经网络是将多个上述单一的神经单元联结在一起形成的网络,即一个神经单元的输出可以是另一个神经单元的输入。每个神经单元的输入可以与前一层的局部接受域相连,来提取局部接受域的特征,局部接受域可以是由若干个神经单元组成的区域。Among them, s=1, 2,...n, n is a natural number greater than 1, Ws is the weight of xs, and b is the bias of the neural unit. f is the activation function of the neural unit, which is used to introduce nonlinear characteristics into the neural network to convert the input signal in the neural unit into an output signal. The output signal of this activation function can be used as the input of the next convolutional layer, and the activation function can be a sigmoid function. A neural network is a network formed by connecting multiple above-mentioned single neural units together, that is, the output of one neural unit can be the input of another neural unit. The input of each neural unit can be connected to the local receptive field of the previous layer to extract the features of the local receptive field. The local receptive field can be an area composed of several neural units.
(2)transformer层(2)Transformer layer
神经网络包括嵌入层和至少一个transformer层,至少一个transformer层可以为N个transformer层(N大于0的整数),其中,每个transformer层包括依次相邻的注意力层、加和与归一化(add&norm)层、前馈(feed forward)层和加和与归一化层。在嵌入层,对当前输入进行嵌入处理,得到多个嵌入向量;在所述注意力层,从所述第一transformer层的上一层获取P个输入向量,以P个输入向量中的任意的第一输入向量为中心,基于预设的注意力窗口范围内的各个输入向量与该第一输入向量之间的关联度,得到该第一输入向量对应的中间向量,如此确定出P个输入向量对应的P个中间向量;在所述池化层,将所述P个中间向量合并为Q个输出向量,其中transformer层中最后一个transformer层得到的多个输出向量用作所述当前输入的特征表示。The neural network includes an embedding layer and at least one transformer layer. At least one transformer layer can be N transformer layers (N is an integer greater than 0), where each transformer layer includes successively adjacent attention layers, summation and normalization. (add&norm) layer, feed forward layer and summation and normalization layer. In the embedding layer, the current input is embedded to obtain multiple embedding vectors; in the attention layer, P input vectors are obtained from the upper layer of the first transformer layer, and any of the P input vectors are The first input vector is the center. Based on the correlation between each input vector within the preset attention window range and the first input vector, the intermediate vector corresponding to the first input vector is obtained. In this way, P input vectors are determined. Corresponding P intermediate vectors; in the pooling layer, the P intermediate vectors are merged into Q output vectors, where the multiple output vectors obtained by the last transformer layer in the transformer layer are used as features of the current input express.
(3)注意力机制(attention mechanism)(3) attention mechanism
注意力机制模仿了生物观察行为的内部过程,即一种将内部经验和外部感觉对齐从而增加部分区域的观察精细度的机制,能够利用有限的注意力资源从大量信息中快速筛选出高价值信息。注意力机制可以快速提取稀疏数据的重要特征,因而被广泛用于自然语言处理任务,特别是机器翻译。而自注意力机制(self-attention mechanism)是注意力机制的改进,其减少了对外部信息的依赖,更擅长捕捉数据或特征的内部相关性。注意力机制的本质思想可以改写为如下公式:The attention mechanism imitates the internal process of biological observation behavior, that is, a mechanism that aligns internal experience and external sensation to increase the precision of observation in some areas, and can use limited attention resources to quickly filter out high-value information from a large amount of information. . The attention mechanism can quickly extract important features of sparse data and is therefore widely used in natural language processing tasks, especially machine translation. The self-attention mechanism is an improvement of the attention mechanism, which reduces the dependence on external information and is better at capturing the internal correlation of data or features. The essential idea of the attention mechanism can be rewritten as the following formula:
其中,Lx=||Source||代表Source的长度,公式含义即将Source中的构成元素想象成是由一系列的数据对构成,此时给定目标Target中的某个元素Query,通过计算Query和各个Key的相似性或者相关性,得到每个Key对应Value的权重系数,然后对Value进行加权求和,即得到了最终的Attention数值。所以本质上Attention机制是对Source中元素的Value值进行加权求和,而Query和Key用来计算对应Value的权重系数。从概念上理解,把Attention可以理解为从大量信息中有选择地筛选出少量重要信息并聚焦到这些重要信息上,忽略大多不重要的信息。聚焦的过程体现在权重系数的计算上,权重越大越聚焦于其对应的Value值上,即权重代表了信息的重要性,而Value是其对应的信息。自注意力机制可以理解为内部Attention(intra attention),Attention机制发生在Target的元素Query和Source中的所有元素之间,自注意力机制指的是在Source内部元素之间或者Target内部元素之间发生的Attention机制,也可以理解为Target=Source这种特殊情况下的注意力计算机制,其具体计算过程是一样的,只是计算对象发生了变化而已。Among them, Lx=||Source|| represents the length of Source. The meaning of the formula is to imagine that the constituent elements in Source are composed of a series of data pairs. At this time, given a certain element Query in the target Target, by calculating the Query and Based on the similarity or correlation of each Key, the weight coefficient of each Key's corresponding Value is obtained, and then the Value is weighted and summed to obtain the final Attention value. So essentially the Attention mechanism is a weighted summation of the Value values of the elements in the Source, and Query and Key are used to calculate the weight coefficient of the corresponding Value. Conceptually, Attention can be understood as selectively filtering out a small amount of important information from a large amount of information and focusing on this important information, while ignoring most of the unimportant information. The process of focusing is reflected in the calculation of the weight coefficient. The greater the weight, the more focused it is on its corresponding Value value. That is, the weight represents the importance of the information, and the Value is its corresponding information. The self-attention mechanism can be understood as internal Attention (intra attention). The Attention mechanism occurs between the Target element Query and all elements in the Source. The self-attention mechanism refers to between the internal elements of the Source or between the internal elements of the Target. The Attention mechanism that occurs can also be understood as the attention calculation mechanism in the special case of Target=Source. The specific calculation process is the same, but the calculation object has changed.
(4)自然语言处理(natural language processing,NLP)(4) Natural language processing (NLP)
自然语言(natural language)即人类语言,自然语言处理(NLP)就是对人类语言的处理。自然语言处理是以一种智能与高效的方式,对文本数据进行系统化分析、理解与信息提取的过程。通过使用NLP及其组件,我们可以管理非常大块的文本数据,或者执行大量的自动化任务,并且解决各式各样的问题,如自动摘要(automatic summarization),机器翻译(machine translation,MT),命名实体识别(named entity recognition,NER),关系提取(relation extraction,RE),信息抽取(information extraction,IE),情感分析,语音识别(speech recognition),问答系统(question answering)以及主题分割等等。Natural language is human language, and natural language processing (NLP) is the processing of human language. Natural language processing is the process of systematically analyzing, understanding and extracting information from text data in an intelligent and efficient way. By using NLP and its components, we can manage very large pieces of text data, or perform a large number of automated tasks, and solve a variety of problems, such as automatic summarization (automatic summarization), machine translation (MT), Named entity recognition (NER), relationship extraction (RE), information extraction (IE), sentiment analysis, speech recognition (speech recognition), question answering system (question answering), topic segmentation, etc. .
(5)预训练语言模型(pre-trained language model)(5) Pre-trained language model (pre-trained language model)
预训练语言模型是一个自然语言序列编码器,为自然语言序列中的每个词进行编码成为一个向量表示,从而进行预测任务。它的训练包含两个阶段。在预训练(pre-training)阶段,该模型在大规模无监督文本上进行语言模型任务的训练,从而学习到一个词表示。在微调(finetuning)阶段,该模型利用预训练阶段学到的参数做初始化,在文本分类(text classification),序列标注(sequence labeling)等下游任务(downstream task)上进行较少步骤的训练,就可以成功把预训练得到的语义信息成功迁移到下游任务上来。The pre-trained language model is a natural language sequence encoder that encodes each word in the natural language sequence into a vector representation to perform prediction tasks. Its training consists of two phases. In the pre-training stage, the model performs language model training on large-scale unsupervised text to learn a word representation. In the finetuning stage, the model uses the parameters learned in the pre-training stage for initialization, and performs fewer steps of training on downstream tasks such as text classification and sequence labeling. The semantic information obtained by pre-training can be successfully transferred to downstream tasks.
(6)自回归语言模型(autoregressive language model)(6) Autoregressive language model
自回归语言模型是指能够根据给定的上下文(如“手机很”)预测下一个可能跟随的词(如“不错”)的模型,该模型通常是给定左侧上文预测右侧下文中的词,但也可以是给定左侧和右侧的上下文预测中间的某个词。An autoregressive language model refers to a model that can predict the next possible following word (such as "nice") based on a given context (such as "mobile phone is very"). This model usually predicts the following word on the right given the left context. word, but could also be a word somewhere in the middle given the context predictions on the left and right.
(7)程序语言(programming language):用来定义计算机指令执行流程的形式化语言。(7) Programming language: a formal language used to define the execution flow of computer instructions.
(8)程序合成(program synthesis):根据提示(例如代码的属性描述等)生成程序的过程。(8) Program synthesis: The process of generating a program based on prompts (such as attribute descriptions of codes, etc.).
(9)代码补全(code completion):根据给定代码片段生成完整代码段落的过程。(9) Code completion: The process of generating a complete code paragraph based on a given code fragment.
(10)函数(function):程序语言中定义某些特定功能的完整代码段落。(10) Function: A complete code paragraph in a programming language that defines some specific functions.
(11)抽象语法树(abstract syntax tree,AST):一种以树状的形式表现程序语言代码的抽象语法结构。(11) Abstract syntax tree (AST): An abstract syntax structure that expresses programming language code in the form of a tree.
(12)因果语言建模(causal language modeling,CLM):通过预测字符序列的下一个字符的方式进行模型训练。(12) Causal language modeling (CLM): Model training is performed by predicting the next character of the character sequence.
(13)掩码语言建模(masked language modeling,MLM):通过预测字符序列中掩码位置上的原始字符的方式进行模型训练。(13) Masked language modeling (MLM): Model training is performed by predicting the original characters at the masked positions in the character sequence.
(14)掩码因果语言建模(masked causal language modeling,MCLM):通过根据前文预测字符序列中掩码位置上的原始字符的方式进行模型训练。(14) Masked causal language modeling (MCLM): Model training is performed by predicting the original characters at the masked positions in the character sequence based on the previous text.
(15)无语言建模(no language modeling,NoLM):不对此范围内的字符进行loss计算,此范围内的字符不参与模型训练。(15) No language modeling (NoLM): No loss calculation is performed on characters within this range, and characters within this range do not participate in model training.
(16)反向传播算法(16)Back propagation algorithm
卷积神经网络可以采用误差反向传播(back propagation,BP)算法在训练过程中修正初始的超分辨率模型中参数的大小,使得超分辨率模型的重建误差损失越来越小。具体地,前向传递输入信号直至输出会产生误差损失,通过反向传播误差损失信息来更新初始的超分辨率模型中参数,从而使误差损失收敛。反向传播算法是以误差损失为主导的反向传播运动,旨在得到最优的超分辨率模型的参数,例如权重矩阵。The convolutional neural network can use the error back propagation (BP) algorithm to modify the size of the parameters in the initial super-resolution model during the training process, so that the reconstruction error loss of the super-resolution model becomes smaller and smaller. Specifically, forward propagation of the input signal until the output will produce an error loss, and the parameters in the initial super-resolution model are updated by back-propagating the error loss information, so that the error loss converges. The backpropagation algorithm is a backpropagation movement dominated by error loss, aiming to obtain the optimal parameters of the super-resolution model, such as the weight matrix.
(17)损失函数(17)Loss function
在训练深度神经网络的过程中,因为希望深度神经网络的输出尽可能的接近真正想要预测的值,所以可以通过比较当前网络的预测值和真正想要的目标值,再根据两者之间的差异情况来更新每一层神经网络的权重向量(当然,在第一次更新之前通常会有初始化的过程,即为深度神经网络中的各层预先配置参数),比如,如果网络的预测值高了,就调整权重向量让它预测低一些,不断地调整,直到深度神经网络能够预测出真正想要的目标值或与真正想要的目标值非常接近的值。因此,就需要预先定义“如何比较预测值和目标值之间的差异”,这便是损失函数(loss function)或目标函数(objective function),它们是用于衡量预测值和目标值的差异的重要方程。其中,以损失函数举例,损失函数的输出值(loss)越高表示差异越大,那么深度神经网络的训练就变成了尽可能缩小这个loss的过程。In the process of training a deep neural network, because we hope that the output of the deep neural network is as close as possible to the value that we really want to predict, we can compare the predicted value of the current network with the really desired target value, and then based on the difference between the two to update the weight vector of each layer of the neural network according to the difference (of course, there is usually an initialization process before the first update, that is, preconfiguring parameters for each layer in the deep neural network). For example, if the predicted value of the network If it is high, adjust the weight vector to make its prediction lower, and continue to adjust until the deep neural network can predict the really desired target value or a value that is very close to the really desired target value. Therefore, it is necessary to define in advance "how to compare the difference between the predicted value and the target value". This is the loss function (loss function) or objective function (objective function), which is used to measure the difference between the predicted value and the target value. Important equations. Among them, taking the loss function as an example, the higher the output value (loss) of the loss function, the greater the difference. Then the training of the deep neural network becomes a process of reducing this loss as much as possible.
(18)反向传播算法(18)Back propagation algorithm
神经网络可以采用误差反向传播(back propagation,BP)算法在训练过程中修正初始的神经网络模型中参数的大小,使得神经网络模型的重建误差损失越来越小。具体地,前向传递输入信号直至输出会产生误差损失,通过反向传播误差损失信息来更新初始的神经网络模型中参数,从而使误差损失收敛。反向传播算法是以误差损失为主导的反向传播运动,旨在得到最优的神经网络模型的参数,例如权重矩阵。The neural network can use the error back propagation (BP) algorithm to modify the size of the parameters in the initial neural network model during the training process, so that the reconstruction error loss of the neural network model becomes smaller and smaller. Specifically, forward propagation of the input signal until the output will produce an error loss, and the parameters in the initial neural network model are updated by backpropagating the error loss information, so that the error loss converges. The backpropagation algorithm is a backpropagation movement dominated by error loss, aiming to obtain the optimal parameters of the neural network model, such as the weight matrix.
程序合成指的是由一定的软件程序来自动合成具有指定功能或者结构的指定程序语言序列,简而言之就是由程序来生成程序,总体上分为程序合成(代码生成)以及代码补全两种。程序合成,即从无到有,可以按一定的自然语言描述或预设的功能描述,生成对应的代码。代码补全,即在已有的代码序列上文基础上,继续生成下文代码序列。Program synthesis refers to using a certain software program to automatically synthesize a specified program language sequence with specified functions or structures. In short, it is a program that generates a program. Generally speaking, it is divided into two categories: program synthesis (code generation) and code completion. kind. Program synthesis, that is, starting from scratch, can generate corresponding code according to a certain natural language description or a preset function description. Code completion, that is, based on the existing code sequence, continue to generate the following code sequence.
语言模型指的是对语言序列,如自然语言或者程序语言的建模,现在大部分使用神经网络来进行语言模型建模,即通过神经网络模型,对语言序列的出现概率进行预测,以生成语言模型为例,即预测每个序列位置出现某个字,词等语言单位的概率,例如著名的GPT语言模型,就是通过搭建多个神经网络计算单元transformer组成解码器网络,解码器通过根据已有的上文,预测下一个位置的语言单位可能出现的词,通过迭代直至整个序列预测完成,即解码完成。Language model refers to the modeling of language sequences, such as natural language or programming language. Nowadays, most neural networks are used for language model modeling, that is, the occurrence probability of language sequences is predicted through the neural network model to generate language. Take the model as an example, which is to predict the probability of a certain word, word and other language unit appearing at each sequence position. For example, the famous GPT language model is to build a decoder network by building multiple neural network computing units transformer. The decoder is based on the existing In the above, the words that may appear in the language unit at the next position are predicted through iteration until the prediction of the entire sequence is completed, that is, decoding is completed.
语言模型的预训练是指通过海量的语言序列语料,训练对应的语言模型,使得语言模型具备预测某个位置某个语言单位出现概率的能力。Language model pre-training refers to training the corresponding language model through massive language sequence corpus, so that the language model has the ability to predict the occurrence probability of a certain language unit at a certain position.
随着深度学习技术的发展,利用深度学习进行序列生成已经被广泛应用在自然语言序列生成的场景上,特别是预训练语言生成模型技术的出现,使得语言生成能力有了质的提升。近年来,类似生成式预训练模型(generative pre-training,GPT)、T5、BART等生成模型也开始逐渐被引入代码生成或者补全中。该技术以Transformer为基础部件,搭件解码器或者,编码-解码器模型架构,再用大量的代码数据进行训练,得到可以根据上文输出相应代码下文的能力,从而进行代码补全或者生成。With the development of deep learning technology, the use of deep learning for sequence generation has been widely used in natural language sequence generation scenarios. In particular, the emergence of pre-trained language generation model technology has led to a qualitative improvement in language generation capabilities. In recent years, generative models such as generative pre-training (GPT), T5, and BART have gradually been introduced into code generation or completion. This technology uses Transformer as the basic component, builds a decoder or encoder-decoder model architecture, and then uses a large amount of code data for training to obtain the ability to output the corresponding code context based on the above, thereby completing or generating code.
现有的技术沿用自然语言处理的生成方式,根据原始的代码进行训练,当前这种方式能够根据已有上下文进行代码的续写,但是针对函数级代码的生成这个场景,代码生成的质量还有待提升。The existing technology follows the natural language processing generation method and trains based on the original code. Currently, this method can continue to write the code based on the existing context. However, for the scenario of function-level code generation, the quality of the code generation has yet to be improved. promote.
为了解决上述问题,本申请实施例提供了一种模型训练方法。下面结合附图对本申请实施例的模型训练方法进行详细的介绍。In order to solve the above problems, embodiments of the present application provide a model training method. The model training method of the embodiment of the present application will be introduced in detail below with reference to the accompanying drawings.
参照图9,图9为本申请实施例提供的一种模型训练方法的流程示意,如图9所示,本申请实施例提供的一种模型训练方法,可以包括步骤901至902,下面分别对这些步骤进行详细的描述。Referring to Figure 9, Figure 9 is a flow diagram of a model training method provided by an embodiment of the present application. As shown in Figure 9, a model training method provided by an embodiment of the present application may include steps 901 to 902. The following are respectively These steps are described in detail.
901、获取第一文本以及第二文本,所述第一文本和所述第二文本为从原始代码文本中提取得到的;其中,所述第一文本为程序代码,所述第二文本为所述程序代码对应的属性描述;所述第一文本和所述第二文本包括已知文本以及预测文本;901. Obtain the first text and the second text. The first text and the second text are extracted from the original code text; wherein the first text is the program code and the second text is the program code. Attribute description corresponding to the program code; the first text and the second text include known text and predicted text;
在一种可能的实现中,为了获取到作为训练样本的第一文本以及第二文本,可以获取到原始的语料(例如本申请实施例中的原始代码文本),并从原始代码文本中提取出第一文本以及第二文本。In a possible implementation, in order to obtain the first text and the second text as training samples, the original corpus (such as the original code text in the embodiment of the present application) can be obtained, and the original code text can be extracted from the original code text. First text and second text.
关于原始代码文本:About the original code text:
其中,原始代码文本可以为已有的程序语言语料及自然语言语料。可选的,可以通过网络(或者其他方式)获取已有的程序语言语料及自然语言语料,该语料包括混合程序语言及自然语言的代码文件、程序语言的代码文件、自然语言的文件。其中,收集的自然语言可以是任意人类语言,程序语言可以是任意编程语言,本申请并不限定。Among them, the original code text can be existing programming language corpus and natural language corpus. Optionally, existing programming language corpus and natural language corpus can be obtained through the Internet (or other methods). The corpus includes code files of mixed programming language and natural language, code files of programming language, and natural language files. The collected natural language can be any human language, and the programming language can be any programming language, which is not limited by this application.
在一种可能的实现中,可以对原始代码文本进行数据去重、文件去重或者函数代码去重等,进而将语料中的重复文件或者重复函数代码去重,减少语料总量,提升处理和训练效率。In one possible implementation, the original code text can be deduplicated from data, files, or function codes, and then duplicate files or duplicate function codes in the corpus can be deduplicated, thereby reducing the total amount of corpus and improving processing and Training efficiency.
关于第一文本和第二文本:Regarding the first and second texts:
在一种可能的实现中,所述第一文本和所述第二文本为从原始代码文本中提取得到的;其中,所述第一文本为程序代码,所述第二文本为所述程序代码对应的属性描述。第二文本可以为自然语言,且第二文本可以是任意的人类沟通时使用的语言,程序代码可以是任意编程语言,本申请并不限定。In a possible implementation, the first text and the second text are extracted from the original code text; wherein the first text is the program code, and the second text is the program code. Corresponding attribute description. The second text can be in natural language, and the second text can be in any language used for human communication, and the program code can be in any programming language, which is not limited by this application.
在一种可能的实现中,所述属性描述包括所述程序代码功能描述或者所述程序代码的实现原理(或者称之为该程序代码的解释)。例如,可以参照图10A,图10A示出的框中为属性描述,其余为程序代码,其中包含两个函数:“euclidean_distance”、“classifier”。参照图10B,图10B为函数“classifier”的一个代码文本以及对应的属性描述的示意,参照图10C,图10C为函数“classifier”的一个代码文本示意,图10D为函数“classifier”的一个属性描述示意。In a possible implementation, the attribute description includes a function description of the program code or an implementation principle of the program code (or it is also called an explanation of the program code). For example, refer to Figure 10A. The box shown in Figure 10A is the attribute description, and the rest is the program code, which includes two functions: "euclidean_distance" and "classifier". Refer to Figure 10B, which shows a code text of the function "classifier" and the corresponding attribute description. Refer to Figure 10C, which shows a code text of the function "classifier". Figure 10D shows an attribute of the function "classifier". Description.
在一种可能的实现中,可以识别原始代码文本中的函数代码段作为第一文本:例如,可以使用一定规则(例如可以使用代码抽象语法树(AST)等程序代码识别方法)识别原始代码文本中的函数代码段。In one possible implementation, the function code segment in the original code text can be identified as the first text: for example, the original code text can be identified using certain rules (for example, a program code identification method such as a code abstract syntax tree (AST) can be used) function code snippet in .
在一种可能的实现中,可以识别原始代码文本中的自然语言作为第二文本(和识别出的第一文本相关联):例如,可以使用一定规则(例如可以使用自然语言识别的方法)来识别原始代码文本中的代码的属性描述。In one possible implementation, the natural language in the original code text can be recognized as the second text (associated with the recognized first text): for example, certain rules can be used (for example, a natural language recognition method can be used) to Identifies the attribute description of the code in the original code text.
在一种可能的实现中,在识别出第一文本和第二文本之后,可以建立第一文本以及第二文本之间的对应关系,以形成“自然语言描述-函数实现代码”(Description-Function)的句对形式样本。可选的,可以在每组自然语言-程序语言前使用表示字符组合来表示接下来的序列为文本语言序列或者具体的代码语言序列,并在样本之后添加样本结束符,进而可以表示出第一文本和第二文本为一对样本。In a possible implementation, after identifying the first text and the second text, a correspondence relationship between the first text and the second text can be established to form a "natural language description-function implementation code" (Description-Function code) ) sentence pair form sample. Optionally, you can use a character combination before each set of natural language-program language to indicate that the following sequence is a text language sequence or a specific code language sequence, and add a sample terminator after the sample to indicate the first The text and the second text are a pair of samples.
本申请实施例中,在构建程序合成的训练样本时,从原始的程序代码中提取代码文本和对应的属性描述并作为一对样本,相比现有技术中直接将原始的程序文件作为训练样本,帮助自然语言模型在训练时可以将代码文本和对应的属性描述之间建立关联,而无需在训练过程中从样本中学习到识别这种关联的能力,降低了训练难度,提高了模型的预测精度。In the embodiment of the present application, when constructing a program-synthesized training sample, the code text and corresponding attribute description are extracted from the original program code and used as a pair of samples. Compared with the prior art, the original program file is directly used as a training sample. , helping the natural language model to establish an association between the code text and the corresponding attribute description during training, without having to learn the ability to identify this association from samples during the training process, reducing the training difficulty and improving the prediction of the model Accuracy.
在一种可能的实现中,可以从原始语料或过滤及清洗后的原始语料中提取第一文本(或者称之为程序语言函数级代码片段)及其相应的第二文本(或者称之为自然语言描述),构造“自然语言描述-程序语言函数代码对”。可选的,可以通过一些规则方法提取程序语言函数级代码片段及其相应的自然语言描述,可选的,可以通过抽象语法树(AST)提取程序语言函数级代码片段及其相应的自然语言描述,可选的,可以通过其他自动化方法提取程序语言函数级代码片段及其相应的自然语言描述。可选的,可以从混合程序语言及自然语言的代码文件中提取程序语言函数代码、自然语言描述,可选的,可以从程序语言的代码文件中提取程序语言函数代码,可选的,可以从自然语言的文件中提取自然语言描述。可选的,可以通过混合程序语言及自然语言的代码文件中程序语言代码片段与自然语言描述的对应关系构造“自然语言描述-程序语言函数代码对”,可选的,可以通过其他程序语言代码片段与自然语言描述的对应关系构造“自然语言描述-程序语言函数代码对”。In a possible implementation, the first text (or program language function-level code fragment) and its corresponding second text (or natural text) can be extracted from the original corpus or the filtered and cleaned original corpus. Language description), constructing "natural language description-programming language function code pair". Optionally, the function-level code fragments of the programming language and their corresponding natural language descriptions can be extracted through some rule methods. Optionally, the function-level code fragments of the programming language and their corresponding natural language descriptions can be extracted through the abstract syntax tree (AST). ,Optionally, program language function-level code snippets ,and their corresponding natural language descriptions can be ,extracted through other automated methods. Optionally, the programming language function code and natural language description can be extracted from the code file of mixed programming language and natural language. Optionally, the programming language function code can be extracted from the code file of the programming language. Extract natural language descriptions from natural language documents. Optionally, the "natural language description-programming language function code pair" can be constructed through the correspondence between the programming language code fragments and the natural language description in the mixed programming language and natural language code files. Optionally, the "natural language description-programming language function code pair" can be constructed through other programming language codes. The correspondence relationship between the fragments and the natural language description constructs a "natural language description-programming language function code pair".
在一种可能的实现中,为了提高训练语料的质量,可以对函数级代码片段(例如代码程序)进行归一化、规范代码语法或者统一代码风格等处理。示例性的,可以使用一定规则(例如代码抽象语法树(AST))对代码进行解析,抽取纯代码部分,对空格、缩进等风格进行统一化,使得数据归一化。In one possible implementation, in order to improve the quality of the training corpus, function-level code fragments (such as code programs) can be normalized, standardized code syntax, or unified code style. For example, certain rules (such as code abstract syntax tree (AST)) can be used to parse the code, extract pure code parts, and unify styles such as spaces and indentations to normalize the data.
在一种可能的实现中,为了提高训练语料的质量,可以对函数级代码片段(例如代码程序)根据其代码语料的统计特性,去除代码语料中自然语言描述部分过短的语料、代码部分过长的语料或者两者长度相差过大的语料,以提升训练语料质量。In a possible implementation, in order to improve the quality of the training corpus, function-level code snippets (such as code programs) can be removed based on the statistical characteristics of their code corpus, and the corpus with too short natural language description part and the excessive code part in the code corpus can be removed. Long corpus or corpus with a large length difference between the two to improve the quality of the training corpus.
在一种可能的实现中,由于训练样本通常难以获取到,也就是训练样本(第一文本和第二文本构成的文本对)的数量较少,可以对已获取训练样本进行数据增强,来得到更多的训练样本。In a possible implementation, since training samples are usually difficult to obtain, that is, the number of training samples (text pairs composed of the first text and the second text) is small, data enhancement can be performed on the obtained training samples to obtain More training samples.
在一种可能的实现中,所述第一文本为根据从原始代码文本中提取得到的第一原始文本得到的;其中,所述第一文本为对所述第一原始文本中的部分文本单元进行修改得到的。In a possible implementation, the first text is obtained based on the first original text extracted from the original code text; wherein the first text is a partial text unit in the first original text. Obtained by modification.
在一种可能的实现中,所述修改包括:删除、增加或者替换。In a possible implementation, the modification includes: deletion, addition or replacement.
在一种可能的实现中,所述替换具体为相同语义的替换。例如可以基于反向翻译技术生成同义片段,或者可以是基于同义句生成技术生成同义片段。In a possible implementation, the replacement is specifically a replacement with the same semantics. For example, synonymous segments may be generated based on back translation technology, or synonymous segments may be generated based on synonymous sentence generation technology.
例如,可以对代码文本中的变量名进行随机替换、(循环)代码段等价替换以及随机插入无效代码段等方式,大量获得高质量自动生成的训练数据,最终提升模型性能。For example, you can randomly replace variable names in the code text, replace equivalent (loop) code segments, and randomly insert invalid code segments to obtain a large amount of high-quality automatically generated training data, ultimately improving model performance.
在一种可能的实现中,所述第二文本为根据从原始代码文本中提取得到的第二原始文本得到的;其中,所述第二文本为对所述第二原始文本中的部分文本单元进行修改得到的。In a possible implementation, the second text is obtained based on the second original text extracted from the original code text; wherein the second text is a partial text unit in the second original text. Obtained by modification.
在一种可能的实现中,所述修改包括:删除、增加或者替换。In a possible implementation, the modification includes: deletion, addition or replacement.
在一种可能的实现中,所述替换具体为相同语义的替换。例如可以基于反向翻译技术生成同义片段,或者可以是基于同义句生成技术生成同义片段。 In a possible implementation, the replacement is specifically a replacement with the same semantics. For example, synonymous segments may be generated based on back translation technology, or synonymous segments may be generated based on synonymous sentence generation technology.
例如,可以通过对自然语言描述中进行同义词替换,句式变换,或者使用自然语言处理技术中的复述生成模型或者方法,数据增广方法,数据加噪方法对自然语言描述进行增广,大量获得高质量自动生成的训练数据,最终提升模型性能。For example, you can augment the natural language description by replacing synonyms, transforming sentence patterns, or using paraphrase generation models or methods in natural language processing technology, data augmentation methods, and data noise methods to obtain a large number of information. High-quality automatically generated training data ultimately improves model performance.
在一种可能的实现中,在模型训练的不同阶段,可以对训练样本进行不同的处理,例如,在模型训练初期迭代阶段可以采用上述方式得到的第一文本以及第二文本,而在模型训练的微调阶段,可以从原始代码文本中提取出适配于所述程序代码的应用领域的文本(同样包括程序代码以及对应的代码属性描述)作为训练样本。In a possible implementation, training samples can be processed differently at different stages of model training. For example, in the initial iterative stage of model training, the first text and the second text obtained in the above way can be used, and in the model training In the fine-tuning stage, text adapted to the application field of the program code (also including program code and corresponding code attribute descriptions) can be extracted from the original code text as a training sample.
在一种可能的实现中,所述第一文本或所述第二文本为从原始代码文本中提取的适配于所述程序代码的应用领域的文本。在一种可能的实现中,所述适配于所述程序代码的应用领域,包括:文本长度或者单行文本长度小于阈值的文本;其中,所述阈值与所述程序代码的应用领域有关。或者,还可以采用除了长度之外的其他维度的信息来量化和程序代码的应用领域。In a possible implementation, the first text or the second text is text extracted from the original code text and adapted to the application field of the program code. In one possible implementation, the adaptation to the application field of the program code includes: text whose length or the length of a single line of text is less than a threshold; wherein the threshold is related to the application field of the program code. Alternatively, information in other dimensions besides length can also be used to quantify and apply the program code.
在一种可能的实现中,可以基于数据样本与函数级代码生成器的应用领域的数据样本的相似度来过滤样本。相似度计算方法,可以是自然语言描述之间的长度关系,可以是程序语言函数代码之间的长度关系,也可以是其他关系。In one possible implementation, the samples may be filtered based on their similarity to data samples from the application domain of the function-level code generator. The similarity calculation method can be the length relationship between natural language descriptions, the length relationship between program language function codes, or other relationships.
在一种可能的实现中,为了训练能够用于进行程序生成功能的模型(也就是本申请实施例中的自然语言模型),可以将训练样本中的部分文本单元作为已知文本,另一部分文本作为待预测的文本,并基于已知文本来预测待预测的文本所在的文本位置的文本。In a possible implementation, in order to train a model that can be used to perform program generation functions (that is, the natural language model in the embodiment of this application), some text units in the training sample can be used as known text, and the other part of the text As the text to be predicted, the text at the text position where the text to be predicted is predicted based on the known text.
由于第一文本和第二文本为不同类型的文本(第一文本为计算机编译语言,第二文本为自然语言),可以针对于第一文本和第二文本通过不同的训练目标进行文本预测过程。Since the first text and the second text are different types of text (the first text is a computer compiled language and the second text is a natural language), the text prediction process can be performed with different training objectives for the first text and the second text.
例如,针对于第一文本,可以采用CLM、MLM、MCLM以及NoLM四种训练目标中的一种进行文本预测过程。For example, for the first text, one of the four training objectives of CLM, MLM, MCLM and NoLM can be used to perform the text prediction process.
例如,针对于第二文本,可以采用CLM、MLM以及MCLM三种训练目标中的一种进行模型训练过程。For example, for the second text, one of the three training objectives of CLM, MLM and MCLM can be used to perform the model training process.
在优化自然语言描述部分时,可选CLM、MLM、MCLM、NoLM四种训练目标中的一种。在优化程序语言函数代码部分时,可选CLM、MLM、MCLM三种训练目标中的一种,注意此处可以不包括NoLM训练目标。自然语言描述部分和程序语言函数代码部分训练目标可自由组合。When optimizing the natural language description part, you can choose one of four training objectives: CLM, MLM, MCLM, and NoLM. When optimizing the program language function code part, you can choose one of the three training objectives: CLM, MLM, and MCLM. Note that the NoLM training objective does not need to be included here. The training objectives of the natural language description part and the programming language function code part can be freely combined.
在一种可能的实现中,在对所述第一文本中的文本进行预测时,可以通过第一方式,从所述第一文本中确定所述预测文本对应的文本位置;在对所述第二文本中的文本进行预测时,可以通过第二方式,从所述第二文本中确定所述预测文本对应的文本位置;所述第一方式和所述第二方式不同。In a possible implementation, when predicting the text in the first text, the text position corresponding to the predicted text may be determined from the first text in a first manner; When predicting text in two texts, the text position corresponding to the predicted text can be determined from the second text through a second method; the first method and the second method are different.
在一种可能的实现中,所述第一方式和所述第二方式分别为如下预测方式的一种:In a possible implementation, the first method and the second method are respectively one of the following prediction methods:
对所述第一文本或所述第二文本的部分或全部文本的文本位置中采样掩码位置,所述掩码位置作为所述预测文本对应的文本位置(也就是MLM);Sample mask positions from the text positions of part or all of the first text or the second text, and use the mask positions as the text positions corresponding to the predicted text (that is, MLM);
将所述已知文本的下文的文本位置作为所述预测文本对应的文本位置;其中,所述已知文本为所述自然语言模型已预测的全部文本(也就是CLM);Use the following text position of the known text as the text position corresponding to the predicted text; wherein the known text is all the texts that have been predicted by the natural language model (that is, CLM);
将所述已知文本的下文的文本位置作为所述预测文本对应的文本位置;其中,所述已知文本为所述自然语言模型已预测的全部文本中未被掩码的文本(也就是MCLM)。The following text position of the known text is used as the text position corresponding to the predicted text; wherein the known text is the unmasked text among all the texts that have been predicted by the natural language model (that is, MCLM ).
在一种可能的实现中,还可以在不对第二文本中的文本进行预测的情况下(也就是NoLM),通过如下预测方式的一种确定所述预测文本对应的文本位置:In a possible implementation, without predicting the text in the second text (that is, NoLM), the text position corresponding to the predicted text can be determined through one of the following prediction methods:
对所述第一文本的部分或全部文本的文本位置中采样掩码位置,所述掩码位置作为所述预测文本对应的文本位置(也就是MLM);Sample mask positions from the text positions of part or all of the first text, and use the mask positions as the text positions corresponding to the predicted text (that is, MLM);
将所述已知文本的下文的文本位置作为所述预测文本对应的文本位置;其中,所述已知文本为所述自然语言模型已预测的全部文本(也就是CLM);Use the following text position of the known text as the text position corresponding to the predicted text; wherein the known text is all the texts that have been predicted by the natural language model (that is, CLM);
将所述已知文本的下文的文本位置作为所述预测文本对应的文本位置;其中,所述已知文本为所述自然语言模型已预测的全部文本中未被掩码的文本(也就是MCLM)。The following text position of the known text is used as the text position corresponding to the predicted text; wherein the known text is the unmasked text among all the texts that have been predicted by the natural language model (that is, MCLM ).
接下来分别介绍CLM、MLM、MCLM以及NoLM。 Next, CLM, MLM, MCLM and NoLM are introduced respectively.
关于CLM:About CLM:
在模型训练的前馈过程中,自然语言模型可以沿着预设的上下文预测方向,依次进行文本的预测,也就是说,自然语言模型已经预测出的词的全部可以作为已知文本,且已知文本用于作为位置文本的上文来进行预测文本所在文本位置的文本预测,例如,可以沿着由上文到下文的顺序(也就是从文本开头到文本末尾的顺序)进行预测,每次可以预测一个文本单元,并在下次预测时基于已经预测出的文本单元继续进行预测,例如,文本可以为“the cat sat on the mat”,在第一次预测时,可以通过初始标识位预测“the”所处的文本位置的文本,在下一次预测时,可以通过初始标识位置以及“the”来预测“cat”所处的文本位置的文本,在下一次预测时,可以通过初始标识位置、“the”以及“cat”来预测“sat”所处的文本位置的文本。In the feedforward process of model training, the natural language model can predict texts sequentially along the preset context prediction direction. In other words, all words that have been predicted by the natural language model can be used as known text, and have been The known text is used as the context of the position text to predict the text location of the text. For example, prediction can be made in the order from the context to the context (that is, from the beginning of the text to the end of the text), each time You can predict a text unit and continue to predict based on the already predicted text unit in the next prediction. For example, the text can be "the cat sat on the mat". In the first prediction, you can predict " The text at the text position of "the" can be predicted by the initial identification position and "the" in the next prediction. The text at the text position of "cat" can be predicted by the initial identification position, "the" in the next prediction. ” and “cat” to predict the text at the text position of “sat”.
在处理第一文本或者第二文本时都可以采用CLM的训练目标进行训练。The training objectives of CLM can be used for training when processing the first text or the second text.
例如,在使用第二文本进行模型训练时,可以采用CLM的训练目标进行自然语言模型的训练,在预测完第二文本后,可以非CLM的训练目标使用第一文本进行模型训练(此时第二文本可以作为已知文本参与使用第一文本进行模型训练的过程)。For example, when using the second text for model training, you can use the CLM training target to train the natural language model. After predicting the second text, you can use the first text as a non-CLM training target for model training (in this case, the first text is used as the training target). The second text can be used as known text to participate in the process of using the first text for model training).
例如,在使用第二文本进行模型训练时,可以采用非CLM的训练目标进行自然语言模型的训练,在预测完第二文本后,可以采用CLM的训练目标使用第一文本进行模型训练(此时第二文本可以作为已知文本参与使用第一文本进行模型训练的过程)。For example, when using the second text for model training, you can use non-CLM training targets to train the natural language model. After predicting the second text, you can use the CLM training targets to use the first text for model training (at this time The second text can be used as a known text to participate in the process of using the first text for model training).
关于MLM:About MLM:
在模型训练的前馈过程中,可以对文本(例如第二文本或者第一文本)中的文本单元进行掩码(例如可以为随机掩码),掩码后的文本单元可以作为预测文本,未被掩码的文本单元可以作为已知文本,自然语言模型可以基于未被掩码的文本单元(或者已经预测出的掩码后的文本单元),依次进行掩码后的文本单元所在文本位置的文本预测,例如,文本可以为“the cat sat on the mat”,对文本进行掩码后,可以得到“the_sat_the mat”,其中,这里的符号“_”的含义是掩码,而不是指下划线在。第一次预测时,可以通过初始标识位以及未被掩码的文本单元,预测其中一个“_”所处的文本位置的文本。In the feedforward process of model training, the text units in the text (for example, the second text or the first text) can be masked (for example, they can be randomly masked), and the masked text units can be used as predicted texts. The masked text unit can be used as known text, and the natural language model can sequentially calculate the text location of the masked text unit based on the unmasked text unit (or the predicted masked text unit). Text prediction, for example, the text can be "the cat sat on the mat". After masking the text, you can get "the_sat_the mat", where the symbol "_" here means masking, not referring to the underline in . In the first prediction, the text at the text position where one of the "_" is located can be predicted through the initial identification bit and the unmasked text unit.
在处理第一文本或者第二文本时都可以采用MCL的训练目标进行训练。The training target of MCL can be used for training when processing the first text or the second text.
例如,在使用第二文本进行模型训练时,可以采用MCL的训练目标进行自然语言模型的训练,在预测完第二文本后,可以非MCL的训练目标使用第一文本进行模型训练(此时第二文本可以作为已知文本参与使用第一文本进行模型训练的过程)。For example, when using the second text for model training, you can use the MCL training target to train the natural language model. After predicting the second text, you can use the first text as a non-MCL training target for model training (in this case, the first text is used for model training). The second text can be used as known text to participate in the process of using the first text for model training).
例如,在使用第二文本进行模型训练时,可以采用非MCL的训练目标进行自然语言模型的训练,在预测完第二文本后,可以采用MCL的训练目标使用第一文本进行模型训练(此时第二文本可以作为已知文本参与使用第一文本进行模型训练的过程)。For example, when using the second text for model training, you can use non-MCL training targets to train the natural language model. After predicting the second text, you can use the MCL training targets to use the first text for model training (at this time The second text can be used as a known text to participate in the process of using the first text for model training).
关于MCLM:About MCLM:
在模型训练的前馈过程中,自然语言模型可以沿着预设的上下文预测方向,依次进行文本的预测,和CLM不同的是,在CLM中自然语言模型已经预测出的词的全部可以作为已知文本,且已知文本用于作为位置文本的上文来进行预测文本所在文本位置的文本预测,在MCLM中,可以对MCLM中自然语言模型已经预测出的词进行掩码,可以使用自然语言模型已经预测出的词中未被掩码的文本单元来进行预测文本所在文本位置的文本预测,例如,可以沿着由上文到下文的顺序(也就是从文本开头到文本末尾的顺序)进行预测。In the feedforward process of model training, the natural language model can predict text sequentially along the preset context prediction direction. The difference from CLM is that in CLM, all words that have been predicted by the natural language model can be used as Known text, and the known text is used as the upper text of the position text to predict the text position of the predicted text. In MCLM, the words that have been predicted by the natural language model in MCLM can be masked, and natural language can be used The unmasked text units in the words that have been predicted by the model are used to predict the text position of the predicted text. For example, it can be performed in the order from the top to the bottom (that is, from the beginning of the text to the end of the text). predict.
例如,可以沿着由上文到下文的顺序(也就是从文本开头到文本末尾的顺序)进行预测,每次可以预测一个文本单元,并在下次预测时基于已经预测出的文本单元继续进行预测,例如,文本可以为“the cat sat on the mat”,在第一次预测时,可以通过初始标识位预测“the”所处的文本位置的文本,并对初始标识位以及“the”进行掩码,若“the”被掩码,则在下一次预测时,可以通过初始标识位置来预测“cat”所处的文本位置的文本。For example, predictions can be made in the order from top to bottom (that is, from the beginning of the text to the end of the text). One text unit can be predicted at a time, and predictions can be continued based on the already predicted text units in the next prediction. , for example, the text can be "the cat sat on the mat". In the first prediction, the text at the text position of "the" can be predicted through the initial identification bit, and the initial identification bit and "the" can be masked. code, if "the" is masked, in the next prediction, the text at the text position of "cat" can be predicted through the initial identification position.
在处理第一文本或者第二文本时都可以采用MCLM的训练目标进行训练。The training objectives of MCLM can be used for training when processing the first text or the second text.
例如,在使用第二文本进行模型训练时,可以采用MCLM的训练目标进行自然语言模型的训练,在预测完第二文本后,可以非MCLM训练目标使用第一文本进行模型训练(此时第二文本可以作为已知文本参与使用第一文本进行模型训练的过程)。For example, when using the second text for model training, you can use the MCLM training target to train the natural language model. After predicting the second text, you can use the first text as a non-MCLM training target for model training (in this case, the second text The text can be used as known text to participate in the process of using the first text for model training).
例如,在使用第二文本进行模型训练时,可以采用非MCLM的训练目标进行自然语言模型的训练,在预测完第二文本后,可以采用MCLM的训练目标使用第一文本进行模型训练(此时第二文本可以作为已知文本参与使用第一文本进行模型训练的过程)。For example, when using the second text for model training, you can use a non-MCLM training target to train the natural language model. After predicting the second text, you can use the MCLM training target to use the first text for model training (at this time The second text can be used as a known text to participate in the process of using the first text for model training).
关于NoLM:About NoLM:
在模型训练的前馈过程中,可以不对第二文本进行预测,而直接将第二文本作为已知文本,并将第一文本作为训练样本,进行第一文本中文本单元的预测,例如可以使用CLM、MLM、MCLM等训练目标将第一文本作为训练样本,进行第一文本中文本单元的预测。In the feedforward process of model training, the second text can not be predicted, but the second text can be directly used as the known text, and the first text can be used as a training sample to predict text units in the first text. For example, you can use Training targets such as CLM, MLM, and MCLM use the first text as a training sample to predict text units in the first text.
例如,第二文本可以作为已知文本参与使用第一文本进行模型训练的过程,可以将CLM、MLM、MCLM等作为训练目标,并使用第一文本进行模型训练(此时第二文本可以作为已知文本参与使用第一文本进行模型训练的过程)。For example, the second text can be used as a known text to participate in the process of using the first text for model training. CLM, MLM, MCLM, etc. can be used as training targets, and the first text can be used for model training (in this case, the second text can be used as a known text). Zhiwen participates in the process of model training using the first text).
在一种可能的实现中,可以将可选的4种训练目标(CLM、MLM、MCLM、NoLM)分别应用在“描述-函数对”(Description-Function)的自然语言描述或函数代码上,组合形成12种训练目标,例如:In a possible implementation, the four optional training objectives (CLM, MLM, MCLM, and NoLM) can be applied to the natural language description or function code of the "Description-Function pair" (Description-Function) respectively, and the combination Form 12 types of training objectives, such as:
(1)全因果语言建模(Description-CLM结合Function-CLM):自然语言描述部分应用CLM训练目标,函数代码部分应用CLM训练目标。同时训练自然语言描述理解能力及函数代码生成能力。(1) Full causal language modeling (Description-CLM combined with Function-CLM): CLM training objectives are applied to the natural language description part, and CLM training objectives are applied to the function code part. At the same time, it trains natural language description understanding ability and function code generation ability.
(2)描述掩码语言建模及代码因果语言建模(Description-MLM结合Function-CLM):自然语言描述部分应用MLM训练目标,函数代码部分应用CLM训练目标。同时训练自然语言描述理解能力及函数代码生成能力。(2) Description mask language modeling and code causal language modeling (Description-MLM combined with Function-CLM): the natural language description part applies MLM training objectives, and the function code part applies CLM training objectives. At the same time, it trains natural language description understanding ability and function code generation ability.
(3)描述掩码因果语言建模及代码语言建模(Description-MCLM+Function-CLM):自然语言描述部分应用MCLM训练目标,函数代码部分应用CLM训练目标。同时训练自然语言描述理解能力及函数代码生成能力。(3) Description mask causal language modeling and code language modeling (Description-MCLM+Function-CLM): The natural language description part applies MCLM training objectives, and the function code part applies CLM training objectives. At the same time, it trains natural language description understanding ability and function code generation ability.
(4)描述无语言建模及代码因果语言建模(Description-NoLM+Function-CLM):自然语言描述部分无训练目标,函数代码部分应用CLM训练目标。仅训练函数代码生成能力。(4) Description-NoLM+Function-CLM: Description-NoLM+Function-CLM: The natural language description part has no training objectives, and the function code part applies CLM training objectives. Only train function code generation capabilities.
以此类推,还可包括:By analogy, it can also include:
(5)Description-CLM结合Function-MLM;(5) Description-CLM combined with Function-MLM;
(6)Description-MLM结合Function-MLM;(6) Description-MLM combined with Function-MLM;
(7)Description-MCLM结合Function-MLM;(7) Description-MCLM combined with Function-MLM;
(8)Description-NoLM结合Function-MLM;(8) Description-NoLM combined with Function-MLM;
(9)Description-CLM结合Function-MCLM;(9) Description-CLM combined with Function-MCLM;
(10)Description-MLM结合Function-MCLM;(10) Description-MLM combined with Function-MCLM;
(11)Description-MCLM结合Function-MCLM;(11) Description-MCLM combined with Function-MCLM;
(12)Description-NoLM结合Function-MCLM;(12) Description-NoLM combined with Function-MCLM;
902、根据所述已知文本,通过自然语言模型,对所述预测文本对应文本位置的文本进行预测,得到文本预测结果;所述预测文本和所述文本预测结果用于更新所述自然语言模型。902. According to the known text, use the natural language model to predict the text corresponding to the text position of the predicted text to obtain a text prediction result; the predicted text and the text prediction result are used to update the natural language model .
在一种可能的实现中,在对自然语言模型进行迭代训练的过程中,可以通过自然语言模型处理已知文本,对预测文本对应文本位置的文本进行预测,得到文本预测结果,并基于文本预测结果和对应真值之间的差异来确定损失,该损失可以用于更新所述自然语言模型。In one possible implementation, in the process of iterative training of the natural language model, the known text can be processed through the natural language model, the text corresponding to the text position of the predicted text can be predicted, the text prediction result is obtained, and based on the text prediction The difference between the result and the corresponding ground truth determines the loss, which can be used to update the natural language model.
在一种实现中,可以通过嵌入层对所述已知文本进行嵌入处理,以得到嵌入向量。In one implementation, the known text can be embedded through an embedding layer to obtain an embedding vector.
其中嵌入层可以称为输入嵌入(input embedding)层。当前输入可以为已知文本中的每个文本单元(或者可以称之为已知数据单元)。嵌入层在获取当前输入后,可以对该当前输入中各个已知数据单元进行嵌入处理,可得到各个已知数据单元对应的嵌入向量。The embedding layer can be called the input embedding layer. The current input can be each text unit in the known text (or it can be called a known data unit). After obtaining the current input, the embedding layer can embed each known data unit in the current input to obtain the embedding vector corresponding to each known data unit.
在一些实施例中,还可以获取每个已知数据单元的位置向量,所述位置向量用于指示已知数据单元的位置;其中,位置用于表示已知数据单元在文本(例如第一文本或者第二文本)中的位置,具体的,所述位置用于指示所述已知数据单元与其他已知数据单元以及所述已知数据单元与预测文本中各个文本单元之间的相对位置关系。 In some embodiments, the position vector of each known data unit can also be obtained, and the position vector is used to indicate the position of the known data unit; wherein the position is used to represent the position of the known data unit in the text (for example, the first text Or the position in the second text), specifically, the position is used to indicate the relative positional relationship between the known data unit and other known data units, as well as the known data unit and each text unit in the predicted text. .
在一种实现中,所述嵌入层可以包括输入嵌入层和位置编码(positional encoding)层。在输入嵌入层,可以对当前输入中的各个已知数据单元进行词嵌入处理,从而得到各个已知数据单元的嵌入向量。在位置编码层,可以获取各个已知数据单元在该当前输入中的位置,进而对各个已知数据单元的位置生成位置向量。In one implementation, the embedding layer may include an input embedding layer and a positional encoding layer. In the input embedding layer, word embedding processing can be performed on each known data unit in the current input to obtain the embedding vector of each known data unit. At the position coding layer, the position of each known data unit in the current input can be obtained, and then a position vector is generated for the position of each known data unit.
在一些示例中,各个已知数据单元在文本(例如第一文本或者第二文本)中的位置可以为各个已知数据单元在文本(例如第一文本或者第二文本)中的绝对位置。以当前输入为“几号应还花呗”为例,其中的“几”的位置可以表示为第一位,“号”的位置可以表示为第二位,……。在一些示例中,各个已知数据单元在文本(例如第一文本或者第二文本)中的位置可以为各个已知数据单元在文本(例如第一文本或者第二文本)中的相对位置。仍以当前输入为“几号应还花呗”为例,其中的“几”的位置可以表示为“号”之前,“号”的位置可以表示为“几”之后、“应”之前,……。当得到当前输入中各个已知数据单元的嵌入向量和位置向量时,可以将各个已知数据单元的位置向量和对应的嵌入向量进行融合,得到各个已知数据单元的嵌入向量,即得到该当前输入对应的多个嵌入向量。应理解,融合的方式可以是对嵌入向量和位置向量进行加法运算,或者是通过其他运算使得嵌入向量可以携带文本(例如第一文本或者第二文本)中的一个已知数据单元以及所述一个已知数据单元在所述文本(例如第一文本或者第二文本)中的位置的信息,这里并不限定具体的融合方式。多个嵌入向量可以表示为具有预设维度的嵌入矩阵。可以设定该多个嵌入向量的个数为M,预设维度为H维,则该多个嵌入向量可以表示为M×H的嵌入矩阵。In some examples, the position of each known data unit in the text (eg, the first text or the second text) may be the absolute position of each known data unit in the text (eg, the first text or the second text). Taking the current input as "What number should I pay back the Huabei?" for example, the position of "number" can be represented as the first digit, the position of "number" can be represented as the second digit,... In some examples, the position of each known data unit in the text (eg, the first text or the second text) may be the relative position of each known data unit in the text (eg, the first text or the second text). Still taking the current input as "what number should I pay back Huabei" as an example, the position of "what number" can be expressed as before "number", and the position of "number" can be expressed as after "what number" and before "should",... …. When the embedding vector and position vector of each known data unit in the current input are obtained, the position vector of each known data unit and the corresponding embedding vector can be fused to obtain the embedding vector of each known data unit, that is, the current Enter multiple corresponding embedding vectors. It should be understood that the fusion method may be to add the embedding vector and the position vector, or to use other operations so that the embedding vector can carry a known data unit in the text (such as the first text or the second text) and the one The information on the position of the data unit in the text (for example, the first text or the second text) is known, and the specific fusion method is not limited here. Multiple embedding vectors can be represented as embedding matrices with preset dimensions. The number of the plurality of embedding vectors can be set to M, and the default dimension is H dimension, then the plurality of embedding vectors can be expressed as an M×H embedding matrix.
在一种实现中,还可以通过嵌入层对预测文本在所述文本(例如第一文本或者第二文本)中的位置进行嵌入处理,以得到用于表示所述文本(例如第一文本或者第二文本)中的预测文本在所述文本(例如第一文本或者第二文本)中的位置的嵌入向量,该嵌入向量可以作为后续自然语言模型的输入。其中,所述位置用于指示预测文本中各个文本单元与所述文本(例如第一文本或者第二文本)中各个已知数据单元之间的相对位置关系,关于位置的描述可以参照上述实施例中关于位置的描述,相似之处这里不再赘述。In one implementation, the position of the predicted text in the text (for example, the first text or the second text) can also be embedded through the embedding layer to obtain a method for representing the text (for example, the first text or the second text). The embedding vector of the position of the predicted text in the text (for example, the first text or the second text) in the second text), and the embedding vector can be used as the input of the subsequent natural language model. Wherein, the position is used to indicate the relative position relationship between each text unit in the predicted text and each known data unit in the text (such as the first text or the second text). For a description of the position, please refer to the above embodiment. Regarding the description of the location in , the similarities will not be repeated here.
在一种实现中,自然语言模型可以包括编码器以及解码器,示例性的,编码器以及解码器可以包括多个transformer层。In one implementation, the natural language model may include an encoder and a decoder. For example, the encoder and decoder may include multiple transformer layers.
transformer层的核心特点在于其采用的独特的注意力机制。在处理自然语言,例如一个句子时,transformer模型利用该注意力机制,为句子中各个词的嵌入向量赋予不同的注意力系数,从而更全面地考虑句子中上下文对各个词的影响。具体的transformer层可以包括依次相邻的多头注意力层、加和与归一化(add&norm)层、前馈(feed forward)层、加和与归一化层。其中,注意力层与嵌入层相连,从嵌入层获取多个嵌入向量作为输入向量,基于多个嵌入向量中各个嵌入向量之间的关联度,对各个嵌入向量进行综合,得到多个输出向量,输出给后续的transformer层。transformer层获取前一层的输出作为输入向量,执行与前一级transformer层类似的操作。The core feature of the transformer layer lies in its unique attention mechanism. When processing natural language, such as a sentence, the transformer model uses this attention mechanism to assign different attention coefficients to the embedding vectors of each word in the sentence, thereby more comprehensively considering the impact of the context on each word in the sentence. The specific transformer layer can include sequentially adjacent multi-head attention layers, addition and normalization (add&norm) layers, feed forward layers, and addition and normalization layers. Among them, the attention layer is connected to the embedding layer, and multiple embedding vectors are obtained from the embedding layer as input vectors. Based on the correlation between the embedding vectors in the multiple embedding vectors, each embedding vector is synthesized to obtain multiple output vectors. Output to the subsequent transformer layer. The transformer layer takes the output of the previous layer as an input vector and performs similar operations to the previous transformer layer.
编码器可以得到多个输出向量,解码器可以将多个所述第一输出向量以及所述嵌入向量作为输入,得到预测文本的向量表示,应理解,预测文本的向量表示可以再经过一个分类器(例如可采用支持向量机,softmax分类器,K-近邻算法等)来恢复出预测文本。The encoder can obtain multiple output vectors, and the decoder can use multiple first output vectors and the embedded vectors as inputs to obtain a vector representation of the predicted text. It should be understood that the vector representation of the predicted text can be further passed through a classifier. (For example, support vector machine, softmax classifier, K-nearest neighbor algorithm, etc. can be used) to restore the predicted text.
在一种可能的实现中,可以基于预测文本以及文本预测结构来构建损失,并基于损失更新自然语言模型。In one possible implementation, the loss can be constructed based on the predicted text as well as the text prediction structure, and the natural language model is updated based on the loss.
在一种可能的实现中,可以基于预测文本以及文本预测结构来构建损失,并将损失或者基于损失得到的梯度传递至其他设备(例如云侧的服务器),由云侧的服务器执行基于损失或者梯度来更新自然语言模型的动作。In a possible implementation, the loss can be constructed based on the predicted text and the text prediction structure, and the loss or the gradient obtained based on the loss is transferred to other devices (such as a server on the cloud side), and the server on the cloud side executes the loss based on the loss or the gradient obtained based on the loss. Gradient to update the actions of the natural language model.
接下来结合软件模块,介绍本申请实施例中的一个应用例:Next, combined with the software module, an application example in the embodiment of this application is introduced:
参照图11,本发明针对函数级代码的生成,整体上包括两个阶段的训练策略,以及代码与文本各自选择不同损失函数训练目标的设计,保证模型第一阶段打基础,第二阶段进阶提升能力的训练提升。在数据准备阶段,针对两阶段目标的不同也设计了针对性的函数级代码语料准备机制,保证能够很好地提升所训练得到生成模型在函数代码生成能力上的出色表现。 Referring to Figure 11, the present invention aims at the generation of function-level code. It generally includes a two-stage training strategy, as well as a design of selecting different loss function training targets for code and text respectively, ensuring that the first stage of the model lays the foundation and the second stage is advanced. Training to improve abilities. In the data preparation stage, a targeted function-level code corpus preparation mechanism is also designed based on the different goals of the two stages, ensuring that it can well improve the excellent performance of the trained generative model in function code generation capabilities.
如图11所示,具体可以包含:As shown in Figure 11, it can specifically include:
第一数据模块:提供最原始的数据,收集大量最原始的包含代码的数据,包括但不限于代码工程文件,独立代码片段,脚本文件等。The first data module: provides the most original data and collects a large amount of the most original data containing code, including but not limited to code project files, independent code snippets, script files, etc.
第二数据模块:基于第一数据模块,提供第一函数级代码生成训练模块训练所需的数据,该模块又可选地由以下装置构成:The second data module: based on the first data module, provides the data required for training the first function-level code generation training module. This module is optionally composed of the following devices:
1、第一数据过滤器:对第一数据模块中的数据进行初步过滤,可选地去除文件过长或者单行代码过长的数据,去除目标代码语言或者自然语言语种以外的数据等。1. First data filter: Preliminarily filter the data in the first data module, optionally removing data with too long files or too long single lines of code, removing data in other than target code languages or natural language languages, etc.
2、函数代码段提取器:针对第一数据过滤器所得到的语料数据,识别语料中所有的函数代码段:2. Function code segment extractor: Based on the corpus data obtained by the first data filter, identify all function code segments in the corpus:
可选地使用一定规则、例如通过不同语言的函数定义规则进行识别抽取。Optionally use certain rules, such as function definition rules in different languages, for identification and extraction.
可选地使用现有工具进行代码抽象语法树(AST)分析,对代码进行分析得到对应代码的语法树,通过语法树分析截取对应的函数级代码片段。Optionally use existing tools to perform code abstract syntax tree (AST) analysis, analyze the code to obtain the syntax tree of the corresponding code, and intercept the corresponding function-level code fragments through syntax tree analysis.
可选的使用自然语言处理自动识别的方法进行代码段抽取,例如序列标注的方法,训练函数标注器标注出所有函数级代码。Optionally, use natural language processing automatic identification methods to extract code segments, such as sequence annotation methods, and train a function annotator to annotate all function-level codes.
3、NL-PL样本构造器,识别函数代码片段对应的自然语言描述(如果有):3. NL-PL sample constructor identifies the natural language description corresponding to the function code fragment (if any):
可选地使用的一定的规则、例如通过注释规则,语言字符规则等进行识别抽取。Optionally use certain rules, such as annotation rules, language character rules, etc. for identification and extraction.
可选地使用现有工具进行代码抽象语法树(AST)分析,对代码进行分析得到对应代码的语法树,通过语法树分析截取对应的函数级代码片段对应的自然语言描述。Optionally use existing tools to perform code abstract syntax tree (AST) analysis, analyze the code to obtain the syntax tree of the corresponding code, and intercept the natural language description corresponding to the corresponding function-level code fragment through syntax tree analysis.
可选的使用自然语言处理自动识别的方法进行代码段抽取,例如序列标注的方法,训练函数标注器标注出所有函数级代码对应的自然语言描述。Optionally, use natural language processing automatic identification methods to extract code segments, such as sequence annotation methods, and train a function annotator to annotate the natural language descriptions corresponding to all function-level codes.
抽取完成的代码级函数和其描述组成“自然语言描述-函数实现代码”(Natural Language–Program Language)的句对形式样本。可选的,可以在不同自然语言、程序语言前使用表示该语言的字符组合来表示接下来的序列为文本语言序列或者具体的代码语言序列,并在样本之后添加样本结束符。The extracted code-level function and its description form a sentence pair sample of "Natural Language Description-Function Implementation Code" (Natural Language–Program Language). Optionally, you can use a combination of characters representing the language before different natural languages and programming languages to indicate that the following sequence is a text language sequence or a specific code language sequence, and add a sample terminator after the sample.
4、数据归一器:可选地对函数级代码片段进行归一化,规范代码语法、统一代码风格,使用可选的一定规则、可选的代码抽象语法树(AST)对代码进行解析,抽取纯代码部分,对空格,缩进等风格进行统一化,使得数据归一以提高语料质量。4. Data normalizer: Optionally normalize function-level code fragments, standardize code syntax, unify code style, and use optional certain rules and optional code abstract syntax tree (AST) to parse the code. Extract the pure code part and unify spaces, indentation and other styles to normalize the data to improve the quality of the corpus.
第三数据模块:基于第二数据模块,提供第二函数级代码生成训练模块训练所需的数据,可选的针对性过滤,可选的数据增强该模块又可选地由以下装置构成:The third data module: Based on the second data module, it provides the data required for training the second function-level code generation training module, optional targeted filtering, and optional data enhancement. This module is optionally composed of the following devices:
1、第二数据过滤器:可选地根据代码语料的统计特性,去除代码语料中自然语言描述部分过短的语料、代码部分过长的语料以及两者长度相差过大的语料。以提升训练语料质量。1. Second data filter: Optionally, based on the statistical characteristics of the code corpus, remove the corpus in which the natural language description part is too short, the code part is too long, and the length difference between the two is too large. To improve the quality of training corpus.
2、函数代码增广器:可选地通过对代码中的变量名进行随机替换、(循环)代码段等价替换以及随机插入无效代码段等方式,大量获得高质量自动生成的训练数据,最终提升模型性能。2. Function code augmenter: Optionally, through random replacement of variable names in the code, equivalent replacement of (cyclic) code segments, and random insertion of invalid code segments, a large amount of high-quality automatically generated training data can be obtained. Finally, Improve model performance.
3、自然语言增广器:可选地通过对自然语言描述中进行同义词替换,句式变换,或者使用自然语言处理技术中的复述生成模型或者方法,数据增广方法,数据加噪方法对自然语言描述进行增广,大量获得高质量自动生成的训练数据,最终提升模型性能。3. Natural language augmenter: Optionally replace synonyms and sentence patterns in natural language descriptions, or use paraphrase generation models or methods in natural language processing technology, data augmentation methods, and data noise methods to enhance natural language amplification. The language description is augmented to obtain a large amount of high-quality automatically generated training data, ultimately improving model performance.
第一函数级代码生成训练模块:使用第二数据模块产生的数据,进行函数代码生成的训练,训练可以采用自然语言生成常见的模带解码器的模行,例如GPT。相应的,第一函数级代码生成训练模块还包括训练目标组合器,用来选择确定训练目标。将可选的4种训练目标(objectives,CLM、MLM、MCLM、NoLM)分别应用在“描述-函数对”(Description-Function)的自然语言描述或函数代码上,组合形成12种训练目标。The first function-level code generation training module: uses the data generated by the second data module to perform function code generation training. The training can use natural language to generate the pattern lines of common pattern decoders, such as GPT. Correspondingly, the first function-level code generation training module also includes a training target combiner for selecting and determining training targets. The optional 4 training objectives (objectives, CLM, MLM, MCLM, NoLM) are applied to the natural language description or function code of the "Description-Function pair" (Description-Function) respectively, and combined to form 12 training objectives.
第二函数级代码生成训练模块:使用第三数据模块产生的数据,在第一函数级代码生成训练模块所训练完成模型的基础上,进一步进行函数代码生成的训练,训练可以采用自然语言生成常见的模带解码器的模行,例如GPT。相应的,第一函数级代码生成训练模块还包括训练目标组合器,用来选择确定训练目标。将可选的4种训练目标(objectives,CLM、MLM、MCLM、NoLM)分别应用在“描述-函数对”(Description-Function)的自然语言描述或函数代码上,组合形成12种训练目标。The second function-level code generation training module: uses the data generated by the third data module to further train the function code generation based on the model trained by the first function-level code generation training module. The training can use natural language to generate common A module line with a decoder, such as GPT. Correspondingly, the first function-level code generation training module also includes a training target combiner for selecting and determining training targets. The optional 4 training objectives (objectives, CLM, MLM, MCLM, NoLM) are applied to the natural language description or function code of the "Description-Function pair" (Description-Function) respectively, and combined to form 12 training objectives.
第一函数级代码生成器的结构可以是任意生成式神经网络模型,例如Transformer解码器和RNN,随机初始化其模型参数。The structure of the first function-level code generator can be any generative neural network model, such as Transformer decoder and RNN, with its model parameters randomly initialized.
第一函数级代码生成器以第二数据模块输出的语料为训练数据。The first function-level code generator uses the corpus output by the second data module as training data.
第一函数级代码生成器以第一组合优化器进行模型参数更新。第一组合优化器分别对自然语言描述部分和程序语言函数代码部分进行优化。The first function-level code generator uses the first combination optimizer to update model parameters. The first combination optimizer optimizes the natural language description part and the programming language function code part respectively.
优化自然语言描述部分时,第一组合优化器可选CLM、MLM、MCLM、NoLM四种训练目标。优化程序语言函数代码部分时,第一组合优化器可选CLM、MLM、MCLM三种训练目标,注意此处无NoLM训练目标。自然语言描述部分和程序语言函数代码部分训练目标可自由组合。When optimizing the natural language description part, the first combination optimizer can choose four training objectives: CLM, MLM, MCLM, and NoLM. When optimizing the program language function code part, the first combination optimizer can choose three training objectives: CLM, MLM, and MCLM. Note that there is no NoLM training objective here. The training objectives of the natural language description part and the programming language function code part can be freely combined.
步骤1:获取函数级代码生成器的训练数据Step 1: Obtain training data for the function-level code generator
第一数据模块获取已有的程序语言语料及自然语言语料。可以从Github网站上爬取源代码文件,其中包括自然语言描述及程序语言代码。The first data module acquires existing programming language corpus and natural language corpus. Source code files can be crawled from the Github website, including natural language descriptions and programming language codes.
步骤2:制备第一函数级代码生成器的训练数据Step 2: Prepare training data for the first function-level code generator
第二数据模块构造第一函数级代码生成器训练所需的数据,可选的进行数据过滤及清洗,可选的进行数据增强。The second data module constructs the data required for training the first function-level code generator, optionally performs data filtering and cleaning, and optionally performs data enhancement.
第二数据模块从第一数据模块输出的语料或第二数据模块过滤及清洗后的语料中提取程序语言函数级代码片段及其相应的自然语言描述,构造“自然语言描述-程序语言函数代码对”。可选的通过一些规则方法提取程序语言函数级代码片段及其相应的自然语言描述,可选的通过抽象语法树(AST)提取程序语言函数级代码片段及其相应的自然语言描述,可选的通过其他自动化方法提取程序语言函数级代码片段及其相应的自然语言描述。可选的从混合程序语言及自然语言的代码文件中提取程序语言函数代码、自然语言描述,可选的从程序语言的代码文件中提取程序语言函数代码,可选的从自然语言的文件中提取自然语言描述。可选的通过混合程序语言及自然语言的代码文件中程序语言代码片段与自然语言描述的对应关系构造“自然语言描述-程序语言函数代码对”,可选的通过其他程序语言代码片段与自然语言描述的对应关系构造“自然语言描述-程序语言函数代码对”。The second data module extracts the program language function-level code fragments and their corresponding natural language descriptions from the corpus output by the first data module or the filtered and cleaned corpus of the second data module, and constructs a "natural language description-program language function code pair" ". Optionally, extract program language function-level code snippets and their corresponding natural language descriptions through some rule methods, optionally extract program language function-level code snippets and their corresponding natural language descriptions through abstract syntax trees (AST), optionally Extract program language function-level code snippets and their corresponding natural language descriptions through other automated methods. Optionally extract the programming language function code and natural language description from the mixed programming language and natural language code files; optionally extract the programming language function code from the programming language code file; optionally extract the programming language function code from the natural language file. Natural language description. Optionally, a "natural language description-programming language function code pair" can be constructed through the corresponding relationship between the program language code snippets and the natural language description in the mixed programming language and natural language code files, and optionally through the correspondence between the program language code snippets and the natural language description. The corresponding relationship of description constructs "natural language description-programming language function code pair".
可以将上例中的“自然语言描述”与“程序语言函数代码”构造为一个“自然语言描述-程序语言函数代码对”。The "natural language description" and "programming language function code" in the above example can be constructed as a "natural language description-programming language function code pair".
步骤3:训练第一函数级代码生成器Step 3: Train the first function-level code generator
第一函数级代码生成器的结构可以是任意生成式神经网络模型,可以是Transformer解码器,例如:The structure of the first function-level code generator can be any generative neural network model, or a Transformer decoder, for example:
第一函数级代码生成器以第二数据模块输出的语料为训练数据。无需标注信息,仅需要第二数据模块输出的无标注语料。例如,将示例函数一“classifier”的“自然语言描述”部分置于图12的<descr>与<python>之间,将示例函数一“classifier”的“程序语言函数代码”部分置于图12的<python>之后,以此作为第一函数级代码生成器的训练输入。The first function-level code generator uses the corpus output by the second data module as training data. No annotation information is required, only the unannotated corpus output by the second data module is required. For example, place the "natural language description" part of the example function "classifier" between <descr> and <python> in Figure 12, and place the "programming language function code" part of the example function "classifier" between After <python> in Figure 12, this is used as the training input of the first function-level code generator.
第一函数级代码生成器以第一组合优化器进行模型参数更新。第一组合优化器分别对自然语言描述部分和程序语言函数代码部分进行优化。优化自然语言描述部分时,第一组合优化器可选CLM、MLM、MCLM、NoLM四种训练目标。优化程序语言函数代码部分时,第一组合优化器可选CLM、MLM、MCLM三种训练目标,注意此处无NoLM训练目标。自然语言描述部分和程序语言函数代码部分训练目标可自由组合。以“自然语言描述”部分选用NoLM,“程序语言函数代码”部分选用CLM为例:The first function-level code generator uses the first combination optimizer to update model parameters. The first combination optimizer optimizes the natural language description part and the programming language function code part respectively. When optimizing the natural language description part, the first combination optimizer can choose four training objectives: CLM, MLM, MCLM, and NoLM. When optimizing the program language function code part, the first combination optimizer can choose three training objectives: CLM, MLM, and MCLM. Note that there is no NoLM training objective here. The training objectives of the natural language description part and the programming language function code part can be freely combined. Take NoLM as the "natural language description" part and CLM as the "programming language function code" part as an example:
如图13所示,可以对图13中“自然语言描述”部分不进行Loss计算,“程序语言函数代码”部分进行CLM Loss计算As shown in Figure 13, Loss calculation can not be performed on the "Natural Language Description" part in Figure 13, and CLM Loss calculation can be performed on the "Programming Language Function Code" part.
步骤4:制备第二函数级代码生成器的训练数据Step 4: Prepare training data for the second function-level code generator
第三数据模块构造第二函数级代码生成器训练所需的数据。第三数据模块对第二数据模块输出的语料进行过滤和可选的数据增强。The third data module constructs the data required for training the second function-level code generator. The third data module performs filtering and optional data enhancement on the corpus output by the second data module.
第三数据模块对第二数据模块输出的语料进行过滤,使训练数据更接近第二函数级代码生成器的应用领域。过滤基于第二数据模块输出的数据样本与第二函数级代码生成器的应用领域的数据样本的相似度。可以对比两个数据集的“自然语言描述”部分的长度,例如统计第二函数级代码生成器的应用领域数据中“自然语言描述”之最大长度Lmax、最小长度Lmin,仅选取第二数据模块输出的数据中“自然语言描述”长度l符合Lmin≤l≤Lmax的样本,以此作为第三数据模块的输出。 The third data module filters the corpus output by the second data module to make the training data closer to the application field of the second function-level code generator. The filtering is based on similarity between the data sample output by the second data module and the data sample in the application domain of the second function-level code generator. You can compare the length of the "natural language description" part of the two data sets, for example, count the maximum length L max and minimum length L min of the "natural language description" in the application field data of the second function-level code generator, and select only the second The samples whose "natural language description" length l in the data output by the data module conforms to L min ≤ l ≤ L max are used as the output of the third data module.
步骤5:训练第二函数级代码生成器Step 5: Train the second function-level code generator
第二函数级代码生成器的结构与第一函数级代码生成器的结构保持一致,并第一函数级代码生成器的模型参数初始化第二函数级代码生成器。The structure of the second function-level code generator is consistent with the structure of the first function-level code generator, and the second function-level code generator is initialized with model parameters of the first function-level code generator.
第二函数级代码生成器以第三数据模块输出的语料为训练数据。无需标注信息,仅需要第三数据模块输出的无标注语料。The second function-level code generator uses the corpus output by the third data module as training data. No annotation information is required, only the unannotated corpus output by the third data module is required.
第二函数级代码生成器以第二组合优化器进行模型参数更新。第二组合优化器分别对自然语言描述部分和程序语言函数代码部分进行优化。优化自然语言描述部分时,第二组合优化器可选CLM、MLM、MCLM、NoLM四种训练目标。优化程序语言函数代码部分时,第二组合优化器可选CLM、MLM、MCLM三种训练目标,注意此处无NoLM训练目标。自然语言描述部分和程序语言函数代码部分训练目标可自由组合。The second function-level code generator uses the second combinatorial optimizer to update model parameters. The second combination optimizer optimizes the natural language description part and the programming language function code part respectively. When optimizing the natural language description part, the second combination optimizer can choose four training objectives: CLM, MLM, MCLM, and NoLM. When optimizing the program language function code part, the second combination optimizer can choose three training objectives: CLM, MLM, and MCLM. Note that there is no NoLM training objective here. The training objectives of the natural language description part and the programming language function code part can be freely combined.
步骤6:优化第二函数级代码生成器的推理参数Step 6: Optimize the inference parameters of the second function-level code generator
使用第二函数级代码生成器进行函数级代码生成时,可选的基于随机采样(sampling)生成或束搜索(beam search)生成,或其他策略生成。此代码生成过程中,存在可选的推理参数影响生成结果的随机性,例如温度(temperature)。可选的,在第三数据模块输出的语料中预留一部分数据不参与第二函数级代码生成器的训练,作为开发集(development set),基于此开发集,可调整前述推理参数,提高第二函数级代码生成器生成代码的正确性。When using the second function-level code generator to generate function-level code, it can optionally be generated based on random sampling (sampling) generation or beam search (beam search) generation, or other strategy generation. During this code generation process, there are optional inference parameters that affect the randomness of the generated results, such as temperature. Optionally, a part of the data output by the third data module is reserved as a development set (development set), which does not participate in the training of the second function-level code generator. Based on this development set, the aforementioned inference parameters can be adjusted to improve the performance of the third function-level code generator. Correctness of code generated by a two-function-level code generator.
本实施例基于Github公开的数据,采用第一、第二、第三数据模块采集、处理出了第一、第二函数级代码生成器的训练数据,并采用第一、第二组合优化器进行训练。可以对输入的“自然语言描述”生成相应的“程序语言函数代码”(Python)。经比较测试,使用本发明获得的代码生成器,在公开的HumanEval测试任务上,可以取得17.07%的Pass@1评分,显著超过当前公开的最佳得分(13.17%)。This embodiment is based on the data disclosed by Github, uses the first, second, and third data modules to collect and process the training data of the first and second function-level code generators, and uses the first and second combined optimizers to perform train. The corresponding "program language function code" (Python) can be generated for the input "natural language description". After comparative testing, using the code generator obtained by the present invention can achieve a Pass@1 score of 17.07% on the public HumanEval test task, significantly exceeding the currently public best score (13.17%).
与现有技术相比,本实例所设计的基于预训练和自然语言描述的函数级代码生成器,可根据自然语言输入,生成对应功能的程序语言函数片段,函数级代码正确性更高。另外,本方案利用公开的Github数据训练模型,数据获取容易,可持续扩展。Compared with the existing technology, the function-level code generator designed in this example based on pre-training and natural language description can generate program language function fragments corresponding to the functions based on natural language input, and the function-level code is more accurate. In addition, this solution uses public Github data to train the model, making data acquisition easy and sustainable expansion.
参照图14,图14为本申请实施例提供的一种模型训练装置的结构示意,如图14所示,本申请实施例提供的一种模型训练装置1400,包括:Referring to Figure 14, Figure 14 is a schematic structural diagram of a model training device provided by an embodiment of the present application. As shown in Figure 14, a model training device 1400 provided by an embodiment of the present application includes:
获取模块1401,用于获取第一文本以及第二文本,所述第一文本和所述第二文本为从原始代码文本中提取得到的;其中,所述第一文本为程序代码,所述第二文本为所述程序代码对应的属性描述;所述第一文本和所述第二文本包括已知文本以及预测文本;The acquisition module 1401 is used to acquire a first text and a second text, where the first text and the second text are extracted from the original code text; wherein the first text is a program code, and the third text is a program code. The second text is the attribute description corresponding to the program code; the first text and the second text include known text and predicted text;
其中,关于获取模块1401的具体描述可以参照上述实施例中步骤901的介绍,这里不再赘述。For a specific description of the acquisition module 1401, please refer to the introduction of step 901 in the above embodiment, and will not be described again here.
预测模块1402,用于根据所述已知文本,通过自然语言模型,对所述预测文本对应文本位置的文本进行预测,得到文本预测结果;所述预测文本和所述文本预测结果用于更新所述自然语言模型。The prediction module 1402 is used to predict the text corresponding to the text position of the predicted text through the natural language model according to the known text, and obtain a text prediction result; the predicted text and the text prediction result are used to update the Describe the natural language model.
其中,关于预测模块1402的具体描述可以参照上述实施例中步骤902的介绍,这里不再赘述。For a specific description of the prediction module 1402, please refer to the introduction of step 902 in the above embodiment, and will not be described again here.
在一种可能的实现中,所述属性描述包括所述程序代码功能描述或者所述程序代码的实现原理。In a possible implementation, the attribute description includes a function description of the program code or an implementation principle of the program code.
在一种可能的实现中,所述装置还包括:In a possible implementation, the device further includes:
文本位置确定模块,用于在对所述第一文本中的文本进行预测时,通过第一方式,从所述第一文本中确定所述预测文本对应的文本位置;A text position determination module, configured to determine the text position corresponding to the predicted text from the first text in a first manner when predicting the text in the first text;
在对所述第二文本中的文本进行预测时,通过第二方式,从所述第二文本中确定所述预测文本对应的文本位置;所述第一方式和所述第二方式不同。When predicting the text in the second text, the text position corresponding to the predicted text is determined from the second text through the second method; the first method and the second method are different.
在一种可能的实现中,所述第一方式和所述第二方式分别为如下预测方式的一种:In a possible implementation, the first method and the second method are respectively one of the following prediction methods:
对所述第一文本或所述第二文本的部分或全部文本的文本位置中采样掩码位置,所述掩码位置作为所述预测文本对应的文本位置; Sample mask positions among text positions of part or all of the first text or second text, and use the mask positions as the text positions corresponding to the predicted text;
将所述已知文本的下文的文本位置作为所述预测文本对应的文本位置;其中,所述已知文本为所述自然语言模型已预测的全部文本;Use the following text position of the known text as the text position corresponding to the predicted text; wherein the known text is all the text that has been predicted by the natural language model;
将所述已知文本的下文的文本位置作为所述预测文本对应的文本位置;其中,所述已知文本为所述自然语言模型已预测的全部文本中未被掩码的文本。The following text position of the known text is used as the text position corresponding to the predicted text; wherein the known text is the unmasked text among all the texts that have been predicted by the natural language model.
在一种可能的实现中,所述文本位置确定模块,还用于:在不对第二文本中的文本进行预测的情况下,通过如下预测方式的一种确定所述预测文本对应的文本位置:In a possible implementation, the text position determination module is also configured to determine the text position corresponding to the predicted text through one of the following prediction methods without predicting the text in the second text:
对所述第一文本的部分或全部文本的文本位置中采样掩码位置,所述掩码位置作为所述预测文本对应的文本位置;Sample mask positions from text positions of part or all of the first text, and use the mask positions as text positions corresponding to the predicted text;
将所述已知文本的下文的文本位置作为所述预测文本对应的文本位置;其中,所述已知文本为所述自然语言模型已预测的全部文本;Use the following text position of the known text as the text position corresponding to the predicted text; wherein the known text is all the text that has been predicted by the natural language model;
将所述已知文本的下文的文本位置作为所述预测文本对应的文本位置;其中,所述已知文本为所述自然语言模型已预测的全部文本中未被掩码的文本。The following text position of the known text is used as the text position corresponding to the predicted text; wherein the known text is the unmasked text among all the texts that have been predicted by the natural language model.
在一种可能的实现中,In one possible implementation,
所述第一文本为根据从原始代码文本中提取得到的第一原始文本得到的;其中,所述第一文本为对所述第一原始文本中的部分文本单元进行修改得到的;或者,The first text is obtained based on the first original text extracted from the original code text; wherein the first text is obtained by modifying some text units in the first original text; or,
所述第二文本为根据从原始代码文本中提取得到的第二原始文本得到的;其中,所述第二文本为对所述第二原始文本中的部分文本单元进行修改得到的。The second text is obtained based on the second original text extracted from the original code text; wherein the second text is obtained by modifying some text units in the second original text.
在一种可能的实现中,所述修改包括:删除、增加或者替换。In a possible implementation, the modification includes: deletion, addition or replacement.
在一种可能的实现中,所述替换具体为相同语义的替换。In a possible implementation, the replacement is specifically a replacement with the same semantics.
在一种可能的实现中,所述第一文本或所述第二文本为从原始代码文本中提取的适配于所述程序代码的应用领域的文本。In a possible implementation, the first text or the second text is text extracted from the original code text and adapted to the application field of the program code.
在一种可能的实现中,所述适配于所述程序代码的应用领域,包括:In a possible implementation, the application fields adapted to the program code include:
文本长度或者单行文本长度小于阈值的文本;其中,所述阈值与所述程序代码的应用领域有关。The text length or the length of a single line of text is less than a threshold; wherein the threshold is related to the application field of the program code.
接下来介绍本申请实施例提供的一种执行设备,请参阅图15,图15为本申请实施例提供的执行设备的一种结构示意图,执行设备1500具体可以表现为虚拟现实VR设备、手机、平板、笔记本电脑、智能穿戴设备、监控数据处理设备或服务器等,此处不做限定。具体的,执行设备1500包括:接收器1501、发射器1502、处理器1503和存储器1504(其中执行设备1500中的处理器1503的数量可以一个或多个,图15中以一个处理器为例),其中,处理器1503可以包括应用处理器15031和通信处理器15032。在本申请的一些实施例中,接收器1501、发射器1502、处理器1503和存储器1504可通过总线或其它方式连接。Next, an execution device provided by an embodiment of the present application is introduced. Please refer to Figure 15. Figure 15 is a schematic structural diagram of an execution device provided by an embodiment of the present application. The execution device 1500 can be embodied as a virtual reality VR device, a mobile phone, Tablets, laptops, smart wearable devices, monitoring data processing equipment or servers, etc. are not limited here. Specifically, the execution device 1500 includes: a receiver 1501, a transmitter 1502, a processor 1503 and a memory 1504 (the number of processors 1503 in the execution device 1500 can be one or more, one processor is taken as an example in Figure 15) , wherein the processor 1503 may include an application processor 15031 and a communication processor 15032. In some embodiments of the present application, the receiver 1501, the transmitter 1502, the processor 1503, and the memory 1504 may be connected through a bus or other means.
存储器1504可以包括只读存储器和随机存取存储器,并向处理器1503提供指令和数据。存储器1504的一部分还可以包括非易失性随机存取存储器(non-volatile random access memory,NVRAM)。存储器1504存储有处理器和操作指令、可执行模块或者数据结构,或者它们的子集,或者它们的扩展集,其中,操作指令可包括各种操作指令,用于实现各种操作。Memory 1504 may include read-only memory and random access memory and provides instructions and data to processor 1503 . A portion of memory 1504 may also include non-volatile random access memory (NVRAM). The memory 1504 stores processor and operating instructions, executable modules or data structures, or a subset thereof, or an extended set thereof, where the operating instructions may include various operating instructions for implementing various operations.
处理器1503控制执行设备的操作。具体的应用中,执行设备的各个组件通过总线系统耦合在一起,其中总线系统除包括数据总线之外,还可以包括电源总线、控制总线和状态信号总线等。但是为了清楚说明起见,在图中将各种总线都称为总线系统。The processor 1503 controls the execution of operations of the device. In specific applications, various components of the execution device are coupled together through a bus system. In addition to the data bus, the bus system may also include a power bus, a control bus, a status signal bus, etc. However, for the sake of clarity, various buses are called bus systems in the figure.
上述本申请实施例揭示的方法可以应用于处理器1503中,或者由处理器1503实现。处理器1503可以是一种集成电路芯片,具有信号的处理能力。在实现过程中,上述方法的各步骤可以通过处理器1503中的硬件的集成逻辑电路或者软件形式的指令完成。上述的处理器1503可以是通用处理器、数字信号处理器(digital signal processing,DSP)、微处理器或微控制器,还可进一步包括专用集成电路(application specific integrated circuit,ASIC)、现场可编程门阵列(field-programmable gate array,FPGA)或者其他可编程逻辑器件、分立门或者晶体管逻辑器件、分立硬件组件。该处理器1503可以实现或者执行本申请实施例中的公开的各方法、步骤及逻辑框图。通用处理器可以是微处理器或者该处理器也可以是任何常规的处理器等。结合本申请实施例所公开的方法的步骤可以直接体现为硬件译码处理器执行完成,或者用译码处理器中的硬件及软件模块组合执行完成。软件模块可以位于随机存储器,闪存、只读存储器,可编程只读存储器或者电可擦写可编程存储器、寄存器等本领域成熟的存储介质中。该存储介质位于存储器1504,处理器1503读取存储器1504中的信息,结合其硬件完成上述方法中涉及模型推理过程的步骤。The methods disclosed in the above embodiments of the present application can be applied to the processor 1503 or implemented by the processor 1503. The processor 1503 may be an integrated circuit chip with signal processing capabilities. During the implementation process, each step of the above method can be completed by instructions in the form of hardware integrated logic circuits or software in the processor 1503 . The above-mentioned processor 1503 can be a general-purpose processor, a digital signal processor (DSP), a microprocessor or a microcontroller, and can further include an application specific integrated circuit (ASIC), a field programmable Gate array (field-programmable gate array, FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components. The processor 1503 can implement or execute each method, step and logical block diagram disclosed in the embodiment of this application. A general-purpose processor may be a microprocessor or the processor may be any conventional processor, etc. The steps of the method disclosed in conjunction with the embodiments of the present application can be directly implemented by a hardware decoding processor, or executed by a combination of hardware and software modules in the decoding processor. The software module can be located in random access memory, flash memory, read-only memory, programmable read-only memory or electrically erasable programmable memory, registers and other mature storage media in this field. The storage medium is located in the memory 1504. The processor 1503 reads the information in the memory 1504 and completes the steps related to the model inference process in the above method in combination with its hardware.
接收器1501可用于接收输入的数字或字符信息,以及产生与执行设备的相关设置以及功能控制有关的信号输入。发射器1502可用于通过第一接口输出数字或字符信息;发射器1502还可用于通过第一接口向磁盘组发送指令,以修改磁盘组中的数据;发射器1502还可以包括显示屏等显示设备。The receiver 1501 may be configured to receive input numeric or character information and generate signal inputs related to performing relevant settings and functional controls of the device. The transmitter 1502 can be used to output numeric or character information through the first interface; the transmitter 1502 can also be used to send instructions to the disk group through the first interface to modify the data in the disk group; the transmitter 1502 can also include a display device such as a display screen .
本申请实施例还提供了一种训练设备,请参阅图16,图16是本申请实施例提供的训练设备一种结构示意图,具体的,训练设备1600由一个或多个服务器实现,训练设备1600可因配置或性能不同而产生比较大的差异,可以包括一个或一个以上中央处理器(central processing units,CPU)1616(例如,一个或一个以上处理器)和存储器1632,一个或一个以上存储应用程序1642或数据1644的存储介质1630(例如一个或一个以上海量存储设备)。其中,存储器1632和存储介质1630可以是短暂存储或持久存储。存储在存储介质1630的程序可以包括一个或一个以上模块(图示没标出),每个模块可以包括对训练设备中的一系列指令操作。更进一步地,中央处理器1616可以设置为与存储介质1630通信,在训练设备1600上执行存储介质1630中的一系列指令操作。The embodiment of the present application also provides a training device. Please refer to Figure 16. Figure 16 is a schematic structural diagram of the training device provided by the embodiment of the present application. Specifically, the training device 1600 is implemented by one or more servers. The training device 1600 There may be relatively large differences due to different configurations or performance, which may include one or more central processing units (CPU) 1616 (for example, one or more processors) and memory 1632, one or more storage applications Storage medium 1630 for program 1642 or data 1644 (eg, one or more mass storage devices). Among them, the memory 1632 and the storage medium 1630 may be short-term storage or persistent storage. The program stored in the storage medium 1630 may include one or more modules (not shown in the figure), and each module may include a series of instruction operations in the training device. Furthermore, the central processor 1616 may be configured to communicate with the storage medium 1630 and execute a series of instruction operations in the storage medium 1630 on the training device 1600 .
训练设备1600还可以包括一个或一个以上电源1626,一个或一个以上有线或无线网络接口1650,一个或一个以上输入输出接口1658;或,一个或一个以上操作系统1641,例如Windows ServerTM,Mac OS XTM,UnixTM,LinuxTM,FreeBSDTM等等。The training device 1600 may also include one or more power supplies 1626, one or more wired or wireless network interfaces 1650, one or more input and output interfaces 1658; or, one or more operating systems 1641, such as Windows ServerTM, Mac OS XTM , UnixTM, LinuxTM, FreeBSDTM and so on.
本申请实施例中,中央处理器1616,用于执行上述实施例中和模型训练相关的动作。In the embodiment of this application, the central processor 1616 is used to perform actions related to model training in the above embodiment.
本申请实施例中还提供一种包括计算机程序产品,当其在计算机上运行时,使得计算机执行如前述执行设备所执行的步骤,或者,使得计算机执行如前述训练设备所执行的步骤。An embodiment of the present application also provides a computer program product that, when run on a computer, causes the computer to perform the steps performed by the foregoing execution device, or causes the computer to perform the steps performed by the foregoing training device.
本申请实施例中还提供一种计算机可读存储介质,该计算机可读存储介质中存储有用于进行信号处理的程序,当其在计算机上运行时,使得计算机执行如前述执行设备所执行的步骤,或者,使得计算机执行如前述训练设备所执行的步骤。Embodiments of the present application also provide a computer-readable storage medium. The computer-readable storage medium stores a program for performing signal processing. When the program is run on a computer, it causes the computer to perform the steps performed by the aforementioned execution device. , or, causing the computer to perform the steps performed by the aforementioned training device.
本申请实施例提供的执行设备、训练设备或终端设备具体可以为芯片,芯片包括:处理单元和通信单元,所述处理单元例如可以是处理器,所述通信单元例如可以是输入/输出接口、管脚或电路等。该处理单元可执行存储单元存储的计算机执行指令,以使执行设备内的芯片执行上述实施例描述的数据处理方法,或者,以使训练设备内的芯片执行上述实施例描述的数据处理方法。可选地,所述存储单元为所述芯片内的存储单元,如寄存器、缓存等,所述存储单元还可以是所述无线接入设备端内的位于所述芯片外部的存储单元,如只读存储器(read-only memory,ROM)或可存储静态信息和指令的其他类型的静态存储设备,随机存取存储器(random access memory,RAM)等。The execution device, training device or terminal device provided by the embodiment of the present application may specifically be a chip. The chip includes: a processing unit and a communication unit. The processing unit may be, for example, a processor. The communication unit may be, for example, an input/output interface. Pins or circuits, etc. The processing unit can execute the computer execution instructions stored in the storage unit, so that the chip in the execution device executes the data processing method described in the above embodiment, or so that the chip in the training device executes the data processing method described in the above embodiment. Optionally, the storage unit is a storage unit within the chip, such as a register, cache, etc. The storage unit may also be a storage unit located outside the chip in the wireless access device, such as Read-only memory (ROM) or other types of static storage devices that can store static information and instructions, random access memory (random access memory, RAM), etc.
具体的,请参阅图17,图17为本申请实施例提供的芯片的一种结构示意图,所述芯片可以表现为神经网络处理器NPU 1700,NPU 1700作为协处理器挂载到主CPU(Host CPU)上,由Host CPU分配任务。NPU的核心部分为运算电路1703,通过控制器1704控制运算电路1703提取存储器中的矩阵数据并进行乘法运算。 Specifically, please refer to Figure 17. Figure 17 is a structural schematic diagram of a chip provided by an embodiment of the present application. The chip can be represented as a neural network processor NPU 1700. The NPU 1700 serves as a co-processor and is mounted to the main CPU (Host). CPU), tasks are allocated by the Host CPU. The core part of the NPU is the arithmetic circuit 1703. The arithmetic circuit 1703 is controlled by the controller 1704 to extract the matrix data in the memory and perform multiplication operations.
在一些实现中,运算电路1703内部包括多个处理单元(Process Engine,PE)。在一些实现中,运算电路1703是二维脉动阵列。运算电路1703还可以是一维脉动阵列或者能够执行例如乘法和加法这样的数学运算的其它电子线路。在一些实现中,运算电路1703是通用的矩阵处理器。In some implementations, the computing circuit 1703 internally includes multiple processing units (Process Engine, PE). In some implementations, arithmetic circuit 1703 is a two-dimensional systolic array. The arithmetic circuit 1703 may also be a one-dimensional systolic array or other electronic circuit capable of performing mathematical operations such as multiplication and addition. In some implementations, arithmetic circuit 1703 is a general-purpose matrix processor.
举例来说,假设有输入矩阵A,权重矩阵B,输出矩阵C。运算电路从权重存储器1702中取矩阵B相应的数据,并缓存在运算电路中每一个PE上。运算电路从输入存储器1701中取矩阵A数据与矩阵B进行矩阵运算,得到的矩阵的部分结果或最终结果,保存在累加器(accumulator)1708中。For example, assume there is an input matrix A, a weight matrix B, and an output matrix C. The arithmetic circuit obtains the corresponding data of matrix B from the weight memory 1702 and caches it on each PE in the arithmetic circuit. The operation circuit takes matrix A data and matrix B from the input memory 1701 to perform matrix operations, and the partial result or final result of the matrix is stored in an accumulator (accumulator) 1708 .
统一存储器1706用于存放输入数据以及输出数据。权重数据直接通过存储单元访问控制器(Direct Memory Access Controller,DMAC)1705,DMAC被搬运到权重存储器1702中。输入数据也通过DMAC被搬运到统一存储器1706中。The unified memory 1706 is used to store input data and output data. The weight data directly passes through the storage unit access controller (Direct Memory Access Controller, DMAC) 1705, and the DMAC is transferred to the weight memory 1702. Input data is also transferred to unified memory 1706 via DMAC.
BIU为Bus Interface Unit即,总线接口单元1710,用于AXI总线与DMAC和取指存储器(Instruction Fetch Buffer,IFB)1709的交互。BIU is the Bus Interface Unit, that is, the bus interface unit 1710, which is used for the interaction between the AXI bus and the DMAC and the Instruction Fetch Buffer (IFB) 1709.
总线接口单元1710(Bus Interface Unit,简称BIU),用于取指存储器1709从外部存储器获取指令,还用于存储单元访问控制器1705从外部存储器获取输入矩阵A或者权重矩阵B的原数据。The bus interface unit 1710 (Bus Interface Unit, BIU for short) is used to fetch the memory 1709 to obtain instructions from the external memory, and is also used for the storage unit access controller 1705 to obtain the original data of the input matrix A or the weight matrix B from the external memory.
DMAC主要用于将外部存储器DDR中的输入数据搬运到统一存储器1706或将权重数据搬运到权重存储器1702中或将输入数据数据搬运到输入存储器1701中。DMAC is mainly used to transfer the input data in the external memory DDR to the unified memory 1706 or the weight data to the weight memory 1702 or the input data to the input memory 1701 .
向量计算单元1707包括多个运算处理单元,在需要的情况下,对运算电路1703的输出做进一步处理,如向量乘,向量加,指数运算,对数运算,大小比较等等。主要用于神经网络中非卷积/全连接层网络计算,如Batch Normalization(批归一化),像素级求和,对特征平面进行上采样等。The vector calculation unit 1707 includes multiple arithmetic processing units. If necessary, the output of the arithmetic circuit 1703 is further processed, such as vector multiplication, vector addition, exponential operation, logarithmic operation, size comparison, etc. Mainly used for non-convolutional/fully connected layer network calculations in neural networks, such as Batch Normalization, pixel-level summation, upsampling of feature planes, etc.
在一些实现中,向量计算单元1707能将经处理的输出的向量存储到统一存储器1706。例如,向量计算单元1707可以将线性函数;或,非线性函数应用到运算电路1703的输出,例如对卷积层提取的特征平面进行线性插值,再例如累加值的向量,用以生成激活值。在一些实现中,向量计算单元1707生成归一化的值、像素级求和的值,或二者均有。在一些实现中,处理过的输出的向量能够用作到运算电路1703的激活输入,例如用于在神经网络中的后续层中的使用。In some implementations, vector calculation unit 1707 can store the processed output vectors to unified memory 1706 . For example, the vector calculation unit 1707 can apply a linear function; or a nonlinear function to the output of the operation circuit 1703, such as linear interpolation on the feature plane extracted by the convolution layer, or a vector of accumulated values, to generate an activation value. In some implementations, vector calculation unit 1707 generates normalized values, pixel-wise summed values, or both. In some implementations, the processed output vector can be used as an activation input to the arithmetic circuit 1703, such as for use in a subsequent layer in a neural network.
控制器1704连接的取指存储器(instruction fetch buffer)1709,用于存储控制器1704使用的指令;The instruction fetch buffer 1709 connected to the controller 1704 is used to store instructions used by the controller 1704;
统一存储器1706,输入存储器1701,权重存储器1702以及取指存储器1709均为On-Chip存储器。外部存储器私有于该NPU硬件架构。The unified memory 1706, the input memory 1701, the weight memory 1702 and the fetch memory 1709 are all On-Chip memories. External memory is private to the NPU hardware architecture.
其中,上述任一处提到的处理器,可以是一个通用中央处理器,微处理器,ASIC,或一个或多个用于控制上述程序执行的集成电路。The processor mentioned in any of the above places can be a general central processing unit, a microprocessor, an ASIC, or one or more integrated circuits used to control the execution of the above programs.
另外需说明的是,以上所描述的装置实施例仅仅是示意性的,其中所述作为分离部件说明的单元可以是或者也可以不是物理上分开的,作为单元显示的部件可以是或者也可以不是物理单元,即可以位于一个地方,或者也可以分布到多个网络单元上。可以根据实际的需要选择其中的部分或者全部模块来实现本实施例方案的目的。另外,本申请提供的装置实施例附图中,模块之间的连接关系表示它们之间具有通信连接,具体可以实现为一条或多条通信总线或信号线。In addition, it should be noted that the device embodiments described above are only illustrative. The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physically separate. The physical unit can be located in one place, or it can be distributed across multiple network units. Some or all of the modules can be selected according to actual needs to achieve the purpose of the solution of this embodiment. In addition, in the drawings of the device embodiments provided in this application, the connection relationship between modules indicates that there are communication connections between them, which can be specifically implemented as one or more communication buses or signal lines.
通过以上的实施方式的描述,所属领域的技术人员可以清楚地了解到本申请可借助软件加必需的通用硬件的方式来实现,当然也可以通过专用硬件包括专用集成电路、专用CPU、专用存储器、专用元器件等来实现。一般情况下,凡由计算机程序完成的功能都可以很容易地用相应的硬件来实现,而且,用来实现同一功能的具体硬件结构也可以是多种多样的,例如模拟电路、数字电路或专用电路等。但是,对本申请而言更多情况下软件程序实现是更佳的实施方式。基于这样的理解,本申请的技术方案本质上或者说对现有技术做出贡献的部分可以以软件产品的形式体现出来,该计算机软件产品存储在可读取的存储介质中,如计算机的软盘、U盘、移动硬盘、ROM、RAM、磁碟或者光盘等,包括若干指令用以使得一台计算机设备(可以是个人计算机,训练设备,或者网络设备等)执行本申请各个实施例所述的方法。Through the above description of the embodiments, those skilled in the art can clearly understand that the present application can be implemented by software plus necessary general hardware. Of course, it can also be implemented by dedicated hardware including dedicated integrated circuits, dedicated CPUs, dedicated memories, Special components, etc. to achieve. In general, all functions performed by computer programs can be easily implemented with corresponding hardware. Moreover, the specific hardware structures used to implement the same function can also be diverse, such as analog circuits, digital circuits or special-purpose circuits. circuit etc. However, for this application, software program implementation is a better implementation in most cases. Based on this understanding, the technical solution of the present application can be embodied in the form of a software product in essence or that contributes to the existing technology. The computer software product is stored in a readable storage medium, such as a computer floppy disk. , U disk, mobile hard disk, ROM, RAM, magnetic disk or optical disk, etc., including several instructions to cause a computer device (which can be a personal computer, training device, or network device, etc.) to execute the steps described in various embodiments of this application. method.
在上述实施例中,可以全部或部分地通过软件、硬件、固件或者其任意组合来实现。当使用软件实现时,可以全部或部分地以计算机程序产品的形式实现。In the above embodiments, it may be implemented in whole or in part by software, hardware, firmware, or any combination thereof. When implemented using software, it may be implemented in whole or in part in the form of a computer program product.
所述计算机程序产品包括一个或多个计算机指令。在计算机上加载和执行所述计算机程序指令时,全部或部分地产生按照本申请实施例所述的流程或功能。所述计算机可以是通用计算机、专用计算机、计算机网络、或者其他可编程装置。所述计算机指令可以存储在计算机可读存储介质中,或者从一个计算机可读存储介质向另一计算机可读存储介质传输,例如,所述计算机指令可以从一个网站站点、计算机、训练设备或数据中心通过有线(例如同轴电缆、光纤、数字用户线(DSL))或无线(例如红外、无线、微波等)方式向另一个网站站点、计算机、训练设备或数据中心进行传输。所述计算机可读存储介质可以是计算机能够存储的任何可用介质或者是包含一个或多个可用介质集成的训练设备、数据中心等数据存储设备。所述可用介质可以是磁性介质,(例如,软盘、硬盘、磁带)、光介质(例如,DVD)、或者半导体介质(例如固态硬盘(Solid State Disk,SSD))等。 The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the processes or functions described in the embodiments of the present application are generated in whole or in part. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions may be stored in or transmitted from one computer-readable storage medium to another, for example, the computer instructions may be transferred from a website, computer, training device, or data The center transmits to another website site, computer, training equipment or data center through wired (such as coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (such as infrared, wireless, microwave, etc.) means. The computer-readable storage medium may be any available medium that a computer can store, or a data storage device such as a training device or a data center integrated with one or more available media. The available media may be magnetic media (eg, floppy disk, hard disk, magnetic tape), optical media (eg, DVD), or semiconductor media (eg, solid state disk (Solid State Disk, SSD)), etc.
Claims (25)
Applications Claiming Priority (2)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN202210857821.1 | 2022-07-20 | ||
| CN202210857821.1A CN117494705A (en) | 2022-07-20 | 2022-07-20 | A model training method and its device |
Publications (1)
| Publication Number | Publication Date |
|---|---|
| WO2024017287A1 true WO2024017287A1 (en) | 2024-01-25 |
Family
ID=89617079
Family Applications (1)
| Application Number | Title | Priority Date | Filing Date |
|---|---|---|---|
| PCT/CN2023/108112 Ceased WO2024017287A1 (en) | 2022-07-20 | 2023-07-19 | Model training method and apparatus therefor |
Country Status (2)
| Country | Link |
|---|---|
| CN (1) | CN117494705A (en) |
| WO (1) | WO2024017287A1 (en) |
Cited By (2)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN119179118A (en) * | 2024-11-23 | 2024-12-24 | 中国地质调查局地球物理调查中心 | Electromagnetic wave data complement method and device, electronic equipment and readable storage medium |
| CN120851148A (en) * | 2025-09-24 | 2025-10-28 | 清华大学苏州汽车研究院(相城) | Method and device for generating traffic vertical domain model and autonomous intelligent transportation system |
Families Citing this family (1)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN119025436B (en) * | 2024-10-30 | 2025-04-18 | 浙江毫微米科技有限公司 | Resource allocation method, device and equipment for code performance analysis |
Citations (5)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| US20210357187A1 (en) * | 2020-05-15 | 2021-11-18 | Microsoft Technology Licensing, Llc. | Neural method completion based on natural language and source code |
| US20210357210A1 (en) * | 2020-05-15 | 2021-11-18 | Microsoft Technology Licensing, Llc. | Automatic generation of code documentation |
| US20220066747A1 (en) * | 2020-08-27 | 2022-03-03 | Microsoft Technology Licensing, Llc. | Unit test case generation with transformers |
| CN114585999A (en) * | 2019-08-01 | 2022-06-03 | 微软技术许可有限责任公司 | Multilingual code line completion system |
| CN114676234A (en) * | 2022-02-22 | 2022-06-28 | 华为技术有限公司 | A model training method and related equipment |
-
2022
- 2022-07-20 CN CN202210857821.1A patent/CN117494705A/en active Pending
-
2023
- 2023-07-19 WO PCT/CN2023/108112 patent/WO2024017287A1/en not_active Ceased
Patent Citations (5)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN114585999A (en) * | 2019-08-01 | 2022-06-03 | 微软技术许可有限责任公司 | Multilingual code line completion system |
| US20210357187A1 (en) * | 2020-05-15 | 2021-11-18 | Microsoft Technology Licensing, Llc. | Neural method completion based on natural language and source code |
| US20210357210A1 (en) * | 2020-05-15 | 2021-11-18 | Microsoft Technology Licensing, Llc. | Automatic generation of code documentation |
| US20220066747A1 (en) * | 2020-08-27 | 2022-03-03 | Microsoft Technology Licensing, Llc. | Unit test case generation with transformers |
| CN114676234A (en) * | 2022-02-22 | 2022-06-28 | 华为技术有限公司 | A model training method and related equipment |
Cited By (2)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN119179118A (en) * | 2024-11-23 | 2024-12-24 | 中国地质调查局地球物理调查中心 | Electromagnetic wave data complement method and device, electronic equipment and readable storage medium |
| CN120851148A (en) * | 2025-09-24 | 2025-10-28 | 清华大学苏州汽车研究院(相城) | Method and device for generating traffic vertical domain model and autonomous intelligent transportation system |
Also Published As
| Publication number | Publication date |
|---|---|
| CN117494705A (en) | 2024-02-02 |
Similar Documents
| Publication | Publication Date | Title |
|---|---|---|
| CN111951805B (en) | A text data processing method and device | |
| CN110263324B (en) | Text processing method, model training method and device | |
| WO2024041479A1 (en) | Data processing method and apparatus | |
| WO2024083121A1 (en) | Data processing method and apparatus | |
| EP4198807A1 (en) | Audio processing method and device | |
| CN113656563B (en) | A neural network search method and related equipment | |
| CN116306672A (en) | A data processing method and device thereof | |
| WO2024017287A1 (en) | Model training method and apparatus therefor | |
| WO2024245343A1 (en) | Data processing method and related device | |
| WO2024260402A1 (en) | Data processing method and apparatus | |
| WO2025040048A1 (en) | Data processing method and apparatus | |
| WO2024239983A1 (en) | Method and apparatus for controllable generation | |
| WO2024213099A1 (en) | Data processing method and apparatus | |
| CN115866291B (en) | Data processing method and device | |
| WO2025026210A1 (en) | Data processing method and apparatus | |
| WO2024199409A1 (en) | Data processing method and apparatus thereof | |
| WO2025067211A1 (en) | Data processing method and apparatus | |
| WO2024230757A1 (en) | Data processing method and related apparatus | |
| WO2025130968A1 (en) | Data processing method and apparatus therefor | |
| WO2025044967A1 (en) | Data processing method and apparatus | |
| CN116052714A (en) | A data processing method and device thereof | |
| WO2024109907A1 (en) | Quantization method and apparatus, and recommendation method and apparatus | |
| WO2025261249A1 (en) | Text generation method and apparatus | |
| WO2025236734A1 (en) | Data processing method and apparatus thereof | |
| CN116521931A (en) | Data processing method and related equipment |
Legal Events
| Date | Code | Title | Description |
|---|---|---|---|
| 121 | Ep: the epo has been informed by wipo that ep was designated in this application |
Ref document number: 23842349 Country of ref document: EP Kind code of ref document: A1 |
|
| NENP | Non-entry into the national phase |
Ref country code: DE |
|
| 122 | Ep: pct application non-entry in european phase |
Ref document number: 23842349 Country of ref document: EP Kind code of ref document: A1 |