o
    }oi3,                     @   s   d dl Z d dl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
 z
d dlmZ dZW n ey7   dZY nw ejje ddZe d	d
 ZG dd dZdS )    N)
DictConfig	OmegaConf)EncDecCTCModel)NeMoCookbookTFz/Export File Format library required to run test)reasonc                  C   s   dt i d} dddddddgdgdgd	d
dddd
gdd}dddg ddd}tt| t|t|d}t|d}|S )Nz>nemo.collections.asr.modules.AudioToMelSpectrogramPreprocessor)clsparamsz+nemo.collections.asr.modules.ConvASREncoder@   reluTi      g        F)
filtersrepeatkernelstridedilationdropoutresidual	separablesese_context_size)feat_in
activation	conv_maskjasperz+nemo.collections.asr.modules.ConvASRDecoder   ) abcdefghijklmnopqrstuvwxyz')r   num_classes
vocabulary)preprocessorencoderdecoder)cfg)dictr   r   )r:   r;   r<   modelConfigmodel_instance rA   J/home/ubuntu/.local/lib/python3.10/site-packages/tests/core/test_fileio.py	asr_model$   s<   %
rC   c                   @   sp   e Zd Zejjdd Zejjdd Zeejjdd Z	ejjdd Z
ejjd	d
 Zejjdd ZdS )
TestFileIOc                 C   s   t  ]}|j}|j|d tj|d}t|tsJ t|jj	dks%J |j
|j
ks-J |jjd jd jjj   }|jjd jd jjj   }t||rYJ W d   dS 1 sdw   Y  dS )z" Test makes sure that the second instance created with the same configuration (BUT NOT checkpoint)
        has different weights. )path2yaml_filer   r   N)tempfileNamedTemporaryFilenameto_config_filer   from_config_file
isinstancelenr<   r9   num_weightsr;   mconvconvweightdatadetachcpunumpynparray_equal)selfrC   fpyaml_filenamenext_instancew1w2rA   rA   rB   test_to_from_config_filel   s   
$$"z#TestFileIO.test_to_from_config_filec              	   C   s  t  v}|j}t  }|jd|jd |j|d W d   n1 s%w   Y  tj|d}t|jj	t|jj	ks>J |j
|j
ksFJ |jjd jd jjj   }|jjd jd jjj   }t||srJ W d   dS 1 s}w   Y  dS )z}" Test makes sure that the second instance created from the same configuration AND checkpoint 
        has the same weights. abcconfig_pathsrc	save_pathNrestore_pathr   )rF   rG   rH   register_artifactsave_tor   restore_fromrL   r<   r9   rM   r;   rN   rO   rP   rQ   rR   rS   rT   rU   rV   )rW   rC   rX   filenameartifact
asr_model2r[   r\   rA   rA   rB    test_save_restore_from_nemo_file   s   

$$"z+TestFileIO.test_save_restore_from_nemo_filec              	   C   s   t  k}|j}td t  }|jd|jd |j|d W d   n1 s*w   Y  td tt	 t
j|d}W d   n1 sJw   Y  td t
j|d}td |j|jksgJ W d   dS 1 srw   Y  dS )zT" Test makes sure that after encrypted save-restore the model has the same weights. test_keyr^   r_   rb   Nrd   )rF   rG   rH   r   set_encryption_keyrf   rg   pytestraisesPermissionErrorr   rh   rM   )rW   rC   rX   ri   rj   rk   
asr_model3rA   rA   rB   .test_eff_save_restore_from_nemo_file_encrypted   s    





"z9TestFileIO.test_eff_save_restore_from_nemo_file_encryptedc              	   C   s  t j|d}t  }tjdd}tjddd}|d W d   n1 s)w   Y  t j|j\}}|j	d	|jd
 |j
|d |j}	d|	j_t|	}
||
 |d tj||jd}t|jjt|jjksqJ |j|jksyJ |jjd jd jjj   }|jjd jd jjj   }t||sJ |jjjdksJ W d   dS 1 sw   Y  dS )z" Test makes sure that the second instance created from the same configuration AND checkpoint
        has the same weights.

        Args:
            tmpdir: fixture providing a temporary directory unique to the test invocation.
        zeff.nemoza+)moder3   F)rt   deletezmagic content 42Nr^   r_   rb   swishr   )re   override_config_path) ospathjoingetcwdrF   rG   writesplitrH   rf   rg   r=   r;   r   r   to_yamlseekr   rh   rL   r<   r9   rM   rN   rO   rP   rQ   rR   rS   rT   rU   rV   )rW   rC   tmpdirri   cwdconf_fprj   _artifact_filenamer=   yaml_cfgrk   r[   r\   rA   rA   rB   .test_save_restore_from_nemo_file_with_override   s,   	


$$"z9TestFileIO.test_save_restore_from_nemo_file_with_overridec                 C   s  t  }tj|d}|| ||| tj|d}tj|s&J tj	|d}t
|jjt
|jjks:J |j|jksBJ |jjd jd jj jd7  _|jjd jd jjj   }|jjd jd jjj   }t||r~J |t| |jjd jd jjj   }|jjd jd jjj   }t||sJ W d    d S 1 sw   Y  d S )Nasr.nemozmodel_weights.ckptrd   r         ?rF   TemporaryDirectoryrx   ry   rz   rg   extract_state_dict_fromexistsr   rh   rL   r<   r9   rM   r;   rN   rO   rP   rQ   rR   rS   rT   rU   rV   load_state_dicttorchload)rW   rC   ckpt_dir	nemo_file	ckpt_pathrk   r[   r\   rA   rA   rB   test_save_model_level_pt_ckpt   s$   

 $$$$"z(TestFileIO.test_save_model_level_pt_ckptc           
      C   s  t  }tj|d}|| |j||dd tj|d}tj|d}tj|d}tj|s6J tj|s>J tj|sFJ tj	|d}t
|jjt
|jjksZJ |j|jksbJ |jjd jd jj jd	7  _|jjd jd jjj   }|jjd jd jjj   }	t||	rJ |jt| |jjd jd jjj   }|jjd jd jjj   }	t||	sJ W d    d S 1 sw   Y  d S )
Nr   T)split_by_modulezencoder.ckptzdecoder.ckptzpreprocessor.ckptrd   r   r   r   )
rW   rC   r   r   encoder_pathdecoder_pathpreprocessor_pathrk   r[   r\   rA   rA   rB   test_save_module_level_pt_ckpt  s,   

 $$$$"z)TestFileIO.test_save_module_level_pt_ckptN)__name__
__module____qualname__ro   markunitr]   rl   requires_effrs   r   r   r   rA   rA   rA   rB   rD   k   s    


.
!rD   )rx   rF   rT   rU   ro   r   	omegaconfr   r   nemo.collections.asr.modelsr   eff.cookbooksr   _EFF_PRESENT_ImportErrorr   skipifr   fixturerC   rD   rA   rA   rA   rB   <module>   s"   
F