§
    l¬¸jK  ã            
       óš   — d dl Z d dlZd dlZd dlmc mZ d dlmZ dZd„ Z	dd„Z
dd„Zd	ej        d
edej        dej        dej        f
d„ZdS )é    N)Únn)Ú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     €úl/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/ultralytics/nn/modules/utils.pyú
<listcomp>z_get_clones.<locals>.<listcomp>   s#   ø€ ÐBÐBÐB°A�$œ-¨Ñ/Ô/ÐBÐBÐBó    )r   Ú
ModuleListÚrange)r   Úns   ` r   Ú_get_clonesr      s0   ø€ õ" Œ=ÐBÐBÐBÐB½¸q¹¼ÐBÑBÔBÑCÔCÐCr   ç{®Gáz„?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   çñ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   7   sW   € ð$ 	
�Š�A˜1ˆÑÔ€AØ	
�Š�SˆÑ	Ô	€BØ
ˆa‰%�Š˜3ˆÑ	Ô	€BÝŒ9�R˜"‘WÑÔÐr   ÚvalueÚvalue_spatial_shapesÚsampling_locationsÚattention_weightsÚreturnc           
      ó’  — | j         \  }}}}|j         \  }}}}	}|	t          |¦  «        z  }
|                      dddd¦  «                             dd¦  «                             d„ |D ¦   «         d¬¦  «        }d|z  dz
                       ddddd¦  «                             dd¦  «                             |
d	¬¦  «        }g }t          |¦  «        D ][\  }\  }}||                              ||z  |||¦  «        }|                     t          j	        |||         d
dd¬¦  «        ¦  «         Œ\|                     dddd¦  «                             ||z  d||	¦  «        }t          j        |d¬¦  «        |z                       d¦  «                             |||z  |¦  «        }|                     dd¦  «                             ¦   «         S )a  Implement multi-scale deformable attention in PyTorch.

    Folds the (num_levels, num_points) axes into a single num_total_points axis so every traced tensor stays at rank <=
    5, the maximum rank supported by CoreML's MIL converter. Numerically equivalent to the rank-6 reference
    implementation on CUDA and CPU.

    Args:
        value (torch.Tensor): Value tensor with shape (bs, num_keys, num_heads, embed_dims).
        value_spatial_shapes (list): Per-level spatial shapes as [(H_0, W_0), ..., (H_{L-1}, W_{L-1})].
        sampling_locations (torch.Tensor): Sampling locations with shape (bs, num_queries, num_heads, num_levels *
            num_points, 2).
        attention_weights (torch.Tensor): Attention weights with shape (bs, num_queries, num_heads, num_levels *
            num_points).

    Returns:
        (torch.Tensor): 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
    r   é   é   r   c                 ó   — g | ]
\  }}||z  ‘ŒS r   r   )r   ÚhÚws      r   r   z7multi_scale_deformable_attn_pytorch.<locals>.<listcomp>n   s    € Ð?gÐ?gÐ?gÉ$È!ÈQÀÀAÁÐ?gÐ?gÐ?gr   éÿÿÿÿ)Údimé   éþÿÿÿÚbilinearÚzerosF)ÚmodeÚpadding_modeÚalign_corners)ÚshapeÚlenÚpermuteÚflattenÚsplitÚ	enumerateÚreshapeÚappendÚFÚgrid_sampler"   ÚcatÚsumÚviewÚ	transposeÚ
contiguous)r'   r(   r)   r*   Úbsr   Ú	num_headsÚ
embed_dimsÚnum_queriesÚnum_total_pointsÚ
num_pointsÚ
value_listÚsampling_gridsÚsampling_value_listÚlevelr0   r1   Úvalue_lÚoutputs                      r   r   r   O   só  € ð4 $)¤;Ñ €Bˆˆ9�jØ-?Ô-EÑ*€A€{�AÐ'¨Ø!¥SÐ)=Ñ%>Ô%>Ñ>€Jð —’˜q ! Q¨Ñ*Ô*×2Ò2°1°aÑ8Ô8×>Ò>Ð?gÐ?gÐRfÐ?gÑ?gÔ?gÐmoÐ>ÑpÔp€JàÐ,Ñ,¨qÑ0×9Ò9¸!¸QÀÀ1ÀaÑHÔH×PÒPÐQRÐTUÑVÔV×\Ò\Ð]gÐmoÐ\ÑpÔp€NàÐÝ"Ð#7Ñ8Ô8ð 
ð 
‰ˆ‰v��1Ø˜UÔ#×+Ò+¨B°©N¸JÈÈ1ÑMÔMˆØ×"Ò"ÝŒM˜' >°%Ô#8¸zÐX_ÐotÐuÑuÔuñ	
ô 	
ð 	
ð 	
ð *×1Ò1°!°Q¸¸1Ñ=Ô=×EÒEÀbÈ9ÁnÐVWÐYdÐfvÑwÔwÐå	ŒÐ&¨BÐ	/Ñ	/Ô	/Ð2CÑ	Cß	ŠˆR‰Œß	Šˆb�)˜jÑ(¨+Ñ	6Ô	6ð ð
 ×Ò˜A˜qÑ!Ô!×,Ò,Ñ.Ô.Ð.r   )r   )r   )r	   Únumpyr   r"   Útorch.nn.functionalr   Ú
functionalrC   Ú__all__r   r   r   ÚTensorÚlistr   r   r   r   ú<module>r\      så   ðð €€€à Ð Ð Ð Ø €€€Ø Ð Ð Ð Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð à
B€ðDð Dð Dð(9ð 9ð 9ð 9ð,ð ð ð ð0//ØŒ<ð//àð//ð œð//ð ”|ð	//ð
 „\ð//ð //ð //ð //ð //ð //r   