o
    óôjk$  ã                   @   sâ   d Z ddlZddlZddlmZ ddlZddlZdZg d¢Z	deej
 defdd	„Zd
ej
dej
dedej
fdd„Zd+dd„Zd,dd„Zd-dd„Zd.dd„Zdd „ Zd!d"„ Zd#d$„ Zd%ej
d&ejdej
fd'd(„ZG d)d*„ d*ƒZdS )/z"Unility functions for Transformer.é    N)ÚListéÿÿÿÿ)uD   You are a helpful assistant. è¯·ç”¨å¹¿ä¸œè¯�è¡¨è¾¾ã€‚<|endofprompt|>uD   You are a helpful assistant. è¯·ç”¨ä¸œåŒ—è¯�è¡¨è¾¾ã€‚<|endofprompt|>uD   You are a helpful assistant. è¯·ç”¨ç”˜è‚ƒè¯�è¡¨è¾¾ã€‚<|endofprompt|>uD   You are a helpful assistant. è¯·ç”¨è´µå·žè¯�è¡¨è¾¾ã€‚<|endofprompt|>uD   You are a helpful assistant. è¯·ç”¨æ²³å�—è¯�è¡¨è¾¾ã€‚<|endofprompt|>uD   You are a helpful assistant. è¯·ç”¨æ¹–åŒ—è¯�è¡¨è¾¾ã€‚<|endofprompt|>uD   You are a helpful assistant. è¯·ç”¨æ¹–å�—è¯�è¡¨è¾¾ã€‚<|endofprompt|>uD   You are a helpful assistant. è¯·ç”¨æ±Ÿè¥¿è¯�è¡¨è¾¾ã€‚<|endofprompt|>uD   You are a helpful assistant. è¯·ç”¨é—½å�—è¯�è¡¨è¾¾ã€‚<|endofprompt|>uD   You are a helpful assistant. è¯·ç”¨å®�å¤�è¯�è¡¨è¾¾ã€‚<|endofprompt|>uD   You are a helpful assistant. è¯·ç”¨å±±è¥¿è¯�è¡¨è¾¾ã€‚<|endofprompt|>uD   You are a helpful assistant. è¯·ç”¨é™•è¥¿è¯�è¡¨è¾¾ã€‚<|endofprompt|>uD   You are a helpful assistant. è¯·ç”¨å±±ä¸œè¯�è¡¨è¾¾ã€‚<|endofprompt|>uD   You are a helpful assistant. è¯·ç”¨ä¸Šæµ·è¯�è¡¨è¾¾ã€‚<|endofprompt|>uD   You are a helpful assistant. è¯·ç”¨å››å·�è¯�è¡¨è¾¾ã€‚<|endofprompt|>uD   You are a helpful assistant. è¯·ç”¨å¤©æ´¥è¯�è¡¨è¾¾ã€‚<|endofprompt|>uD   You are a helpful assistant. è¯·ç”¨äº‘å�—è¯�è¡¨è¾¾ã€‚<|endofprompt|>zXYou are a helpful assistant. Please say a sentence as loudly as possible.<|endofprompt|>zWYou are a helpful assistant. Please say a sentence in a very soft voice.<|endofprompt|>uV   You are a helpful assistant. è¯·ç”¨å°½å�¯èƒ½æ…¢åœ°è¯­é€Ÿè¯´ä¸€å�¥è¯�ã€‚<|endofprompt|>uV   You are a helpful assistant. è¯·ç”¨å°½å�¯èƒ½å¿«åœ°è¯­é€Ÿè¯´ä¸€å�¥è¯�ã€‚<|endofprompt|>uM   You are a helpful assistant. è¯·é�žå¸¸å¼€å¿ƒåœ°è¯´ä¸€å�¥è¯�ã€‚<|endofprompt|>uM   You are a helpful assistant. è¯·é�žå¸¸ä¼¤å¿ƒåœ°è¯´ä¸€å�¥è¯�ã€‚<|endofprompt|>uM   You are a helpful assistant. è¯·é�žå¸¸ç”Ÿæ°”åœ°è¯´ä¸€å�¥è¯�ã€‚<|endofprompt|>u_   You are a helpful assistant. æˆ‘æƒ³ä½“éªŒä¸€ä¸‹å°�çŒªä½©å¥‡é£Žæ ¼ï¼Œå�¯ä»¥å�—ï¼Ÿ<|endofprompt|>u\   You are a helpful assistant. ä½ å�¯ä»¥å°�è¯•ç”¨æœºå™¨äººçš„æ–¹å¼�è§£ç­”å�—ï¼Ÿ<|endofprompt|>ÚxsÚ	pad_valuec                 C   s  t dd„ | D ƒƒ}t| ƒ}| d j}|dkr&tj||| d j| d jd�}nA|dkr@tj||| d jd | d j| d jd�}n'|dkr`tj||| d jd | d jd | d j| d jd�}ntd|› �ƒ‚| 	|¡ t
|ƒD ]}| | ||d	t| | ƒ…f< qp|S )
a  Perform padding for the list of tensors.

    Args:
        xs (List): List of Tensors [(T_1, `*`), (T_2, `*`), ..., (T_B, `*`)].
        pad_value (float): Value for padding.

    Returns:
        Tensor: Padded tensor (B, Tmax, `*`).

    Examples:
        >>> x = [torch.ones(4), torch.ones(2), torch.ones(1)]
        >>> x
        [tensor([1., 1., 1., 1.]), tensor([1., 1.]), tensor([1.])]
        >>> pad_list(x, 0)
        tensor([[1., 1., 1., 1.],
                [1., 1., 0., 0.],
                [1., 0., 0., 0.]])

    c                 S   s   g | ]}t |ƒ‘qS © )Úlen)Ú.0Úitemr   r   úL/var/www/html/academy/tools/voices/clone/CosyVoice/cosyvoice/utils/common.pyÚ
<listcomp>L   s    zpad_list.<locals>.<listcomp>r   é   )ÚdtypeÚdeviceé   é   zUnsupported ndim: N)Úmaxr   ÚndimÚtorchÚzerosr   r   ÚshapeÚ
ValueErrorÚfill_Úrange)r   r   Úmax_lenZbatchsr   Zpad_resÚir   r   r
   Úpad_list8   s:   
ýüû
r   Úpad_outputsÚpad_targetsÚignore_labelÚreturnc                 C   s^   |   | d¡| d¡|  d¡¡ d¡}||k}t | |¡| |¡k¡}t |¡}||  ¡ S )a  Calculate accuracy.

    Args:
        pad_outputs (Tensor): Prediction tensors (B * Lmax, D).
        pad_targets (LongTensor): Target label tensors (B, Lmax).
        ignore_label (int): Ignore label id.

    Returns:
        torch.Tensor: Accuracy value (0.0 - 1.0).

    r   r   r   )ÚviewÚsizeÚargmaxr   ÚsumÚmasked_selectÚdetach)r   r   r   Zpad_predÚmaskÚ	numeratorÚdenominatorr   r   r
   Úth_accuracyi   s   ÿÿÿ
r)   r   c                 C   s   t | | | d ƒS )Nr   )Úint)Úkernel_sizeÚdilationr   r   r
   Úget_padding   ó   r-   ç        ç{®Gáz„?c                 C   s.   | j j}| d¡dkr| jj ||¡ d S d S )NÚConvr   )Ú	__class__Ú__name__ÚfindÚweightÚdataÚnormal_)ÚmÚmeanÚstdÚ	classnamer   r   r
   Úinit_weightsƒ   s   ÿr<   çš™™™™™é?é   é
   çš™™™™™¹?c           	      C   s`   t | ||d�}t || d … ¡ | j¡|k ¡  ¡ }||| kr.tdƒ | |< t| ||ƒ}|S )N)Útop_pÚtop_kÚinf)	Únucleus_samplingr   ÚtensorÚtor   r#   r	   ÚfloatÚrandom_sampling)	Úweighted_scoresÚdecoded_tokensÚsamplingrA   rB   Úwin_sizeZtau_rÚtop_idsZrep_numr   r   r
   Úras_samplingŠ   s   (rN   c           
      C   sº   g g }}d}| j dd�jddd�\}}tt|ƒƒD ]!}||k r;t|ƒ|k r;||| 7 }| || ¡ | || ¡ q t |¡ | ¡}tj|tjd� | j	¡}||j
ddd�  ¡ }	|	S )	Nr/   r   ©ÚdimT)Ú
descendingÚstable)r   r   ©Úreplacement)ÚsoftmaxÚsortr   r   Úappendr   rE   rF   Úlongr   Úmultinomialr	   )
rI   rA   rB   ÚprobÚindicesZcum_probZsorted_valueÚ
sorted_idxr   rM   r   r   r
   rD   “   s   
rD   c                 C   s   | j dd�jddd� ¡ }|S )Nr   rO   r   TrS   )rU   rY   r	   )rI   rJ   rK   rM   r   r   r
   rH   ¥   s   rH   c                 C   s’   | j }|  ¡ | ¡ } }t|jd d ƒ}| j t  d¡kr!|  ¡ } | dd |…f |d |…  |d| d …f ||d …   | dd |…f< |  |¡S )Nr   r   Úcpu.)r   r]   r*   r   r   ÚclonerF   )Zfade_in_melZfade_out_melÚwindowr   Úmel_overlap_lenr   r   r
   Úfade_in_outª   s   ÿ
ra   c                 C   s0   t  | ¡ tj  | ¡ t | ¡ tj | ¡ d S ©N)ÚrandomÚseedÚnpr   Úmanual_seedÚcudaÚmanual_seed_all)rd   r   r   r
   Úset_all_random_seedµ   s   

ri   r&   r   c                 C   sB   | j tjksJ ‚|tjtjtjfv sJ ‚|  |¡} d|  d } | S )Ng      ð?g    _ Â)r   r   ÚboolÚfloat32Úbfloat16Úfloat16rF   )r&   r   r   r   r
   Úmask_to_bias¼   s
   
rn   c                   @   s&   e Zd Zd
dd„Zdd„ Zdd„ Zd	S )ÚTrtContextWrapperr   úcuda:0c                 C   s~   t j|d�| _|| _t|ƒD ]#}| ¡ }tj tj 	|¡¡}|d us)J d 
|¡ƒ‚| j ||g¡ q| j ¡ du s=J dƒ‚d S )N)Úmaxsizez`failed to create trt context, maybe not enough CUDA memory, try reduce current trt concurrent {}Fzno avaialbe estimator context)ÚqueueÚQueueÚtrt_context_poolÚ
trt_enginer   Zcreate_execution_contextr   rg   ÚstreamÚStreamÚformatÚputÚempty)Úselfru   Útrt_concurrentr   Ú_Ztrt_contextZ
trt_streamr   r   r
   Ú__init__È   s   zTrtContextWrapper.__init__c                 C   s   | j  ¡ | jfS rb   )rt   Úgetru   )r{   r   r   r
   Úacquire_estimatorÒ   s   z#TrtContextWrapper.acquire_estimatorc                 C   s   | j  ||g¡ d S rb   )rt   ry   )r{   Úcontextrv   r   r   r
   Úrelease_estimatorÕ   r.   z#TrtContextWrapper.release_estimatorN)r   rp   )r3   Ú
__module__Ú__qualname__r~   r€   r‚   r   r   r   r
   ro   Ç   s    

ro   )r   )r/   r0   )r=   r>   r?   r@   )r=   r>   )Ú__doc__rr   rc   Útypingr   Únumpyre   r   Z	IGNORE_IDZinstruct_listÚTensorr*   r   r)   r-   r<   rN   rD   rH   ra   ri   r   rn   ro   r   r   r   r
   Ú<module>   s.   1ÿ
ÿ



	