docstr: A patched version of nvmlDeviceGetMemoryInfo.
  
  This tries `version=N.nvmlMemory_v2` if the nvmlDeviceGetMemoryInfo_v2
  function is available (for driver >= 515), or fallback to the legacy
  v1 API for (driver < 515) to yield a correct result. See #141.
docstr_tok: ["A","patched","version","of","nvmlDeviceGetMemoryInfo",".","This","tries","`","version=N.nvmlMemory_v2","`","if","the","nvmlDeviceGetMemoryInfo_v2","function","is","available","(","for","driver",">","=","515",")",",","or","fallback","to","the","legacy","v1","API","for","(","driver","<","515",")","to","yield","a","correct","result",".","See","#","141","."]
code: def nvmlDeviceGetMemoryInfo(handle):
      """A patched version of nvmlDeviceGetMemoryInfo.
  
      This tries `version=N.nvmlMemory_v2` if the nvmlDeviceGetMemoryInfo_v2
      function is available (for driver >= 515), or fallback to the legacy
      v1 API for (driver < 515) to yield a correct result. See #141.
      """
      M = pynvml_monkeypatch
  
      if M.has_memoryinfo_v2 is None:
          try:
              pynvml._nvmlGetFunctionPointer(
                  "nvmlDeviceGetMemoryInfo_v2"
              )
              M.has_memoryinfo_v2 = True
          except pynvml.NVMLError_FunctionNotFound:  # type: ignore
              M.has_memoryinfo_v2 = False
  
      if hasattr(pynvml, "nvmlMemory_v2"):  # pynvml >= 11.510.69
          try:
              memory = M.original_nvmlDeviceGetMemoryInfo(
                  handle, version=pynvml.nvmlMemory_v2
              )
          except pynvml.NVMLError_FunctionNotFound:  # type: ignore
              # pynvml >= 11.510 but driver is old (<515.39)
              memory = M.original_nvmlDeviceGetMemoryInfo(handle)
      else:
          if M.has_memoryinfo_v2:
              warnings.warn(
                  "Your NVIDIA driver requires a compatible version of "
                  "pynvml (>= 11.510.69) installed to display the correct "
                  "memory usage information (See #141 for more details). "
                  "Please try `pip install --upgrade nvidia-ml-py`.",
                  category=UserWarning,
              )
          memory = M.original_nvmlDeviceGetMemoryInfo(handle)
  
      return memory
  
file_path: gpustat/gpustat/nvml.py
imports: import atexit
  import functools
  import os
  import sys
  import textwrap
  import warnings
label_desc: Use image node_id 4 for calling the pynvml_monkeypatch obj's underlying member method code with example usage: obj.nvmlDeviceGetMemoryInfo(handle) and returns: memory