a
    d                     @   s   d dl mZ d dlZd dlZd dlmZ d dlmZ i dfddZdd	 Z	e	d
Z
e	dZe	dZe	dZdd ZddgdfddZdd ZdS )    )repeatN)nn)FrozenBatchNorm2d c           	      C   s   | }d}|r||v }|rt | tjjjtjjjfrt| j}| j|_| j|_| jrz| j	j
  |j	_
| jj
  |j_
| jj
|j_
| jj
|j_
| j|_nH|  D ]>\}}|rd||gn|}t|||}||ur||| q|S )a  
    Converts all `BatchNorm2d` and `SyncBatchNorm` layers of provided module into `FrozenBatchNorm2d`. If `module` is
    itself an instance of either `BatchNorm2d` or `SyncBatchNorm`, it is converted into `FrozenBatchNorm2d` and
    returned. Otherwise, the module is walked recursively and submodules are converted in place.

    Args:
        module (torch.nn.Module): Any PyTorch module.
        module_match (dict): Dictionary of full module names to freeze (all if empty)
        name (str): Full module name (prefix)

    Returns:
        torch.nn.Module: Resulting module

    Inspired by https://github.com/pytorch/pytorch/blob/a5895f85be0f10212791145bfedc0261d364f103/torch/nn/modules/batchnorm.py#L762
    T.)
isinstancer   modulesZ	batchnormZBatchNorm2dZSyncBatchNormr   Znum_featuresZaffineweightdataclonedetachbiasZrunning_meanZrunning_varZepsnamed_childrenjoinfreeze_batch_norm_2dZ
add_module)	moduleZmodule_matchnameresZis_matchZ
child_namechildZfull_child_name	new_child r   X/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/open_clip/utils.pyr   	   s(    

r   c                    s    fdd}|S )Nc                    s    t | tjjr| S tt|  S N)r   collectionsabcIterabletupler   )xnr   r   parse2   s    z_ntuple.<locals>.parser   )r   r    r   r   r   _ntuple1   s    r!               c                 C   s   t | |S r   )r!   )r   r   r   r   r   <lambda>=       r&   Zc_fcZc_projTc                 C   s   |   D ]\}}tt| dkr2t|||| t|tjjr||v r| j	| }||j
|j|jd u| j	|< |r| j	| jj|jj | j	| jd ur| j	| jj|j q| S )Nr   )r   lenlistchildrenreplace_linearr   torchr   ZLinearZ_modulesZin_featuresZout_featuresr   r	   r
   Zcopy_)modelZlinear_replacementZinclude_modulesZcopy_weightsr   r   Z
old_moduler   r   r   r+   A   s    

r+   c                 C   s2   |   D ]$}t|dr|jj}|  ||_qd S )Nprepare_for_eval)r   hasattrr	   Zdtyper.   int8_original_dtype)r-   mr0   r   r   r   $convert_int8_model_to_inference_modeT   s
    
r2   )	itertoolsr   collections.abcr   r,   r   Ztorchvision.ops.miscr   r   r!   Z	to_1tupleZ	to_2tupleZ	to_3tupleZ	to_4tupleZ	to_ntupler+   r2   r   r   r   r   <module>   s   (