
    %	&h"              
          d Z ddlZddlmZ ddlmZmZmZ ddl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 dd
lmZmZmZ ddlmZ  ej0                  e      ZdZdZdeej:                     deej:                     deej:                     fdZdej:                  dededej:                  fdZ dej:                  dej:                  dej:                  dedej:                  f
dZ!dej:                  dej:                  dedej:                  fdZ"d edej:                  fd!Z#e G d" d#e             Z$ G d$ d%e	jJ                        Z& G d& d'e	jJ                        Z' G d( d)e	jJ                        Z( G d* d+e	jJ                        Z)d,e(iZ* G d- d.e	jJ                        Z+ G d/ d0e	jJ                        Z, G d1 d2e	jJ                        Z- G d3 d4e	jJ                        Z. G d5 d6e      Z/d7Z0d8Z1 ed9e0       G d: d;e/             Z2d6d;gZ3y)<zPyTorch SuperGlue model.    N)	dataclass)OptionalTupleUnion)nn)PreTrainedModeladd_start_docstrings)SuperGlueConfig   ) find_pruneable_heads_and_indicesprune_linear_layer)ModelOutput%add_start_docstrings_to_model_forwardlogging   )AutoModelForKeypointDetectionr
   z%magic-leap-community/superglue_indoortensor_tuple0tensor_tuple1returnc           
          t        t        | |      D cg c]  \  }}t        j                  ||g       c}}      S c c}}w )a'  
    Concatenate two tuples of tensors pairwise

    Args:
        tensor_tuple0 (`Tuple[torch.Tensor]`):
            Tuple of tensors.
        tensor_tuple1 (`Tuple[torch.Tensor]`):
            Tuple of tensors.

    Returns:
        (`Tuple[torch.Tensor]`): Tuple of concatenated tensors.
    )tupleziptorchcat)r   r   tensor0tensor1s       /var/www/pru.catia.catastroantioquia-mas.com/valormas/lib/python3.12/site-packages/transformers/models/superglue/modeling_superglue.pyconcat_pairsr   %   s8     M[hHij4DGW%))Wg./jkkjs   !>
	keypointsheightwidthc                     t        j                  ||g| j                  | j                        d   }|dz  }|j	                  dd      j
                  dz  }| |dddddf   z
  |dddddf   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)`).
    )devicedtypeNr      T)keepdimgffffff?)r   tensorr#   r$   maxvalues)r   r    r!   sizecenterscalings         r   normalize_keypointsr-   5   sv     <<	0@0@	XY]^DAXFhhq$h'..4Gq$z**gaqj.AAA    log_cost_matrixlog_source_distributionlog_target_distributionnum_iterationsc                 r   t        j                  |      }t        j                  |      }t        |      D ]Z  }|t        j                  | |j	                  d      z   d      z
  }|t        j                  | |j	                  d      z   d      z
  }\ | |j	                  d      z   |j	                  d      z   S )az  
    Perform Sinkhorn Normalization in Log-space for stability

    Args:
        log_cost_matrix (`torch.Tensor` of shape `(batch_size, num_rows, num_columns)`):
            Logarithm of the cost matrix.
        log_source_distribution (`torch.Tensor` of shape `(batch_size, num_rows)`):
            Logarithm of the source distribution.
        log_target_distribution (`torch.Tensor` of shape `(batch_size, num_columns)`):
            Logarithm of the target distribution.

    Returns:
        log_cost_matrix (`torch.Tensor` of shape `(batch_size, num_rows, num_columns)`): Logarithm of the optimal
        transport matrix.
    r%   r   dim)r   
zeros_likerange	logsumexp	unsqueeze)r/   r0   r1   r2   log_u_scalinglog_v_scaling_s          r   log_sinkhorn_iterationsr=   J   s    * $$%<=M$$%<=M>" w/%///TaTkTklmTnBntu2vv/%///TaTkTklmTnBntu2vvw ]44Q77-:Q:QRS:TTTr.   scores	reg_param
iterationsc                    | j                   \  }}}| j                  d      }||z  j                  |       ||z  j                  |       }}|j                  ||d      }	|j                  |d|      }
|j                  |dd      }t	        j
                  t	        j
                  | |	gd      t	        j
                  |
|gd      gd      }||z   j                          }t	        j
                  |j                  |      |j                         d   |z   g      }t	        j
                  |j                  |      |j                         d   |z   g      }|d   j                  |d      |d   j                  |d      }}t        ||||      }||z
  }|S )a  
    Perform Differentiable Optimal Transport in Log-space for stability

    Args:
        scores: (`torch.Tensor` of shape `(batch_size, num_rows, num_columns)`):
            Cost matrix.
        reg_param: (`torch.Tensor` of shape `(batch_size, 1, 1)`):
            Regularization parameter.
        iterations: (`int`):
            Number of Sinkhorn iterations.

    Returns:
        log_optimal_transport_matrix: (`torch.Tensor` of shape `(batch_size, num_rows, num_columns)`): Logarithm of the
        optimal transport matrix.
    r%   N)r2   )shape
new_tensortoexpandr   r   logr=   )r>   r?   r@   
batch_sizenum_rowsnum_columns
one_tensornum_rows_tensornum_columns_tensorsource_reg_paramtarget_reg_param	couplingslog_normalizationr0   r1   log_optimal_transport_matrixs                   r   log_optimal_transportrS   g   s     )/%J+""1%J+3j+@*D*DV*L{]gOgNkNklrNs'O ''
Ha@ ''
A{C  Q2I		599f.>%?DeiiQaclPmoqFrsuvwI),>>CCEE#ii		!	!(	+-?-C-C-Ed-KN_-_` $ii		!	!+	.0C0C0Ed0KN_0_` 	 %,,Z<%,,Z< 5
 $;*,CT^$  $@BS#S ''r.   r5   c                 b    | j                  | j                  |         j                  d      dz
  S Nr   r%   )new_onesrC   cumsum)xr5   s     r   arange_likerY      s)    ::aggcl#**1-11r.   c                   6   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ej                        ed<   dZeeej                        ed	<   y)
KeypointMatchingOutputa,  
    Base class for outputs of 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 and matching_scores tensors are keypoint matching
    information.

    Args:
        loss (`torch.FloatTensor` of shape `(1,)`, *optional*):
            Loss computed during training.
        mask (`torch.IntTensor` of shape `(batch_size, num_keypoints)`):
            Mask indicating which values in matches and matching_scores are keypoint matching information.
        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.
        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_scoresr   maskhidden_states
attentions)__name__
__module____qualname____doc__r\   r   r   FloatTensor__annotations__r]   r^   r   r_   	IntTensorr`   r   ra    r.   r   r[   r[      s    6 )-D(5$$
%,+/GXe''(/37OXe//07-1Ix))*1&*D(5??
#*8<M8E%"3"345<59Ju00129r.   r[   c                   h     e Zd Zdedededdf fdZdej                  dej                  fdZ xZ	S )	SuperGlueMultiLayerPerceptronconfigin_channelsout_channelsr   Nc                     t         |           t        j                  ||      | _        t        j
                  |      | _        t        j                         | _        y N)	super__init__r   LinearlinearBatchNorm1d
batch_normReLU
activation)selfrl   rm   rn   	__class__s       r   rr   z&SuperGlueMultiLayerPerceptron.__init__   s=    ii\:..6'')r.   hidden_statec                     | j                  |      }|j                  dd      }| j                  |      }|j                  dd      }| j                  |      }|S )NrB   )rt   	transposerv   rx   )ry   r{   s     r   forwardz%SuperGlueMultiLayerPerceptron.forward   sV    {{<0#--b"5|4#--b"5|4r.   )
rb   rc   rd   r
   intrr   r   Tensorr   __classcell__rz   s   @r   rk   rk      sA    $ $S $PS $X\ $ELL U\\ r.   rk   c                        e Zd Zdeddf fdZ	 d	dej                  dej                  dee   de	ej                  ee	ej                        f   fdZ
 xZS )
SuperGlueKeypointEncoderrl   r   Nc           	      l   t         |           |j                  }|j                  }dg|z   |gz   }t	        dt        |      dz
        D cg c]  }t        |||dz
     ||          }}|j                  t        j                  |d   |d                t        j                  |      | _        y c c}w )Nr   r%   r}   rB   )rq   rr   keypoint_encoder_sizeshidden_sizer7   lenrk   appendr   rs   
ModuleListencoder)ry   rl   layer_sizesr   encoder_channelsilayersrz   s          r   rr   z!SuperGlueKeypointEncoder.__init__   s    33((3,}< 1c"23a78
 *&2B1q52IK[\]K^_
 
 	bii 0 46Fr6JKL}}V,
s   B1r   r>   output_hidden_statesc                     |j                  d      }t        j                  ||gd      }|rdnd }| j                  D ]  } ||      }|s||fz   } ||fS )Nr   r4   ri   )r9   r   r   r   )ry   r   r>   r   r{   all_hidden_stateslayers          r   r   z SuperGlueKeypointEncoder.forward   sp     !!!$yy)V!4!<"6BD\\ 	HE .L#$5$G!	H ...r.   )Frb   rc   rd   r
   rr   r   r   r   boolr   r   r   r   s   @r   r   r      sn    - -4 -$ 05	/<</ / 'tn	/
 
u||XeELL&9::	;/r.   r   c                   P    e Zd Zd fd	Zdej
                  dej
                  fdZ	 	 	 	 	 	 ddej
                  deej                     deej                     deej                     d	eej                     d
ee	e	ej                           dee
   de	ej
                     fdZ xZS )SuperGlueSelfAttentionc                    t         |           |j                  |j                  z  dk7  r2t	        |d      s&t        d|j                   d|j                   d      |j                  | _        t        |j                  |j                  z        | _        | j                  | j                  z  | _        t        j                  |j                  | j                        | _        t        j                  |j                  | j                        | _        t        j                  |j                  | j                        | _        t        j                  |j                        | _        |xs t#        |dd      | _        | j$                  dk(  s| j$                  d	k(  rF|j&                  | _        t        j(                  d
|j&                  z  dz
  | j                        | _        |j,                  | _        y )Nr   embedding_sizezThe hidden size (z6) is not a multiple of the number of attention heads ()position_embedding_typeabsoluterelative_keyrelative_key_queryr   r%   )rq   rr   r   num_attention_headshasattr
ValueErrorr   attention_head_sizeall_head_sizer   rs   querykeyvalueDropoutattention_probs_dropout_probdropoutgetattrr   max_position_embeddings	Embeddingdistance_embedding
is_decoderry   rl   r   rz   s      r   rr   zSuperGlueSelfAttention.__init__   s    : ::a?PVXhHi#F$6$6#7 8 445Q8 
 $*#=#= #&v'9'9F<V<V'V#W !558P8PPYYv1143E3EF
99V//1C1CDYYv1143E3EF
zz&"E"EF'> (
'-zC
$ ''>9T=Y=Y]q=q+1+I+ID(&(ll1v7U7U3UXY3Y[_[s[s&tD# ++r.   rX   r   c                     |j                         d d | j                  | j                  fz   }|j                  |      }|j	                  dddd      S )NrB   r   r   r%   r   )r*   r   r   viewpermute)ry   rX   new_x_shapes      r   transpose_for_scoresz+SuperGlueSelfAttention.transpose_for_scores	  sL    ffhsmt'?'?AYAY&ZZFF;yyAq!$$r.   r`   attention_mask	head_maskencoder_hidden_statesencoder_attention_maskpast_key_valueoutput_attentionsc                 $   | j                  |      }|d u}	|	r||d   }
|d   }|}n |	rC| j                  | j                  |            }
| j                  | j                  |            }|}n|y| j                  | j                  |            }
| j                  | j                  |            }t	        j
                  |d   |
gd      }
t	        j
                  |d   |gd      }n@| j                  | j                  |            }
| j                  | j                  |            }| j                  |      }|d u}| j                  r|
|f}t	        j                  ||
j                  dd            }| j                  dk(  s| j                  dk(  r|j                  d   |
j                  d   }}|rDt	        j                  |dz
  t        j                  |j                  	      j                  dd      }n@t	        j                  |t        j                  |j                  	      j                  dd      }t	        j                  |t        j                  |j                  	      j                  dd      }||z
  }| j!                  || j"                  z   dz
        }|j%                  |j&                  
      }| j                  dk(  rt	        j(                  d||      }||z   }nE| j                  dk(  r6t	        j(                  d||      }t	        j(                  d|
|      }||z   |z   }|t+        j,                  | j.                        z  }|||z   }t0        j2                  j5                  |d      }| j7                  |      }|||z  }t	        j                  ||      }|j9                  dddd      j;                         }|j=                         d d | j>                  fz   }|j                  |      }|r||fn|f}| j                  r||fz   }|S )Nr   r%   r   r4   rB   r}   r   r   )r$   r#   r$   zbhld,lrd->bhlrzbhrd,lrd->bhlrr   ) r   r   r   r   r   r   r   matmulr~   r   rC   r'   longr#   r   aranger   r   rE   r$   einsummathsqrtr   r   
functionalsoftmaxr   r   
contiguousr*   r   )ry   r`   r   r   r   r   r   r   mixed_query_layeris_cross_attention	key_layervalue_layerquery_layer	use_cacheattention_scoresquery_length
key_lengthposition_ids_lposition_ids_rdistancepositional_embeddingrelative_position_scoresrelative_position_scores_queryrelative_position_scores_keyattention_probscontext_layernew_context_layer_shapeoutputss                               r   r   zSuperGlueSelfAttention.forward  s    !JJ}5
 3$>."<&q)I(+K3N11$((;P2QRI33DJJ?T4UVK3N'11$((=2IJI33DJJ}4MNK		>!#4i"@aHI))^A%6$D!LK11$((=2IJI33DJJ}4MNK//0AB"$.	?? (5N !<<Y5H5HR5PQ''>9T=Y=Y]q=q'2'8'8';Y__Q=O*L!&j1nEJJWdWkWk!l!q!q" "'l%**UbUiUi!j!o!oprtu!v"\\*EJJ}OcOcdiijkmopN%6H#'#:#:8dFbFb;bef;f#g #7#:#:ARAR#:#S ++~=+0<<8H+Wk+l(#36N#N --1EE16>NP[]q1r./4||<LiYm/n,#36T#TWs#s +dii8P8P.QQ%/.@ --//0@b/I ,,7  -	9O_kB%--aAq9DDF"/"4"4"6s";t?Q?Q>S"S%**+BC6G=/2mM]?? 11Gr.   rp   NNNNNF)rb   rc   rd   rr   r   r   r   r   rf   r   r   r   r   r   s   @r   r   r      s    ,4%ell %u|| % 7;15=A>BDH,1c||c !!2!23c E--.	c
  ((9(9:c !)):): ;c !uU->->'?!@Ac $D>c 
u||	cr.   r   c                   \     e Zd Zdef fdZdej                  dej                  fdZ xZS )SuperGlueSelfOutputrl   c                     t         |           t        j                  |j                  |j                        | _        y rp   )rq   rr   r   rs   r   dense)ry   rl   rz   s     r   rr   zSuperGlueSelfOutput.__init__u  s,    YYv1163E3EF
r.   r`   r   c                 (    | j                  |      }|S rp   )r   )ry   r`   argss      r   r   zSuperGlueSelfOutput.forwardy  s    

=1r.   	rb   rc   rd   r
   rr   r   r   r   r   r   s   @r   r   r   t  s,    G GU\\ U\\ r.   r   eagerc                       e Zd Zd fd	Zd Z	 	 	 	 	 	 ddej                  deej                     deej                     deej                     deej                     dee	e	ej                           d	ee
   d
e	ej                     fdZ xZS )SuperGlueAttentionc                     t         |           t        |j                     ||      | _        t        |      | _        t               | _        y )N)r   )	rq   rr    SUPERGLUE_SELF_ATTENTION_CLASSES_attn_implementationry   r   outputsetpruned_headsr   s      r   rr   zSuperGlueAttention.__init__  sC    4V5P5PQ,C
	 *&1Er.   c                 >   t        |      dk(  ry t        || j                  j                  | j                  j                  | j
                        \  }}t        | j                  j                  |      | j                  _        t        | j                  j                  |      | j                  _        t        | j                  j                  |      | j                  _	        t        | j                  j                  |d      | j                  _        | j                  j                  t        |      z
  | j                  _        | j                  j                  | j                  j                  z  | j                  _        | j
                  j                  |      | _        y )Nr   r%   r4   )r   r   ry   r   r   r   r   r   r   r   r   r   r   union)ry   headsindexs      r   prune_headszSuperGlueAttention.prune_heads  s   u:?749900$))2O2OQUQbQb
u
 -TYY__eD		*499==%@		,TYY__eD		.t{{/@/@%QO )-		(E(EE
(R		%"&))"?"?$))B_B_"_		 --33E:r.   r`   r   r   r   r   r   r   r   c           	      p    | j                  |||||||      }| j                  |d   |      }	|	f|dd  z   }
|
S rU   )ry   r   )ry   r`   r   r   r   r   r   r   self_outputsattention_outputr   s              r   r   zSuperGlueAttention.forward  sW     yy!"
  ;;|AF#%QR(88r.   rp   r   )rb   rc   rd   rr   r   r   r   r   rf   r   r   r   r   r   s   @r   r   r     s    ";* 7;15=A>BDH,1|| !!2!23 E--.	
  ((9(9: !)):): ; !uU->->'?!@A $D> 
u||	r.   r   c                       e Zd Zdeddf fdZ	 	 	 	 	 ddej                  deej                     deej                     deej                     d	ed
ede	ej                  ee	ej                        ee	ej                        f   fdZ
 xZS )SuperGlueAttentionalPropagationrl   r   Nc           	      v   t         |           |j                  }t        |      | _        |dz  |dz  |g}t        dt        |      dz
        D cg c]  }t        |||dz
     ||          }}|j                  t        j                  |d   |d                t        j                  |      | _        y c c}w )Nr   r%   r}   rB   )rq   rr   r   r   	attentionr7   r   rk   r   r   rs   r   mlp)ry   rl   r   mlp_channelsr   r   rz   s         r   rr   z(SuperGlueAttentionalPropagation.__init__  s    ((+F3#aq+F 1c,/!34
 *&,q1u2E|TUW
 
 	biiR 0,r2BCD==(
s   B6descriptorsr   r   r   r   r   c                     | j                  |||||      }|d   }|dd  }	t        j                  ||gd      }
|rdnd }| j                  D ]  } ||
      }
|s||
fz   } |
||	fS )N)r   r   r   r   r   r%   r   r4   ri   )r   r   r   r   )ry   r   r   r   r   r   r   attention_outputsr   r   r{   r   r   s                r   r   z'SuperGlueAttentionalPropagation.forward  s     !NN)"7#9/ + 
 #1%%ab)	yy+v!6A>"6BDXX 	HE .L#$5$G!	H
 .	99r.   )NNNFFr   r   s   @r   r   r     s    
) 
)4 
) 268<9="'%*:\\: !.:  (5	:
 !) 6:  : #: 
u||XeELL&9:HU5<<EX<YY	Z:r.   r   c                        e Zd Zdeddf fdZ	 	 	 d
dej                  deej                     dedee   de	ej                  ee	   ee	   f   f
d	Z
 xZS )SuperGlueAttentionalGNNrl   r   Nc                 
   t         |           |j                  | _        |j                  | _        t        j                  t        t        | j                              D cg c]  }t        |       c}      | _
        y c c}w rp   )rq   rr   r   gnn_layers_typeslayers_typesr   r   r7   r   r   r   )ry   rl   r<   rz   s      r   rr   z SuperGlueAttentionalGNN.__init__  sb    !--"33mmV[\_`d`q`q\rVs$tQR%DV%L$tu$ts   !B r   r_   r   r   c           	         |rdnd }|rdnd }|j                   \  }}}	|r||fz   }t        | j                  | j                        D ]  \  }
}d }d }|dk(  r|j	                  dd|| j
                        j                  d      j	                  ||| j
                        }|6|j	                  dddd|      j                  d      j	                  |dd|      nd } |
||||||      }|d   }|r||d   z   }|r||d   z   }||z   } |||fS )Nri   crossrB   r   r%   )r   r   r   r   r   r   )rC   r   r   r  reshaper   flip)ry   r   r_   r   r   r   all_attentionsrH   num_keypointsr<   	gnn_layer
layer_typer   r   gnn_outputsdeltas                   r   r   zSuperGlueAttentionalGNN.forward  sY    #7BD0d'2'8'8$
M1 1[N B%(d6G6G%H 	.!Iz$(!%)"W$''A}d>N>NOT!WWZ8H8HI & ' LLQ1m<AA!DLLZYZ\]_lm ' $#&;'=%9"3K  NE#$5A$F! !/+a.!@%-K=	.> -~==r.   )NFFr   r   s   @r   r   r     s    v v4 v (,"'/4->\\-> u||$->  	->
 'tn-> 
u||Xe_huo=	>->r.   r   c                   `     e Zd Zdeddf fdZdej                  dej                  fdZ xZS )SuperGlueFinalProjectionrl   r   Nc                 t    t         |           |j                  }t        j                  ||d      | _        y )NT)bias)rq   rr   r   r   rs   
final_proj)ry   rl   r   rz   s      r   rr   z!SuperGlueFinalProjection.__init__  s-    (())K4Hr.   r   c                 $    | j                  |      S rp   )r  )ry   r   s     r   r   z SuperGlueFinalProjection.forward  s    {++r.   r   r   s   @r   r  r    s4    I I4 I
,5<< ,ELL ,r.   r  c                   @    e Zd ZdZeZdZdZdej                  ddfdZ
y)SuperGluePreTrainedModelz
    An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained
    models.
    	supergluepixel_valuesmoduler   Nc                    t        |t        j                  t        j                  t        j                  f      rm|j
                  j                  j                  d| j                  j                         |j                  %|j                  j                  j                          yyt        |t        j                        rJ|j                  j                  j                          |j
                  j                  j                  d       yt        |t              r5t        j                  j!                  |j"                  j                  d       yy)zInitialize the weightsg        )meanstdN      ?)
isinstancer   rs   Conv2dConv1dweightdatanormal_rl   initializer_ranger  zero_	LayerNormfill_rk   init	constant_rt   )ry   r  s     r   _init_weightsz&SuperGluePreTrainedModel._init_weights-  s    fryy"))RYY?@ MM&&CT[[5R5R&S{{&  &&( '-KK""$MM$$S) =>GGfmm00#6 ?r.   )rb   rc   rd   re   r
   config_classbase_model_prefixmain_input_namer   Moduler'  ri   r.   r   r  r  #  s0    
 #L#$O7BII 7$ 7r.   r  aO  
    This model is 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 ([`SuperGlueConfig`]): 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:
        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):
            Pixel values. Pixel values can be obtained using [`SuperGlueImageProcessor`]. See
            [`SuperGlueImageProcessor.__call__`] for details.
        output_attentions (`bool`, *optional*):
            Whether or not to return the attentions tensors. 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.
zLSuperGlue model taking images as inputs and outputting the matching of them.c                       e Zd ZdZdeddf fdZ	 	 	 ddej                  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ef   fdZ 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 )SuperGlueForKeypointMatchinga  SuperGlue feature matching middle-end

    Given two sets of keypoints and locations, we determine the
    correspondences by:
      1. Keypoint Encoding (normalization + visual feature and location fusion)
      2. Graph Neural Network with multiple self and cross-attention layers
      3. Final projection layer
      4. Optimal Transport Layer (a differentiable Hungarian matching algorithm)
      5. Thresholding matrix based on mutual exclusivity and a match_threshold

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

    Paul-Edouard Sarlin, Daniel DeTone, Tomasz Malisiewicz, and Andrew
    Rabinovich. SuperGlue: Learning Feature Matching with Graph Neural
    Networks. In CVPR, 2020. https://arxiv.org/abs/1911.11763
    rl   r   Nc                 t   t         |   |       t        j                  |j                        | _        t        |      | _        t        |      | _	        t        |      | _        t        j                  j                  t        j                  d            }| j!                  d|       | j#                          y )Nr  	bin_score)rq   rr   r   from_configkeypoint_detector_configkeypoint_detectorr   keypoint_encoderr   gnnr  final_projectionr   r   	Parameterr'   register_parameter	post_init)ry   rl   r/  rz   s      r   rr   z%SuperGlueForKeypointMatching.__init__l  s     !>!J!J6KjKj!k 8 @*62 8 @HH&&u||C'89	Y7r.   r   r   r>   r    r!   r_   r   r   c	                 D	  &' |rdnd}	|rdnd}
|j                   d   dk(  rD|j                   dd }|j                  |dt        j                        |j	                  |      |	|
fS |j                   \  &}'}|j                  &dz  'd      }|j                  &dz  '| j                  j                        }|j                  &dz  '      }||j                  &dz  '      nd}t        |||      }| j                  |||      }|d   }||z   }|#|j                         }| j                  ||      }n#t        j                  &'f|j                        }| j                  ||||	      }|d   }| j                  |      }|j                  &d'| j                  j                        }|dddf   }|ddd
f   }||j!                  d
d      z  }|| j                  j                  dz  z  }|Q|j                  &d'      }|dddf   j#                  d      j%                  dd'      }|j'                  |dk(  d      }t)        || j*                  | j                  j,                        }|ddddddf   j/                  d      }|ddddddf   j/                  d
      }|j0                  }|j0                  }t3        |d
      d   |j5                  d
|      k(  }t3        |d
      d   |j5                  d
|      k(  }|j7                  d      }t        j8                  ||j:                  j=                         |      }t        j8                  || j                  j>                  kD  ||      }t        j8                  ||j5                  d
|      |      }|||kD  z  } || j5                  d
|      z  }!t        j8                  | ||j7                  d            }"t        j8                  |!||j7                  d            }#t        j@                  |"|#g      j                  &dd      }$t        j@                  ||g      j                  &dd      }%|r+|	|d
   z   }	|	|d
   z   }	|	|fz   }	tC        &'fd|	D              }	|r|
|d   z   }
tC        &'fd|
D              }
|$|%|	|
fS )a=  
        Perform keypoint matching between two images.

        Args:
            keypoints (`torch.Tensor` of shape `(batch_size, 2, num_keypoints, 2)`):
                Keypoints detected in the pair of image.
            descriptors (`torch.Tensor` of shape `(batch_size, 2, descriptor_dim, num_keypoints)`):
                Descriptors of the keypoints detected in the image pair.
            scores (`torch.Tensor` of shape `(batch_size, 2, num_keypoints)`):
                Confidence scores of the keypoints detected in the image pair.
            height (`int`): Image height.
            width (`int`): Image width.
            mask (`torch.Tensor` of shape `(batch_size, 2, num_keypoints)`, *optional*):
                Mask indicating which values in the keypoints, matches and matching_scores tensors are keypoint matching
                information.
            output_attentions (`bool`, *optional*):
                Whether or not to return the attentions tensors. Default to `config.output_attentions`.
            output_hidden_states (`bool`, *optional*):
                Whether or not to return the hidden states of all layers. Default to `config.output_hidden_states`.

        Returns:
            matches (`torch.Tensor` of shape `(batch_size, 2, num_keypoints)`):
                For each image pair, for each keypoint in image0, the index of the keypoint in image1 that was matched
                with. And for each keypoint in image1, the index of the keypoint in image0 that was matched with.
            matching_scores (`torch.Tensor` of shape `(batch_size, 2, num_keypoints)`):
                Scores of predicted matches for each image pair
            all_hidden_states (`tuple(torch.FloatTensor)`, *optional*):
                Tuple of `torch.FloatTensor` (one for the output of each stage) of shape `(1, 2, num_keypoints,
                num_channels)`.
            all_attentions (`tuple(torch.FloatTensor)`, *optional*):
                Tuple of `torch.FloatTensor` (one for each layer) of shape `(1, 2, num_heads, num_keypoints,
                num_keypoints)`.
        ri   Nr   r   rB   r   )r   )r#   )r_   r   r   r%   g      ?g    e)r@   c              3   f   K   | ](  }|j                  d d      j                  dd       * yw)r   rB   r}   N)r  r~   .0rX   rH   r  s     r   	<genexpr>zASuperGlueForKeypointMatching._match_image_pair.<locals>.<genexpr>  s2      &RS		*a;EEb"M&s   .1c              3   H   K   | ]  }|j                  d d        yw)r   rB   N)r  r;  s     r   r=  zASuperGlueForKeypointMatching._match_image_pair.<locals>.<genexpr>  s#     "vbc199ZB}#]"vs   ")"rC   new_fullr   r   	new_zerosr  rl   r   r-   r3  r*   get_extended_attention_maskonesr#   r4  r5  r~   r9   rF   masked_fillrS   r/  sinkhorn_iterationsr(   indicesrY   gatherrD   wherer)   expmatching_thresholdr   r   )(ry   r   r   r>   r    r!   r_   r   r   r   r  rC   r<   encoded_keypointslast_hidden_stateinput_shapeextended_attention_maskr
  projected_descriptorsfinal_descriptorsfinal_descriptors0final_descriptors1mask0max0max1indices0indices1mutual0mutual1zeromatching_scores0matching_scores1valid0valid1matches0matches1r]   r^   rH   r  s(                                         @@r   _match_image_pairz.SuperGlueForKeypointMatching._match_image_pairz  s   X #7BD0d??1"OOCR(E""5"EII">##E*!	  +4//'
A}a%%j1nmQG	!))*q.-I`I`a
Q>>B>Nt||JNM:TX (	65A	 11)VZn1o-a0 "$55%**,K&*&F&Ft[&Y#&+jj*m1LU^UeUe&f# hh(!5/	  
 "!n !% 5 5k B 299*aX\XcXcXoXop.q!t4.q!t4 $&8&B&B1a&HH$++11366<<
A}=DAJ((,33BMJE''
D9F 'vt~~$++JiJij a"crck"&&q)a"crck"&&q)<<<<h*40HOOAx4PPh*40HOOAx4PP  # ;;w0A4H ;;'7$++:X:X'XZjlpq ;;w0@0G0G80TVZ[,t346==H55;;vx1D1DR1HI;;vx1D1DR1HI))Xx0199*aL))%57G$HIQQR\^_acd 14Ea4H H 1KN B 15J4L L % &Wh& ! +k!n<N""vgu"vvN 	
 	
r.   r  labelsreturn_dictc           
         d}|t        d      ||n| j                  j                  }||n| j                  j                  }||n| j                  j                  }|j
                  dk7  s|j                  d      dk7  rt        d      |j                  \  }}}	}
}|j                  |dz  |	|
|      }| j                  |      }|dd \  }}}}|j                  |ddd      j                  |      }|j                  |dd      j                  |      }|j                  |dd| j                  j                        j                  |      }|j                  |dd      }|j                         }|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<   | j                  ||||
||||
      \  }}}}|st        d |||||||fD              S t        |||||||      S )a  
        Examples:

        ```python
        >>> from transformers import AutoImageProcessor, AutoModel
        >>> import torch
        >>> from PIL import Image
        >>> import requests

        >>> url = "https://github.com/magicleap/SuperGluePretrainedNetwork/blob/master/assets/phototourism_sample_images/london_bridge_78916675_4568141288.jpg?raw=true"
        >>> image1 = Image.open(requests.get(url, stream=True).raw)
        >>> url = "https://github.com/magicleap/SuperGluePretrainedNetwork/blob/master/assets/phototourism_sample_images/london_bridge_19481797_2295892421.jpg?raw=true"
        >>> image2 = Image.open(requests.get(url, stream=True).raw)
        >>> images = [image1, image2]

        >>> processor = AutoImageProcessor.from_pretrained("magic-leap-community/superglue_outdoor")
        >>> model = AutoModel.from_pretrained("magic-leap-community/superglue_outdoor")

        >>> with torch.no_grad():
        >>>     inputs = processor(images, return_tensors="pt")
        >>>     outputs = model(**inputs)
        ```Nz9SuperGlue 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   c              3   $   K   | ]  }|| 
 y wrp   ri   )r<  vs     r   r=  z7SuperGlueForKeypointMatching.forward.<locals>.<genexpr>Q  s      = s   )r\   r]   r^   r   r_   r`   ra   )r   rl   r   r   use_return_dictndimr*   rC   r  r2  rE   r   cloner`  r   r[   )ry   r  ra  r   r   rb  r\   rH   r<   channelsr    r!   keypoint_detectionsr   r>   r   r_   absolute_keypointsr]   r^   r`   ra   s                         r   r   z$SuperGlueForKeypointMatching.forward  sG   > XYY1B1N-TXT_T_TqTq$8$D $++JjJj 	 &1%<k$++B]B]!\%6%6q%9Q%>noo1=1C1C.
Ax#++JNHfeT"44\B/B2A/F,	6;%%j!R;>>|L	
Ar255lC!))*aT[[=T=TUXXYef||J2.&__.);Aq!QJ)G%)O1aA:&);Aq!QJ)G&)P1aA:&>B>T>T/!5 ?U 	?
;-  )T=Zde   &+'!
 	
r.   )NNN)NNNN)rb   rc   rd   re   r
   rr   r   r   r   r   r   r   r`  r   SUPERGLUE_INPUTS_DOCSTRINGrf   
LongTensorr   r[   r   r   r   s   @r   r-  r-  V  sT   
" 4 * (,,0/3O
<<O
 \\O
 	O

 O
 O
 u||$O
 $D>O
 'tnO
 
u||U\\5%7	8O
b ++EF .2,0/3&*S
''S
 ))*S
 $D>	S

 'tnS
 d^S
 
u,,	-S
 GS
r.   r-  )4re   r   dataclassesr   typingr   r   r   r   r   transformersr   r	   5transformers.models.superglue.configuration_supergluer
   pytorch_utilsr   r   utilsr   r   r   autor   
get_loggerrb   logger_CONFIG_FOR_DOC__CHECKPOINT_FOR_DOC_r   r   r   r-   r=   rS   rY   r[   r+  rk   r   r   r   r   r   r   r   r  r  SUPERGLUE_START_DOCSTRINGrn  r-  __all__ri   r.   r   <module>r}     sR     ! ) )   > Q Q P P 0 
		H	%$ > lell 3 lE%,,DW l\abgbnbn\o l B5<< B BS BU\\ B*U\\U"\\U #\\U 	U
 \\U:*(%,, *(5<< *(UX *(]b]i]i *(Z2 2 2 ":[ ": ":JBII  /ryy /@CRYY CL"))  #$  0 0f(:bii (:V4>bii 4>n,ryy ,7 72	   RE
#; E
	E
P &'E
Fr.   