o
    %ݫi                     @   s@   d Z ddlZddlZddlmZ eeZ					dddZdS )	zK
Training utilities for pretrained models

Authors
* Artem Ploujnikov 2021
    N)
get_logger
pretrainercheckpointerc                    sJ  t  fdd||fD rtd| d| d | } | }|j|||d}|rtd|j t|j }	t|j	 }
|	|
@ }|D ]<}|j	| }t
j|sZtd| d	|j| }t
j|}t
j|spt
| t
j|r{t
| t|| qEd
}|S td|| | }ddd |D }td| d}|S )ao  
    Saves the necessary files for the pretrained model
    from the best checkpoint found. The goal of this function
    is to export the model for a Pretrainer

    Arguments
    ---------
    hparams: dict
        the hyperparameter file
    min_key: str
        Key to use for finding best checkpoint (lower is better).
        By default, passed to ``self.checkpointer.recover_if_possible()``.
    max_key: str
        Key to use for finding best checkpoint (higher is better).
        By default, passed to ``self.checkpointer.recover_if_possible()``.
    ckpt_predicate: callable
        a filter predicate to locate checkpoints
    pretrainer_key: str
        the key under which the pretrainer is stored
    checkpointer_key: str
        the key under which the checkpointer is stored

    Returns
    -------
    saved: bool
        Whether the save was successful
    c                 3   s    | ]}| vV  qd S )N ).0keyhparamsr   P/home/ubuntu/.local/lib/python3.10/site-packages/speechbrain/utils/pretrained.py	<genexpr>3   s    z&save_for_pretrained.<locals>.<genexpr>z.Incompatible hparams: a checkpointer with key zand a pretrainer with key z are required)min_keymax_keyckpt_predicatez)Saving checkpoint '%s' a pretrained modelzFile z! does not exist in the checkpointTzCUnable to find a matching checkpoint for min_key = %s, max_key = %s
c                 s   s"    | ]}|j  d |j V  qdS )z: N)pathmeta)r   
checkpointr   r   r
   r   Y   s
    
zAvailable checkpoints: %sF)any
ValueErrorfind_checkpointloggerinfor   set	loadableskeys
paramfilesosexistspathsdirnamemakedirsremoveshutilcopyfilelist_checkpointsjoin)r	   r   r   r   pretrainer_keycheckpointer_keyr   r   r   pretrainer_keyscheckpointer_keyskeys_to_saver   source_pathtarget_pathr   savedcheckpointscheckpoints_strr   r   r
   save_for_pretrained   sZ   #





r0   )NNNr   r   )__doc__r   r"   speechbrain.utils.loggerr   __name__r   r0   r   r   r   r
   <module>   s    