
    /j}              
           d dl Z d dlZd dlZd dlZd dlmZ d dlmc mZ	 d dl
mZ dZd ZddZd Zdd	Zd
ej        dej        dej        dej        dej        f
dZdS )    N)uniform_)inverse_sigmoid#multi_scale_deformable_attn_pytorchc                 ^     t          j         fdt          |          D                       S )a  Create a list of cloned modules from the given module.

    Args:
        module (nn.Module): The module to be cloned.
        n (int): Number of clones to create.

    Returns:
        (nn.ModuleList): A ModuleList containing n clones of the input module.

    Examples:
        >>> import torch.nn as nn
        >>> layer = nn.Linear(10, 10)
        >>> clones = _get_clones(layer, 3)
        >>> len(clones)
        3
    c                 8    g | ]}t          j                  S  )copydeepcopy).0_modules     a/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/ultralytics/nn/modules/utils.py
<listcomp>z_get_clones.<locals>.<listcomp>    s#    BBBA$-//BBB    )nn
ModuleListrange)r   ns   ` r   _get_clonesr      s0    " =BBBBqBBBCCCr   {Gz?c                 R    t          t          j        d| z
  | z                       S )a  Initialize conv/fc bias value according to a given probability value.

    This function calculates the bias initialization value based on a prior probability using the inverse sigmoid
    (logit)
    function. It's commonly used in object detection models to initialize classification layers with a specific positive
    prediction probability.

    Args:
        prior_prob (float, optional): Prior probability for bias initialization.

    Returns:
        (float): Bias initialization value calculated from the prior probability.

    Examples:
        >>> bias = bias_init_with_prob(0.01)
        >>> print(f"Bias initialization value: {bias:.4f}")
        Bias initialization value: -4.5951
       )floatnplog)
prior_probs    r   bias_init_with_probr   #   s(    & "&!j.J6777888r   c                     dt          j        | j        j        d                   z  }t	          | j        | |           t          | d          r | j        t	          | j        | |           dS dS dS )a  Initialize the weights and biases of a linear module.

    This function initializes the weights of a linear module using a uniform distribution within bounds calculated from
    the output dimension. If the module has a bias, it is also initialized.

    Args:
        module (nn.Module): Linear module to initialize.

    Examples:
        >>> import torch.nn as nn
        >>> linear = nn.Linear(10, 5)
        >>> linear_init(linear)
    r   r   biasN)mathsqrtweightshaper   hasattrr   )r   bounds     r   linear_initr&   9   s}     	&--a0111EV]UFE***vv -6;#:ufe,,,,,- -#:#:r   h㈵>c                     |                      dd          } |                      |          }d| z
                       |          }t          j        ||z            S )a  Calculate the inverse sigmoid function for a tensor.

    This function applies the inverse of the sigmoid function to a tensor, which is useful in various neural network
    operations, particularly in attention mechanisms and coordinate transformations.

    Args:
        x (torch.Tensor): Input tensor with values in range [0, 1].
        eps (float, optional): Small epsilon value to prevent numerical instability.

    Returns:
        (torch.Tensor): Tensor after applying the inverse sigmoid function.

    Examples:
        >>> x = torch.tensor([0.2, 0.5, 0.8])
        >>> inverse_sigmoid(x)
        tensor([-1.3863,  0.0000,  1.3863])
    r   r   )minmax)r)   )clamptorchr   )xepsx1x2s       r   r   r   M   sW    $ 	
A1A	
S		B
a%3		B9R"Wr   valuevalue_spatial_shapessampling_locationsattention_weightsreturnc                 ~   | j         \  }}}}|j         \  }}}}	}
}|                     d |D             d          }d|z  dz
  }g }t          |          D ]\  }\  }}||                             d                              dd                              ||z  |||          }|dddddd|f                             dd                              dd          }t          j        ||ddd	
          }|                    |           |                    dd                              ||z  d||	|
z            }t          j
        |d                              d          |z                      d                              |||z  |          }|                    dd                                          S )a  Implement multi-scale deformable attention in PyTorch.

    This function performs deformable attention across multiple feature map scales, allowing the model to attend to
    different spatial locations with learned offsets.

    Args:
        value (torch.Tensor): The value tensor with shape (bs, num_keys, num_heads, embed_dims).
        value_spatial_shapes (torch.Tensor): Spatial shapes of the value tensor with shape (num_levels, 2).
        sampling_locations (torch.Tensor): The sampling locations with shape (bs, num_queries, num_heads, num_levels,
            num_points, 2).
        attention_weights (torch.Tensor): The attention weights with shape (bs, num_queries, num_heads, num_levels,
            num_points).

    Returns:
        (torch.Tensor): The output tensor with shape (bs, num_queries, num_heads * embed_dims).

    References:
        https://github.com/IDEA-Research/detrex/blob/main/detrex/layers/multi_scale_deform_attn.py
    c                     g | ]
\  }}||z  S r   r   )r   H_W_s      r   r   z7multi_scale_deformable_attn_pytorch.<locals>.<listcomp>   s     III&"bb2gIIIr   r   )dim   Nr   bilinearzerosF)modepadding_modealign_corners)r#   split	enumerateflatten	transposereshapeFgrid_sampleappendr,   stacksumview
contiguous)r1   r2   r3   r4   bsr   	num_heads
embed_dimsnum_queries
num_levels
num_points
value_listsampling_gridssampling_value_listlevelr8   r9   value_l_sampling_grid_l_sampling_value_l_outputs                        r   r   r   e   s   2 $); B9j;M;S8A{Iz:qII4HIIIqQQJ++a/N$%9:: 6 6xB
 e$,,Q//99!Q??GGYXbdfhjkk *!!!QQQ5.9CCAqIIQQRSUVWWM&Zg]b
 
 
 	""#45555 *33Aq99AA
Y;
Z(?  
(b	1	1	1	9	9"	=	=@Q	Q	R	b)j(+	6	6 
 Aq!!,,...r   )r   )r'   )r	   r    numpyr   r,   torch.nnr   torch.nn.functional
functionalrH   torch.nn.initr   __all__r   r   r&   r   Tensorr   r   r   r   <module>rd      s                         " " " " " "
BD D D(9 9 9 9,- - -(   08/<8/,8/ 8/ |	8/
 \8/ 8/ 8/ 8/ 8/ 8/r   