o
    Me                     @   sR   d dl Z d dlZd dlZd dlmZmZmZmZ g Zdd Z	dej
dfddZdS )    N)get_clusterloggerget_host_name_ip
DeviceModec                 C   s   d}t | }t|}W d   n1 sw   Y  g }d}|d }|D ]M}|d }t|}tddrktdd}|dusCJ dtt|D ]}	td|	d}
|
dus_J d	|	|	|
 qI||f  S |	|d
  q%||fS )a  
    Args:
    rank_table_file_path: ascend npu rank file json
    {
        "status": "completed",
        "version": "1.0",
        "server_count": "2",
        "server_list": [
            {
                "server_id": "192.168.24.217",
                "device": [
                    {
                        "device_id": "0",
                        "device_ip": "192.1.184.23",
                        "rank_id": "0"
                    },
                    {
                        "device_id": "1",
                        "device_ip": "192.2.21.93",
                        "rank_id": "1"
                    }
                ]
            },
            {
                "server_id": "192.168.26.177",
                "device": [
                    {
                        "device_id": "0",
                        "device_ip": "192.1.94.132",
                        "rank_id": "2"
                    },
                    {
                        "device_id": "1",
                        "device_ip": "192.2.94.30",
                        "rank_id": "3"
                    }
                ]
            }
        ]
    }

    Returns:
        node_ips: node ip list
        device_count: number of npu per machine
    Nr   server_listZdeviceZFLAGS_MODELARTSZDLS_TASK_NUMBERzDLS_TASK_NUMBER didn't set!zVC_CUSTOM{}_HOSTSzVC_CUSTOM{}_HOSTS didn't set!Z	server_id)
openjsonloadlenosgetenvrangeintformatappend)Zrank_table_file_pathZ	json_dataZ	json_filenode_ipsdevice_countr   serverZdevice_listnodesnodenode_ip r   UD:\Projects\ConvertPro\env\Lib\site-packages\paddle/distributed/fleet/ascend_utils.py_get_ascend_rankfile   s,   .
r   i  c                    s   | r6t | \}}t|dkr|d }ntjd}d}|r$|t| }nt \}}||v s5J d||f n	dg}|d }d}dd t|D }d	d t||t| D }	g }
|D ] |
 fd
d|	D  qZt	|||
||S )z
    Args:
    rank_table_file: string, ascend npu rank file path
    device_mode: DeviceMode(Int)
    start_port: the start port of current runtime env
       r   ZPADDLE_TRAINER_IDNz/Can't find your local ip {%s} in node_ips: {%s}z	127.0.0.1c                 S   s   g | ]}t |qS r   )str.0xr   r   r   
<listcomp>z   s    z%get_cloud_cluster.<locals>.<listcomp>c                 S   s   g | ]}|qS r   r   r   r   r   r   r   {   s    c                    s   g | ]}d  |f qS )z%s:%dr   )r   portipr   r   r      s    )
r   r
   r   environgetr   r   r   r   r   )Zrank_table_fileZdevice_modeZ
start_portr   r   r   Z
node_index_Zdevices_per_procZ
free_portsZtrainer_endpointsr   r!   r   get_cloud_cluster\   s2   	


r&   )r   r   ZpaddleZ%paddle.distributed.fleet.launch_utilsr   r   r   r   __all__r   Z
ASCEND_NPUr&   r   r   r   r   <module>   s   E