
    BVh)"                        d Z ddlZddlmZ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Z	 ddej*                  dededededej*                  fdZdej*                  deeef   dej*                  fdZdedededej*                  deej*                  ej*                  f   f
dZ G d dej6                        Z G d dej6                        Zy)z!Utils for Sparsecore Checkpoints.    N)AnyDict)dtypes)tensor)constant)	array_ops)gen_control_flow_ops)	manip_ops)	variables)base_sparse_core_table_layoutstnum_sparse_coresoffset_in_shardsize_in_shardshard_rotationreturnc                    | j                   }| j                   d   |z  dk7  r(t        dj                  | j                   d   |            t        j                  | || j                   d   |z  | j                   d   f      }|dd|||z   ddf   }|rt        j                  || d      }t        j                  |d      }||z  |d   f}	t        j                  ||	      S )a.  Unshuffles the sparse core sharded embedding tables to unsharded.

  This converts an input tensor respresenting stacked and sharded embedding
  table into a specific embedding table variable by using the provided
  metadata about the said table within the stacked, sharded embedding table.
  Args:
    t: The input stacked and sharded embedding table from sparsecore.
    num_sparse_cores: The number of sparsecores, this determines the number of
      shards that are present in the input t.
    offset_in_shard: Offset within a shard where the queried table starts.
    size_in_shard: size (number of rows) of this queried table within each shard
      of the input t.
    shard_rotation: The rotation of this table's shards.

  Returns:
    An embedding table which is part of the stacked embedding table t.
  r   zGThe dim of table ({}) should be multiple of number of sparse cores ({})   N)axis)r   r      )shape
ValueErrorformatr   reshaper
   roll	transpose)
r   r   r   r   r   	old_shapeshards_tshardsintermediate_tensor	new_shapes
             \/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/tpu/tpu_embedding_v3_utils.pyunshuffle_from_sc_to_cpur$      s    0 gg) WWQZ""a'
	qwwqz#34 
 

''!*(
(
''!*( A=)HH!KL& ^^F^O!<F "++FI>..	!<)			.		::    value_in_checkpointvariable_shapec                     | j                   j                         }t        t        ||      D cg c]
  \  }}||k\   c}}      }|s| S dgt	        |      z  }t        j                  | ||      S c c}}w )aS  Removes padding, if any, from sparsecore checkpoint.

  Args:
    value_in_checkpoint: input tensor value, usually from checkpoint.
    variable_shape: Expected shape of tensor after removing padding.

  Returns:
    A slice of the input tensor to match the variable_shape if the
    variable shape is a valid slice if the input tensor.
  r   )beginsize)r   as_listallziplenr   slice)r&   r'   checkpoint_value_shapeijis_init_value_paddedr)   s          r#   remove_padding_from_scr4   V   s{     /44<<> 4nEF$!QqAvF 
#*+
+%	,E	OO Gs   A1
row_indicesc                 0    || z  |z   | z  }||| z  z   }||fS )a  Maps a row of a given table to its sparse core shard and position.

  Maps a given a row index of a logical table and its layout in sparse core,
  returns the index of the shard where the row is placed and its relative
  position within
  that sparse core shard.
  Args:
    num_sparse_cores: The number of sparsecores, this determines the number of
      shards present.
    offset_in_shard: Offset within a shard where the queried table starts.
    shard_rotation: The rotation of this table's shards.
    row_indices: row indices of the embedding table being looked up.

  Returns:
    A Tuple representing shard_index and position of the row in that shard.
   )r   r   r   r5   shard_indexposition_in_shards         r#   map_indices_in_shardr:   p   s;    . %%7+ &7G(GG
(	))r%   c                       e Zd ZdZdej
                  fdZdeeej
                  f   fdZ	deeej
                  f   ddfdZ
y)	SparseCoreLayoutsTrackablez2Trackable for sparsecore layouts used in training.proto_str_tensorc                     || _         y N)value)selfr=   s     r#   __init__z#SparseCoreLayoutsTrackable.__init__   s	    !DJr%   r   c                 :    t         j                  | j                  iS r?   )trackable_baseVARIABLE_VALUE_KEYr@   rA   s    r#   _serialize_to_tensorsz0SparseCoreLayoutsTrackable._serialize_to_tensors   s    --tzz::r%   restored_tensorsNc                 ,    t        j                          y r?   )r	   no_op)rA   rH   s     r#   _restore_from_tensorsz0SparseCoreLayoutsTrackable._restore_from_tensors   s    
  r%   )__name__
__module____qualname____doc__r   TensorrB   r   strrG   rK   r7   r%   r#   r<   r<      sR    :"v}} ";T#v}}*<%= ;!"3#56!!r%   r<   c                       e Zd ZdZd Zed        ZdefdZde	e
ej                  f   fdZde
dej                  fd	Zde	e
ej                  f   fd
Zd Zy)SparseCoreStackedTableTrackablez8Trackable for stacked tables generated from sparse core.c           	      ^   i | _         || _        |D ]  }t        |j                        }t	        j
                  |j                  t        j                  ||j                     j                  |t        j                        |t        j                        | j                   |j                  <    y )Ndtype)nameinitial_valuer   rV   )vars_stacked_layoutstupleunsharded_shapetf_variablesVariable
table_name	functoolspartialinitializerr   float32)rA   stacked_layoutstable_to_configtable_layoutr'   s        r#   rB   z(SparseCoreStackedTableTrackable.__init__   s    DI+D' \99:n+7+@+@&&!))l556BBNN
 	,dii''(r%   c                 b    | j                   | j                  d   j                     j                  S )Nr   )rY   rZ   r_   
_unique_idrF   s    r#   rh   z*SparseCoreStackedTableTrackable._unique_id   s(    99T**1-889DDDr%   r   c                 X    t         j                  t        dt        j                        iS )Ng        rU   )rD   rE   tf_constantr   rc   rF   s    r#   rG   z5SparseCoreStackedTableTrackable._serialize_to_tensors   s'     	));v~~,
 r%   rH   c                       fd} ||      S )Nc                    | t         j                     }j                  D ]  }|j                  d   |j                  d   f}t	        ||j
                  |j                  |j                  d   |j
                  z  |j                        }t        ||      }j                  |j                     j                  |        y )Nr   r   )r   r   r   r   r   )rD   rE   rZ   r\   r$   r   sparse_core_shard_row_offsetunsharded_padded_shapesparse_core_shard_rotationr4   rY   r_   assign)rH   value_from_checkpointlayoutr'   t_partrA   s        r#   fnzASparseCoreStackedTableTrackable._restore_from_tensors.<locals>.fn   s    .

+
+
 )) 4&""1%""1%
 *##44"??--a0F4K4KK!<<
 (?		&##$++F34r%   r7   )rA   rH   rt   s   `  r#   rK   z5SparseCoreStackedTableTrackable._restore_from_tensors   s    4. r%   rW   c                      | j                   |   S r?   rY   )rA   rW   s     r#   get_varz'SparseCoreStackedTableTrackable.get_var   s    99T?r%   c                     | j                   S r?   rv   rF   s    r#   get_varsz(SparseCoreStackedTableTrackable.get_vars   s    99r%   c                 T    dj                  | j                  j                               S )Nz#SparseCoreStackedTableTrackable({}))r   rY   keysrF   s    r#   __repr__z(SparseCoreStackedTableTrackable.__repr__   s    077		8HIIr%   N)rL   rM   rN   rO   rB   propertyrh   r   rG   r   rQ   r   rP   rK   r]   r^   rw   ry   r|   r7   r%   r#   rS   rS      s    @$ E ES  Dfmm9K4L  4# ,"7"7 S,"7"778 Jr%   rS   )r   )rO   r`   typingr   r   tensorflow.python.frameworkr   r   'tensorflow.python.framework.constant_opr   rj   tensorflow.python.opsr   r	   r
   r   r]   tensorflow.python.trackabler   rD   !SPARSECORE_LAYOUTS_CHECKPOINT_KEYrP   intr$   r[   r4   r:   	Trackabler<   rS   r7   r%   r#   <module>r      s   (   . . K + 6 + ; >$@ ! 4;}}4;4; 4; 	4;
 4; ]]4;nPP8=c3hP]]P4*** * 	*
 6==&--'(*:!!9!9 !"CJn&>&> CJr%   