Skip to main content

Python Bindings for the NVIDIA Management Library

Project description

Provides a Python interface to GPU management and monitoring functions.

This is a wrapper around the NVML library. For information about the NVML library, see the NVML developer page http://developer.nvidia.com/nvidia-management-library-nvml

Download the latest package from: http://pypi.python.org/pypi/nvidia-ml-py/

Note this file can be run with ‘python -m doctest -v README.txt’ although the results are system dependent

The nvml header file contains function documentation that is relevant to this wrapper. The header file is distributed with. https://developer.nvidia.com/gpu-deployment-kit

The main difference is this library handles allocating structs and passing pointers to the functions, before returning the desired value. Non-success return codes are raised as exceptions as described in the section below.

REQUIRES

Python 2.5, or an earlier version with the ctypes module.

INSTALLATION

Manual Installation:
  • sudo python setup.py install

Pip Installation with python3:
  • python3 -m pip install nvidia-ml-py

USAGE

>>> from pynvml import *
>>> nvmlInit()
>>> print(f"Driver Version: {nvmlSystemGetDriverVersion()}")
Driver Version: 11.515.48
>>> deviceCount = nvmlDeviceGetCount()
>>> for i in range(deviceCount):
...     handle = nvmlDeviceGetHandleByIndex(i)
...     print(f"Device {i} : {nvmlDeviceGetName(handle)}")
...
Device 0 : Tesla K40c
>>> nvmlShutdown()

FUNCTIONS

Python methods wrap NVML functions, implemented in a C shared library. Each function’s use is the same with the following exceptions:

  • Instead of returning error codes, failing error codes are raised as Python exceptions.

    >>> try:
    ...     nvmlDeviceGetCount()
    ... except NVMLError as error:
    ...     print(error)
    ...
    Uninitialized
    
  • C function output parameters are returned from the corresponding Python function left to right.

nvmlReturn_t nvmlDeviceGetEccMode(nvmlDevice_t device,
                                  nvmlEnableState_t *current,
                                  nvmlEnableState_t *pending);

>>> nvmlInit()
>>> handle = nvmlDeviceGetHandleByIndex(0)
>>> (current, pending) = nvmlDeviceGetEccMode(handle)
  • C structs are converted into Python classes.

nvmlReturn_t DECLDIR nvmlDeviceGetMemoryInfo(nvmlDevice_t device,
                                             nvmlMemory_t *memory);
typedef struct nvmlMemory_st {
    unsigned long long total;
    unsigned long long free;
    unsigned long long used;
} nvmlMemory_t;

>>> info = nvmlDeviceGetMemoryInfo(handle)
>>> print(f"Total memory: {info.total}")
Total memory: 5636292608
>>> print(f"Free memory:, {info.free}")
Free memory: 5578420224
>>> print(f"Used memory: {info.used}")
Used memory: 57872384
  • Python handles string buffer creation.

nvmlReturn_t nvmlSystemGetDriverVersion(char* version,
                                        unsigned int length);

>>> version = nvmlSystemGetDriverVersion();
>>> nvmlShutdown()

For usage information see the NVML documentation.

VARIABLES

All meaningful NVML constants and enums are exposed in Python.

The NVML_VALUE_NOT_AVAILABLE constant is not used. Instead None is mapped to the field.

EXCEPTIONS

Since the C library uses return codes and python prefers exception handling, the library converts all return codes to various exceptions. The exceptions are generated automatically via a function at run time instead of being defined manually.

The list of exceptions can be found in NVMLError base class.

The example seen above in the FUNCTIONS section:

>>> try:
...     nvmlDeviceGetCount()
... except NVMLError as error:
...     print(error)
...
Uninitialized

Can be more accurately caught like this:

>>> try:
...     nvmlDeviceGetCount()
... except NVMLError_Uninitialized as error:
...     print(error)
...
Uninitialized

The conversion from name to exception is like this for all exceptions: - NVML_ERROR_UNINITIALIZED => NVMLError_Uninitialized - NVML_ERROR_LIBRARY_NOT_FOUND => NVMLError_LibraryNotFound - NVML_ERROR_ALREADY_INITIALIZED => NVMLError_AlreadyInitialized

RELEASE NOTES

Version 2.285.0 - Added new functions for NVML 2.285. See NVML documentation for more information. - Ported to support Python 3.0 and Python 2.0 syntax. - Added nvidia_smi.py tool as a sample app. Version 3.295.0 - Added new functions for NVML 3.295. See NVML documentation for more information. - Updated nvidia_smi.py tool - Includes additional error handling Version 4.304.0 - Added new functions for NVML 4.304. See NVML documentation for more information. - Updated nvidia_smi.py tool Version 4.304.3 - Fixing nvmlUnitGetDeviceCount bug Version 5.319.0 - Added new functions for NVML 5.319. See NVML documentation for more information. Version 6.340.0 - Added new functions for NVML 6.340. See NVML documentation for more information. Version 7.346.0 - Added new functions for NVML 7.346. See NVML documentation for more information. Version 7.352.0 - Added new functions for NVML 7.352. See NVML documentation for more information. Version 10.418 - Added new functions for NVML 10.418. See NVML documentation for more information. - Fixed issues with using the bindings with Python 3.x - Replaced sample app nvidia_smi.py with example.py Version 11.515.48 - Python3 support added - Updated API to add function new to NVML, bringing pynvml up to date with NVML - Added auto-version to handle byte and string conversion automatically for both structs and functions - Minor bug fixes - Added README.txt correctly in long_description for pypi.org

LICENSE

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

  • Neither the name of the NVIDIA Corporation nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nvidia-ml-py-11.515.75.tar.gz (30.0 kB view details)

Uploaded Source

Built Distribution

nvidia_ml_py-11.515.75-py3-none-any.whl (31.4 kB view details)

Uploaded Python 3

File details

Details for the file nvidia-ml-py-11.515.75.tar.gz.

File metadata

  • Download URL: nvidia-ml-py-11.515.75.tar.gz
  • Upload date:
  • Size: 30.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.7.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.6.9

File hashes

Hashes for nvidia-ml-py-11.515.75.tar.gz
Algorithm Hash digest
SHA256 e3c75f06d5a3201dc51136e00e58c5c132b3be5d604d86c143426adb4e41c490
MD5 c5cc20e1e82fa5ba1bc4e8bac8b98a1c
BLAKE2b-256 91b0ef4993b63c9614f8d86f467de2673133c5f0c14fad929b99a03a115966a2

See more details on using hashes here.

Provenance

File details

Details for the file nvidia_ml_py-11.515.75-py3-none-any.whl.

File metadata

  • Download URL: nvidia_ml_py-11.515.75-py3-none-any.whl
  • Upload date:
  • Size: 31.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.7.1 pkginfo/1.7.1 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.6.9

File hashes

Hashes for nvidia_ml_py-11.515.75-py3-none-any.whl
Algorithm Hash digest
SHA256 5bf5f5240f5a242689c1d1129135a0bd79c8b93d2a282c7229fe32ab63e7999b
MD5 cb6f5beb19668851345831d67d819e4a
BLAKE2b-256 d7433153ee4dd59736bb3fb774cfef095b8c5bcc719456ccd9ff2a6f483c7327

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page