o
    xi]N                     @  s^  U d Z ddlmZ ddlZddlZddlZddlZddlZddlm	Z	m
Z
mZ ddlZddlm  mZ ddlmZ ddlmZ ddlmZmZmZ dd	lmZmZ e	rfdd
lmZ ddlmZ ddlm Z  G dd dZ!eej"e!f Z"G dd dZ#da$de%d< 	 e& Z'd(ddZ(e) 			d)d*ddZ*d+d d!Z+d,d-d"d#Z,e) d,d.d&d'Z-dS )/aa  Global W&B library state.

This module manages global state, which for wandb includes:

- Settings configured through `wandb.setup()`
- The list of active runs
- A subprocess ("the internal service") that asynchronously uploads metrics

This module is fork-aware: in a forked process such as that spawned by the
`multiprocessing` module, `wandb.singleton()` returns a new object, not the
one inherited from the parent process. This requirement comes from backward
compatibility with old design choices: the hardest one to fix is that wandb
was originally designed to have a single run for the entire process that
`wandb.init()` was meant to return. Back then, the only way to create
multiple simultaneous runs in a single script was to run subprocesses, and since
the built-in `multiprocessing` module forks by default, this required a PID
check to make `wandb.init()` ignore the inherited global run.

Another reason for fork-awareness is that the process that starts up
the internal service owns it and is responsible for shutting it down,
and child processes shouldn't also try to do that. This is easier to
redesign.
    )annotationsN)TYPE_CHECKINGAnyUnion)
CONFIG_DIR)
UsageError)asyncio_managerimport_hooks
wb_logging   )config_utilserver)	wandb_run)ServiceConnectionSettingsc                   @  sj   e Zd ZdZdddZd ddZd ddZd ddZd ddZd ddZ	d ddZ
d!ddZd"ddZdS )#_EarlyLoggerzKEarly logger which captures logs in memory until logging can be configured.returnNonec                 C  s   g | _ g | _| j| _d S N)_log
_exceptionwarningwarnself r   I/home/ubuntu/.local/lib/python3.10/site-packages/wandb/sdk/wandb_setup.py__init__3   s   z_EarlyLogger.__init__msgstrargsr   kwargsc                 O     | j tj|||f d S r   )r   appendloggingDEBUGr   r   r!   r"   r   r   r   debug9      z_EarlyLogger.debugc                 O  r#   r   )r   r$   r%   INFOr'   r   r   r   info<   r)   z_EarlyLogger.infoc                 O  r#   r   )r   r$   r%   WARNINGr'   r   r   r   r   ?   r)   z_EarlyLogger.warningc                 O  r#   r   )r   r$   r%   ERRORr'   r   r   r   errorB   r)   z_EarlyLogger.errorc                 O  r#   r   )r   r$   r%   CRITICALr'   r   r   r   criticalE   r)   z_EarlyLogger.criticalc                 O  s   | j |||f d S r   )r   r$   r'   r   r   r   	exceptionH   s   z_EarlyLogger.exceptionlevelc                 O  s   | j ||||f d S r   )r   r$   )r   r2   r   r!   r"   r   r   r   logK   s   z_EarlyLogger.log
new_loggerLoggerc                 C  sh   | |usJ | j D ]\}}}}|j||g|R i | q	| jD ]\}}}|j|g|R i | q d S r   )r   r3   r   r1   )r   r4   r2   r   r!   r"   r   r   r   _flushN   s   z_EarlyLogger._flushNr   r   )r   r    r!   r   r"   r   r   r   )
r2   r    r   r    r!   r   r"   r   r   r   r4   r5   r   r   )__name__
__module____qualname____doc__r   r(   r+   r   r.   r0   r1   r3   r6   r   r   r   r   r   0   s    







r   c                   @  s  e Zd ZdZdKddZedLd	d
ZdMddZdMddZedNddZ	dOddZ
dPddZdddQd d!ZdRd#d$ZdOd%d&ZdSd)d*ZdTd+d,ZedUd.d/ZedVd0d1ZdWd2d3ZdWd4d5ZdXd7d8ZedYd:d;ZdZd=d>Zed[d@dAZd\d]dDdEZd^dGdHZd^dIdJZdS )__WandbSetupzW&B library singleton.pidintr   r   c                 C  sT   t  | _| j  d | _g | _t | _d | _	d | _
|| _t | _d | _d | _d S r   )r   AsyncioManager_asyncerstart_connection_active_runs	threadingLock_active_runs_lock_sweep_config_server_pidr   _logger	_settings_settings_environ)r   r>   r   r   r   r   \   s   



z_WandbSetup.__init__asyncio_manager.AsyncioManagerc                 C     | j S )z*The internal asyncio thread used by wandb.)rA   r   r   r   r   asyncerp      z_WandbSetup.asyncerrunwandb_run.Runc                 C  sR   | j  || jvr| j| W d   dS W d   dS 1 s"w   Y  dS )zAppend a run to the active runs list.

        This must be called when a run is initialized.

        Args:
            run: A newly initialized run.
        N)rG   rD   r$   r   rR   r   r   r   add_active_runu   s   
"z_WandbSetup.add_active_runc                 C  sR   z| j  | j| W d   W dS 1 sw   Y  W dS  ty(   Y dS w )zRemove the run from the active runs list.

        This must be called when a run is finished.

        Args:
            run: A run that is finished or crashed.
        N)rG   rD   remove
ValueErrorrT   r   r   r   remove_active_run   s   &z_WandbSetup.remove_active_runwandb_run.Run | Nonec                 C  sL   | j  | js	 W d   dS | jd W  d   S 1 sw   Y  dS )z;The most recently initialized run that is not yet finished.N)rG   rD   r   r   r   r   most_recent_active_run   s   $z"_WandbSetup.most_recent_active_runc                 C  sF   | j  t| j}W d   n1 sw   Y  |D ]}|  qdS )a  Finish all unfinished runs.

        NOTE: This is slightly inefficient as it finishes runs one at a time.
        This only exists to support using the `reinit="finish_previous"`
        setting together with `reinit="create_new"` which does not seem to be a
        useful pattern. Since `"create_new"` should eventually become the
        default and only behavior, it does not seem worth optimizing.
        N)rG   listrD   finish)r   	runs_copyrR   r   r   r   finish_all_active_runs   s   

z"_WandbSetup.finish_all_active_runsboolc                   sr   | j sdS ddh  fdd| j  D } fddtj D }t| t| kp8t| t| kS )z@Check if os.environ has changed since settings were initialized.FWANDB_SERVICEWANDB_KUBEFLOW_URLc                   (   i | ]\}}| d r| vr||qS WANDB_
startswith.0kvexclude_env_varsr   r   
<dictcomp>       z6_WandbSetup.did_environment_change.<locals>.<dictcomp>c                   rc   rd   rf   rh   rl   r   r   rn      ro   )rM   itemsosenvironsetkeysvalues)r   singleton_envos_envr   rl   r   did_environment_change   s   

z"_WandbSetup.did_environment_changeN)	overridessystem_settings_path
str | Nonedisable_sagemakerry   Settings | Nonec                C  s,  ddl m} | | _t }| jdtj  | jd|  || j_	|r,|| j_
n&ttd }rDt| }t|d | j_
nttddd	d | j_
| j  | jd
 tj | _| j| j | j  | jjs|st r| jd t| j |r| j| t| jd dS )a  Load settings from environment variables, config files, etc.

        Args:
            system_settings_path: Location of system settings file to use.
                If not provided, reads the WANDB_CONFIG_DIR environment
                variable or uses the default location.
            disable_sagemaker: If true, skips modifying settings based on
                SageMaker.
            overrides: Additional settings to apply to the global settings.
        r   r   zCurrent SDK version is zConfigure stats pid to Nsettings~z.configwandbz+Loading settings from environment variableszLoading SageMaker settings)wandb.sdk.wandb_settingsr   rL   rq   getpidrK   r+   r   __version__x_stats_pidsettings_systemgetenvr   pathlibPath
expanduserr    update_from_system_settingsrr   copyrM   update_from_env_varsupdate_from_system_environmentsagemaker_disable	sagemakeris_using_sagemakerset_global_settingsupdate_from_settings	termsetup)r   rz   r|   ry   r   r>   config_dir_str
config_dirr   r   r   _load_settings   s<   


z_WandbSetup._load_settingsr~   c                 C  sN   | j s|r|jnd}|r|jnd}| j|||d dS |r%| j | dS dS )zzUpdate settings, initializing them if necessary.

        Args:
            settings: Overrides to apply, if any.
        NF)rz   r|   ry   )rL   r   r   r   r   )r   r~   rz   r|   r   r   r   _update   s   
z_WandbSetup._updatec                 C  s
   d | _ d S r   )rI   r   r   r   r   update_user_settings  s   
z _WandbSetup.update_user_settingsr4   r5   c                 C  s0   | j |u rd S t| j tr| j | || _ d S r   )rK   
isinstancer   r6   )r   r4   r   r   r   _early_logger_flush  s
   

z_WandbSetup._early_logger_flushc                 C  rO   r   )rK   r   r   r   r   _get_logger  s   z_WandbSetup._get_loggerr   c                 C  s$   | j s| jddd | j sJ | j S )zcThe global wandb settings.

        Initializes settings if they have not yet been loaded.
        NF)rz   r|   )rL   r   r   r   r   r   r~     s   
z_WandbSetup.settingsc                 C  rO   )z5The global wandb settings, or None if not yet loaded.)rL   r   r   r   r   settings_if_loaded,  rQ   z_WandbSetup.settings_if_loadedc                 C  s"   | j r	| j jr	d S | jd}|S )NentityrL   _offlineviewerget)r   r   r   r   r   _get_entity1  s   z_WandbSetup._get_entityc                 C  s   | j r	| j jr	d S | jdS )Nusernamer   r   r   r   r   _get_username7  s   z_WandbSetup._get_username	list[str]c                 C  s<   | j r	| j jr	g S | jd}|rdd |d D }|pg S )Nteamsc                 S  s   g | ]}|d  d qS )nodenamer   )ri   teamr   r   r   
<listcomp>A  s    z*_WandbSetup._get_teams.<locals>.<listcomp>edgesr   )r   r   r   r   r   
_get_teams<  s   z_WandbSetup._get_teamsdict[str, Any]c                 C  s"   | j d u rtj| jd| _ | j jS )Nr~   )rI   r   Serverr~   r   r   r   r   r   r   D  s   
z_WandbSetup.viewerdict[str, Any] | Nonec                 C  sN   | j d u rd S | j j}t }d|v r|d |d< | jdd }|r%||d< |S )Ncode_saving_enabled	save_codeemail)rI   _flagsdictr   r   )r   flagsuser_settingsr   r   r   r   _load_user_settingsK  s   
z_WandbSetup._load_user_settingsr   c                 C  sR   | j j}|rtj|dd| _i }| j jr'| j jD ]}t|}|r&|| q|S )NT)
must_exist)r~   sweep_param_pathr   dict_from_config_filerH   config_pathsupdate)r   
sweep_pathconfigconfig_pathconfig_dictr   r   r   r   [  s   

z_WandbSetup.config	exit_code
int | Nonec                 C  sH   t   | jr| j|pd}nd }| j  |dvr"t| d S d S )Nr   )Nr   )r	    unregister_all_post_import_hooksrC   teardownrA   joinsysexit)r   r   internal_exit_coder   r   r   	_teardowno  s   
z_WandbSetup._teardownr   c                 C  s0   | j r| j S ddlm} || j| j| _ | j S )zBReturns a connection to the service process creating it if needed.r   )service_connection)rC   wandb.sdk.lib.servicer   connect_to_servicerA   r~   )r   r   r   r   r   ensure_service|  s   z_WandbSetup.ensure_servicec                 C  s   | j std| j S )zReturns a connection to the service process, asserting it exists.

        Unlike ensure_service(), this will not start up a service process
        if it didn't already exist.
        z"Expected service process to exist.)rC   AssertionErrorr   r   r   r   assert_service  s   z_WandbSetup.assert_service)r>   r?   r   r   )r   rN   )rR   rS   r   r   )r   rY   r7   )r   r`   )rz   r{   r|   r`   ry   r}   r   r   )r~   r}   r   r   r8   )r   r5   )r   r   )r   r}   )r   r{   )r   r   )r   r   )r   r   )r   r   r   r   r   r   r   )r   r   )r9   r:   r;   r<   r   propertyrP   rU   rX   r[   r_   rx   r   r   r   r   r   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r=   Y   s@    





=







r=   z_WandbSetup | None
_singletonr   c                   C  s   t dddS )a  The W&B singleton for the current process.

    The first call to this in this process (which may be a fork of another
    process) creates the singleton, and all subsequent calls return it
    until teardown(). This does not start the service process.
    F)start_serviceload_settings_setupr   r   r   r   	singleton  s   r   Tr~   r}   r   r`   r   c                 C  s   |s| rt d|s|rt dt }t* tr!tj|kr!t}nt|d}|r-||  |r7|jj	s7|
  |aW d   n1 sCw   Y  | rNt|  |S )ap  Set up library context.

    Args:
        settings: Global settings to set, or updates to the global settings
            if the singleton has already been initialized.
        start_service: Whether to start up the service process.
            NOTE: A service process will only be started if allowed by the
            global settings (after the given updates). The service will not
            start up if the mode resolves to "disabled".
        load_settings: Whether to load settings from the environment
            if creating a new singleton. If False, then settings and
            start_service must be None.
    z/Cannot pass settings if load_settings is False.z3Cannot use start_service if load_settings is False.)r>   N)rW   rq   r   _singleton_lockr   rJ   r=   r   r~   _noopr   _maybe_update_credentials)r~   r   r   r>   current_singletonr   r   r   r     s$   

r   r   r   c                 C  s   | j r
| jr
tdddlm} | j r)|j|j|j| j| j	d| j ddd d	S | jrD|j|j
|j| j| j	d| j| jddd d	S d	S )
aM  Update session credentials if they're set on settings.

    This is a refactoring step for moving credentials into a separate module
    and out of settings. If a user calls `wandb.setup()` explicitly with an
    api_key or other credential, this overwrites credentials that might have
    been set by a call to `wandb.login()`.
    zEThe api_key and identity_token_file settings cannot be used together.r   wbauth)app_url)hostapi_keyzwandb.setup())source)r   pathcredentials_fileN)r   identity_token_filer   wandb.sdk.libr   use_explicit_auth
AuthApiKeyHostUrlbase_urlr   AuthIdentityTokenFiler   )r~   r   r   r   r   r     s.   

r   c                 C  s
   t | dS )aH  Prepares W&B for use in the current process and its children.

    You can usually ignore this as it is implicitly called by `wandb.init()`.

    When using wandb in multiple processes, calling `wandb.setup()`
    in the parent process before starting child processes may improve
    performance and resource utilization.

    Note that `wandb.setup()` modifies `os.environ`, and it is important
    that child processes inherit the modified environment variables.

    See also `wandb.teardown()`.

    Args:
        settings: Configuration settings to apply globally. These can be
            overridden by subsequent `wandb.init()` calls.

    Example:
    ```python
    import multiprocessing

    import wandb


    def run_experiment(params):
        with wandb.init(config=params):
            # Run experiment
            pass


    if __name__ == "__main__":
        # Start backend and set global config
        wandb.setup(settings={"project": "my_project"})

        # Define experiment parameters
        experiment_params = [
            {"learning_rate": 0.01, "epochs": 10},
            {"learning_rate": 0.001, "epochs": 20},
        ]

        # Start multiple processes, each running a separate experiment
        processes = []
        for params in experiment_params:
            p = multiprocessing.Process(target=run_experiment, args=(params,))
            p.start()
            processes.append(p)

        # Wait for all processes to complete
        for p in processes:
            p.join()

        # Optional: Explicitly shut down the backend
        wandb.teardown()
    ```
    r   r   r   r   r   r   setup  s   
8r   r   r   c                 C  sX   ddl m} t t}daW d   n1 sw   Y  |r$|j| d |jdd dS )a  Waits for W&B to finish and frees resources.

    Completes any runs that were not explicitly finished
    using `run.finish()` and waits for all data to be uploaded.

    It is recommended to call this at the end of a session
    that used `wandb.setup()`. It is invoked automatically
    in an `atexit` hook, but this is not reliable in certain setups
    such as when using Python's `multiprocessing` module.
    r   r   N)r   F)update_settings)r   r   r   r   r   unauthenticate_session)r   r   orig_singletonr   r   r   r   9  s   r   )r   r=   )NTT)r~   r}   r   r`   r   r`   r   r=   )r~   r   r   r   r   )r~   r}   r   r=   r   ).r<   
__future__r   r%   rq   r   r   rE   typingr   r   r   r   wandb.integration.sagemakerintegrationr   	wandb.envr   wandb.errorsr   r   r   r	   r
   libr   r   	wandb.sdkr   (wandb.sdk.lib.service.service_connectionr   r   r   r   r5   r=   r   __annotations__rF   r   r   log_to_all_runsr   r   r   r   r   r   r   r   <module>   sH    &  >


2#;