Matrix processing method, processor, system on chip, electronic equipment and storage medium

By parallel processing of the matrices to be processed in large language models in blocks, combined with cache optimization, the problem of low memory utilization of traditional matrix multiplication is solved, and efficient matrix multiplication calculation and memory management are achieved.

CN118656575APending Publication Date: 2024-09-17ALIBABA DAMO (HANGZHOU) TECH CO LTD
View PDF 0 Cites 12 Cited by

Patent Information

Application Number
CN202410676030.8
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-05-28
Publication Date
2024-09-17

AI Technical Summary

Technical Problem

In large language models, traditional matrix multiplication is difficult to effectively utilize memory, resulting in low memory utilization, especially on the device side where memory is tight. The memory access overhead and storage pressure caused by matrix rearrangement are difficult to support, and the multi-core matrix multiplication update algorithm The data reuse is low, resulting in frequent memory refreshes.

Method used

By dividing the matrix to be processed into blocks, multiple sub-matrices are obtained, and multiple processing cores perform matrix multiplication operations on the sub-matrices in parallel. Combined with cache optimization, this avoids frequent cache updates and ensures continuity of memory access, thereby improving memory utilization. .

Benefits of technology

It significantly speeds up the calculation time of matrix multiplication, improves processor memory utilization, avoids frequent memory refresh and memory access overhead, and improves the computing and bandwidth performance of multi-core processors.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118656575A
    Figure CN118656575A
Patent Text Reader

Abstract

The invention discloses a matrix processing method, a processor, a system on chip, electronic equipment and a storage medium. The method relates to the field of data processing, and comprises the following steps: partitioning a first to-be-processed matrix to obtain a plurality of first sub-matrixes, and partitioning a second to-be-processed matrix to obtain a plurality of second sub-matrixes; determining first to-be-processed sub-matrixes corresponding to the plurality of processing cores from the plurality of first sub-matrixes, and determining second to-be-processed sub-matrixes corresponding to the plurality of processing cores from the plurality of second sub-matrixes; and performing matrix multiplication operation on the first to-be-processed sub-matrix and the second to-be-processed sub-matrix in parallel through the plurality of processing cores to obtain matrix multiplication results corresponding to the plurality of processing cores. The technical problem that the memory utilization rate of a processor in the related technology is low is solved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of processors, and in particular to a matrix processing method, a processor, a system on a chip, an electronic device and a storage medium. Background Art

[0002] In large language models, matrix multiplication is usually large in scale. Traditional matrix multiplication implementations are difficult to make good use of memory, which will bring huge pressure on memory access. On the other hand, many improvement algorithms need to rearrange the matrix to make memory access continuous, but on the end side where memory is relatively tight, matrix rearrangement will bring additional memory access overhead and storage pressure. The memory space is difficult to support the temporary memory generated by matrix rearrangement. In the multi-core matrix multiplication update algorithm, the data reuse is low, resulting in frequent memory refresh, making it difficult to fully utilize the memory.

[0003] To address the above-mentioned problems, no effective solution has been proposed yet. Summary of the invention

[0004] The embodiments of the present application provide a matrix processing method, a processor, a system on chip, an electronic device and a storage medium, so as to at least solve the technical problem of low memory utilization of the processor in the related art.

[0005] According to one aspect of an embodiment of the present application, a matrix processing method is provided, which is applied to a processor, wherein the processor includes multiple processing cores, and the method includes: dividing a first matrix to be processed into blocks to obtain multiple first sub-matrices, and dividing a second matrix to be processed into blocks to obtain multiple second sub-matrices; determining first sub-matrices to be processed corresponding to multiple processing cores from the multiple first sub-matrices, and determining second sub-matrices to be processed corresponding to multiple processing cores from the multiple second sub-matrices; performing matrix multiplication operations on the first sub-matrix to be processed and the second sub-matrix to be processed in parallel by the multiple processing cores to obtain matrix multiplication results corresponding to the multiple processing cores.

[0006] According to one aspect of an embodiment of the present application, a matrix processing method is provided, which is applied to a processor, wherein the processor includes multiple processing cores, and the method includes: dividing the input matrix of the neural network model into blocks to obtain multiple first sub-matrices, and dividing the parameter matrix of the neural network model into blocks to obtain multiple second sub-matrices; determining first sub-matrices to be processed corresponding to multiple processing cores from the multiple first sub-matrices, and determining second sub-matrices to be processed corresponding to multiple processing cores from the multiple second sub-matrices; performing matrix multiplication operations on the first sub-matrices to be processed and the second sub-matrices to be processed in parallel by multiple processing cores to obtain matrix multiplication results corresponding to the multiple processing cores; storing the matrix multiplication results corresponding to the multiple processing cores in a matrix to obtain an activation matrix of the neural network model.

[0007] According to one aspect of an embodiment of the present application, a processor is provided, comprising: a scheduling module, used to divide a first matrix to be processed into blocks to obtain multiple first sub-matrices, and to divide a second matrix to be processed into blocks to obtain multiple second sub-matrices; multiple processing cores, used to determine the first sub-matrices to be processed corresponding to the multiple processing cores from the multiple first sub-matrices, and to determine the second sub-matrices to be processed corresponding to the multiple processing cores from the multiple second sub-matrices, and to perform matrix multiplication operations on the first sub-matrices to be processed and the second sub-matrices to be processed in parallel to obtain matrix multiplication results corresponding to the multiple processing cores.

[0008] According to another aspect of the embodiments of the present application, a system on chip is also provided, including: a processor according to any one of the above embodiments.

[0009] According to another aspect of the embodiments of the present application, an electronic device is further provided, including: a memory storing an executable program; and a processor for running the program, wherein the program executes any one of the methods in the above embodiments when running.

[0010] According to another aspect of the embodiments of the present application, a computer terminal is further provided, including: a memory storing an executable program; and a processor for running the program, wherein the method in each embodiment of the present application is executed when the program is running.

[0011] According to another aspect of an embodiment of the present application, a computer-readable storage medium is also provided, the computer-readable storage medium including a stored executable program, wherein when the executable program is running, the device where the computer-readable storage medium is located is controlled to execute the methods in each embodiment of the present application.

[0012] According to another aspect of the embodiments of the present application, a computer program product is also provided, including a computer program, and when the computer program is executed by a processor, the method in each embodiment of the present application is implemented.

[0013] According to another aspect of an embodiment of the present application, a computer program product is also provided, including a non-volatile computer-readable storage medium, wherein the non-volatile computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method in each embodiment of the present application is implemented.

[0014] According to another aspect of the embodiments of the present application, a computer program is also provided, and when the computer program is executed by a processor, the methods in the various embodiments of the present application are implemented.

[0015] In an embodiment of the present application, the first matrix to be processed is divided into blocks to obtain multiple first sub-matrices, and the second matrix to be processed is divided into blocks to obtain multiple second sub-matrices; the first sub-matrices to be processed corresponding to multiple processing cores are determined from the multiple first sub-matrices, and the second sub-matrices to be processed corresponding to multiple processing cores are determined from the multiple second sub-matrices; the first sub-matrices to be processed and the second sub-matrices to be processed are multiplied by multiple processing cores in parallel to obtain the matrix multiplication results corresponding to the multiple processing cores, thereby improving the memory utilization of the processor. It is easy to notice that the first matrix to be processed and the second matrix to be processed can be divided into blocks so that multiple processing cores can process the sub-matrices to be processed in parallel, giving full play to the computing and bandwidth performance of the multi-core processor, and greatly speeding up the calculation time of the matrix multiplication. By determining the first and second sub-matrices to be processed corresponding to the multiple processing cores, more sophisticated task allocation and scheduling can be achieved, thereby better utilizing the processor resources. Such a block and task allocation method helps to avoid frequent cache updates, ensure that memory access has local continuity, and thus improve the utilization of the processor memory, thereby improving the utilization of the processor memory, and thus solving the technical problem of low memory utilization of the processor in the related art.

[0016] It is easy to notice that the above general description and the following detailed description are only for the purpose of exemplifying and explaining the present application, and do not constitute a limitation of the present application. BRIEF DESCRIPTION OF THE DRAWINGS

[0017] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:

[0018] Figure 1 is a hardware structure block diagram of a RISC-V system for implementing a matrix processing method according to an embodiment of the present application;

[0019] Figure 2 is a schematic diagram of a RISC-V architecture system on chip (System On Chip, SOC) according to an embodiment of the present application;

[0020] Figure 3 is a flowchart of a matrix processing method according to Embodiment 1 of the present application;

[0021] Figure 4 is a schematic diagram of an inner product calculation method of a single processing core according to an embodiment of the present application;

[0022] Figure 5 is a schematic diagram of a multiplication-accumulation operation according to an embodiment of the present application;

[0023] Figure 6 is a schematic diagram of performing a multiplication-accumulation operation according to an embodiment of the present application;

[0024] Figure 7 is a schematic diagram of dividing a matrix to be processed into blocks according to an embodiment of the present application;

[0025] Figure 8 is a schematic diagram of element extraction according to an embodiment of the present application;

[0026] Fig. 9 is a schematic diagram of a multi-processing core computing method after block division according to an embodiment of the present application;

[0027] Fig.10 is a flow chart of a matrix processing method according to Embodiment 2 of the present application;

[0028] Fig.11 is a schematic diagram of a matrix processing device according to Embodiment 3 of the present application;

[0029] Fig.12 is a schematic diagram of a matrix processing device according to Embodiment 4 of the present application;

[0030] Fig.13 is a schematic diagram of a processor according to Embodiment 5 of the present application;

[0031] Fig.14 It is a structural block diagram of an electronic device according to an embodiment of the present application. DETAILED DESCRIPTION

[0032] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present application.

[0033] It should be noted that the terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device comprising a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.

[0034] First, some nouns or terms that appear in the description of the embodiments of the present application are subject to the following explanations:

[0035] Large Language Model (LLM) is an artificial intelligence technology that trains a deep learning model based on a large amount of text data to generate natural language text or understand input text. The main advantage of LLM is that it can capture the complexity and diversity of language, thereby achieving more natural and accurate text generation and understanding. This model is widely used in machine translation, text summarization, sentiment analysis, dialogue systems and other fields.

[0036] Generalized Matrix-Matrix Multiplication (GEMM), generalized matrix multiplication is a basic linear algebra operation, commonly used in computer graphics, machine learning, data analysis and other fields. GEMM is an efficient matrix multiplication algorithm that can handle matrix multiplication problems of different sizes and types. It includes three types of matrix operations: matrix multiplication, matrix transposition, and matrix and transposed matrix multiplication. The GEMM algorithm can optimize memory access and computational efficiency, thereby improving overall performance.

[0037] At present, as the demand for LLM inference on the edge becomes increasingly urgent, the single-core performance is no longer sufficient to meet the inference performance of large language models, so there is an urgent need to update the edge processor to support multi-core. Multi-core black iron processors can be installed on the chip, which has great potential in artificial intelligence reasoning.

[0038] In this application, taking an open source model as an example, a key operation in the machine learning model (MLM) is updated on a specified model of processor, which lays the foundation for the processor to run more large language models.

[0039] Example 1

[0040] According to an embodiment of the present application, a matrix processing method is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.

[0041] The method embodiment provided in Embodiment 1 of the present application can be executed in a RISC-V system, a RISC-V chip or a similar device. Figure 1 is a hardware structure block diagram of a RISC-V system for implementing a matrix processing method according to an embodiment of the present application. Figure 1 As shown, the RISC-V system 100 can be divided from the bottom layer to the upper layer into a reduced instruction set architecture 101 (including a basic instruction set 101-1 and an extended instruction set 101-2), a hardware layer 102 (including a processor 102-1, a peripheral hardware circuit 102-2, etc.), an interface layer 103, an operating system layer 104 (supporting multiple operating systems 104-1, 104-2, ..., 104-n, such as Linux, FreeSBD, RT-Thread, etc.), a middleware and library layer 105 (including a system library 105-1, an API 105-2 and a middleware service 105-3) and an application layer 106 (including multiple user programs and services 106-1, 106-2, ..., 106-n). The RISC-V system 100 also includes a tool chain 107 from the underlying hardware to the application layer. The tool chain may include a compiler and assembler 107-1, a linker 107-2, a debugger 107-3, a simulator and emulator 107-4, an integrated development environment 107-5, a hardware description language tool 107-6, a performance analysis tool 107-7, and a version control system 107-8, etc.

[0042] The instruction set architecture 101 defines the basic operations and instruction sets supported by the processor 102-1, including a basic instruction set and an extended instruction set, wherein the basic instruction set represents a basic integer instruction set, such as RV32I and RV64I, and the extended instruction set can be floating point, atomic operations, compressed instructions, etc.

[0043] The interface layer 103 includes the specific design of the processor, such as pipeline design, cache structure, execution unit, branch prediction, etc. This layer is the process of mapping abstract instructions to physical hardware.

[0044] The operating system layer 104 is located above the hardware and provides a hardware abstraction layer and management mechanism, so that applications can interact with the hardware through system calls. The operating system is responsible for managing processor resources, memory, device drivers, task scheduling, etc.

[0045] The middleware and library layer 105 provides a rich set of services and interfaces to help applications run more efficiently. For example, the standard library provides functions such as file operations and mathematical calculations, while the middleware can provide complex services such as network communications and graphical user interfaces.

[0046] The application layer 106 uses the functions and services provided by the lower layer to implement specific application logic. These applications can be command line tools, graphical interface applications, server-side services, etc.

[0047] The tool chain 107 is a key component that connects the underlying hardware to the upper-level software. Various tools in the tool chain 107 function at different levels to support the entire process from hardware design to software development, ensuring the consistency and effectiveness of the entire system design.

[0048] It should be noted that the layered design of the RISC-V architecture allows decoupling between different layers, so that each layer can be developed and optimized independently.

[0049] In an optional embodiment, Figure 2 Schematic diagram of a RISC-V architecture system on chip (SOC) according to an embodiment of the present application. Figure 2 As shown, the SOC includes at least one RISC-V core 202 (only one is shown in the figure), and the RISC-V core 202 is connected to peripheral devices through a bus 204, including but not limited to ROM 206, RAM 208, timer 210, UART (Universal Asynchronous Receiver / Transmitter) 212, GPIO (General Purpose Input / Output) 214, SPI (Serial Peripheral Interface Bus) 216, etc.

[0050] Under the above operating environment, this application provides Figure 3 The matrix processing method shown in FIG. 1 is applied to a processor, which includes multiple processing cores. Figure 3 It is a flowchart of the matrix processing method according to Example 1 of the present application.

[0051] like Figure 3 As shown, the method includes:

[0052] Step S302, dividing the first matrix to be processed into blocks to obtain a plurality of first sub-matrices, and dividing the second matrix to be processed into blocks to obtain a plurality of second sub-matrices;

[0053] The first matrix to be processed and the second matrix to be processed mentioned above may be matrices to be processed in a large language model.

[0054] The processing core in the above processor refers to a computing unit inside the processor, which is mainly responsible for executing instructions, performing arithmetic operations and logical operations. The processor can contain multiple processing cores, which can work in parallel to improve the performance and efficiency of the processor. Different processing cores can execute instructions independently, but different processing cores usually share some processor resources, such as cache, memory controller, etc.

[0055] In a large language model, the first matrix to be processed may be first divided into blocks according to rows, and then the first matrix to be processed may be divided into blocks according to columns to obtain multiple first sub-matrices; the first matrix to be processed may be first divided into blocks according to columns, and then the first matrix to be processed may be divided into blocks according to rows to obtain multiple first sub-matrices; the first matrix to be processed may also be divided into blocks according to both rows and columns to obtain multiple first sub-matrices, and different first sub-matrices may contain a number of continuous rows and columns.

[0056] Similarly, the second matrix to be processed can be first divided into blocks according to rows, and then the second matrix to be processed can be divided into blocks according to columns to obtain multiple second sub-matrices; the second matrix to be processed can also be divided into blocks according to columns, and then the second matrix to be processed can be divided into blocks according to rows to obtain multiple second sub-matrices; the second matrix to be processed can also be divided into blocks according to both rows and columns to obtain multiple second sub-matrices, and different second sub-matrices contain a number of continuous rows and columns.

[0057] By processing the first matrix to be processed and the second matrix to be processed in blocks, the efficiency of parallel calculation of the first matrix to be processed and the second matrix to be processed can be improved, memory usage can be reduced, and hardware resources can be better utilized for calculation. In a large language model, large-scale matrix data can be processed in blocks, thereby speeding up the calculation speed and updating the model performance.

[0058] Step S304, determining first sub-matrices to be processed corresponding to the plurality of processing cores from the plurality of first sub-matrices, and determining second sub-matrices to be processed corresponding to the plurality of processing cores from the plurality of second sub-matrices;

[0059] The first submatrix to be processed may be a first submatrix among multiple first submatrices that has not been processed by multiple processing cores. The first submatrix to be processed may also be a first submatrix among multiple first submatrices that needs to be processed by multiple processing cores.

[0060] The second submatrix to be processed may be a second submatrix among the multiple second submatrices that has not been processed by the multiple processing cores. The second submatrix to be processed may also be a second submatrix among the multiple second submatrices that needs to be processed by the multiple processing cores.

[0061] In an optional embodiment, different processing cores in the processor can be used to perform specific operations on different sub-matrices. This strategy is effective in improving computational efficiency, implementing parallel processing, or designing algorithms for specific problems. For example, when using deep learning for image recognition, different convolutional layers may correspond to different processing cores to extract different features of the image.

[0062] Step S306 , performing matrix multiplication operations on the first to-be-processed sub-matrix and the second to-be-processed sub-matrix in parallel through the multiple processing cores to obtain matrix multiplication results corresponding to the multiple processing cores.

[0063] The matrix multiplication operation mentioned above refers to the operation of multiplying two matrices to obtain a new matrix. When performing a matrix multiplication operation, the number of columns of the first matrix must be equal to the number of rows of the second matrix before the multiplication operation is performed. Optionally, if the dimension of the first matrix is ​​m×n and the dimension of the second matrix is ​​n×p, the dimension of the new matrix obtained by multiplying them is m×p. In the matrix multiplication operation, each element in the new matrix is ​​obtained by multiplying the corresponding row of the first matrix with the corresponding column of the second matrix and then adding them together.

[0064] In an optional embodiment, the process of performing matrix multiplication operation on two to-be-processed sub-matrices may be performed in parallel by multiple processing cores. In parallel computing, each processing core is responsible for processing a portion of the data, and then the processing results are combined to obtain the final result.

[0065] In the process of performing matrix multiplication operations on the first to-be-processed submatrix and the second to-be-processed submatrix in parallel through multiple processing cores, the first to-be-processed submatrix and the second to-be-processed submatrix can be divided into multiple parts, and then assigned to different processing cores for calculation. Different processing cores can independently perform matrix multiplication operations on their assigned parts, and finally obtain matrix multiplication results corresponding to multiple processing cores. This can greatly speed up the speed of matrix multiplication operations and improve computing efficiency. Parallel computing has important applications in large-scale data processing and complex computing tasks, and can make full use of multi-core processors and distributed computing resources, speed up computing speed, and improve the processing power of the system.

[0066] In an optional embodiment, a first sub-matrix to be processed and a second sub-matrix to be processed corresponding to a plurality of processing cores can be determined from a plurality of first sub-matrices and a plurality of second sub-matrices, and then the plurality of processing cores are applied to the first sub-matrix to be processed and the second sub-matrix to be processed. The process involves parallel processing or batch processing operations and can be used in the fields of image processing, matrix operations, etc. By applying a plurality of processing cores to the corresponding sub-matrices, a plurality of data can be processed simultaneously, thereby improving processing efficiency and parallel performance.

[0067] In order to efficiently utilize multi-core computing resources, in this application, the first matrix to be processed is first processed in blocks to obtain multiple first sub-matrices, and the second matrix to be processed is divided into blocks to obtain multiple second sub-matrices. Multiple first sub-matrices and multiple second sub-matrices can be stored in the cache so that each thread can share the data in the processor cache to reduce the access frequency to the main memory in the processor. The matrix multiplication results corresponding to multiple processing cores can be determined based on the first sub-matrix to be processed corresponding to multiple processing cores in the first sub-matrix and the second sub-matrix to be processed corresponding to multiple processing cores in the second sub-matrix, and the matrix multiplication results can be divided into multiple processing cores for parallel processing.

[0068] Exemplarily, it may include an A matrix, a B matrix, and a C matrix, wherein the A matrix may be a first matrix to be processed, the B matrix may be a second matrix to be processed, the A matrix may be divided into blocks according to M to obtain a plurality of first sub-matrices A_BLK, and the B matrix may be divided into blocks according to N to obtain a plurality of second sub-matrices B_BLK. A matrix multiplication operation may be performed on A_BLK and B_BLK in parallel by multiple processing cores to obtain C_BLK+=A_BLK*B_BLK.

[0069] A_BLK, B_BLK, and C_BLK can be saved in the cache, so that the threads of multiple processing cores share the data in L2cache, without frequent access to the memory, reducing the memory access overhead. Each thread can share the data in the cache, thereby reducing the access frequency to the main memory and reducing the latency of data access. During the iteration process, a sub-block C_BLK of the C matrix can be calculated to be equal to the product of A_BLK and B_BLK.

[0070] In an optional embodiment, taking a deep learning model as an example, the matrix multiplication operation is mainly in a specific matrix transposition form. In this form, one input matrix does not need to be transposed, while the other matrix needs to be transposed. The transposed matrix usually represents the weight parameters of the model, and a specific quantization technique is used to reduce its numerical range. Since some processors do not support direct operations on integers and floating-point numbers, the quantized weights need to be dequantized to floating-point types before performing matrix multiplication operations. This processing method helps to improve computational efficiency and accuracy.

[0071] For example, taking the currently released open source language large model (Large Language Model Meta AI 2, referred to as LLaMA2) as an example, matrix multiplication (matmul) is mainly based on transposed matrix a (trans_a) = 0, transposed matrix b (trans_b) = 1, and the form is C[m,n] = A[m,k]*B[n,k], that is, matrix A is not transposed, and matrix B is transposed. Among them, most of the B matrix is ​​weight, using block = 32 block quantization, quantized to int8 or int4 type. Because the processor does not support direct multiplication of floating point and integer, it needs to be dequantized to fp32 or fp16 type during calculation, and then multiplication is performed.

[0072] It should be noted that the calculation of the outer product can make greater use of registers than the inner product, and has a higher data reuse rate. In the large language model, both the A and B matrices are continuous in the K dimension. If the outer product is calculated forcibly, it will cause discrete memory access and bring additional overhead. On this basis, in order to avoid additional overhead, the matrix can be rearranged by calculating the inner product. By calculating the inner product, the memory access overhead can be reduced. Calculating the inner product can reduce the additional overhead caused by discrete memory access, thereby improving memory access efficiency. Since calculating the inner product does not require additional data rearrangement operations, the calculation can be completed more quickly, thereby improving the calculation efficiency. Calculating the inner product can also avoid additional overhead and complex data re-shooting operations, thereby reducing the overall calculation cost.

[0073] Figure 4 FIG. 1 is a schematic diagram of an inner product calculation method of a single processing core according to an embodiment of the present application. Figure 4 As shown, C[m,n]=A[m,k]*B[n,k] is the matrix multiplication result of matrix A and matrix B, where matrix A is in row-major order, matrix B is in column-major order, and matrix C is the matrix multiplication result of matrix A and matrix B. Matrix C is obtained by multiplying and accumulating a row of matrix A and a column of matrix B. The inner product process using the vector execution instruction (vector) is as follows: first, the elements of a row and a column of matrix A and matrix B need to be stored in the vector register, and then the vector multiplication and accumulation operation instruction (vfmacc.vv) is used to perform multiplication and accumulation operations to obtain multiple multiplication and accumulation results until the k loop ends, and then the vector accumulation and summation instruction (vfredosum.vs) is used to accumulate the multiple multiplication and accumulation results into one result and write it into the C matrix.

[0074] Figure 5 is a schematic diagram of a multiplication-accumulation operation according to an embodiment of the present application, such as Figure 5 As shown, c0+=a0×b0, c0+=a1×b1, c0+=an×bn, and the multiple c0s obtained can be summed to obtain c00. Figure 6is a schematic diagram of performing a multiplication-accumulation operation according to an embodiment of the present application, such as Figure 6 As shown, matrix A contains a0-an, matrix B contains b0-bn, and matrix C can be used to store c00.

[0075] Through the above steps, the first matrix to be processed is divided into blocks to obtain multiple first sub-matrices, and the second matrix to be processed is divided into blocks to obtain multiple second sub-matrices; the first sub-matrices to be processed corresponding to multiple processing cores are determined from the multiple first sub-matrices, and the second sub-matrices to be processed corresponding to multiple processing cores are determined from the multiple second sub-matrices; the first sub-matrices to be processed and the second sub-matrices to be processed are performed by multiple processing cores in parallel to obtain the matrix multiplication results corresponding to the multiple processing cores, thereby improving the memory utilization of the processor. It is easy to notice that by dividing the first matrix to be processed and the second matrix to be processed into blocks, multiple processing cores can process the sub-matrices to be processed in parallel, giving full play to the computing and bandwidth performance of the multi-core processor, and greatly speeding up the calculation time of the matrix multiplication. By determining the first and second sub-matrices to be processed corresponding to the multiple processing cores, more refined task allocation and scheduling can be achieved, thereby better utilizing the processor resources. Such a block and task allocation method helps to avoid frequent cache updates, ensure that memory access has local continuity, and thus improve the utilization of the processor memory, thereby improving the utilization of the processor memory, and thus solving the technical problem of low memory utilization of the processor in the related art.

[0076] In the above embodiment of the present application, the processor also includes a cache for storing multiple first sub-matrices, multiple second sub-matrices and matrix multiplication results; the first matrix to be processed is divided into blocks to obtain multiple first sub-matrices, and the second matrix to be processed is divided into blocks to obtain multiple second sub-matrices, including: based on the capacity of the cache, the first matrix to be processed is divided into blocks to obtain multiple first sub-matrices; based on the capacity of the cache, the second matrix to be processed is divided into blocks to obtain multiple second sub-matrices.

[0077] The cache in the above processors is a high-speed temporary memory used to store data and instructions that the processor needs to access frequently. The cache can reduce the number of accesses to the main memory and increase the access speed of data and instructions, thereby speeding up the operation of the processor. The cache in the processor is usually divided into three levels, including the first level cache (L1 cache), the second level cache (L2 cache) and the third level cache (L3 cache). The capacity and speed of caches at different levels are different.

[0078] Since the capacity of the cache is limited, if the matrix to be processed is large, it is difficult to load the entire matrix into the cache for calculation at one time. Therefore, for the first matrix to be processed and the second matrix to be processed, the large matrix can be divided into multiple sub-matrices through block operation. During the calculation process, the sub-matrices can be loaded into the cache one by one for calculation, so as to make full use of the capacity of the cache and improve the calculation efficiency.

[0079] By dividing the first matrix to be processed and the second matrix to be processed into blocks according to the capacity of the cache, a large-scale computing task can be split into multiple small-scale subtasks, thereby better utilizing computing resources and improving computing efficiency.

[0080] Figure 7 This is a schematic diagram of dividing a matrix to be processed according to an embodiment of the present application. In multi-core computing, if the matrix is ​​not divided into blocks, the data accessed by multiple processes within a period of time will be more discrete, resulting in frequent refreshes in the L2 cache, and data is difficult to reuse well in the L2 cache. Therefore, the matrix needs to be divided into blocks. The block method is to divide m, n, and k so that the data accessed by multiple processes within a period of time can be found in the L2 cache without accessing the main memory. Figure 7 As shown, m, n, and k can be divided into m_blk, n_blk, and k_blk sizes respectively, and three sub-matrices A_BLK[m_blk, k_blk], B_BLK[n_blk, k_blk], and C_BLK[m_blk, n_blk] are obtained, which can be placed in the L2 cache. The block size is related to the L2 cache size and the values ​​of m, n, and k.

[0081] Fig. 9 is a schematic diagram of a multi-processing core computing method after block division according to an embodiment of the present application, such as Fig. 9 As shown, the blocked matrix can be distributed to multiple processing cores for calculation. Taking 4 processes as an example, the C matrix can be divided into 4 blocks by rows and columns and distributed to 4 processing cores for calculation. For example, to calculate c00, it is necessary to rely on two pending sub-matrices a00 and b00. To calculate c01, it is necessary to rely on two pending sub-matrices a00 and b01. In this way, the data reuse rate can be improved in the cache. The inner product calculation method of different processing cores of multiple processing cores is used to complete the calculation. After the calculation of multiple processing cores is completed, the next matrix block is traversed and continued to be allocated to multiple processing cores for calculation.

[0082] In the above embodiment of the present application, a first sub-matrix to be processed corresponding to a plurality of processing cores is determined from a plurality of first sub-matrices, and a second sub-matrix to be processed corresponding to a plurality of processing cores is determined from a plurality of second sub-matrices, including: based on the number of the plurality of processing cores, determining a first traversal window corresponding to the plurality of first sub-matrices, and a second traversal window corresponding to the plurality of second sub-matrices; traversing the plurality of first sub-matrices based on the first traversal window to obtain a first sub-matrix to be processed, and traversing the plurality of second sub-matrices based on the second traversal window to obtain a second sub-matrix to be processed.

[0083] The above-mentioned traversal window may be the position and size of the sub-matrix in the entire matrix, and may be used to determine the range of the sub-matrix that needs to be processed by different processing cores.

[0084] In an optional embodiment, a first traversal window corresponding to a plurality of first sub-matrices may be determined according to the number of a plurality of processing cores, and the first traversal window may be a sub-matrix range that the plurality of first sub-matrices may be processed by the plurality of processing cores. A second traversal window corresponding to a plurality of second sub-matrices may be determined according to the number of a plurality of processing cores, and the second traversal window may be a sub-matrix range that the plurality of second sub-matrices may be processed by the plurality of processing cores.

[0085] By traversing multiple first sub-matrices through the first traversal window to obtain the first sub-matrix to be processed, and traversing multiple second sub-matrices through the second traversal window to obtain the second sub-matrix to be processed, multiple sub-matrices to be processed can be obtained, so that they can be allocated to different processing cores for parallel processing, thereby improving processing speed and efficiency. At the same time, by reasonably determining the traversal window corresponding to the sub-matrix, it can be ensured that multiple processing cores will not repeat processing or omit any part.

[0086] In the above embodiment of the present application, multiple first sub-matrices are traversed based on the first traversal window to obtain a first sub-matrix to be processed, and multiple second sub-matrices are traversed based on the second traversal window to obtain a second sub-matrix to be processed, including: based on the second traversal window, traversing the multiple second sub-matrices in a horizontal direction to obtain the second sub-matrix to be processed; based on the first traversal window, traversing the multiple first sub-matrices in a vertical direction to obtain the first sub-matrix to be processed; wherein, when the multiple second sub-matrices complete one traversal in the horizontal direction, the multiple first sub-matrices are traversed in the vertical direction based on the first traversal window.

[0087] In an optional embodiment, multiple second sub-matrices can be traversed in the horizontal direction according to the second traversal window to obtain the second sub-matrix to be processed, so that the entire matrix is ​​divided and processed in the horizontal direction, which is suitable for the situation where operations in the horizontal direction are required; when multiple second sub-matrices complete one traversal in the horizontal direction, multiple first sub-matrices can be traversed in the vertical direction according to the first traversal window, so that the entire matrix can be effectively traversed in the horizontal and vertical directions.

[0088] In the case where multiple second sub-matrices are traversed once in the horizontal direction, multiple first sub-matrices can be traversed in the vertical direction based on the first traversal window. In the case where multiple first sub-matrices are traversed once in the vertical direction by the first traversal window, multiple first sub-matrices can be traversed in the horizontal direction based on the second traversal window. Multiple first sub-matrices and multiple second sub-matrices can be traversed in this cross traversal manner to obtain a first sub-matrix to be processed and a second sub-matrix to be processed.

[0089] By traversing a large matrix in blocks, the entire matrix can be processed more efficiently, reducing the number of repeated calculations and memory accesses, and improving the efficiency of the algorithm. This method of combining horizontal and vertical block traversal can better utilize computing resources and improve the parallelism and overall performance of the algorithm.

[0090] In the above embodiment of the present application, matrix multiplication operations are performed on the first sub-matrix to be processed and the second sub-matrix to be processed in parallel by multiple processing cores to obtain matrix multiplication results corresponding to the multiple processing cores, including: extracting elements of the first sub-matrix to be processed in the horizontal direction to obtain at least one first element; extracting elements of the second sub-matrix to be processed in the vertical direction to obtain at least one second element; using multiplication and addition operation instructions to perform multiplication and addition operations on at least one first element and at least one second element respectively to obtain at least one multiplication and addition operation result; using addition operation instructions to accumulate at least one multiplication and addition operation result to obtain a matrix multiplication result.

[0091] In an optional embodiment, elements of the two sub-matrices to be processed can be extracted in the horizontal direction and the vertical direction respectively to obtain at least one first element and at least one second element, so that the computing resources can be effectively used for parallel computing to improve the computing efficiency, and then the multiplication and addition operation instructions are used to perform multiplication and addition operations on the first element and the second element to obtain the multiplication and addition operation results, which can reduce the number of multiplication and addition calculations and improve the computing speed. Finally, the multiplication and addition operation results are accumulated using the addition operation instructions to obtain the final matrix multiplication result.

[0092] In practical applications, this block processing and parallel computing method can accelerate the calculation process of matrix multiplication, especially for large-scale matrix multiplication, it can significantly reduce the calculation time. In addition, by utilizing the vectorized instructions and parallel computing capabilities of modern computers, the computing efficiency can be further improved to achieve high-performance matrix multiplication operations.

[0093] In the above embodiments of the present application, any one processing core includes at least one register for storing at least one first element, at least one second element and at least one multiplication-addition operation result; extracting elements of the first submatrix to be processed in a horizontal direction to obtain at least one first element includes: determining a first extraction quantity corresponding to the first submatrix to be processed based on the number of at least one register, and extracting the first extraction quantity of elements from the first submatrix to be processed in a horizontal direction to obtain at least one first element; extracting elements of the second submatrix to be processed in a vertical direction to obtain at least one second element includes: determining a second extraction quantity corresponding to the second submatrix to be processed based on the number of at least one register, and extracting the second extraction quantity of elements from the second submatrix to be processed in a vertical direction to obtain at least one second element.

[0094] The first extraction quantity mentioned above may be the number of elements extracted from the first sub-matrix to be processed through the register; the second extraction quantity mentioned above may be the number of elements extracted from the second sub-matrix to be processed through the register.

[0095] The above-mentioned register is a storage device used to store temporary data in the processor. Registers can store instructions, data, addresses and other temporary results for calculations and logical operations within the processor. The functions of registers include fast access, temporary storage, and control signals.

[0096] Since registers are storage devices inside the processor, they can quickly read and write data within one clock cycle, thus speeding up data access. Registers are used to store temporary data and calculation results, which can be used for calculations and logical operations inside the processor to execute various instructions. The data stored in registers can be used as control signals to control various functions and operations of the processor. Registers can be used to transfer data between different components, such as transferring data from memory to the processor's registers for calculations.

[0097] In an optional embodiment, determining the first extraction quantity corresponding to the first to-be-processed submatrix according to the number of registers can ensure that the capacity of the register can be fully utilized when processing data, so as to improve computing efficiency and reduce the cost of data transmission. If the extraction quantity exceeds the capacity of the register, it will cause the data to be difficult to be stored in the register completely, thereby requiring additional data transmission and storage operations, affecting computing efficiency. Therefore, determining the extraction quantity according to the number of registers can better optimize the data processing flow and improve computing performance.

[0098] In an optional embodiment, the first extraction quantity corresponding to the submatrix to be processed can be determined according to the number of registers. Then, the elements of the first extraction quantity are extracted from the first submatrix to be processed in the horizontal direction to obtain at least one first element. The elements of the second extraction quantity are extracted from the second submatrix to be processed in the vertical direction to obtain at least one second element. By determining the extraction quantity and extracting the elements in the horizontal direction, it can be ensured that the required data is correctly extracted and used for subsequent processing. By extracting the first element in the horizontal direction and extracting the second element in the vertical direction, the required data can be obtained faster, thereby accelerating the subsequent processing process and improving the processing efficiency of the data.

[0099] For example, a 4x4 register can be used to divide the submatrix to be processed, and a vector execution instruction (vector) can be used to calculate the inner product, that is, 4 rows of A and 4 rows of B matrices can be read into the register, and multiplication and accumulation calculations can be performed, and then accumulated into 16 registers, and finally 16 C matrix results are obtained. If M and N cannot be divided by 4, the end of M and N can be divided into 1x4, 4x1 and 1x1 to complete the calculation.

[0100] Figure 8 is a schematic diagram of element extraction according to an embodiment of the present application, such as Figure 8 As shown, the processor includes 32 vector registers. In order to make full use of the vector registers as much as possible, the multiplication of 4 rows of A and 4 columns of B can be calculated simultaneously to obtain the results of 16 C matrices.

[0101] In the above embodiment of the present application, the method also includes: storing the matrix multiplication results corresponding to multiple processing cores into a target matrix, wherein the target matrix is ​​used to represent the matrix obtained by performing matrix multiplication operations on the first matrix to be processed and the second matrix to be processed.

[0102] The above target matrix can be used to represent the multiplication relationship between the first matrix to be processed and the second matrix to be processed.

[0103] In an optional embodiment, after performing a matrix multiplication operation on the first matrix to be processed and the second matrix to be processed, the obtained result can be stored in a target matrix. The target matrix is ​​used to represent the matrix obtained by performing a matrix multiplication operation on the first matrix to be processed and the second matrix to be processed. This process can be implemented by parallel computing, that is, the matrix multiplication task is assigned to multiple processing cores for parallel computing, and the partial results calculated by different processing cores will be stored in the corresponding positions of the target matrix. The various results calculated by the processing cores can be summarized in the target matrix to obtain the final matrix multiplication result. This can effectively improve the calculation speed of matrix multiplication and improve the calculation efficiency.

[0104] This application improves the performance of large-scale matrix multiplication in LLM on the processor, fully utilizes the computing and bandwidth performance of multi-core processors, greatly speeds up the calculation time of matrix multiplication, and makes the LLM model reach the level of 1.1 seconds / token on related devices. This application can be used as a deployment support for LLM by multi-core XuanTie processors. Among them, the XuanTie Processor is mainly aimed at mobile devices and edge computing scenarios, and has the characteristics of high performance and low power consumption. The XuanTie processor is a high-performance, low-power processor designed for artificial intelligence applications, suitable for mobile devices and edge computing scenarios.

[0105] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with the relevant laws, regulations and standards of the relevant countries and regions, and provide corresponding operation entrances for users to choose to authorize or refuse.

[0106] It should be noted that, for the aforementioned method embodiments, for the sake of simplicity, they are all expressed as a series of action combinations, but those skilled in the art should be aware that the present application is not limited by the described order of actions, because according to the present application, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required by the present application.

[0107] Through the description of the above implementation methods, those skilled in the art can clearly understand that the method according to the above embodiment can be implemented by means of software plus a necessary general hardware platform, and of course, it can also be implemented by hardware. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk), and includes a number of instructions for a terminal device (which can be a mobile phone, computer, server, or network device, etc.) to execute the methods of each embodiment of the present application.

[0108] Example 2

[0109] According to an embodiment of the present application, a matrix processing method is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.

[0110] Fig.10 10 is a flowchart of a matrix processing method according to Embodiment 2 of the present application. The method includes:

[0111] Step S1002, dividing the input matrix of the neural network model into blocks to obtain a plurality of first sub-matrices, and dividing the parameter matrix of the neural network model into blocks to obtain a plurality of second sub-matrices;

[0112] The input matrix of the above neural network model refers to the input data matrix of the neural network, usually represented as X, which contains the eigenvalues ​​of the training samples.

[0113] The parameter matrix of the above neural network model refers to the weight matrix and bias matrix in the neural network model, which are used to weight and bias the input data to obtain the output result. During the training process of the neural network, these parameters will be continuously updated and adjusted so that the neural network model can better fit the training data.

[0114] Step S1004, determining first sub-matrices to be processed corresponding to multiple processing cores from the multiple first sub-matrices, and determining second sub-matrices to be processed corresponding to multiple processing cores from the multiple second sub-matrices;

[0115] Step S1006, performing matrix multiplication operation on the first to-be-processed sub-matrix and the second to-be-processed sub-matrix in parallel through multiple processing cores to obtain matrix multiplication results corresponding to the multiple processing cores;

[0116] Step S1008, storing the matrix multiplication results corresponding to the multiple processing cores into a matrix to obtain an activation matrix of the neural network model.

[0117] The activation matrix of the above neural network model refers to the result obtained by multiplying the matrices corresponding to multiple processing cores in the neural network model, which is used to represent the activation state of the neuron. This matrix contains the output values ​​of the neurons in each layer of the neural network and can be used for calculation and prediction of the next layer. The above activation matrix is ​​a relatively important intermediate result in the neural network model and is of great significance for the training and prediction of the model.

[0118] Through the above steps, the input matrix of the neural network model is divided into blocks to obtain multiple first sub-matrices, and the parameter matrix of the neural network model is divided into blocks to obtain multiple second sub-matrices; the first sub-matrices to be processed corresponding to multiple processing cores are determined from the multiple first sub-matrices, and the second sub-matrices to be processed corresponding to multiple processing cores are determined from the multiple second sub-matrices; the first sub-matrices to be processed and the second sub-matrices to be processed are performed in parallel by multiple processing cores to obtain the matrix multiplication results corresponding to the multiple processing cores; the matrix multiplication results corresponding to the multiple processing cores are stored in the matrix to obtain the activation matrix of the neural network model, thereby improving the memory utilization of the processor. It is easy to notice that by dividing the first matrix to be processed and the second matrix to be processed into blocks, multiple processing cores can process the sub-matrices to be processed in parallel, giving full play to the computing and bandwidth performance of the multi-core processor, and greatly speeding up the calculation time of the matrix multiplication. By determining the first and second sub-matrices to be processed corresponding to the multiple processing cores, more refined task allocation and scheduling can be achieved, thereby better utilizing the processor resources. Such a blocking and task allocation method helps to avoid frequent cache updates and ensure local continuity of memory access, thereby improving the utilization of processor memory, thereby improving the utilization of processor memory, and thus solving the technical problem of low processor memory utilization in related technologies.

[0119] In the above embodiment of the present application, the processor also includes a cache for storing multiple first sub-matrices, multiple second sub-matrices and matrix multiplication results; the input matrix of the neural network model is divided into blocks to obtain multiple first sub-matrices, and the parameter matrix of the neural network model is divided into blocks to obtain multiple second sub-matrices, including: based on the capacity of the cache, the input matrix is ​​divided into blocks to obtain multiple first sub-matrices; based on the capacity of the cache, the parameter matrix is ​​divided into blocks to obtain multiple second sub-matrices.

[0120] It should be noted that the preferred implementation scheme involved in the above embodiments of the present application is the same as the scheme provided in Example 1, as well as the application scenario and implementation process, but is not limited to the scheme provided in Example 1.

[0121] Example 3

[0122] According to an embodiment of the present application, a matrix processing device for implementing the above matrix processing method is also provided. Fig.11 is a schematic diagram of a matrix processing device according to Embodiment 3 of the present application, such as Fig.11 As shown, the device 1100 includes: a block division module 1102 , a determination module 1104 , and a calculation module 1106 .

[0123] Among them, the blocking module is used to block the first matrix to be processed to obtain multiple first sub-matrices, and to block the second matrix to be processed to obtain multiple second sub-matrices; the determination module is used to determine the first sub-matrices to be processed corresponding to multiple processing cores from the multiple first sub-matrices, and determine the second sub-matrices to be processed corresponding to the multiple processing cores from the multiple second sub-matrices; the operation module is used to perform matrix multiplication operations on the first sub-matrix to be processed and the second sub-matrix to be processed in parallel through multiple processing cores to obtain matrix multiplication results corresponding to multiple processing cores.

[0124] It should be noted that the above-mentioned block module 1102, determination module 1104, and operation module 1106 correspond to steps S302 to S306 in Example 1, and the three modules and the corresponding steps implement the same examples and application scenarios, but are not limited to the contents disclosed in the above-mentioned Example 1. It should be noted that the above-mentioned modules or units can be hardware components or software components stored in a memory (e.g., memory 104) and processed by one or more processors (e.g., processors 102a, 102b, ..., 102n), and the above-mentioned modules can also be part of the device and can be run in the computer terminal 10 provided in Example 1.

[0125] In the above embodiment of the present application, the processor also includes a cache for storing multiple first sub-matrices, multiple second sub-matrices and matrix multiplication results; the blocking module is also used to block the first matrix to be processed based on the capacity of the cache to obtain multiple first sub-matrices; and block the second matrix to be processed based on the capacity of the cache to obtain multiple second sub-matrices.

[0126] In the above embodiment of the present application, the determination module is used to determine, based on the number of multiple processing cores, a first traversal window corresponding to multiple first sub-matrices and a second traversal window corresponding to multiple second sub-matrices; based on the first traversal window, the multiple first sub-matrices are traversed to obtain a first sub-matrix to be processed, and based on the second traversal window, the multiple second sub-matrices are traversed to obtain a second sub-matrix to be processed.

[0127] In the above embodiment of the present application, the determination module is used to traverse the multiple second sub-matrices in the horizontal direction based on the second traversal window to obtain the second sub-matrix to be processed; based on the first traversal window, traverse the multiple first sub-matrices in the vertical direction to obtain the first sub-matrix to be processed; wherein, when the multiple second sub-matrices complete one traversal in the horizontal direction, the multiple first sub-matrices are traversed in the vertical direction based on the first traversal window.

[0128] In the above embodiment of the present application, the operation module is used to extract elements of the first sub-matrix to be processed in the horizontal direction to obtain at least one first element; extract elements of the second sub-matrix to be processed in the vertical direction to obtain at least one second element; use multiplication and addition operation instructions to perform multiplication and addition operations on at least one first element and at least one second element respectively to obtain at least one multiplication and addition operation result; use addition operation instructions to accumulate at least one multiplication and addition operation result to obtain a matrix multiplication result.

[0129] In the above embodiment of the present application, the operation module is used to determine the first extraction quantity corresponding to the first sub-matrix to be processed based on the number of at least one register, and extract the elements of the first extraction quantity from the first sub-matrix to be processed in the horizontal direction to obtain at least one first element; extract elements of the second sub-matrix to be processed in the vertical direction to obtain at least one second element, including: determining the second extraction quantity corresponding to the second sub-matrix to be processed based on the number of at least one register, and extracting the elements of the second extraction quantity from the second sub-matrix to be processed in the vertical direction to obtain at least one second element.

[0130] In the above embodiments of the present application, the device includes: a storage module.

[0131] The storage module is used to store matrix multiplication results corresponding to multiple processing cores into a target matrix, wherein the target matrix is ​​used to represent a matrix obtained by performing a matrix multiplication operation on a first matrix to be processed and a second matrix to be processed.

[0132] It should be noted that the preferred implementation scheme involved in the above embodiments of the present application is the same as the scheme provided in Example 1, as well as the application scenario and implementation process, but is not limited to the scheme provided in Example 1.

[0133] Example 4

[0134] According to an embodiment of the present application, a matrix processing device for implementing the above matrix processing method is also provided. Fig.12 is a schematic diagram of a matrix processing device according to Embodiment 4 of the present application, such as Fig.12 As shown, the device 1200 includes: a blocking module 1202 , a determining module 1204 , a computing module 1206 , and a storage module 1208 .

[0135] Among them, the blocking module is used to block the input matrix of the neural network model to obtain multiple first sub-matrices, and to block the parameter matrix of the neural network model to obtain multiple second sub-matrices; the determination module is used to determine the first sub-matrices to be processed corresponding to multiple processing cores from the multiple first sub-matrices, and determine the second sub-matrices to be processed corresponding to the multiple processing cores from the multiple second sub-matrices; the operation module is used to perform matrix multiplication operations on the first sub-matrix to be processed and the second sub-matrix to be processed in parallel through multiple processing cores to obtain matrix multiplication results corresponding to the multiple processing cores; the storage module is used to store the matrix multiplication results corresponding to the multiple processing cores in the matrix to obtain the activation matrix of the neural network model.

[0136] It should be noted that the above-mentioned block module 1202, determination module 1204, operation module 1206, and storage module 1208 correspond to steps S1002 to S1008 in Example 1, and the four modules and the corresponding steps implement the same examples and application scenarios, but are not limited to the contents disclosed in the above-mentioned Example 1. It should be noted that the above-mentioned modules or units can be hardware components or software components stored in a memory (e.g., memory 104) and processed by one or more processors (e.g., processors 102a, 102b, ..., 102n), and the above-mentioned modules can also be run in the computer terminal 10 provided in Example 1 as part of the device.

[0137] In the above embodiment of the present application, the processor also includes a cache for storing multiple first sub-matrices, multiple second sub-matrices and matrix multiplication results; the blocking module is used to block the input matrix based on the capacity of the cache to obtain multiple first sub-matrices; and the parameter matrix is ​​blocked based on the capacity of the cache to obtain multiple second sub-matrices.

[0138] It should be noted that the preferred implementation scheme involved in the above embodiments of the present application is the same as the scheme provided in Example 1, as well as the application scenario and implementation process, but is not limited to the scheme provided in Example 1.

[0139] Example 5

[0140] According to an embodiment of the present application, a processor is provided. Fig.13 is a schematic diagram of a processor according to Embodiment 5 of the present application, such as Fig.13 As shown, the processor includes: a scheduling module 1302 and multiple processing cores 1304.

[0141] Among them, the scheduling module is used to divide the first matrix to be processed into blocks to obtain multiple first sub-matrices, and to divide the second matrix to be processed into blocks to obtain multiple second sub-matrices; multiple processing cores are used to determine the first sub-matrices to be processed corresponding to the multiple processing cores from the multiple first sub-matrices, and determine the second sub-matrices to be processed corresponding to the multiple processing cores from the multiple second sub-matrices, and perform matrix multiplication operations on the first sub-matrices to be processed and the second sub-matrices to be processed in parallel to obtain matrix multiplication results corresponding to the multiple processing cores.

[0142] In the above embodiment of the present application, the processor also includes: a cache for storing multiple first sub-matrices, multiple second sub-matrices and matrix multiplication results; the scheduling module is also used to divide the first matrix to be processed into blocks based on the capacity of the cache to obtain multiple first sub-matrices; based on the capacity of the cache, divide the second matrix to be processed into blocks to obtain multiple second sub-matrices.

[0143] In the above embodiments of the present application, any one processing core includes: a processing module, used to extract elements of a first sub-matrix to be processed in a horizontal direction to obtain at least one first element; extract elements of a second sub-matrix to be processed in a vertical direction to obtain at least one second element; use multiplication and addition operation instructions to perform multiplication and addition operations on at least one first element and at least one second element respectively to obtain at least one multiplication and addition operation result; use addition operation instructions to accumulate at least one multiplication and addition operation result to obtain a matrix multiplication result.

[0144] In the above embodiments of the present application, any one processing core also includes: at least one register for storing at least one first element, at least one second element and at least one multiplication and addition operation result; the processing module is also used to extract elements of the first sub-matrix to be processed in the horizontal direction to obtain at least one first element, including: based on the number of at least one register, determine the first extraction number corresponding to the first sub-matrix to be processed, and extract the first extraction number of elements from the first sub-matrix to be processed in the horizontal direction to obtain at least one first element; extract elements of the second sub-matrix to be processed in the vertical direction to obtain at least one second element, including: based on the number of at least one register, determine the second extraction number corresponding to the second sub-matrix to be processed, and extract the second extraction number of elements from the second sub-matrix to be processed in the vertical direction to obtain at least one second element.

[0145] Example 6

[0146] According to an embodiment of the present application, a system on chip is provided, comprising a processor according to any one of the above embodiments.

[0147] Example 7

[0148] The embodiment of the present application may provide an electronic device, which may be any electronic device in a group of electronic devices. Optionally, in this embodiment, the electronic device may also be replaced by a terminal device such as a mobile terminal.

[0149] Optionally, in this embodiment, the electronic device may be located in at least one network device among a plurality of network devices of a computer network.

[0150] In this embodiment, the computer terminal can execute the program code in the method.

[0151] Optionally, Fig.14 is a structural block diagram of an electronic device according to an embodiment of the present application. Fig.14 As shown, the electronic device A may include: one or more ( Fig.14 (only one is shown) processor 102, memory 104, storage controller, and peripheral interface, wherein the peripheral interface is connected to the radio frequency module, audio module and display.

[0152] Among them, the memory can be used to store software programs and modules, such as program instructions / modules corresponding to the methods and devices in the embodiments of the present application. The processor executes various functional applications and data processing by running the software programs and modules stored in the memory, that is, implementing the methods in the above embodiments. The memory may include a high-speed random access memory, and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory may further include a memory remotely arranged relative to the processor, and these remote memories may be connected to the terminal A via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.

[0153] The processor can call the information and application programs stored in the memory through the transmission device to execute the following steps: divide the first matrix to be processed into blocks to obtain multiple first sub-matrices, and divide the second matrix to be processed into blocks to obtain multiple second sub-matrices; determine the first sub-matrices to be processed corresponding to multiple processing cores from the multiple first sub-matrices, and determine the second sub-matrices to be processed corresponding to the multiple processing cores from the multiple second sub-matrices; perform matrix multiplication operations on the first sub-matrices to be processed and the second sub-matrices to be processed in parallel through multiple processing cores to obtain matrix multiplication results corresponding to the multiple processing cores.

[0154] The processor can call the information and application programs stored in the memory through the transmission device to execute the following steps: divide the input matrix of the neural network model into blocks to obtain multiple first sub-matrices, and divide the parameter matrix of the neural network model into blocks to obtain multiple second sub-matrices; determine the first sub-matrices to be processed corresponding to the multiple processing cores from the multiple first sub-matrices, and determine the second sub-matrices to be processed corresponding to the multiple processing cores from the multiple second sub-matrices; perform matrix multiplication operations on the first sub-matrices to be processed and the second sub-matrices to be processed in parallel through multiple processing cores to obtain matrix multiplication results corresponding to the multiple processing cores; store the matrix multiplication results corresponding to the multiple processing cores in the matrix to obtain the activation matrix of the neural network model.

[0155] According to the embodiment of the present application, the first matrix to be processed is divided into blocks to obtain multiple first sub-matrices, and the second matrix to be processed is divided into blocks to obtain multiple second sub-matrices; the first sub-matrices to be processed corresponding to multiple processing cores are determined from the multiple first sub-matrices, and the second sub-matrices to be processed corresponding to multiple processing cores are determined from the multiple second sub-matrices; the first sub-matrices to be processed and the second sub-matrices to be processed are multiplied by multiple processing cores in parallel to obtain the matrix multiplication results corresponding to the multiple processing cores, thereby improving the memory utilization of the processor. It is easy to notice that the first matrix to be processed and the second matrix to be processed can be divided into blocks so that multiple processing cores can process the sub-matrices to be processed in parallel, giving full play to the computing and bandwidth performance of the multi-core processor, and greatly speeding up the calculation time of the matrix multiplication. By determining the first and second sub-matrices to be processed corresponding to the multiple processing cores, more sophisticated task allocation and scheduling can be achieved, thereby better utilizing the processor resources. Such a block and task allocation method helps to avoid frequent cache updates, ensure that memory access has local continuity, and thus improve the utilization of the processor memory, thereby improving the utilization of the processor memory, and thus solving the technical problem of low memory utilization of the processor in the related art.

[0156] Those skilled in the art will understand that Fig.14 The structure shown is for illustration only, and the electronic device may also be a smart phone (such as an Android phone, an iOS phone, etc.), a tablet computer, a PDA, a mobile Internet device (Mobile Internet Devices, MID), a PAD, and other terminal devices. Fig.14 It does not limit the structure of the above electronic device. For example, the electronic device A may also include Fig.14 More or fewer components (such as network interfaces, display devices, etc.) shown in the figure, or having Fig.14 Different configurations are shown.

[0157] A person of ordinary skill in the art can understand that all or part of the steps in the various methods of the above embodiments can be completed by instructing the hardware related to the terminal device through a program, and the program can be stored in a computer-readable storage medium, and the storage medium may include: a flash drive, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, etc.

[0158] Example 8

[0159] The embodiment of the present application further provides a computer-readable storage medium. Optionally, in this embodiment, the computer-readable storage medium can be used to store the program code executed by the method provided in the above embodiment.

[0160] Optionally, in this embodiment, the above storage medium may be located in any electronic device in a group of electronic devices in a computer network, or in any mobile terminal in a group of mobile terminals.

[0161] Optionally, in this embodiment, the computer-readable storage medium is configured to store program codes for executing the following steps: dividing the first matrix to be processed into blocks to obtain multiple first sub-matrices, and dividing the second matrix to be processed into blocks to obtain multiple second sub-matrices; determining the first sub-matrices to be processed corresponding to multiple processing cores from the multiple first sub-matrices, and determining the second sub-matrices to be processed corresponding to the multiple processing cores from the multiple second sub-matrices; performing matrix multiplication operations on the first sub-matrix to be processed and the second sub-matrix to be processed in parallel by multiple processing cores to obtain matrix multiplication results corresponding to the multiple processing cores.

[0162] Optionally, the computer-readable storage medium is also configured to store program code for executing the following steps: dividing the input matrix of the neural network model into blocks to obtain multiple first sub-matrices, and dividing the parameter matrix of the neural network model into blocks to obtain multiple second sub-matrices; determining first sub-matrices to be processed corresponding to multiple processing cores from the multiple first sub-matrices, and determining second sub-matrices to be processed corresponding to multiple processing cores from the multiple second sub-matrices; performing matrix multiplication operations on the first sub-matrices to be processed and the second sub-matrices to be processed in parallel by multiple processing cores to obtain matrix multiplication results corresponding to the multiple processing cores; storing the matrix multiplication results corresponding to the multiple processing cores in a matrix to obtain an activation matrix of the neural network model.

[0163] According to the embodiment of the present application, the first matrix to be processed is divided into blocks to obtain multiple first sub-matrices, and the second matrix to be processed is divided into blocks to obtain multiple second sub-matrices; the first sub-matrices to be processed corresponding to multiple processing cores are determined from the multiple first sub-matrices, and the second sub-matrices to be processed corresponding to multiple processing cores are determined from the multiple second sub-matrices; the first sub-matrices to be processed and the second sub-matrices to be processed are multiplied by multiple processing cores in parallel to obtain the matrix multiplication results corresponding to the multiple processing cores, thereby improving the memory utilization of the processor. It is easy to notice that the first matrix to be processed and the second matrix to be processed can be divided into blocks so that multiple processing cores can process the sub-matrices to be processed in parallel, giving full play to the computing and bandwidth performance of the multi-core processor, and greatly speeding up the calculation time of the matrix multiplication. By determining the first and second sub-matrices to be processed corresponding to the multiple processing cores, more sophisticated task allocation and scheduling can be achieved, thereby better utilizing the processor resources. Such a block and task allocation method helps to avoid frequent cache updates, ensure that memory access has local continuity, and thus improve the utilization of the processor memory, thereby improving the utilization of the processor memory, and thus solving the technical problem of low memory utilization of the processor in the related art.

[0164] Example 9

[0165] The embodiment of the present application further provides a computer program product. Optionally, in this embodiment, the computer program product may include a computer program, and the computer program implements the method provided in the embodiment when executed by a processor.

[0166] Example 10

[0167] The embodiments of the present application also provide a computer program product. Optionally, the computer program product may include a non-volatile computer-readable storage medium, which may be used to store a computer program, and when the computer program is executed by a processor, the method provided in the embodiments is implemented.

[0168] Embodiment 11

[0169] The embodiment of the present application further provides a computer program. Optionally, in this embodiment, when the computer program is executed by a processor, the method provided in the above embodiment is implemented.

[0170] The serial numbers of the above-mentioned embodiments of the present application are for description only and do not represent the advantages or disadvantages of the embodiments.

[0171] In the above embodiments of the present application, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, please refer to the relevant description of other embodiments.

[0172] In the several embodiments provided in this application, it should be understood that the disclosed technical content can be implemented in other ways. Among them, the device embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of units or modules, which can be electrical or other forms.

[0173] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.

[0174] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.

[0175] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application, in essence, or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, a server or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: U disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), mobile hard disk, disk or optical disk and other media that can store program codes.

[0176] The above is only a preferred implementation of the present application. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principles of the present application. These improvements and modifications should also be regarded as the scope of protection of the present application.

Claims

1. A matrix processing method, It is characterized in that Applied to a processor, the processor includes multiple processing cores, and the method includes: Divide the first matrix to be processed into blocks to obtain a plurality of first sub-matrices, and divide the second matrix to be processed into blocks to obtain a plurality of second sub-matrices; Determine a first sub-matrix to be processed corresponding to the plurality of processing cores from the plurality of first sub-matrices, and determine a second sub-matrix to be processed corresponding to the plurality of processing cores from the plurality of second sub-matrices; The plurality of processing cores are used to perform matrix multiplication operations on the first to-be-processed sub-matrix and the second to-be-processed sub-matrix in parallel to obtain matrix multiplication results corresponding to the plurality of processing cores.

2. The method according to claim 1, It is characterized in that The processor further includes a cache for storing the plurality of first sub-matrices, the plurality of second sub-matrices and the matrix multiplication result; The first matrix to be processed is divided into blocks to obtain a plurality of first sub-matrices, and the second matrix to be processed is divided into blocks to obtain a plurality of second sub-matrices, including: Based on the capacity of the cache, the first matrix to be processed is divided into blocks to obtain the multiple first sub-matrices; Based on the capacity of the cache, the second matrix to be processed is divided into blocks to obtain the multiple second sub-matrices.

3. The method according to claim 1, It is characterized in that The step of determining the first to-be-processed sub-matrices corresponding to the plurality of processing cores from the plurality of first sub-matrices, and determining the second to-be-processed sub-matrices corresponding to the plurality of processing cores from the plurality of second sub-matrices comprises: Based on the number of the plurality of processing cores, determining first traversal windows corresponding to the plurality of first sub-matrices and second traversal windows corresponding to the plurality of second sub-matrices; The multiple first sub-matrices are traversed based on the first traversal window to obtain the first sub-matrix to be processed, and the multiple second sub-matrices are traversed based on the second traversal window to obtain the second sub-matrix to be processed.

4. The method according to claim 3, It is characterized in that The traversing the plurality of first sub-matrices based on the first traversal window to obtain the first sub-matrix to be processed, and traversing the plurality of second sub-matrices based on the second traversal window to obtain the second sub-matrix to be processed, comprises: Based on the second traversal window, traverse the plurality of second sub-matrices in a horizontal direction to obtain the second sub-matrix to be processed; Based on the first traversal window, traverse the plurality of first sub-matrices in a vertical direction to obtain the first sub-matrix to be processed; Wherein, when the plurality of second sub-matrices are traversed once in the horizontal direction, the plurality of first sub-matrices are traversed in the vertical direction based on the first traversal window.

5. The method according to claim 1, It is characterized in that The performing matrix multiplication operations on the first to-be-processed sub-matrix and the second to-be-processed sub-matrix in parallel by the multiple processing cores to obtain matrix multiplication results corresponding to the multiple processing cores includes: Extracting elements from the first to-be-processed submatrix in a horizontal direction to obtain at least one first element; Extracting elements from the second to-be-processed submatrix in a vertical direction to obtain at least one second element; Using a multiplication-addition instruction, respectively perform multiplication-addition operations on the at least one first element and the at least one second element to obtain at least one multiplication-addition operation result; The at least one multiplication-addition operation result is accumulated using an addition operation instruction to obtain the matrix multiplication result.

6. The method according to claim 5, It is characterized in that Any one of the processing cores comprises at least one register for storing the at least one first element, the at least one second element and the at least one multiplication-addition operation result; The extracting elements of the first submatrix to be processed in the horizontal direction to obtain at least one first element comprises: determining a first extraction quantity corresponding to the first submatrix to be processed based on the quantity of the at least one register, and extracting the first extraction quantity of elements from the first submatrix to be processed in the horizontal direction to obtain the at least one first element; The step of extracting elements of the second submatrix to be processed in a vertical direction to obtain at least one second element includes: determining a second extraction quantity corresponding to the second submatrix to be processed based on the quantity of the at least one register, and extracting the second extraction quantity of elements from the second submatrix to be processed in a vertical direction to obtain the at least one second element.

7. The method according to any one of claims 1 to 6, It is characterized in that The method further comprises: The matrix multiplication results corresponding to the multiple processing cores are stored in a target matrix, wherein the target matrix is ​​used to represent a matrix obtained by performing a matrix multiplication operation on the first matrix to be processed and the second matrix to be processed.

8. A matrix processing method, It is characterized in that Applied to a processor, the processor includes multiple processing cores, and the method includes: The input matrix of the neural network model is divided into blocks to obtain a plurality of first sub-matrices, and the parameter matrix of the neural network model is divided into blocks to obtain a plurality of second sub-matrices; Determine a first sub-matrix to be processed corresponding to the plurality of processing cores from the plurality of first sub-matrices, and determine a second sub-matrix to be processed corresponding to the plurality of processing cores from the plurality of second sub-matrices; Performing matrix multiplication operations on the first to-be-processed sub-matrix and the second to-be-processed sub-matrix in parallel through the multiple processing cores to obtain matrix multiplication results corresponding to the multiple processing cores; The matrix multiplication results corresponding to the multiple processing cores are stored in a matrix to obtain an activation matrix of the neural network model.

9. The method according to claim 8, It is characterized in that The processor further includes a cache for storing the plurality of first sub-matrices, the plurality of second sub-matrices and the matrix multiplication result; the input matrix of the neural network model is divided into blocks to obtain a plurality of first sub-matrices, and the parameter matrix of the neural network model is divided into blocks to obtain a plurality of second sub-matrices, including: Based on the capacity of the cache, the input matrix is ​​divided into blocks to obtain the plurality of first sub-matrices; The parameter matrix is ​​divided into blocks based on the capacity of the cache to obtain the multiple second sub-matrices.

10. A processor, It is characterized in that include: A scheduling module, used for dividing the first matrix to be processed into blocks to obtain a plurality of first sub-matrices, and dividing the second matrix to be processed into blocks to obtain a plurality of second sub-matrices; Multiple processing cores are used to determine first sub-matrices to be processed corresponding to the multiple processing cores from the multiple first sub-matrices, and determine second sub-matrices to be processed corresponding to the multiple processing cores from the multiple second sub-matrices, and perform matrix multiplication operations on the first sub-matrices to be processed and the second sub-matrices to be processed in parallel to obtain matrix multiplication results corresponding to the multiple processing cores.

11. The processor according to claim 10, It is characterized in that The processor further comprises: A cache, used to store the multiple first sub-matrices, the multiple second sub-matrices and the matrix multiplication result; The scheduling module is further used to divide the first matrix to be processed into blocks based on the capacity of the cache to obtain the multiple first sub-matrices; and divide the second matrix to be processed into blocks based on the capacity of the cache to obtain the multiple second sub-matrices.

12. The processor according to claim 10, It is characterized in that Any processing core includes: A processing module is used to extract elements of the first submatrix to be processed in the horizontal direction to obtain at least one first element; extract elements of the second submatrix to be processed in the vertical direction to obtain at least one second element; use multiplication and addition operation instructions to perform multiplication and addition operations on the at least one first element and the at least one second element respectively to obtain at least one multiplication and addition operation result; use addition operation instructions to accumulate the at least one multiplication and addition operation result to obtain the matrix multiplication result.

13. The processor according to claim 12, It is characterized in that Any processing core also includes: at least one register for storing the at least one first element, the at least one second element, and the at least one multiplication-addition operation result; The processing module is further used for extracting elements from the first submatrix to be processed in the horizontal direction to obtain at least one first element, including: determining a first extraction quantity corresponding to the first submatrix to be processed based on the quantity of the at least one register, and extracting the first extraction quantity of elements from the first submatrix to be processed in the horizontal direction to obtain the at least one first element; extracting elements from the second submatrix to be processed in the vertical direction to obtain at least one second element, including: Based on the number of the at least one register, a second extraction number corresponding to the second sub-matrix to be processed is determined, and elements of the second extraction number are extracted from the second sub-matrix to be processed in a vertical direction to obtain the at least one second element.

14. A system on a chip, It is characterized in that include: A processor as claimed in any one of claims 10 to 13.

15. An electronic device, It is characterized in that include: A memory storing an executable program; A processor, configured to run the program, wherein the program executes the method according to any one of claims 1 to 9 when running.

16. A computer-readable storage medium, It is characterized in that The computer-readable storage medium includes a stored executable program, wherein when the executable program is executed, the device where the storage medium is located is controlled to execute the method according to any one of claims 1 to 9.

17. A computer program product, It is characterized in that The invention comprises a computer program which, when executed by a processor, implements the method according to any one of claims 1 to 9.

Citation Information

Cited By

  • Parallel processing method and device of model, electronic equipment and readable storage medium

    CN119960970A

  • Parallel processing method and device of model, electronic equipment and readable storage medium

    CN119960970B

  • Method for performing matrix multiplication operation by processor comprising plurality of computing units

    CN120234516A

  • Matrix multiplication implementation method and device, electronic equipment, storage medium and program product

    CN121210826A

  • Matrix multiplication implementation method and device, electronic equipment, storage medium and program product

    CN121210826B