§
    l¬¸jU  ã                  óx   — d Z ddlmZ ddlmZ ddlZddlmZ ddlmZmZ ddl	m
Z
  G d	„ d
ej        ¦  «        ZdS )zGProvides utility to combine a vision backbone with a language backbone.é    )Úannotations)ÚcopyN)Únn)Ú
SDPBackendÚsdpa_kernelé   )ÚSam3DualViTDetNeckc                  ó^   ‡ — e Zd ZdZ	 	 	 	 ddˆ fd
„Z	 	 ddd„Zdd„Zdd„Zdd„Zdd d„Z	ˆ xZ
S )!ÚSAM3VLBackbonezîThis backbone combines a vision backbone and a language backbone without fusion. As such it is more of a
    convenience wrapper to handle the two backbones together.

    It adds support for activation checkpointing and compilation.
    Fr   Úvisualr	   Úcompile_visualÚboolÚact_ckpt_whole_vision_backboneÚ act_ckpt_whole_language_backbonec                óº   •— t          ¦   «                              ¦   «          |rt          j        |¦  «        n|| _        || _        || _        || _        || _        dS )ae  Initialize the backbone combiner.

        Args:
            visual (Sam3DualViTDetNeck): The vision backbone to use.
            text (nn.Module): The text encoder to use.
            compile_visual (bool): Whether to `torch.compile` the vision backbone.
            act_ckpt_whole_vision_backbone (bool): Whether to checkpoint activations for the whole vision backbone.
            act_ckpt_whole_language_backbone (bool): Whether to checkpoint activations for the whole language backbone.
            scalp (int): Number of trailing (lowest-resolution) feature levels to drop from the backbone output.
        N)	ÚsuperÚ__init__ÚtorchÚcompileÚvision_backboneÚlanguage_backboneÚscalpr   r   )Úselfr   Útextr   r   r   r   Ú	__class__s          €úw/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/ultralytics/models/sam/sam3/vl_combiner.pyr   zSAM3VLBackbone.__init__   s]   ø€ õ& 	‰Œ×ÒÑÔÐØLZÐ3fµ5´=ÀÑ3HÔ3HÐ3HÐ`fˆÔØ!%ˆÔØˆŒ
à.LˆÔ+Ø0PˆÔ-Ð-Ð-ó    NÚsamplesútorch.TensorÚcaptionsú	list[str]Úinput_boxesÚadditional_textúlist[str] | Nonec                ó„   — |                       |¦  «        }|                     |                      |||¦  «        ¦  «         |S )a  Forward pass of the backbone combiner.

        Args:
            samples (torch.Tensor): The input images.
            captions (list[str]): The input captions.
            input_boxes (torch.Tensor, optional): When the text contains box place-holders, the tensor containing their
                spatial features.
            additional_text (list[str], optional): Extra text (different from the captions) to encode in the same
                backbone forward pass.

        Returns:
            (dict): Output dictionary with the following keys: `vision_features` (the output of the vision backbone),
                `language_features` (the output of the language backbone), `language_mask` (the attention mask of the
                language backbone), `vision_pos_enc` (the positional encoding of the vision
                backbone) and, when `additional_text` is provided, `additional_text_features` and
                `additional_text_mask` (the language backbone output and attention mask for the additional text).
        )Úforward_imageÚupdateÚforward_text)r   r   r    r"   r#   Úoutputs         r   ÚforwardzSAM3VLBackbone.forward4   s@   € ð0 ×#Ò# GÑ,Ô,ˆØ�Š�d×'Ò'¨°+¸ÑOÔOÑPÔPÐPØˆr   c                ó$  — | j                              |¦  «        \  }}}}| j        dk    rD|d| j         …         |d| j         …         }}|�"|� |d| j         …         |d| j         …         }}d}|�|�|d         }|||dœ}|d         }||||dœS )zHForward pass of the vision backbone and get both SAM3 and SAM2 features.r   Néÿÿÿÿ©Úvision_featuresÚvision_pos_encÚbackbone_fpn)r.   r/   r0   Úsam2_backbone_out)r   r*   r   )	r   r   Úsam3_featuresÚsam3_posÚsam2_featuresÚsam2_posÚsam2_outputÚsam2_srcÚsam3_srcs	            r   r&   zSAM3VLBackbone.forward_imageP   så   € ð <@Ô;O×;WÒ;WÐX_Ñ;`Ô;`Ñ8ˆ�x °ØŒ:˜Š>ˆ>ð ˜m ¤ ˜mÔ,Ø˜˜DœJ˜;˜Ô'ð $ˆMð Ð(¨XÐ-Aà! - T¤Z K -Ô0Ø˜] ¤
˜{˜]Ô+ð  (�ð
 ˆàÐ$¨Ð)=Ø$ RÔ(ˆHà#+Ø"*Ø -ðð ˆKð ! Ô$ˆà'Ø&Ø)Ø!,ð	
ð 
ð 	
r   c                ó0  — | j                              |¦  «        }|d         }| j         j        €
J d¦   «         ‚| j                              || j         j        ¦  «        \  }}| j        dk    r |d| j         …         |d| j         …         }}|d         ||dœS )z>Forward pass of the vision backbone to get SAM2 features only.r,   NzSAM2 neck is not available.r   r-   )r   ÚtrunkÚ
sam2_convsÚsam_forward_feature_levelsr   )r   r   ÚxsÚxr4   r5   s         r   Úforward_image_sam2z!SAM3VLBackbone.forward_image_sam2r   s´   € àÔ!×'Ò'¨Ñ0Ô0ˆØˆrŒFˆàÔ#Ô.Ð:Ð:Ð<YÑ:Ô:Ð:Ø"&Ô"6×"QÒ"QÐRSÐUYÔUiÔUtÑ"uÔ"uÑˆ�xàŒ:˜Š>ˆ>ð ˜m ¤ ˜mÔ,Ø˜˜DœJ˜;˜Ô'ð $ˆMð  -¨RÔ0Ø&Ø)ð
ð 
ð 	
r   c                ó*  — i }t          |¦  «        }|�||z  }t          t          j        t          j        t          j        g¦  «        5  |                      ||¦  «        \  }}}ddd¦  «         n# 1 swxY w Y   |�:|dd…t          |¦  «         d…f         |d<   |t          |¦  «         d…         |d<   |dd…dt          |¦  «        …f         }|dt          |¦  «        …         }|dd…dt          |¦  «        …f         }||d<   ||d<   ||d<   |S )z!Forward pass of the text encoder.NÚadditional_text_featuresÚadditional_text_maskÚlanguage_featuresÚlanguage_maskÚlanguage_embeds)r   r   r   ÚMATHÚEFFICIENT_ATTENTIONÚFLASH_ATTENTIONr   Úlen)	r   r    r"   r#   r)   Útext_to_encodeÚtext_attention_maskÚtext_memoryÚtext_embedss	            r   r(   zSAM3VLBackbone.forward_text‡   s�  € àˆõ ˜h™œˆØÐ&ð ˜oÑ-ˆNå�*œ/­:Ô+IÍ:ÔKeÐfÑgÔgð 	pð 	pØ<@×<RÒ<RÐSaÐcnÑ<oÔ<oÑ9Ð ¨kð	pð 	pð 	pñ 	pô 	pð 	pð 	pð 	pð 	pð 	pð 	pøøøð 	pð 	pð 	pð 	pð Ð&Ø1<¸Q¸Q¸QÅÀ_ÑAUÔAUÐ@UÐ@WÐ@WÐ=WÔ1XˆFÐ-Ñ.Ø-@Å#ÀoÑBVÔBVÐAVÐAXÐAXÔ-YˆFÐ)Ñ*à! ! ! ! _¥s¨8¡}¤} _Ð"4Ô5ˆØ1°/µC¸±M´M°/ÔBÐØ! ! ! ! _¥s¨8¡}¤} _Ð"4Ô5ˆØ&1ˆÐ"Ñ#Ø"5ˆˆÑØ$/ˆÐ Ñ!àˆs   Á	A0Á0A4Á7A4Úimgszúlist[int] | Nonec                óJ   — |�|nddg}| j                              |¦  «         dS )z+Set the image size for the vision backbone.Nið  )r   Ú	set_imgsz)r   rN   s     r   rQ   zSAM3VLBackbone.set_imgsz¢   s1   € àÐ*��°°t°ˆØÔ×&Ò& uÑ-Ô-Ð-Ð-Ð-r   )FFFr   )r   r	   r   r   r   r   r   r   )NN)r   r   r    r!   r"   r   r#   r$   )r   r   )N)rN   rO   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r*   r&   r?   r(   rQ   Ú__classcell__)r   s   @r   r   r      sØ   ø€ € € € € ðð ð  %Ø/4Ø16ØðQð Qð Qð Qð Qð Qð Qð> %)Ø,0ðð ð ð ð ð8 
ð  
ð  
ð  
ðD
ð 
ð 
ð 
ð*ð ð ð ð6.ð .ð .ð .ð .ð .ð .ð .ð .r   r   )rU   Ú
__future__r   r   r   r   Útorch.nn.attentionr   r   Únecksr	   ÚModuler   © r   r   ú<module>r\      s·   ðð
 NÐ Mà "Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ð Ð à €€€Ø Ð Ð Ð Ð Ð Ø 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6Ð 6à %Ð %Ð %Ð %Ð %Ð %ðS.ð S.ð S.ð S.ð S.�R”Yñ S.ô S.ð S.ð S.ð S.r   