Feature map processing method and device
By constructing a thread warp and reading the input image blocks and convolution kernels respectively, the problem of cache storage pressure in feature map processing is solved and the processing efficiency is improved.
Patent Information
- Application Number
- CN202210190064.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-02-28
- Publication Date
- 2024-09-06
- Estimated Expiration
- 2042-02-28
AI Technical Summary
In existing feature map processing methods, the input feature map needs to be shared among multiple convolution kernels, resulting in cache storage pressure and thus reducing processing efficiency.
By determining the dimensional information of the input feature map and convolution kernel, multiple thread warps are constructed to read input image blocks and convolution kernels respectively, thereby realizing input image block sharing between thread warps and preventing multiple processing threads from reading input at the same time. Different image patches of feature maps.
It improves the efficiency of feature map processing, reduces cache storage pressure, and improves processing performance.
Smart Images

Figure CN116721006B
Abstract
Description
Technical Field
[0001] The present application relates to the field of artificial intelligence technology, and in particular to a feature map processing method and device. Background Art
[0002] With the development of artificial intelligence technology, a technology that uses convolutional neural networks to realize feature map processing has emerged. This model can perform convolution operations on the input feature map by introducing a convolution kernel to obtain an output feature map corresponding to the input feature map.
[0003] At present, since the input convolution map is usually arranged in NCHW memory, when performing convolution operations, in order to ensure the memory continuity of reading the input feature map and storing the output feature map, multiple processing threads in the thread bundle are usually used to simultaneously read different image blocks of the same channel of the input feature map, and use the convolution kernel corresponding to the channel to perform convolution operations, so as to obtain the output image blocks corresponding to each input image block.
[0004] However, in the above feature map processing method, since the input feature map needs to be shared among multiple convolution kernels, it usually relies on a cache method to avoid repeated reading of the input feature map from the memory. However, this reading method will cause storage pressure on the cache. Therefore, the existing feature map processing efficiency is low. Summary of the invention
[0005] Based on this, it is necessary to provide a feature map processing method and device that can improve the efficiency of feature map processing in order to address the above technical problems.
[0006] In a first aspect, the present application provides a feature map processing method, the method comprising:
[0007] Determine an input feature map for convolution operation, multiple convolution kernels, and dimension information of an output feature map corresponding to the input feature map;
[0008] Acquire first size information of each output image block used to constitute the output feature map, and obtain a plurality of thread warps based on the first size information and the dimension information;
[0009] Obtaining input image blocks corresponding to each thread bundle and used to form the input feature map, reading the corresponding input image blocks through each thread bundle, and reading the corresponding multiple convolution kernels through multiple threads included in each thread bundle;
[0010] Controlling each of the thread warps to obtain output image blocks of multiple output channels corresponding to each of the thread warps according to an input image block corresponding to the thread warp and convolution kernels corresponding to multiple threads included in the thread warp;
[0011] Based on the output image blocks of the multiple output channels corresponding to the respective thread warps, output feature maps of the multiple output channels are obtained.
[0012] In one of the embodiments, the dimensional information includes: width information of the output feature map, and height information of the output feature map; the first size information includes first width information of each output image block, and first height information of each output image block; the obtaining of multiple thread bundles based on the first size information and the dimensional information includes: obtaining the number of row image blocks for the output feature map based on the width information of the output feature map, and the first width information; obtaining the number of column image blocks for the output feature map based on the height information of the output feature map, and the first height information; determining the number of output image blocks for the output feature map based on the number of row image blocks and the number of column image blocks, and constructing multiple thread bundles corresponding to the number of output image blocks.
[0013] In one of the embodiments, the number of convolution kernels of the convolution kernel is greater than the number of threads contained in each thread bundle; the construction of multiple thread bundles corresponding to the number of output image blocks includes: grouping multiple output channels according to the number of convolution kernels and the number of threads to obtain multiple output channel combinations; obtaining the construction order of each thread bundle according to the output channel combination in which the output image block corresponding to each thread bundle is located; constructing each thread bundle according to the construction order; wherein the number of thread bundles of each thread bundle located in the same output channel combination is consistent with the number of output image blocks.
[0014] In one of the embodiments, the step of obtaining the input image blocks corresponding to the thread bundles and used to form the input feature map includes: obtaining the output image blocks corresponding to the thread bundles, and determining the output feature map positions corresponding to the output image blocks; obtaining the second size information of the input image blocks based on the first size information of the output image blocks; obtaining the input image blocks corresponding to the output image blocks based on the output feature map positions and the second size information; obtaining the input image blocks corresponding to the thread bundles based on the output image blocks corresponding to the thread bundles and the input image blocks corresponding to the output image blocks.
[0015] In one of the embodiments, obtaining the second size information of each input image block based on the first size information of the output image block includes: obtaining the convolution kernel size information of the convolution kernel and pre-set convolution step information; and obtaining the second size information based on the first size information, the convolution kernel size information, and the convolution step information.
[0016] In one of the embodiments, the input feature map is stored in a memory; the reading of the corresponding input image blocks through the various thread bundles includes: determining a current thread bundle and a current input image block corresponding to the current thread bundle; the current input image block is composed of multiple input pixels; through multiple threads included in the current thread bundle, reading each input pixel included in the current input image block from the memory in the order of distribution of the input pixels, and storing each input pixel in the first vector register in the order of distribution.
[0017] In one of the embodiments, the multiple convolution kernels are stored in a memory, and each convolution kernel carries multiple convolution kernel pixels; the multiple threads included in each thread bundle respectively read the corresponding multiple convolution kernels, including: through the multiple threads corresponding to the current thread bundle, respectively read the convolution kernel pixels included in the corresponding convolution kernel from the memory, and store the convolution kernel pixels in a second vector register; wherein, the convolution kernel pixels located in different convolution kernels and with the same convolution kernel position are stored in the same row of the second vector register.
[0018] In one of the embodiments, before respectively reading the convolution kernel pixels contained in the corresponding convolution kernel from the memory, it also includes: storing the convolution kernel pixels in the memory in a CHWN memory layout.
[0019] In one of the embodiments, the number of convolution kernels of the convolution kernel is greater than the number of threads of the multiple threads included in each thread bundle; the convolution kernel pixels are stored in the memory in a CHWN memory layout, including: grouping multiple convolution kernels according to the number of convolution kernels and the number of threads to obtain multiple convolution kernel combinations; obtaining a memory storage order corresponding to each convolution kernel combination, and storing the multiple convolution kernel combinations in the memory according to the memory storage order, and storing the convolution kernel pixels contained in the convolution kernels in the same convolution kernel combination in the memory in a CHWN memory layout.
[0020] In one of the embodiments, the controlling of each thread bundle obtains output image blocks of multiple output channels corresponding to each thread bundle according to the input image block corresponding to the thread bundle and the convolution kernels corresponding to the multiple threads included in the thread bundle, including: storing the input pixel stored in the first vector register in a scalar register; obtaining the current input pixel from the scalar register, and controlling the multiple threads included in the current thread bundle to read the convolution kernel pixels corresponding to each thread from the second vector register respectively; performing convolution operation on the current input pixel and the convolution kernel pixels corresponding to each thread through each thread included in the current thread bundle to obtain the current output pixel of each output channel corresponding to the current input pixel; and obtaining the output image blocks of each output channel according to the current output pixels of each output channel.
[0021] In one of the embodiments, after obtaining the output image blocks of each output channel, it also includes: storing the output pixels contained in the output image blocks of each output channel into a third vector register according to the row order of each output pixel in the corresponding output image block.
[0022] In a second aspect, the present application further provides a feature map processing device, the device comprising:
[0023] An operation input determination module, used to determine an input feature map for a convolution operation, a plurality of convolution kernels, and dimension information of an output feature map corresponding to the input feature map;
[0024] A thread warp acquisition module, used to acquire first size information of each output image block used to form the output feature map, and obtain a plurality of thread warps based on the first size information and the dimension information;
[0025] An operation input reading module, used for obtaining input image blocks corresponding to each thread bundle and used for constituting the input feature map, reading the corresponding input image blocks through each thread bundle, and reading the corresponding multiple convolution kernels through multiple threads included in each thread bundle;
[0026] An output image block acquisition module, used to control each thread warp to obtain output image blocks of multiple output channels corresponding to each thread warp according to the input image block corresponding to the thread warp and the convolution kernels corresponding to the multiple threads included in the thread warp;
[0027] The feature map output module is used to obtain output feature maps of multiple output channels based on the output image blocks of the multiple output channels corresponding to the thread warps.
[0028] The feature map processing method and device described above determine an input feature map for convolution operation, multiple convolution kernels, and dimension information of an output feature map corresponding to the input feature map; obtain first size information of each output image block used to form the output feature map, and obtain multiple thread bundles based on the first size information and the dimension information; obtain input image blocks corresponding to each thread bundle for forming the input feature map, read the corresponding input image blocks through each thread bundle, and read the corresponding multiple convolution kernels through multiple threads included in each thread bundle; control each thread bundle to obtain output image blocks of multiple output channels corresponding to each thread bundle according to the input image blocks corresponding to the thread bundle and the convolution kernels corresponding to the multiple threads included in the thread bundle; obtain output feature maps of multiple output channels based on the output image blocks of multiple output channels corresponding to each thread bundle. When processing feature maps, the present application can read input image blocks through thread bundles, and use multiple threads of the thread bundle to read corresponding multiple convolution kernels respectively to perform convolution operations, thereby realizing the sharing of input image blocks among multiple threads. Compared with the existing feature map processing methods, the present application does not need to read different image blocks of the input feature map in multiple processing threads at the same time, and thus can improve the processing efficiency of the feature map. BRIEF DESCRIPTION OF THE DRAWINGS
[0029] Figure 1 is a schematic flow chart of a feature graph processing method in one embodiment;
[0030] Figure 2 A schematic diagram of a process of obtaining multiple thread warps in one embodiment;
[0031] Figure 3 A schematic diagram of a flow chart of obtaining multiple thread warps in another embodiment;
[0032] Figure 4 A schematic diagram of a process of obtaining input image blocks corresponding to each thread warp in one embodiment;
[0033] Figure 5 A schematic diagram of a process of obtaining output image blocks of each thread warp in one embodiment;
[0034] Figure 6 A flowchart of a convolution calculation method based on a GPU in an application example;
[0035] Figure 7 A schematic diagram of the decomposition of convolution tasks in an application example;
[0036] Figure 8 A schematic diagram is provided for setting the number of work items in an application instance;
[0037] Fig. 9 A schematic diagram of thread bundle distribution in an application example;
[0038] Fig.10 A schematic diagram for inputting a characteristic graph reading in an application example;
[0039] Fig.11 A schematic diagram of convolution kernel reading in an application example;
[0040] Fig.12 A schematic diagram of the memory layout conversion of a convolution kernel in an application example;
[0041] Fig.13 A schematic diagram of convolution vector multiplication in an application example;
[0042] Fig.14 Output data layout diagram for an application example;
[0043] Fig.15 A schematic diagram of register data distribution after reordering in an application example;
[0044] Fig.16 A schematic diagram of output data reordering in an application example;
[0045] Fig.17 4 is a structural block diagram of a feature graph processing device in one embodiment. DETAILED DESCRIPTION
[0046] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0047] In one embodiment, Figure 1 As shown, a feature map processing method is provided, and this embodiment can be applied to an image processing unit GPU. In this embodiment, the method includes the following steps:
[0048] Step S101, determining an input feature map for convolution operation, multiple convolution kernels, and dimension information of an output feature map corresponding to the input feature map.
[0049] Among them, the input feature map refers to the original feature map input into the convolutional neural network for convolution calculation, the convolution kernel refers to the convolution kernel used to perform convolution operation on the input feature map, and the output feature map refers to the feature map obtained after convolution operation on the input feature map using the convolution kernel. Since there are multiple convolution kernels, the output feature map can also include multiple channels. The dimensional information of the output feature map refers to the information used to describe the output feature map, for example, it can include the width information and height information of the output feature map, etc.
[0050] Specifically, when the user needs to process the feature map, the feature map used for convolution operation and the convolution kernel for realizing the convolution operation can be input into the memory of the image processing unit GPU, and the image processing unit can use the feature map input by the user for the convolution operation as the input feature map, and use the convolution kernel stored in the memory as multiple convolution kernels for performing the convolution operation. After obtaining the relevant information of the input feature map and the convolution kernel, the image processing unit GPU can obtain the dimensional information of the corresponding output feature map based on the above-mentioned input feature map and the relevant information of the convolution kernel.
[0051] Step S102, obtaining first size information of each output image block used to form an output feature map, and obtaining a plurality of thread warps based on the first size information and the dimension information.
[0052] The output image block refers to the image block that constitutes the output feature map. The output feature map can be composed of multiple image blocks, and the above image blocks can be used as output image blocks, and each output image block has the same size, which can be used as the first size information, and the above first size information can also be set in advance by the user. The thread bundle refers to the execution body unit used to perform convolution operations. Each thread bundle can contain multiple threads, and each thread can be executed in parallel in the form of single instruction multiple data, and different thread bundles are used to output output image blocks at different positions.
[0053] For example, the output feature map may include output image block 1, output image block 2, and output image block 3, then the image processing unit GPU may respectively perform convolution operations on output image block 1, output image block 2, and output image block 3 through different thread bundles, which may be thread bundle 1, thread bundle 2, and thread bundle 3. Therefore, after the image processing unit GPU obtains the dimension information of the output feature map, it can obtain the size information of the output feature map from the dimension information, and based on the size information of the output feature map and the first size information of each output image block pre-set by the user, it can determine the multiple output image blocks constituting the output feature map, and construct the corresponding number of thread bundles according to the number of output image blocks, thereby obtaining multiple thread bundles.
[0054] Step S103, obtaining input image blocks corresponding to each thread bundle and used to form an input feature map, reading the corresponding input image blocks through each thread bundle, and reading the corresponding multiple convolution kernels through multiple threads included in each thread bundle;
[0055] Step S104 , controlling each thread warp to obtain output image blocks of multiple output channels corresponding to each thread warp according to the input image block corresponding to the thread warp and the convolution kernels corresponding to the multiple threads included in the thread warp.
[0056] The input image block refers to the image block used to form the input feature map, which is similar to the output image block. In this embodiment, the input feature map may also be composed of multiple image blocks, and the image blocks that constitute the input feature map can be used as input image blocks, and each input image block can correspond to the output image block one by one. For example, the output image block may include: output image block 1, output image block 2 and output image block 3, and the corresponding input image block may also include: input image block 1, input image block 2 and input image block 3, and output image block 1 is the output image block obtained after the convolution operation is performed on input image block 1, output image block 2 is the output image block obtained after the convolution operation is performed on input image block 2, and output image block 3 is the output image block obtained after the convolution operation is performed on input image block 3. Through the above correspondence method, and the thread bundle corresponding to each output image block, the input image block corresponding to each thread bundle can be obtained. For example, if thread bundle 1 is used to obtain output image block 1, then the input image block corresponding to thread bundle 1 can be input image block 1, and thread bundle 2 is used to obtain output image block 2, then the input image block corresponding to thread bundle 2 can be input image block 2. In the above manner, the image processing unit GPU can obtain the input image blocks corresponding to each thread bundle.
[0057] Afterwards, the image processing unit GPU can also read the corresponding input image block through the corresponding thread bundle, so that the threads contained in the thread bundle can realize the sharing of the input image block. At the same time, the corresponding convolution kernel can also be read through the threads contained in the thread bundle. For example, thread bundle 1 can be composed of thread A, thread B and thread C, where thread A is used to read the convolution kernel information of convolution kernel A, thread B is used to read the convolution kernel information of convolution kernel B, thread C can be used to read the convolution kernel information of convolution kernel C, and thread bundle 1 can read the input image block information of input image block 1 and share it among the threads contained therein, then thread A can At the same time, the input image block information of input image block 1 and the convolution kernel information of convolution kernel A are obtained, thereby obtaining the output image block of the output channel corresponding to convolution kernel A. Similarly, thread B can simultaneously obtain the input image block information of input image block 1 and the convolution kernel information of convolution kernel B, thereby obtaining the output image block of the output channel corresponding to convolution kernel B, and thread C can simultaneously obtain the input image block information of input image block 1 and the convolution kernel information of convolution kernel C, thereby obtaining the output image block of the output channel corresponding to convolution kernel C. Through the above steps, the output image blocks of multiple output channels corresponding to the input image block 1 can be obtained.
[0058] Step S105 , obtaining output feature maps of multiple output channels based on the output image blocks of multiple output channels corresponding to each thread warp.
[0059] Finally, through step S103 and step S104, the output image blocks of each output channel corresponding to each thread warp can be obtained. Therefore, the output image blocks of each output channel can be further spliced to obtain the output feature map of each output channel.
[0060] For example, the output channel may include output channel A, output channel B, and output channel C, and the thread bundle may include thread bundle 1, thread bundle 2, and thread bundle 3. Through step S103 and step S104, the output image blocks of each output channel corresponding to thread bundle 1 can be obtained, which are output image block A1, output image block B1, and output image block C1 respectively. The output image blocks of each output channel corresponding to thread bundle 2 can also be obtained, which are output image block A2, output image block B2, and output image block C2 respectively. The output image blocks of each output channel corresponding to thread bundle 3 can also be obtained, which are output image block A3, output image block B4, and output image block C5 respectively. 3. And output image block C3. After that, the output image blocks in the same output channel can be spliced to obtain the output feature maps of each output channel. The output image block A1, the output image block A2 and the output image block A3 can be spliced to obtain the output feature map of output channel A. The output image block B1, the output image block B2 and the output image block B3 can be spliced to obtain the output feature map of output channel B. The output image block C1, the output image block C2 and the output image block C3 can be spliced to obtain the output feature map of output channel C. Through this step, the output feature map of each output channel can be obtained.
[0061] In the above-mentioned feature map processing method, an input feature map for convolution operation, multiple convolution kernels, and dimension information of an output feature map corresponding to the input feature map are determined; first size information of each output image block used to constitute the output feature map is obtained, and multiple thread bundles are obtained based on the first size information and the dimension information; input image blocks corresponding to each thread bundle for constituting the input feature map are obtained, and the corresponding input image blocks are read through each thread bundle, and the corresponding multiple convolution kernels are read through the multiple threads included in each thread bundle; each thread bundle is controlled to obtain the output image blocks of the multiple output channels corresponding to each thread bundle according to the input image blocks corresponding to the thread bundle and the convolution kernels corresponding to the multiple threads included in the thread bundle; and output feature maps of the multiple output channels are obtained based on the output image blocks of the multiple output channels corresponding to each thread bundle. When processing feature maps, the present application can read input image blocks through thread bundles, and use multiple threads of the thread bundle to read corresponding multiple convolution kernels respectively to perform convolution operations, thereby realizing the sharing of input image blocks among multiple threads. Compared with the existing feature map processing methods, the present application does not need to read different image blocks of the input feature map in multiple processing threads at the same time, and thus can improve the processing efficiency of the feature map.
[0062] In one embodiment, the dimension information includes: width information of the output feature map, and height information of the output feature map; the first size information includes first width information of each output image block, and first height information of each output image block; Figure 2 As shown, step S102 may further include:
[0063] Step S201, obtaining the number of row image blocks for the output feature map based on the width information of the output feature map and the first width information;
[0064] Step S202: based on the output feature map height information and the first height information, obtain the number of column image blocks for the output feature map.
[0065] In this embodiment, the dimension information of the output feature map may include the width information and height information of the output feature map, wherein the width information may represent the number of pixels contained in the width direction of the output feature map, and the height information may represent the number of pixels contained in the height direction of the output feature map. The first size information refers to the size information of each output image block, and may include the width information of each output image block, i.e., the first width information, and the height information of each output image block, i.e., the first height information, and the first width information and the first height information may also represent the number of pixels contained in each output image block in the width direction and the height direction, respectively. Therefore, the image processing unit GPU may determine the number of output image blocks contained in the width direction of the output feature map through the width information of the output feature map and the first width information of each output image block, and may obtain the number of image blocks contained in each row of the output feature map, i.e., the number of row image blocks, and may also determine the number of output image blocks contained in the height direction of the output feature map through the height information of the output feature map and the first height information of each output image block, and may obtain the number of image blocks contained in each column of the output feature map, i.e., the number of column image blocks.
[0066] Step S203: determining the number of output image blocks for the output feature map based on the number of row image blocks and the number of column image blocks, and constructing a plurality of thread warps adapted to the number of output image blocks.
[0067] After obtaining the number of row image blocks contained in each row of the output feature map and the number of column image blocks contained in each column, the number of output image blocks contained in the output feature map can be calculated, so as to construct multiple threads corresponding to the number. For example, the number of row image blocks is 5 and the number of column image blocks is 4, which means that each row of the output feature map is composed of 5 output image blocks and each column is composed of 4 output image blocks. Therefore, the number of output image blocks contained in the output feature map is 20, so 20 thread bundles can be constructed.
[0068] In this embodiment, based on the first size information of each output image block and the width information and length information represented in the dimensional information of the output feature map, the number of output image blocks included in the output feature map can be determined, and then the number of constructed thread bundles can be determined. The above method can be used to obtain the number of thread bundles that need to be constructed, so that an appropriate number of thread bundles can be constructed, further improving the efficiency of thread bundle construction.
[0069] Furthermore, the number of convolution kernels of the convolution kernel is greater than the number of threads of the multiple threads included in each thread warp; Figure 3 As shown, step S203 may further include:
[0070] Step S301, grouping multiple output channels according to the number of convolution kernels and the number of threads to obtain multiple output channel combinations.
[0071] In this embodiment, the number of convolution kernels used to perform convolution operations may be greater than the number of threads contained in a thread bundle. In this case, a thread bundle cannot obtain output feature maps of all output channels. For example, a thread bundle contains only 3 threads, and the number of convolution kernels may be 6. Then, for the same input image block, two thread bundles may be required to process it at the same time, where the first thread bundle is used to process the output feature map corresponding to the output channel combination composed of the first three output channels, and the second thread is used to process the output feature map corresponding to the output channel combination composed of the last three output channels. In other words, by grouping the output channels, it can be ensured that all convolution kernels have output feature maps corresponding to the corresponding output channels.
[0072] Specifically, when the number of convolution kernels is greater than the number of threads contained in each thread bundle, the image processing unit GPU can group the output channels corresponding to each convolution kernel according to the number of threads, so as to obtain multiple output channel combinations. If the remaining output channels after grouping are less than one group, they are also processed as a group. For example, a thread bundle contains 3 threads, and the number of convolution kernels may be 7. In this case, the 7 output channels can be divided into 3 groups. The first two output channel combinations contain 3 output channels, and the last output channel combination contains only one output channel.
[0073] Step S302 : obtaining the construction order of each thread warp according to the output channel combination where the output image block corresponding to each thread warp is located.
[0074] The construction order refers to the construction sequence of each thread bundle. In this embodiment, not all thread bundles are constructed at the same time, but there is a sequence. In this way, when there are many convolution kernels, all convolution kernels can be prevented from entering the cache at the same time, which is conducive to improving the locality of the convolution kernel reading time. For example, thread bundles for the same output channel combination can be constructed first. In this way, thread bundles that rely on the same convolution kernel can be constructed first.
[0075] For example, the output channel combination may include output channel combination A and output channel combination B, wherein the thread bundle used to obtain the output feature map may include thread bundle 1, thread bundle 2, thread bundle 3, thread bundle 4, thread bundle 5 and thread bundle 6, wherein thread bundle 1, thread bundle 2 and thread bundle 3 are used to obtain the output feature map corresponding to output channel combination A, and thread bundle 4, thread bundle 5 and thread bundle 6 are used to obtain the output feature map corresponding to output channel combination B, so thread bundle 1, thread bundle 2 and thread bundle 3 correspond to the same output channel combination, so the corresponding construction order A can be set for thread bundle 1, thread bundle 2 and thread bundle 3, and thread bundle 4, thread bundle 5 and thread bundle 6 correspond to the same output channel combination, so the corresponding construction order B can be set for thread bundle 4, thread bundle 5 and thread bundle 6.
[0076] Step S303 , constructing each thread warp according to the construction order; wherein the number of thread warps of each thread warp located in the same output channel combination is adapted to the number of output image blocks.
[0077] Finally, the construction of each thread bundle can be realized according to the above construction order, and the number of thread bundles located in the same output channel combination is made to match the number of output image blocks. For example, if the number of output image blocks is 3, then the number of thread bundles constructed in the same output channel combination is also 3.
[0078] In this embodiment, when the number of convolution kernels is greater than the number of threads included in the thread bundle, the output channels can be grouped, and the grouping of the output image blocks corresponding to the thread bundles can be used to obtain the corresponding construction order. In this way, the thread bundles that rely on the same convolution kernel can be constructed first to avoid a large number of convolution kernels entering the cache at the same time, which can make the reading time locality of the convolution kernel better and the cache hit rate higher.
[0079] In addition, if Figure 4 As shown, step S103 may further include:
[0080] Step S401, obtaining the output image blocks corresponding to the respective thread warps, and determining the output feature map positions corresponding to the respective output image blocks.
[0081] The output feature map position refers to the position of each output image block in the output feature map, which can be represented in the form of coordinates. Each output image block corresponds to position information in the output feature map. For example, if the coordinate information corresponding to a certain output image block is (1, 1), then each output image block is located at the first row and first column in the output feature map, and if the coordinate information corresponding to another output image block is (2, 1), then each output image block is located at the first row and second column in the output feature map, etc. In this embodiment, the position coordinates of the output image block corresponding to each thread warp in the output feature map can be obtained as the corresponding output feature map position.
[0082] Step S402, obtaining second size information of each input image block according to the first size information of the output image block;
[0083] Step S403: Obtain the input image blocks corresponding to the output image blocks according to the output feature map position and the second size information.
[0084] The second size information refers to the size information of each input image block. After obtaining the first size information of each output image block, the corresponding size information of each input image block can be further obtained according to the first size information as the second size information. At the same time, the input feature map can be segmented based on the second size information to obtain multiple input image blocks, and according to the position of each input image block in the input feature map, the output image block matching the position is found to obtain the input image block corresponding to each output image block.
[0085] For example, for input image block 1, the corresponding coordinate information is (1, 1), so the output image block with the output feature map position (1, 1) can be used as the output image block corresponding to input image block 1. Similarly, for input image block 2, the corresponding coordinate information is (1, 2), so the output image block with the output feature map position (1, 2) can be used as the output image block corresponding to input image block 2. In the above manner, the input image block corresponding to each output image block can be found.
[0086] Step S404 , obtaining input image blocks corresponding to each thread warp according to the output image blocks corresponding to each thread warp and the input image blocks corresponding to each output image block.
[0087] Finally, according to the output image blocks corresponding to each thread warp and the input image blocks corresponding to each output image block, a corresponding relationship between the thread warp and the input image blocks can be established, thereby obtaining the input image blocks corresponding to each thread warp.
[0088] In this embodiment, the correspondence between the output image block and the input image block can be established based on the position of the image block in the feature map, so that the correspondence between the thread warp and the input image block can be constructed, thereby improving the accuracy of the thread warp reading the input image block.
[0089] Furthermore, step S402 may further include: obtaining convolution kernel size information of the convolution kernel, and pre-set convolution step information; and obtaining second size information based on the first size information, the convolution kernel size information, and the convolution step information.
[0090] Among them, the convolution kernel size information refers to the size information of each convolution kernel, which may include the convolution kernel channels contained in the convolution kernel, and the distribution information of the convolution kernel pixels contained in each convolution kernel channel. For example, for a certain convolution kernel channel, it can be the number of convolution kernel pixels contained in each row, and the number of convolution kernels contained in each column. The convolution step information is the sliding step information of the convolution kernel in the input feature map set in advance by the user, which may include the sliding step of each row and the sliding step of each column.
[0091] Specifically, after obtaining the first size information, the image processing unit GPU can also obtain the convolution kernel size information for the convolution kernel and the convolution step information for the convolution step, and use the first size information, the convolution kernel size information, and the convolution step information to obtain the second size information of each input image block.
[0092] For example, the first size information of the output image block is m*n, and the convolution kernel size information is K h *K w , the convolution step information is S h and S w , then the second size information of the input image block can be a*b, where a=(m-1)*S h +K h , and b=(n-1)*S w +K w .
[0093] In this embodiment, when obtaining the first size information of the output image block, the convolution kernel size information of the convolution kernel and the convolution step information can be further used to obtain the second size information of the input image block, so that the size of the input image block can be adapted to the size of the output image block, so that each thread bundle can accurately read the corresponding input image block, thereby further improving the accuracy of the output feature map obtained by the output.
[0094] In one embodiment, the input feature map is stored in the memory; step S103 may further include: determining the current thread bundle, and the current input image block corresponding to the current thread bundle; the current input image block is composed of multiple input pixels; through multiple threads included in the current thread bundle, read each input pixel included in the current input image block from the memory according to the distribution order of the input pixels, and store each input pixel in the first vector register according to the distribution order.
[0095] The current thread warp may be any one of the constructed multiple thread warps, and the current input image block refers to the input image block corresponding to the current thread warp, and the input image block may be composed of multiple image pixels, i.e., input pixels, and each input pixel may form the current input image block in a corresponding distribution order. The first vector register may be a vector register for storing input pixels during the convolution operation.
[0096] Specifically, after the user enters the input feature map into the image processing unit GPU, the input feature map can be pre-stored in the memory of the image processing unit GPU. When performing a convolution operation, each thread bundle needs to read the corresponding input image block from the memory and store it in the first vector register to perform the convolution operation. The reading method can be that the multiple threads contained in each current thread bundle read the input pixels contained in the current input image block stored in the memory according to the distribution order of the input pixels, for example, the reading can be performed according to the row distribution order of the pixels, and stored in the first vector register according to the distribution order.
[0097] For example, the number of threads included in the current thread bundle is 64, and the size information of the current input image block can be 6*18, indicating that the distribution order of the input pixels in the current input image block can be that each row in the current input image block contains 18 pixels, and there are 6 rows of pixels in total, then 64 threads can read 3 rows of 54 pixel data each time, and store them in the first pixel register according to the distribution order of the pixel data, that is, the first 3 rows of pixel data are read first and stored in the first pixel register, and the second time the pixel data of the last 3 rows are read and stored in the first pixel register, so that the arrangement order of the input pixels stored in the first pixel register is adapted to the distribution order of the input pixels in the current input image block.
[0098] In this embodiment, when the input pixels of the input image block are read from the memory, they can be stored in the first vector register according to the distribution order of the input pixels in the input image block, thereby ensuring that the storage arrangement order of each input pixel stored in the first vector register is consistent with the distribution order in the current input image block, thereby improving the continuity of the reading of the input feature map data.
[0099] At the same time, multiple convolution kernels are stored in the memory, and each convolution kernel carries multiple convolution kernel pixels; step S402 may further include: through multiple threads corresponding to the current thread bundle, respectively reading the convolution kernel pixels contained in the corresponding convolution kernel from the memory, and storing the convolution kernel pixels in the second vector register; wherein, the convolution kernel pixels located in different convolution kernels and with the same convolution kernel position are stored in the same row of the second vector register.
[0100] In this embodiment, similar to the input feature map, the convolution kernel may also be pre-stored in the memory of the image processing unit GPU, and the second vector register is a vector register for storing convolution kernel pixels during the convolution operation. When performing a convolution operation, each thread included in a thread bundle needs to read the corresponding convolution kernel from the memory, and each convolution kernel may also be composed of multiple convolution kernel pixels. For example, the convolution kernel may include convolution kernel A, convolution kernel B, and convolution kernel C, wherein convolution kernel A may be composed of convolution kernel pixels A1, convolution kernel pixels A2, and convolution kernel pixels A3, convolution kernel B may be composed of convolution kernel pixels B1, convolution kernel pixels B2, and convolution kernel pixels B3, and convolution kernel C may be composed of convolution kernel pixels C1, convolution kernel pixels C2, and convolution kernel pixels C3. Afterwards, when reading the corresponding convolution kernel through the threads included in the current thread bundle, the pixels in the corresponding convolution kernel can be read through thread A, thread B and thread C at the same time, and when storing in the second vector register, since the convolution kernel pixels located in different convolution kernels and with the same convolution kernel position can be processed in parallel by multiple threads of the current thread bundle, in order to facilitate the parallel processing of multiple threads, the convolution kernel pixels located in different convolution kernels and with the same convolution kernel position can be stored in the same row of the second vector register, that is, the convolution kernel pixel A1, the convolution kernel pixel B1 and the convolution kernel pixel C1 can be stored in the same row of the second vector register, the convolution kernel pixel A2, the convolution kernel pixel B2 and the convolution kernel pixel C2 can be stored in the same row of the second vector register, and the convolution kernel pixel A3, the convolution kernel pixel B3 and the convolution kernel pixel C3 can be stored in the same row of the second vector register. Then, when multiple threads perform parallel convolution operations, the convolution kernel pixel A1, the convolution kernel pixel B1, and the convolution kernel pixel C1 can be operated in parallel at the same time, or the convolution kernel pixel A2, the convolution kernel pixel B2, and the convolution kernel pixel C2 can be operated in parallel at the same time, thereby improving the continuity of the convolution kernel reading.
[0101] In this embodiment, when reading the convolution kernel pixels of the convolution kernel from the memory, the convolution kernel pixels located in different convolution kernels and with the same convolution kernel position can be stored in the same row of the second vector register, so that multiple threads can perform parallel operations according to the storage order of the convolution kernel pixels in the second vector register to improve the continuity of reading the input convolution kernel data.
[0102] In addition, before respectively reading the convolution kernel pixels contained in the corresponding convolution kernel from the memory, the method may further include: storing the convolution kernel pixels in the memory in a memory layout of CHWN.
[0103] In this embodiment, since the convolution kernel is read by multiple threads in the thread bundle respectively reading the convolution kernel pixels of different convolution kernels at the same convolution kernel position, if the original NCHW memory layout is adopted, then when each thread reads the convolution kernel pixel from the memory, the memory address will have a larger interval, that is, the memory interval is C*H*W pixels. In order to improve the continuity and locality of the convolution kernel memory access, this embodiment can store the convolution kernel pixels in the memory in a CHWN memory layout. At this time, when each thread reads the convolution kernel pixel from the memory, the address of the convolution kernel pixel is an adjacent memory address, thereby improving the continuity and locality of the convolution kernel memory access.
[0104] In this embodiment, when storing convolution kernel pixels in the memory, a CHWN memory layout can be used. In this way, when reading convolution kernel pixels of different convolution kernels at the same convolution kernel position from the memory through multiple threads, the addresses of the convolution kernel pixels can be placed at adjacent memory addresses, thereby improving the continuity and locality of the convolution kernel memory access.
[0105] In addition, the number of convolution kernels of the convolution kernel is greater than the number of threads of the multiple threads included in each thread bundle; storing the convolution kernel pixels in the memory in the memory layout of CHWN can further include: grouping the multiple convolution kernels according to the number of convolution kernels and the number of threads to obtain multiple convolution kernel combinations; obtaining the memory storage order corresponding to each convolution kernel combination, and storing the multiple convolution kernel combinations in the memory according to the memory storage order, and storing the convolution kernel pixels contained in the convolution kernels in the same convolution kernel combination in the memory layout of CHWN.
[0106] If the number of convolution kernels used to perform convolution operations is greater than the number of threads contained in a thread bundle, then in this case, multiple threads of a thread bundle cannot read all the convolution kernels at the same time. Therefore, the above multiple convolution kernels can be read through multiple thread bundles. Then, the convolution kernels can be grouped according to the number of convolution kernels and the number of threads contained in the thread bundle. For example, a thread bundle is used to read the convolution kernel information of convolution kernel group 1, while another thread bundle is used to read the convolution kernel information of convolution kernel group 2. In order to make the reading of convolution kernel information in the same thread bundle more continuous, in this embodiment, a corresponding storage order in memory can also be set for each convolution kernel group, that is, a memory storage order. The memory storage order can be to first store the convolution kernel pixels of all convolution kernels in convolution kernel group 1, and then store the convolution kernel pixels of all convolution kernels in convolution kernel group 2. The convolution kernel pixels in each convolution kernel group can also be stored through the memory layout of CHWN, thereby realizing the storage of convolution kernel pixels in multiple convolution kernel combinations.
[0107] In this embodiment, when the number of convolution kernels is greater than the number of threads contained in each thread bundle, the multiple convolution kernels can also be grouped, and each convolution kernel combination can be stored in the memory according to the memory storage order corresponding to each group. At the same time, the convolution kernel pixels in the same convolution kernel combination can also be stored through the memory layout of CHWN, thereby further improving the continuity and locality of the convolution kernel memory access in the same thread bundle.
[0108] In one embodiment, Figure 5 As shown, step S104 may further include:
[0109] Step S501: store the input pixel stored in the first vector register in a scalar register.
[0110] Among them, the scalar register can be an SRF register, which can realize data sharing in the thread warp. Since multiple threads in the current thread warp need to share the input pixels in the current input image block, after storing the input pixels in the first vector register, the input pixels stored in the first vector register can be further sent to the scalar register, so that multiple threads in the current thread warp can share the input pixels through the scalar register.
[0111] Step S502, obtaining the current input pixel from the scalar register, and controlling the multiple threads included in the current thread bundle to read the convolution kernel pixel corresponding to each thread from the second vector register respectively;
[0112] Step S503: Perform convolution operation on the current input pixel and the convolution kernel pixel corresponding to each thread through each thread included in the current thread bundle to obtain the current output pixel of each output channel corresponding to the current input pixel.
[0113] The current input pixel refers to any input pixel stored in the scalar register. In this embodiment, the current thread bundle can read the input pixel of any current input image block stored in the scalar register as the current input pixel, and can also enable the multiple threads included in the current thread bundle to read the convolution kernel pixels corresponding to each thread from the second vector register respectively.
[0114] Afterwards, the multiple threads included in the current thread bundle can also perform convolution operations on the current input pixel shared in the current thread bundle and the convolution kernel pixel read by the thread, so as to obtain the output pixels of each output channel corresponding to the current input pixel as the current output pixel of each output channel.
[0115] For example, the current thread bundle may include multiple threads, namely thread A, thread B and thread C. Then thread A, thread B and thread C can respectively read the convolution kernel pixels contained in each convolution kernel from the second vector register. For example, thread A reads the convolution kernel pixels contained in convolution kernel A, thread B reads the convolution kernel pixels contained in convolution kernel B, and thread C reads the convolution kernel pixels contained in convolution kernel C, and uses the current input pixels shared in the current thread bundle to perform corresponding convolution operations, so as to obtain the output pixels corresponding to the current input pixels for each output channel as the current output pixels of each output channel. For example, thread A can perform convolution kernel operations on the convolution kernel pixels included in convolution kernel A and the current input pixel, so as to obtain the current output pixel for output channel A, while thread B can perform convolution kernel operations on the convolution kernel pixels included in convolution kernel B and the current input pixel, so as to obtain the current output pixel for output channel B, and thread C can perform convolution kernel operations on the convolution kernel pixels included in convolution kernel C and the current input pixel, so as to obtain the current output pixel for output channel C. In the above manner, the current output pixel corresponding to the current input pixel in each output channel can be obtained.
[0116] Step S504: obtaining an output image block of each output channel according to the current output pixel of each output channel.
[0117] After performing convolution operations on all current input pixels, the output pixels of each output channel corresponding to each input pixel can be obtained. After that, the output pixels in the same output channel can be arranged according to the arrangement order of the pixels to generate the output image block of each output channel.
[0118] In this embodiment, when performing a convolution operation, the input pixels corresponding to the current thread bundle can be stored in a scalar register, and the input pixels can be shared by each thread in the current thread bundle through the scalar register. At the same time, each thread can also be used to read the convolution kernel pixels of the corresponding convolution kernel, and the convolution kernel pixels and the current input pixels can be used to obtain the current output pixels of each output channel to form a corresponding output image block. In the above manner, the generation of the output image block is realized, and since the input can be shared in the scalar register, the repeated reading of the same input pixel can be avoided, thereby further improving the efficiency of the convolution operation.
[0119] In addition, after step S504, the method may further include: storing the output pixels contained in the output image blocks of each output channel into a third vector register according to the row order of each output pixel in the corresponding output image block.
[0120] Among them, the third vector register is a vector register for storing output pixels of the output image block. In this embodiment, after obtaining the output pixels of each output channel, the output pixels can also be stored in the third vector register, and the storage method adopts the row order of each output pixel in the corresponding output image block for storage, that is, the output pixels in the same row of the output image block are stored in the same third vector register. In this embodiment, the output pixels obtained by each thread are output pixels from different output channels, and at the same time, the memory layout of the output feature map stored in the memory can be the same as the NCHW memory layout of the input feature map. If the output pixels from different output channels are directly stored in the third vector register, it may cause the memory address to be discontinuous during storage. Therefore, in order to ensure the continuity of memory address storage, in this embodiment, the storage method of each output pixel in the third vector register can be changed, that is, stored according to the row order of the output pixels, so that the output pixels in the same row of the output image block can be stored in the third vector register in a continuous address manner. Then, when the NCHW storage of the memory is realized, the memory storage address can be made continuous, thereby improving the continuity of the memory address storage.
[0121] In this embodiment, after obtaining the output pixels, the output pixels can also be reordered and stored in the third vector register according to the row order of each output pixel in the corresponding output image block, thereby improving the continuity of the memory address during data storage and reducing the amount of memory access data.
[0122] In an application example, a convolution calculation method based on GPU is also provided, which optimizes the memory reading and writing in the convolution process. In order to achieve efficient data sharing and improve the locality of memory reading and writing, the following measures are adopted:
[0123] (1) Optimize the splitting and mapping of computing tasks to achieve fast data sharing based on registers;
[0124] (2) Control the calculation order of tasks by designing the number of work items NDRange;
[0125] (3) Split convolution kernel memory layout based on single instruction multiple data structure, and share convolution kernel data through on-chip local memory;
[0126] (4) Output data is reordered to achieve higher data continuity.
[0127] The process of this method can be as follows Figure 6 As shown, the following steps may be specifically included:
[0128] Step 1: Transfer the input feature map to the global memory of the image processing unit GPU.
[0129] Step 2: Transfer the convolution kernel to the global memory of the image processing unit GPU.
[0130] Step 3: Split and map computing tasks.
[0131] Specifically, the GPU hardware has several execution units, each of which can execute several thread bundles in parallel. Each thread bundle is executed in the form of a single instruction multiple data instruction. Taking the length of a single instruction multiple data instruction of 64 as an example, 64 parallel threads are executed simultaneously in a thread bundle.
[0132] In order to share the input feature map between different convolution kernels and avoid repeated reading of the input feature map, the task is split according to the following steps:
[0133] (1) Split each channel of the output feature map into m*n output image blocks, and those that are less than m*n are also considered as one block.
[0134] (2) In the channel direction, SIMD instructions are grouped according to their length, that is, every 64 consecutive channels are considered as a group, and channels less than 64 are also considered as 1 group.
[0135] (3) The 64 m*n small blocks at the same position in each group are assigned to a thread warp for calculation as a task block.
[0136] (4) Each thread in the thread bundle is responsible for the calculation task of the m*n image block of one channel in the task block.
[0137] Taking the block size m=4, n=16 as an example, the splitting method can be as follows Figure 7 As shown in the figure, the size of m*n can be set to 4*16, 2*16, 1*16 or 1*32. The larger the output block of each thread warp calculation, the higher the memory reuse rate, but the more registers are required. You can choose according to the actual hardware situation.
[0138] Step 4: Set the number of work items NDRange.
[0139] The work item number NDRange is an index space that describes the work items of computing tasks in OpenCL. It specifies the dimensions of global work items and the number of each dimension. Each work item corresponds to a thread in a warp, and a work group can consist of one or more warps.
[0140] When GPU splits tasks to construct thread bundles for execution, it will split them in a certain order. The computing tasks corresponding to each dimension of the number of work items can be set according to the splitting order, so as to achieve the purpose of controlling the execution order of computing tasks.
[0141] The output feature map dimensions are: [Batch, Channel, Height, Width], mapped to the 3D work item number NDRange as Figure 8 As shown, the workgroup size determines the number of work items in the group. The number of work items is aligned to SIMD_SIZE. SIMD_SIZE represents the length of a single instruction multiple data instruction. The size is set to [SIMD_SIZE*k, 1, 1]. A workgroup contains k thread warps, which contain k*SIMD_SIZE work items. k can be 2, 4, or 8. Taking k=4 as an example, the steps for GPU to split the number of work items NDRange are as follows:
[0142] (1) Splitting the first dimension, that is, Width / n*SIMD_SIZE, will generate Width / n thread bundles. Every k thread bundles form a work group, where Width represents the width of the output feature map.
[0143] (2) Split the second dimension Height / m, where Height represents the height of the output feature map.
[0144] (3) Split the third dimension Channel / SIMD_SIZE*Batch, where Channel represents the number of channels of the output feature map and Batch represents the batch size of the output feature map.
[0145] Taking the block size m = 4, n = 16 as an example, after constructing the thread warp and splitting the tasks according to the above steps, the tasks calculated by different thread warps are distributed on the output feature map as follows: Fig. 9As shown in the figure, through the above method, the thread bundles of all computing tasks of the first channel group for calculating the output feature map will be constructed first, and then the thread bundles of all computing tasks of the subsequent channel groups will be constructed in sequence. The advantages are: the thread bundles that rely on the same convolution kernel data block are constructed first, avoiding a large number of convolution kernels from entering the cache at the same time, and the convolution kernel reading time locality is better, and the cache hit rate is higher. The thread bundles in the work group rely on the same convolution kernel data, and the convolution kernel data can be shared through the on-chip local memory.
[0146] Step 5, read the feature map input block and share it.
[0147] Since the feature map adopts NCHW memory layout, for data reading continuity, when reading the feature map, SIMD_SIZE threads of the thread warp read a single channel data block of the feature map, and read it cyclically channel by channel during the convolution calculation process, and multiply it with the convolution kernel and accumulate it to the output register.
[0148] The convolution kernel size is K h *K w , the step length is S h , S w , each thread outputs m*n output image block calculation, and the input feature map data block to be read is:
[0149] [(m-1)*S h +K h , (n-1)*S w +K w ]
[0150] Each thread needs all the input block data to participate in the calculation during the convolution process with the convolution kernel. In order to reduce repeated data reading, we use shared registers to realize data sharing within the thread bundle. GPU hardware generally has several shared registers in each thread bundle. Each thread in the thread bundle can access this register, which is called scalar register SRF in our GPU. By putting the read input block data into the SRF register one by one and sharing it, all threads can access all input block data, which is more efficient than sharing through local memory.
[0151] To save vector registers, SIMD_SIZE threads read SIMD_SIZE / ((n-1)*S) each time. w +K w ) lines of data are saved to vector registers.
[0152] For example, if the convolution kernel is 3*3, the compensation is 1,1, m*n is 4*16, and SIMD_SIZE=64, a 6*18 input data block needs to be read. 64 threads read 3 rows of data each time. The first 3 rows of data are read and put into R24, and the second read of the remaining 3 rows of data is put into R25. Each thread reads only 1 data each time, such as Fig.10 shown.
[0153] Step 6: Read the convolution kernel and share it.
[0154] 1. Convolution kernel reading method and data sharing
[0155] According to the task splitting and mapping method, the data that each thread in each thread bundle needs to read is distributed on different convolution kernels. The reading method is as follows: Fig.11 As shown, each thread reads the data at the same position of different convolution kernels and puts them into the vector register to facilitate multiplication with the same input feature map data.
[0156] The convolution kernel uses a channel-by-channel cyclic reading method, reading a single channel K each time h *K w pixels, occupying K h *K w vector registers.
[0157] All work items in each work group can share data through on-chip local memory. All thread warps in the group rely on the same convolution kernel block, so the convolution kernel data can be stored in local memory for other thread warps to read, further reducing cache or memory read operations. The size of local memory that needs to be allocated to each work group is: SIMD_SIZE*K h *K w .
[0158] 2. Memory arrangement based on SIMD grouping.
[0159] From the convolution kernel reading method, we can know that SIMD_SIZE threads in a thread bundle read the pixel data at the same position of different convolution kernels respectively. If the convolution kernel adopts the original NCHW memory layout, when each thread reads its own convolution kernel, the memory address interval is C*H*W pixels. In order to improve the continuity and locality of convolution kernel memory access, the convolution kernel layout is converted according to the following steps:
[0160] (1) Based on the NCHW layout, N is grouped according to SIMD_SIZE size. N that is not SIMD_SIZE aligned needs to be padded with 0 to align with SIMD_SIZE.
[0161] (2) Each group performs memory layout conversion from nCHW (n=SIMD_SIZE) to CHWn.
[0162] After the conversion, the convolution kernel reading continuity and locality are better. Since the convolution kernel memory layout conversion can be completed offline in advance, it will not bring additional overhead. Taking N = 256 and SIMD_SIZE = 64 as an example, the conversion is as follows: Fig.12 shown.
[0163] Step 7: vectorize the convolution calculation.
[0164] The read feature map input data blocks are placed into the scalar register SRF one by one, and multiplied with the convolution kernel data stored in the vector register to realize the vectorized calculation of the convolution calculation in the output channel direction. The calculation steps are as follows:
[0165] (1) Read the single-channel Feature Map input block and convolution kernel data.
[0166] (2) Place the Feature Map data into the scalar register SRF one by one.
[0167] (3) The data placed in the SRF is multiplied by the corresponding convolution kernel and the result is accumulated into the output vector register.
[0168] (4) After the calculation of the channel is completed, the calculation of the next channel is executed in a loop until all channels are calculated.
[0169] (5) Obtain all output blocks of the output feature map.
[0170] Taking the convolution kernel as 3*3, compensation as 1,1, m*n as 4*16, and SIMD_SIZE=64 as an example, the vector multiplication diagram is as follows: Fig.13 shown.
[0171] Step 8: Output reordering.
[0172] 1. Output data original layout.
[0173] Take SIMD_SIZE=64, output block size 1*16, and output channel 64 as an example. Assume that the calculation results Out0~Out15 of a thread warp are stored in vector registers R0~R15. Fig.14 As shown:
[0174] Since the output pixels calculated by 64 threads in a thread bundle are located on different output channels, the output feature map uses the same NCHW memory layout as the input feature map. The memory addresses are discontinuous during storage, which increases cache pressure and memory write requests. This application example reorders the data to achieve better memory continuity during storage.
[0175] 2. Reorder
[0176] Data reordering can be achieved through on-chip local memory or vector register data exchange instructions. Using vector register data exchange instructions to reorder data can be more efficient. Different GPU hardware has different data exchange instructions, and the reordering implementation process will be very different. However, the final exchange result is certain. Taking the output block size of 1*16 and SIMD_SIZE=64 as an example, the exchange is performed according to the following steps:
[0177] (1) Divide the 64 threads R0 to R15 into 4 groups, each with 16 threads. The first group includes threads 0 to 15, and the second group includes threads 16 to 31.
[0178] (2) Each group exchanges data along the diagonal folding position to the corresponding thread position.
[0179] (3) Exchange the thread 0 data of the first group R0 to R15 to the thread 0 to thread 15 of R0.
[0180] (4) Exchange the thread 16 data of the second group R0-R15 to R0 thread 16-thread 31.
[0181] (5) Each subsequent group exchanges the first thread data along the diagonal folding position according to (2) and (3).
[0182] (6) The final R0 is as follows Fig.15 As shown, the memory addresses of every 16 consecutive threads are continuous and output to the memory.
[0183] (7) Repeat operations (2), (3), (4), and (5) for each group of second thread data in R0 to R15, and exchange them to each thread in R1. The exchange position is still the diagonal folding position.
[0184] (8) Repeat the above steps, and finally exchange all thread data from R0 to R15 and store them in the memory.
[0185] The diagram of data exchange between the first four threads in the first thread group is as follows: Fig.16 shown.
[0186] By exchanging and reordering data among the 64 threads in the warp, 16 or 32 pixel addresses can be kept continuous according to the output block size, which significantly improves the memory address continuity during data storage and reduces the amount of memory access data.
[0187] Since the width and height of the output feature map may not be aligned with the output block size m*n, boundary judgment and processing are required when storing data.
[0188] Through the above application examples, we can quickly share input data within the thread bundle through the SRF register, reducing the repeated reading of input data. Through the design of work items and the conversion of convolution kernel memory layout, computing tasks that depend on the same convolution kernel are prioritized for construction and execution, improving the temporal and spatial locality of the convolution kernel. At the same time, by sharing the convolution kernel through local memory, the probability of the same convolution kernel being repeatedly read from the memory is further reduced. In addition, when storing output feature map data, the continuity of the output data address is improved through the data exchange instruction within the thread bundle, reducing the number of memory write operations.
[0189] It should be understood that, although the various steps in the flowcharts involved in the above-mentioned embodiments are displayed in sequence according to the indication of the arrows, these steps are not necessarily executed in sequence according to the order indicated by the arrows. Unless there is a clear explanation in this article, the execution of these steps does not have a strict order restriction, and these steps can be executed in other orders. Moreover, at least a part of the steps in the flowcharts involved in the above-mentioned embodiments can include multiple steps or multiple stages, and these steps or stages are not necessarily executed at the same time, but can be executed at different times, and the execution order of these steps or stages is not necessarily to be carried out in sequence, but can be executed in turn or alternately with other steps or at least a part of the steps or stages in other steps.
[0190] Based on the same inventive concept, the embodiment of the present application also provides a feature graph processing device for implementing the feature graph processing method involved above. The implementation scheme for solving the problem provided by the device is similar to the implementation scheme recorded in the above method, so the specific limitations in one or more feature graph processing device embodiments provided below can refer to the limitations on the feature graph processing method above, and will not be repeated here.
[0191] In one embodiment, Fig.17 As shown, a feature map processing device is provided, including: an operation input determination module 1701, a thread warp acquisition module 1702, an operation input reading module 1703, an output image block acquisition module 1704 and a feature map output module 1705, wherein:
[0192] An operation input determination module 1701 is used to determine an input feature map for a convolution operation, a plurality of convolution kernels, and dimension information of an output feature map corresponding to the input feature map;
[0193] A thread warp acquisition module 1702 is used to acquire first size information of each output image block used to form an output feature map, and obtain multiple thread warps based on the first size information and dimension information;
[0194] The operation input reading module 1703 is used to obtain the input image blocks corresponding to each thread bundle and used to form the input feature map, read the corresponding input image blocks through each thread bundle, and read the corresponding multiple convolution kernels through the multiple threads included in each thread bundle;
[0195] The output image block acquisition module 1704 is used to control each thread warp to obtain the output image blocks of multiple output channels corresponding to each thread warp according to the input image block corresponding to the thread warp and the convolution kernels corresponding to the multiple threads included in the thread warp.
[0196] The feature map output module 1705 is used to obtain output feature maps of multiple output channels based on the output image blocks of multiple output channels corresponding to each thread warp.
[0197] In one embodiment, the dimension information includes: width information of the output feature map, and height information of the output feature map; first size information includes first width information of each output image block, and first height information of each output image block; the thread bundle acquisition module 1702 is further used to obtain the number of row image blocks for the output feature map based on the width information of the output feature map, and the first width information; obtain the number of column image blocks for the output feature map based on the height information of the output feature map, and the first height information; determine the number of output image blocks for the output feature map based on the number of row image blocks and the number of column image blocks, and construct multiple thread bundles corresponding to the number of output image blocks.
[0198] In one embodiment, the number of convolution kernels of the convolution kernel is greater than the number of threads contained in each thread bundle; the thread bundle acquisition module 1702 is further used to group multiple output channels according to the number of convolution kernels and the number of threads to obtain multiple output channel combinations; according to the output channel combination in which the output image blocks corresponding to each thread bundle are located, the construction order of each thread bundle is obtained; according to the construction order, each thread bundle is constructed; wherein the number of thread bundles of each thread bundle located in the same output channel combination is adapted to the number of output image blocks.
[0199] In one embodiment, the operation input reading module 1703 is further used to obtain the output image blocks corresponding to each thread bundle, determine the output feature map position corresponding to each output image block; obtain the second size information of each input image block according to the first size information of the output image block; obtain the input image block corresponding to each output image block according to the output feature map position and the second size information; obtain the input image blocks corresponding to each thread bundle according to the output image blocks corresponding to each thread bundle and the input image blocks corresponding to each output image block.
[0200] In one embodiment, the operation input reading module 1703 is further used to obtain the convolution kernel size information of the convolution kernel and the pre-set convolution step information; based on the first size information, the convolution kernel size information, and the convolution step information, the second size information is obtained.
[0201] In one embodiment, the input feature map is stored in the memory; the operation input reading module 1703 is further used to determine the current thread bundle and the current input image block corresponding to the current thread bundle; the current input image block is composed of multiple input pixels; through multiple threads included in the current thread bundle, each input pixel included in the current input image block is read from the memory according to the distribution order of the input pixels, and each input pixel is stored in the first vector register according to the distribution order.
[0202] In one embodiment, multiple convolution kernels are stored in a memory, and each convolution kernel carries multiple convolution kernel pixels; the operation input reading module 1703 is further used to read the convolution kernel pixels contained in the corresponding convolution kernel from the memory respectively through the multiple threads corresponding to the current thread bundle, and store the convolution kernel pixels in the second vector register; wherein, the convolution kernel pixels located in different convolution kernels and with the same convolution kernel position are stored in the same row of the second vector register.
[0203] In one embodiment, the feature map processing device further includes: a convolution kernel pixel storage module, which is used to store the convolution kernel pixels in the memory in a CHWN memory layout.
[0204] In one embodiment, the number of convolution kernels of the convolution kernel is greater than the number of threads of the multiple threads contained in each thread bundle; the convolution kernel pixel storage module is further used to group the multiple convolution kernels according to the number of convolution kernels and the number of threads to obtain multiple convolution kernel combinations; obtain the memory storage order corresponding to each convolution kernel combination, and store the multiple convolution kernel combinations in the memory according to the memory storage order, and store the convolution kernel pixels contained in the convolution kernels in the same convolution kernel combination in the memory in the memory layout of CHWN.
[0205] In one embodiment, the output image block acquisition module 1704 is used to store the input pixels stored in the first vector register in the scalar register; obtain the current input pixel from the scalar register, and control the multiple threads included in the current thread bundle to read the convolution kernel pixels corresponding to each thread from the second vector register respectively; perform convolution operations on the current input pixel and the convolution kernel pixels corresponding to each thread through each thread included in the current thread bundle to obtain the current output pixel of each output channel corresponding to the current input pixel; and obtain the output image blocks of each output channel according to the current output pixel of each output channel.
[0206] In one embodiment, the feature map processing device also includes: an output pixel reordering module, which is used to store the output pixels contained in the output image blocks of each output channel into a third vector register according to the row order of each output pixel in the corresponding output image block.
[0207] All or part of the above modules can be implemented by software, hardware and their combination. The above modules can be embedded in or independent of the processor in the computer device in the form of hardware, or stored in the memory of the computer device in the form of software, so that the processor can call and execute the operations corresponding to the above modules.
[0208] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties.
[0209] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to the memory, database or other medium used in the embodiments provided in the present application can include at least one of non-volatile and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetoresistive random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. As an illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM). The database involved in each embodiment provided in this application may include at least one of a relational database and a non-relational database. Non-relational databases may include distributed databases based on blockchains, etc., but are not limited to this. The processor involved in each embodiment provided in this application may be a general-purpose processor, a central processing unit, a graphics processor, a digital signal processor, a programmable logic device, a data processing logic device based on quantum computing, etc., but are not limited to this.
[0210] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0211] The above-described embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the present application. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the attached claims.
Claims
1. A feature map processing method, It is characterized in that The method comprises: Determine an input feature map for convolution operation, multiple convolution kernels, and dimension information of an output feature map corresponding to the input feature map; the input feature map is stored in a memory; Acquire first size information of each output image block used to constitute the output feature map, and obtain a plurality of thread warps based on the first size information and the dimension information; The method comprises: obtaining input image blocks corresponding to each thread bundle and used to form the input feature map, reading corresponding input image blocks through each thread bundle, and reading corresponding multiple convolution kernels through multiple threads included in each thread bundle; including: determining a current thread bundle and a current input image block corresponding to the current thread bundle; the current input image block is composed of multiple input pixels; reading each input pixel included in the current input image block from the memory according to the distribution order of the input pixels through multiple threads included in the current thread bundle, and storing each input pixel in the first vector register according to the distribution order; Controlling each of the thread warps to obtain output image blocks of multiple output channels corresponding to each of the thread warps according to an input image block corresponding to the thread warp and convolution kernels corresponding to multiple threads included in the thread warp; Based on the output image blocks of the multiple output channels corresponding to the respective thread warps, output feature maps of the multiple output channels are obtained; The dimension information includes: width information of the output feature map, and height information of the output feature map; the first size information includes first width information of each output image block, and first height information of each output image block; the multiple thread bundles are obtained based on the first size information and the dimension information; including: based on the width information of the output feature map, and the first width information, the number of row image blocks for the output feature map is obtained; based on the height information of the output feature map, and the first height information, the number of column image blocks for the output feature map is obtained; based on the number of row image blocks and the number of column image blocks, the number of output image blocks for the output feature map is determined, and multiple thread bundles corresponding to the number of output image blocks are constructed.
2. The method according to claim 1, It is characterized in that The number of convolution kernels of the convolution kernel is greater than the number of threads of the plurality of threads included in each of the thread warps; The constructing a plurality of thread warps adapted to the number of the output image blocks comprises: Grouping multiple output channels according to the number of convolution kernels and the number of threads to obtain multiple output channel combinations; Obtaining a construction order of each thread warp according to an output channel combination where an output image block corresponding to each thread warp is located; The thread warps are constructed according to the construction sequence; wherein the number of thread warps of the thread warps located in the same output channel combination is adapted to the number of the output image blocks.
3. The method according to claim 1, It is characterized in that The obtaining of input image blocks corresponding to the thread warps and used to form the input feature map includes: Obtain output image blocks corresponding to each thread warp, and determine the output feature map position corresponding to each output image block; Acquire second size information of each input image block according to the first size information of the output image block; Obtaining input image blocks corresponding to each output image block according to the output feature map position and the second size information; According to the output image blocks corresponding to the thread warps and the input image blocks corresponding to the output image blocks, the input image blocks corresponding to the thread warps are obtained.
4. The method according to claim 3, It is characterized in that The acquiring the second size information of each input image block according to the first size information of the output image block comprises: Obtaining convolution kernel size information of the convolution kernel and pre-set convolution step size information; The second size information is obtained based on the first size information, the convolution kernel size information, and the convolution step information.
5. The method according to claim 1, It is characterized in that The multiple convolution kernels are stored in a memory, and each convolution kernel carries multiple convolution kernel pixels; and the multiple threads included in each thread bundle read the corresponding multiple convolution kernels respectively, including: Through the multiple threads corresponding to the current thread bundle, the convolution kernel pixels contained in the corresponding convolution kernel are read from the memory respectively, and the convolution kernel pixels are stored in the second vector register; wherein, the convolution kernel pixels located in different convolution kernels and with the same convolution kernel position are stored in the same row of the second vector register.
6. The method according to claim 5, It is characterized in that Before respectively reading the convolution kernel pixels contained in the corresponding convolution kernel from the memory, the method further includes: The convolution kernel pixels are stored in the memory in a memory layout of CHWN.
7. The method according to claim 6, It is characterized in that The number of convolution kernels of the convolution kernel is greater than the number of threads of the plurality of threads included in each of the thread warps; The storing the convolution kernel pixels in the memory in a CHWN memory layout includes: According to the number of convolution kernels and the number of threads, multiple convolution kernels are grouped to obtain multiple convolution kernel combinations; Obtain a memory storage order corresponding to each convolution kernel combination, and store the multiple convolution kernel combinations in the memory according to the memory storage order, and store the convolution kernel pixels contained in the convolution kernels in the same convolution kernel combination in the memory in a CHWN memory layout.
8. The method according to claim 5, It is characterized in that The controlling each thread warp to obtain output image blocks of multiple output channels corresponding to each thread warp according to the input image block corresponding to the thread warp and the convolution kernels corresponding to the multiple threads included in the thread warp, comprises: storing the input pixel stored in the first vector register in a scalar register; Obtaining a current input pixel from the scalar register, and controlling a plurality of threads included in the current thread warp to read convolution kernel pixels corresponding to each thread from the second vector register respectively; Performing convolution operations on the current input pixel and convolution kernel pixels corresponding to the threads through the threads included in the current thread bundle to obtain current output pixels of the output channels corresponding to the current input pixel; According to the current output pixels of each output channel, an output image block of each output channel is obtained.
9. The method according to claim 8, It is characterized in that After obtaining the output image blocks of each output channel, the method further includes: The output pixels contained in the output image blocks of each output channel are stored in the third vector register according to the row order of each output pixel in the corresponding output image block.
10. A feature map processing device, It is characterized in that The device comprises: An operation input determination module, used to determine an input feature map for convolution operation, a plurality of convolution kernels, and dimension information of an output feature map corresponding to the input feature map; the input feature map is stored in a memory; A thread warp acquisition module, used to acquire first size information of each output image block used to form the output feature map, and obtain a plurality of thread warps based on the first size information and the dimension information; An operation input reading module, used to obtain the input image blocks corresponding to each thread bundle and used to form the input feature map, read the corresponding input image blocks through each thread bundle, and read the corresponding multiple convolution kernels through multiple threads included in each thread bundle; further used to determine the current thread bundle and the current input image block corresponding to the current thread bundle; the current input image block is composed of multiple input pixels; through the multiple threads included in the current thread bundle, read the input pixels included in the current input image block from the memory according to the distribution order of the input pixels, and store the input pixels in the first vector register according to the distribution order; An output image block acquisition module, used to control each thread warp to obtain output image blocks of multiple output channels corresponding to each thread warp according to the input image block corresponding to the thread warp and the convolution kernels corresponding to the multiple threads included in the thread warp; A feature map output module, configured to obtain output feature maps of multiple output channels based on output image blocks of multiple output channels corresponding to each of the thread warps; The dimensional information includes: width information of the output feature map, and height information of the output feature map; the first size information includes first width information of each output image block, and first height information of each output image block; the thread bundle acquisition module is further used to obtain the number of row image blocks for the output feature map based on the width information of the output feature map and the first width information; obtain the number of column image blocks for the output feature map based on the height information of the output feature map and the first height information; determine the number of output image blocks for the output feature map based on the number of row image blocks and the number of column image blocks, and construct multiple thread bundles corresponding to the number of output image blocks.
