Skip to content

Commit d3dee67

Browse files
committed
* Memory Manager
Move the VM memory manager to the runtime level. Use this memory manager for graph runtime.
1 parent 2e571e2 commit d3dee67

File tree

18 files changed

+150
-685
lines changed

18 files changed

+150
-685
lines changed

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

Lines changed: 1 addition & 1 deletion
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_manager.cc"
4546
#include "../src/runtime/minrpc/minrpc_logger.cc"
4647
#include "../src/runtime/module.cc"
4748
#include "../src/runtime/ndarray.cc"
@@ -62,7 +63,6 @@
6263
#include "../src/runtime/workspace_pool.cc"
6364

6465
#ifdef TVM_OPENCL_RUNTIME
65-
#include "../src/runtime/opencl/memory_pool.cc"
6666
#include "../src/runtime/opencl/opencl_device_api.cc"
6767
#include "../src/runtime/opencl/opencl_module.cc"
6868
#include "../src/runtime/opencl/opencl_wrapper/opencl_wrapper.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_manager.cc"
3233
#include "../../src/runtime/module.cc"
3334
#include "../../src/runtime/ndarray.cc"
3435
#include "../../src/runtime/object.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_manager.h

Lines changed: 7 additions & 7 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_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_MANAGER_H_
25+
#define TVM_RUNTIME_MEMORY_MANAGER_H_
2626

2727
#include <tvm/runtime/c_runtime_api.h>
2828
#include <tvm/runtime/ndarray.h>
@@ -36,7 +36,6 @@
3636

3737
namespace tvm {
3838
namespace runtime {
39-
namespace vm {
4039

4140
struct Buffer {
4241
/*! \brief The pointer to the allocated block of memory. */
@@ -60,9 +59,11 @@ class Allocator {
6059
* \param shape The shape of the NDArray.
6160
* \param dtype The datatype of the NDArray.
6261
* \param dev The device where the array is allocated.
62+
* \param mem_scope optional memory scope
6363
* \return The empty NDArray.
6464
*/
65-
NDArray Empty(std::vector<int64_t> shape, DLDataType dtype, Device dev);
65+
NDArray Empty(std::vector<int64_t> shape, DLDataType dtype, Device dev,
66+
Optional<String> mem_scope = NullOpt);
6667
/*! \brief Return the allocator type. */
6768
inline AllocatorType type() const { return type_; }
6869
/*! \brief Allocate a buffer given a size, alignment and type.
@@ -140,8 +141,7 @@ class Storage : public ObjectRef {
140141
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(Storage, ObjectRef, StorageObj);
141142
};
142143

143-
} // namespace vm
144144
} // namespace runtime
145145
} // namespace tvm
146146

147-
#endif // TVM_RUNTIME_VM_MEMORY_MANAGER_H_
147+
#endif // TVM_RUNTIME_MEMORY_MANAGER_H_

include/tvm/runtime/vm/vm.h

Lines changed: 1 addition & 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_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>

src/relay/backend/vm/compiler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
#include <utility>
4242
#include <vector>
4343

44-
#include "../../../runtime/vm/naive_allocator.h"
44+
#include "../../../runtime/naive_allocator.h"
4545
#include "../../../runtime/vm/profiler/vm.h"
4646
#include "../../transforms/pass_utils.h"
4747
#include "../te_compiler.h"

src/runtime/c_runtime_api.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,20 @@ void* DeviceAPI::AllocDataSpaceView(Device dev, void* data, int ndim, const int6
176176
return data;
177177
}
178178

179+
size_t DeviceAPI::GetDataSize(const DLTensor& arr, Optional<String> mem_scope) {
180+
if (!mem_scope.defined() || mem_scope.value() == "global") {
181+
size_t size = 1;
182+
for (tvm_index_t i = 0; i < arr.ndim; ++i) {
183+
size *= static_cast<size_t>(arr.shape[i]);
184+
}
185+
size *= (arr.dtype.bits * arr.dtype.lanes + 7) / 8;
186+
return size;
187+
}
188+
LOG(FATAL) << "Device does not support physical mem computation with "
189+
<< "specified memory scope: " << mem_scope.value();
190+
return 0;
191+
}
192+
179193
void DeviceAPI::CopyDataFromTo(DLTensor* from, DLTensor* to, TVMStreamHandle stream) {
180194
// by default, we can always redirect to the flat memory copy operation.
181195
size_t nbytes = GetDataSize(*from);

src/runtime/graph_executor/graph_executor.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <tvm/runtime/container/string.h>
2727
#include <tvm/runtime/data_type.h>
2828
#include <tvm/runtime/device_api.h>
29+
#include <tvm/runtime/memory_manager.h>
2930
#include <tvm/runtime/ndarray.h>
3031
#include <tvm/runtime/packed_func.h>
3132
#include <tvm/runtime/profiling.h>
@@ -472,7 +473,8 @@ void GraphExecutor::SetupStorage() {
472473
if (!pit.scope.empty()) {
473474
mem_scope = String(pit.scope);
474475
}
475-
storage_pool_.push_back(NDArray::Empty(shape, pit.dtype, dev, mem_scope));
476+
storage_pool_.push_back(MemoryManager::GetOrCreateAllocator(dev, AllocatorType::kPooled)
477+
->Empty(shape, pit.dtype, dev, mem_scope));
476478
}
477479
}
478480

src/runtime/vm/memory_manager.cc renamed to src/runtime/memory_manager.cc

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

2020
/*!
21-
* \file tvm/runtime/vm/memory_manager.cc
21+
* \file tvm/runtime/memory_manager.cc
2222
* \brief Allocate and manage memory for the runtime.
2323
*/
24-
#include <tvm/runtime/vm/memory_manager.h>
24+
#include <tvm/runtime/memory_manager.h>
2525

2626
#include <memory>
2727
#include <utility>
@@ -31,7 +31,6 @@
3131

3232
namespace tvm {
3333
namespace runtime {
34-
namespace vm {
3534

3635
static void BufferDeleter(Object* obj) {
3736
auto* ptr = static_cast<NDArray::Container*>(obj);
@@ -157,19 +156,20 @@ Allocator* MemoryManager::GetAllocator(Device dev) {
157156
return it->second.get();
158157
}
159158

160-
NDArray Allocator::Empty(std::vector<int64_t> shape, DLDataType dtype, DLDevice dev) {
159+
NDArray Allocator::Empty(std::vector<int64_t> shape, DLDataType dtype, DLDevice dev,
160+
Optional<String> mem_scope) {
161161
VerifyDataType(dtype);
162162
NDArray::Container* container = new NDArray::Container(nullptr, shape, dtype, dev);
163163
container->SetDeleter(BufferDeleter);
164-
size_t size = GetDataSize(container->dl_tensor);
164+
size_t size = DeviceAPI::Get(dev)->GetDataSize(container->dl_tensor, mem_scope);
165165
size_t alignment = GetDataAlignment(container->dl_tensor);
166166
Buffer* buffer = new Buffer;
167+
167168
*buffer = this->Alloc(size, alignment, dtype);
168-
container->manager_ctx = reinterpret_cast<void*>(buffer);
169169
container->dl_tensor.data = buffer->data;
170+
container->manager_ctx = reinterpret_cast<void*>(buffer);
170171
return NDArray(GetObjectPtr<Object>(container));
171172
}
172173

173-
} // namespace vm
174174
} // namespace runtime
175175
} // namespace tvm

src/runtime/vm/naive_allocator.h renamed to src/runtime/naive_allocator.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,16 @@
2020
/*!
2121
* \file src/runtime/naive_allocator.h
2222
*/
23-
#ifndef TVM_RUNTIME_VM_NAIVE_ALLOCATOR_H_
24-
#define TVM_RUNTIME_VM_NAIVE_ALLOCATOR_H_
23+
#ifndef TVM_RUNTIME_NAIVE_ALLOCATOR_H_
24+
#define TVM_RUNTIME_NAIVE_ALLOCATOR_H_
2525

2626
#include <tvm/runtime/device_api.h>
27-
#include <tvm/runtime/vm/memory_manager.h>
27+
#include <tvm/runtime/memory_manager.h>
2828

2929
#include <atomic>
3030

3131
namespace tvm {
3232
namespace runtime {
33-
namespace vm {
3433

3534
class NaiveAllocator final : public Allocator {
3635
public:
@@ -59,8 +58,7 @@ class NaiveAllocator final : public Allocator {
5958
Device device_;
6059
};
6160

62-
} // namespace vm
6361
} // namespace runtime
6462
} // namespace tvm
6563

66-
#endif // TVM_RUNTIME_VM_NAIVE_ALLOCATOR_H_
64+
#endif // TVM_RUNTIME_NAIVE_ALLOCATOR_H_

0 commit comments

Comments
 (0)