MMGP: GPU Memory Management

3.7.6 · active · verified Fri Apr 17

mmgp (Memory Management for the GPU Poor) is a low-level Python library for direct GPU memory management, primarily designed for use with `tinygrad`. It provides functions for allocating and freeing raw memory on the GPU. As of version `3.7.6`, it offers granular control over GPU resources, allowing for efficient memory handling in performance-critical applications. Its release cadence is irregular, often aligning with `tinygrad` development.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic GPU memory allocation and deallocation using `mmgp.alloc`, `mmgp.zeros`, and `mmgp.free`. It allocates memory, prints the returned pointer, and then frees it.

import mmgp

# Allocate 1MB of GPU memory
sz = 1024 * 1024 # 1MB
ptr = mmgp.alloc(sz)
print(f"Allocated {sz} bytes at GPU pointer: {hex(ptr)}")

# Allocate 512KB of zero-initialized GPU memory
ptr_zeros = mmgp.zeros(512 * 1024)
print(f"Allocated 512KB zero-initialized at GPU pointer: {hex(ptr_zeros)}")

# Free the allocated memory
mmgp.free(ptr)
print(f"Freed {sz} bytes from GPU pointer: {hex(ptr)}")

mmgp.free(ptr_zeros)
print(f"Freed 512KB from GPU pointer: {hex(ptr_zeros)}")

view raw JSON →