A model quantization method and apparatus, electronic device, program product, and medium
By quantizing the K and V matrices in the MLA architecture in different dimensions, especially by adopting a group quantization strategy, the problem of accuracy loss during model quantization is solved, and the efficiency of quantization processing is improved.
Patent Information
- Application Number
- CN202511231781.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Priority Date
- 2025-05-21
- Filing Date
- 2025-08-29
- Publication Date
- 2026-11-24
AI Technical Summary
In existing technologies, the multi-head latent attention mechanism (MLA) suffers from severe loss of model accuracy during model quantization, resulting in low quantization efficiency.
By decoupling the K matrix and the V matrix and quantizing them separately in different dimensions, specifically quantizing the K matrix in the first dimension and the V matrix in the second dimension, a group quantization strategy is adopted to adapt to the characteristics of different data and avoid errors caused by overall quantization.
It improves the efficiency of model quantization processing, reduces errors caused by quantization, avoids loss of model accuracy, and enhances quantization efficiency.
Smart Images

Figure CN123159408A_ABST
Abstract
Description
[0001] This application claims priority to Chinese Patent Application No. 202510666551.X, filed on May 21, 2025, entitled “A Model Quantization Method, Apparatus, and Device”, the entire contents of which are incorporated herein by reference. Technical Field
[0002] This application relates to the fields of artificial intelligence and machine learning technology, and in particular to a model quantization method, apparatus, electronic device, program product, and medium. Background Technology
[0003] With the widespread application of models across various fields and electronic devices, model development will become more diverse and practically oriented. This necessitates a wider range of deployment scenarios for models. Since the number of model parameters is often enormous, the hardware resources required in these scenarios are substantial. Therefore, higher demands are placed on both the deployment cost and the operational efficiency of models.
[0004] In related technologies, due to the special design of the shared storage space of the K / V cache in the multi-lead latent attention (MLA) mechanism, the model based on the MLA architecture suffers from severe loss of model accuracy during quantization, resulting in low efficiency of model quantization processing. Summary of the Invention
[0005] This application provides a model quantization method, apparatus, electronic device, program product, and medium that can improve the efficiency of model quantization processing.
[0006] Firstly, a model quantization method is provided, which is applied to an electronic device. The electronic device is equipped with a first model, which is a multi-head latent attention mechanism (MLA) architecture. The method includes: receiving an input sequence; processing the input sequence through the first model to generate a latent matrix; wherein the latent matrix is used to represent a compressed key-value (KV) matrix; the KV matrix is related to the input sequence; obtaining the K matrix and V matrix from the latent matrix; quantizing the K matrix in a first dimension; and quantizing the V matrix in a second dimension; wherein the first dimension and the second dimension are different.
[0007] Among them, the K matrix is different from the original K matrix obtained based on the input sequence, and the V matrix is different from the original V matrix obtained based on the input sequence.
[0008] As can be seen from the above, by decoupling the K matrix and V matrix under the MLA architecture and quantizing the K matrix and V matrix separately in different dimensions, it is possible to adapt to the characteristics of different data and avoid quantizing the latent matrix as a whole. This can reduce the error caused by quantization, avoid affecting the accuracy of the model, and further improve the efficiency of model quantization processing.
[0009] In one possible implementation, quantization of the K matrix in the first dimension includes: performing group quantization of the K matrix in the first dimension; group quantization involves dividing the data into blocks or groups and assigning independent quantization parameters to each block or group; quantization of the V matrix in the second dimension includes: performing group quantization of the V matrix in the second dimension.
[0010] As can be seen from the above, group quantization, through its "group-level" granularity, avoids the "one-size-fits-all" problem of global quantization and is simpler than element-wise quantization. It can be flexibly adjusted based on differences in numerical distribution, which not only reduces memory usage but also avoids excessive loss of model accuracy, thereby further improving quantization efficiency.
[0011] In one possible implementation, the inference process of the first model includes a pre-filling stage and a decoding stage, and performs second-dimensional group quantization on the V matrix, including: in the case of the pre-filling stage, obtaining the quantization parameters of each block or group in the V matrix, and performing second-dimensional group quantization on the V matrix based on the quantization parameters of each block or group; in the case of the decoding stage, performing second-dimensional group quantization on the V matrix based on the quantization parameters of each block or group.
[0012] As shown above, the decoding stage eliminates the need to recalculate the quantization parameters already processed in the pre-filling stage, avoiding redundant calculations. The decoding stage reuses the quantization parameters from the pre-filling stage, enabling faster decoding and reducing computation time. Furthermore, since the quantization parameters in the pre-filling stage are calculated based on the overall characteristics of the input sequence, reusing these parameters ensures consistency between the quantization method in the decoding stage and the pre-filling stage, thus avoiding performance fluctuations caused by differences in quantization parameters. Therefore, reusing the quantization parameter set improves overall inference efficiency.
[0013] In one possible implementation, the first dimension includes the token dimension; the second dimension includes the channel dimension.
[0014] As shown above, both per-token and per-channel quantization are fine-grained quantization strategies that can reduce memory usage while ensuring model accuracy. Specifically, each channel of the V matrix contains outliers, which can affect quantization accuracy. By using per-channel quantization on the V matrix, appropriate scaling can be applied based on the actual data conditions of each channel, resulting in more accurate quantization results.
[0015] In one possible implementation, the P matrix and / or Q matrix are quantized in a third dimension; the Q matrix is associated with the input sequence; the P matrix is associated with the input sequence; the quantization granularity of the third dimension includes any one of quantization granularity by word, by group, by channel, or by tensor.
[0016] Here, the Q matrix is the query matrix, whose generation logic depends entirely on the input sequence; the P matrix is the attention probability matrix, whose generation depends entirely on the Q matrix and K matrix derived from the input sequence; therefore, the Q matrix is related to the input sequence, and the P matrix is related to the input sequence.
[0017] It should be noted that in the attention calculation process of the MLA architecture, firstly, based on the obtained latent matrix, the inner product of the query vector Q and the key vector K is calculated, and the attention probability matrix P is obtained through the activation function (Softmax). Secondly, the attention probability matrix P is multiplied by the value vector V to obtain the attention output.
[0018] As shown above, quantizing the P matrix and / or Q matrix in the third dimension can further reduce the storage of intermediate results in model inference and improve the efficiency of model inference. Specifically, quantizing the P matrix can avoid dequantization in the K cache after matrix multiplication quantization, thereby improving the efficiency of model inference.
[0019] In one possible implementation, the K matrix includes low-rank coded data and position-coded data. The K matrix is quantized in the first dimension, which includes: quantizing the low-rank coded data in the first dimension; and not quantizing the position-coded data. The low-rank coded data represents the compressed semantics of the keys in the input sequence; and the position-coded data represents the position information of each token in the input sequence.
[0020] It should be noted that, as seen in the process of obtaining the cached latent vector from the input sequence, the discrete token sequence is transformed into an embedding vector representing high-dimensional semantics through the embedding layer of the first model. Simultaneously, to enable the model to perceive the order of each token, positional encoding is added to the aforementioned embedding vector. Since the K matrix is obtained from the latent matrix, the K matrix includes low-rank encoded data and positional encoded data.
[0021] The core of positional coding data is to encode the positional information of a sequence using trigonometric functions (sine / cosine). The rotation angle is related to the dimension index and the position index. When the sequence length is very long, the high-dimensional trigonometric function values may exhibit extreme values due to periodic superposition. Therefore, positional coding data suffers from severe outliers.
[0022] In addition, during quantization, the scale (scaling factor) and zero point need to be calculated based on the data distribution. If there are serious outliers, the scale will be increased by extreme values, causing normal values to be "overquantized".
[0023] As shown above, quantizing location-encoded data turns the originally continuous location information into discrete "jump values," and even causes RoPE features at different locations to be quantized into the same value, completely losing location distinguishability. Therefore, not quantizing location-encoded data with severe outliers can ensure the integrity of location information and avoid serious loss of model accuracy.
[0024] In one possible implementation, obtaining the K matrix and V matrix from the latent matrix includes: copying the latent matrix to obtain a first latent matrix; and obtaining the K matrix and V matrix based on the latent matrix and the first latent matrix.
[0025] As can be seen from the above, obtaining the K and V matrices by copying the latent matrix can improve the efficiency of decoupling the K and V matrices and facilitate the quantization of the K and V matrices.
[0026] Secondly, a model quantization device is provided. This application can divide the model quantization device into functional modules according to the method provided in the first aspect above. For example, each function can be divided into its own functional modules, or two or more functions can be inherited into a single processing module. For example, this application can divide the model quantization device into a receiving module, a processing module, and a quantization module according to their functions. The descriptions of the possible technical solutions and beneficial effects of the various functional modules described above can be found in the technical solutions provided in the first aspect above or its corresponding possible implementations, and will not be repeated here.
[0027] Thirdly, this application provides an electronic device including a processor and a memory for storing processor-executable instructions; the processor is configured to execute instructions such that the electronic device performs the model quantization method described in the first aspect.
[0028] Fourthly, this application provides a computer-readable storage medium storing at least one computer program, which is loaded and executed by a processor to implement the model quantization method as described in the first aspect above.
[0029] Fifthly, this application provides a computer program product or computer program including computer instructions stored in a computer-readable storage medium. A processor of an electronic device reads the computer instructions from the computer-readable storage medium and executes the computer instructions, causing the electronic device to perform the model quantization method provided in the various optional implementations of the first aspect described above.
[0030] For a detailed description of the second to fifth aspects and their various implementations in this application, please refer to the detailed description in the first aspect and its various implementations; and for a detailed description of the beneficial effects of the second to fifth aspects and their various implementations, please refer to the beneficial effect analysis in the various implementations of the first aspect, which will not be repeated here.
[0031] These or other aspects of this application will become more readily apparent in the following description. Attached Figure Description
[0032] Figure 1 A schematic diagram of the architecture of a large model provided by related technologies is shown;
[0033] Figure 2 The illustration shows a scenario diagram of a model application provided in an embodiment of this application;
[0034] Figure 3 A hardware schematic diagram of an electronic device 100 provided in an embodiment of this application is shown;
[0035] Figure 4 A schematic flowchart of a model quantization method provided in an embodiment of this application is shown;
[0036] Figure 5 This illustration shows a schematic diagram of an attention calculation process provided in an embodiment of this application;
[0037] Figure 6 A schematic diagram of the structure of a model quantization device 400 provided in an embodiment of this application is shown. Detailed Implementation
[0038] The technical solutions of the embodiments of this application will be described below with reference to the accompanying drawings. In the description of this application, unless otherwise stated, " / " indicates that the objects before and after are in an "or" relationship. For example, A / B can represent A or B. "And / or" in this application is merely a description of the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A alone, A and B simultaneously, and B alone, where A and B can be singular or plural. Furthermore, in the description of this application, unless otherwise stated, "multiple" refers to two or more. "At least one of the following" or similar expressions refer to any combination of these items, including any combination of single or plural items. For example, at least one of a, b, or c can represent: a, b, c, ab, ac, bc, or abc, where a, b, and c can be single or multiple. In addition, in order to clearly describe the technical solutions of the embodiments of this application, the terms "first" and "second" are used in the embodiments of this application to distinguish the same or similar items with basically the same function and effect.
[0039] Those skilled in the art will understand that the terms "first," "second," etc., do not limit the quantity or order of execution, and that "first," "second," etc., are not necessarily different. Furthermore, in some embodiments of this application, words such as "exemplary" or "for example" are used to indicate that something is being described as an example, illustration, or description. Any embodiment or design scheme described as "exemplary" or "for example" in the embodiments of this application should not be construed as being more preferred or advantageous than other embodiments or design schemes. Specifically, the use of words such as "exemplary" or "for example" is intended to present the relevant concepts in a concrete manner for ease of understanding.
[0040] Furthermore, the device architecture and business scenarios described in the embodiments of this application are for the purpose of more clearly illustrating the technical solutions of the embodiments of this application, and do not constitute a limitation on the technical solutions provided in the embodiments of this application. As those skilled in the art will know, with the evolution of device architecture and the emergence of new business scenarios, the technical solutions provided in the embodiments of this application are also applicable to similar technical problems.
[0041] First, let me introduce the relevant terms and technologies involved in this application:
[0042] Transformer architecture: This is a neural network structure based on an attention mechanism, primarily used for natural language processing tasks. The transformer architecture consists of two parts: an encoder and a decoder. The encoder is responsible for converting the input sequence into a continuous vector representation, while the decoder generates the next output based on the encoder's output and the already generated output sequence.
[0043] The core role of Q, K, and V in Transformer architecture inference: In the Transformer architecture, the core of the inference process is the self-attention mechanism, and Q, K, and V are the foundation for implementing attention computation.
[0044] Here, Q (Query) represents the "query vector" of the current input sequence. It is a vector generated by the model for the "input information at the current position" and is used to "actively retrieve" relevant information. Q is obtained by mapping the current input tokens (such as words in text, pixel blocks in images, etc.) through a linear layer (Query weight matrix).
[0045] K(Key) represents the feature vector of each position in the input sequence. K is obtained by mapping the tokens of all positions in the input sequence (including the current position and other positions) through a linear layer (Key weight matrix). K is the "index information" used for the "query" and forms a "query-key" matching relationship with Q.
[0046] V (Value) represents the "actual content vector at each position" in the input sequence, which is the "specific information to be extracted after being focused on". V is obtained by mapping the tokens at all positions in the input sequence through a linear layer (Value weight matrix), and it corresponds one-to-one with K (K and V at the same position come from the same token, but are generated through different weight matrices). After calculating the similarity between Q and K (attention weights), V is multiplied by these weights and summed to obtain the "comprehensive information that needs to be focused on at the current position" (attention output). It can be understood that K is like a "term" in a dictionary, Q is the user's "search term", and the attention weights represent the degree of matching between the search term and each term.
[0047] The relationship between Q, K, and V can be simplified as follows: Q finds K to determine "who to follow", then V aggregates the "content to follow", and finally generates an output that integrates relevant information.
[0048] Example: In machine translation, when translating the word "apple," the Q vector represents the "apple" that needs to be translated and matches it with information related to "apple" in the input sequence (such as whether the context refers to fruit or company). Each word in the input sequence generates a corresponding K vector. When Q is "apple," the K vectors of related words such as "fruit" and "company" will have a high similarity to Q. If the Q of "apple" has a high similarity to the K of "fruit," then the V vector corresponding to "fruit" will be given a higher weight, and the final attention output will be more biased towards information related to "fruit."
[0049] P (Probability, Attention Probability): This typically refers to the "output probability distribution" of the model's inference, represented as a floating-point vector. It represents the model's predicted probability for the "next possible token (or category)". P is calculated through layers such as the attention mechanism and feedforward network. The final output is mapped to the vocabulary (or category space) via a linear layer (often called the "output projection layer"), and then normalized by an activation function (Softmax function).
[0050] Example: In text generation, when the model generates "I want to eat", the output P might be "apple" (60%), "banana" (30%), "rice" (10%), and finally "apple" might be selected as the next word.
[0051] The inference process of the attention mechanism is as follows: 1. Generate Q, K, and V based on the tokens in the input sequence; 2. Calculate the attention weights for Q and K (to determine which positions to focus on); 3. Sum the weights with V to obtain the attention output; 4. After processing by subsequent layers, the output is finally mapped to the output probability distribution P, which is used to generate the next output token or predict the category.
[0052] Multi-head attention (MHA) is the core of the transformer architecture. By decomposing attention computation into multiple "heads," the model can simultaneously focus on different parts of the input sequence, thereby capturing richer semantic information. Its core idea is to project features from the input sequence onto multiple low-dimensional subspaces (each subspace is called a "head"), with each head independently computing attention, and finally concatenating and integrating the results.
[0053] Multi-lead latent attention (MLA) is an improved version of MHA in the transformer architecture. MLA uses a mapping mechanism to compress the key-value representations of all attention heads into a smaller latent vector, with the key-value cache sharing the same storage space. During inference, only this latent vector needs to be cached, and multiple attention heads share this compressed set of key-value pair information, thus significantly reducing the storage requirements for key-value pairs. For example, a low-dimensional latent variable C can be introduced to replace the complete key-value storage. When calculating attention, the key and value are then reconstructed from C.
[0054] Large language models (LLMs) are deep learning models trained on large amounts of text data that can generate natural language text or understand the meaning of language text. LLMs can handle various natural language tasks, such as text classification, question answering, and dialogue, and also show potential in handling complex tasks such as code generation and vulnerability detection. In this paper, the term "large language model" may also be used. In some examples, LLMs can be trained using generative deep neural network models based on transformer structures. Commonly used edge-side large language models include, but are not limited to: transformer-based bidirectional encoder representation (BERT) models, transformer-based decoder representation (GPT) models, transformer-based text-to-text-transfer-transformer (T5) models, models combining autoregression and autoencoders (XLNet), transformer-based Deepseek-V3, or transformer-based Deepseek-R1 models. This application does not limit the specific type of large language model.
[0055] Model quantization is a technique that improves inference speed by reducing the precision of parameters and computations in a neural network. Its core lies in converting parameters and operations represented by high-precision floating-point numbers into a low-precision format. Essentially, it maps continuous floating-point numbers to a finite space of discrete integers. Quantization types include high-precision quantization, integer quantization, and binarization / ternarization. Specifically, high-precision quantization can be half-precision floating-point quantization (FP16), using 16 bits to represent the value, including 1 sign bit, 5 exponent bits, and 10 mantissa bits, with a dynamic range of approximately ±65504.
[0056] Integer quantization can specifically be 8-bit quantization (INT8), 4-bit quantization (INT4), or INT2. INT8 represents an 8-bit signed integer, ranging from [-128, 127] to [0, 255]. Mapping FP32 to INT8 typically compresses memory by 4 times. Similarly, INT4 ranges from [-8, 7] with an 8x compression ratio, and INT2 ranges from [-2, 1] with a 16x compression ratio. Binarization quantizes parameters or activation values into only two possible values (usually -1 and 1, or 0 and 1), requiring only 1 bit of storage for each parameter. Ternarization quantizes parameters into three values (usually -1, 0, and 1), requiring 2 bits of storage for each parameter (retaining more information compared to binarization).
[0057] In large model quantization, quantization dimensions include per-tensor quantization, per-token quantization, per-channel quantization, and per-group quantization.
[0058] Per-tensor quantization uses the same set of quantization parameters (scale and zero-point) to quantize the entire tensor, making it the coarsest quantization method.
[0059] Per-group is a fine-grained quantization method. Per-group divides the data into blocks or groups and assigns independent quantization parameters, such as scaling factors and zero points, to each block or group to better adapt to the local characteristics of the data and reduce quantization errors.
[0060] Per-channel assigns independent quantization parameters to each output channel for quantization, a fine-grained quantization strategy between per-tensor and per-group.
[0061] Per-token refers to quantizing each token in the input sequence using a separate set of quantization parameters, which is a fine-grained quantization strategy.
[0062] like Figure 1 As shown, a large model typically includes an input layer, multiple transformer layers, a normalization layer, a linear layer, and an output layer. In some alternative approaches, the transformer layer may be called a processing layer, a data processing layer, or other names, while the input layer, normalization layer, linear layer, and output layer may be called network layers.
[0063] The input layer encodes the input sequence to obtain an input vector. For example, it converts words in the input sequence into word vectors, images into image encoding vectors, or speech into word vectors. In some optional methods, the input layer can also obtain the positional encoding vector of each word (pixel or image patch) in the input sequence, concatenating the positional encoding vector and the input vector as the input to the larger model. The input vector can also be called an input sequence vector, an input vector sequence, or other names; this application does not limit its usage. Furthermore, the positional encoding vector can also be called a position vector or positional encoding; this application does not limit its usage either.
[0064] Multiple transformer layers are used to extract features from the output of the input layer and obtain contextual information to obtain the feature representation of the input sequence, and to perform inference calculations based on the feature representation of the input sequence to obtain the inference representation result of the input sequence.
[0065] The inference representation result can refer to the output of any one of the multiple transformer layers, or the output of the last transformer layer. This inference representation result is used to generate the inference result. This inference representation result can include, but is not limited to, text features, image features, or audio features.
[0066] The inference result indicates the output of the model after processing the input sequence. This inference result can include, but is not limited to: text, query results, generated multimodal content, graphs, tables, audio, and video. Multimodal content includes: images generated from text, translated text, videos generated from text, and style-transformed images.
[0067] In some alternative methods, such as Figure 1 As shown, the multiple transformer layers include L transformer layers: transformer layer 1, transformer layer 2, ..., and transformer layer L. These L transformer layers are executed sequentially; that is, the output of transformer layer 1 serves as the input of transformer layer 2, the output of transformer layer L-1 serves as the input of transformer layer L, and the output of transformer layer L serves as the inference representation of the input sequence. Here, L is a positive integer greater than 2.
[0068] In some alternative approaches, the residual connections between L transformer layers ( Figure 1 (not shown in the diagram), meaning the output of transformer layer 1 is used as the input of transformer layer 2, and the output of transformer layer 1 is used as the input of transformer layer j. The input of transformer layer j is the output of transformer layer j-1 and the output of transformer layer 1, thus increasing the number of transformer layers. Here, j is a positive integer greater than 2 and less than or equal to L.
[0069] The normalization layer normalizes the outputs of the L transformer layers (i.e., the inference representations of the input sequences). The output of the normalization layer is then input into the linear layer for feature processing. The output of the linear layer is input into the output layer, which calculates the inference results of the large model.
[0070] The input sequence is an ordered collection of elements, where each element has a unique position. Elements in the input sequence can be of any type, such as numbers, strings, objects, arrays, tuples, linked lists (or lists), words, stacks, and queues. Strings are sequences of characters commonly used in text processing, cryptography, and image processing. Arrays are sequences of elements of the same type, often used for storing large amounts of data and performing numerical calculations. Linked lists are sequences of nodes, often used to implement dynamic data structures and efficient insertion and deletion operations. Tuples are immutable sequences, often used to group multiple values into a single unit. Lists are mutable sequences, often used for storing and manipulating data. Stacks and queues are special types of sequences commonly used to implement data structures and algorithms.
[0071] Multiple transformer layers can have different structures, or they can have the same structure. Alternatively, at least two transformer layers can have different structures.
[0072] The above Figure 1 The structure of the large model shown is for illustrative purposes only. In practical applications, the large model may have other structures. For example, the large model may also include an encoder and a decoder. The encoder is used to extract features from the input sequence, and the decoder is used to perform inference based on the extracted features to obtain the inference result.
[0073] The application scenarios of the embodiments of this application will be described below by way of example.
[0074] Figure 2 This illustration shows a scenario diagram of a model application provided by an embodiment of this application. For example... Figure 2As shown, electronic device 100 is used to run model A; model A is a quantized model, specifically including the quantization of intermediate values generated during the inference process. The user inputs an input sequence to model A, and model A performs inference based on the input sequence. During inference, model A quantizes the intermediate values generated during the inference process. Model A determines the inference result based on the quantized intermediate values and outputs it through electronic device 100. The user can send the input sequence to electronic device 100 via external devices such as a keyboard; the user can also send the input sequence via voice input. The specific method used by the user to send the input sequence to electronic device 100 depends on the usage scenario of electronic device 100 and is not specifically limited.
[0075] The aforementioned model A can specifically be an MLA architecture. However, since the key-value cache in an MLA architecture is a shared memory space, it can only be quantized using tensor dimensions. Quantization using tensor dimensions is the coarsest-grained quantization method, which poses a significant challenge to model accuracy, leading to a decrease in model precision. This results in low efficiency in model quantization processing.
[0076] In view of this, embodiments of this application provide a model quantization method applied to an electronic device. The electronic device deploys a first model, which is an MLA architecture. Based on the latent matrix obtained from the input sequence using the first model, the electronic device obtains a K matrix and a V matrix, and then quantizes the K matrix and V matrix separately in different dimensions. This method decouples the K matrix and V matrix and quantizes them separately in different dimensions, adapting to the characteristics of different data and avoiding overall quantization of the latent matrix. This reduces quantization errors, prevents impact on model accuracy, and further improves the efficiency of model quantization processing.
[0077] It should be noted that the aforementioned model can be a large-scale model proposed by current AI technology, or it can be a large-scale model proposed by future AI technology. Accordingly, the technical solution provided in this application is applicable not only to current model inference and distributed inference scenarios, but also to future model inference, training, distributed inference, and distributed training technologies. Alternatively, the technical solution provided in this application can also be applied to the runtime scenarios of AI applications, video playback applications, or game applications.
[0078] The system architecture of the embodiments of this application will be described below by way of example.
[0079] Figure 3This illustration shows a hardware schematic of an electronic device 100 provided in an embodiment of this application. The electronic device 100 is used to run a first model, which is a Multi-Head Latent Attention (MLA) architecture. The electronic device 100 includes a server or terminal device. Examples include general-purpose servers, mobile phones, smartwatches, tablets, foldable electronic devices, desktop computers, laptops, handheld computers, laptops, ultra-mobile personal computers (UMPCs), netbooks, cellular phones, personal digital assistants (PDAs), augmented reality (AR) devices, virtual reality (VR) devices, artificial intelligence (AI) devices, wearable devices, in-vehicle devices, etc., and can also be edge devices (e.g., boxes carrying processing chips). Figure 3 As shown, the electronic device 100 includes a processor 110, a memory 120, a communication interface 130, and a bus 140. The processor 110, memory 120, and communication interface 130 are connected via the bus 140. It should be understood that this application does not limit the number of processors 110 and memory 120 in the electronic device 100.
[0080] Specifically, processor 110 can be a central processing unit (CPU), a graphics processing unit (GPU), or a neural processing unit (NPU). Processor 110 receives an input sequence; processes the input sequence using a first model to generate a latent matrix; wherein the latent matrix represents the compressed key-value (KV) matrix; the KV matrix is related to the input sequence; obtains the K matrix and V matrix from the latent matrix; quantizes the K matrix in a first dimension; and quantizes the V matrix in a second dimension; wherein the first dimension and the second dimension are different.
[0081] Memory 120 is used to store processor-executable instructions. Memory 120 can be a data-preserving storage space that does not lose data when power is off, specifically non-volatile RAM (NVRAM), electrically erasable programmable read-only memory (EEPROM), etc. Communication interface 130 is used to realize data transmission and communication between the electronic device and other devices.
[0082] Optionally, the electronic device 100 also includes a preprocessing module and a postprocessing module. The preprocessing module receives the input sequence of natural language and sequentially converts the natural language input sequence into multiple tokens; then sends the multiple tokens to the model. The postprocessing module converts the generated tokens back into natural language and outputs them.
[0083] The pre-processing module and post-processing module can be software, hardware, or a combination of both. If they are software, they can run on the CPU or NPU. The pre-processing and post-processing modules can also be integrated into the model; there are no specific limitations on their form.
[0084] It should be noted that the system architecture and application scenarios described in the embodiments of this application are for the purpose of more clearly illustrating the technical solutions of the embodiments of this application, and do not constitute a limitation on the technical solutions provided in the embodiments of this application. As those skilled in the art will know, with the evolution of system architecture and the emergence of new business scenarios, the technical solutions provided in the embodiments of this application are also applicable to similar technical problems.
[0085] The model quantization method provided in this application is described below with reference to the accompanying drawings. This model quantization method is applicable to... Figure 3 The electronic device 100 shown.
[0086] Figure 4 This document illustrates a flowchart of a model quantization method provided in an embodiment of this application. Figure 4 As shown, the model quantization method includes the following steps.
[0087] S101, the electronic device receives the input sequence and processes it using the first model to generate a latent matrix. The latent matrix represents the compressed KV matrix; the KV matrix is related to the input sequence.
[0088] For example, the first model is an MLA architecture; specifically, the first model can be a large language model based on the MLA architecture. The input sequence is an ordered set of elements, each with a unique position. The latent matrix includes low-rank compressed original K vectors and original V vectors. The original K vectors and original V vectors are obtained by the model based on the input sequence. Here, the K vector refers to the key representation of a single token; the K matrix refers to the key representation of all tokens in the entire input sequence. The V vector is analogous to the V matrix, and the Q vector and Q matrix are analogous. The specific process of generating the latent matrix is as follows.
[0089] First, the first model segments the input sequence into an ordered, discrete sequence of tokens using a token segmenter. This discrete token sequence is then transformed into embedding vectors representing high-dimensional semantics through the embedding layer of the first model. Simultaneously, to enable the model to perceive the order of the tokens, positional encoding is added to these embedding vectors. Common methods include sine and cosine encoding, ultimately resulting in feature vectors containing positional information.
[0090] Secondly, based on the aforementioned feature vectors, the first model generates the original query vector (Q), key vector (K), and value vector (V) through a linear layer.
[0091] Finally, the first model performs low-rank compression using a mapping matrix, mapping all the original K and V vectors in the input sequence to a low-dimensional latent space to obtain the latent matrix. This latent matrix is then cached in a memory buffer for subsequent attention calculations in the first model. Therefore, the latent matrix can represent the compressed KV matrix.
[0092] For example, the input sequence could be "I love natural language processing". First, a word segmenter divides "I love natural language processing" into ordered discrete tokens. The segmentation result is ["I", "love", "natural", "language", "processing"]. Using a vocabulary, the segmentation result is mapped to an integer sequence [342, 56, 1023, 789, 2310]. As shown above, the input sequence has a length of 5, and each token corresponds to a unique integer address. The embedding layer of the first model converts these integer addresses into a high-dimensional embedding matrix. Specifically, the integer address of each token is used to look up the embedding vector representing the semantics through the embedding mapping matrix, ultimately obtaining the embedding matrix of the input sequence. Simultaneously, sine and cosine positional encoding is used to inject positional information into the embedding vector. By fusing semantics and position, the feature matrix of the input sequence is obtained. Next, three independent linear layers convert the feature matrix into the original K, V, and Q matrices.
[0093] Finally, the original K and V matrices are low-rank compressed using the first model to generate a latent matrix, which is then stored in a memory buffer. Specifically, MLA designs dedicated low-rank projection matrices for K and V (e.g., compression dimension (dc = 32, i.e., from 64 to 32) as shown below:
[0094] (Low-rank projection matrix of the key)
[0095] (Low-rank projection matrix of the value)
[0096] The following latent matrix can be obtained from the above low-rank projection matrix:
[0097]
[0098] After splicing the multi-head potential matrices, they are integrated through a fusion matrix to finally obtain the global potential matrix.
[0099] As shown above, the latent matrix is a simplified representation of the original K and V matrices obtained by compression, retaining the core semantic association and positional information with the input sequence, and cached as a K / V cache. The K / V cache can be directly used for subsequent attention calculations in the first model, and is the core mechanism for achieving efficient attention calculations and accelerating sequence reasoning in the MLA architecture.
[0100] S102, the electronic device obtains the K matrix and V matrix from the potential matrix.
[0101] The K matrix and V matrix are different from the original K matrix and original V matrix in S101 above.
[0102] In one possible implementation, the latent matrix is copied to obtain the first latent matrix; based on the latent matrix and the first latent matrix, the K matrix and the V matrix are obtained.
[0103] For example, the latent matrix is determined as a K matrix and the first latent matrix is determined as a V matrix; or, the latent matrix is determined as a V matrix and the first latent matrix is determined as a K matrix.
[0104] For example, an electronic device caches the latent matrix in a memory buffer, forming a key-value (K / V) cache. The electronic device obtains two memory buffers by copying the K / V cache. One of these memory buffers is used as the K cache to store the K matrix, and the other is used as the V cache to store the V matrix.
[0105] S103, the electronic device performs first-dimensional quantization on the K matrix and second-dimensional quantization on the V matrix. The first and second dimensions are different.
[0106] Different matrices have different data distribution characteristics. Combining the data distribution characteristics of each matrix with different dimensions of quantization can make the quantized results more accurate and maintain the overall accuracy of attention calculation while ensuring the calculation speed.
[0107] For example, the first dimension includes the per-token dimension; the second dimension includes the per-channel dimension.
[0108] Per-token and per-channel quantization are both fine-grained quantization strategies that can reduce memory usage while ensuring model accuracy. Specifically, each channel of the V matrix contains outliers that can affect quantization accuracy. By using per-channel quantization on V, appropriate scaling can be applied based on the actual data conditions of each channel, resulting in more accurate quantization results.
[0109] In one possible implementation, quantization of the K matrix in the first dimension includes: performing group quantization of the K matrix in the first dimension; group quantization involves dividing the data into blocks or groups and assigning independent quantization parameters to each block or group; quantization of the V matrix in the second dimension includes: performing group quantization of the V matrix in the second dimension.
[0110] For example, the K matrix is grouped according to the token dimension, with multiple tokens forming a group (e.g., three tokens per group). The electronic device assigns independent quantization parameters (scaling factor and zero point) to each group based on its data distribution characteristics. These data distribution characteristics specifically include normal distribution, uniform distribution, or long-tailed distribution. Other distribution characteristics may include data continuity, extreme values (maximum and / or minimum values), or sparsity.
[0111] For example, in each token vector (e.g., d_model = 768 dimensions), the numerical distributions of different dimensions are not completely independent; there are usually several "semantically related dimension clusters." Group quantization can group semantically / distributively similar dimensions together, making the numerical range of elements within a group more concentrated and avoiding the loss of accuracy caused by global quantization (e.g., a single group per token). Therefore, group quantization along the token dimension can adapt to the local distribution characteristics of the token vector and improve quantization accuracy.
[0112] For example, multiple channels in the V matrix are grouped together, and the electronic device assigns independent quantization parameters based on the data distribution characteristics of each group of data.
[0113] As shown above, the data distribution of different channels may differ, and some channels may have large outliers. By performing grouped quantization along the channel dimension, channels with similar data distributions can be grouped together, and then independent quantization parameters can be set for each group. This can better adapt to the data characteristics of different channels, reduce the impact of outliers on quantization accuracy, and thus improve the overall quantization accuracy.
[0114] In one possible implementation, the inference process of the first model includes a pre-filling stage and a decoding stage, and performs second-dimensional grouping quantization on the V matrix, including:
[0115] In the pre-filling stage, the electronic device acquires the quantization parameters of each block or group in the V matrix, and performs group quantization of the V matrix in the second dimension based on the quantization parameters of each block or group.
[0116] During the decoding phase, the electronic device performs second-dimensional group quantization on the V matrix based on the quantization parameters of each block or group.
[0117] For example, in the pre-filling stage, the data distribution characteristics of each channel of the V matrix are captured in real time, and the quantization parameters for each channel or group are dynamically generated to obtain a quantization parameter set. In the decoding stage, this quantization parameter set is reused to quantize the V matrix.
[0118] As shown above, the decoding stage eliminates the need to recalculate the quantization parameters already processed in the pre-filling stage, avoiding redundant calculations. The decoding stage reuses the quantization parameters from the pre-filling stage, enabling faster decoding and reducing computation time. Furthermore, since the quantization parameters in the pre-filling stage are calculated based on the overall characteristics of the input sequence, reusing these parameters ensures consistency between the quantization method in the decoding stage and the pre-filling stage, thus avoiding performance fluctuations caused by differences in quantization parameters. Therefore, reusing the quantization parameter set improves overall inference efficiency.
[0119] In one possible implementation, the K matrix includes low-rank coded data and position-coded data. Quantization of the K matrix in its first dimension includes: quantizing the low-rank coded data in its first dimension; and not quantizing the position-coded data.
[0120] Among them, low-rank encoded data represents the compressed semantics of the keys in the input sequence; position encoded data represents the positional information of each token in the input sequence.
[0121] For example, as seen in S101 above, where the process of obtaining the cached latent vector from the input sequence shows, the discrete token sequence is converted into an embedding vector representing high-dimensional semantics through the embedding layer of the first model. Simultaneously, to enable the model to perceive the order of each token, positional encoding is added to the aforementioned embedding vector. Since the K matrix is obtained from the latent matrix, the K matrix includes low-rank encoded data and positional encoded data.
[0122] In model inference, the core role of encoding is to transform the input sequence into a vector carrying semantic, positional, and contextual information, providing the foundation for the model's attention computation. Rotary position embedding (RoPE) is an advanced positional encoding technique used in Transformer models to capture sequence positional information. No position embedding (NoPE) is an encoding method that does not introduce independent explicit vectors in the Transformer model. Low-rank encoded data includes data encoded by NoPE; positionally encoded data includes data encoded by RoPE. Since RoPE's core function is to encode sequence positional information using trigonometric functions (sine / cosine), and its rotation angle is related to the dimension index and position index, when the sequence length is very long (e.g., exceeding 10k tokens), the high-dimensional trigonometric function values may exhibit extreme values due to periodic superposition. Therefore, RoPE encoding suffers from significant outliers.
[0123] As shown above, quantization requires calculating the scale (scaling factor) and zero point based on the data distribution. If severe outliers are present, the scale will be inflated by extreme values, causing normal RoPE values to be "over-quantized." This turns originally continuous location information into discrete "jump values," and even RoPE features from different locations may be quantized to the same value, completely losing location distinguishability. Therefore, location-encoded data with severe outliers should not be quantized to ensure the integrity of location information and avoid severe loss of model accuracy.
[0124] (Optional) S104, the electronic device performs third-dimensional quantization on the P matrix and / or Q matrix. The Q matrix is related to the input sequence; the P matrix is related to the input sequence; the third-dimensional quantization granularity includes any one of word-based quantization, group-based quantization, channel-based quantization, or tensor-based quantization.
[0125] For example, the P matrix is quantized in terms of the per-token dimension.
[0126] The attention calculation process in the MLA architecture includes: First, based on the obtained latent matrix, the inner product of the query vector Q and the key vector K is calculated, and the attention probability matrix P is obtained through the activation function (Softmax). Second, the attention probability matrix P is multiplied by the value vector V to obtain the attention output.
[0127] The V matrix after per-channel quantization is typically a low-bit integer, and the P matrix after per-token quantization is also typically a low-bit integer. In this case, matrix multiplication of the P and V matrices does not require dequantization of the V matrix. However, if the P matrix is not quantized, it is typically a floating-point number, in which case dequantization of the quantized V matrix is necessary.
[0128] For example, quantizing the Q matrix using the per-token dimension. Figure 5 A schematic diagram illustrating an attention calculation process provided in an embodiment of this application is shown. Figure 5 As shown, in the first model, the P matrix is obtained through the activation function (Softmax). Figure 5 Before (not shown), the electronic device acquires the K / V cache and copies it, resulting in the K cache and V cache respectively. The K cache is quantized per-token to obtain the quantized K'cache; the V cache is quantized per-channel to obtain the quantized V'cache.
[0129] Calculate the inner product of the quantized K'cache and the quantized Q matrix (Q cache), and obtain the P matrix through the activation function. Figure 5 (Not shown). Quantize the P matrix according to the per-token dimension to obtain P'cache. Perform matrix multiplication between P'cache and V'cache to obtain the attention output ( Figure 5 (Not shown). P'cache and V'cache have the same quantization precision (e.g., INT8).
[0130] Optionally, after performing matrix multiplication between P'cache and V'cache, the results are calibrated using the fused quantization parameters (the scale of P and the per-channel scale of V'cache) to finally obtain the attention output.
[0131] As shown above, quantizing the P matrix and / or Q matrix in the third dimension can further reduce the storage of intermediate results in model inference and improve the efficiency of model inference. Specifically, quantizing the P matrix can avoid dequantization in the K cache after matrix multiplication quantization, thereby improving the efficiency of model inference.
[0132] In summary, this application provides a model quantization method to improve the efficiency of model quantization processing. The method is applied to an electronic device, which deploys a first model, which is a Multi-Head Latent Attention (MLA) architecture. By decoupling the K and V matrices in the MLA architecture and quantizing them separately in different dimensions, the method can adapt to the characteristics of different data and perform quantization in different dimensions, avoiding overall quantization of the latent matrix. This reduces quantization errors and improves the efficiency of model quantization processing.
[0133] The foregoing primarily describes the solutions of the embodiments of this application from a methodological perspective. It is understood that, in order to implement the functions in the aforementioned model quantization method, the model quantization device includes at least one of the hardware structures and software modules corresponding to each function. Those skilled in the art should readily recognize that, in conjunction with the units and algorithm steps of the various examples described in the embodiments disclosed herein, the embodiments of this application can be implemented in hardware or a combination of hardware and computer software. Whether a function is executed in hardware or by computer software driving hardware depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of the embodiments of this application.
[0134] This application embodiment can divide the model quantization device into functional units according to the above method example. For example, each function can be divided into a separate functional unit, or two or more functions can be integrated into one processing unit. The integrated unit can be implemented in hardware or as a software functional unit. It should be noted that the unit division in this application embodiment is illustrative and only represents one logical functional division. In actual implementation, there may be other division methods.
[0135] For example, Figure 6 A schematic diagram of the structure of a model quantization device 400 provided in an embodiment of this application is shown. Figure 6 As shown, this model quantization device can be applied in electronic devices. The model quantization device 400 includes:
[0136] Receiver module 410 is used to receive the input sequence;
[0137] Processing module 420 is used to process the input sequence using the first model to generate a latent matrix; wherein the latent matrix is used to represent the compressed key-value (KV) matrix; the KV matrix is related to the input sequence; and the K matrix and V matrix are obtained from the latent matrix; wherein the receiving module 410 and processing module 420 are specifically used to implement Figure 4 The methods corresponding to S101 and S102.
[0138] Quantization module 430 is used to quantize the K matrix in the first dimension and the V matrix in the second dimension; the first and second dimensions are different. Specifically, quantization module 430 is used to implement... Figure 4 The methods corresponding to S103 and S104.
[0139] In one possible implementation, the quantization module 430 is further used to perform first-dimensional grouped quantization on the K matrix; grouped quantization involves dividing the data into blocks or groups and assigning independent quantization parameters to each block or group; and performing second-dimensional quantization on the V matrix, including:
[0140] Perform grouping quantization on the V matrix in the second dimension.
[0141] In one possible implementation, the quantization module 430 is further configured to, in the case of the pre-filling stage, obtain the quantization parameters of each block or group in the V matrix, and perform second-dimensional group quantization on the V matrix based on the quantization parameters of each block or group; and in the case of the decoding stage, perform second-dimensional group quantization on the V matrix based on the quantization parameters of each block or group.
[0142] In one possible implementation, the quantization module 430 is further used to perform third-dimensional quantization on the P matrix and / or Q matrix; the Q matrix is related to the input sequence; the P matrix is used to transform the attention output, and the processing result is consistent with the length of the input sequence; the third-dimensional quantization granularity includes any one of quantization granularity by word, by group, by channel, or by tensor.
[0143] In one possible implementation, the K matrix includes low-rank encoded data and position encoded data. The quantization module 430 is also used to quantize the low-rank encoded data in the first dimension; the position encoded data is not quantized. The low-rank encoded data represents the compressed semantics of key-value pairs in the input sequence; the position encoded data represents the position information of each token in the input sequence.
[0144] In one possible implementation, the processing module 420 is further configured to copy the latent matrix to obtain a first latent matrix; and based on the latent matrix and the first latent matrix, to obtain the K matrix and the V matrix.
[0145] Optionally, the aforementioned model quantization device 400 can be applied to Figure 3 The above-mentioned electronic device 100 is implemented in the electronic device 100 shown. Figure 4 The model quantization method described in [the document].
[0146] For example, the model quantization device 400 can be embedded into the PyTorch deep learning framework or the vectorized large language model inference (vLLM) inference framework designed specifically for large language models.
[0147] It should be noted that, in other embodiments, the receiving module 410 can be used to execute any step in the model quantization method, the processing module 420 can be used to execute any step in the model quantization method, and the quantization module 430 can be used to execute any step in the model quantization method. The steps implemented by the receiving module 410, the processing module 420, and the quantization module 430 can be specified as needed. By implementing different steps in the model quantization method through the receiving module 410, the processing module 420, and the quantization module 430, all functions of the model quantization device 400 can be achieved.
[0148] For a detailed description of the above-mentioned optional methods, please refer to the foregoing method embodiments, which will not be repeated here. Furthermore, explanations of any of the model quantization devices 400 provided above, as well as descriptions of their beneficial effects, can be found in the above descriptions. Figure 4 The corresponding method implementations will not be described in detail.
[0149] This application also provides a computer-readable storage medium storing instructions that, when executed on an electronic device, cause the electronic device to perform operations corresponding to any one of the implementation schemes and various feasible implementation schemes of the model quantization method.
[0150] This application also provides a computer program product containing instructions that, when run on an electronic device, causes the electronic device to perform operations of any implementation scheme and various feasible implementation schemes corresponding to the model quantization method.
[0151] Those skilled in the art will recognize that the units and algorithm steps of the various examples 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 implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.
[0152] Those skilled in the art will understand that, for the sake of convenience and brevity, the specific working processes of the systems, devices, and units described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here.
[0153] In the above embodiments, implementation can be achieved entirely or partially through software, hardware, firmware, or any combination thereof. When implemented using software, it can be implemented entirely or partially in the form of a computer program product. The computer program product includes one or more computer instructions, which, when loaded and executed on a computer, generate all or part of the processes or functions described in the embodiments of this application. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another. For example, the computer instructions can be transmitted from one network site, computer, server, or data center to another network site, computer, server, or data center via wired (e.g., coaxial cable, fiber optic, digital subscriber line) or wireless (e.g., infrared, microwave, etc.) means. The computer-readable storage medium can be any available medium that a computer can access, or it can be a data storage device such as a server or data center that integrates one or more available media. The available medium can be a magnetic medium (e.g., floppy disk, hard disk, magnetic tape, etc.), an optical medium (e.g., DVD, etc.), or a semiconductor medium (e.g., solid-state drive), etc.
[0154] In the above embodiments, the descriptions of each embodiment have different focuses. For parts not described in detail in a certain embodiment, please refer to the relevant descriptions in other embodiments.
[0155] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.
Claims
1. A model quantization method, characterized in that, The method is applied to an electronic device, the electronic device having a first model deployed thereon, the first model being a Multi-Head Latent Attention (MLA) architecture, the method comprising: Receive input sequence; The input sequence is processed by the first model to generate a latent matrix; wherein the latent matrix is used to represent the compressed key-value (KV) matrix; and the KV matrix is related to the input sequence. Obtain the K matrix and V matrix from the potential matrix; The K matrix is quantized in a first dimension; the V matrix is quantized in a second dimension; wherein the first dimension and the second dimension are different.
2. The method according to claim 1, characterized in that, The quantization of the K matrix in the first dimension includes: The K matrix is subjected to group quantization in the first dimension; the group quantization is to divide the data into blocks or groups and assign independent quantization parameters to each block or group. The quantization of the V matrix in the second dimension includes: The V matrix is then subjected to group quantization in the second dimension.
3. The method according to claim 2, characterized in that, The inference process of the first model includes a pre-filling stage and a decoding stage. The step of performing group quantization of the V matrix in the second dimension includes: In the pre-filling stage, the quantization parameters of each block or group in the V matrix are obtained, and the V matrix is subjected to group quantization of the second dimension based on the quantization parameters of each block or group. In the decoding stage, the V matrix is subjected to group quantization of the second dimension based on the quantization parameters of each block or group.
4. The method according to any one of claims 1 to 3, characterized in that, The first dimension includes the token dimension; the second dimension includes the channel dimension.
5. The method according to any one of claims 1 to 4, characterized in that, The method further includes: A third dimension of quantization is performed on the P matrix and / or Q matrix; the Q matrix is related to the input sequence; the P matrix is related to the input sequence; the quantization granularity of the third dimension includes any one of quantization granularity by word, by group, by channel, or by tensor.
6. The method according to any one of claims 1 to 5, characterized in that, The K matrix includes low-rank coded data and position-coded data. The quantization of the K matrix in its first dimension includes: The low-rank encoded data is quantized in the first dimension; the position encoded data is not quantized. The low-rank encoded data represents the compressed semantics of the keys in the input sequence; the position encoded data represents the positional information of each lexical in the input sequence.
7. The method according to any one of claims 1 to 6, characterized in that, The step of obtaining the K matrix and V matrix from the latent matrix includes: Copy the latent matrix to obtain the first latent matrix; Based on the latent matrix and the first latent matrix, the K matrix and the V matrix are obtained.
8. A model quantization device, characterized in that, The device includes: The receiving module is used to receive the input sequence; The processing module is configured to process the input sequence using the first model to generate a latent matrix; wherein the latent matrix is used to characterize the compressed key-value (KV) matrix; the KV matrix is related to the input sequence; and to obtain the K matrix and V matrix from the latent matrix. The quantization module is used to quantize the K matrix in a first dimension and the V matrix in a second dimension, wherein the first dimension and the second dimension are different.
9. The apparatus according to claim 8, characterized in that, The quantization module is specifically used for: The K matrix is subjected to group quantization in the first dimension; the group quantization is to divide the data into blocks or groups and assign independent quantization parameters to each block or group. The V matrix is then subjected to group quantization in the second dimension.
10. The apparatus according to claim 8 or 9, characterized in that, The inference process of the first model includes a pre-filling stage and a decoding stage. The quantization module is specifically used for: In the pre-filling stage, the quantization parameters of each block or group in the V matrix are obtained, and the V matrix is subjected to group quantization of the second dimension based on the quantization parameters of each block or group. In the decoding stage, the V matrix is subjected to group quantization of the second dimension based on the quantization parameters of each block or group.
11. The apparatus according to any one of claims 8 to 10, characterized in that, The quantization module is also used for: The P matrix and / or Q matrix are quantized in a third dimension; the Q matrix is related to the input sequence; the P matrix is used to transform the attention output, and the processing result is consistent with the length of the input sequence; the quantization granularity of the third dimension includes any one of quantization granularity by word, by group, by channel, or by tensor.
12. The apparatus according to any one of claims 8 to 11, characterized in that, The K matrix includes low-rank coded data and position-coded data, and the quantization module is further used for: The low-rank encoded data is quantized in the first dimension; the position encoded data is not quantized. The low-rank encoded data represents the compressed semantics of key-value pairs in the input sequence; the position encoded data represents the positional information of each lexical unit in the input sequence.
13. The apparatus according to any one of claims 8 to 12, characterized in that, The processing module is specifically used for: Copy the latent matrix to obtain the first latent matrix; Based on the latent matrix and the first latent matrix, the K matrix and V matrix are obtained.
14. An electronic device, characterized in that, The electronic device includes a processor, a memory, and a computer program / instructions stored in the memory; the processor executes the computer program / instructions to enable the electronic device to implement the model quantization method as described in any one of claims 1-7.
15. A computer program product, characterized in that, The computer program product includes instructions that, when executed by an electronic device, cause the electronic device to perform the model quantization method as described in any one of claims 1-7.
16. A computer-readable storage medium, characterized in that, The computer-readable storage medium includes computer program instructions that, when executed by an electronic device, perform the model quantization method as described in any one of claims 1-7.
