Ë
    )täi{1  ã            
       óH  — d dl m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mZmZ  e«       rd dlZd dlmZmZ  e«       rd dlZd d	ed
ee   fd„Zd„ Z	 d!dedededee   fd„Zdededefd„Zd d„Zd dededee   defd„Zd„ Z d„ Z!d„ Z"e#dk(  r e"«        yy)"é    )ÚOptionalN)Ú
model_info)ÚGatedRepoErrorÚRepositoryNotFoundError)Úinit_empty_weights)ÚCustomArgumentParser)Úcalculate_maximum_sizesÚconvert_bytesÚis_timm_availableÚis_transformers_available)Ú
AutoConfigÚ	AutoModelÚrepoÚtokenc                 ó\   — 	 t        | |¬«      S # t        t        f$ r Y yt        $ r Y yw xY w)zAVerifies that the model is on the hub and returns the model info.)r   Úgatedr   )r   ÚOSErrorr   r   )r   r   s     úk/Volumes/fast/ai/experiments/MLX_z-image/.venv/lib/python3.12/site-packages/accelerate/commands/estimate.pyÚverify_on_hubr   (   s6   € ðÜ˜$ eÔ,Ð,øÜ”^Ð$ò ÙÜ"ò Ùðús   ‚ �+ +ª+c                 ó´   — t        «       r"t        | t        «      rd| j                  d   v ryt	        «       r"t        | t
        «      rd| j                  d   v ryy)zG
    Checks what library spawned `error` when a model is not found
    zUnknown modelr   Útimmz$does not appear to have a file namedÚtransformersÚunknown)r   Ú
isinstanceÚRuntimeErrorÚargsr   r   )Úerrors    r   Úcheck_has_modelr   2   sP   € ô Ôœz¨%´Ô>À?ÐV[×V`ÑV`ÐabÑVcÑCcØä!Ô#Ü�uœgÔ&Ø2°e·j±jÀ±mÑCààó    Ú
model_nameÚlibrary_nameÚtrust_remote_codeÚaccess_tokenc                 ó¾  — t        | |«      }|dk(  rt        d| › d�«      ‚|dk(  rt        d| › d�«      ‚|€t        |dd«      }|st        d	| › d
�«      ‚|dk(  �rt	        «       st        d| › d�«      ‚t        d| › d�«       |j                  €t        d| › d�«      ‚|j                  j                  dd«      }t        j                  | ||¬«      }t        «       5  t        }t        |t        «      r>d}|j!                  «       D ]  }	|	j#                  d«      sŒ|	} n |�t        t$        |«      }|j'                  |t(        j*                  |¬«      }
ddd«       |
S |dk(  rTt-        «       st        d| › d�«      ‚t        d| › d�«       t        «       5  t/        j0                  | d¬«      }
ddd«       |
S t        d|› d�«      ‚# 1 sw Y   
S xY w# 1 sw Y   
S xY w)a(  
    Creates an empty model in full precision from its parent library on the `Hub` to calculate the overall memory
    consumption.

    Args:
        model_name (`str`):
            The model name on the Hub
        library_name (`str`):
            The library the model has an integration with, such as `transformers`. Will be used if `model_name` has no
            metadata on the Hub to determine the library.
        trust_remote_code (`bool`, `optional`, defaults to `False`):
            Whether or not to allow for custom models defined on the Hub in their own modeling files. This option
            should only be set to `True` for repositories you trust and in which you have read the code, as it will
            execute code present on the Hub on your local machine.
        access_token (`str`, `optional`, defaults to `None`):
            The access token to use to access private or gated models on the Hub. (for use on the Gradio app)

    Returns:
        `torch.nn.Module`: The torch model that has been initialized on the `meta` device.

    r   zRepo for model `zW` is gated. You must be authenticated to access it. Please run `huggingface-cli login`.r   z•` does not exist on the Hub. If you are trying to access a private repo, make sure you are authenticated via `huggingface-cli login` and have access.Nr!   FzModel `z{` does not have any library metadata on the Hub, please manually pass in a `--library_name` to use (such as `transformers`)r   z
To check `zU`, `transformers` must be installed. Please install it via `pip install transformers`zLoading pretrained config for `z` from `transformers`...úTried to load `z8` with `transformers` but it does not have any metadata.Úauto_map)r"   r   ÚAutoModelFor)Útorch_dtyper"   r   zE`, `timm` must be installed. Please install it via `pip install timm`z` from `timm`...)Ú
pretrainedz	Library `zM` is not supported yet, please open an issue on GitHub for us to add support.)r   r   ÚgetattrÚ
ValueErrorr   ÚImportErrorÚprintÚconfigr   Úgetr   Úfrom_pretrainedr   r   r   ÚdictÚkeysÚ
startswithr   Úfrom_configÚtorchÚfloat32r   r   Úcreate_model)r    r!   r"   r#   r   r&   r.   ÚconstructorÚvalueÚkeyÚmodels              r   Úcreate_empty_modelr<   B   s<  € ô0 ˜z¨<Ó8€Jà�WÒÜØ˜z˜lð  +Bð  Có
ð 	
ð 
�vÒ	ÜØ˜z˜lð +\ð \ó
ð 	
ð ÐÜ˜z¨>¸5ÓAˆÙÜØ˜*˜ð  &að  bóð ð �~Ó%Ü(Ô*ÜØ˜Z˜LÐ(}Ð~óð ô 	Ð/°
¨|Ð;SÐTÔUØ×ÑÐ$Ü °°Ð<tÐuÓvÐvà×$Ñ$×(Ñ(¨°UÓ;ˆÜ×+Ñ+¨JÐJ[ÐcoÔpˆÜÕ!ä#ˆKÜ˜(¤DÔ)Ø�Ø#Ÿ=™=ž?�CØ—~‘~ nÕ5Ø #˜Ùð +ð Ð$Ü")¬,¸Ó">�Kà×+Ñ+¨FÄÇÁÐarÐ+ÓsˆE÷ "ð2 €Lð 
˜Ò	Ü Ô"ÜØ˜Z˜LÐ(mÐnóð ô 	Ð/°
¨|Ð;KÐLÔMÜÕ!Ü×%Ñ% j¸UÔCˆE÷ "ð €Lô Ø˜�~Ð%rÐsó
ð 	
÷- "ð2 €Lú÷ "ð €Lús   Ã=GÄ9GÆGÇGÇGÚheadersÚrowsÚtitlec           	      óz  ‡‡‡— d\  }Šg Št        t        | «      «      D ]?  }|D �cg c]  }||   ‘Œ	 c}| |   gz   }t        d„ |D «       «      }‰j                  |«       ŒA t        t        |d   «      «      D �cg c]  }d‰|   › d�‘Œ }}|› |j	                  |«      › |› �}	dŠˆˆˆfd„}
 |
ddd	«      }t        |«      t        ‰«      kD  r-t        t        |«      t        |«      z
  «      Š‰d
xx   ‰z  cc<    |
ddd	«      } |
d‰d«      |› |j                  t        |«      dz
  «      › |› � |
ddd	«      g}dj	                  |«      dz   }‰d
xx   ‰z  cc<   t        | «      D ��cg c]  \  }}|j                  ‰|   «      ‘Œ }}}||	t        |«      z  › d|› d�z  }t        |«      D ]H  \  }}t        |«      D ��cg c]  \  }}|j                  ‰|   «      ‘Œ }}}||	t        |«      z  › d�z  }ŒJ |ddj	                  ‰D �cg c]  }‰|z  ‘Œ	 c}«      › d�z  }|S c c}w c c}w c c}}w c c}}w c c}w )zJCreates a pretty table from a list of rows, minimal version of `tabulate`.)u   â”‚u   â”€c              3   ó2   K  — | ]  }t        |«      –— Œ y ­w©N)Úlen)Ú.0r9   s     r   Ú	<genexpr>z%create_ascii_table.<locals>.<genexpr>˜   s   è ø€ ÐE±}¨eœs 5Ÿz±}ùs   ‚r   Ú%Úsc                 óh   •— | › |j                  ‰D �cg c]  }‰|z  ‘Œ	 c}«      › ‰‰z  › |› �S c c}w rB   )Újoin)Ú	left_charÚmiddle_charÚ
right_charÚnÚcolumn_widthsÚdiffÚ
in_betweens       €€€r   Úmake_rowz$create_ascii_table.<locals>.make_row    sF   ø€ Ø�˜[×-Ñ-Á}Ó.UÁ}À!¨z¸A«~À}Ñ.UÓVÐWÐXbÐeiÑXiÐWjÐkuÐjvÐwÐwùÒ.Us   “/
u   â”œu   â”¼u   â”¤éÿÿÿÿu   â”Œu   â”�é   u   â”¬Ú
u   â””u   â”´u   â”˜)
ÚrangerC   ÚmaxÚappendrI   ÚsumÚabsÚcenterÚ	enumerateÚtuple)r=   r>   r?   Úsep_charÚiÚrowÚcolumn_valuesÚmax_column_widthÚformatsÚpatternrQ   Ú	separatorÚinitial_rowsÚtableÚtextÚcentered_lineÚlineÚtrM   rN   rO   rP   s                      @@@r   Úcreate_ascii_tablerk   ’   s{  ú€ à'Ñ€HˆjØ€MÜ”3�w“<Ö ˆÙ+/Ó0©4 C˜˜Q›¨4Ñ0°G¸A±J°<Ñ?ˆÜÑE±}ÓEÓEÐØ×ÑÐ-Õ.ð !ô
 16´c¸$¸q¹'³lÔ0CÓDÑ0C¨1��= Ñ#Ð$ AÒ&Ð0C€GÐDà�
˜8Ÿ=™=¨Ó1Ð2°8°*Ð=€GØ€Döxñ ˜  uÓ-€IÜ
ˆ5ƒz”C˜Ó&Ò&Ü”3�u“:¤ I£Ñ.Ó/ˆØ�bÓ˜TÑ!Óñ ˜  uÓ-€Iá�˜
 EÓ*Øˆ*�U—\‘\¤# i£.°1Ñ"4Ó5Ð6°x°jÐAÙ�˜˜uÓ%ð€Lð
 �I‰I�lÓ# dÑ*€EØ�"Ó˜ÑÓÜBKÈGÔBTÔUÑBT±w°q¸$�T—[‘[ ¨qÑ!1Õ2ÐBT€MÑUØ	�œ% Ó.Ñ.Ð/¨r°)°¸BÐ?Ñ?€EÜ˜T–?‰ˆˆ4Ü@IÈ$ÄÔPÁ¹¸¸1˜Ÿ™ -°Ñ"2Õ3ÀˆÑPØ�Gœe MÓ2Ñ2Ð3°2Ð6Ñ6‰ð #ð 
ˆs�5—:‘:±}ÓE±}°!˜z¨A›~°}ÑEÓFÐGÀsÐKÑK€Eà€LùòC 1ùò Eùó, Vùó QùâEs   ¦H"Á:H'Å)H,Ç H2ÈH8c           	      óD  — | �| j                  d«      }nt        d¬«      }|j                  dt        d¬«       |j                  dt        dd	d
g¬«       |j                  dt        dg d¢dg d¢¬«       |j                  dddd¬«       | �|j	                  t
        ¬«       |S )Nzestimate-memoryzLModel size estimator for fitting a model onto device(e.g. cuda, xpu) memory.)Údescriptionr    z'The model name on the Hugging Face Hub.)ÚtypeÚhelpz--library_namez€The library the model has an integration with, such as `transformers`, needed only if this information is not stored on the Hub.r   r   )rn   ro   Úchoicesz--dtypesÚ+)r6   Úfloat16Úint8Úint4zbThe dtypes to use for the model, must be one (or many) of `float32`, `float16`, `int8`, and `int4`)rn   ÚnargsÚdefaultro   rp   z--trust_remote_codeÚ
store_truea  Whether or not to allow for custom models defined on the Hub in their own modeling files. This flag
                should only be used for repositories you trust and in which you have read the code, as it will execute
                code present on the Hub on your local machine.F)Úactionro   rv   )Úfunc)Ú
add_parserr   Úadd_argumentÚstrÚset_defaultsÚestimate_command)Ú
subparsersÚparsers     r   Úestimate_command_parserr�   »   sÑ   € ØÐØ×&Ñ&Ð'8Ó9‰ä%Øfô
ˆð ×Ñ˜¬3Ð5^ÐÔ_Ø
×ÑØÜð PØ˜Ð(ð	 ô ð ×ÑØÜØÚ6ØqÚ6ð ô ð ×ÑØØðBð ð ô ð ÐØ×ÑÔ!1ÐÔ2Ø€Mr   ÚbytesÚmixed_precisionÚmsamp_configÚreturnc                 ó´   — dddddœ}| }| dz  }|dk(  r||d<   ||d<   |dz  |d<   |dz  |d	<   |S |d
v s|dk(  r|€||d<   ||z   |d<   |dz  |d<   |d   |d	<   |S )a²  
    Given an amount of `bytes` and `mixed_precision`, calculates how much training memory is needed for a batch size of
    1.

    Args:
        bytes (`int`):
            The size of the model being trained.
        mixed_precision (`str`):
            The mixed precision that would be ran.
        msamp_config (`str`):
            The msamp config to estimate the training memory for if `mixed_precision` is set to `"fp8"`.
    rR   )r;   Ú	optimizerÚ	gradientsÚsteprS   r6   r;   rˆ   r‡   é   r‰   )rr   Úbfloat16Úfp8© )r‚   rƒ   r„   Úmemory_sizesÚ	fp32_sizeÚ	fp16_sizes         r   Úestimate_training_usager‘   à   s¶   € ð  ¨b¸rÈ2ÑN€LØ€IØ˜‘
€Ià˜)Ò#Ø )ˆ�WÑØ$-ˆ�[Ñ!Ø$-°¡Mˆ�[Ñ!Ø(¨1™}ˆ�VÑð Ðð 
Ð3Ñ	3¸È5Ò8PÐUaÐUið !*ˆ�WÑà$-°	Ñ$9ˆ�[Ñ!à$-°¡Mˆ�[Ñ!Ø+¨KÑ8ˆ�VÑØÐr   c                 óÚ  — 	 t        | j                  | j                  | j                  ¬«      }t        |«      \  }}g }| j                  D ]Y  }|}|d   }	t        ||«      }
|dk(  r|d	z  }|	d	z  }	n|d
k(  r|dz  }|	dz  }	n|dk(  r
|dz  }|	dz  }	|j                  ||	||
g«       Œ[ |S # t        t
        f$ r3}t        |«      }|dk7  rt	        d| j                  › d|› d�«      ‚|‚d}~ww xY w)z9Creates an empty model and gathers the data for the sizes)r!   r"   r   r%   z` with `z=` but a possible model to load was not found inside the repo.Nr   rr   rS   rs   rŠ   rt   é   )r<   r    r!   r"   r   r   r   r	   Údtypesr‘   rW   )r   r;   ÚeÚlibraryÚ
total_sizeÚlargest_layerÚdataÚdtypeÚdtype_total_sizeÚdtype_largest_layerÚdtype_training_sizes              r   Úgather_datarž     s>  € ð
Ü"Ø�O‰O¨$×*;Ñ*;Èt×OeÑOeô
ˆô !8¸Ó >Ñ€J�à€Dà—”ˆØ%ÐØ+¨AÑ.ÐÜ5Ð6FÈÓNÐØ�IÒØ Ñ!ÐØ 1Ñ$ÑØ�fŠ_Ø Ñ!ÐØ 1Ñ$ÑØ�fŠ_Ø Ñ!ÐØ 1Ñ$ÐØ�‰�UÐ/Ð1AÐCVÐWÕXð ð €Køô5 œ'Ð"ò Ü! !Ó$ˆØ�iÒÜØ! $§/¡/Ð!2°(¸7¸)ð  DAð  Bóð ð ˆûðús   ‚,B( Â(C*Â7.C%Ã%C*c                 ón  — t        | «      }|D ]y  }t        |«      D ]i  \  }}t        |t        t        f«      rt        |«      ||<   Œ+t        |t        «      sŒ<t        |j                  «       «      }|dk7  rt        |«      nd||<   Œk Œ{ g d¢}d| j                  › d�}t        |||«      }t        |«       y )NrR   zN/A)rš   zLargest Layerz
Total SizezTraining using AdamzMemory Usage for loading `Ú`)rž   r[   r   ÚintÚfloatr
   r1   rV   Úvaluesr    rk   r-   )	r   r™   r_   r^   ÚitemÚtraining_usager=   r?   rf   s	            r   r~   r~   &  s¦   € Ü�tÓ€DÛˆÜ  –~‰GˆAˆtÜ˜$¤¤e Ô-Ü& tÓ,��A’Ü˜D¤$Õ'Ü!$ T§[¡[£]Ó!3�Ø:HÈBÒ:Nœ ~Ô6ÐTY��A’ñ &ð ò N€Gà(¨¯©Ð(9¸Ð;€EÜ˜w¨¨eÓ4€EÜ	ˆ%…Lr   c                  óN   — t        «       } | j                  «       }t        |«       y rB   )r�   Ú
parse_argsr~   )r€   r   s     r   Úmainr¨   7  s    € Ü$Ó&€FØ×ÑÓ€DÜ�TÕr   Ú__main__rB   )FN)$Útypingr   r5   Úhuggingface_hubr   Úhuggingface_hub.utilsr   r   Ú
accelerater   Úaccelerate.commands.utilsr   Úaccelerate.utilsr	   r
   r   r   r   r   r   r   r|   r   r   Úboolr<   Úlistrk   r�   r¡   r1   r‘   rž   r~   r¨   Ú__name__r�   r   r   Ú<module>r³      sû   ðõ  ã Ý &ß Iå )Ý :÷ó ñ ÔÛß2áÔÛñ˜ð  H¨S¡Mó òð" hlñMØðMØ#&ðMØ;?ðMØW_Ð`cÑWdóMð`& ð &¨Dð &¸ó &óR"ñJ  3ð  ¸ð  ÈHÐUXÉMð  Ðeió  òF òFò"ð ˆzÒÙ…Fð r   