A convolution calculation method, SOC chip, electronic device and storage medium
By abstracting the two-dimensional matrix into a three-dimensional cylinder, data expansion is avoided, the problem of data duplication in the existing convolution method is solved, the computing efficiency and performance are improved, and it is suitable for various convolutional neural networks.
Patent Information
- Application Number
- CN202211718228.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-29
- Publication Date
- 2024-10-11
- Estimated Expiration
- 2042-12-29
AI Technical Summary
Existing convolution methods need to expand data, resulting in a large amount of duplicate data, increasing memory usage and data handling, and reducing computing efficiency and performance.
By abstracting the two-dimensional matrix into a three-dimensional cylinder, the cylinder is obtained from the input feature map and calculated with the convolution kernel during convolution calculation to avoid data expansion and achieve efficient convolution operations.
It improves the efficiency and performance-to-power ratio of convolution calculations, reduces memory usage and data transfer volume, and supports the increasing demand for computing power.
Smart Images

Figure CN116050474B_ABST
Abstract
Description
Technical Field
[0001] The present application belongs to the field of computer technology, and specifically relates to a convolution calculation method, a SOC chip, an electronic device and a storage medium. Background Art
[0002] Convolution is widely used in traditional image filtering and the currently popular deep learning. Its characteristics of local network connection and convolution kernel parameter sharing make convolutional neural network the mainstream in image recognition and image classification. The core operation of convolution is sliding window. Figure 1 The convolution process of a single channel convolution operation is shown. Each weight in the convolution kernel is multiplied and accumulated with the corresponding data in the input feature map according to its relative position in the convolution kernel. The input feature map size of the example is H=6, W=6; the convolution kernel size is R=3, S=3; the output feature map size is P=4, Q=4; stride=1, indicating the relative offset of each convolution. The relationship between the variables is P=(HR) / stride+1, Q=(WS) / stride+1.
[0003] Currently, when performing convolution operations, the convolution operation is usually converted into a matrix operation through im2col (Image to Column). Figure 2 Shows the Figure 1 The principle of converting the convolution operation into matrix operation is shown. Figure 2 Each row of the left matrix corresponds to an input feature map corresponding to a convolution operation. One row has R*S=9 input feature values. Since the step size is 1, the sliding window needs to be moved for 16 convolution operations, so there are a total of P*Q=16 rows. However, due to the sliding window characteristics of the convolution, the left matrix will have a large amount of repeated data in the diagonal direction. The right matrix is the column vector of the expanded convolution kernel, so that the column vector of 16 output feature values can be obtained through matrix operations, which is actually the 4*4 output feature value generated by the convolution operation.
[0004] According to the location and method of expanding data, it is generally divided into explicit GEMM (GEneral Matrix Multiplication), implicit GEMM and pre-compute offset GEMM. However, all three general matrix multiplication methods require the input feature map data to be expanded, which will generate a lot of duplicate data, which will increase memory usage and data handling. Summary of the invention
[0005] In view of this, the purpose of the present application is to provide a convolution calculation method, a SOC chip, an electronic device and a storage medium to improve the problem that the existing convolution method needs to expand the data, resulting in a lot of duplicate data, thereby increasing memory usage and data handling.
[0006] The embodiment of the present application is implemented as follows:
[0007] In a first aspect, an embodiment of the present application provides a convolution calculation method, the method comprising: obtaining input feature map data containing H*W first cylinders, wherein H represents the height of the input feature map data, W represents the width of the input feature map data, and one of the first cylinders contains Cin data at an element position on the H*W dimension along the direction of the input channel, and Cin is the number of input channels. Obtain N convolution kernels containing R*S second cylinders, wherein R represents the height of each of the convolution kernels, S represents the width of each of the convolution kernels, and one of the second cylinders contains Cin data at an element position on the R*S dimension along the direction of the input channel, and N is a positive integer greater than or equal to 1; obtain H*S first cylinders from the input feature map data in each round, and perform convolution calculations on the H*S first cylinders obtained in this round with the corresponding second cylinders in the N convolution kernels to obtain a column in the output feature map, and after Q rounds of convolution calculations, obtain a convolution kernel containing P*Q second cylinders. The output feature map of three cylinders; or, in each round, W*R first cylinders are obtained from the input feature map data, and the W*R first cylinders obtained in this round are convolved with the corresponding second cylinders in N convolution kernels to obtain a row in the output feature map, and after P rounds of convolution calculations, an output feature map containing P*Q third cylinders is obtained; wherein P represents the height of the output feature map, Q represents the width of the output feature map, and each of the third cylinders contains N elements, and each element is the multiplication and accumulation result of R*S second cylinders and their corresponding first cylinders.
[0008] In an embodiment of the present application, by abstracting a row or a column in a two-dimensional matrix into a three-dimensional cylinder (array), when performing convolution calculation, it is only necessary to obtain H*S first cylinders from the input feature map data in each round, and convolve the H*S first cylinders obtained in this round with the corresponding second cylinders in N convolution kernels, respectively, to obtain a column in the output feature map, and after Q rounds of convolution calculation, or, in each round, W*R first cylinders are obtained from the input feature map data, and convolve the W*R first cylinders obtained in this round with the corresponding second cylinders in N convolution kernels, respectively, to obtain a row in the output feature map, and after P rounds of convolution calculation, an output feature map containing P*Q third cylinders can be obtained, thereby achieving the required convolution calculation without expanding the input feature map data, further improving the convolution calculation efficiency and performance-power consumption ratio, and improving the existing convolution method that needs to expand the data, resulting in a lot of duplicate data, thereby increasing memory usage and data handling problems.
[0009] In combination with a possible implementation method of the first aspect, H*S first cylinders are obtained from the input feature map data in each round, and the H*S first cylinders obtained in this round are respectively convolved with the corresponding second cylinders in the N convolution kernels, including: each time, the second cylinder at the i-th element position of each convolution kernel in the N convolution kernels is selected to form a right matrix of Cin*N, and i is 1 to R*S along the R direction; each time, P cylinders corresponding to the second cylinder at the i-th element position are selected from the input feature map data along the H direction to form a left matrix of P*Cin; each time, the left matrix is multiplied with the corresponding right matrix to obtain a product result; the R*S product results are accumulated according to the corresponding element positions to obtain a column in the output feature map.
[0010] In the embodiment of the present application, the second cylinder at the same element position of each convolution kernel is selected each time to form a right matrix, and then P first cylinders corresponding to the currently selected second cylinder are selected to form a left matrix, and the left matrix is multiplied by the right matrix. According to the sliding window order of the convolution and the relative position of the convolution kernel in the input feature map, the previous process is repeated, and the product results of all R*S second cylinders in the convolution kernel are added together. In this way, one round of convolution operation is completed, and a column of P third cylinders is obtained, thereby realizing the convolution operation without expanding the data.
[0011] In combination with a possible implementation manner of the embodiment of the first aspect, W*R first cylinders are obtained from the input feature map data in each round, and the W*R first cylinders obtained in this round are respectively convolved with the corresponding second cylinders in the N convolution kernels, including: each time the second cylinder at the i-th element position of each convolution kernel in the N convolution kernels is selected to form a left matrix of N*Cin, where i is 1 to R*S along the S direction; each time Q cylinders corresponding to the second cylinder at the i-th element position are selected from the input feature map data along the W direction to form a right matrix of Cin*Q; each time the left matrix is multiplied with the corresponding right matrix to obtain a product result; the R*S product results are accumulated according to the corresponding element positions to obtain a row in the output feature map.
[0012] In the embodiment of the present application, the second cylinders at the same element position of each convolution kernel are selected each time to form a left matrix, and then Q first cylinders corresponding to the currently selected second cylinders are selected to form a right matrix. The left matrix is multiplied by the right matrix, and the previous process is repeated according to the sliding window order of the convolution and the relative position of the convolution kernel in the input feature map. The product results of all R*S second cylinders in the convolution kernel are added together. In this way, one round of convolution operation is completed, and a row of Q third cylinders is obtained, thereby realizing the convolution operation without expanding the data.
[0013] In combination with a possible implementation method of the first aspect, H*S first cylinders are obtained from the input feature map data in each round, including: in each round of simulation, a sliding window is performed in the input feature map data along the W direction according to a preset step size, and H*S first cylinders obtained by sliding the window are selected from the input feature map data.
[0014] In an embodiment of the present application, when obtaining H*S first cylinders from the input feature map data in each round, the H*S first cylinders selected are those obtained by simulating sliding windows in the input feature map data along the W direction according to a preset step size, so that the H*S first cylinders obtained from the input feature map data in each round are not exactly the same, thereby ensuring the reliability and accuracy of the convolution calculation.
[0015] In combination with a possible implementation method of the first aspect, W*R first cylinders are obtained from the input feature map data in each round, including: in each round of simulation, a sliding window is performed in the input feature map data along the H direction according to a preset step size, and W*R first cylinders obtained by sliding the window are selected from the input feature map data.
[0016] In an embodiment of the present application, when obtaining W*R first cylinders from the input feature map data in each round, the W*R first cylinders selected are those obtained by simulating sliding windows in the input feature map data along the H direction according to a preset step size, so that the W*R first cylinders obtained from the input feature map data in each round are not exactly the same, thereby ensuring the reliability and accuracy of the convolution calculation.
[0017] In combination with a possible implementation manner of the embodiment of the first aspect, the method also includes: after acquiring the input feature map data, storing the input feature map data into a first vector register stack, wherein the first vector register stack includes a plurality of vector registers with consecutive numbers, each vector register is used to store Cin data contained in at least one of the first cylinders, or a plurality of vector registers are used to store Cin data contained in one of the first cylinders.
[0018] In an embodiment of the present application, when the input feature map is stored in the first vector register stack, each vector register can store Cin data contained in at least one first cylinder, or multiple vector registers can be used to store Cin data contained in a first cylinder, so that the storage method is very flexible and can be applicable to various scenarios.
[0019] In combination with a possible implementation method of the first aspect, the input feature map data is stored in a first vector register stack, including: according to the numbering order of the first cylinders in the input feature map data, the data corresponding to H*W first cylinders are stored in a plurality of vector registers with consecutive numbers in sequence; wherein, taking the number of the first cylinder located at a specified position in the input feature map data as the starting point, each first cylinder is numbered along the H direction or the W direction according to a specific rule.
[0020] In an embodiment of the present application, when storing the data contained in the first cylinder, the data corresponding to H*W first cylinders are stored in sequence in a plurality of vector registers with consecutive numbers according to the numbering order of the first cylinders in the input feature map data, so as to ensure that the numbers of the first cylinders stored in the vector registers are continuous, so as to facilitate the writing of matrix instructions (the requirement of continuous register numbers is required when matrix instructions read data).
[0021] In combination with a possible implementation manner of the embodiment of the first aspect, the method also includes: after obtaining N convolution kernels, storing the N convolution kernels in a second vector register stack, wherein the second vector register stack includes multiple vector registers with consecutive numbers, each vector register is used to store Cin data contained in at least one second cylinder, or multiple vector registers are used to store Cin data contained in one second cylinder.
[0022] In an embodiment of the present application, when N convolution kernels are stored in a second vector register stack, each vector register can be used to store Cin data contained in at least one second cylinder, or multiple vector registers can be used to store Cin data contained in a second cylinder, so that the storage method is very flexible and can be applicable to various scenarios.
[0023] In combination with a possible implementation of the embodiment of the first aspect, the N convolution kernels are stored in a second vector register stack, including: storing the data corresponding to the N*R*S second cylinders in sequence into a plurality of vector registers with consecutive numbers according to the numbering order of the second cylinders, wherein the numbers of the N second cylinders at the same element position in the N convolution kernels are consecutive.
[0024] In an embodiment of the present application, when N convolution kernels are stored in the second vector register stack, the data corresponding to the N*R*S second cylinders are stored in the corresponding vector registers in sequence according to the numbering order of the second cylinders, and the numbers of the N second cylinders at the same element position in the N convolution kernels are continuous to meet the requirement that the register numbers must be continuous when the matrix instruction reads data.
[0025] In combination with a possible implementation manner of the embodiment of the first aspect, each round obtains H*S first cylinders from the input feature map data, and performs convolution calculations on the H*S first cylinders obtained in the round with corresponding second cylinders in N convolution kernels, including: obtaining H*S first cylinders from the input feature map data according to the matrix instruction stream of each round, and obtaining corresponding second cylinders from the N convolution kernels, and performing convolution calculations on the H*S first cylinders obtained in the round with corresponding second cylinders in the N convolution kernels.
[0026] In the embodiment of the present application, by utilizing programmable matrix instructions, the sliding window operation of the convolution can be flexibly implemented, thereby completing convolution operations of different configurations and types.
[0027] In combination with a possible implementation manner of the embodiment of the first aspect, each round of the matrix instruction stream contains R*S matrix instructions; the i-th matrix instruction is used to select the second cylinder where the i-th element position of each convolution kernel among the N convolution kernels is located, and to select P cylinders corresponding to the second cylinder where the i-th element position is located from the input feature map data along the H direction, and i is 1 to R*S along the R direction.
[0028] In the embodiment of the present application, through the programming combination of matrix instructions, each round of matrix instruction stream only needs to contain R*S matrix instructions, and each round of convolution calculation can be implemented with the minimum number of matrix instructions, truly achieving the purpose of high efficiency, flexibility and programmability (the required instructions can be flexibly edited according to R*S).
[0029] In combination with a possible implementation manner of the embodiment of the first aspect, each round obtains W*R first cylinders from the input feature map data, and performs convolution calculations on the W*R first cylinders obtained in the round with corresponding second cylinders in N convolution kernels, including: obtaining W*R first cylinders from the input feature map data according to the matrix instruction stream of each round, and obtaining corresponding second cylinders from the N convolution kernels, and performing convolution calculations on the W*R first cylinders obtained in the round with corresponding second cylinders in the N convolution kernels.
[0030] In combination with a possible implementation manner of the first aspect, each round of the matrix instruction stream includes R*S matrix instructions; the i-th matrix instruction is used to select the second cylinder where the i-th element position of each convolution kernel among the N convolution kernels is located, and to select Q cylinders corresponding to the second cylinder where the i-th element position is located from the input feature map data along the W direction, and i is 1 to R*S along the S direction.
[0031] In the second aspect, the embodiment of the present application also provides a SOC chip, including: a first vector register stack, a second vector register stack, and a tensor engine; the first vector register stack is used to store input feature map data containing H*W first cylinders, wherein each of the first cylinders contains Cin data of an element position on the H*W dimension along the direction of the input channel, H represents the height of the input feature map data, W represents the width of the input feature map data, and Cin is the number of input channels; the second vector register stack is used to store N convolution kernels containing R*S second cylinders, wherein one of the second cylinders contains Cin data of an element position on the R*S dimension along the direction of the input channel; wherein R represents the height of each convolution kernel, S represents the width of each convolution kernel, and N is a positive integer greater than or equal to 1; the tensor engine is used to obtain H*S first cylinders from the first vector register stack in each round, so as to obtain the first cylinders from the first vector register stack in the first round. and obtaining the corresponding second cylinder from the second vector register stack, and performing convolution calculations on the H*S first cylinders obtained in this round with the corresponding second cylinders in the N convolution kernels, respectively, to obtain a column in the output feature map, and after Q rounds of convolution calculations, obtain an output feature map containing P*Q third cylinders; or, obtaining W*R first cylinders from the first vector register stack in each round, and obtaining the corresponding second cylinders from the second vector register stack, and performing convolution calculations on the W*R first cylinders obtained in this round with the corresponding second cylinders in the N convolution kernels, respectively, to obtain a row in the output feature map, and after P rounds of convolution calculations, obtain an output feature map containing P*Q third cylinders; wherein P represents the height of the output feature map, Q represents the width of the output feature map, and each of the third cylinders contains N elements, and each element is the multiplication and accumulation result of R*S second cylinders and their corresponding first cylinders.
[0032] In combination with a possible implementation manner of the second aspect embodiment, the tensor engine is used to obtain H*S first cylinders from the first vector register stack and the corresponding second cylinders from the second vector register stack according to each round of matrix instruction stream, or to obtain W*R first cylinders from the first vector register stack and the corresponding second cylinders from the second vector register stack according to each round of matrix instruction stream.
[0033] In combination with a possible implementation manner of the second aspect, the first vector register stack includes multiple vector registers, each vector register is used to store Cin data contained in at least one of the first cylinders, or multiple vector registers are used to store Cin data contained in one of the cylinders.
[0034] In combination with a possible implementation manner of the second aspect embodiment, the second vector register stack includes multiple vector registers with consecutive numbers, each vector register is used to store Cin data contained in at least one of the second cylinders, or multiple vector registers are used to store Cin data contained in one of the second cylinders.
[0035] In combination with a possible implementation manner of the second aspect embodiment, the SOC chip also includes an instruction scheduling unit, which is used to send a matrix instruction stream required for convolution calculation to the tensor engine.
[0036] In a third aspect, an embodiment of the present application further provides an electronic device, comprising: a shared memory for storing input feature map data and convolution kernels required for convolution calculations; and a SOC chip as described above, wherein the SOC chip is connected to the shared memory.
[0037] In a fourth aspect, an embodiment of the present application further provides an electronic device, comprising: a memory and a processor, the processor being connected to the memory; the memory being used to store programs; the processor being used to call the programs stored in the memory to execute the method provided in the above-mentioned first aspect embodiment and / or any possible implementation method in combination with the first aspect embodiment.
[0038] In a fifth aspect, an embodiment of the present application further provides a computer-readable storage medium having a computer program stored thereon. When the computer program is executed by a processor, the method provided by the above-mentioned first aspect embodiment and / or any possible implementation method in combination with the first aspect embodiment is executed. BRIEF DESCRIPTION OF THE DRAWINGS
[0039] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can also be obtained based on these drawings without creative work. As shown in the accompanying drawings, the above and other purposes, features and advantages of the present application will be clearer. The same reference numerals indicate the same parts in all the drawings. The drawings are not deliberately scaled to the actual size, and the focus is on showing the main purpose of the present application.
[0040] Figure 1 Schematic diagram of the convolution process for a single-channel convolution operation.
[0041] Figure 2 For the general Figure 1 The schematic diagram of the principle of converting the convolution operation into a matrix operation is shown.
[0042] Figure 3A schematic diagram showing different data expansion positions and different data handling paths for three general matrix multiplication methods.
[0043] Figure 4 A flow chart of a convolution calculation method provided in an embodiment of the present application is shown.
[0044] Figure 5A A schematic diagram showing the principle of the first convolution calculation provided in an embodiment of the present application is shown.
[0045] Figure 5B A schematic diagram showing the principle of the second convolution calculation provided in an embodiment of the present application is shown.
[0046] Fig. 6A Shows Figure 5A Schematic diagram of the principle of the first round of convolution calculation in .
[0047] Figure 6B Shows Figure 5B Schematic diagram of the principle of the first round of convolution calculation in .
[0048] Figure 7 Shows Fig. 6A Schematic diagram of the principle of step ① in .
[0049] Figure 8 A schematic diagram showing the principle of abstracting a two-dimensional matrix into a three-dimensional cylinder for convolution calculation provided by an embodiment of the present application is shown.
[0050] Fig. 9 A schematic diagram showing the principle of a convolution calculation with register numbers provided in an embodiment of the present application is shown.
[0051] Fig.10 Shows the implementation Fig. 9 An example diagram of the matrix instruction pseudocode for convolution calculation in .
[0052] Fig.11 A partial schematic diagram of the principles of a convolution calculation provided in an embodiment of the present application is shown.
[0053] Fig.12 A partial schematic diagram of the principles of another convolution calculation provided in an embodiment of the present application is shown.
[0054] Fig.13 A schematic diagram showing the principle of the third convolution calculation provided in an embodiment of the present application is shown.
[0055] Fig.14 Shows Fig.13 Schematic diagram of the principle of the first round of convolution calculation in .
[0056] Fig.15A schematic diagram of the principle of an extended convolution calculation provided in an embodiment of the present application is shown.
[0057] Fig.16 A schematic diagram showing the principle of the fourth convolution calculation provided in an embodiment of the present application is shown.
[0058] Fig.17 Shows Fig.16 Schematic diagram of the principle of the first round of convolution calculation in .
[0059] Fig.18 A schematic structural diagram of a SOC chip provided in an embodiment of the present application is shown.
[0060] Fig.19 A schematic structural diagram of an electronic device provided in an embodiment of the present application is shown. DETAILED DESCRIPTION
[0061] The technical solutions in the embodiments of the present application will be described below in conjunction with the drawings in the embodiments of the present application.
[0062] It should be noted that similar numbers and letters represent similar items in the following drawings, so once an item is defined in one drawing, it is not necessary to further define and explain it in the subsequent drawings. At the same time, in the description of this application, relational terms such as "first", "second", etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply that there is any such actual relationship or order between these entities or operations. Moreover, the term "include", "comprise" or any other variant thereof is intended to cover non-exclusive inclusion, so that the process, method, article or equipment including a series of elements includes not only those elements, but also includes other elements that are not clearly listed, or also includes elements inherent to such process, method, article or equipment. In the absence of more restrictions, the elements limited by the sentence "including one..." do not exclude the existence of other identical elements in the process, method, article or equipment including the elements.
[0063] Given that the three general matrix multiplication methods currently used in convolution operations (explicit general matrix multiplication, implicit general matrix multiplication, and general matrix multiplication with pre-calculated offsets) all require the input feature map data to be expanded, a lot of duplicate data will be generated, which will increase memory usage and data handling volume, thereby reducing chip performance.
[0064] Among them, the explicit general matrix multiplication will expand the input feature map data to 9 times the original size according to the size of the convolution kernel, such as 3*3, and store it in the global memory. The data expansion amount is proportional to the convolution kernel size, which will generate a lot of duplicate data. Taking the 3*3 convolution kernel as an example, it means that there will be 8 times the duplicate data, which will increase the memory usage and data handling volume. The implicit general matrix multiplication and the general matrix multiplication with pre-calculated offsets do not fundamentally solve the problems existing in the explicit general matrix multiplication. The implicit general matrix multiplication only changes the location of the input feature map data expansion from the global memory to the shared memory, reducing the memory usage in the global memory and the data handling volume from the global to the shared memory, but does not reduce the memory usage of the shared memory and the data handling volume from the shared memory to the vector register stack. The general matrix multiplication with pre-calculated offsets uses the characteristics of the convolution sliding window to index the input feature map data with the offset increment, but does not reduce the data handling volume from the shared memory to the vector register stack. In addition, this method requires additional offsets and may cause shared memory access conflicts.
[0065] Figure 3 The different data expansion locations and different data handling paths of the above three general matrix multiplication methods are compared. For explicit general matrix multiplication, the input feature map data will be expanded and stored in the global memory, then transferred to the shared memory, and then moved from the shared memory to the vector register stack. The difference between implicit general matrix multiplication and explicit general matrix multiplication is that the location where the input feature map data is expanded is changed from global memory to shared memory. For implicit general matrix multiplication, the global memory stores the original input feature map data. For general matrix multiplication with pre-calculated offsets, the global memory also stores the original input feature map data. The input feature map data needs to be indexed by the offset increment at the shared memory to expand the input feature map data. No matter which method is used, it is impossible to send the original input feature map data to the vector register stack without repeatedly expanding the original input feature map data or increasing data handling.
[0066] Based on this, the present application provides a convolution calculation method without expanding the original input feature map data to further improve the convolution calculation efficiency and performance-to-power ratio to support the increasing computing power requirements. The present application can send the original input feature map data to the vector register stack without expanding it, and use programmable matrix instructions to flexibly implement the sliding window operation of the convolution, thereby completing convolution operations of different configurations and types.
[0067] The convolution calculation method provided in this application can be applied to each scene involving convolution calculation, such as applied to a convolutional neural network (CNN) of any structure. For example, it can be applied to a CNN with only one convolution layer, and can also be applied to some complex CNNs, such as a CNN including up to hundreds or more convolution layers. In addition, the CNN in the embodiment of the present application can also have a pooling layer, a fully connected layer, etc. In other words, the scheme of the embodiment of the present application is not limited to a specific convolutional neural network. As long as it is a neural network containing a convolution layer, it can be considered as a "convolutional neural network" in this application, and its convolution layer part can be calculated according to the embodiment of the present application.
[0068] The following will be combined Figure 4 , the convolution calculation method provided in the embodiment of the present application is explained.
[0069] S1: Obtain input feature map data containing H*W first cylinders.
[0070] It can be understood that the input feature map data obtained here containing H*W first cylinders can be part or all of the complete input feature map data. If the complete input feature map data is too large, it needs to be divided into a large data block into multiple small data blocks.
[0071] Among them, H represents the height of the input feature map data, W represents the width of the input feature map data, and a first cylinder contains Cin data of an element position in the H*W dimension along the direction of the input channel, where Cin is the number of input channels.
[0072] S2: Get N convolution kernels containing R*S second cylinders.
[0073] Among them, R represents the height of each convolution kernel, S represents the width of each convolution kernel, a second cylinder contains Cin data of an element position in the R*S dimension along the direction of the input channel, and N is a positive integer greater than or equal to 1.
[0074] It is understandable that in the convolutional neural network model, the number and size of convolution kernels corresponding to different layers are different.
[0075] S3: In each round, H*S first cylinders are obtained from the input feature map data, and convolution calculations are performed on the H*S first cylinders obtained in this round with the corresponding second cylinders in N convolution kernels to obtain a column in the output feature map. After Q rounds of convolution calculations, an output feature map containing P*Q third cylinders is obtained.
[0076] After obtaining the input feature map data and the convolution kernel, convolution calculation can be performed. In each round of convolution calculation, H*S first cylinders are obtained from the input feature map data, and the H*S first cylinders obtained in this round are convolved with the corresponding second cylinders in the N convolution kernels to obtain a column in the output feature map (a column includes P third cylinders). After Q rounds of convolution calculation, an output feature map containing P*Q third cylinders is obtained.
[0077] It is understandable that, in each round of convolution calculation, a row in the output feature map is obtained (a row includes Q third cylinders), and after P rounds of convolution calculation, an output feature map containing P*Q third cylinders is obtained. At this time, S3 can be replaced by: in each round, W*R first cylinders are obtained from the input feature map data, and the W*R first cylinders obtained in this round are convolved with the corresponding second cylinders in the N convolution kernels to obtain a row in the output feature map, and after P rounds of convolution calculation, an output feature map containing P*Q third cylinders is obtained.
[0078] Where P represents the height of the output feature map, Q represents the width of the output feature map, and each third cylinder contains N elements, each of which is the product and accumulation result of R*S second cylinders and their corresponding first cylinders. P = (HR) / stride+1, Q = (WS) / stride+1, stride represents the step size.
[0079] It is understandable that when the quotient of (HR) / stride and (WS) / stride is not an integer, the input feature map data needs to be re-divided to ensure that when H and W of the re-divided input feature map data are substituted into the above expression, the quotient of (HR) / stride and (WS) / stride is an integer. Alternatively, the input feature map data needs to be padded and the size of the input feature map data needs to be expanded so that the quotient of (HR) / stride and (WS) / stride is an integer. For example, assuming H*W=6*6, R*S=3*3, stride=2, at this time, the quotient of (HR) / stride and (WS) / stride is not an integer, then the input feature map data needs to be padded or re-divided to expand its size from 6*6 to 9*9, at this time, P*Q=4*4.
[0080] Among them, when each round obtains H*S first cylinders from the input feature map data, each round simulates sliding a window in the input feature map data along the W direction according to a preset step size, and selects the H*S first cylinders obtained by sliding the window from the input feature map data. Therefore, the H*S first cylinders obtained from the input feature map data in each round are not exactly the same. Alternatively, when each round obtains W*R first cylinders from the input feature map data, each round simulates sliding a window in the input feature map data along the H direction according to a preset step size, and selects the W*R first cylinders obtained by sliding the window from the input feature map data. Therefore, the W*R first cylinders obtained from the input feature map data in each round are not exactly the same.
[0081] For better understanding, R*S=3*3, H*W=6*6, stride=1 is taken as an example below, and the corresponding P*Q=4*4. It can be understood that R*S, H*W and stride are not limited to this.
[0082] The principle of the above convolution calculation is as follows Figure 5A and 5B As shown, a total of 4 rounds of convolution operations are required, and each round of convolution calculation obtains a column or a row in the output feature map. Each column contains P third cylinders composed of N (the number of output channels Cout) elements, where N = Cout. Each row contains Q third cylinders composed of N (the number of output channels Cout) elements. Figure 5A In each round, H*S first cylinders are obtained from the input feature map data and convolved with the corresponding second cylinders in N convolution kernels. Figure 5B Each round in is to obtain W*R first cylinders from the input feature map data, and perform convolution calculations with the corresponding second cylinders in the N convolution kernels. It can be understood that Figure 5A In the schematic diagram shown, the sliding window is used from the right to the left as an example, which can also be replaced by sliding the window from the left to the right. If the sliding window is used from the left to the right, the order is the same as Figure 5A The order shown is exactly the opposite, i.e. Figure 5A The fourth round in is the first round. Figure 5A The third round in is the second round. Figure 5A The second round in is the third round. Figure 5A The first round in is the fourth round. Figure 5B In the schematic diagram shown, the sliding window is illustrated from the bottom to the top, which can also be replaced by sliding the window from the top to the bottom. If the sliding window is performed from the top to the bottom, the order is the same as Figure 5B The order shown is exactly the opposite, i.e. Figure 5B The fourth round in is the first round. Figure 5BThe third round in is the second round. Figure 5B The second round in is the third round. Figure 5B The first round in the game is the fourth round.
[0083] In addition, the order between rounds is not fixed and can be set arbitrarily, including but not limited to, calculating the first round first, then calculating the third round, then calculating the second round, and finally calculating the fourth round. The order between rounds can be set by the matrix instruction stream.
[0084] Among them, the process of performing convolution calculations on the H*S first cylinders obtained in this round with the corresponding second cylinders in the N convolution kernels to obtain a column in the output feature map includes: selecting the second cylinder at the i-th element position of each convolution kernel in the N convolution kernels each time to form a right matrix of Cin*N, i is sequentially 1 to R*S along the R direction; selecting P cylinders corresponding to the second cylinder at the i-th element position from the input feature map data along the H direction each time to form a left matrix of P*Cin, multiplying the left matrix each time with the corresponding right matrix to obtain the product result, and accumulating the R*S product results according to the corresponding element positions to obtain a column in the output feature map. That is, each round of convolution calculation includes R*S matrix operations and R*S-1 accumulation operations.
[0085] Among them, each round obtains W*R first cylinders from the input feature map data, and convolves the W*R first cylinders obtained in this round with the corresponding second cylinders in the N convolution kernels to obtain a row in the output feature map. The process includes: each time, the second cylinder at the i-th element position of each convolution kernel in the N convolution kernels is selected to form a left matrix of N*Cin, and i is 1 to R*S along the S direction; each time, Q cylinders corresponding to the second cylinder at the i-th element position are selected from the input feature map data along the W direction to form a right matrix of Cin*Q; each time, the left matrix is multiplied with the corresponding right matrix to obtain a product result; the R*S product results are accumulated according to the corresponding element positions to obtain a row in the output feature map.
[0086] For a better understanding, let's take R*S=3*3, H*W=6*6, and stride=1 as an example. Figure 5A Each round shown consists of 9 small steps, the principle is as follows Fig. 6A As shown, Fig. 6A M=P in Fig. 6AIn the 9 steps shown in , each time the second cylinder where the same element of each convolution kernel is located is selected to form the right matrix of Cin*N. For example, for ①, the second cylinder where the lower right corner of each convolution kernel is located is selected to form the right matrix of Cin*N. Assuming that the lower right corner is the origin of the coordinate system, the second cylinder where the lower right corner of each convolution kernel is located is the second cylinder where the 0th row and 0th column of each convolution kernel are located. For ②, the second cylinder where the 1st row and 0th column of each convolution kernel are located is selected (if the upper left corner is the origin of the coordinate system, the second cylinder where the 1st row and 2nd column of each convolution kernel are selected) to form the right matrix of Cin*N... For ⑨, the second cylinder where the upper left corner of each convolution kernel is located is selected to form the right matrix of Cin*N.
[0087] Figure 5B Each round in Figure 5A Each round in is similar, and also contains R*S steps. The following takes R*S=3*3, H*W=6*6, and stride=1 as an example. Figure 5B Each round shown consists of 9 small steps, the principle is as follows Figure 6B shown.
[0088] It is understandable that each step in each round of convolution (such as Fig. 6A or Figure 6B The order between ①~⑨) is not fixed and can be set arbitrarily. Fig. 6A or Figure 6B The 9 steps shown in the figure are taken as an example. The order of these 9 steps can be set arbitrarily and is not limited to Fig. 6A or Figure 6B The order of steps ① to ⑨ is shown in the figure. The order of each step in each round of convolution can be set by the execution order of each matrix instruction in the matrix instruction stream.
[0089] Based on the above description, and Figure 5A , Figure 5B , Fig. 6A as well as Figure 6B From the principle shown in the figure, it can be seen that when performing convolution calculation, each round of convolution calculation obtains a row in the output feature map or a column in the output feature map. The principles of the two are very similar, and the only difference is the selected data. In order to avoid redundancy, in the subsequent description, the main explanation is that each round of convolution calculation obtains a column in the output feature map.
[0090] for Fig. 6A The calculation process of ① in the above is as follows: Figure 7As shown, the second cylinder at the same element position (such as the bottom right corner) of each convolution kernel is selected to form a right matrix of Cin*N. M (M=P) first cylinders are selected from the input feature map data to form a left matrix of M*Cin. Multiplying a first cylinder of the left matrix with each second cylinder in the right matrix will result in a product containing Cout (Cout=N) slices (one slice represents one data). Similarly, for Fig. 6A In ②~⑨, multiplying a first cylinder of the left matrix with each second cylinder of the right matrix will also obtain a product result containing N slices, and the 9 product results corresponding to the same element position in the output feature map (each product result contains N slices corresponding to the element position) are accumulated according to the corresponding element position to obtain the third cylinder where the element position is located. For example, for Fig. 6A ①~⑨ in each left matrix, the first cylinder ( Fig. 6A The top black cylinder in the input feature map data shown in each step in ) is multiplied by each cylinder in the corresponding right matrix, and then the 9 product results are accumulated according to the corresponding element position to obtain Figure 7 The third cylinder at the upper right corner of the output feature map shown.
[0091] For a better understanding, assume that N = 4. Figure 7 At the upper right corner of the output feature graph shown in the figure, assuming that the 4 slices obtained by the i-th product are Ai, Bi, Ci, Di, then the result of accumulating the 4 slices obtained by each of the 9 products according to the corresponding element positions is: A1+A2+A3+…+A9, B1+B2+B3+…+B9, C1+C2+C3+…+C9, D 1+D2+D3+…+D9, thus obtaining Figure 7 The third cylinder at the upper right corner of the output feature map shown.
[0092] In the embodiment of the present application, by abstracting a row or a column in a two-dimensional matrix into a three-dimensional cylinder (array), it is possible to implement the required convolution calculation without expanding the input feature map data during the convolution calculation. Figure 8 The principle is explained. Figure 8In the figure, M*Cin represents the left matrix, and Cin*N represents the right matrix. A row of the two-dimensional left matrix (containing Cin slices, one slice represents one data) is abstracted as a first cylinder, and a column of the two-dimensional right matrix (containing Cin slices, one slice represents one data) is abstracted as a second cylinder. When multiplying, multiply a first cylinder of each left matrix with each second cylinder in the right matrix and add them together to generate new N slices, and store the N slices in a row in a result cylinder to obtain a third cylinder. Repeating the calculation of the M first cylinders in the left matrix can obtain M third cylinders with a depth of N slices.
[0093] By matching with programmable matrix instructions, the sliding window operation of convolution can be flexibly implemented, thereby completing convolution operations of different configurations and types. In order to facilitate matching with programmable matrix instructions, after obtaining the input feature map data containing H*W first cylinders, the convolution calculation method of the present application also includes: storing the input feature map data in a first vector register stack, the first vector register stack contains a plurality of vector registers with consecutive numbers, each vector register is used to store Cin data contained in at least one first cylinder, or a plurality of vector registers are used to store Cin data contained in a first cylinder. Accordingly, each round of obtaining H*S first cylinders from the input feature map data is changed to: each round of obtaining H*S first cylinders from the first vector register stack.
[0094] It can be understood that when each vector register is used to store Cin data contained in a first cylinder, the first vector register stack contains H*W vector registers with consecutive numbers. When a vector register is used to store Cin data contained in multiple first cylinders, the number of vector registers contained in the first vector register stack is less than H*W; when multiple vector registers are used to store Cin data contained in a first cylinder, the number of vector registers contained in the first vector register stack is greater than H*W.
[0095] Similarly, after obtaining N convolution kernels containing R*S second cylinders, the convolution calculation method further includes: storing the N convolution kernels in a second vector register stack, the second vector register stack containing a plurality of vector registers with consecutive numbers, each vector register being used to store Cin data contained in at least one second cylinder, or a plurality of vector registers being used to store Cin data contained in one second cylinder. Accordingly, the convolution calculation of the H*S first cylinders obtained in this round with the corresponding second cylinders in the N convolution kernels is changed to: the convolution calculation of the H*S first cylinders obtained in this round with the corresponding second cylinders obtained from the second vector register stack.
[0096] It can be understood that when each vector register is used to store Cin data contained in a second cylinder, the second vector register stack contains N*R*S vector registers with consecutive numbers. When one vector register is used to store Cin data contained in multiple second cylinders, the number of vector registers contained in the second vector register stack is less than N*R*S; when multiple vector registers are used to store Cin data contained in a second cylinder, the number of vector registers contained in the second vector register stack is greater than N*R*S.
[0097] By matching the matrix instruction stream, the above convolution calculation can be realized. For example, according to each round of matrix instruction stream, H*S (or W*R) first cylinders are obtained from the first vector register stack and the corresponding second cylinders are obtained from the second vector register stack. After that, the H*S (or W*R) first cylinders obtained in this round are convolved with the corresponding second cylinders obtained from the second vector register stack to obtain a column (or row) in the output feature map. Each round of matrix instruction stream contains R*S matrix instructions, the i-th matrix instruction is used to select the second cylinder where the i-th element position of each convolution kernel in the N convolution kernels is located, and select P (or Q) cylinders corresponding to the second cylinder where the i-th element position is located from the input feature map data along the H (or W) direction, and i is 1 to R*S along the R direction (or along the S direction).
[0098] The entire convolution process can be expressed in the form of matrix instructions, which is truly flexible, efficient, and programmable to adapt to different convolution operations. For example, Fig. 9 The Vx on the cylinder in the figure represents the corresponding vector register number to represent and Fig.10 The correspondence between the instruction pseudocodes in . The input feature map uses V0~V35, the convolution kernel uses V36~V71, and the output feature map uses V72~V87. It can be understood that in Fig. 9 In the schematic diagram shown, the number of convolution kernels is N=4, but its value is not limited to this. Fig. 9 In the schematic diagram shown, one cylinder corresponds to one vector register.
[0099] Understandably, Fig. 9 In the schematic diagram shown, the first cylinder is numbered continuously along the H direction, and the second cylinder is numbered continuously along the R direction. If in each round of convolution calculation, to get a row in the output feature map, you only need to Fig. 9The numbering sequence of the first cylinder in the figure is replaced with continuous numbering along the W direction, and the second cylinder is numbered continuously along the S direction. For example, starting from the lower right corner, numbering along the W direction, at this time, V6 is replaced by V1, and V30 is replaced by V5, that is, the numbering in the W direction and the numbering in the H direction are interchanged.
[0100] Fig.10 The corresponding instruction pseudocode is shown. gemm is a general matrix instruction. Each matrix instruction has four operands. dest is the result matrix register, srca is the left matrix register, srcb is the right matrix register, srcc is the accumulation matrix register, 4vrf represents 4 consecutive registers. The numbers in 4vrf can be modified according to the number of convolution kernels N, such as 5vrf represents 5 consecutive registers. mem_load is a transfer instruction from memory to register stack, ml_mov is a write register instruction, and writing #0 is to initialize the register value to 0. IAxy represents the first cylinder where the xth row and yth column of the input feature map data are located, Wx represents the second cylinder where the xth element position in the convolution kernel is located, and OAxy represents the third cylinder where the xth row and yth column of the output feature map are located.
[0101] For example, for calculation Fig. 9 To output a column in the feature map (such as V72 to V75), that is, OA00 to OA30, only 9 matrix instructions are needed, and each matrix instruction corresponds to Fig. 6A For example, the instruction "Gemm v72, v0, v36, v72 / / IA00*W0" corresponds to Fig. 6A ① in the.
[0102] For the instruction "Gemm v72, v0, v36, v72", the meaning is: v72 means that the 4 consecutive registers starting from v72 (i.e. v72 to v75) are the result matrix registers, v0 means that the 4 consecutive registers starting from v0 (i.e. v0 to v3) are the left matrix registers, and v36 means that the 4 consecutive registers starting from v36 (i.e. v36 to v39) are the right matrix registers. It should be noted that: " / / " is a comment symbol, and "IA00*W0" is a comment for the instruction "Gemm v72, v0, v36, v72".
[0103] by Figure 5ATaking the size shown as an example, the entire convolution calculation process requires 4 rounds, each round of calculation obtains a column of the third cylinder in the output feature map, and 4 rounds of calculation complete the 4 columns of the third cylinder; each round requires R*S=9 matrix instructions, corresponding to each second cylinder in the convolution kernel, a total of 36 matrix instructions. The results of the first round are accumulated in V72~V75, the results of the second round are accumulated in V76~V79, the results of the third round are accumulated in V80~V83, and the results of the fourth round are accumulated in V84~V87.
[0104] Fig.10 OA00~OA30 in Figure 5A The first round of Fig.10 OA01~OA31 in Figure 5A In the second round, Fig.10 OA03~OA33 in Figure 5A The fourth round. By adjusting Fig.10 The execution order of the matrix instruction stream (OA00~OA30, OA01~OA31, OA03~OA33) in the Figure 5A The order of each round of convolution in .
[0105] Understandably, Fig.10 The pseudo code shown is for Fig. 9 For the convolution calculation shown in the figure, when the convolution calculation is performed, if one of the parameters such as the size of the convolution kernel, the size of the input feature map, and the step size changes, the corresponding instructions also need to change accordingly to adapt to different convolution calculations.
[0106] The sliding window operation of the convolution is implemented by matching programmable general instructions to adapt to various convolution settings and convolution types. The programming method is not limited to compiler-generated assembly, hand-written assembly, inline function calls, register-controlled scheduling, or scheduling solidified into hardware behavior.
[0107] The convolution calculation method shown in this application can perform convolution operations without expanding the data in various memories (such as global memory, shared memory, and vector register stack), which greatly reduces memory usage and data handling. Fig. 9 The example shown in the figure shows that the entire convolution process, from global memory to shared memory to vector register stack, has no data duplication (V0 to V36 are all original data without duplication), saving storage space and data handling. Through the programming combination of matrix instructions (9 matrix instructions in each round), the sliding window operation of the convolution is completed, achieving the purpose of high efficiency, flexibility and programmability.
[0108] In one implementation, when writing the data contained in the H*W first cylinders into the first vector register stack, the H*W first cylinders in the input feature map data can be numbered in a specified order. For example, the number of the first cylinder located at a specified position (such as the lower right corner or the upper left corner) of the input feature map data is used as the starting number (such as v0), and each first cylinder is numbered along the H or W direction according to a specific rule. Afterwards, the H*W first cylinders are sequentially stored in consecutively numbered vector registers in the order of the first cylinder numbers. For example, the first cylinder where v0 is located is stored in the vector register numbered v0, the first cylinder where v1 is located is stored in the vector register numbered v1... and the first cylinder where v35 is located is stored in the vector register numbered v35.
[0109] Among them, if a column in the output feature map is obtained in each round of convolution calculation, when the H*W first cylinders in the input feature map data are numbered in a specified order, the number of the first cylinder located at the specified position of the input feature map data (such as the lower right corner or the upper left corner) is used as the starting number (for example, v0), and each first cylinder is numbered along the H direction according to a specific rule. If a row in the output feature map is obtained in each round of convolution calculation, when the H*W first cylinders in the input feature map data are numbered in a specified order, the number of the first cylinder located at the specified position of the input feature map data (such as the lower right corner or the upper left corner) is used as the starting number (for example, v0), and each first cylinder is numbered along the W direction according to a specific rule.
[0110] It can be understood that the result of numbering the first cylinder in the input feature map data is different if the step size is different. For example, if the step size is 1, the number of the first cylinder located at the lower right corner or the upper left corner of the input feature map data is taken as the starting point, and each first cylinder is numbered successively along the H direction. The principle diagram is as follows: Fig. 9 shown.
[0111] If the step size is 2, the number of the first cylinder located at the lower right corner or the upper left corner of the input feature map data is used as the starting point, and the first cylinders whose element position / step remainder is 1 are numbered continuously along the H (or W) direction, and then the first cylinders whose element position / step remainder is 0 are numbered continuously, that is, the first cylinders at odd positions are numbered continuously along the H (or W) direction, and then the first cylinders at even positions are numbered continuously. For example, the first cylinders at positions 1, 3, 5, 7, 9...35 are numbered continuously to obtain v0~v15, and the first cylinders at positions 2, 4, 6, 8, 10...36 are numbered continuously to obtain v16~v35. In order to store, the first cylinders at odd positions are stored continuously in register stacks with consecutive numbers, and the first cylinders at even positions are stored continuously in register stacks with consecutive numbers.
[0112] If the step size is 3, the number of the first cylinder located at the lower right corner or the upper left corner of the input feature map data is used as the starting point, and the first cylinders whose element position / step remainder is 1 are numbered continuously along the H (or W) direction, and then the first cylinders whose element position / step remainder is 2 are numbered continuously, and finally the first cylinders whose element position / step remainder is 0 are numbered continuously. For example, the first cylinders located at the 1st, 4th, 7th, 10th, 13th, ... 34th element positions are numbered continuously to obtain v0 to v11, the first cylinders located at the 2nd, 5th, 8th, 11th, 14th, ... 35th element positions are numbered continuously to obtain v12 to v23, and the first cylinders located at the 3rd, 6th, 9th, 12th, 15th, ... 36th element positions are numbered continuously to obtain v24 to v35.
[0113] If the step size is 4, the number of the first cylinder located at the lower right corner or the upper left corner of the input feature map data is taken as the starting point, and the first cylinders whose element position / step remainder is 1 are numbered continuously along the H (or W) direction, and then the first cylinders whose element position / step remainder is 2 are numbered continuously, and then the first cylinders whose element position / step remainder is 3 are numbered continuously, and finally the first cylinders whose element position / step remainder is 0 are numbered continuously. For example, the first cylinders located at the element positions 1, 5, 9, 13...33 are numbered continuously to obtain v0~v8, the first cylinders located at the element positions 2, 6, 10, 14...34 are numbered continuously to obtain v9~v17, the first cylinders located at the element positions 3, 7, 11, 15...35 are numbered continuously to obtain v18~v26, and the first cylinders located at the element positions 4, 8, 12, 16...36 are numbered continuously to obtain v27~v35.
[0114] The numbering of the remaining steps is not given as an example, and they are all numbered according to the above rules.
[0115] In one implementation, when writing the data contained in the N*R*S second cylinders into the second vector register stack, the R*S second cylinders in the N convolution kernels may be numbered in a specified order (e.g., along the S or R direction), and then the data contained in the second cylinders may be written into the corresponding vector registers in sequence according to the numbering order of the second cylinders. For example, the second cylinders at the same element position in the N convolution kernels may be numbered sequentially, as shown in FIG. Fig. 9 Schematic diagram shown. Fig. 9 In the schematic diagram of FIG. 1 , the second cylinder is numbered along the R direction.
[0116] It is understandable that the convolution calculation method shown in this application can be applied to convolution kernels of different sizes. When the size of the convolution kernel changes from 3x3 to 4x4, it is only necessary to change the input feature map from 6x6 to 7x7, and the same 4x4 output feature map can still be obtained using the same matrix instruction. The entire convolution process still requires 4 rounds to complete, and the matrix instructions for each round become R*S=16, requiring a total of 64 matrix instructions to complete. The principle is as follows: Fig.11 As shown. It is understandable that Fig.11 This is a schematic diagram of the local principle of convolution calculation. It is only for explanation. When the size of the convolution kernel changes from 3x3 to 4x4, if you still want to get a 4x4 output feature map, you need to change the input feature map from 6x6 to 7x7.
[0117] Of course, the input feature map can also be kept at 6x6. In this case, the size of the matrix instruction M needs to be changed to 3. When the convolution kernel is 4x4, a 3x3 input feature map is obtained. The entire convolution process requires 3 rounds of matrix instructions. The matrix instructions for each round are R*S=16, and a total of 48 matrix instructions are required to complete. The schematic diagram is as follows Fig.12 As shown. It is understandable that Fig.12 This is a schematic diagram of the local principle of convolution calculation. It is only for explanation. When the size of the convolution kernel changes from 3x3 to 4x4, if the input feature map remains unchanged at 6x6, the output feature map will change from 4x4 to 3x3.
[0118] The convolution calculation method shown in this application can be applied to convolution calculations of various different step lengths, not only to step
[0119] The ordinary convolution with a stride of 1 is also applicable to the convolution with a stride of 2. It only needs to store the first cylinders at odd positions in consecutive registers, and the first cylinders at even positions in consecutive registers.
[0120] The convolution calculation can be performed using the previous method with a step size of 1. The principle diagram is as follows Fig.13 As shown, Fig.13In the example shown, H*W=9*9, R*S=3*3, stride=2, and accordingly, P*Q=4*4. Fig.13 In the figure, the white first cylinder in the input feature map data represents the first cylinder located at an odd position, and the gray first cylinder in the input feature map data represents the first cylinder located at an even position.
[0121] 0 If the step size is 2, to complete the entire convolution calculation, Q rounds need to be repeated. Since Q = 4, the entire convolution
[0122] The process still requires 4 rounds to complete. In each round, H*S first cylinders are selected from the input feature map data and multiplied with the corresponding second cylinders in the N convolution kernels to obtain a column in the output feature map. The difference is that the left step size in each round is now 2.
[0123] Each round still contains 9 (R*S=9) steps. Fig.13 The first round in the Fig.14 As shown, the difference from the step size of 1 is that the offset step size of each convolution kernel is 2. However, since the odd and even cylinders have been
[0124] They are stored separately, so it does not affect the requirement that the register numbers are continuous when the matrix instructions read data.
[0125] For the convolution with a stride of 3, we only need to store the first cylinders with the remainder of the element position / step length of 1 in consecutively numbered register stacks, store the first cylinders with the remainder of the element position / step length of 2 in consecutively numbered register stacks, and store the first cylinders with the remainder of the element position / step length of 0 in consecutively numbered register stacks. The convolution calculation can be performed using the previous method with a step length of 1, which will not be given as an example here.
[0126] This invention is not only applicable to ordinary convolution, but also to any type of convolution with sliding window behavior, for example, it can be applicable to dilated convolution, separable convolution, grouped convolution, 3D convolution, etc.
[0127] The following example applies to dilated convolution. The purpose of dilated convolution is to expand the receptive field without increasing the convolution kernel and the amount of convolution calculation. If the size of the input feature map remains unchanged, the size of the corresponding output feature map
[0128] will become smaller, such as Fig.15As shown in the example, the coefficient of the dilated convolution (dilate) is 1. For ordinary convolution, the input feature map data corresponding to the convolution kernel is 11, 12, 13, 21, 22, 23, 31, 32, 33. Fig.15 For the extended convolution shown in the figure, the input feature map data corresponding to the convolution kernel is 11, 13, 15, 31, 33, 35, 51, 53,
[0129] 55. It can be seen that the input feature map data corresponding to the convolution kernel is not continuous, but will cross a data 0 (the coefficient of the extended convolution is 1) to take data.
[0130] For the extended convolution with a step size of 1 and a coefficient of 1, it can still be calculated using the ordinary convolution with a step size of 1. The difference is that the starting address of each round of convolution calculation is not a continuous cylinder, but will span a cylinder. The principle diagram is as follows Fig.16 As shown in the example, H*W=8*8, R*S=3*3, stride=1, accordingly,
[0131] P*Q=4*4. For the extended convolution, P=(H-R') / stride+1, R'=R+(R-1)*extension factor, Q=(W5-S') / stride+1, S'=S+(S-1)*extension factor.
[0132] For the extended convolution, the whole convolution process still needs 4 (Q=4) rounds to complete. In each round, H*S first cylinders are selected from the input feature map data and multiplied with the corresponding second cylinders in the N convolution kernels to obtain a column in the output feature map. The difference is that the starting address of each round of convolution calculation is not a continuous cylinder, but will cross a cylinder.
[0133] Each round still contains 9 (R*S=9) steps. Fig.16 The first round in the Fig.17 As shown in the figure, the difference from ordinary convolution is that the input feature map data corresponding to the convolution kernel in each step is different, and it needs to be modified to adapt to the input feature map data of the extended convolution.
[0134] In addition to being applicable to different convolution types, the convolution calculation method shown in this application is also applicable to various convolution settings, including but not limited to: batch size N (i.e., the number of convolution kernels involved in the convolution calculation), input feature map size H*W, convolution kernel size R*S, input channel Cin, output channel Cout, border filling amount (padding), step size (Stride) and other convolution-related parameter settings.
[0135] Based on the same inventive concept, the embodiment of the present application also provides a SOC (System on Chip, also known as system-on-chip) chip, such as Fig.18 As shown, the SOC chip includes: a first vector register file, a second vector register file and a tensor engine.
[0136] The SOC chip can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), a graphics processing unit (Graphics Processing Unit), etc.; the general-purpose processor can be a microprocessor. The SOC chip can also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), or other artificial intelligence chips or any other conventional processors.
[0137] The first vector register stack is used to store input feature map data including H*W first cylinders. The second vector register stack is used to store N convolution kernels including R*S second cylinders.
[0138] A tensor engine is used to obtain H*S first cylinders from the first vector register stack and the corresponding second cylinders from the second vector register stack in each round, and convolve the H*S first cylinders obtained in this round with the corresponding second cylinders in N convolution kernels to obtain a column in the output feature map, and after Q rounds of convolution calculations, an output feature map containing P*Q third cylinders is obtained. Alternatively, the tensor engine is used to obtain W*R first cylinders from the first vector register stack and the corresponding second cylinders from the second vector register stack in each round, and convolve the W*R first cylinders obtained in this round with the corresponding second cylinders in N convolution kernels to obtain a row in the output feature map, and after P rounds of convolution calculations, an output feature map containing P*Q third cylinders is obtained.
[0139] The first vector register stack includes a plurality of vector registers, each of which is used to store Cin data contained in at least one first cylinder, or the plurality of vector registers are used to store Cin data contained in a first cylinder. The second vector register stack includes a plurality of vector registers with consecutive numbers, each of which is used to store Cin data contained in at least one second cylinder, or the plurality of vector registers are used to store Cin data contained in a second cylinder.
[0140] Optionally, the tensor engine is used to obtain H*S first cylinders from the first vector register stack and the corresponding second cylinders from the second vector register stack according to each round of matrix instruction stream. Each round of matrix instruction stream contains R*S matrix instructions, the i-th matrix instruction is used to select the second cylinder where the i-th element position of each convolution kernel in the N convolution kernels is located, and select P cylinders corresponding to the second cylinder where the i-th element position is located from the input feature map data along the H direction, and i is 1 to R*S along the R direction.
[0141] Alternatively, a tensor engine is used to obtain W*R first cylinders from the first vector register stack and the corresponding second cylinders from the second vector register stack according to each round of matrix instruction stream, wherein each round of matrix instruction stream contains R*S matrix instructions, the i-th matrix instruction is used to select the second cylinder where the i-th element position of each convolution kernel among the N convolution kernels is located, and to select Q cylinders corresponding to the second cylinder where the i-th element position is located from the input feature map data along the W direction, and i is 1 to R*S along the S direction.
[0142] Optionally, the SOC chip may further include a third vector register stack for storing the convolution operation result, and for storing the output feature map containing P*Q third cylinders. The third vector register stack may include P*Q vector registers with consecutive numbers, each of which is used to store Cout (Cout=N) data contained in a third cylinder.
[0143] It can be understood that the first vector register file, the second vector register file and the third vector register file can be essentially the same large vector register file (which can include H*W+N*R*S+P*Q vector registers). Here, it is divided into three vector register files for description only for the convenience of description. In addition, the example here is to use a vector register storing the data contained in a cylinder as an example for explanation.
[0144] Optionally, the SOC chip further includes an instruction scheduling unit, which is used to send a matrix instruction stream required for convolution calculation to the tensor engine. The tensor engine performs convolution operation according to the matrix instruction stream sent by the instruction scheduling unit.
[0145] The implementation principle and technical effects provided by the SOC chip embodiment are the same as those of the aforementioned method embodiment. For the sake of brief description, for matters not mentioned in the SOC chip embodiment, reference may be made to the corresponding contents in the aforementioned method embodiment.
[0146] Based on the same inventive concept, an embodiment of the present application further provides an electronic device, comprising: a shared memory and the above-mentioned SOC chip. The shared memory is used to store input feature map data and convolution kernels required for convolution calculation. The SOC chip is connected to the shared memory. When performing convolution calculation, the data stored in the shared memory is moved to the vector register stack in the SOC chip.
[0147] The electronic devices include but are not limited to mobile phones, tablets, computers (notebooks, desktop computers, etc.), servers and other electronic products.
[0148] Based on the same inventive concept, Fig.19 As shown, Fig.19 The electronic device 200 provided in the embodiment of the present application is shown in the structural block diagram. The electronic device 200 comprises: a transceiver 210, a memory 220, a communication bus 230 and a processor 240.
[0149] The transceiver 210, the memory 220, and the processor 240 are electrically connected to each other directly or indirectly to achieve data transmission or interaction. For example, these components can be electrically connected to each other through one or more communication buses 230 or signal lines. Among them, the transceiver 210 is used to send and receive data. The memory 220 is used to store computer programs, such as the computer program includes at least one software function module that can be stored in the memory 220 in the form of software or firmware or solidified in the operating system (OS) of the electronic device 200. The processor 240 is used to execute the executable modules stored in the memory 220, for example, the processor 240 is used to obtain input feature map data containing H*W first cylinders, obtain N convolution kernels containing R*S second cylinders, obtain H*S first cylinders from the input feature map data in each round, and convolve the H*S first cylinders obtained in this round with the corresponding second cylinders in the N convolution kernels to obtain a column in the output feature map, and after Q rounds of convolution calculations, obtain an output feature map containing P*Q third cylinders, or, obtain W*R first cylinders from the input feature map data in each round, and convolve the W*R first cylinders obtained in this round with the corresponding second cylinders in the N convolution kernels to obtain a row in the output feature map, and after P rounds of convolution calculations, obtain an output feature map containing P*Q third cylinders.
[0150] Among them, the memory 220 can be, but is not limited to, random access memory (Random Access Memory, RAM), read only memory (Read Only Memory, ROM), programmable read-only memory (Programmable Read-Only Memory, PROM), erasable programmable read-only memory (Erasable Programmable Read-Only Memory, EPROM), electrically erasable read-only memory (Electric Erasable Programmable Read-Only Memory, EEPROM), etc.
[0151] The processor 240 may be an integrated circuit chip with signal processing capabilities. The above-mentioned processor may be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it may also be a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components. The various methods, steps and logic block diagrams disclosed in the embodiments of the present application may be implemented or executed. The general-purpose processor may be a microprocessor or the processor 240 may also be any conventional processor, etc.
[0152] The electronic devices 200 mentioned above include but are not limited to mobile phones, tablets, computers (notebooks, desktop computers, etc.), servers, etc.
[0153] The embodiment of the present application also provides a non-volatile computer-readable storage medium (hereinafter referred to as storage medium), on which a computer program is stored, and when the computer program is run by a computer such as the above-mentioned electronic device 200, the convolution calculation method shown above is executed. The aforementioned computer-readable storage medium includes: a U disk, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, and other media that can store program codes.
[0154] It should be noted that the various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the various embodiments can be referenced to each other.
[0155] In addition, the functional modules in the various embodiments of the present application may be integrated together to form an independent part, or each module may exist separately, or two or more modules may be integrated to form an independent part.
[0156] If the functions are implemented in the form of software function modules and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, or the part of the technical solution, can be embodied in the form of a software product, which is stored in a computer-readable storage medium and includes several instructions for enabling a computer device (which can be a personal computer, a laptop, a server, or an electronic device, etc.) to perform all or part of the steps of the methods described in the various embodiments of the present application.
[0157] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any technician familiar with the technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.
Claims
1. A convolution calculation method, characterized in that: Applied to a SOC chip, the SOC chip includes a tensor engine, a first vector register file and a second vector register file; the method includes: Obtain input feature map data containing H*W first cylinders, and store them into the first vector register stack, where H represents the height of the input feature map data, W represents the width of the input feature map data, and one first cylinder contains Cin data at an element position in the H*W dimension along the direction of the input channel, where Cin is the number of input channels; Obtain N convolution kernels including R*S second cylinders, and store them in the second vector register stack, where R represents the height of each convolution kernel, S represents the width of each convolution kernel, one second cylinder includes Cin data at an element position in the R*S dimension along the direction of the input channel, and N is a positive integer greater than or equal to 1; The tensor engine obtains H*S first cylinders from the input feature map data in each round, and performs convolution calculations on the H*S first cylinders obtained in the round with the corresponding second cylinders in the N convolution kernels to obtain a column in the output feature map, and obtains an output feature map containing P*Q third cylinders after Q rounds of convolution calculations; or, obtains W*R first cylinders from the input feature map data in each round, and performs convolution calculations on the W*R first cylinders obtained in the round with the corresponding second cylinders in the N convolution kernels to obtain a row in the output feature map, and obtains an output feature map containing P*Q third cylinders after P rounds of convolution calculations; Among them, P represents the height of the output feature map, Q represents the width of the output feature map, each of the third cylinders contains N elements, and each element is the multiplication and accumulation result of R*S second cylinders and their corresponding first cylinders.
2. The method according to claim 1, characterized in that In each round, H*S first cylinders are obtained from the input feature map data, and convolution calculations are performed on the H*S first cylinders obtained in this round with the corresponding second cylinders in the N convolution kernels, including: Each time, the second cylinder where the i-th element of each convolution kernel among the N convolution kernels is located is selected to form the right matrix of Cin*N, where i is 1 to R*S along the R direction; Each time, P cylinders corresponding to the second cylinder where the i-th element is located are selected from the input feature map data along the H direction to form a left matrix of P*Cin; Multiplying the left matrix each time with its corresponding right matrix to obtain a product result; The R*S product results are accumulated according to the corresponding element positions to obtain a column in the output feature map.
3. The method according to claim 1, characterized in that In each round, W*R first cylinders are obtained from the input feature map data, and convolution calculations are performed on the W*R first cylinders obtained in the round with the corresponding second cylinders in the N convolution kernels, including: Each time, the second cylinder where the i-th element of each convolution kernel is located is selected to form a left matrix of N*Cin, and i is taken from 1 to R*S along the S direction; Each time, Q cylinders corresponding to the second cylinder where the i-th element is located are selected from the input feature map data along the W direction to form a right matrix of Cin*Q; Multiplying the left matrix each time with its corresponding right matrix to obtain a product result; Accumulate the R*S product results according to the corresponding element positions to obtain a row in the output feature map.
4. The method according to claim 1, characterized in that: Each round obtains H*S first cylinders from the input feature map data, including: In each round of simulation, a sliding window is performed in the input feature map data along the W direction according to a preset step size, and H*S first cylinders obtained by sliding the window are selected from the input feature map data.
5. The method according to claim 1, characterized in that Each round obtains W*R first cylinders from the input feature map data, including: In each round of simulation, a sliding window is performed in the input feature map data along the H direction according to a preset step size, and W*R first cylinders obtained by sliding the window are selected from the input feature map data.
6. The method according to claim 1, characterized in that The method further comprises: After acquiring the input feature map data, the input feature map data is stored in a first vector register stack, wherein the first vector register stack includes a plurality of vector registers with consecutive numbers, each vector register is used to store Cin data included in at least one of the first cylinders, or a plurality of vector registers are used to store Cin data included in one of the first cylinders.
7. The method according to claim 6, characterized in that Storing the input feature map data into a first vector register file, comprising: According to the numbering order of the first cylinders in the input feature map data, the data corresponding to H*W first cylinders are sequentially stored in multiple vector registers with consecutive numbers; wherein, taking the number of the first cylinder located at a specified position in the input feature map data as the starting point, each first cylinder is numbered along the H or W direction according to a specific rule.
8. The method according to claim 1, characterized in that The method further comprises: After obtaining N convolution kernels, the N convolution kernels are stored in a second vector register stack, wherein the second vector register stack includes a plurality of vector registers with consecutive numbers, each vector register is used to store Cin data included in at least one second cylinder, or a plurality of vector registers are used to store Cin data included in one second cylinder.
9. The method according to claim 8, characterized in that Storing the N convolution kernels into a second vector register file includes: According to the numbering order of the second cylinders, the data corresponding to the N*R*S second cylinders are sequentially stored in a plurality of vector registers with consecutive numbers, wherein the numbers of the N second cylinders at the same element position in the N convolution kernels are consecutive.
10. The method according to claim 1, characterized in that In each round, H*S first cylinders are obtained from the input feature map data, and convolution calculation is performed on the H*S first cylinders obtained in this round with the corresponding second cylinders in the N convolution kernels, including: According to the matrix instruction stream of each round, H*S first cylinders are obtained from the input feature map data, and the corresponding second cylinders are obtained from the N convolution kernels, and convolution calculations are performed on the H*S first cylinders obtained in this round with the corresponding second cylinders in the N convolution kernels respectively.
11. The method according to claim 10, characterized in that Each round of matrix instruction stream contains R*S matrix instructions; the i-th matrix instruction is used to select the second cylinder where the i-th element position of each convolution kernel in the N convolution kernels is located, and to select P cylinders corresponding to the second cylinder where the i-th element position is located from the input feature map data along the H direction, and i is 1 to R*S along the R direction.
12. The method according to claim 1, characterized in that In each round, W*R first cylinders are obtained from the input feature map data, and convolution calculation is performed on the W*R first cylinders obtained in this round with the corresponding second cylinders in the N convolution kernels, including: According to the matrix instruction stream of each round, W*R first cylinders are obtained from the input feature map data, and the corresponding second cylinders are obtained from the N convolution kernels, and convolution calculations are performed on the W*R first cylinders obtained in this round with the corresponding second cylinders in the N convolution kernels respectively.
13. The method according to claim 12, characterized in that Each round of matrix instruction stream contains R*S matrix instructions; the i-th matrix instruction is used to select the second cylinder where the i-th element position of each convolution kernel in the N convolution kernels is located, and to select Q cylinders corresponding to the second cylinder where the i-th element position is located from the input feature map data along the W direction, and i is 1 to R*S along the S direction.
14. A SOC chip, characterized in that: include: A first vector register stack, used to store input feature map data including H*W first cylinders, wherein each of the first cylinders includes Cin data of an element position in the H*W dimension along the direction of the input channel, H represents the height of the input feature map data, W represents the width of the input feature map data, and Cin is the number of input channels; A second vector register stack, used to store N convolution kernels including R*S second cylinders, wherein one of the second cylinders includes Cin data at an element position in the R*S dimension along the direction where the input channel is located; wherein R represents the height of each of the convolution kernels, S represents the width of each of the convolution kernels, and N is a positive integer greater than or equal to 1; A tensor engine is used to obtain H*S first cylinders from the first vector register stack in each round, and obtain the corresponding second cylinders from the second vector register stack, and perform convolution calculations on the H*S first cylinders obtained in the round with the corresponding second cylinders in N convolution kernels, respectively, to obtain a column in the output feature map, and obtain an output feature map containing P*Q third cylinders after Q rounds of convolution calculations; or, obtain W*R first cylinders from the first vector register stack in each round, and obtain the corresponding second cylinders from the second vector register stack, and perform convolution calculations on the W*R first cylinders obtained in the round with the corresponding second cylinders in N convolution kernels, respectively, to obtain a row in the output feature map, and obtain an output feature map containing P*Q third cylinders after P rounds of convolution calculations; Among them, P represents the height of the output feature map, Q represents the width of the output feature map, each of the third cylinders contains N elements, and each element is the multiplication and accumulation result of R*S second cylinders and their corresponding first cylinders.
15. The SOC chip according to claim 14, characterized in that: The tensor engine is used to obtain H*S first cylinders from the first vector register stack and the corresponding second cylinders from the second vector register stack according to each round of matrix instruction stream, or to obtain W*R first cylinders from the first vector register stack and the corresponding second cylinders from the second vector register stack according to each round of matrix instruction stream.
16. The SOC chip according to claim 14, characterized in that: The first vector register stack includes a plurality of vector registers, each of which is used to store Cin data included in at least one of the first cylinders, or a plurality of vector registers are used to store Cin data included in one of the first cylinders.
17. The SOC chip according to claim 14, characterized in that: The second vector register stack includes a plurality of vector registers with consecutive numbers, each of which is used to store Cin data included in at least one of the second cylinders, or a plurality of vector registers are used to store Cin data included in one of the second cylinders.
18. The SOC chip according to claim 14, characterized in that: The SOC chip also includes an instruction scheduling unit, which is used to send a matrix instruction stream required for convolution calculation to the tensor engine.
19. An electronic device, characterized in that: include: Shared memory, used to store the input feature map data and convolution kernels required for convolution calculation; And the SOC chip as described in any one of claims 14-18, wherein the SOC chip is connected to the shared memory.
20. An electronic device, characterized in that: include: A memory and a processor, wherein the processor is connected to the memory; The memory is used to store programs; The processor is used to call the program stored in the memory to execute the method according to any one of claims 1-13.
21. A computer-readable storage medium, characterized in that: A computer program is stored thereon, and when the computer program is executed by a processor, the method according to any one of claims 1 to 13 is executed.
