def initialize(self, log=False): """ Initialize pynvml """ if not self.initialized: if K.backend() == "plaidml.keras.backend": loglevel = "INFO" if self.logger: self.logger.debug("plaidML Detected. Using plaidMLStats") loglevel = self.logger.getEffectiveLevel() self.plaid = plaidlib(loglevel=loglevel, log=log) elif IS_MACOS: if self.logger: self.logger.debug("macOS Detected. Using pynvx") try: pynvx.cudaInit() except RuntimeError: self.initialized = True return else: try: if self.logger: self.logger.debug("OS is not macOS. Using pynvml") pynvml.nvmlInit() except (pynvml.NVMLError_LibraryNotFound, # pylint: disable=no-member pynvml.NVMLError_DriverNotLoaded, # pylint: disable=no-member pynvml.NVMLError_NoPermission) as err: # pylint: disable=no-member if plaidlib is not None: self.plaid = plaidlib(log=log) else: raise err self.initialized = True self.get_device_count() self.get_active_devices() self.get_handles()
def _initialize(self, log=False): """ Initialize the library that will be returning stats for the system's GPU(s). For Nvidia (on Linux and Windows) the library is `pynvml`. For Nvidia (on macOS) the library is `pynvx`. For AMD `plaidML` is used. Parameters ---------- log: bool, optional Whether the class should output information to the logger. There may be occasions where the logger has not yet been set up when this class is queried. Attempting to log in these instances will raise an error. If GPU stats are being queried prior to the logger being available then this parameter should be set to ``False``. Otherwise set to ``True``. Default: ``False`` """ if not self._initialized: if get_backend() == "amd": self._log("debug", "AMD Detected. Using plaidMLStats") loglevel = "INFO" if self._logger is None else self._logger.getEffectiveLevel() self._plaid = plaidlib(log_level=loglevel, log=log) elif IS_MACOS: self._log("debug", "macOS Detected. Using pynvx") try: pynvx.cudaInit() except RuntimeError: self._initialized = True return else: try: self._log("debug", "OS is not macOS. Trying pynvml") pynvml.nvmlInit() except (pynvml.NVMLError_LibraryNotFound, # pylint: disable=no-member pynvml.NVMLError_DriverNotLoaded, # pylint: disable=no-member pynvml.NVMLError_NoPermission) as err: # pylint: disable=no-member if plaidlib is not None: self._log("debug", "pynvml errored. Trying plaidML") self._plaid = plaidlib(log=log) else: msg = ("There was an error reading from the Nvidia Machine Learning " "Library. Either you do not have an Nvidia GPU (in which case " "this warning can be ignored) or the most likely cause is " "incorrectly installed drivers. If this is the case, Please remove " "and reinstall your Nvidia drivers before reporting." "Original Error: {}".format(str(err))) self._log("warning", msg) self._initialized = True return except Exception as err: # pylint: disable=broad-except msg = ("An unhandled exception occured loading pynvml. " "Original error: {}".format(str(err))) if self._logger: self._logger.error(msg) else: print(msg) self._initialized = True return self._initialized = True self._get_device_count() self._get_active_devices() self._get_handles()
def initialize(self, log=False): """ Initialize pynvml """ if not self.initialized: if K.backend() == "plaidml.keras.backend": loglevel = "INFO" if self.logger: self.logger.debug("plaidML Detected. Using plaidMLStats") loglevel = self.logger.getEffectiveLevel() self.plaid = plaidlib(loglevel=loglevel, log=log) elif IS_MACOS: if self.logger: self.logger.debug("macOS Detected. Using pynvx") try: pynvx.cudaInit() except RuntimeError: self.initialized = True return else: try: if self.logger: self.logger.debug("OS is not macOS. Using pynvml") pynvml.nvmlInit() except ( pynvml.NVMLError_LibraryNotFound, # pylint: disable=no-member pynvml.NVMLError_DriverNotLoaded, # pylint: disable=no-member pynvml.NVMLError_NoPermission) as err: # pylint: disable=no-member if plaidlib is not None: self.plaid = plaidlib(log=log) else: msg = ( "There was an error reading from the Nvidia Machine Learning " "Library. Either you do not have an Nvidia GPU (in which case " "this warning can be ignored) or the most likely cause is " "incorrectly installed drivers. If this is the case, Please remove " "and reinstall your Nvidia drivers before reporting." "Original Error: {}".format(str(err))) if self.logger: self.logger.warning(msg) self.initialized = True return except Exception as err: # pylint: disable=broad-except msg = ("An unhandled exception occured loading pynvml. " "Original error: {}".format(str(err))) if self.logger: self.logger.error(msg) else: print(msg) self.initialized = True return self.initialized = True self.get_device_count() self.get_active_devices() self.get_handles()