o
    ϯi                  	   @   s   d dl mZ d dlmZmZ d dlmZ ddejdeddfdd	Z	ddejdeddfddZ
		ddejdededdfddZdS )    N)c2_msra_fillc2_xavier_fill)#SpatioTemporalClsPositionalEncoding{Gz?modelfc_init_stdreturnc                 C   s   |   D ]g}t|tjtjfr	 t| n/t|tj jjrD|jdur9t	|dr2|j
r2|jjd n|jjd |jdurD|jj  t|tjrkt	|drW|jrWt| n	|jjjd|d |jdurk|jj  q| S )a  
    Performs ResNet style weight initialization. That is, recursively initialize the
    given model in the following way for each type:
        Conv - Follow the initialization of kaiming_normal:
            https://pytorch.org/docs/stable/_modules/torch/nn/init.html#kaiming_normal_
        BatchNorm - Set weight and bias of last BatchNorm at every residual bottleneck
            to 0.
        Linear - Set weight to 0 mean Gaussian with std deviation fc_init_std and bias
            to 0.
    Args:
        model (nn.Module): Model to be initialized.
        fc_init_std (float): the expected standard deviation for fully-connected layer.
    Nblock_final_bng              ?xavier_init)meanstd)modules
isinstancennConv2dConv3dr   	batchnorm	_NormBaseweighthasattrr	   datafill_biaszero_Linearr   r   normal_)r   r   m r   S/home/ubuntu/.local/lib/python3.10/site-packages/pytorchvideo/models/weight_init.py_init_resnet_weights   s&   




r    {Gz?trunc_normal_stdc                 C   s   |   D ]P}t|tjr)tjj|j|d t|tjr(|jdur(tj|jd qt|tj	r@tj|jd tj|jd qt|t
rT| D ]
}tjj||d qIqdS )z
    Weight initialization for vision transformers.

    Args:
        model (nn.Module): Model to be initialized.
        trunc_normal_std (float): the expected standard deviation for fully-connected
            layer and ClsPositionalEncoding.
    )r   Nr   r
   )r   r   r   r   inittrunc_normal_r   r   	constant_	LayerNormr   
parameters)r   r"   r   weightsr   r   r   _init_vit_weights2   s   	
r)   resnetinit_stdstylec                 C   s4   |dv sJ |dkrt | |S |dkrt| |S t)aS  
    Performs weight initialization. Options include ResNet style weight initialization
    and transformer style weight initialization.

    Args:
        model (nn.Module): Model to be initialized.
        init_std (float): The expected standard deviation for initialization.
        style (str): Options include "resnet" and "vit".
    )r*   vitr*   r-   )r    r)   NotImplementedError)r   r+   r,   r   r   r   init_net_weightsH   s   

r/   )r   )r!   )r   r*   )torch.nnr   fvcore.nn.weight_initr   r   pytorchvideo.layersr   Modulefloatr    r)   strr/   r   r   r   r   <module>   s    *