o
    ϯi2                     @   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	Z	d dl
Z
d dlZd dlZi dg ddg dd	d
dgdg ddd
dgdd
dgdd
dgdd
dgdd
dgdd
dgdg ddg ddd
dgdd
dgdd
dgdd
dgdd
dgi dd
dgdd
dgdd
dgdd
dgdd
dgd d
dgd!g dd"g dd#d
dgd$d
dgd%d
dgd&d
dgd'd
dgd(g dd)d
dgd*d
dgd+d
dgd
dgd
dgd
dgd
dgd
dgd
dgd
dgd
dgd
dgd
dgd,
Zi d-fd.d/Zd0d1 Z	2	dVd3d4ZdWd5d6Zd7d8 Zd9d: Zd;d< Zd=d> Zd?d@ Zi fdAdBZdCdD ZdEdF ZdGdH ZdXdJdKZdLdM Zd dNlmZmZ d dOlmZm Z m!Z! d dPl"m#Z# dQdR Z$d dSlm%Z% dTdU Z&dS )Y    N)nn)FrozenBatchNorm2d)tqdm	audiocaps)trainvalidtestaudioset)balanced_trainunbalanced_trainevalBBCSoundEffectsr   r   Clotho)r   r   r   free_to_use_soundsparamount_motionsonniss_game_effectswesoundeffectsMACS	freesoundFSD50Kfsd50k_class_labelesc50ESC50_1ESC50_2ESC50_3ESC50_4ESC50_5
audiostockfreesound_no_overlap_noesc50epidemic_sound_effectsVGGSoundurbansound8k_class_labelaudioset_t5audioset_t5_debiasedepidemic_sound_effects_t5"epidemic_sound_effects_t5_debiased	WavText5Kesc50_no_overlapusd8k_no_overlapfsd50k_200_class_labelfma_fullGeniusJamendo)
juno
CMU_ArcticravdesszEuroparl-stcommon_voiceJamendo_16bitgenius_16bit_128
juno_16bitfma_full_16bit_128GTZAN c           	      C   s   | }d}|r
||v }|rOt | tjjjtjjjfrOt| j}| j|_| j|_| jr=| j	j
  |j	_
| jj
  |j_
| jj
|j_
| jj
|j_
| j|_|S |  D ]\}}|r`d||gn|}t|||}||urr||| qS|S )a  
    Converts all `BatchNorm2d` and `SyncBatchNorm` layers of provided module into `FrozenBatchNorm2d`. If `module` is
    itself an instance of either `BatchNorm2d` or `SyncBatchNorm`, it is converted into `FrozenBatchNorm2d` and
    returned. Otherwise, the module is walked recursively and submodules are converted in place.

    Args:
        module (torch.nn.Module): Any PyTorch module.
        module_match (dict): Dictionary of full module names to freeze (all if empty)
        name (str): Full module name (prefix)

    Returns:
        torch.nn.Module: Resulting module

    Inspired by https://github.com/pytorch/pytorch/blob/a5895f85be0f10212791145bfedc0261d364f103/torch/nn/modules/batchnorm.py#L762
    T.)
isinstancer   modules	batchnormBatchNorm2dSyncBatchNormr   num_featuresaffineweightdataclonedetachbiasrunning_meanrunning_varepsnamed_childrenjoinfreeze_batch_norm_2d
add_module)	modulemodule_matchnameresis_match
child_namechildfull_child_name	new_child rT   P/home/ubuntu/.local/lib/python3.10/site-packages/laion_clap/clap_module/utils.pyrI   >   s0   
rI   c                 C   s   |t |  v rdS dS )z!
    Check if dataset exists
    TF)dataset_split)dataset_namedataset_typerT   rT   rU   existg   s   rY      c                 C   s*  g }| D ]}|dur||v rt | }n|}|D ]w}	g }
|r8| d| d|	 d}tj|s7d| d|	 d}n	d| d|	 d}tj|sHqtt|d}| D ]$}|ri|
| d| d|	 d|  qT|
d| d|	 d| d qT|dkrt	
|
t|t|
 }
||
 qqt|g S )	z1
    Get tar path from dataset name and type
    N/z/sizes.jsonz./json_files/rzIpipe:aws s3 --cli-connect-timeout 0 cp s3://s-laion-audio/webdataset_tar/z -rZ   )rV   ospathexistsjsonloadopenkeysappendrandomsampleintlensum)dataset_namesdataset_typesislocaldataset_path
proportionfull_datasetoutputncurrent_dataset_typesstmpsizefilepath_sizeskrT   rT   rU   get_tar_path_from_dataset_nameq   s6   
"
rx   c                    s   t ttfrt fddttD g S t trlt}| W d   n1 s1w   Y   rFfddttD nfddttD dkrjt	d
 ttt S dS )z$
    Get tar path from txt path
    c                    s   g | ]}t |  d qS ))rl   rn   )get_tar_path_from_txts.0i)rl   rn   txt_pathrT   rU   
<listcomp>   s    
z*get_tar_path_from_txts.<locals>.<listcomp>Nc                    &   g | ]} |  d d ddqS )
r   z"pipe:aws s3 cp s3://s-laion-audio/z/mnt/audio_clip/splitreplacerz   linesrT   rU   r~      s    c                    r   )r   r   z.tarz.tar -r   rz   r   rT   rU   r~      s    rZ   z#Sampling tars with proportion of {})r8   listtupleri   rangerh   strrb   	readlinesprintformatre   rf   rg   )r}   rl   rn   frT   )rl   r   rn   r}   rU   ry      s.   

	





ry   c                    s&    fddt |D }t|dS )Nc                    s    g | ]}t j  d d qS )rZ   r   )npre   betar{   _mixup_alpharT   rU   r~      s    z"get_mix_lambda.<locals>.<listcomp>float32)r   r   arrayastype)r   
batch_sizemixup_lambdasrT   r   rU   get_mix_lambda   s   
r   c                 C   s<   |  dd| tj| dgd ddd|    dd}|S )zw
    Args:
      x: (batch_size , ...)
      mixup_lambda: (batch_size,)
    Returns:
      out: (batch_size, ...)
    r   )dimsrZ   )	transposetorchflip)xmixup_lambdaoutrT   rT   rU   do_mixup   s   	r   c                 C   sJ   | j \}}}| dddddddf dd|d}|||| |}|S )a$  Interpolate data in time domain. This is used to compensate the
    resolution reduction in downsampling of a CNN.

    Args:
      x: (batch_size, time_steps, classes_num)
      ratio: int, ratio to interpolate
    Returns:
      upsampled: (batch_size, time_steps * ratio, classes_num)
    NrZ   )shaperepeatreshape)r   ratior   
time_stepsclasses_num	upsampledrT   rT   rU   interpolate   s   
(r   c                 C   sF   | ddddddf  d|| jd  d}	 tj| |fdd}dS )a3  Pad framewise_output to the same length as input frames. The pad value
    is the same as the value of the last frame.
    Args:
      framewise_output: (batch_size, frames_num, classes_num)
      frames_num: int, number of frames to pad
    Outputs:
      output: (batch_size, frames_num, classes_num)
    Nr   rZ   )dim)r   r   r   cat)framewise_output
frames_numpadrp   rT   rT   rU   pad_framewise_output   s   	r   c                 C   s   t d dd t|D }t| d,}ttt|d D ]}t|d | d }|D ]	}|| 	| q.qW d    n1 sCw   Y  t
| t|| t d d S )NzLoad Data...............c                 S   s   g | ]}g qS rT   rT   r   rT   rT   rU   r~      s    zprocess_ipc.<locals>.<listcomp>r\   targetr   z Load Data Succeed...............)logginginfor   h5pyFiler   rh   r   whererd   r   save)
index_pathr   filenameipcr   r|   t_classtrT   rT   rU   process_ipc   s   
r   c                 C   s(   |  d}||d t|d i |S )Nz: r   rZ   )r   updatefloat)rs   o_sprT   rT   rU   save_to_dict  s   
r   c                 C   s  t | }| }W d   n1 sw   Y  i }i }g }g }tt|D ]}d|| v rd|| v rd|| v r|| dd }t|dd d	d }	|dd d	d
 ddt|dd d	d i}
td
t|dD ]}t|d| |
}
qy|
||	< q&d|| v rt|| dd
 d }	t|| dd dd }|	| |	|	 q&tt|D ]}|| || d||< q||fS )z1
    Output dictionary from out.txt log file
    Nz| INFO |z
Eval Epochval_losszEval Epoch: r   	r    rZ   :r6   zTrain EpochzTrain Epoch: zLoss: z ()	num_epoch
train_loss)
rb   r   r   rh   r   rg   r   r   r   rd   )r}   r   r   val_data
train_datatrain_lossestrain_losses_epochr|   liner   dlossrT   rT   rU   get_data_from_log	  sB   

" 

r   c                 C   s   dd l }zddlm} W n   td ddlm} Y t|d}|j| ||jd W d    n1 s5w   Y  t|d}||}W d    n1 sOw   Y  || |ddi ksaJ d	d S )
Nr   )DeepDiffzpip install deepdiffwb)protocolrbT)ignore_string_casez0there is something wrong with the saving process)	pickledeepdiffr   r]   systemrb   dumpHIGHEST_PROTOCOLra   )objr   r   r   filezrT   rT   rU   save_p/  s    
r   c                 C   B   dd l }t| d}||}W d    |S 1 sw   Y  |S )Nr   r   )r   rb   ra   )r   r   r   r   rT   rT   rU   load_pA  s   
r   	data.jsonc                 C   sD   dd l }t|d}|| | W d    d S 1 sw   Y  d S )Nr   w)r`   rb   r   )r@   rM   r`   fprT   rT   rU   	save_jsonI  s   
r   c                 C   r   )Nr   r\   )r`   rb   ra   )rM   r`   r   r@   rT   rT   rU   	load_jsonP  s   
r   )ProcessManager)r   ValueArray)c_wcharc                 C   s   d }| d urBt | jdv rt| }|S t | jdv r"t| }|S t | jdv r1t| }|S t | jdv rBdd l}|| }|S )N)z.pklz.pickle)z.jsonz.txt)z.npyz.npz)z.csvr   )	pathlibPathsuffixr   r   r   ra   pandasread_csv)r^   r   pdrT   rT   rU   load_class_label\  s   

r   )optimc                 C   sl   |  dkrtj| |||d}|S |  dkr!tj| ||d}|S |  dkr2tj| |||d}|S td)Nadamw)lrbetasrF   sgd)r   momentumadamzoptimizer name is not correct)lowerr   AdamWSGDAdam
ValueError)paramsr   r   rF   r   optimizer_name	optimizerrT   rT   rU   get_optimizerv  s    	r  )rZ   N)rZ   )r   )'numpyr   r   r   torchvision.ops.miscr   r   r   r   re   r`   r]   r   rV   rI   rY   rx   ry   r   r   r   r   r   r   r   r   r   r   r   multiprocessingr   r   r   r   ctypesr   r   r   r  rT   rT   rT   rU   <module>   s    	
 !"#0)

)#&
