
    AVhAY                         d Z ddlmZ 	 	 	 	 	 ddZd Z	 	 	 	 	 	 	 ddZ	 	 ddZ	 	 	 	 ddZ	 	 dd	Z	 	 dd
Z		 	 ddZ
	 	 ddZ	 	 ddZ	 	 	 ddZ	 	 	 	 ddZddZy)zTensorFlow collective Ops.    )gen_collective_opsc	                     |dk  rt        d| d      t        j                  | |||||||j                         |	      S )a  Reduces tensors collectively, across devices.

  Args:
    t: the tensor to be reduced.
    group_size: the total number of tensors to be collectively reduced.
      Each must reside on a different device.  Should be a positive integer.
    group_key: an integer identifying the group of devices.
    instance_key: an integer identifying the participating group of Ops.
    merge_op: string naming the binary Op to be applied to compute each
      partial reduction.
    final_op: string naming the unary Op to be applied to each fully
      reduced value.  Can be 'Id' for no operation.
    subdiv_offsets: a list of integer offsets into the tensor at which each
      independent subdivision should begin.  Use [0] if no subdivision should
      be done.
    communication_hint: preferred collective communication.  The implementation
      may fall back to another mechanism.  Options include `auto`, `ring`, and
      `nccl`.
    timeout: a float. If set to a non zero, set a completion timeout to detect
      staleness.  If the timer goes off, a DeadlineExceededError is raised.  The
      timeout value in seconds. This feature is experimental.

  Returns:
    An Op implementing the distributed reduction.

  Raises:
    ValueError: if any of the input parameter constraints are not met.
     zCParameter `group_size` to all_reduce must be at least 1. Received: .)
group_size	group_keyinstance_keymerge_opfinal_opsubdiv_offsetscommunication_hinttimeout_seconds)
ValueErrorr   collective_reducelower)	tr   r   r	   r
   r   r   r   timeouts	            T/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/python/ops/collective_ops.py
all_reducer      sa    J !^
 "",Q0 1 1		-	-#+113	
 	    c                 @    t        j                  | ||      \  }}||fS )a  Assign group key based on group_assignment.

  Args:
    group_assignment: a 2 dimensional integer Tensor that encodes which devices
      belong to the same group. The values are indices of the devices within 0
      to number of devices.
    device_index: integer for the index of the current device
    base_key: integer to offset the resulted group_key. The base key shall be
      unique for different values of group_assignment in the same tf.function.
  Notes: The device_index argument must be consistent with the index of the
    device of this Op in the device assignment list. The behavior of this Op is
    undefined if they are inconsistent.

  Returns:
    group_size, group_key: The group size and group key for the current device.
  )group_assignmentdevice_indexbase_key)r   collective_assign_group_v2)r   r   r   r   r   s        r   assign_group_v2r   G   s/    " -GG'*i 
Y	r   Nc                 p    ||g}ng }t        j                  | ||||||j                         |d||	|
      S )a   Reduces tensors collectively, across devices.

  Args:
    t: the tensor to be reduced.
    group_size: an int32 tensor. The total number of tensors to be collectively
      reduced.  Each must reside on a different device.  Should be a positive
      integer.
    group_key: an int32 tensor identifying the group of devices.
    instance_key: an int32 tensor identifying the participating group of Ops.
    merge_op: string naming the binary Op to be applied to compute each partial
      reduction.
    final_op: string naming the unary Op to be applied to each fully reduced
      value.  Can be 'Id' for no operation.
    communication_hint: preferred collective communication.  The implementation
      may fall back to another mechanism.  Options include `auto`, `ring`, and
      `nccl`.
    timeout: a float. If set to a non zero, set a completion timeout to detect
      staleness.  If the timer goes off, a DeadlineExceededError is raised.  The
      timeout value in seconds. This feature is experimental.
    ordering_token: a resource tensor on the same device as the op to order
      the collectives in a per-device manner by auto control dependency.
      This argument can be omited when there is one collective Op per
      `tf.function`, or when explicit control dependency is used instead of
      auto control dependency.
    max_subdivs_per_device: int specifying the maximum number of subdivisions a
      tensor on a device can be divided into. The runtime uses this contraint to
      parallelize processing of each per-device tensor. Setting to -1 disables
      subdivision and reverts to previous behavior of not sub-dividing tensor.
      Setting to 0 uses sytem defaults.
    name: name of the Op.

  Returns:
    An Op implementing the distributed reduction.
  F)r   r   r	   r
   r   r   r   is_statelessordering_tokenmax_subdivs_per_devicename)r   collective_reduce_v2r   )r   r   r   r	   r
   r   r   r   r   r    r!   s              r   all_reduce_v2r#   _   sX    Z $%NN		0	0+113#3
 r   c           	          |dk  rt        d| d      t        j                  | dg||||j                         |      S )a  Accumulates tensors collectively, across devices, along first dimension.

  Args:
    t: the tensor to participate in the accumulation.
    group_size: the total number of tensors to be collectively accumulated.
      Each must reside on a different device. Should be a positive integer.
    group_key: an integer identifying the group of devices.
    instance_key: an integer identifying the participating group of Ops.
    communication_hint: preferred collective communication. The implementation
      may fall back to another mechanism. Options include `auto`, `ring`, and
      `nccl`.
    timeout: a float. If set to a non zero, set a completion timeout to detect
      staleness. If the timer goes off, a DeadlineExceededError is raised. The
      timeout value in seconds. This feature is experimental.

  Returns:
    An Op implementing the distributed operation.

  Raises:
    ValueError: if any of the input parameter constraints are not met.
  r   zCParameter `group_size` to all_gather must be at least 1. Received: r   r   shaper   r   r	   r   r   )r   r   collective_gatherr   r   r   r   r	   r   r   s         r   
all_gatherr)      s\    6 !^
 ##-,a1 2 2		-	-C+113
 r   c                 j    ||g}ng }t        j                  | ||||j                         |d||	      S )a  Accumulates tensors collectively, across devices, along first dimension.

  Args:
    t: the tensor to participate in the accumulation.
    group_size: an int32 tensor, the total number of tensors to be collectively
      accumulated. Each must reside on a different device. Should be a positive
      integer.
    group_key: an int32 tensor identifying the group of devices.
    instance_key: an int32 tensor identifying the participating group of Ops.
    communication_hint: preferred collective communication. The implementation
      may fall back to another mechanism. Options include `auto`, `ring`, and
      `nccl`.
    timeout: a float. If set to a non zero, set a completion timeout to detect
      staleness. If the timer goes off, a DeadlineExceededError is raised. The
      timeout value in seconds. This feature is experimental.
    ordering_token: a resource tensor on the same device as the op to order
      the collectives in a per-device manner by auto control dependency.
      This argument can be omited when there is one collective Op per
      `tf.function`, or when explicit control dependency is used instead of
      auto control dependency.
    name: name of the Op.

  Returns:
    An Op implementing the distributed operation.
  Fr   r   r	   r   r   r   r   r!   )r   collective_gather_v2r   r   r   r   r	   r   r   r   r!   s           r   all_gather_v2r.      sO    B $%NN		0	0+113#	
 	r   c           	      *   |dk  rt        d| d      | j                  |k7  rt        d| j                   d| d      | j                  |k7  rt        d| j                   d| d      t        j                  | |||||j                         |      S )a  Broadcasts one tensor to a group of others, across devices.

  Args:
    t: the tensor to be sent.
    shape: the shape of the tensor being sent, which must agree with t.
    dtype: the type of the tensor being sent, which must agree with t.
    group_size: one plus the number of receiving tensors, i.e. the total
      number of devices participating.  Each tensor must reside on a
      different device.
    group_key: an integer identifying the group of devices.
    instance_key: an integer identifying the participating group of Ops.
    communication_hint: preferred collective communication.  The implementation
      may fall back to another mechanism.  Options include `auto`, `ring`, and
      `nccl`.
    timeout: If set to a non zero, set a completion timeout to detect staleness.
      If the timer goes off, a DeadlineExceededError is raised.
      The timeout value in seconds. This feature is experimental.

  Returns:
    An Op implementing the distributed broadcast send.

  Raises:
    ValueError: if any of the input parameter constraints are not met.

  Note that the shape and dtype arguments appear redundant since they
  should be obtainable from t.  The are two reasons for including
  them.  First, the shape and type of tensors passed via broadcast must
  be known ahead of time in their most specific form so that the receive
  side can allocate memory for the operation and shape/type inference can
  carry forward from there.  Including the same declarations on the
  send side clarifies a commitment already made.  Secondly, having nearly
  identical use syntax for send and receive sides may simplify tool-driven
  generation of broadcast.
  r   GParameter `group_size` to broadcast_send must be at least 2. Received: r   zIShape of broadcast_send tensor `t` not equal to declared shape. Received z, expected zGType of broadcast_send tensor `t` not equal to declared type. Received r%   )r   r&   dtyper   collective_bcast_sendr   )r   r&   r1   r   r   r	   r   r   s           r   broadcast_sendr3      s    T 1_
	L	#$ $ WW
	GG9Kwa	12 2 WW
	GG9Kwa	12 2 
	1	1+113
 r   c                 T    t        j                  | ||||j                         |      S )as  Broadcasts one tensor to a group of others, across devices.

  Args:
    t: the tensor to be sent.
    group_size: an int32 tensor.  One plus the number of receiving tensors, i.e.
        the total number of devices participating.  Each tensor must reside on a
        different device.
    group_key: an int32 tensor identifying the group of devices.
    instance_key: an int32 tensor identifying the participating group of Ops.
    communication_hint: preferred collective communication.  The implementation
      may fall back to another mechanism.  Options include `auto`, `ring`, and
      `nccl`.
    timeout: If set to a non zero, set a completion timeout to detect staleness.
      If the timer goes off, a DeadlineExceededError is raised.
      The timeout value in seconds. This feature is experimental.

  Returns:
    An Op implementing the distributed broadcast send.
  )r   r   r	   r   r   )r   collective_bcast_send_v2r   r(   s         r   broadcast_send_v2r6   :  s2    2 
	4	4+113
 r   c           	      ~    |dk  rt        d| d      t        j                  | |||||j                         |      S )a  Receives a broadcasts tensor, across devices.

  Args:
    shape: Shape of the tensor to be received.
    dtype: Type of the tensor to be received.
    group_size: one plus the number of receiving tensors, i.e. the total
      number of devices participating.  Each tensor must reside on a
      different device.
    group_key: an integer identifying the group of devices.
    instance_key: an integer identifying the participating group of Ops.
    communication_hint: preferred collective communication.  The implementation
      may fall back to another mechanism.  Options include `auto`, `ring`, and
      `nccl`.
    timeout: If set to a non zero, set a completion timeout to detect staleness.
      If the timer goes off, a DeadlineExceededError is raised.
      The timeout value in seconds. This feature is experimental.

  Returns:
    An Op implementing the broadcast receive.

  Raises:
    ValueError: if any of the input parameter constraints are not met.
  r   r0   r   )r&   Tr   r   r	   r   r   )r   r   collective_bcast_recvr   r&   r1   r   r   r	   r   r   s          r   broadcast_recvr;   \  s\    < 1_
	L	#$ $ 
	1	1+113
 r   c           	      V    t        j                  ||||| |j                         |      S )a  Receives a broadcasts tensor, across devices.

  Args:
    shape: an int tensor.  Shape of the tensor to be received.
    dtype: Type of the tensor to be received.
    group_size: an int32 tensor.  One plus the number of receiving tensors, i.e.
        the total number of devices participating.  Each tensor must reside on a
        different device.
    group_key: an int32 tensor identifying the group of devices.
    instance_key: an int32 tensor identifying the participating group of Ops.
    communication_hint: preferred collective communication.  The implementation
      may fall back to another mechanism.  Options include `auto`, `ring`, and
      `nccl`.
    timeout: If set to a non zero, set a completion timeout to detect staleness.
      If the timer goes off, a DeadlineExceededError is raised.
      The timeout value in seconds. This feature is experimental.

  Returns:
    An Op implementing the broadcast receive.
  )r8   r   r   r	   r&   r   r   )r   collective_bcast_recv_v2r   r:   s          r   broadcast_recv_v2r>     s5    6 
	4	4+113
 r   c                 6    t        j                  | ||||      S )a  Initializes a collective communicator.

  This creates a collective communicator, which represents membership to a
  collective group identified by the group_key. It should be called once per
  member of the group, and each member needs to be on a different device.
  It blocks until all members of the group run this op.

  Communicators of a group can only be initialized once. Trying to initialize
  communicators for an existing group key will result in an error.

  Args:
    group_key: an int32 `tf.Tensor` identifying the group.
    rank: an `tf.Tensor` specifying the rank of this device in the group. If
      specified, the rank is required to be unique in the group.
    group_size: an int32 `tf.Tensor`. The size of the group.
    communication_hint: preferred collective communication.  The implementation
      may fall back to another mechanism.  Options include `auto`, `ring`, and
      `nccl`.
    timeout_seconds: If set to a non zero, set a completion timeout to detect
      staleness. If the timer goes off, a DeadlineExceededError is raised. The
      timeout value in seconds. This feature is experimental.


  Returns:
    A resource `tf.Tensor`.
  r   rankr   r   r   )r   "collective_initialize_communicatorr@   s        r   initialize_communicatorrC     s&    > 
	>	>+%
' 'r   c                 >    |g }t        j                  | ||||      S )a  Reduces tensors mutually.

  Args:
    communicator: the resource `tf.Tensor` returned from
      `initialize_communicator`.
    t: the `tf.Tensor` to be reduced.
    reduction: a string. The name of the operation to reduce the values.
      Accpeted values are `"min"`, `"max"`, `"mul"`, `"add"`.
    group_assignment: Optional int32 `tf.Tensor` with shape [num_groups,
      num_ranks_per_group]. `group_assignment[i]` represents the ranks in the
      `ith` subgroup.
    timeout_seconds: If set to a non zero, set a completion timeout to detect
      staleness. If the timer goes off, a DeadlineExceededError is raised. The
      timeout value in seconds. This feature is experimental.

  Returns:
    The reduced `tf.Tensor`.
  )communicatorinputr   	reductionr   )r   collective_reduce_v3)rE   r   rG   r   r   s        r   all_reduce_v3rI     s2    . 		0	0'%
' 'r   c                 j    ||g}ng }t        j                  | ||||j                         |d||	      S )a  Exchanges tensors mutually.

  Args:
    t: a `tf.Tensor`. The first dimension should have the length as the size of
      the group. `t[i]` is sent to `rank i` within the group.
    group_size: an int32 tensor, the total number of tensors to be mutually
      exchanged. Each must reside on a different device. Should be a positive
      integer.
    group_key: an int32 tensor identifying the group of devices.
    instance_key: an int32 tensor identifying the participating group of Ops.
    communication_hint: preferred collective communication. The implementation
      may fall back to another mechanism. Options include `auto` and `nccl`.
    timeout: a float. If set to a non zero, set a completion timeout to detect
      staleness. If the timer goes off, a DeadlineExceededError is raised. The
      timeout value in seconds. This feature is experimental.
    ordering_token: a resource tensor on the same device as the op to order the
      collectives in a per-device manner by auto control dependency. This
      argument can be omited when there is one collective Op per `tf.function`,
      or when explicit control dependency is used instead of auto control
      dependency.
    name: name of the Op.

  Returns:
    An Op implementing the distributed operation.
  Fr+   )r   collective_all_to_all_v2r   r-   s           r   all_to_all_v2rL     sO    F $%NN		4	4+113#

 
r   c                 <    |g }t        j                  | |||      S )a  Exchanges tensors mutually.

  Args:
    communicator: the resource `tf.Tensor` returned from
      `initialize_communicator`.
    t: a `tf.Tensor`. The first dimension should have the length as the size of
      the group. `t[i]` is sent to `rank i` within the group.
    group_assignment: Optional int32 `tf.Tensor` with shape [num_groups,
      num_ranks_per_group]. `group_assignment[i]` represents the ranks in the
      `ith` subgroup.
    timeout_seconds: If set to a non zero, set a completion timeout to detect
      staleness. If the timer goes off, a DeadlineExceededError is raised. The
      timeout value in seconds. This feature is experimental.

  Returns:
    a `tf.Tensor`. `t[i]` is sent from `rank i` within the group.
  )rE   rF   r   r   )r   collective_all_to_all_v3)rE   r   r   r   s       r   all_to_all_v3rO   *  s/    $ 		4	4'%	
' 'r   )AddId)r   autor   )rP   rQ   rR   r   NN)rR   r   )rR   r   NN)rP   NN)NN)__doc__tensorflow.python.opsr   r   r   r#   r)   r.   r3   r6   r;   r>   rC   rI   rL   rO    r   r   <module>rW      s    ! 4 ""(1h8 !%+!%)+>J #)%X &,!%/p '-=H *0N '-)b *0"P 06,-	$'R "#'"&	'L 	2j'r   