modelexpress-client 0.7.0

Client library for Model Express gRPC server
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

// CUDAPluggableAllocator shim for the VmmArena allocator.
//
// PyTorch calls mx_malloc / mx_free through CUDAPluggableAllocator. Those
// symbols cannot carry a Python object argument, so the module keeps one
// process-global "active arena" pointer. The arena state itself is not global:
// every Python VmmArena owns an ArenaState capsule with its own bump counter,
// allocation map, callbacks, and lifecycle state.
//
// The extension intentionally does not link against CUDA. cuMem* calls stay in
// Python backends so non-CUDA StubBackend tests exercise the same C hot path.

#define PY_SSIZE_T_CLEAN
#include <Python.h>

#include <atomic>
#include <cstdint>
#include <limits>
#include <mutex>
#include <thread>
#include <unordered_map>
#include <utility>
#include <vector>

namespace {

constexpr const char* kArenaCapsuleName = "modelexpress.VmmArenaState";
constexpr int kStateOpen = 0;
constexpr int kStateClosed = 1;

struct AllocRecord {
  size_t size;
  uint64_t handle;
};

struct ArenaState {
  uintptr_t base;
  size_t total_bytes;
  size_t granularity;
  std::atomic<size_t> next_offset;
  std::atomic<size_t> mapped_bytes;
  std::atomic<int> state;
  // in_flight counts threads currently inside a malloc/free call that have
  // passed the OPEN check. close_and_drain CASes state -> CLOSED then spins
  // on in_flight to reach zero before snapshotting the map, so a malloc
  // that won the OPEN check cannot insert into a map the closer has already
  // cleared, and a free that has erased a record cannot decrement
  // mapped_bytes after the closer has reset it.
  std::atomic<int> in_flight;
  PyObject* alloc_callback;
  PyObject* dealloc_callback;
  std::mutex allocations_mutex;
  std::unordered_map<uintptr_t, AllocRecord> allocations;
};

// RAII guard for the in_flight counter. Increments on construction;
// decrements on destruction. Use at the top of every public hot-path
// function on a per-arena state.
class InFlightGuard {
 public:
  explicit InFlightGuard(ArenaState* s) : state_(s) {
    state_->in_flight.fetch_add(1, std::memory_order_seq_cst);
  }
  ~InFlightGuard() {
    state_->in_flight.fetch_sub(1, std::memory_order_seq_cst);
  }
  InFlightGuard(const InFlightGuard&) = delete;
  InFlightGuard& operator=(const InFlightGuard&) = delete;
 private:
  ArenaState* state_;
};

std::mutex g_active_mutex;
PyObject* g_active_capsule = nullptr;

ArenaState*
get_state(PyObject* capsule)
{
  return static_cast<ArenaState*>(PyCapsule_GetPointer(capsule, kArenaCapsuleName));
}

bool
is_power_of_two(size_t value)
{
  return value != 0 && (value & (value - 1)) == 0;
}

bool
round_up(size_t value, size_t granularity, size_t* out)
{
  if (granularity == 0) {
    return false;
  }
  size_t remainder = value % granularity;
  if (remainder == 0) {
    *out = value;
    return true;
  }
  size_t add = granularity - remainder;
  if (value > std::numeric_limits<size_t>::max() - add) {
    return false;
  }
  *out = value + add;
  return true;
}

void
arena_capsule_destructor(PyObject* capsule)
{
  ArenaState* state = get_state(capsule);
  if (!state) {
    PyErr_Clear();
    return;
  }

  PyGILState_STATE gstate = PyGILState_Ensure();
  Py_XDECREF(state->alloc_callback);
  Py_XDECREF(state->dealloc_callback);
  PyGILState_Release(gstate);

  delete state;
}

PyObject*
call_alloc_callback(ArenaState* state, uintptr_t va, size_t size)
{
  PyObject* cb = state->alloc_callback;
  Py_INCREF(cb);
  PyObject* result = PyObject_CallFunction(
      cb, "KK", static_cast<unsigned long long>(va),
      static_cast<unsigned long long>(size));
  Py_DECREF(cb);
  return result;
}

bool
call_dealloc_callback(
    ArenaState* state, uintptr_t va, size_t size, uint64_t handle,
    bool write_unraisable)
{
  PyObject* cb = state->dealloc_callback;
  Py_INCREF(cb);
  PyObject* result = PyObject_CallFunction(
      cb, "KKK", static_cast<unsigned long long>(va),
      static_cast<unsigned long long>(size),
      static_cast<unsigned long long>(handle));
  if (!result) {
    if (write_unraisable) {
      PyErr_WriteUnraisable(cb);
    } else {
      PyErr_Clear();
    }
    Py_DECREF(cb);
    return false;
  }
  Py_DECREF(result);
  Py_DECREF(cb);
  return true;
}

PyObject*
arena_malloc_impl(ArenaState* state, Py_ssize_t requested_size)
{
  if (requested_size <= 0) {
    PyErr_SetString(PyExc_ValueError, "size must be positive");
    return nullptr;
  }
  // Increment in_flight BEFORE checking state, so a concurrent
  // close_and_drain that CASes state to CLOSED will see this thread's
  // increment when it spins on in_flight, and will wait for the malloc
  // body to complete (callback + map insert) before draining.
  InFlightGuard guard(state);
  if (state->state.load(std::memory_order_seq_cst) != kStateOpen) {
    PyErr_SetString(PyExc_RuntimeError, "arena is closed");
    return nullptr;
  }

  size_t aligned_size = 0;
  if (!round_up(static_cast<size_t>(requested_size), state->granularity, &aligned_size)) {
    PyErr_SetString(PyExc_OverflowError, "allocation size overflow during alignment");
    return nullptr;
  }

  // The CAS bump loop touches no Python C API. Release attached state so
  // other threads can run Python code while we spin on the atomic. Overflow
  // is signaled via a local flag; the Python error is raised after
  // re-attaching.
  size_t offset = state->next_offset.load(std::memory_order_relaxed);
  bool overflow = false;
  Py_BEGIN_ALLOW_THREADS
  while (true) {
    if (offset > state->total_bytes || aligned_size > state->total_bytes - offset) {
      overflow = true;
      break;
    }
    size_t next = offset + aligned_size;
    if (state->next_offset.compare_exchange_weak(
            offset, next, std::memory_order_acq_rel, std::memory_order_relaxed)) {
      break;
    }
  }
  Py_END_ALLOW_THREADS
  if (overflow) {
    PyErr_Format(
        PyExc_RuntimeError,
        "allocation of %zd bytes (aligned %zu) at offset %zu would exceed reserved range of %zu bytes",
        requested_size, aligned_size, offset, state->total_bytes);
    return nullptr;
  }

  uintptr_t va = state->base + offset;
  PyObject* result = call_alloc_callback(state, va, aligned_size);
  if (!result) {
    // Intentional: failed backend allocation leaks this VA slot in the 16 TiB
    // reserve, but no physical memory or map entry exists. Recycling failed
    // slots would add synchronization and rollback complexity to the hot path.
    return nullptr;
  }

  unsigned long long handle_value = PyLong_AsUnsignedLongLong(result);
  Py_DECREF(result);
  if (PyErr_Occurred()) {
    return nullptr;
  }

  // Map insert and mapped_bytes accounting touch no Python C API. Release
  // attached state across the mutex + atomic block.
  Py_BEGIN_ALLOW_THREADS
  {
    std::lock_guard<std::mutex> lock(state->allocations_mutex);
    state->allocations.emplace(
        va, AllocRecord{aligned_size, static_cast<uint64_t>(handle_value)});
  }
  state->mapped_bytes.fetch_add(aligned_size, std::memory_order_acq_rel);
  Py_END_ALLOW_THREADS

  return PyLong_FromUnsignedLongLong(static_cast<unsigned long long>(va));
}

void
arena_free_impl(ArenaState* state, uintptr_t ptr, bool write_unraisable)
{
  // Take an in-flight reference so close_and_drain cannot clear the map
  // and reset mapped_bytes between our erase and our fetch_sub. If we are
  // already closed, the close drain has captured our record into the
  // returned list, so doing nothing here is correct.
  InFlightGuard guard(state);

  // State check, map lookup/erase, and mapped_bytes accounting are all
  // pure-C. Release attached state across the block; reattach to call the
  // Python dealloc callback below.
  AllocRecord record{};
  bool found = false;
  Py_BEGIN_ALLOW_THREADS
  if (state->state.load(std::memory_order_seq_cst) == kStateOpen) {
    {
      std::lock_guard<std::mutex> lock(state->allocations_mutex);
      auto it = state->allocations.find(ptr);
      if (it != state->allocations.end()) {
        record = it->second;
        state->allocations.erase(it);
        found = true;
      }
    }
    if (found) {
      state->mapped_bytes.fetch_sub(record.size, std::memory_order_acq_rel);
    }
  }
  Py_END_ALLOW_THREADS

  if (!found) {
    return;
  }

  call_dealloc_callback(
      state, ptr, record.size, record.handle, write_unraisable);
}

}  // namespace

extern "C" {

void*
mx_malloc(ssize_t size, int device, void* stream)
{
  (void)device;
  (void)stream;

  PyGILState_STATE gstate = PyGILState_Ensure();

  PyObject* capsule = nullptr;
  {
    std::lock_guard<std::mutex> lock(g_active_mutex);
    capsule = g_active_capsule;
    Py_XINCREF(capsule);
  }

  if (!capsule) {
    PyGILState_Release(gstate);
    return nullptr;
  }

  ArenaState* state = get_state(capsule);
  if (!state) {
    PyErr_WriteUnraisable(capsule);
    Py_DECREF(capsule);
    PyGILState_Release(gstate);
    return nullptr;
  }

  void* ptr = nullptr;
  PyObject* result = arena_malloc_impl(state, static_cast<Py_ssize_t>(size));
  if (result) {
    unsigned long long val = PyLong_AsUnsignedLongLong(result);
    if (!PyErr_Occurred()) {
      ptr = reinterpret_cast<void*>(static_cast<uintptr_t>(val));
    }
    Py_DECREF(result);
  }
  if (PyErr_Occurred()) {
    PyErr_WriteUnraisable(capsule);
  }

  Py_DECREF(capsule);
  PyGILState_Release(gstate);
  return ptr;
}

void
mx_free(void* ptr, ssize_t size, int device, void* stream)
{
  (void)size;
  (void)device;
  (void)stream;

  PyGILState_STATE gstate = PyGILState_Ensure();

  PyObject* capsule = nullptr;
  {
    std::lock_guard<std::mutex> lock(g_active_mutex);
    capsule = g_active_capsule;
    Py_XINCREF(capsule);
  }

  if (capsule) {
    ArenaState* state = get_state(capsule);
    if (state) {
      arena_free_impl(state, reinterpret_cast<uintptr_t>(ptr), true);
    } else {
      PyErr_WriteUnraisable(capsule);
    }
    Py_DECREF(capsule);
  }

  PyGILState_Release(gstate);
}

}  // extern "C"

static PyObject*
py_arena_create(PyObject* self, PyObject* args)
{
  (void)self;
  unsigned long long base = 0;
  unsigned long long total_bytes = 0;
  unsigned long long granularity = 0;
  PyObject* alloc_cb = nullptr;
  PyObject* dealloc_cb = nullptr;

  if (!PyArg_ParseTuple(
          args, "KKKOO", &base, &total_bytes, &granularity, &alloc_cb,
          &dealloc_cb)) {
    return nullptr;
  }
  if (!PyCallable_Check(alloc_cb) || !PyCallable_Check(dealloc_cb)) {
    PyErr_SetString(PyExc_TypeError, "allocation callbacks must be callable");
    return nullptr;
  }
  if (!is_power_of_two(static_cast<size_t>(granularity))) {
    PyErr_SetString(PyExc_ValueError, "granularity must be a positive power of 2");
    return nullptr;
  }

  auto* state = new ArenaState{
      static_cast<uintptr_t>(base),
      static_cast<size_t>(total_bytes),
      static_cast<size_t>(granularity),
      0,
      0,
      kStateOpen,
      0,  // in_flight
      alloc_cb,
      dealloc_cb,
      {},
      {}};
  Py_INCREF(alloc_cb);
  Py_INCREF(dealloc_cb);

  PyObject* capsule = PyCapsule_New(state, kArenaCapsuleName, arena_capsule_destructor);
  if (!capsule) {
    Py_DECREF(alloc_cb);
    Py_DECREF(dealloc_cb);
    delete state;
    return nullptr;
  }
  return capsule;
}

static PyObject*
py_arena_malloc(PyObject* self, PyObject* args)
{
  (void)self;
  PyObject* capsule = nullptr;
  Py_ssize_t size = 0;
  if (!PyArg_ParseTuple(args, "On", &capsule, &size)) {
    return nullptr;
  }
  ArenaState* state = get_state(capsule);
  if (!state) {
    return nullptr;
  }
  return arena_malloc_impl(state, size);
}

static PyObject*
py_arena_free(PyObject* self, PyObject* args)
{
  (void)self;
  PyObject* capsule = nullptr;
  unsigned long long ptr = 0;
  if (!PyArg_ParseTuple(args, "OK", &capsule, &ptr)) {
    return nullptr;
  }
  ArenaState* state = get_state(capsule);
  if (!state) {
    return nullptr;
  }
  // write_unraisable=true so dealloc errors surface via sys.unraisablehook
  // instead of being silently cleared. Direct-Python callers (arena.free)
  // get the same error visibility as the PyTorch CUDAPluggableAllocator
  // path (mx_free).
  arena_free_impl(state, static_cast<uintptr_t>(ptr), true);
  Py_RETURN_NONE;
}

static PyObject*
py_arena_registered_range(PyObject* self, PyObject* args)
{
  (void)self;
  PyObject* capsule = nullptr;
  if (!PyArg_ParseTuple(args, "O", &capsule)) {
    return nullptr;
  }
  ArenaState* state = get_state(capsule);
  if (!state) {
    return nullptr;
  }
  return Py_BuildValue(
      "KK", static_cast<unsigned long long>(state->base),
      static_cast<unsigned long long>(
          state->next_offset.load(std::memory_order_acquire)));
}

static PyObject*
py_arena_get_used_bytes(PyObject* self, PyObject* args)
{
  (void)self;
  PyObject* capsule = nullptr;
  if (!PyArg_ParseTuple(args, "O", &capsule)) {
    return nullptr;
  }
  ArenaState* state = get_state(capsule);
  if (!state) {
    return nullptr;
  }
  return PyLong_FromUnsignedLongLong(
      static_cast<unsigned long long>(state->next_offset.load(std::memory_order_acquire)));
}

static PyObject*
py_arena_get_mapped_bytes(PyObject* self, PyObject* args)
{
  (void)self;
  PyObject* capsule = nullptr;
  if (!PyArg_ParseTuple(args, "O", &capsule)) {
    return nullptr;
  }
  ArenaState* state = get_state(capsule);
  if (!state) {
    return nullptr;
  }
  return PyLong_FromUnsignedLongLong(
      static_cast<unsigned long long>(state->mapped_bytes.load(std::memory_order_acquire)));
}

static PyObject*
py_arena_get_live_count(PyObject* self, PyObject* args)
{
  (void)self;
  PyObject* capsule = nullptr;
  if (!PyArg_ParseTuple(args, "O", &capsule)) {
    return nullptr;
  }
  ArenaState* state = get_state(capsule);
  if (!state) {
    return nullptr;
  }
  size_t live_count = 0;
  Py_BEGIN_ALLOW_THREADS
  {
    std::lock_guard<std::mutex> lock(state->allocations_mutex);
    live_count = state->allocations.size();
  }
  Py_END_ALLOW_THREADS
  return PyLong_FromSize_t(live_count);
}

static PyObject*
py_arena_is_closed(PyObject* self, PyObject* args)
{
  (void)self;
  PyObject* capsule = nullptr;
  if (!PyArg_ParseTuple(args, "O", &capsule)) {
    return nullptr;
  }
  ArenaState* state = get_state(capsule);
  if (!state) {
    return nullptr;
  }
  if (state->state.load(std::memory_order_acquire) == kStateClosed) {
    Py_RETURN_TRUE;
  }
  Py_RETURN_FALSE;
}

static PyObject*
py_arena_close_and_drain(PyObject* self, PyObject* args)
{
  (void)self;
  PyObject* capsule = nullptr;
  if (!PyArg_ParseTuple(args, "O", &capsule)) {
    return nullptr;
  }
  ArenaState* state = get_state(capsule);
  if (!state) {
    return nullptr;
  }

  int expected = kStateOpen;
  if (!state->state.compare_exchange_strong(
          expected, kStateClosed, std::memory_order_seq_cst,
          std::memory_order_seq_cst)) {
    Py_RETURN_NONE;
  }

  // Wait for any in-flight malloc/free that started before our CAS to
  // CLOSED. Operations that took an InFlightGuard before the CAS will
  // complete their callback + map insert (malloc) or fetch_sub (free);
  // operations that take the guard after the CAS see CLOSED state and
  // bail without touching the map. The spin is bounded by the longest
  // in-flight allocator callback, typically sub-millisecond.
  //
  // Release the GIL while spinning: the in-flight operations whose
  // completion we are waiting on dispatch their cuMem* work through
  // Python backend callbacks, so they need the GIL to make progress.
  // Holding the GIL here would deadlock against those callbacks.
  Py_BEGIN_ALLOW_THREADS
  while (state->in_flight.load(std::memory_order_seq_cst) > 0) {
    std::this_thread::yield();
  }
  Py_END_ALLOW_THREADS

  // Snapshot the map under lock. We deliberately do NOT clear the map
  // yet: if PyList_New or Py_BuildValue fails below we want the records
  // to remain so a retry path can recover them via rollback.
  std::vector<std::pair<uintptr_t, AllocRecord>> drained;
  {
    std::lock_guard<std::mutex> lock(state->allocations_mutex);
    drained.reserve(state->allocations.size());
    for (const auto& item : state->allocations) {
      drained.emplace_back(item.first, item.second);
    }
  }

  // Build the Python list before clearing C-side state. On any failure
  // (PyList_New or Py_BuildValue), roll the state back to OPEN so the
  // arena and its records survive the failure and the caller can retry
  // close_and_drain to recover the live allocations.
  PyObject* list = PyList_New(static_cast<Py_ssize_t>(drained.size()));
  if (!list) {
    state->state.store(kStateOpen, std::memory_order_seq_cst);
    return nullptr;
  }
  for (Py_ssize_t i = 0; i < static_cast<Py_ssize_t>(drained.size()); ++i) {
    PyObject* tuple = Py_BuildValue(
        "KKK", static_cast<unsigned long long>(drained[i].first),
        static_cast<unsigned long long>(drained[i].second.size),
        static_cast<unsigned long long>(drained[i].second.handle));
    if (!tuple) {
      Py_DECREF(list);
      state->state.store(kStateOpen, std::memory_order_seq_cst);
      return nullptr;
    }
    PyList_SET_ITEM(list, i, tuple);
  }

  // Commit: the list owns copies of every record. Now it is safe to
  // clear the authoritative C-side state.
  {
    std::lock_guard<std::mutex> lock(state->allocations_mutex);
    state->allocations.clear();
  }
  state->mapped_bytes.store(0, std::memory_order_release);

  return list;
}

static PyObject*
py_set_active_arena(PyObject* self, PyObject* args)
{
  (void)self;
  PyObject* capsule = nullptr;
  if (!PyArg_ParseTuple(args, "O", &capsule)) {
    return nullptr;
  }

  PyObject* new_capsule = nullptr;
  if (capsule != Py_None) {
    ArenaState* state = get_state(capsule);
    if (!state) {
      return nullptr;
    }
    new_capsule = capsule;
    Py_INCREF(new_capsule);
  }

  PyObject* old_capsule = nullptr;
  {
    std::lock_guard<std::mutex> lock(g_active_mutex);
    old_capsule = g_active_capsule;
    g_active_capsule = new_capsule;
  }
  Py_XDECREF(old_capsule);

  Py_RETURN_NONE;
}

static PyObject*
py_init_module(PyObject* self, PyObject* args)
{
  (void)self;
  PyObject* malloc_cb = nullptr;
  PyObject* free_cb = nullptr;
  if (!PyArg_ParseTuple(args, "OO", &malloc_cb, &free_cb)) {
    return nullptr;
  }
  if (!PyCallable_Check(malloc_cb) || !PyCallable_Check(free_cb)) {
    PyErr_SetString(PyExc_TypeError, "Both arguments must be callables");
    return nullptr;
  }
  Py_RETURN_NONE;
}

static PyMethodDef module_methods[] = {
    {"arena_create", py_arena_create, METH_VARARGS, "Create a VmmArena state capsule"},
    {"arena_malloc", py_arena_malloc, METH_VARARGS, "Allocate from an arena capsule"},
    {"arena_free", py_arena_free, METH_VARARGS, "Free an arena capsule allocation"},
    {"arena_registered_range", py_arena_registered_range, METH_VARARGS, "Return (base, used_bytes)"},
    {"arena_get_used_bytes", py_arena_get_used_bytes, METH_VARARGS, "Return arena bump pointer"},
    {"arena_get_mapped_bytes", py_arena_get_mapped_bytes, METH_VARARGS, "Return live mapped bytes"},
    {"arena_get_live_count", py_arena_get_live_count, METH_VARARGS, "Return live allocation count"},
    {"arena_is_closed", py_arena_is_closed, METH_VARARGS, "Return whether arena is closed"},
    {"arena_close_and_drain", py_arena_close_and_drain, METH_VARARGS, "Close and drain live allocations"},
    {"set_active_arena", py_set_active_arena, METH_VARARGS, "Set active arena capsule for mx_malloc/mx_free"},
    {"init_module", py_init_module, METH_VARARGS, "Compatibility callback validator"},
    {nullptr, nullptr, 0, nullptr}};

#if PY_VERSION_HEX >= 0x030D0000 && defined(Py_mod_gil)
static PyModuleDef_Slot module_slots[] = {
    {Py_mod_gil, Py_MOD_GIL_NOT_USED},
    {0, nullptr},
};
#endif

static struct PyModuleDef vmm_alloc_module = {
    PyModuleDef_HEAD_INIT,
    "_alloc_ext",
    "CUDAPluggableAllocator shim for the VmmArena",
    0,
    module_methods,
#if PY_VERSION_HEX >= 0x030D0000 && defined(Py_mod_gil)
    module_slots,
#else
    nullptr,
#endif
    nullptr,
    nullptr,
    nullptr};

PyMODINIT_FUNC
PyInit__alloc_ext(void)
{
#if PY_VERSION_HEX >= 0x030D0000 && defined(Py_mod_gil)
  return PyModuleDef_Init(&vmm_alloc_module);
#else
  return PyModule_Create(&vmm_alloc_module);
#endif
}