Skip to content

Commit 7b25b15

Browse files
committed
Improve memory leak
1 parent 6e647ca commit 7b25b15

File tree

6 files changed

+13
-47
lines changed

6 files changed

+13
-47
lines changed

mlir-tensorrt/executor/include/mlir-executor/Runtime/API/API.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -795,12 +795,6 @@ class AllocTracker {
795795
/// Returns true if the ptr is released internally.
796796
bool isReleasedInternally(uintptr_t ptr) const;
797797

798-
/// Set the pointer is allocated by TensorRT.
799-
void setTensorRTAllocated(uintptr_t ptr);
800-
801-
/// Get that pointer is allocated by TensorRT.
802-
bool getTensorRTAllocated(uintptr_t ptr);
803-
804798
private:
805799
struct Metadata {
806800
std::atomic<int32_t> externalReferenceCount = {0};

mlir-tensorrt/executor/lib/CAPI/Runtime/Runtime.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "mlir-executor/Runtime/API/API.h"
2828
#include "mlir-executor/Runtime/API/ExecutableFlatbuffer.h"
2929
#include "mlir-executor/Runtime/Backend/Lua/LuaRuntime.h"
30+
#include "mlir-executor/Runtime/Support/Support.h"
3031
#include "mlir-executor/Support/Status.h"
3132
#include "mlir/Support/FileUtilities.h"
3233
#include "llvm/Support/Debug.h"
@@ -324,9 +325,9 @@ MTRT_Status mtrtMemRefCreateExternal(
324325

325326
MTRT_Status mtrtMemRefValueDestroyAsync(MTRT_MemRefValue buffer,
326327
MTRT_Stream stream) {
327-
328328
MemRefValue *memref = unwrap(buffer);
329-
llvm::dbgs() << "[MLIR-TRT] Deallocating memref pointer " << memref->getMemory() << "\n";
329+
MTRT_DBGF("destroying memref pointer 0x%lx asynchronously",
330+
memref->getMemory());
330331
Status s = memref->getClient()->deallocate(
331332
std::unique_ptr<MemRefValue>(memref),
332333
mtrtStreamIsNull(stream) ? std::nullopt
@@ -338,7 +339,7 @@ MTRT_Status mtrtMemRefValueDestroyAsync(MTRT_MemRefValue buffer,
338339

339340
MTRT_Status mtrtMemRefValueDestroy(MTRT_MemRefValue buffer) {
340341
MemRefValue *memref = unwrap(buffer);
341-
llvm::dbgs() << "[MLIR-TRT] Deallocating memref pointer " << memref->getMemory() << "\n";
342+
MTRT_DBGF("destroying memref pointer 0x%lx", memref->getMemory());
342343
Status s =
343344
memref->getClient()->deallocate(std::unique_ptr<MemRefValue>(memref));
344345
if (!s.isOk())

mlir-tensorrt/executor/lib/Runtime/API/API.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -396,20 +396,6 @@ AllocTracker::~AllocTracker() {
396396
MTRT_DBGF("freed %zu bytes of unfreed memory", totalSize);
397397
}
398398

399-
void AllocTracker::setTensorRTAllocated(uintptr_t ptr) {
400-
assert(llvm::is_contained(map, ptr) &&
401-
llvm::formatv("Untracked pointer {0}", ptr).str().c_str());
402-
std::unique_ptr<Metadata> const &metadata = map.at(ptr);
403-
metadata->tensorrtAllocated = true;
404-
}
405-
406-
bool AllocTracker::getTensorRTAllocated(uintptr_t ptr) {
407-
assert(llvm::is_contained(map, ptr) &&
408-
llvm::formatv("Untracked pointer {0}", ptr).str().c_str());
409-
std::unique_ptr<Metadata> const &metadata = map.at(ptr);
410-
return metadata->tensorrtAllocated;
411-
}
412-
413399
void AllocTracker::markReleasedInternally(uintptr_t ptr) {
414400
assert(llvm::is_contained(map, ptr) &&
415401
llvm::formatv("Untracked pointer {0}", ptr).str().c_str());

mlir-tensorrt/executor/lib/Runtime/Backend/Lua/Modules/CUDA/CUDAModule.cpp

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -432,13 +432,9 @@ registerCudaMemoryManagementOps(sol::state_view &lua,
432432
cudaMemcpyDeviceToHost,
433433
stream),
434434
state);
435-
if (allocTracker->getTensorRTAllocated(
436-
reinterpret_cast<uintptr_t>(srcPtr))) {
437-
// Free tensorrt allocate source pointer, since there it won't be
438-
// released by external memref.
439-
SET_LUA_ERROR_IF_CUDART_ERROR(cudaFreeAsync(srcPtr, stream), state);
440-
allocTracker->untrack(reinterpret_cast<uintptr_t>(srcPtr));
441-
}
435+
if (allocTracker->get(src).isInternallyManaged() &&
436+
allocTracker->getExternalReferenceCount(src))
437+
allocTracker->markReleasedInternally(src);
442438
};
443439

444440
lua["__cuda_memcpy_host_pinned2device"] =
@@ -487,13 +483,9 @@ registerCudaMemoryManagementOps(sol::state_view &lua,
487483
cudaMemcpyDeviceToHost,
488484
stream),
489485
state);
490-
if (allocTracker->getTensorRTAllocated(
491-
reinterpret_cast<uintptr_t>(srcPtr))) {
492-
// Free tensorrt allocate source pointer, since there it won't be
493-
// released by external memref.
494-
SET_LUA_ERROR_IF_CUDART_ERROR(cudaFreeAsync(srcPtr, stream), state);
495-
allocTracker->untrack(reinterpret_cast<uintptr_t>(srcPtr));
496-
}
486+
if (allocTracker->get(src).isInternallyManaged() &&
487+
allocTracker->getExternalReferenceCount(src))
488+
allocTracker->markReleasedInternally(src);
497489
};
498490
lua["__cuda_memcpy_device2device"] = [allocTracker](
499491
sol::this_state state,
@@ -518,13 +510,9 @@ registerCudaMemoryManagementOps(sol::state_view &lua,
518510
cudaMemcpyDeviceToDevice,
519511
stream),
520512
state);
521-
if (allocTracker->getTensorRTAllocated(
522-
reinterpret_cast<uintptr_t>(srcPtr))) {
523-
// Free tensorrt allocate source pointer, since there it won't be
524-
// released by external memref.
525-
SET_LUA_ERROR_IF_CUDART_ERROR(cudaFreeAsync(srcPtr, stream), state);
526-
allocTracker->untrack(reinterpret_cast<uintptr_t>(srcPtr));
527-
}
513+
if (allocTracker->get(src).isInternallyManaged() &&
514+
allocTracker->getExternalReferenceCount(src))
515+
allocTracker->markReleasedInternally(src);
528516
return;
529517
};
530518
}

mlir-tensorrt/executor/lib/Runtime/Backend/Lua/Modules/TensorRT/TensorRTModule.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ class OutputAllocatorImpl : public nvinfer1::IOutputAllocator {
155155
if (memory.isOk()) {
156156
mOutputPtr = (*memory).ptr;
157157
mOutputSize = memory->size;
158-
mTracker->setTensorRTAllocated(memory->ptr);
159158
MTRT_DBGF(
160159
"tensorrt module output allocator allocating %lu bytes at 0x%lx",
161160
mOutputSize, mOutputPtr);

mlir-tensorrt/python/bindings/Runtime/RuntimePyBind.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,6 @@ static std::unique_ptr<PyMemRefValue>
346346
createMemRefViewFromDLPack(PyRuntimeClient &client, py::capsule capsule,
347347
std::optional<bool> assertCanonicalStrides) {
348348

349-
llvm::dbgs() << "Creating a memref view from DL pack tensors\n";
350-
351349
DLManagedTensor *managedTensor = static_cast<DLManagedTensor *>(
352350
PyCapsule_GetPointer(capsule.ptr(), "dltensor"));
353351

0 commit comments

Comments
 (0)