§
    l¬¸jq  ã                  óö  — d Z ddlmZ ddlZddlZddlmc mZ ddlmZ ddl	m
Z
mZ ddlmZ ddlmZ dd	lmZmZmZ d
Z G d„ dej        ¦  «        Z G d„ de¦  «        Z G d„ dej        ¦  «        Z G d„ dej        ¦  «        Z G d„ dej        ¦  «        Z G d„ dej        ¦  «        Z G d„ dej        ¦  «        Z G d„ dej        ¦  «        Z G d„ dej        ¦  «        Z G d„ dej        ¦  «        ZdS )zTransformer modules.é    )ÚannotationsN)Únn)Ú	constant_Úxavier_uniform_)Ú
TORCH_1_11é   )ÚConv)Ú_get_clonesÚinverse_sigmoidÚ#multi_scale_deformable_attn_pytorch)
ÚAIFIÚMLPÚDeformableTransformerDecoderÚ!DeformableTransformerDecoderLayerÚLayerNorm2dÚMLPBlockÚMSDeformAttnÚTransformerBlockÚTransformerEncoderLayerÚTransformerLayerc                  óz   ‡ — e Zd ZdZ	 	 	 	 	 ddˆ fd„Zed d!d„¦   «         Z	 	 	 d"d#d„Z	 	 	 d"d#d„Z	 	 	 d"d#d„Z	ˆ xZ
S )$r   aÁ  A single layer of the transformer encoder.

    This class implements a standard transformer encoder layer with multi-head attention and feedforward network,
    supporting both pre-normalization and post-normalization configurations.

    Attributes:
        ma (nn.MultiheadAttention): Multi-head attention module.
        fc1 (nn.Linear): First linear layer in the feedforward network.
        fc2 (nn.Linear): Second linear layer in the feedforward network.
        norm1 (nn.LayerNorm): Layer normalization after attention.
        norm2 (nn.LayerNorm): Layer normalization after feedforward network.
        dropout (nn.Dropout): Dropout layer for the feedforward network.
        dropout1 (nn.Dropout): Dropout layer after attention.
        dropout2 (nn.Dropout): Dropout layer after feedforward network.
        act (nn.Module): Activation function.
        normalize_before (bool): Whether to apply normalization before attention and feedforward.
    é   é   ç        NFÚc1ÚintÚcmÚ	num_headsÚdropoutÚfloatÚactúnn.Module | NoneÚnormalize_beforeÚboolc                óX  •— t          ¦   «                              ¦   «          ddlm} |st	          d¦  «        ‚t          j        |||d¬¦  «        | _        t          j        ||¦  «        | _	        t          j        ||¦  «        | _
        t          j        |¦  «        | _        t          j        |¦  «        | _        t          j        |¦  «        | _        t          j        |¦  «        | _        t          j        |¦  «        | _        |€t          j        ¦   «         n|| _        || _        dS )aÅ  Initialize the TransformerEncoderLayer with specified parameters.

        Args:
            c1 (int): Input dimension.
            cm (int): Hidden dimension in the feedforward network.
            num_heads (int): Number of attention heads.
            dropout (float): Dropout probability.
            act (nn.Module): Activation function.
            normalize_before (bool): Whether to apply normalization before attention and feedforward.
        é   )Ú	TORCH_1_9z]TransformerEncoderLayer() requires torch>=1.9 to use nn.MultiheadAttention(batch_first=True).T)r   Úbatch_firstN)ÚsuperÚ__init__Úutils.torch_utilsr'   ÚModuleNotFoundErrorr   ÚMultiheadAttentionÚmaÚLinearÚfc1Úfc2Ú	LayerNormÚnorm1Únorm2ÚDropoutr   Údropout1Údropout2ÚGELUr!   r#   )	Úselfr   r   r   r   r!   r#   r'   Ú	__class__s	           €úr/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/ultralytics/nn/modules/transformer.pyr*   z TransformerEncoderLayer.__init__3   s  ø€ õ& 	‰Œ×ÒÑÔÐØ2Ð2Ð2Ð2Ð2Ð2àð 	Ý%Øoñô ð õ Ô'¨¨I¸wÐTXÐYÑYÔYˆŒå”9˜R Ñ$Ô$ˆŒÝ”9˜R Ñ$Ô$ˆŒå”\ "Ñ%Ô%ˆŒ
Ý”\ "Ñ%Ô%ˆŒ
Ý”z 'Ñ*Ô*ˆŒÝœ
 7Ñ+Ô+ˆŒÝœ
 7Ñ+Ô+ˆŒà # •2”7‘9”9�9°ˆŒØ 0ˆÔÐÐó    Útensorútorch.TensorÚposútorch.Tensor | NoneÚreturnc                ó   — |€| n| |z   S )z2Add position embeddings to the tensor if provided.© ©r=   r?   s     r;   Úwith_pos_embedz&TransformerEncoderLayer.with_pos_embed[   ó   € ð ˜ˆvˆv¨&°3©,Ð6r<   ÚsrcÚsrc_maskÚsrc_key_padding_maskc           	     óÂ  — |                       ||¦  «        x}}|                      |||||¬¦  «        d         }||                      |¦  «        z   }|                      |¦  «        }|                      |                      |                      |                      |¦  «        ¦  «        ¦  «        ¦  «        }||                      |¦  «        z   }|  	                    |¦  «        S )aµ  Perform forward pass with post-normalization.

        Args:
            src (torch.Tensor): Input tensor.
            src_mask (torch.Tensor, optional): Mask for the src sequence.
            src_key_padding_mask (torch.Tensor, optional): Mask for the src keys per batch.
            pos (torch.Tensor, optional): Positional encoding.

        Returns:
            (torch.Tensor): Output tensor after attention and feedforward.
        ©ÚvalueÚ	attn_maskÚkey_padding_maskr   )
rE   r.   r6   r3   r1   r   r!   r0   r7   r4   )r9   rG   rH   rI   r?   ÚqÚkÚsrc2s           r;   Úforward_postz$TransformerEncoderLayer.forward_post`   s¾   € ð$ ×#Ò# C¨Ñ-Ô-Ð-ˆˆAØ�wŠw�q˜! 3°(ÐMaˆwÑbÔbÐcdÔeˆØ�D—M’M $Ñ'Ô'Ñ'ˆØ�jŠj˜‰oŒoˆØ�xŠx˜Ÿš T§X¢X¨d¯hªh°s©m¬mÑ%<Ô%<Ñ=Ô=Ñ>Ô>ˆØ�D—M’M $Ñ'Ô'Ñ'ˆØ�zŠz˜#‰ŒÐr<   c           	     óÂ  — |                       |¦  «        }|                      ||¦  «        x}}|                      |||||¬¦  «        d         }||                      |¦  «        z   }|                      |¦  «        }|                      |                      |                      |                      |¦  «        ¦  «        ¦  «        ¦  «        }||  	                    |¦  «        z   S )a´  Perform forward pass with pre-normalization.

        Args:
            src (torch.Tensor): Input tensor.
            src_mask (torch.Tensor, optional): Mask for the src sequence.
            src_key_padding_mask (torch.Tensor, optional): Mask for the src keys per batch.
            pos (torch.Tensor, optional): Positional encoding.

        Returns:
            (torch.Tensor): Output tensor after attention and feedforward.
        rK   r   )
r3   rE   r.   r6   r4   r1   r   r!   r0   r7   )r9   rG   rH   rI   r?   rQ   rO   rP   s           r;   Úforward_prez#TransformerEncoderLayer.forward_prez   s¾   € ð$ �zŠz˜#‰ŒˆØ×#Ò# D¨#Ñ.Ô.Ð.ˆˆAØ�wŠw�q˜! 4°8ÐNbˆwÑcÔcÐdeÔfˆØ�D—M’M $Ñ'Ô'Ñ'ˆØ�zŠz˜#‰ŒˆØ�xŠx˜Ÿš T§X¢X¨d¯hªh°t©n¬nÑ%=Ô%=Ñ>Ô>Ñ?Ô?ˆØ�T—]’] 4Ñ(Ô(Ñ(Ð(r<   c                óp   — | j         r|                      ||||¦  «        S |                      ||||¦  «        S )a¿  Forward propagate the input through the encoder module.

        Args:
            src (torch.Tensor): Input tensor.
            src_mask (torch.Tensor, optional): Mask for the src sequence.
            src_key_padding_mask (torch.Tensor, optional): Mask for the src keys per batch.
            pos (torch.Tensor, optional): Positional encoding.

        Returns:
            (torch.Tensor): Output tensor after transformer encoder layer.
        )r#   rT   rR   )r9   rG   rH   rI   r?   s        r;   ÚforwardzTransformerEncoderLayer.forward”   sF   € ð$ Ô ð 	NØ×#Ò# C¨Ð3GÈÑMÔMÐMØ× Ò   hÐ0DÀcÑJÔJÐJr<   )r   r   r   NF©r   r   r   r   r   r   r   r    r!   r"   r#   r$   ©N©r=   r>   r?   r@   rA   r>   ©NNN)
rG   r>   rH   r@   rI   r@   r?   r@   rA   r>   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r*   ÚstaticmethodrE   rR   rT   rV   Ú__classcell__©r:   s   @r;   r   r       sø   ø€ € € € € ðð ð* ØØØ $Ø!&ð&1ð &1ð &1ð &1ð &1ð &1ð &1ðP ð7ð 7ð 7ð 7ñ „\ð7ð )-Ø48Ø#'ðð ð ð ð ð: )-Ø48Ø#'ð)ð )ð )ð )ð )ð: )-Ø48Ø#'ðKð Kð Kð Kð Kð Kð Kð Kð Kr<   r   c                  óX   ‡ — e Zd ZdZ	 	 	 	 	 ddˆ fd„Zd ˆ fd„Ze	 d!d"d„¦   «         Zˆ xZS )#r   zöAIFI transformer layer for 2D data with positional embeddings.

    This class extends TransformerEncoderLayer to work with 2D feature maps by adding 2D sine-cosine positional
    embeddings and handling the spatial dimensions appropriately.
    r   r   r   NFr   r   r   r   r   r    r!   r"   r#   r$   c                óT   •— t          ¦   «                              ||||||¦  «         dS )a»  Initialize the AIFI instance with specified parameters.

        Args:
            c1 (int): Input dimension.
            cm (int): Hidden dimension in the feedforward network.
            num_heads (int): Number of attention heads.
            dropout (float): Dropout probability.
            act (nn.Module): Activation function.
            normalize_before (bool): Whether to apply normalization before attention and feedforward.
        N)r)   r*   )r9   r   r   r   r   r!   r#   r:   s          €r;   r*   zAIFI.__init__²   s.   ø€ õ& 	‰Œ×Ò˜˜R ¨G°SÐ:JÑKÔKÐKÐKÐKr<   Úxr>   rA   c                ó¢  •— |j         dd…         \  }}}|                      ||||¬¦  «        }t          ¦   «                              |                     d¦  «                             ddd¦  «        |                     |j        ¦  «        ¬¦  «        }|                     ddd¦  «                             d|||g¦  «         	                    ¦   «         S )zÝForward pass for the AIFI transformer layer.

        Args:
            x (torch.Tensor): Input tensor with shape [B, C, H, W].

        Returns:
            (torch.Tensor): Output tensor with shape [B, C, H, W].
        r   N)Úlikeé   r   )r?   éÿÿÿÿ)
ÚshapeÚ"build_2d_sincos_position_embeddingr)   rV   ÚflattenÚpermuteÚtoÚdtypeÚviewÚ
contiguous)r9   rd   ÚcÚhÚwÚ	pos_embedr:   s         €r;   rV   zAIFI.forwardÇ   s±   ø€ ð ”'˜!˜"˜"”+‰ˆˆ1ˆaØ×;Ò;¸A¸qÀ!È!Ð;ÑLÔLˆ	å‰GŒG�OŠO˜AŸIšI a™LœL×0Ò0°°A°qÑ9Ô9¸y¿|º|ÈAÌGÑ?TÔ?TˆOÑUÔUˆØ�yŠy˜˜A˜qÑ!Ô!×&Ò&¨¨A¨q°! }Ñ5Ô5×@Ò@ÑBÔBÐBr<   é   ç     ˆÃ@rs   rr   Ú	embed_dimÚtemperaturerf   c               ó‚  — |dz  dk    s
J d¦   «         ‚|                      dt          j        ¬¦  «        }t          j        | ¦  «                             |¦  «        }t          j        |¦  «                             |¦  «        }t
          rt          j        ||d¬¦  «        nt          j        ||¦  «        \  }}|dz  }t          j        |¦  «                             |¦  «        |z  }d||z  z  }|                     ¦   «         d	                              ¦   «         |d
         z  }	|                     ¦   «         d	                              ¦   «         |d
         z  }
t          j	        t          j
        |	¦  «        t          j        |	¦  «        t          j
        |
¦  «        t          j        |
¦  «        gd¦  «        d
         S )aÕ  Build 2D sine-cosine position embedding.

        Args:
            w (int): Width of the feature map.
            h (int): Height of the feature map.
            embed_dim (int): Embedding dimension.
            temperature (float): Temperature for the sine/cosine functions.
            like (torch.Tensor): Tensor whose device the embedding grids are built on.

        Returns:
            (torch.Tensor): Position embedding with shape [1, h*w, embed_dim].
        é   r   zHEmbed dimension must be divisible by 4 for 2D sin-cos position embeddingr   ©rn   Úij)Úindexingg      ð?©.NN)Ú	new_zerosÚtorchÚfloat32ÚarangeÚtype_asr   Úmeshgridrk   r    ÚcatÚsinÚcos)rs   rr   rw   rx   rf   Úgrid_wÚgrid_hÚpos_dimÚomegaÚout_wÚout_hs              r;   rj   z'AIFI.build_2d_sincos_position_embeddingÖ   s~  € ð  ˜1‰} Ò!Ð!Ð!Ð#mÑ!Ô!Ð!à�~Š~˜a¥u¤}ˆ~Ñ5Ô5ˆÝ”˜a‘”×(Ò(¨Ñ.Ô.ˆÝ”˜a‘”×(Ò(¨Ñ.Ô.ˆÝJTÐx�œ¨°ÀÐFÑFÔFÐFÕZ_ÔZhÐioÐqwÑZxÔZx‰ˆ�Ø˜q‘.ˆÝ”˜WÑ%Ô%×-Ò-¨dÑ3Ô3°gÑ=ˆØ�{ EÑ)Ñ*ˆð —’Ñ Ô  Ô+×1Ò1Ñ3Ô3°e¸D´kÑAˆØ—’Ñ Ô  Ô+×1Ò1Ñ3Ô3°e¸D´kÑAˆåŒy�%œ) EÑ*Ô*­E¬I°eÑ,<Ô,<½e¼iÈÑ>NÔ>NÕPUÔPYÐZ_ÑP`ÔP`ÐaÐcdÑeÔeÐfjÔkÐkr<   )r   r   r   NFrW   ©rd   r>   rA   r>   )ru   rv   )rs   r   rr   r   rw   r   rx   r    rf   r>   rA   r>   )	r[   r\   r]   r^   r*   rV   r_   rj   r`   ra   s   @r;   r   r   «   s¹   ø€ € € € € ðð ð ØØØ $Ø!&ðLð Lð Lð Lð Lð Lð Lð*Cð Cð Cð Cð Cð Cð àCJðlð lð lð lñ „\ðlð lð lð lð lr<   r   c                  ó,   ‡ — e Zd ZdZd
ˆ fd„Zdd	„Zˆ xZS )r   zeTransformer layer https://arxiv.org/abs/2010.11929 (LayerNorm layers removed for better performance).rq   r   r   c                ó–  •— t          ¦   «                              ¦   «          t          j        ||d¬¦  «        | _        t          j        ||d¬¦  «        | _        t          j        ||d¬¦  «        | _        t          j        ||¬¦  «        | _        t          j        ||d¬¦  «        | _	        t          j        ||d¬¦  «        | _
        dS )zåInitialize a self-attention mechanism using linear transformations and multi-head attention.

        Args:
            c (int): Input and output channel dimension.
            num_heads (int): Number of attention heads.
        F)Úbias)rw   r   N)r)   r*   r   r/   rO   rP   Úvr-   r.   r0   r1   )r9   rq   r   r:   s      €r;   r*   zTransformerLayer.__init__ú   s®   ø€ õ 	‰Œ×ÒÑÔÐÝ”˜1˜a eÐ,Ñ,Ô,ˆŒÝ”˜1˜a eÐ,Ñ,Ô,ˆŒÝ”˜1˜a eÐ,Ñ,Ô,ˆŒÝÔ'°!¸yÐIÑIÔIˆŒÝ”9˜Q ¨Ð.Ñ.Ô.ˆŒÝ”9˜Q ¨Ð.Ñ.Ô.ˆŒˆˆr<   rd   r>   rA   c                ó
  — |                       |                      |¦  «        |                      |¦  «        |                      |¦  «        ¦  «        d         |z   }|                      |                      |¦  «        ¦  «        |z   S )zØApply a transformer block to the input x and return the output.

        Args:
            x (torch.Tensor): Input tensor.

        Returns:
            (torch.Tensor): Output tensor after transformer layer.
        r   )r.   rO   rP   r’   r1   r0   ©r9   rd   s     r;   rV   zTransformerLayer.forward	  sb   € ð �GŠG�D—F’F˜1‘I”I˜tŸvšv a™yœy¨$¯&ª&°©)¬)Ñ4Ô4°QÔ7¸!Ñ;ˆØ�xŠx˜Ÿš ™œÑ$Ô$ qÑ(Ð(r<   )rq   r   r   r   rŽ   ©r[   r\   r]   r^   r*   rV   r`   ra   s   @r;   r   r   ÷   sW   ø€ € € € € ØoÐoð/ð /ð /ð /ð /ð /ð
)ð 
)ð 
)ð 
)ð 
)ð 
)ð 
)ð 
)r<   r   c                  ó,   ‡ — e Zd ZdZdˆ fd„Zdd„Zˆ xZS )r   a  Vision Transformer block based on https://arxiv.org/abs/2010.11929.

    This class implements a complete transformer block with optional convolution layer for channel adjustment, learnable
    position embedding, and multiple transformer layers.

    Attributes:
        conv (Conv, optional): Convolution layer if input and output channels differ.
        linear (nn.Linear): Learnable position embedding.
        tr (nn.Sequential): Sequential container of transformer layers.
        c2 (int): Output channel dimension.
    r   r   Úc2r   Ú
num_layersc                ó,  •‡‡— t          ¦   «                              ¦   «          d| _        |‰k    rt          |‰¦  «        | _        t	          j        ‰‰¦  «        | _        t	          j        ˆˆfd„t          |¦  «        D ¦   «         Ž | _	        ‰| _
        dS )aL  Initialize a Transformer module with position embedding and specified number of heads and layers.

        Args:
            c1 (int): Input channel dimension.
            c2 (int): Output channel dimension.
            num_heads (int): Number of attention heads.
            num_layers (int): Number of transformer layers.
        Nc              3  ó8   •K  — | ]}t          ‰‰¦  «        V — Œd S rX   )r   )Ú.0Ú_r—   r   s     €€r;   ú	<genexpr>z,TransformerBlock.__init__.<locals>.<genexpr>1  s.   øè è € Ð!]Ð!]ÀaÕ"2°2°yÑ"AÔ"AÐ!]Ð!]Ð!]Ð!]Ð!]Ð!]r<   )r)   r*   Úconvr	   r   r/   ÚlinearÚ
SequentialÚrangeÚtrr—   )r9   r   r—   r   r˜   r:   s     `` €r;   r*   zTransformerBlock.__init__#  s‰   øøø€ õ 	‰Œ×ÒÑÔÐØˆŒ	Ø�Š8ˆ8Ý˜R ™œˆDŒIÝ”i  BÑ'Ô'ˆŒÝ”-Ð!]Ð!]Ð!]Ð!]Ð!]Í5ÐQ[ÑK\ÔK\Ð!]Ñ!]Ô!]Ð^ˆŒØˆŒˆˆr<   rd   r>   rA   c                ó\  — | j         �|                       |¦  «        }|j        \  }}}}|                     d¦  «                             ddd¦  «        }|                      ||                      |¦  «        z   ¦  «                             ddd¦  «                             || j        ||¦  «        S )zíForward propagate the input through the transformer block.

        Args:
            x (torch.Tensor): Input tensor with shape [b, c1, h, w].

        Returns:
            (torch.Tensor): Output tensor with shape [b, c2, h, w].
        Nrg   r   r   )rž   ri   rk   rl   r¢   rŸ   Úreshaper—   )r9   rd   Úbrœ   rr   rs   Úps          r;   rV   zTransformerBlock.forward4  s–   € ð Œ9Ð Ø—	’	˜!‘”ˆAØ”W‰
ˆˆ1ˆa�Ø�IŠI�a‰LŒL× Ò   A qÑ)Ô)ˆØ�wŠw�q˜4Ÿ;š; q™>œ>Ñ)Ñ*Ô*×2Ò2°1°a¸Ñ;Ô;×CÒCÀAÀtÄwÐPQÐSTÑUÔUÐUr<   )r   r   r—   r   r   r   r˜   r   rŽ   r•   ra   s   @r;   r   r     sc   ø€ € € € € ð
ð 
ðð ð ð ð ð ð"Vð Vð Vð Vð Vð Vð Vð Vr<   r   c                  ó:   ‡ — e Zd ZdZej        fd
ˆ fd„Zdd	„Zˆ xZS )r   z+A single block of a multi-layer perceptron.Úembedding_dimr   Úmlp_dimc                óÎ   •— t          ¦   «                              ¦   «          t          j        ||¦  «        | _        t          j        ||¦  «        | _         |¦   «         | _        dS )a  Initialize the MLPBlock with specified embedding dimension, MLP dimension, and activation function.

        Args:
            embedding_dim (int): Input and output dimension.
            mlp_dim (int): Hidden dimension.
            act (type): Activation function class.
        N)r)   r*   r   r/   Úlin1Úlin2r!   )r9   r¨   r©   r!   r:   s       €r;   r*   zMLPBlock.__init__G  sR   ø€ õ 	‰Œ×ÒÑÔÐÝ”I˜m¨WÑ5Ô5ˆŒ	Ý”I˜g }Ñ5Ô5ˆŒ	Ø�3‘5”5ˆŒˆˆr<   rd   r>   rA   c                óx   — |                       |                      |                      |¦  «        ¦  «        ¦  «        S )z¯Forward pass for the MLPBlock.

        Args:
            x (torch.Tensor): Input tensor.

        Returns:
            (torch.Tensor): Output tensor after MLP block.
        )r¬   r!   r«   r”   s     r;   rV   zMLPBlock.forwardT  s,   € ð �yŠy˜Ÿš $§)¢)¨A¡,¤,Ñ/Ô/Ñ0Ô0Ð0r<   )r¨   r   r©   r   rŽ   )	r[   r\   r]   r^   r   r8   r*   rV   r`   ra   s   @r;   r   r   D  sa   ø€ € € € € Ø5Ð5à=?¼Wð ð ð ð ð ð ð ð	1ð 	1ð 	1ð 	1ð 	1ð 	1ð 	1ð 	1r<   r   c                  ó@   ‡ — e Zd ZdZej        dddfdˆ fd„Zdd„Zˆ xZS )r   a±  A simple multi-layer perceptron (also called FFN).

    This class implements a configurable MLP with multiple linear layers, activation functions, and optional sigmoid
    output activation.

    Attributes:
        num_layers (int): Number of layers in the MLP.
        layers (nn.ModuleList): List of linear layers.
        sigmoid (bool): Whether to apply sigmoid to the output.
        act (nn.Module): Activation function.
    FNÚ	input_dimr   Ú
hidden_dimÚ
output_dimr˜   Úsigmoidr$   ÚresidualÚout_normú	nn.Modulec	                ó°  •— t          ¦   «                              ¦   «          || _        |g|dz
  z  }	t          j        d„ t          |g|	¢g |	¢|‘¦  «        D ¦   «         ¦  «        | _        || _         |¦   «         | _        |r||k    rt          d¦  «        ‚|| _
        t          |t          j        ¦  «        s|�J ‚|pt          j        ¦   «         | _        dS )a7  Initialize the MLP with specified input, hidden, output dimensions and number of layers.

        Args:
            input_dim (int): Input dimension.
            hidden_dim (int): Hidden dimension.
            output_dim (int): Output dimension.
            num_layers (int): Number of layers.
            act (type): Activation function class.
            sigmoid (bool): Whether to apply sigmoid to the output.
            residual (bool): Whether to use residual connections.
            out_norm (nn.Module, optional): Normalization layer for the output.
        r   c              3  óF   K  — | ]\  }}t          j        ||¦  «        V — Œd S rX   )r   r/   )r›   ÚnrP   s      r;   r�   zMLP.__init__.<locals>.<genexpr>‡  s0   è è € Ð#gÐ#g¹¸¸1¥B¤I¨a°¡O¤OÐ#gÐ#gÐ#gÐ#gÐ#gÐ#gr<   z5residual is only supported if input_dim == output_dimN)r)   r*   r˜   r   Ú
ModuleListÚzipÚlayersr²   r!   Ú
ValueErrorr³   Ú
isinstanceÚModuleÚIdentityr´   )r9   r¯   r°   r±   r˜   r!   r²   r³   r´   rr   r:   s             €r;   r*   zMLP.__init__m  sà   ø€ õ. 	‰Œ×ÒÑÔÐØ$ˆŒØˆL˜J¨™NÑ+ˆÝ”mÐ#gÐ#gÅÀYÀOÐQRÀOÐUeÐWXÐUeÐZdÐUeÑ@fÔ@fÐ#gÑ#gÔ#gÑgÔgˆŒØˆŒØ�3‘5”5ˆŒØð 	V˜	 ZÒ/Ð/ÝÐTÑUÔUÐUØ ˆŒå˜(¥B¤IÑ.Ô.ÐB°(Ð2BÐ2BÐBØ Ð1¥B¤K¡M¤MˆŒˆˆr<   rd   r>   rA   c                ó¤  — |}t          | j        ¦  «        D ]R\  }}|| j        dz
  k     r4 t          | dt	          j        ¦   «         ¦  «         ||¦  «        ¦  «        n
 ||¦  «        }ŒSt          | dd¦  «        r||z   } t          | dt	          j        ¦   «         ¦  «        |¦  «        }t          | dd¦  «        r|                     ¦   «         n|S )z«Forward pass for the entire MLP.

        Args:
            x (torch.Tensor): Input tensor.

        Returns:
            (torch.Tensor): Output tensor after MLP.
        r   r!   r³   Fr´   r²   )Ú	enumerater»   r˜   Úgetattrr   ÚReLUr¿   r²   )r9   rd   Úorig_xÚiÚlayers        r;   rV   zMLP.forward‘  sÙ   € ð ˆÝ! $¤+Ñ.Ô.ð 	cð 	c‰HˆAˆuØ=>ÀÄÐSTÑATÒ=TÐ=TÐ/•˜˜e¥R¤W¡Y¤YÑ/Ô/°°°a±´Ñ9Ô9Ð9ÐZ_ÐZ_Ð`aÑZbÔZbˆAˆAÝ�4˜ UÑ+Ô+ð 	Ø�F‘
ˆAØ4�G�D˜*¥b¤k¡m¤mÑ4Ô4°QÑ7Ô7ˆÝ% d¨I°uÑ=Ô=ÐDˆq�yŠy‰{Œ{ˆ{À1ÐDr<   )r¯   r   r°   r   r±   r   r˜   r   r²   r$   r³   r$   r´   rµ   rŽ   )	r[   r\   r]   r^   r   rÃ   r*   rV   r`   ra   s   @r;   r   r   `  sy   ø€ € € € € ð
ð 
ð$ ŒGØØØ"ð"2ð "2ð "2ð "2ð "2ð "2ð "2ðHEð Eð Eð Eð Eð Eð Eð Er<   r   c                  ó.   ‡ — e Zd ZdZddˆ fd„Zdd„Zˆ xZS )r   ap  2D Layer Normalization module inspired by Detectron2 and ConvNeXt implementations.

    This class implements layer normalization for 2D feature maps, normalizing across the channel dimension while
    preserving spatial dimensions.

    Attributes:
        weight (nn.Parameter): Learnable scale parameter.
        bias (nn.Parameter): Learnable bias parameter.
        eps (float): Small constant for numerical stability.

    References:
        https://github.com/facebookresearch/detectron2/blob/main/detectron2/layers/batch_norm.py
        https://github.com/facebookresearch/ConvNeXt/blob/main/models/convnext.py
    ç�íµ ÷Æ°>Únum_channelsr   Úepsr    c                ó  •— t          ¦   «                              ¦   «          t          j        t	          j        |¦  «        ¦  «        | _        t          j        t	          j        |¦  «        ¦  «        | _        || _	        dS )zËInitialize LayerNorm2d with the given parameters.

        Args:
            num_channels (int): Number of channels in the input.
            eps (float): Small constant for numerical stability.
        N)
r)   r*   r   Ú	Parameterr€   ÚonesÚweightÚzerosr‘   rÊ   )r9   rÉ   rÊ   r:   s      €r;   r*   zLayerNorm2d.__init__³  s\   ø€ õ 	‰Œ×ÒÑÔÐÝ”l¥5¤:¨lÑ#;Ô#;Ñ<Ô<ˆŒÝ”L¥¤¨\Ñ!:Ô!:Ñ;Ô;ˆŒ	ØˆŒˆˆr<   rd   r>   rA   c                ó  — |                      dd¬¦  «        }||z
                       d¦  «                              dd¬¦  «        }||z
  t          j        || j        z   ¦  «        z  }| j        dd…ddf         |z  | j        dd…ddf         z   S )z¼Perform forward pass for 2D layer normalization.

        Args:
            x (torch.Tensor): Input tensor.

        Returns:
            (torch.Tensor): Normalized output tensor.
        r   T©Úkeepdimrg   N)ÚmeanÚpowr€   ÚsqrtrÊ   rÎ   r‘   )r9   rd   ÚuÚss       r;   rV   zLayerNorm2d.forward¿  s�   € ð �FŠF�1˜dˆFÑ#Ô#ˆØ�‰U�KŠK˜‰NŒN×Ò ¨4ÐÑ0Ô0ˆØ�‰U•e”j  T¤X¡Ñ.Ô.Ñ.ˆØŒ{˜1˜1˜1˜d D˜=Ô)¨AÑ-°´	¸!¸!¸!¸TÀ4¸-Ô0HÑHÐHr<   )rÈ   )rÉ   r   rÊ   r    rŽ   r•   ra   s   @r;   r   r   £  sh   ø€ € € € € ðð ð
ð 
ð 
ð 
ð 
ð 
ð 
ðIð Ið Ið Ið Ið Ið Ið Ir<   r   c                  ó8   ‡ — e Zd ZdZddˆ fd
„Zd„ Z	 ddd„Zˆ xZS )r   aò  Multiscale Deformable Attention Module based on Deformable-DETR and PaddleDetection implementations.

    This module implements multiscale deformable attention that can attend to features at multiple scales with learnable
    sampling locations and attention weights.

    Attributes:
        im2col_step (int): Step size for im2col operations.
        d_model (int): Model dimension.
        n_levels (int): Number of feature levels.
        n_heads (int): Number of attention heads.
        n_points (int): Number of sampling points per attention head per feature level.
        sampling_offsets (nn.Linear): Linear layer for generating sampling offsets.
        attention_weights (nn.Linear): Linear layer for generating attention weights.
        value_proj (nn.Linear): Linear layer for projecting values.
        output_proj (nn.Linear): Linear layer for projecting output.

    References:
        https://github.com/fundamentalvision/Deformable-DETR/blob/main/models/ops/modules/ms_deform_attn.py
    ru   rz   r   Úd_modelr   Ún_levelsÚn_headsÚn_pointsc                ó  •— t          ¦   «                              ¦   «          ||z  dk    rt          d|› d|› �¦  «        ‚||z  }||z  |k    s
J d¦   «         ‚d| _        || _        || _        || _        || _        t          j	        |||z  |z  dz  ¦  «        | _
        t          j	        |||z  |z  ¦  «        | _        t          j	        ||¦  «        | _        t          j	        ||¦  «        | _        |                      ¦   «          dS )a>  Initialize MSDeformAttn with the given parameters.

        Args:
            d_model (int): Model dimension.
            n_levels (int): Number of feature levels.
            n_heads (int): Number of attention heads.
            n_points (int): Number of sampling points per attention head per feature level.
        r   z.d_model must be divisible by n_heads, but got z and z(`d_model` must be divisible by `n_heads`é@   rg   N)r)   r*   r¼   Úim2col_steprÙ   rÚ   rÛ   rÜ   r   r/   Úsampling_offsetsÚattention_weightsÚ
value_projÚoutput_projÚ_reset_parameters)r9   rÙ   rÚ   rÛ   rÜ   Ú_d_per_headr:   s         €r;   r*   zMSDeformAttn.__init__ã  s  ø€ õ 	‰Œ×ÒÑÔÐØ�WÑ Ò!Ð!ÝÐeÈgÐeÐeÐ\cÐeÐeÑfÔfÐfØ Ñ(ˆà˜WÑ$¨Ò/Ð/Ð/Ð1[Ñ/Ô/Ð/àˆÔàˆŒØ ˆŒØˆŒØ ˆŒå "¤	¨'°7¸XÑ3EÈÑ3PÐSTÑ3TÑ UÔ UˆÔÝ!#¤¨7°G¸hÑ4FÈÑ4QÑ!RÔ!RˆÔÝœ) G¨WÑ5Ô5ˆŒÝœ9 W¨gÑ6Ô6ˆÔà×ÒÑ Ô Ð Ð Ð r<   c                óž  — t          | j        j        j        d¦  «         t	          j        | j        t          j        ¬¦  «        dt          j	        z  | j        z  z  }t	          j
        |                     ¦   «         |                     ¦   «         gd¦  «        }||                     ¦   «                              dd¬¦  «        d         z                       | j        ddd	¦  «                             d| j        | j        d¦  «        }t'          | j        ¦  «        D ]}|d
d
…d
d
…|d
d
…fxx         |dz   z  cc<   Œ t	          j        ¦   «         5  t+          j        |                     d¦  «        ¦  «        | j        _        d
d
d
¦  «         n# 1 swxY w Y   t          | j        j        j        d¦  «         t          | j        j        j        d¦  «         t3          | j        j        j        ¦  «         t          | j        j        j        d¦  «         t3          | j        j        j        ¦  «         t          | j        j        j        d¦  «         d
S )zReset module parameters.r   r{   g       @rh   TrÑ   r   r   rg   N)r   rà   rÎ   Údatar€   r‚   rÛ   r�   ÚmathÚpiÚstackr‡   r†   ÚabsÚmaxro   ÚrepeatrÚ   rÜ   r¡   Úno_gradr   rÌ   r‘   rá   r   râ   rã   )r9   ÚthetasÚ	grid_initrÅ   s       r;   rä   zMSDeformAttn._reset_parameters  sA  € å�$Ô'Ô.Ô3°SÑ9Ô9Ð9Ý”˜dœlµ%´-Ð@Ñ@Ô@ÀCÍ$Ì'ÁMÐTXÔT`ÑD`ÑaˆÝ”K §¢¡¤¨v¯zªz©|¬|Ð <¸bÑAÔAˆ	à˜Ÿš™œ×,Ò,¨R¸Ð,Ñ>Ô>¸qÔAÑAßŠT�$”,  1 aÑ(Ô(ßŠV�A�t”} d¤m°QÑ7Ô7ð 	õ
 �t”}Ñ%Ô%ð 	+ð 	+ˆAØ�a�a�a˜˜˜˜A˜q˜q˜q�jÐ!Ð!Ô! Q¨¡UÑ*Ð!Ð!Ñ!Ð!ÝŒ]‰_Œ_ð 	Jð 	JÝ)+¬°i·n²nÀRÑ6HÔ6HÑ)IÔ)IˆDÔ!Ô&ð	Jð 	Jð 	Jñ 	Jô 	Jð 	Jð 	Jð 	Jð 	Jð 	Jð 	Jøøøð 	Jð 	Jð 	Jð 	Jå�$Ô(Ô/Ô4°cÑ:Ô:Ð:Ý�$Ô(Ô-Ô2°CÑ8Ô8Ð8Ý˜œÔ.Ô3Ñ4Ô4Ð4Ý�$”/Ô&Ô+¨SÑ1Ô1Ð1Ý˜Ô(Ô/Ô4Ñ5Ô5Ð5Ý�$Ô"Ô'Ô,¨cÑ2Ô2Ð2Ð2Ð2s   Å2F
Æ
FÆFNÚqueryr>   Ú
refer_bboxrL   Úvalue_shapesÚlistÚ
value_maskr@   rA   c           	     ó�  — |j         dd…         \  }}|j         d         }t          d„ |D ¦   «         ¦  «        |k    sJ ‚|                      |¦  «        }|�)|                     |d         t	          d¦  «        ¦  «        }|                     ||| j        | j        | j        z  ¦  «        }| j        | j	        z  }	|  
                    |¦  «                             ||| j        |	d¦  «        }
|                      |¦  «                             ||| j        |	¦  «        }t          j        |d¦  «        }|j         d         }|dk    r�t          j        ||j        |j        ¬¦  «                             d¦  «        }|dd…ddd…f                              d| j	        d¦  «                             |	d¦  «        }|dd…dd…ddd…dd…f         |
|z  z   }nS|d	k    r:|dd…dd…ddd…dd…f         |
| j	        z  |dd…dd…ddd…dd…f         z  d
z  z   }nt+          d|› d�¦  «        ‚t-          ||||¦  «        }|                      |¦  «        S )aÝ  Perform forward pass for multiscale deformable attention.

        Args:
            query (torch.Tensor): Query tensor with shape [bs, query_length, C].
            refer_bbox (torch.Tensor): Reference bounding boxes with shape [bs, query_length, 1, 2 or 4], range in [0,
                1], top-left (0,0), bottom-right (1, 1). The size-1 axis broadcasts across n_levels.
            value (torch.Tensor): Value tensor with shape [bs, value_length, C].
            value_shapes (list): List with shape [n_levels, 2], [(H_0, W_0), (H_1, W_1), ..., (H_{L-1}, W_{L-1})].
            value_mask (torch.Tensor, optional): Mask tensor with shape [bs, value_length], True for padding elements,
                False for non-padding elements.

        Returns:
            (torch.Tensor): Output tensor with shape [bs, Length_{query}, C].

        References:
            https://github.com/PaddlePaddle/PaddleDetection/blob/develop/ppdet/modeling/transformers/deformable_transformer.py
        Nrg   r   c              3  ó8   K  — | ]}|d          |d         z  V — ŒdS )r   r   NrC   )r›   r×   s     r;   r�   z'MSDeformAttn.forward.<locals>.<genexpr>1  s.   è è € Ð5Ð5 1�1�Q”4˜!˜Aœ$‘;Ð5Ð5Ð5Ð5Ð5Ð5r<   r~   r   rh   )rn   Údevicerz   g      à?z5Last dim of reference_points must be 2 or 4, but got ú.)ri   Úsumrâ   Úmasked_fillr    ro   rÛ   rÙ   rÚ   rÜ   rà   rá   ÚFÚsoftmaxr€   Ú	as_tensorrn   rø   ÚflipÚexpandr¤   r¼   r   rã   )r9   rñ   rò   rL   ró   rõ   ÚbsÚlen_qÚlen_vÚn_total_pointsrà   rá   Ú
num_pointsÚoffset_normalizerÚsampling_locationsÚoutputs                   r;   rV   zMSDeformAttn.forward  s–  € ð2 ”K   ”O‰	ˆˆEØ”˜A”ˆÝÐ5Ð5¨Ð5Ñ5Ô5Ñ5Ô5¸Ò>Ð>Ð>Ð>à—’ Ñ&Ô&ˆØÐ!Ø×%Ò% j°Ô&;½UÀ1¹X¼XÑFÔFˆEØ—
’
˜2˜u d¤l°D´LÀDÄLÑ4PÑQÔQˆð œ¨¬Ñ6ˆØ×0Ò0°Ñ7Ô7×<Ò<¸RÀÈÌÐVdÐfgÑhÔhÐØ ×2Ò2°5Ñ9Ô9×>Ò>¸rÀ5È$Ì,ÐXfÑgÔgÐÝœIÐ&7¸Ñ<Ô<ÐØÔ% bÔ)ˆ
Ø˜Š?ˆ?Ý %¤°ÀEÄKÐX]ÔXdÐ eÑ eÔ e× jÒ jÐkmÑ nÔ nÐØ 1°!°!°!°T¸1¸1¸1°*Ô =× DÒ DÀRÈÌÐXZÑ [Ô [× cÒ cÐdrÐtuÑ vÔ vÐØ!+¨A¨A¨A¨q¨q¨q°$¸¸¸¸1¸1¸1Ð,<Ô!=Ð@PÐSdÑ@dÑ!dÐÐØ˜1Š_ˆ_à˜1˜1˜1˜a˜a˜a  q q q¨"¨1¨"Ð,Ô-Ð0@À4Ä=Ñ0PÐS]Ð^_Ð^_Ð^_ÐabÐabÐabÐdhÐjkÐjkÐjkÐmnÐmoÐmoÐ^oÔSpÑ0pÐsvÑ0vÑvð Ðõ ÐbÐU_ÐbÐbÐbÑcÔcÐcÝ4°U¸LÐJ\Ð^oÑpÔpˆØ×Ò Ñ'Ô'Ð'r<   )ru   rz   r   rz   )rÙ   r   rÚ   r   rÛ   r   rÜ   r   rX   )rñ   r>   rò   r>   rL   r>   ró   rô   rõ   r@   rA   r>   )r[   r\   r]   r^   r*   rä   rV   r`   ra   s   @r;   r   r   Î  sy   ø€ € € € € ðð ð(!ð !ð !ð !ð !ð !ð !ð<3ð 3ð 3ð6 +/ð3(ð 3(ð 3(ð 3(ð 3(ð 3(ð 3(ð 3(ð 3(r<   r   c                  ód   ‡ — e Zd ZdZ	 	 	 	 	 	 	 d$d%ˆ fd„Zed&d„¦   «         Zd'd„Z	 	 	 d(d)d#„Zˆ xZ	S )*r   a6  Deformable Transformer Decoder Layer inspired by PaddleDetection and Deformable-DETR implementations.

    This class implements a single decoder layer with self-attention, cross-attention using multiscale deformable
    attention, and a feedforward network.

    Attributes:
        self_attn (nn.MultiheadAttention): Self-attention module.
        dropout1 (nn.Dropout): Dropout after self-attention.
        norm1 (nn.LayerNorm): Layer normalization after self-attention.
        cross_attn (MSDeformAttn): Cross-attention module.
        dropout2 (nn.Dropout): Dropout after cross-attention.
        norm2 (nn.LayerNorm): Layer normalization after cross-attention.
        linear1 (nn.Linear): First linear layer in the feedforward network.
        act (nn.Module): Activation function.
        dropout3 (nn.Dropout): Dropout in the feedforward network.
        linear2 (nn.Linear): Second linear layer in the feedforward network.
        dropout4 (nn.Dropout): Dropout after the feedforward network.
        norm3 (nn.LayerNorm): Layer normalization after the feedforward network.

    References:
        https://github.com/PaddlePaddle/PaddleDetection/blob/develop/ppdet/modeling/transformers/deformable_transformer.py
        https://github.com/fundamentalvision/Deformable-DETR/blob/main/models/deformable_transformer.py
    ru   r   é   r   Nrz   rÙ   r   rÛ   Úd_ffnr   r    r!   r"   rÚ   rÜ   c                ó¬  •— t          ¦   «                              ¦   «          t          j        |||¬¦  «        | _        t          j        |¦  «        | _        t          j        |¦  «        | _        t          ||||¦  «        | _
        t          j        |¦  «        | _        t          j        |¦  «        | _        t          j        ||¦  «        | _        |€t          j        ¦   «         n|| _        t          j        |¦  «        | _        t          j        ||¦  «        | _        t          j        |¦  «        | _        t          j        |¦  «        | _        dS )aÕ  Initialize the DeformableTransformerDecoderLayer with the given parameters.

        Args:
            d_model (int): Model dimension.
            n_heads (int): Number of attention heads.
            d_ffn (int): Dimension of the feedforward network.
            dropout (float): Dropout probability.
            act (nn.Module): Activation function.
            n_levels (int): Number of feature levels.
            n_points (int): Number of sampling points.
        )r   N)r)   r*   r   r-   Ú	self_attnr5   r6   r2   r3   r   Ú
cross_attnr7   r4   r/   Úlinear1rÃ   r!   Údropout3Úlinear2Údropout4Únorm3)	r9   rÙ   rÛ   r  r   r!   rÚ   rÜ   r:   s	           €r;   r*   z*DeformableTransformerDecoderLayer.__init__e  s  ø€ õ* 	‰Œ×ÒÑÔÐõ Ô.¨w¸ÈÐQÑQÔQˆŒÝœ
 7Ñ+Ô+ˆŒÝ”\ 'Ñ*Ô*ˆŒ
õ ' w°¸'À8ÑLÔLˆŒÝœ
 7Ñ+Ô+ˆŒÝ”\ 'Ñ*Ô*ˆŒ
õ ”y ¨%Ñ0Ô0ˆŒØ # •2”7‘9”9�9°ˆŒÝœ
 7Ñ+Ô+ˆŒÝ”y ¨Ñ0Ô0ˆŒÝœ
 7Ñ+Ô+ˆŒÝ”\ 'Ñ*Ô*ˆŒ
ˆ
ˆ
r<   r=   r>   r?   r@   rA   c                ó   — |€| n| |z   S )z;Add positional embeddings to the input tensor, if provided.rC   rD   s     r;   rE   z0DeformableTransformerDecoderLayer.with_pos_embedŽ  rF   r<   Útgtc           	     óø   — |                       |                      |                      |                      |¦  «        ¦  «        ¦  «        ¦  «        }||                      |¦  «        z   }|                      |¦  «        S )zÕPerform forward pass through the Feed-Forward Network part of the layer.

        Args:
            tgt (torch.Tensor): Input tensor.

        Returns:
            (torch.Tensor): Output tensor after FFN.
        )r  r  r!   r  r  r  )r9   r  Útgt2s      r;   Úforward_ffnz-DeformableTransformerDecoderLayer.forward_ffn“  s_   € ð �|Š|˜DŸMšM¨$¯(ª(°4·<²<ÀÑ3DÔ3DÑ*EÔ*EÑFÔFÑGÔGˆØ�D—M’M $Ñ'Ô'Ñ'ˆØ�zŠz˜#‰ŒÐr<   Úembedrò   ÚfeatsÚshapesrô   Úpadding_maskrM   Ú	query_posc                ón  — |                       ||¦  «        x}}	|                      |                     dd¦  «        |	                     dd¦  «        |                     dd¦  «        |¬¦  «        d                              dd¦  «        }
||                      |
¦  «        z   }|                      |¦  «        }|                      |                       ||¦  «        |                     d¦  «        |||¦  «        }
||                      |
¦  «        z   }|                      |¦  «        }|  	                    |¦  «        S )a?  Perform the forward pass through the entire decoder layer.

        Args:
            embed (torch.Tensor): Input embeddings.
            refer_bbox (torch.Tensor): Reference bounding boxes.
            feats (torch.Tensor): Feature maps.
            shapes (list): Feature shapes.
            padding_mask (torch.Tensor, optional): Padding mask.
            attn_mask (torch.Tensor, optional): Attention mask.
            query_pos (torch.Tensor, optional): Query position embeddings.

        Returns:
            (torch.Tensor): Output tensor after decoder layer.
        r   r   )rM   rg   )
rE   r  Ú	transposer6   r3   r  Ú	unsqueezer7   r4   r  )r9   r  rò   r  r  r  rM   r  rO   rP   r  s              r;   rV   z)DeformableTransformerDecoderLayer.forward   s$  € ð2 ×#Ò# E¨9Ñ5Ô5Ð5ˆˆAØ�nŠn˜QŸ[š[¨¨AÑ.Ô.°·²¸A¸qÑ0AÔ0AÀ5Ç?Â?ÐSTÐVWÑCXÔCXÐdmˆnÑnÔnØô
ç
Š)�A�q‰/Œ/ð 	ð ˜Ÿš cÑ*Ô*Ñ*ˆØ—
’
˜5Ñ!Ô!ˆð �oŠoØ×Ò  yÑ1Ô1°:×3GÒ3GÈÑ3JÔ3JÈEÐSYÐ[gñ
ô 
ˆð ˜Ÿš cÑ*Ô*Ñ*ˆØ—
’
˜5Ñ!Ô!ˆð ×Ò Ñ&Ô&Ð&r<   )ru   r   r
  r   Nrz   rz   )rÙ   r   rÛ   r   r  r   r   r    r!   r"   rÚ   r   rÜ   r   rY   )r  r>   rA   r>   rZ   )r  r>   rò   r>   r  r>   r  rô   r  r@   rM   r@   r  r@   rA   r>   )
r[   r\   r]   r^   r*   r_   rE   r  rV   r`   ra   s   @r;   r   r   L  s¼   ø€ € € € € ðð ð4 ØØØØ $ØØð'+ð '+ð '+ð '+ð '+ð '+ð '+ðR ð7ð 7ð 7ñ „\ð7ðð ð ð ð& -1Ø)-Ø)-ð('ð ('ð ('ð ('ð ('ð ('ð ('ð ('ð ('r<   r   c                  ó4   ‡ — e Zd ZdZddˆ fd	„Z	 	 ddd„Zˆ xZS )r   aq  Deformable Transformer Decoder based on PaddleDetection implementation.

    This class implements a complete deformable transformer decoder with multiple decoder layers and prediction heads
    for bounding box regression and classification.

    Attributes:
        layers (nn.ModuleList): List of decoder layers.
        num_layers (int): Number of decoder layers.
        hidden_dim (int): Hidden dimension.
        eval_idx (int): Index of the layer to use during evaluation.

    References:
        https://github.com/PaddlePaddle/PaddleDetection/blob/develop/ppdet/modeling/transformers/deformable_transformer.py
    rh   r°   r   Údecoder_layerrµ   r˜   Úeval_idxc                ó²   •— t          ¦   «                              ¦   «          t          ||¦  «        | _        || _        || _        |dk    r|n||z   | _        dS )aL  Initialize the DeformableTransformerDecoder with the given parameters.

        Args:
            hidden_dim (int): Hidden dimension.
            decoder_layer (nn.Module): Decoder layer module.
            num_layers (int): Number of decoder layers.
            eval_idx (int): Index of the layer to use during evaluation.
        r   N)r)   r*   r
   r»   r˜   r°   r#  )r9   r°   r"  r˜   r#  r:   s        €r;   r*   z%DeformableTransformerDecoder.__init__Û  sV   ø€ õ 	‰Œ×ÒÑÔÐÝ! -°Ñ<Ô<ˆŒØ$ˆŒØ$ˆŒØ$,°¢M M˜˜°zÀHÑ7LˆŒˆˆr<   Nr  r>   rò   r  r  rô   Ú	bbox_headÚ
score_headÚpos_mlprM   r@   r  c
                ó(  — |}
g }g }d}|                      ¦   «         }t          | j        ¦  «        D �]9\  }} ||
||||	| ||¦  «        ¦  «        }
 ||         |
¦  «        }t          j         |t	          |¦  «        z   ¦  «        }| j        rx|                      ||         |
¦  «        ¦  «         |dk    r|                     |¦  «         n~|                     t          j         |t	          |¦  «        z   ¦  «        ¦  «         nF|| j        k    r;|                      ||         |
¦  «        ¦  «         |                     |¦  «          n!|}| j        r|                     ¦   «         n|}�Œ;t          j	        |¦  «        t          j	        |¦  «        fS )aà  Perform the forward pass through the entire decoder.

        Args:
            embed (torch.Tensor): Decoder embeddings.
            refer_bbox (torch.Tensor): Reference bounding boxes.
            feats (torch.Tensor): Image features.
            shapes (list): Feature shapes.
            bbox_head (nn.Module): Bounding box prediction head.
            score_head (nn.Module): Score prediction head.
            pos_mlp (nn.Module): Position MLP.
            attn_mask (torch.Tensor, optional): Attention mask.
            padding_mask (torch.Tensor, optional): Padding mask.

        Returns:
            dec_bboxes (torch.Tensor): Decoded bounding boxes.
            dec_cls (torch.Tensor): Decoded classification scores.
        Nr   )
r²   rÁ   r»   r€   r   ÚtrainingÚappendr#  Údetachrê   )r9   r  rò   r  r  r%  r&  r'  rM   r  r  Ú
dec_bboxesÚdec_clsÚlast_refined_bboxrÅ   rÆ   ÚbboxÚrefined_bboxs                     r;   rV   z$DeformableTransformerDecoder.forwardê  s£  € ð: ˆØˆ
ØˆØ ÐØ×'Ò'Ñ)Ô)ˆ
Ý! $¤+Ñ.Ô.ð 	Rñ 	R‰HˆAˆuØ�U˜6 :¨u°f¸lÈIÐW^ÐW^Ð_iÑWjÔWjÑkÔkˆFà�9˜Q”< Ñ'Ô'ˆDÝ œ=¨µÀ
Ñ0KÔ0KÑ)KÑLÔLˆLàŒ}ð 	Ø—’˜}˜z¨!œ}¨VÑ4Ô4Ñ5Ô5Ð5Ø˜’6�6Ø×%Ò% lÑ3Ô3Ð3Ð3à×%Ò%¥e¤m°D½?ÐK\Ñ;]Ô;]Ñ4]Ñ&^Ô&^Ñ_Ô_Ð_Ð_Ø�d”mÒ#Ð#Ø—’˜}˜z¨!œ}¨VÑ4Ô4Ñ5Ô5Ð5Ø×!Ò! ,Ñ/Ô/Ð/Ø�à ,ÐØ26´-ÐQ˜×,Ò,Ñ.Ô.Ð.À\ˆJ‰JåŒ{˜:Ñ&Ô&­¬°GÑ(<Ô(<Ð<Ð<r<   )rh   )r°   r   r"  rµ   r˜   r   r#  r   )NN)r  r>   rò   r>   r  r>   r  rô   r%  rµ   r&  rµ   r'  rµ   rM   r@   r  r@   r•   ra   s   @r;   r   r   Ë  st   ø€ € € € € ðð ðMð Mð Mð Mð Mð Mð Mð0 *.Ø,0ð6=ð 6=ð 6=ð 6=ð 6=ð 6=ð 6=ð 6=ð 6=r<   r   ) r^   Ú
__future__r   rè   r€   Útorch.nn.functionalr   Ú
functionalrü   Útorch.nn.initr   r   Úultralytics.utils.torch_utilsr   rž   r	   Úutilsr
   r   r   Ú__all__r¾   r   r   r   r   r   r   r   r   r   r   rC   r<   r;   ú<module>r8     sß  ðà Ð à "Ð "Ð "Ð "Ð "Ð "à €€€à €€€Ø Ð Ð Ð Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø 4Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4Ð 4à 4Ð 4Ð 4Ð 4Ð 4Ð 4à Ð Ð Ð Ð Ð Ø TÐ TÐ TÐ TÐ TÐ TÐ TÐ TÐ TÐ Tð€ðHKð HKð HKð HKð HK˜bœiñ HKô HKð HKðVIlð Ilð Ilð Ilð IlÐ"ñ Ilô Ilð IlðX)ð )ð )ð )ð )�r”yñ )ô )ð )ð>+Vð +Vð +Vð +Vð +V�r”yñ +Vô +Vð +Vð\1ð 1ð 1ð 1ð 1ˆrŒyñ 1ô 1ð 1ð8@Eð @Eð @Eð @Eð @Eˆ"Œ)ñ @Eô @Eð @EðF(Ið (Ið (Ið (Ið (I�"”)ñ (Iô (Ið (IðV{(ð {(ð {(ð {(ð {(�2”9ñ {(ô {(ð {(ð||'ð |'ð |'ð |'ð |'¨¬	ñ |'ô |'ð |'ð~U=ð U=ð U=ð U=ð U= 2¤9ñ U=ô U=ð U=ð U=ð U=r<   