o
    Gi+                     @   s   d 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	Z	ddl
Z
ddl	mZ ddlmZ ddlmZ d	d
lmZ defddZG dd deZdS )zd
Usage example:
    diffusers-cli fp16_safetensors --ckpt_id=openai/shap-e --fp16 --use_safetensors
    N)ArgumentParser	Namespace)import_module)hf_hub_download)version   )logging   )BaseDiffusersCLICommandargsc                 C   s"   | j rtd t| j| j| jS )NzThe `--use_auth_token` flag is deprecated and will be removed in a future version.Authentication is now handled automatically if the user is logged in.)use_auth_tokenwarningswarnFP16SafetensorsCommandckpt_idfp16use_safetensors)r    r   W/home/ubuntu/.local/lib/python3.10/site-packages/diffusers/commands/fp16_safetensors.pyconversion_command_factory#   s
   r   c                   @   s<   e Zd ZedefddZdededefddZd	d
 Z	dS )r   parserc                 C   sZ   |  d}|jdtdd |jdddd |jd	dd
d |jdddd |jtd d S )Nfp16_safetensorsz	--ckpt_idzTRepo id of the checkpoints on which to run the conversion. Example: 'openai/shap-e'.)typehelpz--fp16
store_truez/If serializing the variables in FP16 precision.)actionr   z--use_safetensorsz)If serializing in the safetensors format.z--use_auth_tokenznWhen working with checkpoints having private visibility. When used `hf auth login` needs to be run beforehand.)func)
add_parseradd_argumentstrset_defaultsr   )r   conversion_parserr   r   r   register_subcommand-   s$   
z*FP16SafetensorsCommand.register_subcommandr   r   r   c                 C   sF   t d| _|| _d| | _|| _|| _| js| js!tdd S d S )Nzdiffusers-cli/fp16_safetensorsz/tmp/zQWhen `use_safetensors` and `fp16` both are False, then this command is of no use.)r   
get_loggerloggerr   local_ckpt_dirr   r   NotImplementedError)selfr   r   r   r   r   r   __init__B   s   zFP16SafetensorsCommand.__init__c              	   C   s  t tjt dk rtdddlm} ddlm} t| j	dd}t
|d}t|d	 }W d    n1 s9w   Y  ttd
|}| jd| d |j| j	| jrYtjntjd}|j| j| jrgdnd| jrmdnd d | jd| j d | jrt| j d}n| jrt| j d}d| j d| j d}	g }
|D ]}|
|d|ddd  |d qd}|| j	|
|	|dddj}| jd| d d S )Nz0.9.0zrThe huggingface_hub version must be >= 0.9.0 to use this command. Please update your huggingface_hub installation.r   )create_commit)CommitOperationAddzmodel_index.json)repo_idfilenamer_class_name	diffuserszPipeline class imported: .)torch_dtypeTFr   )safe_serializationvariantzPipeline locally saved to z/*/*.fp16.*z/*/*.safetensorszSerialize variables with FP16: z and safetensors: /   )path_in_repopath_or_fileobjzVariables converted by the [`diffusers`' `fp16_safetensors` CLI](https://github.com/huggingface/diffusers/blob/main/src/diffusers/commands/fp16_safetensors.py).model)r+   
operationscommit_messagecommit_description	repo_type	create_przPR created here: )r   parsehuggingface_hub__version__ImportErrorr)   huggingface_hub._commit_apir*   r   r   openjsonloadgetattrr   r$   infofrom_pretrainedr   torchfloat16float32save_pretrainedr%   r   globappendjoinsplitpr_url)r'   r)   r*   model_indexfpipeline_class_namepipeline_classpipelinemodified_pathsr:   r9   pathr;   
hub_pr_urlr   r   r   runO   sT   (zFP16SafetensorsCommand.runN)
__name__
__module____qualname__staticmethodr   r"   r   boolr(   rZ   r   r   r   r   r   ,   s
    r   )__doc__rM   rD   r   argparser   r   	importlibr   r?   rI   r   	packagingr   utilsr    r
   r   r   r   r   r   r   <module>   s   	