Memory multiplexing method based on operator, compiler, electronic equipment and storage medium
By using ring buffer technology in the processor to segment and splice the input data of single-layer and multi-layer operators, the problem of large data loading and calculation volume in the existing technology is solved, and the processing efficiency of the processor is improved.
Patent Information
- Application Number
- CN202410879015.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-02
- Publication Date
- 2024-10-29
AI Technical Summary
The existing operator calculation method results in a large amount of data loading and calculation in the processor, resulting in low processor processing efficiency, especially in the single-layer and multi-layer operator calculation processes where there are repeated data loading and repeated calculations. question.
Ring buffer technology is used to process input data blocks separately for single-layer and multi-layer operators. Through the segmentation and splicing of data slices and overlapping blocks, the data loading amount is reduced, and deduplication is performed in multi-layer operators to avoid duplication. calculate.
It effectively reduces the amount of data loading and calculation, improves the processing efficiency of the processor, and especially significantly reduces the problems of redundant calculation and repeated data loading in multi-layer operator calculations.
Smart Images

Figure CN118860641A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of neural network model compilation, and in particular to an operator-based memory reuse method, compiler, electronic device and storage medium. Background Art
[0002] The neural network model is composed of a series of operators, and the artificial intelligence (AI) chip is used to support the calculation of each operator in the neural network model. Since the input and output data volumes of the operators are large during the calculation, and the static random access memory (SRAM) in the AI chip has limited capacity, the input data needs to be segmented to obtain multiple input data blocks. In the calculation process of some operators (such as the convolution operator), there is data overlap.
[0003] The existing operator calculation methods include the following two methods: one is that for single-layer operators, the overlap part needs to be loaded repeatedly during the calculation process, resulting in a large amount of data loading; the other is that for multi-layer operators, the first-layer operator needs to load more overlap parts during the calculation process, and at the same time, for operators in other layers, the overlap parts also need to be calculated repeatedly. In addition, the more operator layers there are, the larger the overlap part loaded by the first-layer operator, and the more redundant calculations there are after the second-layer operator, resulting in a large amount of data loading and calculation. Both of the above methods have certain limitations, resulting in low processing efficiency of the processor. Summary of the invention
[0004] The present invention provides an operator-based memory reuse method, a compiler, an electronic device and a storage medium, which are used to solve the defect that the existing operator calculation method has certain limitations, resulting in low processing efficiency of the processor. The method adopts a ring buffer to effectively reduce the data loading amount for two different situations, single-layer operators and multi-layer operators, and then determines the first target output data or the second target output data. The whole process effectively improves the processing efficiency of the processor.
[0005] The present invention provides an operator-based memory reuse method, which is applied to a compiler. The method comprises the following steps.
[0006] Get the operator in the processor.
[0007] In the case where the operator is a single-layer operator, a first data block sequence corresponding to the first input data in the single-layer operator is obtained, where the first data block sequence includes multiple first data blocks; based on the multiple first data blocks and in combination with a ring buffer, first target output data corresponding to the first input data is determined.
[0008] In the case where the operator is a multi-layer operator, the multi-layer operators are traversed, and for the first-layer operator, the first output data corresponding to the second input data in the first-layer operator is determined based on the multiple second data blocks corresponding to the second input data in the first-layer operator in combination with the ring buffer; for the operators in other layers, the first output data corresponding to the operator in the previous layer is determined as the third input data in the operators in other layers; the second output data corresponding to the third input data is determined based on the multiple third data blocks corresponding to the third input data in combination with the ring buffer; the second output data corresponding to the operator in the last layer is determined as the second target output data corresponding to the second input data.
[0009] According to an operator-based memory multiplexing method provided by the present invention, there is an overlapping part between any two adjacent first data blocks among the multiple first data blocks, and the first target output data corresponding to the first input data is determined based on the multiple first data blocks in combination with a ring buffer, including: according to the first data block sequence, for the first first data block, according to the first first data block and the second first data block adjacent to the first first data block, a first data slice and a first overlapping block are determined, wherein the first overlapping block is the overlapping part between the first first data block and the second first data block, and the first data slice is the data of the first first data block except the first overlapping block; for other first data blocks except the first first data block and the last first data block, according to the other first data blocks, the next adjacent first data blocks to the other first data blocks , and the first target overlapping block corresponding to the previous adjacent first data block of the other first data blocks, determine the second data slice and the second overlapping block, wherein the second overlapping block is the overlapping part between the other first data blocks and the next adjacent first data block, and the second data slice is the data of the other first data blocks except the first target overlapping block and the second overlapping block; for the last first data block, determine the third data slice according to the last first data block and the second target overlapping block corresponding to the previous adjacent first data block of the last first data block, and the third data slice is the data of the last first data block except the second target overlapping block; determine the first target output data corresponding to the first input data according to the first data slice, the first overlapping block, all second data slices, all second overlapping blocks and the third data slice, in combination with the ring buffer.
[0010] According to an operator-based memory reuse method provided by the present invention, the first target output data corresponding to the first input data is determined according to the first data slice, the first overlapping block, all second data slices, all second overlapping blocks and the third data slice, in combination with the ring buffer, including: S1, loading the first data slice and the first overlapping block into the ring buffer in sequence, starting calculation from the starting position of the ring buffer, and determining the third output data according to the first data slice and the first overlapping block; S2, traversing all the second data slices and all the second overlapping blocks, loading the first second data slice and the first second overlapping block in sequence after the first overlapping block, and loading the first The starting position of the overlapping block is determined as the new starting position, and the calculation is started from the new starting position. The fourth output data is determined according to the first overlapping block, the first second data slice and the first second overlapping block, and the first second overlapping block is used as the new first overlapping block; S3, the second second data slice is used as the new first second data slice, and the second second overlapping block is used as the new first second overlapping block, and the above step S2 is repeated until the last second data slice and the last second overlapping block are traversed; S4, the first target output data corresponding to the first input data is determined according to the third output data, all the fourth output data and the output data corresponding to the third data slice.
[0011] According to an operator-based memory reuse method provided by the present invention, before determining the first output data corresponding to the previous layer operator as the third input data in the other layer operator, the method also includes: deduplicating the first output data corresponding to the previous layer operator to obtain new first output data; and determining the new first output data as the third input data in the other layer operator.
[0012] According to an operator-based memory multiplexing method provided by the present invention, the first target output data corresponding to the first input data is determined according to the third output data, all the fourth output data and the output data corresponding to the third data slice, including: S1, storing the third output data in the target memory; S2, when the first fourth output data among all the fourth output data is stored in the target memory, splicing the third output data and the first fourth output data to obtain first spliced data; and determining the first spliced data as the new third output data in the target memory; S3, determining the second fourth output data among all the fourth output data as the new first fourth output data, and repeating the above step S2 until the second spliced data is determined according to the last first spliced data and the last fourth output data; S4, when the output data corresponding to the third data slice is stored in the target memory, splicing the second spliced data and the output data corresponding to the third data slice to obtain the first target output data corresponding to the first input data.
[0013] According to an operator-based memory multiplexing method provided by the present invention, obtaining the first data block sequence corresponding to the first input data in the single-layer operator includes: obtaining the first input data in the single-layer operator; evenly dividing the first input data to obtain the multiple first data blocks; and constructing the first data block sequence based on the multiple first data blocks.
[0014] According to an operator-based memory multiplexing method provided by the present invention, the data arrangement format of the multiple first data blocks is a number-height-width-number-of-channels (NHWC) format.
[0015] The present invention also provides a compiler, comprising the following modules.
[0016] The acquisition module is used to obtain the operators in the processor.
[0017] A processing module, used for obtaining, when the operator is a single-layer operator, a first data block sequence corresponding to the first input data in the single-layer operator, the first data block sequence including multiple first data blocks; determining, based on the multiple first data blocks and in combination with a circular buffer, first target output data corresponding to the first input data; when the operator is a multi-layer operator, traversing the multi-layer operators, for the first-layer operator, determining, based on the multiple second data blocks corresponding to the second input data in the first-layer operator and in combination with the circular buffer, first output data corresponding to the second input data; for operators in other layers, determining the first output data corresponding to the operator in the previous layer as the third input data in the operators in other layers; determining, based on the multiple third data blocks corresponding to the third input data and in combination with the circular buffer, second output data corresponding to the third input data; and determining the second output data corresponding to the last-layer operator as the second target output data corresponding to the second input data.
[0018] The present invention also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, and also comprising a compiler, wherein the compiler generates the computer program, the compiler interacts with the processor, and the processor implements any of the operator-based memory reuse methods described above when executing the computer program.
[0019] The present invention also provides a non-transitory computer-readable storage medium on which a computer program is stored. The computer program is generated by a compiler, the compiler interacts with a processor, and when the computer program is executed by the processor, it implements any of the operator-based memory reuse methods described above.
[0020] The present invention also provides a computer program product, including a computer program, wherein the computer program is generated by a compiler, the compiler interacts with a processor, and when the computer program is executed by the processor, the computer program implements any of the above-mentioned operator-based memory reuse methods.
[0021] The operator-based memory reuse method, compiler, electronic device and storage medium provided by the present invention obtain an operator in a processor; when the operator is a single-layer operator, obtain a first data block sequence corresponding to the first input data in the single-layer operator, the first data block sequence includes multiple first data blocks; according to the multiple first data blocks, combined with a ring buffer, determine the first target output data corresponding to the first input data; when the operator is a multi-layer operator, traverse the multi-layer operators, for the first-layer operator, according to the multiple second data blocks corresponding to the second input data in the first-layer operator, combined with the ring buffer, determine the first output data corresponding to the second input data; for other-layer operators, determine the first output data corresponding to the previous-layer operator as the third input data in the other-layer operators; according to the multiple third data blocks corresponding to the third input data, combined with the ring buffer, determine the second output data corresponding to the third input data; determine the second output data corresponding to the last-layer operator as the second target output data corresponding to the second input data. This method uses a ring buffer to effectively reduce the amount of data loading for two different situations, single-layer operators and multi-layer operators, and then determines the first target output data or the second target output data, and the whole process effectively improves the processing efficiency of the processor. BRIEF DESCRIPTION OF THE DRAWINGS
[0022] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0023] Figure 1 It is a schematic diagram of a scenario of a single-layer convolution operator provided by the prior art.
[0024] Figure 2 It is a schematic diagram of a scene of overlapping data provided by the prior art.
[0025] Figure 3 It is a schematic diagram of a scenario of a multi-layer convolution operator provided by the prior art.
[0026] Figure 4 It is a flowchart of the operator-based memory reuse method provided by the present invention.
[0027] Figure 5 It is a schematic diagram of a scenario for determining data slices and overlapping blocks provided by the present invention.
[0028] Figure 6 It is a schematic diagram of a scenario for determining first target output data provided by the present invention.
[0029] Figure 7It is a schematic diagram of a scenario for removing redundant overlapping data provided by the present invention.
[0030] Figure 8 It is a schematic diagram of the structure of the compiler provided by the present invention.
[0031] Fig. 9 It is a structural schematic diagram of the electronic device provided by the present invention. DETAILED DESCRIPTION
[0032] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0033] It should be noted that the compiler involved in the embodiment of the present invention refers to an AI compiler; the processor involved in the embodiment of the present invention refers to an AI processor, and the AI processor supports a ring buffer.
[0034] In order to better understand the embodiments of the present invention, the background technology is first described in detail.
[0035] For the more common hardware structure in the industry, the hardware structure may include a computing unit, a vector register, an SRAM, and a double data rate (DDR) synchronous dynamic random access memory (SDRAM) (DDR SDRAM can be referred to as DDR). First, the input data in the DDR is loaded into the SRAM, and then the data in the SRAM is loaded into the vector register. Next, the data is taken from the vector register to the computing unit for calculation, and the calculation result is output to the vector register. Then, the data in the vector register is stored back to the SRAM, and finally the data in the SRAM is stored back to the DDR. During the whole process, the speed of reading and writing data from the DDR is lower than the speed of reading and writing data from the SRAM.
[0036] Taking the convolution operator as an example, first load the weight data (weight) and bias data (bias) from DDR into SRAM, then divide the input data (input) into multiple input data blocks (input block), load the input block into SRAM, and obtain the output data blocks (output block) corresponding to the multiple input blocks after calculation by the convolution operator. The output blocks obtained each time are spliced together to form the entire output data (output).
[0037] For example, Figure 1 , which is a schematic diagram of a scene of a single-layer convolution operator provided by the prior art. Figure 1 In the figure, the input is divided into multiple input blocks. The red-framed input block0 is the first input block, the green-framed input block1 is the second input block, the purple-framed input block2 is the third input block, and so on.
[0038] from Figure 1 As can be seen, first, the weight and bias are loaded from DDR to SRAM, and input block0 is loaded into SRAM. After the convolution operator is calculated, the output block0 corresponding to input block0 is obtained, and output block0 is stored back from SRAM to DDR. Then, the green frame input block1 is loaded into SRAM, and the output block1 corresponding to input block1 is obtained after the convolution operator is calculated. Output block1 is stored back from SRAM to DDR and spliced after output block0. Since the convolution operator corresponds to the convolution kernel size (kernel size) of 3x3 (or 5x5, 7x7), when the input is split, there is an overlapping data block between two adjacent input blocks. This data block is the overlap part of the two adjacent input blocks. Figure 1 In the figure, overlap0 is the overlap part between the red frame input block0 and the green frame input block1, and overlap1 is the overlap part between the green frame input block1 and the purple frame input block2.
[0039] For example, Figure 2As shown, it is a schematic diagram of the scene of the data overlap part provided by the prior art. For the convolution operator, the number of channels is set to 1, the kernel size is set to 3x3, the stride is set to 1, and the expansion rate is set to 1. Figure 2 In the figure, the blue frame input block1 and the yellow frame input block2 are two adjacent input data blocks, and the purple part is the convolution kernel.
[0040] from Figure 2 It can be seen that the output data block corresponding to the blue frame input block1 is output block1, and the last row of data of output block1 is obtained by convolving the last three rows of data of the blue frame input block1 with the kernel. The output data block corresponding to the yellow frame input block2 is output block2, and the first row of data of output block2 is obtained by convolving the last two rows of data of the blue frame input block1 and the next row of data with the kernel. Therefore, the yellow frame input block2, as the input data block of output block2, will overlap with the blue frame inputblock1 by two rows of data, and the overlapping part is the overlap.
[0041] Taking the convolution operator as an example, the existing operator calculation methods include the following two.
[0042] For a single-layer convolution operator, combined with Figure 1 After the input block is loaded into SRAM, each time a convolution layer is calculated, the calculation result is written back to DDR from SRAM. During the calculation process, the overlap part needs to be loaded repeatedly, which makes the data loading amount larger.
[0043] For multi-layer convolution operators, for example, Figure 3 , which is a schematic diagram of a scenario of a multi-layer convolution operator provided by the prior art. Figure 3 In the case of multi-layer convolution operator fusion, there are two layers of convolution operators, namely convolution1 operator and convolution2 operator. Figure 3As can be seen from the figure, after the input block is loaded into SRAM, it is necessary to calculate the intermediate results of multi-layer convolution and store them in SRAM, and finally write the results from SRAM back to DDR. Specifically, input block0 is loaded into SRAM and calculated by the convolution1 operator to obtain output1block0, which is stored in SRAM. Then output1 block0 is used as the input of the next layer of convolution2 operator. After calculation by the convolution2 operator, output2 block0 is obtained and stored in SRAM, and then output2 block0 is stored back in DDR. Next, inputblock1 is loaded into SRAM and calculated by the convolution1 operator to obtain output1 block1, which is stored in SRAM (the same SRAM buffer as output1 block0). Then output1block1 is used as the input of the next layer of convolution2 operator. After calculation by the convolution2 operator, output2block1 is obtained and stored in SRAM (the same SRAM buffer as output2 block0). Then output2 block1 is stored back in DDR and spliced after output2 block0, and so on. During the calculation process, the first-layer operator needs to load more overlap parts. At the same time, for operators in other layers, the overlap parts need to be calculated repeatedly. Moreover, the more operator layers there are, the larger the overlap parts loaded by the first-layer operators are, and the more redundant calculations are required after the second-layer operators, resulting in larger data loading and calculation amounts.
[0044] Both of the above methods have certain limitations, resulting in low processing efficiency of the processor.
[0045] To solve the above problems, an embodiment of the present invention provides an operator-based memory reuse method, a compiler, an electronic device and a storage medium. The method adopts a circular buffer to effectively reduce the data loading amount for two different situations: single-layer operators and multi-layer operators, and then determines the first target output data or the second target output data. The whole process effectively improves the processing efficiency of the processor.
[0046] The embodiments of the present invention are further described below.
[0047] like Figure 4, which is a flow chart of the operator-based memory reuse method provided by the present invention, and is applied to a compiler. The method includes the following steps 401 to 403.
[0048] Step 401: Obtain an operator in the processor.
[0049] It should be noted that the operators involved in the embodiments of the present invention refer to operators with overlap, such as convolution operators and pooling operators.
[0050] Step 402: When the operator is a single-layer operator, obtain a first data block sequence corresponding to the first input data in the single-layer operator, where the first data block sequence includes multiple first data blocks; determine the first target output data corresponding to the first input data based on the multiple first data blocks in combination with the ring buffer.
[0051] Among them, the ring buffer refers to a memory mode that requires hardware support. When a continuous block of memory is set as a ring buffer, when the hardware reads or writes to the last address of the memory, the address read or written by the hardware will automatically jump to the starting position of the ring buffer to continue the read and write operation. The jump process does not require software participation.
[0052] After obtaining the operator in the processor, if the operator is a single-layer operator, the compiler first obtains the first data block sequence corresponding to the first input data in the single-layer operator, and then determines the first target output data corresponding to the first input data based on the multiple first data blocks included in the first data block sequence and the circular buffer. During the whole process, the data loading amount is effectively reduced, thereby improving the processing efficiency of the processor.
[0053] In some embodiments, the compiler obtains a first data block sequence corresponding to first input data in a single-layer operator, which may include: the compiler obtains the first input data in the single-layer operator; the compiler evenly divides the first input data to obtain multiple first data blocks; the compiler constructs a first data block sequence based on the multiple first data blocks.
[0054] It should be noted that the segmentation involved in the embodiment of the present invention refers to re-dividing the input data into input data blocks, rather than actually separating the input data.
[0055] When the operator is a single-layer operator, the compiler first obtains the first input data in the single-layer operator, and then evenly divides the first input data to obtain multiple first data blocks, and constructs these multiple first data blocks into a first data block sequence, so as to subsequently determine the first target output data corresponding to the first input data.
[0056] In some embodiments, the data arrangement format of the plurality of first data blocks is the NHWC mode of number (Number, N) height (Height, H) width (Width, W) number of channels (Channel, C).
[0057] Exemplarily, in the process of uniformly splitting the first input data, the number of splits N and the height H of each split ensure the continuity of the data, where the N dimension is cut into 1, and the H dimension split size is determined by the SRAM size, ensuring that the multiple first data blocks after splitting, as well as the weight, bias and the first target output data can all be placed on the SRAM.
[0058] In some embodiments, there is an overlapping portion between any two adjacent first data blocks among the multiple first data blocks, and the compiler determines the first target output data corresponding to the first input data based on the multiple first data blocks in combination with the circular buffer, which may include: the compiler follows the first data block sequence, and for the first first data block, the compiler determines the first data slice and the first overlapping block based on the first first data block and the second first data block adjacent to the first first data block, wherein the first overlapping block is the overlapping portion between the first first data block and the second first data block, and the first data slice is the data of the first first data block except the first overlapping block; for the other first data blocks except the first first data block and the last first data block, the compiler determines the first data slice and the first overlapping block based on the other first data blocks and the next adjacent first data blocks to the other first data blocks , and the first target overlapping block corresponding to the previous adjacent first data block of other first data blocks, determine the second data slice and the second overlapping block, wherein the second overlapping block is the overlapping part between other first data blocks and the next adjacent first data block, and the second data slice is the data of other first data blocks except the first target overlapping block and the second overlapping block; for the last first data block, the compiler determines the third data slice according to the last first data block and the second target overlapping block corresponding to the previous adjacent first data block of the last first data block, and the third data slice is the data of the last first data block except the second target overlapping block; the compiler determines the first target output data corresponding to the first input data according to the first data slice, the first overlapping block, all second data slices, all second overlapping blocks and the third data slice in combination with the ring buffer.
[0059] Example 1, such as Figure 5 FIG. 1 is a schematic diagram of a scenario for determining data slices and overlapping blocks provided by the present invention. Figure 5 It can be seen that the compiler determines the first data slice, the first overlapping block, all second data slices, all second overlapping blocks and the third data slice according to the multiple first data blocks (input block0, inputblock1, input block2...) included in the first data block sequence.
[0060] Specifically, the compiler follows the first data block sequence and for the first first data block ( Figure 5 The compiler determines the overlapping portion between the first first data block and the second first data block, namely, the first overlapping block (overlap0), according to the first first data block and the second first data block adjacent to the first first data block (the green frame input block1), and determines the data of the first first data block other than the first overlapping block, namely, the first data slice (input slice0).
[0061] For the first data blocks other than the first first data block and the last first data block, Figure 5 Taking the green-framed input block1 in the example, for other first data blocks (green-framed input block1), the compiler determines the overlapping portion between other first data blocks and the next adjacent first data block, i.e., the second overlapping block (in this case, overlap1), and determines the data of other first data blocks other than the first target overlapping block and the second overlapping block, i.e., the second data slice (in this case, input slice1). For other first data blocks (purple-framed input block2, yellow-framed input block3…), and so on, multiple second data slices and multiple second overlapping blocks will be obtained.
[0062] For the last first data block ( Figure 5 (not specifically shown), the compiler determines a third data slice based on the last first data block and the second target overlapping block corresponding to the previous adjacent first data block of the last first data block, where the third data slice is the data of the last first data block excluding the second target overlapping block.
[0063] Next, the compiler may determine the first target output data corresponding to the first input data according to the first data slice, the first overlapping block, all second data slices, all second overlapping blocks and the third data slice in combination with the ring buffer.
[0064] In some embodiments, the compiler determines the first target output data corresponding to the first input data according to the first data slice, the first overlapping block, all the second data slices, all the second overlapping blocks and the third data slice in combination with the circular buffer, which may include: S1, the compiler loads the first data slice and the first overlapping block into the circular buffer in sequence, starts calculating from the starting position of the circular buffer, and determines the third output data according to the first data slice and the first overlapping block; S2, the compiler traverses all the second data slices and all the second overlapping blocks, loads the first second data slice and the first second overlapping block in sequence after the first overlapping block, and determines the starting position of the first overlapping block The compiler takes the second second data slice as the new first second data slice and the second second overlapping block as the new first overlapping block, and repeats the above step S2 until the last second data slice and the last second overlapping block are traversed; S4, the compiler determines the first target output data corresponding to the first input data according to the third output data, all the fourth output data and the output data corresponding to the third data slice.
[0065] The starting position is also called the start address.
[0066] Combined with Example 1 and Example 2, Figure 6 FIG. 1 is a schematic diagram of a scenario for determining the first target output data provided by the present invention. Figure 6 As can be seen from S1, the compiler loads the first data slice (input slice0) and the first overlapping block (overlap0) into the SRAM buffer in sequence. It should be noted that the SRAM buffer is set as a ring buffer. According to the first data slice (input slice0) and the first overlapping block (overlap0), from the starting position of the ring buffer ( Figure 6 The calculation starts from the starting position 1) in the middle, until the data of the entire red box input block 0 is calculated, and the third output data is determined.
[0067] Next, the compiler starts to traverse all the second data slices and all the second overlapping blocks. S2. After the compiler loads the first second data slice (input slice1) and the first second overlapping block (overlap1) into the first overlapping block (overlap0) in sequence, although there is no space behind the first overlapping block (overlap0), the SRAM buffer is set as a ring buffer. The SRAM storage unit will automatically jump to the front of the ring buffer to continue storing the first second data slice (input slice1) and the first second overlapping block (overlap1), and determine the starting position of the first overlapping block (overlap0) as the new starting position ( Figure 6 Starting position 2 in the program), according to the first overlapping block (overlap0), the first second data slice (input slice1) and the first second overlapping block (overlap1), the calculation starts from the new starting position until the data of the size of the entire green box input block1 is calculated, the fourth output data is determined, and the first second overlapping block (overlap1) is used as the new first overlapping block; S3, the compiler uses the second second data slice (input slice2) as the new first second data slice, and the second second overlapping block (overlap2) as the new first second overlapping block, and repeats the above step S2 until the last second data slice and the last second overlapping block are traversed, and multiple fourth output data will be obtained. Then, the compiler determines the output data corresponding to the third data slice. The process of determining the output data corresponding to the third data slice is the same as the process of determining the fourth output data, which will not be described in detail here.
[0068] In this way, S4, the compiler can determine the first target output data corresponding to the first input data according to the third output data, all the fourth output data and the output data corresponding to the third data slice.
[0069] During the entire process, except for the first data block, each subsequent calculation process reuses the previous overlapping block, so that each overlap part does not need to be loaded repeatedly. The ring buffer reuses the overlap to achieve memory reuse and effectively reduce the amount of data loaded.
[0070] In some embodiments, the compiler determines the first target output data corresponding to the first input data based on the third output data, all the fourth output data and the output data corresponding to the third data slice, which may include: S1, the compiler stores the third output data in the target memory; S2, when the first fourth output data among all the fourth output data is stored in the target memory, the compiler splices the third output data and the first fourth output data to obtain first spliced data; and determines the first spliced data as the new third output data in the target memory; S3, the compiler determines the second fourth output data among all the fourth output data as the new first fourth output data, and repeats the above step S2 until the second spliced data is determined based on the last first spliced data and the last fourth output data; S4, when the output data corresponding to the third data slice is stored in the target memory, the compiler splices the second spliced data with the output data corresponding to the third data slice to obtain the first target output data corresponding to the first input data.
[0071] Exemplarily, the target memory may be the DDR mentioned above.
[0072] In the process of determining the first target output data corresponding to the first input data, S1, the compiler first stores the third output data in the target memory; S2, when the first fourth output data among all the fourth output data is stored in the target memory, the compiler splices the first fourth output data after the third output data to obtain the first spliced data; and determines the first spliced data as the new third output data in the target memory; S3, the compiler determines the second fourth output data among all the fourth output data as the new first fourth output data, and repeats the above step S2 until the second spliced data is determined according to the last first spliced data and the last fourth output data. Specifically, the last fourth output data is spliced after the last first spliced data to obtain the second spliced data. S4, when the output data corresponding to the third data slice is stored in the target memory, the compiler splices the output data corresponding to the third data slice after the second spliced data to obtain the first target output data corresponding to the first input data.
[0073] Step 403: When the operator is a multi-layer operator, traverse the multi-layer operators, and for the first-layer operator, determine the first output data corresponding to the second input data based on multiple second data blocks corresponding to the second input data in the first-layer operator in combination with the circular buffer; for the operators in other layers, determine the first output data corresponding to the operator in the previous layer as the third input data in the operators in other layers; determine the second output data corresponding to the third input data based on multiple third data blocks corresponding to the third input data in combination with the circular buffer; determine the second output data corresponding to the operator in the last layer as the second target output data corresponding to the second input data.
[0074] Optionally, before determining the first output data corresponding to the second input data based on multiple second data blocks corresponding to the second input data in the first-layer operator in combination with the circular buffer, the method may also include: the compiler obtains a second data block sequence corresponding to the second input data in the first-layer operator, and the second data block sequence includes multiple second data blocks.
[0075] Optionally, the compiler obtains a second data block sequence corresponding to the second input data in the first-layer operator, which may include: the compiler obtains the second input data in the first-layer operator; the compiler evenly divides the second input data to obtain multiple second data blocks; and the compiler constructs a second data block sequence based on the multiple second data blocks.
[0076] Optionally, the data arrangement format of the plurality of second data blocks may be set to the NHWC method mentioned above.
[0077] Optionally, there is an overlap between any two adjacent second data blocks in the plurality of second data blocks. There is an overlap between any two adjacent third data blocks in the plurality of third data blocks.
[0078] After obtaining the operator in the processor, if the operator is a multi-layer operator, the compiler starts to traverse the multi-layer operator. The traversal process is as follows.
[0079] For the first-level operator, the compiler first obtains the second data block sequence corresponding to the second input data in the first-level operator, and then determines the first output data corresponding to the second input data based on the multiple second data blocks included in the second data block sequence and the circular buffer. The process by which the compiler determines the first output data is the same as the process by which the compiler determines the first target output data corresponding to the first input data, and will not be described in detail here.
[0080] For other layer operators, the first output data corresponding to the previous layer operator of the other layer operator is determined as the third input data in the other layer operator. For example, assuming that the other layer operator is the second layer operator, then the previous layer operator of the second layer operator is the first layer operator, and the output data (first output data) of the first layer operator is used as the input data (third input data) of the second layer operator. At this time, the third input data can be understood as inter-layer data, and then according to the multiple third data blocks corresponding to the third input data, combined with the ring buffer, the second output data corresponding to the third input data is determined. The process of the compiler determining the second output data is the same as the process of the compiler determining the first target output data corresponding to the first input data, and will not be described in detail here.
[0081] Next, the second output data corresponding to the last layer operator is determined as the second target output data corresponding to the second input data.
[0082] It should be noted that the second input data in the first-layer operator is loaded, and the third input data in the other-layer operators is calculated by the previous-layer operator.
[0083] During the whole process, the use of a ring buffer effectively reduces the amount of data loaded, thereby improving the processing efficiency of the processor.
[0084] In some embodiments, before determining the first output data corresponding to the previous layer operator as the third input data in other layer operators, the method may also include: the compiler deduplicates the first output data corresponding to the previous layer operator to obtain new first output data; and the compiler determines the new first output data as the third input data in other layer operators.
[0085] Among them, deduplication refers to the removal of redundant overlap data.
[0086] For multi-layer operators, in the process of deriving the input data corresponding to the overlap data from the last overlap data, the overlap data becomes larger as time goes forward. Figure 7 FIG. 1 is a schematic diagram of a scenario for removing redundant overlapping data provided by the present invention.
[0087] from Figure 7It can be seen that for multi-layer operators, before deduplication, overlap2 is part of the input data corresponding to the first row of output3, the input of overlap2 is overlap1, and the input of overlap1 is overlap0. It can be found that the overlap data becomes larger as it goes forward, so before calculation, redundant overlap data needs to be removed. Since overlap2 already exists, there is no need to repeat the calculation, and only the data corresponding to output2 needs to be calculated. The input of the first row of output2 needs to include overlap01, and other data in overlap1 except overlap01 is not required, so the actual overlap1 part becomes overlap01 after deduplication. The input data of the first row of output1 needs to include overlap00, so the data in overlap0 except overlap00 is not required, so the actual overlap part becomes overlap00 after deduplication. After deduplication, the larger overlap becomes a smaller overlap, which can effectively reduce the amount of data of multi-layer overlap.
[0088] The compiler deduplicates the first output data corresponding to the previous layer of operators of other layers, that is, removes redundant overlap data, obtains new first output data, and then determines the new first output data as the third input data of other layer operators. In the whole process, the amount of data of multiple layers of overlap is effectively reduced, and the subsequent data loading and calculation amount are reduced, thereby improving the processing efficiency of the processor.
[0089] In an embodiment of the present invention, an operator in a processor is obtained; when the operator is a single-layer operator, a first data block sequence corresponding to the first input data in the single-layer operator is obtained, and the first data block sequence includes multiple first data blocks; based on the multiple first data blocks, combined with a ring buffer, the first target output data corresponding to the first input data is determined; when the operator is a multi-layer operator, the multi-layer operators are traversed, and for the first-layer operator, based on the multiple second data blocks corresponding to the second input data in the first-layer operator, combined with the ring buffer, the first output data corresponding to the second input data is determined; for other-layer operators, the first output data corresponding to the previous-layer operator is determined as the third input data in the other-layer operators; based on the multiple third data blocks corresponding to the third input data, combined with the ring buffer, the second output data corresponding to the third input data is determined; the second output data corresponding to the last-layer operator is determined as the second target output data corresponding to the second input data. This method uses a ring buffer to effectively reduce the amount of data loading for two different situations, single-layer operators and multi-layer operators, and then determines the first target output data or the second target output data. The whole process effectively improves the processing efficiency of the processor.
[0090] The compiler provided by the present invention is described below. The compiler described below and the operator-based memory reuse method described above can be referenced to each other.
[0091] like Figure 8 , which is a schematic diagram of the structure of the compiler provided by the present invention, the compiler 80 includes the following acquisition module 801 and processing module 802.
[0092] The acquisition module 801 is used to acquire the operator in the processor.
[0093] Processing module 802 is used to obtain a first data block sequence corresponding to the first input data in the single-layer operator when the operator is a single-layer operator, and the first data block sequence includes multiple first data blocks; determine the first target output data corresponding to the first input data based on the multiple first data blocks in combination with the circular buffer; traverse the multi-layer operators when the operator is a multi-layer operator, and for the first-layer operator, determine the first output data corresponding to the second input data in the first-layer operator based on the multiple second data blocks corresponding to the second input data in the first-layer operator in combination with the circular buffer; for other-layer operators, determine the first output data corresponding to the previous-layer operator as the third input data in the other-layer operators; determine the second output data corresponding to the third input data based on the multiple third data blocks corresponding to the third input data in combination with the circular buffer; and determine the second output data corresponding to the last-layer operator as the second target output data corresponding to the second input data.
[0094] Optionally, there is an overlapping part between any two adjacent first data blocks in the multiple first data blocks. The processing module 802 is specifically used to determine, according to the first data block sequence, for the first first data block, a first data slice and a first overlapping block based on the first first data block and a second first data block adjacent to the first first data block, wherein the first overlapping block is the overlapping part between the first first data block and the second first data block, and the first data slice is the data of the first first data block except the first overlapping block; for other first data blocks except the first first data block and the last first data block, based on the other first data blocks, the next adjacent first data blocks to the other first data blocks, and the previous adjacent first data blocks corresponding to the other first data blocks The first target overlapping block is used to determine the second data slice and the second overlapping block, wherein the second overlapping block is the overlapping part between the other first data blocks and the next adjacent first data block, and the second data slice is the data of the other first data blocks excluding the first target overlapping block and the second overlapping block; for the last first data block, the third data slice is determined according to the last first data block and the second target overlapping block corresponding to the previous adjacent first data block of the last first data block, and the third data slice is the data of the last first data block excluding the second target overlapping block; the first target output data corresponding to the first input data is determined according to the first data slice, the first overlapping block, all the second data slices, all the second overlapping blocks and the third data slice in combination with the ring buffer.
[0095] Optionally, the processing module 802 is specifically used for S1, loading the first data slice and the first overlapping block into the circular buffer in sequence, starting calculation from the starting position of the circular buffer, and determining the third output data based on the first data slice and the first overlapping block; S2, traversing all the second data slices and all the second overlapping blocks, loading the first second data slice and the first second overlapping block into the first overlapping block in sequence, and determining the starting position of the first overlapping block as a new starting position, starting calculation from the new starting position, determining the fourth output data based on the first overlapping block, the first second data slice and the first second overlapping block, and using the first second overlapping block as the new first overlapping block; S3, using the second second data slice as the new first second data slice, and the second second overlapping block as the new first second overlapping block, and repeating the above step S2 until the last second data slice and the last second overlapping block are traversed; S4, determining the first target output data corresponding to the first input data based on the third output data, all the fourth output data and the output data corresponding to the third data slice.
[0096] Optionally, the processing module 802 is further used to deduplicate the first output data corresponding to the operator of the previous layer to obtain new first output data; and determine the new first output data as the third input data in the operator of the other layer.
[0097] Optionally, the processing module 802 is specifically used for S1, storing the third output data in the target memory; S2, when the first fourth output data among all the fourth output data is stored in the target memory, splicing the third output data and the first fourth output data to obtain first spliced data; and determining the first spliced data as the new third output data in the target memory; S3, determining the second fourth output data among all the fourth output data as the new first fourth output data, and repeating the above step S2 until the second spliced data is determined based on the last first spliced data and the last fourth output data; S4, when the output data corresponding to the third data slice is stored in the target memory, splicing the second spliced data and the output data corresponding to the third data slice to obtain the first target output data corresponding to the first input data.
[0098] Optionally, the processing module 802 is specifically used to obtain the first input data in the single-layer operator; evenly divide the first input data to obtain the multiple first data blocks; and construct the first data block sequence according to the multiple first data blocks.
[0099] Optionally, the data arrangement format of the multiple first data blocks is number-height-width-number-of-channels NHWC mode.
[0100] like Fig. 9 , which is a schematic diagram of the structure of an electronic device provided by the present invention, the electronic device includes the following memory 901, processor 902 and compiler 80.
[0101] The memory 901 is used to store computer programs that can be executed on the processor 902 .
[0102] The processor 902 is used to interact with the compiler 80 and execute computer programs.
[0103] The compiler 80 is used to generate a computer program and interact with the processor 902 to implement the operator-based memory reuse method provided by the above methods, the method comprising: obtaining an operator in the processor; when the operator is a single-layer operator, obtaining a first data block sequence corresponding to the first input data in the single-layer operator, the first data block sequence including multiple first data blocks; determining the first target output data corresponding to the first input data according to the multiple first data blocks in combination with the ring buffer; when the operator is a multi-layer operator, traversing the multi-layer operators, for the first-layer operator, according to the multiple second data blocks corresponding to the second input data in the first-layer operator, in combination with the ring buffer, determining the first output data corresponding to the second input data; for other-layer operators, determining the first output data corresponding to the previous-layer operator as the third input data in the other-layer operator; determining the second output data corresponding to the third input data according to the multiple third data blocks corresponding to the third input data in combination with the ring buffer; and determining the second output data corresponding to the last-layer operator as the second target output data corresponding to the second input data.
[0104] On the other hand, the present invention also provides a computer program product, which includes a computer program, which can be stored on a non-transitory computer-readable storage medium, the computer program is generated by a compiler, the compiler interacts with a processor, and the computer program is executed by the processor to implement the operator-based memory reuse method provided by the above methods, the method comprising: obtaining an operator in the processor; when the operator is a single-layer operator, obtaining a first data block sequence corresponding to the first input data in the single-layer operator, the first data block sequence including multiple first data blocks; according to the multiple first data blocks, combined with the ring buffer, determining the first input The first target output data corresponding to the data; when the operator is a multi-layer operator, traverse the multi-layer operators, for the first-layer operator, according to the multiple second data blocks corresponding to the second input data in the first-layer operator, combined with the ring buffer, determine the first output data corresponding to the second input data; for other layer operators, determine the first output data corresponding to the previous layer operator as the third input data in the other layer operator; according to the multiple third data blocks corresponding to the third input data, combined with the ring buffer, determine the second output data corresponding to the third input data; determine the second output data corresponding to the last layer operator as the second target output data corresponding to the second input data.
[0105] On the other hand, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, the computer program being generated by a compiler, the compiler interacting with a processor, and the computer program being implemented by the processor to execute the operator-based memory reuse method provided by the above-mentioned methods, the method comprising: obtaining an operator in the processor; when the operator is a single-layer operator, obtaining a first data block sequence corresponding to the first input data in the single-layer operator, the first data block sequence comprising a plurality of first data blocks; determining, based on the plurality of first data blocks and in combination with a ring buffer, a first target output corresponding to the first input data data; in the case where the operator is a multi-layer operator, traverse the multi-layer operators, and for the first-layer operator, determine the first output data corresponding to the second input data in the first-layer operator according to the multiple second data blocks corresponding to the second input data in the first-layer operator in combination with the ring buffer; for other-layer operators, determine the first output data corresponding to the previous-layer operator as the third input data in the other-layer operator; determine the second output data corresponding to the third input data according to the multiple third data blocks corresponding to the third input data in combination with the ring buffer; determine the second output data corresponding to the last-layer operator as the second target output data corresponding to the second input data.
[0106] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.
[0107] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.
[0108] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A memory reuse method based on an operator, characterized in that: Applied to a compiler, the method comprises: Get the operator in the processor; In the case where the operator is a single-layer operator, obtaining a first data block sequence corresponding to first input data in the single-layer operator, where the first data block sequence includes multiple first data blocks; determining first target output data corresponding to the first input data according to the multiple first data blocks in combination with a ring buffer; In the case where the operator is a multi-layer operator, the multi-layer operators are traversed, and for the first-layer operator, the first output data corresponding to the second input data in the first-layer operator is determined based on the multiple second data blocks corresponding to the second input data in the first-layer operator in combination with the ring buffer; for the operators in other layers, the first output data corresponding to the operator in the previous layer is determined as the third input data in the operators in other layers; the second output data corresponding to the third input data is determined based on the multiple third data blocks corresponding to the third input data in combination with the ring buffer; the second output data corresponding to the operator in the last layer is determined as the second target output data corresponding to the second input data.
2. The method according to claim 1, characterized in that There is an overlapping portion between any two adjacent first data blocks among the plurality of first data blocks, and determining the first target output data corresponding to the first input data according to the plurality of first data blocks in combination with the ring buffer includes: According to the first data block sequence, for a first first data block, a first data slice and a first overlapping block are determined according to the first first data block and a second first data block adjacent to the first first data block, wherein the first overlapping block is an overlapping portion between the first first data block and the second first data block, and the first data slice is data of the first first data block excluding the first overlapping block; For other first data blocks except the first first data block and the last first data block, determine a second data slice and a second overlapping block according to the other first data blocks, a first target overlapping block corresponding to the next adjacent first data block of the other first data blocks, and a previous adjacent first data block of the other first data blocks, wherein the second overlapping block is an overlapping portion between the other first data block and the next adjacent first data block, and the second data slice is data of the other first data blocks except the first target overlapping block and the second overlapping block; For the last first data block, determine a third data slice according to the last first data block and a second target overlapping block corresponding to a previous adjacent first data block of the last first data block, wherein the third data slice is data of the last first data block excluding the second target overlapping block; According to the first data slice, the first overlapping block, all second data slices, all second overlapping blocks and the third data slice, in combination with the ring buffer, first target output data corresponding to the first input data is determined.
3. The method according to claim 2, characterized in that The determining, according to the first data slice, the first overlapping block, all second data slices, all second overlapping blocks and the third data slice, in combination with the ring buffer, first target output data corresponding to the first input data includes: S1, sequentially loading the first data slice and the first overlapping block into the ring buffer, starting calculation from the start position of the ring buffer, and determining third output data according to the first data slice and the first overlapping block; S2, traversing all the second data slices and all the second overlapping blocks, loading the first second data slice and the first second overlapping block into the first overlapping block in sequence, and determining the starting position of the first overlapping block as a new starting position, starting calculation from the new starting position, determining fourth output data according to the first overlapping block, the first second data slice and the first second overlapping block, and using the first second overlapping block as a new first overlapping block; S3, using the second second data slice as the new first second data slice, and using the second second overlapping block as the new first second overlapping block, and repeating the above step S2 until the last second data slice and the last second overlapping block are traversed; S4. Determine first target output data corresponding to the first input data according to the third output data, all fourth output data, and output data corresponding to the third data slice.
4. The method according to claim 1, characterized in that: Before determining the first output data corresponding to the operator of the previous layer as the third input data in the operator of the other layer, the method further includes: Deduplication of the first output data corresponding to the operator in the previous layer is performed to obtain new first output data; The new first output data is determined as the third input data in the other layer operator.
5. The method according to claim 3, characterized in that: The determining, according to the third output data, all the fourth output data, and the output data corresponding to the third data slice, first target output data corresponding to the first input data comprises: S1, storing the third output data into a target memory; S2, when the first fourth output data among all the fourth output data is stored in the target memory, the third output data and the first fourth output data are spliced to obtain first spliced data; and the first spliced data is determined as the new third output data in the target memory; S3, determining the second fourth output data among all the fourth output data as the new first fourth output data, and repeating the above step S2 until the second spliced data is determined according to the last first spliced data and the last fourth output data; S4. When the output data corresponding to the third data slice is stored in the target memory, the second spliced data and the output data corresponding to the third data slice are spliced to obtain first target output data corresponding to the first input data.
6. The method according to any one of claims 1 to 5, characterized in that: The obtaining a first data block sequence corresponding to the first input data in the single-layer operator includes: Acquire the first input data in the single-layer operator; Evenly dividing the first input data to obtain the plurality of first data blocks; The first data block sequence is constructed according to the multiple first data blocks.
7. The method according to any one of claims 1 to 5, characterized in that: The data arrangement format of the plurality of first data blocks is a number-height-width-number-of-channels (NHWC) format.
8. A compiler, characterized in that include: The acquisition module is used to obtain the operators in the processor; A processing module, configured to, when the operator is a single-layer operator, obtain a first data block sequence corresponding to first input data in the single-layer operator, the first data block sequence including a plurality of first data blocks; determine first target output data corresponding to the first input data according to the plurality of first data blocks in combination with a ring buffer; and, when the operator is a multi-layer operator, traverse the multi-layer operators, and for a first-layer operator, determine first output data corresponding to the second input data according to a plurality of second data blocks corresponding to second input data in the first-layer operator in combination with the ring buffer; For operators in other layers, determining first output data corresponding to operators in a previous layer as third input data in the operators in the other layers; determining second output data corresponding to the third input data according to a plurality of third data blocks corresponding to the third input data and in combination with the ring buffer; The second output data corresponding to the last layer operator is determined as the second target output data corresponding to the second input data.
9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: It also includes a compiler, which generates the computer program, and the compiler interacts with the processor. When the processor executes the computer program, it implements the operator-based memory reuse method as described in any one of claims 1 to 7.
10. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: The computer program is generated by a compiler, the compiler interacts with a processor, and when the computer program is executed by the processor, the operator-based memory reuse method as described in any one of claims 1 to 7 is implemented.
Citation Information
Cited By
Operator calculation method, electronic equipment and storage medium
CN121188334A
Operator computation method, electronic device, and storage medium
CN121188334B
