Skip to content

Use NVML directly and cache nvidia-smi calls - #114

Open
Qubitium wants to merge 3 commits into
mainfrom
fix/nvml-nvidia-smi-cache
Open

Use NVML directly and cache nvidia-smi calls#114
Qubitium wants to merge 3 commits into
mainfrom
fix/nvml-nvidia-smi-cache

Conversation

@Qubitium

@Qubitium Qubitium commented Aug 13, 2026

Copy link
Copy Markdown
Collaborator

Summary

Eliminates repeated nvidia-smi process spawns by adding a zero-dependency NVML ctypes binding and a TTL/LRU cache for nvidia-smi fallback. Caching is now disabled by default and exposed through Device.config.

Changes

  • device_smi/nvidia.py loads libnvidia-ml.so.1 / nvml.dll / libnvidia-ml.dylib via ctypes.CDLL and binds a minimal NVML API (nvmlInit_v2, nvmlDeviceGetCount_v2, nvmlDeviceGetHandleByIndex_v2, nvmlDeviceGetName, nvmlDeviceGetPciInfo_v3, nvmlDeviceGetMemoryInfo, nvmlDeviceGetUtilizationRates, etc.).
  • NvidiaDevice initializes from NVML first and falls back to nvidia-smi if NVML is absent or returns an error. metrics() also prefers NVML and only falls back to nvidia-smi.
  • nvidia-smi calls go through _run_nvidia_smi and a thread-safe _NvidiaSmiCache. All cache mutations are protected by threading.Lock, so the cache is safe under free-threaded (GIL=0) builds. A TTL of 0 disables the cache; a positive value enables stale-within-TTL caching and LRU eviction.
  • device_smi/config.py exposes nvidia_smi_cache_ttl (default 0.0) and nvidia_smi_cache_maxsize (default 16), available as device_smi.config and Device.config.
  • Added regression tests for config validation, disabled caching at TTL=0, TTL caching, LRU eviction, thread safety, and the cached nvidia-smi path.
  • Updated README.md with the new Device.config API and the default-disabled caching behavior.

Usage

from device_smi import Device

# Enable a 2-second nvidia-smi cache (disabled by default, TTL=0)
Device.config.nvidia_smi_cache_ttl = 2.0
Device.config.nvidia_smi_cache_maxsize = 32

No new runtime dependencies are introduced.

Link to Devin session: https://app.devin.ai/sessions/35c36bf93a05479a9d150d6c3b1f10d5
Requested by: @Qubitium

@Qubitium Qubitium self-assigned this Aug 13, 2026
@devin-ai-integration

Copy link
Copy Markdown

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment, CI, and merge conflict monitoring

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant