Data storage method and device, electronic equipment and computer readable storage medium
By introducing low- and high-bit address allocation methods in memory dynamic allocation and automatically switching based on the current state of the idle blockchain list, the problem of difficult to effectively allocate large-size tensors is solved, and a more efficient memory utilization and storage success rate is achieved.
Patent Information
- Application Number
- CN202510029930.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-08
- Publication Date
- 2025-05-30
- Estimated Expiration
- 2045-01-08
AI Technical Summary
In big model business, due to the large tensor size, traditional memory dynamic allocation algorithms are difficult to effectively allocate memory, resulting in the total free memory space being unable to be utilized.
By introducing two allocation methods in dynamic memory allocation: starting from the low-bit address of the memory space and starting from the high-bit address, and automatically switching these allocation methods according to the status of the current free blockchain list, so as to start allocation from both ends of the memory space as much as possible.
This method can automatically balance the usage of low and high-bit addresses of memory space, increase the chance of large blocks of free memory space appearing in the middle, and thus improve the success rate of storing large-size tensors.
Smart Images

Figure CN120066981A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of data storage, and in particular to a data storage method, a data storage device, an electronic device, and a computer-readable storage medium. Background Art
[0002] Dynamic memory allocation is a common method in computer programming and is also widely used in deep learning. A typical application scenario is to allocate memory space for tensors in a computational graph. A computational graph is a basic processing method in computational algebra and is defined as a directed graph, where nodes represent variables and edges represent mathematical operations. The computational graph graphically represents the computational process, which can facilitate viewing the relationships between various tensors and the data flow direction, as Figure 1 shown.
[0003] The typical process of dynamic memory allocation is as follows: First, a memory space of a specified size is allocated for a certain tensor. The memory allocator searches for a qualified memory space and assigns the address of the corresponding space as the return value to the tensor. As the computational graph progresses, when the tensor has completed its calculation and is no longer needed by other parts, the memory space corresponding to the tensor will be released and recycled for use in the space allocation of other tensors.
[0004] Currently, the mainstream memory dynamic allocation algorithms include: First Fit, Next Fit, Best Fit, Worst Fit, etc. The processes of each algorithm are as follows:
[0005] First Fit algorithm: Connect the free partition chain in ascending order of addresses. When performing memory allocation, start searching sequentially from the head of the chain until a partition of a size that can meet the requirements is found. Then, according to the size required by the tensor, allocate memory from this partition, and still chain the remaining free partitions in the free partition chain.
[0006] Next Fit algorithm: Similar to the First Fit algorithm, but when allocating, it does not start searching from the head of the chain, but starts searching from the next partition of the free partition where memory was last allocated.
[0007] Best Fit algorithm: Sort the free partitions in the free partition chain in ascending order of their sizes to form a free partition chain. Each time, search from the head of the chain to find a suitable free partition to allocate memory for the job. In this way, the free partition found each time is the one closest to the size of the tensor to be allocated.
[0008] Worst Fit Algorithm: Contrary to the Best Fit Algorithm, the free partitions in the free partition chain are sorted in descending order according to their sizes to form a free partition chain. Each time space is allocated, it is only necessary to check whether the first free partition meets the requirements.
[0009] Correspondingly, the schematic representations of the free partition chains of several algorithms are as Figure 2 shown.
[0010] Furthermore, the characteristics of several algorithms are shown in the following table:
[0011]
[0012] With the explosion of large model services, the huge tensor sizes brought by large model algorithms have also posed new challenges to memory dynamic allocation algorithms. Due to the large tensor sizes, according to the aforementioned traditional methods, the following phenomena are likely to occur: Although the total free memory space is larger than the size of the tensor to be allocated, since the free memory may consist of multiple free fragmented spaces, it is impossible to perform the allocation. Summary of the Invention
[0013] In view of the above problems, embodiments of the present invention are proposed to provide a data storage method, a data storage device, an electronic device, and a computer-readable storage medium that overcome the above problems or at least partially solve the above problems.
[0014] Embodiments of the present invention disclose a data storage method, the method includes:
[0015] In response to an allocation instruction for a tensor to be stored, determine the current allocation method of the target memory space for storing the tensor to be stored; the current allocation method includes a first allocation method starting from the low address of the complete memory space, or a second allocation method starting from the high address of the complete memory space;
[0016] Search for the target memory space starting from the low address of the complete memory space using the first allocation method, or search for the target memory space starting from the high address of the complete memory space using the second allocation method;
[0017] When the search for the target memory space is successful, store the tensor to be stored in the target memory space.
[0018] In one or more embodiments, the determining the current allocation method of the target memory space for storing the tensor to be stored includes:
[0019] Obtain the current free block chain table corresponding to the complete memory space; the current free block chain table includes at least one free memory space block in the complete memory space;
[0020] Determine whether the starting address of the first free memory space block in the current free blockchain table is 0;
[0021] If so, determine that the current allocation method is the first allocation method.
[0022] In one or more embodiments, it further includes:
[0023] If the starting address of the first free memory space block in the current free blockchain table is not 0, determine whether the ending address of the last free memory space block in the current free blockchain table is the capacity boundary of the complete memory space;
[0024] If so, determine that the current allocation method is the second allocation method.
[0025] In one or more embodiments, it further includes:
[0026] If the ending address of the last free memory space block in the current free blockchain table is not the capacity boundary of the complete memory space, determine whether the historical allocation method of the complete memory space is the first allocation method;
[0027] If so, change the first allocation method to the second allocation method;
[0028] If not, change the historical allocation method to the first allocation method.
[0029] In one or more embodiments, the step of using the first allocation method to search for the target memory space starting from the low address of the complete memory space, or using the second allocation method to search for the target memory space starting from the high address of the complete memory space, includes:
[0030] Starting from the low address, search in the current free blockchain table corresponding to the complete memory space to see if there is a target free memory space block, or starting from the high address, search in the current free blockchain table corresponding to the complete memory space to see if there is a target free memory space block; the size of the target free memory space block is not less than the size of the tensor to be stored;
[0031] If the target free memory space block exists, use the target free memory space block as the target memory space.
[0032] In one or more embodiments, the step of storing the tensor to be stored into the target memory space includes:
[0033] Determine whether the size of the target free memory space block is equal to the size of the tensor to be stored;
[0034] If they are equal, store the tensor to be stored in the target free memory space block, and delete the target free memory space block from the current free memory block chain table to obtain an updated first target free memory block chain table;
[0035] If they are not equal, store the tensor to be stored in the target free memory space block, and update the current free memory block chain table based on the current allocation method to obtain an updated second target free memory block chain table.
[0036] In one or more embodiments, updating the current free memory block chain table based on the current allocation method to obtain an updated second target free memory block chain table includes:
[0037] If the current allocation method is the first allocation method, change the start address of the target free memory space block to the end address of the tensor to be stored to obtain an updated second target free memory block chain table;
[0038] If the current allocation method is the second allocation method, change the end address of the target free memory space block to the start address of the tensor to be stored to obtain an updated second target free memory block chain table.
[0039] Correspondingly, an embodiment of the present invention discloses a data storage device, and the device includes:
[0040] A determination module, configured to determine a current allocation method of a target memory space for storing the tensor to be stored in response to an allocation instruction for the tensor to be stored; the current allocation method includes a first allocation method starting from a low address of a complete memory space or a second allocation method starting from a high address of the complete memory space;
[0041] A search module, configured to search for the target memory space starting from the low address of the complete memory space using the first allocation method or search for the target memory space starting from the high address of the complete memory space using the second allocation method;
[0042] A storage module, configured to store the tensor to be stored in the target memory space when the search for the target memory space is successful.
[0043] In one or more embodiments, the determination module is specifically configured to:
[0044] Obtain a current free memory block chain table corresponding to the complete memory space; the current free memory block chain table includes at least one free memory space block in the complete memory space;
[0045] Determine whether the starting address of the first free memory space block in the current free blockchain table is 0;
[0046] If so, determine that the current allocation method is the first allocation method.
[0047] In one or more embodiments, the determining module is further specifically configured to:
[0048] If the starting address of the first free memory space block in the current free blockchain table is not 0, determine whether the ending address of the last free memory space block in the current free blockchain table is the capacity boundary of the complete memory space;
[0049] If so, determine that the current allocation method is the second allocation method.
[0050] In one or more embodiments, the determining module is further specifically configured to:
[0051] If the ending address of the last free memory space block in the current free blockchain table is not the capacity boundary of the complete memory space, determine whether the historical allocation method of the complete memory space is the first allocation method;
[0052] If so, change the first allocation method to the second allocation method;
[0053] If not, change the historical allocation method to the first allocation method.
[0054] In one or more embodiments, the searching module is specifically configured to:
[0055] Starting from the low address, search in the current free blockchain table corresponding to the complete memory space to find whether there is a target free memory space block, or, starting from the high address, search in the current free blockchain table corresponding to the complete memory space to find whether there is a target free memory space block; the size of the target free memory space block is not less than the size of the tensor to be stored;
[0056] If there is the target free memory space block, use the target free memory space block as the target memory space.
[0057] In one or more embodiments, the storage module includes:
[0058] A determining sub-module, configured to determine whether the size of the target free memory space block is equal to the size of the tensor to be stored;
[0059] The first processing sub-module is configured to, if it is equal, store the to-be-stored tensor in the target free memory space block, and delete the target free memory space block from the current free blockchain table to obtain an updated first target free blockchain table;
[0060] The second processing sub-module is configured to, if it is not equal, store the to-be-stored tensor in the target free memory space block, and update the current free blockchain table based on the current allocation method to obtain an updated second target free blockchain table.
[0061] In one or more embodiments, the second processing sub-module is specifically configured to:
[0062] If the current allocation method is the first allocation method, change the start address of the target free memory space block to the end address of the to-be-stored tensor to obtain an updated second target free blockchain table;
[0063] If the current allocation method is the second allocation method, change the end address of the target free memory space block to the start address of the to-be-stored tensor to obtain an updated second target free blockchain table.
[0064] Correspondingly, an embodiment of the present invention discloses an electronic device, including: a processor, a memory, and a computer program stored on the memory and capable of running on the processor. When the computer program is executed by the processor, each step of the data storage method embodiment described above is implemented.
[0065] Correspondingly, an embodiment of the present invention discloses a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, each step of the data storage method embodiment described above is implemented.
[0066] Embodiments of the present invention have the following advantages:
[0067] In response to an allocation instruction for a tensor to be stored, determine the current allocation method of the target memory space for storing the tensor to be stored; the current allocation method includes a first allocation method starting from the low - address of the complete memory space or a second allocation method starting from the high - address of the complete memory space; then search for the target memory space starting from the low - address of the complete memory space using the first allocation method or search for the target memory space starting from the high - address of the complete memory space using the second allocation method; when the target memory space is successfully found, store the tensor to be stored in the target memory space. In this way, by changing the allocation method, the low - address allocation and high - address allocation are alternately used as much as possible, so as to start the allocation from both ends of the memory space as much as possible, automatically balance the usage of the low - address and high - address of the entire memory space, and then increase the probability of a large free memory space block appearing in the middle, so as to improve the success rate of storing large - size tensors. Description of the Drawings
[0068] Figure 1 is a schematic structural diagram of a computational graph;
[0069] Figure 2 is a schematic diagram of the free partition linked list of the current memory dynamic allocation algorithm;
[0070] Figure 3 is a flowchart of the steps of an embodiment of the data storage method of the present invention;
[0071] Figure 4 is a schematic structural diagram of the free blockchain table of the present invention;
[0072] Figure 5 is a flowchart of determining the current allocation method of the present invention;
[0073] Figure 6 is a schematic diagram of the memory space allocation effect of the first - fit algorithm;
[0074] Figure 7 is a schematic diagram of the memory space allocation effect of the present invention;
[0075] Figure 8 is a structural block diagram of an embodiment of a data storage device of the present invention. Detailed Embodiments
[0076] To make the above - mentioned objects, features, and advantages of the present invention more obvious and understandable, the present invention will be further described in detail below with reference to the drawings and specific embodiments.
[0077] One of the core concepts of the embodiments of the present invention is to alternately use low - address allocation and high - address allocation as much as possible by changing the allocation method, so as to start allocation from both ends of the memory space as much as possible, automatically balance the use of low - address and high - address of the entire memory space, and then increase the probability of a large free memory space block appearing in the middle, so as to improve the success rate of storing large - size tensors.
[0078] Referring to Figure 3 , a step - flow diagram of an embodiment of a data storage method of the present invention is shown, which can be applied to a storage system and specifically may include the following steps:
[0079] Step 301, in response to an allocation instruction for a tensor to be stored, determine the current allocation method of the target memory space for storing the tensor to be stored; the current allocation method includes a first allocation method of starting allocation from the low - address of the complete memory space, or a second allocation method of starting allocation from the high - address of the complete memory space.
[0080] After the storage system obtains an allocation instruction for a tensor to be stored (for ease of description, denoted as "tensor to be stored"), it can determine the current allocation method (denoted as "current allocation method") that needs to be adopted before allocating memory space for the tensor to be stored.
[0081] Among them, the allocation method includes a first allocation method and a second allocation method. The first allocation method can be to start allocating memory space from the low - address of the complete memory space, and the second allocation method can be to start allocating memory space from the high - address of the complete memory; the complete memory space is all the memory space, and the target memory space is a part of the complete memory space. For example, if the head of the complete memory space is the low - address and the tail is the high - address, then the first allocation method is to start allocating memory space from the head, and the second allocation method is to start allocating memory space from the tail.
[0082] In the embodiments of the present invention, the determining the current allocation method of the target memory space for storing the tensor to be stored includes:
[0083] Obtain the current free memory block chain table corresponding to the complete memory space; the current free memory block chain table includes at least one free memory space block in the complete memory space;
[0084] Determine whether the starting address of the first free memory space block in the current free memory block chain table is 0;
[0085] If so, determine that the current allocation method is the first allocation method.
[0086] Specifically, the complete memory space has a corresponding free blockchain table, and the free blockchain table may include at least one free memory space block in the complete memory space. Refer to Figure 4 , which shows a schematic structural diagram of the free blockchain table. Among them, it is assumed that tensor 1 and tensor 2 are stored in the complete memory space, the memory space occupied by tensor 1 is B, and the memory space occupied by tensor 2 is D. Then, there are still three parts A, C, and E in the complete memory space that are not occupied. That is, A, B, C, D, and E together form the complete memory space, and A, C, and E are all free memory space blocks. Sorting them in ascending order of address can generate the free blockchain table.
[0087] If the allocation method is the first allocation method, it means that each free memory space block is allocated in the order of A, C, and E; if the allocation method is the second allocation method, it means that each free memory space block is allocated in the order of E, C, and A. That is to say, the free blockchain table in the embodiments of the present invention is a doubly linked list.
[0088] Based on this, after obtaining the allocation instruction, the free blockchain table at the current moment (denoted as the "current free blockchain table") can be obtained, and then it is determined whether the starting address of the first free memory space block in the current free blockchain table is 0. If so, it means that the starting address of the first free memory space block is the starting address of the memory space, and the lowest address of the memory space is not occupied. Therefore, it can be determined that the current allocation method is to start allocating from the low address, that is, the first allocation method.
[0089] In the embodiments of the present invention, it further includes:
[0090] If the starting address of the first free memory space block in the current free blockchain table is not 0, then determine whether the ending address of the last free memory space block in the current free blockchain table is the capacity boundary of the complete memory space;
[0091] If so, determine that the current allocation method is the second allocation method.
[0092] Specifically, if the starting address of the first free memory space block in the current free blockchain table is not 0, it means that the lowest address of the complete memory space has been occupied, and the first free memory space block is in the middle position of the complete memory space. Therefore, it can be further determined whether the ending address of the last free memory space block in the current free blockchain table is the capacity boundary of the complete memory space. If so, it means that the ending address of the last free memory space block is the ending address of the complete memory space, and the highest address of the complete memory space is not occupied. It can be determined that the current allocation method is to start allocating from the high address, that is, the second allocation method.
[0093] In an embodiment of the present invention, it further includes:
[0094] If the end address of the last free memory space block in the current free blockchain table is not the capacity boundary of the complete memory space, determine whether the historical allocation method of the complete memory space is the first allocation method;
[0095] If so, change the first allocation method to the second allocation method;
[0096] If not, change the historical allocation method to the first allocation method.
[0097] Specifically, if the end address of the last free memory space block in the current free blockchain table is not the capacity boundary of the complete memory space, it means that the highest address of the complete memory space has been occupied, and the last free memory space block is also in the middle of the complete memory space. Therefore, it can be further determined whether the allocation method used for the last memory space allocation (denoted as "historical allocation method") is the first allocation method. If the historical allocation method is the first allocation method, then change the first allocation method to the second allocation method, that is, determine the current allocation method as the second allocation method; if the historical allocation method is the second allocation method, then change the second allocation method to the first allocation method, that is, determine the current allocation method as the first allocation method. In this way, by changing the allocation method, low-address allocation and high-address allocation are alternately used as much as possible, so as to start allocating from both ends of the memory space as much as possible, thereby automatically balancing the use of low addresses and high addresses in the entire memory space, and further increasing the probability of a large free memory space block appearing in the middle, so as to improve the success rate of storing large-size tensors.
[0098] Referring to Figure 5 , a flowchart for determining the current allocation method is shown. Specifically, determine whether the start address of the first free memory space block in the current free blockchain table is 0. If so, it can be determined that the current allocation method is to start allocating from the low address, that is, the first allocation method.
[0099] If the start address of the first free memory space block in the current free blockchain table is not 0, then it can be further determined whether the end address of the last free memory space block in the current free blockchain table is the capacity boundary of the memory space. If so, it can be determined that the current allocation method is to start allocating from the high address, that is, the second allocation method.
[0100] If the end address of the last free memory space block in the current free blockchain table is not the capacity boundary of the memory space, then it is possible to further determine whether the historical allocation method used for the last memory space allocation is the first allocation method.
[0101] If the historical allocation method is the first allocation method, then change the first allocation method to the second allocation method, that is, determine the current allocation method as the second allocation method; if the historical allocation method is the second allocation method, then change the second allocation method to the first allocation method, that is, determine the current allocation method as the first allocation method.
[0102] Step 302, search for the target memory space starting from the low address of the complete memory space using the first allocation method, or search for the target memory space starting from the high address of the complete memory space using the second allocation method.
[0103] Since the current allocation method is one of the first allocation method and the second allocation method, after determining the current allocation method, it is possible to search for the memory space (denoted as "target memory space") for storing the tensor to be stored starting from the low address of the complete memory space using the first allocation method, or search for the target memory space starting from the high and low addresses of the memory space using the second allocation method.
[0104] In the embodiment of the present invention, the searching for the target memory space starting from the low address of the complete memory space using the first allocation method, or searching for the target memory space starting from the high address of the complete memory space using the second allocation method includes:
[0105] Starting from the low address, search in the current free blockchain table corresponding to the complete memory space to see if there is a target free memory space block, or starting from the high address, search in the current free blockchain table corresponding to the complete memory space to see if there is a target free memory space block; the size of the target free memory space block is not less than the size of the tensor to be stored.
[0106] If there is the target free memory space block, then use the target free memory space block as the target memory space.
[0107] Specifically, if the first allocation method is used, then it is possible to start from the low address and search in the current free blockchain table to see if there is a target free memory space block; if the second allocation method is used, then it is possible to start from the high address and search in the current free linked list to see if there is a target free memory space block.
[0108] Among them, the target free memory space block is a free memory space block in the current free blockchain table whose size is not less than the size of the tensor to be stored.
[0109] If there is a target free memory space block, then the target free memory space block can be used as the target memory space.
[0110] Step 303, when the search for the target memory space is successful, store the tensor to be stored in the target memory space.
[0111] If there is a target space memory space block, it means that the search for the target memory space is successful, and then the tensor to be stored can be stored in the target memory space.
[0112] In an embodiment of the present invention, the storing the tensor to be stored in the target memory space includes:
[0113] Determine whether the size of the target free memory space block is equal to the size of the tensor to be stored;
[0114] If they are equal, store the tensor to be stored in the target free memory space block, and delete the target free memory space block from the current free blockchain table to obtain an updated first target free blockchain table;
[0115] If they are not equal, store the tensor to be stored in the target free memory space block, and update the current free blockchain table based on the current allocation method to obtain an updated second target free blockchain table.
[0116] Specifically, when storing the tensor to be stored, it can also be determined whether the size of the target free memory space block is the same as the size of the tensor to be stored. If they are the same, then the tensor to be stored can be stored in the target free memory space block, and the current free blockchain table can be updated, that is, the target free memory space block is deleted from the current free blockchain table, so as to obtain an updated free blockchain table (denoted as "the first target free blockchain table").
[0117] If they are not the same, that is, the size of the tensor to be stored is greater than the size of the target free memory space block, then the tensor to be stored can be stored in the target free memory space block, and the current free blockchain table can be updated according to the current allocation method, so as to obtain an updated free blockchain table (denoted as "the second target free blockchain table").
[0118] In an embodiment of the present invention, the updating the current free blockchain table based on the current allocation method to obtain an updated second target free blockchain table includes:
[0119] If the current allocation method is the first allocation method, change the start address of the target free memory space block to the end address of the tensor to be stored, and obtain an updated second target free memory block list;
[0120] If the current allocation method is the second allocation method, change the end address of the target free memory space block to the start address of the tensor to be stored, and obtain an updated second target free memory block list.
[0121] Specifically, since the size of the tensor to be stored is larger than the size of the target free memory space block, after storing the tensor to be stored in the target free memory space block, there is still remaining space in the target free memory space block. At the same time, because the first allocation method and the second allocation method allocate memory space from both ends, different allocation methods result in different update methods for the target free memory space block.
[0122] Based on this, if the current allocation method is the first allocation method, then after storing the tensor to be stored in the target free memory space block, the start address of the target free memory space block can be changed to the end address of the tensor to be stored, thereby obtaining an updated second target free memory block list.
[0123] If the current allocation method is the second allocation method, then after storing the tensor to be stored in the target free memory space block, the end address of the target free memory space block can be changed to the start address of the tensor to be stored, thereby obtaining an updated second target free memory block list.
[0124] For ease of understanding, Figure 6 a schematic diagram of the memory space allocation effect of the first-fit algorithm is shown, Figure 7 and a schematic diagram of the memory space allocation effect of the embodiment of the present invention is shown.
[0125] Among them, it is assumed that the total capacity of the memory space is 16 basic units. For example, the basic unit can be GBytes, MBytes, etc.
[0126] In the first-fit algorithm, tensors 0 with a size of 1, tensor 1 with a size of 1.4, and tensor 2 with a size of 7.5 are sequentially stored in the memory space. At this time, there is still a free memory space block with a size of 6.1 in the memory space.
[0127] After releasing tensor 1 from the memory space, tensor 3 with a size of 2 is continued to be stored behind tensor 2, and then tensor 2 is released. At this time, there are still two free memory space blocks with sizes of 8.9 and 4.1 in the memory space.
[0128] Continuously store Tensor 4 with a size of 2 and Tensor 5 in sequence after Tensor 1. At this time, there are still two free memory space blocks with sizes of 4.9 and 4.1 in the memory space.
[0129] After releasing Tensor 4, there are still three free memory space blocks with sizes of 2, 4.9, and 4.1 in the memory space. At this time, it is necessary to store Tensor 6 with a size of 7.5. Although the total size of the remaining free memory space blocks in the memory space is 11, it is still impossible to store Tensor 6 with a size of 7.5, resulting in the failure to store Tensor 6.
[0130] In the embodiment of the present invention, since there is no tensor stored in the memory space, for Tensor 0, the first allocation method can be used to store Tensor 0 with a size of 1 at the beginning of the memory space.
[0131] For Tensor 1, since Tensor 0 has been stored at the beginning of the memory space, the second allocation method can be used to store Tensor 1 with a size of 1.4 at the end of the memory space.
[0132] Similarly, for Tensor 2, continue to use the first allocation method to store Tensor 2 with a size of 7.5 behind Tensor 0. After releasing Tensor 1, the second allocation method can be used to store Tensor 3 with a size of 2 at the end of the memory space.
[0133] After releasing Tensor 2, use the first allocation method to store Tensor 4 with a size of 2 behind Tensor 1. At this time, there is still a free memory space block with a size of 11 in the memory space.
[0134] Use the second allocation method to store Tensor 5 with a size of 2 in front of Tensor 3, and then release Tensor 4. At this time, there is still a free memory space block with a size of 11 in the memory space.
[0135] For Tensor 6 with a size of 7.5, use the first allocation method to store it behind Tensor 1. At this time, there is still a free memory space block with a size of 3.5 in the memory space.
[0136] Furthermore, after storing Tensor 1 in the memory space, the start address of the free memory space block can be changed to the end address of Tensor 1, that is, the start address of the free memory space block can be changed from "0" to "1", and the end address is still "15".
[0137] After storing Tensor 2 in the memory space, the end address of the free memory space block can be changed to the start address of Tensor 2. The end address of the free memory space block can be changed from "15" to "13.6", and the start address is still "1".
[0138] It should be noted thatFigure 7 Taking only the number of free memory space blocks as one for example, in practical applications, when releasing Tensor 4, if Tensor 3 is also released exactly (that is, Tensor 0 and Tensor 5 are stored in the memory space), then, before storing Tensor 6, the free blockchain table can include two free memory space blocks.
[0139] Moreover, since the life cycle of the tensor is uncertain, the time to release the tensor is also uncertain. Therefore, in the embodiments of the present invention, after obtaining the allocation instruction, it is necessary to determine the current allocation method in real time, so as to avoid the situation where the memory spaces at both ends are released but not allocated.
[0140] In the embodiments of the present invention, in response to an allocation instruction for a tensor to be stored, determine the current allocation method of the target memory space for storing the tensor to be stored; the current allocation method includes a first allocation method starting from the low address of the complete memory space, or a second allocation method starting from the high address of the complete memory space; then use the first allocation method to search for the target memory space starting from the low address of the complete memory space, or use the second allocation method to search for the target memory space starting from the high address of the complete memory space; when the target memory space is successfully found, store the tensor to be stored in the target memory space. In this way, by changing the allocation method, the low address allocation and the high address allocation are alternately used as much as possible, so as to start the allocation from both ends of the memory space as much as possible, so as to automatically balance the use of the low address and the high address of the entire memory space, and then increase the probability of a large free memory space block appearing in the middle, so as to increase the success rate of storing large-size tensors.
[0141] It should be noted that for the method embodiments, for the sake of simple description, they are all expressed as a series of action combinations. However, those skilled in the art should know that the embodiments of the present invention are not limited by the described action sequence, because according to the embodiments of the present invention, certain steps can be performed in other sequences or simultaneously. Secondly, those skilled in the art should also know that the embodiments described in the specification are all preferred embodiments, and the actions involved are not necessarily essential to the embodiments of the present invention.
[0142] Refer to Figure 8 , which shows the structural block diagram of an embodiment of a data storage device of the present invention, and specifically may include the following modules:
[0143] Determination module 801, configured to determine the current allocation method of the target memory space for storing the tensor to be stored in response to an allocation instruction for the tensor to be stored; the current allocation method includes a first allocation method starting from the lower address of the complete memory space, or a second allocation method starting from the higher address of the complete memory space;
[0144] Search module 802, configured to search for the target memory space starting from the lower address of the complete memory space using the first allocation method, or search for the target memory space starting from the higher address of the complete memory space using the second allocation method;
[0145] Storage module 803, configured to store the tensor to be stored in the target memory space when the search for the target memory space is successful.
[0146] In an embodiment of the present invention, the determination module is specifically configured to:
[0147] Obtain the current free blockchain table corresponding to the complete memory space; the current free blockchain table includes at least one free memory space block in the complete memory space;
[0148] Determine whether the start address of the first free memory space block in the current free blockchain table is 0;
[0149] If so, determine that the current allocation method is the first allocation method.
[0150] In an embodiment of the present invention, the determination module is specifically further configured to:
[0151] If the start address of the first free memory space block in the current free blockchain table is not 0, determine whether the end address of the last free memory space block in the current free blockchain table is the capacity boundary of the complete memory space;
[0152] If so, determine that the current allocation method is the second allocation method.
[0153] In an embodiment of the present invention, the determination module is specifically further configured to:
[0154] If the end address of the last free memory space block in the current free blockchain table is not the capacity boundary of the complete memory space, determine whether the historical allocation method of the complete memory space is the first allocation method;
[0155] If so, change the first allocation method to the second allocation method;
[0156] If not, change the historical allocation method to the first allocation method.
[0157] In an embodiment of the present invention, the searching module is specifically configured to:
[0158] Starting from the low - order address, search in the current free blockchain table corresponding to the complete memory space to check if there is a target free memory space block, or starting from the high - order address, search in the current free blockchain table corresponding to the complete memory space to check if there is a target free memory space block; the size of the target free memory space block is not less than the size of the tensor to be stored.
[0159] If there is the target free memory space block, use the target free memory space block as the target memory space.
[0160] In an embodiment of the present invention, the storage module includes:
[0161] A determination sub - module, configured to determine whether the size of the target free memory space block is equal to the size of the tensor to be stored.
[0162] A first processing sub - module, configured to, if they are equal, store the tensor to be stored into the target free memory space block, and delete the target free memory space block from the current free blockchain table to obtain an updated first target free blockchain table.
[0163] A second processing sub - module, configured to, if they are not equal, store the tensor to be stored into the target free memory space block, and update the current free blockchain table based on the current allocation method to obtain an updated second target free blockchain table.
[0164] In an embodiment of the present invention, the second processing sub - module is specifically configured to:
[0165] If the current allocation method is the first allocation method, change the start address of the target free memory space block to the end address of the tensor to be stored to obtain an updated second target free blockchain table.
[0166] If the current allocation method is the second allocation method, change the end address of the target free memory space block to the start address of the tensor to be stored to obtain an updated second target free blockchain table.
[0167] For the apparatus embodiment, since it is basically similar to the method embodiment, the description is relatively simple. For related parts, refer to the partial description of the method embodiment.
[0168] An embodiment of the present invention further provides an electronic device, including:
[0169] It includes a processor, a memory, and a computer program stored on the memory and capable of running on the processor. When the computer program is executed by the processor, it implements each process of the above-mentioned data storage method embodiment and can achieve the same technical effect. To avoid repetition, it will not be elaborated here.
[0170] An embodiment of the present invention also provides a computer-readable storage medium. A computer program is stored on the computer-readable storage medium. When the computer program is executed by the processor, it implements each process of the above-mentioned data storage method embodiment and can achieve the same technical effect. To avoid repetition, it will not be elaborated here.
[0171] Each embodiment in this specification is described in a progressive manner. The key point of each embodiment is to illustrate the differences from other embodiments. For the same or similar parts among the embodiments, reference can be made to each other.
[0172] Those skilled in the art should understand that the embodiments of the present invention can be provided as a method, a device, or a computer program product. Therefore, the embodiments of the present invention can take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware aspects. Moreover, the embodiments of the present invention can take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0173] The embodiments of the present invention are described with reference to the flowcharts and / or block diagrams of the methods, terminal devices (systems), and computer program products according to the embodiments of the present invention. It should be understood that each process and / or block in the flowcharts and / or block diagrams, as well as the combination of processes and / or blocks in the flowcharts and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to the processors of general-purpose computers, special-purpose computers, embedded processors, or other programmable data processing terminal devices to generate a machine, such that the instructions executed by the processors of the computer or other programmable data processing terminal devices generate a device for implementing the functions specified in Figure 1 one process or multiple processes and / or blocks Figure 1 one block or multiple blocks.
[0174] These computer program instructions can also be stored in a computer-readable memory that can guide the computer or other programmable data processing terminal devices to work in a specific manner, such that the instructions stored in the computer-readable memory generate a manufactured product including an instruction device, and the instruction device implements the functions specified in Figure 1 one process or multiple processes and / or blocks Figure 1 one block or multiple blocks.
[0175] These computer program instructions can also be loaded onto a computer or other programmable data processing terminal device, so that a series of operation steps are executed on the computer or other programmable terminal device to generate a computer-implemented process. Thus, the instructions executed on the computer or other programmable terminal device provide steps for implementing the functions specified in one process or multiple processes and / or blocks. Figure 1 One process or multiple processes and / or blocks Figure 1 Steps for implementing the functions specified in one block or multiple blocks.
[0176] Although the preferred embodiments of the embodiments of the present invention have been described, those skilled in the art can make additional changes and modifications once they learn the basic creative concepts. Therefore, the appended claims are intended to be construed to include the preferred embodiments as well as all changes and modifications falling within the scope of the embodiments of the present invention.
[0177] Finally, it should also be noted that in this article, relational terms such as first and second are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the term "comprising", "including" or any other variant thereof is intended to cover non-exclusive inclusion, so that a process, method, article or terminal device comprising a series of elements not only includes those elements, but also includes other elements not expressly listed, or also includes elements inherent to such process, method, article or terminal device. Without further limitation, an element defined by the statement "comprising one..." does not exclude the presence of additional identical elements in the process, method, article or terminal device comprising the said element.
[0178] The above has introduced in detail a data storage method and a data storage device provided by the present invention. Specific examples are used in this article to elaborate on the principle and implementation manner of the present invention. The description of the above embodiments is only used to help understand the method and its core idea of the present invention; at the same time, for those of ordinary skill in the art, according to the idea of the present invention, there will be changes in the specific implementation manner and application scope. In summary, the content of this specification should not be construed as a limitation to the present invention.
Claims
1. A data storage method, characterized in that: The method comprises: In response to an allocation instruction for a tensor to be stored, determine a current allocation mode of a target memory space for storing the tensor to be stored; the current allocation mode includes a first allocation mode that allocates from a low address of a complete memory space, or a second allocation mode that allocates from a high address of the complete memory space; Using the first allocation method to search for a target memory space from a low address of the complete memory space, or using the second allocation method to search for a target memory space from a high address of the complete memory space; When the target memory space is successfully found, the tensor to be stored is stored in the target memory space.
2. The data storage method according to claim 1, characterized in that: The determining a current allocation mode of a target memory space for storing the tensor to be stored includes: Obtain a current free blockchain table corresponding to the complete memory space; the current free blockchain table includes at least one free memory space block in the complete memory space; Determine whether the starting address of the first free memory space block in the current free blockchain table is 0; If so, it is determined that the current allocation method is the first allocation method.
3. The data storage method according to claim 2, characterized in that: Also includes: If the starting address of the first free memory space block in the current free blockchain list is not 0, determining whether the ending address of the last free memory space block in the current free blockchain list is the capacity boundary of the memory space; If so, it is determined that the current allocation method is the second allocation method.
4. The data storage method according to claim 3, characterized in that: Also includes: If the end address of the last free memory space block in the current free blockchain table is not the capacity boundary of the memory space, determining whether the historical allocation method of the memory space is the first allocation method; If so, changing the first allocation method to the second allocation method; If not, the historical allocation method is changed to the first allocation method.
5. The data storage method according to claim 1, characterized in that: The step of searching the target memory space from the low address of the complete memory space by using the first allocation method, or searching the target memory space from the high address of the complete memory space by using the second allocation method, comprises: Starting from the low address, searching whether there is a target free memory space block in the current free blockchain table corresponding to the complete memory space, or, starting from the high address, searching whether there is a target free memory space block in the current free blockchain table corresponding to the complete memory space; the size of the target free memory space block is not less than the size of the tensor to be stored; If the target free memory space block exists, the target free memory space block is used as the target memory space.
6. The data storage method according to claim 1, characterized in that: The storing the tensor to be stored in the target memory space includes: Determine whether the size of the target free memory space block is equal to the size of the tensor to be stored; If they are equal, the tensor to be stored is stored in the target free memory space block, and the target free memory space block is deleted from the current free blockchain list to obtain an updated first target free blockchain list; If not equal, the tensor to be stored is stored in the target free memory space block, and the current free blockchain table is updated based on the current allocation method to obtain an updated second target free blockchain table.
7. The data storage method according to claim 6, characterized in that: The updating of the current idle blockchain table based on the current allocation mode to obtain an updated second target idle blockchain table includes: If the current allocation mode is the first allocation mode, the starting address of the target free memory space block is changed to the ending address of the tensor to be stored, and an updated second target free blockchain list is obtained; If the current allocation method is the second allocation method, the end address of the target free memory space block is changed to the starting address of the tensor to be stored to obtain an updated second target free blockchain list.
8. A data storage device, characterized in that: The device comprises: A determination module, configured to determine, in response to an allocation instruction for a tensor to be stored, a current allocation mode of a target memory space for storing the tensor to be stored; the current allocation mode includes a first allocation mode of allocating from a low address of the complete memory space, or a second allocation mode of allocating from a high address of the complete memory space; A search module, configured to use the first allocation method to search for a target memory space starting from a low address of the complete memory space, or use the second allocation method to search for a target memory space starting from a high address of the complete memory space; A storage module is used to store the tensor to be stored in the target memory space when the target memory space is successfully found.
9. An electronic device, characterized in that: include: A processor, a memory, and a computer program stored in the memory and capable of running on the processor, wherein when the computer program is executed by the processor, the steps of the data storage method according to any one of claims 1 to 7 are implemented.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the data storage method according to any one of claims 1 to 7 are implemented.
Citation Information
Patent Citations
Data allocation method and device, electronic equipment and computer readable medium
CN111258678A
Heap memory management method and device, equipment and medium
CN113296703A
Device and method capable of configuring FIFO depth
CN113821191A
Memory space management method and device, electronic equipment and storage medium
CN115168243A
Space allocation method and device, electronic equipment and computer readable storage medium
CN116150041A