A language model training method and related device
By dividing the language model by layers and the sequence dimension of the training sample set, and adopting the pipeline parallel training method, the problem of large memory requirements of the pipeline parallel training method is solved, and more efficient memory usage and training efficiency are achieved.
Patent Information
- Application Number
- CN202410623693.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-20
- Publication Date
- 2024-08-23
- Estimated Expiration
- 2044-05-20
AI Technical Summary
The pipeline parallel training method requires a large amount of memory, which may cause insufficient memory on the computing device and affect model training.
Divide the language model into multiple model blocks by layer and deploy them to multiple computing devices. Divide the training sample set and segment it in sequence dimensions. Use pipeline parallelism for model training to reduce activation value data in forward calculations. volume, reducing memory requirements.
It effectively reduces memory requirements and pipeline cavitation rate, and improves the efficiency of model training.
Smart Images

Figure CN118195033B
Abstract
Description
Technical Field
[0001] The present application relates to the field of model training technology, and in particular to a language model training method and related devices. Background Art
[0002] In recent years, with the substantial increase in computing power and data volume, ultra-large-scale language models based on the decoder-only transformer architecture have developed rapidly and have been widely used in many natural language processing tasks. At the same time, research shows that language models based on the decoder-only transformer architecture have a scaling law, that is, the larger the model parameters and the larger the data set, the higher the model performance. This has led to an exponential increase in the number of language model parameters in recent years. For example, the number of parameters of GPT3, OPT, and BLOOM is 175B+, and the number of parameters of PaLM and MT-NLG is 530B+. Training these models is highly challenging.
[0003] In the past few years, a variety of parallel technologies have been proposed to train large-scale language models, and pipeline parallel technology is one of them. Pipeline parallel technology refers to dividing the model into multiple parts that are connected to each other by layer, deploying the divided parts to multiple computing devices, and controlling the calculation order of multiple computing devices by adopting a reasonable scheduling strategy to complete the model training task. At present, when using pipeline parallel training, the training sample set used for each round of training is usually evenly divided into multiple training sample subsets. Then, using multiple training sample subsets, multiple computing devices are controlled to perform model training according to preset scheduling strategies (such as 1F1B (One Forward pass followed by One Backward pass), ZB (Zero bubble), etc.).
[0004] The model training process includes the forward calculation stage and the backward calculation stage. In the forward calculation stage, the computing device will cache the activation values generated by its forward calculation in the memory (the computing device will use the activation values when performing backward calculation). The current pipeline parallel training method has a large demand for memory, which may cause the computing device to run out of memory, and insufficient memory will seriously affect model training. Summary of the invention
[0005] In view of this, the present application provides a language model training method and related devices to solve the problem that the current pipeline parallel training method has a large demand for memory. The technical solution is as follows:
[0006] The first aspect of the present application provides a language model training method, comprising:
[0007] Divide the language model into multiple model blocks by layer, and deploy the multiple model blocks on multiple computing devices, wherein each model block obtained by the division includes one layer or multiple consecutive layers in the language model, and one or more model blocks of the multiple model blocks are deployed on each computing device;
[0008] Acquire a training sample set, wherein the training sample set includes A training sequences of length S acquired from a training sequence set, where A and S are both integers greater than 1;
[0009] Dividing the training sample set to obtain multiple training sample subsets, wherein each training sample subset includes B training sequences of length S, where B is an integer less than A;
[0010] The plurality of training sample subsets are respectively divided in the sequence dimension to obtain subsequence block sets corresponding to the plurality of training sample subsets, wherein each subsequence block in the subsequence block set includes B subsequences;
[0011] The plurality of computing devices are controlled to perform model training by using the subsequence blocks in the subsequence block sets corresponding to the plurality of training sample subsets, respectively, in a pipeline parallel training manner.
[0012] In a possible implementation, dividing the language model into multiple model blocks by layer and deploying the multiple model blocks on multiple computing devices includes:
[0013] Divide the language model into N model blocks by layer, and deploy the N model blocks on N computing devices, wherein one model block of the N model blocks is deployed on each computing device, and N is an integer greater than 1;
[0014] Alternatively, the language model is divided into M model blocks by layer, and the M model blocks are deployed on N computing devices, wherein one model block or multiple discontinuous model blocks among the M model blocks are deployed on each computing device, and M is an integer greater than N.
[0015] In a possible implementation, the dividing the plurality of training sample subsets in the sequence dimension to obtain subsequence block sets corresponding to the plurality of training sample subsets respectively includes:
[0016] For each training sample subset, the training sample subset is divided into C parts of equal length in the sequence dimension, and the C subsequence blocks obtained by the division form a subsequence block set corresponding to the training sample subset, where C is an integer greater than 1.
[0017] In a possible implementation, the method of using each subsequence block in the subsequence block set corresponding to each of the multiple training sample subsets, adopting a pipeline parallel training method, and controlling the multiple computing devices to perform model training includes:
[0018] Inputting each subsequence block in the subsequence block set corresponding to each training sample subset into a computing operation pipeline composed of the plurality of computing devices one by one in sequence for computing, wherein the computing operation pipeline performs forward computing and backward computing for each input subsequence block;
[0019] For each computing device on the computing operation pipeline, after controlling the computing device to complete forward calculation and backward calculation for each subsequence block in the subsequence block sets corresponding to the multiple training sample subsets, the computing device is controlled to update the parameters of the model block deployed by the computing device according to the gradient data obtained by the computing device through reverse calculation for each subsequence block in the subsequence block sets corresponding to the multiple training sample subsets.
[0020] In a possible implementation, the computing process of each computing device on the computing operation pipeline includes a first computing stage, a second computing stage, and a third computing stage;
[0021] Each computing device on the computing operation pipeline performs only forward computing in the first computing stage, performs forward computing and reverse computing alternately in the second computing stage, and performs only reverse computing in the third computing stage.
[0022] The forward computation performed by each computing device in the computing operation pipeline includes causal attention computation;
[0023] In a possible implementation, for a subsequence block in a subsequence block set, a process in which a computing device on the computing operation pipeline performs causal attention calculation on the subsequence block includes:
[0024] Determine the Q matrix, K matrix and V matrix of the subsequence block according to the input data;
[0025] The K matrix of the forward subsequence block of the subsequence block is concatenated with the K matrix of the subsequence block, and the concatenated matrix is used as the target K matrix corresponding to the subsequence block; and the V matrix of the forward subsequence block of the subsequence block is concatenated with the V matrix of the subsequence block, and the concatenated matrix is used as the target V matrix corresponding to the subsequence block, wherein the forward subsequence block of the subsequence block is a subsequence block located before the subsequence block in the subsequence block set where the subsequence block is located;
[0026] Perform causal attention calculation on the Q matrix of the subsequence block, the target K matrix corresponding to the subsequence block, and the target V matrix corresponding to the subsequence block.
[0027] The backward calculation performed by each computing device on the computing operation pipeline includes the calculation of target gradients corresponding to the Q matrix, the K matrix and the V matrix respectively;
[0028] In a possible implementation, a process in which a computing device on the computing operation pipeline determines target gradients corresponding to a Q matrix, a K matrix, and a V matrix of a subsequence block respectively includes:
[0029] Calculate the gradient corresponding to the Q matrix of the subsequence block, and use the obtained gradient as the target gradient corresponding to the Q matrix of the subsequence block;
[0030] Calculate the gradient corresponding to the target K matrix corresponding to the subsequence block, wherein the gradient corresponding to the target K matrix corresponding to the subsequence block includes the gradient corresponding to the K matrix of the subsequence block and the gradient corresponding to the K matrix of the forward subsequence block of the subsequence block; sum the gradient corresponding to the K matrix of the subsequence block in the gradient corresponding to the target K matrix corresponding to the subsequence block and the gradient corresponding to the K matrix of the subsequence block in the gradient corresponding to the target K matrix corresponding to the backward subsequence block of the subsequence block, and use the summed gradient as the target gradient corresponding to the K matrix of the subsequence block; wherein the backward subsequence block of the subsequence block is a subsequence block located after the subsequence block in the subsequence block set where the subsequence block is located;
[0031] Calculate the gradient corresponding to the target V matrix corresponding to the subsequence block, wherein the gradient corresponding to the target V matrix corresponding to the subsequence block includes the gradient corresponding to the V matrix of the subsequence block and the gradient corresponding to the V matrix of the forward subsequence block of the subsequence block; sum the gradient corresponding to the V matrix of the subsequence block in the gradient corresponding to the target V matrix corresponding to the subsequence block and the gradient corresponding to the V matrix of the backward subsequence block of the subsequence block, and use the summed gradient as the target gradient corresponding to the V matrix of the subsequence block.
[0032] The second aspect of the present application provides a language model training device, including: a model segmentation and deployment module, a training sample set acquisition module, a training sample set division module, a training sample subset segmentation module and a model training module;
[0033] The model segmentation and deployment module is used to segment the language model into multiple model blocks by layer, and deploy the multiple model blocks on multiple computing devices, wherein each model block obtained by segmentation includes one layer or multiple consecutive layers in the language model, and one or more model blocks of the multiple model blocks are deployed on each computing device;
[0034] The training sample set acquisition module is used to acquire a training sample set, wherein the training sample set includes A training sequences of length S acquired from a training sequence set, where A and S are both integers greater than 1;
[0035] The training sample set division module is used to divide the training sample set to obtain multiple training sample subsets, wherein each training sample subset includes B training sequences of length S, where B is an integer less than A;
[0036] The training sample subset segmentation module is used to segment the multiple training sample subsets in the sequence dimension respectively to obtain subsequence block sets corresponding to the multiple training sample subsets respectively, wherein each subsequence block in the subsequence block set includes B subsequences;
[0037] The model training module is used to use each subsequence block in the subsequence block set corresponding to the multiple training sample subsets, adopt a pipeline parallel training method, and control the multiple computing devices to perform model training.
[0038] A third aspect of the present application provides an electronic device, comprising at least one processor and a memory connected to the processor, wherein:
[0039] The memory is used to store computer programs;
[0040] The processor is used to execute the computer program so that the electronic device can implement any one of the language model training methods described above.
[0041] In a fourth aspect, the present application provides a computer storage medium, which carries one or more computer programs. When the one or more computer programs are executed by an electronic device, the electronic device can implement any one of the language model training methods described above.
[0042] A fifth aspect of the present application provides a computer program product, comprising computer-readable instructions, which, when executed on an electronic device, enables the electronic device to implement any one of the above-described language model training methods.
[0043] By means of the above technical scheme, the language model training method provided by the present application first divides the language model into multiple model blocks by layer, and deploys the multiple model blocks on multiple computing devices, then obtains a training sample set, and then divides the training sample set to obtain multiple training sample subsets, and then divides the multiple training sample subsets in the sequence dimension to obtain subsequence block sets corresponding to the multiple training sample subsets, and finally uses each subsequence block in the subsequence block sets corresponding to the multiple training sample subsets, adopts a pipeline parallel training method, and controls multiple computing devices to perform model training. After obtaining multiple training sample subsets, the language model training method provided by the present application does not directly use the training sample subsets for model training, but further divides the training sample subsets in the sequence dimension, so that a training sample subset is divided into multiple subsequence blocks, and then the model is trained using each subsequence block obtained by the division. In the forward calculation stage, since the computing device performs forward calculation on the subsequence block instead of calculating on the training sample subset, the data amount of the activation value generated by the forward calculation on the subsequence block is greatly reduced compared to the data amount of the activation value generated by the forward calculation on the training sample subset, thereby greatly reducing the demand for memory. In addition, in addition to effectively reducing the memory demand, using the subsequence blocks obtained by dividing the training sample subset for model training can also effectively reduce the flow water cavitation rate. BRIEF DESCRIPTION OF THE DRAWINGS
[0044] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying any creative work.
[0045] Figure 1 A flow chart of a language model training method provided in an embodiment of the present application;
[0046] Figure 2 A flow chart of controlling multiple computing devices to perform model training using a pipeline parallel training method using each subsequence block in a subsequence block set corresponding to multiple training sample subsets provided in an embodiment of the present application;
[0047] Figure 3 A schematic diagram of performing model training using the 1F1B-I scheduling strategy using each subsequence block in the subsequence block set corresponding to the training sample subset provided in an embodiment of the present application;
[0048] Figure 4A schematic diagram of performing model training using the ZB-V scheduling strategy using each subsequence block in the subsequence block set corresponding to the training sample subset provided in an embodiment of the present application;
[0049] Figure 5 A schematic diagram of the structure of a language model training device provided in an embodiment of the present application;
[0050] Figure 6 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0051] 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 ordinary technicians in this field without creative work are within the scope of protection of this application.
[0052] In view of the large memory requirements of the current pipeline parallel training method, research was conducted. The initial idea was to use recalculation technology, that is, the computing device only caches the input data in the memory, and does not cache the activation values generated in the forward calculation phase. When the computing device performs backward calculation in the backward calculation phase, it re-performs forward calculation on the cached input data to obtain the activation value, and then performs gradient calculation based on the calculated activation value.
[0053] Since the above idea does not need to cache the activation values generated in the forward calculation phase, the demand for memory is greatly reduced. However, research on the above idea found that although the above idea greatly reduces the demand for memory, it brings new problems, namely, introducing a large amount of calculation.
[0054] Further research was conducted to address the problems with the above ideas. Through continuous research, a language model training method with better results was finally proposed. This language model training method can effectively reduce memory requirements while introducing lower additional computing power.
[0055] Next, the language model training method provided by this application is introduced through the following embodiments.
[0056] See also Figure 1 , shows a flow chart of a language model training method provided in an embodiment of the present application, which method may include:
[0057] Step S101: Divide the language model into multiple model blocks by layer, and deploy the multiple model blocks on multiple computing devices.
[0058] Among them, each model block obtained by segmentation includes one layer or multiple continuous layers of the language model, and one model block or multiple model blocks among the multiple model blocks are deployed on each computing device (the multiple model blocks here may be continuous or discontinuous), and one layer, multiple continuous layers, or multiple discontinuous layers of the language model are deployed on each computing device.
[0059] After deploying multiple model blocks on multiple computing devices, parameters of the model blocks deployed on the multiple computing devices may be initialized (eg, randomly initialized).
[0060] Step S102: Obtain a training sample set.
[0061] The training sample set includes A training sequences with a length of S obtained from the training sequence set, the training sequence set includes a large number of training sequences with a length of S, and A and S are both integers greater than 1.
[0062] Step S103: Divide the training sample set to obtain multiple training sample subsets.
[0063] Each training sample subset includes B training sequences of length S, where B is an integer less than A.
[0064] Exemplarily, the training sample set includes 100 training sequences of length S. Then, in this step, the training sample set can be divided into 5 training sample subsets, each of which includes 20 training sequences of length S.
[0065] Step S104: splitting the multiple training sample subsets in the sequence dimension respectively to obtain subsequence block sets corresponding to the multiple training sample subsets respectively.
[0066] The training sample subset is segmented in the sequence dimension, that is, the training sequence contained in the training sample subset is segmented into multiple segments. Since the training sample subset includes multiple training sequences, after the training sample subset is segmented in the sequence dimension, multiple subsequence blocks can be obtained, and each subsequence block includes B subsequences.
[0067] Step S105: using each subsequence block in the subsequence block set corresponding to each of the plurality of training sample subsets, adopting a pipeline parallel training method, and controlling a plurality of computing devices to perform model training.
[0068] The language model training method provided in the embodiment of the present application first divides the language model into multiple model blocks by layer, and deploys the multiple model blocks on multiple computing devices, then obtains a training sample set, and then divides the training sample set to obtain multiple training sample subsets, and then divides the multiple training sample subsets in the sequence dimension to obtain subsequence block sets corresponding to the multiple training sample subsets, and finally uses each subsequence block in the subsequence block sets corresponding to the multiple training sample subsets, adopts a pipeline parallel training method, and controls multiple computing devices to perform model training. After obtaining multiple training sample subsets, the language model training method provided in the embodiment of the present application does not directly use the training sample subsets for model training, but further divides the training sample subsets in the sequence dimension, so that a training sample subset is divided into multiple subsequence blocks, and then the model is trained using each subsequence block obtained by the division. In the forward calculation stage, since the computing device performs forward calculation on the subsequence block instead of forward calculation on the training sample subset, the data amount of the activation value generated by the forward calculation on the subsequence block is greatly reduced compared to the data amount of the activation value generated by the forward calculation on the training sample subset, thereby greatly reducing the demand for memory. In addition, in addition to effectively reducing the memory demand, the use of the subsequence blocks obtained by dividing the training sample subset for model training can also effectively reduce the flow water cavitation rate.
[0069] In another embodiment of the present application, the specific implementation process of "step S101: dividing the language model into multiple model blocks by layer, and deploying the multiple model blocks to multiple computing devices" in the above embodiment is introduced.
[0070] There are many ways to divide the language model into multiple model blocks by layer and deploy the multiple model blocks on multiple computing devices. This embodiment provides the following two implementation methods.
[0071] The first implementation method is: divide the language model into N model blocks by layer, and deploy the N model blocks to N computing devices, where one model block of the N model blocks is deployed on each computing device, and N is an integer greater than 1.
[0072] In order to obtain better training results, preferably, the language model can be evenly segmented, that is, each model block obtained by segmentation contains the same number of layers.
[0073] Exemplarily, the language model includes 8 layers (layer 1 to layer 8), and there are 4 computing devices (computing device 1 to computing device 4). The language model can be divided into 4 model blocks, each model block includes 2 consecutive layers, wherein the 1st model block includes the 1st layer and the 2nd layer, the 2nd model block includes the 3rd layer and the 4th layer, the 3rd model block includes the 5th layer and the 6th layer, and the 4th model block includes the 7th layer and the 8th layer. The 4 model blocks obtained by segmentation are deployed on 4 computing devices. Specifically, the 1st model block is deployed on computing device 1, the 2nd model block is deployed on computing device 2, the 3rd model block is deployed on computing device 3, and the 4th model block is deployed on computing device 4. It should be noted that since the 4 model blocks have a dependency relationship, the 4 computing devices also have a dependency relationship accordingly.
[0074] The second implementation method is: divide the language model into M model blocks by layer, and deploy the M model blocks to N computing devices, wherein one model block or multiple discontinuous model blocks among the M model blocks are deployed on each computing device, and M is an integer greater than N.
[0075] In order to obtain better training results, preferably, the language model can be evenly divided and the same number of model blocks can be deployed on each computing device. If multiple model blocks are deployed on each computing device, multiple discontinuous model blocks are deployed.
[0076] Exemplarily, the language model includes 8 layers (layer 1 to layer 8) and there are 4 computing devices (computing device 1 to computing device 4). The language model can be divided into 8 model blocks, each model block is a layer of the language model, that is, the 1st model block is the 1st layer, the 2nd model block is the 2nd layer, the 3rd model block is the 3rd layer, ..., the 7th model block is the 7th layer, and the 8th model block is the 8th layer. The 8 model blocks obtained by dividing are deployed to 4 computing devices. Specifically, the 1st and 5th layers of the language model are deployed on computing device 1, the 2nd and 6th layers of the language model are deployed on computing device 2, the 3rd and 7th layers of the language model are deployed on computing device 3, and the 4th and 8th layers of the language model are deployed on computing device 4.
[0077] Compared with deploying multiple continuous layers on a computing device, deploying multiple discontinuous layers on a computing device can reduce the amount of computation and computing time of each forward calculation performed by the computing device, and can also reduce the memory usage of the activation values generated by each forward calculation performed by the computing device.
[0078] It should also be noted that if the language model is split and deployed on N computing devices, the pipeline parallelism of pipeline parallel training is N. If multiple discontinuous model blocks are deployed on each computing device, there is also virtual pipeline parallelism. For example, in the above example, 2 discontinuous model blocks are deployed on each computing device, and the virtual pipeline parallelism of pipeline parallel training is 2.
[0079] In another embodiment of the present application, the specific implementation process of "step S104: dividing the multiple training sample subsets in the sequence dimension respectively to obtain subsequence block sets corresponding to the multiple training sample subsets respectively" in the above embodiment is introduced.
[0080] In a possible implementation, the process of dividing the multiple training sample subsets in the sequence dimension to obtain the subsequence block sets corresponding to the multiple training sample subsets may include:
[0081] For each training sample subset, the training sample subset is divided into C parts of equal length in the sequence dimension, and the C subsequence blocks obtained by the division form a subsequence block set corresponding to the training sample subset.
[0082] Where C is an integer greater than 1. The training sample subset is divided into C parts of equal length in the sequence dimension, and C subsequence blocks of length S / C are obtained. The C subsequence blocks of length S / C constitute the subsequence block set corresponding to the training sample subset. Each subsequence block in the subsequence block set corresponding to the training sample subset includes B subsequence blocks of length sequence.
[0083] If the nth training sample subset among multiple training sample subsets is represented as x n , x n In the sequence dimension, it is divided into C parts of equal length to obtain a subsequence block x n 0 、x n 1 , …, x n i 、x n C-1 , where x n i , S′=S / C (C divides S).
[0084] In addition to dividing the training sample subset into C parts in the sequence dimension in the above manner, other methods can be used for division, for example, dividing the training sample subset into C parts of unequal lengths in the sequence dimension. In order to obtain higher training efficiency and better training effect, the embodiment of the present application preferably divides the training sample subset into C parts of equal length in the sequence dimension.
[0085] In another embodiment of the present application, the implementation process of "step S105: utilizing each subsequence block in the subsequence block set corresponding to multiple training sample subsets, adopting a pipeline parallel training method, and controlling multiple computing devices to perform model training" in the above embodiment is introduced.
[0086] See also Figure 2 , shows a flow chart of controlling multiple computing devices to perform model training using each subsequence block in a subsequence block set corresponding to multiple training sample subsets, using a pipeline parallel training method, which may include:
[0087] Step S201: inputting each subsequence block in the subsequence block set corresponding to each training sample subset into a calculation operation pipeline composed of multiple computing devices one by one in order for calculation.
[0088] Among them, a computing operation pipeline composed of multiple computing devices performs forward calculations and backward calculations for each subsequence block of the input.
[0089] When each computing device on the computing operation pipeline performs forward calculations for a subsequence block, it caches the calculated activation values in the memory for use when performing backward calculations for the subsequence block. When each computing device on the computing operation pipeline completes backward calculations for a subsequence block, it clears the activation values cached when performing forward calculations for the subsequence block to free up memory space, thereby facilitating caching the activation values obtained by forward calculations for the next subsequence block. It should be noted that each computing device will generate multiple activation values during the forward calculation process, the intermediate activation values will be cached in the memory, and the last activation value will be output to the next computing device as the final forward calculation result of the computing device.
[0090] Next, a computing operation pipeline composed of multiple computing devices is given for the i-th subsequence block x in a subsequence block set. n i The process of performing forward and backward calculations.
[0091] Forward calculation phase: The first computing device in the calculation operation pipeline performs a subsequence block x n i Perform forward calculation to obtain its own x n i The forward calculation result rf n 1 , the first computing device for x n i The forward calculation result rf n 1 Input the second computing device, the second computing device responds to the input rfn 1 Perform forward calculation to obtain its own x n i Forward calculation result rf n 2 , .., the N-1th computing device targets x n i The forward calculation result rf n N-1 Input the Nth computing device, the Nth computing device responds to the input rf n N-1 Perform forward calculation to obtain its own x n i The forward calculation result rf n N .
[0092] In the backward calculation phase: the Nth computing device calculates the value of x according to its own n i The forward calculation result rf n N Calculate the loss function, and then use the loss function and itself for x n i The activation value of the cache (i.e., the value of the n N-1 The activation value generated by the forward calculation is used for gradient calculation. The calculated gradient includes the gradient for the input data and the gradient for the model parameters (weights). The gradient for the input data calculated by the Nth computing device is input to the N-1th computing device. The N-1th computing device calculates the gradient for the input data based on the input gradient and its own gradient for x. n i The activation value of the cache (i.e., the value of the n N-2 The activation value generated by the forward calculation is used for gradient calculation. The gradient calculated by the N-1th computing device also includes the gradient for the input data and the gradient for the model parameters. The gradient for the input data calculated by the N-1th computing device is input to the N-2th computing device, ..., the gradient for the input data calculated by the second computing device is input to the first computing device. The first computing device calculates the gradient for the input data and its own gradient for x. n i The cached activation value (i.e., the first computing device for the subsequence block x n i The activation value generated by the forward calculation is used for gradient calculation.
[0093] It should be noted that the present application adopts a pipeline parallel approach to control multiple computing devices to calculate each subsequence block in the subsequence block set corresponding to each training sample subset. At the same time, multiple computing devices can perform calculations on different subsequence blocks, which can greatly improve the training efficiency of the language model.
[0094] Step S202: For each computing device on the computing operation pipeline, after controlling the computing device to complete forward calculation and backward calculation for each subsequence block in the subsequence block sets corresponding to multiple training sample subsets, control the computing device to update the parameters of the model block deployed by the computing device according to the gradient data obtained by reverse calculation for each subsequence block in the subsequence block sets corresponding to multiple training sample subsets.
[0095] When pipeline parallel training is performed using each subsequence block in the subsequence block set corresponding to a plurality of training sample subsets, each computing device may use a preset scheduling strategy to perform model training.
[0096] Optionally, the preset scheduling strategy can be a 1F1B scheduling strategy or a ZB scheduling strategy. It should be noted that the reverse calculation includes the gradient calculation for the input data and the gradient calculation for the model parameters. The 1F1B scheduling strategy treats the gradient for the input and the gradient for the model parameters as a whole and calculates the two gradients at the same time. The ZB scheduling strategy separates the gradient for the input and the gradient for the model parameters and calculates the two gradients at different times to reduce the flow water cavitation rate.
[0097] Among them, the 1F1B scheduling strategy includes non-interleaved 1F1B and interleaved 1F1B scheduling strategies (i.e., 1F1B-I scheduling strategy, which performs forward and backward calculations interleavedly). If the 1F1B scheduling strategy is used for model training, the non-interleaved 1F1B scheduling strategy or the interleaved 1F1B scheduling strategy, i.e., 1F1B-I scheduling strategy, can be used. In view of the better effect of the 1F1B-I scheduling strategy (compared with the non-interleaved 1F1B scheduling strategy, the 1F1B-I scheduling strategy has a smaller flow cavitation rate and higher model training efficiency), the 1F1B-I scheduling strategy can be preferably used for model training.
[0098] Among them, the ZB scheduling strategy includes the ZB-H1 scheduling strategy, the ZB-H2 scheduling strategy, the ZB-V scheduling strategy, etc. If the ZB scheduling strategy is used for model training, the ZB-H1 scheduling strategy, the ZB-H2 scheduling strategy, and the ZB-V scheduling strategy can be used. In view of the better effect of the ZB-V scheduling strategy, the ZB-V scheduling strategy can be preferably used for model training.
[0099] It should be noted that the 1F1B-I scheduling strategy and the ZB-V scheduling strategy require that discontinuous layers be deployed on a computing device. For example, the language model includes 8 layers and there are 4 computing devices. Then, the 1st and 5th layers of the language model are deployed on computing device 1, the 2nd and 6th layers of the language model are deployed on computing device 2, the 3rd and 7th layers of the language model are deployed on computing device 3, and the 4th and 8th layers of the language model are deployed on computing device 4. Since discontinuous layers are deployed on the computing devices, the computing devices only perform calculations on one layer at a time.
[0100] See also Figure 3 and Figure 4 , Figure 3 FIG. 1 shows a schematic diagram of model training using each subsequence block in the subsequence block set corresponding to the training sample subset and adopting the 1F1B-I scheduling strategy. Figure 4 A schematic diagram is shown of model training using each subsequence block in the subsequence block set corresponding to the training sample subset and the ZB-V scheduling strategy.
[0101] Figure 3 and Figure 4 Device 0 to device 3 in the figure represent the four computing devices that make up the computing operation pipeline. Two discontinuous model blocks are deployed on each computing device, that is, the pipeline parallelism is 4 and the virtual pipeline parallelism is 2. Figure 3 and Figure 4 0a and 0b in the numerator represent the serial numbers of the two subsequence blocks in the subsequence block set corresponding to the training sample subset with serial number 0 (each training sample subset is divided into two equal-length parts, that is, each training sample subset includes two subsequence blocks). Figure 3 and Figure 4 1a and 1b in the figure represent the serial numbers of two subsequence blocks in the subsequence block set corresponding to the training sample subset with serial number 1, and the others are similar. Figure 3 and Figure 4 Chunks 0-F and Chunks 1-F in the figure represent the forward operation of the first model block in the computing device for the subsequence block with sequence number 0a and the forward operation of the second model block in the computing device for the subsequence block with sequence number 0a, respectively. Figure 3 Chunk1-BW and Chunk0-BW in the figure respectively represent the backward operation (calculation of two gradients) of the second model block in the computing device for the subsequence block with sequence number 0a, and the backward operation (calculation of two gradients) of the first model block in the computing device for the subsequence block with sequence number 0a. Figure 4Chunk1-B and Chunk1-W represent the backward operation of the second model block in the computing device for the subsequence block with sequence number 0a (Chunk1-B represents the gradient calculation of the second model block for the input data, and Chunk1-W represents the gradient calculation of the second model block for the model parameters). Figure 4 Chunk0-B and Chunk0-W represent the backward operation of the first model block in the computing device for the subsequence block with sequence number 0a (Chunk0-B represents the gradient calculation of the first model block for the input data, and Chunk1-W represents the gradient calculation of the first model block for the model parameters). Figure 3 and Figure 4 The Optimizer step in represents the model optimization step, that is, the model parameter update process.
[0102] like Figure 3 and Figure 4 As shown, when the 1F1B-I scheduling strategy or the ZB-V scheduling strategy is adopted, the computing process of each computing device includes the first computing stage (which can be called the warm up stage, i.e. the preheating stage), the second computing stage (which can be called the steady stage, i.e. the stable stage) and the third computing stage (which can be called the cooldown stage, i.e. the cooling stage). The computing device only performs forward computing in the warm up stage, alternately performs forward computing and reverse computing in the steady stage, and only performs reverse computing in the cooldown stage. Alternating forward computing and reverse computing in the steady stage can reduce the memory usage of the device. Figure 3 As shown, the warm up phase of device 0 is from 0a to 3a, the steady phase is from 3b to 4b before the 7th blank grid, and the cooldown phase is from 4a after the 7th blank grid to 7a before the optimizer step.
[0103] Since the language model is based on the decoder-only tansformer architecture, the computing operation pipeline composed of multiple computing devices will perform causal attention calculation when performing forward calculation on the input subsequence block. When performing causal attention calculation, in addition to paying attention to the current subsequence block, it is also necessary to pay attention to the subsequence block before the subsequence block. The process of causal attention calculation is given below.
[0104] For the training sample subset x n The i-th subsequence block x in the corresponding subsequence block set n i , the process of the kth computing device on the computing operation pipeline performing causal attention calculation for the subsequence block may include:
[0105] Step a1: Determine the subsequence block x according to the input data n i The Q matrix, K matrix and V matrix of .
[0106] If the kth computing device is the first computing device in the computing operation pipeline, then the "input data" in step a1 is the subsequence block x n i If the kth computing device is not the first computing device on the computing operation pipeline, then the "input data" in step a1 is the k-1th computing device for the subsequence block x n i The forward calculation result of .
[0107] Step a2: Subsequence block x n i The K matrix of the forward subsequence block is related to the subsequence block x n i The K matrices are concatenated and the concatenated matrices are used as subsequence blocks x n i The corresponding target K matrix, and the subsequence block x n i The V matrix of the forward subsequence block is related to the subsequence block x n i The V matrix is concatenated and the concatenated matrix is used as the subsequence block x n i The corresponding target V matrix.
[0108] Among them, the subsequence block x n i The forward subsequence block of is the subsequence block x n i The subsequence block set is located in the subsequence block x n i The previous subsequence block, that is, x n 0 、x n 1 , …, x n i-1 .
[0109] It should be noted that if the subsequence block x n i If it is the first subsequence block in the subsequence block set, then directly convert the subsequence block x n i The K matrices are used as subsequence blocks x n i The corresponding target K matrix directly converts the subsequence block x n i The V matrix is a subsequence block xn i The corresponding target V matrix.
[0110] Step a3: for subsequence block x n i The Q matrix, subsequence block x n i The corresponding target K matrix and subsequence block x n i The corresponding target V matrix performs causal attention calculation.
[0111] In the forward calculation, for the subsequence block x n i The causal attention calculation of can be expressed as:
[0112] (1)
[0113] Among them, Q n i Represents a subsequence block x n i The Q matrix, K n 0 Represents a subsequence block x n 0 The K matrix, K n 1 Represents a subsequence block x n 1 K matrices, ..., K n i Represents a subsequence block x n i The K matrix, {K n 0 ,K n 1 ,…,K n i} represents a subsequence block x n i The corresponding target K matrix, V n 0 Represents a subsequence block x n 0 The V matrix, V n 1 Represents a subsequence block x n 1 The V matrix, ..., V n i Represents a subsequence block x n i The V matrix, {V n 0 ,V n 1 ,…,V ni} represents a subsequence block x n i The corresponding target V matrix, O n i Represents the subsequence block x n i The causal attention calculation results of .
[0114] Next, we will use a specific example to illustrate the above process: n The corresponding subsequence block set includes 3 subsequence blocks, namely x n 0 、x n 1 、x n 2 , assuming that the kth computing device currently needs to perform a task for the subsequence block x n 2 To perform causal attention calculation, we first calculate the subsequence block x based on the input data n 2 The Q matrix, K matrix and V matrix are obtained. n 2 , K n 2 and V n 2 , then the subsequence block x n 2 K n 2 , subsequence block x n 1 K n 1 , subsequence block x n 0 K n 0 Splicing, the spliced matrix is used as the subsequence block x n 2 The corresponding target K matrix is similarly converted into subsequence blocks x n 2 V n 2 , subsequence block x n 1 V n 1 , subsequence block x n 0 V n 0 Splicing, the spliced matrix is used as the subsequence block x n 2 The corresponding target V matrix is obtained when the subsequence block x n 2 Qn 2 , subsequence block x n 2 The corresponding target K matrix {K n 0 ,K n 1 ,K n 2} and subsequence block x n 2 The corresponding target V matrix {V n 0 ,V n 1 ,V n 2}, we can then perform the subsequence block x n 2 Q n 2 , subsequence block x n 2 The corresponding target K matrix {K n 0 ,K n 1 ,K n 2} and subsequence block x n 2 The corresponding target V matrix {V n 0 ,V n 1 ,V n 2} performs causal attention calculation. It should be noted that the kth computing device performs causal attention calculation for the subsequence block x n 1 When performing causal attention calculation, the subsequence block x is calculated n 1 K n 1 and V n 1 , the kth computing device performs the calculation for the subsequence block x n 0 When performing causal attention calculation, the subsequence block x is calculated n 0 K n 0 and V n 0 .
[0115] When performing backward calculation, each computing device on the computing operation pipeline will calculate the target gradients corresponding to the Q matrix, the K matrix, and the V matrix. Based on the causal attention calculation process given in the above embodiment, the calculation process of the target gradients corresponding to the Q matrix, the K matrix, and the V matrix is given below.
[0116] For the training sample subset x n The i-th subsequence block x in the corresponding subsequence block set n i , the process of the kth computing device on the computing operation pipeline calculating the target gradients corresponding to the Q matrix, the K matrix and the V matrix of the subsequence block may include:
[0117] Step ba, calculate subsequence block x n i The gradient DQ corresponding to the Q matrix n i , the obtained gradient is taken as the subsequence block x n i The Q matrix corresponds to the target gradient.
[0118] Step b-b1: Calculate subsequence block x n i The gradient corresponding to the corresponding target K matrix.
[0119] Among them, the subsequence block x n i The gradients corresponding to the target K matrix include: subsequence block x n i The gradient DK corresponding to the K matrix n ii and the forward subsequence block of this subsequence block (i.e. x n i-1 ,…x n 1 、x n 0 ) of the K matrix corresponding to the gradient (ie DK n (i -1)i , …, DK n 1i , DK n 0i ).
[0120] Subsequence block x n i The gradient corresponding to the target K matrix can be expressed as:
[0121] (2)
[0122] Step b-b2: Subsequence block xn i The corresponding target K matrix corresponds to the gradient neutron sequence block x n i The gradient of the K matrix corresponding to the subsequence block x n i The target K matrix corresponding to the backward subsequence block corresponds to the gradient subsequence block x n i The gradient corresponding to the K matrix is summed, and the summed gradient is used as the subsequence block x n i The K matrix corresponds to the target gradient.
[0123] Among them, the subsequence block x n i The backward subsequence block of n i The subsequence block set is located in the subsequence block x n i The following subsequence block, that is, x n i+1 、x n i+2 , …, x n C-1 .
[0124] Subsequence block x n i The corresponding K matrix corresponds to the target gradient It can be expressed as:
[0125] (3)
[0126] It should be noted that if the subsequence block x n i If it is the last subsequence block in the subsequence block set, then directly replace the subsequence block x n i The corresponding target K matrix corresponds to the gradient neutron sequence block x n i The K matrices corresponding to the gradients of the subsequence blocks x n i The K matrix corresponds to the target gradient.
[0127] For example, the training sample subset x n It includes 3 subsequence blocks, namely x n 0 、x n 1 、x n 2 , calculate x n 2The gradient corresponding to the target K matrix can be obtained as DK n 02 , DK n 12 , DK n 22 , where DK n 22 As x n 2 The target gradient corresponding to the K matrix is calculated n 1 The gradient corresponding to the target K matrix can be obtained as DK n 01 , DK n 11 , DK n 11 With DK n 12 Sum, and we get x n 1 The target gradient corresponding to the K matrix is calculated n 0 The gradient corresponding to the target K matrix can be obtained as DK n 00 , DK n 00 , DK n 01 , DK n 02 Sum, and we get x n 0 The K matrix corresponds to the target gradient.
[0128] Step b-c1, calculate the subsequence block x n i The gradient corresponding to the corresponding target V matrix.
[0129] Among them, the subsequence block x n i The corresponding gradients of the target V matrix include: subsequence block x n i The gradient DV corresponding to the V matrix n ii and the forward subsequence block of this subsequence block (i.e. x n i-1 ,…x n 1 、x n 0 ) of the V matrix corresponding to the gradient (ie DV n (i -1)i 、…、DV n 1i 、DVn 0i ).
[0130] Step b-c2: Subsequence block x n i The corresponding target V matrix corresponds to the gradient neutron sequence block x n i The gradient of the V matrix corresponding to the subsequence block x n i The target V matrix corresponding to the backward subsequence block corresponds to the gradient subsequence block x n i The gradient corresponding to the V matrix is summed up, and the summed gradient is used as the subsequence block x n i The V matrix corresponds to the target gradient.
[0131] Subsequence block x n i The target gradient corresponding to the corresponding V matrix can be expressed as:
[0132] (4)
[0133] It should be noted that if the subsequence block x n i If it is the last subsequence block in the subsequence block set, then directly replace the subsequence block x n i The corresponding target V matrix corresponds to the gradient neutron sequence block x n i The V matrix corresponding to the gradient of the subsequence block x n i The V matrix corresponds to the target gradient.
[0134] For example, the training sample subset x n It includes 3 subsequence blocks, namely x n 0 、x n 1 、x n 2 , calculate x n 2 The gradient corresponding to the target V matrix can be obtained as DV n 02 、DV n 12 、DV n 22 , where DV n 22 As x n 2 The target gradient corresponding to the V matrix is calculated n1 The gradient corresponding to the target V matrix can be obtained as DV n 01 、DV n 11 , DV n 11 With DV n 12 Sum, and we get x n 1 The target gradient corresponding to the V matrix is calculated n 0 The gradient corresponding to the target V matrix can be obtained as DV n 00 , DV n 00 、DV n 01 、DV n 02 Sum, and we get x n 0 The V matrix corresponds to the target gradient.
[0135] Next, taking the language model GPT3 as an example, the activation value memory usage and the flow vacancy rate of the language model training method provided in this application are explained.
[0136] The dimension of the hidden layer of GPT3 is represented as h, the number of transformer decoder layers contained in GPT3 is represented as L, the pipeline parallelism is represented as P (P=N), and the virtual pipeline parallelism is represented as , the number of transformer decoder layers assigned to each computing device is l (l=L / P), and the number of training sample subsets is m, then:
[0137] Using the existing language model training method (directly inputting the training sample subset into the computing operation pipeline composed of N computing devices for calculation), the peak value of the memory occupied by the activation values that need to be cached by each computing device (hereinafter referred to as the peak value of the memory occupied by the activation values that need to be cached as "peak value of activation memory usage") for:
[0138] (5)
[0139] in, is the number of times the first computing device performs forward calculations in the warm up phase, The size of the memory occupied by the activation value of a single layer transformer decoder.
[0140] Using the language model training method provided by the present application (dividing the training sample subset into C parts of equal length in the sequence dimension to obtain a subsequence block set corresponding to the training sample subset, and sequentially inputting each sequence block in the subsequence block set corresponding to the training sample subset into a computing operation pipeline composed of N computing devices for calculation), since the training sample subset is divided into C parts of equal length in the sequence dimension, compared with not dividing the training sample subset, the number of times the first computing device performs forward calculation in the warmup phase increases by C-1, and when performing forward calculation, the activation value of the single-layer transformer decoder occupies M memory B / C, in view of this, the peak memory usage of the activation value of each computing device for:
[0141] (6)
[0142] Compared with the existing language model training method, the language model training method provided in this application saves:
[0143] (7)
[0144] Since C is greater than 1, the memory saved by the language model training method provided in this application is greater than 0.
[0145] Table 1 Comparison of the effects of the language model training method provided by this application and the existing language model training method
[0146]
[0147] The second and third rows of the above table show the number of times the computing device performs forward calculations in the warm up phase, the peak memory occupancy rate of the activation value of the computing device, and the flow vacancy rate when the existing language model training method adopts the 1F1B-I scheduling strategy and the ZB-V scheduling strategy. The fourth and fifth rows of the above table show the number of times the computing device performs forward calculations in the warm up phase, the peak memory occupancy rate of the activation value of the computing device, and the flow vacancy rate when the language model training method provided by the present application adopts the 1F1B-I scheduling strategy and the ZB-V scheduling strategy.
[0148] Taking the GPT3-175B model as an example (h=12288, L=96), when P=12, T=8, P′=8, C=4, S=6144, the 1F1B-I scheduling strategy is used for training. Compared with the language model training method of the prior art, the theoretical activation value memory usage of each computing device will be reduced by 24.65GB when the language model training method provided in this application is used for training. When the ZB-V scheduling strategy is used, compared with the language model training method of the prior art, the theoretical activation value memory usage of each computing device will be reduced by 20.61GB when the language model training method provided in this application is used for training. It should be noted that when performing model training, each computing device uses pipeline parallelism to perform model training, and each computing device can use tensor parallelism to perform calculations. The above T is the tensor parallelism.
[0149] Compared with the existing language model training method, when the language model training method provided by the present application is used for training, the overall flow cavitation rate remains unchanged, but the absolute time of each cavitation becomes 1 / C of the original, so the overall cavitation rate is lower. Since each training sample subset is divided into C parts, the accumulation of parameter gradients is also increased by C-1 times (each computing device will accumulate the parameter gradients calculated for each subsequence block, and then update the parameters of the model block deployed by itself according to the gradient accumulation result), which is an increase of (C-1)×(12h 2 +13h) of multiplication and addition, but much less than the additional 24BSh of the full recalculation method. 2 +12BSh. Therefore, compared with the existing language model training method, the language model training method provided by the present application can effectively reduce memory usage and pipeline vacancy rate while only increasing a relatively low amount of additional calculations, and can maintain efficient MFU (Model FLOPs Utilization).
[0150] The present application also provides a language model training device. Figure 5 As shown, the language model training device may include: a model segmentation and deployment module 501, a training sample set acquisition module 502, a training sample set division module 503, a training sample subset segmentation module 504 and a model training module 505.
[0151] The model segmentation and deployment module 501 is used to segment the language model into multiple model blocks by layer, and deploy the multiple model blocks on multiple computing devices.
[0152] Each model block obtained by segmentation includes one layer or multiple consecutive layers in the language model, and one or more model blocks among the multiple model blocks are deployed on each computing device.
[0153] The training sample set acquisition module 502 is used to acquire a training sample set.
[0154] The training sample set includes A training sequences of length S obtained from the training sequence set, and both A and S are integers greater than 1.
[0155] The training sample set division module 503 is used to divide the training sample set into multiple training sample subsets.
[0156] Each training sample subset includes B training sequences of length S, where B is an integer less than A.
[0157] The training sample subset segmentation module 504 is used to segment the multiple training sample subsets in the sequence dimension to obtain subsequence block sets corresponding to the multiple training sample subsets.
[0158] Each subsequence block in the subsequence block set includes B subsequences.
[0159] The model training module 505 is used to control multiple computing devices to perform model training by using each subsequence block in the subsequence block set corresponding to multiple training sample subsets, adopting a pipeline parallel training method.
[0160] In a possible implementation, when the model segmentation and deployment module 501 segments the language model into multiple model blocks by layer and deploys the multiple model blocks on multiple computing devices, it is specifically used to:
[0161] The language model is divided into N model blocks by layer, and the N model blocks are deployed on N computing devices, wherein one model block of the N model blocks is deployed on each computing device, and N is an integer greater than 1.
[0162] In another possible implementation, when the model segmentation and deployment module 501 segments the language model into multiple model blocks by layer and deploys the multiple model blocks on multiple computing devices, it is specifically used to:
[0163] The language model is divided into M model blocks by layer, and the M model blocks are deployed on N computing devices, wherein one model block or multiple discontinuous model blocks among the M model blocks are deployed on each computing device, and M is an integer greater than N.
[0164] In a possible implementation, when the training sample subset segmentation module 504 segments the multiple training sample subsets in the sequence dimension to obtain the subsequence block sets corresponding to the multiple training sample subsets, the module is specifically configured to:
[0165] For each training sample subset, the training sample subset is divided into C parts of equal length in the sequence dimension, and the C subsequence blocks obtained by the division form a subsequence block set corresponding to the training sample subset, where C is an integer greater than 1.
[0166] In a possible implementation, the model training module 505 uses each subsequence block in the subsequence block set corresponding to each of the multiple training sample subsets, adopts a pipeline parallel training method, and controls multiple computing devices to perform model training, specifically for:
[0167] Inputting each subsequence block in the subsequence block set corresponding to each training sample subset into the computing operation pipeline composed of the plurality of computing devices one by one in sequence for computing, wherein the computing operation pipeline performs forward computing and backward computing for each input subsequence block;
[0168] For each computing device on the computing operation pipeline, after controlling the computing device to complete forward calculation and backward calculation for each subsequence block in the subsequence block sets corresponding to multiple training sample subsets, the computing device is controlled to update the parameters of the model block deployed by the computing device according to the gradient data obtained by the computing device through reverse calculation for each subsequence block in the subsequence block sets corresponding to the multiple training sample subsets.
[0169] In one possible implementation, the computing process of each computing device on the computing operation pipeline includes a first computing stage, a second computing stage, and a third computing stage; each computing device on the computing operation pipeline only performs forward computing in the first computing stage, alternately performs forward computing and reverse computing in the second computing stage, and only performs reverse computing in the third computing stage.
[0170] The forward computation performed by each computing device in the computational operation pipeline includes causal attention computation;
[0171] In a possible implementation, for a subsequence block in a subsequence block set, a process in which a computing device in a computing operation pipeline performs causal attention calculation on the subsequence block includes:
[0172] Determine the Q matrix, K matrix and V matrix of the subsequence block according to the input data;
[0173] The K matrix of the forward subsequence block of the subsequence block is concatenated with the K matrix of the subsequence block, and the concatenated matrix is used as the target K matrix corresponding to the subsequence block; and the V matrix of the forward subsequence block of the subsequence block is concatenated with the V matrix of the subsequence block, and the concatenated matrix is used as the target V matrix corresponding to the subsequence block, wherein the forward subsequence block of the subsequence block is a subsequence block located before the subsequence block in the subsequence block set where the subsequence block is located;
[0174] Perform causal attention calculation on the Q matrix of the subsequence block, the target K matrix corresponding to the subsequence block, and the target V matrix corresponding to the subsequence block.
[0175] The backward calculation performed by each computing device on the computing operation pipeline includes the calculation of the target gradients corresponding to the Q matrix, the K matrix and the V matrix respectively;
[0176] In a possible implementation, a process of determining target gradients corresponding to a Q matrix, a K matrix, and a V matrix of a subsequence block by a computing device on a computing operation pipeline includes:
[0177] Calculate the gradient corresponding to the Q matrix of the subsequence block, and use the obtained gradient as the target gradient corresponding to the Q matrix of the subsequence block;
[0178] Calculate the gradient corresponding to the target K matrix corresponding to the subsequence block, wherein the gradient corresponding to the target K matrix corresponding to the subsequence block includes the gradient corresponding to the K matrix of the subsequence block and the gradient corresponding to the K matrix of the forward subsequence block of the subsequence block; sum the gradient corresponding to the K matrix of the subsequence block in the gradient corresponding to the target K matrix corresponding to the subsequence block and the gradient corresponding to the K matrix of the subsequence block in the gradient corresponding to the target K matrix corresponding to the backward subsequence block of the subsequence block, and use the summed gradient as the target gradient corresponding to the K matrix of the subsequence block; wherein the backward subsequence block of the subsequence block is a subsequence block located after the subsequence block in the subsequence block set where the subsequence block is located;
[0179] Calculate the gradient corresponding to the target V matrix corresponding to the subsequence block, wherein the gradient corresponding to the target V matrix corresponding to the subsequence block includes the gradient corresponding to the V matrix of the subsequence block and the gradient corresponding to the V matrix of the forward subsequence block of the subsequence block; sum the gradient corresponding to the V matrix of the subsequence block in the gradient corresponding to the target V matrix corresponding to the subsequence block and the gradient corresponding to the V matrix of the backward subsequence block of the subsequence block, and use the summed gradient as the target gradient corresponding to the V matrix of the subsequence block.
[0180] After obtaining multiple training sample subsets, the language model training device provided in the embodiment of the present application does not directly use the training sample subsets for model training, but further divides the training sample subsets in the sequence dimension, so that a training sample subset is divided into multiple subsequence blocks, and then the model is trained using each subsequence block obtained by the division. In the forward calculation stage, since the computing device performs forward calculation on the subsequence block instead of forward calculation on the training sample subset, the data amount of the activation value generated by the forward calculation on the subsequence block is greatly reduced compared to the data amount of the activation value generated by the forward calculation on the training sample subset, thereby greatly reducing the demand for memory. In addition, in addition to effectively reducing the memory demand, the use of the subsequence blocks obtained by dividing the training sample subset for model training can also effectively reduce the flow water cavitation rate.
[0181] The present application also provides an electronic device. Figure 6 , shows a schematic diagram of the structure of the electronic device, which may include: at least one processor 601, at least one communication interface 602, at least one memory 603 and at least one communication bus 604.
[0182] In the embodiment of the present application, the number of the processor 601, the communication interface 602, the memory 603, and the communication bus 604 is at least one, and the processor 601, the communication interface 602, and the memory 603 communicate with each other through the communication bus 604;
[0183] The processor 601 may be a central processing unit (CPU), or an application-specific integrated circuit (ASIC), or one or more integrated circuits configured to implement the embodiments of the present application, etc.;
[0184] The memory 603 may include a high-speed RAM memory, and may also include a non-volatile memory, such as at least one disk memory;
[0185] The memory stores a program, and the processor can call the program stored in the memory, and the program is used to implement the language model training method provided in the above embodiment.
[0186] An embodiment of the present application also provides a computer-readable storage medium, which may store a computer program / instruction suitable for execution by a processor. When the computer program / instruction is executed by the processor, the language model training method provided in the above embodiment is implemented.
[0187] An embodiment of the present application also provides a computer program product, including computer-readable instructions. When the computer-readable instructions are executed on an electronic device, the electronic device implements the language model training method provided in the above embodiment.
[0188] Finally, it should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the presence of other identical elements in the process, method, article or device including the elements.
[0189] The various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the various embodiments can be referenced to each other.
[0190] The above description of the disclosed embodiments enables those skilled in the art to implement or use the present application. Various modifications to these embodiments will be apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present application. Therefore, the present application will not be limited to the embodiments shown herein, but will conform to the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. A language model training method, characterized in that: include: Divide the language model into multiple model blocks by layer, and deploy the multiple model blocks on multiple computing devices, wherein each model block obtained by the division includes one layer or multiple consecutive layers in the language model, and one or more model blocks of the multiple model blocks are deployed on each computing device; Obtain a training sample set, wherein the training sample set includes A The length is S The training sequence, A and S are all integers greater than 1; The training sample set is divided into multiple training sample subsets, wherein each training sample subset includes B The length is S The training sequence, B is less than A integer; The plurality of training sample subsets are respectively divided in the sequence dimension to obtain subsequence block sets corresponding to the plurality of training sample subsets, wherein each subsequence block in the subsequence block set includes B subsequences; Using each subsequence block in the subsequence block set corresponding to the plurality of training sample subsets, respectively, and adopting a pipeline parallel training method, the plurality of computing devices are controlled to perform model training; The method of dividing the language model into a plurality of model blocks by layer and deploying the plurality of model blocks on a plurality of computing devices includes: The language model is divided into layers M model block, M Model blocks are deployed to N computing devices, wherein the M a model block or multiple discrete model blocks, M is greater than N An integer.
2. The language model training method according to claim 1, characterized in that: The step of dividing the plurality of training sample subsets in the sequence dimension to obtain subsequence block sets corresponding to the plurality of training sample subsets respectively includes: For each training sample subset, the training sample subset is divided into equal lengths in the sequence dimension C , divide the obtained C subsequence blocks constitute the subsequence block set corresponding to the training sample subset, where: C is an integer greater than 1.
3. The language model training method according to claim 1, characterized in that: The method of using each subsequence block in the subsequence block set corresponding to each of the plurality of training sample subsets, adopting a pipeline parallel training method, and controlling the plurality of computing devices to perform model training includes: Inputting each subsequence block in the subsequence block set corresponding to each training sample subset into a computing operation pipeline composed of the plurality of computing devices one by one in sequence for computing, wherein the computing operation pipeline performs forward computing and backward computing for each input subsequence block; For each computing device on the computing operation pipeline, after controlling the computing device to complete forward calculation and backward calculation for each subsequence block in the subsequence block sets corresponding to the multiple training sample subsets, the computing device is controlled to update the parameters of the model block deployed by the computing device according to the gradient data obtained by the computing device through reverse calculation for each subsequence block in the subsequence block sets corresponding to the multiple training sample subsets.
4. The language model training method according to claim 3, characterized in that: The calculation process of each computing device on the computing operation pipeline includes a first calculation stage, a second calculation stage and a third calculation stage; Each computing device on the computing operation pipeline performs only forward computing in the first computing stage, performs forward computing and reverse computing alternately in the second computing stage, and performs only reverse computing in the third computing stage.
5. The language model training method according to claim 3, characterized in that: The forward computation performed by each computing device in the computing operation pipeline includes causal attention computation; For a subsequence block in a subsequence block set, a process in which a computing device on the computing operation pipeline performs causal attention calculation on the subsequence block includes: Determine the subsequence block based on the input data Q matrix, K Matrix and V matrix; The forward subsequence block of this subsequence block K The matrix and the subsequence block K Matrix concatenation, the concatenated matrix is used as the target corresponding to the subsequence block K matrix, and the forward subsequence block of the subsequence block V The matrix and the subsequence block V Matrix concatenation, the concatenated matrix is used as the target corresponding to the subsequence block V Matrix, wherein the forward subsequence block of the subsequence block is a subsequence block located before the subsequence block in the subsequence block set where the subsequence block is located; For this subsequence block Q Matrix, the target corresponding to the subsequence block K The matrix and the target corresponding to this subsequence block V Matrix for causal attention computation.
6. The language model training method according to claim 5, characterized in that: The backward calculation performed by each computing device in the computing operation pipeline includes Q matrix, K Matrix and V Calculation of target gradients corresponding to the matrices; A computing device in the computing operation pipeline determines a subsequence block Q matrix, K Matrix and V The process of the target gradient corresponding to the matrix includes: Calculate the subsequence block Q The gradient corresponding to the matrix is used as the gradient of the subsequence block. Q The target gradient corresponding to the matrix; Calculate the target corresponding to the subsequence block K The gradient corresponding to the matrix, where the target corresponding to the subsequence block K The gradient corresponding to the matrix includes the gradient of the subsequence block K The gradient corresponding to the matrix and the forward subsequence block of the subsequence block K The gradient corresponding to the matrix; the target corresponding to the subsequence block K The gradient of the subsequence block in the matrix K The gradient corresponding to the matrix is the target corresponding to the backward subsequence block of this subsequence block K The gradient of the subsequence block in the matrix K The gradient corresponding to the matrix is summed, and the summed gradient is used as the subsequence block K The target gradient corresponding to the matrix; wherein the backward subsequence block of the subsequence block is a subsequence block located after the subsequence block in the subsequence block set where the subsequence block is located; Calculate the target corresponding to the subsequence block V The gradient corresponding to the matrix, where the target corresponding to the subsequence block V The gradient corresponding to the matrix includes the gradient of the subsequence block V The gradient corresponding to the matrix and the forward subsequence block of the subsequence block V The gradient corresponding to the matrix; the target corresponding to the subsequence block V The gradient of the subsequence block in the matrix V The gradient corresponding to the matrix is the target corresponding to the backward subsequence block of this subsequence block V The gradient of the subsequence block in the matrix V The gradient corresponding to the matrix is summed, and the summed gradient is used as the subsequence block V The target gradient corresponding to the matrix.
7. A language model training device, characterized in that: include: Model segmentation and deployment module, training sample set acquisition module, training sample set partitioning module, training sample subset segmentation module and model training module; The model segmentation and deployment module is used to segment the language model into multiple model blocks by layer, and deploy the multiple model blocks on multiple computing devices, wherein each model block obtained by segmentation includes one layer or multiple consecutive layers in the language model, and one or more model blocks of the multiple model blocks are deployed on each computing device; The training sample set acquisition module is used to acquire a training sample set, wherein the training sample set includes A The length is S The training sequence, A and S are all integers greater than 1; The training sample set division module is used to divide the training sample set to obtain multiple training sample subsets, wherein each training sample subset includes B The length is S The training sequence, B is less than A integer; The training sample subset segmentation module is used to segment the multiple training sample subsets in the sequence dimension to obtain subsequence block sets corresponding to the multiple training sample subsets, wherein each subsequence block in the subsequence block set includes B subsequences; The model training module is used to control the multiple computing devices to perform model training by using each subsequence block in the subsequence block set corresponding to the multiple training sample subsets, in a pipeline parallel training manner; The model segmentation and deployment module is specifically used for: The language model is divided into layers M model block, M Model blocks are deployed to N computing devices, wherein the M a model block or multiple discrete model blocks, M is greater than N An integer.
8. An electronic device, characterized in that: The method comprises at least one processor and a memory connected to the processor, wherein: The memory is used to store computer programs; The processor is used to execute the computer program so that the electronic device can implement the language model training method as described in any one of claims 1 to 6.
9. A computer storage medium, characterized in that The storage medium carries one or more computer programs, which, when executed by an electronic device, enable the electronic device to implement the language model training method as described in any one of claims 1 to 6.
10. A computer program product, characterized in that It includes computer-readable instructions, and when the computer-readable instructions are executed on an electronic device, the electronic device implements the language model training method as described in any one of claims 1 to 6.
