
    Αi                       S SK Jr  S SKrS SKJrJr  S SKJr  S SK	J
r
  S SKJrJr  \(       a$  S SKJrJr  S SK	Jr  S SKJr  S S	KJr  \\\\/\4   r " S
 S5      r\R0                   S     SS jj5       rSS jrSS jrg)    )annotationsN)TYPE_CHECKINGCallable)	framework)_get_global_group_warn_cur_rank_not_in_group)	GeneratorSequence)Tensor)task)Groupc                  d    \ rS rSr% SrS\S'   S\S'   S\S'   S	\S
'    S         SS jjrSrg)P2POp$   a?  
A class that makes point-to-point operations for "batch_isend_irecv".

This class creates the type of P2P operation, communication buffer, peer rank,
Group. Instances of this class will be passed to
``paddle.distributed.batch_isend_irecv`` for point-to-point communication.

Args:
    op (callable): A function to send data to or receive data from a peer process.
        The type of ``op`` is either ``paddle.distributed.isend`` or ``paddle.distributed.irecv``.
    tensor (Tensor): Tensor to send or receive.
    peer (int): The destination or source rank.
    group (Group, optional): The group instance return by new_group or None for global
        default group. Default: None.

Examples:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env: DISTRIBUTED)

        >>> import paddle
        >>> import paddle.distributed as dist

        >>> dist.init_parallel_env()
        >>> rank = dist.get_rank()
        >>> world_size = dist.get_world_size()

        >>> send_t = paddle.arange(2) + rank
        >>> # paddle.tensor([0, 1])  # Rank-0
        >>> # paddle.tensor([1, 2])  # Rank-1

        >>> recv_t = paddle.empty(shape=[2], dtype=send_t.dtype)

        >>> send_op = dist.P2POp(dist.isend, send_t, (rank + 1) % world_size)
        >>> recv_op = dist.P2POp(dist.irecv, recv_t, (rank - 1 + world_size) % world_size)


_P2POpTypeopr   tensorintpeerGroup | NonegroupNc                    U[         R                  [         R                  4;  a  [        S5      eXl        X l        X0l        Uc  [        5       U l        g UU l        g )NztInvalid ``op`` function. Expected ``op`` to be of type ``paddle.distributed.isend`` or ``paddle.distributed.irecv``.)	distisendirecvRuntimeErrorr   r   r   r   r   )selfr   r   r   r   s        r/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/distributed/communication/batch_isend_irecv.py__init__P2POp.__init__P   sQ     djj$**--0  	,1M&(
u
    )r   r   r   r   N)
r   r   r   r   r   r   r   r   returnNone)__name__
__module____qualname____firstlineno____doc____annotations__r   __static_attributes__ r!   r   r   r   $   se    $L 	NN
I #EE E 	E
 E 
E Er!   r   c              #  F  #    U c
  [        5       OU n U R                  nUR                  5          S v   Ub  [        U5      S:X  a  UR	                  5         g UR	                  U5        g ! Ub  [        U5      S:X  a  UR	                  5         f UR	                  U5        f = f7f)Nr   )r   process_group_start_coalescinglen_end_coalescing)r   taskspgs      r   _coalescing_managerr4   d   s      $)=eE			B&=CJ!O u% =CJ!O u%s   ,B!A( 5B!(6BB!c                   ^ [        U [        5      (       a  [        S U  5       5      (       d  [        S5      eU S   R                  R
                  m[        U4S jU  5       5      (       d  [        S5      eg)zi
Helper to check that the ``p2p_op_list`` is a list of P2POp instances and
all ops use the same backend.
c              3  B   #    U  H  n[        U[        5      v   M     g 7fr"   )
isinstancer   ).0p2p_ops     r   	<genexpr>%_check_p2p_op_list.<locals>.<genexpr>y   s      40;f
65!!s   z[Invalid ``p2p_op_list``. Each op is expected to to be of type ``paddle.distributed.P2POp``.r   c              3  V   >#    U  H  nTUR                   R                  :H  v   M      g 7fr"   )r   backend)r8   r9   r=   s     r   r:   r;      s      I[6w&,,...[s   &)z(All groups need to use the same backend.N)r7   listallr   r   r=   )p2p_op_listr=   s    @r   _check_p2p_op_listrA   t   sy    
 k4(( 40;4 1 1 :
 	

 !n""**GI[IIIEFF Jr!   c                   [        U 5        U S   R                  n[        U5      (       a  g[        R                  " 5       (       a  Uc
  [        5       OUnUR                  n/ n[        X5         U  HR  nUR                  nUR                  nUR                  nUR                  nU" XgU5      n	U	c  MA  UR                  U	5        MT     SSS5        U$ [        S5      e! , (       d  f       U$ = f)a  
Send or Receive a batch of tensors asynchronously and return a list of requests.

Process each of the point-to-point operations in ``p2p_op_list`` and return the
corresponding tasks. NCCL are currently supported.

Args:
    p2p_op_list (List[P2POp]): A list of point-to-point operations(type of each operator is
        ``paddle.distributed.P2POp``). The order of the isend/irecv in the list
        matters and it needs to match with corresponding isend/irecv on the
        remote end.

Returns:
    A list of distributed tasks returned by calling the corresponding
    op in the op_list.

Warning:
    This API only supports the dygraph mode.

Examples:
    .. code-block:: python

        >>> # doctest: +REQUIRES(env: DISTRIBUTED)

        >>> import paddle
        >>> import paddle.distributed as dist

        >>> dist.init_parallel_env()
        >>> rank = dist.get_rank()
        >>> world_size = dist.get_world_size()

        >>> send_t = paddle.arange(2) + rank
        >>> # paddle.tensor([0, 1])  # Rank-0
        >>> # paddle.tensor([1, 2])  # Rank-1

        >>> recv_t = paddle.empty(shape=[2], dtype=send_t.dtype)

        >>> send_op = dist.P2POp(dist.isend, send_t, (rank + 1) % world_size)
        >>> recv_op = dist.P2POp(dist.irecv, recv_t, (rank - 1 + world_size) % world_size)

        >>> tasks = dist.batch_isend_irecv([send_op, recv_op])

        >>> for task in tasks:
        ...     task.wait()

        >>> print(recv_t)
        >>> # paddle.tensor([1, 2])     # Rank-0
        >>> # paddle.tensor([0, 1])     # Rank-1
r   Nz*Don't support static graph mode currently.)rA   r   r   r   in_dynamic_moder   r=   r4   r   r   r   appendr   )
r@   r   r=   r2   r9   r   r   r   
comm_groupr   s
             r   batch_isend_irecvrF      s    d {#N  E"5))  ""',}!#%-- .%YY{{#\\
&
3#LL& & / GHH /. s   .A C2C
C+r"   )r   r   r2   ztask | Noner#   zGenerator[None, None, None])r@   zSequence[P2POp]r#   r$   )r@   zlist[P2POp]r#   z
list[task])
__future__r   
contextlibtypingr   r   paddle.distributeddistributedr   paddler   &paddle.distributed.communication.groupr   r   collections.abcr	   r
   r   paddle.base.corer   r   r   r   r   contextmanagerr4   rA   rF   r,   r!   r   <module>rQ      s    #  * ! 
 3%(63.45J=E =E@ '+&&$& & &G$FIr!   