o
    ci                      @   s   d dl Z d dlmZ d dlmZ d dlmZmZmZm	Z	 d dl
Zd dlmZmZmZ d dlmZ d dlmZ d dlmZmZ d d	lmZ d d
lmZ erRd dlmZ e e Z!eG dd deZeG dd deZeeddG dd dZ"dS )    N)	dataclass)Path)TYPE_CHECKINGListOptionalUnion)CheckpointConfigFailureConfigScalingConfig)
RuntimeEnv)_DEPRECATED)FAIL_FAST_DEPRECATION_MESSAGE%TRAINER_RESOURCES_DEPRECATION_MESSAGE)date_str)	PublicAPI)UserCallbackc                       s>   e Zd ZU dZdZee ed<  fddZe	dd Z
  ZS )r
   a  Configuration for scaling training.

    Args:
        num_workers: The number of workers (Ray actors) to launch.
            Each worker will reserve 1 CPU by default. The number of CPUs
            reserved by each worker can be overridden with the
            ``resources_per_worker`` argument.
        use_gpu: If True, training will be done on GPUs (1 per worker).
            Defaults to False. The number of GPUs reserved by each
            worker can be overridden with the ``resources_per_worker``
            argument.
        resources_per_worker: If specified, the resources
            defined in this Dict is reserved for each worker.
            Define the ``"CPU"`` and ``"GPU"`` keys (case-sensitive) to
            override the number of CPU or GPUs used by each worker.
        placement_strategy: The placement strategy to use for the
            placement group of the Ray actors. See :ref:`Placement Group
            Strategies <pgroup-strategy>` for the possible options.
        accelerator_type: [Experimental] If specified, Ray Train will launch the
            training coordinator and workers on the nodes with the specified type
            of accelerators.
            See :ref:`the available accelerator types <accelerator_types>`.
            Ensure that your cluster has instances with the specified accelerator type
            or is able to autoscale to fulfill the request.

    Example:

        .. testcode::

            from ray.train import ScalingConfig
            scaling_config = ScalingConfig(
                # Number of distributed workers.
                num_workers=2,
                # Turn on/off GPU.
                use_gpu=True,
            )

        .. testoutput::
            :hide:

            ...

    Ntrainer_resourcesc                    s    | j d ur	ttt   d S N)r   DeprecationWarningr   super__post_init__self	__class__ K/home/ubuntu/.local/lib/python3.10/site-packages/ray/train/v2/api/config.pyr   M   s   
zScalingConfig.__post_init__c                 C   s   i S r   r   r   r   r   r   _trainer_resources_not_noneS   s   z)ScalingConfig._trainer_resources_not_none)__name__
__module____qualname____doc__r   r   dict__annotations__r   propertyr   __classcell__r   r   r   r   r
      s   
 ,r
   c                   @   s.   e Zd ZU dZeZeeef e	d< dd Z
dS )r	   aV  Configuration related to failure handling of each training run.

    Args:
        max_failures: Tries to recover a run at least this many times.
            Will recover from the latest checkpoint if present.
            Setting to -1 will lead to infinite recovery retries.
            Setting to 0 will disable retries. Defaults to 0.
    	fail_fastc                 C   s   | j tkr	ttd S r   )r&   r   r   r   r   r   r   r   r   e   s   
zFailureConfig.__post_init__N)r   r   r    r!   r   r&   r   boolstrr#   r   r   r   r   r   r	   X   s   
 	r	   stable)	stabilityc                   @   s   e Zd ZU dZdZee ed< dZee ed< dZ	ee
jj ed< dZee ed< dZee ed< dZeed  ed	< dZeeeef  ed
< eZeed< eZeed< eZeed< eZeed< eZeed< dd ZdS )	RunConfigaD  Runtime configuration for training runs.

    Args:
        name: Name of the trial or experiment. If not provided, will be deduced
            from the Trainable.
        storage_path: [Beta] Path where all results and checkpoints are persisted.
            Can be a local directory or a destination on cloud storage.
            For multi-node training/tuning runs, this must be set to a
            shared storage location (e.g., S3, NFS).
            This defaults to the local ``~/ray_results`` directory.
        storage_filesystem: [Beta] A custom filesystem to use for storage.
            If this is provided, `storage_path` should be a path with its
            prefix stripped (e.g., `s3://bucket/path` -> `bucket/path`).
        failure_config: Failure mode configuration.
        checkpoint_config: Checkpointing configuration.
        callbacks: [DeveloperAPI] A list of callbacks that the Ray Train controller
            will invoke during training.
        worker_runtime_env: [DeveloperAPI] Runtime environment configuration
            for all Ray Train worker actors.
    Nnamestorage_pathstorage_filesystemfailure_configcheckpoint_configr   	callbacksworker_runtime_envsync_configverbosestopprogress_reporterlog_to_filec                    s   ddl m} | jd u r|| _| jst | _| jst | _t| jtr(| j	 | _d}g d}|D ]}t
| |tkr@t||q0| jsKdt  | _| jpOg | _| jpUi | _ddlm  t fdd| jD smtd	d S )
Nr   )DEFAULT_STORAGE_PATHaI  `RunConfig({})` is deprecated. This configuration was a Ray Tune API that did not support Ray Train usage well, so we are dropping support going forward. If you heavily rely on these configurations, you can run Ray Train as a single Ray Tune trial. See this issue for more context: https://github.com/ray-project/ray/issues/49454)r3   r4   r5   r6   r7   zray_train_run-RayTrainCallbackc                 3   s    | ]}t | V  qd S r   )
isinstance).0cbr9   r   r   	<genexpr>   s    z*RunConfig.__post_init__.<locals>.<genexpr>zAll callbacks must be instances of `ray.train.UserCallback`. Passing in a Ray Tune callback is no longer supported. See this issue for more context: https://github.com/ray-project/ray/issues/49454)ray.train.constantsr8   r-   r/   r	   r0   r   r;   r   as_posixgetattrr   r   formatr,   r   r1   r2   ray.train.v2.api.callbackr:   all
ValueError)r   r8   run_config_deprecation_messageunsupported_paramsparamr   r9   r   r      s4   

zRunConfig.__post_init__)r   r   r    r!   r,   r   r(   r#   r-   r.   pyarrowfs
FileSystemr/   r	   r0   r   r1   r   r2   r   r"   r   r   r3   r4   r5   r6   r7   r   r   r   r   r   r+   k   s   
 r+   )#loggingdataclassesr   pathlibr   typingr   r   r   r   
pyarrow.fsrI   ray.air.configr   r	   FailureConfigV1r
   ScalingConfigV1ray.runtime_envr    ray.train.v2._internal.constantsr   &ray.train.v2._internal.migration_utilsr   r   ray.train.v2._internal.utilr   ray.util.annotationsr   	ray.trainr   	getLoggerr   loggerr+   r   r   r   r   <module>   s*    
: