o
    i                     @   sb   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 ddl	m
Z
 dd ZG d	d
 d
ejZdS )    N)nn   )WNConv1dEncoderBlock)Activation1d)activationsc                 C   s6   t | tjrtjj| jdd tj| jd d S d S )Ng{Gz?)stdr   )
isinstancer   Conv1dinittrunc_normal_weight	constant_biasm r   J/home/ubuntu/.local/lib/python3.10/site-packages/neucodec/codec_encoder.pyinit_weights   s   r   c                       sX   e Zd Zdg ddddddf fdd	Zd	d
 Zdd Zdd Zdd Zdd Z  Z	S )CodecEncoder0   )   r      r      )r      	   i      @   c                    s   t    t|| _|| _|| _|}td|dddg| _t	|D ]\}	}
|d9 }|  jt
||
|dg7  _q!tj| j | _ttj|ddd	t||dddg| _tj| j | _|   d S )
Nr      r   )kernel_sizepaddingr   )stride	dilationsT)alpha_logscale)
activation)super__init__npprod
hop_lengthngf	up_ratiosr   conv_blocks	enumerater   r   
Sequentialr   r   	SnakeBetaconv_final_blockreset_parameters)selfr*   r+   r"   
hidden_dimdepthheadspos_meb_dimd_modelir!   	__class__r   r   r&      s&   


zCodecEncoder.__init__c                 C   s&   |  |}| |}|ddd}|S )Nr   r   r   )r,   r0   permuter2   xr   r   r   forward6   s   

zCodecEncoder.forwardc                 C   s
   |  |S N)blockr<   r   r   r   	inference<   s   
zCodecEncoder.inferencec                 C      dd }|  | dS )z:Remove weight normalization module from all of the layers.c                 S   s*   z
t jj|  W d S  ty   Y d S w r?   )torchr   utilsremove_weight_norm
ValueErrorr   r   r   r   _remove_weight_normB   s
   z<CodecEncoder.remove_weight_norm.<locals>._remove_weight_normNapply)r2   rG   r   r   r   rE   ?   s   zCodecEncoder.remove_weight_normc                 C   rB   )z9Apply weight normalization module from all of the layers.c                 S   s"   t | tjrtjj|  d S d S r?   )r	   r   r
   rC   rD   weight_normr   r   r   r   _apply_weight_normM   s   z:CodecEncoder.apply_weight_norm.<locals>._apply_weight_normNrH   )r2   rK   r   r   r   apply_weight_normJ   s   zCodecEncoder.apply_weight_normc                 C   s   |  t d S r?   )rI   r   )r2   r   r   r   r1   S   s   zCodecEncoder.reset_parameters)
__name__
__module____qualname__r&   r>   rA   rE   rL   r1   __classcell__r   r   r9   r   r      s    $	r   )rC   numpyr'   r   moduler   r   alias_free_torchr    r   r   Moduler   r   r   r   r   <module>   s    