o
    @T¼iM  ã                   @   s|   d Z ddlmZ ddlZddlmZ dejdejdefdd„Z	dej
dejdefd	d
„Zdeejejf dejfdd„ZdS )zUtility functions for pruning.é    )ÚUnionNÚlayerÚindexÚdimc                 C   óŽ   |dkrd}t |ƒ| _n|dkrd}t |ƒ| _nt‚t | j ||¡ ¡  	¡ ¡| _| j
durC|dkrEt | j
 d|¡ ¡  	¡ ¡| _
dS dS dS )zPrune linear layer in place.Úinputé   Úoutputr   N)ÚlenÚin_featuresÚout_featuresÚ
ValueErrorÚnnÚ	ParameterÚweightÚindex_selectÚcloneÚdetachÚbias©r   r   r   © r   úR/home/ubuntu/.local/lib/python3.10/site-packages/linacodec/module/pruning_utils.pyÚprune_linear_layer	   ó   "ÿr   c                 C   r   )zPrune conv1d in place.r   r   r	   r   N)r
   Úin_channelsÚout_channelsr   r   r   r   r   r   r   r   r   r   r   r   Úprune_conv1d_layer   r   r   Ú	layernormc                 C   s€   t  | j d|¡ ¡  ¡ ¡| _t  | j d|¡ ¡  ¡ ¡| _t| t jƒr,t	|ƒf| _
dS t| t jƒr>t	|ƒ| _t	|ƒ| _dS dS )z(Prune layer norm or group norm in place.r   N)r   r   r   r   r   r   r   Ú
isinstanceÚ	LayerNormr
   Únormalized_shapeÚ	GroupNormÚ
num_groupsÚnum_channels)r   r   r   r   r   Úprune_layer_norm+   s   
þr$   )Ú__doc__Útypingr   ÚtorchÚtorch.nnr   ÚLinearÚ
LongTensorÚstrr   ÚConv1dr   r   r!   r$   r   r   r   r   Ú<module>   s    $