
    fThE              
          S r SSKrSSKJr  SSKJrJrJr  SSKrSSKJ	r	  SSK
Jr  SSKJr  SS	KJrJr  SS
KJrJrJr  SSKJr  \R.                  " \5      rS\\R4                     S\\R4                     S\\R4                     4S jrS\R4                  S\S\S\R4                  4S jrS\R4                  S\R4                  S\R4                  S\S\R4                  4
S jrS\R4                  S\R4                  S\S\R4                  4S jrS\S\R4                  4S jr \ " S  S!\5      5       r! " S" S#\	RD                  5      r# " S$ S%\	RD                  5      r$ " S& S'\	RD                  5      r% " S( S)\	RD                  5      r&S*\%0r' " S+ S,\	RD                  5      r( " S- S.\	RD                  5      r) " S/ S0\	RD                  5      r* " S1 S2\	RD                  5      r+\ " S3 S4\5      5       r,\" S5S69 " S7 S8\,5      5       r-S4S8/r.g)9zPyTorch SuperGlue model.    N)	dataclass)OptionalTupleUnion)nn)PreTrainedModel)SuperGlueConfig   ) find_pruneable_heads_and_indicesprune_linear_layer)ModelOutputauto_docstringlogging   )AutoModelForKeypointDetectiontensor_tuple0tensor_tuple1returnc           
          [        [        X5       VVs/ s H  u  p#[        R                  " X#/5      PM     snn5      $ s  snnf )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       h/var/www/auris/envauris/lib/python3.13/site-packages/transformers/models/superglue/modeling_superglue.pyconcat_pairsr   "   s7     MHijHi4DG%))W./Hijkkjs   #?
	keypointsheightwidthc                     [         R                  " X!/U R                  U R                  S9S   nUS-  nUR	                  SSS9R
                  S-  nXSS2SSS24   -
  USS2SSS24   -  $ )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,   2   sr     <<	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                 l   [         R                  " U5      n[         R                  " U5      n[        U5       HU  nU[         R                  " XR	                  S5      -   SS9-
  nU[         R                  " XR	                  S5      -   SS9-
  nMW     XR	                  S5      -   UR	                  S5      -   $ )aJ  
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.   r/   r0   r1   log_u_scalinglog_v_scaling_s          r   log_sinkhorn_iterationsr<   G   s    * $$%<=M$$%<=M>"/%///TkTklmTnBntu2vv/%///TkTklmTnBntu2vv # 44Q77-:Q:QRS:TTTr-   scores	reg_param
iterationsc                 
   U R                   u  p4nU R                  S5      nXF-  R                  U 5      XV-  R                  U 5      pUR                  X4S5      n	UR                  USU5      n
UR                  USS5      n[        R
                  " [        R
                  " X	/S5      [        R
                  " X/S5      /S5      nXx-   R                  5       * n[        R
                  " UR                  U5      UR                  5       S   U-   /5      n[        R
                  " UR                  U5      UR                  5       S   U-   /5      nUS   R                  US5      US   R                  US5      p[        XXS9nX-
  nU$ )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)r1   )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_normalizationr/   r0   log_optimal_transport_matrixs                   r   log_optimal_transportrR   d   s     )/%J+""1%J+3+@*D*DV*L{OgNkNklrNs' ''
a@ ''
A{C  Q2I		599f%?DeiiQaPmoqFrsuvwI)>CCEE#ii		!	!(	+-?-C-C-Ed-KN_-_` $ii		!	!+	.0C0C0Ed0KN_0_` 	 %,,Z<%,,Z< 5
 $;,C$  $@#S ''r-   r4   c                 b    U R                  U R                  U   5      R                  S5      S-
  $ Nr   r$   )new_onesrB   cumsum)xr4   s     r   arange_likerX      s)    ::aggcl#**1-11r-   c                   >   \ rS rSr% SrSr\\R                     \	S'   Sr
\\R                     \	S'   Sr\\R                     \	S'   Sr\\R                     \	S'   Sr\\R                     \	S'   Sr\\\R                        \	S	'   Sr\\\R                        \	S
'   Srg)KeypointMatchingOutput   a  
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____firstlineno____doc__r\   r   r   FloatTensor__annotations__r]   r^   r   r_   	IntTensorr`   r   ra   __static_attributes__rb   r-   r   rZ   rZ      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-   rZ   c                   v   ^  \ rS rSrS\S\S\SS4U 4S jjrS\R                  S\R                  4S	 jr	S
r
U =r$ )SuperGlueMultiLayerPerceptron   configin_channelsout_channelsr   Nc                    > [         TU ]  5         [        R                  " X#5      U l        [        R
                  " U5      U l        [        R                  " 5       U l        g N)	super__init__r   LinearlinearBatchNorm1d
batch_normReLU
activation)selfro   rp   rq   	__class__s       r   ru   &SuperGlueMultiLayerPerceptron.__init__   s;    ii:..6'')r-   hidden_statec                     U R                  U5      nUR                  SS5      nU R                  U5      nUR                  SS5      nU R                  U5      nU$ )NrA   )rw   	transposery   r{   )r|   r   s     r   forward%SuperGlueMultiLayerPerceptron.forward   sV    {{<0#--b"5|4#--b"5|4r-   )r{   ry   rw   )rc   rd   re   rf   r	   intru   r   Tensorr   rk   __classcell__r}   s   @r   rm   rm      sF    $ $S $PS $X\ $ELL U\\  r-   rm   c                      ^  \ rS rSrS\SS4U 4S jjr SS\R                  S\R                  S\\	   S\
\R                  \\
\R                        4   4S	 jjrS
rU =r$ )SuperGlueKeypointEncoder   ro   r   Nc           	      r  > [         TU ]  5         UR                  nUR                  nS/U-   U/-   n[	        S[        U5      S-
  5       Vs/ s H  n[        XUS-
     XE   5      PM     nnUR                  [        R                  " US   US   5      5        [        R                  " U5      U l        g s  snf )Nr
   r$   r   rA   )rt   ru   keypoint_encoder_sizeshidden_sizer6   lenrm   appendr   rv   
ModuleListencoder)r|   ro   layer_sizesr   encoder_channelsilayersr}   s          r   ru   !SuperGlueKeypointEncoder.__init__   s    33((3,}< 1c"23a78
8 *&1q52IK[K^_8 	 
 	bii 0 46Fr6JKL}}V,
s   B4r   r=   output_hidden_statesc                     UR                  S5      n[        R                  " X/SS9nU(       a  SOS nU R                   H  nU" U5      nU(       d  M  XT4-   nM     XE4$ )Nr   r3   rb   )r8   r   r   r   )r|   r   r=   r   r   all_hidden_stateslayers          r   r    SuperGlueKeypointEncoder.forward   se     !!!$yy)!4!<"6BD\\E .L##$5$G! " ..r-   )r   )Frc   rd   re   rf   r	   ru   r   r   r   boolr   r   rk   r   r   s   @r   r   r      ss    - -4 -$ 05	/<</ / 'tn	/
 
u||XeELL&9::	;/ /r-   r   c                   b  ^  \ rS rSrSU 4S jjrS\R                  S\R                  4S jr      SS\R                  S\\R                     S\\R                     S	\\R                     S
\\R                     S\\
\
\R                           S\\   S\
\R                     4S jjrSrU =r$ )SuperGlueSelfAttention   c                   > [         TU ]  5         UR                  UR                  -  S:w  a7  [	        US5      (       d&  [        SUR                   SUR                   S35      eUR                  U l        [        UR                  UR                  -  5      U l        U R                  U R                  -  U l        [        R                  " UR                  U R                  5      U l        [        R                  " UR                  U R                  5      U l        [        R                  " UR                  U R                  5      U l        [        R                  " UR                  5      U l        U=(       d    [#        USS5      U l        U R$                  S:X  d  U R$                  S	:X  aG  UR&                  U l        [        R(                  " S
UR&                  -  S-
  U R                  5      U l        UR,                  U l        g )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$   )rt   ru   r   num_attention_headshasattr
ValueErrorr   attention_head_sizeall_head_sizer   rv   querykeyvalueDropoutattention_probs_dropout_probdropoutgetattrr   max_position_embeddings	Embeddingdistance_embedding
is_decoderr|   ro   r   r}   s      r   ru   SuperGlueSelfAttention.__init__   s    : ::a?PVXhHiHi#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-   rW   r   c                     UR                  5       S S U R                  U R                  4-   nUR                  U5      nUR	                  SSSS5      $ )NrA   r   r   r$   r
   )r)   r   r   viewpermute)r|   rW   new_x_shapes      r   transpose_for_scores+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                 V   U R                  U5      nUS Ln	U	(       a  Ub  US   n
US   nUnGOU	(       aC  U R                  U R                  U5      5      n
U R                  U R                  U5      5      nUnOUbu  U R                  U R                  U5      5      n
U R                  U R                  U5      5      n[        R
                  " US   U
/SS9n
[        R
                  " US   U/SS9nO@U R                  U R                  U5      5      n
U R                  U R                  U5      5      nU R                  U5      nUS LnU R                  (       a  X4n[        R                  " XR                  SS5      5      nU R                  S:X  d  U R                  S:X  Ga  UR                  S   U
R                  S   nnU(       aB  [        R                  " US-
  [        R                  UR                  S	9R                  SS5      nO>[        R                  " U[        R                  UR                  S	9R                  SS5      n[        R                  " U[        R                  UR                  S	9R                  SS5      nUU-
  nU R!                  UU R"                  -   S-
  5      nUR%                  UR&                  S
9nU R                  S:X  a  [        R(                  " SUU5      nUU-   nOHU R                  S:X  a8  [        R(                  " SUU5      n[        R(                  " SU
U5      nUU-   U-   nU[*        R,                  " U R.                  5      -  nUb  X-   n[0        R2                  R5                  USS9nU R7                  U5      nUb  UU-  n[        R                  " UU5      nUR9                  SSSS5      R;                  5       nUR=                  5       S S U R>                  4-   nUR                  U5      nU(       a  UU4OU4nU R                  (       a  UU4-   nU$ )Nr   r$   r   r3   rA   r   r   r   )r#   r"   r#   zbhld,lrd->bhlrzbhrd,lrd->bhlrr
   ) r   r   r   r   r   r   r   matmulr   r   rB   r&   longr"   r   aranger   r   rD   r#   einsummathsqrtr   r   
functionalsoftmaxr   r   
contiguousr)   r   )r|   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   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 !<<5H5H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-   )r   r   r   r   r   r   r   r   r   r   r   rs   NNNNNF)rc   rd   re   rf   ru   r   r   r   r   rh   r   r   r   rk   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 cr-   r   c                   j   ^  \ rS rSrS\4U 4S jjrS\R                  S\R                  4S jrSr	U =r
$ )SuperGlueSelfOutputiq  ro   c                    > [         TU ]  5         [        R                  " UR                  UR                  5      U l        g rs   )rt   ru   r   rv   r   dense)r|   ro   r}   s     r   ru   SuperGlueSelfOutput.__init__r  s,    YYv1163E3EF
r-   r`   r   c                 (    U R                  U5      nU$ rs   r   )r|   r`   argss      r   r   SuperGlueSelfOutput.forwardv  s    

=1r-   r   rc   rd   re   rf   r	   ru   r   r   r   rk   r   r   s   @r   r   r   q  s1    G GU\\ U\\  r-   r   eagerc                   .  ^  \ rS rSrSU 4S jjrS r      SS\R                  S\\R                     S\\R                     S\\R                     S\\R                     S	\\
\
\R                           S
\\   S\
\R                     4S jjrSrU =r$ )SuperGlueAttentioni  c                    > [         TU ]  5         [        UR                     " XS9U l        [        U5      U l        [        5       U l        g )N)r   )	rt   ru    SUPERGLUE_SELF_ATTENTION_CLASSES_attn_implementationr|   r   outputsetpruned_headsr   s      r   ru   SuperGlueAttention.__init__  s@    4V5P5PQ
	 *&1Er-   c                 6   [        U5      S:X  a  g [        XR                  R                  U R                  R                  U R
                  5      u  p[        U R                  R                  U5      U R                  l        [        U R                  R                  U5      U R                  l        [        U R                  R                  U5      U R                  l	        [        U R                  R                  USS9U R                  l        U R                  R                  [        U5      -
  U R                  l        U R                  R                  U R                  R                  -  U R                  l        U R
                  R                  U5      U l        g )Nr   r$   r3   )r   r   r|   r   r   r   r   r   r   r   r   r   r   union)r|   headsindexs      r   prune_headsSuperGlueAttention.prune_heads  s   u:?79900$))2O2OQUQbQb

 -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    U R                  UUUUUUU5      nU R                  US   U5      n	U	4USS  -   n
U
$ rT   )r|   r   )r|   r`   r   r   r   r   r   r   self_outputsattention_outputr   s              r   r   SuperGlueAttention.forward  sW     yy!"
  ;;|AF#%QR(88r-   )r   r   r|   rs   r   )rc   rd   re   rf   ru   r   r   r   r   rh   r   r   r   rk   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                   *  ^  \ rS rSrS\SS4U 4S jjr     SS\R                  S\\R                     S\\R                     S	\\R                     S
\	S\	S\
\R                  \\
\R                        \\
\R                        4   4S jjrSrU =r$ )SuperGlueAttentionalPropagationi  ro   r   Nc           	      |  > [         TU ]  5         UR                  n[        U5      U l        US-  US-  U/n[        S[        U5      S-
  5       Vs/ s H  n[        XUS-
     X4   5      PM     nnUR                  [        R                  " US   US   5      5        [        R                  " U5      U l        g s  snf )Nr   r$   r   rA   )rt   ru   r   r   	attentionr6   r   rm   r   r   rv   r   mlp)r|   ro   r   mlp_channelsr   r   r}   s         r   ru   (SuperGlueAttentionalPropagation.__init__  s    ((+F3#aq+F 1c,/!34
4 *&q1u2E|W4 	 
 	biiR 0,r2BCD==(
s   B9descriptorsr   r   r   r   r   c                     U R                  UUUUUS9nUS   nUSS  n	[        R                  " X/SS9n
U(       a  SOS nU R                   H  nU" U
5      n
U(       d  M  X4-   nM     XU	4$ )N)r   r   r   r   r   r$   r   r3   rb   )r	  r   r   r
  )r|   r  r   r   r   r   r   attention_outputsr   r	  r   r   r   s                r   r   'SuperGlueAttentionalPropagation.forward  s     !NN)"7#9/ + 
 #1%%ab)	yy+!6A>"6BDXXE .L##$5$G! 
 	99r-   )r	  r
  )NNNFFr   r   s   @r   r  r    s    
) 
)4 
) 268<9="'%*:\\: !.:  (5	:
 !) 6:  : #: 
u||XeELL&9:HU5<<EX<YY	Z: :r-   r  c                      ^  \ rS rSrS\SS4U 4S jjr   SS\R                  S\\R                     S\	S	\\	   S\
\R                  \\
   \\
   4   4
S
 jjrSrU =r$ )SuperGlueAttentionalGNNi  ro   r   Nc                   > [         TU ]  5         UR                  U l        UR                  U l        [
        R                  " [        [        U R                  5      5       Vs/ s H  n[        U5      PM     sn5      U l
        g s  snf rs   )rt   ru   r   gnn_layers_typeslayers_typesr   r   r6   r   r  r   )r|   ro   r;   r}   s      r   ru    SuperGlueAttentionalGNN.__init__  sh    !--"33mmV[\_`d`q`q\rVs$tVsQR%DV%LVs$tu$ts   "Br  r_   r   r   c           
      4   U(       a  SOS nU(       a  SOS nUR                   u  pxn	U(       a  XQ4-   n[        U R                  U R                  5       H  u  pS nS nUS:X  a  UR	                  SSXR
                  5      R                  S5      R	                  XxU R
                  5      nUb6  UR	                  SSSSU5      R                  S5      R	                  USSU5      OS nU
" UUUUUUS9nUS   nU(       a  X^S   -   nU(       a  XnS   -   nX-   nM     XU4$ )Nrb   crossrA   r   r$   )r   r   r   r   r   r   )rB   r   r   r  reshaper   flip)r|   r  r_   r   r   r   all_attentionsrG   num_keypointsr;   	gnn_layer
layer_typer   r   gnn_outputsdeltas                   r   r   SuperGlueAttentionalGNN.forward  sB    #7BD0d'2'8'8$
1 1N B%(d6G6G%H!I$(!%)"W$''A}>N>NOT!WWZ8H8HI & ' LLQ1m<AA!DLLZYZ\]_lm ' $#&;'=%9"3K  NE#$5A$F! !/a.!@%-K= &I> ~==r-   )r   r   r  )NFFr   r   s   @r   r  r    s    v v4 v (,"'/4->\\-> u||$->  	->
 'tn-> 
u||Xe_huo=	>-> ->r-   r  c                   n   ^  \ rS rSrS\SS4U 4S jjrS\R                  S\R                  4S jrSr	U =r
$ )	SuperGlueFinalProjectioni  ro   r   Nc                 n   > [         TU ]  5         UR                  n[        R                  " X"SS9U l        g )NT)bias)rt   ru   r   r   rv   
final_proj)r|   ro   r   r}   s      r   ru   !SuperGlueFinalProjection.__init__  s+    (())K4Hr-   r  c                 $    U R                  U5      $ rs   r&  )r|   r  s     r   r    SuperGlueFinalProjection.forward  s    {++r-   r)  r   r   s   @r   r#  r#    s9    I I4 I
,5<< ,ELL , ,r-   r#  c                   F    \ rS rSr\rSrSrS\R                  SS4S jr
Srg)	SuperGluePreTrainedModeli   	supergluepixel_valuesmoduler   Nc                    [        U[        R                  [        R                  [        R                  45      (       ak  UR
                  R                  R                  SU R                  R                  S9  UR                  b%  UR                  R                  R                  5         gg[        U[        R                  5      (       aJ  UR                  R                  R                  5         UR
                  R                  R                  S5        g[        U[        5      (       a5  [        R                  R!                  UR"                  R                  S5        gg)zInitialize the weightsg        )meanstdN      ?)
isinstancer   rv   Conv2dConv1dweightdatanormal_ro   initializer_ranger%  zero_	LayerNormfill_rm   init	constant_rw   )r|   r/  s     r   _init_weights&SuperGluePreTrainedModel._init_weights&  s    fryy"))RYY?@@ MM&&CT[[5R5R&S{{&  &&( '--KK""$MM$$S) =>>GGfmm00#6 ?r-   rb   )rc   rd   re   rf   r	   config_classbase_model_prefixmain_input_namer   Moduler@  rk   rb   r-   r   r,  r,     s)    "L#$O7BII 7$ 7r-   r,  zV
    SuperGlue model taking images as inputs and outputting the matching of them.
    )custom_introc                     ^  \ rS rSrSrS\SS4U 4S jjr   SS\R                  S\R                  S	\R                  S
\	S\	S\
\R                     S\
\   S\
\   S\\R                  \R                  \\4   4S jjr\    SS\R                  S\
\R                      S\
\   S\
\   S\
\   S\\\4   4S jj5       rSrU =r$ )SuperGlueForKeypointMatchingi5  a  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
ro   r   Nc                 x  > [         TU ]  U5        [        R                  " UR                  5      U l        [        U5      U l        [        U5      U l	        [        U5      U l        [        R                  R                  [        R                  " S5      5      nU R!                  SU5        U R#                  5         g )Nr3  	bin_score)rt   ru   r   from_configkeypoint_detector_configkeypoint_detectorr   keypoint_encoderr  gnnr#  final_projectionr   r   	Parameterr&   register_parameter	post_init)r|   ro   rJ  r}   s      r   ru   %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	                 \	  ^&^' U(       a  SOSn	U(       a  SOSn
UR                   S   S:X  aA  UR                   SS nUR                  US[        R                  S9UR	                  U5      U	U
4$ UR                   u  m&nm'nUR                  T&S-  T'S5      nUR                  T&S-  T'U R                  R                  5      nUR                  T&S-  T'5      nUb  UR                  T&S-  T'5      OSn[        XU5      nU R                  XUS9nUS   nX.-   nUb"  UR                  5       nU R                  Xo5      nO![        R                  " T&T'4UR                  S9nU R                  UUUUS	9nUS   nU R                  U5      nUR                  T&ST'U R                  R                  5      nUSS2S4   nUSS2S
4   nUUR!                  S
S5      -  nX0R                  R                  S-  -  nUbQ  UR                  T&ST'5      nUSS2S4   R#                  S5      R%                  SST'5      nUR'                  US:H  S5      n[)        X0R*                  U R                  R,                  S9nUSS2SS2SS24   R/                  S5      nUSS2SS2SS24   R/                  S
5      nUR0                  nUR0                  n[3        US
5      S   UR5                  S
U5      :H  n[3        US
5      S   UR5                  S
U5      :H  nUR7                  S5      n[        R8                  " UUR:                  R=                  5       U5      n[        R8                  " UU R                  R>                  :  UU5      n[        R8                  " UUR5                  S
U5      U5      nUUU:  -  n UU R5                  S
U5      -  n![        R8                  " U UUR7                  S5      5      n"[        R8                  " U!UUR7                  S5      5      n#[        R@                  " U"U#/5      R                  T&SS5      n$[        R@                  " UU/5      R                  T&SS5      n%U(       a+  XS
   -   n	U	US
   -   n	U	U4-   n	[C        U&U'4S jU	 5       5      n	U(       a  U
US   -   n
[C        U&U'4S jU
 5       5      n
U$U%U	U
4$ )aM  
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)`.
rb   Nr   r   rA   r   )r   )r"   )r_   r   r   r$   g      ?g    e)r?   c              3   j   >#    U  H(  oR                  TS TS5      R                  SS5      v   M*     g7f)r   rA   r   N)r  r   .0rW   rG   r  s     r   	<genexpr>ASuperGlueForKeypointMatching._match_image_pair.<locals>.<genexpr>  s3      &WhRS		*a;EEb"MMWhs   03c              3   L   >#    U  H  oR                  TS STT5      v   M     g7f)r   rA   N)r  rW  s     r   rY  rZ    s&     "vgubc99ZB}#]#]gus   !$)"rB   new_fullr   r   	new_zerosr  ro   r   r,   rN  r)   get_extended_attention_maskonesr"   rO  rP  r   r8   rE   masked_fillrR   rJ  sinkhorn_iterationsr'   indicesrX   gatherrC   wherer(   expmatching_thresholdr   r   )(r|   r   r  r=   r   r    r_   r   r   r   r  rB   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^   rG   r  s(                                         @@r   _match_image_pair.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 (	5A	 11)Zn1o-a0 "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 'v~~$++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 1a4H H 1KN B 15J4L L % &Wh& ! +k!n<N""vgu"vvN 	
 	
r-   r.  labelsreturn_dictc                    SnUb  [        S5      eUb  UOU R                  R                  nUb  UOU R                  R                  nUb  UOU R                  R                  nUR
                  S:w  d  UR                  S5      S:w  a  [        S5      eUR                  u  pxpnUR                  US-  XU5      nU R                  U5      nUSS u  pnnUR                  USSS5      R                  U5      nUR                  USS5      R                  U5      nUR                  USSU R                  R                  5      R                  U5      nUR                  USS5      nUR                  5       nUSS2SS2SS2S	4   U-  USS2SS2SS2S	4'   USS2SS2SS2S4   U
-  USS2SS2SS2S4'   U R                  UUUU
UUUUS
9u  nnnnU(       d  [        S UUUUUUU4 5       5      $ [        UUUUUUUS9$ )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)   rA   r   )r_   r   r   c              3   0   #    U  H  nUc  M  Uv   M     g 7frs   rb   )rX  vs     r   rY  7SuperGlueForKeypointMatching.forward.<locals>.<genexpr>1  s      eA es   	)r\   r]   r^   r   r_   r`   ra   )r   ro   r   r   use_return_dictndimr)   rB   r  rM  rD   r   cloner}  r   rZ   )r|   r.  r  r   r   r  r\   rG   r;   channelsr   r    keypoint_detectionsr   r=   r  r_   absolute_keypointsr]   r^   r`   ra   s                         r   r   $SuperGlueForKeypointMatching.forward  s?   > XYY1B1N-TXT_T_TqTq$8$D $++JjJj 	 &1%<k$++B]B]!\%6%6q%9Q%>noo1=1C1C.
x#++JNHeT"44\B/B2A/F,	;%%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-   )rP  rO  rM  rN  )NNN)NNNN)rc   rd   re   rf   rg   r	   ru   r   r   r   r   r   r   r}  r   rh   
LongTensorr   rZ   r   rk   r   r   s   @r   rH  rH  5  sM   " 4 * (,,0/3O
<<O
 \\O
 	O

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

 'tnS
 d^S
 
u,,	-S
 S
r-   rH  )/rg   r   dataclassesr   typingr   r   r   r   r   transformersr   5transformers.models.superglue.configuration_supergluer	   pytorch_utilsr   r   utilsr   r   r   autor   
get_loggerrc   loggerr   r   r   r,   r<   rR   rX   rZ   rE  rm   r   r   r   r   r   r  r  r#  r,  rH  __all__rb   r-   r   <module>r     sB     ! ) )   ( Q Q 9 9 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 7 7( 
E
#; E

E
P &'E
Fr-   