o
    h                     @   s   d dl Z d dlZd dlmZmZmZ e  edZddede	de	fdd	Z
defd
dZdd Z			dddZdd ZedkrGe  dS dS )    N)UnivNetConfigUnivNetModelloggingztransformers.models.univnet config
old_prefix
new_prefixc                 C   s  i }| d|| d< | d|| d< | d|| d< t | jD ]b}| d| d|| d	| d
< | d| d|| d	| d< | d| d|| d	| d< | d| d|| d	| d< | d| d|| d	| d< | d| d|| d	| d< q%| d|| d< | d|| d< | d|| d< | d|| d< | d|| d< | d|| d< |S )Nz.input_conv.weight_gz.input_conv.0.weight_gz.input_conv.weight_vz.input_conv.0.weight_vz.input_conv.biasz.input_conv.0.bias.resblocks.z.conv1.weight_gz.residual_convs..1.weight_gz.conv1.weight_v.1.weight_vz.conv1.bias.1.biasz.conv2.weight_gz.3.weight_gz.conv2.weight_vz.3.weight_vz.conv2.biasz.3.biasz.kernel_conv.weight_gz.kernel_conv.weight_vz.kernel_conv.biasz.bias_conv.weight_gz.bias_conv.weight_vz.bias_conv.bias)rangekernel_predictor_num_blocks)r   r   r   mappingi r   a/var/www/html/ai/venv/lib/python3.10/site-packages/transformers/models/univnet/convert_univnet.py get_kernel_predictor_key_mapping   s$        "r   c              	   C   s(  i }t t| jD ]|}d| d|d| d< d| d|d| d< d| d|d| d< t| d| d	d| d	d
}|| t t| j| D ]8}d| d| d|d| d| d< d| d| d|d| d| d< d| d| d|d| d| d< qLq	d|d< d|d< d|d< |S )Nz
resblocks.z.convt_pre.weight_gz
res_stack.z.convt_pre.1.weight_gz.convt_pre.weight_vz.convt_pre.1.weight_vz.convt_pre.biasz.convt_pre.1.biasz.kernel_predictor)r   r   r	   z.conv.weight_gz.conv_blocks.r
   z.conv.weight_vr   z
.conv.biasr   zconv_post.weight_gzconv_post.1.weight_gzconv_post.weight_vzconv_post.1.weight_vzconv_post.biaszconv_post.1.bias)r   lenresblock_stride_sizesr   updateresblock_dilation_sizes)r   r   r   kernel_predictor_mappingjr   r   r   get_key_mapping8   s$   
$$&r   c                 C   sF   i }|   D ]\}}||v rq||v r|| }|||< q|||< q|S )N)items)
state_dictkeys_to_modifykeys_to_removemodel_state_dictkeyvaluenew_keyr   r   r   rename_state_dictX   s   

r#   Fc                 C   s   t j| dd}|d }|d urt|}nt }t|}t }	t|||	}
t|}|  |	|
 |
  |j||d |rJtd || d S d S )Ncpu)map_locationmodel_g)safe_serializationzPushing to the hub...)torchloadr   from_pretrainedr   setr#   r   apply_weight_normload_state_dictremove_weight_normsave_pretrainedprintpush_to_hub)checkpoint_pathpytorch_dump_folder_pathconfig_pathrepo_idr'   model_state_dict_baser   r   r   r   hf_state_dictmodelr   r   r   convert_univnet_checkpointf   s"   
r9   c                  C   s   t  } | jddd tdd | jdd tdd | jddd td	d | jd
d tdd | jdddd |  }t|j|j|j|j	|j
 d S )Nz--checkpoint_pathTzPath to original checkpoint)requireddefaulttypehelpz--config_pathz*Path to hf config.json of model to convert)r;   r<   r=   z--pytorch_dump_folder_pathz!Path to the output PyTorch model.z--push_to_hubu4   Where to upload the converted model on the 🤗 hub.z--safe_serialization
store_truez.Whether to save the model using `safetensors`.)actionr=   )argparseArgumentParseradd_argumentstr
parse_argsr9   r2   r3   r4   r1   r'   )parserargsr   r   r   main   s(   
rG   __main__)r   r   )NNF)r@   r(   transformersr   r   r   set_verbosity_info
get_loggerloggerrC   r   r   r#   r9   rG   __name__r   r   r   r   <module>   s    
 
"
