pymtml is Python bindings for the Moore Threads Management Library (MTML) - a C-based API for monitoring and managing Moore Threads GPU devices. It provides:
- Native MTML bindings - Direct Python wrappers for MTML C library functions (Linux: libmtml.so, Windows: mtml.dll)
- NVML compatibility layer - Drop-in replacement for NVIDIA's pynvml library
Moore Threads GPUs use MUSA (Meta-computing Unified System Architecture) as their compute platform, analogous to NVIDIA's CUDA.
pymtml.py- Main library with all MTML bindings and NVML wrapper functionsmtml_2.2.0.h- C header file defining the MTML API (reference for adding new bindings)test_pymtml.py- Tests for native MTML APIstest_pynvml.py- Tests for NVML-compatible wrapper APIstest_sglang_compat.py- Tests for sglang framework compatibility
Projects using pynvml can switch to pymtml with a single import change:
# Replace: import pynvml
import pymtml as pynvml
# All pynvml.nvml* functions work the same
pynvml.nvmlInit()
device = pynvml.nvmlDeviceGetHandleByIndex(0)
name = pynvml.nvmlDeviceGetName(device)
pynvml.nvmlShutdown()# Format code (isort + black)
make format
# Lint code (flake8)
make lint
# Run tests
make test # pytest
python test_pymtml.py # Native MTML API tests
python test_pynvml.py # NVML wrapper tests
python test_sglang_compat.py # sglang compatibility tests
# Build wheel package
make build
# Clean build artifacts
make clean
# Publish to PyPI
make publish
# Run all (format, lint, test, build)
make all- Formatter: isort + black (not yapf)
- Linter: flake8 with max-line-length=120
- Naming conventions:
- Native MTML functions:
mtmlXxx()(e.g.,mtmlDeviceGetName) - NVML wrapper functions:
nvmlXxx()(e.g.,nvmlDeviceGetName) - Constants:
MTML_XXXorNVML_XXX
- Native MTML functions:
- ctypes patterns: Use
c_uint,c_char,byref(),POINTER()for C bindings - Error handling: Raise
MTMLError(aliased asNVMLError) for all failures
- Find the function signature in
mtml_2.2.0.h - Define any new structs in pymtml.py using
_PrintableStructure - Implement the wrapper function following existing patterns:
def mtmlDeviceGetSomething(device):
global libHandle
c_result = c_uint()
fn = _mtmlGetFunctionPointer("mtmlDeviceGetSomething")
ret = fn(device, byref(c_result))
_mtmlCheckReturn(ret)
return c_result.value- Add corresponding NVML wrapper if applicable
- Add test cases to
test_pymtml.pyand/ortest_pynvml.py
- Always run tests before committing:
python test_pymtml.py && python test_pynvml.py - Tests require Moore Threads GPU hardware with driver and MTML library installed (Linux: libmtml.so, Windows: mtml.dll)
- Test init/shutdown cycles: The library supports multiple init/shutdown cycles
- Check for segfaults: Library shutdown must not cause crashes
- This library loads MTML library dynamically via ctypes (Linux: libmtml.so, Windows: mtml.dll)
- No network operations or external data fetching
- GPU operations require appropriate system permissions
- Handle device handles carefully - don't use after shutdown
mtmlLibraryInit() # or nvmlInit()
# ... use library ...
mtmlLibraryShutDown() # or nvmlShutdown()count = mtmlLibraryCountDevice()
for i in range(count):
device = mtmlLibraryInitDeviceByIndex(i)
# ... query device ...device = mtmlLibraryInitDeviceByIndex(0)
gpu = mtmlDeviceInitGpu(device)
memory = mtmlDeviceInitMemory(device)
# ... use gpu/memory ...
mtmlDeviceFreeGpu(gpu)
mtmlDeviceFreeMemory(memory)nvmlDeviceGetCudaComputeCapability()returns(0, 0)unless torch_musa is available- Use
patch_torch_c_for_musa()to patch torch._C with functions from torch_musa._MUSAC - PCI busId field may be empty from driver; library auto-fills from sbdf if needed