Skip to content

Commit 62aa950

Browse files
srkreddy1238echuraev
authored andcommitted
[VM] Memory Manager moved up to runtime (apache#15833)
* [VM] memory Manager moved up to runtime Now graph runtime also uses the same memory manager This acommodates a common memory manager with pooled and naive support. As a follow up we can move the WorkspacePool to use this common memory manager. * * update dependents with new file addition. * * define memory_manager under new namespace * * use ShapeTuple across vm executor and memory_manager * * ShapeTuple across the Allocators * * GetDataSize is moved to DeviceAPI and memory_manager uses this interface. * * review comments * * Make compiler happy with unused variables * * lint * Update src/runtime/memory/memory_manager.cc Co-authored-by: Egor Churaev <egor.churaev@gmail.com> * * allow multiple allocators to coexist for the same device. Using available allocator instead of requested is leading to an unpexpected crash --------- Co-authored-by: Egor Churaev <egor.churaev@gmail.com>
1 parent 8a2ca34 commit 62aa950

File tree

19 files changed

+159
-94
lines changed

19 files changed

+159
-94
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ list(APPEND COMPILER_SRCS "src/target/datatype/myfloat/myfloat.cc")
356356
tvm_file_glob(GLOB RUNTIME_SRCS
357357
src/runtime/*.cc
358358
src/runtime/vm/*.cc
359+
src/runtime/memory/*.cc
359360
src/runtime/disco/*.cc
360361
src/runtime/minrpc/*.cc
361362
src/runtime/relax_vm/*.cc

apps/android_camera/app/src/main/jni/tvm_runtime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "../src/runtime/graph_executor/graph_executor.cc"
4141
#include "../src/runtime/library_module.cc"
4242
#include "../src/runtime/logging.cc"
43+
#include "../src/runtime/memory/memory_manager.cc"
4344
#include "../src/runtime/minrpc/minrpc_logger.cc"
4445
#include "../src/runtime/module.cc"
4546
#include "../src/runtime/ndarray.cc"

apps/android_deploy/app/src/main/jni/tvm_runtime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "../src/runtime/graph_executor/graph_executor.cc"
3636
#include "../src/runtime/library_module.cc"
3737
#include "../src/runtime/logging.cc"
38+
#include "../src/runtime/memory/memory_manager.cc"
3839
#include "../src/runtime/module.cc"
3940
#include "../src/runtime/ndarray.cc"
4041
#include "../src/runtime/object.cc"

apps/android_rpc/app/src/main/jni/tvm_runtime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "../src/runtime/graph_executor/graph_executor_factory.cc"
4343
#include "../src/runtime/library_module.cc"
4444
#include "../src/runtime/logging.cc"
45+
#include "../src/runtime/memory/memory_manager.cc"
4546
#include "../src/runtime/minrpc/minrpc_logger.cc"
4647
#include "../src/runtime/module.cc"
4748
#include "../src/runtime/ndarray.cc"

apps/bundle_deploy/runtime.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "../../src/runtime/graph_executor/graph_executor.cc"
3030
#include "../../src/runtime/library_module.cc"
3131
#include "../../src/runtime/logging.cc"
32+
#include "../../src/runtime/memory/memory_manager.cc"
3233
#include "../../src/runtime/module.cc"
3334
#include "../../src/runtime/ndarray.cc"
3435
#include "../../src/runtime/object.cc"

apps/howto_deploy/tvm_runtime_pack.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
// Graph executor
6565
#include "../../src/runtime/graph_executor/graph_executor.cc"
6666
#include "../../src/runtime/graph_executor/graph_executor_factory.cc"
67+
#include "../../src/runtime/memory/memory_manager.cc"
6768

6869
// Uncomment the following lines to enable RPC
6970
// #include "../../src/runtime/rpc/rpc_session.cc"

golang/src/tvm_runtime_pack.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646

4747
// Graph executor
4848
#include "src/runtime/graph_executor/graph_executor.cc"
49+
#include "src/runtime/memory/memory_manager.cc"
4950

5051
// Uncomment the following lines to enable RPC
5152
// #include "../../src/runtime/rpc/rpc_session.cc"

include/tvm/runtime/device_api.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ class TVM_DLL DeviceAPI {
9595
*/
9696
virtual void GetAttr(Device dev, DeviceAttrKind kind, TVMRetValue* rv) = 0;
9797

98+
/*!
99+
* \brief Get the physical memory size required.
100+
* \param arr the tensor object.
101+
* \param mem_scope the memory scope if any
102+
* \return the memory size.
103+
*/
104+
virtual size_t GetDataSize(const DLTensor& arr, Optional<String> mem_scope = NullOpt);
105+
98106
/*!
99107
* \brief Query the device for specified properties.
100108
*

include/tvm/runtime/vm/memory_manager.h renamed to include/tvm/runtime/memory/memory_manager.h

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818
*/
1919

2020
/*!
21-
* \file tvm/runtime/vm/memory_manager.h
21+
* \file tvm/runtime/memory/memory_manager.h
2222
* \brief Abstract device memory management API
2323
*/
24-
#ifndef TVM_RUNTIME_VM_MEMORY_MANAGER_H_
25-
#define TVM_RUNTIME_VM_MEMORY_MANAGER_H_
24+
#ifndef TVM_RUNTIME_MEMORY_MEMORY_MANAGER_H_
25+
#define TVM_RUNTIME_MEMORY_MEMORY_MANAGER_H_
2626

2727
#include <tvm/runtime/c_runtime_api.h>
2828
#include <tvm/runtime/ndarray.h>
@@ -37,22 +37,22 @@
3737

3838
namespace tvm {
3939
namespace runtime {
40-
namespace vm {
40+
namespace memory {
41+
42+
enum AllocatorType {
43+
kNaive = 1,
44+
kPooled,
45+
};
4146

4247
struct Buffer {
4348
/*! \brief The pointer to the allocated block of memory. */
4449
void* data{nullptr};
4550
/*! \brief The size of the block. */
4651
size_t size{0};
47-
/*! \brief The shape of the tensor. */
48-
std::vector<int64_t> shape;
4952
/*! \brief The context of the allocated buffers. */
5053
Device device;
51-
};
52-
53-
enum AllocatorType {
54-
kNaive = 1,
55-
kPooled,
54+
/*! \brief The allocator that created this buffer. */
55+
AllocatorType alloc_type;
5656
};
5757

5858
class Allocator {
@@ -63,9 +63,11 @@ class Allocator {
6363
* \param shape The shape of the NDArray.
6464
* \param dtype The datatype of the NDArray.
6565
* \param dev The device where the array is allocated.
66+
* \param mem_scope The device memory scope hint.
6667
* \return The empty NDArray.
6768
*/
68-
NDArray Empty(std::vector<int64_t> shape, DLDataType dtype, Device dev);
69+
NDArray Empty(ShapeTuple shape, DLDataType dtype, Device dev,
70+
Optional<String> mem_scope = NullOpt);
6971
/*! \brief Return the allocator type. */
7072
inline AllocatorType type() const { return type_; }
7173
/*! \brief Allocate a buffer given a size, alignment and type.
@@ -76,13 +78,12 @@ class Allocator {
7678
*/
7779
virtual Buffer Alloc(size_t nbytes, size_t alignment, DLDataType type_hint) = 0;
7880
/*! \brief Allocate a buffer given a shape and type.
79-
* \param ndims The rank of the tensor.
8081
* \param shape The shape of the tensor.
8182
* \param type_hint A type hint to the allocator.
8283
* \param mem_scope A memory scope of the buffer.
8384
* \return A sized allocation in the form of a buffer.
8485
*/
85-
virtual Buffer Alloc(int ndims, int64_t* shape, DLDataType type_hint,
86+
virtual Buffer Alloc(ShapeTuple shape, DLDataType type_hint,
8687
const std::string& mem_scope = "") = 0;
8788
/*! \brief Free a buffer allocated by the allocator.
8889
* \param buffer The buffer to free.
@@ -94,7 +95,7 @@ class Allocator {
9495
virtual size_t UsedMemory() const = 0;
9596

9697
protected:
97-
virtual Buffer Alloc(Device dev, int ndims, int64_t* shape, DLDataType type_hint,
98+
virtual Buffer Alloc(Device dev, ShapeTuple shape, DLDataType type_hint,
9899
const std::string& mem_scope);
99100

100101
private:
@@ -114,16 +115,18 @@ class MemoryManager {
114115
/*!
115116
* \brief Get an allocator given the context.
116117
* \param dev The TVM device
118+
* \param type The allocator type
117119
* \return The memory allocator.
118120
*/
119-
static Allocator* GetAllocator(Device dev);
121+
static Allocator* GetAllocator(Device dev, AllocatorType type);
120122

121123
private:
122124
MemoryManager() {}
123125

124126
protected:
125127
std::mutex mu_;
126-
std::unordered_map<Device, std::unique_ptr<Allocator>> allocators_;
128+
std::unordered_map<Device, std::unordered_map<AllocatorType, std::unique_ptr<Allocator>>>
129+
allocators_;
127130
};
128131

129132
/*! \brief An object representing a storage allocation. */
@@ -133,13 +136,13 @@ class StorageObj : public Object {
133136
Buffer buffer;
134137

135138
/*! \brief Allocate an NDArray from a given piece of storage. */
136-
NDArray AllocNDArray(size_t offset, std::vector<int64_t> shape, DLDataType dtype);
139+
NDArray AllocNDArray(size_t offset, ShapeTuple shape, DLDataType dtype);
137140

138141
/*! \brief The deleter for an NDArray when allocated from underlying storage. */
139142
static void Deleter(Object* ptr);
140143

141144
~StorageObj() {
142-
auto alloc = MemoryManager::Global()->GetAllocator(buffer.device);
145+
auto alloc = MemoryManager::Global()->GetAllocator(buffer.device, buffer.alloc_type);
143146
alloc->Free(buffer);
144147
}
145148

@@ -156,8 +159,8 @@ class Storage : public ObjectRef {
156159
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(Storage, ObjectRef, StorageObj);
157160
};
158161

159-
} // namespace vm
162+
} // namespace memory
160163
} // namespace runtime
161164
} // namespace tvm
162165

163-
#endif // TVM_RUNTIME_VM_MEMORY_MANAGER_H_
166+
#endif // TVM_RUNTIME_MEMORY_MEMORY_MANAGER_H_

include/tvm/runtime/vm/vm.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@
2525
#define TVM_RUNTIME_VM_VM_H_
2626

2727
#include <tvm/runtime/container/closure.h>
28+
#include <tvm/runtime/memory/memory_manager.h>
2829
#include <tvm/runtime/module.h>
2930
#include <tvm/runtime/object.h>
3031
#include <tvm/runtime/packed_func.h>
3132
#include <tvm/runtime/registry.h>
3233
#include <tvm/runtime/vm/bytecode.h>
3334
#include <tvm/runtime/vm/executable.h>
34-
#include <tvm/runtime/vm/memory_manager.h>
3535

3636
#include <memory>
3737
#include <string>
@@ -41,6 +41,13 @@
4141

4242
namespace tvm {
4343
namespace runtime {
44+
45+
using memory::Allocator;
46+
using memory::AllocatorType;
47+
using memory::MemoryManager;
48+
using memory::Storage;
49+
using memory::StorageObj;
50+
4451
namespace vm {
4552

4653
/*!

0 commit comments

Comments
 (0)