o
    `۷i                     @   s   d dl mZmZ d dlmZmZ d dlmZ d dl	m
Z
mZmZ eddG dd deZeG d	d
 d
eZeG dd deZdS )    )ABCabstractmethod)AnyDict)get_train_context)
DeprecatedDeveloperAPI	PublicAPIstable)	stabilityc                   @   s   e Zd ZdZedeeef fddZedefddZ	edefddZ
ed	d
 ZedefddZedefddZedefddZedefddZedefddZedefddZedefddZeedd ZdS )TrainContextz(Abstract interface for training context.returnc                 C   s   ddl m} t|)zB[Deprecated] User metadata dict passed to the Trainer constructor.r   )!_GET_METADATA_DEPRECATION_MESSAGE)ray.train.contextr   DeprecationWarning)selfr    r   N/home/ubuntu/vllm_env/lib/python3.10/site-packages/ray/train/v2/api/context.pyget_metadata   s   zTrainContext.get_metadatac                 C      ddl m} t|d)z4[Deprecated] Trial name for the corresponding trial.r   *_TUNE_SPECIFIC_CONTEXT_DEPRECATION_MESSAGEget_trial_namer   r   r   formatr   r   r   r   r   r         zTrainContext.get_trial_namec                 C   r   )z2[Deprecated] Trial id for the corresponding trial.r   r   get_trial_idr   r   r   r   r   r      r   zTrainContext.get_trial_idc                 C   r   )z9[Deprecated] Trial resources for the corresponding trial.r   r   get_trial_resourcesr   r   r   r   r   r   '   r   z TrainContext.get_trial_resourcesc                 C   r   )a  [Deprecated] Log directory corresponding to the trial directory for a Tune session.
        This is deprecated for Ray Train and should no longer be called in Ray Train workers.

        If this directory is needed, please pass it into the `train_loop_config` directly.
        r   r   get_trial_dirr   r   r   r   r   r   0   s   zTrainContext.get_trial_dirc                 C      dS )z,Experiment name for the corresponding trial.Nr   r   r   r   r   get_experiment_name=   s   z TrainContext.get_experiment_namec                 C   r    )a  Get the current world size (i.e. total number of workers) for this run.

        .. testcode::

            import ray.train
            from ray.train.torch import TorchTrainer

            NUM_WORKERS = 2

            def train_fn_per_worker(config):
                assert ray.train.get_context().get_world_size() == NUM_WORKERS

            trainer = TorchTrainer(
                train_fn_per_worker,
                scaling_config=ray.train.ScalingConfig(num_workers=NUM_WORKERS),
            )
            trainer.fit()

        Nr   r!   r   r   r   get_world_sizeB      zTrainContext.get_world_sizec                 C   r    )a  Get the world rank of this worker.

        .. testcode::

            import ray.train
            from ray.train.torch import TorchTrainer

            def train_fn_per_worker(config):
                if ray.train.get_context().get_world_rank() == 0:
                    print("Worker 0")

            trainer = TorchTrainer(
                train_fn_per_worker,
                scaling_config=ray.train.ScalingConfig(num_workers=2),
            )
            trainer.fit()

        Nr   r!   r   r   r   get_world_rankY      zTrainContext.get_world_rankc                 C   r    )a  Get the local rank of this worker (rank of the worker on its node).

        .. testcode::

            import ray.train
            from ray.train.torch import TorchTrainer

            def train_fn_per_worker(config):
                if ray.train.get_context().get_local_rank() == 0:
                    print("Local rank 0 worker")

            trainer = TorchTrainer(
                train_fn_per_worker,
                scaling_config=ray.train.ScalingConfig(num_workers=2),
            )
            trainer.fit()

        Nr   r!   r   r   r   get_local_ranko   r&   zTrainContext.get_local_rankc                 C   r    )a!  Get the local world size of this node (i.e. number of workers on this node).

        Example:

            .. testcode::

                import ray.train
                from ray.train.torch import TorchTrainer

                def train_fn_per_worker():
                    print(ray.train.get_context().get_local_world_size())

                trainer = TorchTrainer(
                    train_fn_per_worker,
                    scaling_config=ray.train.ScalingConfig(num_workers=2),
                )
                trainer.fit()

        Nr   r!   r   r   r   get_local_world_size   r$   z!TrainContext.get_local_world_sizec                 C   r    )a  Get the rank of this node.

        Example:

            .. testcode::

                import ray.train
                from ray.train.torch import TorchTrainer

                def train_fn_per_worker():
                    print(ray.train.get_context().get_node_rank())

                trainer = TorchTrainer(
                    train_fn_per_worker,
                    scaling_config=ray.train.ScalingConfig(num_workers=1),
                )
                trainer.fit()

        Nr   r!   r   r   r   get_node_rank   r$   zTrainContext.get_node_rankc                 C   r    )aE  Returns the :class:`~ray.train._internal.storage.StorageContext` storage
        context which gives advanced access to the filesystem and paths
        configured through `RunConfig`.

        NOTE: This is a DeveloperAPI, and the `StorageContext` interface may change
        without notice between minor versions.
        Nr   r!   r   r   r   get_storage   s   
zTrainContext.get_storageN)__name__
__module____qualname____doc__r   r   strr   r   r   r   r   r   r   r"   intr#   r%   r'   r(   r)   r   r*   r   r   r   r   r   
   s6    
r   c                   @   sl   e Zd ZdZdefddZdefddZdefddZdefd	d
Z	defddZ
defddZdd ZdS )DistributedTrainContextz4Implementation of TrainContext for distributed mode.r   c                 C   
   t   S N)get_internal_train_contextr"   r!   r   r   r   r"         
z+DistributedTrainContext.get_experiment_namec                 C   r2   r3   )r4   r#   r!   r   r   r   r#      r5   z&DistributedTrainContext.get_world_sizec                 C   r2   r3   )r4   r%   r!   r   r   r   r%      r5   z&DistributedTrainContext.get_world_rankc                 C   r2   r3   )r4   r'   r!   r   r   r   r'      r5   z&DistributedTrainContext.get_local_rankc                 C   r2   r3   )r4   r(   r!   r   r   r   r(      r5   z,DistributedTrainContext.get_local_world_sizec                 C   r2   r3   )r4   r)   r!   r   r   r   r)      r5   z%DistributedTrainContext.get_node_rankc                 C   r2   r3   )r4   r*   r!   r   r   r   r*      r5   z#DistributedTrainContext.get_storageN)r+   r,   r-   r.   r/   r"   r0   r#   r%   r'   r(   r)   r*   r   r   r   r   r1      s    r1   c                   @   s   e Zd ZdZ					ddededededed	efd
dZdefddZdefddZdefddZ	defddZ
defddZdefddZdd ZdS )LocalTrainContextz.Implementation of TrainContext for local mode.   r   experiment_name
world_size
world_rank
local_ranklocal_world_size	node_rankc                 C   s(   || _ || _|| _|| _|| _|| _d S r3   )r8   r9   r:   r;   r<   r=   )r   r8   r9   r:   r;   r<   r=   r   r   r   __init__   s   	
zLocalTrainContext.__init__r   c                 C      | j S r3   )r8   r!   r   r   r   r"         z%LocalTrainContext.get_experiment_namec                 C   r?   r3   )r9   r!   r   r   r   r#      r@   z LocalTrainContext.get_world_sizec                 C   r?   r3   )r:   r!   r   r   r   r%      r@   z LocalTrainContext.get_world_rankc                 C   r?   r3   )r;   r!   r   r   r   r'      r@   z LocalTrainContext.get_local_rankc                 C   r?   r3   )r<   r!   r   r   r   r(      r@   z&LocalTrainContext.get_local_world_sizec                 C   r?   r3   )r=   r!   r   r   r   r)      r@   zLocalTrainContext.get_node_rankc                 C   s   t d)Nz+Local storage context not yet implemented. )NotImplementedErrorr!   r   r   r   r*      s   zLocalTrainContext.get_storageN)r7   r   r   r7   r   )r+   r,   r-   r.   r/   r0   r>   r"   r#   r%   r'   r(   r)   r*   r   r   r   r   r6      s6    
r6   N)abcr   r   typingr   r   (ray.train.v2._internal.execution.contextr   r4   ray.util.annotationsr   r   r	   r   r1   r6   r   r   r   r   <module>   s     6