o
    Me                     @   sX   d Z ddlm  mZ ddlmZ ddlmZ ej	Z	dd Z
dd Zd	d
 Zdd ZdS )z*Utilities related to distributed training.    N)flags)backendc                 C   s   | j jS )N)extendedZ_global_batch_size)Zdistribution_strategy r   [D:\Projects\ConvertPro\env\Lib\site-packages\keras/distribute/distributed_training_utils.pyglobal_batch_size_supported   s   r   c                 O   s   d}d|v r| d}n
tj rtj }t|}|s@|r@tj r@|  |j	
| ||W  d   S 1 s;w   Y  | |i |S )a=  Call a function that uses replica-local variables.

    This function correctly handles calling `fn` in a cross-replica
    context.

    Args:
      fn: The function to call.
      *args: Positional arguments to the `fn`.
      **kwargs: Keyword argument to `fn`.

    Returns:
      The result of calling `fn`.
    Nstrategy)poptf
distributeZhas_strategyget_strategyr   Zis_tpu_strategyZin_cross_replica_contextscoper   Zcall_for_each_replica)fnargskwargsr   Zis_tpur   r   r   call_replica_local_fn    s   



 r   c                 C   s   t | tjjot | tjS )z.Returns whether `v` is a distributed variable.)
isinstancer
   r   ZDistributedValuesVariable)vr   r   r   is_distributed_variable?   s   r   c                  C   s   t j} h d}| dkr2t j}|stdtjjj|d}tj	| tj
j| tjj|}|S | dkr>tjj }|S | dkrItj }|S | dkr\tjj }tjj|}|S | dkrhtjd	}|S td
|  d| )a  Creates a `tf.distribute.Strategy` object from flags.

    Example usage:

    ```python
    strategy = utils.get_strategy()
    with strategy.scope():
      model = tf.keras.Sequential([tf.keras.layers.Dense(10)])

    model.compile(...)
    train_ds, test_ds = ...
    model.fit(train_ds, validation_data=test_ds, epochs=10)
    ```

    Returns:
      `tf.distribute.Strategy` instance.
    >   multi_worker_mirrored
one_devicetpuparameter_servermirroredr   zdWhen using a TPU strategy, you must set the flag `keras_distribute_strategy_tpu_addr` (TPU address).)r   r   r   r   r   z/gpu:0zNUnknown distribution strategy flag. Received: keras_distribute_strategy_class=z. It should be one of )FLAGSZkeras_distribute_strategy_classZ"keras_distribute_strategy_tpu_addr
ValueErrorr
   r   cluster_resolverZTPUClusterResolverconfigZexperimental_connect_to_clusterr   ZexperimentalZinitialize_tpu_systemZTPUStrategyZMultiWorkerMirroredStrategyZMirroredStrategyZTFConfigClusterResolverZParameterServerStrategyZOneDeviceStrategy)clsZaccepted_stratsZtpu_addrr   r   r   r   r   r   F   sL   

r   )__doc__Ztensorflow.compat.v2compatv2r
   Zabslr   Zkerasr   r   r   r   r   r   r   r   r   r   <module>   s   