Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/android_camera/app/src/main/jni/tvm_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@
#include "../src/runtime/workspace_pool.cc"

#ifdef TVM_OPENCL_RUNTIME
#include "../src/runtime/opencl/memory_pool.cc"
#include "../src/runtime/opencl/opencl_device_api.cc"
#include "../src/runtime/opencl/opencl_module.cc"
#include "../src/runtime/opencl/opencl_wrapper/opencl_wrapper.cc"
#include "../src/runtime/opencl/texture_pool.cc"
#include "../src/runtime/source_utils.cc"
#endif

Expand Down
1 change: 0 additions & 1 deletion apps/android_deploy/app/src/main/jni/tvm_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,5 @@
#include "../src/runtime/opencl/opencl_device_api.cc"
#include "../src/runtime/opencl/opencl_module.cc"
#include "../src/runtime/opencl/opencl_wrapper/opencl_wrapper.cc"
#include "../src/runtime/opencl/texture_pool.cc"
#include "../src/runtime/source_utils.cc"
#endif
1 change: 0 additions & 1 deletion apps/android_rpc/app/src/main/jni/tvm_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
#include "../src/runtime/opencl/opencl_device_api.cc"
#include "../src/runtime/opencl/opencl_module.cc"
#include "../src/runtime/opencl/opencl_wrapper/opencl_wrapper.cc"
#include "../src/runtime/opencl/texture_pool.cc"
#include "../src/runtime/source_utils.cc"
#endif

Expand Down
21 changes: 21 additions & 0 deletions include/tvm/runtime/device_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ enum DeviceAttrKind : int {
kDriverVersion = 12,
kL2CacheSizeBytes = 13,
kTotalGlobalMemory = 14,
kImagePitchAlignment = 15,
};

#ifdef TVM_KALLOC_ALIGNMENT
Expand Down Expand Up @@ -134,12 +135,32 @@ class TVM_DLL DeviceAPI {
*/
virtual void* AllocDataSpace(Device dev, int ndim, const int64_t* shape, DLDataType dtype,
Optional<String> mem_scope = NullOpt);

/*!
* \brief Create a new view with given spec over existing tensor.
* \param dev The device device to perform operation.
* \param data The source array.
* \param shape The shape of allocated tensor.
* \param dtype The type of elements.
* \param mem_scope The memory scope of allocated tensor.
* \return The allocated device pointer.
*/
Copy link
Member

Choose a reason for hiding this comment

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

Looking at this set of changes. I feel that maybe we need something different that makes the memory allocator more explicit. Let me elaborate in reply

virtual void* AllocDataSpaceView(Device dev, void* data, ShapeTuple shape, DLDataType dtype,
Optional<String> mem_scope = NullOpt);
/*!
* \brief Free a data space on device.
* \param dev The device device to perform operation.
* \param ptr The data space.
*/
virtual void FreeDataSpace(Device dev, void* ptr) = 0;

/*!
* \brief Free a view data space on device.
* \param dev The device device to perform operation.
* \param ptr The data space view.
*/
virtual void FreeDataSpaceView(Device dev, void* ptr);

/*!
* \brief copy data from one place to another
* \note This API is designed to support special memory with shape dependent layout.
Expand Down
16 changes: 11 additions & 5 deletions include/tvm/runtime/memory/memory_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ namespace memory {
enum AllocatorType {
kNaive = 1,
kPooled,
kAny,
};

struct Buffer {
Expand Down Expand Up @@ -83,8 +84,7 @@ class Allocator {
* \param mem_scope A memory scope of the buffer.
* \return A sized allocation in the form of a buffer.
*/
virtual Buffer Alloc(ShapeTuple shape, DLDataType type_hint,
const std::string& mem_scope = "") = 0;
Buffer Alloc(ShapeTuple shape, DLDataType type_hint, const std::string& mem_scope = "");
/*! \brief Free a buffer allocated by the allocator.
* \param buffer The buffer to free.
*/
Expand All @@ -97,8 +97,8 @@ class Allocator {
virtual size_t UsedMemory() const = 0;

protected:
virtual Buffer Alloc(Device dev, ShapeTuple shape, DLDataType type_hint,
const std::string& mem_scope);
std::atomic<size_t> used_memory_;
Device device_;

private:
AllocatorType type_;
Expand All @@ -120,7 +120,7 @@ class MemoryManager {
* \param type The allocator type
* \return The memory allocator.
*/
static Allocator* GetAllocator(Device dev, AllocatorType type);
static Allocator* GetAllocator(Device dev, AllocatorType type = AllocatorType::kAny);
/*! \brief Clear the allocators. */
static void Clear();

Expand Down Expand Up @@ -164,6 +164,12 @@ class Storage : public ObjectRef {
};

} // namespace memory

using memory::Allocator;
using memory::AllocatorType;
using memory::MemoryManager;
using memory::StorageObj;

} // namespace runtime
} // namespace tvm

Expand Down
4 changes: 3 additions & 1 deletion include/tvm/runtime/ndarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,11 @@ class NDArray : public ObjectRef {
* \brief Create a NDArray that shares the data memory with the current one.
* \param shape The shape of the new array.
* \param dtype The data type of the new array.
* \param mem_scope The memory scope of the array.
* \note The memory size of new array must be smaller than the current one.
*/
TVM_DLL NDArray CreateView(ShapeTuple shape, DLDataType dtype);
TVM_DLL NDArray CreateView(ShapeTuple shape, DLDataType dtype,
Optional<String> mem_scope = NullOpt);
/*!
* \brief Create a reference view of NDArray that
* represents as DLManagedTensor.
Expand Down
33 changes: 20 additions & 13 deletions src/relay/backend/graph_plan_memory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,16 @@ class StorageAllocator : public StorageAllocaBaseVisitor {
VLOG_CONTEXT << "StorageAllocator";
VLOG(1) << "planning:" << std::endl << PrettyPrint(func);
prototype_ = StorageAllocaInit(&arena_).GetInitTokenMap(func);
// Backup the virtual devices as token reuse might lost the original memory scope
std::unordered_map<const ExprNode*, std::vector<VirtualDevice>> virtual_device_map_;
for (const auto& kv : prototype_) {
std::vector<VirtualDevice> virtual_devices;
virtual_devices.reserve(kv.second.size());
for (StorageToken* tok : kv.second) {
virtual_devices.push_back(tok->virtual_device);
}
virtual_device_map_.insert({kv.first, virtual_devices});
}
this->Run(func);

// The value of smap contains two integer arrays where the first array
Expand All @@ -252,9 +262,13 @@ class StorageAllocator : public StorageAllocaBaseVisitor {
}
num_nodes++;
storage_ids.push_back(tok->storage_id);
virtual_devices.push_back(tok->virtual_device);
sid_sizes_byte.push_back(allocator_.GetMemorySize(tok));
}
ICHECK(kv.second.size() == virtual_device_map_[kv.first].size())
<< "Mismatch of tokens and virtual devices";
for (auto vdev : virtual_device_map_[kv.first]) {
virtual_devices.push_back(vdev);
}
auto storage_info = backend::StorageInfo(std::move(storage_ids), std::move(virtual_devices),
std::move(sid_sizes_byte));
smap.Set(GetRef<Expr>(kv.first), storage_info);
Expand Down Expand Up @@ -356,34 +370,27 @@ class StorageAllocator : public StorageAllocaBaseVisitor {

class TokenAllocator {
public:
StorageToken* Alloc(StorageToken* proto) {
return Is2DStorage(proto) ? token_2d_.Alloc(proto, storage_ids_++)
: token_1d_.Alloc(proto, storage_ids_++);
}
StorageToken* Alloc(StorageToken* proto) { return token_mixed_.Alloc(proto, storage_ids_++); }
StorageToken* Request(StorageToken* proto) {
StorageToken* token =
Is2DStorage(proto) ? token_2d_.Request(proto) : token_1d_.Request(proto);
StorageToken* token = token_mixed_.Request(proto);
return token ? token : this->Alloc(proto);
}
void CheckForRelease(StorageToken* tok) {
return Is2DStorage(tok) ? token_2d_.CheckForRelease(tok) : token_1d_.CheckForRelease(tok);
}
void CheckForRelease(StorageToken* tok) { return token_mixed_.CheckForRelease(tok); }

size_t GetMemorySize(StorageToken* tok) {
// TODO(amalyshe): figure out who requries sizes and for what
// size in case of texture is not enough - we can return any value if it
// assumed to be used for memory allocatoion or we can return real size
// if it is just for information
return Is2DStorage(tok) ? 0 : token_1d_.GetMemorySize(tok);
return token_mixed_.GetMemorySize(tok);
}
static bool Is2DStorage(StorageToken* tok) {
return relay::Is2DStorage(tok->virtual_device->memory_scope);
}

private:
int64_t storage_ids_{0};
TokenAllocator1D token_1d_;
TokenAllocator2D token_2d_;
TokenAllocatorMixed token_mixed_;
};

private:
Expand Down
Loading