Model quantification method and device, data processing method and device, equipment and storage medium
By converting model parameters from high precision to low precision and using error matrix replacement, combined with matrix decomposition and self-iterative adjustment, the problem of model performance degradation caused by post-training quantization is solved, and the effect of maintaining performance while reducing memory usage is achieved.
Patent Information
- Application Number
- CN202310796628.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-06-30
- Publication Date
- 2024-10-11
AI Technical Summary
Post-training quantization leads to the problem of reduced model performance. Existing technologies usually sacrifice greater model performance when reducing model memory usage.
By converting the model parameters of the first data storage type of the machine learning model into parameters of the second data storage type, and obtaining the error matrix used to characterize the relationship between the two, replacing the original model parameters to reduce storage space usage, At the same time, matrix decomposition and self-iteration are used to adjust the matrix decomposition results to reduce performance loss.
While reducing the memory footprint of the model, it effectively keeps the model performance from being significantly reduced and improves the deployment efficiency of the model, especially the value of deploying large language models in resource-limited environments.
Smart Images

Figure CN118761479A_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of computer technology, and in particular to a model quantization method, a data processing method, a device, a equipment and a storage medium. Background Art
[0002] Large Language Model (LLM) requires a lot of memory in reasoning applications, mainly because of the need to save model parameters and gradients.
[0003] Post-training quantization refers to the process of quantizing the model weights from full precision to low precision after training a deep learning model. In simple terms, post-training quantization can be understood as a model compression technology that converts floating-point storage (operation) to integer storage (operation).
[0004] The memory footprint of a model can be reduced through post-training quantization, but this often comes at the expense of model performance.
[0005] In fact, post-training quantization reduces the model's memory usage at the expense of model performance. Generally speaking, the more the model's memory usage is reduced, the greater the sacrifice in model performance.
[0006] Therefore, how to improve the degradation of model performance caused by post-training quantization is a technical problem that needs to be solved urgently. Summary of the invention
[0007] A technical problem to be solved by the present disclosure is how to improve the phenomenon of model performance degradation caused by post-training quantization.
[0008] According to a first aspect of the present disclosure, a model quantization method is provided, including: converting a first model parameter of a first data storage type of a machine learning model into a second model parameter of a second data storage type, the second model parameter occupying a smaller storage space than the first model parameter; obtaining an error matrix for characterizing the error between the second model parameter and the first model parameter; and replacing the first model parameter with the second model parameter and the error matrix.
[0009] Optionally, obtaining an error matrix for characterizing errors between the second model parameters and the first model parameters includes: dividing the machine learning model into multiple model parts; and obtaining an error matrix for characterizing errors between the second model parameters and the first model parameters of each model part.
[0010] Optionally, obtaining an error matrix for characterizing an error between a second model parameter and a first model parameter of each model part includes: obtaining a matrix decomposition result of the error matrix of each model part by means of matrix decomposition.
[0011] Optionally, a singular value decomposition algorithm is used to perform matrix decomposition, wherein the matrix decomposition result is recorded as U*S*V, U represents a left singular vector matrix, S represents a diagonal matrix, and V represents a right singular vector matrix.
[0012] Optionally, the method also includes: for each of the model parts, adjusting the matrix decomposition result of the model part based on the difference between the second output and the first output, wherein the second output is the output obtained by processing the input of the model part using the second model parameters of the model part and the matrix decomposition result, and the first output is the output obtained by processing the input using the first model parameters of the model part.
[0013] Optionally, adjusting the matrix decomposition result of the model part based on the difference between the second output and the first output includes: adjusting the matrix decomposition result of the model part with the goal of reducing the difference between the second output and the first output.
[0014] Optionally, the method further includes: determining whether the difference between the second output and the first output is less than a first threshold; if the difference is less than the first threshold, stopping adjusting the matrix decomposition result of the model part.
[0015] Optionally, the method also includes: merging adjacent model parts whose differences are less than a second threshold into one model part; adjusting the matrix decomposition results of the model parts constituting the merged model part based on the difference between the fourth output and the third output of the merged model part, wherein the fourth output is the output obtained by processing the input using the second model parameters of the model parts constituting the merged model part and the matrix decomposition result, and the third output is the output obtained by processing the input using the first model parameters of the merged model part.
[0016] Optionally, the method also includes: determining whether the difference between the fourth output and the third output is less than a second threshold; if the difference is less than the second threshold, stopping adjusting the matrix decomposition results of each model part constituting the merged model part.
[0017] Optionally, the method further includes: if there is currently only one merged model part, and the difference between the fourth output and the third output of the merged model part is less than a second threshold, stopping adjusting the matrix decomposition result.
[0018] Optionally, the machine learning model is a trained model; or the machine learning model is a pre-trained model, and the method further includes: using the data set to fine-tune the error matrix without adjusting the second model parameters.
[0019] Optionally, the machine learning model is a large language model, which is used to process input to obtain output, and the input and the output respectively include one or more of text, image, audio, and video.
[0020] According to a second aspect of the present disclosure, a data processing method is provided, including: obtaining a prediction sample; the prediction sample includes one or more of text, image, audio, and video; processing the prediction sample using a second model parameter and an error matrix to obtain a prediction result of the prediction sample, wherein the second model parameter is a model parameter of a second data storage type obtained by converting a first model parameter of a first data storage type of a machine learning model into a data storage type, and the error matrix is used to characterize the error between the second model parameter and the first model parameter.
[0021] Optionally, the error matrix is obtained using the method described in the first aspect above.
[0022] According to a third aspect of the present disclosure, a model quantization device is provided, including: a conversion module, used to convert a first model parameter of a first data storage type of a machine learning model into a second model parameter of a second data storage type, the second model parameter occupying a smaller storage space than the first model parameter; an acquisition module, used to acquire an error matrix used to characterize the error between the second model parameter and the first model parameter; and a replacement module, used to replace the first model parameter with the second model parameter and the error matrix.
[0023] Optionally, the acquisition module includes: a division module, used to divide the machine learning model into multiple model parts; and an acquisition sub-module, used to obtain an error matrix for characterizing the error between the second model parameters and the first model parameters of each model part.
[0024] Optionally, the acquisition submodule obtains the matrix decomposition result of the error matrix of each model part by matrix decomposition.
[0025] Optionally, the acquisition submodule performs matrix decomposition using a singular value decomposition algorithm, wherein the matrix decomposition result is recorded as U*S*V, U represents a left singular vector matrix, S represents a diagonal matrix, and V represents a right singular vector matrix.
[0026] Optionally, the device also includes: an adjustment module, used to adjust the matrix decomposition result of the model part based on the difference between the second output and the first output for each of the model parts, wherein the second output is the output obtained by processing the input of the model part using the second model parameters of the model part and the matrix decomposition result, and the first output is the output obtained by processing the input using the first model parameters of the model part.
[0027] Optionally, the adjustment module adjusts the matrix decomposition result of the model part with the goal of reducing the difference between the second output and the first output.
[0028] Optionally, the device also includes: a first judgment module, used to judge whether the difference between the second output and the first output is less than a first threshold; the adjustment module is also used to stop adjusting the matrix decomposition result of the model part if the difference is less than the first threshold.
[0029] Optionally, the device also includes: a merging module, used to merge adjacent model parts whose differences are less than a second threshold into one model part; the adjustment module also adjusts the matrix decomposition results of the model parts constituting the merged model part based on the difference between the fourth output and the third output of the merged model part, wherein the fourth output is the output obtained by processing the input using the second model parameters of the model parts constituting the merged model part and the matrix decomposition result, and the third output is the output obtained by processing the input using the first model parameters of the merged model part.
[0030] Optionally, the device further comprises: a second judgment module, configured to judge whether the difference between the fourth output and the third output is less than a second threshold;
[0031] The adjustment module is also used to stop adjusting the matrix decomposition results of each model part constituting the merged model part if the difference is less than a second threshold.
[0032] Optionally, the adjustment module is also used to stop adjusting the matrix decomposition result if there is only one merged model part at present and the difference between the fourth output and the third output of the merged model part is less than a second threshold.
[0033] Optionally, the machine learning model is a trained model; or the machine learning model is a pre-trained model, and the device also includes: a fine-tuning module, used to fine-tune the error matrix using a data set without adjusting the second model parameters.
[0034] Optionally, the machine learning model is a large language model, which is used to process input to obtain output, and the input and the output respectively include one or more of text, image, audio, and video.
[0035] According to a fourth aspect of the present disclosure, a data processing device is provided, comprising: an acquisition module, used to acquire prediction samples; the prediction samples include one or more of text, images, audio, and video; a processing module, used to process the prediction samples using second model parameters and an error matrix to obtain prediction results of the prediction samples, wherein the second model parameters are model parameters of a second data storage type obtained by converting the first model parameters of a first data storage type of a machine learning model into a data storage type, and the error matrix is used to characterize the error between the second model parameters and the first model parameters.
[0036] Optionally, the error matrix is obtained using the method described in the first aspect above.
[0037] According to a fifth aspect of the present disclosure, a computing device is provided, comprising: a processor; and a memory on which executable code is stored, and when the executable code is executed by the processor, the processor executes the method described in the first aspect or the second aspect above.
[0038] According to a sixth aspect of the present disclosure, a computer program product is provided, comprising an executable code, which, when executed by a processor of an electronic device, enables the processor to execute the method described in the first aspect or the second aspect above.
[0039] According to the seventh aspect of the present disclosure, a non-temporary machine-readable storage medium is provided, on which executable code is stored. When the executable code is executed by a processor of an electronic device, the processor executes the method described in the first aspect or the second aspect above.
[0040] The present disclosure, on the basis of converting the first model parameter of the first data storage type of the machine learning model into the second model parameter of the second data storage type, further obtains an error matrix for characterizing the error between the second model parameter and the first model parameter, and replaces the first model parameter with the second model parameter and the error matrix. The error matrix can eliminate the degradation of model performance caused by the error between the second model parameter and the first model parameter to a certain extent, thereby ensuring that the model performance is not significantly reduced while reducing the model memory usage. BRIEF DESCRIPTION OF THE DRAWINGS
[0041] The above and other objects, features and advantages of the present disclosure will become more apparent through a more detailed description of exemplary embodiments of the present disclosure in conjunction with the accompanying drawings, wherein like reference numerals generally represent like components in the exemplary embodiments of the present disclosure.
[0042] Figure 1 A schematic flowchart of a model quantization method according to an embodiment of the present disclosure is shown.
[0043] Figure 2 A schematic diagram of the model structure of the currently popular large language model GPT2 is shown.
[0044] Figure 3 A schematic diagram of the structure of each transformer in GPT2 is shown.
[0045] Figure 4 A schematic structural diagram of a model quantization device according to an embodiment of the present disclosure is shown.
[0046] Figure 5 A schematic structural diagram of a data processing device according to an embodiment of the present disclosure is shown.
[0047] Figure 6 A schematic diagram of the structure of a computing device according to an embodiment of the present disclosure is shown. DETAILED DESCRIPTION
[0048] The preferred embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. Although the preferred embodiments of the present disclosure are shown in the accompanying drawings, it should be understood that the present disclosure can be implemented in various forms and should not be limited by the embodiments described herein. On the contrary, these embodiments are provided to make the present disclosure more thorough and complete, and to fully convey the scope of the present disclosure to those skilled in the art.
[0049] First, this disclosure proposes a new model quantization scheme.
[0050] The model quantization scheme disclosed in the present invention aims to reduce the model memory usage while ensuring that the model performance is not significantly reduced.
[0051] The model quantization scheme disclosed in the present invention can be used to perform model quantization (Model Quantization) on a machine learning model trained using a data set, and can also be used to perform model quantization on a machine learning model before training.
[0052] That is, the machine learning model in the present disclosure can be a trained model or a pre-trained model.
[0053] Post-training quantization (PTQ) refers to the process of quantizing the model's weights from full precision (such as bf32) to low precision (such as int4) after training a deep learning model. This method can significantly reduce the model's memory usage and improve the model's efficiency without losing too much performance.
[0054] Compared with Quantization Aware Training (QAT), post-training quantization has simpler engineering implementation and lower computational cost. Therefore, the present disclosure can be used for post-training quantization (PTQ).
[0055] The model quantization scheme disclosed in the present invention can also be combined with model fine-tuning to provide convenience for a wider range of users.
[0056] The machine learning model to be quantified in the present disclosure may be any model constructed using machine learning technology, and in particular may be a model including a large number of parameters, such as a large language model (LLM). A large language model refers to a deep learning model trained using a large amount of text data, which can generate natural language text or understand the meaning of language text. A large language model may be, but is not limited to, a generative model, such as a GPT model.
[0057] The input of the generative model can be unimodal data (such as one of text, image, audio, and video), or multimodal data (such as at least two of text, image, audio, and video); the output of the generative model can be unimodal data or multimodal data. In other words, the generative model can support the processing of one or more modal data to generate one or more modal data. For example, the generative model can be a large model with multimodal data processing capabilities and multimodal data generation capabilities. For another example, the generative model can also be a small model that only has text processing capabilities and text generation capabilities.
[0058] The technical solution provided by the present disclosure can be applied to any application scenario that includes one or more of text, images, audio, and video. For example, the technical solution provided by the present disclosure can be used in at least one of the following scenarios:
[0059] Image processing scenarios include: optical character recognition (OCR), face recognition, object recognition and image classification. For example, OCR can be applied to bill (such as invoice) recognition, handwriting recognition, etc., face recognition can be applied to security and other fields, object recognition can be applied to traffic sign recognition in autonomous driving scenarios, and image classification can be applied to "take a photo and buy" and "find the same item" on e-commerce platforms.
[0060] Voice recognition scenarios, including products that enable human-computer interaction through voice, such as mobile phone voice assistants (such as Siri on Apple phones) and smart speakers;
[0061] Natural language processing scenarios, including: reviewing text (such as contracts, legal documents, and customer service records), identifying spam content (such as identifying spam text messages), and classifying text (emotion, intent, and topic).
[0062] Automatic control scenarios include: prediction of adjustment operations of mine groups, wind turbine groups and air conditioning systems; specifically, for mine groups, a group of adjustment operations with high mining rates can be predicted; for wind turbine groups, a group of adjustment operations with high power generation efficiency can be predicted; for air conditioning systems, a group of adjustment operations that can meet demand while saving energy can be predicted;
[0063] Intelligent question-and-answer scenarios, including chatbots and intelligent customer service;
[0064] Business decision-making scenarios, including: scenarios in the fields of financial technology, medical care, and municipal administration, among which:
[0065] The financial technology field includes: marketing (such as coupon usage prediction, ad click behavior prediction, user portrait mining, etc.) and customer acquisition, anti-fraud, anti-money laundering, underwriting and credit scoring, and commodity price prediction;
[0066] Medical fields include: disease screening and prevention, personalized health management and auxiliary diagnosis;
[0067] The municipal sector includes: social governance and regulatory enforcement, resource environment and facility management, industrial development and economic analysis, public services and livelihood security, and smart cities (deployment and management of various urban resources such as public transportation, online car-hailing, and shared bicycles);
[0068] Recommended business scenarios, including: recommendations of news, advertising, music, consulting, videos, and financial products (such as financial management, insurance, etc.);
[0069] Search scenarios include: web search, image search, text search, video search, etc.;
[0070] Abnormal behavior detection scenarios include: abnormal behavior detection of State Grid customers’ electricity consumption, network malicious traffic detection, abnormal behavior detection in operation logs, etc.
[0071] Figure 1 A schematic flowchart of a model quantization method according to an embodiment of the present disclosure is shown.
[0072] See also Figure 1In step S110, first model parameters of the first data storage type of the machine learning model are converted into second model parameters of the second data storage type.
[0073] Model parameters, that is, network parameters in a machine learning model. Model parameters mainly include, but are not limited to, weights and activation values. Model parameters in the present disclosure may specifically refer to weights. That is, the present disclosure may mainly quantify weights in a machine learning model.
[0074] The first data storage type may also be referred to as a full-precision data storage type. The first model parameter of the machine learning model stored in the first data storage type does not suffer from performance loss, but occupies a large amount of memory. The first data storage type may refer to a floating point type (such as bf32).
[0075] The second data storage type may also be referred to as a low-precision data storage type. The second model parameter of the machine learning model stored according to the second data storage type takes up less memory, but there is a performance loss. The second data storage type may refer to an integer type (such as int4).
[0076] The specific implementation of converting the first model parameter stored in the first data storage type into the second model parameter stored in the second data storage type is a mature technology in the art. For example, the model parameter can be converted according to the mapping formula between the first data storage type and the second data storage type.
[0077] The converted second model parameters occupy less storage space than the first model parameters.
[0078] In step S120, an error matrix is obtained.
[0079] There is a certain error between the second model parameters and the first model parameters. There is a certain error between the output obtained by processing the input (such as sample data) using the second model parameters and the output obtained by processing the same input using the first model parameters, which will lead to a decrease in the performance of the machine learning model.
[0080] To this end, the present disclosure proposes that an error matrix for characterizing the error between the second model parameters and the first model parameters can be obtained to alleviate the degradation of model performance caused by model quantization.
[0081] In step S130, the first model parameters are replaced by the second model parameters and the error matrix.
[0082] Replacing the first model parameters with the second model parameters and the error matrix means that in the process of processing sample data using the machine learning model, the operation of processing the input originally using the first model parameters is changed to processing the input using the second model parameters and the error matrix.
[0083] For example, for a matrix projection Y=WX in a machine learning model, X is the input, W is the weight (corresponding to the first model parameter mentioned above), and Y is the output. After converting W from a 32-bit floating point type to a 4-bit integer type, the input X is processed using the integer weight (corresponding to the second model parameter mentioned above), and there will be an error Y^=(W 4-bits – 32-bits )X. The present disclosure can use an error matrix W′=(W 4-bits – 32-bits ) represents this error. 4-bits and replace the error matrix W^ 32-bits , that is, Y=(W 4-bits X)+(W′X).
[0084] Under the effect of the error matrix W^, the output Y obtained by processing the input X using the second model parameters and the error matrix W^ can approximate the output Y obtained by processing the input X using the first model parameters. Therefore, the model memory usage can be reduced while ensuring that the model performance is not significantly reduced.
[0085] The replacement operation in step S130 may refer to replacement during application, or may refer to direct replacement after model quantization.
[0086] As an example, when a machine learning model is saved in a device or system using the model, the second model parameters can be stored in the form of a second data storage type, and the error matrix can be saved at the same time. The second model parameters and the error matrix can be regarded as a quantized machine learning model. That is, the quantized machine learning model consists of the second model parameters and the error matrix.
[0087] Machine learning models generally have many parameters. If we directly obtain an error matrix that represents the error between the second model parameters and the first model parameters of the entire model, it will be difficult and cannot effectively ensure that the model performance is not significantly reduced.
[0088] To this end, the present disclosure proposes that the machine learning model can be divided into multiple model parts, and different model parts have their own model parameters. The machine learning model is usually a hierarchical structure, so each layer in the machine learning model can be regarded as a model part according to the network structure of the machine learning model.
[0089] Therefore, when executing step S110, the first model parameters of the first data storage type of each model part can be converted into second model parameters of the second data storage type; when executing step S120, an error matrix for characterizing the errors between the second model parameters and the first model parameters of each model part can be obtained.
[0090] In order to reduce the size of the error matrix, the present disclosure proposes that the matrix decomposition results of the error matrix of each model part can be obtained by matrix decomposition. That is, the error matrix can be approximately represented by the matrix decomposition results.
[0091] Matrix decomposition refers to the low-rank decomposition of a matrix. Specifically, it means decomposing a large matrix into multiple low-rank small matrices. These small matrices can be used to approximate the large matrix through calculation.
[0092] The matrix decomposition method may include but is not limited to at least one of the following: singular value decomposition algorithm (Singular Value Decomposition, SVD), triangular decomposition method, QR decomposition method.
[0093] Taking the SVD decomposition algorithm as an example, the error matrix W^ can be decomposed into U*S*V, that is, the matrix decomposition result can be recorded as U*S*V, where U represents the left singular vector matrix, S represents the diagonal matrix, and V represents the right singular vector matrix.
[0094] The principle of SVD decomposition algorithm can be summarized as follows: a non-negative matrix A n×d , after SVD decomposition, it can be represented by the multiplication of three matrices. Among them, U is an n×n unitary matrix, which is also a left singular vector matrix; ∑ is an n×d non-negative real diagonal matrix, and V * That is, the conjugate transpose of V, which is an n×n unitary matrix, also called a right singular matrix. Such a decomposition is called the singular value decomposition of A, and the elements on the ∑ diagonal are recorded as the singular values of A.
[0095] Therefore, the present disclosure can introduce a low-rank matrix decomposition to represent W^ with a smaller matrix. Taking SVD decomposition as an example, the present disclosure can introduce the matrix W^=U∑V. Then Y=WX+U∑VX. In this way, the original large error matrix becomes three smaller low-rank matrices. Limited by the size and accuracy of U / ∑ / V, such quantization is often accompanied by precision loss, and the low-rank matrix needs to be adjusted.
[0096] To this end, the present disclosure proposes a matrix decomposition result adjustment method through self-iteration. Self-iteration refers to the process of obtaining the optimal solution by continuously iterating on the existing model without requiring additional data or reference models during the adjustment process.
[0097] Specifically, for each model part, the matrix decomposition result of the model part can be adjusted based on the difference between the second output and the first output, wherein the second output is the output obtained by processing the input of the model part using the second model parameters of the model part and the matrix decomposition result, and the first output is the output obtained by processing the input using the first model parameters of the model part.
[0098] The matrix decomposition result of the model part is adjusted based on the difference between the second output and the first output, that is, the matrix decomposition result of the model part is adjusted with the goal of reducing the difference between the second output and the first output.
[0099] The end condition for adjusting the matrix decomposition result can be set to that the difference between the second output and the first output is less than the first threshold. That is, when the difference between the second output and the first output is less than the first threshold, it can be considered that the matrix decomposition result of the model part performs well, and the adjustment of the matrix decomposition result of the model part can be stopped.
[0100] Therefore, the present disclosure can also determine whether the difference between the second output and the first output is less than a first threshold; if the difference is less than the first threshold, stop adjusting the matrix decomposition result of the model part.
[0101] Taking into account the error transmission between adjacent model parts, the present disclosure proposes that adjacent model parts whose differences are less than a second threshold can also be merged into one model part, and the second threshold can be equal to the first threshold; based on the difference between the fourth output and the third output of the merged model part, the matrix decomposition results of the model parts constituting the merged model part are adjusted, wherein the fourth output is the output obtained by processing the input using the second model parameters of the model parts constituting the merged model part and the matrix decomposition results, and the third output is the output obtained by processing the input using the first model parameters of the merged model part.
[0102] Two adjacent model parts refer to the output of the previous model part being used as the input of the subsequent model part. The input and output of the model part (especially the model part located in the middle part of the machine learning model) can refer to the feature matrix in the process of processing the sample data input to the model, rather than data in the form of text, pictures, etc. with specific meanings in the real world.
[0103] Adjusting the matrix decomposition results of the model parts constituting the merged model part based on the difference between the fourth output and the third output of the merged model part means that if the difference between the fourth output and the third output of the merged model part is large (such as greater than the second threshold), then the matrix decomposition results of the model parts constituting the merged model part can continue to be adjusted. Conversely, if the difference between the fourth output and the third output of the merged model part is small (such as less than the second threshold), the matrix decomposition results of the model parts constituting the merged model part may no longer be adjusted.
[0104] Therefore, the present disclosure can also determine whether the difference between the fourth output and the third output is less than the second threshold; if the difference is less than the second threshold, stop adjusting the matrix decomposition results of each model part that constitutes the merged model part.
[0105] For example, assuming that the differences between two adjacent model parts 10 and model parts 11 are both less than the second threshold value, it indicates that the matrix decomposition results of model part 10 and the matrix decomposition results of model part 11 currently perform better. At this time, model part 10 and model part 11 can be merged into one model part to determine whether the matrix decomposition results of the current model part 10 and the matrix decomposition results of model part 11 still perform well in the merged model part. If the performance is not good (i.e., the difference between the merged model parts is large, such as greater than the second threshold value), continue to adjust the matrix decomposition results of model part 10 and / or the matrix decomposition results of model part 11. In this way, the error transmission problem can be solved.
[0106] It should be noted that the merged model part can continue to be merged with the adjacent model part. That is, the merging process is a continuous expansion process. Through continuous merging, if there is only one merged model part in the end, and the difference between the fourth output and the third output of the merged model part is less than the second threshold, it indicates that the error transmission problem in the entire model has obtained effective results, and at this time, the adjustment of the matrix decomposition result can be stopped (i.e., ended).
[0107] The merging in the self-iterative adjustment process of the present disclosure may be performed from bottom to top or from top to bottom, and may also be performed in parallel without following a specific order.
[0108] The following is an exemplary description of the self-iterative matrix decomposition result adjustment process disclosed in the present invention in conjunction with specific embodiments.
[0109] Figure 2 A schematic diagram of the model structure of the currently popular large language model GPT2 is shown.
[0110] See also Figure 2, GPT2 is mainly composed of 12 decode-only transformers.
[0111] Figure 3 A schematic diagram of the structure of each transformer in GPT2 is shown.
[0112] The structure of GPT2 is a mature technology in this field, so this disclosure will not be described in detail. Figure 2 , Figure 3 The model structure shown.
[0113] for Figure 2 , Figure 3 For the model shown, the matrix decomposition results can be adjusted according to the following self-iterative steps.
[0114] Step 1) Perform low-rank matrix factorization quantization on each underlying model.
[0115] Step 2) Pre-cut multiple layers according to the model situation (corresponding to the model part mentioned above).
[0116] Step 3) Fine-tune the low-rank matrix decomposition results of each Layer.
[0117] 3.1. Run the full-precision model of the layer to obtain X and Y, where X is the input of the layer and Y is the output of the layer, which corresponds to the first output mentioned above.
[0118] 3.2. Input X to the quantized layer and get Y'=Layer(X). [If the layer has only one W, it is actually WX], and the W in the quantized layer includes the converted weights and the low-rank matrix decomposition results.
[0119] 3.3. Calculate the difference between Y' and Y (corresponding to the difference mentioned above), and determine whether to fine-tune based on the difference between Y' and Y. If fine-tuning is required, update the low-rank decomposition matrix in W.
[0120] Step 4) Take the current poor result of each layer as the current fine-tuning status.
[0121] Step 5) Based on the current fine-tuning situation, consider merging adjacent layers with smaller differences.
[0122] Step 6) If there is only one layer and the error of the layer difference result is within the acceptable range (ie, the difference is less than the second threshold), proceed to step 7); otherwise proceed to step 3)
[0123] Step 7) self-iteration is completed.
[0124] In summary, the present invention can reduce engineering difficulty and quantization calculation cost through post-training quantization; it can also solve the problem of reduced model performance after model quantization through matrix decomposition; further, it can also improve the quantization effect after matrix decomposition through self-iteration, and further solve the problem of performance degradation.
[0125] From the perspective of technology and effect, the present disclosure significantly reduces the performance loss of the model after quantization through matrix decomposition and self-iteration methods. Under the premise of ensuring performance, it reduces the memory requirements for deploying large language models and improves the deployment efficiency of the model. This is of great value for deploying and using large language models in resource-limited environments.
[0126] From a product perspective, this disclosure can help researchers and developers overcome memory limitations and achieve more efficient model training and deployment.
[0127] In addition, as mentioned above, the method of the present disclosure can also be combined with model fine-tuning to provide convenience for a wider range of users. Specifically, the machine learning model in the present disclosure can be a pre-trained model pre-trained on big data, and the present disclosure can use the data set to fine-tune the error matrix (such as the matrix decomposition result that characterizes the error matrix) without adjusting the second model parameters. In this way, the workload of fine-tuning can be further reduced.
[0128] Secondly, the present disclosure can also be implemented as a data processing method. The method comprises the following steps:
[0129] Obtaining a prediction sample; the prediction sample includes one or more of text, image, audio, and video;
[0130] The prediction sample is processed using the second model parameters and the error matrix to obtain a prediction result of the prediction sample, wherein the second model parameter is a model parameter of a second data storage type obtained by converting the first model parameter of the first data storage type of the machine learning model, and the error matrix is used to characterize the error between the second model parameter and the first model parameter.
[0131] In this embodiment, the second model parameters and the error matrix can be regarded as a quantized machine learning model.
[0132] The second model parameter may refer to the converted model parameter of each model part in the machine learning model. The error matrix may refer to the matrix decomposition result of the error matrix of each model part. For the method of obtaining the matrix decomposition result, please refer to the relevant description above.
[0133] The model quantization method disclosed in the present invention may also be implemented as a model quantization device. Figure 4The structure diagram of the model quantization device according to an embodiment of the present disclosure is shown. The functional units of the model quantization device can be implemented by hardware, software, or a combination of hardware and software that implements the principles of the present disclosure. It can be understood by those skilled in the art that Figure 4 The functional units described can be combined or divided into sub-units to implement the principles of the above invention. Therefore, the description herein can support any possible combination, division, or further limitation of the functional units described herein. The following is a brief description of the functional units that the model quantization device can have and the operations that each functional unit can perform. For the details involved, please refer to the relevant description above, which will not be repeated here.
[0134] See also Figure 4 The model quantization device 400 may include a conversion module 410, an acquisition module 420, and a replacement module 430. The conversion module 410 is used to convert a first model parameter of a first data storage type of a machine learning model into a second model parameter of a second data storage type, wherein the second model parameter occupies less storage space than the first model parameter.
[0135] The acquisition module 420 is used to acquire an error matrix used to characterize the error between the second model parameters and the first model parameters.
[0136] The replacement module 430 is used to replace the first model parameters with the second model parameters and the error matrix.
[0137] The acquisition module 420 may include a division module and an acquisition submodule. The division module is used to divide the machine learning model into multiple model parts; the acquisition submodule is used to obtain an error matrix for characterizing the error between the second model parameters and the first model parameters of each model part.
[0138] The acquisition submodule can obtain the matrix decomposition result of the error matrix of each model part by matrix decomposition. Optionally, the acquisition submodule can perform matrix decomposition using a singular value decomposition algorithm, wherein the matrix decomposition result is recorded as U*S*V, where U represents a left singular vector matrix, S represents a diagonal matrix, and V represents a right singular vector matrix.
[0139] The model quantization device 400 may further include an adjustment module. The adjustment module is used to adjust the matrix decomposition result of the model part based on the difference between the second output and the first output for each of the model parts, wherein the second output is an output obtained by processing the input of the model part using the second model parameter of the model part and the matrix decomposition result, and the first output is an output obtained by processing the input using the first model parameter of the model part. The adjustment module may adjust the matrix decomposition result of the model part with the goal of reducing the difference between the second output and the first output.
[0140] The model quantization device 400 may further include a first judgment module. The first judgment module is used to judge whether the difference between the second output and the first output is less than a first threshold; the adjustment module may also be used to stop adjusting the matrix decomposition result of the model part if the difference is less than the first threshold.
[0141] The model quantization device 400 may also include a merging module. The merging module is used to merge adjacent model parts whose differences are less than the second threshold into one model part; the adjustment module also adjusts the matrix decomposition results of the model parts constituting the merged model part based on the difference between the fourth output and the third output of the merged model part, wherein the fourth output is the output obtained by processing the input using the second model parameters of the model parts constituting the merged model part and the matrix decomposition result, and the third output is the output obtained by processing the input using the first model parameters of the merged model part.
[0142] The model quantization device 400 may further include a second judgment module. The second judgment module is used to judge whether the difference between the fourth output and the third output is less than a second threshold; the adjustment module may also be used to stop adjusting the matrix decomposition results of each model part constituting the merged model part if the difference is less than the second threshold.
[0143] The adjustment module may also be configured to stop adjusting the matrix decomposition result if there is currently only one merged model part and the difference between the fourth output and the third output of the merged model part is less than a second threshold.
[0144] In the present disclosure, the machine learning model may be a trained model; or the machine learning model may also be a pre-trained model, and the model quantization device 400 may further include a fine-tuning module, which is used to fine-tune the error matrix using a data set without adjusting the second model parameters.
[0145] In the present disclosure, the machine learning model may be a large language model, which is used to process input to obtain output, and the input and the output may respectively include one or more of text, image, audio, and video.
[0146] The data processing method disclosed in the present invention may also be implemented as a data processing device. Figure 5 The structure diagram of a data processing device according to an embodiment of the present disclosure is shown. The functional units of the data processing device can be implemented by hardware, software, or a combination of hardware and software that implements the principles of the present disclosure. It can be understood by those skilled in the art that Figure 5 The functional units described may be combined or divided into sub-units to implement the principles of the above invention. Therefore, the description herein may support any possible combination, division, or further limitation of the functional units described herein. The following is a brief description of the functional units that a data processing device may have and the operations that each functional unit may perform. For the details involved, please refer to the relevant description above, which will not be repeated here.
[0147] See also Figure 5 , the data processing device 500 may include an acquisition module 510 and a processing module 520 .
[0148] The acquisition module 510 is used to acquire prediction samples; the prediction samples include one or more of text, image, audio, and video.
[0149] The processing module 520 is used to process the prediction sample using the second model parameters and the error matrix to obtain the prediction result of the prediction sample, wherein the second model parameter is the model parameter of the second data storage type obtained by converting the first model parameter of the first data storage type of the machine learning model, and the error matrix is used to characterize the error between the second model parameter and the first model parameter.
[0150] The second model parameters and the error matrix can be regarded as a quantized machine learning model.
[0151] The second model parameter may refer to the converted model parameter of each model part in the machine learning model. The error matrix may refer to the matrix decomposition result of the error matrix of each model part. For the method of obtaining the matrix decomposition result, please refer to the relevant description above.
[0152] Figure 6 A schematic diagram of the structure of a computing device that can be used to implement the above-mentioned model quantization method or data processing method according to an embodiment of the present invention is shown.
[0153] See also Figure 6 , the computing device 600 includes a memory 610 and a processor 620 .
[0154] The processor 620 may be a multi-core processor or may include multiple processors. In some embodiments, the processor 620 may include a general-purpose main processor and one or more special coprocessors, such as a graphics processing unit (GPU), a digital signal processor (DSP), etc. In some embodiments, the processor 620 may be implemented using a customized circuit, such as an application-specific integrated circuit (ASIC) or a field programmable gate array (FPGA).
[0155] The memory 610 may include various types of storage units, such as system memory, read-only memory (ROM), and permanent storage devices. Among them, ROM can store static data or instructions required by the processor 620 or other modules of the computer. The permanent storage device may be a readable and writable storage device. The permanent storage device may be a non-volatile storage device that does not lose the stored instructions and data even after the computer is powered off. In some embodiments, the permanent storage device uses a large-capacity storage device (such as a magnetic or optical disk, flash memory) as a permanent storage device. In some other embodiments, the permanent storage device may be a removable storage device (such as a floppy disk, optical drive). The system memory may be a readable and writable storage device or a volatile readable and writable storage device, such as a dynamic random access memory. The system memory may store some or all instructions and data required by the processor at run time. In addition, the memory 610 may include any combination of computer-readable storage media, including various types of semiconductor memory chips (DRAM, SRAM, SDRAM, flash memory, programmable read-only memory), and disks and / or optical disks may also be used. In some embodiments, the memory 610 may include a readable and / or writable removable storage device, such as a laser disc (CD), a read-only digital versatile disc (e.g., DVD-ROM, double-layer DVD-ROM), a read-only Blu-ray disc, an ultra-density optical disc, a flash memory card (e.g., SD card, mini SD card, Micro-SD card, etc.), a magnetic floppy disk, etc. The computer-readable storage medium does not include carrier waves and transient electronic signals transmitted wirelessly or wired.
[0156] The memory 610 stores executable codes, and when the executable codes are processed by the processor 620 , the processor 620 can execute the model quantization method or data processing method mentioned above.
[0157] The model quantization method, data processing method, device, equipment and storage medium according to the present invention have been described in detail above with reference to the accompanying drawings.
[0158] In addition, the method according to the present invention may also be implemented as a computer program or a computer program product, which includes computer program code instructions for executing the above steps defined in the above method of the present invention.
[0159] Alternatively, the present invention may also be implemented as a non-temporary machine-readable storage medium (or computer-readable storage medium, or machine-readable storage medium) on which executable code (or computer program, or computer instruction code) is stored. When the executable code (or computer program, or computer instruction code) is executed by a processor of an electronic device (or computing device, server, etc.), the processor executes the various steps of the above-mentioned method according to the present invention.
[0160] Those skilled in the art will further appreciate that the various illustrative logical blocks, modules, circuits, and algorithm steps described in connection with the disclosure herein may be implemented as electronic hardware, computer software, or a combination of both.
[0161] The flow chart and block diagram in the accompanying drawings show the possible architecture, function and operation of the system and method according to multiple embodiments of the present invention. In this regard, each square box in the flow chart or block diagram can represent a part of a module, program segment or code, and the part of the module, program segment or code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the square box can also occur in a sequence different from that marked in the accompanying drawings. For example, two continuous square boxes can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each square box in the block diagram and / or flow chart, and the combination of the square boxes in the block diagram and / or flow chart can be implemented with a dedicated hardware-based system that performs the specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0162] The embodiments of the present invention have been described above, and the above description is exemplary, not exhaustive, and is not limited to the disclosed embodiments. Many modifications and variations will be apparent to those of ordinary skill in the art without departing from the scope and spirit of the described embodiments. The terms used herein are selected to best explain the principles of the embodiments, practical applications, or improvements to the technology in the market, or to enable other persons of ordinary skill in the art to understand the embodiments disclosed herein.
Claims
1. A model quantization method, characterized in that: include: Converting a first model parameter of a first data storage type of a machine learning model into a second model parameter of a second data storage type, wherein the second model parameter occupies less storage space than the first model parameter; Obtaining an error matrix for characterizing the error between the second model parameters and the first model parameters; The first model parameters are replaced by the second model parameters and the error matrix.
2. The method according to claim 1, characterized in that Obtaining an error matrix for characterizing an error between the second model parameter and the first model parameter, comprising: dividing the machine learning model into a plurality of model parts; An error matrix for characterizing the errors between the second model parameters and the first model parameters of each model part is obtained.
3. The method according to claim 2, characterized in that Obtaining an error matrix for characterizing the error between the second model parameters and the first model parameters of each model part, including: The matrix decomposition results of the error matrix of each model part are obtained by matrix decomposition.
4. The method according to claim 3, characterized in that Also includes: For each of the model parts, the matrix decomposition result of the model part is adjusted based on the difference between the second output and the first output, wherein the second output is the output obtained by processing the input of the model part using the second model parameters of the model part and the matrix decomposition result, and the first output is the output obtained by processing the input using the first model parameters of the model part.
5. A data processing method, characterized in that: include: Get prediction samples; The prediction sample includes one or more of text, image, audio, and video; The prediction sample is processed using the second model parameters and the error matrix to obtain a prediction result of the prediction sample, wherein the second model parameter is a model parameter of a second data storage type obtained by converting the first model parameter of the first data storage type of the machine learning model, and the error matrix is used to characterize the error between the second model parameter and the first model parameter.
6. A model quantization device, characterized in that: include: a conversion module, configured to convert a first model parameter of a first data storage type of a machine learning model into a second model parameter of a second data storage type, wherein the second model parameter occupies less storage space than the first model parameter; An acquisition module, used for acquiring an error matrix used to characterize the error between the second model parameter and the first model parameter; A replacement module is used to replace the first model parameters with the second model parameters and the error matrix.
7. A data processing device, characterized in that: include: An acquisition module, used to acquire a prediction sample; the prediction sample includes one or more of text, image, audio, and video; A processing module is used to process the prediction sample using a second model parameter and an error matrix to obtain a prediction result of the prediction sample, wherein the second model parameter is a model parameter of a second data storage type obtained by converting the first model parameter of a first data storage type of a machine learning model into a data storage type, and the error matrix is used to characterize the error between the second model parameter and the first model parameter.
8. A computing device comprising: processor; as well as A memory having executable codes stored thereon, which, when executed by the processor, causes the processor to execute the method according to any one of claims 1 to 5.
9. A computer program product, comprising executable codes, which, when executed by a processor of an electronic device, causes the processor to perform the method according to any one of claims 1 to 5.
10. A non-transitory machine-readable storage medium having executable codes stored thereon, which, when executed by a processor of an electronic device, causes the processor to execute the method according to any one of claims 1 to 5.
Citation Information
Patent Citations
Arithmetic apparatus and arithmetic method
US20220076122A1
