rlightning.utils.ray

Ray utilities module for distributed computing.

This module provides utilities for working with Ray actors and tasks, including task submission, object resolution, and actor mixins.

Available components:
  • RayActorMixin: Mixin class for Ray actor functionality.

  • TaskSubmitter: Task submission utility with pending task management.

  • resolve_object: Utility to resolve Ray ObjectRefs to values.

class rlightning.utils.ray.RayActorMixin[source]

Bases: object

Mixin class providing Ray actor functionality.

This class provides common methods for Ray actors, including GPU management, node identification, and network address retrieval. Should be used as a base class for any class that needs to be a Ray actor.

classmethod as_remote(num_cpus: int | None = None, num_gpus: int | None = None, memory: int | None = None, object_store_memory: int | None = None, resources: Dict[str, float] | None = None) Type[source]

Create a remote class for Ray Actor initialization.

Parameters:
  • num_cpus – Number of CPUs required.

  • num_gpus – Number of GPUs required.

  • memory – Memory required in bytes.

  • object_store_memory – Object store memory required.

  • resources – Custom resource requirements.

Returns:

Ray remote class.

init(*args: Any, **kwargs: Any) None[source]

Initialize the remote class.

Must be implemented in derived classes.

Raises:

NotImplementedError – Always, as this must be overridden.

class rlightning.utils.ray.TaskSubmitter(max_pending_tasks_each_worker: int = 1)[source]

Bases: object

Task submitter for local and remote Ray actor methods.

Manages task submission to either local class functions or remote Ray actors, ensuring that the number of pending tasks does not exceed a specified limit.

This class is thread-safe for concurrent task submissions.

max_pending_tasks

Maximum number of pending tasks allowed per actor.

worker_ref_to_pending_tasks

Mapping from actor handles to pending task refs.

submit(method: Callable, *args: Any, _block: bool = False, **kwargs: Any)[source]

Submit a task to a local method or Ray actor.

This method is thread-safe for concurrent submissions.

Parameters:
  • method – The method to be called (local or ray.actor.ActorMethod).

  • *args – Positional arguments for the method.

  • _block – If True, block until the remote task completes and return the actual result instead of a future.

  • **kwargs – Keyword arguments for the method.

Returns:

the actual result. For remote methods with _block=False: a ray.ObjectRef.

Return type:

For local methods or when _block=True

Raises:

ValueError – If _block is not a boolean value.

rlightning.utils.ray.resolve_object(obj: MockModule('ray.ObjectRef') | Sequence[MockModule('ray.ObjectRef')] | Mapping[Any, MockModule('ray.ObjectRef')] | Any) Any[source]

Top-level resolution of Ray ObjectRef(s).

Resolves ObjectRef instances to their actual values. No recursion into nested containers - only handles top-level ObjectRefs.

Parameters:

obj – An ObjectRef, a non-empty Sequence of ObjectRef, or a non-empty Mapping[Any, ObjectRef].

Returns:

The resolved value or a container of the same type when batch-resolved; otherwise, returns the input unchanged.

Submodules