An im2col conversion method and conversion device
By optimizing the data placement mode and parallel im2col conversion module processing, the problem of low im2col conversion efficiency in the existing technology is solved, more efficient two-dimensional convolution calculations are achieved, and the computing performance of the hardware circuit is improved.
Patent Information
- Application Number
- CN202410815654.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-24
- Publication Date
- 2024-09-17
- Estimated Expiration
- 2044-06-24
AI Technical Summary
In the existing technology, im2col conversion efficiency is low and it is difficult to support multiple calculation modes of two-dimensional convolution calculations, resulting in hardware circuits that are not efficient when performing matrix multiplication calculations.
A data placement mode and im2col conversion method are proposed. By optimizing the data placement of the input feature map and the output feature map in the L1_buffer, multiple im2col conversion modules are used to execute in parallel to reduce bank conflicts and improve data transmission efficiency.
The im2col conversion efficiency is improved, more data can be moved in on-chip SRAM at one time, bank conflicts are reduced, computing efficiency is improved, and it is close to the physical limit.
Smart Images

Figure CN118378007B
Abstract
Description
Technical Field
[0001] The present invention relates to artificial intelligence, and in particular to an im2col conversion method and a conversion device. Background Art
[0002] im2col is an important step to convert two-dimensional convolution calculation into matrix multiplication. Two-dimensional convolution calculation is the most important calculation mode in convolutional neural network, accounting for more than 90% of the total calculation amount. At the same time, two-dimensional convolution calculation is also a relatively flexible calculation mode with many configuration parameters. It can be configured into multiple calculation modes through different parameter combinations. These configuration parameters include convolution kernel height, convolution kernel width, convolution calculation step size in horizontal and vertical directions, convolution calculation in horizontal and vertical directions. The void ratio, as well as the filling information and filling mode of the input feature map in the four directions of top, bottom, left and right.
[0003] If we want to directly support two-dimensional convolution calculations at the hardware circuit level, it is difficult to support multiple calculation modes of two-dimensional convolution calculations because the hardware circuit is difficult to dynamically adjust and configure. The general approach to two-dimensional convolution calculations is to use im2col conversion (such as Fig.11 As shown in the figure, the two-dimensional convolution calculation is converted into a matrix multiplication calculation with a fixed calculation mode, and then the matrix multiplication calculation is performed using a hardware circuit. It can be seen that the im2col conversion step is an important step in the two-dimensional convolution calculation, and its execution efficiency will directly affect the final efficiency of the two-dimensional convolution calculation. Summary of the invention
[0004] In view of the above-mentioned shortcomings of the prior art, the present invention provides an im2col conversion method and a conversion device, which can effectively overcome the defect of low im2col conversion efficiency in the prior art.
[0005] To achieve the above objectives, the present invention is implemented through the following technical solutions:
[0006] A data placement mode, the data placement mode including a data placement mode of an input feature map and an output feature map in L1_buffer, and a data placement mode of sliding window expansion data of the input feature map in L1_buffer;
[0007] The data placement mode of the input feature map and the output feature map in L1_buffer includes:
[0008] At a specific depth of a bank, the data of all channels at the same feature map position are placed first. If the data of all channels cannot be placed at this depth, the data of the remaining channels are placed at the next depth in the bank.
[0009] When all the data of all channels at the same feature map position are placed, if the current depth in the bank is not filled, fill the remaining positions with 0 until the bank boundary, and continue to place the data of all channels at the next position in the same column of the feature map position at the next depth corresponding to the current depth in the bank;
[0010] The input feature map is divided into several blocks based on adjacent columns, so that the input feature map is distributed as evenly as possible in each block of feature map data. Each block of feature map data is placed in a bank according to the above data placement mode;
[0011] The data placement mode of the sliding window expansion data of the input feature map in L1_buffer includes:
[0012] The sliding window expansion data of the input feature map corresponds to an output feature map position, and the sliding window expansion data of the input feature map is placed in the corresponding bank according to the data placement mode of the corresponding output feature map position;
[0013] During the data placement process, channel data is placed first, and after the channel data is placed, if it is not aligned to the bank boundary, it is filled with 0 to the bank boundary.
[0014] An im2col conversion method, taking each position in the output feature map as the center, traverses the position of each element in the input feature map in the sliding window expansion corresponding to each position in the output feature map, and calculates the position of each element in the source L1_buffer according to the position of each element in the input feature map in the sliding window expansion of the input feature map based on the data placement mode;
[0015] According to the output feature map, the position of each element in the sliding window expansion of the input feature map in the destination L1_buffer is calculated. After reading the data from the source L1_buffer, it is written to the corresponding position in the destination L1_buffer.
[0016] An im2col conversion device includes an output feature map address generator, an input feature map address generator, a source L1_buffer address generator, and a destination L1_buffer address generator;
[0017] Output feature map address generator, traversing all positions in the output feature map;
[0018] The input feature map address generator traverses the sliding window expansion of the input feature map corresponding to the position of the output feature map currently traversed by the output feature map address generator, and calculates the position of each element in the sliding window expansion in the input feature map;
[0019] The source L1_buffer address generator calculates the position of each element in the source L1_buffer according to the position in the input feature map calculated by the input feature map address generator and based on the data placement mode, and generates the corresponding bank index, bank depth index, and bank data offset at the corresponding bank depth;
[0020] The destination L1_buffer address generator traverses the sliding window expansion of the input feature map corresponding to the output feature map position currently traversed by the output feature map address generator, calculates the position of each element in the sliding window expansion of the input feature map in the destination L1_buffer, and generates the corresponding bank index, bank depth index, and bank data offset at the corresponding bank depth.
[0021] Preferably, when the output feature map address generator traverses an output feature map position, the state lasts for several cycles, and during these cycles, the input feature map address generator continuously calculates the position of each element in the input feature map in the sliding window expansion of the input feature map corresponding to the output feature map position;
[0022] Among them, the number of state cycles of the output feature map address generator is proportional to the product of the convolution kernel height, the convolution kernel width, and the number of input feature map channels. The input feature map address generator is based on the convolution kernel height, the convolution kernel width, the number of input channels, the convolution kernel step size, the hole rate, and the input feature map. Figure 4 The weekly filling information calculates the position of each element in the input feature map in the sliding window expansion of the input feature map;
[0023] The states of the source L1_buffer address generator and the input feature map address generator correspond one to one, and each time the input feature map address generator calculates an input feature map address, the source L1_buffer address generator calculates a corresponding source L1_buffer address;
[0024] The destination L1_buffer address generator calculates the position of each element in the destination L1_buffer in the sliding window expansion of the input feature map according to the convolution kernel height, the convolution kernel width, and the number of input channels.
[0025] An im2col conversion method is based on a data placement mode. Each time a bank depth address of a source L1_buffer is calculated, all elements at the corresponding bank depth are read from the source L1_buffer, and all elements at the bank depth are moved to the bank depth address of the corresponding bank depth of the destination L1_buffer at one time.
[0026] An im2col conversion device includes an output feature map address generator, an input feature map address generator, a source L1_buffer address generator, and a destination L1_buffer address generator;
[0027] Output feature map address generator, traversing all positions in the output feature map;
[0028] The input feature map address generator traverses the sliding window expansion of the input feature map corresponding to the position of the output feature map currently traversed by the output feature map address generator, and calculates the position of each element in the sliding window expansion in the input feature map;
[0029] The source L1_buffer address generator calculates the position of each element in the source L1_buffer according to the position in the input feature map calculated by the input feature map address generator and based on the data placement mode, and generates the corresponding bank index and bank depth index;
[0030] The destination L1_buffer address generator traverses the sliding window expansion of the input feature map corresponding to the output feature map position currently traversed by the output feature map address generator, calculates the position of each element in the sliding window expansion of the input feature map in the destination L1_buffer, and generates the corresponding bank index and bank depth index.
[0031] An im2col conversion method uses multiple im2col conversion modules to respectively process data of a bank in a destination L1_buffer. Based on a data placement mode, each im2col conversion module traverses each element in a sliding window expansion of an input feature map corresponding to an output feature map position placed in a corresponding bank, and calculates a bank depth address of a destination L1_buffer. After receiving data sent by a source L1_buffer, the destination L1_buffer puts all data into the bank depth address corresponding to the bank depth.
[0032] An im2col conversion device comprises an im2col conversion module and a bank conflict arbitration logic module, wherein the im2col conversion module comprises an output feature map address generator, an input feature map address generator, a source L1_buffer address generator and a destination L1_buffer address generator;
[0033] The output feature map address generator traverses all positions in the output feature map of a bank corresponding to the im2col conversion module;
[0034] The input feature map address generator traverses the sliding window expansion of the input feature map corresponding to the position of the output feature map currently traversed by the output feature map address generator, and calculates the position of each element in the sliding window expansion in the input feature map;
[0035] The source L1_buffer address generator calculates the position of each element in the source L1_buffer according to the position in the input feature map calculated by the input feature map address generator and based on the data placement mode, and generates the corresponding bank index and bank depth index;
[0036] The destination L1_buffer address generator, according to the output feature map position currently traversed by the output feature map address generator, traverses the sliding window expansion of the input feature map corresponding to the position, calculates the position of each element in the sliding window expansion of the input feature map in the bank corresponding to the im2col conversion module in the destination L1_buffer, and generates the corresponding bank depth index;
[0037] The bank conflict arbitration logic module is used to perform logical arbitration on the bank conflicts generated in the source L1_buffer, and judge whether there is a bank conflict according to the source L1_buffer address sent by the multi-way im2col conversion module each time. If there is a bank conflict, a batch of source L1_buffer addresses without bank conflicts are screened and sent to the source L1_buffer until all the source L1_buffer addresses sent by the multi-way im2col conversion modules are processed.
[0038] Compared with the prior art, the im2col conversion method and conversion device provided by the present invention have the following beneficial effects:
[0039] 1) A data placement mode for sliding window expansion data of multi-channel feature maps and input feature maps after im2col conversion is proposed, so that the data at the deep address of an on-chip SRAM are all the data of each channel at the same feature map position. The full bit width data output by the on-chip SRAM in one cycle can be used to move more data at one time. If the feature map data bit width is FW bits and the on-chip SRAM bit width is BW bits, this method can improve the efficiency of im2col conversion by BW / FW times;
[0040] 2) It is proposed to set up multiple im2col conversion modules, and utilize the data placement mode of the sliding window expansion data after the multi-channel feature map and the input feature map converted by im2col in the present application. The bank index of the sliding window in the destination L1_buffer after expansion is the same as the bank index of each element in the sliding window expansion in the source L1_buffer in most cases. This allows multiple im2col conversion modules to perform im2col conversion in parallel. If the source L1_buffer and the destination L1_buffer have B banks, then in most cases where no bank conflict occurs, this method can further increase the im2col conversion efficiency by B times, reaching the physical limit of on-chip multiple bank data transmission. BRIEF DESCRIPTION OF THE DRAWINGS
[0041] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the prior art descriptions are briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention, and for ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0042] Figure 1 It is a module schematic diagram of the first im2col conversion device in the present invention;
[0043] Figure 2 It is a module schematic diagram of the second im2col conversion device in the present invention;
[0044] Figure 3 It is a module schematic diagram of the third im2col conversion device in the present invention;
[0045] Figure 4 Schematic diagram of the data placement mode of the input feature map in L1_buffer in the present invention;
[0046] Figure 5 It is a schematic diagram of a multi-channel input feature map;
[0047] Figure 6 for Figure 5 Schematic diagram of the data placement mode of the input feature map in L1_buffer shown;
[0048] Figure 7 for Figure 5 The schematic diagram of the data placement mode of the sliding window expanded data in a bank after the input feature map is converted by im2col is shown;
[0049] Figure 8 for Figure 7The schematic diagram of the data placement mode of sliding window expansion data in multiple banks of L1_buffer is shown;
[0050] Fig. 9 A schematic diagram of a hardware circuit for performing matrix multiplication calculations;
[0051] Fig.10 For passing Fig. 9 Schematic diagram of data placement mode of output feature map in L1_buffer obtained after matrix multiplication calculation by hardware circuit shown;
[0052] Fig.11 Schematic diagram of data rearrangement after im2col conversion. DETAILED DESCRIPTION
[0053] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0054] The present invention is applied to a deep learning acceleration core hardware device, and the data has been transmitted to the on-chip memory SRAM inside the deep learning acceleration core through the on-chip bus. These memories are called L1_buffers. In order to speed up the data transmission speed and improve the parallelism of data transmission, the L1_buffer inside the deep learning acceleration core is generally composed of multiple small-block memories. These small-block memories are called banks, and each bank can transmit data in parallel. The deep learning acceleration core generally includes multiple L1_buffers, and an L1_buffer contains multiple banks. Data is transmitted from the L1_buffer to the computing unit or from one L1_buffer to another L1_buffer. When data is transmitted between these L1_buffers, various data conversions can be performed through hardware circuits, including im2col conversion.
[0055] Each bank can only write a bit width of data or read a bit width of data per cycle, and the write / read bit width of each bank is the same. In order to improve data transmission efficiency, the output bit width of a bank is generally larger than the bit width of a single data element processed in the deep learning field. In the deep learning field, the bit width of a single data is generally 8bit or 16bit, and the write / read bit width of each bank is generally 32bit, 64bit or 128bit. In a deep learning acceleration core used by the method proposed in the present invention, the write / read bit width of each bank is 128bit.
[0056] In order to improve the efficiency of data transmission or rearrangement, general hardware circuits should make full use of the output bit width of the on-chip memory SRAM. If a hardware circuit only uses one of the data (8bit or 16bit) each time it reads data from the SRAM, its efficiency is considered to be relatively low. If a hardware circuit can use all the data it reads from the SRAM each time, its efficiency in using the SRAM is considered to have reached the physical limit.
[0057] A data placement mode, which includes a data placement mode of an input feature map and an output feature map in an L1_buffer, and a data placement mode of sliding window expansion data of the input feature map in an L1_buffer.
[0058] The data placement mode of the input feature map and the output feature map in L1_buffer includes:
[0059] At a specific depth of a bank, the data of all channels at the same feature map position are placed first. If the data of all channels cannot be placed at this depth, the data of the remaining channels are placed at the next depth in the bank.
[0060] When all the data of all channels at the same feature map position are placed, if the current depth in the bank is not filled, fill the remaining positions with 0 until the bank boundary, and continue to place the data of all channels at the next position in the same column of the feature map position at the next depth corresponding to the current depth in the bank;
[0061] The input feature map is divided into several blocks in units of adjacent columns, so that the input feature map is distributed as evenly as possible in each block of feature map data. Each block of feature map data is placed in a bank according to the above data placement mode.
[0062] The data placement mode of the sliding window expansion data of the input feature map in L1_buffer includes:
[0063] The sliding window expansion data of the input feature map corresponds to an output feature map position, and the sliding window expansion data of the input feature map is placed in the corresponding bank according to the data placement mode of the corresponding output feature map position;
[0064] During the data placement process, channel data is placed first, and after the channel data is placed, if it is not aligned to the bank boundary, it is filled with 0 to the bank boundary.
[0065] Figure 4 Schematic diagram of the data placement mode of the input feature map in L1_buffer in the present invention. In this data placement mode, the width of the two-dimensional feature map, that is, the number of columns of the two-dimensional feature map, is FC, the number of banks in an L1_buffer is R, and the two-dimensional feature map is evenly divided into R blocks along the column direction. Each bank can store at most Column data, and only the first Bank storage Column data, the next bank stores the rest Column data, the following banks are not filled with data;
[0066] in, To round up, To round down.
[0067] Figure 4 In the example, N represents the number of columns of the input feature map stored in a bank. N is obtained by dividing the number of input feature map columns FC by the number of banks in L1_buffer, that is, N= ; CI represents the number of input channels that can be stored in one depth position of each bank.
[0068] Figure 5 This is a schematic diagram of a multi-channel input feature map, the size of which is 1000*1000 and the number of channels is 26. Figure 6 for Figure 5 The figure shows the data placement pattern of the input feature map in the first bank (bank0) of L1_buffer.
[0069] Figure 7 for Figure 5The following is a schematic diagram of the data placement mode of the sliding window expansion data obtained after the input feature map is converted by im2col. The figure is the sliding window expansion data of the input feature map with a convolution kernel size of 3x3 and 26 input channels, and the data element bit width is 8 bits, and the bit width of each bank in L1_buffer is 128 bits. Since the convolution kernel size is 3x3, there are 9 input feature map positions, and each position has 26 channels of data. Since the data element bit width is 8 bits and the bit width of each bank in L1_buffer is 128 bits, each bank depth can store 16 channels of data, and the input feature map has 26 channels, so the data of each feature map position is placed at 2 bank depths, and the first bank depth places the data of the first 16 channels of a feature map position. After the second bank depth places the data of the remaining 10 channels, the remaining positions are filled with 0.
[0070] The reason for specifying the data placement mode is to improve the parallelism of the subsequent im2col conversion device for each channel data. Here, a feature is used: for the data of all channels at the same feature map position, according to the placement mode of the original feature map, the placement mode of these different channel data in the source L1_buffer and the destination L1_buffer is the same, and the order remains unchanged. Therefore, the large bit width of a bank can be used to transmit the data of the entire bank output port bit width at one time, thereby improving the im2col conversion efficiency.
[0071] Figure 8 for Figure 7 The following is a schematic diagram of the data placement mode of the sliding window expansion data in multiple banks of L1_buffer. In this figure, L1_buffer has 16 banks and the size of the output feature map is 1000*1000. It is worth noting that Figure 8 The last bank only stores 55 columns of sliding window expansion data of the input feature map, while the first 15 banks store 63 columns of sliding window expansion data of the input feature map. Therefore, the data stored in the last bank is less than that in the first 15 banks, and a total of 63*15+55=1000 columns of sliding window expansion data of the input feature map are stored.
[0072] Fig. 9This is a schematic diagram of a hardware circuit that performs matrix multiplication calculations. Each row of PE inputs the data of a bank in the destination L1_buffer (also called input_buffer in this hardware circuit) after im2col conversion. Each column of PE inputs the convolution kernel weight of a bank in weight_buffer, and each bank in weigh_buffer stores data for an output channel. The data in each bank in the destination L1_buffer after Im2col conversion is multiplied by the convolution kernel weight of each output channel. The calculation results of each row of PE are spliced together and placed in a bank in output_buffer. The calculation results of each row of PE are the convolution calculation results of different output channels at the same output feature map position, that is, the results of different output channels are still placed in a bank of output_buffer after splicing, and the data of each output channel is still in accordance with the input feature map. Figure 1 The consistent data placement pattern is stored in output_buffer.
[0073] Fig.10 For passing Fig. 9 The hardware circuit shown in the figure shows the data placement pattern of the output feature map in L1_buffer after matrix multiplication calculation. In the figure, L1_buffer has 16 banks and the size of the output feature map is 1000*1000. After matrix multiplication calculation, the data placement pattern of the output feature map in output_buffer is consistent with the data placement pattern of the input feature map in the source L1_buffer. It can be directly used for the next im2col conversion without transformation. It is worth noting that Fig.10 The last bank in the example stores only 55 columns of output feature map elements, while the first 15 banks store 63 columns of output feature map elements each. Therefore, the data stored in the last bank is less than that in the first 15 banks, and a total of 63*15+55=1000 columns of output feature map data are stored.
[0074] An im2col conversion method, taking each position in the output feature map as the center, traverses the position of each element in the input feature map in the sliding window expansion corresponding to each position in the output feature map, and calculates the position of each element in the source L1_buffer according to the position of each element in the input feature map in the sliding window expansion of the input feature map based on the data placement mode;
[0075] According to the output feature map, the position of each element in the sliding window expansion of the input feature map in the destination L1_buffer is calculated. After reading the data from the source L1_buffer, it is written to the corresponding position in the destination L1_buffer.
[0076] An im2col conversion device, such as Figure 1 As shown, it includes an output feature map address generator, an input feature map address generator, a source L1_buffer address generator and a destination L1_buffer address generator;
[0077] Output feature map address generator, traversing all positions in the output feature map;
[0078] The input feature map address generator traverses the sliding window expansion of the input feature map corresponding to the position of the output feature map currently traversed by the output feature map address generator, and calculates the position of each element in the sliding window expansion in the input feature map;
[0079] The source L1_buffer address generator calculates the position of each element in the source L1_buffer according to the position in the input feature map calculated by the input feature map address generator and based on the data placement mode, and generates the corresponding bank index, bank depth index, and bank data offset at the corresponding bank depth;
[0080] The destination L1_buffer address generator traverses the sliding window expansion of the input feature map corresponding to the output feature map position currently traversed by the output feature map address generator, calculates the position of each element in the sliding window expansion of the input feature map in the destination L1_buffer, and generates the corresponding bank index, bank depth index, and bank data offset at the corresponding bank depth.
[0081] Specifically, when the output feature map address generator traverses an output feature map position, the state lasts for several cycles, during which the input feature map address generator continuously calculates the position of each element in the input feature map in the sliding window expansion of the input feature map corresponding to the output feature map position;
[0082] Among them, the number of state cycles of the output feature map address generator is proportional to the product of the convolution kernel height, the convolution kernel width, and the number of input feature map channels. The input feature map address generator is based on the convolution kernel height, the convolution kernel width, the number of input channels, the convolution kernel step size, the hole rate, and the input feature map. Figure 4 The weekly filling information calculates the position of each element in the input feature map in the sliding window expansion of the input feature map;
[0083] The states of the source L1_buffer address generator and the input feature map address generator correspond one to one. Each time the input feature map address generator calculates an input feature map address, the source L1_buffer address generator calculates a corresponding source L1_buffer address.
[0084] The destination L1_buffer address generator calculates the position of each element in the sliding window expansion of the input feature map in the destination L1_buffer according to the convolution kernel height, convolution kernel width, and number of input channels.
[0085] The above-mentioned im2col conversion method is the most basic im2col conversion execution scheme, and only one data can be moved in each cycle. According to the data placement mode proposed in this application, there is an optimization method that can utilize all the output port data of each bank to improve the im2col conversion efficiency. In the data placement mode proposed in this application, in the source L1_buffer and the destination L1_buffer, the data of multiple channels of the same feature map position are placed in the same bank depth within a bank. If the data of all channels of the same feature map position cannot be aligned to the bank boundary, the remaining positions are filled with 0. In other words, in each bank depth of the source L1_buffer and the destination L1_buffer, the data of each channel of the same feature map position are stored. This produces the first optimization method:
[0086] An im2col conversion method is based on a data placement mode. Each time a bank depth address of a source L1_buffer is calculated, all elements at the corresponding bank depth are read from the source L1_buffer, and all elements at the bank depth are moved to the bank depth address of the corresponding bank depth of the destination L1_buffer at one time.
[0087] If the bit width of each bank of L1_buffer is BW and the bit width of feature map data is FW, each bank depth can store BW / FW data of different channels at the same feature map position, that is, the first optimization method mentioned above can move BW / FW data per cycle.
[0088] An im2col conversion device, such as Figure 2 As shown, it includes an output feature map address generator, an input feature map address generator, a source L1_buffer address generator and a destination L1_buffer address generator;
[0089] Output feature map address generator, traversing all positions in the output feature map;
[0090] The input feature map address generator traverses the sliding window expansion of the input feature map corresponding to the position of the output feature map currently traversed by the output feature map address generator, and calculates the position of each element in the sliding window expansion in the input feature map;
[0091] The source L1_buffer address generator calculates the position of each element in the source L1_buffer according to the position in the input feature map calculated by the input feature map address generator and based on the data placement mode, and generates the corresponding bank index and bank depth index;
[0092] The destination L1_buffer address generator traverses the sliding window expansion of the input feature map corresponding to the output feature map position currently traversed by the output feature map address generator, calculates the position of each element in the sliding window expansion of the input feature map in the destination L1_buffer, and generates the corresponding bank index and bank depth index.
[0093] Taking the bank width of 128 bits and the single data width of 8 bits as an example, in the most basic im2col conversion execution scheme mentioned above, 8 bits of data are moved per cycle, while according to the first optimization method mentioned above, 128 bits of data can be moved per cycle, and the data conversion speed is increased by 16 times.
[0094] Figure 2 and Figure 1 The difference is that the source L1_buffer address generator only generates two source L1_buffer addresses, namely, bank index and bank depth index. The source L1_buffer reads all the data at the bank and bank depth, and transmits the data of the entire bank width at this position. Similarly, the destination L1_buffer address generator also only generates two destination L1_buffer addresses, namely, bank index and bank depth index. After receiving the data of the entire bank width sent by the source L1_buffer, the destination L1_buffer puts all the data into the bank depth corresponding to the bank depth address.
[0095] Furthermore, by taking advantage of the fact that each bank in L1_buffer can write / read data in parallel, the first optimization method mentioned above can be further optimized. This leads to the second optimization method:
[0096] An im2col conversion method uses multiple im2col conversion modules to respectively process data of a bank in a destination L1_buffer. Based on a data placement mode, each im2col conversion module traverses each element in a sliding window expansion of an input feature map corresponding to an output feature map position placed in a corresponding bank, and calculates a bank depth address of a destination L1_buffer. After receiving data sent by a source L1_buffer, the destination L1_buffer puts all data into the bank depth address corresponding to the bank depth.
[0097] An im2col conversion device, such as Figure 3 As shown, it includes an im2col conversion module and a bank conflict arbitration logic module, and the im2col conversion module includes an output feature map address generator, an input feature map address generator, a source L1_buffer address generator, and a destination L1_buffer address generator;
[0098] The output feature map address generator traverses all positions in the output feature map of a bank corresponding to the im2col conversion module;
[0099] The input feature map address generator traverses the sliding window expansion of the input feature map corresponding to the position of the output feature map currently traversed by the output feature map address generator, and calculates the position of each element in the sliding window expansion in the input feature map;
[0100] The source L1_buffer address generator calculates the position of each element in the source L1_buffer according to the position in the input feature map calculated by the input feature map address generator and based on the data placement mode, and generates the corresponding bank index and bank depth index;
[0101] The destination L1_buffer address generator, according to the output feature map position currently traversed by the output feature map address generator, traverses the sliding window expansion of the input feature map corresponding to the position, calculates the position of each element in the sliding window expansion of the input feature map in the bank corresponding to the im2col conversion module in the destination L1_buffer, and generates the corresponding bank depth index;
[0102] The bank conflict arbitration logic module is used to perform logical arbitration on the bank conflicts generated in the source L1_buffer. It determines whether there is a bank conflict based on the source L1_buffer address sent by the multi-way im2col conversion module each time. If there is a bank conflict, a batch of source L1_buffer addresses without bank conflicts are screened and sent to the source L1_buffer until all the source L1_buffer addresses sent by the multi-way im2col conversion modules are processed.
[0103] Since multiple im2col conversion modules will generate multiple addresses, these addresses are not necessarily distributed in different banks in the source L1_buffer, and the bank indexes may be the same, which will cause a bank conflict. The so-called bank conflict refers to reading data from two different addresses of the same bank in the same cycle, or writing two copies of data to the same bank. Since these parallel im2col conversion modules target data from different banks in the destination L1_buffer, each im2col conversion module generates a corresponding destination L1_buffer address for a specific bank of the destination L1_buffer, so there will definitely be no bank conflict in the destination L1_buffer, but it may be generated in the source L1_buffer.
[0104] Since the step size is not 1 in the two-dimensional convolution process, the size of the output feature map may be different from the size of the input feature map. For example, for a 14*14 input feature map, when the padding on all sides is 1, for a 3*3 convolution kernel, the step sizes in the horizontal and vertical directions are both 2, then the size of the output feature map is 7*7. In this case, according to the input feature map data placement mode designed by the present invention, the first 14 banks of the source L1_buffer store 14 columns of data of the input feature map, and the sliding windows corresponding to the 7 columns of data of the output feature map are expanded and each is located in the first 7 banks of the destination L1_buffer.
[0105] When calculating the address of the data to be retrieved from the source L1_buffer for these 7 columns of data, the im2col conversion module of the first bank of the destination L1_buffer may fetch data from the first and second banks of the source L1_buffer; the im2col conversion module of the second bank of the destination L1_buffer may fetch data from the second, third, and fourth banks of the source L1_buffer; the im2col conversion module of the third bank of the destination L1_buffer may fetch data from the fourth, fifth, and sixth banks of the source L1_buffer. The address issued by each im2col conversion module may cause a bank conflict with the address issued by the im2col conversion modules of the two adjacent banks of the destination L1_buffer. When the size of the convolution kernel is relatively large, it may even cause a bank conflict with the address issued by four or more adjacent im2col conversion modules. Therefore, a bank conflict arbitration logic module needs to be set in the im2col conversion device to perform logical arbitration on the bank conflict generated in the source L1_buffer.
[0106] Through the above method, multiple im2col conversion modules can work in parallel. When the input feature map is large, multiple columns of input feature map data can be placed in each bank in the source L1_buffer, and the convolution kernel size is relatively small, the address generated by each im2col conversion module is mostly in the corresponding bank of the corresponding source L1_buffer (that is, the source L1_buffer address generated by the first im2col conversion module is mostly in the first bank of the source L1_buffer; the source L1_buffer address generated by the second im2col conversion module is mostly in the second bank of the source L1_buffer; the source L1_buffer address generated by the third im2col conversion module is mostly in the third bank of the source L1_buffer; and so on), and no bank conflict will occur. If the number of banks in the source L1_buffer and the destination L1_buffer is B, then in most cases, B im2col conversion modules can work in parallel. When a bank conflict occurs in a few cases, it is resolved by the bank conflict arbitration logic module.
[0107] Compared with the first optimization method, in most cases without bank conflicts, this optimization method can increase the im2col conversion efficiency by B times (B is the number of banks in L1_buffer), and can achieve the effect of simultaneously reading data from B banks of the source L1_buffer and simultaneously writing data to B banks of the destination L1_buffer. The data bit width of each read and write reaches the full data bit width of each bank, which has reached the physical limit of data communication.
[0108] The above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit the same. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features may be replaced by equivalents. Such modifications or replacements will not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. An im2col conversion method, characterized in that: Taking each position in the output feature map as the center, traverse the position of each element in the input feature map in the sliding window expansion corresponding to each position in the output feature map, and calculate the position of each element in the source L1_buffer according to the position of each element in the input feature map in the sliding window expansion of the input feature map based on the data placement mode; Calculate the position of each element in the sliding window expansion of the input feature map in the destination L1_buffer according to the output feature map, read the data from the source L1_buffer, and write the corresponding position in the destination L1_buffer; Based on the data placement mode, each time the bank depth address of a source L1_buffer is calculated, all elements at the corresponding bank depth are read from the source L1_buffer, and all elements at the bank depth are moved to the bank depth address of the corresponding bank depth of the destination L1_buffer at one time; The data placement mode includes the data placement mode of the input feature map and the output feature map in L1_buffer, and the data placement mode of the sliding window expansion data of the input feature map in L1_buffer; The data placement mode of the input feature map and the output feature map in L1_buffer includes: At a specific depth of a bank, the data of all channels at the same feature map position are placed first. If the data of all channels cannot be placed at this depth, the data of the remaining channels are placed at the next depth in the bank. When all the data of all channels at the same feature map position are placed, if the current depth in the bank is not filled, fill the remaining positions with 0 until the bank boundary, and continue to place the data of all channels at the next position in the same column of the feature map position at the next depth corresponding to the current depth in the bank; The input feature map is divided into several blocks based on adjacent columns, so that the input feature map is distributed as evenly as possible in each block of feature map data. Each block of feature map data is placed in a bank according to the above data placement mode; The data placement mode of the sliding window expansion data of the input feature map in L1_buffer includes: The sliding window expansion data of the input feature map corresponds to an output feature map position, and the sliding window expansion data of the input feature map is placed in the corresponding bank according to the data placement mode of the corresponding output feature map position; During the data placement process, channel data is placed first, and after the channel data is placed, if it is not aligned to the bank boundary, it is filled with 0 to the bank boundary.
2. An im2col conversion device, used to execute the im2col conversion method according to claim 1, characterized in that: It includes an output feature map address generator, an input feature map address generator, a source L1_buffer address generator and a destination L1_buffer address generator; Output feature map address generator, traversing all positions in the output feature map; The input feature map address generator traverses the sliding window expansion of the input feature map corresponding to the position of the output feature map currently traversed by the output feature map address generator, and calculates the position of each element in the sliding window expansion in the input feature map; The source L1_buffer address generator calculates the position of each element in the source L1_buffer according to the position in the input feature map calculated by the input feature map address generator and based on the data placement mode, and generates the corresponding bank index and bank depth index; The destination L1_buffer address generator traverses the sliding window expansion of the input feature map corresponding to the output feature map position currently traversed by the output feature map address generator, calculates the position of each element in the sliding window expansion of the input feature map in the destination L1_buffer, and generates the corresponding bank index and bank depth index.
3. An im2col conversion method, used to perform im2col conversion based on the im2col conversion method of claim 1, characterized in that: Multiple im2col conversion modules are used to process the data of a bank in a destination L1_buffer respectively. Based on the data placement mode, each im2col conversion module traverses each element in the sliding window expansion of the input feature map corresponding to the output feature map position placed in the corresponding bank, and calculates a bank depth address of a destination L1_buffer. After the destination L1_buffer receives the data sent by the source L1_buffer, it puts all the data into the bank depth address of the corresponding bank depth; The im2col conversion module is used to execute the im2col conversion method described in claim 1.
4. An im2col conversion device, used to execute the im2col conversion method according to claim 3, characterized in that: It includes an im2col conversion module and a bank conflict arbitration logic module, wherein the im2col conversion module includes an output feature map address generator, an input feature map address generator, a source L1_buffer address generator, and a destination L1_buffer address generator; The output feature map address generator traverses all positions in the output feature map of a bank corresponding to the im2col conversion module; The input feature map address generator traverses the sliding window expansion of the input feature map corresponding to the position of the output feature map currently traversed by the output feature map address generator, and calculates the position of each element in the sliding window expansion in the input feature map; The source L1_buffer address generator calculates the position of each element in the source L1_buffer according to the position in the input feature map calculated by the input feature map address generator and based on the data placement mode, and generates the corresponding bank index and bank depth index; The destination L1_buffer address generator, according to the output feature map position currently traversed by the output feature map address generator, traverses the sliding window expansion of the input feature map corresponding to the position, calculates the position of each element in the sliding window expansion of the input feature map in the bank corresponding to the im2col conversion module in the destination L1_buffer, and generates the corresponding bank depth index; The bank conflict arbitration logic module is used to perform logical arbitration on the bank conflicts generated in the source L1_buffer, and judge whether there is a bank conflict according to the source L1_buffer address sent by the multi-way im2col conversion module each time. If there is a bank conflict, a batch of source L1_buffer addresses without bank conflicts are screened and sent to the source L1_buffer until all the source L1_buffer addresses sent by the multi-way im2col conversion modules are processed.
