Skip to content

Ray

RayParallelBackend(config)

Bases: BaseParallelBackend

Class used to wrap ray to make it transparent to algorithms.

It shouldn't be initialized directly. You should instead call init_parallel_backend().

PARAMETER DESCRIPTION
config

instance of ParallelConfig with cluster address, number of cpus, etc.

TYPE: ParallelConfig

Source code in src/pydvl/utils/parallel/backends/ray.py
def __init__(self, config: ParallelConfig):
    self.config = {"address": config.address, "logging_level": config.logging_level}
    if self.config["address"] is None:
        self.config["num_cpus"] = config.n_cpus_local
    if not ray.is_initialized():
        ray.init(**self.config)
    # Register ray joblib backend
    register_ray()

wrap(fun, **kwargs)

Wraps a function as a ray remote.

PARAMETER DESCRIPTION
fun

the function to wrap

TYPE: Callable

kwargs

keyword arguments to pass to @ray.remote

DEFAULT: {}

RETURNS DESCRIPTION
Callable

The .remote method of the ray RemoteFunction.

Source code in src/pydvl/utils/parallel/backends/ray.py
def wrap(self, fun: Callable, **kwargs) -> Callable:
    """Wraps a function as a ray remote.

    Args:
        fun: the function to wrap
        kwargs: keyword arguments to pass to @ray.remote

    Returns:
        The `.remote` method of the ray `RemoteFunction`.
    """
    if len(kwargs) > 0:
        return ray.remote(**kwargs)(fun).remote  # type: ignore
    return ray.remote(fun).remote  # type: ignore

Last update: 2023-09-02
Created: 2023-09-02