
    )j~"                     *   d dl Z d dlZd dlZd dlZd dlZd dlmZ d dlmZm	Z	 d dl
mZ d dlmZ d dlmZmZ d dlmZ d dlmZ d dlmZ d d	lmZmZ d d
lmZ ddlmZ dZe	e G d d                                  Zd Z d Z!d Z"d Z#ddZ$ddZ%d Z&dS )    N)	dataclass)partialtotal_ordering)Path)TemporaryDirectory)LiteralOptional)LocalEntryNotFoundError)Hostfile)launch_jaccllaunch_ring)tqdm   )hf_repo_to_pathi  @c                   h    e Zd ZU ed         ed<   eed<   ee         ed<   d Zd Ze	d             Z
dS )	DirectoryEntry	directorysymlinkfile
entry_typepathdstc                     t          ddd          }|| j                 }||j                 }||k     p||k    o| j        |j        k     S )Nr   r      r   )dictr   r   )selfother
order_typeo1o2s        V/lsinfo/ai/hellotax_ai/base_platform/venv/lib/python3.11/site-packages/mlx_lm/share.py__lt__zDirectoryEntry.__lt__    sP    Aqq999
(()Bw?28>	EJ(>?    c                 b    | j         |j         k    o| j        |j        k    o| j        |j        k    S N)r   r   r   )r   r   s     r"   __eq__zDirectoryEntry.__eq__&   s6    Ou// &	UZ'&EI%	
r$   c                 .   dddd|                                 |                                f         }|                                r|                                nd } | |t          |                    |                    t          |                    S )Nr   r   r   ))TF)FT)FF)is_dir
is_symlinkreadlinkstrrelative_to)clsrootr   r   r   s        r"   	from_pathzDirectoryEntry.from_path-   s     '$"
 
 ++--**
*	,

 "&!2!2<dmmooos:s4#3#3D#9#9::CHHEEEr$   N)__name__
__module____qualname__r   __annotations__r,   r	   r#   r'   classmethodr0    r$   r"   r   r      s          67777
III	#@ @ @
 
 
 F F [F F Fr$   r   c                  H    t           j        |d<   t          dg| dR i | d S )Nr   z[31m[ERROR]z[0m)sysstderrprint)argskwargss     r"   errorr=   9   s7    ZF6N	
8d8I88888888r$   c           	         | j         t          d          t          j        | j                   }|j        dk    rt          d          t          |j                  dk    rt          d          t          j        |j        t          t          j                              |j        dd dd          }t          j        d	d
dg}| j        |d| j        gz  }| j        |d| j        gz  }| j        |d| j        gz  }| j        |d| j        gz  }|j        dk    rt'          d |j        ||           d S |j        dk    s|j        dk    rt)          d |j        ||           d S t          d          )NzNo hostfile provided z,Backend needs to be defined in the hostfile.r   z.More than one node needs to be in the hostfileFiC~  )backendcwdenvverbosepythonstarting_portconnections_per_ipz-mmlx_lmshare--path--model--tmpdir--dstringjacclz
jaccl-ringz7Only ring, jaccl and jaccl-ring backends are supported.)hostfile
ValueErrorr   	from_filer@   lenhostsargparse	Namespacer,   r   rA   envsr8   
executabler   modeltmpdirr   r   r   )r;   rO   launch_argscmds       r"   launchr\   >   s   }/000!$-00H2GHHH
8>aIJJJ$ 

OOM  K 		C y$)$$z	4:&&{
DK((x""6!!D(.+s;;;;;		W	$	$(8L(H(HT8>;<<<<<RSSSr$   c                                                        s( j        t                               j                   gfS  fd                     d          D             } t          |          fS )Nc                 F    g | ]}t                               |          S r6   )r   r0   ).0fr   s     r"   
<listcomp>zget_files.<locals>.<listcomp>l   s)    HHH1^%%dA..HHHr$   *)r)   parentr   r0   rglobsorted)r   filess   ` r"   	get_filesrg   h   sj    ;;== J{^55dk4HHIIIHHHH

3HHHEr$   c                 h    | dk    r	| dz  ddS | dk    r	| dz  ddS | dk    r	| dz  ddS | ddS )	Ng    eAz.2z GB/sg    .Az MB/sg     @@z KB/sz B/sr6   )xs    r"   	format_bwrj   p   sm    Cxxc'#####Cxxc'#####Cxxc'#####====r$   c           	         |pt           j                                        }t          t           j        j        |          }d}t          j                    }|                                |k    r:t          | |z  d          5 }|                    dd           |	                                }|                    d           t          |dd|dd	          }	 |                    t                    }	|	st          j         |d                     njt          j         |t          |	                               t          j         ||	                     |                    t          |	                     |                                 d d d            n# 1 swxY w Y   nt          | |z  d
          5 }d }	 |d                                          }
|
dk    r= |t          j        |
t           j                            }	t          j        |	           |
dk    rd } |d                                          }
|
dk    r= |t          j        |
t           j                            }t          j        |           |                    t-          |	                     |}	|
dk    d d d            n# 1 swxY w Y   |t          j                    |z
  fS )Ngroupr   rbr   BTr   F)totalunit
unit_scaledescpositionleavewbdtype)mxdistributedinitr   all_sumtimerankopenseektellr   read
CHUNK_SIZEevalrR   
async_evalupdatecloseitemzerosuint8writebytes)r   r   srcrm   r|   
total_size
start_timer`   pbardata
chunk_size	next_datas               r"   
share_filer   z   s   *R^((**Ebn,E:::GJJzz||s$+t$$ 	FF1aLLLJFF1III   D'vvj)) GGGAJJ'''D		**+++ggdmm,,,CII&&&' JJLLL-	 	 	 	 	 	 	 	 	 	 	 	 	 	 	2 $+t$$ 	!D **JA~~wrx
"(CCCDDq.. 	$WQZZ__..
>> '28(L(L(L M MIM),,,d$$$  q..	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	!" ty{{Z///s&   DFF #F <C5J==KKc                 j   |pt           j                                        }t          t           j        j        |          }|                                |k    r\t          j        |          }t          j         |t          |                               t          j         ||                     n |d          
                                } |t          j        |t           j                            }t          j        t          |                    }|D ]W}|j        dk    r| |j        z                                   *|j        dk    r"| |j        z                      |j                   Xd}	d}
t)          t          |          dd|                                |k              }|D ]l}|j        d	k    r$t+          | |j        ||          \  }}|	|z  }	|
|z  }
|                    d
           |                    t1          |	|
z                       m|                                 d S )Nrl   r   rw   r   r   gư>Files)rp   rs   rt   disabler   r   )speed)ry   rz   r{   r   r|   r~   pickledumpsr   rR   r   r   r   loadsr   r   r   mkdir
symlink_tor   r   r   r   set_postfixrj   r   )r   rf   r   rm   r|   	file_listfile_list_sizer   r   r   
total_timer   sts                 r"   share_filesr      s	   *R^((**Ebn,E:::Gzz||sL''	
I''(((
	""#### !**wrxbh???@@U4[[))  	8 	8D+--	!((****I--	!--dh777 JJc%jjwEJJLLTWDWXXXD C C?f$$dDIsE::DAq!OJ!OJAyj)@AABBBBJJLLLLLr$   c                  	   t          j        d          } |                     dt          d           |                     dt          d           |                     dt          d	           |                     d
t          d           |                     dt          d           |                                 }|j        |j        cxu rn n|                     d           t          j	        t          j
                   t          j                                        }|                                dk    rt          |           d S d }g }|j        ;t          |j                  x}                                rt#          |          \  }}n|j        x	 t%          |j                  }|j        j        dk    rt+          dt          |                     t#          |j        j                  \  }}n# t,          $ r
}Y d }~nd }~ww xY wt          j                            t1          |          dk              }|                                                                }|                                                                }|s2t          d           t          d           t9          j        d           |j        H|                                |k    rt          |                               d          }t          j!        t          j        "                    t1          |                               t          j!        t          j        "                    |                     nt          j        "                    d                                          }	t          j        "                    t          j#        |	t          j$                            }t          tK          |          &                    d                    }n,|                                |k    rt          |j                  }tO          |j(                  5 }
|                                |k    rtS          ||||           nKtS          t          |
          |||           |*                    dd           tW          j,        |
|           d d d            d S # 1 swxY w Y   d S )Nz8Distribute a model to other nodes using MLX distributed.)descriptionrI   z"Path to a file or folder to share.)typehelprJ   z.The path to a local model or Hugging Face repoz
--hostfilez8The file containing the hosts and connection informationrL   zCThe destination path in other nodes (defaults to --path or --model)rK   z?Intermediate temporary directory to ensure successfull transferz)One of --path or --model must be providedr   	snapshotsz=The model repository appears to be corrupted, it resolved to r   z/The --path needs to exist in at least one node.zAIf it is a remote repository download it first with `hf download`zutf-8rw   )dirT)parentsexist_ok)-rT   ArgumentParseradd_argumentr,   
parse_argsr   rX   r=   ry   set_default_devicecpurz   r{   sizer\   r   existsrg   r   rc   namerP   	Exception
all_gatherrR   argmaxr   anyr8   exitr   r~   encoder   r|   r   r   r   decoder   rY   r   r   osrename)parserr;   worldr   rf   ehas_filer   r   	data_sizetmps              r"   mainr      s   $N  F s1UVVV
"R     G    
 R    
 N     DyDJ&&&&&&&&@AAA"&!!!N!!Ezz||qt DEy$ty//"9$!A!A!C!Cooee			"4:..D{;.. _TWX\T]T]__   $DK$677KD%% 	 	 	DDDD	~((Ua88H
//


 
 
"
"C||~~""$$H ?@@@QRRR x::<<3t99##G,,DGBN**3t9955666GBN**4001111..q116688I>))"(9BH*M*M*MNNDd**73344DD			DH~~		,	,	, !::<<3eS%0000S		5#u555JJtdJ333Ic4   ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !s&   AG7 7
HHA7SSSr&   )'rT   r   r   r8   r}   dataclassesr   	functoolsr   r   pathlibr   tempfiler   typingr   r	   mlx.corecorery   huggingface_hub.errorsr
   mlx._distributed_utils.commonr   mlx._distributed_utils.launchr   r   r   utilsr   r   r   r=   r\   rg   rj   r   r   r   r6   r$   r"   <module>r      s    				  



  ! ! ! ! ! ! - - - - - - - -       ' ' ' ' ' ' $ $ $ $ $ $ $ $       : : : : : : 2 2 2 2 2 2 C C C C C C C C       " " " " " "
 
F F F F F F F  F<9 9 9
'T 'T 'TT    10 10 10 10h" " " "JO! O! O! O! O!r$   