Skip to content

Commit

Permalink
refactor-ruff-b024
Browse files Browse the repository at this point in the history
Signed-off-by: SimoTw <[email protected]>

add

Signed-off-by: SimoTw <[email protected]>
  • Loading branch information
simotw committed Jan 18, 2025
1 parent 3cfe8ae commit ec42def
Show file tree
Hide file tree
Showing 16 changed files with 50 additions and 18 deletions.
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ ignore = [
"F841",
"B018",
"B023",
"B024",
"B026",
"B027",
"B035",
Expand Down
2 changes: 1 addition & 1 deletion python/ray/_private/runtime_env/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@


@DeveloperAPI
class RuntimeEnvPlugin(ABC):
class RuntimeEnvPlugin(ABC): # noqa: B024
"""Abstract base class for runtime environment plugins."""

name: str = None
Expand Down
24 changes: 16 additions & 8 deletions python/ray/air/_internal/device_manager/torch_device_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,47 @@
from typing import List, Union

import torch
import abc


class TorchDeviceManager(ABC):
"""This class contains the function needed for supporting
an acclerator family in Ray AI Library.
"""

@abc.abstractmethod
def is_available(self) -> bool:
"""Validate if device is available."""
...
raise NotImplementedError

@abc.abstractmethod
def get_devices(self) -> List[torch.device]:
"""Gets the correct torch device configured for this process"""
...
raise NotImplementedError

@abc.abstractmethod
def set_device(self, device: Union[torch.device, int, str, None]):
"""Set the correct device for this process"""
...
raise NotImplementedError

@abc.abstractmethod
def supports_stream(self) -> bool:
"""Validate if the device type support create a stream"""
...
raise NotImplementedError

@abc.abstractmethod
def create_stream(self, device: torch.device):
"""Create a device stream"""
...
raise NotImplementedError

@abc.abstractmethod
def get_stream_context(self, stream):
"""Get a stream context of device. If device didn't support stream,
this should return a empty context manager instead of None.
this should return a empty context manager instead of None.
"""
...
raise NotImplementedError

@abc.abstractmethod
def get_current_stream(self):
"""Get current stream on accelerators like torch.cuda.current_stream"""
...
raise NotImplementedError
6 changes: 6 additions & 0 deletions python/ray/air/execution/resources/resource_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class ResourceManager(abc.ABC):
"""

@abc.abstractmethod
def request_resources(self, resource_request: ResourceRequest):
"""Request resources.
Expand All @@ -75,6 +76,7 @@ def request_resources(self, resource_request: ResourceRequest):
"""
raise NotImplementedError

@abc.abstractmethod
def cancel_resource_request(self, resource_request: ResourceRequest):
"""Cancel resource request.
Expand All @@ -84,10 +86,12 @@ def cancel_resource_request(self, resource_request: ResourceRequest):
"""
raise NotImplementedError

@abc.abstractmethod
def has_resources_ready(self, resource_request: ResourceRequest) -> bool:
"""Returns True if resources for the given request are ready to be acquired."""
raise NotImplementedError

@abc.abstractmethod
def acquire_resources(
self, resource_request: ResourceRequest
) -> Optional[AcquiredResources]:
Expand All @@ -98,6 +102,7 @@ def acquire_resources(
"""
raise NotImplementedError

@abc.abstractmethod
def free_resources(self, acquired_resource: AcquiredResources):
"""Free acquired resources from usage and return them to the resource manager.
Expand Down Expand Up @@ -132,6 +137,7 @@ def update_state(self):
"""
pass

@abc.abstractmethod
def clear(self):
"""Reset internal state and clear all resources.
Expand Down
3 changes: 1 addition & 2 deletions python/ray/air/util/torch_dist.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"""

import os
from abc import ABC
from collections import defaultdict
from datetime import timedelta
from typing import Callable, List, T
Expand All @@ -20,7 +19,7 @@
from ray.train._internal.utils import get_address_and_port


class TorchDistributedWorker(ABC):
class TorchDistributedWorker:
"""Defines the interfaces required by the init_torch_dist_process_group().
This is modeled after RayTrainerWorker, which allows arbitrary functions
Expand Down
1 change: 1 addition & 0 deletions python/ray/data/preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ def transform_batch(self, data: "DataBatchType") -> "DataBatchType":
return self._transform_batch(data)

@DeveloperAPI
@abc.abstractmethod
def _fit(self, ds: "Dataset") -> "Preprocessor":
"""Sub-classes should override this instead of fit()."""
raise NotImplementedError()
Expand Down
3 changes: 1 addition & 2 deletions python/ray/tests/spark/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import pytest
import sys
from unittest import mock
from abc import ABC

import ray

Expand Down Expand Up @@ -66,7 +65,7 @@ def _setup_ray_cluster(*args, **kwds):
_logger = logging.getLogger(__name__)


class RayOnSparkCPUClusterTestBase(ABC):
class RayOnSparkCPUClusterTestBase:
spark = None
num_total_cpus = None
num_total_gpus = None
Expand Down
2 changes: 1 addition & 1 deletion python/ray/train/_internal/accelerator.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import abc


class Accelerator(abc.ABC):
class Accelerator(abc.ABC): # noqa: B024
"""A utility that contains methods to accelerate training."""
1 change: 1 addition & 0 deletions python/ray/tune/logger/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def __init__(self, config: Dict, logdir: str, trial: Optional["Trial"] = None):
def _init(self):
pass

@abc.abstractmethod
def on_result(self, result):
"""Given a result, appends it to the existing log."""

Expand Down
2 changes: 2 additions & 0 deletions python/ray/tune/stopper/stopper.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ class Stopper(abc.ABC):
"""

@abc.abstractmethod
def __call__(self, trial_id: str, result: Dict[str, Any]) -> bool:
"""Returns true if the trial should be terminated given the result."""
raise NotImplementedError

@abc.abstractmethod
def stop_all(self) -> bool:
"""Returns true if the experiment should be terminated."""
raise NotImplementedError
Expand Down
1 change: 1 addition & 0 deletions python/ray/tune/tune.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ def _ray_auto_init(entrypoint: str):


class _Config(abc.ABC):
@abc.abstractmethod
def to_dict(self) -> dict:
"""Converts this configuration to a dict format."""
raise NotImplementedError
Expand Down
3 changes: 1 addition & 2 deletions python/ray/util/state/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import logging
import sys
import warnings
from abc import ABC
from dataclasses import asdict, field, fields
from enum import Enum, unique
from typing import Any, Dict, List, Optional, Set, Tuple, Union
Expand Down Expand Up @@ -244,7 +243,7 @@ def state_column(*, filterable: bool, detail: bool = False, format_fn=None, **kw
return field(**kwargs)


class StateSchema(ABC):
class StateSchema:
"""Schema class for Ray resource abstraction.
The child class must be dataclass. All child classes
Expand Down
5 changes: 5 additions & 0 deletions release/ray_release/cluster_manager/cluster_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,15 @@ def _annotate_cluster_compute(
)
return cluster_compute

@abc.abstractmethod
def build_configs(self, timeout: float = 30.0):
raise NotImplementedError

@abc.abstractmethod
def delete_configs(self):
raise NotImplementedError

@abc.abstractmethod
def start_cluster(self, timeout: float = 600.0):
raise NotImplementedError

Expand All @@ -134,9 +137,11 @@ def terminate_cluster(self, wait: bool = False):
except Exception as e:
logger.exception(f"Could not terminate cluster: {e}")

@abc.abstractmethod
def terminate_cluster_ex(self, wait: bool = False):
raise NotImplementedError

@abc.abstractmethod
def get_cluster_address(self) -> str:
raise NotImplementedError

Expand Down
9 changes: 9 additions & 0 deletions release/ray_release/command_runner/command_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,17 @@ def get_full_command_env(self, env: Optional[Dict] = None):

return full_env

@abc.abstractmethod
def prepare_local_env(self, ray_wheels_url: Optional[str] = None):
"""Prepare local environment, e.g. install dependencies."""
raise NotImplementedError

@abc.abstractmethod
def prepare_remote_env(self):
"""Prepare remote environment, e.g. upload files."""
raise NotImplementedError

@abc.abstractmethod
def wait_for_nodes(self, num_nodes: int, timeout: float = 900.0):
"""Wait for cluster nodes to be up.
Expand All @@ -81,6 +84,7 @@ def wait_for_nodes(self, num_nodes: int, timeout: float = 900.0):
"""
raise NotImplementedError

@abc.abstractmethod
def save_metrics(self, start_time: float, timeout: float = 900.0):
"""Obtains Prometheus metrics from head node and saves them
to ``self.metrics_output_json``.
Expand All @@ -94,6 +98,7 @@ def save_metrics(self, start_time: float, timeout: float = 900.0):
"""
raise NotImplementedError

@abc.abstractmethod
def run_command(
self,
command: str,
Expand Down Expand Up @@ -127,14 +132,18 @@ def get_last_logs(self) -> Optional[str]:
logger.exception(f"Error fetching logs: {e}")
return None

@abc.abstractmethod
def get_last_logs_ex(self):
raise NotImplementedError

@abc.abstractmethod
def fetch_results(self) -> Dict[str, Any]:
raise NotImplementedError

@abc.abstractmethod
def fetch_metrics(self) -> Dict[str, Any]:
raise NotImplementedError

@abc.abstractmethod
def fetch_artifact(self, artifact_path):
raise NotImplementedError
2 changes: 2 additions & 0 deletions release/ray_release/file_manager/file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ class FileManager(abc.ABC):
def __init__(self, cluster_manager: ClusterManager):
self.cluster_manager = cluster_manager

@abc.abstractmethod
def upload(self, source: Optional[str] = None, target: Optional[str] = None):
"""Upload source to target.
Infers target dir from basename if not stated.
"""
raise NotImplementedError

@abc.abstractmethod
def download(self, source: str, target: str):
"""Download source_dir to target_dir."""
raise NotImplementedError
3 changes: 2 additions & 1 deletion rllib/connectors/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ def in_eval(self):
def __str__(self, indentation: int = 0):
return " " * indentation + self.__class__.__name__

@abc.abstractmethod
def to_state(self) -> Tuple[str, Any]:
"""Serialize a connector into a JSON serializable Tuple.
Expand Down Expand Up @@ -333,7 +334,7 @@ def transform(self, ac_data: ActionConnectorDataType) -> ActionConnectorDataType


@OldAPIStack
class ConnectorPipeline(abc.ABC):
class ConnectorPipeline:
"""Utility class for quick manipulation of a connector pipeline."""

def __init__(self, ctx: ConnectorContext, connectors: List[Connector]):
Expand Down

0 comments on commit ec42def

Please sign in to comment.