add naive allocator for debugging (#140)

* add naive allocator only for debugging

* merge redundant api

---------

Co-authored-by: whjthu <haojie0429@gmail.com>
This commit is contained in:
kilinchange 2023-10-10 16:42:23 +08:00 committed by GitHub
parent 90b9a80f72
commit 1151101fb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 2 deletions

View File

@ -64,7 +64,7 @@ class GraphObj : public Object {
void optimize(); void optimize();
void dataMalloc(); void dataMalloc(bool useNaiveAllocator = false);
/** /**
* @brief Add an operator and create its outputs. Output tensor arguments * @brief Add an operator and create its outputs. Output tensor arguments

View File

@ -123,9 +123,19 @@ void GraphObj::optimize() {
} }
} }
void GraphObj::dataMalloc() { void GraphObj::dataMalloc(bool useNaiveAllocator) {
// topological sorting first // topological sorting first
IT_ASSERT(topo_sort() == true); IT_ASSERT(topo_sort() == true);
if (useNaiveAllocator) {
// used for debugging memory out-of-bounds access, tensors will not be
// released correctly
// note: behavior may not match running in non-naive mode, and it may
// not reproduce the bug
for (auto &tensor : tensors) {
tensor->dataMalloc();
}
return;
}
// count the number of times all tensors are used // count the number of times all tensors are used
std::unordered_map<TensorObj *, size_t> tensorToRefCount; std::unordered_map<TensorObj *, size_t> tensorToRefCount;
// record the memory address offsets of all tensors to be allocated // record the memory address offsets of all tensors to be allocated