o
    xi                     @   s   d dl Z d dlmZmZmZ d dlmZ d dlmZm	Z	 eG dd dZ
eG dd dZeG d	d
 d
ZdeeB deddfddZdeeB de
deddfddZe	dZdeeB deeef fddZdedeeef dB dedefddZdS )    N)asdict	dataclassfields)Path)AnyTypeVarc                   @   sf  e Zd ZU dZeed< dZeed< dZeed< dZeed< d	Z	eed
< dZ
eed< dZedB ed< dZedB ed< dZeed< dZeed< dZeed< dZeed< dZeed< dZeed< dZeed< dZeed< dZeed < dZeed!< dZeed"< d#Zeed$< d%Zeed&< d'Zeed(< ed)efd*d+Zed)efd,d-Z ed)efd.d/Z!ed)efd0d1Z"dS )2ModelConfig   
latent_dim   latent_patch_sizei   	model_dim   
num_layers   	num_headsg      @	mlp_ratiog@Ntext_mlp_ratiospeaker_mlp_ratio        dropouti  text_vocab_sizezsbintuitions/sarashina2.2-0.5btext_tokenizer_repoTtext_add_bosi   text_dim   text_layers
   
text_headsspeaker_dimspeaker_layersspeaker_headsspeaker_patch_sizei   timestep_embed_dim   
adaln_rankgh㈵>norm_epsreturnc                 C      | j | j S N)r
   r   self r,   ./home/ubuntu/Irodori-TTS/irodori_tts/config.pypatched_latent_dim       zModelConfig.patched_latent_dimc                 C   r(   r)   )r.   r"   r*   r,   r,   r-   speaker_patched_latent_dim$   r/   z&ModelConfig.speaker_patched_latent_dimc                 C      | j d u r| jS t| j S r)   )r   r   floatr*   r,   r,   r-   text_mlp_ratio_resolved(      

z#ModelConfig.text_mlp_ratio_resolvedc                 C   r1   r)   )r   r   r2   r*   r,   r,   r-   speaker_mlp_ratio_resolved.   r4   z&ModelConfig.speaker_mlp_ratio_resolved)#__name__
__module____qualname__r
   int__annotations__r   r   r   r   r   r2   r   r   r   r   r   strr   boolr   r   r   r   r    r!   r"   r#   r%   r&   propertyr.   r0   r3   r5   r,   r,   r,   r-   r      s>   
 r   c                   @   sf  e Zd ZU dZeed< dZeed< dZeed< dZ	eed< d	Z
eed
< dZeed< d	Zeed< d	Zeed< dZeed< dZeed< dZeed< dZeed< dZeed< dZeed< dZeed< dZeed< dZeed< d Zeed!< d Zeed"< d#Zeed$< d%Zeed&< d'Zeed(< d)Zeed*< d Zeed+< d,Z eed-< d Z!eed.< d/Z"eed0< d	Z#eed1< d2Z$eed3< d4Z%eed5< d6Z&eed7< d8Z'eed9< d#Z(eed:< d#Z)eed;< d<Z*eed=< d<Z+ed>B ed?< d/Z,eed@< d,Z-eedA< d4Z.eedB< d/Z/eedC< dDZ0eedE< dZ1eedF< d	Z2eedG< dHZ3eedI< d>Z4ed>B edJ< d>Z5ed>B edK< dLZ6eedM< d	Z7eedN< d Z8eedO< d>S )PTrainConfig manifest_pathoutputs
output_dir   
batch_size   num_workersFdataloader_persistent_workersdataloader_prefetch_factor
allow_tf32compile_modelg-C6?learning_rateg{Gz?weight_decaymuon	optimizer?
adam_beta1g+?
adam_beta2g:0yE>adam_epsgffffff?muon_momentummatch_rms_adamwmuon_adjust_lr_fnnonelr_schedulerr   warmup_stepsstable_stepsg?min_lr_scalei@ 	max_stepsd   	log_everyi  
save_everycheckpoint_best_nr   valid_ratiovalid_everyTprogressprogress_all_ranksbf16	precision      ?grad_clip_normr   gradient_accumulation_stepsr$   max_text_lentext_condition_dropoutspeaker_condition_dropouti  max_latent_stepsNfixed_target_latent_stepsfixed_target_full_masktimestep_logit_meantimestep_logit_stdtimestep_stratifiedgMbP?timestep_mintimestep_maxwandb_enabledzIrodori-TTSwandb_projectwandb_entitywandb_run_nameonline
wandb_modeddp_find_unused_parametersseed)9r6   r7   r8   r@   r;   r:   rB   rD   r9   rF   rG   r<   rH   rI   rJ   rK   r2   rL   rN   rP   rQ   rR   rS   rU   rW   rX   rY   rZ   r[   r]   r^   r_   r`   ra   rb   rc   re   rg   rh   ri   rj   rk   rl   rm   rn   ro   rp   rq   rr   rs   rt   ru   rv   rw   ry   rz   r{   r,   r,   r,   r-   r>   5   sd   
 r>   c                   @   s   e Zd ZU dZeed< dZeed< dZeed< dZ	e
ed< d	Zed	B ed
< dZeed< dZeed< d	Zed	B ed< d	Zed	B ed< d	Zed	B ed< dZeed< d	Zed	B ed< dZed	B ed< d	Zed	B ed< d	Zed	B ed< dZeed< d	S )SamplingConfig(   	num_stepsg      @cfg_scale_textg      @cfg_scale_speakerindependentcfg_guidance_modeN	cfg_scaleg      ?	cfg_min_trf   	cfg_max_ttruncation_factor	rescale_krescale_sigmaTcontext_kv_cachespeaker_kv_scalerO   speaker_kv_min_tspeaker_kv_max_layerssequence_lengthr   r{   )r6   r7   r8   r~   r9   r:   r   r2   r   r   r;   r   r   r   r   r   r   r   r<   r   r   r   r   r{   r,   r,   r,   r-   r|   j   s"   
 r|   pathpayloadr'   c                 C   s4   t | } | jjddd | jtj|dddd d S )NT)parentsexist_okrE   )indentutf-8encoding)r   parentmkdir
write_textjsondumps)r   r   r,   r,   r-   	save_json   s   r   	model_cfg	train_cfgc                 C   s   t | t|t|d d S )N)modeltrain)r   r   )r   r   r   r,   r,   r-   dump_configs   s   r   Tc              
   C   sn   zddl }W n ty } ztd|d}~ww |t| jdd}|du r)i S t|ts5td|  |S )zH
    Load experiment config YAML. Returns {} for an empty document.
    r   NzKPyYAML is required for --config support. Install with `pip install pyyaml`.r   r   zConfig root must be a mapping: )	yamlImportErrorRuntimeError	safe_loadr   	read_text
isinstancedict
ValueError)r   r   excr   r,   r,   r-   load_experiment_yaml   s    
r   base	overridessectionc                 C   s   |du r| S t |tstd| ddd t| D }tt|| }|r0td| d| t| }|| t| di |S )	zP
    Merge mapping overrides into a dataclass instance with key validation.
    NzConfig section 'z' must be a mapping.c                 S   s   h | ]}|j qS r,   )name).0fr,   r,   r-   	<setcomp>   s    z,merge_dataclass_overrides.<locals>.<setcomp>zUnknown keys in 'z
' config: r,   )	r   r   r   r   sortedsetr   updatetype)r   r   r   allowedunknownmergedr,   r,   r-   merge_dataclass_overrides   s   

r   )r   dataclassesr   r   r   pathlibr   typingr   r   r   r>   r|   r;   r   r   r   r   r   r   r,   r,   r,   r-   <module>   s    -4*