o
    ci                     @   sL   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Z
dS )	    )AnyDict)get_train_context)
DeprecatedDeveloperAPI	PublicAPIstable)	stabilityc                   @   s   e 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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edd ZdS )TrainContext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   L/home/ubuntu/.local/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   -   s   zTrainContext.get_trial_dirc                 C   
   t   S )z,Experiment name for the corresponding trial.)get_internal_train_contextget_experiment_namer   r   r   r   r    :   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
            from ray import train
            from ray.train import ScalingConfig
            from ray.train.tensorflow import TensorflowTrainer

            NUM_WORKERS = 2

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

            trainer = TensorflowTrainer(
                train_loop_per_worker,
                scaling_config=ScalingConfig(num_workers=NUM_WORKERS),
            )
            trainer.fit()

        .. testoutput::
            :hide:

            ...
        )r   get_world_sizer!   r   r   r   r"   >      
zTrainContext.get_world_sizec                 C   r   )ar  Get the world rank of this worker.

        .. testcode::

            import ray
            from ray import train
            from ray.train import ScalingConfig
            from ray.train.tensorflow import TensorflowTrainer

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

            trainer = TensorflowTrainer(
                train_loop_per_worker,
                scaling_config=ScalingConfig(num_workers=2),
            )
            trainer.fit()

        .. testoutput::
            :hide:

            ...
        )r   get_world_rankr!   r   r   r   r$   Z   s   
zTrainContext.get_world_rankc                 C   r   )a  Get the local rank of this worker (rank of the worker on its node).

        .. testcode::

            import torch

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

            def train_loop_per_worker(config):
                if torch.cuda.is_available():
                    torch.cuda.set_device(train.get_context().get_local_rank())
                ...

            trainer = TorchTrainer(
                train_loop_per_worker,
                scaling_config=ScalingConfig(num_workers=2, use_gpu=True),
            )
            trainer.fit()

        .. testoutput::
            :hide:

            ...
        )r   get_local_rankr!   r   r   r   r%   u   s   
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
                from ray import train
                from ray.train import ScalingConfig
                from ray.train.torch import TorchTrainer

                def train_loop_per_worker():
                    print(train.get_context().get_local_world_size())

                trainer = TorchTrainer(
                    train_loop_per_worker,
                    scaling_config=ScalingConfig(num_workers=1),
                )
                trainer.fit()

            .. testoutput::
                :hide:

                ...
        )r   get_local_world_sizer!   r   r   r   r&      r#   z!TrainContext.get_local_world_sizec                 C   r   )az  Get the rank of this node.

        Example:

            .. testcode::

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

                def train_loop_per_worker():
                    print(train.get_context().get_node_rank())

                trainer = TorchTrainer(
                    train_loop_per_worker,
                    scaling_config=ScalingConfig(num_workers=1),
                )
                trainer.fit()

            .. testoutput::
                :hide:

                ...
        )r   get_node_rankr!   r   r   r   r'      r#   zTrainContext.get_node_rankc                 C   r   )aF  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 developer API, and the `StorageContext` interface may change
        without notice between minor versions.
        )r   get_storager!   r   r   r   r(      s   
	zTrainContext.get_storageN)__name__
__module____qualname__r   r   strr   r   r   r   r   r   r    intr"   r$   r%   r&   r'   r   r(   r   r   r   r   r
   	   s&    
r
   N)typingr   r   (ray.train.v2._internal.execution.contextr   r   ray.util.annotationsr   r   r   r
   r   r   r   r   <module>   s
    