Text translation method, device, kernel function combination method, server and medium
By combining the initial parameter matrix and reducing the number of calls to the underlying kernel function, the problem of large calculations in the neural network machine translation method is solved, the translation efficiency is improved and the accuracy of the translation results is maintained.
Patent Information
- Application Number
- CN202210194576.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-03-01
- Publication Date
- 2024-10-18
- Estimated Expiration
- 2042-03-01
AI Technical Summary
The neural network machine translation method based on the attention mechanism has a large amount of calculation and low translation efficiency.
By combining the initial parameter matrix, the number of calls to the underlying kernel function is reduced, decoding efficiency is improved, and text translation efficiency is improved.
The amount of calculation is reduced, the efficiency of text translation is improved, and the accuracy of the translation results is ensured.
Smart Images

Figure CN114580443B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computers, and in particular to a text translation method, device, kernel function combination method, server and medium. Background Art
[0002] Machine Translation (MT), also known as automatic translation, is the process of converting one natural language into another natural language using a computer. With the rise of deep learning, deep neural network technology has also been applied to MT in recent years, and neural machine translation (NMT) has become a new generation of translation technology.
[0003] The most widely used machine translation method at present is the neural network machine translation method based on the attention mechanism. This method first uses a neural network encoder to encode the source language sequence into one or a group of real number vectors, and then uses a neural network decoder and the attention mechanism to convert the source language sequence vector into a target language sequence to complete the translation process.
[0004] However, the neural network machine translation method based on the attention mechanism requires a lot of calculations and has low translation efficiency. Summary of the invention
[0005] Embodiments of the present application provide a text translation method, device, kernel function combination method, server and medium, which can improve translation efficiency.
[0006] An embodiment of the present application provides a text translation method, including: encoding text content to be translated to obtain a content embedding vector; obtaining multiple initial parameter matrices; combining at least two initial parameter matrices to obtain a combined parameter matrix; performing a linear transformation on the content embedding vector according to a target parameter matrix to obtain an attention weight, wherein the target parameter matrix includes the combined parameter matrix and the initial parameter matrices that are not combined in the multiple initial parameter matrices; performing weight calculation on the content embedding vector according to the attention weight to obtain an attention vector; and obtaining a translation result according to the attention vector.
[0007] The embodiment of the present application also provides a text translation device, including: an encoding unit, used to encode the text content to be translated to obtain a content embedding vector; an acquisition unit, used to obtain multiple initial parameter matrices; a combination unit, used to combine at least two initial parameter matrices to obtain a combined parameter matrix; a linear transformation unit, used to perform a linear transformation on the content embedding vector according to a target parameter matrix to obtain an attention weight, wherein the target parameter matrix includes the combined parameter matrix and a parameter matrix that is not combined in the initial parameters; a calculation unit, used to perform weight calculation on the content embedding vector according to the attention weight to obtain an attention vector; and a translation unit, used to obtain a translation result according to the attention vector.
[0008] An embodiment of the present application also provides a kernel function combination method, including: obtaining at least two initial kernel functions in a text translation model; combining the at least two initial kernel functions to obtain a combined kernel function, so as to update the text translation model according to the combined kernel function to obtain an updated text translation model, and using the updated text translation model for any text translation method provided in the embodiment of the present application.
[0009] An embodiment of the present application also provides a server, including a memory storing multiple instructions; the processor loads instructions from the memory to execute the steps in any text translation method provided in the embodiment of the present application.
[0010] An embodiment of the present application also provides a computer-readable storage medium, which stores a plurality of instructions, and the instructions are suitable for a processor to load to execute the steps in any text translation method provided in the embodiment of the present application.
[0011] The embodiment of the present application also provides a computer program product, including a computer program / instruction, which, when executed by a processor, implements the steps in any text translation method provided in the embodiment of the present application.
[0012] The embodiment of the present application can encode the text content to be translated to obtain a content embedding vector; obtain multiple initial parameter matrices; combine at least two initial parameter matrices to obtain a combined parameter matrix; perform a linear transformation on the content embedding vector according to a target parameter matrix to obtain an attention weight, wherein the target parameter matrix includes the combined parameter matrix and the initial parameter matrices that are not combined in the multiple initial parameter matrices; perform weight calculation on the content embedding vector according to the attention weight to obtain an attention vector; and obtain a translation result according to the attention vector. By combining the initial parameter matrices, the embodiment of the present application can merge similar processing processes, reduce the amount of calculation, improve decoding efficiency, and thus improve text translation efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0013] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For those skilled in the art, other drawings can be obtained based on these drawings without creative work.
[0014] Figure 1a is a scenario diagram of a text translation method provided in an embodiment of the present application;
[0015] Figure 1b It is a flowchart of a text translation method provided in an embodiment of the present application;
[0016] Figure 2a It is a schematic diagram of the structure of the decoding layer provided in an embodiment of the present application;
[0017] Figure 2b is a flowchart of a text translation method provided by another embodiment of the present application;
[0018] Figure 2c is an experimental data table of a decoding test experiment provided in an embodiment of the present application;
[0019] Figure 2d is a schematic diagram of the acceleration ratio data of the decoding test experiment provided in the embodiment of the present application;
[0020] Figure 3 is a structural diagram of a text translation device provided in an embodiment of the present application;
[0021] Figure 4 It is a flowchart of the kernel function combination method provided in the embodiment of the present application;
[0022] Figure 5 It is a schematic diagram of the structure of the server provided in the embodiment of the present application. DETAILED DESCRIPTION
[0023] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of this application.
[0024] Embodiments of the present application provide a text translation method, device, kernel function combination method, server and medium.
[0025] The text translation device can be integrated into an electronic device, which can be a terminal, a server, or other devices. The terminal can be a mobile phone, a tablet computer, a smart Bluetooth device, a laptop, or a personal computer (PC), etc. The server can be a single server or a server cluster composed of multiple servers.
[0026] In some embodiments, the terminal includes but is not limited to mobile phones, computers, intelligent voice interaction devices, smart home appliances, vehicle terminals, aircraft, etc. The embodiments of the present invention can be applied to various scenarios, including but not limited to cloud technology, artificial intelligence, smart transportation, assisted driving, etc.
[0027] In some embodiments, the text translation device may also be integrated into multiple electronic devices. For example, the text translation device may be integrated into multiple servers, and the text translation method of the present application may be implemented by multiple servers.
[0028] In some embodiments, the server may also be implemented in the form of a terminal.
[0029] The embodiment of the present application can be applied to a neural network machine translation model based on an attention mechanism, such as a neural network machine translation model including a Transformer module. The decoding acceleration method of the existing neural network machine translation model can be divided into two categories: a decoding acceleration method based on a network layer and a decoding acceleration method based on a model layer. The decoding acceleration method based on the network layer mainly caches the intermediate results of the Cross-attention layer and the Self-attention layer at the decoding end with a Cache (cache memory) to achieve the purpose of decoding acceleration. The method has a coarse optimization granularity and brings less acceleration benefits. The decoding acceleration method based on the model layer usually adopts a knowledge distillation (KD) strategy and a lighter Attention mechanism to optimize the decoding efficiency. The method requires changes to the model structure. Although it can bring benefits in decoding acceleration, there is a certain degree of loss in model performance (BLEU). However, the method of the embodiment of the present application can reduce the number of calls to the underlying kernel function by combining adjacent and similar underlying kernel functions in the neural network machine translation model based on the attention mechanism, thereby achieving the purpose of decoding acceleration. This method does not change the calculation method of the original model and is an equivalent operation of the original model.
[0030] For example, refer to Figure 1a, the server can be equipped with a text translation device. The server can obtain the text content to be translated from the client, encode the text content to be translated, and obtain a content embedding vector; obtain multiple initial parameter matrices; combine at least two initial parameter matrices to obtain a combined parameter matrix; perform a linear transformation on the content embedding vector according to the target parameter matrix to obtain an attention weight, the target parameter matrix includes the combined parameter matrix and the initial parameter matrices that are not combined in the multiple initial parameter matrices; perform weight calculation on the content embedding vector according to the attention weight to obtain an attention vector; obtain a translation result according to the attention vector, and return the translation result to the client.
[0031] The following are detailed descriptions of each.
[0032] Artificial Intelligence (AI) is a technology that uses digital computers to simulate human perception of the environment, acquire knowledge, and use knowledge. This technology can enable machines to have functions similar to human perception, reasoning, and decision-making. The basic technologies of artificial intelligence generally include sensors, dedicated artificial intelligence chips, cloud computing, distributed storage, big data processing technology, operation / interaction systems, mechatronics, and other technologies. Artificial intelligence software technology mainly includes computer vision technology, speech processing technology, natural language processing technology, as well as machine learning / deep learning, autonomous driving, smart transportation, and other major directions.
[0033] Among them, natural language processing (NLP) is an important direction in the fields of computer science and artificial intelligence. It studies various theories and methods that can achieve effective communication between people and computers using natural language. Natural language processing is a science that integrates linguistics, computer science, and mathematics. Therefore, research in this field will involve natural language, that is, the language people use in daily life, so it is closely related to the study of linguistics. Natural language processing technology usually includes text processing, semantic understanding, machine translation, robot question answering, knowledge graph and other technologies.
[0034] With the research and advancement of artificial intelligence technology, artificial intelligence technology has been studied and applied in many fields, such as common smart homes, smart wearable devices, virtual assistants, smart speakers, smart marketing, unmanned driving, automatic driving, drones, robots, smart medical care, smart customer service, Internet of Vehicles, automatic driving, smart transportation, etc. I believe that with the development of technology, artificial intelligence technology will be applied in more fields and play an increasingly important role.
[0035] The text translation method provided in the embodiment of the present application can be applied to a variety of different scenarios, such as machine translation, dialogue question and answer, text summarization and other scenarios, and the embodiment of the present application does not limit the specific application scenario. For example, when applied to the application scenario of machine translation, it can be used in applications that support translation functions such as electronic dictionary applications, e-book applications, web browsing applications, social applications, and image and text recognition applications. The content to be translated is received by obtaining the above-mentioned application, and the translation result is output after processing by the method provided in the embodiment of the present application. When applied to the application scenario of dialogue question and answer, taking the virtual assistant set in the intelligent terminal as an example, the virtual assistant can obtain the questions about translation raised by the user to the virtual assistant in the form of voice or text, and the translation result is output after processing by the method provided in the embodiment of the present application, and the translation result is fed back to the user in the form of voice or text.
[0036] In this embodiment, a text translation method involving artificial intelligence is provided, such as Figure 1b As shown, the specific process of the text translation method can be as follows:
[0037] 110. Encode the text content to be translated to obtain a content embedding vector.
[0038] The text content to be translated may be Chinese text, English text, or text in another language. The embodiment of the present application does not limit the language type of the text content to be translated.
[0039] For different application scenarios, the text to be translated can come from different types of content to be translated. For example, the content to be translated includes at least one of text type content, picture type content, audio type content, and video type content, wherein the picture type content includes photos taken by the camera assembly of the terminal or pictures containing the content to be translated, etc. The source of the text to be translated in the embodiment of the present application is not limited. For example, the text content to be translated can be identified from content of types such as pictures, audio, and video through technologies such as text recognition and speech recognition.
[0040] The content embedding vector refers to the vector obtained after embedding each word in the text to be translated. After obtaining the text content to be translated, the text content can be encoded by Word2Vec or an encoder to obtain a content embedding vector. For example, each word in the text content to be translated is embedded by an encoder, and each word is represented by an N-dimensional vector to obtain a word embedding vector for each word, and the word embedding vectors of each word are combined to obtain a content embedding vector. After obtaining the word embedding vector of each word, the position of each word in the text to be translated can also be obtained to generate a position vector for each word.
[0041] 120. Obtain multiple initial parameter matrices.
[0042] The initial parameter matrix refers to a parameter matrix that can be used to determine the attention weight. In neural network technology, when processing the input content embedding vector based on the attention mechanism, the initial parameter matrix can usually be obtained by presetting or training the neural network model containing the attention layer, and the attention weight can be calculated through the initial parameter matrix. For example, when using a neural network machine translation model containing a Transformer module to process the input content embedding vector, the initial parameter matrix W can be obtained. Q (request vector parameter matrix), W K (key vector parameter matrix) and W V (value vector parameter matrix), by W Q , W K and W V By performing linear transformation on the content embedding vectors respectively, we can obtain the attention weights Q (request vector sequence), K (key vector sequence) and V (value vector sequence).
[0043] 130. Combine at least two initial parameter matrices to obtain a combined parameter matrix.
[0044] All or part of the initial parameter matrices can be combined by concatenation, addition, linear combination, etc. to obtain a combined parameter matrix. For example, the initial parameter matrix includes initial parameter matrix 1, initial parameter matrix 2, and initial parameter matrix 3. Initial parameter matrix 2 can be concatenated to the back of initial parameter matrix 1, and initial parameter matrix 3 can be concatenated to the back of initial parameter matrix 2 to obtain a combined parameter matrix. The concatenation order is not limited here. In addition, all or part of the initial parameter matrices can be selected and combined according to the application scenario or different neural network models.
[0045] In the process of calculating the attention weights through the initial parameter matrix, when there are multiple initial parameter matrices, each initial parameter matrix is usually processed separately to obtain the attention weights corresponding to each initial parameter matrix. For example, in a neural network machine translation model containing a Transformer module, each initial parameter matrix W Q (request vector parameter matrix), W K (key vector parameter matrix) and W V (value vector parameter matrix) are linearly transformed to obtain the attention weights Q (request vector sequence), K (key vector sequence) and V (value vector sequence), where W Q , W K and W VIt is learned during the training process. In the above process, multiple data calculations need to be performed based on multiple initial parameter matrices, which results in large amount of calculation and low efficiency.
[0046] However, the embodiment of the present application can merge similar processing processes by combining initial parameter matrices to combine adjacent and similar underlying kernel functions to reduce the number of calls to the underlying kernel functions, thereby reducing the amount of calculation, improving decoding efficiency, and improving text translation efficiency.
[0047] A kernel function refers to a function used to process input data (such as an input vector) in a neural network model, and may include but is not limited to at least one of a weight matrix, an activation function, a normalization function, a bias function, a residual function, etc. Kernel functions may be combined in a manner including but not limited to combining multiple kernel functions based on algebraic operation rules, or obtaining a composite function by using one kernel function as the input of another kernel function, or thread groups and the like.
[0048] In some embodiments, the combination of kernel functions can be to combine multiple kernel functions into a large-grained combined kernel function, which can perform function-level calculations, and use the multiple kernel functions to perform mathematical-level calculations in the order in which the multiple kernel functions process the input data. For example, the combination process of the kernel functions can be a set of thread groups set to process the multiple kernel functions, and the thread group can be started by calling the combined kernel function. In this way, when the combined kernel function is called, the thread group can be started and the multiple kernel functions can be used in sequence in the thread group to perform mathematical-level calculations on the input data. Therefore, before the combined kernel function is called, several kernel functions usually need to be called several times, and after the combined kernel function is called, the calculation process of the multi-step kernel function can be completed by calling the combined kernel function to start the corresponding thread group, which can reduce the number of kernel function calls, reduce the amount of calculation, and improve the decoding efficiency, so as to improve the text translation efficiency.
[0049] In some embodiments, in a neural network machine translation model including a Transformer module, when using the initial parameter matrix to calculate the attention weight, the three functions Q=x*W are used: Q , K=x*W K And V = x * W V This is the kernel function in the attention layer. In the embodiment of the present application, the three kernel functions can be combined by combining the initial parameter matrix to obtain a combined kernel function. Specifically, the initial parameter matrix W is first concatenated. K and W V Get W K / W V , and then combine the above three kernel functions to obtain the combined kernel function "Q, K_V = x*W Q ,encoder_output*(WK / W V )”, where / represents concatenation and x represents the content embedding vector. Therefore, when calling the combined kernel function, you only need to start “x*W Q "The corresponding thread group and "encoder_output*(W K / W V )" to perform mathematical level calculations, such as "x*W Q "The corresponding thread group can use Q = x * W Q Perform mathematical calculations, "encoder_output*(W K / W V )" The corresponding thread group can use K_V = x*(W K / W V ) to perform math-level calculations.
[0050] It should be noted that the process of combining multiple kernel functions to obtain a combined kernel function can be performed after step 130 or before step 110, that is, before executing the text translation method of the embodiment of the present application, the combined kernel function can be obtained according to the method of combining to obtain the combined kernel function in the embodiment of the present application, and the text translation model can be updated by the obtained combined kernel function, or the combined kernel function can be obtained by combining during the execution of the text translation method of the embodiment of the present application.
[0051] For example, before step 110, a text translation model (such as a neural network machine translation model including a Transformer module) and the corresponding kernel functions of the initial parameter matrix in the text translation model (i.e., the three kernel functions mentioned above) can be obtained, and at least two initial parameter matrices can be combined to obtain a combined kernel function based on the corresponding kernel functions of the initial parameter matrices, and the text translation model can be updated through the combined kernel function (such as replacing the corresponding kernel function of the initial parameter matrix with the combined kernel function). In this way, the updated text translation model can be used to execute the text translation method of the embodiment of the present application.
[0052] 140. According to the target parameter matrix, the content embedding vector is linearly transformed to obtain the attention weight, and the target parameter matrix includes the combined parameter matrix and the initial parameter matrix that is not combined in the multiple initial parameter matrices.
[0053] Among them, the linear transformation can map a vector belonging to one vector space to another vector space. In the embodiment of the present application, the target parameter matrix can be used to perform a linear transformation on the content embedding vector, and the content embedding vector can be mapped to different vector spaces respectively to obtain the attention weight corresponding to the content embedding vector.
[0054] When at least two of the multiple initial parameter matrices are combined, the initial parameter matrices in the multiple initial parameter matrices may be combined in whole or in part. If all of the initial parameter matrices are combined to obtain a combined parameter matrix, the target parameter matrix may be the combined parameter matrix. If some of the initial parameter matrices are combined to obtain a combined parameter matrix, the target parameter matrix may include the combined parameter matrix and the uncombined initial parameter matrix, for example, the initial parameter matrix includes initial parameter matrix 1, initial parameter matrix 2, and initial parameter matrix 3, and the initial parameter matrix 1 and the initial parameter matrix 2 are concatenated to obtain a combined parameter matrix, then the target parameter matrix includes the combined parameter matrix and the initial parameter matrix 3.
[0055] When the target parameter matrix includes a combined parameter matrix and an uncombined initial parameter matrix, an attention weight can be obtained by combining the combined parameter matrix and the uncombined initial parameter matrix. For example, in the above example, the combined parameter matrix and the initial parameter matrix 3 can be used to perform two linear transformations on the content embedding vector in succession to obtain the attention weight, and the order of the linear transformation is not limited here. The attention weights can also be obtained separately for the combined parameter matrix and the uncombined initial parameter matrix. For example, in the above example, the content embedding vector can be linearly transformed according to the combined parameter matrix to obtain the attention weight 1, and the content embedding vector can be linearly transformed according to the initial parameter matrix 3 to obtain the attention weight 2. Thus, the attention weight obtained in step 140 includes the attention weight 1 and the attention weight 2. In practical applications, the attention weight can also be represented by splicing the attention weight 1 and the attention weight 2, and the splicing order is not limited here.
[0056] In the embodiment of the present application, after combining at least two initial parameter matrices to obtain a combined parameter matrix, the combined parameter matrix and the uncombined initial parameter matrix can be used to calculate the attention weight, so that the obtained attention weight is associated with all the initial parameter matrices, that is, it is affected by all the initial parameter matrices, making the obtained attention weight more accurate, thereby improving the efficiency of text translation while also ensuring the accuracy of the translation results.
[0057] In some implementations, the text translation method of the embodiment of the present application may be applied to a neural network machine translation model including a Transformer module, whereby the multiple initial parameter matrices may include a request vector parameter matrix, a key vector parameter matrix, and a value vector parameter matrix, and step 130 may include step 1.1, as follows:
[0058] 1.1. Combine the key vector parameter matrix and the value vector parameter matrix to obtain a combined parameter matrix.
[0059] Step 140 may include step 1.2, as follows:
[0060] 1.2. The content embedding vector is linearly transformed through the request vector parameter matrix and the combination parameter matrix to obtain the attention weight. The attention weight includes the request vector weight corresponding to the request vector parameter matrix and the combination weight corresponding to the combination parameter matrix.
[0061] When applied to a neural network machine translation model including a Transformer module, the content embedding vector can be linearly transformed using a combination parameter matrix and a request vector parameter matrix, and the content embedding vector can be mapped to two different vector spaces to obtain corresponding combination weights and request vector weights. The embodiment of the present application reduces three linear transformations to two by combining a key vector parameter matrix and a value vector parameter matrix, reduces the number of calls to the underlying kernel function, reduces the amount of calculation, improves decoding efficiency, and thus improves text translation efficiency.
[0062] In some embodiments, step 140 may include steps 2.1 to 2.2, as follows:
[0063] 2.1. Mask the target parameter matrix to obtain the processed target parameter matrix;
[0064] 2.2. According to the processed target parameter matrix, the content embedding vector is linearly transformed to obtain the attention weight.
[0065] Among them, masking processing refers to the operation of covering some components in the target parameter matrix, and the parts that need to be covered can be determined according to the specific application scenario. For example, a matrix corresponding to the components that need to be covered in the target parameter matrix can be generated, and the matrix components are all 0, and the components corresponding to the target parameter matrix are covered with the matrix, so that the components of the covered position of the processed target parameter matrix are 0. In this way, by masking the target parameter matrix, the covered part of the data can be excluded from the operation, the amount of calculation can be reduced, the decoding efficiency can be improved, and the text translation efficiency can be improved. It should be noted that the target parameter matrix can include the combined parameter matrix and the request vector parameter matrix obtained by step 1.1. In this way, in step 2.2, the content embedding vector can be linearly transformed according to the combined parameter matrix and the request vector parameter matrix respectively to obtain the request vector weight corresponding to the request vector parameter matrix and the combined weight corresponding to the combined parameter matrix.
[0066] The existing neural network machine translation model containing the Transformer module usually includes an Encoder-Decoder. The encoder can map the natural language sequence (text content to be translated) into a content embedding vector, which is a mathematical expression containing the natural language sequence. The decoder maps the content embedding vector into a natural language sequence (translation result), thereby realizing machine translation. In the decoding of the network model, a multi-head attention layer (Multi-Head Attention) and a masked multi-head attention layer (Masked Multi-Head Attention) can usually be included. The masked multi-head attention layer can be used in the network model training process. The position information after masking can be used to train the decoder of the network model. The multi-head attention layer can be used in the use process of the network model. The content embedding vector corresponding to the text content to be translated can be processed to obtain the translation result. The calculation process of the multi-head attention layer and the masked multi-head attention layer is similar, both of which include calculating each initial parameter matrix W Q , W K and W V The content embedding vector is linearly transformed to obtain the attention weights Q, K and V, and the attention vector is obtained by weighting the content embedding vector according to Q, K and V. The content embedding vectors input in the two-layer network can be the same, and the initial parameter matrix W corresponding to the two-layer network is Q , W K and W V Can be different.
[0067] Thus, when the text translation method of the embodiment of the present application is applied to a neural network machine translation model including a Transformer module, the initial parameter matrix W corresponding to any one of the multi-head attention layers and the masked multi-head attention layer can be Q , W K and W V At least two initial parameter matrices in the network model are combined to obtain a combined parameter matrix, and the combined parameter matrix and the initial parameter matrices that are not combined in the multiple initial parameter matrices are used to perform a linear transformation on the content embedding vector to obtain an attention vector. The initial parameter matrices corresponding to the multi-head attention layer and the masked multi-head attention layer in the network model can also be combined at the same time. The combination method of the initial parameter matrices corresponding to the multi-head attention layer and the masked multi-head attention layer can be the same or different.
[0068] In some embodiments, for the multi-head attention layer, the key vector parameter matrix and the value vector parameter matrix can be combined to obtain a combined parameter matrix by referring to the methods in steps 1.1 and 1.2.
[0069] In some embodiments, for a masked multi-head attention layer, a key vector parameter matrix, a value vector parameter matrix, and a request vector parameter matrix may be combined to obtain a combined parameter matrix.
[0070] Since the masked multi-head attention layer is used in the model training process, the training process needs to be repeatedly performed, and the amount of calculation is much greater than the model use process to adjust the network model parameters. Therefore, all three initial parameter matrices can be combined into a combined parameter matrix. Compared with the method of combining only part of the parameter matrix, similar underlying kernel functions are integrated to the maximum extent, reducing the number of calls to the underlying kernel functions, which can greatly improve the model training efficiency and improve the text translation efficiency. And since the combined parameter matrix is also affected by the three initial parameter matrices, it has almost no effect on the accuracy of the training results. In comparison, the multi-head attention layer is used in the model use process, and the processing result can be output after processing the input content embedding vector once. Therefore, the method of combining part of the initial parameter matrix can be adopted. Since among the three initial parameter matrices, the attention weight Q corresponding to the request vector parameter matrix can be used to learn the relationship between itself and other elements, the attention weight K corresponding to the key vector parameter matrix is used to learn the relationship between other elements and itself, and the attention weight V corresponding to the value vector parameter matrix represents the specific information of each element, and the element corresponds to the element in the content embedding vector. In this way, in order to clearly distinguish the relationship between elements, the attention weight V can be combined with any one of the attention weights Q and V to calculate the weights of the two different associations, the relationship between itself and other elements and the relationship between other elements and itself, respectively, so as to improve the efficiency of text translation while ensuring the accuracy of the translation results. Specifically, for the multi-head attention layer, compared with combining the request vector parameter matrix and the value vector parameter matrix, combining the key vector parameter matrix and the value vector parameter matrix can achieve higher accuracy of the translation results.
[0071] In addition, when the multi-head attention layer in the network model and the initial parameter matrix corresponding to the masked multi-head attention layer are combined at the same time, the above-mentioned different combinations can also improve the efficiency of text translation while ensuring the accuracy of the translation results.
[0072] 150. The content embedding vector is weighted according to the attention weight to obtain the attention vector.
[0073] After obtaining the attention weights, the attention vector corresponding to each word embedding vector in the content embedding vector can be determined according to the attention weights. Since the attention weights incorporate the information of each word embedding vector in the content embedding vector, the obtained attention vector has a global dependency relationship, which can improve the accuracy of text translation. For example, for the masked multi-head attention layer, the attention weights can be linearly combined with the content embedding vector to obtain the attention vector. For the multi-head attention layer, the dot product operation can be used to obtain the attention vector.
[0074] For example, for the masked multi-head attention layer, the calculation process of the attention weight can be expressed as “Q_K_V=x*(W Q / W K / W V )", where _ represents connection, / represents concatenation, and x represents the content embedding vector. Q / W K / W V The three initial parameter matrices are combined to obtain the attention weight Q_K_V. The calculation process only needs to call the kernel function once. Q , K=x*W K And V = x*W V Q, K, and V are calculated separately, and the corresponding kernel function must be called once in each calculation process. Therefore, the three kernel functions need to be called three times. However, in the embodiment of the present application, by combining three initial parameter matrices, only the kernel function "Q_K_V=x*(W Q / W K / W V )" to start its corresponding thread group, and adopt Q_K_V=x*(W Q / W K / W V ) performs mathematical level calculations, so only one kernel function needs to be called. Obviously, the embodiment of the present application can simplify the three kernel function call processes into one, which can reduce the amount of calculation, improve decoding efficiency, and thus improve text translation efficiency.
[0075] For example, for a multi-head attention layer, the calculation process of the attention weight can be expressed as “Q, K_V = x*W Q ,encoder_output*(W K / W V )", where _ represents connection, / represents concatenation, and x represents the content embedding vector. K / W V The two initial parameter matrices are combined to obtain the attention weight K_V. The calculation process is through "Q, K_V = x*W Q,encoder_output*(W K / W V )" to calculate the attention weight, just call "x*W Q " and "encoder_output*(W K / W V )" to start "x*W Q "The corresponding thread group and "encoder_output*(W K / W V )" corresponding to the thread group, where "x*W Q "The corresponding thread group can use Q = x * W Q Perform mathematical calculations, "encoder_output*(W K / W V )" The corresponding thread group can use K_V = x*(W K / W V ) to perform mathematical level calculations, that is, only two kernel functions need to be called. Therefore, the embodiment of the present application can simplify the three kernel function call processes into two, which can reduce the amount of calculation, improve decoding efficiency, and thus improve text translation efficiency.
[0076] It should be noted that when the decoder of the neural network machine translation model including the Transformer module includes multiple sequentially connected decoding layers, each decoding layer may include at least a masked multi-head attention layer and a multi-head attention layer. The vector obtained after the content embedding vector is processed by one decoding layer is input into the next decoding layer until the attention vector is obtained after the input into all the decoding layers. Each decoding layer may perform the processing process of steps 120 to 140 on the input vector in the masked multi-head attention layer or the multi-head attention layer.
[0077] In some implementations, after using the attention weight to calculate the weight of the content embedding vector to obtain the initial attention vector, the obtained initial attention vector is usually subjected to residual connection and layer normalization (Add & Normalization) processing. Step 150 may include steps 3.1 to 3.3, as follows:
[0078] 3.1. Calculate the weight of the content embedding vector according to the attention weight to obtain the initial attention vector;
[0079] 3.2. Obtain a batch processing function that includes deviation processing and residual processing;
[0080] 3.3. The initial attention vector is subjected to bias processing, residual processing and batch processing through the batch processing function to obtain the attention vector.
[0081] The batch function including bias processing and residual processing can be obtained by fusing the bias function, the residual function and the batch function. For example, in a neural network machine translation model including a Transformer module, after the multi-head attention layer and the masked multi-head attention layer, a residual connection and a normalization layer are usually connected, and the bias summation operation (f 1 =x 1 +b), residual network summation operation (rn_f 1 =Residual_Add(f 1 ,x 2 )) and layer normalization operations (ln_f 1 =LayerNormalization(rn_f 1 )) to improve the problem of gradient disappearance in deep models, break the network symmetry, improve the network degradation problem, accelerate convergence, and make the distribution of each feature similar. Each operation of this process requires calling a kernel function, which requires a large amount of calculation. In the embodiment of the present application, the addition and summation operation, the residual network summation operation, and the layer normalization operation are combined to obtain a batch processing function including deviation processing and residual processing. The combination method may include but is not limited to combining the three kernel functions corresponding to these three operations based on algebraic operation rules, composite functions, or thread groups. For example, the three kernel functions "f 1 =x 1 +b", "rn_f 1 =Residual_Add(f 1 ,x 2 ))" and "ln_f 1 =LayerNormalization(rn_f 1 )” combination to obtain the batch processing function “f 1 =Add_Bias_Residual_LayerNormalization(x 1 ,x 2 ,b)”, where b represents the deviation value, x 1 and x 2Respectively represent different word embedding vectors. The combined batch function is a large-grained function composed of the above three kernel functions, which can perform function-level calculations and use the above three kernel functions in sequence for mathematical-level calculations. For example, by calling the batch function obtained by splicing, the corresponding thread group can be started in sequence, and the above three kernel functions are used through the thread group to perform mathematical-level calculations. In this way, the kernel function only needs to be called once to complete the three operations of addition and summation, residual network summation, and layer normalization. Therefore, reducing the number of calls to the underlying kernel function can reduce the amount of calculation, improve decoding efficiency, and thus improve text translation efficiency.
[0082] In some embodiments, before step 3.1, the following steps may be included: obtaining a normalization function including deviation processing; performing deviation processing and normalization processing on the attention weights through the normalization function. The deviation value of the deviation processing may be obtained or pre-set according to the application scenario, and the normalization function including the deviation processing may be obtained by combining the normalization function and the deviation function. The combination method may include but is not limited to combining the two functions based on algebraic operation rules, composite functions or thread group communication methods. For example, the normalization function and the deviation function may be combined to obtain a large-grained normalization function including deviation processing, and the combined function corresponds to a group of thread groups that can perform deviation processing and normalization processing, so that the corresponding thread group can be started by calling the combined function, and the normalization function and the deviation function are sequentially used through the thread group to perform deviation processing and normalization processing on the attention weights. The deviation function refers to a function used for deviation calculation, and the normalization function may be a Softmax function. By utilizing a normalization function that includes bias processing, the bias processing and normalization processing operations on the attention weight are combined into one operation, and the at least two kernel function call processes are simplified to one. The number of calls to the underlying kernel function is reduced, which can reduce the amount of calculation, improve decoding efficiency, and thus improve text translation efficiency.
[0083] For example, for the masked multi-head attention layer, the normalization function including bias processing can be expressed as “Output = Add_Bias_Softmax(Q_K_V, b Q , b K , b V )", where _ represents the connection, Q_K_V represents the attention weight, and b Q 、b K and b V They are used to represent the bias values corresponding to Q, K, and V respectively. For example, for the multi-head attention layer, the normalization function including bias processing can be expressed as "Output = Add_Bias_Softmax(Q, K_V, b Q , b K , bV )", where _ represents the connection, "Q, K_V" represents the attention weight, b Q , b K and b V They are used to represent the deviation values corresponding to Q, K and V respectively. Among them, the combined normalization function including deviation processing can be a large-grained function composed of a normalization function and a deviation function, which can perform function-level calculations, and use the above-mentioned normalization function and deviation function in sequence to perform mathematical-level calculations. For example, the corresponding thread group can be started by calling the normalization function including deviation processing, and the normalization function and the deviation function are used in sequence through the thread group to perform mathematical-level calculations. Since in the prior art, it is necessary to perform deviation processing on Q, K, and V, and the kernel function must be called once for each calculation process, so it is necessary to call the kernel function three times. In addition, normalization, output results and other operations are required, and it is necessary to call the kernel function at least three times. The embodiment of the present application simplifies the multiple kernel function call processes into one, which can reduce the amount of calculation, improve decoding efficiency, and improve text translation efficiency.
[0084] 160. According to the attention vector, the translation result is obtained.
[0085] After obtaining the attention vector, the vector can be input into the output layer to obtain the translation result. The output layer can be a fully connected layer. In the output layer, a probability prediction can be performed on each word embedding vector in the attention vector, and the prediction result can be output to obtain the translation result.
[0086] In some embodiments, the attention vector may be transformed before being input into the output layer. For example, the attention vector may be transformed using a weight matrix, a second weight matrix, a bias value, and an activation function including a bias process. Step 160 may include steps 4.1 to 4.5, as follows:
[0087] 4.1. Obtain a first weight matrix, a second weight matrix, a deviation value, and an activation function including deviation processing;
[0088] 4.2. Calculate the weight of the attention vector using the first weight matrix to obtain the first vector;
[0089] 4.3. The first vector is subjected to deviation processing and nonlinear changes through the activation function to obtain the second vector;
[0090] 4.4. Perform weight calculation and deviation processing on the second vector using the second weight matrix and the deviation value to obtain an intermediate vector;
[0091] 4.5. Based on the intermediate vector, obtain the translation result.
[0092] Among them, the first weight matrix and the second weight matrix can be weight matrices obtained or pre-set according to the application scenario, the deviation value can be a value obtained or pre-set according to the application scenario, and the deviation value can be in the form of a number, a matrix or a vector.
[0093] The activation function including the bias processing may be a function obtained by fusing the activation function and the bias function. For example, when applied to a neural network machine translation model including a Transformer module, a feedforward neural network (FFN) for spatial transformation may be connected after each multi-head attention layer. In the feedforward neural network, weighted sum operations (f 1 =W 1 *x 1 +b 1 ), activation operation (act_f 1 =ReLU(f 1 )) and once again the weighted sum operation (f 2 =W 2 *act_f 1 +b 2 ), transform the attention vector. The weighted sum operation of this process requires calling the kernel function twice, and the activation operation requires calling the kernel function once. In the embodiment of the present application, the addition sum operation and the activation operation are combined to obtain an activation function including deviation processing. The combination method may include but is not limited to combining the above three functions based on algebraic operation rules, composite functions or thread group communication methods. For example, the original weighted sum operation and activation operation can be expressed as: "f 1 =W 1 *x 1 ”, and “act_f 1 =Add_Bias_ReLU(f 1 , b 1 ), where W 1 represents the first weight matrix, W 2 represents the second weight matrix, b 1 , b 2 Indicates the deviation value, x 1 Represents a word embedding vector, where the combined function "act_f 1 =Add_Bias_ReLU(f 1 , b 1 )" can be the deviation value b 1 And the large-grained function composed of activation functions, which can perform function-level calculations and use the deviation value b sequentially 1 and activation operation functions for mathematical level calculations, for example, by calling the combined function "act_f1 =Add_Bias_ReLU(f 1 , b 1 )” can start the corresponding thread group, and use the deviation value b in sequence through the thread group 1 The activation function is then used to perform mathematical calculations in sequence. Therefore, the kernel function call process, which originally took at least three times, can be simplified to two times, which can reduce the amount of calculation, improve decoding efficiency, and thus improve text translation efficiency. In actual applications, the feedforward neural network can be used after the residual connection and layer normalization layers.
[0094] In the process of obtaining the translation result according to the intermediate vector, the intermediate vector can be input into the output layer to obtain the translation result, or the intermediate vector can be further processed and then input into the output layer. In some embodiments, the intermediate vector can also be transformed before being input into the output layer. For example, the intermediate vector can be transformed using an activation function including deviation processing. Step 4.5 can include steps 4.5.1 to 4.5.3, as follows:
[0095] 4.5.1. Obtain the residual function including deviation processing;
[0096] 4.5.2. The intermediate vector is processed by residual function to obtain the feature vector;
[0097] 4.5.3. Obtain the translation result based on the feature vector.
[0098] The residual function including the bias processing may be a function obtained by combining the residual function and the bias function. For example, when applied to a neural network machine translation model including a Transformer module, residual processing and bias processing may be performed on each decoding layer output part, and the bias operation (f 1 =x 1 +b) and the residual summation operation (rn_f 1 =Residual_Add(f 1 ,x 2 )) transforms the intermediate vector. The deviation operation and the residual sum operation of this process each need to call a kernel function once, and in the embodiment of the present application, the deviation operation and the residual sum operation are combined to obtain a residual function including deviation processing. The combination method may include but is not limited to combining the deviation operation and the corresponding function of the residual sum operation based on algebraic operation rules, composite functions or thread group communication methods. For example, the original deviation operation and residual sum operation can be expressed as: f1 = Add_Bias_Residual(x 1 ,x 2 ,b), where b represents the deviation value, x 1 and x2 They represent different word embedding vectors respectively. The combined function can be a large-grained function composed of a bias function and a residual function. Functional-level calculations can be performed, and the bias function and the residual function can be used sequentially for mathematical-level calculations. For example, by calling the combined function, the corresponding thread group can be started, and the bias function and the activation function can be used sequentially through the thread group to perform mathematical-level calculations. Therefore, the original two kernel function call processes can be simplified to one, which can reduce the amount of calculation, improve decoding efficiency, and thus improve text translation efficiency.
[0099] It should be noted that the function forms and parameters (such as deviation values) corresponding to all residual processing, deviation processing and other processes in the embodiments of the present application may be different in different processing processes and may be set or adjusted according to the application scenario. The function forms and parameters of the deviation functions and the deviation functions in the batch function including deviation processing and residual processing, the activation function including deviation processing, the residual function including deviation processing, etc., may also be different and may be set or adjusted according to the application scenario.
[0100] The text translation scheme provided by the embodiment of the present application can be applied to various neural network machine translation models. For example, taking the neural network machine translation model applied to the Transformer module as an example, the text content to be translated is encoded to obtain a content embedding vector; multiple initial parameter matrices are obtained; at least two initial parameter matrices are combined to obtain a combined parameter matrix; according to the target parameter matrix, the content embedding vector is linearly transformed to obtain an attention weight, and the target parameter matrix includes a combined parameter matrix and an initial parameter matrix that is not combined in the multiple initial parameter matrices; the content embedding vector is weighted according to the attention weight to obtain an attention vector; according to the attention vector, the translation result is obtained. The scheme provided by the embodiment of the present application can be combined with the initial parameter matrix to merge similar processing processes, so as to combine adjacent and similar underlying kernel functions to reduce the number of calls to the underlying kernel function, reduce the amount of calculation, improve decoding efficiency, and improve text translation efficiency.
[0101] In addition, after combining at least two initial parameter matrices to obtain a combined parameter matrix, the embodiment of the present application can use the combined parameter matrix and the uncombined initial parameter matrix to calculate the attention weight, so that the attention weight is associated with all the initial parameter matrices, that is, it is affected by all the initial parameter matrices, making the obtained attention weight more accurate, while improving the efficiency of text translation while also ensuring the accuracy of the translation results.
[0102] Blockchain is a new application model of computer technologies such as distributed data storage, peer-to-peer transmission, consensus mechanism, encryption algorithm, etc. Blockchain is essentially a decentralized database, a string of data blocks generated by cryptographic methods. Each data block contains a batch of network transaction information, which is used to verify the validity of its information (anti-counterfeiting) and generate the next block. Blockchain can include the underlying blockchain platform, platform product service layer, and application service layer.
[0103] The underlying blockchain platform can include management, basic services, smart contracts, and operations management processing modules. The management module is responsible for the information management of all blockchain participants, including maintaining public and private key generation (account management), key management, and maintaining the corresponding relationship between blockchain participants and blockchain addresses (authority management), etc., and, under authorization, manages transactions and provides risk control rule configuration (risk control audit); the basic service module is deployed on all blockchain node devices to verify the validity of business requests, and records valid requests to storage after consensus is reached. For a new business request, the basic service first performs interface adaptation analysis and authentication processing (interface adaptation), and then encrypts the business information through the consensus algorithm (consensus management). After that, it is completely and consistently transmitted to the shared ledger (network communication) and recorded and stored; the smart contract module is responsible for the registration and issuance of contracts, as well as contract triggering and contract execution. Developers can define the contract logic in a programming language and publish it to the blockchain (contract registration). According to the logic of the contract terms, the key or other events are called to trigger the execution to complete the contract logic. At the same time, it also provides the function of contract upgrade and cancellation; the operation management module is mainly responsible for the deployment, configuration modification, contract setting, cloud adaptation and real-time status visualization output of the product during the product release process, such as alarm, network status detection, node equipment health status detection, etc.
[0104] The platform product service layer provides the basic capabilities and implementation framework of typical applications. Developers can superimpose business features based on these basic capabilities to complete the blockchain implementation of business logic. The application service layer provides application services based on blockchain solutions for business participants to use.
[0105] In one embodiment, the server provided by the present application can be used as a node in a blockchain system, encodes the text content to be translated to obtain a content embedding vector; obtains multiple initial parameter matrices; combines at least two initial parameter matrices to obtain a combined parameter matrix; linearly transforms the content embedding vector according to a target parameter matrix to obtain an attention weight, wherein the target parameter matrix includes the combined parameter matrix and the initial parameter matrices that are not combined in the multiple initial parameter matrices; weights the content embedding vector according to the attention weight to obtain an attention vector; after obtaining a translation result according to the attention vector, the translation result is verified, and after the verification is passed, it is stored in the blockchain as a new block to ensure that these extraction results will not be tampered with.
[0106] The method described in the above embodiment will be further described in detail below.
[0107] In this embodiment, the method of the embodiment of the present application will be described in detail by taking a neural network machine translation model including a Transformer module as an example.
[0108] The neural network machine translation model including the Transformer module includes at least an encoder and a decoder. The decoder can be composed of multiple decoding layers stacked, such as Figure 2a As shown, each decoding layer can be composed of a three-sublayer connection structure, wherein the first sublayer includes a masked multi-head attention layer and a first residual connection and a normalization layer, the second sublayer includes a multi-head attention layer and a second residual connection and a normalization layer, and the third sublayer includes a feedforward neural network layer and a third residual connection and a normalization layer.
[0109] like Figure 2b As shown, the specific process of a text translation method is as follows:
[0110] 210. Encode the text content to be translated to obtain a content embedding vector, and input the content embedding vector into a decoder.
[0111] The text to be translated can be input into the decoder of the neural network machine translation model including the Transformer module to map the text to a content embedding vector. The content embedding vector is then input into the decoder for processing to map the content embedding vector to the translated text content.
[0112] Before step 210, a training process of the neural network machine translation model including the Transformer module may also be included as follows:
[0113] 1. Obtain training samples and input them into the decoder.
[0114] The training sample may include text content, and the text content may be encoded to obtain a vector to be trained and then input into a decoder for decoding.
[0115] 2. For each decoding layer, in the masked multi-head attention layer, the input training samples are linearly transformed using the combined parameter matrix used for training to obtain the initial attention weights used for training.
[0116] In each decoding layer, the function “Q_K_V=x*(W Q / W K / W V )" to calculate the initial attention weight "Q_K_V" for training. Among them, _ represents connection, / represents concatenation, and x represents the input vector. Therefore, the calculation process only needs to call the kernel function once, which can reduce the amount of calculation, improve decoding efficiency, and thus improve text translation efficiency.
[0117] After obtaining the initial attention weights for training, we can also use the normalization function containing the bias "Output = Add_Bias_Softmax(Q_K_V, b Q , b K , b V )”, bias and normalize the initial attention weights used for training, where _ represents connection, “Q_K_V” represents attention weight, and b Q , b K and b V They are used to represent the deviation values corresponding to Q, K and V respectively.
[0118] 3. Calculate the weights of the training samples according to the initial attention weights for training to obtain the initial attention vector for training, and input the initial attention vector for training into the first residual connection and normalization layer.
[0119] After the initial attention weight "Q_K_V" for training is calculated by the above function, the dot product operation can be performed between the initial attention weight for training and the input vector to obtain the initial attention vector for training.
[0120] The processing methods of the following steps 4 to 7 are the same as those of steps 240 to 270 and will not be repeated here.
[0121] 4. In the first residual connection and normalization layer, the initial attention vector is processed by a batch processing function including bias processing and residual processing to obtain an attention vector, and the attention vector is input into the feedforward neural network.
[0122] 5. In the feedforward neural network layer, the attention vector is processed by the first weight matrix, the second weight matrix, the bias value, and the activation function including the bias processing to obtain an intermediate vector, and the intermediate vector is input into the third residual connection and normalization layer.
[0123] 6. In the third residual connection and standardization layer, the intermediate vector is processed by the residual function to obtain the feature vector, and the feature vector is input into the output layer.
[0124] 7. Input the output result of the decoding layer into the output layer to obtain the prediction result.
[0125] 8. Based on the preset loss function, prediction results and training samples, train the neural network machine translation model including the Transformer module. For example, the preset loss function can be used to calculate the loss of the prediction results and the training samples, and the parameters of the neural network machine translation model including the Transformer module can be updated by gradient descent according to the loss, and the parameters of the neural network machine translation model including the Transformer module can be iteratively updated until the calculated loss is less than the preset threshold, and the neural network machine translation model including the Transformer module obtained at this time is used in the text translation process.
[0126] It should be noted that in the training process (steps 1 to 8) and the use process (steps 210 to 270) of the neural network machine translation model including the Transformer module of the present application, the masked multi-head attention layer and the multi-head attention layer can also be used simultaneously. The data transmission process between the masked multi-head attention layer, the multi-head attention layer, the first residual connection and the standardization layer, the second residual connection and the standardization layer, and the third residual connection and the standardization layer can refer to the existing neural network machine translation model including the Transformer module. The data processing process inside the masked multi-head attention layer, the multi-head attention layer, the first residual connection and the standardization layer, the second residual connection and the standardization layer, and the third residual connection and the standardization layer is performed in accordance with the embodiment of the present application. In this way, the method of the embodiment of the present application can be used to improve the data processing process of the masked multi-head attention layer, the multi-head attention layer, the first residual connection and the standardization layer, the second residual connection and the standardization layer, and the third residual connection and the standardization layer in the existing neural network machine translation model including the Transformer module, reduce the number of calls to the underlying kernel function, reduce the amount of calculation, improve the decoding efficiency, and improve the text translation efficiency. However, it will not change the original structure of the existing neural network machine translation model including the Transformer module, and will not cause any loss in model performance.
[0127] 220. For each decoding layer, in the multi-head attention layer, the input vector is linearly transformed using the combined parameter matrix and the request vector parameter matrix to obtain the initial attention weight.
[0128] In each decoding layer, the function "Q, K_V = x*W can be used Q ,encoder_output*(W K / W V )" to calculate the initial attention weights "Q, K_V". Among them, _ represents connection, / represents concatenation, and x represents the input vector. Therefore, the calculation process only needs to call the kernel function twice, which can reduce the amount of calculation, improve the decoding efficiency, and thus improve the efficiency of text translation.
[0129] After obtaining the initial attention weights, we can also use the normalization function "Output = Add_Bias_Softmax(Q, K_V, b Q , b K , b V )” performs bias processing and normalization processing on the initial attention weight, and uses the processed initial attention weight in the weight calculation process in step 230, where _ represents connection, “Q, K_V” represents the initial attention weight, b Q 、b K and b V They are used to represent the deviation values corresponding to Q, K and V respectively.
[0130] It should be noted that when using the neural network machine translation model including the Transformer module for text translation, the stacked multiple decoding layers in the decoder can process the input vector in series in sequence. Steps 220 to 270 are the processing process of any decoding layer in the decoder. If the decoding layer is the first decoding layer of the input, the vector input in step 220 is the content embedding vector.
[0131] 230. In the multi-head attention layer, the input vector is weighted according to the initial attention weight to obtain the initial attention vector, and the initial attention vector is input into the second residual connection and normalization layer.
[0132] After the initial attention weights "Q, K_V" are calculated by the above function, the initial attention weights can be dot-producted with the input vector to obtain the initial attention vector. The obtained initial attention vector is input into the next second residual connection and normalization layer for processing.
[0133] 240. In the second residual connection and normalization layer, the initial attention vector is processed by a batch processing function including bias processing and residual processing to obtain an attention vector, and the attention vector is input into the feedforward neural network layer.
[0134] The processing of the initial attention vector in the second residual connection and normalization layer includes but is not limited to bias processing, residual processing, and batch processing operations. The batch processing function "f 1 =Add_Bias_Residual_LayerNormalization(x 1 ,x 2 ,b)” Implement the above operation to get the attention vector, where b represents the deviation value, x 1 and x 2 Represent different word embedding vectors respectively. Therefore, through this function, only one kernel function needs to be called to complete the three operations of addition and summation, residual network summation, and layer normalization. Therefore, reducing the number of calls to the underlying kernel function can reduce the amount of calculation, improve decoding efficiency, and thus improve text translation efficiency.
[0135] 250. In the feedforward neural network layer, the attention vector is processed by the first weight matrix, the second weight matrix, the bias value, and the activation function including the bias processing to obtain an intermediate vector, and the intermediate vector is input into the third residual connection and normalization layer.
[0136] After obtaining the attention vector, the attention vector can be weighted summed and activated in the feedforward neural network layer. The function "f 1 =W 1 *x 1 ","act_f 1 =Add_Bias_ReLU(f 1 , b 1 )" and "f 2 =W 2 *act_f 1 +b 2 "Perform weighted operation, activation and summation operation, and weighted summation operation in sequence, where W 1 represents the first weight matrix, W 2 represents the second weight matrix, b 1 , b 2 Indicates the deviation value, x 1 Represents the word embedding vector. Therefore, through the above function, only four kernel functions need to be called to implement the activation operation and two weighted sum operations, which can reduce the amount of calculation, improve the decoding efficiency, and thus improve the efficiency of text translation.
[0137] 260. In the third residual connection and normalization layer, the intermediate vector is processed by a residual function including bias processing to obtain a feature vector, and the feature vector is input into the next decoding layer.
[0138] After the attention vector is processed by the feedforward neural network layer to obtain an intermediate vector, the intermediate vector can be further processed in the third residual connection and normalization layer to obtain a feature vector, which includes but is not limited to residual processing and bias processing. For example, the function "f1 = Add_Bias_Residual(x 1 ,x 2 ,b)” implements the deviation operation and residual summation operation, where b represents the deviation value, x 1 and x 2 They represent different word embedding vectors. Therefore, through this function, only one kernel function needs to be called to implement the deviation operation and residual summation operation, which can reduce the amount of calculation, improve the decoding efficiency, and thus improve the text translation efficiency.
[0139] 270. Input the output result of the last decoding layer into the output layer to obtain the translation result.
[0140] Finally, the vector output from the decoder (i.e., the output result of the last decoding layer) is mapped to the translation result text in the output layer. For example, the probability distribution of words can be determined in the output layer, and the word with the highest probability is selected as the output result to obtain the translation result.
[0141] From the above, it can be seen that the text translation method provided in this embodiment combines functions of multiple operation processes in the masked multi-head attention layer, the multi-head attention layer, the first residual connection and normalization layer, the second residual connection and normalization layer, and the third residual connection and normalization layer of the decoding layer to process the input vector, which can reduce the number of calls to the kernel function, reduce the amount of calculation, and improve the decoding efficiency, so as to improve the text translation efficiency.
[0142] In addition, it has been experimentally verified that Figure 2c as well as Figure 2d As shown, compared with the existing neural network machine translation model including the Transformer module, the neural network machine translation model including the Transformer module using the text translation method of this embodiment can significantly improve the decoding speed. Figure 2c as well as Figure 2d The experimental results of decoding tests on the existing neural network machine translation model including the Transformer module and the neural network machine translation model including the Transformer module using the text translation method of this embodiment are shown under different BatchSizes and SequenceLengths, wherein: Figure 2c It is the experimental data table of the decoding test experiment. Figure 2d yes Figure 2c The table is a bar graph of the acceleration ratio data, in which Baseline represents the decoding speed of the existing neural network machine translation model including the Transformer module, Baseline+Kernel Fusion represents the decoding speed of the neural network machine translation model including the Transformer module using the text translation method of this embodiment, and SpeedUp represents the acceleration ratio between the decoding speeds of the neural network machine translation model including the Transformer module using the text translation method of this embodiment and the existing neural network machine translation model including the Transformer module. Figure 2c It can be seen from the results that the neural network machine translation model including the Transformer module using the text translation method of this embodiment can achieve an average acceleration ratio of x2.55 in different scenarios. Obviously, the text translation method of this embodiment including the Transformer module effectively improves the decoding efficiency of the current Transformer model, greatly saves hardware costs under the condition of tight computing resources, and can be widely used in business scenarios combining related technologies such as cross-language classification and small language machine translation.
[0143] In order to better implement the above method, the embodiment of the present application also provides a text translation device, which can be integrated in an electronic device, and the electronic device can be a terminal, a server, etc. Among them, the terminal can be a mobile phone, a tablet computer, a smart Bluetooth device, a laptop, a personal computer, etc.; the server can be a single server or a server cluster composed of multiple servers.
[0144] For example, in this embodiment, the method of the embodiment of the present application will be described in detail by taking the text translation device specifically integrated in the server as an example.
[0145] For example, Figure 3 As shown, the text translation device may include an encoding unit 310, an acquisition unit 320, a combination unit 330, a linear transformation unit 340, a calculation unit 350, and a translation unit 360, as follows:
[0146] (I) Encoding Unit 310
[0147] It is used to encode the text content to be translated and obtain the content embedding vector.
[0148] (II) Acquisition Unit 320
[0149] Used to obtain multiple initial parameter matrices.
[0150] (III) Combination unit 330
[0151] Used to combine at least two initial parameter matrices to obtain a combined parameter matrix.
[0152] In some implementations, the multiple initial parameter matrices include a request vector parameter matrix, a key vector parameter matrix, and a value vector parameter matrix. The combination unit 330 may be specifically used for the following steps:
[0153] Combining the key vector parameter matrix and the value vector parameter matrix to obtain a combined parameter matrix;
[0154] According to the target parameter matrix, the content embedding vector is linearly transformed to obtain the attention weights, including:
[0155] The content embedding vector is linearly transformed through the request vector parameter matrix and the combination parameter matrix to obtain the attention weight, which includes the request vector weight corresponding to the request vector parameter matrix and the combination weight corresponding to the combination parameter matrix.
[0156] (IV) Linear transformation unit 340
[0157] It is used to linearly transform the content embedding vector according to the target parameter matrix to obtain the attention weight. The target parameter matrix includes the combined parameter matrix and the parameter matrix that is not combined in the initial parameters.
[0158] In some implementations, the linear transformation unit 340 may be specifically used for the following steps:
[0159] Perform masking processing on the target parameter matrix to obtain a processed target parameter matrix;
[0160] According to the processed target parameter matrix, the content embedding vector is linearly transformed to obtain the attention weight.
[0161] (V) Computing unit 350
[0162] Used to calculate the weight of the content embedding vector according to the attention weight to obtain the attention vector.
[0163] In some implementations, the calculation unit 350 may be specifically used for the following steps:
[0164] Calculate the weight of the content embedding vector according to the attention weight to obtain the initial attention vector;
[0165] Get the batch processing function including deviation processing and residual processing;
[0166] The initial attention vector is processed by the batch function through bias processing, residual processing and batch processing to obtain the attention vector.
[0167] (VI) Translation Unit 360
[0168] Used to obtain translation results based on the attention vector.
[0169] In some implementations, the translation unit 360 may be specifically used for the following steps:
[0170] Obtain a first weight matrix, a second weight matrix, a bias value, and an activation function including bias processing;
[0171] Perform weight calculation on the attention vector using a first weight matrix to obtain a first vector;
[0172] The first vector is subjected to deviation processing and nonlinear changes through an activation function to obtain a second vector;
[0173] Perform weight calculation and deviation processing on the second vector using the second weight matrix and the deviation value to obtain an intermediate vector;
[0174] According to the intermediate vector, the translation result is obtained.
[0175] In some implementations, the step of obtaining a translation result according to the intermediate vector may include the following steps:
[0176] Get the residual function including deviation processing;
[0177] The residual function is used to process the intermediate vector, perform residual processing and deviation processing, and obtain the eigenvector;
[0178] According to the feature vector, the translation result is obtained.
[0179] In specific implementation, the above units can be implemented as independent entities, or can be arbitrarily combined to be implemented as the same or several entities. The specific implementation of the above units can refer to the previous method embodiments, which will not be repeated here.
[0180] As can be seen from the above, the text translation device of this embodiment can merge similar processing processes by combining initial parameter matrices, thereby reducing the amount of calculation, improving decoding efficiency, and thus improving text translation efficiency.
[0181] The present application also provides a kernel function combination method, such as Figure 4 As shown, the specific process of the kernel function combination method can be as follows:
[0182] 410. Obtain at least two initial kernel functions in the text translation model.
[0183] 420. Combine at least two initial kernel functions to obtain a combined kernel function, so as to update the text translation model according to the combined kernel function to obtain an updated text translation model.
[0184] The text translation model can be a neural network machine translation model including a Transformer module. For example, the kernel functions in the model can be grouped according to the network structure of the text translation model to determine at least one group of adjacent and mergeable initial kernel functions. At least two initial kernel functions corresponding to each group are obtained in step 410, and at least two kernel functions in the group are merged to obtain a combined kernel function for each group of initial kernel functions in step 420. The obtained combined kernel function is used to replace the initial kernel function used to combine the combined kernel function in the text translation model to obtain an updated text translation model, and the updated text translation model is used for any text translation method provided in the embodiments of the present application.
[0185] For example, in the multi-head attention layer of the text translation model, the initial kernel function Q = x*W can be obtained Q , K=x*W K And V = x * W V , and the combined kernel function "Q, K_V = x*W Q ,encoder_output*(W K / W V )". You can also get the initial kernel function Q 1 =Q+b Q , K 1 =V+b K , V 1 =V+b V , scores = Q 1 *K 1 , weights=softmax(scores) and output=weights*V 1 , and the combined kernel function "Output = Add_Bias_Softmax(Q, K_V, b Q , b K , b V )”.
[0186] For example, in the masked multi-head attention layer of the text translation model, the initial kernel function Q = x*W can be obtained Q , K=x*W K And V = x * W V , and combine to get Q_K_V=x*(W Q / W K / W V ). You can also get the initial kernel function Q 1 =Q+b Q , K 1 =V+b K, V 1 =V+b V , scores = Q 1 *K 1 , weights=softmax(scores) and output=weights*V 1 , and the combined kernel function "Output = Add_Bias_Softmax(Q_K_V, b Q , b K , b V )”.
[0187] For example, in the residual connection and normalization layer of the text translation model, the initial kernel function f can be obtained 1 =x 1 +b,rn_f 1 =Residual_Add(f 1 ,x 2 ) and ln_f 1 =LayerNormalization(rn_f 1 ), and the combined kernel function “f 1 =Add_Bias_Residual_LayerNormalization(x 1 ,x 2 ,b)”.
[0188] For example, in the feedforward neural network of the text translation model, the initial kernel function f can be obtained 1 =W 1 *x 1 +b 1 and act_f 1 =ReLU(f 1 ), and the combined kernel function “f 1 =W 1 *x 1 " and "act_f 1 =Add_Bias_ReLU(f 1 , b 1 )”.
[0189] For example, in the output layer of the decoding layer of the text translation model, the initial kernel function f can be obtained 1 =x 1 +b and rn_f 1 =Residual_Add(f 1 ,x 2 ), and the combined kernel function f1 = Add_Bias_Residual(x 1,x 2 ,b).
[0190] It should be noted that the above specific implementation can be referred to the previous method embodiment, which will not be repeated here.
[0191] The embodiment of the present application combines initial kernel functions and updates the text translation model according to the combined initial kernel functions, so as to improve the decoding efficiency of the updated text translation model and thus improve the text translation efficiency.
[0192] The embodiment of the present application also provides an electronic device, which can be a terminal, a server, etc. The terminal can be a mobile phone, a tablet computer, a smart Bluetooth device, a laptop, a personal computer, etc. The server can be a single server or a server cluster composed of multiple servers, etc.
[0193] In some embodiments, the text translation device may also be integrated into multiple electronic devices. For example, the text translation device may be integrated into multiple servers, and the text translation method of the present application may be implemented by multiple servers.
[0194] In this embodiment, the electronic device of this embodiment is a server as an example for detailed description, for example, Figure 5 As shown, it shows a schematic diagram of the structure of the server involved in the embodiment of the present application, specifically:
[0195] The server may include one or more processing core processors 510, one or more computer-readable storage media memories 520, a power supply 530, an input module 540, and a communication module 550. Those skilled in the art will appreciate that Figure 5 The server structure shown in the figure does not constitute a limitation on the server, and may include more or fewer components than shown in the figure, or combine certain components, or arrange the components differently. Among them:
[0196] The processor 510 is the control center of the SSS. It uses various interfaces and lines to connect various parts of the entire server. It executes various functions of the server and processes data by running or executing software programs and / or modules stored in the memory 520 and calling data stored in the memory 520. In some embodiments, the processor 510 may include one or more processing cores; in some embodiments, the processor 510 may integrate an application processor and a modem processor, wherein the application processor mainly processes the operating system, user interface, and application programs, and the modem processor mainly processes wireless communications. It is understandable that the above-mentioned modem processor may not be integrated into the processor 510.
[0197] The memory 520 can be used to store software programs and modules. The processor 510 executes various functional applications and data processing by running the software programs and modules stored in the memory 520. The memory 520 may mainly include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function (such as a sound playback function, an image playback function, etc.), etc.; the data storage area may store data created according to the use of the server, etc. In addition, the memory 520 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other volatile solid-state storage devices. Accordingly, the memory 520 may also include a memory controller to provide the processor 510 with access to the memory 520.
[0198] The server also includes a power supply 530 for supplying power to various components. In some embodiments, the power supply 530 can be logically connected to the processor 510 through a power management system, so that the power management system can manage charging, discharging, power consumption, etc. The power supply 530 can also include any components such as one or more DC or AC power supplies, recharging systems, power failure detection circuits, power converters or inverters, and power status indicators.
[0199] The server may further include an input module 540, which may be used to receive input digital or character information and generate keyboard, mouse, joystick, optical or trackball signal inputs related to user settings and function controls.
[0200] The server may further include a communication module 550. In some embodiments, the communication module 550 may include a wireless module. The server may perform short-range wireless transmission through the wireless module of the communication module 550, thereby providing users with wireless broadband Internet access. For example, the communication module 550 may be used to help users send and receive emails, browse web pages, and access streaming media.
[0201] Although not shown, the server may further include a display unit, etc., which will not be described in detail herein. Specifically, in this embodiment, the processor 510 in the server will load the executable files corresponding to the processes of one or more application programs into the memory 520 according to the following instructions, and the processor 510 will run the application programs stored in the memory 520, thereby realizing various functions, as follows:
[0202] The text content to be translated is encoded to obtain a content embedding vector; multiple initial parameter matrices are obtained; at least two initial parameter matrices are combined to obtain a combined parameter matrix; according to a target parameter matrix, the content embedding vector is linearly transformed to obtain an attention weight, wherein the target parameter matrix includes a combined parameter matrix and initial parameter matrices that are not combined in the multiple initial parameter matrices; according to the attention weight, the content embedding vector is weighted to obtain an attention vector; and according to the attention vector, a translation result is obtained.
[0203] The specific implementation of the above operations can be found in the previous embodiments, which will not be described in detail here.
[0204] As can be seen from the above, in this application, by combining the initial parameter matrix, similar processing processes can be integrated, the amount of calculation can be reduced, the decoding efficiency can be improved, and the text translation efficiency can be improved.
[0205] A person of ordinary skill in the art will appreciate that all or part of the steps in the various methods of the above embodiments may be completed by instructions, or by controlling related hardware through instructions. The instructions may be stored in a computer-readable storage medium and loaded and executed by a processor.
[0206] To this end, an embodiment of the present application provides a computer-readable storage medium, in which a plurality of instructions are stored, and the instructions can be loaded by a processor to execute the steps in any text translation method provided in the embodiment of the present application. For example, the instructions can execute the following steps:
[0207] The text content to be translated is encoded to obtain a content embedding vector; multiple initial parameter matrices are obtained; at least two initial parameter matrices are combined to obtain a combined parameter matrix; according to a target parameter matrix, the content embedding vector is linearly transformed to obtain an attention weight, wherein the target parameter matrix includes a combined parameter matrix and initial parameter matrices that are not combined in the multiple initial parameter matrices; according to the attention weight, the content embedding vector is weighted to obtain an attention vector; and according to the attention vector, a translation result is obtained.
[0208] The storage medium may include: a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, etc.
[0209] According to one aspect of the present application, a computer program product or computer program is provided, the computer program product or computer program including a computer program / instruction, the computer program / instruction being stored in a computer-readable storage medium. A processor of a computer device reads the computer program / instruction from the computer-readable storage medium, and the processor executes the computer program / instruction, so that the computer device performs the steps in any one of the text translation methods provided in the above embodiments.
[0210] Since the instructions stored in the storage medium can execute the steps in any text translation method provided in the embodiments of the present application, the beneficial effects that can be achieved by any text translation method provided in the embodiments of the present application can be achieved. Please refer to the previous embodiments for details and will not be repeated here.
[0211] The above is a detailed introduction to a text translation method, device, kernel function combination method, server and medium provided in the embodiments of the present application. Specific examples are used in this article to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only used to help understand the method of the present application and its core idea; at the same time, for technical personnel in this field, according to the ideas of the present application, there will be changes in the specific implementation methods and application scope. In summary, the content of this specification should not be understood as a limitation on the present application.
Claims
1. A text translation method, characterized in that: include: Encode the text content to be translated to obtain a content embedding vector; Obtaining multiple initial parameter matrices and initial kernel functions corresponding to each initial parameter matrix; Combining at least two initial parameter matrices to obtain a combined parameter matrix, including: By combining at least two initial parameter matrices, a combined kernel function is obtained according to the initial kernel functions corresponding to the initial parameter matrices, wherein the combination process of the initial kernel functions is to set a group of thread groups for processing the initial kernel functions corresponding to the initial parameter matrices, and the thread groups are started by calling the combined kernel function; According to a target parameter matrix, a linear transformation is performed on the content embedding vector to obtain an attention weight, wherein the target parameter matrix includes the combined parameter matrix and an uncombined initial parameter matrix among the multiple initial parameter matrices, including: When the combined kernel function of the combined parameter matrix is called, the corresponding thread group is started, and the initial kernel function in the combined kernel function is sequentially used in the corresponding thread group to linearly change the content embedding vector to obtain an attention weight; Performing weight calculation on the content embedding vector according to the attention weight to obtain an attention vector; A translation result is obtained according to the attention vector.
2. The text translation method according to claim 1, characterized in that: The multiple initial parameter matrices include a request vector parameter matrix, a key vector parameter matrix, and a value vector parameter matrix, and the combining at least two initial parameter matrices to obtain a combined parameter matrix includes: Combining the key vector parameter matrix and the value vector parameter matrix to obtain a combined parameter matrix; The linear transformation of the content embedding vector according to the target parameter matrix to obtain the attention weight includes: The content embedding vector is linearly transformed by the request vector parameter matrix and the combination parameter matrix to obtain attention weights, wherein the attention weights include the request vector weights corresponding to the request vector parameter matrix and the combination weights corresponding to the combination parameter matrix.
3. The text translation method according to claim 1, characterized in that: The linear transformation of the content embedding vector according to the target parameter matrix to obtain the attention weight includes: Performing masking processing on the target parameter matrix to obtain a processed target parameter matrix; According to the processed target parameter matrix, the content embedding vector is linearly transformed to obtain an attention weight.
4. The text translation method according to claim 1, characterized in that: The step of performing weight calculation on the content embedding vector according to the attention weight to obtain the attention vector includes: Performing weight calculation on the content embedding vector according to the attention weight to obtain an initial attention vector; Get the batch processing function including deviation processing and residual processing; The initial attention vector is subjected to deviation processing, residual processing and batch processing by the batch processing function to obtain an attention vector.
5. The text translation method according to claim 1, characterized in that: Obtaining a translation result according to the attention vector includes: Obtain a first weight matrix, a second weight matrix, a bias value, and an activation function including bias processing; Performing weight calculation on the attention vector using the first weight matrix to obtain a first vector; Performing deviation processing and nonlinear changes on the first vector by using the activation function to obtain a second vector; Performing weight calculation and deviation processing on the second vector using the second weight matrix and the deviation value to obtain an intermediate vector; A translation result is obtained according to the intermediate vector.
6. The text translation method according to claim 5, characterized in that: Obtaining a translation result according to the intermediate vector includes: Get the residual function including deviation processing; Performing residual processing and deviation processing on the intermediate vector by using the residual function to obtain a feature vector; A translation result is obtained according to the feature vector.
7. A text translation device, characterized in that: include: An encoding unit, used for encoding the text content to be translated to obtain a content embedding vector; An acquisition unit, used to acquire a plurality of initial parameter matrices and an initial kernel function corresponding to each initial parameter matrix; The combining unit is used to combine at least two initial parameter matrices to obtain a combined parameter matrix, including: By combining at least two initial parameter matrices, a combined kernel function is obtained according to the initial kernel functions corresponding to the initial parameter matrices, wherein the combination process of the initial kernel functions is to set a group of thread groups for processing the initial kernel functions corresponding to the initial parameter matrices, and the thread groups are started by calling the combined kernel function; A linear transformation unit, configured to perform a linear transformation on the content embedding vector according to a target parameter matrix to obtain an attention weight, wherein the target parameter matrix includes the combined parameter matrix and a parameter matrix that is not combined in the initial parameters, including: When the combined kernel function of the combined parameter matrix is called, the corresponding thread group is started, and the initial kernel function in the combined kernel function is sequentially used in the corresponding thread group to linearly change the content embedding vector to obtain an attention weight; A calculation unit, configured to perform weight calculation on the content embedding vector according to the attention weight to obtain an attention vector; A translation unit is used to obtain a translation result according to the attention vector.
8. A kernel function combination method, characterized in that: include: Obtain at least two initial kernel functions in the text translation model; The at least two initial kernel functions are combined to obtain a combined kernel function, wherein the combination process of the initial kernel functions is to set a group of thread groups for processing the initial kernel functions corresponding to the initial parameter matrices, and the thread groups are started by calling the combined kernel function so as to update the text translation model according to the combined kernel function to obtain an updated text translation model, and the updated text translation model is used for the steps in the text translation method according to any one of claims 1 to 6.
9. A server, characterized in that: The invention comprises a processor and a memory, wherein the memory stores a plurality of instructions; the processor loads instructions from the memory to execute the steps in the text translation method according to any one of claims 1 to 6.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a plurality of instructions, and the instructions are suitable for being loaded by a processor to execute the steps in the text translation method according to any one of claims 1 to 6.
11. A computer program product comprising a computer program / instructions, characterized in that When the computer program / instructions are executed by a processor, the steps in the text translation method according to any one of claims 1 to 6 are implemented.
Citation Information
Patent Citations
Text processing model operation method and device, electronic equipment and storage medium
CN110909527A
