
    9i3                         S SK r S SKJr  SS\S\4S jjr " S S\R                  5      rSS jrSS jr	 " S	 S
\R                  5      r
 " S S\R                  5      r " S S\R                  5      rg)    N	drop_probtrainingc                    US:X  d  U(       d  U $ SU-
  nU R                   S   4SU R                  S-
  -  -   nU[        R                  " X@R                  U R
                  S9-   nUR                  5         U R                  U5      U-  nU$ )a  Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks).
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.sh different form of dropout in a.sh 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.sh layer name and use
'survival rate' as the argument.
           r   r   )dtypedevice)shapendimtorchrandr	   r
   floor_div)xr   r   	keep_probr   random_tensoroutputs          h/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/modelscope/models/multi_modal/ofa/resnet.py	drop_pathr      s     BhIIWWQZNU	
 E

WWQXX!/ /MUU9-FM    c                   6   ^  \ rS rSrSrSU 4S jjrS rSrU =r$ )DropPath'   z^Drop paths (Stochastic Depth) per sample  (when applied in main path of residual blocks).
    c                 6   > [         [        U ]  5         Xl        g N)superr   __init__r   )selfr   	__class__s     r   r   DropPath.__init__+   s    h&("r   c                 B    [        XR                  U R                  5      $ r   )r   r   r   r   r   s     r   forwardDropPath.forward/   s    NNDMM::r   )r   r   )	__name__
__module____qualname____firstlineno____doc__r   r$   __static_attributes____classcell__r    s   @r   r   r   '   s    #; ;r   r   c                 8    [         R                  " U USUUUSUS9$ )z3x3 convolution with padding   F)kernel_sizestridepaddinggroupsbiasdilationnnConv2d)	in_planes
out_planesr1   r3   r5   s        r   conv3x3r;   3   s+    99 r   c                 0    [         R                  " XSUSS9$ )z1x1 convolutionr   F)r0   r1   r4   r6   )r9   r:   r1   s      r   conv1x1r=   @   s    991V%I Ir   c                   B   ^  \ rS rSrSr      SU 4S jjrS rSrU =r$ )
BasicBlockF   r   c	                 `  > [         [        U ]  5         Uc  [        R                  nUS:w  d  US:w  a  [        S5      eUS:  a  [        S5      e[        XU5      U l        U" U5      U l	        [        R                  " SS9U l        [        X"5      U l        U" U5      U l        X@l        X0l        g )Nr   @   z3BasicBlock only supports groups=1 and base_width=64z(Dilation > 1 not supported in BasicBlockTinplace)r   r?   r   r7   BatchNorm2d
ValueErrorNotImplementedErrorr;   conv1bn1ReLUreluconv2bn2
downsampler1   )
r   inplanesplanesr1   rN   r3   
base_widthr5   
norm_layerr    s
            r   r   BasicBlock.__init__I   s     	j$(*JQ;**EG Ga<%:< < Xv6
f%GGD)	V,
f%$r   c                      er   )rH   rI   rK   rL   rM   rN   r   r   identityouts       r   r$   BasicBlock.forwardd   s    ur   )rI   rM   rH   rL   rN   rK   r1   )r   Nr   rB   r   N	r&   r'   r(   r)   	expansionr   r$   r+   r,   r-   s   @r   r?   r?   F   s+    I
   6 r   r?   c                   D   ^  \ rS rSrSr       SU 4S jjrS rSrU =r$ )
Bottleneckx      c
                   > [         [        U ]  5         Uc  [        R                  n[        X&S-  -  5      U-  n
[        X5      U l        U" U
5      U l        [        XX5U5      U l
        U" U
5      U l        [        XU R                  -  5      U l        U" X R                  -  5      U l        [        R                  " SS9U l        X@l        X0l        U	S:  a  ['        U	5      U l        g [        R(                  " 5       U l        g )Ng      P@TrC   r   )r   r\   r   r7   rE   intr=   rH   rI   r;   rL   rM   rZ   conv3bn3rJ   rK   rN   r1   r   Identityr   )r   rO   rP   r1   rN   r3   rQ   r5   rR   drop_path_ratewidthr    s              r   r   Bottleneck.__init__   s     	j$(*JF3./069X-
e$U68D
e$UT^^$;<
f~~56GGD)	$-3 "9; 	r   c                    UnU R                  U5      nU R                  U5      nU R                  U5      nU R                  U5      nU R	                  U5      nU R                  U5      nU R                  U5      nU R                  U5      nU R                  b  U R                  U5      nX R                  U5      -   nU R                  U5      nU$ r   )	rH   rI   rK   rL   rM   ra   rb   rN   r   rU   s       r   r$   Bottleneck.forward   s    jjmhhsmiinjjohhsmiinjjohhsm??&q)H,,iin
r   )
rI   rM   rb   rH   rL   ra   rN   r   rK   r1   )r   Nr   rB   r   Nr   rY   r-   s   @r   r\   r\   x   s1     I
    #G6 r   r\   c                   X   ^  \ rS rSrSr      SU 4S jjr   S	S jrS rS rSr	U =r
$ )
ResNet   ak  
Deep residual network, copy from https://github.com/pytorch/vision/blob/main/torchvision/models/resnet.py.

You can see more details from https://arxiv.org/abs/1512.03385

step 1. Get image embedding with `7` as the patch image size, `2` as stride.
step 2. Do layer normalization, relu activation and max pooling.
step 3. Go through three times residual branch.
c           	        > [         [        U ]  5         Uc  [        R                  nX`l        SU l        SU l        Uc  / SQn[        U5      S:w  a  [        SR                  U5      5      eX0l        X@l        [        R                  " SU R                  SSSS	S
9U l        U" U R                  5      U l        [        R                   " SS9U l        [        R$                  " SSSS9U l        U R)                  [*        SUS   US9U l        U R)                  [*        SUS   SUS   US9U l        U R)                  [*        SUS   SUS   US9U l        U R3                  5        H  n[5        U[        R                  5      (       a+  [        R6                  R9                  UR:                  SSS9  MM  [5        U[        R<                  [        R                  [        R>                  45      (       d  M  [        R6                  RA                  UR:                  S5        [        R6                  RA                  URB                  S5        M     U(       a  U R3                  5        H  n[5        U[*        5      (       a6  [        R6                  RA                  URD                  R:                  S5        MN  [5        U[F        5      (       d  Me  [        R6                  RA                  URH                  R:                  S5        M     gg)aX  
Args:
    layers (`Tuple[int]`): There are three layers in resnet, so the length
        of layers should greater then three. And each element in `layers` is
        the number of `Bottleneck` in relative residual branch.
    zero_init_residual (`bool`, **optional**, default to `False`):
        Whether or not to zero-initialize the last BN in each residual branch.
    groups (`int`, **optional**, default to `1`):
        The number of groups. So far, only the value of `1` is supported.
    width_per_group (`int`, **optional**, default to `64`):
        The width in each group. So far, only the value of `64` is supported.
    replace_stride_with_dilation (`Tuple[bool]`, **optional**, default to `None`):
        Whether or not to replace stride with dilation in each residual branch.
    norm_layer (`torch.nn.Module`, **optional**, default to `None`):
        The normalization module. If `None`, will use  `torch.nn.BatchNorm2d`.
    drop_path_rate (`float`, **optional**, default to 0.0):
        Drop path rate. See more details about drop path from
        https://arxiv.org/pdf/1605.07648v4.pdf.
NrB   r   )FFFr/   zHreplace_stride_with_dilation should be None or a 3-element tuple, got {}      F)r0   r1   r2   r4   TrC   )r0   r1   r2   r   )rd      )r1   dilaterd      fan_outrK   )modenonlinearity)%r   rj   r   r7   rE   _norm_layerrO   r5   lenrF   formatr3   rQ   r8   rH   rI   rJ   rK   	MaxPool2dmaxpool_make_layerr\   layer1layer2layer3modules
isinstanceinitkaiming_normal_weightSyncBatchNorm	GroupNorm	constant_r4   rb   r?   rM   )
r   layerszero_init_residualr3   width_per_groupreplace_stride_with_dilationrR   rd   mr    s
            r   r   ResNet.__init__   sk   6 	fd$&J%'/ ,A(+,1 <<BF!==?@ @ )YYt}}!AquN
dmm,GGD)	||!QG&&F1In ' F&&1I/2) ' + &&1I/2) ' + A!RYY''''HH96 ( CA--r~~r||LN N!!!((A.!!!&&!,   \\^a,,GG%%aeellA6:..GG%%aeellA6	 $ r   c                 .   U R                   nSnU R                  n	U(       a  U =R                  U-  sl        SnUS:w  d  U R                  X!R                  -  :w  aJ  [        R
                  " [        U R                  X!R                  -  U5      U" X!R                  -  5      5      n/ n
U
R                  U" U R                  X$XR                  U R                  X5      5        X!R                  -  U l        [        R                  " SXc5       Vs/ s H  oR                  5       PM     nn[        SU5       HH  nU
R                  U" U R                  UU R                  U R                  U R                  UX   S95        MJ     [        R
                  " U
6 $ s  snf )a  
Making a single residual branch.

step 1. If dilate==`True`, switch the value of dilate and stride.
step 2. If the input dimension doesn't equal to th output output dimension
    in `block`, initialize a down sample module.
step 3. Build a sequential of `blocks` number of `block`.

Args:
    block (`torch.nn.Module`): The basic block in residual branch.
    planes (`int`): The output dimension of each basic block.
    blocks (`int`): The number of `block` in residual branch.
    stride (`int`, **optional**, default to `1`):
        The stride using in conv.
    dilate (`bool`, **optional**, default to `False`):
        Whether or not to replace dilate with stride.
    drop_path_rate (`float`, **optional**, default to 0.0):
        Drop path rate. See more details about drop path from
        https://arxiv.org/pdf/1605.07648v4.pdf.

Returns:
    A sequential of basic layer with type `torch.nn.Sequential[block]`
Nr   r   )r3   rQ   r5   rR   rd   )ru   r5   rO   rZ   r7   
Sequentialr=   appendr3   rQ   r   linspaceitemrange)r   blockrP   blocksr1   rp   rd   rR   rN   previous_dilationr   r   dpris                 r   rz   ResNet._make_layer  sN   < %%

 MMMMV#MFQ;$--6OO+CCv'?H6OO34J
 $--[[//#4B	C 0!&>!JK!JAvvx!JKq&!AMMMM;;#!]])#&6+, " }}f%% Ls   Fc                     U R                  U5      nU R                  U5      nU R                  U5      nU R                  U5      nU R	                  U5      nU R                  U5      nU R                  U5      nU$ r   )rH   rI   rK   ry   r{   r|   r}   r#   s     r   _forward_implResNet._forward_implQ  sb    JJqMHHQKIIaLLLOKKNKKNKKNr   c                 $    U R                  U5      $ r   )r   r#   s     r   r$   ResNet.forward[  s    !!!$$r   )ru   rQ   rI   rH   r5   r3   rO   r{   r|   r}   ry   rK   )Fr   rB   NNr   )r   Fr   )r&   r'   r(   r)   r*   r   rz   r   r$   r+   r,   r-   s   @r   rj   rj      sC     %*!#.2  #S7r  #&<&|% %r   rj   )r   F)r   r   r   r   )r   torch.nnr7   floatboolr   Moduler   r;   r=   r?   r\   rj    r   r   <module>r      so     E $ (	;ryy 	;
I/ /d8 8vi%RYY i%r   