Ë
    T^(h/Q ã                   óR  — d Z ddlmZ ddlmZ ddlmZmZmZm	Z	m
Z
 ddlZddlZddlmZmZ ddlmZ dd	lmZmZ dd
lmZmZ ddlmZ ddlmZmZmZmZmZmZm Z  ddl!m"Z"m#Z#m$Z$  e«       rddl%m&Z&  ejN                  e(«      Z)dZ*dej                  dej                  fd„Z+dej                  dej                  fd„Z,e G d„ de«      «       Z-dedefd„Z.dedefd„Z/d„ Z0d„ Z1e G d„ de«      «       Z2e G d „ d!e«      «       Z3 G d"„ d#ejh                  «      Z5 G d$„ d%ejh                  «      Z6 G d&„ d'ejh                  «      Z7 G d(„ d)ejh                  «      Z8 G d*„ d+ejh                  «      Z9 G d,„ d-e«      Z:d.Z;d/Z<d0Z=d1Z>d2Z?d3Z@ G d4„ d5ejh                  «      ZA G d6„ d7ejh                  «      ZB G d8„ d9e:«      ZC G d:„ d;ejh                  «      ZD G d<„ d=e:«      ZE ee;«       G d>„ d?e:«      «       ZF G d@„ dAejh                  «      ZG G dB„ dCejh                  «      ZH G dD„ dEe:«      ZIg dF¢ZJy)GzPyTorch OWLv2 model.é    )Ú	dataclass)Ú	lru_cache)ÚAnyÚDictÚOptionalÚTupleÚUnionN)ÚTensorÚnné   )ÚACT2FN)Ú _create_4d_causal_attention_maskÚ_prepare_4d_attention_mask)ÚBaseModelOutputÚBaseModelOutputWithPooling)ÚPreTrainedModel)ÚModelOutputÚadd_start_docstringsÚ%add_start_docstrings_to_model_forwardÚis_vision_availableÚloggingÚreplace_return_docstringsÚ	torch_inté   )ÚOwlv2ConfigÚOwlv2TextConfigÚOwlv2VisionConfig)Úcenter_to_corners_formatz"google/owlv2-base-patch16-ensembleÚlogitsÚreturnc                 ó’   — t         j                  j                  | t        j                  t        | «      | j                  ¬«      «      S )N©Údevice)r   Ú
functionalÚcross_entropyÚtorchÚarangeÚlenr#   )r   s    úf/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/owlv2/modeling_owlv2.pyÚcontrastive_lossr*   5   s/   € Ü�=‰=×&Ñ& v¬u¯|©|¼CÀ»KÐPV×P]ÑP]Ô/^Ó_Ð_ó    Ú
similarityc                 óZ   — t        | «      }t        | j                  «       «      }||z   dz  S )Ng       @)r*   Út)r,   Úcaption_lossÚ
image_losss      r)   Ú
owlv2_lossr1   :   s,   € Ü# JÓ/€LÜ! *§,¡,£.Ó1€JØ˜:Ñ%¨Ñ,Ð,r+   c                   ó  — e Zd ZU dZdZeej                     ed<   dZ	eej                     ed<   dZ
eej                     ed<   dZeej                     ed<   dZeej                     ed<   dZeed<   dZeed	<   d
ee   fd„Zy)ÚOwlv2Outputa!  
    Args:
        loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `return_loss` is `True`):
            Contrastive loss for image-text similarity.
        logits_per_image (`torch.FloatTensor` of shape `(image_batch_size, text_batch_size)`):
            The scaled dot product scores between `image_embeds` and `text_embeds`. This represents the image-text
            similarity scores.
        logits_per_text (`torch.FloatTensor` of shape `(text_batch_size, image_batch_size)`):
            The scaled dot product scores between `text_embeds` and `image_embeds`. This represents the text-image
            similarity scores.
        text_embeds (`torch.FloatTensor` of shape `(batch_size * num_max_text_queries, output_dim`):
            The text embeddings obtained by applying the projection layer to the pooled output of [`Owlv2TextModel`].
        image_embeds (`torch.FloatTensor` of shape `(batch_size, output_dim`):
            The image embeddings obtained by applying the projection layer to the pooled output of
            [`Owlv2VisionModel`].
        text_model_output (Tuple[`BaseModelOutputWithPooling`]):
            The output of the [`Owlv2TextModel`].
        vision_model_output (`BaseModelOutputWithPooling`):
            The output of the [`Owlv2VisionModel`].
    NÚlossÚlogits_per_imageÚlogits_per_textÚtext_embedsÚimage_embedsÚtext_model_outputÚvision_model_outputr    c                 óH   ‡ — t        ˆ fd„‰ j                  «       D «       «      S )Nc              3   ód   •K  — | ]'  }|d vr‰|   nt        ‰|«      j                  «       –— Œ) y­w©)r9   r:   N©ÚgetattrÚto_tuple©Ú.0ÚkÚselfs     €r)   ú	<genexpr>z'Owlv2Output.to_tuple.<locals>.<genexpr>`   ó=   øè ø€ ò 
àð Ð LÑLˆD�ŠGÔRYÐZ^Ð`aÓRb×RkÑRkÓRmÓmñ
ùó   ƒ-0©ÚtupleÚkeys©rD   s   `r)   r@   zOwlv2Output.to_tuple_   ó#   ø€ Üó 
à—Y‘Y“[ô
ó 
ð 	
r+   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r4   r   r&   ÚFloatTensorÚ__annotations__r5   r6   r7   r8   r9   r   r:   r   r   r@   © r+   r)   r3   r3   @   s›   … ñð* )-€Dˆ(�5×$Ñ$Ñ
%Ó,Ø48Ð�h˜u×0Ñ0Ñ1Ó8Ø37€O�X˜e×/Ñ/Ñ0Ó7Ø/3€K�˜%×+Ñ+Ñ,Ó3Ø04€L�(˜5×,Ñ,Ñ-Ó4Ø48ÐÐ1Ó8Ø6:ÐÐ3Ó:ð
˜% ™*ô 
r+   r3   r.   c                 ó  — | j                  «       r>| j                  t        j                  t        j                  fv r| S | j                  «       S | j                  t        j                  t        j                  fv r| S | j                  «       S ©N)	Úis_floating_pointÚdtyper&   Úfloat32Úfloat64ÚfloatÚint32Úint64Úint)r.   s    r)   Ú_upcastr^   g   s`   € à×ÑÔØ—G‘G¤§¡¬u¯}©}Ð=Ñ=ˆqÐLÀ1Ç7Á7Ã9ÐLà—G‘G¤§¡¬U¯[©[Ð9Ñ9ˆqÐF¸q¿u¹u»wÐFr+   Úboxesc                 óf   — t        | «      } | dd…df   | dd…df   z
  | dd…df   | dd…df   z
  z  S )a´  
    Computes the area of a set of bounding boxes, which are specified by its (x1, y1, x2, y2) coordinates.

    Args:
        boxes (`torch.FloatTensor` of shape `(number_of_boxes, 4)`):
            Boxes for which the area will be computed. They are expected to be in (x1, y1, x2, y2) format with `0 <= x1
            < x2` and `0 <= y1 < y2`.

    Returns:
        `torch.FloatTensor`: a tensor containing the area for each box.
    Né   r   r   r   )r^   )r_   s    r)   Úbox_arearb   p   sB   € ô �E‹N€EØ’!�Q�$‰K˜%¢ 1 ™+Ñ%¨%²°1°©+¸ºaÀ¸d¹Ñ*CÑDÐDr+   c                 ó^  — t        | «      }t        |«      }t        j                  | d d …d d d…f   |d d …d d…f   «      }t        j                  | d d …d dd …f   |d d …dd …f   «      }||z
  j	                  d¬«      }|d d …d d …df   |d d …d d …df   z  }|d d …d f   |z   |z
  }||z  }	|	|fS )Nra   r   ©Úminr   )rb   r&   Úmaxre   Úclamp)
Úboxes1Úboxes2Úarea1Úarea2Úleft_topÚright_bottomÚwidth_heightÚinterÚunionÚious
             r)   Úbox_iourr   �   sÊ   € Ü�VÓ€EÜ�VÓ€Eä�y‰y˜¢ 4¨¨!¨ Ñ,¨f²Q¸¸¸°U©mÓ<€HÜ—9‘9˜V¢A t¨Q©R KÑ0°&º¸A¹B¸±-Ó@€Là  8Ñ+×2Ñ2°qÐ2Ó9€LØššA˜q˜Ñ! L²²A°q°Ñ$9Ñ9€Eà’!�T�'‰N˜UÑ" UÑ*€Eà
�%‰-€CØ�ˆ:Ðr+   c                 ó  — | dd…dd…f   | dd…dd…f   k\  j                  «       st        d| › �«      ‚|dd…dd…f   |dd…dd…f   k\  j                  «       st        d|› �«      ‚t        | |«      \  }}t        j                  | dd…ddd…f   |dd…dd…f   «      }t        j
                  | dd…ddd…f   |dd…dd…f   «      }||z
  j                  d¬«      }|dd…dd…df   |dd…dd…df   z  }|||z
  |z  z
  S )zâ
    Generalized IoU from https://giou.stanford.edu/. The boxes should be in [x0, y0, x1, y1] (corner) format.

    Returns:
        `torch.FloatTensor`: a [N, M] pairwise matrix, where N = len(boxes1) and M = len(boxes2)
    Nra   z<boxes1 must be in [x0, y0, x1, y1] (corner) format, but got z<boxes2 must be in [x0, y0, x1, y1] (corner) format, but got r   rd   r   )ÚallÚ
ValueErrorrr   r&   re   rf   rg   )rh   ri   rq   rp   Útop_leftÚbottom_rightrn   Úareas           r)   Úgeneralized_box_ioury   ’   s*  € ð ’1�a‘b�5‰M˜V¢A r¨ r E™]Ñ*×/Ñ/Ô1ÜÐWÐX^ÐW_Ð`ÓaÐaØ’1�a‘b�5‰M˜V¢A r¨ r E™]Ñ*×/Ñ/Ô1ÜÐWÐX^ÐW_Ð`ÓaÐaÜ˜ Ó(�J€Cˆä�y‰y˜¢ 4¨¨!¨ Ñ,¨f²Q¸¸¸°U©mÓ<€HÜ—9‘9˜V¢A t¨Q©R KÑ0°&º¸A¹B¸±-Ó@€Là  8Ñ+×2Ñ2°qÐ2Ó9€LØšš1˜a˜Ñ  <²²1°a°Ñ#8Ñ8€Dà�$˜‘, $Ñ&Ñ&Ð&r+   c                   ól  — e Zd ZU dZdZeej                     ed<   dZ	ee
   ed<   dZeej                     ed<   dZeej                     ed<   dZeej                     ed<   dZeej                     ed<   dZeej                     ed	<   dZeej                     ed
<   dZeed<   dZeed<   dee   fd„Zy)ÚOwlv2ObjectDetectionOutputa‰	  
    Output type of [`Owlv2ForObjectDetection`].

    Args:
        loss (`torch.FloatTensor` of shape `(1,)`, *optional*, returned when `labels` are provided)):
            Total loss as a linear combination of a negative log-likehood (cross-entropy) for class prediction and a
            bounding box loss. The latter is defined as a linear combination of the L1 loss and the generalized
            scale-invariant IoU loss.
        loss_dict (`Dict`, *optional*):
            A dictionary containing the individual losses. Useful for logging.
        logits (`torch.FloatTensor` of shape `(batch_size, num_patches, num_queries)`):
            Classification logits (including no-object) for all queries.
        objectness_logits (`torch.FloatTensor` of shape `(batch_size, num_patches, 1)`):
            The objectness logits of all image patches. OWL-ViT represents images as a set of image patches where the
            total number of patches is (image_size / patch_size)**2.
        pred_boxes (`torch.FloatTensor` of shape `(batch_size, num_patches, 4)`):
            Normalized boxes coordinates for all queries, represented as (center_x, center_y, width, height). These
            values are normalized in [0, 1], relative to the size of each individual image in the batch (disregarding
            possible padding). You can use [`~Owlv2ImageProcessor.post_process_object_detection`] to retrieve the
            unnormalized bounding boxes.
        text_embeds (`torch.FloatTensor` of shape `(batch_size, num_max_text_queries, output_dim`):
            The text embeddings obtained by applying the projection layer to the pooled output of [`Owlv2TextModel`].
        image_embeds (`torch.FloatTensor` of shape `(batch_size, patch_size, patch_size, output_dim`):
            Pooled output of [`Owlv2VisionModel`]. OWLv2 represents images as a set of image patches and computes image
            embeddings for each patch.
        class_embeds (`torch.FloatTensor` of shape `(batch_size, num_patches, hidden_size)`):
            Class embeddings of all image patches. OWLv2 represents images as a set of image patches where the total
            number of patches is (image_size / patch_size)**2.
        text_model_output (Tuple[`BaseModelOutputWithPooling`]):
            The output of the [`Owlv2TextModel`].
        vision_model_output (`BaseModelOutputWithPooling`):
            The output of the [`Owlv2VisionModel`].
    Nr4   Ú	loss_dictr   Úobjectness_logitsÚ
pred_boxesr7   r8   Úclass_embedsr9   r:   r    c                 óH   ‡ — t        ˆ fd„‰ j                  «       D «       «      S )Nc              3   ód   •K  — | ]'  }|d vr‰|   nt        ‰|«      j                  «       –— Œ) y­wr=   r>   rA   s     €r)   rE   z6Owlv2ObjectDetectionOutput.to_tuple.<locals>.<genexpr>Ú   rF   rG   rH   rK   s   `r)   r@   z#Owlv2ObjectDetectionOutput.to_tupleÙ   rL   r+   )rM   rN   rO   rP   r4   r   r&   rQ   rR   r|   r   r   r}   r~   r7   r8   r   r9   r   r:   r   r   r@   rS   r+   r)   r{   r{   ª   sÔ   … ñ ðD )-€Dˆ(�5×$Ñ$Ñ
%Ó,Ø $€Iˆx˜‰~Ó$Ø*.€FˆH�U×&Ñ&Ñ'Ó.Ø59Ð�x × 1Ñ 1Ñ2Ó9Ø.2€J�˜×*Ñ*Ñ+Ó2Ø/3€K�˜%×+Ñ+Ñ,Ó3Ø04€L�(˜5×,Ñ,Ñ-Ó4Ø04€L�(˜5×,Ñ,Ñ-Ó4Ø48ÐÐ1Ó8Ø6:ÐÐ3Ó:ð
˜% ™*ô 
r+   r{   c                   ó0  — e Zd ZU dZdZeej                     ed<   dZ	eej                     ed<   dZ
eej                     ed<   dZeej                     ed<   dZeej                     ed<   dZeej                     ed<   dZeed	<   dZeed
<   dee   fd„Zy)Ú%Owlv2ImageGuidedObjectDetectionOutputaÇ  
    Output type of [`Owlv2ForObjectDetection.image_guided_detection`].

    Args:
        logits (`torch.FloatTensor` of shape `(batch_size, num_patches, num_queries)`):
            Classification logits (including no-object) for all queries.
        target_pred_boxes (`torch.FloatTensor` of shape `(batch_size, num_patches, 4)`):
            Normalized boxes coordinates for all queries, represented as (center_x, center_y, width, height). These
            values are normalized in [0, 1], relative to the size of each individual target image in the batch
            (disregarding possible padding). You can use [`~Owlv2ImageProcessor.post_process_object_detection`] to
            retrieve the unnormalized bounding boxes.
        query_pred_boxes (`torch.FloatTensor` of shape `(batch_size, num_patches, 4)`):
            Normalized boxes coordinates for all queries, represented as (center_x, center_y, width, height). These
            values are normalized in [0, 1], relative to the size of each individual query image in the batch
            (disregarding possible padding). You can use [`~Owlv2ImageProcessor.post_process_object_detection`] to
            retrieve the unnormalized bounding boxes.
        image_embeds (`torch.FloatTensor` of shape `(batch_size, patch_size, patch_size, output_dim`):
            Pooled output of [`Owlv2VisionModel`]. OWLv2 represents images as a set of image patches and computes
            image embeddings for each patch.
        query_image_embeds (`torch.FloatTensor` of shape `(batch_size, patch_size, patch_size, output_dim`):
            Pooled output of [`Owlv2VisionModel`]. OWLv2 represents images as a set of image patches and computes
            image embeddings for each patch.
        class_embeds (`torch.FloatTensor` of shape `(batch_size, num_patches, hidden_size)`):
            Class embeddings of all image patches. OWLv2 represents images as a set of image patches where the total
            number of patches is (image_size / patch_size)**2.
        text_model_output (Tuple[`BaseModelOutputWithPooling`]):
            The output of the [`Owlv2TextModel`].
        vision_model_output (`BaseModelOutputWithPooling`):
            The output of the [`Owlv2VisionModel`].
    Nr   r8   Úquery_image_embedsÚtarget_pred_boxesÚquery_pred_boxesr   r9   r:   r    c                 óH   ‡ — t        ˆ fd„‰ j                  «       D «       «      S )Nc              3   ód   •K  — | ]'  }|d vr‰|   nt        ‰|«      j                  «       –— Œ) y­wr=   r>   rA   s     €r)   rE   zAOwlv2ImageGuidedObjectDetectionOutput.to_tuple.<locals>.<genexpr>  rF   rG   rH   rK   s   `r)   r@   z.Owlv2ImageGuidedObjectDetectionOutput.to_tuple  rL   r+   )rM   rN   rO   rP   r   r   r&   rQ   rR   r8   r„   r…   r†   r   r9   r   r:   r   r   r@   rS   r+   r)   rƒ   rƒ   à   s²   … ñð> +/€FˆH�U×&Ñ&Ñ'Ó.Ø04€L�(˜5×,Ñ,Ñ-Ó4Ø6:Ð˜ ×!2Ñ!2Ñ3Ó:Ø59Ð�x × 1Ñ 1Ñ2Ó9Ø48Ð�h˜u×0Ñ0Ñ1Ó8Ø04€L�(˜5×,Ñ,Ñ-Ó4Ø48ÐÐ1Ó8Ø6:ÐÐ3Ó:ð
˜% ™*ô 
r+   rƒ   c                   ó¢   ‡ — e Zd Zdefˆ fd„Zdej                  dededej                  fd„Zddej                  d	e
dej                  fd
„Zˆ xZS )ÚOwlv2VisionEmbeddingsÚconfigc                 ó¸  •— t         ‰| �  «        |j                  | _        || _        |j                  | _        t        j                  t        j                  |j                  «      «      | _
        t        j                  |j                  | j
                  |j                  |j                  d¬«      | _        |j                  |j                  z  dz  | _        | j                  dz   | _        t        j"                  | j                   | j
                  «      | _        | j'                  dt        j(                  | j                   «      j+                  d«      d¬«       y )NF)Úin_channelsÚout_channelsÚkernel_sizeÚstrideÚbiasra   r   Úposition_ids©r   éÿÿÿÿ©Ú
persistent)ÚsuperÚ__init__Ú
patch_sizer‹   Úhidden_sizeÚ	embed_dimr   Ú	Parameterr&   ÚrandnÚclass_embeddingÚConv2dÚnum_channelsÚpatch_embeddingÚ
image_sizeÚnum_patchesÚnum_positionsÚ	EmbeddingÚposition_embeddingÚregister_bufferr'   Úexpand©rD   r‹   Ú	__class__s     €r)   r˜   zOwlv2VisionEmbeddings.__init__  s  ø€ Ü‰ÑÔØ ×+Ñ+ˆŒØˆŒØ×+Ñ+ˆŒÜ!Ÿ|™|¬E¯K©K¸×8JÑ8JÓ,KÓLˆÔä!Ÿy™yØ×+Ñ+ØŸ™Ø×)Ñ)Ø×$Ñ$Øô 
ˆÔð #×-Ñ-°×1BÑ1BÑBÀqÑHˆÔØ!×-Ñ-°Ñ1ˆÔÜ"$§,¡,¨t×/AÑ/AÀ4Ç>Á>Ó"RˆÔØ×Ñ˜^¬U¯\©\¸$×:LÑ:LÓ-M×-TÑ-TÐU\Ó-]ÐjoÐÕpr+   Ú
embeddingsÚheightÚwidthr    c                 óÒ  — |j                   d   dz
  }| j                  j                  j                  d«      }|j                   d   dz
  }t        j
                  j                  «       s%||k(  r ||k(  r| j                  | j                  «      S |dd…dd…f   }|dd…dd…f   }|j                   d   }	|| j                  z  }
|| j                  z  }t        |dz  «      }|j                  d|||	«      }|j                  dddd«      }t        j                  j                  ||
|fdd	¬
«      }|j                  dddd«      j                  dd|	«      }t	        j                   ||fd¬«      S )a   
        This method allows to interpolate the pre-trained position encodings, to be able to use the model on higher resolution
        images. This method is also adapted to support torch.jit tracing.

        Adapted from:
        - https://github.com/facebookresearch/dino/blob/de9ee3df6cf39fac952ab558447af1fa1365362a/vision_transformer.py#L174-L194, and
        - https://github.com/facebookresearch/dinov2/blob/e1277af2ba9496fbadf7aec6eba56e8d882d1e35/dinov2/models/vision_transformer.py#L179-L211
        r   r   Nr”   g      à?r   ra   ÚbicubicF)ÚsizeÚmodeÚalign_corners©Údim)Úshaper¦   ÚweightÚ	unsqueezer&   ÚjitÚ
is_tracingr’   r™   r   ÚreshapeÚpermuter   r$   ÚinterpolateÚviewÚcat)rD   r«   r¬   r­   r£   r¦   r¤   Úclass_pos_embedÚpatch_pos_embedr´   Ú
new_heightÚ	new_widthÚsqrt_num_positionss                r)   Úinterpolate_pos_encodingz.Owlv2VisionEmbeddings.interpolate_pos_encoding)  sv  € ð !×&Ñ& qÑ)¨AÑ-ˆØ!×4Ñ4×;Ñ;×EÑEÀaÓHÐØ*×0Ñ0°Ñ3°aÑ7ˆô �y‰y×#Ñ#Ô%¨+¸Ò*FÈ6ÐUZÊ?Ø×*Ñ*¨4×+<Ñ+<Ó=Ð=à,ªQ°°°¨UÑ3ˆØ,ªQ°±¨UÑ3ˆà×Ñ˜rÑ"ˆà˜tŸ™Ñ.ˆ
Ø˜TŸ_™_Ñ,ˆ	ä& }°cÑ'9Ó:ÐØ)×1Ñ1°!Ð5GÐI[Ð]`ÓaˆØ)×1Ñ1°!°Q¸¸1Ó=ˆäŸ-™-×3Ñ3ØØ˜iÐ(ØØð	 4ó 
ˆð *×1Ñ1°!°Q¸¸1Ó=×BÑBÀ1ÀbÈ#ÓNˆä�y‰y˜/¨?Ð;ÀÔCÐCr+   Úpixel_valuesrÄ   c                 óh  — |j                   \  }}}}| j                  |«      }|j                  d«      j                  dd«      }| j                  j                  |dd«      }t        j                  ||gd¬«      }	|r|	| j                  |	||«      z   }	|	S |	| j                  | j                  «      z   }	|	S )Nra   r   r”   r³   )rµ   r¡   ÚflattenÚ	transposerž   r¨   r&   r¾   rÄ   r¦   r’   )
rD   rÅ   rÄ   Ú
batch_sizeÚ_r¬   r­   Úpatch_embedsr   r«   s
             r)   ÚforwardzOwlv2VisionEmbeddings.forwardR  s»   € Ø'3×'9Ñ'9Ñ$ˆ
�A�v˜uØ×+Ñ+¨LÓ9ˆØ#×+Ñ+¨AÓ.×8Ñ8¸¸AÓ>ˆà×+Ñ+×2Ñ2°:¸qÀ"ÓEˆÜ—Y‘Y ¨lÐ;ÀÔCˆ
Ù#Ø# d×&CÑ&CÀJÐPVÐX]Ó&^Ñ^ˆJð Ðð $ d×&=Ñ&=¸d×>OÑ>OÓ&PÑPˆJØÐr+   ©F)rM   rN   rO   r   r˜   r&   r
   r]   rÄ   rQ   ÚboolrÌ   Ú__classcell__©rª   s   @r)   rŠ   rŠ     sm   ø„ ðqÐ0õ qð*'D°5·<±<ð 'DÈð 'DÐUXð 'DÐ]b×]iÑ]ió 'DñR E×$5Ñ$5ð ÐQUð Ðbg×bnÑbn÷ r+   rŠ   c            	       ó¦   ‡ — e Zd Zdefˆ fd„Z	 	 	 ddeej                     deej                     deej                     dej                  fd„Z
ˆ xZS )	ÚOwlv2TextEmbeddingsr‹   c                 ó^  •— t         ‰| �  «        t        j                  |j                  |j
                  «      | _        t        j                  |j                  |j
                  «      | _        | j                  dt        j                  |j                  «      j                  d«      d¬«       y )Nr’   r“   Fr•   )r—   r˜   r   r¥   Ú
vocab_sizerš   Útoken_embeddingÚmax_position_embeddingsr¦   r§   r&   r'   r¨   r©   s     €r)   r˜   zOwlv2TextEmbeddings.__init__b  s„   ø€ Ü‰ÑÔÜ!Ÿ|™|¨F×,=Ñ,=¸v×?QÑ?QÓRˆÔÜ"$§,¡,¨v×/MÑ/MÈv×OaÑOaÓ"bˆÔð 	×ÑØœEŸL™L¨×)GÑ)GÓH×OÑOÐPWÓXÐejð 	õ 	
r+   Ú	input_idsr’   Úinputs_embedsr    c                 óÆ   — |�|j                   d   n|j                   d   }|€| j                  d d …d |…f   }|€| j                  |«      }| j                  |«      }||z   }|S )Nr”   éþÿÿÿ)rµ   r’   rÕ   r¦   )rD   r×   r’   rØ   Ú
seq_lengthÚposition_embeddingsr«   s          r)   rÌ   zOwlv2TextEmbeddings.forwardl  s{   € ð -6Ð,A�Y—_‘_ RÒ(À}×GZÑGZÐ[]ÑG^ˆ
àÐØ×,Ñ,ªQ°°°¨^Ñ<ˆLàÐ Ø ×0Ñ0°Ó;ˆMà"×5Ñ5°lÓCÐØ"Ð%8Ñ8ˆ
àÐr+   )NNN)rM   rN   rO   r   r˜   r   r&   Ú
LongTensorrQ   r
   rÌ   rÏ   rÐ   s   @r)   rÒ   rÒ   a  sj   ø„ ð
˜õ 
ð 15Ø37Ø59ñ	à˜E×,Ñ,Ñ-ðð ˜u×/Ñ/Ñ0ðð   × 1Ñ 1Ñ2ð	ð
 
�‰÷r+   rÒ   c                   ó  ‡ — e Zd ZdZˆ fd„Zdej                  dedefd„Z	 	 	 ddej                  de	ej                     d	e	ej                     d
e	e
   deej                  e	ej                     e	eej                        f   f
d„Zˆ xZS )ÚOwlv2Attentionz=Multi-headed attention from 'Attention Is All You Need' paperc                 ó
  •— t         ‰| �  «        || _        |j                  | _        |j
                  | _        | j                  | j                  z  | _        | j                  | j                  z  | j                  k7  r&t        d| j                  › d| j                  › d�«      ‚| j                  dz  | _	        |j                  | _        t        j                  | j                  | j                  «      | _        t        j                  | j                  | j                  «      | _        t        j                  | j                  | j                  «      | _        t        j                  | j                  | j                  «      | _        y )Nz;embed_dim must be divisible by num_heads (got `embed_dim`: z and `num_heads`: z).ç      à¿)r—   r˜   r‹   rš   r›   Únum_attention_headsÚ	num_headsÚhead_dimru   ÚscaleÚattention_dropoutÚdropoutr   ÚLinearÚk_projÚv_projÚq_projÚout_projr©   s     €r)   r˜   zOwlv2Attention.__init__„  s  ø€ Ü‰ÑÔØˆŒØ×+Ñ+ˆŒØ×3Ñ3ˆŒØŸ™¨$¯.©.Ñ8ˆŒØ�=‰=˜4Ÿ>™>Ñ)¨T¯^©^Ò;ÜØMÈdÏnÉnÐM]ð ^Ø—N‘NÐ# 2ð'óð ð —]‘] DÑ(ˆŒ
Ø×/Ñ/ˆŒä—i‘i §¡°·±Ó?ˆŒÜ—i‘i §¡°·±Ó?ˆŒÜ—i‘i §¡°·±Ó?ˆŒÜŸ	™	 $§.¡.°$·.±.ÓAˆ�r+   ÚtensorÚseq_lenÚbszc                 óŽ   — |j                  ||| j                  | j                  «      j                  dd«      j	                  «       S )Nr   ra   )r½   rã   rä   rÈ   Ú
contiguous)rD   rí   rî   rï   s       r)   Ú_shapezOwlv2Attention._shape—  s7   € Ø�{‰{˜3 ¨¯©¸¿¹ÓG×QÑQÐRSÐUVÓW×bÑbÓdÐdr+   Úhidden_statesÚattention_maskÚcausal_attention_maskÚoutput_attentionsr    c                 óÊ  — |j                  «       \  }}}| j                  |«      | j                  z  }| j                  | j	                  |«      d|«      }	| j                  | j                  |«      d|«      }
|| j                  z  d| j                  f} | j                  |||«      j                  |Ž } |	j                  |Ž }	 |
j                  |Ž }
|	j                  d«      }t        j                  ||	j                  dd«      «      }|j                  «       || j                  z  ||fk7  r/t        d|| j                  z  ||f› d|j                  «       › �«      ‚|�{|j                  «       |d||fk7  r#t        d|d||f› d|j                  «       › �«      ‚|j                  || j                  ||«      |z   }|j                  || j                  z  ||«      }|�{|j                  «       |d||fk7  r#t        d|d||f› d|j                  «       › �«      ‚|j                  || j                  ||«      |z   }|j                  || j                  z  ||«      }t        j                  j                  |d¬«      }|r?|j                  || j                  ||«      }|j                  || j                  z  ||«      }nd}t        j                  j!                  || j                   | j"                  ¬	«      }|j%                  |
j&                  «      }t        j                  ||
«      }|j                  «       || j                  z  || j                  fk7  r7t        d
|| j                  || j                  f› d|j                  «       › �«      ‚|j                  || j                  || j                  «      }|j                  dd«      }|j)                  |||«      }| j+                  |«      }||fS )z#Input shape: Batch x Time x Channelr”   r   ra   z$Attention weights should be of size z	, but is Nz!Attention mask should be of size r³   )ÚpÚtrainingz `attn_output` should be of size )r°   rë   rå   rò   ré   rê   rã   rä   r½   r&   ÚbmmrÈ   ru   r   r$   Úsoftmaxrç   rù   ÚtorW   rº   rì   )rD   ró   rô   rõ   rö   rï   Útgt_lenr›   Úquery_statesÚ
key_statesÚvalue_statesÚ
proj_shapeÚsrc_lenÚattn_weightsÚattn_weights_reshapedÚ
attn_probsÚattn_outputs                    r)   rÌ   zOwlv2Attention.forwardš  së  € ð #0×"4Ñ"4Ó"6ÑˆˆW�ið —{‘{ =Ó1°D·J±JÑ>ˆØ—[‘[ §¡¨]Ó!;¸RÀÓEˆ
Ø—{‘{ 4§;¡;¨}Ó#=¸rÀ3ÓGˆà˜DŸN™NÑ*¨B°·±Ð>ˆ
ØC�t—{‘{ <°¸#Ó>×CÑCÀZÐPˆØ$�Z—_‘_ jÐ1ˆ
Ø(�|×(Ñ(¨*Ð5ˆà—/‘/ !Ó$ˆÜ—y‘y ¨z×/CÑ/CÀAÀqÓ/IÓJˆà×ÑÓ 3¨¯©Ñ#7¸À'Ð"JÒJÜØ6¸¸d¿n¹nÑ8LÈgÐW^Ð7_Ð6`ð aØ ×%Ñ%Ó'Ð(ð*óð ð !Ð,Ø$×)Ñ)Ó+°°Q¸ÀÐ/IÒIÜ Ø7¸¸aÀÈ'Ð8RÐ7Sð TØ-×2Ñ2Ó4Ð5ð7óð ð (×,Ñ,¨S°$·.±.À'È7ÓSÐVkÑkˆLØ'×,Ñ,¨S°4·>±>Ñ-AÀ7ÈGÓTˆLàÐ%Ø×"Ñ"Ó$¨¨a°¸'Ð(BÒBÜ Ø7¸¸aÀÈ'Ð8RÐ7SÐS\Ð]k×]pÑ]pÓ]rÐ\sÐtóð ð (×,Ñ,¨S°$·.±.À'È7ÓSÐVdÑdˆLØ'×,Ñ,¨S°4·>±>Ñ-AÀ7ÈGÓTˆLä—}‘}×,Ñ,¨\¸rÐ,ÓBˆáð
 %1×$5Ñ$5°c¸4¿>¹>È7ÐT[Ó$\Ð!Ø0×5Ñ5°c¸D¿N¹NÑ6JÈGÐU\Ó]‰Là$(Ð!ä—]‘]×*Ñ*¨<¸4¿<¹<ÐRV×R_ÑR_Ð*Ó`ˆ
ð  —]‘] <×#5Ñ#5Ó6ˆ
ä—i‘i 
¨LÓ9ˆà×ÑÓ #¨¯©Ñ"6¸ÀÇÁÐ!OÒOÜØ2°C¸¿¹ÈÐRV×R_ÑR_Ð3`Ð2að bØ×$Ñ$Ó&Ð'ð)óð ð
 "×&Ñ& s¨D¯N©N¸GÀTÇ]Á]ÓSˆØ!×+Ñ+¨A¨qÓ1ˆØ!×)Ñ)¨#¨w¸	ÓBˆà—m‘m KÓ0ˆàÐ1Ð1Ð1r+   ©NNF)rM   rN   rO   rP   r˜   r&   r
   r]   rò   r   rÎ   r   rÌ   rÏ   rÐ   s   @r)   rß   rß   �  sµ   ø„ ÙGôBð&e˜UŸ\™\ð e°Cð e¸có eð 26Ø8<Ø,1ñO2à—|‘|ðO2ð ! §¡Ñ.ðO2ð  (¨¯©Ñ5ð	O2ð
 $ D™>ðO2ð 
ˆu�|‰|˜X e§l¡lÑ3°X¸eÀEÇLÁLÑ>QÑ5RÐRÑ	S÷O2r+   rß   c                   óV   ‡ — e Zd Zˆ fd„Zdej
                  dej
                  fd„Zˆ xZS )ÚOwlv2MLPc                 ó  •— t         ‰| �  «        || _        t        |j                     | _        t        j                  |j                  |j                  «      | _
        t        j                  |j                  |j                  «      | _        y rU   )r—   r˜   r‹   r   Ú
hidden_actÚactivation_fnr   rè   rš   Úintermediate_sizeÚfc1Úfc2r©   s     €r)   r˜   zOwlv2MLP.__init__î  sd   ø€ Ü‰ÑÔØˆŒÜ# F×$5Ñ$5Ñ6ˆÔÜ—9‘9˜V×/Ñ/°×1IÑ1IÓJˆŒÜ—9‘9˜V×5Ñ5°v×7IÑ7IÓJˆ�r+   ró   r    c                 ól   — | j                  |«      }| j                  |«      }| j                  |«      }|S rU   )r  r  r  )rD   ró   s     r)   rÌ   zOwlv2MLP.forwardõ  s4   € ØŸ™ Ó/ˆØ×*Ñ*¨=Ó9ˆØŸ™ Ó/ˆØÐr+   )rM   rN   rO   r˜   r&   r
   rÌ   rÏ   rÐ   s   @r)   r	  r	  í  s$   ø„ ôKð U§\¡\ð °e·l±l÷ r+   r	  c                   ó    ‡ — e Zd Zdefˆ fd„Z	 d	dej                  dej                  dej                  dee   de	ej                     f
d„Zˆ xZS )
ÚOwlv2EncoderLayerr‹   c                 óD  •— t         ‰| �  «        |j                  | _        t	        |«      | _        t        j                  | j                  |j                  ¬«      | _	        t        |«      | _        t        j                  | j                  |j                  ¬«      | _        y ©N©Úeps)r—   r˜   rš   r›   rß   Ú	self_attnr   Ú	LayerNormÚlayer_norm_epsÚlayer_norm1r	  ÚmlpÚlayer_norm2r©   s     €r)   r˜   zOwlv2EncoderLayer.__init__þ  sm   ø€ Ü‰ÑÔØ×+Ñ+ˆŒÜ'¨Ó/ˆŒÜŸ<™<¨¯©¸F×<QÑ<QÔRˆÔÜ˜FÓ#ˆŒÜŸ<™<¨¯©¸F×<QÑ<QÔRˆÕr+   ró   rô   rõ   rö   r    c                 óÎ   — |}| j                  |«      }| j                  ||||¬«      \  }}||z   }|}| j                  |«      }| j                  |«      }||z   }|f}|r||fz  }|S )aI  
        Args:
            hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)`
            attention_mask (`torch.FloatTensor`): attention mask of size
                `(batch, 1, tgt_len, src_len)` where padding elements are indicated by very large negative values.
                `(config.encoder_attention_heads,)`.
            output_attentions (`bool`, *optional*):
                Whether or not to return the attentions tensors of all attention layers. See `attentions` under
                returned tensors for more detail.
        )ró   rô   rõ   rö   )r  r  r  r  )rD   ró   rô   rõ   rö   Úresidualr  Úoutputss           r)   rÌ   zOwlv2EncoderLayer.forward  s’   € ð" !ˆà×(Ñ(¨Ó7ˆØ&*§n¡nØ'Ø)Ø"7Ø/ð	 '5ó '
Ñ#ˆ�|ð ! =Ñ0ˆà ˆØ×(Ñ(¨Ó7ˆØŸ™ Ó/ˆØ  =Ñ0ˆà Ð"ˆáØ˜�Ñ&ˆGàˆr+   rÍ   )rM   rN   rO   r   r˜   r&   r
   r   rÎ   r   rQ   rÌ   rÏ   rÐ   s   @r)   r  r  ý  sf   ø„ ðS˜{õ Sð -2ñ&à—|‘|ð&ð Ÿ™ð&ð  %Ÿ|™|ð	&ð
 $ D™>ð&ð 
ˆu× Ñ Ñ	!÷&r+   r  c                   ó(   — e Zd ZdZeZdZdZdgZd„ Z	y)ÚOwlv2PreTrainedModelz†
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    Úowlv2Tr  c                 óL
  — | j                   j                  }t        |t        «      rj|j                  j
                  j                  j                  d|dz  ¬«       |j                  j
                  j                  j                  d|dz  ¬«       �nåt        |t        «      rì| j                   j                  }t        j                  j                  |j                  d|j                  dz  |z  ¬«       t        j                  j                  |j                  j
                  |j                   j                  |z  ¬«       t        j                  j                  |j                  j
                  |j                   j                  |z  ¬«       �nét        |t         «      �r-| j                   j                  }|j                  dz  d|j                   j"                  z  dz  z  |z  }|j                  dz  |z  }t        j                  j                  |j$                  j
                  |¬«       t        j                  j                  |j&                  j
                  |¬«       t        j                  j                  |j(                  j
                  |¬«       t        j                  j                  |j*                  j
                  |¬«       �n«t        |t,        «      rÙ| j                   j                  }|j                   j.                  dz  d|j                   j"                  z  dz  z  |z  }d|j                   j.                  z  dz  |z  }t        j                  j                  |j0                  j
                  |¬«       t        j                  j                  |j2                  j
                  |¬«       nÂt        |t4        «      r²t        j                  j                  |j6                  j
                  |j8                  dz  | j                   j                  z  ¬«       t        j                  j                  |j:                  j
                  |j<                  dz  | j                   j                  z  ¬«       t        |t        j>                  «      rI|j@                  j                  jC                  «        |j
                  j                  jE                  d«       t        |t        jF                  «      r2|j@                  �%|j@                  j                  jC                  «        yyy)	zInitialize the weightsç        g{®Gáz”?)ÚmeanÚstdrá   )r&  ra   ç      ð?N)$r‹   Úinitializer_factorÚ
isinstancerÒ   rÕ   r¶   ÚdataÚnormal_r¦   rŠ   r   Úinitrž   r›   r¡   Úinitializer_rangerß   Únum_hidden_layersrë   ré   rê   rì   r	  rš   r  r  Ú
Owlv2ModelÚtext_projectionÚtext_embed_dimÚvisual_projectionÚvision_embed_dimr  r‘   Úzero_Úfill_rè   )rD   ÚmoduleÚfactorÚin_proj_stdÚout_proj_stdÚfc_stds         r)   Ú_init_weightsz"Owlv2PreTrainedModel._init_weights;  s–  € à—‘×/Ñ/ˆÜ�fÔ1Ô2Ø×"Ñ"×)Ñ)×.Ñ.×6Ñ6¸CÀVÈdÁ]Ð6ÔSØ×%Ñ%×,Ñ,×1Ñ1×9Ñ9¸sÈÐQUÉÐ9ÖVÜ˜Ô 5Ô6Ø—[‘[×3Ñ3ˆFÜ�G‰G�O‰O˜F×2Ñ2¸À&×BRÑBRÐTXÑBXÐ[aÑBaˆOÔbÜ�G‰G�O‰O˜F×2Ñ2×9Ñ9¸v¿}¹}×?^Ñ?^ÐagÑ?gˆOÔhÜ�G‰G�O‰O˜F×5Ñ5×<Ñ<À&Ç-Á-×BaÑBaÐdjÑBjˆOÖkÜ˜¤Õ/Ø—[‘[×3Ñ3ˆFØ!×+Ñ+¨TÑ1°q¸6¿=¹=×;ZÑ;ZÑ7ZÐ_cÑ6cÑdÐgmÑmˆKØ"×,Ñ,¨dÑ2°fÑ<ˆLÜ�G‰G�O‰O˜FŸM™M×0Ñ0°kˆOÔBÜ�G‰G�O‰O˜FŸM™M×0Ñ0°kˆOÔBÜ�G‰G�O‰O˜FŸM™M×0Ñ0°kˆOÔBÜ�G‰G�O‰O˜FŸO™O×2Ñ2¸ˆOÖEÜ˜¤Ô)Ø—[‘[×3Ñ3ˆFØ!Ÿ=™=×4Ñ4°dÑ:ÀÀFÇMÁM×DcÑDcÑ@cÐhlÑ?lÑmÐpvÑvˆKØ˜&Ÿ-™-×3Ñ3Ñ3¸Ñ<¸vÑEˆFÜ�G‰G�O‰O˜FŸJ™J×-Ñ-°6ˆOÔ:Ü�G‰G�O‰O˜FŸJ™J×-Ñ-°;ˆOÕ?Ü˜¤
Ô+Ü�G‰G�O‰OØ×&Ñ&×-Ñ-Ø×)Ñ)¨4Ñ/°$·+±+×2PÑ2PÑPð ô ô �G‰G�O‰OØ×(Ñ(×/Ñ/Ø×+Ñ+¨TÑ1°D·K±K×4RÑ4RÑRð ô ô �fœbŸl™lÔ+Ø�K‰K×Ñ×"Ñ"Ô$Ø�M‰M×Ñ×$Ñ$ SÔ)Ü�fœbŸi™iÔ(¨V¯[©[Ð-DØ�K‰K×Ñ×"Ñ"Õ$ð .EÐ(r+   N)
rM   rN   rO   rP   r   Úconfig_classÚbase_model_prefixÚsupports_gradient_checkpointingÚ_no_split_modulesr;  rS   r+   r)   r!  r!  0  s(   „ ñð
 €LØÐØ&*Ð#Ø,Ð-Ðó&%r+   r!  a?  

    This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
    library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
    etc.)

    This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
    Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
    and behavior.

    Parameters:
        config ([`Owvl2Config`]): Model configuration class with all the parameters of the model.
            Initializing with a config file does not load the weights associated with the model, only the
            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.
a  
    Args:
        input_ids (`torch.LongTensor` of shape `(batch_size * num_max_text_queries, sequence_length)`):
            Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`AutoTokenizer`]. See
            [`PreTrainedTokenizer.encode`] and [`PreTrainedTokenizer.__call__`] for details. [What are input
            IDs?](../glossary#input-ids)
        attention_mask (`torch.Tensor` of shape `(batch_size, num_max_text_queries, sequence_length)`, *optional*):
            Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:
            - 1 for tokens that are **not masked**,
            - 0 for tokens that are **masked**.
            [What are attention masks?](../glossary#attention-mask)
        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
            tensors for more detail.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
a#  
    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values.
        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
            tensors for more detail.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        interpolate_pos_encoding (`bool`, *optional*, defaults `False`):
            Whether to interpolate the pre-trained position encodings.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
aÌ  
    Args:
        input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
            Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`AutoTokenizer`]. See
            [`PreTrainedTokenizer.encode`] and [`PreTrainedTokenizer.__call__`] for details. [What are input
            IDs?](../glossary#input-ids)
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values.
        attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
            Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:
            - 1 for tokens that are **not masked**,
            - 0 for tokens that are **masked**.
            [What are attention masks?](../glossary#attention-mask)
        return_loss (`bool`, *optional*):
            Whether or not to return the contrastive loss.
        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
            tensors for more detail.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        interpolate_pos_encoding (`bool`, *optional*, defaults `False`):
            Whether to interpolate the pre-trained position encodings.
        return_base_image_embeds (`bool`, *optional*):
            Whether or not to return the base image embeddings.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
aŠ  
    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values.
        input_ids (`torch.LongTensor` of shape `(batch_size * num_max_text_queries, sequence_length)`, *optional*):
            Indices of input sequence tokens in the vocabulary. Indices can be obtained using [`AutoTokenizer`]. See
            [`PreTrainedTokenizer.encode`] and [`PreTrainedTokenizer.__call__`] for details. [What are input
            IDs?](../glossary#input-ids).
        attention_mask (`torch.Tensor` of shape `(batch_size, num_max_text_queries, sequence_length)`, *optional*):
            Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:
            - 1 for tokens that are **not masked**,
            - 0 for tokens that are **masked**.
            [What are attention masks?](../glossary#attention-mask)
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the last hidden state. See `text_model_last_hidden_state` and
            `vision_model_last_hidden_state` under returned tensors for more detail.
        interpolate_pos_encoding (`bool`, *optional*, defaults `False`):
            Whether to interpolate the pre-trained position encodings.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
aï  
    Args:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values.
        query_pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values of query image(s) to be detected. Pass in one query image per target image.
        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned
            tensors for more detail.
        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for
            more detail.
        interpolate_pos_encoding (`bool`, *optional*, defaults `False`):
            Whether to interpolate the pre-trained position encodings.
        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
c                   ó¤   ‡ — e Zd ZdZdefˆ fd„Z	 	 	 	 	 ddeej                     deej                     dee	   dee	   dee	   d	e
eef   fd
„Zˆ xZS )ÚOwlv2Encoderz¯
    Transformer encoder consisting of `config.num_hidden_layers` self attention layers. Each layer is a
    [`Owlv2EncoderLayer`].

    Args:
        config: Owlv2Config
    r‹   c                 óÂ   •— t         ‰| �  «        t        j                  t	        |j
                  «      D �cg c]  }t        |«      ‘Œ c}«      | _        d| _        y c c}w )NF)	r—   r˜   r   Ú
ModuleListÚranger.  r  ÚlayersÚgradient_checkpointing)rD   r‹   rÊ   rª   s      €r)   r˜   zOwlv2Encoder.__init__é  sH   ø€ Ü‰ÑÔÜ—m‘mÌÈf×NfÑNfÓHgÖ$hÀ1Ô%6°vÕ%>Ò$hÓiˆŒØ&+ˆÕ#ùò %is   ¶Arô   rõ   rö   Úoutput_hidden_statesÚreturn_dictr    c                 óð  — |�|n| j                   j                  }|�|n| j                   j                  }|�|n| j                   j                  }|rdnd}|rdnd}|}	| j                  D ]_  }
|r||	fz   }| j
                  r,| j                  r | j                  |
j                  |	|||«      }n |
|	|||¬«      }|d   }	|sŒW||d   fz   }Œa |r||	fz   }|st        d„ |	||fD «       «      S t        |	||¬«      S )aŸ  
        Args:
            inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`).
            attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
                Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:
                - 1 for tokens that are **not masked**,
                - 0 for tokens that are **masked**.
                [What are attention masks?](../glossary#attention-mask)
            causal_attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, *optional*):
                Causal mask for the text model. Mask values selected in `[0, 1]`:
                - 1 for tokens that are **not masked**,
                - 0 for tokens that are **masked**.
                [What are attention masks?](../glossary#attention-mask)
            output_attentions (`bool`, *optional*):
                Whether or not to return the attentions tensors of all attention layers. See `attentions` under
                returned tensors for more detail.
            output_hidden_states (`bool`, *optional*):
                Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors
                for more detail.
            return_dict (`bool`, *optional*):
                Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
        NrS   )rö   r   r   c              3   ó&   K  — | ]	  }|€Œ|–— Œ y ­wrU   rS   )rB   Úvs     r)   rE   z'Owlv2Encoder.forward.<locals>.<genexpr>3  s   è ø€ Òe˜qÐWXÑWdœÑeùó   ‚Š)Úlast_hidden_stateró   Ú
attentions)r‹   rö   rG  Úuse_return_dictrE  rF  rù   Ú_gradient_checkpointing_funcÚ__call__rI   r   )rD   rØ   rô   rõ   rö   rG  rH  Úencoder_statesÚall_attentionsró   Úencoder_layerÚlayer_outputss               r)   rÌ   zOwlv2Encoder.forwardî  s=  € ð> 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆá3™¸ˆÙ0™°dˆà%ˆØ!Ÿ[™[ò 	FˆMÙ#Ø!/°=Ð2BÑ!B�Ø×*Ò*¨t¯}ª}Ø $× AÑ AØ!×*Ñ*Ø!Ø"Ø)Ø%ó!‘ñ !.Ø!Ø"Ø)Ø&7ô	!�ð *¨!Ñ,ˆMâ Ø!/°=ÀÑ3CÐ2EÑ!E‘ð-	Fñ0  Ø+¨}Ð.>Ñ>ˆNáÜÑe ]°NÀNÐ$SÔeÓeÐeÜØ+¸>ÐVdô
ð 	
r+   ©NNNNN)rM   rN   rO   rP   r   r˜   r   r&   r
   rÎ   r	   r   r   rÌ   rÏ   rÐ   s   @r)   rA  rA  à  s•   ø„ ñð,˜{õ ,ð 26Ø8<Ø,0Ø/3Ø&*ñH
ð ! §¡Ñ.ðH
ð  (¨¯©Ñ5ð	H
ð
 $ D™>ðH
ð ' t™nðH
ð ˜d‘^ðH
ð 
ˆu�oÐ%Ñ	&÷H
r+   rA  c                   óè   ‡ — e Zd Zdefˆ fd„Z ee«       eee¬«      	 	 	 	 	 dde	j                  dee	j                     dee	j                     dee   dee   d	ee   d
eeef   fd„«       «       Zˆ xZS )ÚOwlv2TextTransformerr‹   c                 óÔ   •— t         ‰| �  «        || _        |j                  }t	        |«      | _        t        |«      | _        t        j                  ||j                  ¬«      | _        y r  )r—   r˜   r‹   rš   rÒ   r«   rA  Úencoderr   r  r  Úfinal_layer_norm)rD   r‹   r›   rª   s      €r)   r˜   zOwlv2TextTransformer.__init__;  sQ   ø€ Ü‰ÑÔØˆŒØ×&Ñ&ˆ	Ü-¨fÓ5ˆŒÜ# FÓ+ˆŒÜ "§¡¨Y¸F×<QÑ<QÔ RˆÕr+   ©Úoutput_typer<  r×   rô   r’   rö   rG  rH  r    c                 ó,  — |�|n| j                   j                  }|�|n| j                   j                  }|�|n| j                   j                  }|j	                  «       }|j                  d|d   «      }| j                  ||¬«      }t        ||j                  |j                  ¬«      }	|�t        ||j                  «      }| j                  |||	|||¬«      }
|
d   }| j                  |«      }|t        j                  |j                  d   |j                  ¬«      |j!                  t        j"                  «      j%                  d¬«      j!                  |j                  «      f   }|s
||f|
dd z   S t'        |||
j(                  |
j*                  ¬	«      S )
ú
        Returns:
        Nr”   )r×   r’   r"   )rØ   rô   rõ   rö   rG  rH  r   r³   r   ©rM  Úpooler_outputró   rN  )r‹   rö   rG  rO  r°   r½   r«   r   rW   r#   r   rZ  r[  r&   r'   rµ   rü   r]   Úargmaxr   ró   rN  )rD   r×   rô   r’   rö   rG  rH  Úinput_shaperó   rõ   Úencoder_outputsrM  Úpooled_outputs                r)   rÌ   zOwlv2TextTransformer.forwardC  s§  € ð 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆà—n‘nÓ&ˆØ—N‘N 2 {°2¡Ó7ˆ	ØŸ™°)È,˜ÓWˆô
 !AØ˜×,Ñ,°]×5IÑ5Iô!
Ðð Ð%ä7¸È×H[ÑH[Ó\ˆNàŸ,™,Ø'Ø)Ø"7Ø/Ø!5Ø#ð 'ó 
ˆð ,¨AÑ.ÐØ ×1Ñ1Ð2CÓDÐð *Ü�L‰LÐ*×0Ñ0°Ñ3Ð<M×<TÑ<TÔUØ�L‰LœŸ™Ó#×*Ñ*¨rÐ*Ó2×5Ñ5Ð6G×6NÑ6NÓOðQñ
ˆñ
 Ø% }Ð5¸ÈÈÐ8KÑKÐKä)Ø/Ø'Ø)×7Ñ7Ø&×1Ñ1ô	
ð 	
r+   rV  )rM   rN   rO   r   r˜   r   ÚOWLV2_TEXT_INPUTS_DOCSTRINGr   r   r&   r
   r   rÎ   r	   r   rÌ   rÏ   rÐ   s   @r)   rX  rX  :  sÀ   ø„ ðS˜õ Sñ +Ð+FÓGÙÐ+EÐTcÔdð 26Ø/3Ø,0Ø/3Ø&*ñ<
à—<‘<ð<
ð ! §¡Ñ.ð<
ð ˜uŸ|™|Ñ,ð	<
ð
 $ D™>ð<
ð ' t™nð<
ð ˜d‘^ð<
ð 
ˆuÐ0Ð0Ñ	1ò<
ó eó Hô<
r+   rX  c                   óò   ‡ — e Zd ZeZdefˆ fd„Zdej                  fd„Zd„ Z	 e
e«       eee¬«      	 	 	 	 ddej                  deej                     d	ee   d
ee   dee   deeef   fd„«       «       Zˆ xZS )ÚOwlv2TextModelr‹   c                 ód   •— t         ‰| �  |«       t        |«      | _        | j	                  «        y rU   )r—   r˜   rX  Ú
text_modelÚ	post_initr©   s     €r)   r˜   zOwlv2TextModel.__init__ˆ  s&   ø€ Ü‰Ñ˜Ô Ü.¨vÓ6ˆŒà�‰Õr+   r    c                 óB   — | j                   j                  j                  S rU   ©rj  r«   rÕ   rK   s    r)   Úget_input_embeddingsz#Owlv2TextModel.get_input_embeddingsŽ  s   € Ø�‰×)Ñ)×9Ñ9Ð9r+   c                 ó:   — || j                   j                  _        y rU   rm  )rD   Úvalues     r)   Úset_input_embeddingsz#Owlv2TextModel.set_input_embeddings‘  s   € Ø5:ˆ�‰×"Ñ"Õ2r+   r\  r×   rô   rö   rG  rH  c                 ó.   — | j                  |||||¬«      S )au  
        Returns:

        Examples:
        ```python
        >>> from transformers import AutoProcessor, Owlv2TextModel

        >>> model = Owlv2TextModel.from_pretrained("google/owlv2-base-patch16")
        >>> processor = AutoProcessor.from_pretrained("google/owlv2-base-patch16")
        >>> inputs = processor(
        ...     text=[["a photo of a cat", "a photo of a dog"], ["photo of a astranaut"]], return_tensors="pt"
        ... )
        >>> outputs = model(**inputs)
        >>> last_hidden_state = outputs.last_hidden_state
        >>> pooled_output = outputs.pooler_output  # pooled (EOS token) states
        ```©r×   rô   rö   rG  rH  )rj  )rD   r×   rô   rö   rG  rH  s         r)   rÌ   zOwlv2TextModel.forward”  s)   € ð8 �‰ØØ)Ø/Ø!5Ø#ð ó 
ð 	
r+   )NNNN)rM   rN   rO   r   r<  r˜   r   ÚModulern  rq  r   rf  r   r   r&   r
   r   rÎ   r	   r   rÌ   rÏ   rÐ   s   @r)   rh  rh  …  sÂ   ø„ Ø"€Lð˜õ ð: b§i¡ió :ò;ñ +Ð+FÓGÙÐ+EÐTcÔdð 26Ø,0Ø/3Ø&*ñ 
à—<‘<ð 
ð ! §¡Ñ.ð 
ð $ D™>ð	 
ð
 ' t™nð 
ð ˜d‘^ð 
ð 
ˆuÐ0Ð0Ñ	1ò 
ó eó Hô 
r+   rh  c                   ó´   ‡ — e Zd Zdefˆ fd„Z ee«       eee¬«      	 	 	 	 dde	j                  dee   dee   dee   dee   d	eeef   fd
„«       «       Zˆ xZS )ÚOwlv2VisionTransformerr‹   c                 ó0  •— t         ‰| �  «        || _        t        |«      | _        t        j                  |j                  |j                  ¬«      | _	        t        |«      | _        t        j                  |j                  |j                  ¬«      | _        y r  )r—   r˜   r‹   rŠ   r«   r   r  rš   r  Úpre_layernormrA  rZ  Úpost_layernormr©   s     €r)   r˜   zOwlv2VisionTransformer.__init__»  sk   ø€ Ü‰ÑÔØˆŒä/°Ó7ˆŒÜŸ\™\¨&×*<Ñ*<À&×BWÑBWÔXˆÔÜ# FÓ+ˆŒÜ Ÿl™l¨6×+=Ñ+=À6×CXÑCXÔYˆÕr+   r\  rÅ   rö   rG  rÄ   rH  r    c                 ó(  — |�|n| j                   j                  }|�|n| j                   j                  }|�|n| j                   j                  }| j                  j
                  j                  j                  }|j                  |«      }| j	                  ||¬«      }| j                  |«      }| j                  ||||¬«      }|d   }	|	dd…ddd…f   }
| j                  |
«      }
|s
|	|
f|dd z   S t        |	|
|j                  |j                  ¬«      S )r_  N)rÄ   )rØ   rö   rG  rH  r   r   r`  )r‹   rö   rG  rO  r«   r¡   r¶   rW   rü   rx  rZ  ry  r   ró   rN  )rD   rÅ   rö   rG  rÄ   rH  Úexpected_input_dtyperó   rd  rM  re  s              r)   rÌ   zOwlv2VisionTransformer.forwardÄ  s-  € ð 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆð  $Ÿ™×>Ñ>×EÑE×KÑKÐØ#—‘Ð';Ó<ˆàŸ™¨ÐOg˜ÓhˆØ×*Ñ*¨=Ó9ˆàŸ,™,Ø'Ø/Ø!5Ø#ð	 'ó 
ˆð ,¨AÑ.ÐØ)ª!¨Q²¨'Ñ2ˆà×+Ñ+¨MÓ:ˆáØ% }Ð5¸ÈÈÐ8KÑKÐKä)Ø/Ø'Ø)×7Ñ7Ø&×1Ñ1ô	
ð 	
r+   )NNFN)rM   rN   rO   r   r˜   r   ÚOWLV2_VISION_INPUTS_DOCSTRINGr   r   r&   rQ   r   rÎ   r	   r   rÌ   rÏ   rÐ   s   @r)   rv  rv  º  s¨   ø„ ðZÐ0õ Zñ +Ð+HÓIÙÐ+EÐTeÔfð -1Ø/3Ø38Ø&*ñ,
à×'Ñ'ð,
ð $ D™>ð,
ð ' t™nð	,
ð
 #+¨4¡.ð,
ð ˜d‘^ð,
ð 
ˆuÐ0Ð0Ñ	1ò,
ó gó Jô,
r+   rv  c                   óÞ   ‡ — e Zd ZeZdZdefˆ fd„Zdej                  fd„Z	 e
e«       eee¬«      	 	 	 	 	 ddeej                      dee   dee   d	ed
ee   deeef   fd„«       «       Zˆ xZS )ÚOwlv2VisionModelrÅ   r‹   c                 ód   •— t         ‰| �  |«       t        |«      | _        | j	                  «        y rU   )r—   r˜   rv  Úvision_modelrk  r©   s     €r)   r˜   zOwlv2VisionModel.__init__ú  s'   ø€ Ü‰Ñ˜Ô Ü2°6Ó:ˆÔà�‰Õr+   r    c                 óB   — | j                   j                  j                  S rU   )r€  r«   r¡   rK   s    r)   rn  z%Owlv2VisionModel.get_input_embeddings   s   € Ø× Ñ ×+Ñ+×;Ñ;Ð;r+   r\  rö   rG  rÄ   rH  c                 ó.   — | j                  |||||¬«      S )aä  
        Returns:

        Examples:
        ```python
        >>> from PIL import Image
        >>> import requests
        >>> from transformers import AutoProcessor, Owlv2VisionModel

        >>> model = Owlv2VisionModel.from_pretrained("google/owlv2-base-patch16")
        >>> processor = AutoProcessor.from_pretrained("google/owlv2-base-patch16")
        >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
        >>> image = Image.open(requests.get(url, stream=True).raw)

        >>> inputs = processor(images=image, return_tensors="pt")

        >>> outputs = model(**inputs)
        >>> last_hidden_state = outputs.last_hidden_state
        >>> pooled_output = outputs.pooler_output  # pooled CLS states
        ```©rÅ   rö   rG  rÄ   rH  )r€  )rD   rÅ   rö   rG  rÄ   rH  s         r)   rÌ   zOwlv2VisionModel.forward  s+   € ð< × Ñ Ø%Ø/Ø!5Ø%=Ø#ð !ó 
ð 	
r+   ©NNNFN)rM   rN   rO   r   r<  Úmain_input_namer˜   r   rt  rn  r   r|  r   r   r   r&   rQ   rÎ   r	   r   rÌ   rÏ   rÐ   s   @r)   r~  r~  ö  sÄ   ø„ Ø$€LØ$€OðÐ0õ ð< b§i¡ió <ñ +Ð+HÓIÙÐ+EÐTeÔfð 59Ø,0Ø/3Ø).Ø&*ñ"
à˜u×0Ñ0Ñ1ð"
ð $ D™>ð"
ð ' t™nð	"
ð
 #'ð"
ð ˜d‘^ð"
ð 
ˆuÐ0Ð0Ñ	1ò"
ó gó Jô"
r+   r~  c                   ó0  ‡ — e Zd ZeZdefˆ fd„Z ee«      	 	 	 	 	 ddee	j                     dee	j                     dee   dee   dee   de	j                  fd	„«       Z ee«      	 	 	 	 	 dd
ee	j                     dee   dee   dedee   de	j                  fd„«       Z ee«       eee¬«      	 	 	 	 	 	 	 	 	 ddee	j&                     d
ee	j                     dee	j                     dee   dee   dee   dedee   dee   deeef   fd„«       «       Zˆ xZS )r/  r‹   c                 ó<  •— t         ‰| �  |«       t        |j                  t        «      s"t        dt        |j                  «      › d�«      ‚t        |j                  t        «      s"t        dt        |j                  «      › d�«      ‚|j                  }|j                  }|j                  | _	        |j                  | _        |j                  | _        t        |«      | _        t        |«      | _        t#        j$                  | j                  | j                  d¬«      | _        t#        j$                  | j                  | j                  d¬«      | _        t#        j*                  t-        j.                  |j0                  «      «      | _        | j5                  «        y )NzLconfig.text_config is expected to be of type Owlv2TextConfig but is of type ú.zPconfig.vision_config is expected to be of type Owlv2VisionConfig but is of type F)r‘   )r—   r˜   r)  Útext_configr   Ú	TypeErrorÚtypeÚvision_configr   Úprojection_dimrš   r1  r3  rX  rj  rv  r€  r   rè   r2  r0  rœ   r&   rí   Úlogit_scale_init_valueÚlogit_scalerk  )rD   r‹   r‰  rŒ  rª   s       €r)   r˜   zOwlv2Model.__init__/  sR  ø€ Ü‰Ñ˜Ô ä˜&×,Ñ,¬oÔ>ÜðÜ˜×+Ñ+Ó,Ð-¨Qð0óð ô
 ˜&×.Ñ.Ô0AÔBÜðÜ˜×-Ñ-Ó.Ð/¨qð2óð ð
 ×(Ñ(ˆØ×,Ñ,ˆà$×3Ñ3ˆÔØ)×5Ñ5ˆÔØ -× 9Ñ 9ˆÔä.¨{Ó;ˆŒÜ2°=ÓAˆÔä!#§¡¨4×+@Ñ+@À$×BUÑBUÐ\aÔ!bˆÔÜ!Ÿy™y¨×)<Ñ)<¸d×>QÑ>QÐX]Ô^ˆÔÜŸ<™<¬¯©°V×5RÑ5RÓ(SÓTˆÔð 	�‰Õr+   r×   rô   rö   rG  rH  r    c                 óŽ   — |�|n| j                   j                  }| j                  |||¬«      }|d   }| j                  |«      }|S )aØ  
        Returns:
            text_features (`torch.FloatTensor` of shape `(batch_size, output_dim`): The text embeddings obtained by
            applying the projection layer to the pooled output of [`Owlv2TextModel`].

        Examples:
        ```python
        >>> from transformers import AutoProcessor, Owlv2Model

        >>> model = Owlv2Model.from_pretrained("google/owlv2-base-patch16-ensemble")
        >>> processor = AutoProcessor.from_pretrained("google/owlv2-base-patch16-ensemble")
        >>> inputs = processor(
        ...     text=[["a photo of a cat", "a photo of a dog"], ["photo of a astranaut"]], return_tensors="pt"
        ... )
        >>> text_features = model.get_text_features(**inputs)
        ```)r×   rô   rH  r   )r‹   rO  rj  r0  )	rD   r×   rô   rö   rG  rH  Útext_outputre  Útext_featuress	            r)   Úget_text_featureszOwlv2Model.get_text_featuresO  sP   € ð4 &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆð —o‘o°	È.Ðfq�oÓrˆØ# A™ˆØ×,Ñ,¨]Ó;ˆàÐr+   rÅ   rÄ   c                 óú   — |�|n| j                   j                  }|�|n| j                   j                  }|�|n| j                   j                  }| j	                  |||||¬«      }|d   }| j                  |«      }|S )aO  
        Returns:
            image_features (`torch.FloatTensor` of shape `(batch_size, output_dim`): The image embeddings obtained by
            applying the projection layer to the pooled output of [`Owlv2VisionModel`].

        Examples:
        ```python
        >>> from PIL import Image
        >>> import requests
        >>> from transformers import AutoProcessor, Owlv2Model

        >>> model = Owlv2Model.from_pretrained("google/owlv2-base-patch16-ensemble")
        >>> processor = AutoProcessor.from_pretrained("google/owlv2-base-patch16-ensemble")
        >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
        >>> image = Image.open(requests.get(url, stream=True).raw)
        >>> inputs = processor(images=image, return_tensors="pt")
        >>> image_features = model.get_image_features(**inputs)
        ```rƒ  r   )r‹   rö   rG  rO  r€  r2  )	rD   rÅ   rö   rG  rÄ   rH  Úvision_outputsre  Úimage_featuress	            r)   Úget_image_featureszOwlv2Model.get_image_featuresr  s˜   € ð8 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆà×*Ñ*Ø%Ø/Ø!5Ø%=Ø#ð +ó 
ˆð ' qÑ)ˆØ×/Ñ/°Ó>ˆàÐr+   r\  Úreturn_lossÚreturn_base_image_embedsc
           	      ó(  — |�|n| j                   j                  }|�|n| j                   j                  }|	�|	n| j                   j                  }	| j	                  |||||	¬«      }
| j                  |||||	¬«      }|d   }| j                  |«      }|
d   }| j                  |«      }|t        j                  j                  |ddd¬«      z  }|t        j                  j                  |ddd¬«      z  }| j                  j                  «       j                  |j                  «      }t        j                  ||j!                  «       «      |z  }|j!                  «       }d}|rt#        |«      }|}|	s||||||
f}|�|f|z   S |S t%        |||||||
¬	«      S )
ai  
        Returns:

        Examples:
        ```python
        >>> from PIL import Image
        >>> import requests
        >>> from transformers import AutoProcessor, Owlv2Model

        >>> model = Owlv2Model.from_pretrained("google/owlv2-base-patch16-ensemble")
        >>> processor = AutoProcessor.from_pretrained("google/owlv2-base-patch16-ensemble")
        >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
        >>> image = Image.open(requests.get(url, stream=True).raw)
        >>> inputs = processor(text=[["a photo of a cat", "a photo of a dog"]], images=image, return_tensors="pt")
        >>> outputs = model(**inputs)
        >>> logits_per_image = outputs.logits_per_image  # this is the image-text similarity score
        >>> probs = logits_per_image.softmax(dim=1)  # we can take the softmax to get the label probabilities
        ```Nrƒ  rs  r   ra   r”   T)Úordr´   Úkeepdim)r4   r5   r6   r7   r8   r9   r:   )r‹   rö   rG  rO  r€  rj  r0  r2  r&   ÚlinalgÚnormr�  Úexprü   r#   Úmatmulr.   r1   r3   )rD   r×   rÅ   rô   r˜  rö   rG  rÄ   r™  rH  r•  Útext_outputsr7   r8   Útext_embeds_normr�  r6   r5   r4   Úoutputs                       r)   rÌ   zOwlv2Model.forward¡  sÕ  € ðB 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×B]ÑB]ˆà×*Ñ*Ø%Ø/Ø!5Ø%=Ø#ð +ó 
ˆð —‘ØØ)Ø/Ø!5Ø#ð 'ó 
ˆð # 1‘oˆØ×*Ñ*¨;Ó7ˆØ% aÑ(ˆØ×-Ñ-¨lÓ;ˆð $¤e§l¡l×&7Ñ&7¸È!ÐQSÐ]aÐ&7Ó&bÑbˆØ&¬¯©×):Ñ):¸;ÈAÐSUÐ_cÐ):Ó)dÑdÐð ×&Ñ&×*Ñ*Ó,×/Ñ/°×0CÑ0CÓDˆäŸ,™,Ð'7¸¿¹Ó9IÓJÈ[ÑXˆØ*×,Ñ,Ó.ÐàˆÙÜ˜oÓ.ˆDà&ˆáØ&¨¸ÀlÐT`ÐbpÐqˆFØ)-Ð)9�T�G˜fÑ$ÐE¸vÐEäØØ-Ø+Ø#Ø%Ø*Ø .ô
ð 	
r+   rV  r„  )	NNNNNNFNN)rM   rN   rO   r   r<  r˜   r   rf  r   r&   r
   rÎ   rQ   r“  r|  r—  ÚOWLV2_INPUTS_DOCSTRINGr   r3   rÝ   r	   r   rÌ   rÏ   rÐ   s   @r)   r/  r/  *  s  ø„ ð €Lð˜{õ ñ@ +Ð+FÓGð -1Ø15Ø,0Ø/3Ø&*ñ à˜EŸL™LÑ)ð ð ! §¡Ñ.ð ð $ D™>ð	 ð
 ' t™nð ð ˜d‘^ð ð 
×	Ñ	ò ó Hð ñD +Ð+HÓIð 59Ø,0Ø/3Ø).Ø&*ñ,à˜u×0Ñ0Ñ1ð,ð $ D™>ð,ð ' t™nð	,ð
 #'ð,ð ˜d‘^ð,ð 
×	Ñ	ò,ó Jð,ñ\ +Ð+AÓBÙ¨;À[ÔQð 15Ø48Ø15Ø&*Ø,0Ø/3Ø).Ø37Ø&*ñW
à˜E×,Ñ,Ñ-ðW
ð ˜u×0Ñ0Ñ1ðW
ð ! §¡Ñ.ð	W
ð
 ˜d‘^ðW
ð $ D™>ðW
ð ' t™nðW
ð #'ðW
ð #+¨4¡.ðW
ð ˜d‘^ðW
ð 
ˆu�kÐ!Ñ	"òW
ó Ró CôW
r+   r/  c                   ób   ‡ — e Zd Zddedefˆ fd„Zdej                  dej                  fd„Z	ˆ xZ
S )ÚOwlv2BoxPredictionHeadr‹   Úout_dimc                 ó"  •— t         ‰| �  «        |j                  j                  }t	        j
                  ||«      | _        t	        j
                  ||«      | _        t	        j                  «       | _	        t	        j
                  ||«      | _
        y rU   )r—   r˜   rŒ  rš   r   rè   Údense0Údense1ÚGELUÚgeluÚdense2)rD   r‹   r§  r­   rª   s       €r)   r˜   zOwlv2BoxPredictionHead.__init__ÿ  sb   ø€ Ü‰ÑÔà×$Ñ$×0Ñ0ˆÜ—i‘i  uÓ-ˆŒÜ—i‘i  uÓ-ˆŒÜ—G‘G“IˆŒ	Ü—i‘i  wÓ/ˆ�r+   r–  r    c                 ó°   — | j                  |«      }| j                  |«      }| j                  |«      }| j                  |«      }| j                  |«      }|S rU   )r©  r¬  rª  r­  )rD   r–  r£  s      r)   rÌ   zOwlv2BoxPredictionHead.forward  sM   € Ø—‘˜^Ó,ˆØ—‘˜6Ó"ˆØ—‘˜VÓ$ˆØ—‘˜6Ó"ˆØ—‘˜VÓ$ˆØˆr+   )é   )rM   rN   rO   r   r]   r˜   r&   r
   rQ   rÌ   rÏ   rÐ   s   @r)   r¦  r¦  þ  s3   ø„ ñ0˜{ð 0°Sõ 0ð e§l¡lð °u×7HÑ7H÷ r+   r¦  c            	       óž   ‡ — e Zd Zdefˆ fd„Zdej                  deej                     deej                     de	ej                     fd„Z
ˆ xZS )ÚOwlv2ClassPredictionHeadr‹   c                 ó”  •— t         ‰| �  «        |j                  j                  }|j                  j                  | _        t        j                  | j
                  |«      | _        t        j                  | j
                  d«      | _	        t        j                  | j
                  d«      | _
        t        j                  «       | _        y )Nr   )r—   r˜   r‰  rš   rŒ  Ú	query_dimr   rè   r©  Úlogit_shiftr�  ÚELUÚelu)rD   r‹   r§  rª   s      €r)   r˜   z!Owlv2ClassPredictionHead.__init__  sƒ   ø€ Ü‰ÑÔà×$Ñ$×0Ñ0ˆØ×-Ñ-×9Ñ9ˆŒä—i‘i §¡°Ó8ˆŒÜŸ9™9 T§^¡^°QÓ7ˆÔÜŸ9™9 T§^¡^°QÓ7ˆÔÜ—6‘6“8ˆ�r+   r8   Úquery_embedsÚ
query_maskr    c                 ó0  — | j                  |«      }|€S|j                  }|j                  d d \  }}t        j                  ||| j
                  f«      j                  |«      }||fS |t        j                  j                  |dd¬«      dz   z  }|t        j                  j                  |dd¬«      dz   z  }t        j                  d||«      }| j                  |«      }	| j                  |«      }
| j                  |
«      dz   }
||	z   |
z  }|�†|j                  dkD  rt        j                  |d¬	«      }t        j                  |d
k(  t        j                   |j"                  «      j$                  |«      }|j                  t        j&                  «      }||fS )Nra   r”   T)r´   rœ  g�íµ ÷Æ°>z...pd,...qd->...pqr   rÚ   r³   r   )r©  r#   rµ   r&   Úzerosr³  rü   r�  rž  Úeinsumr´  r�  r¶  Úndimr·   ÚwhereÚfinforW   re   rX   )rD   r8   r·  r¸  Úimage_class_embedsr#   rÉ   r£   Úpred_logitsr´  r�  s              r)   rÌ   z Owlv2ClassPredictionHead.forward  s…  € ð "Ÿ[™[¨Ó6ÐØÐØ'×.Ñ.ˆFØ&8×&>Ñ&>¸rÀÐ&BÑ#ˆJ˜ÜŸ+™+ z°;ÀÇÁÐ&OÓP×SÑSÐTZÓ[ˆKØÐ!3Ð4Ð4ð 0´5·<±<×3DÑ3DÐEWÐ]_ÐimÐ3DÓ3nÐquÑ3uÑvÐØ#¤u§|¡|×'8Ñ'8¸È2ÐW[Ð'8Ó'\Ð_cÑ'cÑdˆô —l‘lÐ#7Ð9KÈ\ÓZˆð ×&Ñ& |Ó4ˆØ×&Ñ& |Ó4ˆØ—h‘h˜{Ó+¨aÑ/ˆØ" [Ñ0°KÑ?ˆàÐ!Ø�‰ Ò"Ü"Ÿ_™_¨Z¸RÔ@�
äŸ+™+ j°A¡o´u·{±{À;×CTÑCTÓ7U×7YÑ7YÐ[fÓgˆKØ%Ÿ.™.¬¯©Ó7ˆKàÐ/Ð0Ð0r+   )rM   rN   rO   r   r˜   r&   rQ   r   r
   r   rÌ   rÏ   rÐ   s   @r)   r±  r±    s_   ø„ ð	˜{õ 	ð!1à×'Ñ'ð!1ð ˜u×0Ñ0Ñ1ð!1ð ˜UŸ\™\Ñ*ð	!1ð
 
ˆu× Ñ Ñ	!÷!1r+   r±  c                   óh  ‡ — e Zd ZeZdefˆ fd„Zedededej                  fd„«       Z
dej                  dej                  fd„Z ed	¬
«      	 d"dededeej                     dej                  fd„«       Z	 d#dej                  dej                  dedej                  fd„Z	 	 d$dej                  deej                     deej                     deej                     fd„Z	 	 	 d%dej                  dej                  dej                  dee   dee   dedeej                     fd„Z	 	 	 d%dej                  dee   dee   dedeej                     f
d„Z	 d#dej                  dej                  dedej                  fd„Z ee«       eee¬«      	 	 	 	 	 d&dej                  deej                     dee   dee   dedee   defd „«       «       Z ee«       eee¬«      	 	 	 	 	 d&dej                  dej                  deej                     dee   dee   dedee   defd!„«       «       Zˆ xZS )'ÚOwlv2ForObjectDetectionr‹   c                 óÞ  •— t         ‰| �  |«       t        |«      | _        t	        |«      | _        t        |«      | _        t        |d¬«      | _        t        j                  |j                  j                  |j                  j                  ¬«      | _        t        j                  «       | _        || _        | j"                  j                  j$                  | j"                  j                  j&                  z  | _        | j"                  j                  j$                  | j"                  j                  j&                  z  | _        | j-                  | j(                  | j*                  «      | _        y )Nr   )r§  r  )r—   r˜   r/  r"  r±  Ú
class_headr¦  Úbox_headÚobjectness_headr   r  rŒ  rš   r  Ú
layer_normÚSigmoidÚsigmoidr‹   r¢   r™   Únum_patches_heightÚnum_patches_widthÚcompute_box_biasÚbox_biasr©   s     €r)   r˜   z Owlv2ForObjectDetection.__init__E  sû   ø€ Ü‰Ñ˜Ô ä Ó'ˆŒ
Ü2°6Ó:ˆŒÜ.¨vÓ6ˆŒÜ5°fÀaÔHˆÔäŸ,™, v×';Ñ';×'GÑ'GÈV×MaÑMa×MpÑMpÔqˆŒÜ—z‘z“|ˆŒØˆŒØ"&§+¡+×";Ñ";×"FÑ"FÈ$Ï+É+×JcÑJc×JnÑJnÑ"nˆÔØ!%§¡×!:Ñ!:×!EÑ!EÈÏÉ×IbÑIb×ImÑImÑ!mˆÔØ×-Ñ-¨d×.EÑ.EÀt×G]ÑG]Ó^ˆ�r+   rÊ  rË  r    c                 ój  — t        j                  d|dz   t         j                  ¬«      }t        j                  d| dz   t         j                  ¬«      }t        j                  ||d¬«      \  }}t        j                  ||fd¬«      }|dxx   |z  cc<   |dxx   | z  cc<   |j                  dd	«      }|S )
Nr   )rW   Úxy)Úindexingr”   r³   ©.r   ©.r   ra   )r&   r'   rX   ÚmeshgridÚstackr½   )rÊ  rË  Úx_coordinatesÚy_coordinatesÚxxÚyyÚbox_coordinatess          r)   Ú!normalize_grid_corner_coordinatesz9Owlv2ForObjectDetection.normalize_grid_corner_coordinatesT  s¤   € ô Ÿ™ QÐ(9¸AÑ(=ÄUÇ]Á]ÔSˆÜŸ™ QÐ(:¸QÑ(>ÄeÇmÁmÔTˆÜ—‘ ¨}ÀtÔL‰ˆˆBô  Ÿ+™+ r¨2 h°BÔ7ˆØ˜ÓÐ#4Ñ4ÓØ˜ÓÐ#5Ñ5Óð *×.Ñ.¨r°1Ó5ˆàÐr+   r–  c                 óR   — |j                  «       }| j                  |«      }|d   }|S )a#  Predicts the probability that each image feature token is an object.

        Args:
            image_features (`torch.FloatTensor` of shape `(batch_size, num_patches, hidden_dim)`)):
                Features extracted from the image.
        Returns:
            Objectness scores.
        rÑ  )ÚdetachrÆ  )rD   r–  r}   s      r)   Úobjectness_predictorz,Owlv2ForObjectDetection.objectness_predictorf  s4   € ð (×.Ñ.Ó0ˆØ ×0Ñ0°Ó@ÐØ-¨fÑ5ÐØ Ð r+   ra   )ÚmaxsizeÚfeature_mapc                 óÌ  — |�t        d«      ‚| j                  ||«      }t        j                  |dd«      }t        j                  |dz   «      t        j
                  | dz   «      z
  }t        j                  |d«      }|dxx   |z  cc<   |dxx   |z  cc<   t        j                  |dz   «      t        j
                  | dz   «      z
  }t        j                  ||gd¬«      }|S )	NzOfeature_map has been deprecated as an input. Please pass in num_patches insteadr$  r'  g-Cëâ6?rÑ  rÒ  r”   r³   )ru   rÚ  r&   ÚclipÚlogÚlog1pÚ	full_liker¾   )	rD   rÊ  rË  rß  rÙ  Úbox_coord_biasÚbox_sizeÚbox_size_biasrÍ  s	            r)   rÌ  z(Owlv2ForObjectDetection.compute_box_biast  sß   € ð
 Ð"ÜÐnÓoÐoà×@Ñ@ÐASÐUfÓgˆÜŸ*™* _°c¸3Ó?ˆô Ÿ™ ?°TÑ#9Ó:¼U¿[¹[È/ÐIYÐ\`ÑI`Ó=aÑaˆô —?‘? >°3Ó7ˆØ�ÓÐ-Ñ-ÓØ�ÓÐ.Ñ.ÓÜŸ	™	 (¨T¡/Ó2´U·[±[À(ÀÈTÑAQÓ5RÑRˆô —9‘9˜n¨mÐ<À"ÔEˆØˆr+   Úimage_featsrÄ   c                 óî   — | j                  |«      }|r$|j                  \  }}}}| j                  ||«      }n| j                  }|j	                  |j
                  «      }||z  }| j                  |«      }|S )a  
        Args:
            image_feats:
                Features extracted from the image, returned by the `image_text_embedder` method.
            feature_map:
                A spatial re-arrangement of image_features, also returned by the `image_text_embedder` method.
            interpolate_pos_encoding:
                Whether to interpolate the pre-trained position encodings.
        Returns:
            pred_boxes:
                List of predicted boxes (cxcywh normalized to 0, 1) nested within a dictionary.
        )rÅ  rµ   rÌ  rÍ  rü   r#   rÉ  )	rD   rè  rß  rÄ   r~   rÊ   rÊ  rË  rÍ  s	            r)   Úbox_predictorz%Owlv2ForObjectDetection.box_predictor�  s|   € ð& —]‘] ;Ó/ˆ
ñ $Ø:E×:KÑ:KÑ7ˆAÐ!Ð#4°aØ×,Ñ,Ð-?ÐARÓS‰Hà—}‘}ˆHà—;‘;˜{×1Ñ1Ó2ˆØ�hÑˆ
Ø—\‘\ *Ó-ˆ
ØÐr+   r·  r¸  c                 ó6   — | j                  |||«      \  }}||fS )a8  
        Args:
            image_feats:
                Features extracted from the `image_text_embedder`.
            query_embeds:
                Text query embeddings.
            query_mask:
                Must be provided with query_embeddings. A mask indicating which query embeddings are valid.
        )rÄ  )rD   rè  r·  r¸  rÀ  r¿  s         r)   Úclass_predictorz'Owlv2ForObjectDetection.class_predictor¯  s)   € ð -1¯O©O¸KÈÐWaÓ,bÑ)ˆÐ(àÐ/Ð0Ð0r+   r×   rÅ   rô   rö   rG  c           	      ó¢  — | j                  ||||||d¬«      }|rX|j                  \  }}}	}
|	| j                  j                  j                  z  }|
| j                  j                  j                  z  }n| j
                  }| j                  }|j                  d   }| j                   j                  j                  |«      }t        j                  |d d …d d…d d …f   |d d …d d…f   j                  «      }|d d …dd …d d …f   |z  }| j                  |«      }|j                  d   |||j                  d   f}|j                  |«      }|d   }|||fS )NT)rÅ   r×   rô   rö   rG  rÄ   rH  r   r   r”   éüÿÿÿ)r"  rµ   r‹   rŒ  r™   rÊ  rË  r:   r€  ry  r&   Úbroadcast_torÇ  rº   )rD   r×   rÅ   rô   rö   rG  rÄ   r  rÊ   r¬   r­   rÊ  rË  rM  r8   Úclass_token_outÚnew_sizer7   s                     r)   Úimage_text_embedderz+Owlv2ForObjectDetection.image_text_embedderÃ  sv  € ð —*‘*Ø%ØØ)Ø/Ø!5Ø%=Øð ó 
ˆñ $Ø".×"4Ñ"4ÑˆAˆq�&˜%Ø!'¨4¯;©;×+DÑ+D×+OÑ+OÑ!OÐØ %¨¯©×)BÑ)B×)MÑ)MÑ MÑà!%×!8Ñ!8ÐØ $× 6Ñ 6Ðð $×7Ñ7¸Ñ:ÐØ—z‘z×.Ñ.×=Ñ=Ð>OÓPˆô  ×,Ñ,¨\º!¸R¸a¸RÂ¸(Ñ-CÀ\ÒRSÐUXÐVXÐUXÐRXÑEY×E_ÑE_Ó`ˆð $¢A q¡rª1 HÑ-°Ñ?ˆØ—‘ |Ó4ˆð ×Ñ˜qÑ!ØØØ×Ñ˜rÑ"ð	
ˆð $×+Ñ+¨HÓ5ˆØ˜b‘kˆà˜\¨7Ð3Ð3r+   c                 óŽ  — | j                   j                  ||d¬«      }|rX|j                  \  }}}}|| j                  j                  j
                  z  }	|| j                  j                  j
                  z  }
n| j                  }	| j                  }
|d   }| j                   j                  j                  |«      }t        j                  |d d …d d…d d …f   |d d …d d…f   j                  «      }|d d …dd …d d …f   |z  }| j                  |«      }|j                  d   |	|
|j                  d   f}|j                  |«      }||fS )NT)rÅ   rÄ   rH  r   r   r”   )r"  r€  rµ   r‹   rŒ  r™   rÊ  rË  ry  r&   rï  rÇ  rº   )rD   rÅ   rö   rG  rÄ   r•  rÊ   r¬   r­   rÊ  rË  rM  r8   rð  rñ  s                  r)   Úimage_embedderz&Owlv2ForObjectDetection.image_embedder÷  s_  € ð Ÿ™×0Ñ0Ø%Ð@XÐfjð 1ó 
ˆñ $Ø".×"4Ñ"4ÑˆAˆq�&˜%Ø!'¨4¯;©;×+DÑ+D×+OÑ+OÑ!OÐØ %¨¯©×)BÑ)B×)MÑ)MÑ MÑà!%×!8Ñ!8ÐØ $× 6Ñ 6Ðð +¨1Ñ-ÐØ—z‘z×.Ñ.×=Ñ=Ð>OÓPˆô  ×,Ñ,¨\º!¸R¸a¸RÂ¸(Ñ-CÀ\ÒRSÐUXÐVXÐUXÐRXÑEY×E_ÑE_Ó`ˆð $¢A q¡rª1 HÑ-°Ñ?ˆØ—‘ |Ó4ˆð ×Ñ˜qÑ!ØØØ×Ñ˜rÑ"ð	
ˆð $×+Ñ+¨HÓ5ˆà˜nÐ-Ð-r+   Úquery_image_featuresÚquery_feature_mapc                 ój  — | j                  |«      \  }}| j                  |||«      }t        |«      }g }g }	|j                  }
t	        |j
                  d   «      D �]  }t        j                  g d¢g|
¬«      }||   }t        ||«      \  }}t        j                  |d   dk(  «      rt        ||«      }t        j                  |«      dz  }|d   |k\  j                  «       }|j                  «       sŒ˜||   |j                  d«         }t        j                  ||   d¬«      }t        j                   d||«      }|t        j"                  |«         }|j%                  ||   |   «       |	j%                  |«       �Œ" |r+t        j&                  |«      }t        j&                  |	«      }nd	\  }}|||fS )
Nr   )r   r   r   r   r"   r$  gš™™™™™é?r   )Úaxiszd,id->i©NN)rì  rê  r   r#   rD  rµ   r&   rí   rr   rt   ry   rf   ÚnonzeroÚnumelÚsqueezer%  r»  ÚargminÚappendrÔ  )rD   rõ  rö  rÄ   rÊ   r   r~   Úpred_boxes_as_cornersÚbest_class_embedsÚbest_box_indicesÚpred_boxes_deviceÚiÚeach_query_boxÚeach_query_pred_boxesÚiousÚiou_thresholdÚselected_indsÚselected_embeddingsÚmean_embedsÚmean_simÚbest_box_indr·  Úbox_indicess                          r)   Úembed_image_queryz)Owlv2ForObjectDetection.embed_image_query"  s±  € ð ×.Ñ.Ð/CÓD‰ˆˆ<Ø×'Ñ'Ð(<Ð>OÐQiÓjˆ
Ü 8¸Ó DÐð ÐØÐØ1×8Ñ8ÐäÐ+×1Ñ1°!Ñ4Ó5ó 	6ˆAÜ"Ÿ\™\ª<¨.ÐARÔSˆNØ$9¸!Ñ$<Ð!Ü˜nÐ.CÓD‰GˆD�!ô �y‰y˜˜a™ C™Ô(Ü*¨>Ð;PÓQ�ô "ŸI™I d›O¨cÑ1ˆMà! !™W¨Ñ5×>Ñ>Ó@ˆMØ×"Ñ"Õ$Ø&2°1¡o°m×6KÑ6KÈAÓ6NÑ&OÐ#Ü#Ÿj™j¨°a©¸qÔA�Ü Ÿ<™<¨	°;Ð@SÓT�Ø,¬U¯\©\¸(Ó-CÑD�Ø!×(Ñ(¨°a©¸Ñ)FÔGØ ×'Ñ'¨Ö5ð'	6ñ* Ü Ÿ;™;Ð'8Ó9ˆLÜŸ+™+Ð&6Ó7‰Kà(2Ñ%ˆL˜+à˜[¨*Ð4Ð4r+   r\  Úquery_pixel_valuesrH  c           
      ó®  — |�|n| j                   j                  }|�|n| j                   j                  }|�|n| j                   j                  }| j	                  ||¬«      d   }| j	                  ||||¬«      \  }}	|j
                  \  }
}}}t        j                  ||
||z  |f«      }|j
                  \  }
}}}t        j                  ||
||z  |f«      }| j                  |||«      \  }}}| j                  ||¬«      \  }}| j                  |||«      }|s+|||||||	j                  «       f}t        d„ |D «       «      }|S t        ||||||d|	¬«      S )aW  
        Returns:

        Examples:
        ```python
        >>> import requests
        >>> from PIL import Image
        >>> import torch
        >>> from transformers import AutoProcessor, Owlv2ForObjectDetection

        >>> processor = AutoProcessor.from_pretrained("google/owlv2-base-patch16-ensemble")
        >>> model = Owlv2ForObjectDetection.from_pretrained("google/owlv2-base-patch16-ensemble")

        >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
        >>> image = Image.open(requests.get(url, stream=True).raw)
        >>> query_url = "http://images.cocodataset.org/val2017/000000001675.jpg"
        >>> query_image = Image.open(requests.get(query_url, stream=True).raw)
        >>> inputs = processor(images=image, query_images=query_image, return_tensors="pt")

        >>> # forward pass
        >>> with torch.no_grad():
        ...     outputs = model.image_guided_detection(**inputs)

        >>> target_sizes = torch.Tensor([image.size[::-1]])

        >>> # Convert outputs (bounding boxes and class logits) to Pascal VOC format (xmin, ymin, xmax, ymax)
        >>> results = processor.post_process_image_guided_detection(
        ...     outputs=outputs, threshold=0.9, nms_threshold=0.3, target_sizes=target_sizes
        ... )
        >>> i = 0  # Retrieve predictions for the first image
        >>> boxes, scores = results[i]["boxes"], results[i]["scores"]
        >>> for box, score in zip(boxes, scores):
        ...     box = [round(i, 2) for i in box.tolist()]
        ...     print(f"Detected similar object with confidence {round(score.item(), 3)} at location {box}")
        Detected similar object with confidence 0.938 at location [327.31, 54.94, 547.39, 268.06]
        Detected similar object with confidence 0.959 at location [5.78, 360.65, 619.12, 366.39]
        Detected similar object with confidence 0.902 at location [2.85, 360.01, 627.63, 380.8]
        Detected similar object with confidence 0.985 at location [176.98, -29.45, 672.69, 182.83]
        Detected similar object with confidence 1.0 at location [6.53, 14.35, 624.87, 470.82]
        Detected similar object with confidence 0.998 at location [579.98, 29.14, 615.49, 489.05]
        Detected similar object with confidence 0.985 at location [206.15, 10.53, 247.74, 466.01]
        Detected similar object with confidence 0.947 at location [18.62, 429.72, 646.5, 457.72]
        Detected similar object with confidence 0.996 at location [523.88, 20.69, 586.84, 483.18]
        Detected similar object with confidence 0.998 at location [3.39, 360.59, 617.29, 499.21]
        Detected similar object with confidence 0.969 at location [4.47, 449.05, 614.5, 474.76]
        Detected similar object with confidence 0.966 at location [31.44, 463.65, 654.66, 471.07]
        Detected similar object with confidence 0.924 at location [30.93, 468.07, 635.35, 475.39]
        ```N)rÅ   rÄ   r   )rÅ   rö   rG  rÄ   )rè  r·  c              3   ó&   K  — | ]	  }|€Œ|–— Œ y ­wrU   rS   ©rB   Úxs     r)   rE   zAOwlv2ForObjectDetection.image_guided_detection.<locals>.<genexpr>¶  ó   è ø€ Ò> °±œ1Ñ>ùrL  )r8   r„   r…   r†   r   r   r9   r:   )r‹   rö   rG  rH  rô  rµ   r&   rº   r  rì  rê  r@   rI   rƒ   )rD   rÅ   r  rö   rG  rÄ   rH  rö  rß  r•  rÉ   rÊ  rË  Ú
hidden_dimrè  Úquery_image_featsr·  r  r†   rÀ  r   r…   r£  s                          r)   Úimage_guided_detectionz.Owlv2ForObjectDetection.image_guided_detectionN  sÑ  € ðv 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×BYÑBYˆð !×/Ñ/Ø+ÐF^ð 0ó 
à
ñÐð '+×&9Ñ&9Ø%Ø/Ø!5Ø%=ð	 ':ó '
Ñ#ˆ�^ð IT×HYÑHYÑEˆ
Ð&Ð(9¸:Ü—m‘m K°*Ð>PÐSdÑ>dÐfpÐ1qÓrˆàHY×H_ÑH_ÑEˆ
Ð&Ð(9¸:Ü!ŸM™MØ 
Ð,>ÐARÑ,RÐT^Ð_ó
Ðð <@×;QÑ;QØÐ0Ð2Jó<
Ñ8ˆÐ&Ð(8ð
 '+×&:Ñ&:À{ÐamÐ&:Ó&nÑ#ˆ�lð !×.Ñ.¨{¸KÐIaÓbÐáàØ!Ø!Ø ØØØ×'Ñ'Ó)ðˆFô Ñ> fÔ>Ó>ˆFØˆMä4Ø$Ø0Ø/Ø-ØØ%Ø"Ø .ô	
ð 		
r+   c           
      ó$  — |�|n| j                   j                  }|�|n| j                   j                  }|�|n| j                   j                  }| j	                  ||||||¬«      \  }}	}
|
j
                  }|
j                  }|	j                  \  }}}}t        j                  |	|||z  |f«      }|j                  d   |z  }|j                  |||j                  d   «      }|j                  |||j                  d   «      }|d   dkD  }| j                  |||«      \  }}| j                  |«      }| j                  ||	|«      }|s:|||||	||j                  «       |j                  «       f}t        d„ |D «       «      }|S t        |	|||||||¬«      S )aR  
        Returns:

        Examples:
        ```python
        >>> import requests
        >>> from PIL import Image
        >>> import torch

        >>> from transformers import Owlv2Processor, Owlv2ForObjectDetection

        >>> processor = Owlv2Processor.from_pretrained("google/owlv2-base-patch16-ensemble")
        >>> model = Owlv2ForObjectDetection.from_pretrained("google/owlv2-base-patch16-ensemble")

        >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
        >>> image = Image.open(requests.get(url, stream=True).raw)
        >>> text_labels = [["a photo of a cat", "a photo of a dog"]]
        >>> inputs = processor(text=text_labels, images=image, return_tensors="pt")
        >>> outputs = model(**inputs)

        >>> # Target image sizes (height, width) to rescale box predictions [batch_size, 2]
        >>> target_sizes = torch.tensor([(image.height, image.width)])
        >>> # Convert outputs (bounding boxes and class logits) to Pascal VOC format (xmin, ymin, xmax, ymax)
        >>> results = processor.post_process_grounded_object_detection(
        ...     outputs=outputs, target_sizes=target_sizes, threshold=0.1, text_labels=text_labels
        ... )
        >>> # Retrieve predictions for the first image for the corresponding text queries
        >>> result = results[0]
        >>> boxes, scores, text_labels = result["boxes"], result["scores"], result["text_labels"]
        >>> for box, score, text_label in zip(boxes, scores, text_labels):
        ...     box = [round(i, 2) for i in box.tolist()]
        ...     print(f"Detected {text_label} with confidence {round(score.item(), 3)} at location {box}")
        Detected a photo of a cat with confidence 0.614 at location [341.67, 23.39, 642.32, 371.35]
        Detected a photo of a cat with confidence 0.665 at location [6.75, 51.96, 326.62, 473.13]
        ```)r×   rÅ   rô   rö   rG  rÄ   r   r”   rÑ  c              3   ó&   K  — | ]	  }|€Œ|–— Œ y ­wrU   rS   r  s     r)   rE   z2Owlv2ForObjectDetection.forward.<locals>.<genexpr>&  r  rL  )r8   r7   r~   r   r}   r   r9   r:   )r‹   rö   rG  rH  rò  r9   r:   rµ   r&   rº   rì  rÝ  rê  r@   rI   r{   )rD   r×   rÅ   rô   rö   rG  rÄ   rH  r·  rß  r  r¡  r•  rÉ   rÊ  rË  r  rè  Úmax_text_queriesr¸  rÀ  r   r}   r~   r£  s                            r)   rÌ   zOwlv2ForObjectDetection.forwardÄ  sî  € ð^ 2CÐ1NÑ-ÐTX×T_ÑT_×TqÑTqÐà$8Ð$DÑ È$Ï+É+×JjÑJjð 	ð &1Ð%<‘kÀ$Ç+Á+×BYÑBYˆð .2×-EÑ-EØØ%Ø)Ø/Ø!5Ø%=ð .Fó .
Ñ*ˆ�k 7ð ×0Ñ0ˆØ ×4Ñ4ˆàHS×HYÑHYÑEˆ
Ð&Ð(9¸:Ü—m‘m K°*Ð>PÐSdÑ>dÐfpÐ1qÓrˆð %Ÿ?™?¨1Ñ-°Ñ;ÐØ#×+Ñ+¨JÐ8HÈ,×J\ÑJ\Ð]_ÑJ`Óaˆð ×%Ñ% jÐ2BÀIÇOÁOÐTVÑDWÓXˆ	Ø˜vÑ&¨Ñ*ˆ
ð '+×&:Ñ&:¸;ÈÐV`Ó&aÑ#ˆ�lð !×5Ñ5°kÓBÐð ×'Ñ'¨°[ÐBZÓ[ˆ
áàØ!ØØØØØ×%Ñ%Ó'Ø×'Ñ'Ó)ð	ˆFô Ñ> fÔ>Ó>ˆFØˆMä)Ø$Ø$Ø!ØØ/Ø%Ø*Ø .ô	
ð 		
r+   rU   rÍ   rù  r  r„  ) rM   rN   rO   r   r<  r˜   Ústaticmethodr]   r&   r
   rÚ  rQ   rÝ  r   r   rÌ  rÎ   rê  r   rì  rò  rô  r  r   Ú4OWLV2_IMAGE_GUIDED_OBJECT_DETECTION_INPUTS_DOCSTRINGr   rƒ   r  Ú'OWLV2_OBJECT_DETECTION_INPUTS_DOCSTRINGr{   rÌ   rÏ   rÐ   s   @r)   rÂ  rÂ  B  sÂ  ø„ Ø€Lð_˜{õ _ð ð¸cð ÐVYð Ð^c×^jÑ^jò ó ðð !°5×3DÑ3Dð !È×IZÑIZó !ñ �qÔð koñØ"%ðØ:=ðØLTÐUZ×UfÑUfÑLgðà	�‰òó ðð6 */ñ	à×&Ñ&ðð ×&Ñ&ðð #'ð	ð
 
×	Ñ	óðJ 59Ø-1ñ	1à×&Ñ&ð1ð ˜u×0Ñ0Ñ1ð1ð ˜UŸ\™\Ñ*ð	1ð
 
ˆu× Ñ Ñ	!ó1ð2 -1Ø/3Ø).ñ14à—<‘<ð14ð ×'Ñ'ð14ð Ÿ™ð	14ð
 $ D™>ð14ð ' t™nð14ð #'ð14ð 
ˆu× Ñ Ñ	!ó14ðn -1Ø/3Ø).ñ(.à×'Ñ'ð(.ð $ D™>ð(.ð ' t™nð	(.ð
 #'ð(.ð 
ˆu× Ñ Ñ	!ó(.ð^ */ñ	*5à#×/Ñ/ð*5ð !×,Ñ,ð*5ð #'ð	*5ð
 
×	Ñ	ó*5ñX +Ð+_Ó`ÙÐ+PÐ_jÔkð ;?Ø,0Ø/3Ø).Ø&*ñr
à×'Ñ'ðr
ð % U×%6Ñ%6Ñ7ðr
ð $ D™>ð	r
ð
 ' t™nðr
ð #'ðr
ð ˜d‘^ðr
ð 
/òr
ó ló aðr
ñh +Ð+RÓSÙÐ+EÐT_Ô`ð
 26Ø,0Ø/3Ø).Ø&*ñl
à—<‘<ðl
ð ×'Ñ'ðl
ð ! §¡Ñ.ð	l
ð
 $ D™>ðl
ð ' t™nðl
ð #'ðl
ð ˜d‘^ðl
ð 
$òl
ó aó Tôl
r+   rÂ  )r/  r!  rh  r~  rÂ  )KrP   Údataclassesr   Ú	functoolsr   Útypingr   r   r   r   r	   r&   Útorch.utils.checkpointr
   r   Úactivationsr   Úmodeling_attn_mask_utilsr   r   Úmodeling_outputsr   r   Úmodeling_utilsr   Úutilsr   r   r   r   r   r   r   Úconfiguration_owlv2r   r   r   Útransformers.image_transformsr   Ú
get_loggerrM   ÚloggerÚ_CHECKPOINT_FOR_DOCr*   r1   r3   r^   rb   rr   ry   r{   rƒ   rt  rŠ   rÒ   rß   r	  r  r!  ÚOWLV2_START_DOCSTRINGrf  r|  r¤  r  r  rA  rX  rh  rv  r~  r/  r¦  r±  rÂ  Ú__all__rS   r+   r)   ú<module>r.     sr  ðñ å !Ý ß 4Õ 4ã Û ß å !ß dß KÝ -÷÷ ñ ÷ QÑ Pñ ÔÝFð 
ˆ×	Ñ	˜HÓ	%€à:Ð ð`˜UŸ\™\ð `¨e¯l©ló `ð
-˜5Ÿ<™<ð -¨E¯L©Ló -ð ô"
�+ó "
ó ð"
ðLGˆvð G˜&ó GðE�Fð E˜vó Eò"ò"'ð0 ô2
 ó 2
ó ð2
ðj ô-
¨Kó -
ó ð-
ôbJ˜BŸI™Iô Jô\˜"Ÿ)™)ô ô@h2�R—Y‘Yô h2ôXˆr�y‰yô ô /˜Ÿ	™	ô /ôf1%˜?ô 1%ðhÐ ð Ð ð*!Ð ð Ð ð:+Ð 'ð,8Ð 4ô(V
�2—9‘9ô V
ôtG
˜2Ÿ9™9ô G
ôV1
Ð)ô 1
ôj8
˜RŸY™Yô 8
ôx1
Ð+ô 1
ñh Ð+Ó,ôN
Ð%ó N
ó -ðN
ôd˜RŸY™Yô ô(-1˜rŸy™yô -1ô`p
Ð2ô p
òf r�r+   