Tensor structure transformation method and device, electronic equipment, storage medium and program product
By storing part of the tensor into memory with better read and write speed, the time overhead problem caused by memory handling in tensor structure transformation is solved, and the overall speed is improved.
Patent Information
- Application Number
- CN202410956379.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-16
- Publication Date
- 2024-11-01
AI Technical Summary
The prior art has a large time overhead due to memory handling during tensor structure transformation, which affects the overall speed.
By storing a part of the tensor to be transformed into a second memory with a read-write speed better than the original memory, and when the central processor responds to the structure transformation instructions, the other part is stored in a third memory with a better read-write speed, thereby reducing the Processing time.
The speed of tensor structure transformation is significantly improved, especially when the time overhead is reduced when large amounts of memory handling is involved.
Smart Images

Figure CN118886464A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of data processing, and in particular to a tensor structure transformation method, device, electronic equipment, storage medium and program product. Background Art
[0002] Tensor structure transformation is an operation that needs to be performed frequently during the training and application of neural network models.
[0003] Taking the tensor connection operation as an example, when connecting two tensors in the prior art, the central processing unit moves another tensor from the original storage space to the target storage space after completing the operation of moving one tensor from the original storage space to the target storage space.
[0004] Although the tensor structure transformation operation itself contains very little computational effort, when it involves a large amount of memory transfer, it will also cause a certain proportion of time overhead, making the overall completion speed of the tensor structure transformation slower. Summary of the invention
[0005] The present invention aims to solve at least one of the technical problems existing in the related art. To this end, the present invention proposes a tensor structure transformation method to improve the structure transformation speed of the tensor.
[0006] The present invention also provides a tensor structure transformation device, electronic equipment, storage medium and program product.
[0007] The tensor structure transformation method according to the first aspect of the present invention is applied to a central processing unit, and includes: receiving an instruction to perform a structural transformation on at least one target tensor in a first memory; In response to the instruction, extract a first part of all the target tensors and store them in a first position of the second memory, and store a second part of all the target tensors in a third memory, wherein the first part and the second part are determined from all the target tensors determined based on an instruction type of the instruction, and the reading and writing speed of the third memory is better than that of the first memory; The second part temporarily stored in the third memory is read and stored in the second position of the second memory.
[0008] According to the tensor structure transformation method of the embodiment of the present invention, while storing a part of the structure tensor to be transformed from the first memory to the second memory, by storing another part of the tensor to be transformed in a third memory having a better reading and writing speed than the first memory, the central processing unit can obtain the other part from the third memory having a better reading and writing efficiency, greatly reducing the time for processing the other part, thereby improving the overall structure transformation speed of the tensor to be transformed.
[0009] According to an embodiment of the present invention, the step of reading the second part temporarily stored in the third memory and storing it in the second position of the second memory includes: The sending of the second part from the first memory to the third memory is implemented based on a memory access controller working in parallel with the central processing unit.
[0010] According to an embodiment of the present invention, the third memory and the central processing unit are integrated on the same chip.
[0011] According to an embodiment of the present invention, when the third memory includes two sub-memories, the reading of the second part temporarily stored in the third memory and storing it in the second position of the second memory includes: Reading a second part temporarily stored in a sub-memory of the third memory, and storing it in a second position of the second memory; Reading a next second part temporarily stored in another sub-memory of the third memory, and storing it in another second position of the second memory; The second part is sent from the first memory to the sub-memory of the third memory during the process of sending the first part to the second memory; the next second part is sent from the first memory to another sub-memory of the third memory during the process of sending the second part to the second memory.
[0012] According to one embodiment of the present invention, if the total data volume of the target tensor is greater than twice the storage capacity of the third memory, the third memory includes at least two sub-memories with the same storage capacity.
[0013] According to one embodiment of the present invention, after extracting the first part of all the target tensors and storing them in the first position of the second memory, the method further includes: Obtaining a determination result of whether the second part temporarily stored in the sub-memory is complete; If so, reading the second portion and storing it in the second location of the second memory; If not, wait until the second part is completely temporarily stored in the third memory, then read the second part and store it in the second position of the second memory.
[0014] According to one embodiment of the present invention, extracting a first portion of all the target tensors and storing them in a first position of a second memory includes: Extracting a first portion of all the target tensors, and storing the first portion in at least two of the first locations of the second memory; Correspondingly, the second part temporarily stored in the third memory is read and stored in the second position of the second memory, including: Reading the second part temporarily stored in the third memory, and storing it in at least two of the second locations of the second memory; The storage address of any one of the first positions is adjacent to the storage addresses of two of the second positions respectively.
[0015] According to one embodiment of the present invention, before extracting the first part of all the target tensors and storing them in the first position of the second memory, the size of the first part is determined based on the product of the total data amount of the target tensor and the first division coefficient; The first division coefficient is determined based on a theoretical speed at which the central processing unit accesses the first memory, a theoretical speed at which the central processing unit accesses the third memory, and a first speed factor; The first speed factor is an influencing factor for reducing the instruction response speed of the central processing unit.
[0016] According to one embodiment of the present invention, before extracting the first part of all the target tensors and storing them in the first position of the second memory, determining the size of the first part based on the product of the total data amount of the target tensor and the second split coefficient; The second division coefficient is determined based on a theoretical speed at which the central processing unit accesses the first memory, a speed at which the memory access controller controls the second portion to be sent from the first memory to the third memory, and a second speed factor; The second speed factor is an impact factor that reduces the instruction response speed of the central processing unit due to the influence of the memory access controller.
[0017] According to one embodiment of the present invention, before extracting the first part of all the target tensors and storing them in the first position of the second memory, the method further includes: Determining the sizes of the first part and the second part based on the storage capacity of the sub-storage; The size of the second part is equal to the storage capacity of the sub-memory; The size of the first part is determined based on the product of the total data volume of the target tensor and a third division coefficient; the third division coefficient is determined based on the storage capacity of the sub-memory.
[0018] A tensor structure transformation device according to an embodiment of the second aspect of the present invention includes: An instruction receiving module, configured to receive an instruction for performing a structural transformation on at least one target tensor in the first memory; a first part extraction module, configured to extract a first part of all the target tensors and store it in a first position of a second memory in response to the instruction, and store a second part of all the target tensors in a third memory, wherein the first part and the second part are determined from all the target tensors determined based on an instruction type of the instruction, and the third memory has a better reading and writing speed than the first memory; The second part reading module is used to read the second part temporarily stored in the third memory and store it in the second position of the second memory.
[0019] According to an embodiment of the third aspect of the present invention, an electronic device includes a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the tensor structure transformation method described above is implemented.
[0020] According to the non-transitory computer-readable storage medium of the fourth aspect of the present invention, a computer program is stored thereon, and when the computer program is executed by a processor, the tensor structure transformation method as described in any one of the above is implemented.
[0021] A computer program product according to an embodiment of the fifth aspect of the present invention comprises a computer program, which, when executed by a processor, implements any of the tensor structure transformation methods described above.
[0022] The above one or more technical solutions in the embodiments of the present invention have at least one of the following technical effects: While storing a part of the structural tensor to be transformed from the first memory to the second memory, by storing another part of the tensor to be transformed in a third memory having a better reading and writing speed than the first memory, the central processing unit can obtain the other part from the third memory having a better reading and writing efficiency, thereby greatly reducing the time for processing the other part, thereby improving the overall structural transformation speed of the tensor to be transformed.
[0023] Additional aspects and advantages of the present invention will be given in part in the following description and in part will be obvious from the following description, or will be learned through practice of the present invention. BRIEF DESCRIPTION OF THE DRAWINGS
[0024] In order to more clearly illustrate the technical solutions in the embodiments of the present invention or related technologies, the drawings required for use in the embodiments or related technical descriptions are briefly introduced below. Obviously, the drawings described below are only 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.
[0025] Figure 1 It is one of the flow charts of the tensor structure transformation method provided by the present invention.
[0026] Figure 2 This is an example diagram of a tensor structure transformation in which the instruction is a Concat operator in the related art.
[0027] Figure 3 It is a schematic diagram of the tensor structure transformation process in which the instruction is the Concat operator in the related technology.
[0028] Figure 4 This is an example diagram of the reading and writing speeds of the first memory and the third memory provided by the present invention.
[0029] Figure 5 This is an example diagram of tensor concatenation in which the instruction provided by the present invention is the Concat operator.
[0030] Figure 6 This is an example diagram of tensor slicing where the instruction provided by the present invention is the Slice operator.
[0031] Figure 7 This is one of the effect schematic diagrams of the tensor structure transformation method provided by the present invention.
[0032] Figure 8 This is the second flow chart of the tensor structure transformation method provided by the present invention.
[0033] Fig. 9 This is the third flow chart of the tensor structure transformation method provided by the present invention.
[0034] Fig.10 It is a structural schematic diagram of the tensor structure transformation device provided by the present invention.
[0035] Fig.11 It is a structural schematic diagram of the electronic device provided by the present invention. DETAILED DESCRIPTION
[0036] The following embodiments of the present invention are described in further detail in conjunction with the accompanying drawings and examples. The following examples are used to illustrate the present invention, but are not intended to limit the scope of the present invention.
[0037] In the description of the embodiments of the present invention, it should be noted that the terms "center", "longitudinal", "lateral", "up", "down", "front", "back", "left", "right", "vertical", "horizontal", "top", "bottom", "inside", "outside" and the like indicate positions or positional relationships based on the positions or positional relationships shown in the accompanying drawings, and are only for the convenience of describing the embodiments of the present invention and simplifying the description, and do not indicate or imply that the devices or elements referred to must have a specific orientation, be constructed and operate in a specific orientation, and therefore cannot be understood as limitations on the embodiments of the present invention. In addition, the terms "first", "second", and "third" are used for descriptive purposes only and cannot be understood as indicating or implying relative importance.
[0038] In the description of the embodiments of the present invention, it should be noted that, unless otherwise clearly specified and limited, the terms "connected" and "connection" should be understood in a broad sense, for example, it can be a fixed connection, a detachable connection, or an integral connection; it can be a mechanical connection or an electrical connection; it can be a direct connection or an indirect connection through an intermediate medium. For ordinary technicians in this field, the specific meanings of the above terms in the embodiments of the present invention can be understood according to specific circumstances.
[0039] In the embodiments of the present invention, unless otherwise clearly specified and limited, the first feature being "above" or "below" the second feature may mean that the first and second features are in direct contact, or the first and second features are in indirect contact through an intermediate medium. Moreover, the first feature being "above", "above" and "above" the second feature may mean that the first feature is directly above or obliquely above the second feature, or simply means that the first feature is higher in level than the second feature. The first feature being "below", "below" and "below" the second feature may mean that the first feature is directly below or obliquely below the second feature, or simply means that the first feature is lower in level than the second feature.
[0040] In the description of this specification, the description with reference to the terms "one embodiment", "some embodiments", "example", "specific example", or "some examples" etc. means that the specific features, structures, materials or characteristics described in conjunction with the embodiment or example are included in at least one embodiment or example of the embodiment of the present invention. In this specification, the schematic representations of the above terms do not necessarily refer to the same embodiment or example. Moreover, the specific features, structures, materials or characteristics described may be combined in any one or more embodiments or examples in a suitable manner. In addition, those skilled in the art may combine and combine the different embodiments or examples described in this specification and the features of the different embodiments or examples, without contradiction.
[0041] Combine the following Figure 1-Figure 11 The tensor structure transformation method, device, electronic device, storage medium and program product of the present invention are described.
[0042] Figure 1 It is one of the flow charts of the tensor structure transformation method provided by the present invention, such as Figure 1 As shown, the tensor structure transformation method is applied to a central processing unit, including but not limited to the following steps 101 to 103.
[0043] Step 101: Receive an instruction to perform a structural transformation on at least one target tensor in a first memory.
[0044] The instruction is an operation instruction that performs a structural transformation on the structure of the tensor but does not change the tensor itself; the target tensor is the object of the central processor to respond to the instruction and perform the structural transformation operation; the first memory is a memory that stores the target tensor.
[0045] Taking the application field of neural networks as an example, the instructions for structural transformation of the target tensor include but are not limited to operator (operation) types such as the concat operator, the transpose operator, and the slice operator. These operators will not change the tensor itself, but will change the structure or shape of the tensor so that the tensor can be input into the next layer of the network model built based on convolutional neural networks (CNN), recurrent neural networks (RNN), etc.
[0046] Correspondingly, the target tensor involves both data fixedly stored in the memory and temporary variables generated during the training and application of the neural network. The space for storing data and temporary variables is also the first memory.
[0047] Optionally, the number of target tensors is at least one, and the actual number depends on the instruction type of the instruction for performing the structural transformation, and can be one or two or more.
[0048] When the instruction type of the instruction is a Concat operator for connecting two or more tensors along a certain dimension, the number of target tensors is two or more.
[0049] It can be understood that the first memory for storing two or more different target tensors can be either the same storage device or two or more different storage devices.
[0050] For example, if we have two tensors of shape [2, 3], they can be concatenated into a tensor of shape [4, 3] or [2, 6] using the Concat operator, depending on which dimension the concatenation is done along.
[0051] When the instruction type of the instruction is a Transpose operator for exchanging tensor dimensions, the number of target tensors is one.
[0052] For example, transpose a tensor of shape [2, 3] into a tensor of shape [3, 2].
[0053] When the instruction type of the instruction is a Slice operator that extracts a portion of data from a tensor, the number of target tensors is 1. By executing the tensor operator and specifying the starting position and size of the portion of variables to be extracted, a new tensor containing this portion of data can be returned.
[0054] Specifically, when transforming a data structure, the central processing unit first receives an instruction to perform a structural transformation on at least one target tensor stored in a first memory, and determines, based on the instruction, the specific type of data structure transformation to be performed, the number of target tensors, and the first memory in which the target tensors are stored.
[0055] Step 102: In response to the instruction, extract the first part of all the target tensors and store them in the first position of the second memory, and store the second part of all the target tensors in the third memory.
[0056] The first part and the second part are determined from all the target tensors determined based on the instruction type of the instruction, and the reading and writing speed of the third memory is better than that of the first memory.
[0057] The second memory is a storage space where the target tensor with a changed structure is stored after the central processor executes the structural transformation instruction. The first position is an address determined based on the instruction to perform structural transformation on the target tensor and stores the first part of the target tensor after structural transformation.
[0058] It can be understood that the first memory and the second memory can be two different storage devices or different storage spaces on the same storage device.
[0059] The reading and writing speed of the third memory is better than that of the first memory, that is, the speed at which the central processing unit accesses the third memory is faster than the speed at which the central processing unit accesses the first memory.
[0060] Specifically, after receiving an instruction to perform a structural transformation on at least one target tensor in a first memory, the central processing unit responds to the instruction, determines a first position of a second memory where the target tensor needs to be stored after the structural transformation, and stores a first part of the target tensor from the first memory to a first position of the second memory.
[0061] While the central processor is sending the first part to the second memory, the second part is sent from the first memory to a third memory having a reading and writing speed higher than that of the first memory.
[0062] For example, if the instruction type of the instruction is the Concat operator, and the target tensor A and the target tensor B need to be connected together, then the first part can be the target tensor A, and the second part can be the target tensor B. Alternatively, the first part can be the target tensor A and a part of the target tensor B, and the second part can be the remaining part of the target tensor B.
[0063] For another example, if the instruction type of the instruction is a Slice operator and the target tensor C needs to be split, then the first part can be a part of the target tensor C, and the second part is the remaining part of the target tensor C.
[0064] Step 103: Read the second part temporarily stored in the third memory and store it in the second position of the second memory.
[0065] The second part is sent from the first memory to the third memory during the process of the central processor sending the first part to the second memory.
[0066] The second location is determined based on the instruction to perform structure transformation on the target tensor and is an address for storing a second portion of the target tensor after the structure transformation.
[0067] Specifically, after the central processing unit completes extracting the first part of the target tensor and stores it in the first position of the second memory, the temporarily stored second part is read from the third memory and stored in the second position of the second memory.
[0068] By storing the first part of the target tensor from the first memory to the first position of the second memory and storing the second part from the first memory to the second position of the second memory, the target tensor stored in the second memory completes the structural transformation.
[0069] Optionally, the first memory is any one of memories such as pseudo static random access memory (PSRAM), dynamic random access memory (DRAM), etc.
[0070] Optionally, the second memory is any one of memories such as pseudo static random access memory (PSRAM) and dynamic random access memory (DRAM).
[0071] Optionally, the third memory is any one of memories such as Data Tightly Coupled Memory (DTCM) and Static Random Access Memory (SRAM).
[0072] Figure 2 This is an example diagram of a tensor structure transformation in which the instruction is a Concat operator in the related art, such as Figure 2As shown, tensor 0 and tensor 1 are respectively stored in the first memory. Now, if we want to connect tensor 0 and tensor 1 into tensor 2, then during the operation, we need to read tensor 0 from the first memory and write it to the first position of the second memory, and read tensor 1 from the first memory and write it to the second position of the second memory, so as to obtain tensor 2 and complete the structural connection transformation of the target tensor.
[0073] Although the tensor structure transformation operation itself contains very little computational effort, mainstream network model frameworks such as Tensorflow, Pytorch, MNN, and NCNN currently all use this method. When a large amount of memory is involved, it will also cause a certain proportion of time overhead.
[0074] Figure 3 is a schematic diagram of the tensor structure transformation process of the Concat operator in the related art, such as Figure 3 As shown, when the process of reading tensor 0 and tensor 1 from the first memory and writing them to the second memory cannot be avoided, the central processing unit needs to complete the step of reading tensor 0 from the first memory and writing it to the first position of the second memory, and then read tensor 0 from the first memory and write it to the second position of the second memory.
[0075] During the model inference process, tensors are generally stored in PSRAM (i.e., the first memory). PSRAM is a semiconductor memory that combines the characteristics of both DRAM and SRAM. PSRAM uses an SRAM interface that does not require refresh operations to simplify memory control, but its storage unit is still DRAM, which means that when reading data, it needs to perform pre-charging and sensing operations. Both operations will consume a certain amount of time, resulting in a reduction in access speed. In addition, since PSRAM needs to be refreshed regularly to maintain data integrity, this will also take up some time, further affecting its access speed.
[0076] Therefore, when the reading and writing speed of PSRAM is not ideal, the efficiency of the related technology in performing tensor structure transformation operations is relatively low.
[0077] In the present invention, after receiving the instruction of tensor structure transformation, the central processing unit will respond to the instruction and start the step of storing the first part of all target tensors to the first position of the second memory. After the step is started, the second part of all target tensors will be sent from the first memory to the third memory with a read / write speed better than the first memory. That is, while the first part of all target tensors is stored in the first position of the second memory, the second part of all target tensors is being sent from the first memory to the third memory with a read / write speed better than the first memory.
[0078] After the CPU completes the step of storing the first part of all target tensors in the second memory, the CPU reads the second part of all target tensors from the third memory to the second position of the second memory to complete the structural transformation of the target tensors.
[0079] In the case where the read and write speed of the third memory is better than that of the first memory, the speed at which the CPU reads the second part from the third memory and writes it to the second memory will be improved. When a large amount of memory transfer is involved, the time overhead will be significantly reduced.
[0080] It should be noted that, under the condition that the reading and writing speed of the third memory is ensured to be faster than that of the first memory, the present invention does not limit the specific selection of the third memory.
[0081] Figure 4 is an example diagram of the read and write speeds of the first memory and the third memory provided by the present invention, such as Figure 4 As shown, in one embodiment, the first memory is PSRAM with a read / write speed of 430MB / s, and the third memory is DTCM with a read / write speed of 8000MB / s. When the CPU extracts the first part of the target tensor at a speed of 430MB / s and writes it from PSRAM to the second memory ( Figure 4 During the process (not shown), the second part of the target tensor is written from PSRAM to DTCM. After that, the CPU can extract the second part of the target tensor from DTCM and write it to the second memory ( Figure 4 ), the speed is greatly improved.
[0082] The tensor structure transformation method provided by the present invention stores a part of the structure tensor to be transformed from the first memory to the second memory, and at the same time, stores another part of the tensor to be transformed into a third memory with a reading and writing speed better than the first memory. The central processing unit can obtain the other part from the third memory with a better reading and writing efficiency, greatly reducing the time for processing the other part, thereby improving the overall structural transformation speed of the tensor to be transformed.
[0083] Based on the above embodiment, as an optional embodiment, the extracting the first part of all the target tensors and storing them in the first position of the second memory includes: extracting the first part of all the target tensors, and storing the first part in at least two of the first positions of the second memory; Correspondingly, the reading of the second part temporarily stored in the third memory and storing it in the second position of the second memory includes: reading the second part temporarily stored in the third memory and storing it in at least two of the second positions of the second memory; The storage address of any one of the first positions is adjacent to the storage addresses of two of the second positions respectively.
[0084] Specifically, after the CPU extracts the first part of the target tensor from the first memory, the CPU divides the first part into at least two first sub-parts, which are respectively stored at the storage addresses of at least two first positions on the second memory. After completing the storage of the first part, the CPU reads the second part temporarily stored in the third memory, and divides the second part into at least two second sub-parts. For each second sub-part, it is stored at the storage address of the second position between two adjacent first positions.
[0085] Furthermore, a storage address of a first position is adjacent to storage addresses of two second positions, and a storage address of a second position is adjacent to storage addresses of two first positions. Thus, when the central processor stores the second part in its entirety to the second position of the second memory, the cross structure transformation between all target tensors is completed.
[0086] It can be understood that when a first sub-part and a second sub-part are located at the head or tail of the tensor obtained after the target tensor structure is transformed, their storage addresses are only adjacent to the storage address of a first position or the storage address of a second position.
[0087] Figure 5 This is an example diagram of tensor concatenation with the Concat operator as the instruction provided by the present invention, such as Figure 5 As shown, the target tensor includes two tensors, tensor 0 and tensor 1, stored in the first memory, wherein tensor 0 is the first part of all target tensors determined based on the Concat operator, and tensor 1 is the second part of all target tensors determined based on the Concat operator.
[0088] After receiving the Concat operator that cross-connects tensor 0 and tensor 1, the central processing unit responds to the Concat operator, extracts tensor 0 as the first part from the first memory, and divides tensor 0 into 8 first sub-parts, which are stored in 8 different first positions of the second memory respectively.
[0089] After completing the storage of tensor 0, the central processing unit reads tensor 1 temporarily stored in the third memory as the second part, and divides tensor 1 into 8 first sub-parts, which are stored in 8 different second locations of the second memory respectively.
[0090] Furthermore, the storage addresses of each first position and second position are cross-connected. Then, when the CPU completes storing tensor 1, the cross-connection of tensor 0 and tensor 1 is also completed.
[0091] The tensor structure transformation method provided by the present invention can achieve cross-connection between different target tensors by setting the storage address of each first position storing the first part and the storage address of each second position storing the second part to be cross-adjacent, thereby realizing the diversification of the splicing dimensions of the tensor structure.
[0092] Figure 6 This is an example diagram of tensor segmentation with the instruction Slice operator provided by the present invention, such as Figure 6 As shown, the target tensor is tensor 0 stored in the first memory, and the sub-parts a, b, c and d of the white part in tensor 0 are the first part of all target tensors determined by the central processor after receiving the Slice operator that slices tensor 1 from tensor 0, and the sub-parts e, f, g and h in the white part are the second part of all target tensors determined. It can be seen that the first part and the second part can be composed of several discontinuous sub-parts in the target tensor.
[0093] In the process of the central processing unit extracting the first part consisting of sub-parts a, b, c and d from the first memory and storing it in the second memory, sub-parts e, f, g and h are temporarily stored in the third memory as the second part. When the central processing unit completes storing the first part in the first position, the second part consisting of sub-parts e, f, g and h is read from the third memory and stored in the second position of the second memory, thereby realizing the separation of tensor 1 from tensor 0.
[0094] Based on the above embodiment, as an optional embodiment, before extracting the first part of all the target tensors and storing them in the first position of the second memory, the size of the first part is determined based on the product of the total data amount of the target tensor and the first division coefficient; The first division coefficient is determined based on a theoretical speed at which the central processing unit accesses the first memory, a theoretical speed at which the central processing unit accesses the third memory, and a first speed factor; The first speed factor is an influencing factor for reducing the instruction response speed of the central processing unit.
[0095] In the process of the central processing unit sending the first part to the second memory, the specific implementation means of sending the second part from the first memory to the third memory may affect the instruction response speed of the central processing unit. By considering the influencing factors that reduce the instruction response speed of the central processing unit, the sizes of the first part and the second part can be more accurately determined to further improve the structural transformation speed of the tensor to be transformed.
[0096] Specifically, after receiving and responding to the instruction of tensor structure transformation, the central processing unit first determines the size of the first part before starting to extract the first part of the target tensor and storing it in the first position of the second memory.
[0097] When determining the size of the first part, the first division coefficient is determined based on the influence factor of reducing the instruction response speed of the central processing unit, the theoretical speed of the central processing unit accessing the first memory, and the theoretical speed of the central processing unit accessing the third memory, and the size of the first part is further determined based on the total data volume of all target tensors and the product of the first division coefficient. The size of the second part is the difference between the total data volume of all target tensors and the size of the first part.
[0098] As an optional embodiment, an expression for determining the first division coefficient is as follows: ; in, is the first division coefficient, is the theoretical speed at which the CPU accesses the first memory, The theoretical speed of the CPU accessing the third memory. The first speed factor that reduces the instruction response speed of the central processing unit.
[0099] In this embodiment, it is assumed that the total data size of all target tensors is , in the first division coefficient is In the case of , the size of the second part is , and the total time taken by the CPU to complete processing of all target tensors is expressed as follows: ; in, is the total time consumed.
[0100] To find Smallest value, we need to , Take the derivative and set it to 0, then solve When the derivative of a function is 0, the function reaches its maximum or minimum extreme value.
[0101] So, yes The expression for the derivative is as follows: .
[0102] Set the derivative to 0 and solve to get the first division coefficient. expression.
[0103] The tensor structure transformation method provided by the present invention determines the size of the first part and the second part based on a first speed factor that reduces the instruction response speed of the central processing unit, the theoretical speed of the central processing unit accessing the first memory, and the theoretical speed of the central processing unit accessing the third memory, ensuring that when the central processing unit completes the step of sending the first part to the second memory, the second part has also been completely sent to the third memory, so that the time interval between the central processing unit processing the first part and the second part can be as short as possible, thereby further improving the overall structure transformation speed of the tensor to be transformed.
[0104] Based on the above embodiment, as an optional embodiment, the reading the second part temporarily stored in the third memory and storing it in the second position of the second memory includes: The sending of the second part from the first memory to the third memory is implemented based on a memory access controller working in parallel with the central processing unit.
[0105] The memory access controller refers to a controller that works in parallel with the central processing unit and controls the second part of all target tensors to be sent from the first memory to the third memory.
[0106] Specifically, while the central processing unit starts extracting the first part of all target tensors and storing them in the first position of the second memory, the memory access controller working in parallel with the central processing unit starts controlling the second part to be sent from the first memory to the third memory.
[0107] Optionally, the memory access controller is a direct memory access (DMA) or an integrated direct memory access controller (iDMA).
[0108] DMA and iDMA are technologies for moving data around in a computer, allowing hardware subsystems (such as graphics cards or network cards) to directly access system memory without going through the CPU.
[0109] Figure 7 This is one of the schematic diagrams of the effect of the tensor structure transformation method provided by the present invention. Figure 7 As shown, the first memory and the second memory are both PSRAM, the third memory is DTCM, tensor 0 is the first part of all target tensors, and tensor 1 is the second part of all target tensors. The area above the time axis shows the time consumed when copying tensor 0 and tensor 1 when only the CPU is used, and the area below the time axis shows the time consumed when copying tensor 0 and tensor 1 under the control of the CPU.
[0110] like Figure 7As shown, when the present invention uses the memory access controller to move tensor 1 to DTCM, although the memory access controller and the central processing unit usually share the system bus, when the memory access controller is performing data transmission, the central processing unit may need to wait for the access right to the bus, which may cause the data access speed of the central processing unit to decrease, thereby increasing the time for copying tensor 0 as shown in the timeline ab segment. However, since tensor 1 is moved to the DTCM with better read and write speed in advance through the memory access controller, the time for the central processing unit to process all target tensors (i.e., complete processing of tensor 0 and tensor 1) is improved as shown in the timeline cd segment.
[0111] Furthermore, when the present invention is applied to scenarios such as voice wake-up models, voice noise reduction models, and voice command word recognition, the tensor processing speed can be increased by up to 10.34%.
[0112] The tensor structure transformation method provided by the present invention, while the central processing unit stores the first part from the first memory to the second memory, is based on a memory access controller working in parallel with the central processing unit to store the second part from the first memory to the third memory. Without occupying the resources of the central processing unit, the data that the central processing unit needs to process next can be moved to the third memory with better reading and writing speed, thereby further improving the overall structure transformation speed of the tensor to be transformed.
[0113] Based on the above embodiment, as an optional embodiment, the third memory and the central processing unit are integrated on the same chip.
[0114] Optionally, the chip is equipped with a DTCM cache that can be used as a third memory, and the DSP (Digital Signal Processing) processor in the chip accesses the cache at a speed much higher than the speed of accessing the first memory such as PSRAM. While the iDMA memory access controller is used to send the second part to the DTCM cache, the DSP processor can perform other tasks, and the two have the characteristic of being performed in parallel.
[0115] The tensor structure transformation method provided by the present invention integrates the third memory and the central processing unit on the same chip. When processing tensors with smaller data volumes, it can avoid the transmission delay caused by the need to access peripherals, compared to the case where the third memory and the central processing unit are not integrated on the same chip.
[0116] Based on the above embodiment, as an optional embodiment, before extracting the first part of all the target tensors and storing them in the first position of the second memory, the size of the first part is determined based on the product of the total data amount of the target tensor and the second division coefficient; The second division coefficient is determined based on a theoretical speed at which the central processing unit accesses the first memory, a speed at which the memory access controller controls the second portion to be sent from the first memory to the third memory, and a second speed factor; The second speed factor is an impact factor that reduces the instruction response speed of the central processing unit due to the influence of the memory access controller.
[0117] Specifically, when the second part is sent from the first memory to the third memory based on the memory access controller working in parallel with the central processing unit, before the central processing unit starts to extract the first part, the size of the first part is determined based on the second speed factor that reduces the instruction response speed of the central processing unit due to the influence of the memory access controller, the theoretical speed of the central processing unit accessing the first memory, and the speed at which the memory access controller controls the second part to be sent from the first memory to the third memory. The size of the second part is the difference between the total data volume of all target tensors and the size of the first part.
[0118] In one embodiment, when the control of sending the second part from the first memory to the third memory is implemented based on the memory access controller, the total time consumed by the central processor to process all target tensors is The expression is as follows: ; The expression of the second division coefficient is as follows: ; in, is the second division coefficient, is the theoretical speed at which the CPU accesses the first memory, The second speed factor is the factor by which the CPU's instruction response speed is reduced by the memory access controller. controlling, by the memory access controller, a speed at which the second portion is sent from the first memory to the third memory, is the total data size of all target tensors.
[0119] The tensor structure transformation method provided by the present invention, when implementing the tensor structure transformation based on a memory access controller working in parallel with a central processing unit, determines the first part and the second part in combination with a second speed factor that reduces the instruction response speed of the central processing unit due to the influence of the memory access controller, so that the length of all target tensors when divided into the first part and the second part can reduce the adverse effect of the instruction response speed of the central processing unit being affected by the memory access controller, thereby further improving the overall structure transformation speed of the tensor to be transformed.
[0120] Based on the above embodiment, as an optional embodiment, if the total data volume of the target tensor is greater than twice the storage capacity of the third memory, the third memory includes at least two sub-memories with the same storage capacity.
[0121] The sub-memory can be obtained by evenly dividing the storage space of the same storage device as the third memory, or by adding a storage device with the same capacity as the sub-memory of the third memory.
[0122] Based on the above embodiment, as an optional embodiment, when the third memory includes two sub-memories, the second part temporarily stored in the third memory is read and stored in the second position of the second memory, including: Reading a second part temporarily stored in a sub-memory of the third memory, and storing it in a second position of the second memory; Reading a next second part temporarily stored in another sub-memory of the third memory, and storing it in another second position of the second memory; The second part is sent from the first memory to the sub-memory of the third memory during the process of sending the first part to the second memory; the next second part is sent from the first memory to another sub-memory of the third memory during the process of sending the second part to the second memory.
[0123] Figure 8 This is the second flow chart of the tensor structure transformation method provided by the present invention, such as Figure 8 As shown, the first memory and the second memory are both PSRAM, the third memory includes two sub-memories DTCM0 and DTCM1, part0 is the first part of all target tensors, and part1, part2, and part3 are the second parts of all target tensors.
[0124] When the CPU copies part0, iDMA copies part1 to DTCM0; after the CPU completes copying part0, it copies part1 from DTCM0; after iDMA completes copying part1, it copies part2 to DTCM1; after the CPU completes copying part1, it copies part2 from DTCM0, and so on, until the structural transformation of all target tensors is completed.
[0125] The tensor structure transformation method provided by the present invention, when the data amount of the target tensor that needs to be structurally transformed is large, performs batch processing by setting at least two third memories, and stores the second part in the third memory in turn based on the memory access controller, so that the central processing unit can continuously extract the pre-stored tensors from the third memory with excellent reading and writing speed.
[0126] Based on the above embodiment, as an optional embodiment, before extracting the first part of all the target tensors and storing them in the first position of the second memory, it also includes: Determining the sizes of the first part and the second part based on the storage capacity of the sub-storage; The size of the second part is equal to the storage capacity of the sub-memory; The size of the first part is determined based on the product of the total data volume of the target tensor and a third division coefficient; the third division coefficient is determined based on the storage capacity of the sub-memory.
[0127] Specifically, after receiving and responding to the instruction of tensor structure transformation, the central processing unit first determines the size of the first part and the second part based on the storage capacity of the sub-memory before starting to extract the first part of the target tensor and store it in the first position of the second memory. After determining the size of the second part as the storage capacity of the sub-memory, the third division coefficient is determined based on the storage capacity of the sub-memory, and the size of the first part is determined according to the product of the total data volume of the target tensor and the third division coefficient.
[0128] As an optional embodiment, the expression of the third division coefficient is as follows: ; in, is the third division coefficient, is the total data size of the target tensor, The storage capacity of the sub-memory.
[0129] The tensor structure transformation method provided by the present invention sets the size of the second part in the target tensor to be the same as that of any sub-memory when the amount of data to be processed is relatively large, exceeds the capacity of the third memory, and is processed in batches, and determines the size of the first part based on the capacity of the sub-memory, so as to maximize the use of the storage space of the sub-memory to further improve the processing speed of the tensor structure transformation when the amount of data is large.
[0130] Based on the above embodiment, as an optional embodiment, after extracting the first part of all the target tensors and storing them in the first position of the second memory, the method further includes: Obtaining a determination result of whether the second part temporarily stored in the sub-memory is complete; If so, reading the second portion and storing it in the second location of the second memory; If not, wait until the second part is completely temporarily stored in the third memory, then read the second part and store it in the second position of the second memory.
[0131] Optionally, whether the second part in the sub-memory is complete is determined by determining whether data is stored at an address on the sub-memory where the second part should be stored, or by determining whether the accumulated time for storing the second part in the third memory satisfies a market requirement determined based on the storage speed.
[0132] Fig. 9 This is the third flow chart of the tensor structure transformation method provided by the present invention, such as Fig. 9 As shown, in one embodiment, after extracting the first part tensor 0 of all the target tensors and storing it in the first position of the second memory, it is determined whether the second part tensor 1 has been completely temporarily stored in the third memory. If so, tensor 1 is copied from the third memory to the second position of the second memory to complete the tensor structure transformation.
[0133] The tensor structure transformation method provided by the present invention first determines whether the second part of the target tensor is complete in the third memory before extracting it from the third memory and storing it in the second memory, and only moves the data if it is complete, thereby ensuring data integrity.
[0134] Fig.10 is a schematic diagram of the structure of the tensor structure transformation device provided by the present invention, such as Fig.10 As shown, the tensor structure transformation device is integrated in a central processing unit, including but not limited to an instruction receiving module 1001, a first part extraction module 1002 and a second part reading module 1003 and other structures.
[0135] The instruction receiving module 1001 is used to receive an instruction to perform a structural transformation on at least one target tensor in the first memory.
[0136] The first part extraction module 1002 is used to respond to the instruction, extract the first part of all the target tensors and store them in the first position of the second memory, and store the second part of all the target tensors in the third memory, wherein the first part and the second part are determined from all the target tensors determined based on the instruction type of the instruction, and the reading and writing speed of the third memory is better than that of the first memory.
[0137] The second part reading module 1003 is used to read the second part temporarily stored in the third memory and store it in the second position of the second memory.
[0138] It should be noted that the tensor structure transformation device provided by the present invention can execute the tensor structure transformation method described in any of the above embodiments during specific operation, which will not be elaborated in this embodiment.
[0139] The tensor structure transformation device provided by the present invention stores a part of the structure tensor to be transformed from the first memory to the second memory, and at the same time, stores another part of the tensor to be transformed into a third memory with a reading and writing speed better than the first memory. The central processing unit can obtain the other part from the third memory with a better reading and writing efficiency, greatly reducing the time for processing the other part, thereby improving the overall structural transformation speed of the tensor to be transformed.
[0140] Fig.11 is a schematic diagram of the structure of the electronic device provided by the present invention, such as Fig.11 As shown, the electronic device may include: a processor 1110, a communication interface 1120, a memory 1130 and a communication bus 1140, wherein the processor 1110, the communication interface 1120 and the memory 1130 communicate with each other through the communication bus 1140. The processor 1110 may call the logic instructions in the memory 1130 to execute any of the above-mentioned tensor structure transformation methods applied to the central processing unit, and the tensor structure transformation method includes but is not limited to the following steps: receiving an instruction to perform a structural transformation on at least one target tensor in the first memory; in response to the instruction, extracting the first part of all the target tensors and storing them in the first position of the second memory, and storing the second part of all the target tensors in the third memory, wherein the first part and the second part are determined from all the target tensors determined based on the instruction type of the instruction, and the read and write speed of the third memory is better than that of the first memory; reading the second part temporarily stored in the third memory and storing it in the second position of the second memory.
[0141] In addition, the logic instructions in the above-mentioned memory 1130 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on such an understanding, the technical solution of the present invention is essentially or the part that contributes to the relevant technology or the part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (Read-Only Memory, ROM), random access memory (Random Access Memory, RAM), disk or optical disk and other media that can store program codes.
[0142] On the other hand, an embodiment of the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, is implemented to execute any of the above-mentioned tensor structure transformation methods applied to a central processing unit, the tensor structure transformation method including but not limited to the following steps: receiving an instruction to perform a structural transformation on at least one target tensor in a first memory; in response to the instruction, extracting a first part of all the target tensors and storing it in a first position of a second memory, and storing a second part of all the target tensors in a third memory, wherein the first part and the second part are determined from all the target tensors determined based on an instruction type of the instruction, and the reading and writing speed of the third memory is better than that of the first memory; reading the second part temporarily stored in the third memory and storing it in a second position of the second memory.
[0143] On the other hand, an embodiment of the present invention discloses a computer program product, which includes a computer program stored on a non-transitory computer-readable storage medium, and the computer program includes program instructions. When the program instructions are executed by a computer, the computer can execute any of the above-mentioned tensor structure transformation methods applied to a central processing unit, and the tensor structure transformation method includes but is not limited to the following steps: receiving an instruction to perform a structural transformation on at least one target tensor in a first memory; in response to the instruction, extracting a first part of all the target tensors and storing them in a first position of a second memory, and storing a second part of all the target tensors in a third memory, wherein the first part and the second part are determined from all the target tensors determined based on the instruction type of the instruction, and the reading and writing speed of the third memory is better than that of the first memory; reading the second part temporarily stored in the third memory and storing it in a second position of the second memory.
[0144] 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.
[0145] 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 relevant technology 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 embodiment.
[0146] Finally, it should be noted that the above embodiments are only used to illustrate the present invention, rather than to limit the present invention. Although the present invention is described in detail with reference to the embodiments, it should be understood by those skilled in the art that various combinations, modifications or equivalent substitutions of the technical solutions of the present invention do not depart from the spirit and scope of the technical solutions of the present invention, and should be included in the scope of the claims of the present invention.
Claims
1. A tensor structure transformation method, It is characterized in that Applied to CPU, including: receiving an instruction to perform a structural transformation on at least one target tensor in a first memory; In response to the instruction, extract a first part of all the target tensors and store them in a first position of the second memory, and store a second part of all the target tensors in a third memory, wherein the first part and the second part are determined from all the target tensors determined based on an instruction type of the instruction, and the reading and writing speed of the third memory is better than that of the first memory; The second part temporarily stored in the third memory is read and stored in the second position of the second memory.
2. The tensor structure transformation method according to claim 1, It is characterized in that The step of reading the second part temporarily stored in the third memory and storing it in the second position of the second memory includes: The sending of the second part from the first memory to the third memory is implemented based on a memory access controller working in parallel with the central processing unit.
3. The tensor structure transformation method according to claim 2, It is characterized in that The third memory and the central processing unit are integrated on the same chip.
4. The tensor structure transformation method according to claim 1, It is characterized in that In the case where the third memory includes two sub-memories, the reading of the second part temporarily stored in the third memory and storing it in the second position of the second memory includes: Reading a second part temporarily stored in a sub-memory of the third memory, and storing it in a second position of the second memory; Reading a next second part temporarily stored in another sub-memory of the third memory, and storing it in another second position of the second memory; The one second part is sent from the first memory to the one sub-memory of the third memory during the process of sending the first part to the second memory; the next second part is sent from the first memory to another sub-memory of the third memory during the process of sending the one second part to the second memory.
5. The tensor structure transformation method according to claim 4, It is characterized in that If the total data volume of the target tensor is greater than twice the storage capacity of the third memory, the third memory includes at least two sub-memories with the same storage capacity.
6. The tensor structure transformation method according to claim 4, It is characterized in that After extracting the first part of all the target tensors and storing them in the first position of the second memory, the method further includes: Obtaining a determination result of whether the second part temporarily stored in the sub-memory is complete; If so, reading the second portion and storing it in the second location of the second memory; If not, wait until the second part is completely temporarily stored in the third memory, then read the second part and store it in the second position of the second memory.
7. The tensor structure transformation method according to claim 1, It is characterized in that The extracting a first portion of all the target tensors and storing them in a first position of a second memory includes: Extracting a first portion of all the target tensors, and storing the first portion in at least two of the first locations of the second memory; Correspondingly, the second part temporarily stored in the third memory is read and stored in the second position of the second memory, including: Reading the second part temporarily stored in the third memory, and storing it in at least two of the second locations of the second memory; The storage address of any one of the first positions is adjacent to the storage addresses of two of the second positions respectively.
8. The tensor structure transformation method according to claim 1, It is characterized in that Before extracting the first part of all the target tensors and storing them in the first position of the second memory, determining the size of the first part based on the product of the total data amount of the target tensor and the first division coefficient; The first division coefficient is determined based on a theoretical speed at which the central processing unit accesses the first memory, a theoretical speed at which the central processing unit accesses the third memory, and a first speed factor; The first speed factor is an influencing factor for reducing the instruction response speed of the central processing unit.
9. The tensor structure transformation method according to claim 2, It is characterized in that Before extracting the first part of all the target tensors and storing them in the first position of the second memory, determining the size of the first part based on the product of the total data amount of the target tensor and the second division coefficient; The second division coefficient is determined based on a theoretical speed at which the central processing unit accesses the first memory, a speed at which the memory access controller controls the second portion to be sent from the first memory to the third memory, and a second speed factor; The second speed factor is an impact factor that reduces the instruction response speed of the central processing unit due to the influence of the memory access controller.
10. The tensor structure transformation method according to claim 5, It is characterized in that Before extracting the first part of all the target tensors and storing them in the first position of the second memory, the method further includes: Determining the sizes of the first part and the second part based on the storage capacity of the sub-storage; The size of the second part is equal to the storage capacity of the sub-memory; The size of the first part is determined based on the product of the total data volume of the target tensor and a third division coefficient; the third division coefficient is determined based on the storage capacity of the sub-memory.
11. A tensor structure transformation device, It is characterized in that Integrated in the CPU, including: An instruction receiving module, configured to receive an instruction for performing a structural transformation on at least one target tensor in the first memory; a first part extraction module, configured to extract a first part of all the target tensors and store it in a first position of a second memory in response to the instruction, and store a second part of all the target tensors in a third memory, wherein the first part and the second part are determined from all the target tensors determined based on an instruction type of the instruction, and the third memory has a better reading and writing speed than the first memory; The second part reading module is used to read the second part temporarily stored in the third memory and store it in the second position of the second memory.
12. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, It is characterized in that When the processor executes the program, the tensor structure transformation method according to any one of claims 1 to 10 is implemented.
13. A non-transitory computer-readable storage medium having a computer program stored thereon, It is characterized in that When the computer program is executed by a processor, the tensor structure transformation method according to any one of claims 1 to 10 is implemented.
14. A computer program product comprising a computer program, It is characterized in that When the computer program is executed by a processor, the tensor structure transformation method according to any one of claims 1 to 10 is implemented.
Citation Information
Patent Citations
Data processing method, deep learning model deployment method and device, and chip
CN116627357A
Operator execution method and device, equipment and storage medium
CN117808048A
Processing a space release command to free release space in a consistency group
US20180341405A1
