Skip to content

Commit

Permalink
lint: update ruff rules
Browse files Browse the repository at this point in the history
  • Loading branch information
XuehaiPan committed Feb 16, 2024
1 parent 64e3533 commit 1710579
Show file tree
Hide file tree
Showing 22 changed files with 135 additions and 131 deletions.
3 changes: 2 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ select = B,C,E,F,W,Y,SIM
ignore =
# E203: whitespace before ':'
# E241: whitespace after ':'
# E704: multiple statements on one line (def)
# W503: line break before binary operator
# W504: line break after binary operator
# format by black
E203,E241,W503,W504,
E203,E241,E704,W503,W504,
# E501: line too long
# W505: doc line too long
# too long docstring due to long example blocks
Expand Down
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ repos:
- id: debug-statements
- id: double-quote-string-fixer
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.8
rev: v0.2.1
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Expand All @@ -34,7 +34,7 @@ repos:
hooks:
- id: isort
- repo: https://github.com/psf/black
rev: 23.12.0
rev: 24.2.0
hooks:
- id: black
- repo: https://github.com/asottile/pyupgrade
Expand All @@ -43,7 +43,7 @@ repos:
- id: pyupgrade
args: [--py37-plus] # sync with requires-python
- repo: https://github.com/pycqa/flake8
rev: 6.1.0
rev: 7.0.0
hooks:
- id: flake8
additional_dependencies:
Expand Down
2 changes: 1 addition & 1 deletion nvitop-exporter/nvitop_exporter/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -637,5 +637,5 @@ def update_device(self, device: Device) -> None: # pylint: disable=too-many-loc
pid,
username,
)
except KeyError:
except KeyError: # noqa: PERF203
pass
28 changes: 14 additions & 14 deletions nvitop/api/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@
import threading
import time
from collections import OrderedDict
from collections.abc import Hashable
from typing import TYPE_CHECKING, Any, Callable, ClassVar, Generator, Iterable, NamedTuple, overload

from nvitop.api import libcuda, libcudart, libnvml
Expand All @@ -132,6 +131,8 @@


if TYPE_CHECKING:
from collections.abc import Hashable

from typing_extensions import Literal # Python 3.8+
from typing_extensions import Self # Python 3.11+

Expand Down Expand Up @@ -1568,7 +1569,7 @@ def query_nvlink_throughput_counters() -> tuple[tuple[int | NaType, int]]:

if interval is not None:
if not interval >= 0.0:
raise ValueError('`interval` must be a non-negative number, got {interval!r}.')
raise ValueError(f'`interval` must be a non-negative number, got {interval!r}.')
if interval > 0.0:
self._nvlink_throughput_counters = query_nvlink_throughput_counters()
time.sleep(interval)
Expand Down Expand Up @@ -2125,7 +2126,7 @@ def processes(self) -> dict[int, GpuProcess]:
for s in sorted(samples, key=lambda s: s.timeStamp):
try:
processes[s.pid].set_gpu_utilization(s.smUtil, s.memUtil, s.encUtil, s.decUtil)
except KeyError:
except KeyError: # noqa: PERF203
pass
if not found_na:
for pid in set(processes).difference(s.pid for s in samples):
Expand Down Expand Up @@ -2309,7 +2310,7 @@ def mig_devices(self) -> list[MigDevice]:
for mig_index in range(max_mig_device_count):
try:
mig_device = MigDevice(index=(self.index, mig_index))
except libnvml.NVMLError:
except libnvml.NVMLError: # noqa: PERF203
break
else:
mig_devices.append(mig_device)
Expand Down Expand Up @@ -2968,16 +2969,14 @@ def _get_global_physical_device() -> PhysicalDevice:
def _parse_cuda_visible_devices(
cuda_visible_devices: str | None,
format: Literal['index'], # pylint: disable=redefined-builtin
) -> list[int] | list[tuple[int, int]]:
...
) -> list[int] | list[tuple[int, int]]: ...


@overload
def _parse_cuda_visible_devices(
cuda_visible_devices: str | None,
format: Literal['uuid'], # pylint: disable=redefined-builtin
) -> list[str]:
...
) -> list[str]: ...


@functools.lru_cache()
Expand All @@ -2986,7 +2985,7 @@ def _parse_cuda_visible_devices( # pylint: disable=too-many-branches,too-many-s
format: Literal['index', 'uuid'] = 'index', # pylint: disable=redefined-builtin
) -> list[int] | list[tuple[int, int]] | list[str]:
"""The underlining implementation for :meth:`parse_cuda_visible_devices`. The result will be cached."""
assert format in ('index', 'uuid')
assert format in {'index', 'uuid'}

try:
physical_device_attrs = _get_all_physical_device_attrs()
Expand Down Expand Up @@ -3062,9 +3061,9 @@ def strip_identifier(identifier: str) -> str:
identifier = identifier.strip()
if len(identifier) > 0 and (
identifier[0].isdigit()
or (len(identifier) > 1 and identifier[0] in ('+', '-') and identifier[1].isdigit())
or (len(identifier) > 1 and identifier[0] in {'+', '-'} and identifier[1].isdigit())
):
offset = 1 if identifier[0] in ('+', '-') else 0
offset = 1 if identifier[0] in {'+', '-'} else 0
while offset < len(identifier) and identifier[offset].isdigit():
offset += 1
identifier = identifier[:offset]
Expand Down Expand Up @@ -3181,12 +3180,13 @@ def _cuda_visible_devices_parser(

count = libcuda.cuDeviceGetCount()
uuids = [libcuda.cuDeviceGetUuid(libcuda.cuDeviceGet(i)) for i in range(count)]
queue.put(uuids)
return
except Exception as ex: # noqa: BLE001 # pylint: disable=broad-except
queue.put(ex)
if verbose:
raise ex
raise
else:
queue.put(uuids)
return
finally:
# Ensure non-empty queue
queue.put(libcuda.CUDAError_NotInitialized()) # pylint: disable=no-member
4 changes: 2 additions & 2 deletions nvitop/api/libnvml.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@


if not callable(getattr(_pynvml, 'nvmlInitWithFlags', None)):
raise ImportError(
raise ImportError( # noqa: TRY004
'Your installed package `nvidia-ml-py` is corrupted. Please reinstall package '
'`nvidia-ml-py` via `pip3 install --force-reinstall nvidia-ml-py nvitop`.',
)
Expand Down Expand Up @@ -444,7 +444,7 @@ def nvmlQuery(
and len(UNKNOWN_FUNCTIONS) < UNKNOWN_FUNCTIONS_CACHE_SIZE
):
UNKNOWN_FUNCTIONS[identifier] = (func, e2)
LOGGER.error(
LOGGER.exception(
(
'ERROR: A FunctionNotFound error occurred while calling %s.\n'
'Please verify whether the `nvidia-ml-py` package is '
Expand Down
9 changes: 3 additions & 6 deletions nvitop/api/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def wrapped(self: GpuProcess, *args: Any, **kwargs: Any) -> Any:
pass
# See also `GpuProcess.failsafe`
if fallback is _RAISE or not getattr(_USE_FALLBACK_WHEN_RAISE, 'value', False):
raise ex
raise
if isinstance(fallback, tuple):
if isinstance(ex, host.AccessDenied) and fallback == ('No Such Process',):
return ['No Permissions']
Expand Down Expand Up @@ -232,10 +232,7 @@ def _gone(self) -> bool:
def _gone(self, value: bool) -> None:
if value:
with self.INSTANCE_LOCK:
try:
del self.INSTANCES[self.pid]
except KeyError:
pass
self.INSTANCES.pop(self.pid, None)
self._super_gone = value

def __repr__(self) -> str:
Expand Down Expand Up @@ -434,7 +431,7 @@ def as_snapshot(
for attr in ('command', 'running_time', 'running_time_human'):
try:
attributes[attr] = getattr(self, attr)()
except (host.AccessDenied, host.ZombieProcess):
except (host.AccessDenied, host.ZombieProcess): # noqa: PERF203
attributes[attr] = ad_value

return Snapshot(real=self, **attributes)
Expand Down
6 changes: 3 additions & 3 deletions nvitop/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,9 +632,9 @@ def utilization2string(utilization: int | float | NaType) -> str: # noqa: PYI04

def boolify(string: str, default: Any = None) -> bool:
"""Convert the given value, usually a string, to boolean."""
if string.lower() in ('true', 'yes', 'on', 'enabled', '1'):
if string.lower() in {'true', 'yes', 'on', 'enabled', '1'}:
return True
if string.lower() in ('false', 'no', 'off', 'disabled', '0'):
if string.lower() in {'false', 'no', 'off', 'disabled', '0'}:
return False
if default is not None:
return bool(default)
Expand Down Expand Up @@ -708,7 +708,7 @@ def __iter__(self) -> Iterator[str]:
"""Support ``for name in snapshot`` syntax and ``*`` tuple unpack ``[*snapshot]`` syntax."""

def gen() -> Generator[str, None, None]:
yield from (name for name in self.__dict__ if name not in ('real', 'timestamp'))
yield from (name for name in self.__dict__ if name not in {'real', 'timestamp'})

return gen()

Expand Down
7 changes: 5 additions & 2 deletions nvitop/callbacks/lightning.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
from __future__ import annotations

import time
from typing import Any
from typing import TYPE_CHECKING, Any

import lightning.pytorch as pl # pylint: disable=import-error
from lightning.pytorch.callbacks import Callback # pylint: disable=import-error
from lightning.pytorch.utilities import rank_zero_only # pylint: disable=import-error
from lightning.pytorch.utilities.exceptions import ( # pylint: disable=import-error
Expand All @@ -34,6 +33,10 @@
from nvitop.callbacks.utils import get_devices_by_logical_ids, get_gpu_stats


if TYPE_CHECKING:
import lightning.pytorch as pl


# Modified from pytorch_lightning.callbacks.GPUStatsMonitor
class GpuStatsLogger(Callback): # pylint: disable=too-many-instance-attributes
"""Automatically log GPU stats during training stage. :class:`GpuStatsLogger` is a callback and
Expand Down
7 changes: 5 additions & 2 deletions nvitop/callbacks/pytorch_lightning.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
from __future__ import annotations

import time
from typing import Any
from typing import TYPE_CHECKING, Any

import pytorch_lightning as pl # pylint: disable=import-error
from pytorch_lightning.callbacks import Callback # pylint: disable=import-error
from pytorch_lightning.utilities import rank_zero_only # pylint: disable=import-error
from pytorch_lightning.utilities.exceptions import ( # pylint: disable=import-error
Expand All @@ -34,6 +33,10 @@
from nvitop.callbacks.utils import get_devices_by_logical_ids, get_gpu_stats


if TYPE_CHECKING:
import pytorch_lightning as pl


# Modified from pytorch_lightning.callbacks.GPUStatsMonitor
class GpuStatsLogger(Callback): # pylint: disable=too-many-instance-attributes
"""Automatically log GPU stats during training stage. :class:`GpuStatsLogger` is a callback and
Expand Down
86 changes: 38 additions & 48 deletions nvitop/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import curses
import os
import sys
import textwrap

from nvitop.api import HostProcess, libnvml
from nvitop.gui import UI, USERNAME, Device, colored, libcurses, set_color, setlocale_utf8
Expand Down Expand Up @@ -240,31 +241,29 @@ def posfloat(argstring: str) -> float:
gpu_util_thresh = list(
map(int, os.getenv('NVITOP_GPU_UTILIZATION_THRESHOLDS', '').split(',')),
)[:2]
if (
len(gpu_util_thresh) != 2
or min(gpu_util_thresh) <= 0
or max(gpu_util_thresh) >= 100
):
raise ValueError
except ValueError:
pass
else:
args.gpu_util_thresh = gpu_util_thresh
if (
len(gpu_util_thresh) == 2
and min(gpu_util_thresh) > 0
and max(gpu_util_thresh) < 100
):
args.gpu_util_thresh = gpu_util_thresh
if args.mem_util_thresh is None:
try:
mem_util_thresh = list(
map(int, os.getenv('NVITOP_MEMORY_UTILIZATION_THRESHOLDS', '').split(',')),
)[:2]
if (
len(mem_util_thresh) != 2
or min(mem_util_thresh) <= 0
or max(mem_util_thresh) >= 100
):
raise ValueError
except ValueError:
pass
else:
args.mem_util_thresh = mem_util_thresh
if (
len(mem_util_thresh) == 2
and min(mem_util_thresh) > 0
and max(mem_util_thresh) < 100
):
args.mem_util_thresh = mem_util_thresh

return args

Expand Down Expand Up @@ -385,9 +384,11 @@ def main() -> int:

if len(libnvml.UNKNOWN_FUNCTIONS) > 0:
unknown_function_messages = [
'ERROR: Some FunctionNotFound errors occurred while calling:'
if len(libnvml.UNKNOWN_FUNCTIONS) > 1
else 'ERROR: A FunctionNotFound error occurred while calling:',
(
'ERROR: Some FunctionNotFound errors occurred while calling:'
if len(libnvml.UNKNOWN_FUNCTIONS) > 1
else 'ERROR: A FunctionNotFound error occurred while calling:'
),
]
unknown_function_messages.extend(
f' nvmlQuery({(func.__name__ if not isinstance(func, str) else func)!r}, *args, **kwargs)'
Expand All @@ -404,41 +405,30 @@ def main() -> int:
)

if libnvml._pynvml_installation_corrupted: # pylint: disable=protected-access
message = '\n'.join(
(
'WARNING: The `nvidia-ml-py` package is corrupted. Please reinstall it using:',
'',
' pip3 install --force-reinstall nvitop nvidia-ml-py',
'',
'or install `nvitop` in an isolated environment:',
'',
' pip3 install --upgrade pipx',
' pipx install nvitop',
'',
),
message = textwrap.dedent(
"""
WARNING: The `nvidia-ml-py` package is corrupted. Please reinstall it using:
pip3 install --force-reinstall nvitop nvidia-ml-py
or install `nvitop` in an isolated environment:
pip3 install --upgrade pipx
pipx run nvitop
""",
)
messages.append(message)
messages.append(message.strip() + '\n')

if len(messages) > 0:
for message in messages:
if message.startswith('ERROR:'):
message = message.replace(
'ERROR:',
colored('ERROR:', color='red', attrs=('bold',)),
1,
)
elif message.startswith('WARNING:'):
message = message.replace(
'WARNING:',
colored('WARNING:', color='yellow', attrs=('bold',)),
1,
)
elif message.startswith('HINT:'):
message = message.replace(
'HINT:',
colored('HINT:', color='green', attrs=('bold',)),
1,
)
for prefix, color in (('ERROR:', 'red'), ('WARNING:', 'yellow'), ('HINT:', 'green')):
if message.startswith(prefix):
message = message.replace(
prefix,
colored(prefix, color=color, attrs=('bold',)),
1,
)
break
print(message, file=sys.stderr)
return 1
return 0
Expand Down
2 changes: 1 addition & 1 deletion nvitop/gui/library/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def mig_devices(self):
for mig_index in range(self.max_mig_device_count()):
try:
mig_device = MigDevice(index=(self.index, mig_index))
except libnvml.NVMLError:
except libnvml.NVMLError: # noqa: PERF203
break
else:
mig_devices.append(mig_device)
Expand Down

0 comments on commit 1710579

Please sign in to comment.