o
    Tӵi                     @   sv   d dl Z d dlZd dlZd dlmZmZ d dlZd dlZd dlm	Z	 ddl
mZmZ ddlmZ G dd de	jZdS )	    N)ListTuple)nn   )EncoderDecoder)ResidualVectorQuantizec                       s   e Zd Zddg dddg dddd	g d
ddf fdd	Zdd Zdejdeejeej f fddZ	dejdeej fddZ
deej dejfddZedd Zedd Z  ZS )SNACiD  @   )   r      r   Ni   )r   r   r   r       i      )r         r   Tc                    s   t    || _|| _|| _|| _|| _|d u r |dt|  }|| _t	
|| _t||||d| _t|
| _|| _|	| _|
| _|| _t|||	|
d| _t||||||d| _d S )Nr   )	depthwiseattn_window_size)	input_dimcodebook_sizecodebook_dim
vq_strides)super__init__sampling_rateencoder_dimencoder_ratesdecoder_dimdecoder_rateslen
latent_dimnpprod
hop_lengthr   encodern_codebooksr   r   r   r   r   	quantizerr   decoder)selfr   r   r   r   r   r   r   r   r   r   noiser   	__class__ =/home/ubuntu/.local/lib/python3.10/site-packages/snac/snac.pyr      sF   

zSNAC.__init__c                 C   sX   |j d }t| jd | jpd}| j| }t|| | | }tj	|d|f}|S )Nr   r   )
shapemathlcmr   r   r"   ceilr   
functionalpad)r'   
audio_datalengthr0   pad_to	right_padr+   r+   r,   
preprocessB   s   

zSNAC.preprocessr4   returnc                 C   sJ   |j d }| |}| |}| |\}}| |}|dd |f |fS )Nr-   .)r.   r8   r#   r%   r&   )r'   r4   r5   zz_qcodes	audio_hatr+   r+   r,   forwardJ   s   



zSNAC.forwardc                 C   s&   |  |}| |}| |\}}|S N)r8   r#   r%   )r'   r4   r:   _r<   r+   r+   r,   encodeR   s   

zSNAC.encoder<   c                 C   s   | j |}| |}|S r?   )r%   
from_codesr&   )r'   r<   r;   r=   r+   r+   r,   decodeX   s   
zSNAC.decodec                 C   sF   t |d}t|}W d    n1 sw   Y  | di |}|S )Nrr+   )openjsonload)clsconfig_pathfconfigmodelr+   r+   r,   from_config]   s
   zSNAC.from_configc                 K   s   ddl m} tj|s-|d|dd|}|d|dd|}| |}tj|dd}n| tj|d}tjtj|ddd}|	| |
  |S )	Nr   )hf_hub_downloadzconfig.json)repo_idfilenamezpytorch_model.bincpu)map_locationr+   )huggingface_hubrN   ospathisdirrM   torchrG   joinload_state_dicteval)rH   rO   kwargsrN   rI   
model_pathrL   
state_dictr+   r+   r,   from_pretrainedd   s   

zSNAC.from_pretrained)__name__
__module____qualname__r   r8   rW   Tensorr   r   r>   rA   rC   classmethodrM   r^   __classcell__r+   r+   r)   r,   r	      s,    3$
r	   )rF   r/   rT   typingr   r   numpyr    rW   r   layersr   r   vqr   Moduler	   r+   r+   r+   r,   <module>   s    