
    BVhU)                         d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm	Z	 ddlm
Z
 dd	lmZ dd
lmZ ddlmZ dddZ	 ddddZ	 ddddZ	 	 ddddZy)a  Standard functions for creating slots.

A slot is a `Variable` created with the same first m-dimension as a primary
variable or `Tensor`. A slot is always scoped in the namespace of the primary
object and typically has the same device and type.

Slots are typically used as accumulators to track values associated with
the primary object:

```python
# Optimizers can create a slot for each variable to track accumulators
accumulators = {var : create_zeros_slot(var, "momentum") for var in vs}
for var in vs:
  apply_momentum(var, accumulators[var], lr, grad, momentum_tensor)

# Slots can also be used for moving averages
mavg = create_slot(var, var.initialized_value(), "exponential_moving_avg")
update_mavg = mavg.assign_sub((mavg - var) * (1 - decay))
```
    )xla_sharding)distribute_lib)	array_ops)cond)init_ops)ref_variable)resource_variable_ops)variable_scope)variable_v1)	variablesFcopy_xla_shardingc          
         t        j                         j                  }t        j                         j                  d       t	        |      r|nd}t        j                  |       rd}nt        | t        j                        rd}nd}t        j                  ||d||||      }	t        j                         j                  |       t        | t        j                        r| j                  r|	j                  t        | j                   j                  dz         d }
| j                  }|	j"                  j$                  }||dkD  rh|	j'                  t        j                  j)                  |j*                  dz   |
z   |j,                  d| |j.                  d| |j0                  d|              d }|r5 || j"                  |	j"                        rt3        j4                  | |	d	      }	|	S )
z-Helper function for creating a slot variable.NTF)initializer	trainableuse_resourceshapedtypevalidate_shape/r   c                 t    | j                   d uxr) |j                   d uxr | j                   |j                   k(  S N)rank)primary_shape
slot_shapes     W/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/training/slot_creator.py_has_same_rankz(_create_slot_var.<locals>._has_same_ranko   s<    d* 2zd/J 2*//13    )use_sharding_op)r
   get_variable_scopepartitionerset_partitionercallabler	   is_resource_variable
isinstancer   RefVariableget_variabler   Variable_save_slice_infonamelenopr   ndims_set_save_slice_infoSaveSliceInfo	full_name
full_shape
var_offset	var_shaper   copy_sharding)primaryvalscoper   r   r   r   current_partitionerr   slotreal_slot_name
slice_infonr   s                 r   _create_slot_varr>   2   s    '99;GG##%55d; C=%d%//8L'<334LL		$	$#
%$ ##%556IJ ++,1I1I YYs7::??S#89"=N))J 	

AyAE




*
*""S(>9##BQ')>)>r)B""2A&()3 >'--D%%gtUKD	+r   c                   |j                         j                         }t        | t        j                        r| j
                  }n| j                  j                  }t        j                  d|dz   |z         5  |rTt        j                         }|j                  j                  |       5  t        | |d|dd|      cddd       cddd       S t        | |d|dd|      cddd       S # 1 sw Y   nxY w	 ddd       y# 1 sw Y   yxY w)a  Create a slot initialized to the given value.

  The type of the slot is determined by the given value.

  Args:
    primary: The primary `Variable` or `Tensor`.
    val: A `Tensor` specifying the initial value of the slot.
    name: Name to use for the slot variable.
    colocate_with_primary: Boolean.  If True the slot is located
      on the same device as `primary`.
    copy_xla_sharding: Boolean. If True also copies XLA sharding
      from primary.

  Returns:
    A `Variable` object.
  Nr    r   )	get_shapeis_fully_definedr&   r   r)   _shared_namer-   r+   r
   r   get_strategyextendedcolocate_vars_withr>   )r6   r7   r+   colocate_with_primaryr   r   prefixdistribution_strategys           r   create_slotrJ   x   s   6 ==?335.++,!!FZZ__F$$T6C<$+>? /,99; ))<<WE 1/11 1/ / 





-// /1 1 1/ / /s*   82D *C*<	D D *C3	/D  D	c                   |j                         }t        | t        j                        r| j                  }n| j
                  j                  }t        j                  d|dz   |z         5  |rTt        j                         }	|	j                  j                  |       5  t        | |d||||      cddd       cddd       S t        | |d||||      cddd       S # 1 sw Y   nxY w	 ddd       y# 1 sw Y   yxY w)aX  Creates a slot initialized using an `Initializer`.

  The type of the slot is determined by the given value.

  Args:
    primary: The primary `Variable` or `Tensor`.
    initializer: An `Initializer`.  The initial value of the slot.
    shape: Shape of the initial value of the slot.
    dtype: Type of the value of the slot.
    name: Name to use for the slot variable.
    colocate_with_primary: Boolean.  If True the slot is located
      on the same device as `primary`.
    copy_xla_sharding: Boolean. If True also copies XLA sharding
      from primary.

  Returns:
    A `Variable` object.
  Nr   r@   r   )rB   r&   r   r)   rC   r-   r+   r
   r   rD   rE   rF   r>   )
r6   r   r   r   r+   rG   r   r   rH   rI   s
             r   create_slot_with_initializerrL      s    > ))+.++,!!FZZ__F$$T6C<$+>? /,99; ))<<WE 1/11 1/ / 





-// /1 1 1/ / /s*   *2C2C.	C2C2C%	!C22C;Nc          	          | j                   } j                         }|j                         r&t        j                         }t         ||||||      S t         t        j                        rKt        j                  t        j                  t        j                          j                   fd            }nt        j                         }t        j                  ||      }t!         ||||      S )a  Create a slot initialized to 0 with same shape as the primary object.

  Args:
    primary: The primary `Variable` or `Tensor`.
    name: Name to use for the slot variable.
    dtype: Type of the slot variable.  Defaults to the type of `primary`.
    colocate_with_primary: Boolean.  If True the slot is located
      on the same device as `primary`.
    copy_xla_sharding: Boolean. If True also copies XLA sharding
      from primary.

  Returns:
    A `Variable` object.
  )rG   r   c                       j                   S r   )initial_value)r6   s   r   <lambda>z#create_zeros_slot.<locals>.<lambda>  s    g++ r   )r   )r   rA   rB   r   zeros_initializerrL   r&   r   r)   r   r   r   r   is_variable_initialized
read_valuezerosrJ   )r6   r+   r   rG   r   r   r   r7   s   `       r   create_zeros_slotrU      s    ( ]MME  "*  ",,.K'3+- - '9--.??
))11':G<N<N+-.j
 ??7+j
//*E
2C3+- -r   )T)NT)__doc__+tensorflow.python.compiler.xla.experimentalr   tensorflow.python.distributer   tensorflow.python.opsr   r   r   r   r	   r
   r   r   r>   rJ   rL   rU    r   r   <module>r[      sx    , E 7 + & * . 7 0 - + (-CR '+4/
 #(4/x 8<8/ 498/z !,0/-
 )./-r   