
    &`i#                        d dl mZ d dlZd dlmZ d dlmZmZmZ d dl	m
Z
 d dlmZ d dlmZmZ  ed	          d
ee         fd            Z ed	          d
ee         fd            Z ed	          d
efd            Z ed	           G d d                      Z ed	          e
	 ddededed
efd                        ZdS )    )OptionalN)TPUAcceleratorManager)VALID_TPU_TYPESget_chips_per_hostreserve_tpu_slice)client_mode_wrap)	PublicAPI)PlacementGroupplacement_groupalpha)	stabilityreturnc                  <    t          j                    } | dk    rd} | S )z
    Return the name of the TPU pod that the worker is a part of.

    Returns:
        The name of the TPU pod. Returns None if not part of a TPU pod.
     N)r   get_current_node_tpu_name)tpu_names    `/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/util/tpu.pyget_current_pod_namer      s%     %>@@H2~~O    c                  (    t          j                    S )z
    Count the number of workers associated with the TPU pod that the worker belongs to.

    Returns:
        The total number of workers in the TPU pod. Returns None if the worker is not
        part of a TPU pod.
    )r   "get_num_workers_in_current_tpu_pod r   r   get_current_pod_worker_countr      s     !CEEEr   c                  (    t          j                    S )z
    Return the number of TPU chips on the node.
    Returns:
        The total number of chips on the TPU node. Returns 0 if none are found.
    )r   !get_current_node_num_acceleratorsr   r   r   get_num_tpu_chips_on_noder   )   s     !BDDDr   c                   *   e Zd ZdZ	 	 	 	 ddededed	ed
ee         f
dZdefdZd Z	 	 	 dded	ed
ee         de	fdZ
ede	fd            Zedefd            Zedefd            Zedefd            Zedefd            Zedefd            ZdS )SlicePlacementGroupa	  
    A handle to a placement group reservation for a TPU slice.

    The following definitions are added for clarity:

    - Accelerator type: A string describing the accelerator type and version (e.g. TPU-V2, TPU-V6E).
    - Accelerator version: The accelerator generation only (e.g. v6e, v5p, v5litepod).
    - Pod type: The TPU accelerator version and the number of chips in a topology. (e.g. v6e-128, v5p-8).
    - Accelerator topology: The physical topology representing the structure (e.g. 2x2x2, 16x16).

        Args:
            topology: The TPU topology string (e.g. "2x2x2").
            accelerator_version: The TPU accelerator generation (e.g. "v6e", "v5p", "v4").
            strategy: PlacementGroup parameter. The strategy to create the placement group. Currently default to "SPREAD"

             - "PACK": Packs Bundles into as few nodes as possible.
             - "SPREAD": Places Bundles across distinct nodes as even as possible.
             - "STRICT_PACK": Packs Bundles into one node. The group is
               not allowed to span multiple nodes.
             - "STRICT_SPREAD": Packs Bundles across distinct nodes.

            lifetime: PlacementGroup parameter. Either `None`, which defaults to the placement group
                will fate share with its creator and will be deleted once its
                creator is dead, or "detached", which means the placement group
                will live as a global object independent of the creator.

            num_slices: Number of TPU slices in the SlicePlacementGroup. Defaults to 1 when unspecified.

        Examples:

        .. testcode:: python
            :skipif: True

            import ray
            from ray.util.scheduling_strategies import PlacementGroupSchedulingStrategy
            from ray.util.tpu import SlicePlacementGroup

            slice_handle = SlicePlacementGroup(topology="4x4", accelerator_version="v6e")
            slice_pg = slice_handle.placement_group
            ray.get(slice_pg.ready(), timeout=10)

            @ray.remote(num_cpus=0, resources={'TPU': 4})
            def spmd_task(world, rank):
                print(f"Current TPU is rank {rank} of {world}")

            tasks = [
                spmd_task.options(
                    scheduling_strategy=PlacementGroupSchedulingStrategy(
                        placement_group=slice_pg,
                    )
                ).remote(world=4, rank=i)
                for i in range(slice_handle.num_workers)
            ]

    SPREADr   N   topologyaccelerator_versionstrategynamelifetimec                     |                                                                 | _        |                                                                 | _        || _        |                                  |                     |||          | _        d S )N)striplower	_topology_accelerator_version_num_slices_validate_tpu_config_reserve_slice_placement_group)selfr!   r"   r#   r$   r%   
num_slicess          r   __init__zSlicePlacementGroup.__init__m   s     "))//11$7$=$=$?$?$E$E$G$G!%!!### !% 3 3!
 !
r   c                 L    |t           vrt          d| dt                      d S )NzInvalid accelerator version: z. Must be one of: )r   
ValueError)r/   r"   s     r   _accelerator_version_checkz.SlicePlacementGroup._accelerator_version_check   s<    o55h0ChhWfhh   65r   c                    |                      | j                   t          j        | j        | j                  s t          d| j         d| j         d          d}| j                                                                                            d          D ]}|t          |          z  }t          | j        | j                  | _        || j        z  | _        | j        | j        z  | _        d S )N)tpu_accelerator_versiontpu_topologyzInvalid accelerator topology: 'z' for accelerator version: ''r    x)r4   r"   r   !is_valid_tpu_accelerator_topologyr)   r3   r'   r(   splitintr   _chips_per_host_num_workers_per_slicer+   _num_workers)r/   total_chipsvalues      r   r,   z(SlicePlacementGroup._validate_tpu_config   s    	''(@AAA$F$($<
 
 
 	 E$. E E)-)AE E E  
 ^))++113399#>> 	& 	&E3u::%KK1ND4 
  
 '2T5I&I# 7$:JJr   r   c                 .   g }g }d| j                                         z   }t          | j                  D ]K}t	          | j        |          }|t          j        j        |ig| j	        z  z  }|d| j
        ig| j	        z  z  }Lt          |||||          }	|	S )z8Performs the two-step scheduling to reserve a TPU slice.zTPU-TPU)bundlesr#   r$   r%   bundle_label_selector)r"   upperranger0   r   r)   ray_rayletRAY_NODE_TPU_SLICE_NAME_KEYr>   r=   r   )
r/   r#   r$   r%   rE   rD   accelerator_type_
slice_namepgs
             r   r-   z"SlicePlacementGroup._reserve_slice   s     !# "D$<$B$B$D$DDt'' 	U 	UA +4>;KLLJ!8*E&+&, ,!  4569TTTGG"7
 
 
 	r   c                     | j         S )z%The underlying PlacementGroup object.)r.   r/   s    r   r   z#SlicePlacementGroup.placement_group   s     $$r   c                     | j         S )z0The number of chips per host for this TPU slice.)r=   rP   s    r   chips_per_hostz"SlicePlacementGroup.chips_per_host   s     ##r   c                     | j         S )z5The total number of hosts in the SlicePlacementGroup.)r?   rP   s    r   num_workerszSlicePlacementGroup.num_workers   s       r   c                     | j         S )z'The physical topology of the TPU slice.)r)   rP   s    r   r!   zSlicePlacementGroup.topology   s     ~r   c                     | j         S )z&The TPU accelerator type of the slice.)r*   rP   s    r   r"   z'SlicePlacementGroup.accelerator_version   s     ((r   c                     | j         S )z8The number of TPU slices this SlicePlacementGroup spans.)r+   rP   s    r   r0   zSlicePlacementGroup.num_slices   s     r   )r   r   Nr    )r   r   N)__name__
__module____qualname____doc__strr   r1   r4   r,   r
   r-   propertyr   r<   rR   rT   r!   r"   r0   r   r   r   r   r   3   s       6 6z !"&
 

 !

 
 
 3-
 
 
 
.c    K K K4 !"&	   3-	
 
   > % % % % X% $ $ $ $ X$
 !S ! ! ! X! #    X )S ) ) ) X)  C       X     r   r   r    r!   r"   r0   c                 "    t          d| ||d|S )aS  Asynchronously creates a PlacementGroup for a TPU slice.

    A slice placement group reserves num_slices TPU slice(s) and creates a placement
    group for scheduling tasks.

    Args:
        topology: The desired TPU pod topology (e.g. "4x4", "2x8").
        accelerator_version: The TPU accelerator generation, (e.g. "V4", "V5P", "V6E").
        num_slices: The number of tpu slices within the placement group
        **kwargs: Additional arguments for the placement group, such as 'name', 'lifetime', or 'strategy'.

    Returns:
        The handle for the created SlicePlacementGroup.
    )r!   r"   r0   r   )r   )r!   r"   r0   kwargss       r   slice_placement_groupr`      s3    .  /  	  r   )r    )typingr   rH   ray._private.acceleratorsr   ray._private.accelerators.tpur   r   r   ray._private.client_mode_hookr   ray.util.annotationsr	   ray.util.placement_groupr
   r   r\   r   r<   r   r   r   r`   r   r   r   <module>rg      s         



 ; ; ; ; ; ;         
 ; : : : : : * * * * * * D D D D D D D D W
hsm 
 
 
 
 WFhsm F F F F WE3 E E E E Wj  j  j  j  j  j  j  j Z W   
       r   