
    BVh+                     2    d Z ddlmZ dZdZ G d de      Zy)z3Helper library for sharding during TPU compilation.    )tensor_shape   c                       e Zd ZdZd Zd Zd Zd Zed        Z	d Z
ed        Zd	 Zed
        Zd Zd Zd ZddZd Zd Zy)ShardingPolicyz7An object use to hold the sharding policy for a Tensor.c                 <    d | _         d| _        d | _        d| _        y )Nr   F)_number_of_shards_number_of_partitions_shard_dimension_frozenselfs    R/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/tpu/tpu_sharding.py__init__zShardingPolicy.__init__   s!    !D!"D DDL    c                 j    | j                   | j                  yd| j                   | j                  fz  S )NzShardingPolicy(unset)z&ShardingPolicy(%d shards dimension %d))number_of_shardsshard_dimensionr   s    r   __str__zShardingPolicy.__str__"   s=    $(<(<(D$6$$d&:&:;< =r   c                     | j                   t        | _         | j                  t        j                  t
              | _        y y N)r   _DEFAULT_NUMBER_OF_SHARDSr
   r   as_dimension_DEFAULT_SHARD_DIMENSIONr   s    r   _fill_default_valuesz#ShardingPolicy._fill_default_values)   s=    %8d$*77
"$d %r   c                 L    | j                   s| j                          d| _         yy)zPrevents further modification to the sharding policy.

    Any values that have not been set when freeze is called are set to
    defaults. If the ShardingPolicy is already frozen, this is a NoOp.
    TN)r   r   r   s    r   freezezShardingPolicy.freeze0   s#     <<
!dl r   c                     | j                   S )zBReturns the number of shards in the policy or None if unspecified.)r   r   s    r   r   zShardingPolicy.number_of_shards:   s     !!!r   c                     | j                   r+| j                  |k7  rt        d| d| j                         y|dkD  r|| _        yt        d| d      )au  Sets the number of shards for the current policy.

    If the policy has been frozen then number_of_shards must match the
    existing setting.

    Args:
      number_of_shards: The number of shards to use in the policy.

    Raises:
      ValueError: If the policy has been frozen and number_of_shards
        differs from the frozen value; or number_of_shards <= 0.
    z!Can't set sharding policy to use z( shards since it has been frozen to use r   z shards; value must be > 0N)r   r   
ValueError)r   r   s     r   set_number_of_shardsz#ShardingPolicy.set_number_of_shards?   s     ||			#3	3/0@/A B))-)?)?(@BC 	C 
4
 
A	!1/0@/A B   ! 	!r   c                     | j                   S )zFReturns the number of partitions of the policy or None if unspecified.)r	   r   s    r   number_of_partitionsz#ShardingPolicy.number_of_partitionsY   s     %%%r   c                     | j                   r,| j                  |k7  rt        d| d| j                   d      y|| _        y)ae  Sets the number of partitions for the current policy.

    If the policy has been frozen then shard_dimension must match the
    existing setting.

    Args:
      number_of_partitions: The number of partitions to use in the policy.

    Raises:
      ValueError: If the policy has been frozen and shard_dimension
        differs from the frozen value.
    z"Can't set number_of_partitions to z! since it has been frozen to use .N)r   r	   r   )r   r"   s     r   set_number_of_partitionsz'ShardingPolicy.set_number_of_partitions^   s[     ||		#	#';	;01E0F G))-)C)C(DAGH 	H 
<
 $8d r   c                     | j                   S )zAReturns the shard dimension of the policy or None if unspecified.)r
   r   s    r   r   zShardingPolicy.shard_dimensions   s        r   c                     | j                   r*| j                  |k7  rt        d|| j                  fz        yt        j                  |      | _        y)a  Sets the shard dimension for the current policy.

    If the policy has been frozen then shard_dimension must match the
    existing setting.

    Args:
      shard_dimension: The shard dimension to use in the policy.

    Raises:
      ValueError: If the policy has been frozen and shard_dimension
        differs from the frozen value, or shard_dimension can't be
        interpreted as a Dimension.
    zCCan't set shard dimension to %d since it has been frozen to use %d.N)r   r
   r   r   r   )r   r   s     r   set_shard_dimensionz"ShardingPolicy.set_shard_dimensionx   s[     ||			/	1($*?*?@AB 	B 
2
 +77Hdr   c                     |j                   | j                  |j                          |j                  | j                  |j                         yy)zMerges the policy of another policy into the current policy.

    Args:
      other: The policy to merge into this one.

    Raises:
      ValueError: If this policy has been frozen and the merge conflicts with
      the frozen policy.
    N)r   r    r   r(   )r   others     r   mergezShardingPolicy.merge   sI     )
 6 67(
u445 )r   c                 ~   t        j                  |      }|j                         }| j                  | j                  |sy|| j                     *t        d|j                          d| j                   d      | j                  dkD  r!|| j                  xx   | j                  z  cc<   t        j                  |      S )av  Returns the shape of an unpartitioned Tensor.

    When given the shape of a 'sharded-size' Tensor, returns the shape
    of the full shape of its unpartitioned Tensor.

    Args:
      shape: The shape of the sharded Tensor.

    Returns:
      The shape of the unpartitioned version of the Tensor.

    Raises:
      ValueError: if shape has unknown sharded dimension
    NShape & must have a fixed size for dimension z that is known. r   )r   as_shapeas_listr
   r	   r   )r   shapedimss      r   get_unpartitioned_shapez&ShardingPolicy.get_unpartitioned_shape   s     !!%(E==?D%)C)C)KD!!"*0 1$$($9$9#::JL M M!!A%
4  !T%?%??!  &&r   Nc                 6   | j                   | j                  y|0|dk  s|| j                  k\  rt        d| d| j                   d      t	        j
                  |      }| j                  dk(  r|S |j                  }|t        d| d      || j                   k  r)t        d|j                          d	| j                          |j                         }|| j                      *t        d|j                          d
| j                    d      || j                      | j                  z  dk7  r6t        d|j                          d| j                   d| j                          || j                   xx   | j                  z  cc<   t	        j                  |      S )ay  Returns the shape of a shard of a full Tensor.

    When given the shape of a 'full-size' Tensor, returns the shape of
    the sub-Tensor after it has been sharded. Freezes the policy if it
    has not yet been frozen.

    Args:
      shape: The shape of the full-size Tensor to be sharded.
      shard_index: The index of the shard whose shape should be returned.
        shard_index can be None for sharding policies that use the same shape
        for every shard.

    Returns:
      The shape of the sharded version of the Tensor.

    Raises:
      ValueError: If shard_index is None when shards are of different
        shapes; or shard_index is not None and
        !(0<=shard_index<number_of_shards); or shape does not have at
        least self.shard_dimension+1 dimensions; or the value of
        shape's shard dimension is not a multiple of
        self.number_of_shards
    Nr   zRequested shard_index z , but shard_index must be in [0,z).r   r-   z must be a known shape." does not contain shard_dimension r.   z$ that is known at construction time.z cannot be sharded  ways along dimension )	r
   r   r   r   r   r/   ndimsr0   TensorShape)r   r1   shard_indexr7   r2   s        r   get_sharded_shapez ShardingPolicy.get_sharded_shape   s   0 $(>(>(F	qK4+@+@@$[M 2(()-. 	. !!%(E"lKKE}w&=>??%%%5==?##E""#%& & ==?DD!!"*5==?##I""##GIJ J 	T""#d&<&<<B5==?##6t7M7M6N O""&"7"7!8:; ; 			D$:$::##D))r   c                    t        j                  |      }| j                  dk(  r|S |j                  }|t	        d| d      || j
                  k  r*t	        d|j                          d| j
                   d      |j                         }|| j
                  xx   | j                  z  cc<   t        j                  |      S )aJ  Return the unsharded shape that would generate a given sharded shape.

    Args:
      shape: the sharded shape to unshard

    Returns:
      The unsharded shape.

    Raises:
      ValueError: if shape is unknown or does not contain
        self.shard_dimension
      TypeError: if shape is not convertible to a TensorShape
    r   r-   z must be statically known.r5   z. Rank is too small.)r   r/   r   r7   r   r
   r0   r8   )r   r1   r7   r2   s       r   _unshard_shapezShardingPolicy._unshard_shape   s     !!%(E"lKKE}w&@ABB%%%0 1**.*?*?)@ A,- . . ==?D		4#9#99##D))r   c           
         | j                          t        |      | j                  k7  r't        d| dt        |       d| j                         |D cg c]  }| j	                  |       }}t        | j                  dz
        D ]O  }||   j                  || j                  dz
           r(t        d| d| j                   d| j                   d       |d	   S c c}w )
a  Returns the shape of an unsharded Tensor given a list of shards.

    When given a list of shapes of shards, returns the shape of the
    unsharded Tensor that would generate the shards. Sets defaults for the
    policy if number_of_shards or shard_dimension is None.

    Args:
      shapes: The shapes of the Tensor shards to be combined.

    Returns:
      The shape of the unsharded version of the Tensor.

    Raises:
      ValueError: if shapes is not a list of length
        self.number_of_shards; or any element of shapes is not a valid
        shape consistent with the sharding policy; or the list of
        shapes is not a valid sharding of a full shape.
      TypeError: if an element of shapes is not convertible to a
        TensorShape
    zShapes z is length z/ but must be a list of length number_of_shards=r   zSharded shapes z3 are not consistent shards of a full shape sharded r6   r$   r   )r   lenr   r   r<   rangeis_compatible_withr   )r   shapessunsharded_shapesis        r   get_unsharded_shapez"ShardingPolicy.get_unsharded_shape  s   * 	
6{d+++F8;s6{m 4%%)%:%:$;=> > 9??1++A.??4((1,- 2a 33
40014
57fX &,,- .--.a12 	22 A @s   Cr   )__name__
__module____qualname____doc__r   r   r   r   propertyr   r    r"   r%   r   r(   r+   r3   r:   r<   rE    r   r   r   r      s    ?=$ " "!4 & &8* ! !I,6'65*n*:"r   r   N)rI   tensorflow.python.frameworkr   r   r   objectr   rK   r   r   <module>rN      s)    : 5  UV Ur   