a
    áñh¨  ã                   @   sÖ   d dl Z d dlmZmZmZmZ d dlmZ d dlm	Z	 d dl
mZmZmZ d dlmZ ddlmZmZ g d¢Zd	d
ddœZG dd„ deƒZG dd„ deƒZG dd„ deƒZdd„ Zddd„Zddd„Zddd„ZdS ) é    N)Ú
BottleneckÚ
BasicBlockÚResNetÚ
model_urls)Úload_state_dict_from_url)Ú	QuantStubÚDeQuantStubÚfuse_modules)ÚOptionalé   )Ú_replace_reluÚquantize_model)ÚQuantizableResNetÚresnet18Úresnet50Úresnext101_32x8dzJhttps://download.pytorch.org/models/quantized/resnet18_fbgemm_16fa66dd.pthzJhttps://download.pytorch.org/models/quantized/resnet50_fbgemm_bf931d71.pthzQhttps://download.pytorch.org/models/quantized/resnext101_32x8_fbgemm_09835ccf.pth)Zresnet18_fbgemmZresnet50_fbgemmZresnext101_32x8d_fbgemmc                       s,   e Zd Z‡ fdd„Zdd„ Zdd„ Z‡  ZS )ÚQuantizableBasicBlockc                    s(   t t| ƒj|i |¤Ž tjj ¡ | _d S ©N)Úsuperr   Ú__init__ÚtorchÚnnÚ	quantizedÚFloatFunctionalÚadd_relu©ÚselfÚargsÚkwargs©Ú	__class__© úf/home/yuhw/wc/uboone-dl-vtx/venv/lib/python3.9/site-packages/torchvision/models/quantization/resnet.pyr      s    zQuantizableBasicBlock.__init__c                 C   s\   |}|   |¡}|  |¡}|  |¡}|  |¡}|  |¡}| jd urJ|  |¡}| j ||¡}|S r   )Úconv1Úbn1ÚreluÚconv2Úbn2Ú
downsampler   ©r   ÚxÚidentityÚoutr!   r!   r"   Úforward   s    






zQuantizableBasicBlock.forwardc                 C   s@   t jj| g d¢ddggdd� | jr<t jj| jddgdd� d S )N©r#   r$   r%   r&   r'   T©ZinplaceÚ0Ú1)r   Úquantizationr	   r(   ©r   r!   r!   r"   Ú
fuse_model-   s    ÿÿz QuantizableBasicBlock.fuse_model©Ú__name__Ú
__module__Ú__qualname__r   r-   r4   Ú__classcell__r!   r!   r   r"   r      s   r   c                       s,   e Zd Z‡ fdd„Zdd„ Zdd„ Z‡  ZS )ÚQuantizableBottleneckc                    sB   t t| ƒj|i |¤Ž tj ¡ | _tjdd�| _tjdd�| _	d S )NFr/   )
r   r:   r   r   r   r   Úskip_add_reluZReLUÚrelu1Úrelu2r   r   r!   r"   r   5   s    zQuantizableBottleneck.__init__c                 C   sz   |}|   |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}|  |¡}| jd urh|  |¡}| j	 
||¡}|S r   )r#   r$   r<   r&   r'   r=   Úconv3Úbn3r(   r;   r   r)   r!   r!   r"   r-   ;   s    









zQuantizableBottleneck.forwardc                 C   sB   t | g d¢g d¢ddggdd� | jr>tjj | jddgdd� d S )	N)r#   r$   r<   )r&   r'   r=   r>   r?   Tr/   r0   r1   )r	   r(   r   r2   r3   r!   r!   r"   r4   M   s    
þþz QuantizableBottleneck.fuse_modelr5   r!   r!   r   r"   r:   4   s   r:   c                       s,   e Zd Z‡ fdd„Zdd„ Zdd„ Z‡  ZS )r   c                    s2   t t| ƒj|i |¤Ž tj ¡ | _tj ¡ | _d S r   )	r   r   r   r   r2   r   Úquantr   Údequantr   r   r!   r"   r   W   s    zQuantizableResNet.__init__c                 C   s"   |   |¡}|  |¡}|  |¡}|S r   )r@   Z_forward_implrA   )r   r*   r!   r!   r"   r-   ]   s    


zQuantizableResNet.forwardc                 C   sD   t | g d¢dd� |  ¡ D ]$}t|ƒtks6t|ƒtkr| ¡  qdS )a  Fuse conv/bn/relu modules in resnet models

        Fuse conv+bn+relu/ Conv+relu/conv+Bn modules to prepare for quantization.
        Model is modified in place.  Note that this operation does not change numerics
        and the model after modification is in floating point
        r.   Tr/   N)r	   ÚmodulesÚtyper:   r   r4   )r   Úmr!   r!   r"   r4   f   s    zQuantizableResNet.fuse_modelr5   r!   r!   r   r"   r   U   s   	r   c                 K   sv   t ||fi |¤Ž}t|ƒ |r.d}t||ƒ n|dv s:J ‚|rr|rTt| d |  }	nt|  }	t|	|d�}
| |
¡ |S )NZfbgemm)TFÚ_)Úprogress)r   r   r   Úquant_model_urlsr   r   Zload_state_dict)ÚarchÚblockZlayersÚ
pretrainedrF   Úquantizer   ÚmodelÚbackendZ	model_urlZ
state_dictr!   r!   r"   Ú_resnett   s    ÿ
rN   FTc                 K   s   t dtg d¢| ||fi |¤ŽS )a!  ResNet-18 model from
    `"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>`_

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    r   )é   rO   rO   rO   )rN   r   ©rJ   rF   rK   r   r!   r!   r"   r   ‹   s
    ÿÿr   c                 K   s   t dtg d¢| ||fi |¤ŽS )a!  ResNet-50 model from
    `"Deep Residual Learning for Image Recognition" <https://arxiv.org/pdf/1512.03385.pdf>`_

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    r   )é   é   é   rQ   ©rN   r:   rP   r!   r!   r"   r   —   s
    ÿÿr   c                 K   s.   d|d< d|d< t dtg d¢| ||fi |¤ŽS )a8  ResNeXt-101 32x8d model from
    `"Aggregated Residual Transformation for Deep Neural Networks" <https://arxiv.org/pdf/1611.05431.pdf>`_

    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    é    Úgroupsé   Zwidth_per_groupr   )rQ   rR   é   rQ   rT   rP   r!   r!   r"   r   £   s    ÿÿr   )FTF)FTF)FTF)r   Ztorchvision.models.resnetr   r   r   r   Ztorch.nnr   Ztorchvision.models.utilsr   Ztorch.quantizationr   r   r	   Ztorch._jit_internalr
   Úutilsr   r   Ú__all__rG   r   r:   r   rN   r   r   r   r!   r!   r!   r"   Ú<module>   s$   ú
!

