
    )`i                     P    d Z ddlmZ ddlZddlmZ ddlmZ  G d d          ZdS )	a3  
Copyright (c) 2023 by FlashInfer team.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
    )OptionalN)ProcessGroup   )get_nvshmem_modulec                       e Zd ZdZ	 	 ddedededej        dej        d	ee	         d
e
fdZd Zdej        dej        ddfdZd ZdS )NVSHMEMAllReduceaO  
    An AllReduce implementation for Single-Node and Multi-Node NVLink communication.
    This class handles NVLINK-specific allreduce operations, optimized for NVLink-enabled clusters.
    Note: Requires an active torch.distributed process group to be initialized
    prior to creating an instance of this class.

    Args:
        local_rank (int): The local rank of the current process.
        world_size (int): The total number of processes in the distributed group.
        max_buffer_elements (int): The maximum number of elements that can be stored in
        the buffer. This is used to allocate memory in nvshmem symm heap. set to the
        largest tensor size you will be reducing.
        dtype (torch.dtype): The data type of the tensors to be reduced.
        device (torch.device): The device on which the tensors are located.
        group (torch.distributed.ProcessGroup, optional): The torch.distributed process group to use.
        should_init (bool, optional): Whether to initialize nvshmem. Defaults to True.
    Raises:
        RuntimeError: If nvshmem fails to initialize.
    NT
local_rank
world_sizemax_buffer_elementsdtypedevicegroupshould_initc                    || _         || _        || _        || _        || _        || _        t                      | _        || _        | j        r| 	                                 | j        
                                }| j                                        }	||k    rt          d| d| d| d           |	|k    rt          d| d| d|	 d           | j                            |g| j        | j        j                  | _        | j                            |g| j        | j        j                  | _        t"          j                            | j                   d S )NzWARNING: Rank z: PE mismatch! Expected PE z	, got PE T)flushz : World size mismatch! Expected z, got )r	   r
   r   r   r   r   r   nvshmem_moduler   init_nvshmemnvshmem_my_penvshmem_n_pesprintnvshmem_mallocindexsymm_buffer_inputsymm_buffer_outputtorchdistributedbarrier)
selfr	   r
   r   r   r   r   r   my_pen_pess
             u/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/flashinfer/comm/nvshmem_allreduce.py__init__zNVSHMEMAllReduce.__init__.   s    %$
#6 
022& 	  #1133#1133Jddd
dd]bdd    JfffZff_dff    "&!4!C!C !JK"
 "

 #'"5"D"D !JK#
 #

 	!!$*-----    c                    t          j        | j                                        t           j        d          }| j        dk    r| j                            |           t           j                            |d           t           j        	                    | j
                   | j                            || j        | j                  }t           j                                         |dk    rt          d          d S )Ncpu)r   r   r   )srczFailed to initialize nvshmem)r   zerosr   nvshmem_unique_id_sizeuint8r	   nvshmem_get_unique_idr   	broadcastr   r   nvshmem_initr
   cudasynchronizeRuntimeError)r   uidinit_statuss      r!   r   zNVSHMEMAllReduce.init_nvshmem_   s    k6688+
 
 

 ?a55c:::##CQ#///!!$*---)66$/
 
 	
   !=>>> r#   inpoutreturnc                 z    | j                             | j        | j        |||                                           d S N)r   %nvshmem_allreduce_on_stream_with_copyr   r   numel)r   r2   r3   s      r!   
all_reducezNVSHMEMAllReduce.all_reducep   sD    AA#"IIKK	
 	
 	
 	
 	
r#   c                     | ` | `t          j                            | j                   t          j                                         | j        r| j	        
                                 d S d S r6   )r   r   r   r   r   r   r-   r.   r   r   nvshmem_finalize)r   s    r!   shutdownzNVSHMEMAllReduce.shutdowny   sk    "#!!$*---
    	30022222	3 	3r#   )NT)__name__
__module____qualname____doc__intr   r   r   r   r   boolr"   r   Tensorr9   r<    r#   r!   r   r      s         6 )- /. /./. /. !	/.
 {/. /. %/. /. /. /. /.b? ? ?"
el 
 
$ 
 
 
 
3 3 3 3 3r#   r   )	r@   typingr   r   torch.distributedr   nvshmemr   r   rD   r#   r!   <module>rH      s             * * * * * * ' ' ' ' ' 'f3 f3 f3 f3 f3 f3 f3 f3 f3 f3r#   