Convolution operation circuit, device and method
By designing a convolution operation circuit and using a single instruction and a multiplier and adder to perform convolution operations, the problems of low computing efficiency and high power consumption in the existing technology are solved, and efficient convolution calculations and low-power data processing are achieved. .
Patent Information
- Application Number
- CN201911067252.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2019-11-04
- Publication Date
- 2024-09-13
- Estimated Expiration
- 2039-11-04
AI Technical Summary
Existing technologies have low computational efficiency and high power consumption when performing convolution calculations, and existing chips have storage and communication bottlenecks when processing large-scale data.
A convolution operation circuit is designed, including a convolution operation control circuit and an array of operation units. The convolution operation is performed through a single instruction, and the multiplier and adder are used to calculate the product and accumulation value of the input data and weight data to achieve efficient convolution operation. product operation.
It improves the efficiency of convolution calculations, reduces power consumption, reduces the number of data accesses, and improves the ability to process large-scale data.
Smart Images

Figure CN112784973B
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of neural network computing, and in particular to a convolution operation circuit, device and method. Background Art
[0002] With the development of science and technology, human society is rapidly entering the intelligent era. An important feature of the intelligent era is that people obtain more and more types of data, the amount of data obtained is increasing, and the speed of processing data is getting higher and higher. Chips are the cornerstone of data processing, which fundamentally determines people's ability to process data. From the perspective of application fields, there are two main routes for chips: one is the general chip route, such as CPU (Central Processing Unit), etc., which can provide great flexibility, but the effective computing power is relatively low when processing algorithms in specific fields; the other is the dedicated chip route, such as TPU (Tensor Processing Unit), etc., which can play a higher effective computing power in certain specific fields, but in the face of flexible and changeable more general fields, their processing capabilities are relatively poor or even unable to process. Due to the wide variety and huge amount of data in the intelligent era, chips are required to have both extremely high flexibility, able to process algorithms in different fields and changing with each passing day, and extremely strong processing capabilities, able to quickly process huge and rapidly growing amounts of data.
[0003] Convolutional computing is often required in artificial intelligence computing. There are usually two existing solutions for implementing convolutional computing:
[0004] (1) CPU solution: In this solution, if it is a single-core CPU, the matrix involved in the convolution calculation will be decomposed into scalars for calculation, and the convolution operation will be realized by combining scalar instructions; if it is a multi-core CPU, multiple cores may execute their own scalar instructions in parallel to realize the convolution operation. However, this solution has the following disadvantages: the underlying program is complex, and generally requires multiple layers of loops to realize the convolution operation; the convolution operation is realized by general computing instructions, which is inefficient and requires multiple branch jumps; the CPU cache is limited, and the realization of a relatively large convolution operation requires multiple data transfers from the chip, which affects the efficiency; the CPU needs to access data multiple times, which will increase the calculation time to realize the convolution operation; the CPU needs to access data multiple times, which will increase the computing power consumption to realize the convolution operation; if it is multi-core parallel computing, the communication between cores is complex, and the communication performance may become a bottleneck.
[0005] (2) GPU (Graphics Processing Unit) solution: In this solution, the GPU will decompose the convolution operation into multiple instruction operations, which are mainly vector instructions. The convolution operation is implemented by combining and executing vector instructions. However, this solution has the following disadvantages: the underlying program is complex, and generally requires multiple layers of loops to implement the convolution operation; the convolution operation is implemented by combining vector instructions multiple times, which is inefficient; the GPU needs to access data multiple times, which increases the computing time to implement the convolution operation; the GPU needs to access data multiple times, which increases the computing power consumption to implement the convolution operation; the GPU cache is limited, and implementing relatively large convolution operations requires multiple off-chip data transfers, which affects efficiency. Summary of the invention
[0006] This summary is provided to introduce concepts in a brief form that will be described in detail in the detailed description below. This summary is not intended to identify key features or essential features of the claimed technical solution, nor is it intended to limit the scope of the claimed technical solution.
[0007] In order to solve the technical problems of low computational efficiency and high power consumption in the prior art when performing convolution calculations, the present disclosure proposes the following technical solutions:
[0008] In a first aspect, an embodiment of the present disclosure provides a convolution operation circuit, comprising:
[0009] Convolution operation control circuit;
[0010] A computing unit array, wherein the computing unit array includes a plurality of computing units;
[0011] The convolution operation control circuit is used to receive a convolution operation instruction, and transmit the input data and the weight data to the operation units participating in the operation among the multiple operation units one by one according to the operation order indicated by the convolution operation instruction, and the operation units participating in the operation perform convolution operation operations on the input data and the weight data according to the instructions of the instruction, wherein the instruction is a single instruction.
[0012] Furthermore, the convolution operation instruction includes an instruction name, a starting address of input data, a starting address of weight data, and a starting address of output data.
[0013] Furthermore, the operation unit includes an operator, and the operator includes at least a multiplier and an adder;
[0014] The operation unit is used to combine the multiplier and the adder according to the convolution operation instruction to perform a convolution operation.
[0015] Further, the operation unit involved in the operation performs a convolution operation on the input data and the weight data according to the instruction of the instruction, including:
[0016] Calculating the product of the input data and the weight data by a multiplier;
[0017] Calculating the accumulated value of the product by an adder;
[0018] The accumulated value is outputted.
[0019] Furthermore, the input data is row vector data of the first input matrix; and the weight data is row vector data of the convolution kernel.
[0020] In a second aspect, an embodiment of the present disclosure provides a convolution operation device, including:
[0021] A memory, used to store convolution operation instructions, input data, weight data, and output data;
[0022] An instruction fetch module, connected to the memory, and configured to fetch the convolution operation instruction from the memory;
[0023] A decoding module, connected to the instruction fetch module, for decoding the convolution operation instruction acquired by the instruction fetch module;
[0024] A register, used to store the attribute data of the input data, the attribute data of the weight data and the attribute data of the output data;
[0025] An execution module, connected to the decoding module, the memory and the register, comprises the convolution operation circuit as described above, and is used to execute the decoded convolution operation instruction.
[0026] Further, the execution module obtains the decoded convolution operation instruction from the decoding module;
[0027] The execution module obtains the attribute data of the input data, the attribute data of the weight data and the attribute data of the output data from the register;
[0028] The execution module acquires the input data and the weight data for calculation from the memory according to the attribute data of the input data and the attribute data of the weight data;
[0029] The execution module calculates the input data and the weight data according to the decoded convolution operation instruction to obtain output data;
[0030] The execution module stores the output data in the memory according to the attribute data of the output data.
[0031] Furthermore, the input data is the data of the first input matrix, the weight data is the data of the convolution kernel, and the output data is the data of the output matrix obtained by convolution; the attribute data of the input data include the number of rows, columns, depth and depth storage interval of the first input matrix; the attribute data of the weight data include the number of rows, columns, step size and row storage interval of the convolution kernel; the attribute data of the output data include the number of rows, columns, depth and depth storage interval of the output matrix.
[0032] Further, the execution module obtains the input data and the weight data for calculation from the memory according to the attribute data of the input data and the attribute data of the weight data, including:
[0033] The execution module reads the data of the first input matrix according to a preset first reading method and attribute data of the input data;
[0034] The execution module reads the data of the convolution kernel according to a preset second reading method and attribute data of the weight data.
[0035] Furthermore, the first reading method is reading by row or reading by column; the second reading method is reading by row or reading by column.
[0036] In a third aspect, an embodiment of the present disclosure provides a matrix operation method, which is a matrix operation method based on any convolution operation circuit described in the first aspect, including:
[0037] Fetch the convolution operation instruction from the memory;
[0038] Decoding the convolution operation instruction, and sending the decoded convolution operation instruction to the convolution operation circuit;
[0039] Based on the decoded convolution operation instruction, the convolution operation circuit obtains input data and weight data from the memory and performs operation, and stores the operation result in the memory after the operation is completed.
[0040] In a fourth aspect, an embodiment of the present disclosure provides an electronic device, comprising: a memory for storing computer-readable instructions; and one or more processors for executing the computer-readable instructions, so that when the processor is executed, any convolution operation method described in the third aspect is implemented.
[0041] In a fifth aspect, an embodiment of the present disclosure provides a non-transitory computer-readable storage medium, which stores computer instructions, and the computer instructions are used to enable a computer to execute any convolution operation method described in the third aspect.
[0042] In a sixth aspect, an embodiment of the present disclosure provides a computer program product, comprising computer instructions. When the computer instructions are executed by a computing device, the computing device can perform any convolution operation method described in the aforementioned third aspect.
[0043] In a seventh aspect, an embodiment of the present disclosure provides a chip, comprising any matrix operation circuit described in the first aspect.
[0044] In an eighth aspect, an embodiment of the present disclosure provides a computing device, comprising any chip described in the seventh aspect.
[0045] The disclosed embodiments disclose a convolution operation circuit, device and method. The convolution operation circuit includes: a control circuit; a convolution operation control circuit; an operation unit array, wherein the operation unit array includes multiple operation units; the convolution operation control circuit is used to receive a convolution operation instruction, and transmit the input data and the weight data to the operation units participating in the operation in the multiple operation units one by one according to the operation order indicated by the convolution operation instruction, and the operation units participating in the operation perform convolution operation operations on the input data and the weight data according to the instructions of the instructions, wherein the instruction is a single instruction. Through the above method, the technical problems of low computational efficiency and high power consumption in the prior art when performing convolution calculations are solved.
[0046] The above description is only an overview of the technical solution of the present invention. In order to more clearly understand the technical means of the present invention, it can be implemented in accordance with the contents of the specification. In order to make the above and other purposes, features and advantages of the present invention more obvious and easy to understand, the following preferred embodiments are specifically cited and described in detail with reference to the accompanying drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0047] The above and other features, advantages and aspects of the embodiments of the present disclosure will become more apparent with reference to the following detailed description in conjunction with the accompanying drawings. Throughout the accompanying drawings, the same or similar reference numerals represent the same or similar elements. It should be understood that the drawings are schematic and the originals and elements are not necessarily drawn to scale.
[0048] Figure 1 A schematic diagram of the structure of a matrix operation circuit provided in an embodiment of the present disclosure;
[0049] Figure 2 A schematic diagram of the structure of a computing unit provided in an embodiment of the present disclosure;
[0050] Figure 3 A further structural schematic diagram of a computing unit provided in an embodiment of the present disclosure;
[0051] Figure 4a-4bAn overall schematic diagram of a convolution operation in an embodiment of the present disclosure;
[0052] Figure 5-Figure 9 The decomposition calculation process of the convolution operation provided by the embodiment of the present disclosure;
[0053] Fig.10 It is a schematic diagram of a specific example of convolution operation in the embodiment of the present disclosure.
[0054] Fig.11 A schematic diagram of the structure of a matrix computing device provided in an embodiment of the present disclosure;
[0055] Figure 12a-12e A schematic diagram of the storage format of input data, weight data and output data in the present disclosure;
[0056] Fig.13 A schematic diagram of block calculation of convolution operation in the present disclosure;
[0057] Figure 14a-14f The diagram is a schematic diagram of an example of performing a convolution operation using the convolution operation circuit disclosed in the embodiment of the present disclosure. DETAILED DESCRIPTION
[0058] Embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. Although certain embodiments of the present disclosure are shown in the accompanying drawings, it should be understood that the present disclosure can be implemented in various forms and should not be construed as being limited to the embodiments described herein, which are instead provided for a more thorough and complete understanding of the present disclosure. It should be understood that the drawings and embodiments of the present disclosure are only for exemplary purposes and are not intended to limit the scope of protection of the present disclosure.
[0059] It should be understood that the various steps described in the method embodiments of the present disclosure may be performed in different orders and / or in parallel. In addition, the method embodiments may include additional steps and / or omit the steps shown. The scope of the present disclosure is not limited in this respect.
[0060] The term "including" and its variations used herein are open inclusions, i.e., "including but not limited to". The term "based on" means "based at least in part on". The term "one embodiment" means "at least one embodiment"; the term "another embodiment" means "at least one additional embodiment"; the term "some embodiments" means "at least some embodiments". The relevant definitions of other terms will be given in the following description.
[0061] It should be noted that the concepts such as "first" and "second" mentioned in the present disclosure are only used to distinguish different devices, modules or units, and are not used to limit the order or interdependence of the functions performed by these devices, modules or units.
[0062] It should be noted that the modifications of "one" and "plurality" mentioned in the present disclosure are illustrative rather than restrictive, and those skilled in the art should understand that unless otherwise clearly indicated in the context, it should be understood as "one or more".
[0063] The names of the messages or information exchanged between multiple devices in the embodiments of the present disclosure are only used for illustrative purposes and are not used to limit the scope of these messages or information.
[0064] Figure 1 The structure diagram of the convolution operation circuit provided by the embodiment of the present disclosure is shown in FIG. Figure 1 The convolution operation circuit 100 includes a convolution operation control circuit 101 and an operation unit array 102, wherein the operation unit array includes a plurality of operation units (PUs) 103, and the operation units 103 are as follows: Figure 2 As shown, it includes a first input register (Rin1) 104, a second input register (Rin2) 105 and an output register (Rout) 106, wherein the first input register 104 is used to receive input data, and the second input register 105 is used to receive weight data; the convolution operation control circuit 101 is used to receive a convolution operation instruction, and transmits the input data and the weight data to the operation units 103 participating in the operation one by one in the operation order indicated by the convolution operation instruction. The operation unit 103 participating in the operation performs a convolution operation on the input data and the weight data according to the instruction of the instruction, wherein the instruction is a single instruction; the output register 106 is used to store the operation result of the operation, that is, the output data.
[0065] The operation order indicated by the convolution operation instruction means that during the convolution operation, the convolution operation is divided into multiple steps to be executed, and the order between the steps is the operation order indicated by the convolution operation instruction. The specific operation order can be referred to the description of the instruction below.
[0066] The operation unit participating in the operation refers to at least one operation unit that performs a specific operation among multiple operation units.
[0067] In the present disclosure, the convolution operation instruction includes an instruction name, a first address of input data, a first address of weight data, and a first address of output data. The following table shows an exemplary instruction format:
[0068]
[0069] The instruction name corresponds to the instruction meaning, format and implemented operation. The first address of the input data and the first address of the weight data define the reading addresses of the two source operands of the instruction respectively, and the first address of the output data defines the storage address of the destination operand of the instruction. The convolution instruction includes multiplication and addition operations. It first performs multiplication on the input data and the weight data, and then accumulates the results of the multiplication to obtain the convolution result of the input data and the weight data, that is, the output data. The calculation process of an output data is as follows:
[0070]
[0071] in
[0072] Figure 3 A further structural diagram of the computing unit provided in the embodiment of the present disclosure is shown in FIG. Figure 3 As shown, in addition to the first input register, the second input register and the output register, the operation unit also includes an operator, and the operator includes at least a multiplier 301 and an adder 302, wherein the operation unit is used to combine the multiplier and the adder according to the convolution operation instruction to perform a convolution operation.
[0073] Specifically, the operation unit involved in the operation performs a convolution operation on the input data and the weight data according to the instruction, including: calculating the product of the input data and the weight data by the multiplier; calculating the accumulated value of the product by the adder; and storing the accumulated value in the output register. The input data in the first input register is the data read into the first input register in sequence according to the first address of the input data; the weight data in the second input register is the data read into the second input register in sequence according to the first address of the weight data.
[0074] For example, after the convolution operation control circuit transmits the input data and weight data to the operation unit, the operation unit calculates the product of the input data in the first input register and the weight data in the second input register through the multiplier. As in the above example, in one clock cycle, if the data received in the first input register is a1 and the data received in the second input register is b1, the multiplier in the operation unit calculates the product of a1*b1, and then sends the product of a1*b1 to the adder for accumulation. Since the accumulator has no input in the previous clock cycle at this time, the accumulation result of the accumulator is a1. *b1; then in the next clock cycle, continue the above calculation operation. At this time, the data received in the first input register is a2, and the data received in the second input register is b2. The multiplier in the operation unit calculates the product of a2*b2, and then sends the product of a2*b2 to the adder for accumulation. The input of the accumulator in the previous clock cycle is a1*b1, so a1*b1+a2*b2 is calculated in this clock cycle; continue to do the above operation until a row of input data and a column of weight data are calculated, and the final accumulated value is obtained, and then the accumulated value is stored in the output register. The convolution operation control circuit stores the accumulated value in the output register in the system memory according to the output data first address in the instruction.
[0075] An example convolution operation is as follows:
[0076] Figure 4a-4b FIG. 1 is a schematic diagram of a convolution operation in an embodiment of the present disclosure. Figure 4a This is a schematic diagram of the overall convolution operation process, where the diagram is explained as follows:
[0077] Win: the width of the input feature map;
[0078] Hin: the height of the input feature map;
[0079] Cin: the number of channels of the input feature map, hereinafter referred to as the depth of the input feature map;
[0080] S_Cin: the physical storage interval of the depth of the input feature map;
[0081] Wout: width of the output feature map;
[0082] Hout: the height of the output feature map;
[0083] Cout: the number of channels of the output feature map, hereinafter referred to as the depth of the output feature map;
[0084] S_Cout: the physical storage interval of the depth of the output feature map;
[0085] Kw: width of the convolution kernel;
[0086] Kh: height of the convolution kernel;
[0087] S_kernel: the spacing of the convolution kernel in physical storage;
[0088] N_dilation: dilation value of the convolution kernel;
[0089] S_K: the step size of the convolution slide;
[0090] N_pad_u: padding number of zeros on the top;
[0091] N_pad_d: pad the number of zeros at the bottom;
[0092] N_pad_l: pad the left side with 0;
[0093] N_pad_r: pad the number of zeros by square.
[0094] The feature points on the input feature map constitute the first input matrix; the output feature map is the output matrix, and a feature point on the output feature map is a data on the output matrix. When performing a convolution operation, the convolution kernel slides on the input feature map. Each time it slides, it multiplies and accumulates the corresponding data in the input feature map to extract an output feature point, that is, a data on the output matrix.
[0095] Figure 4b This is a schematic diagram of the calculation of an input feature point with depth. Figure 4b As shown in the figure, the convolution kernel slides on the input feature map. When it stops at a position, it will perform corresponding data multiplication and accumulation with the feature point in the input feature map at that position to obtain the output feature point corresponding to that position. There are Cout convolution kernels in total, and each convolution kernel will perform data multiplication and accumulation with the feature point in the input feature map at the same position to obtain Cout output feature points in the depth direction. The Cout output feature points constitute a feature point with depth on the entire output feature map, and the depth of this point is Cout. The convolution kernel will slide the entire input feature map to obtain the entire output feature map.
[0096] For a convolution kernel at depth l (1 <= l <= Cout), the formula for feature extraction is as follows:
[0097]
[0098] Dout is a point with depth in the output feature map, and its superscript l corresponds to the depth of the output; Din refers to the data corresponding to the convolution kernel in the input feature map, its superscript i corresponds to the depth of the input feature map, j and k correspond to the width and height of the convolution kernel respectively; w is the convolution kernel, its superscript l and i correspond to the depth of the output feature map and the depth of the input feature map respectively, and j and k correspond to the width and height of this convolution kernel respectively.
[0099] For a convolution kernel of size Kh*Kw*Cin, it can be divided into Kh partial convolution kernels of Kw*Cin for partial feature extraction. Each time, 1 / Kh of the entire feature extraction is realized, that is, the feature corresponding to the partial convolution kernel of Kw*Cin. The partial results obtained are: Finally, add these Kh partial results to get the final result
[0100]
[0101] in, It can be divided into Kw steps, each step achieves Then add Kw partial results to get the final result The implementation is to multiply a row of input data matrix (i.e., part of the first input matrix) and a column of weight matrix (i.e., part of the convolution kernel), which is implemented as follows Figure 5 shown.
[0102] The implementation of is also a multiplication of a row of input data matrix and a column of weight matrix, but the number of data in the row and column is Kw times the number of data in the Figure 6 shown.
[0103] And for the convolution of the points to get a complete output feature map, that is The implementation is still the multiplication of a row of input data matrix and a column of weight matrix, but the number of elements in this row and this column is Kh times the number of elements in, which can be realized as Figure 7 shown.
[0104] The number of convolution kernels is Cout, so the depth of the output feature point is Cout. You can multiply a row of the input data matrix with the convolution kernel matrix of the Cout column composed of Cout convolution kernels, that is, the weight matrix, to obtain a feature point with depth. This feature point is a vector, and the length of the vector is the depth Cout of the output feature point. It is implemented as follows Figure 8 shown.
[0105] Since the process of implementing convolution or partial convolution of a neural network is the sliding process of the convolution kernel on the input feature map, it can be seen as a process in which the data of the input feature map changes with sliding while the weight remains unchanged. In this way, the process of implementing convolution of a neural network becomes the sliding of the convolution kernel on the output feature map Wout*Hout times, and the multiplication of the input data matrix of the Wout*Hout row and the weight matrix of the Cout column results in the output data matrix of the Wout*Hout row and the Cout column. Its implementation is as follows Fig. 9 Show.
[0106] The above-mentioned convolution operation only needs to use a single instruction in the present disclosure, that is, the above-mentioned convolution operation instruction can complete the entire convolution process. It only requires that the input data, weights and output data are stored in a predetermined manner, and a single convolution operation circuit is used to realize the entire convolution.
[0107] Specifically, in the convolution operation circuit in the above embodiment: the input data is the row vector data of the first input matrix; the weight data is the row vector data of the convolution kernel. In other words, the data in the first input register and the data in the second input register need to be the row vector data of the matrix, so that the calculated data is the convolution result. Fig.10 The figure shows a schematic diagram of the above convolution calculation, taking the first input matrix as a 3*3*2 matrix, the convolution kernel as two 2*2*2 matrices as an example, the step size is 1, and the output matrix is a 2*2*2 matrix. Fig.10 As shown, when performing convolution, the convolution kernel slides on the first input matrix, and the data of the first input matrix read out is shown in 1001, each row is the data of the first input matrix corresponding to a position of the convolution kernel 1002, including 4 numbers with a depth of 2, a total of 8 data; a column in 1002 is the data read out row by row by a convolution kernel in a depth-first manner, including 4 numbers with a depth of 2, a total of 8 data; a row of data in 1001 is multiplied and accumulated with a column of data in 1002 to obtain a data in 1003, a data in 1003 is one of 2 values in a data with a depth of 2 on the output matrix, and each row of the output data corresponds to a data with a depth of 2 in the output matrix.
[0108] Combination Figure 1-3 The PU array shown briefly explains how the PU array implements convolution calculations.
[0109] like Fig.10As shown in , 1001 is the storage structure of the first input matrix, 1002 is the storage structure of the convolution kernel, 1003 is the storage structure of the output matrix, and 1004 is the data read from 1001 by the control unit according to the convolution operation order. Each row of data represents the data block corresponding to the convolution kernel sliding on the first input matrix. After the operation unit reads out the data, it sends the data to the operation units involved in the operation one by one. For example: Fig.10 The first value 1 of the first row of the first input matrix data 1004 is sent to Rin1 of PU11 and Rin1 of PU12, the first value 3 of the second row of the first input matrix data 1004 is sent to Rin1 of PU21 and Rin1 of PU22, the first value 7 of the third row of the first input matrix data 1004 is sent to Rin1 of PU31 and Rin1 of PU32, and the first value 9 of the fourth row of the first input matrix data 1004 is sent to Rin1 of PU41 and Rin1 of PU42; the convolution kernel 1002 is The first value 0.1 of the first column of convolution kernel 1002 is sent to Rin2 of PU11, Rin2 of PU21, Rin2 of PU31 and Rin2 of PU41, and the first value 0.9 of the second column of convolution kernel 1002 is sent to Rin2 of PU12, Rin2 of PU22, Rin2 of PU32 and Rin2 of PU42; PU11, PU12, PU21, PU22, PU31, PU32, PU41 and PU42 perform data multiplication operations respectively and send the results to the output register for storage; in the next clock cycle, the first The second value 2 of the first row of the input matrix data 1004 is sent to Rin1 of PU11 and Rin1 of PU12, the second value 4 of the second row of the first input matrix data 1004 is sent to Rin1 of PU21 and Rin1 of PU22, the second value 8 of the third row of the first input matrix data 1004 is sent to Rin1 of PU31 and Rin1 of PU32, and the second value 10 of the fourth row of the first input matrix data 1004 is sent to Rin1 of PU41 and Rin1 of PU42; the convolution kernel 1002 is used for the first The second value 0.2 in a column is sent to Rin2 of PU11, Rin2 of PU21, Rin2 of PU31 and Rin2 of PU41, and the second value 1.0 in the second column of convolution kernel 1002 is sent to Rin2 of PU12, Rin2 of PU22, Rin2 of PU32 and Rin2 of PU42. PU11, PU12, PU21, PU22, PU31, PU32, PU41 and PU42 respectively perform the data multiplication operation and send the results to the output register for accumulation with the last saved result.
[0110] By analogy, the multiplication and accumulation result of the convolution is finally obtained.
[0111] Fig.11 Schematic diagram of the structure of the matrix computing device provided by the embodiment of the present disclosure. Fig.11 As shown, the convolution operation device 1100 includes: a memory 1101, which is used to store convolution operation instructions, input data, weight data and output data; an instruction fetch module 1102, which is connected to the memory 1101 and is used to obtain the convolution operation instruction from the memory 1101; a decoding module 1103, which is connected to the instruction fetch module 1102 and is used to decode the convolution operation instruction obtained by the instruction fetch module 1102; a register 1104, which is used to store the attribute data of the input data, the attribute data of the weight data and the attribute data of the output data; an execution module 1105, which is connected to the decoding module 1103, the memory 1101 and the register 1104, includes the convolution operation circuit in the above embodiment, and is used to execute the decoded convolution operation instruction.
[0112] In one embodiment, the execution module obtains the decoded matrix operation instruction from the decoding module; the execution module obtains the attribute data of the input data, the attribute data of the weight data and the attribute data of the output data from the register; the execution module obtains the input data and the weight data for calculation from the memory according to the attribute data of the input data and the attribute data of the weight data; the execution module calculates the input data and the weight data according to the decoded convolution operation instruction to obtain the output data; the execution module stores the output data in the memory according to the attribute data of the output data. Wherein, the input data is the data of the first input matrix, the weight data is the data of the convolution kernel, and the output data is the data of the output matrix obtained by convolution; the attribute data of the input data includes the number of rows, columns, depth and depth storage interval of the first input matrix; the attribute data of the weight data includes the number of rows, columns, step size and storage interval between the convolution kernels; the attribute data of the output data includes the number of rows, columns, depth and depth storage interval of the output matrix. Among them, the number of rows and columns defines the size of the matrix, the input data storage interval defines the storage address difference of the input data in depth, the output data storage interval defines the storage address difference of the output data in depth, and the storage interval of the convolution kernel defines the storage address difference between two convolution kernels. For example, the first input matrix has 10 int8 matrix elements in the depth direction. If it is stored continuously, then the row storage interval is 10Byte. If it is stored at a certain interval, such as 20Byte, then 10Byte is the matrix element, and the other 10Byte does not belong to this matrix, which may be invalid data or data for other purposes.
[0113] Optionally, the execution module obtains the input data and the weight data for calculation from the memory according to the attribute data of the input data and the attribute data of the weight data, including: the execution module reads the data of the first input matrix according to a preset first reading method and the attribute data of the input data; the execution module reads the data of the convolution kernel according to a preset second reading method and the attribute data of the weight data. Optionally, the first reading method is reading by row or by column; the second reading method is reading by row or by column.
[0114] For example, if the attribute data defines that the number of rows of the first input matrix is 5, the number of columns is 5, the depth is 2, the depth storage interval is 4, and the preset reading method is to read by row, then the first row of the first input matrix is read according to the starting address, number of columns, depth and depth storage interval of the first input matrix in the instruction. From the number of columns, it can be known that the first row has 5 matrix elements, and the depth of each matrix element is 2. Then, after reading 2*5=10 data, a row of data of the first input matrix is obtained, and then the starting address plus 5 depth storage intervals are used as the new starting address to read the second row, which also has 5 matrix elements. After reading 5 times in sequence, the execution module can obtain all the data of the first input matrix.
[0115] Similarly, the data of the convolution kernel is obtained in the same way. Optionally, the execution module stores the data of the output matrix in the memory according to the attribute data of the output matrix, including: the execution module stores the data of the output matrix in the memory according to a preset storage method and the attribute data of the output matrix. Among them, the predetermined storage method is row storage or column storage, and the specific storage method is similar to reading, but the direction is opposite, which will not be repeated here.
[0116] Fig.12a FIG. 1 is a schematic diagram of the storage order and format of the first input matrix in the present disclosure. Fig.12aAs shown, it is an example of the first input matrix in the above embodiment. When it is stored in the memory, it is stored in the order of depth Cin first, then width Win, and finally height Hin, and the depth interval is S_Cin. Taking Cin=2, Win=3, Hin=3, S_Cin=4 as an example, the first point of the first row in Hin=3 rows is stored first. Due to the depth, the one point contains 2 data. Since S_Cin=4, the storage address of 4 data is added to the first address of the storage, and then the second point is stored, which also contains 2 data. Then, the storage address of 4 data is added to the first address of the point, and then the third point is stored, which also contains 2 data, until the third point is stored. There are 3*2=6 data in the first row, including 3 points in the Win direction; then the first point of the second row in Hin=3 rows is stored, and the storage is carried out in this way until all the points are stored. An example of the storage order and format of the first input matrix is as follows: Figure 12b shown.
[0117] Fig.12c A schematic diagram of the storage order and format of the convolution kernel disclosed in the present invention is shown in FIG. Fig.12c As shown, it is an example of the convolution kernel in the above embodiment. When it is stored in the memory, it is stored in rows in a manner that Cout (the number of convolution kernels) is prioritized, and each column stores a convolution kernel. In the column direction, it is stored in a manner that the depth Cin of the convolution kernel is prioritized, followed by the width Kw, and then the height Kh. Taking Cout = 2, Cin = 2, Kw = 2, Kh = 2, S_kernel = 2 as an example, a total of Cin*Kw*Kh = 8 rows of data will be stored, and 2 data will be stored in each row. First, store the first data in the first row and first column of the first convolution kernel with a depth of 2, and then store the first data in the first row and first column of the second convolution kernel with a depth of 2, to complete the storage of the first row of the convolution kernel; then calculate the first address of the second row by adding S_kernel to the first address of the convolution kernel, store the second data in the first row and first column of the first convolution kernel with a depth of 2, and then store the second data in the first row and first column of the second convolution kernel with a depth of 2, to complete the storage of the second row of the convolution kernel, and store in this way until all points are stored. Examples of the storage order and format of convolution kernels are as follows: Fig.12d shown.
[0118] Because the output matrix can be used as the first input matrix for the next convolution calculation, in one embodiment, the storage order and format of the output matrix are the same as those of the first input matrix, such as Fig.12e Shown is a schematic diagram of the storage order and format of the output matrix of the present disclosure.
[0119] like Fig.12aThe rs1 shown in is the first address of the first input matrix. The data of the matrix read in can be controlled by setting the storage format and the reading method. For example, according to the above example, the row vector data of the first input matrix can be read out by storing in order in the depth direction, reading in rows and depth interval. Fig.12c The rs2 shown in is the starting address of the convolution kernel. The data of the matrix read in can be controlled by setting the storage format and the reading method. For example, if the data is stored in the order in the above example, combined with row reading and the row spacing of the convolution kernel, the row vector data of the convolution kernel can be read out. After the data of the first input matrix and the convolution kernel are read out, the convolution operation instruction is used to complete the convolution operation of a single instruction. Fig.12e The rd1 shown in is the first address of the output matrix. The output data calculated by the above convolution operation instruction is calculated according to Fig.12e The method in the above example starts with storing from rd1 until all output data is stored.
[0120] Before calling the above convolution operation instruction, it is necessary to define the parameters of the convolution operation. In the present disclosure, register 1104 is set to store the parameters of the convolution, and the attribute data of the first input matrix, the convolution kernel and the output matrix can be set. An example configuration of register 1104 is shown in the following table:
[0121]
[0122] Among them, FM_in is the first input matrix, FM_out is the output matrix, and Kernel is the convolution kernel.
[0123] In the convolution operation, the convolution operation control circuit calculates the addresses of the input data, weight data, and output data in the order of logical operations; the operation unit array reads the input data and weight data from the memory according to the address, performs calculations, and then stores the calculation results into the memory according to the address of the output data to complete the entire convolution operation.
[0124] However, since the number of operation units in the operation unit array is limited, for example, only M*N, and the number of elements in the output matrix is much more than M*N, in the convolution operation, the input data and weights are usually read in blocks, and the output data is also stored in blocks. The schematic diagram of the convolution operation is as follows Fig.13 As shown, the input data and weight data are divided into a plurality of M*N data blocks and input into the computing unit array, and the calculation results are also divided into a plurality of M*N data blocks and output respectively.
[0125] like Figures 14a-14f This is an example of the convolution operation circuit performing a convolution operation according to the convolution operation instruction. In this example, the parameters of the convolution operation are as follows:
[0126] The operation unit array M*N is 2*2;
[0127] The attribute data of the input data (first input matrix) is: Win=Hin=Cin=4, S_Cin=8;
[0128] The attribute data of the output data (output matrix) is: Wout=Hout=Cout=4, S_Cout=8;
[0129] The attribute data of the weight data (convolution kernel) is: S_kernel=8, Kw=Kh=1, N_dilation=1, S_K=1;
[0130] The parameters of Padding are: N_pad_u=N_pad_d=N_pad_l=N_pad_r=0.
[0131] The entire convolution diagram is as follows Fig.14a As shown: 4*4*4 input data is convolved with 4 1*1*4 weight data to obtain 4*4*4 output data.
[0132] The storage format of the input data is as follows: Fig.14b As shown in , each row in the input data storage format stores a point in the input data with a depth of 4, and the interval between two adjacent points is 8. The storage format of weight data is as follows Fig.14c As shown in , each column in the storage format of the weight data stores 4 points of a convolution kernel, and each row stores 4 points of the convolution kernel at the same depth, with a row interval of 8. The storage format of the output data is as follows Fig.14d As shown, the storage format of the output data is the same as that of the input data. Each row in the output data storage format stores a point in the output data with a depth of 4, and the interval between two adjacent points is 8.
[0133] The actual calculation process of convolution is as follows Fig.14e and Fig.14f Specifically, in one clock cycle, the convolution operation control circuit reads input data from the first input matrix and sends the input data to the first input register of the operation unit. The convolution operation control circuit reads weight data from the convolution kernel and sends the weight data to the second input register of the operation unit. In the next clock cycle, the operator in the operation unit uses the input data in the first input register and the weight data in the second input register to perform multiplication operation, and accumulates the accumulated value of the multiplication and addition operation in the previous cycle. Fig.14eAs shown, since the size of the operation unit array is 2*2, the input data and weight data are taken for calculation in units of 2*2. The data and weights are taken for the first time to perform matrix multiplication and addition operations, and then the intermediate data is temporarily stored. Each operation unit PU implements the multiplication and addition of the corresponding rows of input data and columns of weight data. For example, when the input data and weight data have been sent to the first input register and the second input register of the operation unit, in the first clock cycle, PU11 calculates 1*0.1=0.1. Since 0.1 is only a part of the entire convolution, it is temporarily stored in the output register instead of being written to the memory. Similarly, PU12 calculates 1*0.5=0.5; PU21 calculates 5*0.1=0.5; PU22 calculates 5*0.5=2.5. After calculating the above data, in the second clock cycle, PU11 calculates 2*0.2=0.4, and accumulates the last calculation result 0.1 to obtain: 0.4+0.1=0.5, and temporarily stores it in the output register; PU12 calculates 2*0.6=1.2, and accumulates the last calculation result 0.5 to obtain: 1.2+0.5=1.7, and temporarily stores it in the output register; PU21 calculates 6*0.2=1.2, and accumulates the last calculation result 0.5 to obtain: 1.2+0.5=1.7, and temporarily stores it in the output register; PU22 calculates 6*0.6=3.6, and accumulates the last calculation result 2.5 to obtain: 3.6+2.5=6.1, and temporarily stores it in the output register. In the third clock cycle, PU11 calculates 3*0.3=0.9, and accumulates the last calculation result 0.5 to obtain: 0.9+0.5=1.4, and temporarily stores it in the output register; PU12 calculates 3*0.7=2.1, and accumulates the last calculation result 1.7 to obtain: 2.1+1.7=3.8, and temporarily stores it in the output register; PU21 calculates 7*0.3=2.1, and accumulates the last calculation result 1.7 to obtain: 2.1+1.7=3.8, and temporarily stores it in the output register; PU22 calculates 7*0.7=4.9, and accumulates the last calculation result 6.1 to obtain: 4.9+6.1=11, and temporarily stores it in the output register. In the fourth clock cycle, PU11 calculates 4*0.4=1.6, and accumulates the last calculation result 1.4, and obtains: 1.6+1.4=3, and temporarily stores it in the output register; PU12 calculates 4*0.8=3.2, and accumulates the last calculation result 3.8, and obtains: 3.2+3.8=7, and temporarily stores it in the output register; PU21 calculates 8*0.4=3.2, and accumulates the last calculation result 3.8, and obtains: 3.2+3.8=7, and temporarily stores it in the output register; PU22 calculates 8*0.8=6.4, and accumulates the last calculation result 7.4, and obtains: 6.4+11=17.4, and temporarily stores it in the output register to obtain the output data, such as Fig.14f shown.
[0134] Finally, all output results will be stored in the storage area.
[0135] It can be understood that each operation unit PU can have multiple first input registers, second input registers and output registers, so that the convolution operation control circuit can alternate between different registers when transmitting data to the first input register and the second input register and reading data from the output register, so as to achieve parallel operations of transmitting data to the registers and performing operations on the operation units, thereby improving computing efficiency.
[0136] The embodiment of the present disclosure also provides a convolution operation method, which is a convolution operation method based on any of the aforementioned convolution operation circuits, including: taking out a convolution operation instruction from a memory; decoding the convolution operation instruction, and sending the decoded convolution operation instruction to the convolution operation circuit; based on the decoded convolution operation instruction, the convolution operation circuit obtains input data and weight data from the memory and performs operations, and stores the operation results in the memory after the operations are completed.
[0137] An embodiment of the present disclosure also provides an electronic device, including: a memory for storing computer-readable instructions; and one or more processors for executing the computer-readable instructions, so that when the processors are executed, any of the convolution operation methods in the aforementioned embodiments is implemented.
[0138] The embodiments of the present disclosure also provide a non-transitory computer-readable storage medium, which stores computer instructions, and the computer instructions are used to enable a computer to execute any of the convolution operation methods described in the aforementioned embodiments.
[0139] An embodiment of the present disclosure provides a computer program product, including computer instructions. When the computer instructions are executed by a computing device, the computing device can perform any of the convolution operation methods in the aforementioned embodiments.
[0140] An embodiment of the present disclosure provides a chip, comprising the convolution operation circuit described in any one of the aforementioned embodiments.
[0141] An embodiment of the present disclosure provides a computing device, comprising the chip described in any one of the aforementioned embodiments.
[0142] The flowchart and block diagram in the accompanying drawings of the present disclosure illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present disclosure. In this regard, each box in the flowchart or block diagram can represent a module, a program segment or a part of a code, and the module, the program segment or a part of the code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some implementations as replacements, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram and / or the flowchart, and the combination of the boxes in the block diagram and / or the flowchart can be implemented with a dedicated hardware-based system that performs the specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0143] The units involved in the embodiments described in the present disclosure may be implemented by software or hardware, wherein the name of a unit does not, in some cases, limit the unit itself.
[0144] The functions described above herein may be performed at least in part by one or more hardware logic components. For example, without limitation, exemplary types of hardware logic components that may be used include: field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chips (SOCs), complex programmable logic devices (CPLDs), and the like.
[0145] In the context of the present disclosure, a machine-readable medium may be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, device, or equipment. A machine-readable medium may be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium may include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or equipment, or any suitable combination of the foregoing. A more specific example of a machine-readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
Claims
1. A convolution operation circuit, comprising: Convolution operation control circuit; An operation unit array, wherein the operation unit array includes a plurality of operation units, each of the operation units includes a plurality of first input registers, second input registers and output registers, the first input registers are used to receive input data, the second input registers are used to receive weight data, and the output registers are used to store the operation result of the convolution operation, that is, the output data; The convolution operation control circuit is used to receive a convolution operation instruction, and transmit the input data and the weight data to the operation units participating in the operation among the multiple operation units one by one according to the operation order indicated by the convolution operation instruction, and the operation units participating in the operation perform the convolution operation operation on the input data and the weight data according to the instruction of the instruction, wherein the instruction is a single instruction; Among them, the storage format of the output data is the same as the storage format of the input data, the input data has a corresponding depth storage interval, the weight data has a corresponding storage interval between convolution kernels, and the output data has a corresponding depth storage interval.
2. The convolution operation circuit according to claim 1, characterized in that: The convolution operation instruction includes an instruction name, a first address of input data, a first address of weight data and a first address of output data.
3. The convolution operation circuit according to claim 1, wherein: The operation unit includes an operator, and the operator includes at least a multiplier and an adder; The operation unit is used to combine the multiplier and the adder according to the convolution operation instruction to perform a convolution operation.
4. The convolution operation circuit according to claim 3, characterized in that: The operation unit involved in the operation performs a convolution operation on the input data and the weight data according to the instruction of the instruction, including: Calculating the product of the input data and the weight data by the multiplier; Calculating the accumulated value of the product by the adder; The accumulated value is outputted.
5. The convolution operation circuit according to claim 4, characterized in that: The input data is row vector data of the first input matrix; The weight data is the row vector data of the convolution kernel.
6. A convolution operation device, comprising: A memory, used to store convolution operation instructions, input data, weight data, and output data; An instruction fetch module, connected to the memory, and configured to fetch the convolution operation instruction from the memory, wherein the convolution operation instruction is a single instruction; A decoding module, connected to the instruction fetch module, for decoding the convolution operation instruction acquired by the instruction fetch module; A register, used to store attribute data of the input data, attribute data of the weight data, and attribute data of the output data, wherein the storage format of the output data is the same as the storage format of the input data, the attribute data of the input data includes a depth storage interval, the attribute data of the weight data includes a storage interval between convolution kernels, and the attribute data of the output data includes a depth storage interval; An execution module, connected to the decoding module, the memory and the register, comprises a convolution operation circuit as described in any one of claims 1 to 5, and is used to execute the decoded convolution operation instruction.
7. The convolution operation device according to claim 6, wherein: The execution module obtains the decoded convolution operation instruction from the decoding module; The execution module obtains the attribute data of the input data, the attribute data of the weight data and the attribute data of the output data from the register; The execution module acquires the input data and the weight data for calculation from the memory according to the attribute data of the input data and the attribute data of the weight data; The execution module calculates the input data and the weight data according to the decoded convolution operation instruction to obtain output data; The execution module stores the output data in the memory according to the attribute data of the output data.
8. The convolution operation device according to claim 7, wherein: The input data is the data of the first input matrix, the weight data is the data of the convolution kernel, and the output data is the data of the output matrix obtained by convolution; The attribute data of the input data includes the number of rows, the number of columns, the depth and the depth storage interval of the first input matrix; The attribute data of the weight data includes the number of rows, the number of columns, the step size and the row storage interval of the convolution kernel; The attribute data of the output data includes the number of rows, the number of columns, the depth and the depth storage interval of the output matrix.
9. The convolution operation device according to claim 7, wherein: The execution module obtains the input data and the weight data for calculation from the memory according to the attribute data of the input data and the attribute data of the weight data, including: The execution module reads the data of the first input matrix according to a preset first reading method and attribute data of the input data; The execution module reads the data of the convolution kernel according to a preset second reading method and attribute data of the weight data.
10. The convolution operation device according to claim 9, wherein: The first reading mode is reading by row or reading by column; The second reading method is reading by row or reading by column.
11. A convolution operation method, which is a convolution operation method based on the convolution operation circuit according to any one of claims 1 to 5, characterized in that: include: fetching a convolution operation instruction from a memory, wherein the convolution operation instruction is a single instruction; Decoding the convolution operation instruction, and sending the decoded convolution operation instruction to the convolution operation circuit; Based on the decoded convolution operation instruction, the convolution operation circuit obtains input data and weight data from the memory and performs operations, and stores the operation results in the memory after the operations are completed, wherein the storage format of the operation results is the same as the storage format of the input data, the attribute data of the input data includes a depth storage interval, the attribute data of the weight data includes a storage interval between convolution kernels, and the attribute data of the operation results includes a depth storage interval.
