An optimization method based on the first layer 4-bit convolution calculation
By using SIMD instructions and cross-storage technology in convolution calculations, the convolution calculations on the Beijing Ingenic chip are optimized, solving the problem of slow speed in the existing technology and achieving high efficiency in convolution calculations.
Patent Information
- Application Number
- CN202011373523.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-11-30
- Publication Date
- 2024-08-30
- Estimated Expiration
- 2040-11-30
AI Technical Summary
The existing technology has the problem of slow speed in convolution calculation. Especially when using C language programs, the optimization algorithm is weak, and the 128-bit register on the Beijing Ingenic chip is limited, making it difficult to efficiently process the convolution calculation of color images.
An optimization method based on the first-layer 4-bit convolution calculation is adopted, and the three-channel image and depth data are processed in the innermost loop through the simd instruction. Cross-stored convolution kernel data is used to perform 8-bit multiplication and 16-bit accumulation operations to improve calculation efficiency.
The convolution calculation speed is doubled, the optimization operation is simplified, and the operating efficiency on the chip is improved.
Smart Images

Figure CN114581281B
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of image recognition, and in particular to an optimization method based on first-layer 4-bit convolution calculation. Background Art
[0002] With the development of the times, the application of image recognition technology has become more and more common. There are also various optimization methods for image recognition. In particular, for the optimization of convolution calculations, current optimization methods include, for example, an optimization method designed based on the SIMD instruction set of T and X series chips such as Beijing Junzheng T30 and T31. This algorithm is suitable for the operation of vector (vector) instructions. The registers of T30 and T31 are 128-bit registers, and the number of registers is limited. This problem of the number of registers must be considered in the optimization design. On the Beijing Junzheng chip, the C program is used directly, and the speed is very slow. In addition, the algorithm of the prior art is an algorithm designed for the first layer input data to be a color image, that is, three-channel data. The limited data of the convolution kernel data is 4 bits, and it is stored as 8 bits when stored. The depth of the output data, that is, the feature map, is 16. The step size used in the convolution calculation is 2, and the width and length of the convolution kernel are 3 and 3, respectively.
[0003] Common technical terms in the prior art include:
[0004] 1. SIMD instruction: Single instruction stream multiple data stream, that is, one operation instruction can execute multiple data streams, which can improve the operation speed of the program. In a more popular understanding, it is a kind of vector (vector) calculation. Different chips have different specific instruction sets.
[0005] 2. Convolution kernel: The convolution kernel is a matrix used for image processing and a parameter for performing operations with the original image. The convolution kernel is usually composed of a column matrix (for example, a 3*3 matrix), and each square in the area has a weight value. The matrix shape is generally 1×1, 3×3, 5×5, 7×7, 1×3, 3×1, 2×2, 1×5, 5×1, …
[0006] 3. Convolution: Place the center of the convolution kernel on the pixel to be calculated, calculate the product of each element in the kernel and the image pixel value it covers and sum them up. The resulting structure is the new pixel value at that position. This process is called convolution.
[0007] 4. Feature map: The result obtained after the input data is calculated through convolution is called a feature map (or output data). The result generated after the data is fully connected is also called a feature map (or output data). The size of the feature map is generally expressed as length × width × depth, or 1 × depth. Summary of the invention
[0008] In order to solve the above problems in the prior art, the object of the present invention is to:
[0009] The method adopted by the present invention realizes a doubling of the speed.
[0010] Specifically, the present invention provides an optimization method based on the first layer of 4-bit convolution calculation, which implements a complete convolution calculation process for an input 3-channel color image with an output data depth of 16. The SIMD instruction operation involved is in the innermost loop. In the innermost loop, the three channels of the image and the three groups of depth data in sequence are regarded as a group of data for processing and calculation, wherein the storage order of the convolution kernel data is stored in accordance with the required order of the convolution calculation, that is, the convolution kernel data stored according to the convolution calculation, two adjacent rows, that is, two groups of data at the output depth are cross-stored; the SIMD instruction is in the innermost loop, and eight consecutive rows need to be cross-stored for every two adjacent rows, and each data in the 9th row is cross-calculated with 0. At this time, the data is equivalent to storing ten rows of data.
[0011] The method loads data continuously when loading data. In the innermost loop, one of the data loaded each time is copied to the register of the variable of the SIMD instruction, and an 8-bit multiplication SIMD instruction calculation is performed. After the data is converted to 16 bits, an accumulation SIMD instruction calculation is performed; or in the convolution calculation, a SIMD instruction of multiplication and adjacent addition is used to realize the conversion of 8-bit data into 16-bit data.
[0012] The method comprises the following steps:
[0013] S1, initialization setting parameters:
[0014] Assume that the input data indata is a set of data with an input depth in_depth of 32, a width in_width of 512, and a height in_height of 512; the convolution kernel data filter_data is a set of data with an output depth out_depth of 128, an input depth in_depth of 32, which is consistent with the input data depth, a convolution kernel width ft_w of 3, and a convolution kernel height ft_h of 3; Assume that the output data, i.e., the structure of the feature map outdata, has a depth of out_depth, a width of out_width, and a height of out_height; in the convolution calculation, there is a step length, and the step length is stride;
[0015] Assume simd type variable registers: sum_0,sum_1,sum_2,sum_3,in_0,in_1,in_2,in_3,in_4,in_value,ft_0,ft_1,ft_2,ft_3,ft_4,ft_5,ft_6,ft_7,ft_8,ft_9, other parameters are pointers or regular data;
[0016] S2, The first - layer loop: Let j = 0;
[0017] S2.1, Determine whether j < out_width * out_height holds,
[0018] S2.2, If not, stop the first - layer loop; if it holds, then
[0019] Execute: out_w = j % out_width;
[0020] out_h = j / out_width;
[0021] in_h = out_h * stride;
[0022] in_w = out_w * stride;
[0023] in_ptr = indata + in_w * in_depth, in_ptr is the input data pointer;
[0024] out_ptr = outdata + j * out_depth, out_ptr is the output data pointer; Execute: Determine whether out_w % 2 == 0 holds. If not, do not execute the following execution body. If it holds, then execute the following execution body: Initialize the registers sum_0, sum_1, sum_2, sum_3 to 0:
[0025] sum_0 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; Initialize to 0;
[0026] sum_1 = sum_0; Initialize to 0;
[0027] sum_2 = sum_0; Initialize to 0;
[0028] sum_3 = sum_0; Initialize to 0;
[0029] Execute: Step S3;
[0030] S2.3, Set j = j + 1, and return to step S2.1;
[0031] S3, The innermost loop: Let p = 0;
[0032] S3.1, Determine whether p < ft_h holds,
[0033] S3.2, if not, stop the innermost loop and return to step S2.3; if true, execute: obtain the input data pointer, expressed as a_loc = a_ptr + (in_h + p) * in_width * in_depth, a_loc is the input data pointer;
[0034] Get the convolution kernel data pointer, expressed as b_ptr = filter_data + 160*p, b_ptr is the convolution kernel data pointer;
[0035] Load the input data into register in_value, expressed as
[0036] in_value=ingenic_load(a_loc,0);
[0037] Copy two consecutive data to registers in_0, in_1, in_2, in_3, in_4 respectively, expressed as:
[0038] in_0=ingenic_copy2(in_value,0);
[0039] in_1=ingenic_copy2(in_value,1);
[0040] in_2=ingenic_copy2(in_value,2);
[0041] in_3=ingenic_copy2(in_value,3);
[0042] in_4=ingenic_copy2(in_value,4);
[0043] Load the convolution kernel data into registers ft_0, ft_1, ft_2, ft_3, ft_4, ft_5, ft_6, ft_7, ft_8, ft_9 respectively, expressed as:
[0044] ft_0=ingenic_load(b_ptr,0),;
[0045] ft_1=ingenic_load(b_ptr,16);
[0046] ft_2=ingenic_load(b_ptr,32);
[0047] ft_3=ingenic_load(b_ptr,48);
[0048] ft_4 = ingenic_load(b_ptr, 64);
[0049] ft_5 = ingenic_load(b_ptr, 80);
[0050] ft_6 = ingenic_load(b_ptr, 96);
[0051] ft_7 = ingenic_load(b_ptr, 112);
[0052] ft_8 = ingenic_load(b_ptr, 128);
[0053] ft_9 = ingenic_load(b_ptr, 144);
[0054] The simd instruction multiplies and then adds adjacent values to obtain sum_0 and Sum_1:
[0055] sum_0 = ingenic_muladd_h(sum_0, in_0, ft_0);
[0056] sum_1 = ingenic_muladd_h(sum_1, in_0, ft_1);
[0057] sum_0 = ingenic_muladd_h(sum_0, in_1, ft_2);
[0058] sum_1 = ingenic_muladd_h(sum_1, in_1, ft_3),
[0059] sum_0 = ingenic_muladd_h(sum_0, in_2, ft_4);
[0060] sum_1 = ingenic_muladd_h(sum_1, in_2, ft_5);
[0061] sum_0 = ingenic_muladd_h(sum_0, in_3, ft_6);
[0062] sum_1 = ingenic_muladd_h(sum_1, in_3, ft_7);
[0063] sum_0 = ingenic_muladd_h(sum_0, in_4, ft_8);
[0064] sum_1=ingenic_muladd_h(sum_1,in_4,ft_9);
[0065] Assign in_3 to in_0; assign in_4 to in_1;
[0066] in_0 = in_3;
[0067] in_1 = in_4;
[0068] Copy two consecutive data to registers in_2, in_3, and in_4 respectively, expressed as
[0069] in_2=ingenic_copy2(in_value,5);
[0070] in_3=ingenic_copy2(in_value,6);
[0071] in_4=ingenic_copy2(in_value,7);
[0072] The simd instruction multiplies and then adds adjacent operations to get sum_10, sum_11:
[0073] sum_10=ingenic_muladd_h(sum_10,in_0,ft_0);
[0074] sum_11=ingenic_muladd_h(sum_11,in_0,ft_1);
[0075] sum_10=ingenic_muladd_h(sum_10,in_1,ft_2);
[0076] sum_11=ingenic_muladd_h(sum_11,in_1,ft_3);
[0077] sum_10=ingenic_muladd_h(sum_10,in_2,ft_4);
[0078] sum_11=ingenic_muladd_h(sum_11,in_2,ft_5);
[0079] sum_10=ingenic_muladd_h(sum_10,in_3,ft_6);
[0080] sum_11=ingenic_muladd_h(sum_11,in_3,ft_7);
[0081] sum_10=ingenic_muladd_h(sum_10,in_4,ft_8);
[0082] sum_11=ingenic_muladd_h(sum_11,in_4,ft_9);
[0083] S3.3, perform p=p+1; return to step S3.1.
[0084] In step S1, one of the input depth direction data of the input data is:
[0085] [x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,…x32]; this is an example, the depth is 32, and it ends at 32.
[0086] The data in the two output depth directions of the convolution kernel data are:
[0087] [d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,…d128];
[0088] [e1,e2,e3,e4,e5,e6,e7,e8,e9,e10,e11,e12,e13,e14,e15,e16,…e128]. Here is an example, the depth is 128, ending at 128
[0089] In the step S1, stride=2; out_depth=16.
[0090] In the innermost loop, in_value=ingenic_load(a_loc,0) loads 16 data and stores them in the in_value register. Assume that they are read in sequence as A1r, A1g, A1b; B1r, B1g, B1b; C1r, C1g, C1b; D1r, D1g, D1b; E1r, E1g, E1b; F1r; these 16 data effectively use the first 15 data, and the first 15 data are represented as the first layer;
[0091] Among them, the first layer represented by the first 9 data A1r, A1g, A1b, B1r, B1g, B1b, C1r, C1g, C1b is used to calculate the cumulative value of the first group of output data sum_0 and sum_1;
[0092] Among them, the first layer represented by the 7th to 15th data C1r, C1g, C1b; D1r, D1g, D1b; E1r, E1g, E1b is used to calculate the accumulated values of the second group sum_2 and sum_3 of the output data;
[0093] This happens because the input data depth is 3, the convolution kernel width is 3, the stride is 2, two groups are calculated at a time, 15 data are used, and the register stores 16 8-bit data.
[0094] In the first 9 data, the first layer is regarded as a loop, that is, the iteration of the innermost loop. This layer of data is treated as a whole. There are 9 data, and the SIMD instruction of multiplication and adjacent addition requires even data, so a random other data is added at the end to make an even number of data. When performing multiplication, it is multiplied with 0, and the value is eliminated when adding; if 1 data is output, then 9 convolution kernel data are required, and 9 convolution and data and one 0 data are actually used. If 16 data are output, 144 convolution kernel data are required, and 160 convolution kernel data are actually used; the SIMD instruction of multiplication and adjacent addition is expressed as: vrd=ingenic_muladd_h(vrd,vrs,vrt), which can realize 8-bit multiplication, accumulation, and generate the required 16 bits.
[0095] The step S3.2 further comprises:
[0096] In_0 = ingenic_copy2 (in_value, 0), the data stored in the register is: in_0 = [A1r, A1g, A1r, A1g, A1r, A1g, A1r, A1g, A1r, A1g, A1r, A1g, A1r, A1g, A1r, A1g];
[0097] In_1=ingenic_copy2(in_value,1), the data stored in the register is: in_1=[A1b,B1r,A1b,B1r,A1b,B1r,A1b,B1r,A1b,B1r,A1b,B1r,A1b,B1r,A1b,B1r,A1b,B1r];
[0098] In_2=ingenic_copy2(in_value,2), the data stored in the register is: in_2=[B1g,B1b,B1g,B1b,B1g,B1b,B1g,B1b,B1g,B1b,B1g,B1b,B1g,B1b,B1g,B1b,B1g,B1b];
[0099] In_3 = ingenic_copy2 (in_value, 3), the data stored in the register is: in_3 = [C1r, C1g, C1r, C1g, C1r, C1g, C1r, C1g, C1r, C1g, C1r, C1g, C1r, C1g, C1r, C1g];
[0100] In_4=ingenic_copy2(in_value,4), the data stored in the register is: in_4=[C1b,D1r,C1b,D1r,C1b,D1r,C1b,D1r,C1b,D1r,C1b,D1r,C1b,D1r,C1b,D1r,C1b,D1r];
[0101] The ft_0=ingenic_load(b_ptr,0), the data stored in the register is: ft_0=[a1_1,a2_1,a1_2,a2_2,a1_3,a2_3,…,a1_8,a2_8];
[0102] The ft_1=ingenic_load(b_ptr,16), the data stored in the register is: ft_1=[a1_9,a2_9,a1_10,a2_10,a1_11,a2_11,…,a1_16,a2_16];
[0103] The ft_2=ingenic_load(b_ptr,32), the data stored in the register is: ft_2=[a3_1,b1_1,a3_2,b1_2,a3_3,b1_3,…,a3_8,b1_8];
[0104] The ft_3=ingenic_load(b_ptr,48), the data stored in the register is: ft_3=[a3_8,b1_8,a3_9,b1_9,a3_10,b1_10,…,a3_16,b1_16];
[0105] The ft_4=ingenic_load(b_ptr,64), the data stored in the register is: ft_4=[b2_1,b3_1,b2_2,b3_2,b2_3,b3_3,…,b2_8,b3_8];
[0106] The ft_5=ingenic_load(b_ptr,80), the data stored in the register is: ft_5=[b2_9,b3_9,b2_10,b3_10,b2_11,b3_11,…,b2_16,b3_16];
[0107] The ft_6=ingenic_load(b_ptr,96), the data stored in the register is: ft_6=[c1_1,c2_1,c1_2,c2_2,c1_3,c2_3,…,c1_8,c2_8];
[0108] The ft_7=ingenic_load(b_ptr,112), the data stored in the register is: ft_7=[c1_9,c2_9,c1_10,c2_10,c1_11,c2_11,…,c1_16,c2_16];
[0109] The ft_8=ingenic_load(b_ptr,128), the data stored in the register is: ft_8=[c3_1,0,c3_2,0,c3_3,0,…,c3_8,0];
[0110] The ft_9=ingenic_load(b_ptr,144), the data stored in the register is: ft_9=[c3_9,0,c3_10,0,c3_11,0,…,c3_16,0];
[0111] Later, in_0, in_1, in_2, in_3, in_4, ft_0, ft_1, ft_2, ft_3, ft_4, ft_5, ft_6, ft_7, ft_8, and ft_9 are used to perform multiplication and adjacent addition of simd instructions to calculate sum_0 and sum_1; the innermost loop is completed, that is, a convolution calculation is completed, and sum_0 and sum_1 are obtained in one calculation. At the same time, it is calculated that the two registers store 8 16-bit data, a total of 16 data; after the steps are executed, the entire output data result can be completed.
[0112] The method includes a mode of calculating two sets of data, that is, loading the convolution kernel data once, calculating the two sets of data at the same time, and calculating the required input data loaded by the two sets of output data after the innermost loop is completed once.
[0113] The first set of output data sum_0 and sum_1 are calculated using the input data loaded after the innermost loop completes to calculate the first set of output data;
[0114] The second set of output data sum_2 and sum_3 are calculated using the input data loaded after the innermost loop completes to calculate the second set of output data;
[0115] The combination of the input data loaded by the first set of output data and the input data loaded by the second set of output data is the required input data loaded by the two sets of output data calculated after the innermost loop is completed, and the overlapping shared parts in the middle are C1r, C1g, C1b, C2r, C2g, C2b, C2r, C2g, C2b;
[0116] The method for calculating the second set of input data is:
[0117] The in_0=in_3; the data stored in the register is:
[0118] in_0=[C1r,C1g,C1r,C1g,C1r,C1g,C1r,C1g,C1r,C1g,C1r,C1g,C1r,C1g,C1r,C1g]
[0119] The in_1=in_4; the data stored in the register is:
[0120] in_4=[C1b,D1r,C1b,D1r,C1b,D1r,C1b,D1r,C1b,D1r,C1b,D1r,C1b,D1r,C1b,D1r];
[0121] The in_2=ingenic_copy2(in_value,5); the data stored in the register is: in_2=[D1g, D1b, D1g, D1b, D1g, D1b, D1g, D1b, D1g, D1b, D1g, D1b, D1g, D1b, D1g, D1b];
[0122] in_3 = ingenic_copy2 (in_value, 6); the data stored in the register is: in_3 = [E1r, E1g, E1r, E1g, E1r, E1g, E1r, E1g, E1r, E1g, E1r, E1g, E1r, E1g, E1r, E1g];
[0123] The in_4=ingenic_copy2(in_value,7); the data stored in the register is: in_4=[E1b,F1r,E1b,F1r,E1b,F1r,E1b,F1r,E1b,F1r,E1b,F1r,E1b,F1r,E1b,F1r,E1b,F1r];
[0124] Later, in_0, in_1, in_2, in_3, in_4, ft_0, ft_1, ft_2, ft_3, ft_4, ft_5, ft_6, ft_7, ft_8, ft_9 are used to perform multiplication and adjacent addition of simd instructions to calculate sum_2 and sum_3.
[0125] In summary, the advantages that can be achieved by applying the method of this application are: since the method using C language program in the prior art is to calculate one data at a time, there are relatively few optimization algorithms used, and the optimization strength is very small, but the speed of running the algorithm on the chip will be very slow. However, the optimization operation of this method is simple, and the running speed can be doubled by simply adding relevant SIMD instructions in the innermost loop of the method. BRIEF DESCRIPTION OF THE DRAWINGS
[0126] The drawings described herein are used to provide a further understanding of the present invention, constitute a part of this application, and do not constitute a limitation of the present invention.
[0127] Figure 1 It is a schematic diagram of the three-dimensional space of data.
[0128] Figure 2 It is a schematic diagram of the data storage structure.
[0129] Figure 3 yes Figure 2 The output depth is compressed into a particle, and the four-dimensional space becomes a schematic diagram of the three-dimensional space.
[0130] Figure 4 The output depth out_depth is 5, and the data in the output depth direction is [a1_1, a1_2, a1_3, …, a1_n]. The schematic diagram represented by the dotted line is used for visual understanding.
[0131] Figure 5 That is Figure 3 The first rectangle in the figure shows a schematic diagram with two dotted lines in the rectangle.
[0132] Figure 6 The width w of the convolution kernel is 3, and the data in the width direction of a convolution kernel includes three sets of input data in the depth direction. This is a schematic diagram for visual understanding.
[0133] Figure 7 Shows Fig. 9 Schematic diagram of the first layer of the first 9 data A1r, A1g, A1b, B1r, B1g, B1b, C1r, C1g, C1b.
[0134] Figure 8 Shows Fig. 9Schematic diagram of the first layer from the 7th to the 15th data C1r, C1g, C1b; D1r, D1g, D1b; E1r, E1g, E1b.
[0135] Fig. 9 Shows from Figure 1 The diagram is a schematic diagram of the first layer of 15 data that are effectively used, with 16 data being read starting from the upper left.
[0136] Fig.10 It is a flowchart of an embodiment of the method. DETAILED DESCRIPTION
[0137] In order to more clearly understand the technical content and advantages of the present invention, the present invention is now further described in detail in conjunction with the accompanying drawings.
[0138] 1. Introduction to the storage of input data and input convolution kernel data
[0139] The input data is a normal color image, which can be understood as a depth with three channels, that is, RGB data. The order of input data storage in data is depth (RGB or GBR three data constitute one depth), width and height. The three-dimensional space of the data is as follows Figure 1 shown.
[0140] The storage method of convolution kernel data is: first store the output depth, then the input depth, then the convolution kernel width, and finally the convolution kernel height. Figure 2 ). In the figure, the output depth refers to the specific number of data depths, the input depth refers to the number of input depths, the convolution kernel width refers to the number of input depths, and the convolution kernel height refers to the number of convolution kernel widths. Its spatial structure is a four-dimensional structure and cannot be represented in three-dimensional space. Compressing its output depth into a particle, the four-dimensional space becomes a three-dimensional space, which is represented as follows Figure 3 shown.
[0141] According to the characteristics of the input data image, the convolution kernel data related information is set, the output depth out_depth is n (n=16), the input depth in_depth is 3, the convolution kernel width ft_w is 3, and the convolution kernel height ft_h is 3. The details are as follows:
[0142] The output depth out_depth is 5, and the data in the output depth direction is [a1_1, a1_2, a1_3, ..., a1_n]. Figure 4 The dotted line in .
[0143] The input depth in_depth is 3. An input depth direction data includes three sets of output depth data, which can be expressed as:
[0144] [a1_1,a1_2,a1_3,…,a1_n;
[0145] a2_1,a2_2,a2_3,…,a1_n;
[0146] a3_1,a3_2,a3_3,…,a1_n;];
[0147] For visual understanding Figure 5 As shown, Figure 5 That is Figure 3 The first rectangle in the figure shows that there are two dashed lines in the rectangle.
[0148] The width w of the convolution kernel is 3. The data in the width direction of a convolution kernel includes three sets of input data in the depth direction, which can be expressed as:
[0149] {[a1_1,a1_2,a1_3,…,a1_n;
[0150] a2_1,a2_2,a2_3,…,a1_n;
[0151] a3_1,a3_2,a3_3,…,a1_n;];
[0152] [b1_1,b1_2,b1_3,…,b1_n;
[0153] b2_1,b2_2,b2_3,…,b1_n;
[0154] b3_1,b3_2,b3_3,…,b1_n;];
[0155] [c1_1,c1_2,c1_3,…,c1_n;
[0156] c2_1,c2_2,c2_3,…,c1_n;
[0157] c3_1,c3_2,c3_3,…,c1_n;]}
[0158] Image understanding Figure 6 shown.
[0159] The height of the convolution kernel h is 3. Figure 2 The entire data is represented as:
[0160] {{[a1_1,a1_2,a1_3,…,a1_n;
[0161] a2_1,a2_2,a2_3,…,a1_n;
[0162] a3_1,a3_2,a3_3,…,a1_n;]; <h2 style=";text-align:left;direction:ltr">
[0163] <h2 style=";text-align:left;direction:ltr"> [b1_1,b1_2,b1_3,…,b1_n;<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0164] <h2 style=";text-align:left;direction:ltr"> b2_1,b2_2,b2_3,…,b1_n;<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0165] <h2 style=";text-align:left;direction:ltr"> b3_1,b3_2,b3_3,…,b1_n;];<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0166] <h2 style=";text-align:left;direction:ltr"> [c1_1,c1_2,c1_3,…,c1_n;<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0167] <h2 style=";text-align:left;direction:ltr"> c2_1,c2_2,c2_3,…,c1_n;<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0168] <h2 style=";text-align:left;direction:ltr"> c3_1,c3_2,c3_3,…,c1_n;]}<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0169] <h2 style=";text-align:left;direction:ltr"> {[d1_1,d1_2,d1_3,…,d1_n;<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0170] <h2 style=";text-align:left;direction:ltr"> d2_1,d2_2,d2_3,…,d1_n;<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0171] <h2 style=";text-align:left;direction:ltr"> d3_1,d3_2,d3_3,…,d1_n;];<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0172] <h2 style=";text-align:left;direction:ltr"> [e1_1,e1_2,e1_3,…,e1_n;<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0173] <h2 style=";text-align:left;direction:ltr"> e2_1,e2_2,e2_3,…,e1_n;<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0174] <h2 style=";text-align:left;direction:ltr"> e3_1,e3_2,e3_3,…,e1_n;];<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0175] <h2 style=";text-align:left;direction:ltr"> [f1_1,f1_2,f1_3,…,f1_n;<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0176] <h2 style=";text-align:left;direction:ltr"> f2_1,f2_2,f2_3,…,f1_n;<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0177] <h2 style=";text-align:left;direction:ltr"> f3_1,f3_2,f3_3,…,f1_n;]}<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0178] <h2 style=";text-align:left;direction:ltr"> {[g1_1,g1_2,g1_3,…,g1_n;<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0179] <h2 style=";text-align:left;direction:ltr"> g2_1,g2_2,g2_3,…,g1_n;<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0180] <h2 style=";text-align:left;direction:ltr"> g3_1,g3_2,g3_3,…,g1_n;];<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0181] <h2 style=";text-align:left;direction:ltr"> [h1_1,h1_2,h1_3,…,h1_n;<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0182] <h2 style=";text-align:left;direction:ltr"> h2_1,h2_2,h2_3,…,h1_n;<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0183] h3_1,h3_2,h3_3,…,h1_n;];
[0184] [j1_1,j1_2,j1_3,…,j1_n;
[0185] j2_1,j2_2,j2_3,…,j1_n;
[0186] j3_1,j3_2,j3_3,…,j1_n;]}}
[0187] For ease of understanding, we use semicolons, brackets, and curly brackets to separate the data. The actual chip stores one-dimensional vectors. In the subsequent cross-storage, the brackets and curly brackets will be removed.
[0188] 2. A new way to store convolution kernel data.
[0189] The convolution kernel data is stored in the order required by the convolution calculation, so that no data is wasted when loaded into the cache, all loaded data is used, and inappropriate loading is avoided and repeated loading times are reduced. Figure 2 The storage method shown is as follows. Now, according to the convolution kernel data stored according to the convolution calculation, two adjacent rows, that is, two sets of data at the output depth, are stored crosswise.
[0190] Data order before cross storage:
[0191] a1_1,a1_2,a1_3,…,a1_n;
[0192] a2_1,a2_2,a2_3,…,a2_n;
[0193] The order of the data after cross storage is:
[0194] a1_1,a2_1,a1_2,a2_2,a1_3,a2_3,…,a1_n,a2_n;
[0195] Eight consecutive rows are stored alternately with every two adjacent rows, and the ninth row of data is stored alternately with 0, so the data is equivalent to storing ten rows of data.
[0196] Data order before cross storage:
[0197] {[a1_1,a1_2,a1_3,…,a1_n;
[0198] a2_1,a2_2,a2_3,…,a1_n;
[0199] a3_1,a3_2,a3_3,…,a1_n;];
[0200] [b1_1,b1_2,b1_3,…,b1_n;
[0201] b2_1,b2_2,b2_3,…,b1_n;
[0202] b3_1,b3_2,b3_3,…,b1_n;];
[0203] [c1_1,c1_2,c1_3,…,c1_n;
[0204] c2_1,c2_2,c2_3,…,c1_n;
[0205] c3_1,c3_2,c3_3,…,c1_n;]}
[0206] Data order after cross storage:
[0207] a1_1,a2_1,a1_2,a2_2,a1_3,a2_3,…,a1_n,a2_n;
[0208] a3_1,b1_1,a3_2,b1_2,a3_3,b1_3,…,a3_n,b1_n;
[0209] b2_1,b3_1,b2_2,b3_2,b2_3,b3_3,…,b2_n,b3_n;
[0210] c1_1,c2_1,c1_2,c2_2,c1_3,c2_3,…,c1_n,c2_n;
[0211] c3_1,0,c3_2,0,c3_3,0,…,c3_n,0;
[0212] In the convolution calculation, the particularity of the SIMD instruction used requires that each data in the 9th row be cross-calculated with 0. When loading data, it is loaded continuously, so the data in the 9th row needs to be cross-stored with 0. The storage order is as follows:
[0213] Data order before cross storage:
[0214] c3_1,c3_2,c3_3,…,c1_n;
[0215] Data order after cross storage:
[0216] c3_1,0,c3_2,0,c3_3,0,…,c3_n,0;
[0217] The cross-storage order of convolution kernel data is as follows:
[0218] <h2 style=";text-align:left;direction:ltr">{{a1_1,a2_1,a1_2,a2_2,a1_3,a2_3,…,a1_n,a2_n;<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0219] <h2 style=";text-align:left;direction:ltr"> a3_1,b1_1,a3_2,b1_2,a3_3,b1_3,…,a3_n,b1_n;<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0220] <h2 style=";text-align:left;direction:ltr"> b2_1,b3_1,b2_2,b3_2,b2_3,b3_3,…,b2_n,b3_n;<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0221] <h2 style=";text-align:left;direction:ltr"> c1_1,c2_1,c1_2,c2_2,c1_3,c2_3,…,c1_n,c2_n;<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0222] <h2 style=";text-align:left;direction:ltr"> c3_1,0,c3_2,0,c3_3,0,…,c3_n,0;}<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0223] <h2 style=";text-align:left;direction:ltr"> {d1_1,d2_1,d1_2,d2_2,d1_3,d2_3,…,d1_n,d2_n;<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0224] <h2 style=";text-align:left;direction:ltr"> d3_1,e1_1,d3_2,e1_2,d3_3,e1_3,…,d3_n,e1_n;<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0225] <h2 style=";text-align:left;direction:ltr"> e2_1,e3_1,e2_2,e3_2,e2_3,e3_3,…,e2_n,e3_n;<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0226] <h2 style=";text-align:left;direction:ltr"> f1_1,f2_1,f1_2,f2_2,f1_3,f2_3,…,f1_n,f2_n;<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0227] <h2 style=";text-align:left;direction:ltr"> f3_1,0,f3_2,0,f3_3,0,…,f3_n,0;}<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0228] <h2 style=";text-align:left;direction:ltr"> {g1_1,g2_1,g1_2,g2_2,g1_3,g2_3,…,g1_n,g2_n;<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0229] <h2 style=";text-align:left;direction:ltr"> g3_1,h1_1,g3_2,h1_2,g3_3,h1_3,…,g3_n,h1_n;<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0230] <h2 style=";text-align:left;direction:ltr"> h2_1,h3_1,h2_2,h3_2,h2_3,h3_3,…,h2_n,h3_n;<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0231] <h2 style=";text-align:left;direction:ltr"> j1_1,j2_1,j1_2,j2_2,j1_3,j2_3,…,j1_n,j2_n;<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0232] <h2 style=";text-align:left;direction:ltr"> j3_1,0,j3_2,0,j3_3,0,…,j3_n,0;}}<h2 style=";text-align:left;direction:ltr"> <h2 style=";text-align:left;direction:ltr">
[0233] 3. SIMD instruction algorithm.
[0234] 1) Introduction to simd instructions
[0235] The simd instructions involved are as follows.
[0236] a) SIMD instructions for multiplication and adjacent addition:
[0237] vrd=ingenic_muladd_h(vrd,vrs,vrt);
[0238] Input variables are vrd, vrs, vrt, and the output variable is vrd. vrd, vrs, vrt are simd type variables, which are 128-bit registers. vrd stores 8 int16_t data, and vrs and vrt store 16 int8_t data. Since there are multiplication and addition in the operation, 4-bit data needs 16-bit storage after operation, so we store 4-bit input data into 8-bit.
[0239] vrd=[vrd0,vrd1,vrd2,vrd3,vrd4,vrd5,vrd6,vrd7];
[0240] vrs=[vrs0,vrs1,vrs2,vrs3,vrs4,vrs5,vrs6,vrs7,vrs8,vrs9,vrs10,vrs11,vrs12,vrs13,vrs14,vrs15];
[0241] vrt=[vrt0,vrt1,vrt2,vrt3,vrt4,vrt5,
[0242] vrt6,vrt7,vrt8,vrt9,vrt10,vrt11,vrt12,vrt13,vrt14,vrt15];
[0243] Equivalent operations:
[0244] vrd0:=vrd0+vrs0*vrt0+vrs1*vrt1;
[0245] vrd1:=vrd1+vrs2*vrt2+vrs3*vrt3; ...
[0247] vrd7:=vrd7+vrs14*vrt14+vrs15*vrt15;
[0248] b) Load data simd instruction:
[0249] The input data to be loaded is currently the pointer to the data. 128eit of data is loaded starting from the location pointed to by the data in memory. If it is 8eit of data, 16 are loaded. If it is 16eit of data, 8 are loaded. The data is loaded into the variable vrd.
[0250] vrd = ingenic_load(indata)
[0251] c) Copy or copy the specified element simd instruction:
[0252] Copies the specified position in the variable to the output variable. The specified position is represented by i, and 8 bits of data are copied each time.
[0253] vrs=[vrs0,vrs1,vrs2,vrs3,vrs4,vrs5,vrs6,vrs7,vrs8,vrs9,vrs10,vrs11,vrs12,vrs13,vrs14,vrs15];
[0254] Copy two consecutive data
[0255] vrd=ingenic_copy2(vrs,i)
[0256] For example:
[0257] i=0;
[0258] vrd=[vrs0,vrs1,vrs0,vrs1,vrs0,vrs1,vrs0,vrs1,vrs0,vrs1,vrs0,vrs1,vrs0,vrs1,vrs0,vrs1];
[0259] i=3;
[0260] vrd=[vrs6,vrs7,vrs6,vrs7,vrs6,vrs7,vrs6,vrs7,vrs6,vrs7,vrs6,vrs7,vrs6,vrs7,vrs6,vrs7];
[0261] 2) SIMD instructions implement convolution calculations
[0262] There are many ways to implement convolution, multiplication and accumulation. In SIMD instructions, both multiplication and addition can be implemented, but different algorithms have different efficiency and time consumption after execution. The following method can reduce redundancy to a lower level.
[0263] Let's assume that the input data indata is a set of data with an input depth in_depth of 32, a width in_width of 512, and a height in_height of 512; the convolution kernel data filter_data is a set of data with an output depth out_depth of 128, an input depth in_depth of 32 (consistent with the input data depth), a convolution kernel width ft_w of 3, and a convolution kernel height ft_h of 3. Let the structure of the output data (feature map) outdata be: depth out_depth, width out_width, and height out_height. In the convolution calculation, there is a stride, let the stride be stride. The data in the input depth direction of the input data is:
[0264] [x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,…x32]
[0265] The data in the two output depth directions of the convolution kernel data are:
[0266] [d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,…d128]
[0267] [e1,e2,e3,e4,e5,e6,e7,e8,e9,e10,e11,e12,e13,e14,e15,e16,…e128]
[0268] …
[0269] a) Common C algorithm implementation pseudo code:
[0270]
[0271]
[0272] The C program algorithm performs convolution calculations in accordance with the conventional storage methods for input data and input convolution kernel data. Calculate each value in turn according to the output data storage order to obtain each data of the output data (feature map). Since C is a data-by-data calculation, there are relatively few optimization algorithms used, and the optimization effort is small, but running the above algorithm on the chip will be very slow. The following is an optimization method using the simd instruction algorithm.
[0273] b) SIMD instruction algorithm
[0274] Each time one of the loaded data is copied to the register of the variable of the simd instruction, an 8-bit multiplication simd instruction is calculated, and then converted to 16 bits, and then the accumulation simd instruction is calculated. This multiplication and accumulation is implemented in the innermost loop. Its pseudo code is as follows: simd type variable register: sum_0, sum_1, sum_2, sum_3, in_0, in_1, in_2, in_3, in_4, in_value, ft_0, ft_1, ft_2, ft_3, ft_4, ft_5, ft_6, ft_7, ft_8, ft_9, other parameters are pointers or regular data.
[0275]
[0276]
[0277]
[0278] This pseudo code implements a complete convolution calculation process for the input 3-channel color image and the output data depth of 16. In this algorithm, the SIMD instruction operation is mainly involved in the innermost loop. The rest can be regarded as the same as the C program algorithm design. In the innermost loop, the three channels of the image and the three groups of depth data in the width are regarded as one group of data for processing and calculation, making full use of the characteristics of the specific output depth and integrating its data into the innermost.
[0279] In the innermost for loop, in_value = ingenic_load (a_loc, 0) loads 16 data into the in_value register. Assume that Figure 1 The reading starts from the upper left corner, and is A1r, A1g, A1b; B1r, B1g, B1b; C1r, C1g, C1b; D1r, D1g, D1b; E1r, E1g, E1b; F1r. These 16 data effectively use the first 15 data. The first 15 data are as follows: Fig. 9 The first layer is shown. The first 9 data are A1r, A1g, A1b, B1r, B1g, B1b, C1r, C1g, C1b, as shown Figure 7 The first layer shown is used to calculate the cumulative value of the first group of output data sum_0 and sum_1, from the 7th to the 15th data C1r, C1g, C1b; D1r, D1g, D1b; E1r, E1g, E1b, as shown Figure 8The first layer shown is used to calculate the accumulated values of the second group of sum_2 and sum_3 of the output data. The reason for this situation is that the input data depth is 3, the convolution kernel width is 3, the step size is 2, two groups are calculated at a time, 15 data are used, and the register stores 16 8-bit data, so the situation occurs.
[0280] Figure 7 The data shown, the width and height are the width and height of the convolution kernel. After the convolution calculation, a set of output data is obtained. Figure 7 The data shown is also the input data loaded after the innermost loop is completed to calculate the first set of output data. Figure 7 The data shown is the input data loaded after the innermost loop is completed to calculate the second set of output data. Fig. 9 It is the input data required to load two sets of output data after the innermost loop is completed. In the existing SIMD instructions, there is no instruction to directly convert 8-bit data into 16-bit data. You need to select SIMD instructions and shift instructions to complete a conversion SIMD instruction. The SIMD instruction for multiplication and adjacent addition is:
[0281] vrd=ingenic_muladd_h(vrd,vrs,vrt)
[0282] It can realize 8-bit multiplication and accumulation, and can also generate the required 16-bit to avoid data overflow. However, this SIMD instruction needs to design a complete algorithm, otherwise its effect will not be as good as the ordinary design. Figure 7 In the data, the first layer is regarded as a loop, that is, the iteration of the innermost for loop p=0 in the above pseudocode. This layer of data is treated as a whole. There are 9 data, and the SIMD instruction of multiplication and adjacent addition requires an even number of data, so a random other data is added to make an even number of data. When performing multiplication, it is multiplied with 0, and the value is eliminated when adding. If one data is output, then 9 convolution kernel data are needed, and the actual use is 9 convolution and data and one 0 data. If 16 data are output, then 144 convolution kernel data are needed, and the actual use is 160 convolution kernel data. The relevant instructions and loading data are as follows:
[0283] in_0 = ingenic_copy2 (in_value, 0), the data stored in the register is:
[0284] in_0=[A1r,A1g,A1r,A1g,A1r,A1g,A1r,A1g,A1r,A1g,A1r,A1g,A1r,A1g,A1r,A1g]
[0285] in_1=ingenic_copy2(in_value,1), the data stored in the register is:
[0286] in_1=[A1b,B1r,A1b,B1r,A1b,B1r,A1b,B1r,A1b,B1r,A1b,B1r,A1b,B1r,A1b,B1r]
[0287] in_2=ingenic_copy2(in_value,2), the data stored in the register is:
[0288] in_2=[B1g,B1b,B1g,B1b,B1g,B1b,B1g,B1b,B1g,B1b,B1g,B1b,B1g,B1b,B1g,B1b,]
[0289] in_3=ingenic_copy2(in_value,3), the data stored in the register is:
[0290] in_3=[C1r,C1g,C1r,C1g,C1r,C1g,C1r,C1g,C1r,C1g,C1r,C1g,C1r,C1g,C1r,C1g]
[0291] in_4 = ingenic_copy2 (in_value, 4), the data stored in the register is:
[0292] in_4=[C1b,D1r,C1b,D1r,C1b,D1r,C1b,D1r,C1b,D1r,C1b,D1r,C1b,D1r,C1b,D1r]
[0293] ft_0 = ingenic_load (b_ptr, 0), the data stored in the register is:
[0294] ft_0=[a1_1,a2_1,a1_2,a2_2,a1_3,a2_3,…,a1_8,a2_8]
[0295] ft_1 = ingenic_load (b_ptr, 16), the data stored in the register is:
[0296] ft_1=[a1_9,a2_9,a1_10,a2_10,a1_11,a2_11,…,a1_16,a2_16]
[0297] ft_2 = ingenic_load (b_ptr, 32), the data stored in the register is:
[0298] ft_2=[a3_1,b1_1,a3_2,b1_2,a3_3,b1_3,…,a3_8,b1_8]
[0299] ft_3 = ingenic_load (b_ptr, 48), the data stored in the register is:
[0300] ft_3=[a3_8,b1_8,a3_9,b1_9,a3_10,b1_10,…,a3_16,b1_16]
[0301] ft_4 = ingenic_load (b_ptr, 64), the data stored in the register is:
[0302] ft_4=[b2_1,b3_1,b2_2,b3_2,b2_3,b3_3,…,b2_8,b3_8]
[0303] ft_5 = ingenic_load (b_ptr, 80), the data stored in the register is:
[0304] ft_5=[b2_9,b3_9,b2_10,b3_10,b2_11,b3_11,…,b2_16,b3_16]
[0305] ft_6 = ingenic_load (b_ptr, 96), the data stored in the register is:
[0306] ft_6=[c1_1,c2_1,c1_2,c2_2,c1_3,c2_3,…,c1_8,c2_8]
[0307] ft_7 = ingenic_load (b_ptr, 112), the data stored in the register is:
[0308] ft_7=[c1_9,c2_9,c1_10,c2_10,c1_11,c2_11,…,c1_16,c2_16]
[0309] ft_8 = ingenic_load (b_ptr, 128), the data stored in the register is:
[0310] ft_8=[c3_1,0,c3_2,0,c3_3,0,…,c3_8,0]
[0311] ft_9 = ingenic_load (b_ptr, 144), the data stored in the register is:
[0312] ft_9 = [c3_9, 0, c3_10, 0, c3_11, 0, …, c3_16, 0]
[0313] Subsequently, use in_0, in_1, in_2, in_3, in_4, ft_0, ft_1, ft_2, ft_3, ft_4, ft_5, ft_6, ft_7, ft_8, ft_9 to execute the SIMD instruction of multiplying and then adding adjacent values to calculate sum_0 and sum_1. Complete the for(p = 0; p < ft_h; ++p) loop, that is, complete one convolution calculation. One calculation obtains sum_0 and sum_1, and at the same time, it is calculated that these two registers store 8 16-bit data, a total of 16 data. After the entire pseudocode is executed, the entire output data result can be completed.
[0314] c) Calculate the patterns of two groups of data
[0315] Reduce the repeated loading of data and calculate the patterns of two groups of data. Load the convolution kernel data once and calculate two groups of data at the same time, which can also improve the efficiency of loading the input data. The data loading once is as Fig. 9 shown. The first group of output data sum_0 and sum_1 are calculated using Figure 7 , and the second group of output data sum_2 and sum_3 are calculated using Figure 8 . Figure 7 and Figure 8 The combination is Fig. 9 , and the middle overlapping and shared part is C1r, C1g, C1b, C2r, C2g, C2b, C2r, C2g, C2b. The second group of input data in the pseudocode is:
[0316] in_0 = in_3; The data stored in the register is:
[0317] in_0 = [C1r, C1g, C1r, C1g, C1r, C1g, C1r, C1g, C1r, C1g, C1r, C1g, C1r, C1g, C1r, C1g]
[0318] in_1 = in_4; The data stored in the register is:
[0319] in_4 = [C1b, D1r, C1b, D1r, C1b, D1r, C1b, D1r, C1b, D1r, C1b, D1r, C1b, D1r, C1b, D1r]
[0320] in_2 = ingenic_copy2(in_value, 5); The data stored in the register is:
[0321] in_2=[D1g,D1b,D1g,D1b,D1g,D1b,D1g,D1b,D1g,D1b,D1g,D1b,D1g,D1b,D1g,D1b]
[0322] in_3 = ingenic_copy2 (in_value, 6); the data stored in the register is:
[0323] in_3=[E1r,E1g,E1r,E1g,E1r,E1g,E1r,E1g,E1r,E1g,E1r,E1g,E1r,E1g,E1r,E1g]
[0324] in_4 = ingenic_copy2 (in_value, 7); the data stored in the register is:
[0325] in_4=[E1b,F1r,E1b,F1r,E1b,F1r,E1b,F1r,E1b,F1r,E1b,F1r,E1b,F1r,E1b,F1r]
[0326] Then, in_0, in_1, in_2, in_3, in_4, ft_0, ft_1, ft_2, ft_3, ft_4, ft_5, ft_6, ft_7, ft_8, ft_9 are used to perform multiplication and adjacent addition of simd instructions to calculate sum_2 and sum_3. The above method can realize the reuse of some input data and the reuse of convolution kernel data.
[0327] Through the above SIMD instruction algorithm, the speed can be increased by nearly 30 times.
[0328] More systematically, Fig.10 As shown, the method of the present invention comprises the following steps:
[0329] S1, initialization setting parameters:
[0330] Assume that the input data indata is a set of data with an input depth in_depth of 32, a width in_width of 512, and a height in_height of 512; the convolution kernel data filter_data is a set of data with an output depth out_depth of 128, an input depth in_depth of 32, which is consistent with the input data depth, a convolution kernel width ft_w of 3, and a convolution kernel height ft_h of 3; Assume that the output data, i.e., the structure of the feature map outdata, has a depth of out_depth, a width of out_width, and a height of out_height; in the convolution calculation, there is a step length, and the step length is stride;
[0331] Set SIMD type variable registers: sum_0, sum_1, sum_2, sum_3, in_0, in_1, in_2, in_3, in_4, in_value, ft_0, ft_1, ft_2, ft_3, ft_4, ft_5, ft_6, ft_7, ft_8, ft_9, and other parameters are pointers or regular data;
[0332] S2, the first - layer loop: Set j = 0;
[0333] S2.1, Determine whether j < out_width * out_height holds.
[0334] S2.2, If not, stop the first - layer loop; if it holds, execute: out_w = j % out_width; where the range of j traversal is the width of the feature map multiplied by the height. Now, when finding the position in the width direction, the remainder operation is used. For example, if j is 1.5 * out_width, after taking the remainder, it is 0.5 * out_width, and j / out_width is 1 (here, the integer part is taken, and the decimal part is discarded, that is, integer - to - integer division, and the result is an integer, and the decimal part is completely discarded). The current position is the 0.5 * out_width - th column in the first row (counting starts from the 0 - th row).
[0335] out_h = j / out_width;
[0336] in_h = out_h * stride;
[0337] in_w = out_w * stride;
[0338] in_ptr = indata + in_w * in_depth, in_ptr is the input data pointer;
[0339] out_ptr = outdata + j * out_depth, out_ptr is the output data pointer; Execute: Determine whether out_w % 2 == 0 holds. If not, do not execute the following execution body; if it holds, execute the following execution body: Initialize the registers sum_0, sum_1, sum_2, sum_3 to 0:
[0340] sum_0 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; Initialize to 0;
[0341] sum_1 = sum_0; Initialize to 0;
[0342] sum_2 = sum_0; Initialize to 0;
[0343] sum_3 = sum_0; Initialize to 0;
[0344] Execute: Step S3;
[0345] S2.3, perform j = j + 1, return to Step S2.1;
[0346] S3, innermost loop: Set p = 0;
[0347] S3.1, determine whether p < ft_h holds,
[0348] S3.2, if not, stop the innermost loop, return to Step S2.3; if it holds, then execute: Obtain the input data pointer, denoted as a_loc = a_ptr + (in_h + p) * in_width * in_depth, where a_loc is the input data pointer;
[0349] Obtain the convolution kernel data pointer, denoted as b_ptr = filter_data + 160 * p, where b_ptr is the convolution kernel data pointer;
[0350] Load the input data into the register in_value, denoted as
[0351] in_value = ingenic_load(a_loc, 0);
[0352] Copy two consecutive data respectively into the registers in_0, in_1, in_2, in_3, in_4, denoted as:
[0353] in_0 = ingenic_copy2(in_value, 0);
[0354] in_1 = ingenic_copy2(in_value, 1);
[0355] in_2 = ingenic_copy2(in_value, 2);
[0356] in_3 = ingenic_copy2(in_value, 3);
[0357] in_4 = ingenic_copy2(in_value, 4);
[0358] Load the convolution kernel data into the registers ft_0, ft_1, ft_2, ft_3, ft_4, ft_5, ft_6, ft_7, ft_8, ft_9 respectively, denoted as:
[0359] ft_0 = ingenic_load(b_ptr, 0);
[0360] ft_1 = ingenic_load(b_ptr, 16);
[0361] ft_2 = ingenic_load(b_ptr, 32);
[0362] ft_3 = ingenic_load(b_ptr, 48);
[0363] ft_4 = ingenic_load(b_ptr, 64);
[0364] ft_5 = ingenic_load(b_ptr, 80);
[0365] ft_6 = ingenic_load(b_ptr, 96);
[0366] ft_7 = ingenic_load(b_ptr, 112);
[0367] ft_8 = ingenic_load(b_ptr, 128);
[0368] ft_9 = ingenic_load(b_ptr, 144);
[0369] The simd instruction multiplies and then adds adjacent values to obtain sum_0 and Sum_1:
[0370] sum_0 = ingenic_muladd_h(sum_0, in_0, ft_0);
[0371] sum_1 = ingenic_muladd_h(sum_1, in_0, ft_1);
[0372] sum_0 = ingenic_muladd_h(sum_0, in_1, ft_2);
[0373] sum_1 = ingenic_muladd_h(sum_1, in_1, ft_3);
[0374] sum_0 = ingenic_muladd_h(sum_0, in_2, ft_4);
[0375] sum_1 = ingenic_muladd_h(sum_1, in_2, ft_5);
[0376] sum_0=ingenic_muladd_h(sum_0,in_3,ft_6);
[0377] sum_1=ingenic_muladd_h(sum_1,in_3,ft_7);
[0378] sum_0=ingenic_muladd_h(sum_0,in_4,ft_8);
[0379] sum_1=ingenic_muladd_h(sum_1,in_4,ft_9);
[0380] Assign in_3 to in_0; assign in_4 to in_1;
[0381] in_0 = in_3;
[0382] in_1 = in_4;
[0383] Copy two consecutive data to registers in_2, in_3, and in_4 respectively, expressed as
[0384] in_2=ingenic_copy2(in_value,5);
[0385] in_3=ingenic_copy2(in_value,6);
[0386] in_4=ingenic_copy2(in_value,7);
[0387] The simd instruction multiplies and then adds adjacent operations to get sum_10, sum_11:
[0388] sum_10=ingenic_muladd_h(sum_10,in_0,ft_0);
[0389] sum_11=ingenic_muladd_h(sum_11,in_0,ft_1);
[0390] sum_10=ingenic_muladd_h(sum_10,in_1,ft_2);
[0391] sum_11=ingenic_muladd_h(sum_11,in_1,ft_3);
[0392] sum_10=ingenic_muladd_h(sum_10,in_2,ft_4);
[0393] sum_11=ingenic_muladd_h(sum_11,in_2,ft_5);
[0394] sum_10=ingenic_muladd_h(sum_10,in_3,ft_6);
[0395] sum_11=ingenic_muladd_h(sum_11,in_3,ft_7);
[0396] sum_10=ingenic_muladd_h(sum_10,in_4,ft_8);
[0397] sum_11=ingenic_muladd_h(sum_11,in_4,ft_9);
[0398] S3.3, perform p=p+1; return to step S3.1.
[0399] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, the embodiments of the present invention may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
Claims
1. An optimization method based on the first layer 4-bit convolution calculation, characterized in that: The method implements a complete convolution calculation process for an input 3-channel color image with a data depth of 16. The involved SIMD instruction operations are in the innermost loop. In the innermost loop, the three channels of the image and three groups of depth data in the width direction are regarded as a group of data for processing and calculation. Among them, the convolution kernel data is stored in the order required by the convolution calculation, that is, for the convolution kernel data stored according to the convolution calculation, the adjacent two rows are cross-stored in two groups of data in the output depth; the SIMD instruction is in the innermost loop and requires cross-storing every two adjacent rows among eight consecutive rows, and each data in the 9th row is cross-calculated with 0. At this time, the data is equivalent to storing ten rows of data; when the method loads data, it loads continuously. In the innermost loop, one data in each loaded data is copied to the register of the SIMD instruction variable for 8-bit multiplication SIMD instruction calculation, and then after conversion to 16-bit, accumulative SIMD instruction calculation is performed; or in the convolution calculation, the SIMD instruction of multiplying and then adding adjacent numbers is used to implement the conversion of 8-bit data to 16-bit data.
2. According to claim 1, an optimization method based on the first layer 4-bit convolution calculation is characterized in that: The method includes the following steps: S1. Initialize and set parameters: Let the input data indata be a group of data with an input depth in_depth of 32, a width in_width of 512, and a height in_height of 512; the convolution kernel data filter_data is a group of data with an output depth out_depth of 128, an input depth in_depth of 32, which is the same as the input data depth, a convolution kernel width ft_w of 3, and a convolution kernel height ft_h of 3; let the structure of the output data, that is, the feature map outdata: the depth is out_depth, the width is out_width, and the height is out_height; in the convolution calculation, there is a stride, let the stride be stride; Let the SIMD type variable registers be: sum_0, sum_1, sum_2, sum_3, in_0, in_1, in_2, in_3, in_4, in_value, ft_0, ft_1, ft_2, ft_3, ft_4, ft_5, ft_6, ft_7, ft_8, ft_9, and other parameters are pointers or regular data; S2. The first layer of loop: Let j = 0; S2.
1. Judge whether j < out_width * out_height holds; S2.
2. If it does not hold, stop the first layer of loop; if it holds, then Execute: out_w = j % out_width; out_h = j / out_width; in_h = out_h * stride; in_w = out_w * stride; in_ptr = indata + in_w * in_depth, in_ptr is the input data pointer; out_ptr = outdata + j * out_depth, where out_ptr is the output data pointer; Execute: Determine whether out_w % 2 == 0 holds. If it does not hold, do not execute the following execution body; If it holds, then execute the following execution body: Initialize the registers sum_0, sum_1, sum_2, sum_3 to 0: sum_0 = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; Initialize to 0; sum_1 = sum_0; Initialize to 0; sum_2 = sum_0; Initialize to 0; sum_3 = sum_0; Initialize to 0; Execute: Step S3; S2.3, perform j = j + 1, and return to step S2.1; S3, the innermost loop: Let p = 0; S3.1, determine whether p < ft_h holds, S3.2, if it does not hold, then stop the innermost loop and return to step S2.3; If it holds, then Execute: Obtain the input data pointer, denoted as a_loc = a_ptr + (in_h + p) * in_width * in_depth, where a_loc is the input data pointer; Obtain the convolution kernel data pointer, denoted as b_ptr = filter_data + 160 * p, where b_ptr is the convolution kernel data pointer; Load the input data into the register in_value, denoted as in_value = ingenic_load(a_loc,0); Copy two consecutive data respectively into the registers in_0, in_1, in_2, in_3, in_4, denoted as: in_0 = ingenic_copy2(in_value,0); in_1 = ingenic_copy2(in_value,1); in_2 = ingenic_copy2(in_value,2); in_3 = ingenic_copy2(in_value,3); in_4 = ingenic_copy2(in_value,4); Load the convolution kernel data into the registers ft_0, ft_1, ft_2, ft_3, ft_4, ft_5, ft_6, ft_7, ft_8, ft_9 respectively, denoted as: ft_0 = ingenic_load(b_ptr,0); ft_1 = ingenic_load(b_ptr,16); ft_2 = ingenic_load(b_ptr,32); ft_3 = ingenic_load(b_ptr,48); ft_4 = ingenic_load(b_ptr,64); ft_5 = ingenic_load(b_ptr,80); ft_6 = ingenic_load(b_ptr,96); ft_7 = ingenic_load(b_ptr,112); ft_8=ingenic_load(b_ptr,128); ft_9=ingenic_load(b_ptr,144); The simd instruction multiplies and then adds adjacent numbers to get sum_0, sum_1: sum_0 = ingenic_muladd_h (sum_0, in_0, ft_0); sum_1=ingenic_muladd_h(sum_1,in_0,ft_1); sum_0=ingenic_muladd_h(sum_0,in_1,ft_2); sum_1=ingenic_muladd_h(sum_1,in_1,ft_3), sum_0=ingenic_muladd_h(sum_0,in_2,ft_4); sum_1=ingenic_muladd_h(sum_1,in_2,ft_5); sum_0=ingenic_muladd_h(sum_0,in_3,ft_6); sum_1=ingenic_muladd_h(sum_1,in_3,ft_7); sum_0=ingenic_muladd_h(sum_0,in_4,ft_8); sum_1=ingenic_muladd_h(sum_1,in_4,ft_9); Assign in_3 to in_0; assign in_4 to in_1; i n_0 = i n_3; i n_1 = i n_4; Copy two consecutive data to registers in_2, in_3, and in_4 respectively, expressed as in_2=ingenic_copy2(in_value,5); in_3=ingenic_copy2(in_value,6); in_4=ingenic_copy2(in_value,7); The simd instruction multiplies and then adds adjacent operations to get sum_10, sum_11: sum_10=ingenic_muladd_h(sum_10,in_0,ft_0); sum_11=ingenic_muladd_h(sum_11,in_0,ft_1); sum_10=ingenic_muladd_h(sum_10,in_1,ft_2); sum_11=ingenic_muladd_h(sum_11,in_1,ft_3); sum_10=ingenic_muladd_h(sum_10,in_2,ft_4); sum_11=ingenic_muladd_h(sum_11,in_2,ft_5); sum_10=ingenic_muladd_h(sum_10,in_3,ft_6); sum_11=ingenic_muladd_h(sum_11,in_3,ft_7); sum_10=ingenic_muladd_h(sum_10,in_4,ft_8); sum_11=ingenic_muladd_h(sum_11,in_4,ft_9); S3.3, perform p=p+1; return to step S3.
1.
3. The optimization method based on the first layer 4-bit convolution calculation according to claim 2, characterized in that: In step S1, the data in an input depth direction of the input data is: [x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, ... x32]; The data in the two output depth directions of the convolution kernel data are: [d1,d2,d3,d4,d5,d6,d7,d8,d9,d10,d11,d12,d13,d14,d15,d16,…d128]; [e1,e2,e3,e4,e5,e6,e7,e8,e9,e10,e11,e12,e13,e14,e15,e16,…e128].
4. The optimization method based on the first layer 4-bit convolution calculation according to claim 2, characterized in that: In the step S1, stride=2; out_depth=16.
5. The optimization method based on the first layer 4-bit convolution calculation according to claim 4, characterized in that: In the innermost loop, in_value=ingenic_load(a_loc,0) loads 16 data and stores them in the in_value register. Assume that they are read in sequence as A1r, A1g, A1b; B1r, B1g, B1b; C1r, C1g, C1b; D1r, D1g, D1b; E1r, E1g, E1b; F1r; these 16 data effectively use the first 15 data, and the first 15 data are represented as the first layer; Among them, the first layer represented by the first 9 data A1r, A1g, A1b, B1r, B1g, B1b, C1r, C1g, C1b is used to calculate the cumulative value of the first group of output data sum_0 and sum_1; among them, the first layer represented by the 7th to the 15th data C1r, C1g, C1b; D1r, D1g, D1b; E1r, E1g, E1b is used to calculate the cumulative value of the second group of output data sum_2 and sum_3; This happens because the input data depth is 3, the convolution kernel width is 3, the stride is 2, two groups are calculated at a time, 15 data are used, and the register stores 16 8-bit data.
6. The optimization method based on the first layer 4-bit convolution calculation according to claim 5, characterized in that: In the first 9 data, the first layer is regarded as a loop, that is, the iteration of the innermost loop. This layer of data is treated as a whole. There are 9 data, and the SIMD instruction of multiplication and adjacent addition requires an even number of data, so a random other data is added at the end to make an even number of data. When performing multiplication, it is multiplied with 0, and the random other data is eliminated when adding; if 1 data is output, then 9 convolution kernel data are required, and the actual use is 9 convolution and data and one 0 data. If 16 data are output, then 144 convolution kernel data are required, and the actual use is 160 convolution kernel data; the SIMD instruction of multiplication and adjacent addition is expressed as: vrd = ingenic_muladd_h (vrd, vrs, vrt), which can realize 8-bit multiplication, accumulation, and generate the required 16 bits.
7. The optimization method based on the first layer 4-bit convolution calculation according to claim 6, characterized in that: The step S3.2 further comprises: In_0=ingenic_copy2(in_value,0), the data stored in the register is: in_0=[A1 r,A1 g,A1 r,A1 g,A1 r,A1 g,A1 r,A1 g,A1 r,A1 g,A1 r,A1 g,A1 r,A1 g,A1 r,A1 g,A1 r,A1 g]; In_1=ingenic_copy2(in_value,1), the data stored in the register is: in_1=[A1 b,B1r,A1 b,B1 r,A1 b,B1 r,A1 b,B1 r,A1 b,B1 r,A1 b,B1 r,A1 b,B1 r,A1 b,B1 r,A1 b,B1 r]; In_2=ingenic_copy2(in_value,2), the data stored in the register is: in_2=[B1 g,B1 b,B1 g,B1 b,B1 g,B1 b,B1 g,B1 b,B1 g,B1 b,B1 g,B1 b,B1 g,B1 b,B1 g,B1 b,B1 g,B1 b]; In_3=ingenic_copy2(in_value,3), the data stored in the register is: in_3=[C1 r,C1g,C1 r,C1 g,C1 r,C1 g,C1 r,C1 g,C1 r,C1 g,C1 r,C1 g,C1 r,C1 g,C1 r,C1 g]; In_4=ingenic_copy2(in_value,4), the data stored in the register is: in_4=[C1 b,D1r,C1 b,D1 r,C1 b,D1 r,C1 b,D1 r,C1 b,D1 r,C1 b,D1 r,C1 b,D1 r,C1 b,D1 r]; The ft_0=ingenic_load(b_ptr,0), the data stored in the register is: ft_0=[a1_1,a2_1,a1_2,a2_2,a1_3,a2_3,…,a1_8,a2_8]; The ft_1=ingenic_load(b_ptr,16), the data stored in the register is: ft_1=[a1_9,a2_9,a1_10,a2_10,a1_11,a2_11,…,a1_16,a2_16]; The ft_2=ingenic_load(b_ptr,32), the data stored in the register is: ft_2=[a3_1,b1_1,a3_2,b1_2,a3_3,b1_3,…,a3_8,b1_8]; The ft_3=ingenic_load(b_ptr,48), the data stored in the register is: ft_3=[a3_8,b1_8,a3_9,b1_9,a3_10,b1_10,…,a3_16,b1_16]; The ft_4=ingenic_load(b_ptr,64), the data stored in the register is: ft_4=[b2_1,b3_1,b2_2,b3_2,b2_3,b3_3,…,b2_8,b3_8]; The ft_5=ingenic_load(b_ptr,80), the data stored in the register is: ft_5=[b2_9,b3_9,b2_10,b3_10,b2_11,b3_11,…,b2_16,b3_16]; The ft_6=ingenic_load(b_ptr,96), the data stored in the register is: ft_6=[c1_1,c2_1,c1_2,c2_2,c1_3,c2_3,…,c1_8,c2_8]; The ft_7=ingenic_load(b_ptr,112), the data stored in the register is: ft_7=[c1_9,c2_9,c1_10,c2_10,c1_11,c2_11,…,c1_16,c2_16]; The ft_8=ingenic_load(b_ptr,128), the data stored in the register is: ft_8=[c3_1,0,c3_2,0,c3_3,0,…,c3_8,0]; The ft_9=ingenic_load(b_ptr,144), the data stored in the register is: ft_9=[c3_9,0,c3_10,0,c3_11,0,…,c3_16,0]; Later, in_0, in_1, in_2, in_3, in_4, ft_0, ft_1, ft_2, ft_3, ft_4, ft_5, ft_6, ft_7, ft_8, and ft_9 are used to perform multiplication and adjacent addition of simd instructions to calculate sum_0 and sum_1; the innermost loop is completed, that is, a convolution calculation is completed, and sum_0 and sum_1 are obtained in one calculation. At the same time, it is calculated that the two registers store 8 16-bit data, a total of 16 data; after the steps are executed, the entire output data result can be completed.
8. The optimization method based on the first layer 4-bit convolution calculation according to claim 7, characterized in that: The method includes a mode of calculating two sets of data, that is, loading the convolution kernel data once, calculating the two sets of data at the same time, and calculating the required input data loaded by the two sets of output data after the innermost loop is completed once. The first set of output data sum_0 and sum_1 are calculated using the input data loaded after the innermost loop completes to calculate the first set of output data; The second set of output data sum_2 and sum_3 are calculated using the input data loaded after the innermost loop completes to calculate the second set of output data; The combination of the input data loaded by the first set of output data and the input data loaded by the second set of output data is the required input data loaded by the two sets of output data calculated after the innermost loop is completed, and the overlapping shared parts in the middle are C1 r, C1 g, C1 b, C2r, C2g, C2b, C2r, C2g, C2b; The method to calculate the second set of input data is: The in_0=in_3; the data stored in the register is: in_0=[C1 r,C1 g,C1 r,C1 g,C1 r,C1 g,C1 r,C1 g,C1 r,C1 g,C1 r,C1 g,C1 r,C1 g,C1 r,C1 g]; The in_1=in_4; the data stored in the register is: in_4=[C1 b,D1 r,C1 b,D1 r,C1 b,D1 r,C1 b,D1 r,C1 b,D1 r,C1 b,D1 r,C1 b,D1 r,C1 b,D1 r]; The in_2=ingenic_copy2(in_value,5); the data stored in the register is: in_2=[D1 g,D1 b,D1 g,D1 b,D1 g,D1 b,D1 g,D1 b,D1 g,D1 b,D1 g,D1 b,D1 g,D1 b,D1 g,D1 b]; The in_3=ingenic_copy2(in_value,6); the data stored in the register is: in_3=[E1 r,E1g,E1 r,E1 g,E1 r,E1 g,E1 r,E1 g,E1 r,E1 g,E1 r,E1 g,E1 r,E1 g,E1 r,E1 g]; The data stored in the register is: in_4 = ingenic_copy2 (in_value, 7); the data stored in the register is: in_4 = [E1 b, F1r, E1 b, F1 r, E1 b, F1 r, E1 b, F1 r, E1 b, F1 r, E1 b, F1 r, E1 b, F1 r, E1 b, F1 r]; Later, in_0, in_1, in_2, in_3, in_4, ft_0, ft_1, ft_2, ft_3, ft_4, ft_5, ft_6, ft_7, ft_8, ft_9 are used to perform multiplication and adjacent addition of simd instructions to calculate sum_2 and sum_3.
Citation Information
Patent Citations
Data format conversion method of channel coding and decoding based on GPP and SIMD technologies
CN103746771A
Parallel processing method for multi-input multi-output matrix convolution
CN108205702A
