o
    hU                  	   @   s  d Z ddlmZmZmZ ddlZddl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mZmZ dd	lmZ dd
lmZmZmZmZmZ ddlmZ ddlmZ ee Z!dZ"dZ#g dZ$dZ%dZ&dgZ'd3dej(de)de*dej(fddZ+G dd dej,Z-G dd dej,Z.G dd dej,Z/G d d! d!ej,Z0G d"d# d#ej,Z1G d$d% d%ej,Z2G d&d' d'eZ3d(Z4d)Z5ed*e4G d+d, d,e3Z6ed-e4G d.d/ d/e3Z7ed0e4G d1d2 d2e3eZ8dS )4z PyTorch ConvNext model.    )OptionalTupleUnionN)nn)BCEWithLogitsLossCrossEntropyLossMSELoss   )ACT2FN)BackboneOutputBaseModelOutputWithNoAttention(BaseModelOutputWithPoolingAndNoAttention$ImageClassifierOutputWithNoAttention)PreTrainedModel)add_code_sample_docstringsadd_start_docstrings%add_start_docstrings_to_model_forwardloggingreplace_return_docstrings)BackboneMixin   )ConvNextConfigr   zfacebook/convnext-tiny-224)r   i      r   ztabby, tabby cat        Finput	drop_probtrainingreturnc                 C   sd   |dks|s| S d| }| j d fd| jd   }|tj|| j| jd }|  | || }|S )aF  
    Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).

    Comment by Ross Wightman: This is the same as the DropConnect impl I created for EfficientNet, etc networks,
    however, the original name is misleading as 'Drop Connect' is a different form of dropout in a separate paper...
    See discussion: https://github.com/tensorflow/tpu/issues/494#issuecomment-532968956 ... I've opted for changing the
    layer and argument names to 'drop path' rather than mix DropConnect as a layer name and use 'survival rate' as the
    argument.
    r   r   r   )r   )dtypedevice)shapendimtorchrandr   r   floor_div)r   r   r   	keep_probr    random_tensoroutput r)   d/var/www/html/ai/venv/lib/python3.10/site-packages/transformers/models/convnext/modeling_convnext.py	drop_path@   s   
r+   c                       sT   e Zd ZdZddee ddf fddZdejdejfdd	Z	de
fd
dZ  ZS )ConvNextDropPathzXDrop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).Nr   r   c                    s   t    || _d S N)super__init__r   )selfr   	__class__r)   r*   r/   X   s   

zConvNextDropPath.__init__hidden_statesc                 C   s   t || j| jS r-   )r+   r   r   r0   r3   r)   r)   r*   forward\   s   zConvNextDropPath.forwardc                 C   s   d | jS )Nzp={})formatr   )r0   r)   r)   r*   
extra_repr_   s   zConvNextDropPath.extra_reprr-   )__name__
__module____qualname____doc__r   floatr/   r"   Tensorr5   strr7   __classcell__r)   r)   r1   r*   r,   U   s
    r,   c                       s8   e Zd ZdZd
 fdd	Zdejdejfdd	Z  ZS )ConvNextLayerNormaA  LayerNorm that supports two data formats: channels_last (default) or channels_first.
    The ordering of the dimensions in the inputs. channels_last corresponds to inputs with shape (batch_size, height,
    width, channels) while channels_first corresponds to inputs with shape (batch_size, channels, height, width).
    ư>channels_lastc                    s`   t    tt|| _tt|| _|| _	|| _
| j
dvr*td| j
 |f| _d S )N)rB   channels_firstzUnsupported data format: )r.   r/   r   	Parameterr"   onesweightzerosbiasepsdata_formatNotImplementedErrornormalized_shape)r0   rL   rI   rJ   r1   r)   r*   r/   i   s   

zConvNextLayerNorm.__init__xr   c                 C   s   | j dkrtjj|| j| j| j| j}|S | j dkr]|j	}|
 }|jddd}|| djddd}|| t|| j  }|j|d}| jd d d d f | | jd d d d f  }|S )NrB   rC   r   T)keepdim   )r   )rJ   r"   r   
functional
layer_normrL   rF   rH   rI   r   r<   meanpowsqrtto)r0   rM   input_dtypeusr)   r)   r*   r5   s   s   
	
,zConvNextLayerNorm.forward)rA   rB   )	r8   r9   r:   r;   r/   r"   r=   r5   r?   r)   r)   r1   r*   r@   c   s    
r@   c                       s6   e Zd ZdZ fddZdejdejfddZ  Z	S )ConvNextEmbeddingszThis class is comparable to (and inspired by) the SwinEmbeddings class
    found in src/transformers/models/swin/modeling_swin.py.
    c                    sL   t    tj|j|jd |j|jd| _t|jd ddd| _	|j| _d S )Nr   kernel_sizestriderA   rC   rI   rJ   )
r.   r/   r   Conv2dnum_channelshidden_sizes
patch_sizepatch_embeddingsr@   	layernormr0   configr1   r)   r*   r/      s   
zConvNextEmbeddings.__init__pixel_valuesr   c                 C   s4   |j d }|| jkrtd| |}| |}|S )Nr   zeMake sure that the channel dimension of the pixel values match with the one set in the configuration.)r    r_   
ValueErrorrb   rc   )r0   rf   r_   
embeddingsr)   r)   r*   r5      s   



zConvNextEmbeddings.forward
r8   r9   r:   r;   r/   r"   FloatTensorr=   r5   r?   r)   r)   r1   r*   rY      s    rY   c                       s8   e Zd ZdZd	 fdd	ZdejdejfddZ  Z	S )
ConvNextLayera3  This corresponds to the `Block` class in the original implementation.

    There are two equivalent implementations: [DwConv, LayerNorm (channels_first), Conv, GELU,1x1 Conv]; all in (N, C,
    H, W) (2) [DwConv, Permute to (N, H, W, C), LayerNorm (channels_last), Linear, GELU, Linear]; Permute back

    The authors used (2) as they find it slightly faster in PyTorch.

    Args:
        config ([`ConvNextConfig`]): Model configuration class.
        dim (`int`): Number of input channels.
        drop_path (`float`): Stochastic depth rate. Default: 0.0.
    r   c                    s   t    tj||dd|d| _t|dd| _t|d| | _t	|j
 | _td| || _|jdkrAtj|jt| dd	nd | _|d
krOt|| _d S t | _d S )Nr   r	   )r[   paddinggroupsrA   rI      r   T)requires_gradr   )r.   r/   r   r^   dwconvr@   rc   Linearpwconv1r
   
hidden_actactpwconv2layer_scale_init_valuerD   r"   rE   layer_scale_parameterr,   Identityr+   )r0   re   dimr+   r1   r)   r*   r/      s   

$zConvNextLayer.__init__r3   r   c                 C   s|   |}|  |}|dddd}| |}| |}| |}| |}| jd ur-| j| }|dddd}|| | }|S )Nr   rO   r	   r   )rq   permuterc   rs   ru   rv   rx   r+   )r0   r3   r   rM   r)   r)   r*   r5      s   






zConvNextLayer.forward)r   ri   r)   r)   r1   r*   rk      s    rk   c                       s8   e Zd ZdZd
 fdd	Zdejdejfdd	Z  Z	S )ConvNextStagea  ConvNeXT stage, consisting of an optional downsampling layer + multiple residual blocks.

    Args:
        config ([`ConvNextConfig`]): Model configuration class.
        in_channels (`int`): Number of input channels.
        out_channels (`int`): Number of output channels.
        depth (`int`): Number of residual blocks.
        drop_path_rates(`List[float]`): Stochastic depth rates for each layer.
    rO   Nc              	      s   t    |ks|dkr!tt|dddtj|||d| _nt | _p,dg| tj fddt|D  | _	d S )	Nr   rA   rC   r]   rZ   r   c                    s   g | ]}t  | d qS ))rz   r+   )rk   ).0jre   drop_path_ratesout_channelsr)   r*   
<listcomp>   s    z*ConvNextStage.__init__.<locals>.<listcomp>)
r.   r/   r   
Sequentialr@   r^   downsampling_layerry   rangelayers)r0   re   in_channelsr   r[   r\   depthr   r1   r   r*   r/      s   


zConvNextStage.__init__r3   r   c                 C   s   |  |}| |}|S r-   )r   r   r4   r)   r)   r*   r5      s   

zConvNextStage.forward)rO   rO   rO   Nri   r)   r)   r1   r*   r|      s    
r|   c                       sN   e Zd Z fddZ		ddejdee dee dee	e
f fd	d
Z  ZS )ConvNextEncoderc              	      s   t    t | _dd td|jt|j	
|j	D }|jd }t|jD ]$}|j| }t||||dkr9dnd|j	| || d}| j| |}q(d S )Nc                 S   s   g | ]}|  qS r)   )tolist)r}   rM   r)   r)   r*   r      s    z,ConvNextEncoder.__init__.<locals>.<listcomp>r   rO   r   )r   r   r\   r   r   )r.   r/   r   
ModuleListstagesr"   linspacedrop_path_ratesumdepthssplitr`   r   
num_stagesr|   append)r0   re   r   prev_chsiout_chsstager1   r)   r*   r/      s&   



zConvNextEncoder.__init__FTr3   output_hidden_statesreturn_dictr   c                 C   sj   |rdnd }t | jD ]\}}|r||f }||}q|r"||f }|s/tdd ||fD S t||dS )Nr)   c                 s   s    | ]	}|d ur|V  qd S r-   r)   )r}   vr)   r)   r*   	<genexpr>  s    z*ConvNextEncoder.forward.<locals>.<genexpr>)last_hidden_stater3   )	enumerater   tupler   )r0   r3   r   r   all_hidden_statesr   layer_moduler)   r)   r*   r5      s   


zConvNextEncoder.forward)FT)r8   r9   r:   r/   r"   rj   r   boolr   r   r   r5   r?   r)   r)   r1   r*   r      s    
r   c                   @   s$   e Zd ZdZeZdZdZdd ZdS )ConvNextPreTrainedModelz
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    convnextrf   c                 C   st   t |tjtjfr#|jjjd| jjd |j	dur!|j	j
  dS dS t |tjr8|j	j
  |jjd dS dS )zInitialize the weightsr   )rR   stdNg      ?)
isinstancer   rr   r^   rF   datanormal_re   initializer_rangerH   zero_	LayerNormfill_)r0   moduler)   r)   r*   _init_weights  s   
z%ConvNextPreTrainedModel._init_weightsN)	r8   r9   r:   r;   r   config_classbase_model_prefixmain_input_namer   r)   r)   r)   r*   r     s    r   aJ  
    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it
    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and
    behavior.

    Parameters:
        config ([`ConvNextConfig`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
aF  
    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See
            [`ConvNextImageProcessor.__call__`] for details.

        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
zQThe bare ConvNext model outputting raw features without any specific head on top.c                       sj   e Zd Z fddZeeeeee	de
d			ddejdee dee d	eeef fd
dZ  ZS )ConvNextModelc                    sJ   t  | || _t|| _t|| _tj|j	d |j
d| _|   d S )Nrn   )r.   r/   re   rY   rh   r   encoderr   r   r`   layer_norm_epsrc   	post_initrd   r1   r)   r*   r/   I  s   

zConvNextModel.__init__vision)
checkpointoutput_typer   modalityexpected_outputNrf   r   r   r   c                 C   s   |d ur|n| j j}|d ur|n| j j}|d u rtd| |}| j|||d}|d }| |ddg}|sC||f|dd   S t|||j	dS )Nz You have to specify pixel_valuesr   r   r   r   r   )r   pooler_outputr3   )
re   r   use_return_dictrg   rh   r   rc   rR   r   r3   )r0   rf   r   r   embedding_outputencoder_outputsr   pooled_outputr)   r)   r*   r5   V  s(   
zConvNextModel.forward)NNN)r8   r9   r:   r/   r   CONVNEXT_INPUTS_DOCSTRINGr   _CHECKPOINT_FOR_DOCr   _CONFIG_FOR_DOC_EXPECTED_OUTPUT_SHAPEr"   rj   r   r   r   r   r5   r?   r)   r)   r1   r*   r   D  s,    	
r   z
    ConvNext Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for
    ImageNet.
    c                       st   e Zd Z fddZeeeeee	e
d				ddejdeej dee dee d	eeef f
d
dZ  ZS )ConvNextForImageClassificationc                    sR   t  | |j| _t|| _|jdkrt|jd |jnt | _	| 
  d S )Nr   r   )r.   r/   
num_labelsr   r   r   rr   r`   ry   
classifierr   rd   r1   r)   r*   r/     s   
$z'ConvNextForImageClassification.__init__)r   r   r   r   Nrf   labelsr   r   r   c                 C   sb  |dur|n| j j}| j|||d}|r|jn|d }| |}d}|dur| j jdu rP| jdkr6d| j _n| jdkrL|jtj	ksG|jtj
krLd| j _nd| j _| j jdkrnt }	| jdkrh|	| | }n+|	||}n%| j jdkrt }	|	|d| j|d}n| j jdkrt }	|	||}|s|f|dd  }
|dur|f|
 S |
S t|||jd	S )
a  
        labels (`torch.LongTensor` of shape `(batch_size,)`, *optional*):
            Labels for computing the image classification/regression loss. Indices should be in `[0, ...,
            config.num_labels - 1]`. If `config.num_labels == 1` a regression loss is computed (Mean-Square loss), If
            `config.num_labels > 1` a classification loss is computed (Cross-Entropy).
        Nr   r   
regressionsingle_label_classificationmulti_label_classificationr   rO   )losslogitsr3   )re   r   r   r   r   problem_typer   r   r"   longintr   squeezer   viewr   r   r3   )r0   rf   r   r   r   outputsr   r   r   loss_fctr(   r)   r)   r*   r5     s>   


"


z&ConvNextForImageClassification.forward)NNNN)r8   r9   r:   r/   r   r   r   _IMAGE_CLASS_CHECKPOINTr   r   _IMAGE_CLASS_EXPECTED_OUTPUTr"   rj   r   
LongTensorr   r   r   r5   r?   r)   r)   r1   r*   r     s0    
r   zQ
    ConvNeXt backbone, to be used with frameworks like DETR and MaskFormer.
    c                       sZ   e Zd Z fddZeeeeed		dde	j
dee dee defd	d
Z  ZS )ConvNextBackbonec                    s   t  | t  | t|| _t|| _|jd g|j | _i }t	| j
| jD ]\}}t|dd||< q)t|| _|   d S )Nr   rC   )rJ   )r.   r/   _init_backbonerY   rh   r   r   r`   num_featureszip_out_featureschannelsr@   r   
ModuleDicthidden_states_normsr   )r0   re   r   r   r_   r1   r)   r*   r/     s   

zConvNextBackbone.__init__)r   r   Nrf   r   r   r   c                 C   s   |dur|n| j j}|dur|n| j j}| |}| j|d|d}|r&|jn|d }d}t| j|D ]\}}	|| jv rG| j	| |	}	||	f7 }q2|sV|f}
|rT|
|f7 }
|
S t
||r_|ddS dddS )az  
        Returns:

        Examples:

        ```python
        >>> from transformers import AutoImageProcessor, AutoBackbone
        >>> import torch
        >>> from PIL import Image
        >>> import requests

        >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
        >>> image = Image.open(requests.get(url, stream=True).raw)

        >>> processor = AutoImageProcessor.from_pretrained("facebook/convnext-tiny-224")
        >>> model = AutoBackbone.from_pretrained("facebook/convnext-tiny-224")

        >>> inputs = processor(image, return_tensors="pt")
        >>> outputs = model(**inputs)
        ```NTr   r   r)   )feature_mapsr3   
attentions)re   r   r   rh   r   r3   r   stage_namesout_featuresr   r   )r0   rf   r   r   r   r   r3   r   r   hidden_stater(   r)   r)   r*   r5     s:   



zConvNextBackbone.forward)NN)r8   r9   r:   r/   r   r   r   r   r   r"   r=   r   r   r5   r?   r)   r)   r1   r*   r     s    
r   )r   F)9r;   typingr   r   r   r"   torch.utils.checkpointr   torch.nnr   r   r   activationsr
   modeling_outputsr   r   r   r   modeling_utilsr   utilsr   r   r   r   r   utils.backbone_utilsr   configuration_convnextr   
get_loggerr8   loggerr   r   r   r   r   &CONVNEXT_PRETRAINED_MODEL_ARCHIVE_LISTr=   r<   r   r+   Moduler,   r@   rY   rk   r|   r   r   CONVNEXT_START_DOCSTRINGr   r   r   r   r)   r)   r)   r*   <module>   sZ   
 , /;L