-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Relay][Runtime] Add memory manager for NDArray #3121
Conversation
Credit to @icemelon9 and @wweic
src/runtime/pooled_allocator.h
Outdated
buf.size = size; | ||
buf.data = DeviceAPI::Get(ctx_)->AllocDataSpace(ctx_, size, alignment, type_hint); | ||
used_memory_.fetch_add(size, std::memory_order_relaxed); | ||
LOG(INFO) << "allocate " << size << " B, used memory " << used_memory_ << " B"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LOG(INFO) << "allocate " << size << " B, used memory " << used_memory_ << " B"; | |
LOG(DEBUG) << "allocate " << size << " B, used memory " << used_memory_ << " B"; |
Co-Authored-By: jroesch <[email protected]>
lgtm |
src/runtime/memory_manager.h
Outdated
|
||
private: | ||
std::mutex mu_; | ||
std::unordered_map<TVMContext, std::unique_ptr<Allocator>> allocators_; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Add space, so that it is compatible with old runtime)
src/runtime/memory_manager.h
Outdated
|
||
virtual Buffer Alloc(size_t nbytes, size_t alignment, TVMType type_hint) = 0; | ||
virtual void Free(const Buffer& buffer) = 0; | ||
virtual size_t UsedMemory() = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
UsedMemory(Const)?
include/tvm/runtime/ndarray.h
Outdated
@@ -32,6 +32,10 @@ | |||
|
|||
namespace tvm { | |||
namespace runtime { | |||
|
|||
class Allocator; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove Allocator(see my other comment about reverse the dependency)
allocator depend on ndarry.h
golang/src/tvm_runtime_pack.cc
Outdated
@@ -32,6 +32,7 @@ | |||
#include "src/runtime/threading_backend.cc" | |||
#include "src/runtime/thread_pool.cc" | |||
#include "src/runtime/ndarray.cc" | |||
#include "src/runtime/memory_manager.cc" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for now, do not include memory_manager.cc, consider add back once we have all the vm changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't link correctly without this line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if you break the dependency of the NDArrary from memory_manager(move allocator aware alloca to allocator->Empty), you should be able to break the link dp
src/runtime/memory_manager.cc
Outdated
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
consider moving all the memory manager into src/runtime/vm, because it is used for dynamic memory allocation, and basic version of runtime does not need it
Summary of my main review comments:
|
@@ -296,6 +299,19 @@ class NDArray::Container { | |||
dl_tensor.strides = nullptr; | |||
dl_tensor.byte_offset = 0; | |||
} | |||
|
|||
Container(void* data, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we just use new and set it up in the impl? To minimize the constructor logic in here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why minimize? it creates even more chances to do it wrong code for construction should be constructors
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are certain cases where the shape vector is not needed, when converting from DLPack. Anyway if we want to keep it in this way , try to use copy initializers to initialize shape, this will remove the need for move
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can keep the constructor this way because it's non-trivial to correctly initialize the shape in container and DLTensor in my mind. If there's use case to create Container without shape in the future, we can revisit this or add a new constructor.
src/runtime/vm/memory_manager.cc
Outdated
delete ptr; | ||
} | ||
|
||
NDArray Allocator::EmptyNDArray(std::vector<int64_t> shape, DLDataType dtype, DLContext ctx) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
debating whether to use Allocator->EmptyNDArray or Allocator->Empty, if we assume Empty always corresponds to NDArray, perhaps we can remove the NDArray
some nits, looks good in general, please see if you can fix |
@@ -296,6 +299,19 @@ class NDArray::Container { | |||
dl_tensor.strides = nullptr; | |||
dl_tensor.byte_offset = 0; | |||
} | |||
|
|||
Container(void* data, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can keep the constructor this way because it's non-trivial to correctly initialize the shape in container and DLTensor in my mind. If there's use case to create Container without shape in the future, we can revisit this or add a new constructor.
Please fix @icemelon9 's comment and retrigger the CI. this is almost ready to be merged |
@icemelon9 please double check and merge it in if you think it is ready |
Thanks everyone. This is now merged. |
* Add support for custom NDArray memory management Credit to @icemelon9 and @wweic * Fix copy-paste issue * Fix naive allocator.h * Remove buffer field * Apply Wei's suggestions. Co-Authored-By: jroesch <[email protected]> * Fix Wei's suggestion * Fix go rts * Break MM dependency * Add docs and clean up diff * Add more docs * Move to VM folder * Fix lint * Remove Go dep. * Rename to Empty * Address Haichen's comments
* Add support for custom NDArray memory management Credit to @icemelon9 and @wweic * Fix copy-paste issue * Fix naive allocator.h * Remove buffer field * Apply Wei's suggestions. Co-Authored-By: jroesch <[email protected]> * Fix Wei's suggestion * Fix go rts * Break MM dependency * Add docs and clean up diff * Add more docs * Move to VM folder * Fix lint * Remove Go dep. * Rename to Empty * Address Haichen's comments
This factors out the memory management code from the previous runtime into a separate PR.
See #2889 and #3120.