
    GJj                         d dl Z d dlZd dlZd dlZd dlmZmZ d dlmZ d dl	m
Z
mZ e G d d             Ze G d d             Z G d	 d
e j                        Zd Zd Zd Zd Zy)    N)	dataclassfield)Path)OptionalUnionc                   X    e Zd ZU eed<   eed<   ee   ed<   eeeeee   f         ed<   y)Hostrankssh_hostnameipsrdmaN)	__name__
__module____qualname__int__annotations__strlistr   r        g/Users/ahmed/devFolder/claude-voice/.venv/lib/python3.12/site-packages/mlx/_distributed_utils/common.pyr	   r	      s3    
I	cN
xc49n-.
//r   r	   c                   v    e Zd ZU ee   ed<   dZeed<    ee      Z	ee   ed<   d Z
ed        Zed
d       Zy	)Hostfilehosts backend)default_factoryenvsc           	          | j                   | j                  | j                  D cg c]&  }|j                  |j                  |j
                  d( c}dS c c}w )N)sshr   r   )r   r   r   )r   r   r   r   r   r   )selfhs     r   to_jsonzHostfile.to_json   sS    ||II #A quuaffE#
 	
s   +Ac                 :   t        |      }|j                         st        dt        |       d      	 t	        j
                  t        |            }d}g }g }t        |t              r|d   }|d   }|d   }nt        |t              r|}t        |      D cg c]6  \  }}t        ||d   |j                  dg       |j                  d	g             8 }}} | |||      S c c}}w # t        $ r*}t        d
t        |       dt        |       d      |d}~ww xY w)a  Parse the json hostfile that contains both the hostnames to ssh into and
        the ips to communicate over when using the ring backend. It can also
        contain the backend to be used and environment variables to set when
        launching a distributed job.

        Example:

            {
                "backend": "jaccl",
                "envs": [
                    "MLX_METAL_FAST_SYNCH=1"
                ],
                "hosts": [
                    {"ssh": "hostname1", "ips": ["123.123.123.1"], "rdma": [null, "rdma_en2", "rdma_en3"]},
                    {"ssh": "hostname2", "ips": ["123.123.123.2"], "rdma": ["rdma_en2", null, "rdma_en3"]},
                    ...
                    {"ssh": "hostnameN", "ips": ["123.123.123.N"], "rdma": ["rdma_en2", "rdma_en3", null]},
                ]
            }

        Args:
            hostfile (str): The path to the json file containing the host
                information
        z	Hostfile z doesn't existr   r   r   r   r    r   r   zFailed to parse hostfile z ()N)r   exists
ValueErrorr   jsonloadopen
isinstancedictr   	enumerater	   get	Exception)	clshostfiledatar   r   r   ir"   es	            r   	from_filezHostfile.from_file$   s+   4 > yX~FGG	99T(^,DGDE$%y/F|WD$' &e,,DAq Q%!%%r"2AEE&"4EF,  
 ugt,,  	+CM?"SVHAF	s*   A$C' ;C!C' !C' '	D0%DDc           
      (   g }t        |j                  d            D ][  \  }}|dk(  rt        d      	 t        j                  |       |g}t        |      D ]  }|j                  t        |||g              ! ]  | |      S # t        $ r g }Y Dw xY w)N,r   zHostname cannot be empty)r-   splitr'   	ipaddress
ip_addressrangeappendr	   )r0   hostlistrepeatsr   r3   r"   r   s          r   	from_listzHostfile.from_listZ   s    hnnS12DAqBw !;<<$$Q'c 7^T!QR01 $ 3 5z	  s   BBBN)   )r   r   r   r   r	   r   r   r   r   r   r#   classmethodr5   r?   r   r   r   r   r      sW    :GSD1D$s)1
 3 3j  r   r   c                       e Zd ZddZy)OptionalBoolActionNc                     |j                  d      rt        || j                  d       y t        || j                  d       y )Nz--no-FT)
startswithsetattrdest)r!   parser	namespacevaluesoption_strings        r   __call__zOptionalBoolAction.__call__k   s0    ##G,Ityy%0Ityy$/r   )N)r   r   r   rL   r   r   r   rC   rC   j   s    0r   rC   c                 <    t        |       } | dk  rt        d      | S )Nr   zNumber should be positive)r   r'   )xs    r   positive_numberrO   r   s#    AAAv455Hr   c                 R    | sy t         j                  |d<   t        dg|di | y )Nfilez[32m[INFO][0msysstderrprint)verboseargskwargss      r   logrZ   y   s-    ZZF6N	
7T7977r   c                  L    t         j                  |d<   t        dg| di | y )NrQ   z[33m[WARN]rR   rS   rX   rY   s     r   log_warningr]      s'    ZZF6N	
7T7977r   c                  L    t         j                  |d<   t        dg| di | y )NrQ   z[31m[ERROR]rR   rS   r\   s     r   	log_errorr_      s'    ZZF6N	
8d8I88r   )argparser9   r(   rT   dataclassesr   r   pathlibr   typingr   r   r	   r   ActionrC   rO   rZ   r]   r_   r   r   r   <module>re      sv       
 (  " 0 0 0 R R Rj0 088
9r   