Skip to content
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

Merged
merged 15 commits into from
May 2, 2019

Conversation

jroesch
Copy link
Member

@jroesch jroesch commented Apr 30, 2019

This factors out the memory management code from the previous runtime into a separate PR.

See #2889 and #3120.

src/runtime/naive_allocator.h Outdated Show resolved Hide resolved
src/runtime/naive_allocator.h Outdated Show resolved Hide resolved
src/runtime/pooled_allocator.h Outdated Show resolved Hide resolved
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";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
LOG(INFO) << "allocate " << size << " B, used memory " << used_memory_ << " B";
LOG(DEBUG) << "allocate " << size << " B, used memory " << used_memory_ << " B";

@weberlo
Copy link
Contributor

weberlo commented Apr 30, 2019

lgtm

src/runtime/pooled_allocator.h Outdated Show resolved Hide resolved
include/tvm/runtime/ndarray.h Outdated Show resolved Hide resolved
src/runtime/ndarray.cc Outdated Show resolved Hide resolved

private:
std::mutex mu_;
std::unordered_map<TVMContext, std::unique_ptr<Allocator>> allocators_;
Copy link
Member

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)


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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UsedMemory(Const)?

src/runtime/pooled_allocator.h Outdated Show resolved Hide resolved
@@ -32,6 +32,10 @@

namespace tvm {
namespace runtime {

class Allocator;
Copy link
Member

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

@@ -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"
Copy link
Member

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

Copy link
Member Author

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

Copy link
Member

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

/*
* 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
Copy link
Member

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

src/runtime/memory_manager.h Outdated Show resolved Hide resolved
@tqchen
Copy link
Member

tqchen commented Apr 30, 2019

Summary of my main review comments:

  • Currently, NDArray needs to be aware of Allocator, it does not have to be the case. Instead, we can implement allocator->Empty(args), which creates an empty NDArray using the allocator. This means that we need to create array creation logic into memory_manager.cc.
    • The advantage is that the memory manager will become a plugin
  • Consider, move memory manager related logic into src/vm, given that they are being used by vm and not necessarily static graph runtime.

@tqchen tqchen added status: need review status: need update need update based on feedbacks labels Apr 30, 2019
@tqchen tqchen self-assigned this May 1, 2019
@@ -296,6 +299,19 @@ class NDArray::Container {
dl_tensor.strides = nullptr;
dl_tensor.byte_offset = 0;
}

Container(void* data,
Copy link
Member

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

Copy link
Member Author

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

Copy link
Member

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

Copy link
Member

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.

delete ptr;
}

NDArray Allocator::EmptyNDArray(std::vector<int64_t> shape, DLDataType dtype, DLContext ctx) {
Copy link
Member

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

@tqchen
Copy link
Member

tqchen commented May 1, 2019

some nits, looks good in general, please see if you can fix

src/runtime/vm/memory_manager.cc Outdated Show resolved Hide resolved
src/runtime/vm/memory_manager.cc Show resolved Hide resolved
@@ -296,6 +299,19 @@ class NDArray::Container {
dl_tensor.strides = nullptr;
dl_tensor.byte_offset = 0;
}

Container(void* data,
Copy link
Member

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.

@tqchen
Copy link
Member

tqchen commented May 1, 2019

Please fix @icemelon9 's comment and retrigger the CI. this is almost ready to be merged

@tqchen
Copy link
Member

tqchen commented May 2, 2019

@icemelon9 please double check and merge it in if you think it is ready

@icemelon icemelon merged commit 83cb872 into apache:master May 2, 2019
@icemelon icemelon added status: accepted and removed status: need review status: need update need update based on feedbacks labels May 2, 2019
@icemelon
Copy link
Member

icemelon commented May 2, 2019

Thanks everyone. This is now merged.

wweic pushed a commit to wweic/tvm that referenced this pull request May 13, 2019
* 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
wweic pushed a commit to neo-ai/tvm that referenced this pull request May 13, 2019
* 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
@jroesch jroesch deleted the runtime-mm branch February 4, 2021 04:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants