Skip to main content

Python 3 Bindings for the NVIDIA Management Library

Project description

Python 3 compatible bindings to the NVIDIA Management Library. Can be used to query the state of the GPUs on your system. This was ported from the NVIDIA provided python bindings nvidia-ml-py, which only supported python 2. I have forked from version 7.352.0. The old library was itself a wrapper around the NVIDIA Management Library.

In addition to the functions to query the state of the GPU, I have written a function to ‘restrict’ the available GPUs by setting the CUDA_VISIBLE_DEVICES environment variable. See the Utils section below for more info.

Requires

Python 3.5+.

Installation

From PyPi:

$ pip install py3nvml

From GitHub:

$ pip install -e git+https://github.com/fbcotter/py3nvml#egg=py3nvml

Or, download and pip install:

$ git clone https://github.com/fbcotter/py3nvml
$ cd py3nvml
$ pip install .

Package Description

Utils

(Added by me - not ported from NVIDIA library)

You can call the grab_gpus(num_gpus, gpu_select) function to check the available gpus and set the CUDA_VISIBLE_DEVICES environment variable as need be. This is useful if you have a shared resource, and are using a library like tensorflow where calls to tf.Session() grabs all available gpus.

E.g.

import py3nvml
py3nvml.grab_gpus(3)
sess = tf.Session() # now we only grab 3 gpus!

Or the following will grab 2 gpus from the first 4 (and leave any higher gpus untouched)

import py3nvml
py3nvml.grab_gpus(num_gpus=2, gpu_select=[0,1,2,3])
sess = tf.Session()

This will look for 3 available gpus in the range of gpus from 0 to 3. The range option is not necessary, and it only serves to restrict the search space for the grab_gpus.

Regular Usage

(below here is everything ported from pynvml)

from py3nvml.py3nvml import *
nvmlInit()
print("Driver Version: {}".format(str(nvmlSystemGetDriverVersion())))
# e.g. will print:
#   Driver Version: 352.00
deviceCount = nvmlDeviceGetCount()
for i in range(deviceCount):
    handle = nvmlDeviceGetHandleByIndex(i)
    print("Device {}: {}".format(i, str(nvmlDeviceGetName(handle))))
# e.g. will print:
#  Device 0 : Tesla K40c
#  Device 1 : Tesla K40c

nvmlShutdown()

Additionally, see py3nvml.nvidia_smi.py. This does the equivalent of the nvidia-smi command:

nvidia-smi -q -x

With

import py3nvml.nvidia_smi as smi
print(smi.XmlDeviceQuery())

Function description

As stated above, the pynvml library consists of python methods which wrap several 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. E.g. They could be wrapped with exception handlers.

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

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

    Can be called like so:

    nvmlInit()
    handle = nvmlDeviceGetHandleByIndex(0)
    (current, pending) = nvmlDeviceGetEccMode(handle)
  • C structs are converted into Python classes. E.g. the C struct:

    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;

    Becomes:

    info = nvmlDeviceGetMemoryInfo(handle)
    print("Total memory: {}".format(info.total))
    # will print:
    #   Total memory: 5636292608
    print("Free memory: {}".format(info.free))
    # will print:
    #   Free memory: 5578420224
    print("Used memory: ".format(info.used))
    # will print:
    #   Used memory: 57872384
  • Python handles string buffer creation. E.g. the C function:

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

    Can be called like so:

    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.

Release Notes (for pynvml)

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.

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

py3nvml-0.1.0rc3.tar.gz (26.7 kB view details)

Uploaded Source

Built Distribution

py3nvml-0.1.0rc3-py3-none-any.whl (31.0 kB view details)

Uploaded Python 3

File details

Details for the file py3nvml-0.1.0rc3.tar.gz.

File metadata

  • Download URL: py3nvml-0.1.0rc3.tar.gz
  • Upload date:
  • Size: 26.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for py3nvml-0.1.0rc3.tar.gz
Algorithm Hash digest
SHA256 58e37338a2bee0355047034d8b1263696ad1293338e75a84c02945bb6a8d7d86
MD5 728fc0daeac85f79d54155dd484d25d2
BLAKE2b-256 57b5c0a75d6c0097cb20aed814349d21c8f0ffd057c39611af1bf7de627e966e

See more details on using hashes here.

File details

Details for the file py3nvml-0.1.0rc3-py3-none-any.whl.

File metadata

File hashes

Hashes for py3nvml-0.1.0rc3-py3-none-any.whl
Algorithm Hash digest
SHA256 60b9d927263f2cfc7c7df6c1ecd23020e04c375f36de7514e123466dc02927db
MD5 f637d6bfa7da469c52118426a302347e
BLAKE2b-256 cd10d9852d56f2b199c0131dbb595dcd6906a48d5596c92a4f6bcf11568302cb

See more details on using hashes here.

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