o
    ci                     @   s   d dl Z d dlmZmZmZmZmZ d dlZd dlm	Z	 d dl
mZmZmZmZ d dlmZ d dlmZmZ eddG d	d
 d
ZdS )    N)DictListLiteralOptionalUnion)ActorHandle)DataIteratorDatasetExecutionOptions	NodeIdStr)ExecutionResources)DeveloperAPI	PublicAPIstable)	stabilityc                   @   s   e Zd ZdZ			ddeed ee f dee	 de
fdd	Zd
edefddZedeeef dedeee  deee  deeeef  f
ddZede	fddZdS )
DataConfigzClass responsible for configuring Train dataset preprocessing.

    For advanced use cases, this class can be subclassed and the `configure()` method
    overriden for custom data preprocessing.
    allNTdatasets_to_splitexecution_optionsenable_shard_localityc                 C   sZ   t |ts	|dkr|| _ntdt|j d| d|p t | _|| _	d| _
d| _dS )a  Construct a DataConfig.

        Args:
            datasets_to_split: Specifies which datasets should be split among workers.
                Can be set to "all" or a list of dataset names. Defaults to "all",
                i.e. split all datasets.
            execution_options: The execution options to pass to Ray Data. By default,
                the options will be optimized for data ingest. When overriding this,
                base your options off of `DataConfig.default_ingest_options()`.
            enable_shard_locality: If true, when sharding the datasets across Train
                workers, locality will be considered to minimize cross-node data transfer.
                This is on by default.
        r   zV`datasets_to_split` should be a 'all' or a list of strings of dataset names. Received z with value .g        N)
isinstancelist_datasets_to_split	TypeErrortype__name__r   default_ingest_options_execution_options_enable_shard_locality_num_train_cpus_num_train_gpus)selfr   r   r    r#   S/home/ubuntu/.local/lib/python3.10/site-packages/ray/train/_internal/data_config.py__init__   s   

zDataConfig.__init__num_train_cpusnum_train_gpusc                 C   s   || _ || _dS )zSet the total number of CPUs and GPUs used by training.

        If CPU or GPU resource limits are not set, they will be set to the
        total cluster resources minus the resources used by training.
        N)r    r!   )r"   r&   r'   r#   r#   r$   set_train_total_resources7   s   
z$DataConfig.set_train_total_resourcesdatasets
world_sizeworker_handlesworker_node_idsreturnc                 K   s  dd t |D }| D ]\}}|jdu r|| q| jdkr(t| }	nt| j}	| jr2|nd}
| D ]M\}}t	| j
}| rS|jt| j| jd|_||}||j_||	v rvt|j|d|
dD ]
\}}||| |< qjq8t |D ]
}| || |< qzq8|S )a  Configure how Train datasets should be assigned to workers.

        Args:
            datasets: The datasets dict passed to Train by the user.
            world_size: The number of Train workers in total.
            worker_handles: The actor handles of the Train workers.
            worker_node_ids: The node ids of the Train workers.
            kwargs: Forwards compatibility placeholder.

        Returns:
            A list of dataset splits for each worker. The size of the list must be
            equal to `world_size`. Each element of the list contains the assigned
            `DataIterator` instances by name for the worker.
        c                 S   s   g | ]}i qS r#   r#   ).0_r#   r#   r$   
<listcomp>X   s    z(DataConfig.configure.<locals>.<listcomp>Nr   )cpugpuT)equallocality_hints)rangeitemsnameset_namer   setkeysr   copydeepcopyr   is_resource_limits_defaultexclude_resourcesaddr   r    r!   contextr   	enumeratestreaming_splititerator)r"   r)   r*   r+   r,   kwargsoutputdataset_namedatasetr   r4   r7   dsr   isplitr#   r#   r$   	configureA   s@   




zDataConfig.configurec                  C   s2   t jj } t| jj| jj| jj| jj	| jj
dS )zThe default Ray Data options used for data ingest.

        By default, configurations are carried over from what is already set
        in DataContext.
        )locality_with_outputresource_limitsr>   preserve_orderverbose_progress)raydataDataContextget_currentr
   r   rL   rM   r>   rN   rO   )ctxr#   r#   r$   r      s   z!DataConfig.default_ingest_options)r   NT)r   
__module____qualname____doc__r   r   r   strr   r
   boolr%   floatr(   r   r   r	   intr   r   r   rK   staticmethodr   r#   r#   r#   r$   r      s6    
$



@r   )r;   typingr   r   r   r   r   rP   	ray.actorr   ray.datar   r	   r
   r   9ray.data._internal.execution.interfaces.execution_optionsr   ray.util.annotationsr   r   r   r#   r#   r#   r$   <module>   s    