o
    ?e+                     @   s,   d Z ddlmZ dZdZG dd deZdS )z3Helper library for sharding during TPU compilation.    )tensor_shape   c                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zed
d Z	dd Z
edd Zdd Zedd Zdd Zdd Zdd Zd!ddZdd Zdd  ZdS )"ShardingPolicyz7An object use to hold the sharding policy for a Tensor.c                 C   s   d | _ d| _d | _d| _d S )Nr   F)_number_of_shards_number_of_partitions_shard_dimension_frozenself r   c/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/tensorflow/python/tpu/tpu_sharding.py__init__   s   
zShardingPolicy.__init__c                 C   s(   | j d u s
| jd u rdS d| j | jf S )NzShardingPolicy(unset)z&ShardingPolicy(%d shards dimension %d))number_of_shardsshard_dimensionr	   r   r   r   __str__"   s
   
zShardingPolicy.__str__c                 C   s.   | j d u rt| _ | jd u rtt| _d S d S N)r   _DEFAULT_NUMBER_OF_SHARDSr   r   as_dimension_DEFAULT_SHARD_DIMENSIONr	   r   r   r   _fill_default_values)   s   


z#ShardingPolicy._fill_default_valuesc                 C   s   | j s|   d| _ dS dS )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	   r   r   r   freeze0   s   
zShardingPolicy.freezec                 C      | j S )zBReturns the number of shards in the policy or None if unspecified.)r   r	   r   r   r   r   :      zShardingPolicy.number_of_shardsc                 C   sL   | j r| j|krtd| d| j dS |dkr|| _dS 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   r   r   r   set_number_of_shards?   s   


z#ShardingPolicy.set_number_of_shardsc                 C   r   )zFReturns the number of partitions of the policy or None if unspecified.)r   r	   r   r   r   number_of_partitionsY   r   z#ShardingPolicy.number_of_partitionsc                 C   s6   | j r| j|krtd| d| j ddS || _dS )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   r   r   r   set_number_of_partitions^   s   

z'ShardingPolicy.set_number_of_partitionsc                 C   r   )zAReturns the shard dimension of the policy or None if unspecified.)r   r	   r   r   r   r   s   r   zShardingPolicy.shard_dimensionc                 C   s6   | j r| j|krtd|| jf dS t|| _dS )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   r   r   r   set_shard_dimensionx   s   
z"ShardingPolicy.set_shard_dimensionc                 C   s4   |j dur| |j  |jdur| |j dS dS )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
   otherr   r   r   merge   s
   


zShardingPolicy.mergec                 C   s   t |}| }| jdu s| jdu s|sdS || j du r,td|  d| j d| jdkr;|| j  | j9  < t |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dimsr   r   r   get_unpartitioned_shape   s   



z&ShardingPolicy.get_unpartitioned_shapeNc                 C   s*  | j du s
| jdu rdS |dur%|dk s|| jkr%td| d| j dt|}| jdkr1|S |j}|du r@td| d|| j krRtd|  d	| j  | }|| j  du rktd|  d
| j  d|| j  | j dkrtd|  d| j d| j  || j   | j  < t|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#   ndimsr$   TensorShape)r
   r%   Zshard_indexr*   r&   r   r   r   get_sharded_shape   sH   



z ShardingPolicy.get_sharded_shapec                 C   s   t |}| jdkr|S |j}|du rtd| d|| jkr.td|  d| j d| }|| j  | j9  < t |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   Nr!   z must be statically known.r(   z. Rank is too small.)r   r#   r   r*   r   r   r$   r+   )r
   r%   r*   r&   r   r   r   _unshard_shape   s   




zShardingPolicy._unshard_shapec              	      s       t| jkrtd| dt| d j  fdd|D }t jd D ]}|| | jd  sItd| d j d	 j d
q+|d S )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=c                    s   g | ]}  |qS r   )r-   ).0sr	   r   r   
<listcomp>&  s    z6ShardingPolicy.get_unsharded_shape.<locals>.<listcomp>r   zSharded shapes z3 are not consistent shards of a full shape sharded r)   r   r   )r   lenr   r   rangeZis_compatible_withr   )r
   ZshapesZunsharded_shapesir   r	   r   get_unsharded_shape  s*   z"ShardingPolicy.get_unsharded_shaper   )__name__
__module____qualname____doc__r   r   r   r   propertyr   r   r   r   r   r   r    r'   r,   r-   r4   r   r   r   r   r      s(    




7r   N)r8   Ztensorflow.python.frameworkr   r   r   objectr   r   r   r   r   <module>   s
   