Skip to content

Commit 0e094ec

Browse files
committed
Fix segmentaiton fault
1 parent a7a1ae0 commit 0e094ec

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,11 @@ class GpuAllocatorWrapper : public GpuAllocator {
668668

669669
// Static method to create a GpuAllocator from MTRT_GpuAllocator
670670
static std::unique_ptr<GpuAllocator> create(MTRT_GpuAllocator gpuAllocator) {
671+
if (!gpuAllocator.ptr || !gpuAllocator.allocate ||
672+
!gpuAllocator.deallocate) {
673+
llvm::errs() << "Invalid MTRT_GpuAllocator passed to create()";
674+
return nullptr;
675+
}
671676
return std::make_unique<GpuAllocatorWrapper>(gpuAllocator);
672677
}
673678
};
@@ -679,8 +684,10 @@ MTRT_Status mtrtRuntimeSessionCreate(MTRT_RuntimeSessionOptions options,
679684
RuntimeSessionOptions *cppOptions = unwrap(options);
680685
Executable *cppExecutable = unwrap(executable);
681686

682-
std::unique_ptr<GpuAllocator> allocator =
683-
gpuAllocator.ptr ? GpuAllocatorWrapper::create(gpuAllocator) : nullptr;
687+
std::unique_ptr<GpuAllocator> allocator;
688+
if (gpuAllocator.ptr) {
689+
allocator.reset(GpuAllocatorWrapper::create(gpuAllocator).release());
690+
}
684691

685692
StatusOr<std::unique_ptr<RuntimeSession>> session =
686693
createRuntimeSessionWithLuaBackend(cppExecutable->getView(),

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ class PyRuntimeValue : public PyMTRTWrapper<PyRuntimeValue, MTRT_RuntimeValue> {
191191
// routing.
192192
class PyGpuAllocator {
193193
public:
194+
py::object pySelf;
195+
PyGpuAllocator(py::object self) : pySelf(self) {}
196+
194197
virtual ~PyGpuAllocator() = default;
195198
virtual std::uintptr_t allocate(uint64_t size) = 0;
196199
virtual bool deallocate(std::uintptr_t ptr) = 0;
@@ -201,6 +204,7 @@ class PyGpuAllocator {
201204
private:
202205
// Trampoline function: Routes C-style allocation calls to C++ virtual method.
203206
static void *pyGpuAllocatorAllocate(void *self, uint64_t size) {
207+
py::gil_scoped_acquire acquire;
204208
auto *allocator = static_cast<PyGpuAllocator *>(self);
205209
std::uintptr_t ptr = allocator->allocate(size);
206210
return reinterpret_cast<void *>(ptr);
@@ -209,6 +213,7 @@ class PyGpuAllocator {
209213
// Trampoline function: Routes C-style deallocation calls to C++ virtual
210214
// method.
211215
static bool pyGpuAllocatorDeallocate(void *self, void *memory) {
216+
py::gil_scoped_acquire acquire;
212217
auto *allocator = static_cast<PyGpuAllocator *>(self);
213218
return allocator->deallocate(reinterpret_cast<std::uintptr_t>(memory));
214219
}
@@ -969,7 +974,8 @@ PYBIND11_MODULE(_api, m) {
969974
py::arg("nccl_uuid") = py::str(""));
970975

971976
py::class_<PyGpuAllocator, PyGpuAllocatorTrampoline>(m, "GpuAllocator")
972-
.def(py::init<>())
977+
.def(py::init<>(
978+
[](py::object self) { return new PyGpuAllocatorTrampoline(self); }))
973979
.def("allocate", &PyGpuAllocator::allocate)
974980
.def("deallocate", &PyGpuAllocator::deallocate)
975981
.def("get_capi_object", &PyGpuAllocator::getCApiObject);
@@ -983,7 +989,8 @@ PYBIND11_MODULE(_api, m) {
983989
if (gpu_allocator.is_none()) {
984990
// Create session without custom allocator
985991
s = mtrtRuntimeSessionCreate(
986-
options, exe, MTRT_GpuAllocator{nullptr}, &session);
992+
options, exe, MTRT_GpuAllocator{nullptr, nullptr, nullptr},
993+
&session);
987994
} else {
988995
try {
989996
PyGpuAllocator &allocator =

mlir-tensorrt/test/python/IntegrationTests/test_stablehlo_add.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
class CupyGPUAllocator(runtime.GpuAllocator):
1919
def __init__(self):
20-
super().__init__()
20+
super().__init__(self)
2121
self.allocations = {} # Keep track of allocations
2222

2323
def allocate(self, size):

0 commit comments

Comments
 (0)