
     `iF                        d dl mZ d dlmZmZmZ d dlZd dlZd dlm	Z	 d dl
mZ ddlmZ ddlmZ dd	lmZmZ dd
lmZ ddlmZmZmZ ddlmZ ddlmZ ddlmZ ddlm Z  e ed           G d de                                  Z! G d de	j"                  Z#d Z$dBdZ%dej&        de'dej&        fdZ(	 dCde	j"        d ej&        d!ej&        d"ej&        d#eej&                 d$e)d%e)d&ee         fd'Z* G d( d)e	j"                  Z+ G d* d+e	j"                  Z, G d, d-e	j"                  Z-d.ej&        d/ej&        d0ej&        dej&        fd1Z. G d2 d3e	j"                  Z/ G d4 d5e	j"                  Z0e G d6 d7e                      Z1d8ej&        d9e)de2ej&        ej&        f         fd:Z3d;ej&        d<e'd=e'dej&        fd>Z4 ed?           G d@ dAe1                      Z5d7dAgZ6dS )D    )	dataclass)CallableOptionalUnionN)nnpad_sequence   )ACT2FN)FlashAttentionKwargs)ALL_ATTENTION_FUNCTIONSPreTrainedModel)Unpack)ModelOutputTransformersKwargsauto_docstring)deprecate_kwarg)can_return_tuple   )AutoModelForKeypointDetection   )LightGlueConfiga  
    Base class for outputs of LightGlue keypoint matching models. Due to the nature of keypoint detection and matching,
    the number of keypoints is not fixed and can vary from image to image, which makes batching non-trivial. In the
    batch of images, the maximum number of matches is set as the dimension of the matches and matching scores. The mask
    tensor is used to indicate which values in the keypoints, matches, matching_scores and prune tensors are keypoint
    matching information.
    )custom_introc                   L   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ej                          ed	<   dZeeej                          ed
<   dS )LightGlueKeypointMatchingOutputa  
    loss (`torch.FloatTensor` of shape `(1,)`, *optional*):
        Loss computed during training.
    matches (`torch.FloatTensor` of shape `(batch_size, 2, num_matches)`):
        Index of keypoint matched in the other image.
    matching_scores (`torch.FloatTensor` of shape `(batch_size, 2, num_matches)`):
        Scores of predicted matches.
    keypoints (`torch.FloatTensor` of shape `(batch_size, num_keypoints, 2)`):
        Absolute (x, y) coordinates of predicted keypoints in a given image.
    prune (`torch.IntTensor` of shape `(batch_size, num_keypoints)`):
        Pruning mask indicating which keypoints are removed and at which layer.
    mask (`torch.BoolTensor` of shape `(batch_size, num_keypoints)`):
        Mask indicating which values in matches, matching_scores, keypoints and prune are keypoint matching
        information.
    hidden_states (`Tuple[torch.FloatTensor, ...]`, *optional*):
        Tuple of `torch.FloatTensor` (one for the output of each stage) of shape `(batch_size, 2, num_channels,
        num_keypoints)` returned when `output_hidden_states=True` is passed or when
        `config.output_hidden_states=True`
    attentions (`Tuple[torch.FloatTensor, ...]`, *optional*):
        Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, 2, num_heads, num_keypoints,
        num_keypoints)` returned when `output_attentions=True` is passed or when
        `config.output_attentions=True`
    Nlossmatchesmatching_scores	keypointsprunemaskhidden_states
attentions)__name__
__module____qualname____doc__r   r   torchFloatTensor__annotations__r   r   r   r    	IntTensorr!   r"   tupler#        /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/transformers/models/lightglue/modeling_lightglue.pyr   r   '   s          0 )-D(5$
%,,,+/GXe'(///37OXe/0777-1Ix)*111'+E8EO$+++(,D(5$
%,,,8<M8E%"345<<<59Ju01299999r.   r   c                        e Zd Zdef fdZ	 ddej        dee         de	e
ej                 e
ej        ej        f         f         fdZ xZS )	LightGluePositionalEncoderconfigc                     t                                                       t          j        d|j        |j        z  dz  d          | _        d S )Nr   Fbias)super__init__r   Lineardescriptor_dimnum_attention_heads	projectorselfr2   	__class__s     r/   r7   z#LightGluePositionalEncoder.__init__U   sG    1f&;v?Y&Y]^&^ejkkkr.   Fr   output_hidden_statesreturnc                     |                      |          }|                    dd          }t          j        |          }t          j        |          }||f}|r||fn|f}|S )Nr   dim)r;   repeat_interleaver(   cossin)r=   r   r?   projected_keypoints
embeddingscosinessinesoutputs           r/   forwardz"LightGluePositionalEncoder.forwardY   st     #nnY77(::1":EE
)J''	*%%u%
6J]*122Q[P]r.   F)r$   r%   r&   r   r7   r(   Tensorr   boolr   r,   rM   __classcell__r>   s   @r/   r1   r1   T   s        l l l l l l l
 OT	 		=Ed^		uU\"E%,*D$EE	F	 	 	 	 	 	 	 	r.   r1   c                     | dd d df         }| ddd df         }t          j        | |gd                              d          }|S )N.r   r   rB   rC   )r(   stackflatten)xx1x2rot_xs       r/   rotate_halfr[   e   sU    	
3!8B	
319BK"b	r***22266ELr.   c                 l   | j         }|                                 } |                                }|                    |          }|                    |          }| |z  t          |           |z  z   }||z  t          |          |z  z   }|                    |          |                    |          fS )a  Applies Rotary Position Embedding to the query and key tensors.

    Args:
        q (`torch.Tensor`): The query tensor.
        k (`torch.Tensor`): The key tensor.
        cos (`torch.Tensor`): The cosine part of the rotary embedding.
        sin (`torch.Tensor`): The sine part of the rotary embedding.
        position_ids (`torch.Tensor`, *optional*):
            Deprecated and unused.
        unsqueeze_dim (`int`, *optional*, defaults to 1):
            The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and
            sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note
            that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and
            k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes
            cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have
            the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2.
    Returns:
        `tuple(torch.Tensor)` comprising of the query and key tensors rotated using the Rotary Position Embedding.
    dtype)r^   float	unsqueezer[   to)	qkrF   rG   position_idsunsqueeze_dimr^   q_embedk_embeds	            r/   apply_rotary_pos_embrh   m   s    ( GE			A			A
--
&
&C
--
&
&C3w;q>>C/0G3w;q>>C/0G::E:""GJJUJ$;$;;;r.   r"   n_repr@   c                     | j         \  }}}}|dk    r| S | dddddddddf                             |||||          } |                     |||z  ||          S )z
    This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch,
    num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim)
    r   N)shapeexpandreshape)r"   ri   batchnum_key_value_headsslenhead_dims         r/   	repeat_kvrr      s    
 2?1D.Ehzz!!!!QQQaaa"23::5BUW\^bdlmmM  (;e(CT8TTTr.           modulequerykeyvalueattention_maskscalingdropoutkwargsc                 R   t          || j                  }t          || j                  }	t          j        ||                    dd                    |z  }
|$|d d d d d d d |j        d         f         }|
|z   }
t          j                            |
dt          j	                  
                    |j                  }
t          j                            |
|| j                  }
t          j        |
|	          }|                    dd                                          }||
fS )Nr   r
   rT   rB   )rD   r^   )ptrainingr   )rr   num_key_value_groupsr(   matmul	transposerk   r   
functionalsoftmaxfloat32ra   r^   rz   r~   
contiguous)rt   ru   rv   rw   rx   ry   rz   r{   
key_statesvalue_statesattn_weightscausal_maskattn_outputs                r/   eager_attention_forwardr      s    3 ;<<JUF$?@@L<z';';Aq'A'ABBWLL!$QQQ111.D
0@0D.D%DE#k1=((2U](SSVVW\WbccL=((6?([[L,|\::K''1--88::K$$r.   c                   :    e Zd ZdZdedef fdZ eddd          	 	 	 	 dd
ej	        de
eej	        ej	        f                  de
ej	                 de
ej	                 de
ej	                 dee         deej	        e
ej	                 f         fd            Z xZS )LightGlueAttentionz=Multi-headed attention from 'Attention Is All You Need' paperr2   	layer_idxc                    t                                                       || _        || _        t	          |d|j        |j        z            | _        |j        |j        z  | _	        | j        dz  | _
        |j        | _        d| _        t          j        |j        |j        | j        z  |j                  | _        t          j        |j        |j        | j        z  |j                  | _        t          j        |j        |j        | j        z  |j                  | _        t          j        |j        | j        z  |j        |j                  | _        d S )Nrq   g      Tr4   )r6   r7   r2   r   getattrhidden_sizer:   rq   ro   r   ry   attention_dropout	is_causalr   r8   attention_biasq_projk_projv_projo_projr=   r2   r   r>   s      r/   r7   zLightGlueAttention.__init__   sB   "
F4F&Jd4dee$*$>&B\$\!}d*!'!9i :T] JQWQf
 
 
 i :T] JQWQf
 
 
 i :T] JQWQf
 
 
 i&68JQWQf
 
 
r.   past_key_valuepast_key_valuesz4.58)new_nameversionNr"   position_embeddingsrx   encoder_hidden_statesencoder_attention_maskr{   r@   c                    |j         d d         }g |d| j        R }|                     |                              |                              dd          }	|d u}
|
r|n|}|
r|n|}|                     |                              |                              dd          }|                     |                              |                              dd          }||\  }}t          |	|||          \  }	}t          }| j	        j
        dk    rt          | j	        j
                 } || |	|||f| j        sdn| j        | j        d|\  }} |j        g |dR                                  }|                     |          }||fS )NrB   r   r   eagerrs   )rz   ry   )rk   rq   r   viewr   r   r   rh   r   r2   _attn_implementationr   r~   r   ry   rm   r   r   )r=   r"   r   rx   r   r   r{   input_shapehidden_shapequery_statesis_cross_attentioncurrent_statescurrent_attention_maskr   r   rF   rG   attention_interfacer   r   s                       r/   rM   zLightGlueAttention.forward   s    $)#2#.88b8$-88{{=1166|DDNNqRSTT2$>2DW..-;M!a!7!7Sa[[0055lCCMMaQRSS
{{>2277EEOOPQSTUU**HC';L*VY[^'_'_$L*(?;+w66"9$+:Z"[$7$7"	%
  $}HCC$2HL	%
 	%
 	%
 	%
!\ *k);;;;;;FFHHkk+..L((r.   )NNNN)r$   r%   r&   r'   r   intr7   r   r(   rO   r   r,   r   r   rM   rQ   rR   s   @r/   r   r      s"       GG
 
3 
 
 
 
 
 
. _%0A6RRR LP158<9=*) *)|*) &eEL%,,F&GH*) !.	*)
  (5*) !) 6*) -.*) 
u|Xel33	4*) *) *) SR*) *) *) *) *)r.   r   c                   H     e Zd Zdef fdZdej        dej        fdZ xZS )LightGlueMLPr2   c                 T   t                                                       || _        t          |j                 | _        t          j        |j        |j                  | _	        t          j        |j        |j
                  | _        t          j        |j        d          | _        d S )NT)elementwise_affine)r6   r7   r2   r   
hidden_actactivation_fnr   r8   intermediate_sizefc1r   fc2	LayerNorm
layer_normr<   s     r/   r7   zLightGlueMLP.__init__   s    #F$569V5v7OPP9V5v7IJJ,v'?TXYYYr.   r"   r@   c                     |                      |          }|                     |          }|                     |          }|                     |          }|S N)r   r   r   r   )r=   r"   s     r/   rM   zLightGlueMLP.forward  sN    //66**=99//r.   	r$   r%   r&   r   r7   r(   rO   rM   rQ   rR   s   @r/   r   r      sq        Z Z Z Z Z Z ZU\ el        r.   r   c                        e Zd Zdedef fdZ	 	 ddej        dej        dej        dee	         d	ee	         d
e
ej        ee
ej                          ee
ej                          f         fdZ xZS )LightGlueTransformerLayerr2   r   c                     t                                                       t          ||          | _        t	          |          | _        t          ||          | _        t	          |          | _        d S r   )r6   r7   r   self_attentionr   self_mlpcross_attention	cross_mlpr   s      r/   r7   z"LightGlueTransformerLayer.__init__  s_    0CC$V,,1&)DD%f--r.   Fdescriptorsr   rx   r?   output_attentionsr@   c                 2   |rdnd }|rdnd }|r||fz   }|j         \  }}	}
|                     ||||          \  }}t          j        ||gd          }|                     |          }||z   }|r||f}|                    dd|	|
                              d                              ||	|
          }|B|                    dddd|	                              d                              |dd|	          nd }|                     ||||          \  }}t          j        ||gd          }|                     |          }||z   }|r>||f}||                    ||	|
          fz   |z   |                    ||	|
          fz   |z   }|r
||fz   |fz   }|||fS )Nr-   )r   rx   r   rB   rC   r   r   )r   r   r   )	rk   r   r(   catr   rm   flipr   r   )r=   r   r   rx   r?   r   all_hidden_statesall_attentions
batch_sizenum_keypointsr9   attention_outputself_attentionsintermediate_statesoutput_statesself_attention_descriptorsself_attention_hidden_statesr   r   cross_attention_outputcross_attentionscross_intermediate_statescross_output_statescross_attention_hidden_statess                           r/   rM   z!LightGlueTransformerLayer.forward  sY    #7@BBD0:d 	C 1[N B4?4E1
M> -1,?,? ))/	 -@ -
 -
)/ $i6F(GRPPP&9::%0=%@" 	P,?+O( '..r1m^TTT!WWWZ?? 	 ) ""2q!Q>>CCAFFNNz[\^_anooo 	 483G3G&"7#9/	 4H 4
 4
0 0 %*I/IKa.bhj$k$k$k!"nn-FGG03FF 	-FH[,\)!-55j-Q_``bc./ &&z=.QQST 0	0   	W+.@@DTCVVN-~==r.   )FF)r$   r%   r&   r   r   r7   r(   rO   r   rP   r,   rM   rQ   rR   s   @r/   r   r   
  s        . .3 . . . . . . 05,1H> H>\H> <H> 	H>
 'tnH> $D>H> 
u|XeEL&9:HU5<EX<YY	ZH> H> H> H> H> H> H> H>r.   r   
similaritymatchability0matchability1c                    | j         \  }}}t          j                            |          t          j                            |                              dd          z   }t          j                            | d          }t          j                            |                     dd                                          d                              dd          }|                     ||dz   |dz   fd          }	||z   |z   |	ddd|d|f<   t          j                            |                    d                     |	dddddf<   t          j                            |                    d                     |	dddddf<   |	S )z;create the log assignment matrix from logits and similarityr   r   rB   rT   r   N)	rk   r   r   
logsigmoidr   log_softmaxr   new_fullsqueeze)
r   r   r   r   num_keypoints_0num_keypoints_1certaintiesscores0scores1scoress
             r/   sigmoid_log_double_softmaxr   ]  sw    4>3C0J-**=99BM<T<TUb<c<c<m<mnoqr<s<ssKm''
A66Gm''
(<(<R(D(D(O(O(Q(QSTUU__`bdfggG  *o.A?UVCV!WYZ[[F4;g4E4SF111 0 00111=3H3H3L3L2LMMF111crc2:11=3H3H3L3L2LMMF111b#2#:Mr.   c                   z     e Zd Zdef fdZdej        dej        dej        fdZdej        dej        fdZ xZ	S )LightGlueMatchAssignmentLayerr2   c                     t                                                       |j        | _        t          j        | j        | j        d          | _        t          j        | j        dd          | _        d S )NTr4   r   )r6   r7   r9   r   r8   final_projectionmatchabilityr<   s     r/   r7   z&LightGlueMatchAssignmentLayer.__init__m  sf    $3 "	$*=t?RY] ^ ^ ^Id&914HHHr.   r   r!   r@   c                 .   |j         \  }}}|                     |          }|t          j        | j        |j                  dz  z  }|                    |dz  d||          }|d d df         }|d d df         }||                    dd          z  }	||                    |dz  d|          }|d d df                             d          }
|d d df                             d                              dd          }|
|z  }|		                    |dk    t          j
        |	j                  j                  }	|                     |          }|                    |dz  d|d          }|d d df         }|d d df         }t          |	||          }|S )Ndeviceg      ?r   r   r   rB   rT   )rk   r   r(   tensorr9   r   rm   r   r`   masked_fillfinfor^   minr   r   )r=   r   r!   r   r   r9   m_descriptorsm_descriptors0m_descriptors1r   mask0mask1r   matchability_0matchability_1r   s                   r/   rM   z%LightGlueMatchAssignmentLayer.forwardt  s   4?4E1
M>--k::%T5HQ^Qe(f(f(fjn(nn%--jAoq-Q_``&qqq!t,&qqq!t,#n&>&>r2&F&FF
<<
aMBBDAJ((,,EAJ((,,66r2>>E5=D#//	5;zGW;X;X;\]]J ((55#++J!OQqQQ%aaad+%aaad+ ,JWWr.   c                     |                      |          }t          j                            |                              d          }|S )z0Get matchability of descriptors as a probabilityrB   )r   r   r   sigmoidr   )r=   r   r   s      r/   get_matchabilityz.LightGlueMatchAssignmentLayer.get_matchability  s>    ((55},,\::BB2FFr.   )
r$   r%   r&   r   r7   r(   rO   rM   r   rQ   rR   s   @r/   r   r   l  s        I I I I I I I5< u|     4EL U\        r.   r   c                   H     e Zd Zdef fdZdej        dej        fdZ xZS )LightGlueTokenConfidenceLayerr2   c                     t                                                       t          j        |j        d          | _        d S Nr   )r6   r7   r   r8   r9   tokenr<   s     r/   r7   z&LightGlueTokenConfidenceLayer.__init__  s3    Yv4a88


r.   r   r@   c                     |                      |                                          }t          j                            |                              d          }|S )NrB   )r   detachr   r   r   r   )r=   r   r   s      r/   rM   z%LightGlueTokenConfidenceLayer.forward  sG    

;--//00%%e,,44R88r.   r   rR   s   @r/   r   r     sj        9 9 9 9 9 9 9
5< EL        r.   r   c                   2    e Zd ZU dZeed<   dZdZdZdZ	dZ
dS )LightGluePreTrainedModelz
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    r2   	lightgluepixel_valuesFTN)r$   r%   r&   r'   r   r*   base_model_prefixmain_input_namesupports_gradient_checkpointing_supports_flash_attn_supports_sdpar-   r.   r/   r  r    sD          
 #$O&+#NNNr.   r  r   	thresholdc                 `   | j         \  }}}| ddddddf                             d          }| ddddddf                             d          }|j        }|j        }t          j        |j         d         |j                  d         }t          j        |j         d         |j                  d         }	||                    d|          k    }
|	|                    d|          k    }|j                                        }|	                    d          }t          j
        |
||          }t          j
        ||                    d|          |          }|
||k    z  }||                    d|          z  }t          j
        ||d          }t          j
        ||d          }t          j        ||g                              dd                              |dz  d          }t          j        ||g                              dd                              |dz  d          }||fS )z1obtain matches from a score matrix [Bx M+1 x N+1]NrB   r   r   r   r   )rk   maxindicesr(   aranger   gathervaluesexp
new_tensorwhererU   r   rm   )r   r  r   _max0max1matches0matches1indices0indices1mutual0mutual1zeromatching_scores0matching_scores1valid0valid1r   r   s                      r/   get_matches_from_scoresr#    s   |J1!!!SbS#2#+""1%%D!!!SbS#2#+""1%%D|H|H |HN1-hoFFFtLH|HN1-hoFFFtLH(//!X666G(//!X666G ;??D??1D{7D$77{7,<,C,CAx,P,PRVWW(945Fv}}Q111F {68R00H{68R00Hk8X.//99!Q??GG
UVXZ[[Gk#35E"FGGQQRSUVWW__`jmn`nprssOO##r.   r   heightwidthc                     t          j        ||g| j        | j                  d         }|dz  }|                    d          j        dz  }| |ddddf         z
  |d         z  } | S )a  
    Normalize keypoints locations based on image image_shape

    Args:
        keypoints (`torch.Tensor` of shape `(batch_size, num_keypoints, 2)`):
            Keypoints locations in (x, y) format.
        height (`int`):
            Image height.
        width (`int`):
            Image width.

    Returns:
        Normalized keypoints locations of shape (`torch.Tensor` of shape `(batch_size, num_keypoints, 2)`).
    r   r^   Nr   rB   .).NN)r(   r   r   r^   r  r  )r   r$  r%  sizeshiftscales         r/   normalize_keypointsr+    sr     <	0@	XXXY]^D1HEHHRLL!#EU3aaa<00E/4JJIr.   zV
    LightGlue model taking images as inputs and outputting the matching of them.
    c                   4    e Zd ZdZdef fdZdedefdZ	 d$de	j
        d	e	j
        d
ee         dee	j
        ee	j
        e	j
        f         f         fdZde	j
        dede	j
        de	j
        de	j
        f
dZd%dZde	j
        de	j
        dede	j
        fdZde	j
        d	e	j
        de	j
        de	j
        de	j
        de	j
        defdZd Zde	j
        de	j
        de	j
        de	j
        dee	j
        e	j
        f         f
dZ	 	 	 d&d	e	j
        de	j
        dededee	j
                 dee         d
ee         dee	j
        e	j
        e	j
        eef         fd Zee	 	 	 d&d!e	j        d"ee	j                 dee         d
ee         deeef         f
d#                        Z xZS )'LightGlueForKeypointMatchingar  
    LightGlue is a model matching keypoints in images by leveraging detections from a keypoint detector such as
    SuperPoint. It is based on the SuperGlue architecture and is designed to be lightweight and efficient.
    It consists of :
        1. Keypoint Encoder
        2. A Graph Neural Network with self and cross attention layers
        3. Matching Assignment layers

    The correspondence ids use -1 to indicate non-matching points.

    Philipp Lindenberger, Paul-Edouard Sarlin and Marc Pollefeys. LightGlue: Local Feature Matching at Light Speed.
    In ICCV 2023. https://huggingface.co/papers/2306.13643
    r2   c                 n   t                                                     t          j        j        j                  | _        j        j        | _        j	        | _	        j
        | _        j        | _        j        | _        j        | _        | j	        | j        k    r't          j        | j        | j	        d          | _        nt          j                    | _        t'                    | _        t          j        fdt-          j
                  D                       | _        t          j        fdt-          j
                  D                       | _        t          j        fdt-          j
        dz
            D                       | _        |                                  d S )N)trust_remote_codeTr4   c                 2    g | ]}t          |           S ))r   )r   ).0ir2   s     r/   
<listcomp>z9LightGlueForKeypointMatching.__init__.<locals>.<listcomp>  s'    eee&v;;;eeer.   c                 .    g | ]}t                    S r-   )r   r1  r  r2   s     r/   r3  z9LightGlueForKeypointMatching.__init__.<locals>.<listcomp>  s"    \\\q*622\\\r.   c                 .    g | ]}t                    S r-   )r   r5  s     r/   r3  z9LightGlueForKeypointMatching.__init__.<locals>.<listcomp>  s"    ```q*622```r.   r   )r6   r7   r   from_configkeypoint_detector_configr/  keypoint_detectordescriptor_decoder_dim keypoint_detector_descriptor_dimr9   num_hidden_layers
num_layersfilter_thresholddepth_confidencewidth_confidencer   r8   input_projectionIdentityr1   positional_encoder
ModuleListrangetransformer_layersmatch_assignment_layerstoken_confidence	post_initr<   s    `r/   r7   z%LightGlueForKeypointMatching.__init__  s      !>!J+v?W"
 "
 "
 170O0f-$3 2 & 7 & 7 & 7$"GGG$&Id.SUYUhos$t$t$tD!!$&KMMD!"<V"D"D"$-eeeeU6KcEdEdeee#
 #
 (*}\\\\E&BZ<[<[\\\(
 (
$ !#````E&BZ]^B^<_<_```!
 !
 	r.   layer_indexr@   c                 x    ddt          j        d|z  | j        z            z  z   }t          j        |dd          S )z-scaled confidence threshold for a given layerg?g?g      r   r   )npr  r=  clip)r=   rJ  r  s      r/   _get_confidence_thresholdz6LightGlueForKeypointMatching._get_confidence_threshold  s;    #tk'9DO'K L LLL	wy!Q'''r.   Fr   r   r?   c                     |                                                                 }|                     |          }|                     ||          }||fS )Nr?   )r  r   rA  rC  )r=   r   r   r?   projected_descriptorskeypoint_encoding_outputs         r/   _keypoint_processingz1LightGlueForKeypointMatching._keypoint_processing  s[     "((**5577 $ 5 5k B B#'#:#:9[o#:#p#p $&>>>r.   keypoint_confidencesr!   
num_pointsc                    |j         \  }}|| j        dz
  k     r|                    |dk    d          }|                    |dz  d          }|                     |          }d||k                                                         d          |z  z
  }|| j        k    }	n t          j	        |t          j
                  }	|	S )zRevaluate whether we should stop inference based on the confidence of the keypointsr   r   r   rB   g      ?rC   r]   )rk   r=  r   rm   rN  r_   sumr?  r(   onesrP   )
r=   rT  rJ  r!   rU  r   r  r  ratio_confidentearly_stopped_pairss
             r/   _get_early_stopped_image_pairsz;LightGlueForKeypointMatching._get_early_stopped_image_pairs'  s     

A1,,, $8#C#CDAIq#Q#Q #7#?#?
aQS#T#T 66{CCI!%9I%E$L$L$N$N$R$RWX$R$Y$Y\f$ffO"1D4I"I #(*Zuz"J"J"J""r.   Nc                     |||         }||         } | j         |         ||          }t          || j                  \  }}||fS r   )rG  r#  r>  )r=   r   r!   rJ  early_stopsr   r   r   s           r/   _get_keypoint_matchingz3LightGlueForKeypointMatching._get_keypoint_matching:  sW    "%k2K$D:-k:;MM#:64CX#Y#Y ''r.   confidencesr   c                 ^    |d| j         z
  k    }||||                     |          k    z  }|S )z#mask points which should be removedr   )r@  rN  )r=   r_  r   rJ  keeps        r/   _get_pruning_maskz.LightGlueForKeypointMatching._get_pruning_maskB  s<    T223"K4#A#A+#N#NNNDr.   r  prune_outputc                    |j         \  }}	}	| j        |                             |          }
|                     ||
|                              |dk    t          j        d                    fd||d         |d         |fD             \  }}}}}t          |          D ]}||||         fxx         dz  cc<   d ||||fD             \  }}}}||f}t          |dd          }|||||fS )	z
        For a given layer, prune keypoints based on the confidence of the keypoints and the matchability of the
        descriptors.
        r   Fc              3   L   K   | ]}d  t          |          D             V  dS )c                 $    g | ]\  }}||         S r-   r-   )r1  tr!   s      r/   r3  zULightGlueForKeypointMatching._do_layer_keypoint_pruning.<locals>.<genexpr>.<listcomp>^  s     GGGDQtWGGGr.   N)zip)r1  r   pruned_keypoints_masks     r/   	<genexpr>zJLightGlueForKeypointMatching._do_layer_keypoint_pruning.<locals>.<genexpr>]  sY       c
 c
 HGC0E$F$FGGGc
 c
 c
 c
 c
 c
r.   r   c              3   8   K   | ]}t          |d           V  dS )T)batch_firstNr   )r1  pruned_tensors     r/   rj  zJLightGlueForKeypointMatching._do_layer_keypoint_pruning.<locals>.<genexpr>e  sK       S
 S
 D999S
 S
 S
 S
 S
 S
r.   TrB   rl  padding_value)	rk   rG  r   rb  r   r(   r   rE  r	   )r=   r   r   r!   r  rc  rT  rJ  r   r  descriptors_matchabilitypruned_descriptorspruned_keypoints_0pruned_keypoints_1pruned_maskpruned_indicesr2  pruned_keypointsri  s                     @r/   _do_layer_keypoint_pruningz7LightGlueForKeypointMatching._do_layer_keypoint_pruningI  sw    ',
Aq#'#?#L#]#]^i#j#j  $ 6 67KMegr s s 5 A A$!)U\Z_M`M` a ac
 c
 c
 c
&	!ilDY[bcc
 c
 c
_.0BKQ_ z"" 	4 	4AN1--...!3....S
 S
"46HJ\^i!jS
 S
 S
O.0BK /0BC%n$VXYYY!#3^[R^^^r.   c                    t          j                  t          j        j        d                   }|         }|         d ||fD             \  }}d ||fD             \  }}fd||||fD             \  }}}}||||fS )Nr   c              3   :   K   | ]}t          |d d          V  dS )TrB   rn  Nr   r1  r   s     r/   rj  zMLightGlueForKeypointMatching._concat_early_stopped_outputs.<locals>.<genexpr>{  sE       3
 3
 TDDD3
 3
 3
 3
 3
 3
r.   c              3   :   K   | ]}t          |d d          V  dS )Tr   rn  Nr   rz  s     r/   rj  zMLightGlueForKeypointMatching._concat_early_stopped_outputs.<locals>.<genexpr>  sE       >
 >
 TCCC>
 >
 >
 >
 >
 >
r.   c              3   (   K   | ]}|         V  d S r   r-   )r1  r   early_stops_indicess     r/   rj  zMLightGlueForKeypointMatching._concat_early_stopped_outputs.<locals>.<genexpr>  sE       g
 g
 &'g
 g
 g
 g
 g
 g
r.   )r(   rU   r  rk   )r=   r}  final_pruned_keypoints_indices!final_pruned_keypoints_iterationsr   r   idsorder_indicess    `      r/   _concat_early_stopped_outputsz:LightGlueForKeypointMatching._concat_early_stopped_outputsn  s     $k*=>>l.4Q788+C01-@3
 3
"$BC3
 3
 3
//>
 >
*,MN>
 >
 >
::g
 g
 g
 g
 .1	g
 g
 g
c"@Bc ./PRY[jjjr.   r   r   r   c                 r   |j         \  }fd|||fD             \  }}}|d d df         }|d d df         }|d d df         }|d d df         }	|d d df         }
|d d df         }t          j        dz  d|fd|j        |j                  }t          j        dz  d|f|j        |j                  }t          dz            D ]}t          j        ||         dk    d||                             d||         	                    d                              ||d||         f<   t          j        |	|         dk    d||                             d|	|         	                    d                              ||d||         f<   |
|         ||d||         f<   ||         ||d||         f<   ||fS )Nc              3   L   K   | ]}|                     d z  d d          V  dS )r   rB   N)rm   )r1  r   r   s     r/   rj  zJLightGlueForKeypointMatching._do_final_keypoint_pruning.<locals>.<genexpr>  sH       -
 -
7=FNN:?Ar22-
 -
 -
 -
 -
 -
r.   r   r   r   rB   r'  )r   )
rk   r(   fullr   r^   zerosrE  r  r  clamp)r=   r  r   r   r   r  r  r  r  r  r  r   _matches_matching_scoresr2  r   s                  @r/   _do_final_keypoint_pruningz7LightGlueForKeypointMatching._do_final_keypoint_pruning  s+     
A-
 -
 -
 -
BI7TcAd-
 -
 -
)/ 111a4=111a4=111a4=111a4=*111a40*111a40 :zQ=A2gndkdqrrr ;1_a/oNc
 
 
 zQ'' 	F 	FA*/+r!2x{'9'9!Xa[=N=NST=N=U=U'V'V+ +HQ8A;&' +0+r!2x{'9'9!Xa[=N=NST=N=U=U'V'V+ +HQ8A;&' 3C12EQ8A;./2B12EQ8A;.//)))r.   r$  r%  r   c           
      	  ( |rdnd }|rdnd }	|j         d         dk    r\|j         d d         }
|                    |
dt          j                  |                    |
          |                    |
          ||	fS |j        }|j         \  }}}}t          j        |                    |d          d          }|                    |dz  |d          }||                    |dz  |          nd }|                    |dz  || j                  }t          j	        |dz  |          }t          |||          }|                     |||	          \  }}|d         }| j        dk    }| j        dk    }g }g }g }g }g }t          j	        d||                              |dz  d          }t          j        |          }t!          | j                  D ]}|                                }||                     ||          }n#t          j        ||d
         f|j                  } | j        |         |||||          }|\  }}} |r||z   }|r|	| z   }	|r|| j        dz
  k     r0 | j        |         |          }!|                     |!|||          }"n t          j        |t          j                  }"t          j        |"          rT|"                    d          (|(         }#|                     |||(          \  }$}%|                    t;          |#                     |                    t;          |$                     |                    t;          |%                     |rP|                    t;          |(                              |                    t;          |(                              ||"          }t=          (fd||d         |d         ||fD                       \  }}&}'}}|&|'f}|r"t=          (fd|||!fD                       \  }}}!t          j        |"          r n%|r!|                      ||||||!|          \  }}}}}|r<|r:| !                    |||||          \  }}}}| "                    ||||          \  }}n>|                     ||| j        dz
            \  }}t          j        |          | j        z  }|                    |d|          }|||||	fS )Nr-   r   r   rB   r]   r   rC   r   rP  rT   )rx   r?   r   )rU  )r]  c              3   *   K   | ]}|          V  d S r   r-   r1  r   r]  s     r/   rj  zALightGlueForKeypointMatching._match_image_pair.<locals>.<genexpr>  sF       V V" |,V V V V V Vr.   c              3   *   K   | ]}|          V  d S r   r-   r  s     r/   rj  zALightGlueForKeypointMatching._match_image_pair.<locals>.<genexpr>#  sF       l l & #K<0l l l l l lr.   )#rk   r   r(   r   	new_zerosr   rW  rm   r;  r  r+  rS  r?  r@  rl   	ones_likerE  r=  r(  get_extended_attention_maskrX  rF  rH  r[  rP   anyrE   r^  extendlistr,   allrw  r  r  ))r=   r   r   r$  r%  r!   r   r?   r   r   rk   r   r   r  initial_num_keypointsnum_points_per_pairimage_indicesrR  do_early_stopdo_keypoint_pruningr}  r   r   r~  r  pruned_keypoints_indicespruned_keypoints_iterationsrJ  r   extended_attention_masklayer_outputr"   	attentionrT  rZ  early_stopped_image_indicesearly_stopped_matchesearly_stopped_matching_scoreskeypoints_0
keypoint_1r]  s)                                           @r/   _match_image_pairz.LightGlueForKeypointMatching._match_image_pair  sz    #7@BBD0:d?1""OCRC(E""5"EI">>##E**##E**!  !2;//
A,a#iZ(D(D!LLL%%j1n6KQOO	FJFVt||JN,ABBB\`!))*q.:OQUQvwwZ!^FCCC'	65AA	040I0I9M 1J 1
 1
-- -Q/	 -1 #3a7 )+&,.)#(<3HQW#X#X#X#_#_`jmn`npr#s#s &+o6N&O&O# 11 R	 R	K%**,,K*.*J*J4Q\*]*]''*/*j+b/5R[d[k*l*l*l'?42;?6%9"3  L 5A1K	# F$5$E!  <!/)!; 01!444+M4+@+Mk+Z+Z( +/*M*M,k4L_ +N + +''
 +0*Zuz*R*R*R'9011  #6"G"G"J"JK2?2L/KOKfKf#T;K Lg L LH)+H (..t4O/P/PQQQNN4(=#>#>???#**40M+N+NOOO* q6==dC[\gCh>i>ijjj9@@FabmFnAoAoppp +>?R>R*S'PU V V V V'2IaL)A,PTVc&dV V V Q QMKj$ "-j 9I* fk l l l l !9 ; 4+l l l g gc02MOc 9011 E"  33#!03,#  dY(@$Hc  	c0 	c 22'25#  h*,MwXg (,'F'F.%	( ($G__ (,'B'B;PTVZVehiVi'j'j$G_050P0PSWSb0b-,M,U,U0-
 -
)
 -
 	
r.   r  labelsc           
      l   d }|t          d          ||n| j        j        }||n| j        j        }|j        dk    s|                    d          dk    rt          d          |j        \  }}}}	}
|                    |dz  ||	|
          }|                     |          }|d d         \  }}}}|                    |ddd          	                    |          }|                    |dd| j
                  	                    |          }|                    |dd          }|                                }|d d d d d d df         |
z  |d d d d d d df<   |d d d d d d df         |	z  |d d d d d d df<   |                     |||	|
|||	          \  }}}}}t          ||||||||
          S )Nz9LightGlue is not trainable, no labels should be provided.   r   r   zOInput must be a 5D tensor of shape (batch_size, 2, num_channels, height, width)   rB   r   )r!   r   r?   )r   r   r   r   r    r!   r"   r#   )
ValueErrorr2   r   r?   ndimr(  rk   rm   r9  ra   r;  cloner  r   )r=   r  r  r   r?   r   r   r  channelsr$  r%  keypoint_detectionsr   r   r!   absolute_keypointsr   r   r    r"   r#   s                        r/   rM   z$LightGlueForKeypointMatching.forward`  s@    XYYY1B1N--TXT_Tq$8$D  $+Jj 	 !!\%6%6q%9%9Q%>%>nooo1=1C.
Ax#++JNHfeTT"44\BB*=bqb*A'	1k4%%j!R;;>>|LL	!))*aT=bccffgstt||J2..&__..);AAAqqq!!!QJ)G%)O111aaaA:&);AAAqqq!!!QJ)G&)P111aaaA:&EIE[E[/!5 F\ F
 F
B%
 /+'!	
 	
 	
 		
r.   rN   r   )NNN)r$   r%   r&   r'   r   r7   r   r_   rN  r(   rO   r   rP   r,   rS  r[  r^  rb  rw  r  r  r  r   r   r)   
LongTensorr   r   rM   rQ   rR   s   @r/   r-  r-    sw              @(S (U ( ( ( ( jo? ? <?49L?X`aeXf?	u|U5<#=>>	?? ? ? ?#$)L#?B#JO,#didp#	# # # #&( ( ( (U\ 5< ^a fkfr    #_\#_ <#_ l	#_
 #_ l#_ $l#_ #_ #_ #_ #_Jk k k@#*#* #* 	#*
 |#* 
u|U\)	*#* #* #* #*V (,,0/3k
 k
<k
 \k
 	k

 k
 u|$k
 $D>k
 'tnk
 
u|U\5<E	Fk
 k
 k
 k
Z  .2,0/33
 3
'3
 )*3
 $D>	3

 'tn3
 
u55	63
 3
 3
 ^ 3
 3
 3
 3
 3
r.   r-  r   )rs   )7dataclassesr   typingr   r   r   numpyrL  r(   r   torch.nn.utils.rnnr	   activationsr   modeling_flash_attention_utilsr   modeling_utilsr   r   processing_utilsr   utilsr   r   r   utils.deprecationr   utils.genericr   auto.modeling_autor   configuration_lightgluer   r   Moduler1   r[   rh   rO   r   rr   r_   r   r   r   r   r   r   r   r  r,   r#  r+  r-  __all__r-   r.   r/   <module>r     s  ( " ! ! ! ! ! , , , , , , , , , ,            + + + + + + ! ! ! ! ! ! B B B B B B F F F F F F F F & & & & & & D D D D D D D D D D 0 0 0 0 0 0 - - - - - - > > > > > > 4 4 4 4 4 4    :  :  :  :  :k  :  :   :F       "  < < < <<	UU\ 	U# 	U%, 	U 	U 	U 	U& % %I%<% 
% <	%
 U\*% % % '(% % % %4E) E) E) E) E) E) E) E)P    29   "P> P> P> P> P>	 P> P> P>f-2\JO,
\   & & & & &BI & & &R	 	 	 	 	BI 	 	 	        $EL $U $uU\[`[gMgGh $ $ $ $@5<  S U\    ,   
j
 j
 j
 j
 j
#; j
 j
 
j
Z &'E
Fr.   