WO2025158201A1 - 模型权重存储、计算处理方法及设备、存储介质和程序 - Google Patents
模型权重存储、计算处理方法及设备、存储介质和程序Info
- Publication number
- WO2025158201A1 WO2025158201A1 PCT/IB2024/062540 IB2024062540W WO2025158201A1 WO 2025158201 A1 WO2025158201 A1 WO 2025158201A1 IB 2024062540 W IB2024062540 W IB 2024062540W WO 2025158201 A1 WO2025158201 A1 WO 2025158201A1
- Authority
- WO
- WIPO (PCT)
- Prior art keywords
- matrix
- weight matrix
- storage
- quantization
- column
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
Classifications
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06N—COMPUTING ARRANGEMENTS BASED ON SPECIFIC COMPUTATIONAL MODELS
- G06N5/00—Computing arrangements using knowledge-based models
- G06N5/04—Inference or reasoning models
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F12/00—Accessing, addressing or allocating within memory systems or architectures
- G06F12/02—Addressing or allocation; Relocation
- G06F12/08—Addressing or allocation; Relocation in hierarchically structured memory systems, e.g. virtual memory systems
- G06F12/0802—Addressing of a memory level in which the access to the desired data or data block requires associative addressing means, e.g. caches
- G06F12/0806—Multiuser, multiprocessor or multiprocessing cache systems
- G06F12/0813—Multiuser, multiprocessor or multiprocessing cache systems with a network or matrix configuration
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06N—COMPUTING ARRANGEMENTS BASED ON SPECIFIC COMPUTATIONAL MODELS
- G06N3/00—Computing arrangements based on biological models
- G06N3/02—Neural networks
- G06N3/04—Architecture, e.g. interconnection topology
- G06N3/0464—Convolutional networks [CNN, ConvNet]
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06N—COMPUTING ARRANGEMENTS BASED ON SPECIFIC COMPUTATIONAL MODELS
- G06N3/00—Computing arrangements based on biological models
- G06N3/02—Neural networks
- G06N3/08—Learning methods
-
- G—PHYSICS
- G06—COMPUTING OR CALCULATING; COUNTING
- G06N—COMPUTING ARRANGEMENTS BASED ON SPECIFIC COMPUTATIONAL MODELS
- G06N3/00—Computing arrangements based on biological models
- G06N3/02—Neural networks
- G06N3/04—Architecture, e.g. interconnection topology
- G06N3/0495—Quantised networks; Sparse networks; Compressed networks
Definitions
- a neural network model includes several network layers (such as convolutional layers and fully connected layers), and different network layers correspond to different weight matrices.
- the weight values in the weight matrices of each network layer are generally in a floating-point data format, such as FP16. Storing the weight matrix in this data format consumes more storage space.
- the weight matrices of each network layer in a neural network model typically require quantization. For example, this is done using the near-lossless LLM (Large Language Model) weight compression (SpQR) quantization method.
- the quantization results of the weight matrix of that layer include a quantized weight matrix, a quantization parameter matrix, and a non-quantized weight matrix.
- the quantization parameter matrix includes a zero-value matrix and a quantization scale matrix.
- the quantized weight matrix is composed of the quantized results of the weight values in the weight matrix that require quantization. Quantization is implemented using the quantization parameter matrix.
- the non-quantized weight matrix is composed of the weight values in the weight matrix that do not require quantization. The proportion of weight values that do not require quantization is very small (typically less than 1%), so the non-quantized weight matrix is a sparse matrix.
- the data formats used for element values in the quantization weight matrix, quantization parameter matrix, and non-quantization weight matrix differ. For example, element values in the quantization weight matrix are in INT4 format, while element values in the non-quantization weight matrix are in FP16 format.
- the zero values (zeros) in the zero value matrix are in INT4 format, and the quantization scales (scales) in the quantization scale matrix are in FP16 format.
- the element values in the corresponding matrices can be stored row by row in memory.
- the non-quantization weight matrix is a sparse matrix, meaning only a small number of elements are non-zero, storing the elements in each row of the non-quantization weight matrix continuously, as is done for the quantization weight matrix and quantization parameter matrix, would consume a significant amount of memory space. Therefore, to save memory space, the non-quantization weight matrix is typically stored using the coordinating format (COO) storage structure.
- COO coordinating format
- a triple (row, col, data) is used to store the nonzero elements in the unquantized weight matrix, where row and col are the row and column numbers of the nonzero element, respectively, and data is the value of the nonzero element.
- row and col are the row and column numbers of the nonzero element, respectively, and data is the value of the nonzero element.
- This matrix multiplication refers to the multiplication between the input matrix of any network layer in the neural network model and the weight matrix of that network layer.
- embodiments of the present disclosure provide a model weight calculation processing method, the method comprising: in response to input of an input matrix of a target network layer in a neural network model obtained based on an inference task, obtaining a quantized weight matrix and a quantized parameter matrix corresponding to the target network layer; obtaining multiple storage matrices for storing a non-quantized weight matrix corresponding to the target network layer, the multiple storage matrices including a first storage matrix for storing non-zero element values in the non-quantized weight matrix by column, a second storage matrix for storing row numbers corresponding to non-zero element values in the non-quantized weight matrix, a third storage matrix for storing the number of non-zero element values in each column of the non-quantized weight matrix, and a fourth storage matrix for storing starting index positions corresponding to each column of the non-quantized weight matrix in the first storage matrix; wherein the non-quantized weight matrix is a sparse matrix, and a quantization result of the weight matrix of the
- the first calculation result corresponds to the product of the input matrix and an inverse quantization weight matrix
- the second calculation result corresponds to the product of the input matrix and the unquantized weight matrix
- the inverse quantization weight matrix is obtained by inverse quantizing the quantization weight matrix based on the quantization parameter matrix.
- embodiments of the present disclosure provide a model weight calculation and processing device, comprising: an acquisition module configured to, in response to an input matrix of a target network layer in a neural network model obtained based on an inference task, acquire a quantized weight matrix and a quantization parameter matrix corresponding to the target network layer, and acquire multiple storage matrices for storing a non-quantized weight matrix corresponding to the target network layer, wherein the multiple storage matrices include a first storage matrix for storing non-zero element values in the non-quantized weight matrix by column, a second storage matrix for storing row numbers corresponding to non-zero element values in the non-quantized weight matrix, a third storage matrix for storing the number of non-zero element values in each column of the non-quantized weight matrix, and a fourth storage matrix for storing the starting index positions corresponding to each column of the non-quantized weight matrix in the first storage matrix; wherein the non-quantized weight matrix is a sparse matrix, and the quantization result of the
- an embodiment of the present disclosure provides an electronic device comprising: a memory, a processor, and a communication interface.
- the memory stores executable code, and when the executable code is executed by the processor, the processor is enabled to at least implement the model weight calculation method described in the first aspect.
- an embodiment of the present disclosure provides a non-transitory machine-readable storage medium, the non-transitory machine-readable storage medium storing executable code, and when the executable code is executed by the processor of the electronic device, the processor is enabled to at least implement the model weight calculation method described in the first aspect.
- embodiments of the present disclosure provide a computer program product, comprising a computer program that, when executed by a processor of an electronic device, enables the processor to at least implement the model weight calculation method described in the first aspect.
- embodiments of the present disclosure provide a model weight storage method, comprising: obtaining a quantization result of a weight matrix of a target network layer in a neural network model, the quantization result comprising a quantized weight matrix, a quantization parameter matrix, and a non-quantized weight matrix, wherein the non-quantized weight matrix is a sparse matrix; generating multiple storage matrices for storing the non-quantized weight matrix, the multiple storage matrices comprising a first storage matrix for storing non-zero element values in the non-quantized weight matrix by column, a second storage matrix for storing row numbers corresponding to non-zero element values in the non-quantized weight matrix, a third storage matrix for storing the number of non-zero element values in each column of the non-quantized weight matrix, and a fourth storage matrix for storing the starting index positions corresponding to each column of the non-quantized weight matrix in the first storage matrix; and storing the multiple storage matrices, the quantized weight matrix, and
- an embodiment of the present disclosure provides a model weight storage device, comprising: an acquisition module, configured to acquire a quantization result of a weight matrix of a target network layer in a neural network model, wherein the quantization result includes a quantization weight matrix, a quantization parameter matrix, and a non-quantization weight matrix, and the non-quantization weight matrix is a sparse matrix; a generation module, configured to generate multiple storage matrices for storing the non-quantization weight matrix, wherein the multiple storage matrices include a first storage matrix for storing each non-zero element value in the non-quantization weight matrix by column, a second storage matrix for storing the row number corresponding to each non-zero element value in the non-quantization weight matrix in the non-quantization weight matrix, a third storage matrix for storing the number of non-zero element values in each column of the non-quantization weight matrix, and a fourth storage matrix for storing the starting index position corresponding to each column in the non-quantization weight matrix in
- an embodiment of the present disclosure provides an electronic device comprising: a memory, a processor, and a communication interface; wherein the memory stores executable code, and when the processor executes the executable code, the processor The processor can at least implement the model weight storage method described in the fifth aspect.
- embodiments of the present disclosure provide a non-transitory machine-readable storage medium storing executable code. When the executable code is executed by a processor of an electronic device, the processor can at least implement the model weight storage method described in the fifth aspect.
- embodiments of the present disclosure provide a computer program product containing a computer program. When executed by a processor of an electronic device, the processor can at least implement the model weight storage method described in the fifth aspect.
- the model weight calculation solution provided by embodiments of the present disclosure first quantizes the weight matrices of each network layer of a neural network model to save storage space.
- the quantization results of the target network layer's weight matrix include a quantized weight matrix, a quantization parameter matrix, and a non-quantized weight matrix.
- the non-quantized weight matrix is a sparse matrix.
- a new storage structure for storing the non-quantized weight matrix is provided. Specifically, multiple storage matrices are generated for storing the unquantized weight matrix.
- These include a first storage matrix for storing the values of each non-zero element in the unquantized weight matrix by column, a second storage matrix for storing the row number corresponding to each non-zero element in the unquantized weight matrix, a third storage matrix for storing the number of non-zero elements in each column of the unquantized weight matrix, and a fourth storage matrix for storing the starting index position corresponding to each column in the first storage matrix.
- these four storage matrices enable continuous storage of each non-zero element in the unquantized weight matrix and the position coordinates (row and column numbers) of each non-zero element in the unquantized weight matrix across different storage matrices.
- These four storage matrices can be stored continuously in the storage space, just as the quantized weight matrix is normally stored.
- the first and second calculation results corresponding to the same position in the output matrix are determined based on the multiple storage matrices.
- the target calculation result for the same position is then obtained by summing the first and second calculation results.
- the first calculation result corresponds to the product of the input matrix and the inverse quantized weight matrix, which is obtained by inverse quantizing the quantized weight matrix based on the quantization parameter matrix.
- the second calculation result corresponds to the product of the input matrix and the unquantized weight matrix.
- Figure 2 is a flowchart of a model weight storage method provided by an embodiment of the present disclosure
- Figure 3 is a schematic diagram of a new storage structure for storing unquantized weight matrices provided by an embodiment of the present disclosure
- Figure 4 is a schematic diagram of a storage method for weight matrix quantization results provided by an embodiment of the present disclosure
- Figure 5 is a flowchart of a model weight calculation and processing method provided by an embodiment of the present disclosure
- Figure 6 is a schematic diagram of the structure of a model weight calculation and processing device provided by an embodiment of the present disclosure
- Figure 7 is a schematic diagram of the structure of a model weight storage device provided by an embodiment of the present disclosure
- Figure 8 is a schematic diagram of the structure of an electronic device provided by an embodiment of the present disclosure.
- the speed of matrix multiplication between the input matrix and the corresponding weight matrix directly affects the efficiency of the inference phase, so accelerating the matrix multiplication speed of the input matrix and the weight matrix is necessary.
- the following diagram illustrates the results of quantizing a weight matrix using the SpQR quantization method, as well as the calculation method used during inference.
- the input matrix of a network layer of a neural network model (such as a large language model) is...
- the input matrix is X
- the weight matrix is W.
- the matrix multiplication of the two results in the output matrix out.
- the dimensions of X are x_row*x_column, and that each element value is in fp16 format.
- each element value i.e., weight value
- the weight matrices of each network layer in the neural network model can be quantized, and the quantized results stored in video memory. Therefore, when obtaining the product of the input matrix X and the weight matrix W, the multiplication of the input matrix X and the weight matrix W is not performed directly, because the weight matrix W is not actually stored, but rather the quantized result of the weight matrix W.
- the quantization parameter matrix includes a zero value matrix Z and a quantization scale matrix S.
- the quantization scale matrix can also be called a quantization coefficient matrix.
- the quantized weight matrix W' includes the quantization results of each weight value in the weight matrix W that requires quantization.
- the data format of these weight values is fp16.
- the data format of the quantized results of these weight values is irrt4 (not limited to this format and may also be other integer formats with a length less than fp16).
- the unquantized weight matrix O includes each weight value in the weight matrix W that does not require quantization (these values are also called outliers values), and thus these weight values maintain the fp16 data format.
- the original dimensions of the quantized weight matrix W' and the non-quantized weight matrix O are: w_row * w_column.
- the positions corresponding to weight values that do not require quantization in the quantized weight matrix W' can be set to a certain set value, while the positions corresponding to weight values that require quantization in the non-quantized weight matrix O can be set to zero. Therefore, the non-quantized weight matrix O is actually a sparse matrix with only a small number of non-zero elements.
- the rectangular bars in the figure represent the multiple non-zero elements in different columns.
- the weight values at which positions in the weight matrix W require quantization and which positions do not require quantization can be determined using existing methods, which will not be elaborated here.
- the quantization parameter matrix namely the zero value matrix Z and the quantization scale matrix S, are first calculated to complete the quantization process based on the quantization parameter matrix.
- the zero value matrix Z gives the zero value (zero) corresponding to each element value in the quantization weight matrix W' during the quantization process, that is, the zero value corresponding to each weight value that needs to be quantized;
- the quantization scale matrix S gives the quantization scale (scale) corresponding to each element value in the quantization weight matrix W' during the quantization process, that is, the quantization scale corresponding to each weight value that needs to be quantized.
- the weight values in the weight matrix W can be grouped, and each group shares the same zero value and quantization scale, such as grouping by row: For example, in the same column Each group consists of 128 rows. Therefore, a group length can be pre-set: groups i ze, which is 128 in the above example. Based on this, it can be understood that in order to reduce the storage space occupied by the quantization parameter matrix, as shown in Figure 1, the dimensions of the zero value matrix Z and the quantization scale matrix S can be: w row/groups i ze, w column number, that is, each group stores only one zero value and quantization scale. During inference, the original dimensions can be restored, that is, w_row*w_column.
- the data format of each zero value in the zero value matrix Z is consistent with the data format of each element value in the quantization weight matrix W', which is in nt4 format.
- the data format of each quantization scale in the quantization scale matrix S is consistent with the original data format of each weight value in the weight matrix W, which is in fp16 format.
- the calculation formula for the element value in the i-th row and j-th column of the output matrix out is shown above. Specifically, it is the sum of the first and second calculation results.
- the first calculation result reflects the multiplication of the quantized weight matrix W' after dequantization by the input matrix X
- the second calculation result reflects the multiplication of the input matrix X by the unquantized weight matrix 0.
- the dequantization process can be simply expressed as: (W' - Z) * So.
- these matrices are all non-sparse matrices.
- the element values in the corresponding matrices can be stored row by row in the storage space.
- a continuous storage space can be allocated in the video memory.
- the values of the first row of the quantization weight matrix are sequentially stored in this storage space.
- the values of the second row of the elements are sequentially stored in this storage space, and so on.
- the element value at the storage location "i*w_columu+k" in this storage space is the value of the element in the i-th row and k-th column of the quantization weight matrix W'.
- the zero-value matrix Z and the quantization scale matrix S are similarly described and will not be further explained. Since the non-quantization weight matrix 0 is a sparse matrix, meaning that only a small number of elements are non-zero, if the continuous storage method described above for non-sparse matrices such as the quantization weight matrix W' is used, the elements in each row of the non-quantization weight matrix 0 are concatenated. Storing the non-quantized weight matrix continuously would consume a significant amount of memory space. Therefore, to save space, the coo (coordination format) storage structure is typically used for non-quantized weight matrices.
- the non-zero element values in the non-quantized weight matrix are stored using a triplet (row, col, data), where row and col represent the row and column numbers of the non-zero element, respectively, and data represents the non-zero element value.
- row and col represent the row and column numbers of the non-zero element, respectively
- data represents the non-zero element value.
- Solutions for accelerating matrix multiplication using acceleration methods such as CUDA (Compute Unified Device Architecture) are all based on the premise of being able to traverse the storage space to find the element value based on the element's subscript (i.e., row and column number) using the aforementioned traversal method.
- CUDA Computer Unified Device Architecture
- the non-quantized weight matrix 0 When storing the non-quantized weight matrix 0 using the COO storage structure, only the subscript indices and values of the non-zero element values are stored. Therefore, when performing matrix multiplication, such as the calculation of Xik * 0kj , after finding Xjj using the aforementioned continuous storage method, it is necessary to first query the COO storage structure for the index item (k, j). If so, the corresponding element value is further searched.
- the embodiments of the present disclosure provide a new storage structure for storing the non-quantized weight matrix 0. Based on this new storage structure, not only can the amount of storage space occupied when storing the non-quantized weight matrix 0 be reduced, but also accelerated multiplication calculations of the input matrix and the weight matrix can be achieved. Based on the following embodiments, the new storage structure and the matrix multiplication calculation process based on the new storage structure are illustrated. The methods provided in the following embodiments can be executed by a graphics processor (GPU) included in an electronic device.
- GPU graphics processor
- the electronic device can be a terminal device such as a PC, a laptop, a smartphone, or a server, such as a cloud server.
- Figure 2 is a flow chart of a model weight storage method provided by an embodiment of the present disclosure. As shown in Figure 2, the method can include the following steps:
- the quantization result includes a quantized weight matrix, a quantized parameter matrix, and a non-quantized weight matrix.
- the non-quantized weight matrix is a sparse matrix.
- the multiple storage matrices include a first storage matrix for storing each non-zero element value in the unquantized weight matrix by column, a second storage matrix for storing a row number corresponding to each non-zero element value in the unquantized weight matrix in the unquantized weight matrix, a third storage matrix for storing the number of non-zero element values in each column of the unquantized weight matrix, and a fourth storage matrix for storing a starting index position corresponding to each column in the unquantized weight matrix in the first storage matrix.
- the quantization results include the quantization weight matrix W', the unquantized weight matrix 0, and the quantization parameter matrix consisting of the zero-value matrix Z and the quantization scale matrix S.
- the non-sparse matrices, quantization weight matrix W', zero-value matrix Z, and quantization scale matrix S are stored using the "contiguous storage" method described above. Simply put, three memory blocks are allocated in the video memory to store these three matrices.
- the storage addresses of each memory block are continuous, and the elements of each matrix are stored row by row in the corresponding memory block.
- the unquantized weight matrix 0 which is a sparse matrix
- a new storage structure consisting of the four memory matrices described above is used. In practice, these four memory matrices can be four row vectors.
- the first storage matrix (which can be expressed as outliers va I) is used to store the non-zero element values in the unquantized weight matrix W by column. These non-zero element values are weight values in the weight matrix W that do not require quantization.
- the first storage matrix is used to store all non-zero element values (i.e., outliers values) contained in the unquantized weight matrix 0.
- the storage order is to first traverse column by column and, for each column, store the values row by row in the order of the first row (row1), the second row (row2), etc.
- the second storage matrix (which can be expressed as outliers row) is used to store the row numbers corresponding to the non-zero element values in the unquantized weight matrix 0.
- the dimension of the second storage matrix is equal to the dimension of the first storage matrix.
- the dimension of the first storage matrix can be: 1 * M.
- the dimension of the second storage matrix is also: 1 * M, which is used to record the row number in the unquantized weight matrix 0 that actually corresponds to each non-zero element value in the first storage matrix.
- each row number can be represented using, for example, the unit32 data format.
- the third storage matrix (which can be represented as out I i ers co I umn num) is used to store the number of non-zero element values (as described above, refers to non-zero element values) in each column of the unquantized weight matrix 0.
- the fourth storage matrix (which can be expressed as out I i ers co I umn start) is used to store the starting index position corresponding to each column in the non-quantized weight matrix 0 in the first storage matrix.
- the starting index position corresponding to the nth column is determined according to the starting index position corresponding to the n-1th column and the number of non-zero element values contained in the n-1th column, and the starting index position of the starting column (that is, the first column with column number 0: column 0) is 0.
- the position index range of the first storage matrix is [0, M-1], where M is the number of non-zero element values stored in the first storage matrix.
- the first storage matrix outliers_value [1.0, 3.0, 2.0], that is, according to the traversal order of the columns, the non-zero element values in the non-quantized weight matrix 0 are stored row by row in each column into the first storage matrix.
- the second storage matrix outliers_row [2, 3, 4], that is, sequentially storing the row number corresponding to each non-zero element value in the first storage matrix in the non-quantized weight matrix 0, where 2 is the row number corresponding to the element value 1.0, 3 is the row number corresponding to the element value 3.0, and 4 is the row number corresponding to the element value 2.0.
- the third storage matrix outlierscount_num [0, 1, 0, 0, 2], that is, storing the number of non-zero element values contained in each column of the non-quantized weight matrix 0.
- the first column i.e., column 0
- the second column i.e., column 2) contains one non-zero element value (1.0), so the corresponding number is 1.
- the third and fourth columns do not contain any non-zero element values, so the corresponding number is 0.
- the fifth column (i.e., column 4) contains two non-zero element values (3.0 and 2.0), so the corresponding number is 2.
- the fourth storage matrix out I i ers co I umn_start [0, 0, 1, 1, 1], which stores the starting index positions corresponding to each column in the unquantized weight matrix 0 in the first storage matrix. Assuming that the first storage matrix stores M element values, its position index range is [0, M-1]. Based on this, it can be understood that regardless of whether the first column (column 0) in the unquantized weight matrix 0 contains non-zero elements, its corresponding starting index position in the first storage matrix is 0.
- the above describes a new storage structure for storing unquantized weight matrix 0, provided in embodiments of the present disclosure. Based on this storage structure, accelerated computation of the multiplication of an input matrix and a weight matrix can be achieved. The specific computation process will be described in subsequent embodiments.
- the non-quantized weight matrix 0 can be stored using the new storage structure mentioned above, and the weight The quantization weight matrix W' and quantization parameter matrix obtained after quantizing the matrix W can be stored based on the method described above.
- the quantization weight matrix W' and the quantization parameter matrix may be stored in the following manner: obtaining an initial quantization weight matrix and an initial quantization parameter matrix of the weight matrix of the target network layer, wherein the initial quantization parameter matrix includes an initial zero value matrix, and the initial quantization weight matrix and the initial zero value matrix have the same dimension and element values have the same first data format; grouping the initial quantization weight matrix by row to obtain a grouped quantization weight matrix, wherein element values within a group corresponding to the same column in the initial quantization weight matrix are concatenated into element values in the second data format for storage in the grouped quantization weight matrix; grouping the initial zero value matrix by column to obtain a grouped zero value matrix, wherein element values within a group corresponding to the same row in the initial zero value matrix are concatenated into element values in the second data format for storage in the grouped zero value matrix, and the initial quantization weight matrix and the initial zero value
- the above storage method is illustrated in conjunction with FIG4 .
- the initial quantization weight matrix is W1
- the initial zero value matrix is Z1
- their dimensions are w_row*w_column
- their element values are all in int4 format (the first data format).
- the initial zero value matrix Z1 can be grouped on a row basis according to the set groups i ze. The zero values within each row group corresponding to the same column are the same, and only one is stored. For details, refer to the relevant diagram in FIG1 .
- FIG4 illustrates a situation where grouped storage based on groups i ze is performed.
- the initial quantization weight matrix W1 and the initial zero value matrix Z1 use the same group length of 8.
- the initial quantization weight matrix W1 is grouped by row to obtain a grouped quantization weight matrix W2.
- the initial zero value matrix Z1 is grouped by column to obtain a grouped zero value matrix Z2.
- the dimension of the grouped quantization weight matrix W2 becomes: w_row/8 * w_col umn
- the dimension of the grouped quantization weight matrix W2 becomes: w_row/groups i ze * w_col umn/8.
- the values of the elements within the same group corresponding to the same column in the initial quantization weight matrix W1 are concatenated into element values in the second data format and stored in the grouped quantization weight matrix W2.
- the eight element values in the first group (first 8 rows) corresponding to the first column of the initial quantization weight matrix W1 in the int4 format can be combined into a value in the int32 format (second data format) and stored in the first row and first column of the grouped quantization weight matrix W2.
- the element values in the group corresponding to the same row in the initial zero value matrix Z1 are concatenated into element values in the second data format and stored in the grouped zero value matrix Z2.
- the eight element values in the first group (first 8 columns) corresponding to the first row of the initial zero value matrix Z1 in the int4 format can be combined into a value in the int32 format and stored.
- the eight element values in int4 format are combined into an int32 value. Simply put, these eight element values are sequentially represented as 4-bit binary strings, resulting in a 32-bit binary string. This binary string is then formatted to obtain the corresponding decimal value.
- This storage method can further reduce the storage space occupied by the quantized weight matrix. It should be noted that during the inference phase, when the target network layer's input matrix and weight matrix need to be multiplied, the grouped quantized weight matrix W2 and the grouped zero-value matrix Z2 need to be loaded from the storage space and restored to the initial quantized weight matrix W1 and the initial zero-value matrix Z1.
- FIG5 is a flowchart of a model weight calculation method provided in an embodiment of the present disclosure. As shown in FIG5 , the method may include the following steps:
- 501 In response to an input matrix of a target network layer in a neural network model obtained based on an inference task, obtain a quantization weight matrix and a quantization parameter matrix corresponding to the target network layer.
- the multiple storage matrices include a first storage matrix for storing non-zero element values in the unquantized weight matrix by column, a second storage matrix for storing row numbers corresponding to non-zero element values in the unquantized weight matrix, a third storage matrix for storing the number of non-zero element values in each column of the unquantized weight matrix, and a fourth storage matrix for storing starting index positions corresponding to each column of the unquantized weight matrix in the first storage matrix.
- the dequantized weight matrix is obtained by dequantizing the quantized weight matrix based on the quantization parameter matrix.
- the multiplication process of the input matrix X of the target network layer and its weight matrix W is used as an example for description.
- the weight matrix W of the target network layer has been quantized.
- the quantized weight matrix results include the quantized weight matrix W', the quantization parameter matrix (including the zero-value matrix Z and the quantization scale matrix S mentioned above), and the unquantized weight matrix O.
- the quantized weight matrix W', the zero-value matrix Z, and the quantization scale matrix S are stored using the continuous storage method described above and have been stored in the corresponding storage space of the video memory.
- the unquantized weight matrix O, as a sparse matrix, is stored using the new storage structure described in the previous embodiment, and the corresponding multiple storage matrices have been stored in the corresponding storage space of the video memory.
- the output matrix of the calculation result is represented as out. Based on the input of the reasoning task, each network layer in the neural network model will sequentially receive its own input matrix.
- the input matrix of a network layer in a neural network model can be the matrix obtained by encoding the input sentence.
- the output matrix obtained by multiplying this input matrix with the weight matrix of this network layer can serve as the input matrix for the next network layer.
- the output matrix can be processed (such as downsampling) and then used as the input matrix for the next network layer.
- the multiple computational objects that are actually calculated with the input matrix X must first be read from the video memory: the quantized weight matrix W', the quantization parameter matrix (zero value matrix Z), and the quantization scale matrix S, as well as multiple storage matrices for storing the unquantized weight matrix 0.
- These matrices are then loaded into a pre-allocated target cache space in the video memory. In practical applications, this target cache space can be determined based on the dimensions of the weight matrix of the largest network layer in the neural network model.
- the output matrix ut has an element value in the i-th row and j-th column, determined by the sum of the first calculation result (the first item added) and the second calculation result (the second item added). Therefore, during the calculation process, the first and second calculation results can be calculated separately and then added to OU. Furthermore, as shown in the above calculation formula, the calculation of the first and second calculation results actually involves the separate calculation of element values at several positions in different matrices, which are ultimately added together. Therefore, parallel multithreading methods such as CUDA can be used to accelerate the calculation. Specifically, for the calculation process of the first calculation result, the calculation positions corresponding to the multiple parallel threads can be first determined.
- the calculation position corresponding to the target thread includes the first position identifier i, the second position identifier j, and the third position identifier k.
- the target thread is any of the multiple threads. The meanings of these position identifiers are described in the above calculation formula.
- the target thread searches the input matrix X, the quantization weight matrix, and the quantization parameter matrix to obtain the first, second, and third element values based on the first position identifier i, the second position identifier j, and the third position identifier k.
- the quantization parameter matrix includes the zero-value matrix Z and the quantization scale matrix S, so the third element value includes the third element values found in the zero-value matrix Z and the quantization scale matrix S, respectively.
- the first element value corresponds to the position of the input matrix X with the first position identifier i as the row number and the third position identifier k as the column number, which is X/.
- the second element value corresponds to the position of the quantization weight matrix W' with the third position identifier k as the row number and the second position identifier j as the column number, which is W'kj.
- the third element value corresponds to the position of the quantization parameter matrix with the third position identifier k as the column number.
- the positions with position identifier k as the row number and position identifier j as the column number are Zkj and Skj.
- the target position of the output matrix is updated based on the first element value x ⁇ , the second element value v/ ⁇ , and the third element values Z/cj and Scj.
- the first calculation result of the first calculation result is obtained. Specifically, these element values are calculated according to the above calculation formula to obtain a sub-item to be accumulated in the first calculation result corresponding to the current first position identifier i, second position identifier j, and third position identifier k.
- the value range of k is determined by the number of columns x_co I umn in the input matrix X.
- the actual number of threads required is x_column, where each thread corresponds to the same first position identifier i and second position identifier j, but has a different third position identifier k.
- the following first describes the process of the target thread calculating or determining the corresponding second calculation result, still using the target thread as an example.
- the following describes the thread setting method and the method for determining the position identifier corresponding to the thread.
- the target thread no longer simply reads the corresponding element value from the corresponding matrix according to the position identifier, as when calculating the first calculation result.
- the target thread must first determine the fourth element value in the unquantized weight matrix 0 based on the second position identifier j, the third position identifier k, and multiple storage matrices.
- the fourth element value corresponds to the element value at the target sort position in the column of the unquantized weight matrix 0 with the second position identifier j as the column number.
- the target sort position is the value corresponding to the third position identifier k.
- the target thread determines a fifth element value in the input matrix X.
- the fifth element value corresponds to a position in the input matrix X having the first position identifier i as a row number and the fourth position identifier as a column number.
- the fourth position identifier corresponds to the row number of the fourth element value in the unquantized weight matrix 0. Thereafter, the target thread updates the second calculation result of the target position OUttj in the output matrix according to the product of the fourth element value and the fifth element value.
- the process of determining the fourth element value and the fifth element value includes: querying the number of non-zero element values contained in the target with the second position identifier j as the column number of the non-quantized weight matrix 0 in the third storage matrix out I i ers_co I umn_num out li ers_co l umn_num [j] o If the number of non-zero element values is greater than the numerical value corresponding to the third position identifier k, querying the starting index position corresponding to the target column in the first storage matrix out I i ers_va I in the fourth storage matrix out I i ers_co I umn_start: out I i ers_co I umn_start [ j], that is, if out I i ers_co I umn_num [j] >k, then find the starting index position out I i ers_co I umn_start [ j] corresponding to the jth column in the first storage matrix out I i ers_va I ,
- the fourth element value corresponding to the target index position is queried in the first storage matrix out li ers_va l, where the target index position is the sum of the values corresponding to the starting index position and the third position identifier: out li ers_co I umn_start [ j] + k o. Therefore, the fourth element value is: out I i ers_va I [out I i ers_co I umn_start [ j] + k] o .
- the actual row number corresponding to the fourth element value in the non-quantized weight matrix 0 is: out I i ers_row [out I i ers_co I umn_start [j] + k] , that is, the actual row number corresponding to the fourth element value in the non-quantized weight matrix 0 is found in the second storage matrix out I i ers_row. That is, the fourth element value is the non-zero element value of the ith row and jth column in the unquantized weight matrix 0.
- the fifth element value of the input matrix X to be multiplied with it should be the element value of the ith row and jth column in the input matrix X: XiOrow o
- the fourth position identifier is Or. *.
- the product of XiOrow * 0orowj is used as a calculation sub-item in the second calculation result of 014% • and is superimposed on 014% •.
- the calculation method for the input matrix X and the unquantized weight matrix 0 is essentially to traverse the unquantized matrix according to the input matrix X.
- the calculation idea of traversing the input matrix X according to the position of the non-zero element value in the non-quantized weight matrix 0 is adopted.
- the non-zero element values contained in the non-quantized weight matrix 0 and the row number and column number corresponding to each non-zero element value in the non-quantized weight matrix 0 are stored using the storage structure of the above-mentioned multiple storage matrices.
- the non-zero element values required for calculation by the current thread can be queried according to the multiple storage matrices according to the above-mentioned traversal method, and the element values that need to be searched in the input matrix are determined accordingly, thereby realizing accelerated calculation based on multi-threading.
- the non-zero element values of the jth column in the non-quantized weight matrix it is not necessary to correspond to the calculation dimension of the quantized weight matrix in this matrix multiplication, thereby realizing the memory access of the complex non-zero element values of 0(1), thereby improving the reasoning speed.
- the following introduces the setting of multi-threading and the method for determining the position identifier corresponding to the thread.
- the number of threads is determined as: out_row * out_column * x_column. This yields the total number of threads required.
- the upper limit of thread numbers supported by GPUs is limited. For example, some GPUs limit thread numbers to 1024. If the total number of threads exceeds this upper limit, the concepts of thread blocks and thread grids are necessary. In large-scale neural network models (such as large language models), the dimensions of the weight matrix are often very large, exceeding these limits.
- the thread block dimensions can be set first, and then the thread grid dimensions can be determined based on the set thread block dimensions.
- the thread block dimensions determine the number of threads contained in each thread block, while the thread grid dimensions determine the number of thread blocks contained in the thread grid.
- the dimension of the thread block can be three-dimensional: b I lockd i m. x, b I lockd i m. y, b I lockd i m. z
- the dimension of the thread grid can be three-dimensional: griddim.x, griddim.y, griddim.z. Based on the above three dimensions, the number of threads that can be included in a thread block is: b I ockd i m.
- the number of thread blocks that can be included in a thread grid is: gridd i m. x*gridd i m. y*gridd i m. z
- the process of determining the calculation positions corresponding to multiple threads includes: According to the thread The grid dimension, the thread block dimension, the relative position of each thread block in the thread grid, and the relative position of each thread in its own thread block are used to determine the identifiers corresponding to each of the multiple threads. Furthermore, the computation positions corresponding to each of the multiple threads are determined based on the identifiers corresponding to each of the multiple threads.
- N griddi m.x*griddi m.y*griddi m.z*blockdi m.x*blockdi m.y*blockdi m.z.
- black l dx.x, black l dx.y, and blockdi m.z respectively represent the relative position of the current thread block in its own thread grid
- thread I dx.x, thread I dx.y, and thread I dx.z respectively represent the relative position of the current thread in its own thread block.
- thread I d thread I d thread I dx. x+thread I dx. y*b I ockd i m. x+thread I dx. z*b I ockd i m. x* b I ockd i m. y o.
- thread I d thread I d thread I dx. x+thread I dx. y*b I ockd i m. x+thread I dx. z*b I ockd i m. x* b I ockd i m. y o.
- M total number of threads in a thread block
- FIG. 6 is a schematic diagram of the structure of a model weight calculation and processing device provided by an embodiment of the present disclosure.
- the device includes: an acquisition module 11 and an inference module 12.
- the acquisition module 11 is configured to, in response to an input matrix of a target network layer in a neural network model obtained based on an inference task, acquire a quantization weight matrix and a quantization parameter matrix corresponding to the target network layer, and acquire multiple storage matrices for storing a non-quantization weight matrix corresponding to the target network layer.
- the multiple storage matrices include a first storage matrix for storing non-zero element values in the non-quantization weight matrix by column, a second storage matrix for storing row numbers corresponding to non-zero element values in the non-quantization weight matrix, a third storage matrix for storing the number of non-zero element values in each column of the non-quantization weight matrix, and a fourth storage matrix for storing the starting index position corresponding to each column of the non-quantization weight matrix in the first storage matrix.
- the non-quantization weight matrix is The matrix is a sparse matrix, and the quantization result of the weight matrix of the target network layer includes the quantized weight matrix, the quantization parameter matrix, and the unquantized weight matrix.
- the inference module 12 is configured to determine, based on the multiple stored matrices, a first calculation result and a second calculation result corresponding to the same position in the output matrix of the target network layer, and obtain a target calculation result for the same position based on the cumulative sum of the first calculation result and the second calculation result.
- the first calculation result corresponds to the product of the input matrix and the inverse quantized weight matrix
- the second calculation result corresponds to the product of the input matrix and the unquantized weight matrix.
- the inverse quantized weight matrix is obtained by inverse quantizing the quantized weight matrix based on the quantization parameter matrix.
- FIG7 is a schematic structural diagram of a model weight storage device provided in an embodiment of the present disclosure.
- the device includes: an acquisition module 21, a generation module 22, and a storage module 23.
- An acquisition module 21 is configured to obtain quantization results for the weight matrix of a target network layer in a neural network model.
- the quantization results include a quantized weight matrix, a quantization parameter matrix, and a non-quantized weight matrix, where the non-quantized weight matrix is a sparse matrix.
- a generation module 22 is configured to generate multiple storage matrices for storing the non-quantized weight matrix.
- the multiple storage matrices include a first storage matrix for storing the non-zero element values in the non-quantized weight matrix by column, a second storage matrix for storing the row number corresponding to each non-zero element value in the non-quantized weight matrix, a third storage matrix for storing the number of non-zero element values in each column of the non-quantized weight matrix, and a fourth storage matrix for storing the starting index position corresponding to each column in the non-quantized weight matrix in the first storage matrix.
- a storage module 23 is configured to store the multiple storage matrices, the quantized weight matrix, and the quantization parameter matrix. The apparatus shown in FIG7 can perform the relevant steps in the aforementioned embodiments.
- the structure of the apparatus shown in FIG6 and FIG7 can be implemented as an electronic device.
- the electronic device may include: a processor 31, a memory 32, and a communication interface 33.
- the memory 32 stores executable code.
- the processor 31 can at least implement the model weight calculation processing method or the model weight storage method provided in the aforementioned embodiment.
- the embodiment of the present disclosure provides a non-transitory machine-readable storage medium.
- the non-transitory machine-readable storage medium stores executable code.
- the processor can at least implement the model weight calculation processing method or the model weight storage method provided in the aforementioned embodiment.
- the device embodiment described above is merely illustrative, wherein the network elements described as separate components may or may not be physically separated. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of this embodiment. Those skilled in the art can understand and implement it without inventive labor. Through the description of the above embodiments, those skilled in the art can clearly understand that each embodiment can be implemented with the help of It can be implemented by adding the necessary general hardware platform, or of course, by combining hardware and software.
- the present disclosure can take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM (Compact Disk Read-Only Memory), optical storage, etc.) containing computer-usable program code.
- the present disclosure provides a computer program product that includes a computer program. When executed by a processor of an electronic device, the computer program product enables the processor to at least implement the model weight calculation method or model weight storage method provided in the aforementioned embodiments.
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Physics & Mathematics (AREA)
- Mathematical Physics (AREA)
- General Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- Data Mining & Analysis (AREA)
- Computational Linguistics (AREA)
- Evolutionary Computation (AREA)
- Computing Systems (AREA)
- Artificial Intelligence (AREA)
- Software Systems (AREA)
- Biophysics (AREA)
- General Health & Medical Sciences (AREA)
- Molecular Biology (AREA)
- Biomedical Technology (AREA)
- Life Sciences & Earth Sciences (AREA)
- Health & Medical Sciences (AREA)
- Complex Calculations (AREA)
Abstract
本公开实施例提供一种模型权重存储、计算处理方法及设备、存储介质和程序,该方法包括:获取目标网络层的量化权重矩阵、量化参数矩阵和存储非量化权重矩阵的多个存储矩阵,包括按列存储非量化权重矩阵中各非零元素值的第一存储矩阵,存储各非零元素值在非量化权重矩阵中的行号的第二存储矩阵,存储非量化权重矩阵中各列的非零元素值数量的第三存储矩阵,存储非量化权重矩阵中各列在第一存储矩阵中对应的起始索引位置的第四存储矩阵。根据多个存储矩阵确定输出矩阵中同一位置的第一计算结果和第二计算结果,第一计算结果为输入矩阵与反量化权重矩阵的乘积,第二计算结果为输入矩阵与非量化权重矩阵的乘积。通过该方案可以实现矩阵乘法的加速计算。
Description
模型权重存储、 计算处理方法及设备、 存储介质和程序 本公开要求于 2024年 01 月 23 日提交中国专利局、 申请号为 202410102457. 7、 申请 名称为 “模型权重存储、 计算处理方法及设备、 存储介质和程序 ” 的中国专利申请的优先 权, 其全部内容通过引用结合在本公开中。 技术领域 本公开涉及人工智能技 术领域, 尤其涉及一种模型权重存储、 计算处理方法及设 备、 存储介质和程序。 背景技术 随着人工智 能技术的不断发展, 权重值规模更大的神经网络模型层出不穷, 比如大语 言模型等。 神经网络模型包含若干网络层 (比如卷积层、 全连接层等等) , 不同网络层对 应有不同的权重矩阵。 神经网络模型训练完成后得到的各网络层的权重矩阵中的权重值一 般是采用某种浮点数的数据格式, 比如 fp 16格式。按照这种数据格式进行权重矩阵的存储, 将会占用更大的存储空间。 为节省存储空间, 对于神经网络模型的各网络层的权重矩阵通常需要进行量化处理, 比如采用近乎无损的 LLM (Large Language Mode I , 大语言模型) 权重压缩的稀疏量化表 示 (A Sparse-Quant i zed Representat i on for Near-Loss I ess LLM We i ght Compress i on, 简 称 SpQR) 量化方法进行量化处理。 以某神经网络模型中的任一网络层来说, 该网络层的权 重矩阵的量化结果包括量化权重矩阵、 量化参数矩阵和非量化权重矩阵, 量化参数矩阵包 括零值矩阵和量化尺度矩阵。 其中, 量化权重矩阵由权重矩阵中需要进行量化的权重值的 量化结果构成, 量化过程采用上述量化参数矩阵实现, 而非量化权重矩阵由权重矩阵中不 需要进行量化处理的权重值构成。 不需要进行量化的权重值占比非常小 (通常小于 1 %) , 所以非量化权重矩阵为稀疏矩阵。 量化权重矩阵、 量化参数矩阵和非量化权重矩阵中元素 值所采用的数据格式不相同, 比如量化权重矩阵中元素值为 i nt4格式的, 非量化权重矩阵 中元素值为 fp16格式的, 零值矩阵中给出的各零值(zero)为 i nt4格式的, 量化尺度矩阵中 给出的各量化尺度 (sca l e) 为 fp16格式的。 对于非稀疏矩 阵的上述量化权重矩阵和量化参数矩阵, 可以按照行的顺序, 将相应矩 阵中的元素值逐行地存储到存储空间中即可, 而由于非量化权重矩阵是个稀疏矩阵, 即其 中仅有少量元素值是非零的, 如果按照量化权重矩阵、 量化参数矩阵的连续存储方式, 将 该非量化权重矩阵中各行元素连续地存储下来, 将会占用很多存储空间。 因此, 为了节省 存储空间, 针对非量化权重矩阵, 通常采用 coo (coord i nate format) 存储结构来进行存
储。 在 coo存储结构中, 采用三元组 ( row, co l , data ) 来存储非量化权重矩阵中的非零元 素, 其中 row, co l分别是非零元素的行号和列号, data即为该非零元素的值。 虽然采用 c。。存储结构可以降低存储非量化权重矩阵时对存储空间的占 用, 但是 这样却不利 于在神经网络模型的推理 阶段加速完成矩阵乘法的计算 , 该矩阵乘法是指 神经网络模型 中的任一网络层的输入矩阵 与该网络层的权重矩阵之间 的乘法计算。 发明内容 本公开实施例提供一种模型权重存储 、 计算处理方法及设备、 存储介质和程序, 用以 提高矩阵乘法的计算效率。 第一方面, 本公开实施例提供一种模型权重计算处理方法, 所述方法包括: 响应于基于推理任务得到的神经 网络模型中目标网络层的输入矩阵的输入, 获取所述 目标网络层对应的量化权重矩阵和量化参数矩阵; 获取 用于存储所述目标网络层对应的非量化权重矩阵的多个存储矩阵, 所述多个存储 矩阵中包括用于按列存储所述非量化权重矩阵中各非零元素值的第一存储矩阵, 用于存储 所述非量化权重矩 阵中各非零元素值在所述非量化权重矩阵中对应的行号 的第二存储矩 阵, 用于存储所述非量化权重矩阵中各列的非零元素值数量的第三存储矩阵, 用于存储所 述非量化权重矩阵中各列在所述第一存储矩阵中对应的起始索引位置的第四存储矩阵; 其 中, 所述非量化权重矩阵为稀疏矩阵, 所述目标网络层的权重矩阵的量化结果包括所述量 化权重矩阵、 所述量化参数矩阵和所述非量化权重矩阵; 根据 所述多个存储矩阵确定所述目标网络层的输出矩阵 中同一位置对应的第一计算 结果和第二计算结果, 以根据所述第一计算结果和所述第二计算结果的累加和得到所述同 一位置的目标计算结果, 其中, 所述第一计算结果对应于所述输入矩阵与反量化权重矩阵 的乘积, 所述第二计算结果对应于所述输入矩阵与所述非量化权重矩阵的乘积, 所述反量 化权重矩阵是基于所述量化参数矩阵对所述量化权重矩阵进行反量化处理得到的。 第二方面, 本公开实施例提供一种模型权重计算处理装置, 所述装置包括: 获取模块 , 用于响应于基于推理任务得到的神经网络模型中目标网络层的输入矩阵的 输入, 获取所述目标网络层对应的量化权重矩阵和量化参数矩阵, 以及获取用于存储所述 目标网络层对应的非量化权重矩阵的多个存储矩阵, 所述多个存储矩阵中包括用于按列存 储所述非量化权重矩阵中各非零元素值的第一存储矩阵, 用于存储所述非量化权重矩阵中 各非零元素值在所述非量化权重矩阵中对应的行号的第二存储矩阵, 用于存储所述非量化 权重矩阵中各列的非零元素值数量的第三存储矩阵, 用于存储所述非量化权重矩阵中各列 在所述第一存储矩阵中对应的起始索引位置的第四存储矩阵; 其中, 所述非量化权重矩阵 为稀疏矩阵, 所述目标网络层的权重矩阵的量化结果包括所述量化权重矩阵、 所述量化参 数矩阵和所述非量化权重矩阵; 推理模块 , 用于根据所述多个存储矩阵确定所述目标网络层的输出矩阵中同一位置对
应的第一计算结果和第二计算结果, 以根据所述第一计算结果和所述第二计算结果的累加 和得到所述同一位置的目标计算结果, 其中, 所述第一计算结果对应于所述输入矩阵与反 量化权重矩阵的乘积, 所述第二计算结果对应于所述输入矩阵与所述非量化权重矩阵的乘 积, 所述反量化权重矩阵是基于所述量化参数矩阵对所述量化权重矩阵进行反量化处理得 到的。 第三方面, 本公开实施例提供一种电子设备, 包括: 存储器、 处理器、 通信接口; 其 中, 所述存储器上存储有可执行代码, 当所述可执行代码被所述处理器执行时, 使所述处 理器至少可以实现如第一方面所述的模型权重计算处理方法。 第四方面, 本公开实施例提供了一种非暂时性机器可读存储介质, 所述非暂时性机器 可读存储介质上存储有可执行代码, 当所述可执行代码被电子设备的处理器执行时, 使所 述处理器至少可以实现如第一方面所述的模型权重计算处理方法。 另外, 本公开实施例提供了一种计算机程序产品, 所述计算机程序产品中包含计算机 程序, 当其被电子设备的处理器执行时, 使所述处理器至少可以实现如第一方面所述的模 型权重计算处理方法。 第五方面, 本公开实施例提供了一种模型权重存储方法, 包括: 获取神经 网络模型中目标网络层的权重矩阵的量化结果, 所述量化结果中包括量化权 重矩阵、 量化参数矩阵和非量化权重矩阵, 所述非量化权重矩阵为稀疏矩阵; 生成 用于存储所述非量化权重矩阵的多个存储矩阵, 所述多个存储矩阵中包括用于按 列存储所述非量化权重矩阵中各非零元素值的第一存储矩阵, 用于存储所述非量化权重矩 阵中各非零元素值在所述非量化权重矩阵中对应的行号的第二存储矩阵, 用于存储所述非 量化权重矩阵中各列的非零元素值数量的第三存储矩阵, 用于存储所述非量化权重矩阵中 各列在所述第一存储矩阵中对应的起始索引位置的第四存储矩阵; 存储所述 多个存储矩阵、 所述量化权重矩阵和所述量化参数矩阵。 第六方面, 本公开实施例提供了一种模型权重存储装置, 包括: 获取模块 , 用于获取神经网络模型中目标网络层的权重矩阵的量化结果, 所述量化结 果中包括量化权重矩阵、 量化参数矩阵和非量化权重矩阵, 所述非量化权重矩阵为稀疏矩 阵; 生成模块 , 用于生成用于存储所述非量化权重矩阵的多个存储矩阵, 所述多个存储矩 阵中包括用于按列存储所述非量化权重矩阵中各非零元素值的第一存储矩阵, 用于存储所 述非量化权重矩阵中各非零元素值在所述非量化权 重矩阵中对应的行号的第二存储矩阵, 用于存储所述非量化权重矩阵中各列的非零元素值数量的第三存储矩阵, 用于存储所述非 量化权重矩阵中各列在所述第一存储矩阵中对应的起始索引位置的第四存储矩阵; 存储模块 , 用于存储所述多个存储矩阵、 所述量化权重矩阵和所述量化参数矩阵。 第七方面, 本公开实施例提供一种电子设备, 包括: 存储器、 处理器、 通信接口; 其 中, 所述存储器上存储有可执行代码, 当所述可执行代码被所述处理器执行时, 使所述处
理器至少可以实现如第五方面所述的模型权重存储方法。 第八方面, 本公开实施例提供了一种非暂时性机器可读存储介质, 所述非暂时性机器 可读存储介质上存储有可执行代码, 当所述可执行代码被电子设备的处理器执行时, 使所 述处理器至少可以实现如第五方面所述的模型权重存储方法。 第九方面, 本公开实施例提供了一种计算机程序产品, 所述计算机程序产品中包含计 算机程序, 当其被电子设备的处理器执行时, 使所述处理器至少可以实现如第五方面所述 的模型权重存储方法。 本公开实施例提供 的模型权重计算处理方案, 对于神经网络模型的各网络层的权重矩 阵首先进行量化处理, 以节省存储空间。 以任一目标网络层来说, 目标网络层的权重矩阵 的量化结果包括量化权重矩阵、 量化参数矩阵和非量化权重矩阵。 非量化权重矩阵是个稀 疏矩阵, 为了节省存储空间同时保证目标网络层的输入矩阵与该权重矩阵的矩阵乘法计算 速度, 提供了一种新的存储非量化权重矩阵的存储结构。 具体地, 生成用于存储非量化权 重矩阵的多个存储矩阵, 其中包括用于按列存储非量化权重矩阵中各非零元素值的第一存 储矩阵, 用于存储非量化权重矩阵中各非零元素值在非量化权重矩阵中对应的行号的第二 存储矩阵, 用于存储非量化权重矩阵中各列的非零元素值数量的第三存储矩阵, 用于存储 非量化权重矩阵中各列在第一存储矩阵中对应的起始索引位置的第四存储矩阵。 由此可见, 通过上述 4个存储矩阵实现了非量化权重矩阵中各非零元素值以及各非零元素值的位置坐 标 (行号、 列号) 在不同存储矩阵中的连续存储, 将这 4个存储矩阵如量化权重矩阵一样 正常连续地存储到存储空间即可。 当需要计算目标网络层 的输入矩阵与权重矩阵的 乘积时, 根据上述多个存储矩阵 确定作为乘积 结果的输出矩阵中同一位 置对应的第一计算结果和 第二计算结果, 以根 据第一计算 结果和第二计算结果的 累加和得到该同一位置的 目标计算结果。 其中, 第 一计算结 果对应于输入矩阵与反量化权 重矩阵的乘积, 反量化权重矩阵是基于量化参 数矩阵对量 化权重矩阵进行反量化处理 得到的, 第二计算结果对应于输入矩阵与非量 化权重矩 阵的乘积。 其中, 第二计算结果的计算过程中, 可以基于上述 4个存储矩阵 实现输入矩 阵与非量化权重矩阵的加速 乘法计算, 从而提升目标网络层的输入矩阵与 权重矩阵 的乘法计算效率。 附图说明 为了更清楚地说明本公开实施例中的技术方案, 下面将对实施例描述中所需要使用的 附图作一简单地介绍, 显而易见地, 下面描述中的附图是本公开的一些实施例, 对于本领 域普通技术人员来讲, 在不付出创造性劳动的前提下, 还可以根据这些附图获得其他的附 图。 图 1为本公开实施例提供的一种传统的基于权重矩阵量化结果进行矩阵乘法计算的原 理示意图;
图 2为本公开实施例提供的一种模型权重存储方法的流程图; 图 3为本公开实施例提供的一种用于存储非量化权重矩阵的新存储结构的示意图; 图 4为本公开实施例提供的一种权重矩阵量化结果的存储方式的示意图; 图 5为本公开实施例提供的一种模型权重计算处理方法的流程图; 图 6为本公开实施例提供的一种模型权重计算处理装置的结构示意图; 图 7为本公开实施例提供的一种模型权重存储装置的结构示意图; 图 8为本公开实施例提供的一种 电子设备的结构示意图。 具体实施方式 为使本公开实施例的目的、 技术方案和优点更加清楚, 下面将结合本公开实施例中的 附图, 对本公开实施例中的技术方案进行清楚、 完整地描述, 显然, 所描述的实施例是本 公开一部分实施例, 而不是全部的实施例。 基于本公开中的实施例, 本领域普通技术人员 在没有作出创造性劳动前提下所获得的所有其他实施例, 都属于本公开保护的范围。 需要说明的是, 本公开实施例中所涉及的用户信息 (包括但不限于用户设备信息、 用 户个人信息等) 和数据 (包括但不限于用于分析的数据、 存储的数据、 展示的数据等) , 均为经用户授权或者经过各方充分授权的信息和数据, 并且相关数据的收集、 使用和处理 需要遵守相关国家和地区的相关法律法规和标准, 并提供有相应的操作入口, 供用户选择 授权或者拒绝。 下面结 合附图对 本公开的 一些实施 方式作详 细说明。 在各实施例之间不 冲 突的 情况下, 下述的实施例及实 施例中的 特征可以 相互组合 。 另外, 下述各方 法实 施例中的 步骤时序仅 为一种举例 , 而非严格限定。 大语 言模型的火 热发展, 在众多应用场景 中都被广泛 使用, 然而模型的 参 数量 动则达到 百亿以上 , 这使得常规的消 费级显卡单 卡无法进 行模型推 理。 因 此, 通过量化的方法进 行模型参 数的压缩 存储成为 一种主流 方法, 而其中主要 的模 型参数即 为模型中多 个网络层的 权重矩阵 o SpQR是一种比较好的量化方法 , 但是 由于该方 法中保留 了 1 %以内的权重值 (作为 o ut I i ers , 即异常值) 不进行 量化 , 这种量化方式会导 致无法快速 推理。 可以理 解的是 , 在推理阶段, 会基于当前的 推理任务 逐层地产 生每个 网络 层的 输入矩阵 , 每个网络层的输 入矩阵与该 网络层的 权重矩 阵的矩阵乘 法计算 结果 作为该 网络层的输 出矩阵, 进而输入 下一网络层 。 输入矩阵与相应 权重矩 阵的 矩阵乘法 计算速度 直接影响 推理阶段 的效率, 所以加速输 入矩阵 与权重矩 阵的 矩阵乘法计 算速度是 必要的。 下面先 结合图 1 示意采用 SpQR量化方法对一权重矩 阵进行量化 得到的结 果, 以及 推理时的计 算方法。 如图 1 中所示, 假设神经网络模型 (比如大语言模型) 的某个网络层的输
入矩 阵为 X,权重矩阵 为 W,两者进行矩阵 乘法计算最 终得到的输 出矩阵为 out。 其 中, 假设 X 的维度为: x_row*x_co I umn , 并且其中每个元素值的数据格式为 fp16 格式; 假设权重矩阵 W的维度为 : w_row*w_co I umn, 并且其中每个元素值 (即权重值) 的数据格式为 fp16格式。 可以 理解的是 , 为了降低权重矩 阵对存储 空间的占 用, 在推理阶段之前 , 即 比如在神经 网络模型 训练完毕 之后, 便可以对神 经网络模型 中各网络 层的权 重矩 阵分别进 行量化处 理, 将量化结果存 储到显存 空间中, 所以在想要得到输 入矩 阵 X与权重 矩阵 W的乘积时, 并非直接进行 输入矩阵 X与权重矩 阵 W的相 乘处 理, 因为实际并未存 储权重矩 阵 W, 而是存储的权重 矩阵 W的量化 结果。 如图 1 中所示, 假设对权重矩阵 W进行 SpQR量化处理 后得到 了图中示意的 量化 权重矩阵 W' , 量化参数矩阵以及非 量化权重 矩阵 0, 其中, 量化参数矩阵 包括 零值矩阵 Z和量化尺度 矩阵 S, 其中, 量化尺度矩阵也可以称 为量化系数 矩 阵 。 其中 ,量化权重矩阵 W' 中包含权重矩阵 W中需要进行量化 处理的各 个权重 值的 量化结果 。 在量化前, 这些权重值的数据 格式是 fp16, 量化后, 这些权重 值的 量化结果 的数据格式 为 i rrt4(不以此为限, 还可以是长度小于 fp16的其他 整型 格式) 。 非量化权重矩阵 0 中包含权重矩阵 W 中不需要进行 量化处理 的各 权重 值 (这些值也称为异常值即 out I i ers值) , 从而这些权重值保持 f p16的 数据 格式。如图 1 中所示,量化权重矩阵 W'和非量化权重矩阵 0的原始维度 为: w_row*w_co I umn , 但是实际上, 量化权重矩阵 W' 中对应于不需要进行量化的权 重值 的位置可 以设置为某 种设定值 , 而非量化权重矩阵 0 中对应于需要量 化的 权重 值的位置 可以设置 为零, 由此可见, 非量化权重矩 阵 0 实际上是一个仅有 少量 元素值 为非零值的 稀疏矩阵 , 图中示意的矩形 条即为不 同列中存 在的多个 非零 元素值。 而权重 矩阵 W 中哪些位置的权重值 需要进行 量化处理 , 亦即哪些位置的权 重值 不需要进 行量化处理 , 可以采用现有相 关方法确定 , 在此不展开赘述。 在产生 上述量化 权重矩阵 W' 的过程中, 会先计算出量化参数矩 阵, 即零值 矩 阵 Z和量化尺度 矩阵 S, 以便基于量化参数矩阵完 成量化处 理。 其中, 零值矩 阵 Z给出了在量 化过程中 , 量化权重矩阵 W' 中各元素值对应的零值 (zero) , 即每 个需要进 行量化的权 重值所对应 的零值 ; 类似地, 量化尺度矩阵 S 给出了 在量 化过程中 , 量化权重矩阵 W' 中各元素值对应的量 化尺度 (sca l e) , 即每 个需 要进行量化 的权重值 所对应的量 化尺度 。 在实 际应用中 , 为简化计算复杂度 , 不需要每个需要 进行量化 的权重值 独 立地 对应有 自己的零值和 量化尺度 , 而是可以对权重矩 阵 W 中的权重值进行分 组 , 每组共用同一个零 值和量化 尺度, 比如按照行 进行分组 : 比如同一列中的
每 128行为一组。 所以可以预先 设定一个分 组长度 : groups i ze, 上述举例中其 取值 为 128 o 基于 此, 可以理解的是 , 为了降低量化参数 矩阵的存 储对存储 空间的 占用 量 , 如图 1 中所示, 实际存储的零值矩阵 Z和量化尺度矩 阵 S的维度 可以是 : 行数 为 w row/groups i ze , 列数为 w_co I umn, 即每个分组仅存储一个零值和量 化尺 度。 在推理计算时再 恢复出原始 的维度即 可, 即 w_r ow*w_co I umn o 举例 来说 , 假设 w_row=5120 , w_co I umn=15360 , groups i ze=128 , 那么 w row/groups i ze=40, 从而零值矩阵 Z中的列 i 中包含 40个零值, 第一个零值 对应 于量化权 重矩阵 VT 中列 i 中的前 128个元素, 第二个零值对应于量化权重 矩 阵 W' 中列 i 中的第二组内的 128 个元素, 以此类推。 在推理阶段还原回 w_row=5120 , w_co I umn=15360的维度, 即将上述第一个零值复制 128份, 形成 列 i 中的第一组内的 128行元素, 以此类推。 量化尺度矩阵 S同理 。 如图 1 中所示,零值矩阵 Z中各零值的数 据格式与 量化权重矩 阵 W' 中各元 素值 的数据格 式一致, 为 i nt4格式, 而量化尺度矩阵 S中各量化尺度 的数据格 式与 权重矩阵 W中各权重值 的原始数据 格式一致 , 为 fp16格式。 由 矩 阵乘法 原 理可 知 , 输 出 矩阵的维 度 为 : out_row=x_row , out co I umn=w_co I umn。 基于上 述权重矩 阵 W 的量化结果, 输出矩阵的计算过程 可以表示 为如下公 式 : k<x_column
k = 0 输出矩 阵 out 的第 i 行第 j 列元素值的计算公式如上 所示, 具体为第一计 算结 果与第二 计算结果的 加和, 其中, 第一计算结果反映的是对 量化权重 矩阵 W' 进行 反量化处 理后与输入 矩阵 X相乘, 第二计算结果反 映的是输 入矩阵 X 与非 量化 权重矩阵 0相乘。 其中, 反量化处理可以简 单表示为 : (W' -Z) *So 针 对上述量化权重矩阵 W' 、 零值矩阵 Z和量化尺度矩阵 S来说, 这几个矩阵都是非稀 疏矩阵, 在进行存储的时候, 可以按照行的顺序, 将相应矩阵中的元素值逐行地存储到存 储空间中即可。 以量化权重矩阵 W' 为例, 可以在显存中划分一段连续的存储空间, 将量 化权重矩阵 中第一行元素值依次存入该存储空间, 之后, 将第二行元素值接着依次存 入该存储空间, 以此类推, 基于此, 当想要查询该量化权重矩阵 W' 中的第 i行第 k列的元 素值时, 可以基于 “行号 *总列数+列号" 的位置索引在该存储空间中找到该元素值, 即读 取该存储空间内 " i*w_co I umu+k”这个存储位置的元素值即为量化权重矩阵 W' 中的第 i行 第 k列的元素值。 零值矩阵 Z和量化尺度矩阵 S同理, 不赘述。 而由于非量化权重矩阵 0是个稀疏矩阵, 即其中仅有少量元素值是非零的, 如果按照 量化权重矩阵 W' 等非稀疏矩阵的上述连续存储方式, 将该非量化权重矩阵 0中各行元素连
续地存储下来,将会占用很多存储空间。因此, 为了节省存储空间,针对非量化权重矩阵, 通常采用 coo (coord i nate format ) 存储结构来进行存储。 在 coo存储结构中, 采用三元 组 ( row, co l , data) 来存储非量化权重矩阵中的非零元素值, 其中 row, co l分别是非零元 素值的行号和列号, data即为该非零元素值。 虽然采用 c。。存储结构可以降低存储非量化权重矩阵时对存储空间的占用, 但是这样 却不利于在神经网络模型的推理阶段加速完成输入矩阵与权重矩阵的乘法计算。 因为在显 存中存储的不再是非量化权重矩阵 0中每行的全部元素值, 便无法再基于上述 “行号 *总列 数+列号”的遍历方式快速地找到当前计算 014% •时需要从非量化权重矩阵 0中读取的元素值。 而使用诸如 CUDA (Compute Un i f i ed Dev i ce Arch i tecture,统一计算设备架构) 等加速方 式来实现矩阵乘法的加速计算的方案中, 都是基于能够基于元素值的下标 (即行列号) 在 其存储空间中基于上述遍历方式遍历找到该元素值这一前提。 在基于 coo存储结构存储非量化权重矩阵 0时, 由于存储下来的是只是非零元素值的下 标索引和数值, 那么在进行矩阵乘法的计算时, 比如进行 Xik * 0kj的计算时, 根据上述连 续存储方式找到 X诙后, 需要在该 coo存储结构中先查询有无 (k, j ) 这个索引项, 如果有, 则进一步查询这个索引项对应的元素值, 而如果没有这个索引项, 说明此次计算是无需计 算的, 那么查找 X及的操作是无用的, 这都不利于实现快速的计算。 基于此 , 本公开实施例提供 了一种新 的用于存储 非量化权重 矩阵 0 的存储 结构 , 基于该新的存储结 构, 不仅可以降低存储 非量化权 重矩阵 0 时对存储空 间的 占用量, 而且还可以实现输入矩 阵与权重 矩阵的加速 乘法计算 。 基于下 述实施例 对该新的 存储结构 以及基于 该新的存 储结构的矩 阵乘法计 算过 程进行示 例说明, 以下实施例提供的 方法可以 由某电子设 备中包含 的图像 处理 器 ( Graph i cs Process i ng Un i t , 简称 GPU) 来执行, 该电子设备可以是诸 如 PC机、笔记本电脑、智能手机 等终端设 备, 也可以是服务器, 比如云服务器。 图 2为本公开实施例提 供的一种 模型权重 存储方法 的流程图,如图 2所示, 该方 法可以包括 如下步骤 :
201、 获取神经网络模型 中目标 网络层的权重 矩阵的量化 结果, 量化结果中 包括 量化权重 矩阵、 量化参数矩 阵和非量 化权重矩 阵, 非量化权重矩 阵为稀疏 矩阵 。
202、 生成用于存储非量 化权重矩 阵的多个存 储矩阵, 多个存储矩阵中包括 用于 按列存储 非量化权 重矩阵中 各非零元 素值的第 一存储矩 阵, 用于存储非量 化权 重矩阵 中各非零元 素值在非 量化权重 矩阵中对 应的行号 的第二存 储矩阵, 用于 存储非量 化权重矩 阵中各列 的非零元 素值数量 的第三存 储矩阵, 用于存储 非量 化权重矩 阵中各列 在第一存 储矩阵中对 应的起始 索引位置 的第四存 储矩阵 。
203、 存储多个存储矩 阵、 量化权重矩阵和 量化参数矩 阵。 本实施 例中, 假设目标网络层 的权重矩阵 为图 1 中示意的权重矩阵 W, 则其
量化 结果包括量 化权重矩 阵 W' 、非量化权重矩阵 0以及由零值矩 阵 Z和量化尺 度矩 阵 S构成的量化 参数矩 阵。 其中, 量化权重矩阵 W' 、 零值矩阵 Z和量化尺 度矩 阵 S 这些非稀疏矩阵的存储 方式采用 前文介绍 的 “连续存储” 方式进行存 储, 简单来说就是在显 存中分别 划分用于 存储这三个 矩阵的三 块存储 空间, 每 个存 储空间的 存储地址 是连续的 , 每个矩阵中的元 素逐行地 存入相应 的存储空 间 中。 而针对 作为稀疏矩 阵的非量 化权重矩 阵 0,采用由上述 四个存储矩 阵构成的 新的 存储结构进 行存储 。 实际上, 这四个存储矩阵 可以是四个 行向量 。 首先需 要说 明的是, 因为非量化权重矩阵 0 是稀疏矩阵, 实际上参与计算的仅仅 是其 中的 非零元素值 。 具体地 , 第一存储矩阵 (可以表示为 out I i ers va I) , 用于按列存储非量化 权重 矩阵中各非 零元素值 , 这些非零元素值即 为权重矩 阵 W 中不需要进行量化 的权 重值, 在上文的举例 中, 为 fp16格式的浮点数。 所以第一存储 矩阵就是 用 于存 储非量化权 重矩阵 0包含的所 有非零元素 值 (亦即 out I i ers值), 存储顺 序为 先逐列进行 遍历, 针对每一列, 按照其中第一行 (row1)、 第二行(row2) … 的逐 行顺序进行 存储。 第二存 储矩阵 (可以表示为 out I i ers row) , 用于存储非量化权重矩阵 0中 各非 零元素值在 非量化权 重矩阵 0 中对应的行号。 由此可知, 第二存储矩阵的 维度 与第一存储 矩阵的维 度是相等的 , 假设非量化权重矩 阵 0 中一共有 M个非 零元 素值,那么第一存 储矩阵的 维度可以是 : 1 *M,第二存储矩阵的维度同样是: 1 *M, 用于记录第一存储矩 阵中每个 非零元素值 在非量化 权重矩阵 0中真实对应 的行 号。 在第二存储矩 阵中, 每个行号可 以采用比如 un i t32数据格式来表示。 第三存 储矩阵 (可以表示为 out I i ers co I umn num) , 用于存储非量化权重 矩阵 0 中各列的非零 元素值 (如上文所述, 是指非零元素值) 的数量。 由于非 量化 权重矩阵 0的列数即为 权重矩阵 W的列数 w_co I umu, 所以第三存储矩阵的 维度 是: 1 *w_co l umu。 第四存 储矩阵 (可以表示为 out I i ers co I umn start) , 用于存储非量化权 重矩 阵 0 中各列在第一存 储矩阵中对 应的起始 索引位置 。 实际上, 第 n列对应 的起 始索引位置 根据第 n-1 列对应的起始索引位 置和第 n-1 列包含的非零元素 值数 量确定, 而起始列 (即列号为 0的第一列: 列 0) 的起始索引位置为 0。 其 中, 第一存储矩阵的位置 索引范围 为 [0, M-1 ] , 其中, M 为第一存储矩阵中存储 的非 零元素值数 量。 在得到 非量化权重 矩阵 0 对应的上述四个存 储矩阵后 , 由于这四个存储矩 阵是 非稀疏矩 阵, 从而可以如量化权重 矩阵 W' 等非稀疏矩阵一样, 采用 “连续 存储” 的方式进 行存储, 即在显存中分配分 别用于存储 这四个存 储矩阵的 四个
存储 空间, 每个存储空 间是地址 连续的, 将一存储矩阵中包含 的元素值 依次存 入对 应的存储 空间即可。 为了便 于理解, 下面结合图 3来示例说 明上述四个 存储矩阵 的存储内容 。 在图 3 中, 假设非量化权重矩阵 0是一个 5*5的矩 阵, 从而其行号和列号 的取 值范围均 为 [0, 4] , 为便于描述, 将元素值在该矩阵中的位置坐标的形式表 示为 : ( rowi , co I umn j) , 从而如图 3中所示, 假设 (2, 1) 位置的元素值为 1 . 0, (3, 4) 位置的元素值为 3. 0, (4, 4) 位置的元素值为 2. 0, 其他位置的元素值 均为 0。 基于上述假设生成如下 四个存储矩 阵: 第一存 储矩阵 out l i ers_va l = [1. 0, 3. 0, 2. 0] , 即按照列的遍历顺序, 每列 中逐 行地将该非 量化权重 矩阵 0中的非 零元素值存 储到第一 存储矩阵 中。 第二存 储矩阵 out I i ers_row= [2, 3, 4] , 即依次存储第一存储矩阵中每个非 零元 素值在该非 量化权重 矩阵 0中对应 的行号, 其中, 2为元素值 1. 0对应的行 号, 3为元素值 3. 0对应的行号, 4为元素值 2. 0对应的行号。 第三存 储矩阵 out I i ers co I umn_num= [0, 1 , 0, 0, 2] ,即存储非量化权重矩阵 0 中每一列 包含的非零 元素值数量 。 其中, 第一列 (即列 0) 中不包含非零元素 值, 所以对应的数量为 0, 第二列 (即列 2) 中包含 1 个非零元素值 (为 1 . 0), 所 以对应的数量 为 1 , 同理, 第三列和第四列中不包含非零元 素值, 所以对应的 数量 为 0, 第五列 (即列 4) 中包含 2个非零元素值 (为 3. 0和 2. 0), 所以对应 的数 量为 2o 第四存 储矩阵 out I i ers co I umn_start= [0, 0, 1 , 1 , 1 ] ,即存储非量化权重矩 阵 0 中各列在第一存 储矩阵中对 应的起始 索引位置 。 其中, 假设第一存储矩阵 中存 储有 M 个元素值, 则其位置索引范 围为 [0, M-1 ] , 基于此, 可以理解的是, 非量 化权重矩 阵 0中第一列 (列 0) 中不管有没有非零元素, 其在第一存储矩阵 中对 应的起始 索引位置都 是 0。而由于上述举例中非量 化权重矩 阵 0中第一列(列 0) 没有非零元素 值, 所以第二列 (列 1) 在第一存储矩阵中对应的起始位 置索 引为 0+0=0, 其中, 相加的第一个 0是指前一列对 应的起始位 置索引, 相加的第 二个 0是指前一列 中包含的 非零元素值 数量。 类似地, 第三列 (列 2) 在第一存 储矩 阵中对应 的起始位置 索引为: 0+1 =1 , 即由前一列的起始位置索引 0和前一 列包 含的非零 元素值数量 1 的加和结果确定。 第四列 (列 3) 在第一存储矩阵中 对应 的起始位置 索引为 : 1 +0=1 o 第五列 (列 4) 在第一存储矩阵中对应的起始 位置 索引为: 1 +0=1 o 以上介 绍了本公 开实施例提 供的一种 新的用于存 储非量化权 重矩阵 0 的存 储结 构, 基于该存储结 构可以实现 输入矩 阵与权重矩 阵的乘 法的加速计 算。 具 体的 计算过程将 在后续实 施例中介 绍。 如上文 所述, 非量化权重矩 阵 0 可以采用上述新的存 储结构来存 储, 而权
重矩 阵 W量化后得到的 量化权重矩 阵 W ' 和量化参数矩阵可以基于 前文中介绍 的 方式 存储。 在一可选实施例 中, 为进一步节省量化权 重矩阵 W' 和量化参数矩阵 对存 储空间的 占用,还可以通过 如下方式 来存储量化 权重矩阵 W ' 和量化参数矩 阵 : 获取 目标网络层的权重矩阵的初始量化权重矩阵和初始量化参数矩阵, 初始量化参数 矩阵中包括初始零值矩阵, 初始量化权重矩阵和初始零值矩阵具有相同的维度且元素值具 有相同的第一数据格式; 对初始量化权 重矩阵按照行进行分组, 得到分组后量化权重矩阵, 其中, 初始量化权 重矩阵中 同一列对应的组内元素值拼接为第二数据格式的元素值 以存储到分组后量化权 重矩阵中; 对初始零值矩 阵按照列进行分组, 得到分组后零值矩阵, 其中, 初始零值矩阵中同一 行对应的组内元素值拼接为第二数据格式的元素值以存储到分组后零值矩阵, 初始量化权 重矩阵和初始零值矩阵按照相同分组长度进行分组处理; 存储分组后量化权重矩阵和分组后零值矩阵。 为便于理解, 结合图 4来示例说明上述存储方式。 假 设初始 量化权 重矩阵 为 W1 , 初始零值矩阵 为 Z1 , 且假设其维度均为: w_row*w_co I umn, 且其元素值均为 i nt4格式 (第一数据格式) 。 在 一可选实施例中, 可以如上文所述, 对初始零值矩阵 Z1按照设定的 groups i ze进行 基于行的分组, 对应于同一列的每个行分组内的零值是相同的, 仅存储一个即可。 具体参 考图 1中的相关示意。 需要说明的是, 图 4所示实施例对应的存储方式, 既适用于对初始零值矩阵 Z1进行基 于 groups i ze的分组存储也可以适用于不对初始零值矩阵 Z1进行基于 groups i ze的分组存 储的情形, 图 4中示意的是进行了 groups i ze分组存储的情形。 在 图 4中, 设置初始量化权重矩阵 W1和初始零值矩阵 Z1采用的相同分组长度 =8, 对初 始量化权重矩阵 W1按照行进行分组, 得到分组后量化权重矩阵 W2, 对初始零值矩阵 Z1按照 列进行分组, 得到分组后零值矩阵 Z2。 如图 4中所示, 经过该分组处理后, 分组后量化权 重矩 阵 W2的维度变为: w_row/8 *w_co I umn , 分组后量化权重矩阵 W2的维度变为: w_row/ groups i ze *w_co l umn/8。 其中, 初始量化权重矩阵 W1中同一列对应的组内元素值拼接为第二数据格式的元素值 以存储到所述分组后量化权重矩阵 W2。 以初始量化权重矩阵 W1中第一列对应的第一组 (前 8行) 内的 8个 i nt4格式的元素值来说, 这 8个 i nt4格式的元素值可以合并为一个 i nt32格式 (第二数据格式) 的数值来存储到分组后量化权重矩阵 W2中的第一行第一列的位置。 其中, 初始零值矩阵 Z1中同一行对应的组内元素值拼接为所述第二数据格式的元素值 以存储到分组后零值矩阵 Z2。 以初始零值矩阵 Z1中第一行对应的第一组 (前 8列) 内的 8个 i nt4格式的元素值来说, 这 8个 i nt4格式的元素值可以合并为一个 i nt32格式的数值来存储
到分组后零值矩阵 Z2中的第一行第一列的位置。 其 中, 8个 i nt4格式的元素值合并为一个 i nt32格式的数值, 简单来说就是将这 8个元 素值依次表示为 4b i t长度的二进制字符串, 那么最终得到一个 32b i t长度的二进制字符串, 将这个二进制字符串进行格式转换, 得到对应的十进制数值。 通过上述存储 方式, 可以进一步降低权重矩阵的量化结果对存储空间的占用。 只是 需要说明的是, 在推理阶段, 当需要进行目标网络层的输入矩阵与权重矩阵的乘 法计算时, 需要从存储空间中加载分组后量化权重矩阵 W2和分组后零值矩阵 Z2, 并将分组 后量化权重矩阵 W2和分组后零值矩阵 Z2, 还原为初始量化权重矩阵 W1和初始零值矩阵 Z1 另外, 针对比如量化尺度矩阵 S, 当进行如图 1中所示的 groups i ze分组的存储处理时, 也 需要还原为初始的维度为 w_row*w_co I umn的矩阵。 下 面介绍目标网络层的输入矩阵与权重矩阵的乘法计算过程。 图 5 为本公开实施例提 供的一种模 型权重计 算处理方 法的流程 图, 如图 5 所示 , 该方法可以包括如 下步骤:
501、 响应于基于推理任务得到的神经网络模型中目标网络层的输入矩阵的输入, 获 取目标网络层对应的量化权重矩阵和量化参数矩阵。
502、 获取用于存储目标网络层对应的非量化权重矩阵的多个存储矩阵, 多个存储矩 阵中包括用于按列存储非量化权重矩阵中各非零元素值的第一存储矩阵, 用于存储非量化 权重矩阵中各非零元素值在非量化权重矩阵中对应的行号的第二存储矩阵, 用于存储非量 化权重矩阵中各列的非零元素值数量的第三存储矩阵, 用于存储非量化权重矩阵中各列在 第一存储矩阵中对应的起始索引位置的第四存储矩阵。
503、 根据多个存储矩阵确定目标网络层的输出矩阵中同一位置对应的第一计算结果 和第二计算结果, 以根据第一计算结果和所述第二计算结果的累加和得到同一位置的目标 计算结果, 其中, 第一计算结果对应于输入矩阵与反量化权重矩阵的乘积, 第二计算结果 对应于输入矩阵与非量化权重矩阵的乘积, 反量化权重矩阵是基于量化参数矩阵对量化权 重矩阵进行反量化处理得到的。 本实施 例中, 以目标网络层 的输入矩 阵 X 与其权重矩阵 W的乘法计算 过程 为例 进行说明 。 其中, 目标网络层的权重矩阵 W 已经被量化处 理, 权重矩阵的 量化 结果包括量 化权重矩 阵 W' 、 量化参数矩阵 (包括上文中的零值矩阵 Z和量 化尺 度矩阵 S) 和非量化权重矩阵 0, 其中, 量化权重矩阵 W' 、 零值矩阵 Z和 量化 尺度矩阵 S 采用上文中的连 续存储方 式进行存储 , 已经存储到显存的 相应 存储 空间, 作为稀疏矩阵 的非量化权 重矩阵 0 采用前述实施例 介绍的新存 储结 构进 行存储, 将对应的 多个存储矩 阵已经存 入显存 中相应存储 空间。 作为计算 结果 的输出矩 阵表示为 out o 基于推 理任务的 输入, 神经网络模 型中的每 个网络层会 依次得 到自己的输 入矩 阵。 比如推理任务 是预测输 入句子中 某位置上 应该填充 什么词语 , 此时,
神经 网络模型 的某网络层 的输入矩 阵可以是 对这个输 入句子进 行某种编 码后得 到的 矩阵, 而该输入矩 阵与该网 络层的权重 矩阵进行 乘法计算 得到的输 出矩阵 可 以作为下一 网络层的 输入矩阵 , 当然, 还可以对该输出矩 阵进行其他 一些处 理 (比如降采样等) 后作为下一网络 层的输入矩 阵。 以 目标网络层来说 , 在得到其输入矩阵 X, 需要进行该输入矩阵 X与该目标 网络层 的权重矩 阵 W的乘 法计算时 , 需要先从显存中读取 实际与输 入矩阵 X进 行计 算的多个计 算对象: 量化权重矩 阵 W' 、 量化参数矩阵: 零值矩阵 Z和量化 尺度 矩阵 S , 以及用于存储非量化权重矩 阵 0的多个存 储矩阵, 将这些矩阵加 载 到显 存中预先分 配的目标 缓存空间 。 实际应 用中, 该目标缓存 空间可 以根据神经 网络模型 中规模最 大的网络层 的权 重矩阵的 维度确定 。 因为实际上, 不同网络层对 应的权重 矩阵的维 度可能 不 同, 而该目标缓存空 间是各网 络层共用 的, 即每次计算某 网络层时 , 计算相 关的 上述多种 矩阵都需 要被加载 到该目标 缓存空间 以进行计算 , 所以该目标缓 存空 间至少要 大于最大 权重矩阵 的维度。 另外, 计算过程中产生的中 间结果和 最终 计算结果 (比如上文中的第 一计算结 果、 第二计算结果 、 目标计算结果) 都可 以暂存到该 目标缓存 空间。 如上文 所述, 出矩阵 out的计算公 式如下 : k<x_column
k = 0 根据该计算公 式可知,输出矩阵。 ut中第 i行第 j列的元素值由相加的第一计算结果(相 加的第一项) 和第二计算结果 (相加的第二项) 的加和结果确定, 所以计算过程中可以分 别计算第一计算结果和第二计算结果, 将两个计算结果分别叠加到 OU% •即可。 而且, 由上 述计算公式可知, 第一计算结果和第二计算结果的计算过程中实际涉及到不同矩阵中若干 位置处的元素值的分别计算, 最终累加到一起。 因此, 可以采用诸如 CUDA等并行多线程来 加速计算的方法。 针对 第一计算结果的计算过程来说, 具体地, 可以先确定并行的多个线程各自对应的 计算位置, 其中, 目标线程对应的计算位置包括第一位置标识 i、 第二位置标识 j和第三位 置标识 k, 目标线程是多个线程中任一个。 这几个位置标识的含义参见上述计算公式。 之 后, 目标线程根据第一位置标识 i、 第二位置标识 j和第三位置标识 k, 在输入矩阵 X、 量化 权重矩阵 和量化参数矩阵中查询得到第一元素值、 第二元素值和第三元素值。 其中, 量化参数矩阵包括零值矩阵 Z和量化尺度矩阵 S, 所以第三元素值包括从零值矩阵 Z和量化 尺度矩阵 S中分别查询到的第三元素值。 其 中, 第一元素值对应于输入矩阵 X的以第一位置标识 i作为行号以第三位置标识 k作 为列号的位置, 为 X/。 第二元素值对应于量化权重矩阵 W' 的以第三位置标识 k作为行号以 第二位置标识 j作为列号的位置, 为 W'kj a 第三元素值对应于量化参数矩阵的以第三位置
标识 k作为行号以第二位置标识 j作为列号的位置, 为 Zkj和 Skj。 之后 , 根据第一元素值 x^、 第二元素值 v/缶和第三元素值 Z/cj和 Scj更新输出矩阵的目 标位置。1£心 •的第一计算结果。 具体地, 就是按照上述计算公式将这些元素值进行相应的计 算, 得到当前的第一位置标识 i、 第二位置标识 j和第三位置标识 k下对应的第一计算结果 中的一个被累加的计算子项。 按照类似的计算过程, 基于上述计算公式, 固定第一位置标 识 i和第二位置标识 j不变, 遍历第三位置标识 k的不同取值, 便可以得到多个计算子项, 分别累加到第一计算结果中, 最终得到第一计算结果。 由上述公式可知, k的取值范围由 输入矩阵 X的列数 x_co I umn决定。 以上对对应于第一位置标识 i、 第二位置标识 j和第三位置标识 k的目标线程计算得到 第一计算结果中被累加的一个计算子项的过程进行了说明。 可以理解的是, 针对 on% •对应 的第一计算结果来说, 实际需要的线程数量为 x_co l umn个, 其中的各个线程对应于相同的 第一位置标识 i和第二位置标识 j , 只是对应的第三位置标识 k各不相同。 下 面先仍以上述目标线程为例, 介绍目标线程计算。或订对应的第二计算结果的过程, 然后再介绍线程的设置方法以及线程对应的位置标识的确定方法。 针对 第二计算结果的计算过程来说, 目标线程不再像计算第一计算结果时直接按照位 置标识在相应矩阵中直接读取相应的元素值进行计算即可, 而是需要先根据第二位置标识 j、 第三位置标识 k和多个存储矩阵, 确定非量化权重矩阵 0中的第四元素值, 第四元素值 对应于非量化权重矩 阵 0的以第二位置标识 j作为列号的一列中的目标排序位置上的元素 值, 目标排序位置为第三位置标识 k对应的数值。 另外, 目标线程在输入矩阵 X中确定第五 元素值, 第五元素值对应于输入矩阵 X的以第一位置标识 i作为行号以第四位置标识作为列 号的位置, 第四位置标识对应于第四元素值在非量化权重矩阵 0中的行号。 之后, 目标线 程根据第四元素值和第五元素值的乘积更新输出矩阵的目标位置 OUttj的第二计算结果。 其 中, 具体地, 第四元素值和第五元素值的确定过程包括: 在 第三存储矩阵 out I i ers_co I umn_num中查询非量化权重矩阵 0的以第二位置标识 j作 为列号的目标中包含的非零元素值数量列 out l i ers_co l umn_num [j] o 若该非零元素值数量 大于第三位置标识 k对应的数值, 则在第四存储矩阵 out I i ers_co I umn_start中查询目标列 在第一存储矩阵 out I i ers_va I中对应的起始索引位置: out I i ers_co I umn_start [ j] , 即若 out I i ers_co I umn_num [j] >k, 则找到第 j列在第一存储矩阵 out I i ers_va I中对应的起始索 引位置 out I i ers_co I umn_start [ j] o 而如果 out I i ers_co I umn_num [ j] Wk, 则跳过, 即不 再进行计算。 在 第一存储矩阵 out l i ers_va l中查询目标索引位置对应的第四元素值, 目标索引位置 为起始索引位置和第三位置标识对应的数值之和: out l i ers_co I umn_start [ j] +ko 所以第 四元素值即为: out I i ers_va I [out I i ers_co I umn_start [ j] +k] o 而第四元素值在非量化权 重矩阵 0中对应的真实行号。顷为: out I i ers_row [out I i ers_co I umn_start [j] +k] , 即在第 二存储矩阵 out I i ers_row中找到得到第四元素值在非量化权重矩阵 0中对应的真实行号。
也就是说, 第四元素值是非量化权重矩阵 0中第。顷行, 第 j列的非零元素值 -.Oorowja按照矩 阵乘法的原理, 与其相乘的输入矩阵 X中的第五元素值应该是输入矩阵 X中第 i行第。顷列的 元素值: XiOrow o 基于此, 上述第四位置标识即为 Or。*。 之后 , 将 XiOrow * 0orowj的乘积作为 OU% •的第二计算结果中的一个计算子项, 叠加到 014% •上即可。 由上述计算过程可知, 本公开实施例中, 针对输入矩阵 X与非量化权重矩阵 0的计算不 计算方式本质上来说是按照输入矩阵 X来遍历非量化
0并不是按照连续存储方式完整地将其中各行元素值 连续地进行存储, 所以这个遍历无法实现。 本公开实施例中采用按照非量化权重矩阵 0中 非零元素值的位置来遍历输入矩阵 X的计算思路。 而非量化权重矩阵 0中包含的非零元素值 以及每个非零元素值在非量化权重矩阵 0对应的行号、 列号采用上述多个存储矩阵的存储 结构进行存储, 在进行计算时可以按照上述遍历方法依据该多个存储矩阵查询得到当前线 程所需计算的非零元素值, 并据此确定需要在输入矩阵中查找的元素值, 从而实现了基于 多线程的加速计算。 通过这种方式, 对于非量化权重矩阵 0中第 j列的非零元素值, 不需要 和本次矩阵乘法中量化权重矩阵的计算维度对应上, 从而实现 0(1)复杂的非零元素值的访 存, 从而可以提升推理速度。 下 面对多线程的设置以及线程对应的位置标识的确定方法进行介绍。 具体地 , 根据输出矩阵 out的行数 o ut_row和列数 out_co I umn, 以及输入矩阵 X的列数 x_co I umn, 确定多个线程的数量: out_row*out_co I umn*x_co I umn。 这样就得到了总共需 要的线程数量。 由于实际应用中, GPU上支持的线程标号的上限是有限制的, 比如有些 GPU 上线程标号最大到 1024。 而如果上述线程总数超过该标号上限的限制, 则需要引入线程块 (block) 、 线程格 (grid) 的概念。 在大规模的神经网络模型 (比如大语言模型) 下, 权重矩阵的维度往往很大, 会突破上述限制。 因此, 可以先设定线程块的维度, 再根据设定的线程块的维度, 确定需要设置的线程 格的维度, 其中, 线程块的维度确定了每个线程块中包含的线程数量, 线程格的维度确定 了线程格中包含的线程块的数量。 其 中, 线程块的维度可以是三维的: b I ockd i m. x, b I ockd i m. y, b I ockd i m. z,线程格 的维度可以是三维的: griddim.x, griddim.y, griddim.z。 基 于上述三个维度, 线程块内可以包含的线程的数量为: b I ockd i m. x*b I ockd i m. y* b I ockd im. z o 类似地, 线程格内可以包含的线程块的数量为: gr i dd i m. x*gr i dd i m. y* gr i dd i m. zo 实 际应用中,可以先设定 b I ockd i m. x, b I ockd i m. y和 b I ockd i m. z的取值,比如 (32, 1, 1), 基于该设定结果以及线程总数就知道需要多少个线程块, 从而确定 griddim. x、 gr iddim. y 和 griddim. z的取值即可。 基于上述 线程的设置结果, 确定多个线程各自对应的计算位置的过程包括: 根据线程
格的维度、 线程块的维度以及各线程块在线程格中的相对位置、 各线程在所属线程块中的 相对位置, 确定多个线程各自对应的标识, 进而根据多个线程各自对应的标识确定多个线 程各自对应的计算位置。 下面介绍上述计算过程。 假设线程总数为 N, 则 N= gr i dd i m. x*gr i dd i m. y* gr i dd i m. z* b I ockd i m. x*b I ockd i m. y* b I ockd i m. zo 下述计算过程中, 以 b l ock l dx. x、 b l ock l dx. y、 b I ock I dx. z分别表示当前线程块在所属的线程格中的相对位置 , thread I dx. x、 thread I dx. y、 thread I dx. z分别表示当前线程在所属线程块中的相对位置。 从 而确定各 线程的标识 的过程, 即为将所有的 线程排成一 个序列, 序列号为 0, 1 , 2, …, N, 在其中找到当前线程的序列号的过程。 该序列号即为线程的标识。 首先 , 先找到当前线程位于线程格中的哪一个线程块: b l ock i d b I ock I d=b I ock I dx. x+b I ock I dx. y*gr i dd i m. x+b I ock I dx. z*gr i dd i m. x*gr i dd i m. yo 之后 , 找到当前线程位于线程块中的哪一个线程: thread I d thread I d=thread I dx. x+thread I dx. y*b I ockd i m. x+thread I dx. z*b I ockd i m. x* b I ockd i m. yo 之后 , 计算一个线程块中一共有多少个线程: M
M=b I ockd i m. x*b I ockd i m. y*b I ockd i m. zo 最终, 求得当前线程的序列号: i dx i dx=thread I d+M*b I ock I do 这样就得 到的一个线程的标识, 这个标识相当于是将所有线程展开成一维空间, 每个 线程在该一维空间中对应的位置。 而上述 (i , j, k) 是三维空间, 根据设定的映射公式便 可以确定每个线程在该三维空间中对应的三个位置标识: (i , j, k) o 以上线程标识的计算和线程对应的三维位置坐标 的计算过程可以参考现有相关技术 实现, 在此不过多展开。 以下将详细描述本公开的一个或多个实施例的模型权重计算处理、 模型权重存储装置。 本领域技术人员可以理解, 这些装置均可使用市售的硬件组件通过本方案所教导的步骤进 行配置来构成。 图 6为本公开实施例提供的一种模型权重计算处理装置的结构示意图, 如图 6所示, 该 装置包括: 获取模块 1 1、 推理模块 12。 获取模块 1 1 , 用于响应于基于推理任务得到的神经网络模型中目标网络层的输入矩阵 的输入, 获取所述目标网络层对应的量化权重矩阵和量化参数矩阵, 以及获取用于存储所 述目标网络层对应的非量化权重矩阵的多个存储矩阵, 所述多个存储矩阵中包括用于按列 存储所述非量化权重矩阵中各非零元素值的第一存储矩阵, 用于存储所述非量化权重矩阵 中各非零元素值在所述非量化权重矩阵中对应的行号的第二存储矩阵, 用于存储所述非量 化权重矩阵中各列的非零元素值数量的第三存储矩阵, 用于存储所述非量化权重矩阵中各 列在所述第一存储矩阵中对应的起始索引位置的第四存储矩阵; 其中, 所述非量化权重矩
阵为稀疏矩阵, 所述目标网络层的权重矩阵的量化结果包括所述量化权重矩阵、 所述量化 参数矩阵和所述非量化权重矩阵。 推理模块 12 , 用于根据所述多个存储矩阵确定所述目标网络层的输出矩阵中同一位置 对应的第一计算结果和第二计算结果, 以根据所述第一计算结果和所述第二计算结果的累 加和得到所述同一位置的目标计算结果, 其中, 所述第一计算结果对应于所述输入矩阵与 反量化权重矩阵的乘积, 所述第二计算结果对应于所述输入矩阵与所述非量化权重矩阵的 乘积, 所述反量化权重矩阵是基于所述量化参数矩阵对所述量化权重矩阵进行反量化处理 得到的。 图 6所示装置可以执行前述实施例中的步骤, 详细的执行过程和技术效果参见前述实 施例中的描述, 在此不再赘述。 图 7为本公开实施例提供的一种模型权重存储装置的结构示意图, 如图 7所示, 该装置 包括: 获取模块 21、 生成模块 22、 存储模块 23。 获取模块 21 , 用于获取神经网络模型中目标网络层的权重矩阵的量化结果, 所述量化 结果中包括量化权重矩阵、 量化参数矩阵和非量化权重矩阵, 所述非量化权重矩阵为稀疏 矩阵。 生成模块 22, 用于生成用于存储所述非量化权重矩阵的多个存储矩阵, 所述多个存储 矩阵中包括用于按列存储所述非量化权重矩阵中各非零元素值的第一存储矩阵, 用于存储 所述非量化权重矩 阵中各非零元素值在所述非量化权重矩阵中对应的行号 的第二存储矩 阵, 用于存储所述非量化权重矩阵中各列的非零元素值数量的第三存储矩阵, 用于存储所 述非量化权重矩阵中各列在所述第一存储矩阵中对应的起始索引位置的第四存储矩阵。 存储模块 23, 用于存储所述多个存储矩阵、 所述量化权重矩阵和所述量化参数矩阵。 图 7所示装置可以执行前述实施例中的相关步骤, 详细的执行过程和技术效果参见前 述实施例中的描述, 在此不再赘述。 在一个 可能的设计中, 上述图 6、 图 7所示装置的结构可实现为一电子设备。 如图 8所 示, 该电子设备可以包括: 处理器 31、 存储器 32、 通信接口 33。 其中, 存储器 32上存储有 可执行代码, 当所述可执行代码被处理器 31执行时, 使处理器 31至少可以实现如前述实施 例中提供的模型权重计算处理方法或模型权重存储方法。 另外, 本公开实施例提供了一种非暂时性机器可读存储介质, 所述非暂时性机器可读 存储介质上存储有可执行代码, 当所述可执行代码被电子设备的处理器执行时, 使所述处 理器至少可以实现如前述实施例中提供的模型权重计算处理方法或模型权重存储方法。 以上所描述的装置实施例仅仅是示意性的, 其中所述作为分离部件说明的网元可以是 或者也可以不是物理上分开的。 可以根据实际的需要选择其中的部分或者全部模块来实现 本实施例方案的目的。 本领域普通技术人员在不付出创造性的劳动的情况下, 即可以理解 并实施。 通过 以上的实施方式的描述, 本领域的技术人员可以清楚地了解到各实施方式可借助
加必需的通用硬件平台的方式来实现, 当然也可以通过硬件和软件结合的方式来实现。 基 于这样的理解, 上述技术方案本质上或者说对现有技术做出贡献的部分可以以计算机产品 的形式体现出来, 本公开可采用在一个或多个其中包含有计算机可用程序代码的计算机可 用存储介质 (包括但不限于磁盘存储器、 CD-ROM (Compact D i sc Read-On I y Memory, 光 盘只读存储器) 、 光学存储器等) 上实施的计算机程序产品的形式。 本公开实施例提供 了一种计算机程序产品, 计算机程序产品中包含计算机程序, 当其 被电子设备的处理器执行时, 使处理器至少可以实现如前述实施例中提供的模型权重计算 处理方法或模型权重存储方法。 最后应说明的是: 以上实施例仅用以说明本公开的技术方案, 而非对其限制; 尽 管参照前述 实施例对本公开进行 了详细的说明, 本领域的普通技术人员应当理解: 其 依然可 以对前述各实施例所记载的技 术方案进行修改, 或者对其中部分技术特征进行 等同替换 ; 而这些修改或者替换, 并不使相应技术方案的本质脱离本公开各实施例技 术方案的精 神和范围。
Claims
1、 一种模型权重计算处理方法, 其中, 包括: 响应于基于推理任务得到的神经 网络模型中目标网络层的输入矩阵的输入, 获取所述 目标网络层对应的量化权重矩阵和量化参数矩阵; 获取 用于存储所述目标网络层对应的非量化权重矩阵的多个存储矩阵, 所述多个存储 矩阵中包括用于按列存储所述非量化权重矩阵中各非零元素值的第一存储矩阵, 用于存储 所述非量化权重矩 阵中各非零元素值在所述非量化权重矩阵中对应的行号 的第二存储矩 阵, 用于存储所述非量化权重矩阵中各列的非零元素值数量的第三存储矩阵, 用于存储所 述非量化权重矩阵中各列在所述第一存储矩阵中对应的起始索引位置的第四存储矩阵; 其 中, 所述非量化权重矩阵为稀疏矩阵, 所述目标网络层的权重矩阵的量化结果包括所述量 化权重矩阵、 所述量化参数矩阵和所述非量化权重矩阵; 根据 所述多个存储矩阵确定所述目标网络层的输出矩阵 中同一位置对应的第一计算 结果和第二计算结果, 以根据所述第一计算结果和所述第二计算结果的累加和得到所述同 一位置的目标计算结果, 其中, 所述第一计算结果对应于所述输入矩阵与反量化权重矩阵 的乘积, 所述第二计算结果对应于所述输入矩阵与所述非量化权重矩阵的乘积, 所述反量 化权重矩阵是基于所述量化参数矩阵对所述量化权重矩阵进行反量化处理得到的。
2、根据权利要求 1所述的方法,其中,所述目标网络层的输出矩阵的获取步骤, 包括: 确定并行的 多个线程各自对应的计算位置, 其中, 目标线程对应的计算位置包括第一 位置标识、 第二位置标识和第三位置标识, 所述目标线程是所述多个线程中任一个; 根据所 述第一位置标识、 所述第二位置标识和所述第三位置标识, 在所述输入矩阵、 所述量化权重矩阵和所述量化参数矩阵中查询得到第一元素值、 第二元素值和第三元素值, 其中, 所述第一元素值对应于所述输入矩阵的以所述第一位置标识作为行号以所述第三位 置标识作为列号的位置, 所述第二元素值对应于所述量化权重矩阵的以所述第三位置标识 作为行号以所述第二位置标识作为列号的位置, 所述第三元素值对应于所述量化参数矩阵 的以所述第三位置标识作为行号以所述第二位置标识作为列号的位置; 根据所述 第一元素值、 所述第二元素值和所述第三元素值更新所述输出矩阵的目标位 置的第一计算结果, 所述目标位置对应于所述输出矩阵的以所述第一位置标识作为行号以 所述第二位置标识作为列号的位置。
3、根据权利要求 2所述的方法,其中,所述目标网络层的输出矩阵的获取步骤, 包括: 根据所述 第二位置标识、 所述第三位置标识和所述多个存储矩阵, 确定所述非量化权 重矩阵中的第四元素值, 所述第四元素值对应于所述非量化权重矩阵的以所述第二位置标 识作为列号的一列中的目标排序位置上的元素值, 所述目标排序位置为所述第三位置标识 对应的数值; 在所述输入矩 阵中确定第五元素值, 所述第五元素值对应于所述输入矩阵的以所述第 一位置标识作为行号以第四位置标识作为列号的位置, 所述第四位置标识对应于所述第四
元素值在所述非量化权重矩阵中的行号; 根据 所述第四元素值和所述第五元素值的乘积更新所述 输出矩阵的所述目标位置的 第二计算结果。
4、 根据权利要求 3所述的方法, 其中, 所述根据所述第二位置标识、 所述第三位置标 识和所述多个存储矩阵, 确定所述非量化权重矩阵中的第四元素值, 包括: 在所 述第三存储矩阵中查询所述非量化权重矩阵的 以所述第二位置标识作为列号的 目标列中包含的非零元素值数量; 若所述非零元素值数量 大于所述第三位置标识对应的数值, 则在所述第四存储矩阵中 查询所述目标列在所述第一存储矩阵中对应的起始索引位置; 在所述 第一存储矩阵中查询目标索引位置对应的第四元素值, 所述目标索引位置为所 述起始索引位置和所述第三位置标识对应的数值之和; 所述在所述输入矩阵 中确定第五元素值, 包括: 在所述 第二存储矩阵中查询所述目标索引位置对应的行号, 并确定所述目标索引位置 对应的行号作为所述第四位置标识; 在所 述输入矩阵中确定以所述第一位置标识作为行号 以所述第四位置标识作为列号 的位置上的第五元素值。
5、 根据权利要求 2所述的方法, 其中, 所述方法还包括: 根据所述输 出矩阵的行数和列数, 以及所述输入矩阵的列数, 确定所述多个线程的数 量; 根据设定的线程块的维度, 确定需要设置的线程格的维度, 其中, 线程块的维度确定 了每个线程块中包含的线程数量, 线程格的维度确定了线程格中包含的线程块的数量; 所述确定并行的多个线程各 自对应的计算位置, 包括: 根据所述线程格的维度、 所述线程块的维度以及各线程块在线程格中的相对位置、 各 线程在所属线程块中的相对位置, 确定所述多个线程各自对应的标识; 根据所述 多个线程各自对应的标识确定所述多个线程各自对应的计算位置。
6、 根据权利要求 1所述的方法, 其中, 所述方法还包括: 响应于所述输入矩阵的输入, 将所述输入矩阵、 所述量化权重矩阵、 量化参数矩阵和 所述多个存储矩阵加载到目标缓存空间, 以在所述目标缓存空间中计算得到第一计算结果、 所述第二计算结果和所述目标计算结果。
7、 根据权利要求 1至 6任一项所述的方法, 其中, 所述方法还包括: 获取所述 目标网络层的权重矩阵的初始量化权重矩阵和初始量化参数矩阵, 所述初始 量化参数矩阵中包括初始零值矩阵, 所述初始量化权重矩阵和初始零值矩阵具有相同的维 度且元素值具有相同的第一数据格式; 对所述初始量化权重矩阵按照行进行分组, 得到分组后量化权重矩阵, 其中, 所述初 始量化权重矩阵 中同一列对应的组内元素值拼接为第二数据格式的元素值 以存储到所述
分组后量化权重矩阵; 对所述初始零值矩 阵按照列进行分组, 得到分组后零值矩阵, 其中, 所述初始零值矩 阵中同一行对应的 组内元素值拼接为所述第二数据格式的元素值以存储到所 述分组后零 值矩阵, 所述初始量化权重矩阵和所述初始零值矩阵按照相同分组长度进行分组处理; 存储所述分组后量化权重矩 阵和所述分组后零值矩阵。
8、 根据权利要求 7所述的方法, 其中, 所述获取所述目标网络层对应的量化权重矩阵 和量化参数矩阵, 包括: 加载所述分组后量化权重矩 阵和所述分组后零值矩阵; 将所述分组后量化权重矩 阵和所述分组后零值矩阵, 还原为所述初始量化权重矩阵和 所述初始零值矩阵。
9、 一种模型权重存储方法, 其中, 包括 获取神经 网络模型中目标网络层的权重矩阵的量化结果, 所述量化结果中包括量化权 重矩阵、 量化参数矩阵和非量化权重矩阵, 所述非量化权重矩阵为稀疏矩阵; 生成 用于存储所述非量化权重矩阵的多个存储矩阵, 所述多个存储矩阵中包括用于按 列存储所述非量化权重矩阵中各非零元素值的第一存储矩阵, 用于存储所述非量化权重矩 阵中各非零元素值在所述非量化权重矩阵中对应的行号的第二存储矩阵, 用于存储所述非 量化权重矩阵中各列的非零元素值数量的第三存储矩阵, 用于存储所述非量化权重矩阵中 各列在所述第一存储矩阵中对应的起始索引位置的第四存储矩阵; 存储所述 多个存储矩阵、 所述量化权重矩阵和所述量化参数矩阵。
10、 一种电子设备, 其中, 包括: 存储器、 处理器、 通信接口; 其中, 所述存储器上 存储有可执行代码, 当所述可执行代码被所述处理器执行时, 使所述处理器执行如权利要 求 1至 8中任一项所述的模型权重计算处理方法。
11、 一种非暂时性机器可读存储介质, 其中, 所述非暂时性机器可读存储介质上存储 有可执行代码, 当所述可执行代码被电子设备的处理器执行时, 使所述处理器执行如权利 要求 1至 8中任一项所述的模型权重计算处理方法。
12、 一种计算机程序产品, 其中, 包括: 计算机程序, 当所述计算机程序被电子 设备的处理 器执行时, 使所述处理器执行如权利要求 1 至 8中任一项所述的模型权重 计算处理 方法。
Applications Claiming Priority (2)
| Application Number | Priority Date | Filing Date | Title |
|---|---|---|---|
| CN202410102457.7 | 2024-01-23 | ||
| CN202410102457.7A CN120373447A (zh) | 2024-01-23 | 2024-01-23 | 模型权重存储、计算处理方法及设备、存储介质和程序 |
Publications (1)
| Publication Number | Publication Date |
|---|---|
| WO2025158201A1 true WO2025158201A1 (zh) | 2025-07-31 |
Family
ID=96437225
Family Applications (1)
| Application Number | Title | Priority Date | Filing Date |
|---|---|---|---|
| PCT/IB2024/062540 Pending WO2025158201A1 (zh) | 2024-01-23 | 2024-12-12 | 模型权重存储、计算处理方法及设备、存储介质和程序 |
Country Status (2)
| Country | Link |
|---|---|
| CN (1) | CN120373447A (zh) |
| WO (1) | WO2025158201A1 (zh) |
Citations (3)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| CN110334067A (zh) * | 2019-06-17 | 2019-10-15 | 腾讯科技(深圳)有限公司 | 一种稀疏矩阵压缩方法、装置、设备及存储介质 |
| US20200293284A1 (en) * | 2018-03-23 | 2020-09-17 | Amazon Technologies, Inc. | Accelerated quantized multiply-and-add operations |
| US20230351144A1 (en) * | 2022-04-28 | 2023-11-02 | Qualcomm Incorporated | Instruction Set Architecture for Implementing Linear Activation Functions in Neural Networks |
-
2024
- 2024-01-23 CN CN202410102457.7A patent/CN120373447A/zh active Pending
- 2024-12-12 WO PCT/IB2024/062540 patent/WO2025158201A1/zh active Pending
Patent Citations (3)
| Publication number | Priority date | Publication date | Assignee | Title |
|---|---|---|---|---|
| US20200293284A1 (en) * | 2018-03-23 | 2020-09-17 | Amazon Technologies, Inc. | Accelerated quantized multiply-and-add operations |
| CN110334067A (zh) * | 2019-06-17 | 2019-10-15 | 腾讯科技(深圳)有限公司 | 一种稀疏矩阵压缩方法、装置、设备及存储介质 |
| US20230351144A1 (en) * | 2022-04-28 | 2023-11-02 | Qualcomm Incorporated | Instruction Set Architecture for Implementing Linear Activation Functions in Neural Networks |
Also Published As
| Publication number | Publication date |
|---|---|
| CN120373447A (zh) | 2025-07-25 |
Similar Documents
| Publication | Publication Date | Title |
|---|---|---|
| KR102787012B1 (ko) | 가속화된 임베딩 계층 컴퓨테이션들 | |
| Fuketa et al. | Image-classifier deep convolutional neural network training by 9-bit dedicated hardware to realize validation accuracy and energy efficiency superior to the half precision floating point format | |
| WO2024259915A1 (zh) | 模型量化方法、装置、设备及介质 | |
| CN119884398A (zh) | 用于数据查询的方法、设备和计算机程序产品 | |
| WO2023048901A1 (en) | Systems and methods for training and using optimized neural networks | |
| WO2025001443A1 (zh) | 一种神经网络模型的生成方法及装置 | |
| CN119808860A (zh) | 混合专家模型的优化方法、装置、设备、介质及程序 | |
| CN117893397A (zh) | 一种图像数据处理方法、装置、设备及介质 | |
| JP2023024960A (ja) | 効率的なニューラルネットワーク実行のためのメモリ使用の最適化 | |
| CN120373447A (zh) | 模型权重存储、计算处理方法及设备、存储介质和程序 | |
| KR102878348B1 (ko) | 클러스터링 기반 양자화를 활용한 경량 텐서 손실 압축 기법 | |
| WO2021179117A1 (zh) | 神经网络通道数搜索方法和装置 | |
| Ma et al. | Layer-by-layer quantization method for neural network parameters | |
| CN116740414B (zh) | 图像识别方法、装置、电子设备和存储介质 | |
| CN111461144A (zh) | 一种用于加速卷积神经网络的方法 | |
| CN117197576B (zh) | 一种基于非线性池化与深度可分离卷积的适用于mcu部署的图像分类方法 | |
| CN121255478B (zh) | 算子执行方法、电子设备、存储介质和程序产品 | |
| CN119942275B (zh) | 基于保秩分解与知识蒸馏的图像领域模型压缩方法、装置、设备及介质 | |
| CN118921145B (zh) | 一种p-ldpc码的编码方法、装置、介质及程序产品 | |
| CN122047460A (zh) | 大语言模型的索引方法、装置、计算机设备和存储介质 | |
| WO2026000675A1 (en) | Flexible inter-tile communication in multi-tile neural network accelerator | |
| Gao et al. | RF-FE Pruning: Receptive-Field and Feature-Map Based Pruning for Neural Networks | |
| CN121072742A (zh) | 模型推理优化方法、装置、设备和存储介质 | |
| WO2025149841A1 (zh) | 模型压缩方法、模型部署方法、电子设备及存储介质 | |
| CN121706994A (zh) | 推理加速方法及装置 |
Legal Events
| Date | Code | Title | Description |
|---|---|---|---|
| 121 | Ep: the epo has been informed by wipo that ep was designated in this application |
Ref document number: 24920061 Country of ref document: EP Kind code of ref document: A1 |