o
    㥵i                     @   s   d dl Z d dlZd dl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mZ eedd	Zd
e
ddfddZdedefddZdededefddZdefddZdS )    N)	find_spec)Callable
DictConfig   )RankedLogger)enforce_tagsprint_config_treeT)rank_zero_onlycfgreturnc                 C   s   |  dstd dS | j drtd td | j dr-td t| d	d
 | j drAtd t| d	d	d dS dS )zApplies optional utilities before the task is started.

    Utilities:
    - Ignoring python warnings
    - Setting tags from command line
    - Rich config printing
    extrasz*Extras config not found! <cfg.extras=null>Nignore_warningsz<Disabling python warnings! <cfg.extras.ignore_warnings=True>ignorer   z.Enforcing tags! <cfg.extras.enforce_tags=True>T)save_to_fileprint_configz>Printing config tree with Rich! <cfg.extras.print_config=True>)resolver   )	getlogwarningr   infowarningsfilterwarningsr   r	   r    r   K/home/ubuntu/.local/lib/python3.10/site-packages/fish_speech/utils/utils.pyr      s   






r   	task_funcc                    s   dt f fdd}|S )aO  Optional decorator that controls the failure behavior when executing the task function.

    This wrapper can be used to:
    - make sure loggers are closed even if the task function raises an exception (prevents multirun failure)
    - save the exception to a `.log` file
    - mark the run as failed with a dedicated file in the `logs/` folder (so we can find and rerun it later)
    - etc. (adjust depending on your needs)

    Example:
    ```
    @utils.task_wrapper
    def train(cfg: DictConfig) -> Tuple[dict, dict]:

        ...

        return metric_dict, object_dict
    ```
    r   c              
      s   z@z	 | d\}}W n t y } ztd |d }~ww W td| jj  tdr=dd l}|jr=td |	  ||fS td| jj  tdr`dd l}|jratd |	  w w w )Nr    zOutput dir: wandbr   zClosing wandb!)
	Exceptionr   	exceptionr   pathsrun_dirr   r   runfinish)r   metric_dictobject_dictexr   r   r   r   wrapB   s.   



ztask_wrapper.<locals>.wrapr   )r   r)   r   r(   r   task_wrapper.   s   r*   r%   metric_namec                 C   sR   |s	t d dS || vrtd| d| |  }t d| d| d |S )z?Safely retrieves value of the metric logged in LightningModule.z7Metric name is None! Skipping metric value retrieval...Nz%Metric value not found! <metric_name=z>
Make sure metric name logged in LightningModule is correct!
Make sure `optimized_metric` name in `hparams_search` config is correct!zRetrieved metric value! <=>)r   r   r   item)r%   r+   metric_valuer   r   r   get_metric_valued   s   

r0   seedc                 C   s   | dk r|  } | dkrd} t |  tj |  t|  tj r.tj|  tj|  tjj	 r@dtjj	_
dtjj	_d S d S )Nr   l        TF)randomr1   nptorchmanual_seedcudais_availablemanual_seed_allbackendscudnndeterministic	benchmark)r1   r   r   r   set_seedx   s   



r=   )r2   r   importlib.utilr   typingr   numpyr3   r4   	omegaconfr   loggerr   
rich_utilsr   r	   __name__r   r   r*   dictstrfloatr0   intr=   r   r   r   r   <module>   s    6