Model training method and device, electronic equipment and storage medium
By performing gradient iteration and asynchronous training on the forward inference results in the tangent space of the model training end, the problem of long model training time is solved, the model training efficiency is improved, and the utilization of computing resources is optimized.
Patent Information
- Application Number
- CN202410694024.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-30
- Publication Date
- 2024-09-24
AI Technical Summary
In the existing technology, model training takes a long time, resulting in low model training efficiency. This is mainly due to the decoupling of model training and model inference, and the large computing resources required for model training, which results in a very long time collection of sample data under the same computing resources. time.
By performing gradient iteration on the intermediate inference results in each forward inference process in the tangent space of the model training end, the gradient training samples are determined, and the forward inference and forward derivation are simultaneously calculated in different devices, relying on the forward The intermediate results of inference realize efficient asynchronous training and make full use of the intermediate inference results to save the time of obtaining model training samples.
Without affecting the efficiency of model inference, the efficiency of model training is greatly improved, the time of obtaining model training samples is saved, and the utilization of computing resources is optimized.
Smart Images

Figure CN118690192A
Abstract
Description
Technical Field
[0001] The present invention relates to the field of artificial intelligence technology, and in particular to a model training method, device, electronic equipment and storage medium. Background Art
[0002] For a trained large language model, you can perform model inference through interaction between the user and the large language model, or collect a certain amount of data or a certain period of time to perform model inference input prompt words and output prediction words as sample data for model training. However, since model training and model inference are decoupled, and model training requires a lot of computing resources, it takes a long time to collect a certain amount of sample data with the same computing resources as those used for model inference, which increases the time required for model training and leads to low model training efficiency. Summary of the invention
[0003] The present invention provides a model training method, device, electronic device and storage medium, which are used to solve the defect of low model training efficiency caused by the long time required for model training in the prior art. The forward reasoning and forward derivation are synchronously calculated in different devices, and the forward derivation relies on the intermediate reasoning result of the forward reasoning to achieve efficient asynchronous training. All the intermediate reasoning results in each forward reasoning are fully utilized to determine the gradient training samples for model training, and all the intermediate reasoning results are not wasted, which greatly saves the time for obtaining model training samples. Therefore, in the same computing resources as those used for model reasoning, the model reasoning efficiency is not affected and the model training efficiency can be greatly improved.
[0004] The present invention provides a model training method, which is applied to the tangent space of the model training end, and the method comprises the following steps.
[0005] During each forward reasoning process of the large language model, gradient iteration is performed on the intermediate reasoning results in each forward reasoning process; each of the intermediate reasoning results is provided by the model reasoning end; Determine the gradient training sample corresponding to each forward reasoning based on the results of each gradient iteration; The large language model is trained based on each of the gradient training samples.
[0006] According to a model training method provided by the present invention, gradient iteration is performed on the intermediate reasoning results in each forward reasoning process, including: Performing gradient calculation on the first intermediate reasoning result corresponding to the current layer and / or the input data of the previous layer during each forward reasoning process to determine the first intermediate gradient result corresponding to the current layer; The above process is repeatedly performed to traverse each layer in the large language model until the target gradient result of the last layer in the large language model is determined.
[0007] According to a model training method provided by the present invention, gradient calculation is performed on the first intermediate reasoning result corresponding to the current layer and / or the input data of the previous layer in each forward reasoning process to determine the first intermediate gradient result corresponding to the current layer, including: Determining a disturbance direction of the current layer based on a weight parameter of the current layer; Based on the first intermediate inference result of the large language model in each forward inference process and / or the previous layer of input data, gradient calculation is performed in the disturbance direction to obtain the first intermediate gradient result.
[0008] According to a model training method provided by the present invention, the step of determining the gradient training sample corresponding to each forward reasoning based on each gradient iteration result includes: For each gradient iteration result, the gradient training sample corresponding to the current forward reasoning is determined based on the target gradient result corresponding to the last layer in the gradient iteration result; wherein the target gradient result is determined based on the target reasoning result corresponding to the last layer in the current forward reasoning process.
[0009] According to a model training method provided by the present invention, the method further includes: The following process is performed for each forward reasoning: For each layer in the large language model, receive the previous layer input data and / or the first intermediate inference result corresponding to the current layer sent by the model inference end, and when the gradient has not iterated to the current layer, add a layer identifier to the previous layer input data and / or the first intermediate inference result and store them.
[0010] According to a model training method provided by the present invention, the method further includes: When it is determined that the memory space storage occupancy rate of the model training end reaches a first preset occupancy rate threshold, the model inference end is instructed to send new previous layer input data and / or new first intermediate inference results to the processor memory space, or to send them to the external storage space when the processor memory space occupancy rate reaches a second preset occupancy rate threshold.
[0011] According to a model training method provided by the present invention, the training of the large language model based on each of the gradient training samples includes: Determine a target forward reasoning result of positive feedback from the user from each forward reasoning result; When it is determined that the number of the target forward reasoning results reaches a number threshold, the model parameters of the large language model are updated based on the target gradient training samples corresponding to each of the target forward reasoning results.
[0012] The present invention also provides a model training device, which is applied to the tangent space of the model training end. The device includes: a gradient iteration unit, a sample determination unit and a model training unit.
[0013] A gradient iteration unit is used to perform gradient iteration on the intermediate reasoning results in each forward reasoning process of the large language model; each intermediate reasoning result is provided by the main space of the model reasoning end; a sample determination unit is used to determine the gradient training sample corresponding to each forward reasoning based on each gradient iteration result; A model training unit is used to train the large language model based on each of the gradient training samples.
[0014] The present invention also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the model training method described above is implemented.
[0015] The present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements any of the model training methods described above.
[0016] The present invention provides a model training method, device, electronic device and storage medium, wherein the model training method, in each forward reasoning process of a large language model in the main space of the model reasoning end, the tangent space of the model training end performs gradient iteration on the intermediate reasoning results in each forward reasoning, and determines the gradient training samples corresponding to each forward reasoning based on each gradient iteration result, so as to train the large language model based on each gradient training sample. In this way, by synchronously calculating forward reasoning and forward derivation in different devices, and forward derivation relying on the intermediate reasoning results of forward reasoning to achieve efficient asynchronous training, all the intermediate reasoning results in each forward reasoning are fully utilized to determine the gradient training samples used for model training, without wasting all the intermediate reasoning results, and greatly saving the time for obtaining model training samples, so that, in the same computing resources as those used for model reasoning, the model reasoning efficiency is not affected, and the model training efficiency can be greatly improved. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0018] Figure 1 It is one of the flow charts of the model training method provided by the present invention.
[0019] Figure 2 This is the second flow chart of the model training method provided by the present invention.
[0020] Figure 3 It is a structural schematic diagram of the model training device provided by the present invention.
[0021] Figure 4 It is a structural schematic diagram of the electronic device provided by the present invention. DETAILED DESCRIPTION
[0022] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0023] In the embodiments of the present invention, "at least one" refers to one or more, and "more than one" refers to two or more. "And / or" describes the association relationship of associated objects, indicating that three relationships may exist. For example, A and / or B may represent: A exists alone, A and B exist at the same time, and B exists alone, where A and B may be singular or plural. In the textual description of the present invention, the character " / " generally indicates that the previous and next associated objects are in an "or" relationship. In addition, it should be noted that the serial numbers themselves, such as "first", "second", etc., used to distinguish the objects described in the present invention are only used to distinguish the objects described, and do not have any order or technical meaning.
[0024] In the field of artificial intelligence technology, large model reasoning deployment refers to deploying a large language model that has been pre-trained with massive samples and fine-tuned with instructions in a local cluster, cloud server or other device, so that users can interact with the large language model through, for example, an application programming interface (API). That is, the user inputs a text prompt sentence through the API, and the large language model obtains the logical value (logits) output by the last output layer based on the text prompt sentence, and predicts the next word (next token) according to the pre-set decoding strategy (such as the greedy strategy). After that, the large language model will perform the next model reasoning based on the user's text prompt sentence and the token predicted by the current model reasoning (or the historical tokens retained in the form of a key-value cache (KV cache)). The entire model reasoning process will continue to iterate until the end symbol appears or the iteration ends when the prediction obtains tokens that meet the preset required length. Afterwards, the user can also conduct the next round of interaction based on the current round of dialogue output by the large language model. In addition, the user can also score a round of dialogue output by the large language model, such as liking or checking a round of dialogue output by the large language model. At least one round of dialogue that has been liked or checked (and may subsequently undergo some manual or automated filtering) can usually be used as high-quality training sample data to fine-tune the large language model.
[0025] In the related existing methods, after deploying the trained large language model in a local cluster, cloud server or other device, the API service that can access the large language model is opened to users. Usually, the API service is free or charged in proportion to the number of tokens generated by the model. When the user interacts with the large language model (for example, inputting text prompt words, liking or checking the predicted words output by the model, etc.), the data of the interaction between the large language model and the user is usually stored as a sample on the server side. Afterwards, when a certain amount of data is collected or the input prompt words and output predicted words inferred by the model after a certain period of reasoning, the model will be fine-tuned and trained. In addition, when computing resources are limited, it is necessary to reduce the scale of the computing cluster equipment deployed for large model reasoning, or even shut it down completely, in order to free up computing resources for model training.
[0026] However, since model training and model inference are decoupled and model training requires a lot of computing resources, it takes a long time to collect a certain amount of sample data with the same computing resources as those used for model inference, which makes the time required for model training longer, resulting in very low model training efficiency.
[0027] In order to solve the above technical problems, the present invention provides a model training method, device, electronic device and storage medium, which are used to determine model training samples and perform model training by making full use of intermediate reasoning results in the model reasoning process, thereby greatly improving the efficiency of model training results.
[0028] Combine the following Figure 1-Figure 4 The model training method, device, electronic device and storage medium of the present invention are described, wherein the executor of the model training method is the model training end, and the model training end uses the tangent space to determine the gradient training samples required for the subsequent training model, and the model training end can be a hardware device or server deployed with a large language model, the hardware device can be a graphics processing unit (GPU) device or other device, and the server can be a cloud server or a server cluster, etc. The present invention does not make specific limitations on this. Furthermore, the executor of the model training method can also be applied to a model training device set in a hardware device or server, and the model training device can be implemented by software, hardware or a combination of both. The following describes the model training method by taking the tangent space of the first GPU device used for model training as an example in which the executor of the model training method is used.
[0029] In order to facilitate understanding of the model training method provided by the embodiment of the present invention, the model training method provided by the present invention will be described in detail below through the following exemplary embodiments. It is understandable that the following exemplary embodiments can be combined with each other, and the same or similar concepts or processes may not be repeated in some embodiments.
[0030] Reference Figure 1 , which is one of the flow charts of the model training method provided by the present invention, such as Figure 1 As shown, the model training method may include the following steps 110 to 130.
[0031] Step 110: During each forward reasoning process of the large language model, gradient iteration is performed on the intermediate reasoning results in each forward reasoning process; each intermediate reasoning result is provided by the main space of the model reasoning end.
[0032] Step 120: Determine the gradient training sample corresponding to each forward reasoning based on each gradient iteration result.
[0033] The large language model may be a model that has a forward reasoning function and has been deployed in the first GPU device, such as a trained Transformer model.
[0034] The model inference end may also be a hardware device such as a GPU device or other devices, or may also be other servers such as a cloud server or a server cluster; and a main space for model inference is provided in the model inference end.
[0035] The model inference end and the model training end are two different hardware devices or two different servers.
[0036] Exemplarily, when the model training end is a first GPU device, the model reasoning end may be a second GPU device, and the second GPU device uses the main space for model reasoning.
[0037] Specifically, considering that the forward reasoning process of the large language model is a forward reasoning process, and each forward reasoning process is when the user inputs a text prompt sentence to the large language model through an API, or inputs a text prompt sentence and a previous predicted token, the above input data will be calculated layer by layer starting from the first layer of the large language model, and the intermediate reasoning results calculated at each layer will be input to the next layer for calculation and will be discarded after calculation, until the last layer is combined with a pre-set decoding strategy (such as a greedy strategy) to obtain the next token prediction; at this time, in order to save the time required for model training, the principle that the forward process and the gradient process are calculated simultaneously in the forward automatic differentiation principle can be used, and gradient iteration is performed by making full use of the intermediate reasoning results in each forward reasoning process, so as to determine the corresponding gradient training sample at the end of each forward reasoning, without wasting each intermediate reasoning result in each forward reasoning process, and the model training purpose can be achieved without changing the existing large language model deployment.
[0038] Exemplarily, it should be noted that the present invention uses forward automatic differentiation to implement the concept of synchronous calculation of the main space and the tangent space in the dual number method used in the present invention while being compatible with the existing reasoning deployment system, and places the calculation of different spaces in different devices. The principle of the dual number system is: x=a+1*∈ (1).
[0039] In formula (1), the space where a is located is the main space and its value is a, ∈ represents the abstract mark of the tangent space and its value is 1; thus, it can be seen that x is a dual number.
[0040] Dual numbers can be used to find the derivative of a function, and its derivative formula can be expressed as formula (2) through Taylor expansion.
[0041]
[0042] In formula (2), f(a) represents a function with a as the independent variable, and f(x) represents a function with x as the independent variable. It represents the derivative of the function f(a) with respect to x.
[0043] Since the main space and tangent space in the dual number method are different spaces and do not interact with each other, they are not affected by the calculation accuracy (such as the finite difference method). In addition, the calculation operations in the tangent space (such as addition, subtraction, multiplication and division, etc.) all follow the pre-set derivation calculation rules. For example, the process of derivation of two different function pairs with x as the independent variable is expressed as When , the chain rule is Following the product-rule is and the quotient-rule is
[0044] For example, g(x) = e x , then the chain-rule can be expressed as formula (3).
[0045]
[0046] In order to make full use of the intermediate inference results in each forward inference process, the present invention realizes efficient asynchronous training (the tangent space depends on the calculation results of the main space, but the main space does not depend on the calculation results of the tangent space). As shown in the above example, since the main space has calculated f(a) and the tangent space also needs e f(a) , so the tangent space can directly obtain its calculated result f(a) from the main space without calculating it again.
[0047] In addition, when the input is multi-element (such as model weight vector, matrix, etc.), forward automatic differentiation will calculate the directional gradient (directional derivative) in the tangent space as a scalar. The disturbance direction v corresponding to the directional gradient, v~Normal(0,1) is a Gaussian random number distribution with a mean of 0 and a variance of 1. In this case, the disturbance direction corresponds to a set direction; or, v=[1,-1,1,...,1] is a Rademacher random number distribution with a probability of 50% of 1 and a probability of 50% of 0. In this case, the disturbance direction also corresponds to a set direction; or, it is other random number distributions with a mean of 0 and a variance of 1, which can be expressed as formula (4).
[0048]
[0049] In formula (4), a represents all the weight parameters of the model (stored in the main space), v represents a disturbance direction corresponding to all the weight parameters of the model (generated according to the random seed in the tangent space), f(a) represents the model loss function calculated in the main space; x represents the dual number set of the model weight parameter and the corresponding disturbance direction, which can be specifically expressed as x=a+v∈; Represents the directional gradient scalar calculated in tangent space.
[0050] Based on formula (4), we can get the gradient estimate of the loss function for all weight parameters of the model. For details, please refer to formula (5).
[0051]
[0052] In summary, we can get the forward automatic differentiation example shown in Table 1, including the calculation process for the even number system.
[0053] Table 1 In Table 1, x is the model input, is the model weight parameter W b and W f The gradient scalar of and They are the model weight gradients and is an approximation of; η represents the learning rate, W b and W f are the weight parameters of different layers of the model, y, z, u and v represent the output results of different layers of the model from bottom to top, L represents the output model loss function, v b and v f Represents the weight parameter W b and W f Their respective directions, and Represents the weight parameter W b The derivatives in different directions, and Represents the weight parameter W f Derivatives in different directions, primal represents the main space, tangent represents the tangent space, X b is the model weight W b , X f is the model weight W f ; Model loss function L([W b , W f ,]) = 947 and the model parameter gradient scalar All of them can be calculated through the even number system (as shown in Table 1).
[0054] In combination with the iterative calculation principle of performing gradient calculation on the tangent space (tangent) of the even system according to the calculation results of the primary space (primal) in Table 1, the present invention sets the primary space at the model reasoning end and the tangent space at the model training end, and in each forward reasoning process of the large language model, the primary space at the model reasoning end provides the tangent space at the model training end with the intermediate reasoning results of each forward reasoning process, so that the tangent space at the model training end determines the gradient training sample corresponding to each forward reasoning by performing gradient iteration on the intermediate reasoning results of each forward reasoning process.
[0055] Step 130: Train the large language model based on each gradient training sample.
[0056] The number of gradient training samples may be the same as and correspond to the number of forward reasoning times at the model reasoning end, and the number of forward reasoning times may be the cumulative number of times when the model reasoning reaches the stopping condition, or the cumulative number of times when the number of target gradient training samples that meet the preset training conditions in the gradient training samples is sufficient. The present invention does not make specific limitations on this.
[0057] Specifically, for each determined gradient training sample, the tangent space of the first GPU device first selects a target gradient training sample that meets preset training conditions from all gradient training samples, such as each target gradient training sample corresponding to a predicted word unit that has the highest match with the user demand; further, all target gradient training samples are used to train the large language model, such as updating the weight parameters of the large language model.
[0058] The model training method provided by the present invention performs gradient iteration on the intermediate reasoning results in each forward reasoning process of the large language model in the main space of the model reasoning end, and determines the gradient training samples corresponding to each forward reasoning process based on each gradient iteration result, so as to train the large language model based on each gradient training sample. In this way, forward reasoning and forward derivation are synchronously calculated in different devices, and forward derivation relies on the intermediate reasoning results of forward reasoning to achieve efficient asynchronous training. All intermediate reasoning results in each forward reasoning are fully utilized to determine the gradient training samples used for model training, and all intermediate reasoning results are not wasted, which greatly saves the model training calculation time and storage requirements. Therefore, in the same computing resources as those used for model reasoning, it does not affect the model reasoning efficiency and can also greatly improve the model training efficiency.
[0059] Based on the above Figure 1In the model training method shown, in an exemplary embodiment, the tangent space of the first GPU device in step 110 performs gradient iteration on the intermediate reasoning result in each forward reasoning process, and its specific implementation process may include: performing gradient calculation on the first intermediate reasoning result corresponding to the current layer and / or the input data of the previous layer in each forward reasoning process to determine the second intermediate gradient result corresponding to the current layer; repeating the above process to traverse each layer in the large language model until the target gradient result of the last layer in the large language model is determined.
[0060] Specifically, refer to Figure 2 The second flowchart of the model training method shown in Figure 2 In the main space of the second GPU, each model inference needs to input the text prompt sentence as input data to the input layer of the large language model, or input the text prompt sentence and the next token predicted by the last model inference, and calculate it layer by layer from bottom to top through the large language model; considering that the gradient calculation of the current layer in the large language model needs to use the inference input of the previous layer and the inference output of the current layer, such as the tangent space in the calculation The current layer inference output x is required 10 and the previous layer input data x, at this time, the main space of the second GPU device can provide the first intermediate inference result corresponding to the current layer and the previous layer input data to the tangent space of the first GPU device. Alternatively, considering that the gradient calculation of the current layer in the large language model only requires the current layer inference output, such as the current layer inference output is e x When only the inference output e is used x Calculate the current layer gradient, at which time the main space of the second GPU device can provide the first intermediate inference result corresponding to the current layer to the tangent space of the first GPU device. Alternatively, considering that the gradient calculation of the current layer of the large language model only needs to use the previous layer of inference input, the main space of the second GPU device can provide the previous layer of input data to the tangent space of the first GPU device; in this way, the tangent space of the first GPU device can calculate the second intermediate gradient result corresponding to the current layer based on the first intermediate inference result corresponding to the current layer and / or the previous layer of input data.
[0061] It should be noted that if the current layer of gradient calculation is not the first layer, calculating the gradient of the current layer requires not only the first intermediate inference result corresponding to the current layer and / or the input data of the previous layer, but also the intermediate gradient result output from the previous layer to the current layer.
[0062] Based on the above Figure 1In the model training method shown, in an example embodiment, the tangent space of the first GPU device performs gradient calculation on the first intermediate reasoning result corresponding to the current layer and / or the input data of the previous layer in each forward reasoning process, and determines the first intermediate gradient result corresponding to the current layer. The specific implementation process may include: determining the disturbance direction of the current layer based on the weight parameter of the current layer; performing gradient calculation in the disturbance direction based on the first intermediate reasoning result of the large language model in each forward reasoning process and / or the input data of the previous layer, and obtaining the first intermediate gradient result.
[0063] Specifically, the weight parameters of each layer in the large language model can be pre-stored in the main space of the second GPU device, and a random number seed can be used to generate a perturbation direction for the weight parameters of each layer, each perturbation direction corresponding to the size of the weight parameters of the corresponding layer. In this way, the tangent space of the first GPU device can calculate the second intermediate gradient result corresponding to the current layer based on the first intermediate inference result corresponding to the current layer and / or the input data of the previous layer, or can also calculate the second intermediate gradient result corresponding to the current layer based on the first intermediate inference result corresponding to the current layer and / or the input data of the previous layer, and the intermediate gradient result output from the previous layer to the current layer.
[0064] It should be noted that when there is one first GPU device for calculating the forward derivative, the gradient of each current layer in one disturbance direction can be calculated; in order to improve the accuracy of gradient estimation, multiple first GPU devices can be allocated to perform gradient calculations in multiple disturbance directions at the same time; that is, the present invention is also applicable to forward gradient calculations in multiple disturbance directions, as shown in formula (6).
[0065]
[0066] In formula (6), v1, v2, ... are the sets of disturbance directions corresponding to the first GPU devices, ∈1, ∈2, ... are the tangent spaces corresponding to the first GPU devices, a represents all the weight parameters of the model (stored in the main space), f(a) represents the model loss function calculated in the main space; x represents the set of multivariate numbers (the dual number is a binary number) of the model weight parameters and the corresponding disturbance directions, which can be specifically expressed as x=a+v1∈1+v2∈2+...; Represents the directional gradient scalar calculated in different tangent spaces on different devices.
[0067] In order to simultaneously calculate the tangent space gradients of multiple perturbation directions, the same GPU device containing the tangent space can be used or distributed to multiple GPU devices each containing a tangent space. Here, the model parameter weights that occupy a large space are stored in the device used by the main space (i.e., the first GPU device), while the tangent space only needs to process the weight parameters of the single block model in each layer of the large language model, and the directional gradient of the single-direction loss function that needs to be saved is only a scalar. Finally, by averaging the directional derivatives of different perturbation directions and the dot product results of the perturbation direction, a more accurate model parameter gradient is obtained as shown in formula (7).
[0068]
[0069] In formula (7), K represents the number of disturbance directions.
[0070] Based on the above Figure 1 In the model training method shown in FIG. 1 , in an exemplary embodiment, the tangent space of the first GPU device in step 110 determines the gradient training sample corresponding to each forward reasoning based on each gradient iteration result, and the specific implementation process may include: For each gradient iteration result, the gradient training sample corresponding to the current forward reasoning is determined based on the target gradient result corresponding to the last layer in the gradient iteration result; wherein the target gradient result is determined based on the target reasoning result corresponding to the last layer in the current forward reasoning process. The model loss function is determined based on the target gradient result of the last layer in the gradient iteration result.
[0071] Specifically, refer to Figure 2 The second flow chart of the model training method shown in the figure shows that each forward reasoning process of the large language model in the main space of the second GPU device is when the user inputs a text prompt sentence to the large language model through the API, or inputs a text prompt sentence and the previous prediction token, and starts from the first layer (Layer 1) to calculate layer by layer. The intermediate reasoning results calculated in each layer will not only be input to the next layer, but also transmitted to the tangent space of the first GPU device; until the last layer calculates and outputs the target reasoning result, the last layer of the large language model in the tangent space of the first GPU device can calculate the target gradient result of the last layer in the corresponding disturbance direction based on the target reasoning result, and determine the target gradient result as the gradient training sample. This cycle is repeated until a large number of gradient training samples can be obtained when the model reasoning is completed.
[0072] It should be noted that if Figure 2As shown in the figure, each time forward reasoning reaches the last layer, the next token prediction can be obtained based on the target reasoning result output by the last layer and the preset decoding strategy (such as the greedy strategy), and then the model loss function at the end of the current forward reasoning is determined.
[0073] Based on the above Figure 1 In the model training method shown, in an exemplary embodiment, considering that the model inference speed of the second GPU device is faster than the gradient iteration speed of the first GPU device, the tangent space of the first GPU device can be stored after adding a layer identifier to it, so that the subsequent gradient iteration to the layer can be directly read without recalculation. Based on this, the model training method provided by the present invention can also include: The following process is performed for each forward reasoning: For each layer in the large language model, the previous layer input data and / or the first intermediate inference result corresponding to the current layer sent by the model inference end are received, and when the gradient has not been iterated to the current layer, the previous layer input data and / or the first intermediate inference result are added with a layer identifier and stored.
[0074] Specifically, during each forward reasoning process, the main space of the second GPU device calculates each intermediate reasoning result corresponding to a layer, and sends it and at least one of the input data of the previous layer to the tangent space of the first GPU device. If the current layer of the gradient iteration in the tangent space of the first GPU device is consistent with the layer where the received intermediate reasoning result is located, the gradient calculation of the current layer can be performed directly; conversely, if the current layer of the gradient iteration in the tangent space of the first GPU device is inconsistent with the layer where the received intermediate reasoning result is located, that is, the gradient has not been iterated to the layer where the received intermediate reasoning result is located, then the currently received data can be added with a layer identifier and stored so that it can be directly read when the subsequent gradient iteration reaches the layer without the need to calculate again, thereby improving the flexibility of generating gradient training samples.
[0075] Based on the above Figure 1 In the model training method shown, in an exemplary embodiment, considering that the device where the tangent space for gradient calculation is located (such as the first GPU device) has a large memory occupancy rate and cannot be stored normally, the device where the tangent space for forward reasoning is located (such as the second GPU device) can be instructed to transfer data to other devices for storage next time. Based on this, the model training method provided by the present invention can also include: When it is determined that the memory space occupancy rate of the model training end reaches a first preset occupancy rate threshold, the model reasoning end is instructed to send new previous layer input data and / or new first intermediate reasoning results to the processor memory space, or to send them to the external storage space when the processor memory space occupancy rate reaches a second preset occupancy rate threshold.
[0076] The processor memory space may specifically be the memory space of a central processing unit (CPU).
[0077] The external storage space may specifically be a hard disk memory with a relatively large storage capacity, such as a mechanical hard disk (Hard Disk Drive, HDD) or a solid state disk (Solid State Disk or Solid State Drive, SSD).
[0078] Specifically, since the calculation contents of the main space and the tangent space are different, and the calculation of the tangent space usually depends on the input and / or intermediate calculation results of the main space, it may cause the data processing on the tangent space device (that is, the first GPU device) to be not fast enough, resulting in data accumulation. In this regard, the present invention utilizes the characteristics of multi-level storage in hardware devices to solve the above-mentioned data accumulation problem, specifically by caching through storage modules at different levels. That is, when the memory space storage occupancy rate of the device used by the tangent space (such as the first GPU device) reaches a first preset occupancy rate threshold (such as 95%), a first prompt message can be output. This first prompt message can be directly sent to the second GPU device through a direct device connection, or forwarded to the second GPU device by the CPU. This first prompt message is used to inform the second GPU device that its own memory space storage occupancy rate is large enough and can no longer store new data, and to inform the device used by the main space (such as the second GPU device) Send the new input data of the previous layer and / or the new first intermediate inference result to the CPU memory space, add the layer identifier first and then store it, so that when the subsequent gradient iteration reaches this layer, it can also be directly read from the CPU memory without recalculation; if the CPU memory space storage occupancy rate also reaches the second preset occupancy rate threshold (for example, 90%), the CPU will send a second prompt message to the second GPU device to inform the second GPU device to send the new input data of the previous layer and / or the new first intermediate inference result to the external storage space with larger storage capacity next time, add the layer identifier first and then store it, so that when the subsequent gradient iteration reaches this layer, it can also be directly read from the external storage space without recalculation. In this way, the problem of data transmission loss and data backlog caused by untimely gradient calculation is solved by using a multi-level storage method, thereby ensuring that the entire gradient training sample determination process is more efficient and flexible.
[0079] It should be noted that if the external storage space reaches a preset threshold, no data will be received, but this situation almost never happens because the external storage space is very large, and the difference in the amount of computation between the main space and the tangent space is not enough to make the data cache to be consumed reach such a large order of magnitude.
[0080] Based on the above Figure 1 In the model training method shown in an example embodiment, the specific implementation process of step 120 may include: first, determining the target forward reasoning result of the user's positive feedback from each forward reasoning result; then, when it is determined that the number of target forward reasoning results reaches a quantity threshold, updating the model parameters of the large language model based on the target gradient training samples corresponding to each target forward reasoning result.
[0081] Specifically, considering that the user will have at least one round of conversation with the large language model through the API, each round of conversation will perform multiple forward reasoning and obtain forward reasoning results containing reply text sentences. At this time, the target forward reasoning result for the user's positive feedback in all forward reasoning results can be obtained. For example, when the user likes or ticks the reply text sentence, the corresponding forward reasoning result is the target forward reasoning result.
[0082] When the number of collected target forward reasoning results reaches a quantity threshold B (eg, B=512), the model parameters of the large language model are updated using the target gradient training samples corresponding to all target forward reasoning results.
[0083] For example, in Figure 2 In the flowchart of the model training method shown, the second GPU device receives a text prompt sentence input by the user. The length of the text prompt sentence is N, that is, it includes N tokens. The text prompt sentence is converted into an initial latent space variable after tokenization and discrete word element vectorization (embedding lookup) operations, and the size is [N, d], where d represents the size of the latent space dimension of the large language model.
[0084] 2.1 The model weight parameters of the large language model in the main space of the second GPU device for the current iteration step are used as the initial value of the main space variable X in the second GPU device. The current iteration step is also the current forward reasoning; the tangent space of the first GPU device randomly generates different disturbance directions v1, v2,... as the initial value of the tangent space dX of the first GPU device.
[0085] 2.2 When the main space of the second GPU device performs the main space calculation of the current layer and updates the output X; the tangent space of the first GPU device performs the tangent space calculation of the current layer (or according to the operator definition, during the tangent space calculation process, the corresponding layer waits for the output X of the main space of the second GPU device to be updated, and then performs the current layer gradient calculation of the tangent space), updates the output dX, and deletes the calculated X from the corresponding storage space to release space.
[0086] 2.3 Repeat the above process until the last layer in the large language model.
[0087] 2.4 At this time, the main space of the second GPU device detokenizes the forward reasoning results finally output by the large language model and converts them into text to facilitate intuitive feedback to users.
[0088] 2.5 During the entire forward reasoning process, the tangent space of the first GPU device always maintains the gradient calculation operation in the direction of the model weight parameter, and saves the obtained gradient and the random number seed corresponding to the generated disturbance direction to the storage hard disk. The gradient saved here is the gradient scalar average value corresponding to the text prompt sentence, which is specifically expressed as formula (8) and formula (9).
[0089]
[0090]
[0091] In formula (8) and formula (9), Represents the gradient scalar corresponding to the text prompt sentence, v i represents the i-th disturbance direction.
[0092] When the large language model in the main space of the second GPU device performs autoregressive prediction of the next word, j=1, 2, ..., n iterations of forward reasoning are performed, and the process includes: 3.1 Update the value of N to 1 and the corresponding latent space variable size to [1, d]. The forward reasoning process is similar to 2.1 to 2.5 above. The saved gradient is the gradient corresponding to a single word, which can be referred to in formula (10).
[0093]
[0094] In formula (10), when j=1, the first next token is predicted based on the text prompt sentence, and when j=2, the second next token is predicted based on the text prompt sentence and the first next token; It represents the gradient scalar value corresponding to the next token predicted by the j-th forward reasoning, and a represents the input model weight parameter and the next token input by the j-th forward reasoning.
[0095] 3.2 After the forward inference in the main space of the second GPU device is completed, the multiple gradient training samples that can be collected can be shown as formula (11).
[0096]
[0097] There may be multiple rounds of dialogue between the large language model and the user, and the operations are consistent with the above process. In each round of dialogue, the user can evaluate the reply text sentence output by the large language model. The system background will record and mark the corresponding gradient value to determine the target gradient corresponding to the positive feedback evaluation, and thus determine the corresponding target gradient training sample.
[0098] It should be noted that when the number of target gradient training samples obtained reaches a preset number threshold B (for example, B=512), the weight parameters of the large language model are updated: in, represents the bth target gradient training sample, and η represents a very small learning rate to prevent excessive updating of the model weights.
[0099] The model training device provided by the present invention is described below, and the model training device described below and the model training method described above can be referred to each other. The model training device described below is applied to the tangent space of the model training end.
[0100] Reference Figure 3 , which is a schematic diagram of the structure of the model training device provided by the present invention, such as Figure 3 As shown, the model training device 300 includes: a gradient iteration unit 310, a sample determination unit 320 and a model training unit 330.
[0101] The sample determination unit 310 is used to perform gradient iteration on the intermediate reasoning results in each forward reasoning process of the large language model; each intermediate reasoning result is provided by the main space of the model reasoning end.
[0102] The sample determination unit 320 is used to determine the gradient training sample corresponding to each forward reasoning based on each gradient iteration result.
[0103] The model training unit 330 is used to train the large language model based on each gradient training sample.
[0104] Optionally, the gradient iteration unit 310 is specifically used to perform gradient calculation on the first intermediate reasoning result corresponding to the current layer and / or the input data of the previous layer in each forward reasoning process to determine the first intermediate gradient result corresponding to the current layer; repeat the above process to traverse each layer in the large language model until the target gradient result of the last layer in the large language model is determined.
[0105] Optionally, the gradient iteration unit 310 is specifically used to determine the perturbation direction of the current layer based on the weight parameters of the current layer; perform gradient calculation in the perturbation direction based on the first intermediate inference result of the large language model in each forward inference process and / or the previous layer input data to obtain a first intermediate gradient result.
[0106] Optionally, the sample determination unit 320 is specifically used to determine, for each gradient iteration result, a gradient training sample corresponding to the current forward reasoning based on a target gradient result corresponding to the last layer in the gradient iteration result; wherein the target gradient result is determined based on the target reasoning result corresponding to the last layer in the current forward reasoning process.
[0107] Optionally, the model training device provided by the present invention may further include a data processing unit, which is specifically configured to perform the following process for each forward reasoning: For each layer in the large language model, the previous layer input data and / or the first intermediate inference result corresponding to the current layer sent by the model inference end are received, and when the gradient has not been iterated to the current layer, the previous layer input data and / or the first intermediate inference result are added with a layer identifier and stored.
[0108] Optionally, the data processing unit is further used to determine that when the memory space storage occupancy rate of the model training end reaches a first preset occupancy rate threshold, instruct the model inference end to send new previous layer input data and / or new first intermediate inference results to the processor memory space, or to send them to the external storage space when the processor memory space occupancy rate reaches a second preset occupancy rate threshold.
[0109] Optionally, the model training unit 330 is specifically used to determine the target forward reasoning result of the user's positive feedback from each forward reasoning result; when it is determined that the number of target forward reasoning results reaches a quantity threshold, the model parameters of the large language model are updated based on the target gradient training samples corresponding to each target forward reasoning result.
[0110] The model training device 300 provided in an embodiment of the present invention can execute the technical solution in any embodiment in which the execution subject is the model training method. Its implementation principle and beneficial effects are similar to the implementation principle and beneficial effects of the above-mentioned model training method. Please refer to the implementation principle and beneficial effects of the above-mentioned model training method, and no further details will be given here.
[0111] Figure 4 An example of a physical structure diagram of an electronic device is shown in FIG. Figure 4As shown, the electronic device may include: a processor 410, a communication interface 420, a memory 430 and a communication bus 440, wherein the processor 410, the communication interface 420 and the memory 430 communicate with each other through the communication bus 440. The processor 410 may call the logic instructions in the memory 430 to execute the model training method, which includes: in each forward reasoning process of the large language model, gradient iteration is performed on the intermediate reasoning results in each forward reasoning process; each intermediate reasoning result is provided by the main space of the model reasoning end; based on each gradient iteration result, the gradient training sample corresponding to each forward reasoning is determined; and the large language model is trained based on each gradient training sample.
[0112] In addition, the logic instructions in the above-mentioned memory 430 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on such an understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk and other media that can store program codes.
[0113] On the other hand, the present invention also provides a computer program product, which includes a computer program, and the computer program can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can execute the model training method provided by the above methods, and the method includes: in the process of each forward reasoning of the large language model, gradient iteration is performed on the intermediate reasoning results in each forward reasoning process; each intermediate reasoning result is provided by the main space of the model reasoning end; based on each gradient iteration result, the gradient training sample corresponding to each forward reasoning is determined; and the large language model is trained based on each gradient training sample.
[0114] On the other hand, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, is implemented to execute the model training method provided by the above-mentioned methods, the method comprising: in each forward reasoning process of the large language model, performing gradient iteration on the intermediate reasoning results in each forward reasoning process; each intermediate reasoning result is provided by the main space of the model reasoning end; based on each gradient iteration result, determining the gradient training sample corresponding to each forward reasoning; and training the large language model based on each gradient training sample.
[0115] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.
[0116] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.
[0117] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A model training method, It is characterized in that Applied in the tangent space of the model training end, the method includes: During each forward reasoning process of the large language model, gradient iteration is performed on the intermediate reasoning results in each forward reasoning process; each intermediate reasoning result is provided by the main space of the model reasoning end; Determine the gradient training sample corresponding to each forward reasoning based on the results of each gradient iteration; The large language model is trained based on each of the gradient training samples.
2. The model training method according to claim 1, It is characterized in that Performing gradient iteration on the intermediate reasoning results in each forward reasoning process, including: Performing gradient calculation on the first intermediate reasoning result corresponding to the current layer and / or the input data of the previous layer during each forward reasoning process to determine the first intermediate gradient result corresponding to the current layer; The above process is repeatedly performed to traverse each layer in the large language model until the target gradient result of the last layer in the large language model is determined.
3. The model training method according to claim 2, It is characterized in that Performing gradient calculation on the first intermediate reasoning result corresponding to the current layer and / or the input data of the previous layer in each forward reasoning process to determine the first intermediate gradient result corresponding to the current layer includes: Determining a disturbance direction of the current layer based on a weight parameter of the current layer; Based on the first intermediate inference result of the large language model in each forward inference process and / or the previous layer of input data, gradient calculation is performed in the disturbance direction to obtain the first intermediate gradient result.
4. The model training method according to any one of claims 1 to 3, It is characterized in that The step of determining the gradient training sample corresponding to each forward reasoning based on each gradient iteration result includes: For each gradient iteration result, the gradient training sample corresponding to the current forward reasoning is determined based on the target gradient result corresponding to the last layer in the gradient iteration result; wherein the target gradient result is determined based on the target reasoning result corresponding to the last layer in the current forward reasoning process.
5. The model training method according to any one of claims 1 to 3, It is characterized in that The method further comprises: The following process is performed for each forward reasoning: For each layer in the large language model, receive the previous layer input data and / or the first intermediate inference result corresponding to the current layer sent by the model inference end, and when the gradient has not iterated to the current layer, add a layer identifier to the previous layer input data and / or the first intermediate inference result and store them.
6. The model training method according to claim 5, It is characterized in that The method further comprises: When it is determined that the memory space storage occupancy rate of the model training end reaches a first preset occupancy rate threshold, the model inference end is instructed to send new previous layer input data and / or new first intermediate inference results to the processor memory space, or to send them to the external storage space when the processor memory space occupancy rate reaches a second preset occupancy rate threshold.
7. The model training method according to any one of claims 1 to 3, It is characterized in that The training of the large language model based on each of the gradient training samples includes: Determine a target forward reasoning result of positive feedback from the user from each forward reasoning result; When it is determined that the number of the target forward reasoning results reaches a number threshold, the model parameters of the large language model are updated based on the target gradient training samples corresponding to each of the target forward reasoning results.
8. A model training device, It is characterized in that Applied in the tangent space of the model training end, the device includes: A gradient iteration unit, used for performing gradient iteration on the intermediate reasoning results in each forward reasoning process of the large language model; each of the intermediate reasoning results is provided by the main space of the model reasoning end; A sample determination unit, used to determine the gradient training sample corresponding to each forward reasoning based on each gradient iteration result; A model training unit is used to train the large language model based on each of the gradient training samples.
9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, It is characterized in that When the processor executes the program, it implements the model training method as described in any one of claims 1 to 7.
10. A non-transitory computer-readable storage medium having a computer program stored thereon, It is characterized in that When the computer program is executed by a processor, it implements the model training method as described in any one of claims 1 to 7.
