a
    Sic7                     @   s~   d Z ddlZddlZddlm  mZ ddiZdZdd Z	dd	 Z
d
d Zdd Zdd Zdd Zdd Zdd Zdd ZdS )a  Utilities that help manage directory path in distributed settings.

In multi-worker training, the need to write a file to distributed file
location often requires only one copy done by one worker despite many workers
that are involved in training. The option to only perform saving by chief is
not feasible for a couple of reasons: 1) Chief and workers may each contain
a client that runs the same piece of code and it's preferred not to make
any distinction between the code run by chief and other workers, and 2)
saving of model or model's related information may require SyncOnRead
variables to be read, which needs the cooperation of all workers to perform
all-reduce.

This set of utility is used so that only one copy is written to the needed
directory, by supplying a temporary write directory path for workers that don't
need to save, and removing the temporary directory once file writing is done.

Example usage:
```
# Before using a directory to write file to.
self.log_write_dir = write_dirpath(self.log_dir, get_distribution_strategy())
# Now `self.log_write_dir` can be safely used to write file to.

...

# After the file is written to the directory.
remove_temp_dirpath(self.log_dir, get_distribution_strategy())

```

Experimental. API is subject to change.
    NzMetadata-FlavorGoogleGCE_METADATA_IPc                 C   s   | j j}dt| S )Nworkertemp_)extended_task_idstr)strategytask_id r
   c/var/www/html/django/DPS/env/lib/python3.9/site-packages/keras/distribute/distributed_file_utils.py_get_base_dirpath8   s    r   c                 C   s   |  t|S N)endswithr   dirpathr   r
   r
   r   _is_temp_dir=   s    r   c                 C   s4   t | |r| }ntj| t|}tjj| |S r   )	r   ospathjoinr   tfiogfilemakedirs)r   r   temp_dirr
   r
   r   _get_temp_dirA   s
    
r   c                 C   sB   |du rt j }|du r| S |j s,| S |jjr8| S t| |S )ao  Returns the writing dir that should be used to save file distributedly.

    `dirpath` would be created if it doesn't exist.

    Args:
      dirpath: Original dirpath that would be used without distribution.
      strategy: The tf.distribute strategy object currently used.

    Returns:
      The writing dir path that should be used to save with distribution.
    N)r   
distributeget_strategyr   _in_multi_worker_modeshould_checkpointr   r   r
   r
   r   write_dirpathJ   s    

r   c                 C   sJ   |du rt j }|du rdS |j rF|jjsFt jjj	t
| | dS )zRemoves the temp path after writing is finished.

    Args:
      dirpath: Original dirpath that would be used without distribution.
      strategy: The tf.distribute strategy object currently used.
    N)r   r   r   r   r   r   compatv1r   DeleteRecursivelyr   r   r
   r
   r   remove_temp_dirpathf   s    
r#   c                 C   s,   t j| }t j| }t jt|||S )a  Returns the writing file path to be used to save file distributedly.

    Directory to contain `filepath` would be created if it doesn't exist.

    Args:
      filepath: Original filepath that would be used without distribution.
      strategy: The tf.distribute strategy object currently used.

    Returns:
      The writing filepath that should be used to save file with distribution.
    )r   r   dirnamebasenamer   r   )filepathr   r   baser
   r
   r   write_filepath   s    r(   c                 C   s   t tj| | dS )zRemoves the temp path for file after writing is finished.

    Args:
      filepath: Original filepath that would be used without distribution.
      strategy: The tf.distribute strategy object currently used.
    N)r#   r   r   r$   )r&   r   r
   r
   r   remove_temp_dir_with_filepath   s    r)   c                  C   sP   dt jtd } z tjd| df tdd}|jW S  tjjyJ   Y dS 0 dS )	z9Detect whether the current running environment is on GCP.zhttp://zmetadata.google.internalz%s/computeMetadata/v1/%szinstance/hostname   )headerstimeoutFN)	r   environget_GCE_METADATA_URL_ENV_VARIABLErequestsGCP_METADATA_HEADERstatus_code
exceptionsRequestException)gce_metadata_endpointresponser
   r
   r   _on_gcp   s    r7   c                   C   s   t  rtjdsdS dS )NTPUTF)r7   r   configlist_physical_devicesr
   r
   r
   r   %support_on_demand_checkpoint_callback   s    r;   )__doc__r   r0   tensorflow.compat.v2r    v2r   r1   r/   r   r   r   r   r#   r(   r)   r7   r;   r
   r
   r
   r   <module>   s    	
