[go: up one dir, main page]

CN118672779A - Memory multiplexing method, device, equipment and medium - Google Patents

Memory multiplexing method, device, equipment and medium Download PDF

Info

Publication number
CN118672779A
CN118672779A CN202410773908.XA CN202410773908A CN118672779A CN 118672779 A CN118672779 A CN 118672779A CN 202410773908 A CN202410773908 A CN 202410773908A CN 118672779 A CN118672779 A CN 118672779A
Authority
CN
China
Prior art keywords
memory
memory block
tensor
block
sub
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202410773908.XA
Other languages
Chinese (zh)
Inventor
王垒
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Kunlun Core Beijing Technology Co ltd
Original Assignee
Kunlun Core Beijing Technology Co ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Kunlun Core Beijing Technology Co ltd filed Critical Kunlun Core Beijing Technology Co ltd
Priority to CN202410773908.XA priority Critical patent/CN118672779A/en
Publication of CN118672779A publication Critical patent/CN118672779A/en
Pending legal-status Critical Current

Links

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]
    • G06F9/5005Allocation of resources, e.g. of the central processing unit [CPU] to service a request
    • G06F9/5011Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals
    • G06F9/5016Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals the resource being the memory

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Memory System (AREA)

Abstract

本公开提供了一种内存复用方法、装置、设备及介质,涉及人工智能技术领域,尤其涉及深度学习、内存管理技术领域。实现方案为:为多个子图中首先被执行的第一子图的多个第一张量相应地分配多个内存块;针对多个子图中在第一子图后执行的每个第二子图,分别执行下述操作:将当前每个内存块的第一标记的值重置为预设值;以及依次针对该第二子图的每个第二张量,执行如下操作:在当前的多个内存块中获取该第二张量的可复用内存块;响应于获取到该第二张量的可复用内存块,在可复用内存块中确定该第二张量复用的内存空间;以及基于该第二张量复用的内存空间,更新可复用内存块的第一标记的值。

The present disclosure provides a memory reuse method, device, equipment and medium, which relates to the field of artificial intelligence technology, especially to the field of deep learning and memory management technology. The implementation scheme is: allocate multiple memory blocks accordingly for multiple first tensors of the first sub-graph that is first executed in multiple sub-graphs; for each second sub-graph executed after the first sub-graph in the multiple sub-graphs, respectively perform the following operations: reset the value of the first tag of each current memory block to a preset value; and for each second tensor of the second sub-graph in turn, perform the following operations: obtain a reusable memory block of the second tensor in the current multiple memory blocks; in response to obtaining the reusable memory block of the second tensor, determine the memory space reused by the second tensor in the reusable memory block; and based on the memory space reused by the second tensor, update the value of the first tag of the reusable memory block.

Description

内存复用方法、装置、设备及介质Memory reuse method, device, equipment and medium

技术领域Technical Field

本公开涉及人工智能技术领域,尤其涉及深度学习、内存管理技术领域,具体涉及一种用于包括多个子图的计算图的内存复用方法、装置、电子设备、计算机可读存储介质和计算机程序产品。The present disclosure relates to the field of artificial intelligence technology, in particular to the field of deep learning and memory management technology, and specifically to a memory reuse method, device, electronic device, computer-readable storage medium, and computer program product for a computational graph including multiple subgraphs.

背景技术Background Art

人工智能是研究使计算机来模拟人的某些思维过程和智能行为(如学习、推理、思考、规划等)的学科,既有硬件层面的技术也有软件层面的技术。人工智能硬件技术一般包括如传感器、专用人工智能芯片、云计算、分布式存储、大数据处理等技术;人工智能软件技术主要包括计算机视觉技术、语音识别技术、自然语言处理技术以及机器学习/深度学习、大数据处理技术、知识图谱技术等几大方向。Artificial intelligence is a discipline that studies how computers can simulate certain human thought processes and intelligent behaviors (such as learning, reasoning, thinking, planning, etc.). It includes both hardware-level and software-level technologies. Artificial intelligence hardware technologies generally include technologies such as sensors, dedicated artificial intelligence chips, cloud computing, distributed storage, and big data processing; artificial intelligence software technologies mainly include computer vision technology, speech recognition technology, natural language processing technology, as well as machine learning/deep learning, big data processing technology, knowledge graph technology, and other major directions.

深度学习编译器(Tensor Virtual Machine,TVM)是一个开源的深度学习编译器栈,它允许用户在不同的硬件平台上高效执行深度学习模型。TVM的一个重要特性是BYOC(Bring Your Own Codegen to TVM),这是一个框架,使得用户可以将自己的AI芯片加速器集成到TVM中,从而实现对特定硬件的优化支持。BYOC的核心思想是将深度学习模型的计算图(Computation Graph)划分为不同的子图(Subgraphs),这些子图可以基于硬件的特性和优化策略进行定制和优化。Tensor Virtual Machine (TVM) is an open source deep learning compiler stack that allows users to efficiently execute deep learning models on different hardware platforms. An important feature of TVM is BYOC (Bring Your Own Codegen to TVM), which is a framework that allows users to integrate their own AI chip accelerators into TVM to achieve optimized support for specific hardware. The core idea of BYOC is to divide the computation graph of the deep learning model into different subgraphs, which can be customized and optimized based on the characteristics and optimization strategies of the hardware.

在此部分中描述的方法不一定是之前已经设想到或采用的方法。除非另有指明,否则不应假定此部分中描述的任何方法仅因其包括在此部分中就被认为是现有技术。类似地,除非另有指明,否则此部分中提及的问题不应认为在任何现有技术中已被公认。The methods described in this section are not necessarily methods that have been previously conceived or employed. Unless otherwise indicated, it should not be assumed that any method described in this section is considered to be prior art simply because it is included in this section. Similarly, unless otherwise indicated, the issues mentioned in this section should not be considered to have been recognized in any prior art.

发明内容Summary of the invention

本公开提供了一种用于包括多个子图的计算图的内存复用方法、装置、电子设备、计算机可读存储介质和计算机程序产品。The present disclosure provides a method, apparatus, electronic device, computer-readable storage medium, and computer program product for memory reuse of a computational graph comprising multiple subgraphs.

根据本公开的一方面,提供了一种用于包括多个子图的计算图的内存复用方法,包括:为多个子图中首先被执行的第一子图的多个第一张量相应地分配多个内存块,多个内存块中每个内存块对应各自的内存块信息和复用标记信息,内存块信息包括相应内存块的地址范围和内存大小,复用标记信息包括用于记录相应内存块中可复用内存大小的第一标记;针对多个子图中在第一子图后执行的每个第二子图,分别执行下述第一操作:将当前多个内存块中每个内存块的第一标记的值重置为预设值,预设值表示相应内存块中全部内存可被复用;以及依次针对该第二子图的每个第二张量,执行如下第二操作:基于该第二张量所需内存大小以及各个内存块的第一标记的当前值,在当前的多个内存块中获取该第二张量的可复用内存块;响应于获取到该第二张量的可复用内存块,基于该第二张量所需内存大小、可复用内存块的地址范围和第一标记的当前值,在可复用内存块中确定该第二张量复用的内存空间;以及基于该第二张量复用的内存空间,更新可复用内存块的第一标记的值。According to one aspect of the present disclosure, a memory reuse method for a computational graph including multiple subgraphs is provided, comprising: allocating multiple memory blocks accordingly for multiple first tensors of a first subgraph that is first executed among the multiple subgraphs, each of the multiple memory blocks corresponding to respective memory block information and reuse mark information, the memory block information comprising an address range and a memory size of the corresponding memory block, the reuse mark information comprising a first mark for recording a reusable memory size in the corresponding memory block; for each second subgraph executed after the first subgraph among the multiple subgraphs, respectively performing the following first operation: resetting the value of the first mark of each memory block in the current multiple memory blocks to a preset value, the preset value indicates that all memories in the corresponding memory block can be reused; and for each second tensor of the second subgraph, the following second operation is performed in sequence: based on the memory size required by the second tensor and the current value of the first tag of each memory block, a reusable memory block of the second tensor is obtained in the current multiple memory blocks; in response to obtaining the reusable memory block of the second tensor, based on the memory size required by the second tensor, the address range of the reusable memory block and the current value of the first tag, a memory space reused by the second tensor is determined in the reusable memory block; and based on the memory space reused by the second tensor, the value of the first tag of the reusable memory block is updated.

根据本公开的另一方面,提供了一种用于包括多个子图的计算图的内存复用装置,包括:分配单元,被配置为为多个子图中首先被执行的第一子图的多个第一张量相应地分配多个内存块,多个内存块中每个内存块对应各自的内存块信息和复用标记信息,内存块信息包括相应内存块的地址范围和内存大小,复用标记信息包括用于记录相应内存块中可复用内存大小的第一标记;执行单元,被配置为针对多个子图中在第一子图后执行的每个第二子图,分别执行下述第一操作,执行单元包括:第一设置子单元,被配置为将当前多个内存块中每个内存块的第一标记的值重置为预设值,预设值表示相应内存块中全部内存可被复用;以及执行子单元,被配置为依次针对该第二子图的每个第二张量,执行如下第二操作,执行子单元:获取模块,被配置为基于该第二张量所需内存大小以及各个内存块的第一标记的当前值,在当前的多个内存块中获取该第二张量的可复用内存块;确定模块,被配置为响应于获取到该第二张量的可复用内存块,基于该第二张量所需内存大小、可复用内存块的地址范围和第一标记的当前值,在可复用内存块中确定该第二张量复用的内存空间;以及第一更新模块,被配置为基于该第二张量复用的内存空间,更新可复用内存块的第一标记的值。According to another aspect of the present disclosure, a memory reuse device for a computational graph including multiple subgraphs is provided, comprising: an allocation unit, configured to allocate multiple memory blocks correspondingly to multiple first tensors of a first subgraph that is first executed in the multiple subgraphs, each of the multiple memory blocks corresponding to respective memory block information and reuse mark information, the memory block information comprising an address range and a memory size of the corresponding memory block, the reuse mark information comprising a first mark for recording a reusable memory size in the corresponding memory block; an execution unit, configured to respectively perform the following first operation for each second subgraph executed after the first subgraph in the multiple subgraphs, the execution unit comprising: a first setting subunit, configured to reset the value of the first mark of each memory block in the current multiple memory blocks to a preset value, the preset Set a value indicating that all memories in the corresponding memory block can be reused; and an execution subunit is configured to perform the following second operation for each second tensor of the second subgraph in turn, and the execution subunit: an acquisition module is configured to acquire a reusable memory block of the second tensor in the current multiple memory blocks based on the memory size required by the second tensor and the current value of the first tag of each memory block; a determination module is configured to determine the memory space reused by the second tensor in the reusable memory block in response to acquiring the reusable memory block of the second tensor based on the memory size required by the second tensor, the address range of the reusable memory block and the current value of the first tag; and a first update module is configured to update the value of the first tag of the reusable memory block based on the memory space reused by the second tensor.

根据本公开的另一方面,提供了一种电子设备,包括:至少一个处理器;以及与所述至少一个处理器通信连接的存储器;其中所述存储器存储有可被所述至少一个处理器执行的指令,所述指令被所述至少一个处理器执行,以使所述至少一个处理器能够执行上述用于包括多个子图的计算图的内存复用方法。According to another aspect of the present disclosure, an electronic device is provided, comprising: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to execute the above-mentioned memory reuse method for a computational graph including multiple subgraphs.

根据本公开的另一方面,提供了一种存储有计算机指令的非瞬时计算机可读存储介质,其中,所述计算机指令用于使计算机执行上述用于包括多个子图的计算图的内存复用方法。According to another aspect of the present disclosure, a non-transitory computer-readable storage medium storing computer instructions is provided, wherein the computer instructions are used to cause a computer to execute the above-mentioned memory reuse method for a computational graph including multiple subgraphs.

根据本公开的另一方面,提供了一种计算机程序产品,包括计算机程序,其中,所述计算机程序在被处理器执行时实现上述用于包括多个子图的计算图的内存复用方法。According to another aspect of the present disclosure, a computer program product is provided, comprising a computer program, wherein the computer program implements the above-mentioned memory reuse method for a computational graph comprising multiple subgraphs when executed by a processor.

根据本公开的一个或多个实施例,通过对前序子图相应的每个已分配的内存块进行标记,在对下一子图的各个张量进行内存分配时,首先根据已分配内存块的标记,判断已分配内存块中是否有可复用内存块;若有,则复用其中内存空间,从而避免为多个子图分配内存时整体上内存占用较大,子图较多或张量规模较大时容易内存溢出的问题。According to one or more embodiments of the present disclosure, by marking each allocated memory block corresponding to the previous subgraph, when allocating memory for each tensor of the next subgraph, first, based on the mark of the allocated memory block, it is determined whether there is a reusable memory block in the allocated memory block; if so, the memory space therein is reused, thereby avoiding the problem of large overall memory usage when allocating memory for multiple subgraphs, and easy memory overflow when there are many subgraphs or a large tensor scale.

应当理解,本部分所描述的内容并非旨在标识本公开的实施例的关键或重要特征,也不用于限制本公开的范围。本公开的其它特征将通过以下的说明书而变得容易理解。It should be understood that the content described in this section is not intended to identify the key or important features of the embodiments of the present disclosure, nor is it intended to limit the scope of the present disclosure. Other features of the present disclosure will become easily understood through the following description.

附图说明BRIEF DESCRIPTION OF THE DRAWINGS

附图示例性地示出了实施例并且构成说明书的一部分,与说明书的文字描述一起用于讲解实施例的示例性实施方式。所示出的实施例仅出于例示的目的,并不限制权利要求的范围。在所有附图中,相同的附图标记指代类似但不一定相同的要素。The accompanying drawings exemplarily illustrate the embodiments and constitute a part of the specification, and together with the text description of the specification, are used to explain the exemplary implementation of the embodiments. The embodiments shown are for illustrative purposes only and do not limit the scope of the claims. In all drawings, the same reference numerals refer to similar but not necessarily identical elements.

图1示出了根据本公开的实施例的可以在其中实施本文描述的各种方法的示例性系统的示意图;FIG1 shows a schematic diagram of an exemplary system in which various methods described herein may be implemented according to an embodiment of the present disclosure;

图2示出了根据本公开的实施例的用于包括多个子图的计算图的内存复用方法的流程图;FIG2 shows a flowchart of a memory reuse method for a computation graph including multiple subgraphs according to an embodiment of the present disclosure;

图3示出了根据本公开的示例性实施例给出的用于包括多个子图的计算图的内存复用方法的流程框图;FIG3 shows a flowchart of a memory reuse method for a computation graph including multiple subgraphs according to an exemplary embodiment of the present disclosure;

图4示出了根据本公开的实施例的用于包括多个子图的计算图的内存复用装置的结构框图;FIG4 shows a structural block diagram of a memory reuse device for a computation graph including multiple subgraphs according to an embodiment of the present disclosure;

图5示出了能够用于实现本公开的实施例的示例性电子设备的结构框图。FIG. 5 shows a block diagram of an exemplary electronic device that can be used to implement the embodiments of the present disclosure.

具体实施方式DETAILED DESCRIPTION

以下结合附图对本公开的示范性实施例做出说明,其中包括本公开实施例的各种细节以助于理解,应当将它们认为仅仅是示范性的。因此,本领域普通技术人员应当认识到,可以对这里描述的实施例做出各种改变和修改,而不会背离本公开的范围。同样,为了清楚和简明,以下的描述中省略了对公知功能和结构的描述。The following is a description of exemplary embodiments of the present disclosure in conjunction with the accompanying drawings, including various details of the embodiments of the present disclosure to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope of the present disclosure. Similarly, for the sake of clarity and conciseness, descriptions of well-known functions and structures are omitted in the following description.

在本公开中,除非另有说明,否则使用术语“第一”、“第二”等来描述各种要素不意图限定这些要素的位置关系、时序关系或重要性关系,这种术语只是用于将一个要素与另一要素区分开。在一些示例中,第一要素和第二要素可以指向该要素的同一实例,而在某些情况下,基于上下文的描述,它们也可以指代不同实例。In the present disclosure, unless otherwise specified, the use of the terms "first", "second", etc. to describe various elements is not intended to limit the positional relationship, temporal relationship, or importance relationship of these elements, and such terms are only used to distinguish one element from another element. In some examples, the first element and the second element may refer to the same instance of the element, and in some cases, based on the description of the context, they may also refer to different instances.

在本公开中对各种所述示例的描述中所使用的术语只是为了描述特定示例的目的,而并非旨在进行限制。除非上下文另外明确地表明,如果不特意限定要素的数量,则该要素可以是一个也可以是多个。此外,本公开中所使用的术语“和/或”涵盖所列出的项目中的任何一个以及全部可能的组合方式。The terms used in the description of various examples in this disclosure are only for the purpose of describing specific examples and are not intended to be limiting. Unless the context clearly indicates otherwise, if the number of elements is not specifically limited, the element can be one or more. In addition, the term "and/or" used in this disclosure covers any one of the listed items and all possible combinations.

TVM子图分割后,在运行之前会先为每个子图分配内存块(如AI芯片显存),再在对应的显存上运行算子进行推理运算。为了不让显存分配操作占用推理耗时,显存分配全部发生在算子运行前,即先为每个子图的各个张量统一分配显存块后,再依据子图的拓扑关系依次运行算子。因为算子运行还没有开始,在为后面的子图分配显存时前面子图占用的显存并不会释放。相关技术中,各个子图可以在AI芯片空闲内存池中去分配各自所需的内存块,也就是说各个子图间的显存分配是完全独立的,占用的总显存会不断累加,这样会造成整体上内存占用较大,子图较多或张量规模较大时容易内存溢出的问题。After the TVM subgraph is split, a memory block (such as the AI chip memory) will be allocated to each subgraph before running, and then the operator will be run on the corresponding memory for inference. In order to prevent the memory allocation operation from taking up the time of inference, all memory allocation occurs before the operator runs, that is, the memory blocks are uniformly allocated for each tensor of each subgraph, and then the operators are run in sequence according to the topological relationship of the subgraph. Because the operator has not started running, the memory occupied by the previous subgraph will not be released when the memory is allocated for the subsequent subgraph. In related technologies, each subgraph can allocate the memory blocks it needs from the free memory pool of the AI chip, that is, the memory allocation between each subgraph is completely independent, and the total memory occupied will continue to accumulate, which will cause the overall memory usage to be large, and memory overflow problems are prone to occur when there are many subgraphs or the scale of tensors is large.

本公开的实施例给出了一种用于包括多个子图的计算图的内存复用方法,通过对前序子图相应的每个已分配的内存块进行标记,在对下一子图的各个张量进行内存分配时,首先根据已分配内存块的标记,判断已分配内存块中是否有可复用内存块;若有,则复用其中内存空间,从而避免为多个子图分配内存时整体上内存占用较大,子图较多或张量规模较大时容易内存溢出的问题。An embodiment of the present disclosure provides a memory reuse method for a computational graph including multiple subgraphs. By marking each allocated memory block corresponding to the previous subgraph, when allocating memory for each tensor of the next subgraph, first, based on the mark of the allocated memory block, it is determined whether there is a reusable memory block in the allocated memory block; if so, the memory space therein is reused, thereby avoiding the problem of large overall memory usage when allocating memory for multiple subgraphs, and easy memory overflow when there are many subgraphs or a large tensor scale.

下面将结合附图详细描述本公开的实施例。The embodiments of the present disclosure will be described in detail below with reference to the accompanying drawings.

图1示出了根据本公开的实施例可以将本文描述的各种方法和装置在其中实施的示例性系统100的示意图。参考图1,该系统100包括一个或多个客户端设备101、102、103、104、105和106、服务器120以及将一个或多个客户端设备耦接到服务器120的一个或多个通信网络110。客户端设备101、102、103、104、105和106可以被配置为执行一个或多个应用程序。FIG1 shows a schematic diagram of an exemplary system 100 in which various methods and apparatuses described herein may be implemented according to an embodiment of the present disclosure. Referring to FIG1 , the system 100 includes one or more client devices 101, 102, 103, 104, 105, and 106, a server 120, and one or more communication networks 110 coupling the one or more client devices to the server 120. The client devices 101, 102, 103, 104, 105, and 106 may be configured to execute one or more applications.

在本公开的实施例中,服务器120可以运行使得能够执行用于包括多个子图的计算图的内存复用方法的一个或多个服务或软件应用。In an embodiment of the present disclosure, the server 120 may run one or more services or software applications that enable execution of a memory reuse method for a computation graph including multiple subgraphs.

在某些实施例中,服务器120还可以提供其他服务或软件应用,这些服务或软件应用可以包括非虚拟环境和虚拟环境。在某些实施例中,这些服务可以作为基于web的服务或云服务提供,例如在软件即服务(SaaS)模型下提供给客户端设备101、102、103、104、105和/或106的用户。In some embodiments, server 120 may also provide other services or software applications, which may include non-virtualized environments and virtualized environments. In some embodiments, these services may be provided as web-based services or cloud services, such as provided to users of client devices 101, 102, 103, 104, 105, and/or 106 under a software as a service (SaaS) model.

在图1所示的配置中,服务器120可以包括实现由服务器120执行的功能的一个或多个组件。这些组件可以包括可由一个或多个处理器执行的软件组件、硬件组件或其组合。操作客户端设备101、102、103、104、105和/或106的用户可以依次利用一个或多个客户端应用程序来与服务器120进行交互以利用这些组件提供的服务。应当理解,各种不同的系统配置是可能的,其可以与系统100不同。因此,图1是用于实施本文所描述的各种方法的系统的一个示例,并且不旨在进行限制。In the configuration shown in FIG. 1 , the server 120 may include one or more components that implement the functions performed by the server 120. These components may include software components, hardware components, or a combination thereof that can be executed by one or more processors. Users operating client devices 101, 102, 103, 104, 105, and/or 106 may in turn utilize one or more client applications to interact with the server 120 to utilize the services provided by these components. It should be understood that a variety of different system configurations are possible, which may be different from the system 100. Therefore, FIG. 1 is an example of a system for implementing the various methods described herein and is not intended to be limiting.

用户可以使用客户端设备101、102、103、104、105和/或106来发送计算图的执行指令。客户端设备可以提供使客户端设备的用户能够与客户端设备进行交互的接口。客户端设备还可以经由该接口向用户输出信息。尽管图1仅描绘了六种客户端设备,但是本领域技术人员将能够理解,本公开可以支持任何数量的客户端设备。The user may use client devices 101, 102, 103, 104, 105 and/or 106 to send execution instructions for the computation graph. The client device may provide an interface that enables a user of the client device to interact with the client device. The client device may also output information to the user via the interface. Although FIG. 1 depicts only six client devices, those skilled in the art will appreciate that the present disclosure may support any number of client devices.

客户端设备101、102、103、104、105和/或106可以包括各种类型的计算机设备,例如便携式手持设备、通用计算机(诸如个人计算机和膝上型计算机)、工作站计算机、可穿戴设备、智能屏设备、自助服务终端设备、服务机器人、游戏系统、瘦客户端、各种消息收发设备、传感器或其他感测设备等。这些计算机设备可以运行各种类型和版本的软件应用程序和操作系统,例如MICROSOFT Windows、APPLE iOS、类UNIX操作系统、Linux或类Linux操作系统(例如GOOGLE Chrome OS);或包括各种移动操作系统,例如MICROSOFT WindowsMobile OS、iOS、Windows Phone、Android。便携式手持设备可以包括蜂窝电话、智能电话、平板电脑、个人数字助理(PDA)等。可穿戴设备可以包括头戴式显示器(诸如智能眼镜)和其他设备。游戏系统可以包括各种手持式游戏设备、支持互联网的游戏设备等。客户端设备能够执行各种不同的应用程序,例如各种与Internet相关的应用程序、通信应用程序(例如电子邮件应用程序)、短消息服务(SMS)应用程序,并且可以使用各种通信协议。Client devices 101, 102, 103, 104, 105 and/or 106 may include various types of computer devices, such as portable handheld devices, general-purpose computers (such as personal computers and laptop computers), workstation computers, wearable devices, smart screen devices, self-service terminal devices, service robots, game systems, thin clients, various messaging devices, sensors or other sensing devices, etc. These computer devices may run various types and versions of software applications and operating systems, such as MICROSOFT Windows, APPLE iOS, UNIX-like operating systems, Linux or Linux-like operating systems (such as GOOGLE Chrome OS); or include various mobile operating systems, such as MICROSOFT Windows Mobile OS, iOS, Windows Phone, Android. Portable handheld devices may include cellular phones, smart phones, tablet computers, personal digital assistants (PDAs), etc. Wearable devices may include head-mounted displays (such as smart glasses) and other devices. Game systems may include various handheld game devices, Internet-enabled game devices, etc. Client devices are capable of executing various different applications, such as various Internet-related applications, communication applications (such as email applications), short message service (SMS) applications, and may use various communication protocols.

网络110可以是本领域技术人员熟知的任何类型的网络,其可以使用多种可用协议中的任何一种(包括但不限于TCP/IP、SNA、IPX等)来支持数据通信。仅作为示例,一个或多个网络110可以是局域网(LAN)、基于以太网的网络、令牌环、广域网(WAN)、因特网、虚拟网络、虚拟专用网络(VPN)、内部网、外部网、区块链网络、公共交换电话网(PSTN)、红外网络、无线网络(例如蓝牙、WIFI)和/或这些和/或其他网络的任意组合。The network 110 may be any type of network known to those skilled in the art that may support data communications using any of a variety of available protocols, including but not limited to TCP/IP, SNA, IPX, etc. By way of example only, the one or more networks 110 may be a local area network (LAN), an Ethernet-based network, a token ring, a wide area network (WAN), the Internet, a virtual network, a virtual private network (VPN), an intranet, an extranet, a blockchain network, a public switched telephone network (PSTN), an infrared network, a wireless network (e.g., Bluetooth, WIFI), and/or any combination of these and/or other networks.

服务器120可以包括一个或多个通用计算机、专用服务器计算机(例如PC(个人计算机)服务器、UNIX服务器、中端服务器)、刀片式服务器、大型计算机、服务器群集或任何其他适当的布置和/或组合。服务器120可以包括运行虚拟操作系统的一个或多个虚拟机,或者涉及虚拟化的其他计算架构(例如可以被虚拟化以维护服务器的虚拟存储设备的逻辑存储设备的一个或多个灵活池)。在各种实施例中,服务器120可以运行提供下文所描述的功能的一个或多个服务或软件应用。Server 120 may include one or more general purpose computers, dedicated server computers (e.g., PC (personal computer) servers, UNIX servers, mid-range servers), blade servers, mainframe computers, server clusters, or any other suitable arrangement and/or combination. Server 120 may include one or more virtual machines running virtual operating systems, or other computing architectures involving virtualization (e.g., one or more flexible pools of logical storage devices that may be virtualized to maintain a server's virtual storage device). In various embodiments, server 120 may run one or more services or software applications that provide the functionality described below.

服务器120中的计算单元可以运行包括上述任何操作系统以及任何商业上可用的服务器操作系统的一个或多个操作系统。服务器120还可以运行各种附加服务器应用程序和/或中间层应用程序中的任何一个,包括HTTP服务器、FTP服务器、CGI服务器、JAVA服务器、数据库服务器等。The computing units in the server 120 may run one or more operating systems including any of the above operating systems and any commercially available server operating systems. The server 120 may also run any of a variety of additional server applications and/or middle-tier applications, including HTTP servers, FTP servers, CGI servers, JAVA servers, database servers, etc.

在一些实施方式中,服务器120可以包括一个或多个应用程序,以分析和合并从客户端设备101、102、103、104、105和/或106的用户接收的数据馈送和/或事件更新。服务器120还可以包括一个或多个应用程序,以经由客户端设备101、102、103、104、105和/或106的一个或多个显示设备来显示数据馈送和/或实时事件。In some implementations, server 120 may include one or more applications to analyze and consolidate data feeds and/or event updates received from users of client devices 101, 102, 103, 104, 105, and/or 106. Server 120 may also include one or more applications to display data feeds and/or real-time events via one or more display devices of client devices 101, 102, 103, 104, 105, and/or 106.

在一些实施方式中,服务器120可以为分布式系统的服务器,或者是结合了区块链的服务器。服务器120也可以是云服务器,或者是带人工智能技术的智能云计算服务器或智能云主机。云服务器是云计算服务体系中的一项主机产品,以解决传统物理主机与虚拟专用服务器(VPS,Virtual Private Server)服务中存在的管理难度大、业务扩展性弱的缺陷。In some embodiments, the server 120 may be a server of a distributed system, or a server combined with a blockchain. The server 120 may also be a cloud server, or an intelligent cloud computing server or intelligent cloud host with artificial intelligence technology. A cloud server is a host product in a cloud computing service system to solve the defects of difficult management and weak business scalability in traditional physical hosts and virtual private servers (VPS) services.

系统100还可以包括一个或多个数据库130。在某些实施例中,这些数据库可以用于存储数据和其他信息。例如,数据库130中的一个或多个可用于存储诸如音频文件和视频文件的信息。数据库130可以驻留在各种位置。例如,由服务器120使用的数据库可以在服务器120本地,或者可以远离服务器120且可以经由基于网络或专用的连接与服务器120通信。数据库130可以是不同的类型。在某些实施例中,由服务器120使用的数据库例如可以是关系数据库。这些数据库中的一个或多个可以响应于命令而存储、更新和检索到数据库以及来自数据库的数据。The system 100 may also include one or more databases 130. In some embodiments, these databases may be used to store data and other information. For example, one or more of the databases 130 may be used to store information such as audio files and video files. The databases 130 may reside in various locations. For example, the database used by the server 120 may be local to the server 120, or may be remote from the server 120 and may communicate with the server 120 via a network-based or dedicated connection. The databases 130 may be of different types. In some embodiments, the databases used by the server 120 may be, for example, relational databases. One or more of these databases may store, update, and retrieve data to and from the databases in response to commands.

在某些实施例中,数据库130中的一个或多个还可以由应用程序使用来存储应用程序数据。由应用程序使用的数据库可以是不同类型的数据库,例如键值存储库,对象存储库或由文件系统支持的常规存储库。In some embodiments, one or more of the databases 130 may also be used by applications to store application data. The databases used by the applications may be different types of databases, such as a key-value store, an object store, or a conventional store backed by a file system.

图1的系统100可以以各种方式配置和操作,以使得能够应用根据本公开所描述的各种方法和装置。The system 100 of FIG. 1 may be configured and operated in various ways to enable application of various methods and apparatuses described according to the present disclosure.

根据一些实施例,如图2所示,提供了一种用于包括多个子图的计算图的内存复用方法,包括:步骤S201、为多个子图中首先被执行的第一子图的多个第一张量相应地分配多个内存块,多个内存块中每个内存块对应各自的内存块信息和复用标记信息,内存块信息包括相应内存块的地址范围和内存大小,复用标记信息包括用于记录相应内存块中可复用内存大小的第一标记;步骤S202、针对多个子图中在第一子图后执行的每个第二子图,分别执行下述第一操作:步骤S2021、将当前多个内存块中每个内存块的第一标记的值重置为预设值,预设值表示相应内存块中全部内存可被复用;以及步骤S2022、依次针对该第二子图的每个第二张量,执行如下第二操作:步骤S2022-1、基于该第二张量所需内存大小以及各个内存块的第一标记的当前值,在当前的多个内存块中获取该第二张量的可复用内存块;步骤S2022-2、响应于获取到该第二张量的可复用内存块,基于该第二张量所需内存大小、可复用内存块的地址范围和第一标记的当前值,在可复用内存块中确定该第二张量复用的内存空间;以及步骤S2022-3、基于该第二张量复用的内存空间,更新可复用内存块的第一标记的值。According to some embodiments, as shown in FIG. 2 , a memory reuse method for a computational graph including multiple subgraphs is provided, including: step S201, allocating multiple memory blocks accordingly for multiple first tensors of a first subgraph that is first executed among multiple subgraphs, each of the multiple memory blocks corresponding to respective memory block information and reuse mark information, the memory block information including an address range and a memory size of the corresponding memory block, the reuse mark information including a first mark for recording a reusable memory size in the corresponding memory block; step S202, for each second subgraph executed after the first subgraph among the multiple subgraphs, respectively performing the following first operations: step S2021, resetting the value of the first mark of each memory block in the current multiple memory blocks to a preset value, the preset value table Indicates that all memories in the corresponding memory block can be reused; and step S2022, perform the following second operation for each second tensor of the second sub-graph in turn: step S2022-1, based on the memory size required by the second tensor and the current value of the first tag of each memory block, obtain the reusable memory block of the second tensor in the current multiple memory blocks; step S2022-2, in response to obtaining the reusable memory block of the second tensor, based on the memory size required by the second tensor, the address range of the reusable memory block and the current value of the first tag, determine the memory space reused by the second tensor in the reusable memory block; and step S2022-3, based on the memory space reused by the second tensor, update the value of the first tag of the reusable memory block.

由此,通过对前序子图相应的每个已分配的内存块进行标记,在对下一子图的各个张量进行内存分配时,首先根据已分配内存块的标记,判断已分配内存块中是否有可复用内存块;若有,则复用其中内存空间。由于各个子图的运行是顺序进行的,不同子图间的内存复用不会产生数据覆盖的问题。通过上述方法,能够实现不同子图间的内存复用,避免为多个子图分配内存时整体上内存占用较大,子图较多或张量规模较大时容易内存溢出的问题。Therefore, by marking each allocated memory block corresponding to the previous subgraph, when allocating memory for each tensor of the next subgraph, first determine whether there is a reusable memory block in the allocated memory block based on the mark of the allocated memory block; if so, reuse the memory space therein. Since each subgraph runs sequentially, memory reuse between different subgraphs will not cause data overwrite problems. Through the above method, memory reuse between different subgraphs can be achieved, avoiding the problem of large overall memory usage when allocating memory for multiple subgraphs, and easy memory overflow when there are many subgraphs or large tensor scales.

在一些实施例中,上述多个子图可以在CPU或GPU上运行。In some embodiments, the multiple subgraphs described above may be run on a CPU or a GPU.

在一些实施例中,针对上述多个子图的内存分配可以是对CPU内存空间的分配或GPU内存空间(即显存)的分配。In some embodiments, the memory allocation for the above-mentioned multiple sub-graphs may be the allocation of CPU memory space or GPU memory space (ie, video memory).

在一些实施例中,可以在CPU或GPU的内存池中为各个第一张量分配相应的内存块。In some embodiments, a corresponding memory block may be allocated to each first tensor in a memory pool of a CPU or a GPU.

在一些实施例中,为多个子图中首先被执行的第一子图的多个第一张量相应地分配多个内存块可以是为每个第一张量分配一个预设内存大小的内存块,其中,预设内存大小大于或等于每个第一张量所需的内存大小。In some embodiments, allocating multiple memory blocks accordingly for multiple first tensors of a first subgraph that is first executed among multiple subgraphs may be allocating a memory block of a preset memory size to each first tensor, wherein the preset memory size is greater than or equal to the memory size required for each first tensor.

在一些实施例中,多个内存块中每个内存块的内存大小可以是基于相应张量的所需内存大小确定的。In some embodiments, the memory size of each memory block in the plurality of memory blocks may be determined based on a required memory size of a corresponding tensor.

由此,通过根据每个张量实际所需内存大小为每个张量分配内存块,从而能够进一步节省所用内存,避免内存溢出的风险。Therefore, by allocating a memory block to each tensor according to the actual memory size required by each tensor, the used memory can be further saved and the risk of memory overflow can be avoided.

在一些实施例中,多个内存块中每个内存块对应各自的内存块信息和复用标记信息。In some embodiments, each memory block in the plurality of memory blocks corresponds to respective memory block information and reuse mark information.

在一些实施例中,内存块信息可以包括相应内存块的地址范围和内存大小。In some embodiments, the memory block information may include an address range and a memory size of the corresponding memory block.

在一些实施例中,内存块信息还可以包括该内存块的标识信息、当前内存块所存储数据的数据类型等信息。In some embodiments, the memory block information may also include identification information of the memory block, data type of data stored in the current memory block, and other information.

在一些实施例中,复用标记信息可以包括用于记录相应内存块中已被复用的内存大小的第一标记。In some embodiments, the reuse mark information may include a first mark for recording the size of the memory that has been reused in the corresponding memory block.

在一些示例性实施例中,该第一标记可以为未占用地址偏移量(unuse_offset),也即一个相对的地址,可以用于表示相应内存块中当前已占用的空间。In some exemplary embodiments, the first mark may be an unused address offset (unuse_offset), that is, a relative address, which may be used to indicate the currently occupied space in the corresponding memory block.

在对每个第二子图进行内存分配前,可以将当前已有的多个内存块中每个内存块的第一标记的值重置为零(即预设值),也即,通过第一标记表示当前的各个内存块中的内存空间均未被当前的第二子图的张量数据所复用。Before allocating memory for each second sub-graph, the value of the first mark of each memory block in the currently existing multiple memory blocks can be reset to zero (i.e., the preset value). That is, the first mark indicates that the memory space in each current memory block is not reused by the tensor data of the current second sub-graph.

随后,可以分别对当前第二子图中的每个第二张量,根据该第二张量所需内存大小,在上述多个内存块中找到一个剩余的可复用内存足够的可复用内存块,并在该内存块中确定一个地址范围,以作为该第二张量所复用的内存空间。Subsequently, for each second tensor in the current second subgraph, according to the memory size required by the second tensor, a reusable memory block with sufficient remaining reusable memory can be found in the above-mentioned multiple memory blocks, and an address range can be determined in the memory block to serve as the memory space reused by the second tensor.

其中,每个内存块中的剩余的可复用内存可以通过该内存块的内存大小和上述第一标记的当前值确定。例如,通过该内存块的内存大小减去第一标记的当前值,从而获得该内存块中的剩余的可复用内存。The remaining reusable memory in each memory block can be determined by the memory size of the memory block and the current value of the first tag, for example, by subtracting the current value of the first tag from the memory size of the memory block to obtain the remaining reusable memory in the memory block.

在一些示例性实施例中,该第一标记也可以为相应内存块中剩余的可复用内存空间的大小。In some exemplary embodiments, the first mark may also be the size of the remaining reusable memory space in the corresponding memory block.

在对每个第二子图进行内存分配前,可以将当前已有的多个内存块中每个内存块的第一标记的值重置为各个内存块的内存大小(即预设值),也即,通过第一标记表示当前的各个内存块中的内存空间均未被当前的第二子图的张量数据所复用。Before allocating memory for each second sub-graph, the value of the first mark of each memory block in the currently existing multiple memory blocks can be reset to the memory size of each memory block (i.e., the preset value). That is, the first mark indicates that the memory space in each current memory block is not reused by the tensor data of the current second sub-graph.

随后,可以分别对当前第二子图中的每个第二张量,根据该第二张量所需内存大小,在上述多个内存块中找到一个剩余的可复用内存足够的可复用内存块,并在该内存块中确定一个地址范围,以作为该第二张量所复用的内存空间。Subsequently, for each second tensor in the current second subgraph, according to the memory size required by the second tensor, a reusable memory block with sufficient remaining reusable memory can be found in the above-mentioned multiple memory blocks, and an address range can be determined in the memory block to serve as the memory space reused by the second tensor.

在一些实施例中,在每确定一个第二张量的所复用的内存空间后,即对该可复用内存块的第一标记的值进行更新,从而更新该内存块的剩余的可被复用的内存空间大小。In some embodiments, after each reused memory space of a second tensor is determined, the value of the first tag of the reusable memory block is updated, thereby updating the remaining reusable memory space size of the memory block.

在一些示例性实施例中,当第一标记为未占用地址偏移量,则可以通过在当前值的基础上加上该第二张量复用的内存空间大小来更新该第一标记的值。In some exemplary embodiments, when the first tag is an unoccupied address offset, the value of the first tag can be updated by adding the size of the memory space reused by the second tensor to the current value.

在一些示例性实施例中,当第一标记为相应内存块中剩余的可复用内存空间的大小,则可以通过在当前值的基础上减去该第二张量复用的内存空间大小来更新该第一标记的值。In some exemplary embodiments, when the first tag is the size of the remaining reusable memory space in the corresponding memory block, the value of the first tag can be updated by subtracting the size of the memory space reused by the second tensor from the current value.

在一些实施例中,当继续进行下一个第二张量的内存复用时,则可以通过内存块的第一标记更新后的值来进行剩余的可复用内存的判断。In some embodiments, when the memory reuse of the next second tensor is continued, the remaining reusable memory can be determined by the updated value of the first mark of the memory block.

在一些实施例中,当所有内存块中的剩余的可被复用的内存空间均无法满足第二张量对内存空间的需求时,则可以在上述内存池中新申请一个内存块,以分配给该第二张量,同时记录该第二张量的内存块的相关信息,以对上述多个内存块的信息进行更新,使得在后序子图的内存复用中,可以对该第二张量的内存块也进行内存复用。In some embodiments, when the remaining reusable memory space in all memory blocks cannot meet the memory space demand of the second tensor, a new memory block can be applied for in the above-mentioned memory pool to be allocated to the second tensor, and the relevant information of the memory block of the second tensor is recorded at the same time to update the information of the above-mentioned multiple memory blocks, so that in the memory reuse of the subsequent sub-graph, the memory block of the second tensor can also be reused.

在一些实施例中,多个内存块中每个内存块各自的内存块信息和复用标记信息作为一个信息块可以记录在链表中,并且各个内存块的信息块按照内存大小由小到大排列,基于该第二张量所需内存大小以及各个内存块的第一标记的当前值,在当前的多个内存块中获取该第二张量的可复用内存块可以包括:依次检测当前链表中的各个内存块的信息是否满足第一预设条件,第一预设条件包括相应内存块的可复用内存大小大于该第二张量所需内存大小;以及响应于检测到满足第一预设条件的第一内存块,将第一内存块确定为该第二张量的可复用内存块。In some embodiments, the memory block information and reuse mark information of each memory block in a plurality of memory blocks can be recorded in a linked list as an information block, and the information blocks of each memory block are arranged from small to large according to the memory size. Based on the memory size required for the second tensor and the current value of the first mark of each memory block, obtaining the reusable memory block of the second tensor in the current plurality of memory blocks can include: sequentially detecting whether the information of each memory block in the current linked list satisfies a first preset condition, the first preset condition including that the reusable memory size of the corresponding memory block is greater than the memory size required for the second tensor; and in response to detecting a first memory block that satisfies the first preset condition, determining the first memory block as a reusable memory block for the second tensor.

由此,通过将各个内存块的信息块按序存放在链表中,并按内存空间从小到大的顺序对每个内存块的信息进行检测,直至检测到可复用内存空间为止,从而能够进一步提升检测效率,同时,也能避免将较大内存空间用于较小规模的张量,从而提升已分配内存的复用率。Therefore, by storing the information blocks of each memory block in a linked list in order, and detecting the information of each memory block in order from small to large memory space, until reusable memory space is detected, the detection efficiency can be further improved. At the same time, it can also avoid using a larger memory space for a smaller-scale tensor, thereby improving the reuse rate of the allocated memory.

在一些实施例中,可以设置一个用于记录上述内存块的信息的链表。其中,每个内存块的信息可以以信息块的形式统一记录在上述链表中。In some embodiments, a linked list for recording the information of the memory blocks may be provided, wherein the information of each memory block may be uniformly recorded in the linked list in the form of an information block.

在一些实施例中,每个内存块的信息块可以包括上述内存块信息和复用标记信息。该链表的基本类型可以被定义为一个用于保存上述信息块的结构体Buffer。上述链表mem_pool可以表示为:In some embodiments, the information block of each memory block may include the above memory block information and reuse mark information. The basic type of the linked list may be defined as a structure Buffer for storing the above information blocks. The above linked list mem_pool may be represented as:

std::list<Buffer>mem_pool;std::list<Buffer>mem_pool;

struct Buffer{struct Buffer{

NDArray pool;NDArray pool;

size_t unuse_offset=0;size_t unuse_offset = 0;

};};

其中,Buffer表示用于记录上述信息块的结构体Buffer,NDArray pool表示内存块信息,unuse_offset表示第一标记(未占用地址偏移量),其初始值被设置为0。Among them, Buffer represents the structure Buffer used to record the above information block, NDArray pool represents the memory block information, unuse_offset represents the first mark (unoccupied address offset), and its initial value is set to 0.

在一些实施例中,上述链表中的多个信息块按照各个内存块的内存大小由小到大排列。In some embodiments, the multiple information blocks in the above linked list are arranged from small to large according to the memory size of each memory block.

在一些实施例中,可以按照链表中各个内存块的排列顺序,对各个内存块进行检测,以确定其是否满足第一预设条件,也即该内存块的可复用内存大小是否大于该第二张量所需内存大小,直至检测到满足上述第一预设条件的内存块为止。In some embodiments, each memory block can be tested according to the arrangement order of each memory block in the linked list to determine whether it meets the first preset condition, that is, whether the reusable memory size of the memory block is larger than the memory size required by the second tensor, until a memory block that meets the above-mentioned first preset condition is detected.

在一些实施例中,若在遍历所有内存块的信息块后,均未检测到满足第一预设条件的内存块,则可以在内存池中新申请一个内存块,以分配给该第二张量,同时记录该第二张量的内存块的信息块,并将该信息块按照相应内存块的内存大小,插入到链表中的相应位置,以更新该链表,从而使得在后序子图的内存复用中,可以对该第二张量的内存块也进行内存复用。In some embodiments, if no memory block that meets the first preset condition is detected after traversing the information blocks of all memory blocks, a new memory block can be applied for in the memory pool to be allocated to the second tensor. At the same time, the information block of the memory block of the second tensor is recorded, and the information block is inserted into the corresponding position in the linked list according to the memory size of the corresponding memory block to update the linked list, so that the memory block of the second tensor can also be reused in the memory reuse of the subsequent sub-graph.

在一些实施例中,复用标记信息还可以包括第二标记,第二标记用于表示相应内存块是否可以被正在进行第一操作的第二子图的第二张量所复用,第二操作还可以包括:响应于未获取到满足第一预设条件的内存块,从内存池中未分配的内存空间中,为该第二张量分配第二内存块;以及基于第二内存块的内存大小,将第二内存块的信息块插入到链表中,以获取更新后的链表,其中,第二内存块的第二标记的当前值置为第一值,第一值用于表示相应内存块不可以被正在进行第一操作的第二子图的第二张量所复用。In some embodiments, the reuse mark information may also include a second mark, and the second mark is used to indicate whether the corresponding memory block can be reused by the second tensor of the second subgraph that is undergoing the first operation. The second operation may also include: in response to not obtaining a memory block that meets the first preset condition, allocating a second memory block for the second tensor from the unallocated memory space in the memory pool; and based on the memory size of the second memory block, inserting the information block of the second memory block into the linked list to obtain an updated linked list, wherein the current value of the second mark of the second memory block is set to the first value, and the first value is used to indicate that the corresponding memory block cannot be reused by the second tensor of the second subgraph that is undergoing the first operation.

由此,当未获取到符合第一预设条件的内存块后,从内存池中未分配的部分新划分一个内存块分配给该第二张量,同时将该内存块的信息插入到链表中,以实现链表的更新,提升后续子图的内存复用率;同时,通过将第二标记设置为第一值,从而避免当前子图中的后续张量数据因为复用该内存块而造成数据覆盖的问题,提升数据的安全性和可靠性。Therefore, when a memory block that meets the first preset condition is not obtained, a new memory block is allocated from the unallocated part of the memory pool and allocated to the second tensor, and the information of the memory block is inserted into the linked list to update the linked list and improve the memory reuse rate of subsequent sub-graphs; at the same time, by setting the second mark to the first value, the problem of data overwrite caused by the reuse of the memory block in the subsequent tensor data in the current sub-graph is avoided, thereby improving the security and reliability of the data.

在一些实施例中,每个内存块的信息块的复用标记信息还可以包括第二标记,第二标记用于表示相应内存块是否可以被正在进行第一操作的第二子图(也即当前正在进行内存分配的第二子图)的第二张量所复用。上述链表mem_pool可以表示为:In some embodiments, the reuse mark information of the information block of each memory block may further include a second mark, and the second mark is used to indicate whether the corresponding memory block can be reused by the second tensor of the second subgraph that is currently performing the first operation (that is, the second subgraph that is currently performing memory allocation). The above linked list mem_pool can be expressed as:

std::list<Buffer>mem_pool;std::list<Buffer>mem_pool;

struct Buffer{struct Buffer{

NDArray pool;NDArray pool;

bool is_same_subgraph=false;bool is_same_subgraph=false;

size_t unuse_offset=0;size_t unuse_offset = 0;

};};

其中,Buffer表示用于记录上述信息块的结构体Buffer,NDArray pool表示内存块信息,unuse_offset表示第一标记(未占用地址偏移量),其初始值被设置为0,is_same_subgraph表示第二标记,其初始值可以为设置为false(第二值),用于表示相应内存块可以被正在进行第一操作的第二子图的第二张量所复用。Among them, Buffer represents a structure Buffer used to record the above information block, NDArray pool represents memory block information, unuse_offset represents a first tag (unoccupied address offset), whose initial value is set to 0, and is_same_subgraph represents a second tag, whose initial value can be set to false (second value), which is used to indicate that the corresponding memory block can be reused by the second tensor of the second subgraph that is performing the first operation.

当将上述新分配的内存块插入链表时,将该内存块的第二标记设置为第一值(例如为true),从而避免该第二子图中的后续第二张量复用该内存块,造成该第二子图在运行过程中的数据覆盖的问题。When the newly allocated memory block is inserted into the linked list, the second flag of the memory block is set to the first value (for example, true), so as to avoid the subsequent second tensor in the second subgraph reusing the memory block, causing the problem of data overwriting of the second subgraph during operation.

在一些实施例中,第一预设条件还可以包括相应内存块的第二标记的当前值为第二值,第二值表示相应内存块可以被正在进行第一操作的第二子图的第二张量所复用。In some embodiments, the first preset condition may further include that a current value of a second tag of the corresponding memory block is a second value, and the second value indicates that the corresponding memory block can be reused by a second tensor of a second subgraph that is undergoing the first operation.

由此,通过检测每个内存块的第二标记是否为第二值,从而避免在链表中可能存在刚刚插入的用于存放同一子图的张量数据的内存块的情况下,对该内存块进行复用而造成的数据覆盖的问题,提升数据的安全性和可靠性。Therefore, by detecting whether the second flag of each memory block is the second value, the problem of data overwrite caused by reusing the memory block that may have just been inserted into the linked list for storing tensor data of the same subgraph can be avoided, thereby improving data security and reliability.

在一些实施例中,第一操作还可以包括:在针对每个第二子图执行第二操作之前,将当前链表中的每个内存块的第二标记重置为第二值。In some embodiments, the first operation may further include: before performing the second operation on each second subgraph, resetting the second mark of each memory block in the current linked list to a second value.

由此,通过在对每个子图进行内存分配前,先将当前链表中所包含的内存块的第二标记重置为第二值,使得待分配内存的子图可以复用当前链表中的所有内存块,进一步提升内存块的复用率。Therefore, by resetting the second mark of the memory block contained in the current linked list to the second value before allocating memory to each subgraph, the subgraph to be allocated memory can reuse all the memory blocks in the current linked list, further improving the reuse rate of the memory block.

在一些实施例中,通过上述方法使每个子图中的每个张量数据都获取到用于缓存该张量数据的内存块后,即可在相应的运行装置(如CPU或GPU)上依次执行各个子图,从而在各个子图的运行过程中无需再对各个子图的数据进行内存分配,保证了多个子图的执行效率的同时,通过上述方法进一步降低了内存溢出的风险。In some embodiments, after each tensor data in each subgraph obtains the memory block used to cache the tensor data through the above method, each subgraph can be executed in sequence on the corresponding running device (such as CPU or GPU), so that there is no need to allocate memory for the data of each subgraph during the running process of each subgraph. While ensuring the execution efficiency of multiple subgraphs, the risk of memory overflow is further reduced through the above method.

图3示出了根据本公开的示例性实施例给出的用于包括多个子图的计算图的内存复用方法的流程框图。FIG3 shows a flowchart of a method for memory reuse of a computation graph including multiple subgraphs according to an exemplary embodiment of the present disclosure.

在一些示例性实施例中,如图3所示,提供了一种用于包括多个子图的计算图的内存复用方法,可以包括:步骤S301、创建全局有序链表mem_pool,以管理内存块;步骤S302、为第一子图的各个第一张量分配内存,并将每个内存块的信息块记录在链表mem_pool;步骤S303、将当前链表mem_pool中所有unuse_offset重置为0,所有is_same_subgraph重置为false;步骤S304、针对下一子图中的每个张量,均执行下述操作:步骤S305、在链表mem_pool中查找可复用内存块;步骤S306、响应于查找到可复用内存块,基于该张量所需内存大小、可复用内存块的地址范围和unuse_offset的当前值,在可复用内存块中确定该张量所复用的内存空间;步骤S307、更新链表mem_pool中该可复用内存块的unuse_offset,并对该子图中的下一张量执行步骤S305;步骤S308、响应于没有查找到可复用内存块,从内存池中未分配的内存空间中,为该张量分配内存块;步骤S309、将该内存块的信息块插入到链表mem_pool中,并将该内存块的is_same_subgraph重置为true,并对该子图中的下一张量执行步骤S305;步骤S310、响应于当前子图的每个张量均已执行上述内存分配的操作,执行步骤S303,并对下一子图执行步骤S304。In some exemplary embodiments, as shown in FIG3 , a memory reuse method for a computational graph including multiple subgraphs is provided, which may include: step S301, creating a global ordered linked list mem_pool to manage memory blocks; step S302, allocating memory for each first tensor of the first subgraph, and recording the information block of each memory block in the linked list mem_pool; step S303, resetting all unuse_offsets in the current linked list mem_pool to 0, and resetting all is_same_subgraphs to false; step S304, for each tensor in the next subgraph, performing the following operations: step S305, searching for a reusable memory block in the linked list mem_pool; step S306, in response to finding a reusable memory block, based on the memory size required for the tensor, the location of the reusable memory block The address range and the current value of unuse_offset are used to determine the memory space reused by the tensor in the reusable memory block; step S307, update the unuse_offset of the reusable memory block in the linked list mem_pool, and execute step S305 for the next tensor in the subgraph; step S308, in response to not finding a reusable memory block, allocate a memory block for the tensor from the unallocated memory space in the memory pool; step S309, insert the information block of the memory block into the linked list mem_pool, reset is_same_subgraph of the memory block to true, and execute step S305 for the next tensor in the subgraph; step S310, in response to each tensor of the current subgraph having executed the above-mentioned memory allocation operation, execute step S303, and execute step S304 for the next subgraph.

在一些实施例中,如图4所示,提供了一种用于包括多个子图的计算图的内存复用装置400,装置包括:分配单元410,被配置为为多个子图中首先被执行的第一子图的多个第一张量相应地分配多个内存块,多个内存块中每个内存块对应各自的内存块信息和复用标记信息,内存块信息包括相应内存块的地址范围和内存大小,复用标记信息包括用于记录相应内存块中已被复用的内存大小的第一标记;执行单元420,被配置为针对多个子图中在第一子图后执行的每个第二子图,分别执行下述第一操作,执行单元420包括:第一设置子单元421,被配置为将当前多个内存块中每个内存块的第一标记的值重置为零;以及执行子单元422,被配置为依次针对该第二子图的每个第二张量,执行如下第二操作,执行子单元422:获取模块422-1,被配置为基于该第二张量所需内存大小以及各个内存块的可复用内存大小,在当前的多个内存块中获取该第二张量的可复用内存块,其中,各个内存块的可复用内存大小基于相应内存块的内存大小和第一标记的当前值确定;确定模块422-2,被配置为响应于获取到该第二张量的可复用内存块,基于该第二张量所需内存大小、可复用内存块的地址范围和第一标记的当前值,在可复用内存块中确定该第二张量复用的内存空间;以及第一更新模块422-3,被配置为基于该第二张量复用的内存空间,更新可复用内存块的第一标记的值。In some embodiments, as shown in FIG. 4 , a memory reuse device 400 for a computation graph including multiple subgraphs is provided, the device comprising: an allocation unit 410, configured to allocate multiple memory blocks correspondingly to multiple first tensors of a first subgraph that is first executed in multiple subgraphs, each of the multiple memory blocks corresponding to respective memory block information and reuse mark information, the memory block information comprising an address range and a memory size of the corresponding memory block, the reuse mark information comprising a first mark for recording the size of the memory that has been reused in the corresponding memory block; an execution unit 420, configured to respectively perform the following first operation for each second subgraph executed after the first subgraph in the multiple subgraphs, the execution unit 420 comprising: a first setting subunit 421, configured to reset the value of the first mark of each memory block in the current multiple memory blocks to zero; and an execution subunit 422, The subunit 422 is configured to perform the following second operation for each second tensor of the second subgraph in turn: an acquisition module 422-1 is configured to acquire a reusable memory block of the second tensor in the current multiple memory blocks based on the memory size required by the second tensor and the reusable memory size of each memory block, wherein the reusable memory size of each memory block is determined based on the memory size of the corresponding memory block and the current value of the first tag; a determination module 422-2 is configured to determine the memory space reused by the second tensor in the reusable memory block in response to acquiring the reusable memory block of the second tensor based on the memory size required by the second tensor, the address range of the reusable memory block and the current value of the first tag; and a first update module 422-3 is configured to update the value of the first tag of the reusable memory block based on the memory space reused by the second tensor.

其中,上述用于包括多个子图的计算图的内存复用装置400中的单元410、单元420、子单元421、子单元422以及模块422-1~模块422-3所执行的操作与上述用于包括多个子图的计算图的内存复用方法中的步骤S201、步骤S202、步骤S2021、步骤S2022、步骤S2022-1~步骤S2022-3的操作类似,在此不做赘述。Among them, the operations performed by unit 410, unit 420, sub-unit 421, sub-unit 422 and module 422-1 to module 422-3 in the above-mentioned memory reuse device 400 for a computational graph including multiple sub-graphs are similar to the operations of steps S201, step S202, step S2021, step S2022, step S2022-1 to step S2022-3 in the above-mentioned memory reuse method for a computational graph including multiple sub-graphs, and are not repeated here.

在一些实施例中,多个内存块中每个内存块各自的内存块信息和复用标记信息作为一个信息块可以记录在链表中,并且各个内存块的信息块按照内存大小由小到大排列,获取模块可以被进一步配置为:依次检测当前链表中的各个内存块的信息是否满足第一预设条件,第一预设条件包括相应内存块的可复用内存大小大于该第二张量所需内存大小;以及响应于检测到满足第一预设条件的第一内存块,将第一内存块确定为该第二张量的可复用内存块。In some embodiments, the memory block information and reuse mark information of each memory block in a plurality of memory blocks can be recorded in a linked list as an information block, and the information blocks of each memory block are arranged from small to large according to the memory size. The acquisition module can be further configured to: detect in turn whether the information of each memory block in the current linked list satisfies a first preset condition, the first preset condition including that the reusable memory size of the corresponding memory block is greater than the memory size required by the second tensor; and in response to detecting a first memory block that satisfies the first preset condition, determine the first memory block as a reusable memory block for the second tensor.

在一些实施例中,复用标记信息还包括第二标记,第二标记用于表示相应内存块是否可以被正在进行第一操作的第二子图的第二张量所复用,执行子单元还可以包括:分配模块,被配置为响应于未获取到满足第一预设条件的内存块,从内存池中未分配的内存空间中,为该第二张量分配第二内存块;以及第二更新模块,被配置为基于第二内存块的内存大小,将第二内存块的信息块插入到链表中,以获取更新后的链表,其中,第二内存块的第二标记的当前值置为第一值,第一值用于表示相应内存块不可以被正在进行第一操作的第二子图的第二张量所复用。In some embodiments, the reuse mark information also includes a second mark, and the second mark is used to indicate whether the corresponding memory block can be reused by the second tensor of the second subgraph that is undergoing the first operation. The execution subunit may also include: an allocation module, configured to allocate a second memory block for the second tensor from the unallocated memory space in the memory pool in response to not obtaining a memory block that meets the first preset condition; and a second update module, configured to insert the information block of the second memory block into the linked list based on the memory size of the second memory block to obtain an updated linked list, wherein the current value of the second mark of the second memory block is set to the first value, and the first value is used to indicate that the corresponding memory block cannot be reused by the second tensor of the second subgraph that is undergoing the first operation.

在一些实施例中,第一预设条件还可以包括相应内存块的第二标记的当前值为第二值,第二值表示相应内存块可以可以被正在进行第一操作的第二子图的第二张量所复用。In some embodiments, the first preset condition may further include that a current value of a second tag of the corresponding memory block is a second value, and the second value indicates that the corresponding memory block can be reused by a second tensor of a second subgraph that is undergoing the first operation.

在一些实施例中,执行单元还可以包括:第二设置子单元,被配置为在针对每个第二子图执行第二操作之前,将当前链表中的每个内存块的第二标记重置为第二值。In some embodiments, the execution unit may further include: a second setting subunit configured to reset the second mark of each memory block in the current linked list to a second value before executing the second operation on each second subgraph.

在一些实施例中,多个内存块中每个内存块的内存大小可以基于相应张量的所需内存大小确定。In some embodiments, the memory size of each memory block in the plurality of memory blocks may be determined based on a required memory size of a corresponding tensor.

根据本公开的实施例,还提供了一种电子设备、一种可读存储介质和一种计算机程序产品。According to an embodiment of the present disclosure, an electronic device, a readable storage medium and a computer program product are also provided.

参考图5,现将描述可以作为本公开的服务器或客户端的电子设备500的结构框图,其是可以应用于本公开的各方面的硬件设备的示例。电子设备旨在表示各种形式的数字电子的计算机设备,诸如,膝上型计算机、台式计算机、工作台、个人数字助理、服务器、刀片式服务器、大型计算机、和其它适合的计算机。电子设备还可以表示各种形式的移动装置,诸如,个人数字处理、蜂窝电话、智能电话、可穿戴设备和其它类似的计算装置。本文所示的部件、它们的连接和关系、以及它们的功能仅仅作为示例,并且不意在限制本文中描述的和/或者要求的本公开的实现。With reference to Fig. 5, the structural block diagram of the electronic device 500 that can be used as the server or client of the present disclosure will now be described, which is an example of a hardware device that can be applied to various aspects of the present disclosure. The electronic device is intended to represent various forms of digital electronic computer equipment, such as laptop computers, desktop computers, workbenches, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device can also represent various forms of mobile devices, such as personal digital processing, cellular phones, smart phones, wearable devices and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely examples, and are not intended to limit the implementation of the present disclosure described and/or required herein.

如图5所示,电子设备500包括计算单元501,其可以根据存储在只读存储器(ROM)502中的计算机程序或者从存储单元508加载到随机访问存储器(RAM)503中的计算机程序,来执行各种适当的动作和处理。在RAM 503中,还可存储电子设备500操作所需的各种程序和数据。计算单元501、ROM 502以及RAM 503通过总线504彼此相连。输入/输出(I/O)接口505也连接至总线504。As shown in Figure 5, electronic device 500 includes a computing unit 501, which can perform various appropriate actions and processes according to a computer program stored in a read-only memory (ROM) 502 or a computer program loaded from a storage unit 508 into a random access memory (RAM) 503. In RAM 503, various programs and data required for the operation of electronic device 500 can also be stored. Computing unit 501, ROM 502 and RAM 503 are connected to each other via bus 504. Input/output (I/O) interface 505 is also connected to bus 504.

电子设备500中的多个部件连接至I/O接口505,包括:输入单元506、输出单元507、存储单元508以及通信单元509。输入单元506可以是能向电子设备500输入信息的任何类型的设备,输入单元506可以接收输入的数字或字符信息,以及产生与电子设备的用户设置和/或功能控制有关的键信号输入,并且可以包括但不限于鼠标、键盘、触摸屏、轨迹板、轨迹球、操作杆、麦克风和/或遥控器。输出单元507可以是能呈现信息的任何类型的设备,并且可以包括但不限于显示器、扬声器、视频/音频输出终端、振动器和/或打印机。存储单元508可以包括但不限于磁盘、光盘。通信单元509允许电子设备500通过诸如因特网的计算机网络和/或各种电信网络与其他设备交换信息/数据,并且可以包括但不限于调制解调器、网卡、红外通信设备、无线通信收发机和/或芯片组,例如蓝牙设备、802.11设备、WiFi设备、WiMax设备、蜂窝通信设备和/或类似物。Multiple components in the electronic device 500 are connected to the I/O interface 505, including: an input unit 506, an output unit 507, a storage unit 508, and a communication unit 509. The input unit 506 can be any type of device that can input information to the electronic device 500. The input unit 506 can receive input digital or character information and generate key signal input related to user settings and/or function control of the electronic device, and can include but is not limited to a mouse, a keyboard, a touch screen, a track pad, a track ball, a joystick, a microphone, and/or a remote controller. The output unit 507 can be any type of device that can present information, and can include but is not limited to a display, a speaker, a video/audio output terminal, a vibrator, and/or a printer. The storage unit 508 can include but is not limited to a disk, an optical disk. The communication unit 509 allows the electronic device 500 to exchange information/data with other devices through a computer network such as the Internet and/or various telecommunication networks, and can include but is not limited to a modem, a network card, an infrared communication device, a wireless communication transceiver, and/or a chipset, such as a Bluetooth device, an 802.11 device, a WiFi device, a WiMax device, a cellular communication device, and/or the like.

计算单元501可以是各种具有处理和计算能力的通用和/或专用处理组件。计算单元501的一些示例包括但不限于中央处理单元(CPU)、图形处理单元(GPU)、各种专用的人工智能(AI)计算芯片、各种运行机器学习模型算法的计算单元、数字信号处理器(DSP)、以及任何适当的处理器、控制器、微控制器等。计算单元501执行上文所描述的各个方法和处理,例如上述用于包括多个子图的计算图的内存复用方法。例如,在一些实施例中,上述用于包括多个子图的计算图的内存复用方法可被实现为计算机软件程序,其被有形地包含于机器可读介质,例如存储单元508。在一些实施例中,计算机程序的部分或者全部可以经由ROM502和/或通信单元509而被载入和/或安装到电子设备500上。当计算机程序加载到RAM 503并由计算单元501执行时,可以执行上述用于包括多个子图的计算图的内存复用方法的一个或多个步骤。备选地,在其他实施例中,计算单元501可以通过其他任何适当的方式(例如,借助于固件)而被配置为执行上述用于包括多个子图的计算图的内存复用方法。The computing unit 501 may be a variety of general and/or special processing components with processing and computing capabilities. Some examples of the computing unit 501 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various dedicated artificial intelligence (AI) computing chips, various computing units running machine learning model algorithms, digital signal processors (DSPs), and any appropriate processors, controllers, microcontrollers, etc. The computing unit 501 performs the various methods and processes described above, such as the above-mentioned memory reuse method for a computing graph including multiple subgraphs. For example, in some embodiments, the above-mentioned memory reuse method for a computing graph including multiple subgraphs may be implemented as a computer software program, which is tangibly contained in a machine-readable medium, such as a storage unit 508. In some embodiments, part or all of the computer program may be loaded and/or installed on the electronic device 500 via ROM 502 and/or communication unit 509. When the computer program is loaded into RAM 503 and executed by the computing unit 501, one or more steps of the above-mentioned memory reuse method for a computing graph including multiple subgraphs may be performed. Alternatively, in other embodiments, the computing unit 501 may be configured in any other appropriate manner (e.g., by means of firmware) to execute the above-mentioned memory reuse method for a computation graph including multiple subgraphs.

本文中以上描述的系统和技术的各种实施方式可以在数字电子电路系统、集成电路系统、场可编程门阵列(FPGA)、专用集成电路(ASIC)、专用标准产品(ASSP)、芯片上系统的系统(SOC)、复杂可编程逻辑设备(CPLD)、计算机硬件、固件、软件、和/或它们的组合中实现。这些各种实施方式可以包括:实施在一个或者多个计算机程序中,该一个或者多个计算机程序可在包括至少一个可编程处理器的可编程系统上执行和/或解释,该可编程处理器可以是专用或者通用可编程处理器,可以从存储系统、至少一个输入装置、和至少一个输出装置接收数据和指令,并且将数据和指令传输至该存储系统、该至少一个输入装置、和该至少一个输出装置。Various implementations of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chips (SOCs), complex programmable logic devices (CPLDs), computer hardware, firmware, software, and/or combinations thereof. These various implementations can include: being implemented in one or more computer programs that can be executed and/or interpreted on a programmable system including at least one programmable processor, which can be a special purpose or general purpose programmable processor that can receive data and instructions from a storage system, at least one input device, and at least one output device, and transmit data and instructions to the storage system, the at least one input device, and the at least one output device.

用于实施本公开的方法的程序代码可以采用一个或多个编程语言的任何组合来编写。这些程序代码可以提供给通用计算机、专用计算机或其他可编程数据处理装置的处理器或控制器,使得程序代码当由处理器或控制器执行时使流程图和/或框图中所规定的功能/操作被实施。程序代码可以完全在机器上执行、部分地在机器上执行,作为独立软件包部分地在机器上执行且部分地在远程机器上执行或完全在远程机器或服务器上执行。The program code for implementing the method of the present disclosure may be written in any combination of one or more programming languages. These program codes may be provided to a processor or controller of a general-purpose computer, a special-purpose computer, or other programmable data processing device, so that the program code, when executed by the processor or controller, implements the functions/operations specified in the flow chart and/or block diagram. The program code may be executed entirely on the machine, partially on the machine, partially on the machine and partially on a remote machine as a stand-alone software package, or entirely on a remote machine or server.

在本公开的上下文中,机器可读介质可以是有形的介质,其可以包含或存储以供指令执行系统、装置或设备使用或与指令执行系统、装置或设备结合地使用的程序。机器可读介质可以是机器可读信号介质或机器可读储存介质。机器可读介质可以包括但不限于电子的、磁性的、光学的、电磁的、红外的、或半导体系统、装置或设备,或者上述内容的任何合适组合。机器可读存储介质的更具体示例会包括基于一个或多个线的电气连接、便携式计算机盘、硬盘、随机存取存储器(RAM)、只读存储器(ROM)、可擦除可编程只读存储器(EPROM或快闪存储器)、光纤、便捷式紧凑盘只读存储器(CD-ROM)、光学储存设备、磁储存设备、或上述内容的任何合适组合。In the context of the present disclosure, a machine-readable medium may be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, device, or equipment. A machine-readable medium may be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium may include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or equipment, or any suitable combination of the foregoing. A more specific example of a machine-readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.

为了提供与用户的交互,可以在计算机上实施此处描述的系统和技术,该计算机具有:用于向用户显示信息的显示装置(例如,CRT(阴极射线管)或者LCD(液晶显示器)监视器);以及键盘和指向装置(例如,鼠标或者轨迹球),用户可以通过该键盘和该指向装置来将输入提供给计算机。其它种类的装置还可以用于提供与用户的交互;例如,提供给用户的反馈可以是任何形式的传感反馈(例如,视觉反馈、听觉反馈、或者触觉反馈);并且可以用任何形式(包括声输入、语音输入或者、触觉输入)来接收来自用户的输入。To provide interaction with a user, the systems and techniques described herein can be implemented on a computer having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and pointing device (e.g., a mouse or trackball) through which the user can provide input to the computer. Other types of devices can also be used to provide interaction with the user; for example, the feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including acoustic input, voice input, or tactile input).

可以将此处描述的系统和技术实施在包括后台部件的计算系统(例如,作为数据服务器)、或者包括中间件部件的计算系统(例如,应用服务器)、或者包括前端部件的计算系统(例如,具有图形用户界面或者网络浏览器的用户计算机,用户可以通过该图形用户界面或者该网络浏览器来与此处描述的系统和技术的实施方式交互)、或者包括这种后台部件、中间件部件、或者前端部件的任何组合的计算系统中。可以通过任何形式或者介质的数字数据通信(例如,通信网络)来将系统的部件相互连接。通信网络的示例包括:局域网(LAN)、广域网(WAN)和互联网。The systems and techniques described herein may be implemented in a computing system that includes back-end components (e.g., as a data server), or a computing system that includes middleware components (e.g., an application server), or a computing system that includes front-end components (e.g., a user computer with a graphical user interface or a web browser through which a user can interact with implementations of the systems and techniques described herein), or a computing system that includes any combination of such back-end components, middleware components, or front-end components. The components of the system may be interconnected by any form or medium of digital data communication (e.g., a communications network). Examples of communications networks include: a local area network (LAN), a wide area network (WAN), and the Internet.

计算机系统可以包括客户端和服务器。客户端和服务器一般远离彼此并且通常通过通信网络进行交互。通过在相应的计算机上运行并且彼此具有客户端-服务器关系的计算机程序来产生客户端和服务器的关系。服务器可以是云服务器,也可以为分布式系统的服务器,或者是结合了区块链的服务器。A computer system may include a client and a server. The client and the server are generally remote from each other and usually interact through a communication network. The relationship of client and server is generated by computer programs running on respective computers and having a client-server relationship with each other. The server may be a cloud server, a server of a distributed system, or a server combined with a blockchain.

应该理解,可以使用上面所示的各种形式的流程,重新排序、增加或删除步骤。例如,本公开中记载的各步骤可以并行地执行、也可以顺序地或以不同的次序执行,只要能够实现本公开公开的技术方案所期望的结果,本文在此不进行限制。It should be understood that the various forms of processes shown above can be used to reorder, add or delete steps. For example, the steps described in this disclosure can be performed in parallel, sequentially or in a different order, as long as the desired results of the technical solutions disclosed in this disclosure can be achieved, and this document is not limited here.

虽然已经参照附图描述了本公开的实施例或示例,但应理解,上述的方法、系统和设备仅仅是示例性的实施例或示例,本发明的范围并不由这些实施例或示例限制,而是仅由授权后的权利要求书及其等同范围来限定。实施例或示例中的各种要素可以被省略或者可由其等同要素替代。此外,可以通过不同于本公开中描述的次序来执行各步骤。进一步地,可以以各种方式组合实施例或示例中的各种要素。重要的是随着技术的演进,在此描述的很多要素可以由本公开之后出现的等同要素进行替换。Although the embodiments or examples of the present disclosure have been described with reference to the accompanying drawings, it should be understood that the above-mentioned methods, systems and devices are merely exemplary embodiments or examples, and the scope of the present invention is not limited by these embodiments or examples, but only by the claims after authorization and their equivalent scope. Various elements in the embodiments or examples may be omitted or replaced by their equivalent elements. In addition, each step may be performed in an order different from that described in the present disclosure. Further, the various elements in the embodiments or examples may be combined in various ways. It is important that with the evolution of technology, many of the elements described herein may be replaced by equivalent elements that appear after the present disclosure.

Claims (15)

1. A memory multiplexing method for a computational graph comprising a plurality of subgraphs, the method comprising:
Correspondingly distributing a plurality of memory blocks for a plurality of first tensors of a first sub-graph which is executed first in the plurality of sub-graphs, wherein each memory block in the plurality of memory blocks corresponds to respective memory block information and multiplexing mark information, the memory block information comprises an address range and a memory size of the corresponding memory block, and the multiplexing mark information comprises a first mark used for recording the reusable memory size in the corresponding memory block;
for each second sub-graph of the plurality of sub-graphs, which is executed after the first sub-graph, respectively executing the following first operations:
resetting the value of the first flag of each memory block in the current plurality of memory blocks to a preset value, wherein the preset value represents that all memories in the corresponding memory blocks can be multiplexed; and
For each second tensor of the second sub-graph in turn, the following second operation is performed:
Acquiring reusable memory blocks of the second tensor from the plurality of memory blocks based on the memory size required by the second tensor and the current value of the first mark of each memory block;
In response to obtaining the reusable memory block of the second tensor, determining a memory space for multiplexing the second tensor in the reusable memory block based on the required memory size of the second tensor, the address range of the reusable memory block and the current value of the first flag; and
And updating the value of the first mark of the reusable memory block based on the memory space multiplexed by the second tensor.
2. The method of claim 1, wherein the respective memory block information and multiplexing flag information of each of the plurality of memory blocks are recorded as one information block in a linked list, and the information blocks of the respective memory blocks are arranged from small to large according to the memory size, and the obtaining the reusable memory block of the second tensor in the current plurality of memory blocks based on the memory size required by the second tensor and the current value of the first flag of the respective memory block comprises:
Sequentially detecting whether the information of each memory block in the current linked list meets a first preset condition, wherein the first preset condition comprises that the size of the reusable memory of the corresponding memory block is larger than the size of the memory required by the second tensor; and
And in response to detecting the first memory block meeting the first preset condition, determining the first memory block as the reusable memory block of the second tensor.
3. The method of claim 2, wherein the multiplexing flag information further comprises a second flag indicating whether the corresponding memory block can be multiplexed by a second tensor of a second sub-graph that is performing the first operation, the second operation further comprising:
In response to not obtaining the memory blocks meeting the first preset condition, distributing a second memory block for the second tensor from unallocated memory spaces in the memory pool; and
And inserting the information block of the second memory block into the linked list based on the memory size of the second memory block to acquire an updated linked list, wherein the current value of the second flag of the second memory block is set to be a first value, and the first value is used for indicating that the corresponding memory block cannot be multiplexed by the second tensor of the second sub-graph in which the first operation is being performed.
4. The method of claim 3, wherein the first preset condition further comprises a current value of a second flag of the corresponding memory block being a second value indicating that the corresponding memory block may be multiplexed by a second tensor of a second sub-graph in which the first operation is being performed.
5. The method of claim 4, wherein the first operation further comprises:
and before the second operation is executed for each second sub-graph, resetting the second mark of each memory block in the current linked list to the second value.
6. The method of any of claims 1-5, wherein a memory size of each memory block of the plurality of memory blocks is determined based on a desired memory size of a corresponding tensor.
7. A memory multiplexing device for a computational graph comprising a plurality of subgraphs, the device comprising:
An allocation unit configured to allocate a plurality of memory blocks, for a plurality of first tensors of a first sub-graph that is executed first among the plurality of sub-graphs, respectively, each memory block of the plurality of memory blocks corresponding to respective memory block information including an address range and a memory size of the corresponding memory block and multiplexing flag information including a first flag for recording a reusable memory size in the corresponding memory block;
an execution unit configured to perform the following first operations, respectively, for each second sub-graph of the plurality of sub-graphs, which is executed after the first sub-graph, the execution unit including:
a first setting subunit configured to reset a value of a first flag of each memory block of the current plurality of memory blocks to a preset value, where the preset value indicates that all memories in the corresponding memory block can be multiplexed; and
An execution subunit configured to perform, for each second tensor of the second sub-graph in turn, a second operation of:
an obtaining module configured to obtain a reusable memory block of the second tensor from the plurality of memory blocks at present based on a required memory size of the second tensor and a current value of a first flag of each memory block;
a determining module configured to determine, in response to acquiring the reusable memory block of the second tensor, a memory space multiplexed by the second tensor in the reusable memory block based on a required memory size of the second tensor, an address range of the reusable memory block, and a current value of the first flag; and
And a first updating module configured to update a value of a first flag of the reusable memory block based on the memory space multiplexed by the second tensor.
8. The apparatus of claim 7, wherein the respective memory block information and multiplexing flag information for each of the plurality of memory blocks are recorded as one information block in a linked list, and the information blocks for the respective memory blocks are arranged in a memory size from small to large, the acquisition module being further configured to:
Sequentially detecting whether the information of each memory block in the current linked list meets a first preset condition, wherein the first preset condition comprises that the size of the reusable memory of the corresponding memory block is larger than the size of the memory required by the second tensor; and
And in response to detecting the first memory block meeting the first preset condition, determining the first memory block as the reusable memory block of the second tensor.
9. The apparatus of claim 8, wherein the multiplexing flag information further comprises a second flag indicating whether a corresponding memory block can be multiplexed by a second tensor of a second sub-graph that is performing the first operation, the execution subunit further comprising:
The allocation module is configured to allocate a second memory block for the second tensor from unallocated memory spaces in the memory pool in response to not acquiring the memory block meeting the first preset condition; and
And a second updating module configured to insert an information block of the second memory block into the linked list based on a memory size of the second memory block to obtain an updated linked list, where a current value of a second flag of the second memory block is set to a first value, and the first value is used to indicate that the corresponding memory block cannot be multiplexed by a second tensor of a second sub-graph that is performing the first operation.
10. The apparatus of claim 9, wherein the first preset condition further comprises a current value of a second flag of the respective memory block being a second value indicating that the respective memory block may be multiplexed by a second tensor of a second sub-graph that is performing the first operation.
11. The apparatus of claim 10, the execution unit further comprising:
And the second setting subunit is configured to reset the second flag of each memory block in the current linked list to the second value before the second operation is performed on each second sub-graph.
12. The apparatus of any of claims 7-11, wherein a memory size of each memory block of the plurality of memory blocks is determined based on a desired memory size of a respective tensor.
13. An electronic device, comprising:
At least one processor; and
A memory communicatively coupled to the at least one processor; wherein the method comprises the steps of
The memory stores instructions executable by the at least one processor to enable the at least one processor to perform the method of any one of claims 1-6.
14. A non-transitory computer readable storage medium storing computer instructions for causing a computer to perform the method of any one of claims 1-6.
15. A computer program product comprising a computer program, wherein the computer program, when executed by a processor, implements the method of any of claims 1-6.
CN202410773908.XA 2024-06-14 2024-06-14 Memory multiplexing method, device, equipment and medium Pending CN118672779A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202410773908.XA CN118672779A (en) 2024-06-14 2024-06-14 Memory multiplexing method, device, equipment and medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202410773908.XA CN118672779A (en) 2024-06-14 2024-06-14 Memory multiplexing method, device, equipment and medium

Publications (1)

Publication Number Publication Date
CN118672779A true CN118672779A (en) 2024-09-20

Family

ID=92729427

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202410773908.XA Pending CN118672779A (en) 2024-06-14 2024-06-14 Memory multiplexing method, device, equipment and medium

Country Status (1)

Country Link
CN (1) CN118672779A (en)

Similar Documents

Publication Publication Date Title
EP4152178A1 (en) Method for training image-text matching model, apparatus, storage medium, and computer program product
CN114091672B (en) Distributed model reasoning method and device, electronic equipment and medium
CN112527281B (en) Operator upgrading method and device based on artificial intelligence, electronic equipment and medium
CN113986788B (en) Data processing method and device, chip, electronic equipment and medium
US20220237376A1 (en) Method, apparatus, electronic device and storage medium for text classification
CN113377614A (en) Generation method and device of call chain information, electronic equipment and storage medium
CN113220367A (en) Applet running method and device, electronic equipment and storage medium
CN113377809A (en) Data processing method and apparatus, computing device, and medium
CN116306396A (en) Chip verification method and device, equipment and medium
CN114417780B (en) State synchronization method and device, electronic equipment and storage medium
CN113641544A (en) Method, apparatus, device, medium and product for detecting application status
CN112579587A (en) Data cleaning method and device, equipment and storage medium
WO2024193001A1 (en) Upgrading method and apparatus, device, and storage medium
CN118672779A (en) Memory multiplexing method, device, equipment and medium
CN114998963A (en) Image detection method and method for training image detection model
CN114860455A (en) Request processing method, device, equipment and storage medium
CN112860681A (en) Data cleaning method and device, computer equipment and medium
CN116842156B (en) Data generation method, device, equipment and medium
CN118733259A (en) Method, device, equipment and medium for segmenting computational graphs of deep learning tasks
EP4152174A1 (en) Data processing method and apparatus, and computing device and medium
CN118821675A (en) Chip verification method, device, equipment and medium
CN118821676A (en) Chip verification method, device, equipment and medium
CN118132001A (en) Data processing method, data processing system, chip, device and medium
CN114399333A (en) Method, device, equipment and storage medium for detecting medium information releasing effect
CN118940686A (en) Chip verification method, device, equipment and medium

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination