o
    bi#                     @   s  d dl Z d dlZd dlZd dlmZ d dlmZmZmZm	Z	m
Z
mZ d dlmZmZ erLd dlmZ d dlmZ d dlmZ d dlmZ d d	lmZmZ h d
Zh dZh dZddhZh dZG dd deZde de
e  fddZ!d7ddZ"dd Z#ded	 fddZ$d8d!d"Z%d#d$ Z&d%d& Z'd'e	ed(  d)ee e(f fd*d+Z)d'e	ed(  d)e*fd,d-Z+d9d0d1Z,d)e*fd2d3Z-d4ed)dfd5d6Z.dS ):    N)Enum)TYPE_CHECKINGDictListOptionalSetUnion)TagKeyrecord_extra_usage_tag)StorageContextBaseTrainerCallbackTrialSchedulerBasicVariantGeneratorSearcher>   TorchTrainerHorovodTrainerXGBoostTrainerLightGBMTrainerTensorflowTrainer>   r   r   r   r   DataParallelTrainer>   AxSearchTuneBOHB
HEBOSearchZOOptSearchOptunaSearchBayesOptSearchHyperOptSearchNevergradSearchConcurrencyLimiterRepeater>	   PB2FIFOSchedulerHyperBandForBOHBHyperBandSchedulerMedianStoppingRuleAsyncHyperBandSchedulerPopulationBasedTrainingResourceChangingSchedulerPopulationBasedTrainingReplayc                   @   s   e Zd ZdZdZdZdZdS )AirEntrypointz	Tuner.fitzTrainer.fitztune.runztune.run_experimentsN)__name__
__module____qualname__TUNERTRAINERTUNE_RUNTUNE_RUN_EXPERIMENTS r6   r6   K/home/ubuntu/.local/lib/python3.10/site-packages/ray/air/_internal/usage.pyr.   B   s
    r.   allowed_module_path_prefix	whitelistc                 C   s(   | j }| jj}||r||v r|S dS )a  Find the class name of the object. If the object is not
    under `allowed_module_path_prefix` or if its class is not in the whitelist,
    return "Custom".

    Args:
        obj: The object under inspection.
        allowed_module_path_prefix: If the `obj`'s class is not under
            the `allowed_module_path_prefix`, its class name will be anonymized.
        whitelist: If the `obj`'s class is not in the `whitelist`,
            it will be anonymized.
    Returns:
        The class name to be tagged with telemetry.
    Custom)r0   	__class__r/   
startswith)objr8   r9   module_pathcls_namer6   r6   r7   _find_class_nameI   s
   r@   trainerr   c                 C   6   ddl m} t| |sJ t| dt}ttj| d S )Nr   r   	ray.train)ray.train.trainerr   
isinstancer@   AIR_TRAINERSr
   r	   AIR_TRAINER)rA   r   trainer_namer6   r6   r7   tag_air_trainer_      rI   c                 C   rB   )Nr   )r   rC   )&ray.train.v2.api.data_parallel_trainerr   rE   r@   TRAIN_V2_TRAINERSr
   r	   TRAIN_TRAINER)rA   r   rH   r6   r6   r7   tag_train_v2_trainerg   rJ   rN   searcherc                 C   sj   ddl m}m} t| |rttjd d S t| |r1t| dt	t
}|t
v r)d S ttj| d S J d)Nr   r   r   zray.tune.searchFzZNot expecting a non-BasicVariantGenerator, non-Searcher type passed in for `tag_searcher`.)ray.tune.searchr   r   rE   r
   r	   TUNE_SEARCHERr@   TUNE_SEARCHERSunionTUNE_SEARCHER_WRAPPERS)rO   r   r   searcher_namer6   r6   r7   tag_searchero   s   

rV   	schedulerr   c                 C   rB   )Nr   r   zray.tune.schedulers)ray.tune.schedulersr   rE   r@   TUNE_SCHEDULERSr
   r	   TUNE_SCHEDULER)rW   r   scheduler_namer6   r6   r7   tag_scheduler   rJ   r\   c                   C      t tjd d S N1)r
   r	    AIR_SETUP_WANDB_INTEGRATION_USEDr6   r6   r6   r7   tag_setup_wandb      ra   c                   C   r]   r^   )r
   r	   !AIR_SETUP_MLFLOW_INTEGRATION_USEDr6   r6   r6   r7   tag_setup_mlflow   rb   rd   	callbacksr   returnc                 C   s   ddl m} ddlm} ddlm} ddlm} ddlm	} ddl
m} ddlm} ||||f| }d	d
 |D }	tt}
| pAg } | D ]/}t||sLqD|jj}||	v r]|
|  d7  < qDt||rk|
d  d7  < qD|
d  d7  < qD|
S )zHCreates a map of callback class name -> count given a list of callbacks.r   )CometLoggerCallback)MLflowLoggerCallback)WandbLoggerCallbackr   )LoggerCallback)AimLoggerCallback)DEFAULT_CALLBACK_CLASSESc                 S   s   g | ]}|j qS r6   )r/   ).0callback_clsr6   r6   r7   
<listcomp>   s    z$_count_callbacks.<locals>.<listcomp>   CustomLoggerCallbackCustomCallback)ray.air.integrations.cometrg   ray.air.integrations.mlflowrh   ray.air.integrations.wandbri   ray.tuner   ray.tune.loggerrj   ray.tune.logger.aimrk   ray.tune.utils.callbackrl   collectionsdefaultdictintrE   r;   r/   )re   rg   rh   ri   r   rj   rk   rl   built_in_callbackscallback_namescallback_countscallbackcallback_namer6   r6   r7   _count_callbacks   s6   


r   c                 C   s2   | sdS t | }|rt|}ttj| dS dS )a  Records built-in callback usage via a JSON str representing a
    dictionary mapping callback class name -> counts.

    User-defined callbacks will increment the count under the `CustomLoggerCallback`
    or `CustomCallback` key depending on which of the provided interfaces they subclass.
    NOTE: This will NOT track the name of the user-defined callback,
    nor its implementation.

    This will NOT report telemetry if no callbacks are provided by the user.

    Returns:
        bool: True if usage was recorded, False otherwise.
    FN)r   jsondumpsr
   r	   AIR_CALLBACKS)re   r   callback_counts_strr6   r6   r7   tag_callbacks   s   
r   storager   c                 C   s>   h d}| j r
d}n| jj|v r| jj}nd}ttj| dS )a  Records the storage configuration of an experiment.

    The storage configuration is set by `RunConfig(storage_path, storage_filesystem)`.

    The possible storage types (defined by `pyarrow.fs.FileSystem.type_name`) are:
    - 'local' = pyarrow.fs.LocalFileSystem. This includes NFS usage.
    - 'mock' = pyarrow.fs._MockFileSystem. This is used for testing.
    - ('s3', 'gcs', 'abfs', 'hdfs'): Various remote storage schemes
        with default implementations in pyarrow.
    - 'custom' = All other storage schemes, which includes ALL cases where a
        custom `storage_filesystem` is provided.
    - 'other' = catches any other cases not explicitly handled above.
    >   s3gcsabfshdfsmocklocalcustomotherN)custom_fs_providedstorage_filesystem	type_namer
   r	   AIR_STORAGE_CONFIGURATION)r   r9   storage_config_tagr6   r6   r7   tag_storage_type   s   
r   c                  C   s|   ddl m}  ddlm} ddlm} tt | ||}g }|D ]}|t	j
v r,|| q |r<t|}ttj| dS dS )ac  Records usage of environment variables exposed by the Ray AIR libraries.

    NOTE: This does not track the values of the environment variables, nor
    does this track environment variables not explicitly included in the
    `all_ray_air_env_vars` allow-list.

    Returns:
        bool: True if at least one environment var is supplied by the user.
    r   )AIR_ENV_VARS)TRAIN_ENV_VARS)TUNE_ENV_VARSTF)ray.air.constantsr   ray.train.constantsr   ray.tune.constantsr   sortedsetrS   osenvironappendr   r   r
   r	   )r   r   r   all_ray_air_env_varsuser_supplied_env_varsenv_varenv_vars_strr6   r6   r7   tag_ray_air_env_vars   s    



r   
entrypointc                 C   s   | t v sJ ttj| j dS )z.Records the entrypoint to an AIR training run.N)r.   r
   r	   AIR_ENTRYPOINTvalue)r   r6   r6   r7   tag_air_entrypoint  s   r   )rA   r   )rW   r   )r   r   )/rz   r   r   enumr   typingr   r   r   r   r   r   ray._private.usage.usage_libr	   r
   ray.train._internal.storager   rD   r   rv   r   rX   r   rP   r   r   rF   rL   rR   rT   rY   r.   strr@   rI   rN   rV   r\   ra   rd   r|   r   boolr   r   r   r   r6   r6   r6   r7   <module>   s@     	

"&
 