Quantization method, electronic device, chip system and storage medium for large language model
Through adaptive learning and the update of the difference loss function, the scaling coefficient is adjusted and group quantization is performed, which solves the problem of accuracy decrease caused by abnormal outliers in the quantization process of large language models and improves the accuracy of the quantized model.
Patent Information
- Application Number
- CN202410355944.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-03-27
- Publication Date
- 2024-09-13
- Estimated Expiration
- 2044-03-27
AI Technical Summary
During the quantization process of large language models, the scaling scale is abnormally large due to abnormal outliers, resulting in a decrease in the accuracy of the quantized model.
Through adaptive learning and the update of the difference loss function, the scaling coefficient is adjusted to reduce the impact of outliers on the weights, and the quantization accuracy is improved through finer group quantization.
It effectively reduces the accuracy decline of the quantized model and improves the accuracy of the quantized large language model.
Smart Images

Figure CN118095373B
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of model quantization, and in particular to a quantization method, electronic device, chip system and storage medium for a large language model. Background Art
[0002] The large language model is a deep learning model trained based on massive text data. The large language model can not only generate natural language text, but also deeply understand the meaning of the text and handle various natural language tasks, such as text summarization, question answering, translation, etc. The large language model has a very large amount of data, so it is necessary to quantize the large language model, that is, map the floating-point data in the large language model to fixed-point data, thereby reducing the number of parameters and improving the calculation speed.
[0003] Currently, after the large language model has been trained, it can be quantized through the post-training quantization method. However, this quantization method is related to the maximum and minimum values of the floating-point weights before quantization. If there are abnormal outliers in the weights before quantization, it may cause a large deviation in the weights after quantization, resulting in a decrease in the accuracy of the quantized model. Summary of the invention
[0004] The present application provides a quantization method, electronic device, chip system and storage medium for a large language model, which can improve the accuracy of the quantized model.
[0005] In order to achieve the above-mentioned purpose, the first aspect of the present application adopts the following technical solutions:
[0006] The first aspect of the present application provides a quantization method for a large language model, comprising:
[0007] Based on the set scaling factor, a large language model with quantized weights is obtained;
[0008] Inputting the first business data into the large language model after quantization of weights to obtain a first output;
[0009] Inputting the first business data into a large language model without quantized weights to obtain a second output;
[0010] Obtaining a difference loss function according to the first output and the second output;
[0011] The scaling coefficient is updated by the difference loss function, and the difference loss function is updated by the latest updated scaling coefficient until the difference loss function converges, and the large language model after quantization weight obtained by the corresponding scaling coefficient when the difference loss function converges is the quantized large language model.
[0012] The method can obtain the large language model after quantizing the weights based on the scaling coefficient set for each weight of the large language model, and then input the same business data into the large language model after quantizing the weights and the large language model without quantizing the weights, so that the difference in the outputs of the two converges, and the large language model corresponding to the scaling coefficient with the smallest difference is the quantized large language model; the problem of abnormally large scaling caused by outliers of the weights can be reduced through adaptive learning a; and the quantization accuracy can be improved through finer group quantization.
[0013] As an implementation of the first aspect, updating the scaling factor by the difference loss function, and updating the difference loss function by the latest updated scaling factor until the difference loss function converges, includes:
[0014] Updating the scaling factor to a first scaling factor by using the difference loss function;
[0015] Based on the first scaling factor, updating the large language model after quantization weights;
[0016] Inputting the second service data into the large language model after the latest quantization weight, to obtain a third output;
[0017] Inputting the second service data into the large language model without quantized weights to obtain a fourth output;
[0018] updating the difference loss function according to the third output and the fourth output;
[0019] If the most recently updated difference loss function does not converge, the scaling factor continues to be updated to the second scaling factor according to the most recently updated difference loss function, and the difference loss function is updated by the second scaling factor until the most recently updated difference loss function converges.
[0020] In the present application, after obtaining the difference loss function according to the initial scaling coefficient, it is necessary to reversely update the scaling coefficients of each weight according to the latest difference loss function, and then update the difference loss function again according to the updated scaling coefficients of each weight, and continue to reversely update the scaling coefficients of each weight according to the latest updated difference loss function. The execution is repeated in this way until the difference loss function converges. When the difference loss function converges, the latest large language model after quantization of the weights can be obtained, and different business data are used for different numbers of cycles.
[0021] As another implementation of the first aspect, based on the set scaling factor, a large language model after quantization weights is obtained, including:
[0022] For the i-th weight of the b-th block in the large language model, where b is a natural number greater than or equal to 1, the maximum value of b is the number of blocks in the large language model, and i is a natural number greater than or equal to 1, the maximum value of i is the number of quantifiable weights in the b-th block in the large language model:
[0023] According to the scaling factor set for the ith weight of the bth block in the large language model, an initial scaling scale of the ith weight of the bth block is obtained, wherein the initial scaling scale of the ith weight of the bth block is related to the scaling factor set for the ith weight of the bth block, and a maximum element and a minimum element in the ith weight of the bth block;
[0024] According to each element in the ith weight of the bth block and the initial scaling scale of the ith weight of the bth block, the quantized value of each element in the ith weight of the bth block is obtained;
[0025] After quantizing each element of each weight of each block in the large language model, a large language model with quantized weights is obtained.
[0026] In the present application, different scaling factors are set for different weights, and the scaling scale is determined by the scaling factors, so that the problem of excessive scale caused by abnormal outliers of the weights can be adjusted by the scaling factors; of course, different scaling factors can be set for different weights, so that the scaling factor of each weight is adaptively adjusted according to the range of the maximum element and the minimum element of each weight, thereby obtaining a larger language model after quantization with higher accuracy.
[0027] As another implementation of the first aspect, the initial scaling scale of the i-th weight of the b-th block is:
[0028] scale_bi=a_bi(X_bimax-X_bimin) / (2 N -1), where scale_bi represents the initial scaling scale of the ith weight of the bth block, a_bi represents the scaling factor set for the ith weight of the bth block, X_bimax represents the maximum element in the ith weight of the bth block, X_bimin represents the minimum element in the ith weight of the bth block, and N is the quantization bit;
[0029] The quantized value of the kth element in the ith weight of the bth block is:
[0030] X_bikq=X_bikfloat / scale_bi+zero_bi, where X_bikq represents the fixed-point value of the k-th element in the i-th weight of the b-th block, X_bikfloat represents the floating-point value of the k-th element in the i-th weight of the b-th block, and zero_bi=0-round(X_bimin / scale_bi, num_digits) or zero_bi =255-round(X_bimax / scale_bi, num_digits).
[0031] As another implementation of the first aspect, the difference loss function is a mean square error loss function between an output of the large language model after quantizing the weights and an output of the large language model without quantizing the weights.
[0032] As another implementation of the first aspect, obtaining a large language model after quantization weights based on the set scaling coefficient includes:
[0033] Based on the set group size and the set scaling factor, a large language model with quantized weights corresponding to each group size is obtained;
[0034] The step of inputting the first service data into the large language model after quantization of the weights to obtain the first output includes:
[0035] Inputting the first service data into the large language model after quantization weights corresponding to each group size, to obtain a first output corresponding to each group size;
[0036] The obtaining a difference loss function according to the first output and the second output includes:
[0037] According to the first output and the second output corresponding to each grouping size, a difference loss function corresponding to each grouping size is obtained;
[0038] The updating of the scaling factor by the difference loss function and the updating of the difference loss function by the latest updated scaling factor until the difference loss function converges comprises:
[0039] For a first grouping size, a scaling factor corresponding to the first grouping size is updated by a difference loss function corresponding to the first grouping size, and the difference loss function corresponding to the first grouping size is updated by a newly updated scaling factor corresponding to the first grouping size until the difference loss function corresponding to the first grouping size converges. When the difference loss function converges, the large language model after quantization weights obtained by the grouping size and scaling factor corresponding to the minimum difference loss function is the quantized large language model, and the first grouping size is any one of the preset multiple grouping sizes.
[0040] In the present application, the weights in the large language model can be grouped in dimensions based on multiple grouping sizes, and for the elements in each small group under each grouping size of the weight, the quantized elements corresponding to each element are calculated through the maximum and minimum values of the elements in the small group and the scaling coefficient a of the weight; in this way, the quantized elements of each weight in the large language model are obtained; the same business data is input into the large language model with quantized weights and the large language model with unquantized weights, so that the difference in the outputs of the two converges, and the large language model corresponding to the grouping method and scaling coefficient with the smallest difference is the quantized large language model; the problem of abnormally large scaling caused by outliers of the weight can be reduced by adaptive learning a; and the quantization accuracy is improved by finer grouping quantization.
[0041] As another implementation of the first aspect, based on the set group size and the set scaling factor, a large language model after quantization weights corresponding to each group size is obtained, including:
[0042] For the jth grouping of the ith weight of the bth block in the large language model, where b is a natural number greater than or equal to 1, the maximum value of b is the number of blocks in the large language model, i is a natural number greater than or equal to 1, the maximum value of i is the number of quantifiable weights in the bth block in the large language model, j is a natural number greater than or equal to 1, the maximum value of j is the number of groups of the ith weight of the bth block in the large language model:
[0043] According to the scaling factor set for the i-th weight of the b-th block in the large language model, an initial scaling scale of the j-th group of the i-th weight of the b-th block is obtained, wherein the initial scaling scale of the j-th group of the i-th weight of the b-th block is related to the scaling factor set for the i-th weight of the b-th block, and a maximum element and a minimum element in the j-th group of the i-th weight of the b-th block;
[0044] According to each element in the jth group of the ith weight of the bth block and the initial scaling scale of the jth group of the ith weight of the bth block, the quantized value of each element in the jth group of the ith weight of the bth block is obtained;
[0045] After quantizing each element in each group of each weight of each block in the large language model, a large language model with quantized weights is obtained.
[0046] In this application, the weights can be quantized in a finer granularity by grouping, thereby improving the accuracy of the large language model.
[0047] As another implementation of the first aspect, the initial scaling scale of the j-th group of the i-th weight of the b-th block is:
[0048] scale_bij=a_bi(X_bijmax-X_bijmin) / (2 N -1), where scale_bij represents the initial scaling scale of the jth group in the ith weight of the bth block, a_bi represents the scaling factor set for the ith weight of the bth block, X_bijmax represents the maximum element in the jth group of the ith weight of the bth block, X_bijmin represents the minimum element in the jth group of the ith weight of the bth block, and N is the quantization bit;
[0049] The quantized value of each element in the jth group of the ith weight of the bth block is:
[0050] X_bijkq=X_bijkfloat / scale_bij+zero_bij, where X_bijkq represents the fixed-point value of the k-th element in the j-th group of the i-th weight of the b-th block, X_bijkfloat represents the floating-point value of the k-th element in the j-th group of the i-th weight of the b-th block, and zero_bij=0-round(X_bijmin / scale_bij, num_digits) or zero_bij =255-round(X_bijmax / scale_bij, num_digits).
[0051] In the present application, different groups under the same weight use the same scaling factor, but because the maximum and minimum values within each group are different, the scaling scales under different groups under the same weight are also different, thereby improving the accuracy of quantization.
[0052] In a second aspect, an electronic device is provided, comprising a processor, wherein the processor is used to call a computer program stored in a memory to implement any method of the first aspect of the present application.
[0053] In a third aspect, a chip system is provided, including a processor, wherein the processor is coupled to a memory, and the processor executes a computer program stored in the memory so that an electronic device implements any method of the first aspect of the present application.
[0054] In a fourth aspect, a computer-readable storage medium is provided, wherein the computer-readable storage medium stores a computer program, and when the computer instructions are executed on an electronic device, the electronic device implements any one of the methods in the first aspect of the present application.
[0055] In a fifth aspect, an embodiment of the present application provides a computer program product, which, when executed on a device, enables the electronic device to execute any one of the methods in the first aspect of the present application.
[0056] It can be understood that the beneficial effects of the second to fifth aspects mentioned above can be found in the relevant description of the first aspect mentioned above, and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS
[0057] Figure 1 A schematic diagram of a hardware structure of an electronic device provided in an embodiment of the present application;
[0058] Figure 2 A schematic diagram of a flow chart of a quantization method for a large language model provided in an embodiment of the present application;
[0059] Figure 3 A schematic diagram of the structure of a large language model provided in an embodiment of the present application and a process of obtaining a large language model with quantized weights for a certain grouping size;
[0060] Figure 4 A schematic diagram of a process for training a scaling factor and a process for determining a target large language model through multiple grouping sizes provided in an embodiment of the present application. DETAILED DESCRIPTION
[0061] In the following description, specific details such as specific system structures and technologies are provided for illustration rather than limitation, so as to provide a thorough understanding of the embodiments of the present application. However, it should be clear to those skilled in the art that the present application can also be implemented in other embodiments without these specific details.
[0062] It should be understood that when used in the present specification and the appended claims, the term "comprising" indicates the presence of described features, wholes, steps, operations, elements and / or components, but does not exclude the presence or addition of one or more other features, wholes, steps, operations, elements, components and / or combinations thereof.
[0063] It should also be understood that in the embodiments of the present application, "one or more" refers to one, two or more than two; "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. The character " / " generally indicates that the associated objects before and after are in an "or" relationship.
[0064] In addition, in the description of the present application specification and the appended claims, the terms "first", "second", "third", "fourth", etc. are only used to distinguish the descriptions and cannot be understood as indicating or implying relative importance.
[0065] References to "one embodiment" or "some embodiments" etc. described in the specification of this application mean that one or more embodiments of the present application include specific features, structures or characteristics described in conjunction with the embodiment. Therefore, the statements "in one embodiment", "in some embodiments", "in some other embodiments", "in some other embodiments", etc. that appear in different places in this specification do not necessarily refer to the same embodiment, but mean "one or more but not all embodiments", unless otherwise specifically emphasized in other ways. The terms "including", "comprising", "having" and their variations all mean "including but not limited to", unless otherwise specifically emphasized in other ways.
[0066] The embodiment of the present application provides a quantization method for a large language model, which can be applied to an electronic device, and the quantized large language model can also be applied to the electronic device and other electronic devices. The electronic device in the embodiment of the present application can be a tablet computer, a mobile phone, a wearable device, a notebook computer, an ultra-mobile personal computer (UMPC), a netbook, a personal digital assistant (PDA), etc. The embodiment of the present application does not limit the specific type of the electronic device.
[0067] Figure 1 The electronic device 100 may include a processor 110, an external memory interface 120, an internal memory 121, a universal serial bus (USB) interface 130, a charging management module 140, a power management module 141, a battery 142, an antenna 1, an antenna 2, a mobile communication module 150, a wireless communication module 160, an audio module 170, a speaker 170A, a receiver 170B, a microphone 170C, an earphone interface 170D, a sensor module 180, a button 190, a motor 191, an indicator 192, a camera 193, a display screen 194, and a subscriber identification module (SIM) card interface 195. The sensor module 180 may include a pressure sensor 180A, a gyroscope sensor 180B, an air pressure sensor 180C, a magnetic sensor 180D, an acceleration sensor 180E, a distance sensor 180F, a proximity light sensor 180G, a fingerprint sensor 180H, a temperature sensor 180J, a touch sensor 180K, an ambient light sensor 180L, a bone conduction sensor 180M, etc.
[0068] It is to be understood that the structure illustrated in the embodiment of the present application does not constitute a specific limitation on the electronic device 100. In other embodiments of the present application, the electronic device 100 may include more or fewer components than shown in the figure, or combine some components, or split some components, or arrange the components differently. The components shown in the figure may be implemented in hardware, software, or a combination of software and hardware.
[0069] The processor 110 may include one or more processing units, for example, the processor 110 may include an application processor (AP), a modem processor, a graphics processor (GPU), an image signal processor (ISP), a controller, a memory, a video codec, a digital signal processor (DSP), a baseband processor, and / or a neural-network processing unit (NPU), etc. Different processing units may be independent devices or integrated into one or more processors.
[0070] The processor 110 may also be provided with a memory for storing instructions and data. In some embodiments, the memory in the processor 110 is a cache memory. The memory may store instructions or data that the processor 110 has just used or cyclically used. If the processor 110 needs to use the instruction or data again, it may be directly called from the memory. This avoids repeated access, reduces the waiting time of the processor 110, and thus improves the efficiency of the system.
[0071] The internal memory 121 can be used to store computer executable program codes, and the executable program codes include instructions. The processor 110 executes various functional applications and data processing of the electronic device 100 by running the instructions stored in the internal memory 121. The internal memory 121 may include a program storage area and a data storage area. Among them, the program storage area can store an operating system and an application required for at least one function (such as an image playback function, etc.). The touch sensor 180K is also called a "touch panel". The touch sensor 180K can be set on the display screen 194, and the touch sensor 180K and the display screen 194 form a touch screen, also known as a "touch screen". The touch sensor 180K is used to detect touch operations acting on or near it. The touch sensor can pass the detected touch operation to the application processor to determine the type of touch event. Visual output related to the touch operation can be provided through the display screen 194. In other embodiments, the touch sensor 180K can also be set on the surface of the electronic device 100, which is different from the position of the display screen 194.
[0072] The electronic device 100 implements the display function through a GPU, a display screen 194, and an application processor. The GPU is a microprocessor for image processing, which connects the display screen 194 and the application processor. The GPU is used to perform mathematical and geometric calculations for graphics rendering. The processor 110 may include one or more GPUs that execute program instructions to generate or change display information.
[0073] The display screen 194 is used to display images, videos, etc. The display screen 194 includes a display panel. The display panel can be a liquid crystal display (LCD), an organic light-emitting diode (OLED), an active-matrix organic light-emitting diode or an active-matrix organic light-emitting diode (AMOLED), a flexible light-emitting diode (FLED), Miniled, MicroLed, Micro-OLED, a quantum dot light-emitting diode (QLED), etc. In some embodiments, the electronic device 100 may include 1 or N display screens 194, where N is a positive integer greater than 1.
[0074] The embodiment of the present application does not particularly limit the specific structure of the execution subject of the quantization method of a large language model, as long as the code of the quantization method of a large language model of the embodiment of the present application can be run to communicate according to the quantization method of a large language model provided by the embodiment of the present application. For example, the execution subject of the quantization method of a large language model provided by the embodiment of the present application can be a functional module in an electronic device that can call and execute a program, or a communication device applied to an electronic device, such as a chip.
[0075] The Large Language Model (LLM) is a deep learning model trained based on massive text data. The LLM can not only generate natural language text, but also deeply understand the meaning of the text and handle various natural language tasks such as text summarization, question answering, translation, etc.
[0076] Large language models usually have a model structure with billions or tens of billions of parameters, and are usually composed of multiple transformer decoder blocks with the same structure stacked together. Taking the 7B model as an example, the main parameter configuration is a 32-layer transformer decoder block, each layer has a hidden dimension of 4096, the number of attention heads is 32, the input vector dimension is 120,000 words, and each word is represented by a vector of 4096 floating-point numbers, totaling 7 billion parameters. Under floating-point FP16 precision, a model with such a large number of parameters occupies 15G of ROM storage. Under INT4 quantization storage, it is approximately a 3.42G model and a 980MB vocabulary, which is approximately 4.38G. Therefore, it is usually necessary to quantize large language models.
[0077] Model quantization is to convert a floating-point model (parameters are floating-point type) into a fixed-point model (parameters are fixed-point type) in some way. For example, the weights in the floating-point model are all float32. Through model quantization, the model is converted into a fixed-point model with weights all int8.
[0078] It can be understood that the floating-point model before quantization has a large number of parameters, a large amount of calculation, a lot of memory usage, and high precision; the fixed-point model after quantization has a small number of parameters, fast calculation speed, and small memory usage, but it will lose precision. Therefore, model quantization can reduce the complexity of large language models.
[0079] Model quantization methods include: Quantization Aware Training (QAT) and Post-Training Quantization (PTQ). Quantization Aware Training simulates quantization during the training process of the network model. Post-Training Quantization can directly convert a pre-trained floating-point model into a fixed-point model without the original training process, usually without data or with only a small calibration dataset.
[0080] It is understandable that quantization-aware training has better results with less accuracy loss, but it needs to be strongly coupled with the training process. Although post-training quantization has slightly worse results, it is relatively simple to implement and does not need to be strongly coupled with the training process.
[0081] The embodiment of the present application provides a post-training quantization method for quantizing a large language model. The quantization method for a large language model provided by the embodiment of the present application is described in detail below.
[0082] First, we describe a method for quantizing a large language model after training. Model quantization is to establish a mapping relationship between floating-point data and fixed-point data, so as to obtain greater benefits (for example, in terms of parameter quantity, computing speed, and memory usage) with less precision loss.
[0083] Taking int8 as an example, the data range after quantization is [-128, 127]; that is, the maximum value of the elements in the fixed-point weight after quantization is 127, and the minimum value of the elements in the fixed-point weight after quantization is -128.
[0084] Calculate the quantization scale: scale = (Xmax-Xmin) / [127-(-128)], that is, scale = (Xmax-Xmin) / 256. Among them, Xmax represents the maximum value of the element in the floating-point weight before quantization, and Xmin represents the minimum value of the element in the floating-point weight before quantization. -128 represents the minimum value of the element in the fixed-point weight after quantization, and 127 represents the maximum value of the element in the fixed-point weight after quantization.
[0085] Calculate zero = 0-round (Xmin / scale) or zero = 255-round (Xmax / scale), where the round() function indicates rounding to the specified number of decimal places. For example, round(a, 4) rounds a to 4 decimal places.
[0086] Calculate Xq = Xfloat / scale + zero, where Xq represents the fixed-point element in the fixed-point weight after quantization, and Xfloat represents the floating-point element in the floating-point weight before quantization.
[0087] In this way, the floating-point weights in a large language model can be quantized into fixed-point weights. However, the values of the elements in the fixed-point weights are determined by the scale, and the scale is determined by the maximum and minimum values of the elements in the fixed-point weights after quantization. Therefore, there may be a problem that the maximum tensor of the floating-point weights is 12288*4096, and the minimum tensor is 4096*4096. The range of Xmax-Xmin is likely to increase in scale due to an abnormal value (for example, the value of an element in a floating-point weight is abnormal and very large, or the value of an element in a floating-point weight is abnormal and very small), resulting in a large deviation in Xq and a decrease in the accuracy of the final model.
[0088] The large language model quantization method provided in the embodiment of the present application can perform adaptive scaling to reduce the impact of abnormal outliers, and at the same time perform multi-dimensional grouping quantization to improve the accuracy of the model.
[0089] Below through Figure 2 Another large language model quantization method provided in an embodiment of the present application is described. The method comprises the following steps:
[0090] S101, setting a grouping size, where the grouping size is used to group elements in the weight of a large language model.
[0091] In the embodiment of the present application, four grouping sizes can be set: 4*16, 4*32, 4*64, 4*128; of course, fewer or more grouping sizes can be set in actual applications. Similarly, other grouping sizes can also be set, for example, 8*16, 8*32, etc. can be set.
[0092] S102, set the zoom scale to a(Xmax-Xmin) / (2 N -1), where a is the scaling factor and N is the number of quantized bits.
[0093] As an example, if quantization is int4, N is 4 and scale = a(Xmax-Xmin) / 15; if quantization is int8, scale = a(Xmax-Xmin) / 255.
[0094] S103, obtaining a large language model after quantization weights based on each group size and scaling scale.
[0095] In the embodiment of the present application, taking one of the grouping sizes as an example, the elements in the weights of the large language model are grouped according to the grouping size.
[0096] As an example, see Figure 3 A large language model includes 32 transformer decoder blocks, each block includes 5 quantizable weights, and for a weight of a block, the dimension of weight X011 is 12288*4096. That is, weight X011 contains 12288*4096 elements. Of course, the dimension of weight can also be 4096*4096. Other examples are not given one by one.
[0097] A1, group the weight dimensions according to the first grouping size 4*16 to obtain multiple groups of weight elements.
[0098] Reference Figure 3 , group the weight X011 according to 4*16, and each group includes 64 weight elements.
[0099] A2, according to the maximum and minimum values in each group of weight elements, obtain the scale corresponding to each group of weight elements = a (Xmax-Xmin) / (2 N -1).
[0100] That is, the scale of the first group of weight elements is obtained by using the maximum value (Xmax) in the first group of weight elements, the minimum value (Xmin) in the first group of weight elements and the scaling factor a;
[0101] The scale of the second group of weight elements is obtained by using the maximum value (Xmax) of the second group of weight elements, the minimum value (Xmin) of the second group of weight elements and the scaling factor a;
[0102] …
[0103] A3, according to the scale corresponding to each group of weight elements, get the zero corresponding to each group of weight elements. Zero = 0-round (Xmin / scale) or zero = 255-round (Xmax / scale).
[0104] Correspondingly, the first group of weight elements has corresponding zero; the second group of weight elements also has corresponding zero. The scales in different groups of weight elements may be the same or different; similarly, the zeros of different groups of weight elements may be the same or different.
[0105] A4, according to the scale and zero corresponding to each group of weight elements, each group of quantized weight elements Xq = Xfloat / scale + zero is obtained. Each group of quantized weight elements is combined together to form a quantized weight, and each quantized weight constitutes a quantized large language model.
[0106] In the embodiment of the present application, each floating-point weight element before quantization is divided by the scale corresponding to the group to which the floating-point weight element belongs and then added with the zero corresponding to the group to which the floating-point weight element belongs to, so as to obtain each fixed-point weight element after quantization. In this way, the quantized weight X011 can be obtained. The quantized weight X011 includes the scaling factor a.
[0107] Reference Figure 3 As shown, for each weight in each transformer decoder block in the large language model, steps A1 to A4 need to be executed repeatedly to obtain the quantized elements corresponding to each weight in each block, and the large language model with quantized weights can be obtained.
[0108] In practical applications, different groups of a weight element can use different scaling factors a, for example, the first group uses a1, the second group uses a2, etc.; different groups of a weight can also use the same scaling factor a, that is, each group of the weight uses the same scaling factor a. Similarly, different weights in a block can use different scaling factors a, that is, the first weight uses scaling factor a11 (each group in the first weight uses scaling factor a11), and the second weight uses scaling relationship tree a22 (each group in the second weight uses scaling factor a22); of course, different weights in a block can also use the same scaling factor, and the scaling factors of weights between different blocks in a large language model are different (that is, each weight in the first block uses scaling factor a111, and each weight in the second block uses scaling factor a222).
[0109] As an example, when a large language model contains 32 transformer decoder blocks, each block contains 5 quantizable weights, and different groups of each weight in each block use the same scaling factor, and different weights in each block use different scaling factors, the large language model will have 32*5=160 scaling factors (a011, a012, a013, a014, a015, a121, a022...a325, for details, please refer to Figure 3 In the original model shown, 160 weights correspond to one scaling factor each).
[0110] As another example, when a large language model contains 32 transformer decoder blocks, each block contains 5 quantizable weights, and different groups of weights in each block use the same scaling factor, and different weights in each block use the same scaling factor, the large language model will have 32 scaling factors (a1, a2, a3...a32, see Figure 3 In the large language model shown, each of the 32 blocks corresponds to a scaling factor).
[0111] The embodiments of the present application do not limit the specific method to be adopted.
[0112] S105, S104, input the business data into the large language model after quantization weights, and obtain the output Model_out_quant of the large language model after quantization weights.
[0113] It should be noted that in this step, the business data can be training data, test data or verification data when training a large language model, and of course, it can also be re-collected sample data.
[0114] The weight of the large language model contains the scaling factor a. Similarly, the output Model_out_quant of the large language model after quantization of the weights also contains a.
[0115] Of course, in practical applications, reference Figure 4 , different grouping sizes can get the corresponding Model_out_quant.
[0116] S105, inputting the business data into the large language model without quantized weights to obtain the output Model_out_fp of the large language model without quantized weights.
[0117] The business data in step S105 is the same as the business data in step S104.
[0118] S106 , obtaining a mean square error loss function Δloss=MSE(Model_out_fp, Model_out_quant) according to the output Model_out_fp of the large language model before quantization and the output Model_out_quant of the large language model after weight quantization.
[0119] In the embodiment of the present application, in theory, if the accuracy of the large language model after quantization weights is to be similar to the accuracy of the large language model before quantization, the mean square error loss function needs to converge or converge to 0.
[0120] According to the above description, it can be understood that the mean square error loss function carries an adaptive scaling factor a. That is, by continuously adjusting a, the mean square error loss function tends to converge or tends to converge to 0.
[0121] Of course, the mean square error loss function is only an example. In practical applications, the loss function can also be any function that can characterize the difference between the output Model_out_fp of the large language model before quantization and the output Model_out_quant of the large language model after quantization weights.
[0122] In this step, the mean square error loss function corresponding to each group size can be obtained, that is, Δloss16, Δloss32, Δloss64, and Δloss128 can be obtained.
[0123] S107, updating the scaling factor a according to the mean square error loss function.
[0124] S108, looping through S103 to S107 until the mean square error loss function converges when looping to S106, and obtaining a mean square error loss function that tends to converge under each grouping size.
[0125] In the embodiment of the present application, the mean square error loss function is considered to have converged after a certain number of loop executions.
[0126] It should be noted that the same business data is used to input the large language model before quantization and the large language model after quantization each time S103 to S107 are executed in a loop; different business data are used to input the large language model before quantization and the large language model after quantization between different loop times. That is, in the i-th loop, business data i is input into the large language model before quantization and the large language model after quantization; in the i+1-th loop, business data i+1 is input into the large language model before quantization and the large language model after quantization.
[0127] Reference Figure 4 As shown, in the embodiment of the present application, for each group size, the Δloss corresponding to the group size and the set of scaling factors (composed of multiple scaling factors) determined when the Δloss converges can be obtained. Accordingly, according to the multiple group sizes listed above, the following can be obtained: the set of scaling factors corresponding to Δloss16 (4*16), the set of scaling factors corresponding to Δloss32 (4*32), the set of scaling factors corresponding to Δloss64 (4*64), and the set of scaling factors corresponding to Δloss128 (4*128).
[0128] S109, quantizing each weight of each block in the large language model according to multiple scaling coefficients in the minimum mean square error loss function and corresponding grouping sizes.
[0129] In the embodiment of the present application, the Δloss corresponding to different group sizes are compared, and the a corresponding to the smallest Δloss when tending to converge is used as the final scaling factor a, and the group size corresponding to the smallest Δloss when tending to converge is used as the final group size.
[0130] B1. According to the determined scaling factor, calculate the scaling scale scale_bij=a_bi(X_bijmax-X_bijmin) / (2 N -1), where scale_bij represents the initial scaling scale of the jth group in the ith weight of the bth block, a_bi represents the scaling factor of the ith weight in the bth block, X_bijmax represents the maximum value of the element in the jth group of the ith weight in the bth block before quantization, X_bimin represents the minimum value of the element in the jth group of the ith weight in the bth block before quantization, and N is the quantization bit.
[0131] Of course, in practical applications, different scaling factors can be set for each group in each weight, and the scaling scale is updated to scale_bij=a_bij(X_bijmax-X_bijmin) / (2 N -1). Among them, scale_bij represents the scaling scale of the jth group of the ith weight in the bth block, a_bij represents the scaling factor of the jth group of the ith weight in the bth block. X_bijmax represents the maximum value of the jth group of the ith weight in the bth block before quantization, and X_bimin represents the minimum value of the jth group of the ith weight in the bth block before quantization.
[0132] B2. Calculate zero_bij according to the scaling scale, zero_bij = 0-round (X_bijmin / scale_bij) or zero_bij = 255-round (X_bijmax / scale_bij).
[0133] B3. Calculate the quantized value of the kth element in the jth group of the ith weight in the bth block according to the scaling scales scale_bij and zero_bij, X_bijkq=X_bijfloat / scale_bij +zero_bij.
[0134] Each element in each floating-point weight in the large language model is replaced with the quantized weight X_bijkq to obtain the large language model with quantized weights.
[0135] As another example of this application, refer to Figure 4 As shown in the figure, after the mean square error loss functions corresponding to each group size have converged, among the mean square error loss functions corresponding to multiple group sizes, the group size corresponding to the smallest mean square error loss function and the latest updated scaling factor, the latest large language model obtained is the large language model after the latest quantization weights in the cyclic training process.
[0136] In the embodiment of the present application, the scaling coefficient a of adaptive learning can control the abnormal outlier value of the weight from causing the scale to be abnormally large; through group quantization, the weights in different blocks can adopt different grouping strategies to improve the quantization accuracy.
[0137] In the above examples, quantizing a large language model using multiple grouping sizes and scaling factors is used as an example. In actual applications, it is also possible not to use multiple grouping sizes to quantize a large language model. For example, only one grouping size and scaling factor are used to quantize the large language model. When the mean square error loss function corresponding to this grouping size converges, the quantized large language model obtained by the corresponding scaling factor is recorded as the quantized large language model.
[0138] Of course, it is also possible to quantize the large language model using only the scaling factor without grouping, as described below:
[0139] Based on the set scaling factor, a large language model with quantized weights is obtained;
[0140] Inputting the first business data into the large language model after quantization of weights to obtain a first output;
[0141] Inputting the first business data into a large language model without quantized weights to obtain a second output;
[0142] Obtaining a difference loss function according to the first output and the second output;
[0143] The scaling coefficient is updated by the difference loss function, and the difference loss function is updated by the latest updated scaling coefficient until the difference loss function converges, and the large language model after quantization weight obtained by the corresponding scaling coefficient when the difference loss function converges is the quantized large language model.
[0144] As another embodiment of the present application, based on the set scaling factor, a large language model after quantization weights is obtained, including:
[0145] For the i-th weight of the b-th block in the large language model, where b is a natural number greater than or equal to 1, the maximum value of b is the number of blocks in the large language model, and i is a natural number greater than or equal to 1, the maximum value of i is the number of quantifiable weights in the b-th block in the large language model:
[0146] According to the scaling factor set for the ith weight of the bth block in the large language model, an initial scaling scale of the ith weight of the bth block is obtained, wherein the initial scaling scale of the ith weight of the bth block is related to the scaling factor set for the ith weight of the bth block, and a maximum element and a minimum element in the ith weight of the bth block;
[0147] According to each element in the ith weight of the bth block and the initial scaling scale of the ith weight of the bth block, the quantized value of each element in the ith weight of the bth block is obtained;
[0148] After quantizing each element of each weight of each block in the large language model, a large language model with quantized weights is obtained.
[0149] As another embodiment of the present application, the initial scaling scale of the i-th weight of the b-th block is:
[0150] scale_bi = a_bi (X_bimax - X_bimin) / (2N-1), where scale_bi represents the initial scaling scale of the ith weight of the bth block, a_bi represents the scaling factor set for the ith weight of the bth block, X_bimax represents the maximum element in the ith weight of the bth block, X_bimin represents the minimum element in the ith weight of the bth block, and N is the quantization bit;
[0151] The quantized value of the kth element in the ith weight of the bth block is:
[0152] X_bikq=X_bikfloat / scale_bi+zero_bi, where X_bikq represents the fixed-point value of the k-th element in the i-th weight of the b-th block, X_bikfloat represents the floating-point value of the k-th element in the i-th weight of the b-th block, and zero_bi=0-round(X_bimin / scale_bi, num_digits) or zero_bi =255-round(X_bimax / scale_bi, num_digits).
[0153] In the above example, different groups of different weights in the same block use the same scaling factor, different weights in the same block use different scaling factors, and different weights in different blocks use different scaling factors as examples. When a large language model contains 32 transformers decoder blocks, each block contains 5 quantifiable weights, and different groups of each weight in each block use the same scaling factor, and different weights in each block use different scaling factors, the large language model will have 32*5=160 scaling factors (a011, a012, a013, a014, a015, a121, a022...a325). Then 160 scaling factors need to be updated each time the scaling factor is updated.
[0154] The embodiment of the present application also provides another implementation method, that is, only the scaling coefficient of one weight is set each time, and other weights are not scaled, so as to train and obtain the scaling coefficient of the weight; the scaling coefficient of each weight is cyclically updated in this way, and after obtaining the scaling coefficient of each weight, a large language model with quantized weights is obtained through the obtained scaling coefficient of each weight.
[0155] Of course, it is also possible to set only the scaling factor of one weight and multiple grouping sizes each time, and not scale other weights, so as to train to obtain the optimal grouping size and scaling factor for the weight; in this way, the optimal grouping size and scaling factor of each weight are cyclically updated, and after obtaining the optimal grouping size and scaling factor of each weight, the large language model with quantized weights is obtained by obtaining the optimal grouping size and scaling factor of each weight.
[0156] It should be understood that the size of the serial numbers of the steps in the above embodiments does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present application.
[0157] The embodiment of the present application further provides a computer-readable storage medium, which stores a computer program. When the computer program is executed on an electronic device, the steps in the above-mentioned method embodiments can be implemented.
[0158] The embodiment of the present application also provides a computer program product. When the computer program product is run on an electronic device or a wireless router, the electronic device can implement the steps in the above-mentioned various method embodiments.
[0159] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the present application implements all or part of the processes in the above-mentioned embodiment method, which can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a computer-readable storage medium, and the computer program can implement the steps of the above-mentioned various method embodiments when executed by the processor. Among them, the computer program includes computer program code, and the computer program code can be in source code form, object code form, executable file or some intermediate form. The computer-readable medium may at least include: any entity or device that can carry the computer program code to the first device, a recording medium, a computer memory, a read-only memory (ROM, Read-Only Memory), a random access memory (RAM, Random Access Memory), an electric carrier signal, a telecommunication signal, and a software distribution medium. For example, a USB flash drive, a mobile hard disk, a magnetic disk or an optical disk. In some jurisdictions, according to legislation and patent practice, computer-readable media cannot be electric carrier signals and telecommunication signals.
[0160] The embodiment of the present application further provides a chip, the chip includes a processor, the processor is coupled to a memory, and the processor calls a computer program stored in the memory to implement the steps of any method embodiment of the present application. The chip can be a single chip or a chip module composed of multiple chips.
[0161] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described or recorded in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0162] Those of ordinary skill in the art will appreciate that the units and method steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.
[0163] The above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application 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. 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 application, and should all be included in the protection scope of the present application.
Claims
1. A quantization method for a large language model, characterized in that: include: Based on the set multiple group sizes and the set scaling factor, a large language model with quantized weights corresponding to each group size is obtained; Inputting first service data into a large language model after quantization weights corresponding to each group size, to obtain a first output corresponding to each group size, wherein the first service data is text data; Inputting the first business data into a large language model without quantized weights to obtain a second output; According to the first output and the second output corresponding to each grouping size, respectively, a difference loss function corresponding to each grouping size is obtained; For a first grouping size, a scaling factor corresponding to the first grouping size is updated by a difference loss function corresponding to the first grouping size, and the difference loss function corresponding to the first grouping size is updated by a newly updated scaling factor corresponding to the first grouping size until the difference loss function corresponding to the first grouping size converges. When the difference loss function corresponding to each grouping size converges, the large language model after quantization weights obtained by the grouping size and scaling factor corresponding to the minimum difference loss function is the quantized large language model, and the first grouping size is any one of the multiple grouping sizes set.
2. The method according to claim 1, characterized in that The updating of the scaling factor corresponding to the first grouping size by the difference loss function corresponding to the first grouping size, and the updating of the difference loss function corresponding to the first grouping size by the latest updated scaling factor corresponding to the first grouping size, until the difference loss function corresponding to the first grouping size converges, includes: Updating the scaling factor corresponding to the first grouping size to a first scaling factor by using a difference loss function corresponding to the first grouping size; Based on the first scaling factor, updating the large language model after quantization weights corresponding to the first grouping size; Inputting second service data into the large language model after the latest quantization weight corresponding to the first grouping size to obtain a third output corresponding to the first grouping size, wherein the second service data is text data; Inputting the second service data into the large language model without quantized weights to obtain a fourth output; updating a difference loss function corresponding to the first grouping size according to the third output and the fourth output corresponding to the first grouping size; If the latest updated difference loss function corresponding to the first group size does not converge, the scaling factor corresponding to the first group size continues to be updated to the second scaling factor according to the latest updated difference loss function corresponding to the first group size, and the difference loss function corresponding to the first group size is updated by the second scaling factor until the latest updated difference loss function corresponding to the first group size converges.
3. The method according to claim 1, characterized in that The difference loss function corresponding to the first grouping size is a mean square error loss function between an output of the large language model after quantization of the weights corresponding to the first grouping size and an output of the large language model without quantization of the weights.
4. The method according to any one of claims 1 to 3, characterized in that Based on the set multiple group sizes and the set scaling factor, a large language model with quantized weights corresponding to each group size is obtained, including: For the jth grouping of the ith weight of the bth block in the large language model, where b is a natural number greater than or equal to 1, the maximum value of b is the number of blocks in the large language model, i is a natural number greater than or equal to 1, the maximum value of i is the number of quantifiable weights in the bth block in the large language model, j is a natural number greater than or equal to 1, the maximum value of j is the number of groups of the ith weight of the bth block in the large language model: According to the scaling factor set for the i-th weight of the b-th block in the large language model, an initial scaling scale of the j-th group of the i-th weight of the b-th block is obtained, wherein the initial scaling scale of the j-th group of the i-th weight of the b-th block is related to the scaling factor set for the i-th weight of the b-th block, and a maximum element and a minimum element in the j-th group of the i-th weight of the b-th block; According to each element in the jth group of the ith weight of the bth block and the initial scaling scale of the jth group of the ith weight of the bth block, the quantized value of each element in the jth group of the ith weight of the bth block is obtained; After quantizing each element in each group of each weight of each block in the large language model, a large language model with quantized weights is obtained.
5. The method according to claim 4, characterized in that The initial scaling scale of the jth group of the ith weight of the bth block is: scale_bij=a_bi(X_bijmax-X_bijmin) / (2 N -1), where scale_bij represents the initial scaling scale of the jth group in the ith weight of the bth block, a_bi represents the scaling factor set for the ith weight of the bth block, X_bijmax represents the maximum element in the jth group of the ith weight of the bth block, X_bijmin represents the minimum element in the jth group of the ith weight of the bth block, and N is the quantization bit; The quantized values of each element in the jth group of the ith weight of the bth block are: X_bijkq=X_bijkfloat / scale_bij+zero_bij, where X_bijkq represents the fixed-point value of the k-th element in the j-th group of the i-th weight of the b-th block, X_bijkfloat represents the floating-point value of the k-th element in the j-th group of the i-th weight of the b-th block, and zero_bij=0-round(X_bijmin / scale_bij, num_digits) or zero_bij =255-round(X_bijmax / scale_bij, num_digits).
6. An electronic device, characterized in that: It comprises one or more processors and one or more memories; wherein, the one or more memories are coupled to the one or more processors, and the one or more memories are used to store computer programs, and when the one or more processors execute the computer programs, the electronic device executes the method as described in any one of claims 1-5.
7. A chip system, the chip system is applied to electronic equipment, the chip system comprises one or more processors, characterized in that: The processor is used to call computer instructions so that the electronic device executes the method as described in any one of claims 1-5.
8. A computer-readable storage medium comprising a computer program, characterized in that: When the computer program is executed on an electronic device, the electronic device is enabled to execute the method according to any one of claims 1 to 5.
