Model training acceleration method and device
By freezing and attenuating network layers whose gradient change rate is lower than the freezing threshold during deep learning model training, combined with dynamic batch size adjustment, the problem of extended model training time is solved and the training speed and efficiency are significantly improved.
Patent Information
- Application Number
- CN202311782784.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-12-22
- Publication Date
- 2024-10-18
AI Technical Summary
As the scale of deep learning models increases, the computing power of graphics processors cannot meet the needs of model training, resulting in extended training time and increased computing resources.
During the model training process, the network layer whose gradient change rate is lower than the freezing threshold is frozen, the back propagation is stopped, and the freezing threshold is attenuated according to the attenuation coefficient, and the network layer is gradually frozen until the model training reaches the preset accuracy or number of times, dynamically Adjust batch size to improve memory utilization.
It effectively reduces the memory usage, improves the computing efficiency and training speed, avoids freezing the untrained convergence layer in advance, and significantly speeds up the model training process.
Smart Images

Figure CN118798283A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of artificial intelligence, and in particular to a model training acceleration method and device. Background Art
[0002] With the development of deep learning, models based on deep learning have demonstrated their superior performance in a large number of tasks. Researchers have found that as the depth and width of the network increase, the degree of nonlinearity also increases, and the corresponding ability to express real-world problems becomes stronger. Therefore, more and more institutions have proposed the concept of large-scale, pre-trained language models such as BERT and GPT, whose parameters have expanded from the initial 300 million to 100 billion.
[0003] However, as the model size increases, the computing power of the graphics processing unit (GPU) does not increase proportionally, which means that the longer the model training time is, the more computing resources are required. Summary of the invention
[0004] The embodiments of the present application provide a model training acceleration method and device to solve the problem in the related art that the computing power cannot meet the growth of the model scale, resulting in long training time and increased computing resources.
[0005] In a first aspect, a model training acceleration method is provided, comprising:
[0006] During the model training process, the network layer whose gradient change rate is lower than the freezing threshold is frozen, and the freezing is used to stop the back propagation of the network layer;
[0007] Attenuating the freezing threshold according to the attenuation coefficient to obtain an attenuated freezing threshold;
[0008] The model training is performed using the unfrozen network layers, and the network layers whose gradient change rate is lower than the attenuation freezing threshold are frozen until the model training reaches a preset accuracy or a preset number of times.
[0009] In a second aspect, a model training acceleration device is provided, comprising:
[0010] A first freezing module is used to freeze the network layer whose gradient change rate is lower than the freezing threshold during the model training process, and the freezing is used to stop the back propagation of the network layer;
[0011] An attenuation module, used for performing attenuation processing on the freezing threshold according to an attenuation coefficient to obtain an attenuated freezing threshold;
[0012] The second freezing module is used to use the unfrozen network layers to perform model training, and freeze the network layers whose gradient change rate is lower than the attenuation freezing threshold until the model training reaches a preset accuracy or a preset number of times.
[0013] In a third aspect, a terminal device is provided, comprising: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the computer program implements the steps of the method described in the first aspect when executed by the processor.
[0014] In a fourth aspect, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the steps of the method described in the first aspect are implemented.
[0015] In an embodiment of the present application, first, during the model training process, the network layer whose gradient change rate is lower than the freezing threshold is frozen so that the frozen network layer stops back propagation, and then the freezing threshold is attenuated according to the attenuation coefficient to obtain the attenuated freezing threshold, and finally the unfrozen network layer is used for model training, and the network layer whose gradient change rate is lower than the attenuated freezing threshold is frozen until the model training reaches a preset accuracy or a preset number of times. In the embodiment of the present application, part of the network layer is gradually frozen during the model training process to stop back propagation, which effectively reduces the memory usage, improves the computing efficiency, and improves the training speed. At the same time, the freezing threshold is attenuated to avoid freezing the network layer that has not been trained and converged in advance. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:
[0017] Figure 1 is a flow chart of the model training acceleration method provided in an embodiment of the present application;
[0018] Figure 2 It is a detailed flowchart of the model training acceleration method provided in the embodiment of the present application;
[0019] Figure 3 It is a structural diagram of the freezing training process provided in an embodiment of the present application;
[0020] Figure 4 It is a schematic diagram of a model training acceleration device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0021] 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 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.
[0022] The terms "first", "second", etc. in the specification and claims of the present application are used to distinguish similar objects, and are not used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable under appropriate circumstances, so that the embodiments of the present application can be implemented in an order other than those illustrated or described here. In addition, the "and / or" in the specification and claims represents at least one of the connected objects, and the character " / " generally represents that the objects associated with each other are in an "or" relationship.
[0023] In conjunction with the accompanying drawings, a model training acceleration method and device provided by the embodiment of the present application is described in detail through specific embodiments and their application scenarios. The present application is based on the fact that the neural network gradually converges from the input end to the output end. Therefore, it is possible to consider freezing and pausing the back propagation of the model layer (i.e., the network layer) that has converged near the input end to speed up the model training speed; at the same time, in order to improve the utilization rate of the GPU video memory, the free video memory after freezing is used to dynamically increase the batch size (batch_size) to further improve the training speed.
[0024] like Figure 1 As shown, it is a flow chart of a model training acceleration method provided in an embodiment of the present application. Figure 1 As shown, the model training acceleration method may include: the contents shown in S101 to S103.
[0025] In S101, during the model training process, the network layer whose gradient change rate is lower than the freezing threshold is frozen, and the freezing is used to stop the back propagation of the network layer.
[0026] In this embodiment, the relative rate of change of gradient is used as an example for explanation. In the actual freezing algorithm, other methods such as feature map similarity may be used as freezing criteria. This embodiment does not make specific limitations, and the actual application shall prevail.
[0027] In S102, the freezing threshold is attenuated according to the attenuation coefficient to obtain an attenuated freezing threshold.
[0028] It should be noted that during the training process, the gradient change rate will gradually decrease. Therefore, if a fixed freezing threshold is used, the model layers that have not been trained and converged will be frozen in advance. Therefore, the freezing threshold also needs to be attenuated to a certain extent.
[0029] In S103, the model training is performed using the unfrozen network layers, and the network layers whose gradient change rate is lower than the attenuation freezing threshold are frozen until the model training reaches a preset accuracy or a preset number of times.
[0030] In an embodiment of the present application, first, during the model training process, the network layer whose gradient change rate is lower than the freezing threshold is frozen so that the frozen network layer stops back propagation, and then the freezing threshold is attenuated according to the attenuation coefficient to obtain the attenuated freezing threshold, and finally the unfrozen network layer is used for model training, and the network layer whose gradient change rate is lower than the attenuated freezing threshold is frozen until the model training reaches a preset accuracy or a preset number of times. In the embodiment of the present application, part of the network layer is gradually frozen during the model training process to stop back propagation, which effectively reduces the memory usage, improves the computing efficiency, and improves the training speed. At the same time, the freezing threshold is attenuated to avoid freezing the network layer that has not been trained and converged in advance.
[0031] In a possible implementation of the present application, during the model training process, freezing the network layer whose gradient change rate is lower than the freezing threshold may include: during the model training process, determining the gradient change rate of each current network layer; freezing the network layer whose gradient change rate is lower than the freezing threshold so that the network layer stops back propagation.
[0032] That is, the parameter gradients of each layer of the current network are recorded and compared with the gradients of each layer of the last iteration. If the gradients of each layer of the last iteration are not recorded, the gradients are skipped. Determining the gradient change rate of each current network layer may include: determining the gradient change rate according to the following formula.
[0033]
[0034] Among them, ratio is the gradient change rate; G current is the layer gradient of the current iteration; G prev is the layer gradient of the previous iteration.
[0035] In this embodiment, after calculating the relative change rate of the gradient of each layer, the relative change rate of the gradient of each layer is sorted from high to low, and the layers whose relative change rate of the gradient is lower than the threshold are frozen according to the set freezing threshold and are not back-propagated. Since the gradient change rate will gradually decrease during the training process, if a fixed freezing threshold is used, the model layer that has not been trained and converged will be frozen in advance, so the freezing threshold needs to be attenuated to a certain extent, and the attenuation formula is as follows.
[0036] ε′=α×ε
[0037] Among them, ε′ is the attenuation freezing threshold; α is the attenuation coefficient; ε is the freezing threshold.
[0038] After the model is partially frozen, the frozen layers do not need to be back-propagated to save the memory usage of the back-propagation parameters, in order to improve the utilization of the memory after freezing. In a possible implementation of the present application, after freezing the network layer whose gradient change rate is lower than the freezing threshold, the model training acceleration method may also include: adjusting the batch size of the data used for each training; and training the model using the adjusted batch size.
[0039] In a possible implementation of the present application, adjusting the batch size of data used for each training may include: adjusting the batch size of data used for each training according to the video memory occupied by the back-propagation parameters; when the batch size increases to the point where the video memory cannot run, obtaining the batch size corresponding to the last normal operation of the video memory as the initial batch size; and adjusting the initial batch size using a binary search method to obtain a target batch size.
[0040] In this embodiment, batch_size is dynamically increased for experimental training. After each adjustment of batch_size, a corresponding batch is used for experimental training. If the training can proceed normally, it means that there is still space in the video memory. Then, the video memory is increased until it exceeds the video memory and cannot run. Then, the last batch_size that can run normally is used as the initial batch_size. In this part, the batch_size is roughly adjusted by exponential growth. The pseudo code is shown in Table 1.
[0041] Table 1
[0042]
[0043]
[0044] Since the rough batch_size adjustment of exponential growth may not fully utilize the video memory, this embodiment uses the binary search method to finely adjust the batch_size to find the optimal batch_size until all video memory is fully utilized. The pseudo code is shown in Table 2.
[0045] Table 2
[0046]
[0047] After the above adjustment, the optimal batch_size of the current frozen model is obtained. If the frozen model does not change during subsequent training, the current optimal batch_size remains unchanged. After completing the current optimal batch_size, the normal training process continues with the batch_size. When the model is trained to the specified accuracy or reaches the system preset training times (epoch), the training process is terminated and the corresponding internal event (checkpoint) is saved.
[0048] like Figure 2 The figure shows a detailed flow chart of the model training acceleration method. The details are as follows:
[0049] After the model is obtained, the data is input normally to train each batch until the training reaches the freezing evaluation interval, then the training is suspended for evaluation to determine whether the network layer is frozen. The evaluation process specifically includes the following steps (this proposal only takes the relative change rate of gradient as an example. In the actual freezing algorithm, other methods such as feature map similarity can be used as freezing criteria. The specific process follows the following steps): Record the parameter gradients of each layer of the current network and compare them with the gradients of each layer in the last iteration. If there is no gradient record of each layer in the last iteration, skip it. Calculate the relative change rate of the gradient of each layer in this iteration. After calculating the relative change rate of the gradient of each layer, sort the relative change rate of the gradient of each layer from high to low, and freeze the layers with the relative change rate of the gradient below the threshold according to the set freezing threshold without backpropagation. Since the gradient change rate will gradually decrease during the training process, if a fixed freezing threshold is used, the model layer that has not been trained and converged will be frozen in advance, so the freezing threshold also needs to be attenuated to a certain extent.
[0050] After the model is partially frozen, the frozen layers do not need to be back-propagated to save the memory usage of back-propagation parameters. In order to improve the utilization rate of the video memory after freezing, an adaptive dynamic batch_size adjustment method is adopted in this proposal. The specific process of the sub-steps included in this step is as follows: Use batch_size to dynamically increase for experimental training. After each batch_size adjustment, use the corresponding batch to perform an experimental training. If the training can proceed normally, it means that there is still free memory. Then continue to increase the video memory until it exceeds the video memory and cannot run. Then use the last batch_size that can run normally as the initial batch_size. In this part, use the exponential growth method to roughly adjust the batch_size first; because the rough batch_size adjustment of exponential growth may not fully utilize the video memory, then use the binary search method in this method to fine-tune the batch_size to find the optimal batch_size until all video memory is utilized; after the above adjustment, the optimal batch_size of the current frozen model is obtained. If the frozen model does not change in subsequent training, the current optimal batch_size remains unchanged.
[0051] After completing the current optimal batch_size, continue the normal training process with the batch_size until the training reaches the frozen evaluation interval and jump to the above step for frozen evaluation to continue evaluating whether the model needs to be frozen.
[0052] When the model is trained to the specified accuracy or reaches the system preset epoch, the training process is terminated and the corresponding checkpoint is saved.
[0053] In a specific embodiment of the present application, Figure 3 As shown in the figure, the model is fully adjustable from time T0. After a period of training, as shown in the figure, at time T1, the first two layers have been trained, so this embodiment freezes the first two layers (with Figure 3 The gray block indicates that at time T2, when another layer converges, it is frozen and no back propagation is performed to calculate the gradient. When the training freezes to the last few layers, the training stops and the training continues until the accuracy requirements are met.
[0054] This embodiment, based on the characteristic that the model layers gradually converge from the input end to the output end during training, dynamically freezes some model layers according to certain criteria in the entire training process to stop back propagation and improve the training speed. At the same time, in order to utilize the frozen video memory, a dynamic batch_size adjustment method is used to improve the video memory utilization in real time. Increasing the batch_size can also accelerate the model training process.
[0055] The embodiment of the present application can accelerate the average epoch time in model training in a single GPU system by progressively freezing the model layers during training, stopping the back propagation of the frozen model layers to reduce the amount of calculation, and can further accelerate the model training process by dynamically adjusting batch_size. This embodiment can accelerate single GPU training and can also be further accelerated by cooperating with multi-GPU data parallelism. Compared with fine-tuning training, this embodiment is for the process of training from scratch, and can dynamically adjust the frozen layers instead of specifying the frozen layers at the beginning of fine-tuning training, so it has better flexibility.
[0056] like Figure 4 As shown, it is a schematic diagram of a model training acceleration device provided in an embodiment of the present application. Figure 4 As shown, the model training acceleration device may include: a first freezing module 401, a decay module 402 and a second freezing module 403.
[0057] Among them, the first freezing module 401 is used to freeze the network layer whose gradient change rate is lower than the freezing threshold during the model training process, and the freezing is used to stop the network layer from backpropagating; the attenuation module 402 is used to attenuate the freezing threshold according to the attenuation coefficient to obtain the attenuated freezing threshold; the second freezing module 403 is used to use the unfrozen network layer for model training, and freeze the network layer whose gradient change rate is lower than the attenuated freezing threshold until the model training reaches a preset accuracy or a preset number of times.
[0058] In the embodiment of the present application, first, the first freezing module 401 freezes the network layer whose gradient change rate is lower than the freezing threshold during the model training process, so that the frozen network layer stops back propagation, and then the attenuation module 402 attenuates the freezing threshold according to the attenuation coefficient to obtain the attenuated freezing threshold, and finally the second freezing module 403 uses the unfrozen network layer to perform model training, and freezes the network layer whose gradient change rate is lower than the attenuated freezing threshold until the model training reaches a preset accuracy or a preset number of times. The embodiment of the present application gradually freezes some network layers during the model training process to stop back propagation, effectively reducing the memory usage, improving the computing efficiency, and improving the training speed. At the same time, the freezing threshold is attenuated to avoid freezing the network layer that has not been trained and converged in advance.
[0059] In a possible implementation of the present application, the first freezing module 401 is used to: determine the gradient change rate of each current network layer during the model training process; and freeze the network layer whose gradient change rate is lower than the freezing threshold so that the network layer stops back propagation.
[0060] In a possible implementation manner of the present application, the first freezing module 401 is used to determine the gradient change rate according to the following formula.
[0061]
[0062] Among them, ratio is the gradient change rate; G current is the layer gradient of the current iteration; G prev is the layer gradient of the previous iteration.
[0063] In a possible implementation of the present application, the model training acceleration device also includes an adjustment module and a training module.
[0064] Among them, the adjustment module is used to adjust the batch size of the data used in each training; the training module is used to train the model using the adjusted batch size.
[0065] In a possible implementation of the present application, the adjustment module is used to: adjust the batch size of data used for each training according to the video memory occupied by the back-propagation parameters; when the batch size increases to the point where the video memory cannot run, obtain the batch size corresponding to the last normal operation of the video memory as the initial batch size; and adjust the initial batch size using the binary search method to obtain the target batch size.
[0066] The function of the model training acceleration device of this application has been Figure 1-Figure 3 The method embodiment shown is described in detail, so for any details not provided in the description of this embodiment, please refer to the relevant descriptions in the aforementioned embodiments, which will not be repeated here.
[0067] Optionally, an embodiment of the present application also provides a terminal device, including a processor, a memory, and a computer program stored in the memory and executable on the processor. When the computer program is executed by the processor, each process of the above-mentioned model training acceleration method embodiment is implemented, and the same technical effect can be achieved. To avoid repetition, it will not be described here.
[0068] Optionally, the embodiment of the present application further provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by the processor, each process of the above-mentioned model training acceleration method embodiment is implemented, and the same technical effect can be achieved. To avoid repetition, it is not repeated here. Among them, the computer-readable storage medium is such as a read-only memory (ROM), a random access memory (RAM), a disk or an optical disk, etc.
[0069] It should be noted that, in this article, the terms "include", "comprises" or any other variations 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, an element defined by the sentence "comprises a ..." does not exclude the existence of other identical elements in the process, method, article or device including the element.
[0070] Through the description of the above implementation methods, those skilled in the art can clearly understand that the above-mentioned embodiment methods can be implemented by means of software plus a necessary general hardware platform, and of course by hardware, but in many cases the former is a better implementation method. Based on such an understanding, the technical solution of the present application, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, a magnetic disk, or an optical disk), and includes a number of instructions for a terminal (which can be a mobile phone, a computer, a server, an air conditioner, or a network device, etc.) to execute the methods described in each embodiment of the present application.
[0071] The embodiments of the present application are described above in conjunction with the accompanying drawings, but the present application is not limited to the above-mentioned specific implementation methods. The above-mentioned specific implementation methods are merely illustrative and not restrictive. Under the guidance of the present application, ordinary technicians in this field can also make many forms without departing from the purpose of the present application and the scope of protection of the claims, all of which are within the protection of the present application.
Claims
1. A model training acceleration method, It is characterized in that include: During the model training process, the network layer whose gradient change rate is lower than the freezing threshold is frozen, and the freezing is used to stop the back propagation of the network layer; Attenuating the freezing threshold according to the attenuation coefficient to obtain an attenuated freezing threshold; The model training is performed using the unfrozen network layers, and the network layers whose gradient change rate is lower than the attenuation freezing threshold are frozen until the model training reaches a preset accuracy or a preset number of times.
2. The method according to claim 1, It is characterized in that In the process of model training, freezing the network layer whose gradient change rate is lower than the freezing threshold includes: During model training, determine the gradient change rate of each network layer; The network layer whose gradient change rate is lower than the freezing threshold is frozen so that the network layer stops back propagation.
3. The method according to claim 2, It is characterized in that Determining the gradient change rate of each current network layer includes: Determine the gradient rate as shown below: Among them, ratio is the gradient change rate; G current is the layer gradient of the current iteration; G prev is the layer gradient of the previous iteration.
4. The method according to claim 1, It is characterized in that After freezing the network layer whose gradient change rate is lower than the freezing threshold, the method further includes: Adjust the batch size of the data used for each training; The model is trained using the adjusted batch size.
5. The method according to claim 4, It is characterized in that The adjustment of the batch size of the data used for each training includes: Adjust the batch size of the data used for each training according to the video memory occupied by the back-propagation parameters; When the batch size increases to the point where the video memory cannot run, obtaining the batch size corresponding to the video memory that last ran normally as the initial batch size; The initial batch size is adjusted using a binary method to obtain a target batch size.
6. A model training acceleration device, It is characterized in that include: A first freezing module is used to freeze the network layer whose gradient change rate is lower than the freezing threshold during the model training process, and the freezing is used to stop the back propagation of the network layer; An attenuation module, used for performing attenuation processing on the freezing threshold according to an attenuation coefficient to obtain an attenuated freezing threshold; The second freezing module is used to use the unfrozen network layers to perform model training, and freeze the network layers whose gradient change rate is lower than the attenuation freezing threshold until the model training reaches a preset accuracy or a preset number of times.
7. The device according to claim 6, It is characterized in that The first freezing module is used to: During model training, determine the gradient change rate of each network layer; The network layer whose gradient change rate is lower than the freezing threshold is frozen so that the network layer stops back propagation.
8. The device according to claim 7, It is characterized in that The first freezing module is used to: Determine the gradient rate as shown below: Among them, ratio is the gradient change rate; G current is the layer gradient of the current iteration; G prev is the layer gradient of the previous iteration.
9. The device according to claim 6, It is characterized in that The device also includes: The adjustment module is used to adjust the batch size of the data used in each training; A training module is used to train the model using the adjusted batch size.
10. The device according to claim 9, It is characterized in that The adjustment module is used for: Adjust the batch size of the data used for each training according to the video memory occupied by the back-propagation parameters; When the batch size increases to the point where the video memory cannot run, obtaining the batch size corresponding to the video memory that last ran normally as the initial batch size; The initial batch size is adjusted using a binary method to obtain a target batch size.
