+
    QV-jÒ'  ã                  óp   € R t ^ RIHt ^ RIt^ RIHu Ht RR R lltRR R llt	RR R llt
RR	 R
 lltR# )uÿ  Vision utility functions for pre-computing very dynamic and
data-dependent tensors that can break model graph capturing.

All functions are standalone (no model weights) and compute tensors from
`grid_thw` + config scalars. They are used by vision encoders and can be
precomputed before `torch.compile` / `torch.export` tracing since they
use untraceable ops (`repeat_interleave`, `.tolist()`, `nonzero()`, loops).

Each `get_*` accepts an optional `kwargs` dict; if it contains the
precomputed tensor under the natural key (`"cu_seqlens"`, `"position_ids"`,
â€¦), the function pops and returns it instead of computing. Vision encoders
write `x = get_vision_x(..., kwargs=kwargs)` and the matching key is
removed from the caller's kwargs as a side-effect of the pop.
)ÚannotationsNc               ó$   € V ^8„  d   QhRRRRRR/# )é   Úgrid_thwútorch.TensorÚkwargsúdict | NoneÚreturn© )Úformats   "Új/Volumes/fast/ai/experiments/ui-tars-smoke/.venv/lib/python3.14/site-packages/transformers/vision_utils.pyÚ__annotate__r   #   s"   € ÷ .ñ . Lð .¸+ð .ÐQ]ñ .ó    c                óp  € Ve   VP                  RR4      ;pe   V# \        P                  ! V R,          V R,          ,          V R,          4      P                  ^ \        P                  P                  4       '       d   V P                  M\        P                  R7      p\        P                  ! VR^ R7      # )	um  Get cumulative sequence lengths from vision grid info, or pop from `kwargs` if precomputed.

Args:
    grid_thw: `(num_images_or_videos, 3)` â€” temporal, height, width per entry.
    kwargs: optional caller kwargs â€” if it contains `"cu_seqlens"` it is popped and returned.

Returns:
    `cu_seqlens`: `(total_patches + 1,)` int32 cumulative sequence boundaries.
NÚ
cu_seqlens)ÚdimÚdtype)Úvalue)ºNNNé   )r   r   )r   é    )r   r   )
ÚpopÚtorchÚrepeat_interleaveÚcumsumÚjitÚ
is_tracingr   Úint32ÚFÚpad)r   r   r   s   && r   Úget_vision_cu_seqlensr    #   s�   € ð Ò¨V¯Z©Z¸ÀdÓ-KÐK˜zÒXØÐÜ×(Ò(¨°$­¸(À4½.Õ)HÈ(ÐSWÍ.ÓY×`Ñ`Ø¤u§y¡y×';Ñ';×'=Ò'=�X—^’^Ä5Ç;Á;ð aó €Jô �5Š5�˜V¨1Ô-Ð-r   c               ó(   € V ^8„  d   QhRRRRRRRR/# )r   r   r   Úspatial_merge_sizezint | torch.Tensorr   r   r	   r
   )r   s   "r   r   r   5   s,   € ÷ *ñ *Øð*Ø0Bð*ØLWð*àñ*r   c                ó  € Ve   VP                  RR4      ;pe   V# V P                  p\        V\        4      '       d2   \        P
                  ! V.VR7      P                  \        V 4      4      p. p\        V P                  4       VP                  4       4       EFV  w  w  rVrx\        V4      \        V4      \        V4      \        V4      3w  rVrx\        P                  ! WdR7      P                  ^4      P                  RV4      p	V	P                  Wh,          W‡V,          V4      P                  ^^4      P                  4       p	\        P                  ! WtR7      P                  ^ 4      P                  VR4      p
V
P                  Wh,          W‡V,          V4      P                  ^^4      P                  4       p
VP                  \        P                   ! Wš.RR7      P#                  V^4      4       EKY  	  \        P$                  ! V^ R7      # )uà  Get (row, col) position IDs for vision rotary embeddings, or pop from `kwargs` if precomputed.

Args:
    grid_thw: `(num_images_or_videos, 3)`
    spatial_merge_size: merge block size â€” either a single `int` (same for all images)
        or a `(num_images_or_videos,)` tensor (per-image).
    kwargs: optional caller kwargs â€” if it contains `"position_ids"` it is popped and returned.

Returns:
    `position_ids`: `(total_tokens, 2)` long â€” (row, col) position per token.
NÚposition_ids©Údevice©r   éÿÿÿÿ)r   r&   Ú
isinstanceÚintr   ÚtensorÚexpandÚlenÚzipÚtolistÚarangeÚ	unsqueezeÚreshapeÚ	transposeÚflattenÚappendÚstackÚrepeatÚcat)r   r"   r   r$   r&   ÚtÚhÚwÚ
merge_sizeÚhpos_idsÚwpos_idss   &&&        r   Úget_vision_position_idsr?   5   s¤  € ð Ò¨v¯z©z¸.È$Ó/OÐO˜|Ò\ØÐØ�_‰_€FÜÐ$¤c×*Ò*Ü"Ÿ\š\Ð+=Ð*>ÀvÔN×UÑUÔVYÐZbÓVcÓdÐà€LÜ!$ X§_¡_Ó%6Ð8J×8QÑ8QÓ8S×!TÑ‰	ˆˆqÜ! !›f¤c¨!£f¬c°!«f´c¸*³oÐEÑˆˆaÜ—<’< Ô1×;Ñ;¸AÓ>×EÑEÀbÈ!ÓLˆØ×#Ñ# A¥O°ZÀjÅÐR\Ó]×gÑgÐhiÐklÓm×uÑuÓwˆä—<’< Ô1×;Ñ;¸AÓ>×EÑEÀaÈÓLˆØ×#Ñ# A¥O°ZÀjÅÐR\Ó]×gÑgÐhiÐklÓm×uÑuÓwˆØ×ÑœEŸKšK¨Ð(<À"ÔE×LÑLÈQÐPQÓR×Sñ "Uô �9Š9�\ qÔ)Ð)r   c               ó0   € V ^8„  d   QhRRRRRRRRRRR	R
/# )r   r   r   r"   r*   Úwindow_sizeÚ
patch_sizer   r   r	   ú!tuple[torch.Tensor, torch.Tensor]r
   )r   s   "r   r   r   V   sD   € ÷ :+ñ :+Øð:+àð:+ð ð:+ð ð	:+ð
 ð:+ð 'ñ:+r   c                ó
  € Ve0   VP                  RR4      pVP                  RR4      pVe   Ve   WV3# . p^ .p^ pW!,          V,          pV^,          p	V P                  4        EF¼  w  r«p\        V
4      \        V4      \        V4      rËp
W±,          pWÁ,          p\        P                  ! W­,          V,          4      P                  W­V4      pW�V,          ,
          pWŽV,          ,
          pVV,           V,          pVV,           V,          p\        P                  ! V^ V^ V3RR4      pVP                  V
VVVV4      pVP                  ^ ^^^^4      P                  V
VV,          Wˆ4      pVR8g  P                  ^^.4      P                  R4      pVP                  R4      pVVR8g  ,          pVP                  VV,           4       VP                  ^ 4      V	,          VR,          ,           pVP                  VP                  4       4       WzV,          V,          ,          pEK¿  	  \        P                  ! V^ R7      p\        P                  ! W`P                  \        P                   R7      p\        P"                  ! V4      pWV3# )	ux  Get window attention indices, or pop `"window_index"`/`"cu_window_seqlens"` from `kwargs` if both precomputed.

Args:
    grid_thw: `(num_images_or_videos, 3)`
    spatial_merge_size: merge block size from vision config.
    window_size: window size from vision config.
    patch_size: patch size from vision config.
    kwargs: optional caller kwargs â€” if it contains both `"window_index"` and `"cu_window_seqlens"` they are popped and returned.

Returns:
    `window_index`: `(total_tokens,)` long â€” reorder indices for windowed attention.
    `cu_window_seqlens`: `(num_windows + 1,)` int32 â€” cumulative window boundaries.
NÚwindow_indexÚcu_window_seqlensÚconstantr'   )r&   r   iœÿÿÿr(   )r   r/   r*   r   r0   r2   r   r   ÚpermuteÚsumr5   r   Úextendr8   r+   r&   r   Úunique_consecutive)r   r"   rA   rB   r   rE   rF   Úwindow_index_idÚvit_merger_window_sizeÚspatial_merge_unitÚgrid_tÚgrid_hÚgrid_wÚ
llm_grid_hÚ
llm_grid_wÚindexÚpad_hÚpad_wÚnum_windows_hÚnum_windows_wÚindex_paddedÚseqlensÚ	index_newÚcu_seqlens_tmps   &&&&&                   r   Úget_vision_window_indexr]   V   s`  € ð( ÒØ—z‘z .°$Ó7ˆØ"ŸJ™JÐ':¸DÓAÐØÒ#Ð(9Ò(EØÐ2Ð2Ø€LØ ˜cÐØ€OØ(Õ>À*ÕLÐØ+¨QÕ.Ðà"*§/¡/×"3Ñˆ˜Ü!$ V£¬c°&«k¼3¸v»;˜ˆØÕ1ˆ
ØÕ1ˆ
Ü—’˜VÕ0°:Õ=Ó>×FÑFÀvÐ[eÓfˆØ&Ð6LÕ)LÕLˆØ&Ð6LÕ)LÕLˆØ# eÕ+Ð0FÕFˆØ# eÕ+Ð0FÕFˆÜ—u’u˜U Q¨¨q°%Ð$8¸*ÀdÓKˆØ#×+Ñ+Ø�MÐ#9¸=ÐJ`ó
ˆð $×+Ñ+¨A¨q°!°Q¸Ó:×BÑBØ�M MÕ1Ð3Ió
ˆð   4Ñ'×,Ñ,¨a°¨VÓ4×<Ñ<¸RÓ@ˆØ#×+Ñ+¨BÓ/ˆØ  °Ñ!5Õ6ˆ	Ø×Ñ˜I¨Õ7Ô8Ø Ÿ™¨Ó*Ð-?Õ?ÐBSÐTVÕBWÕWˆØ× Ñ  ×!6Ñ!6Ó!8Ô9Ø JÕ.°Õ;Õ;‹ñ- #4ô0 —9’9˜\¨qÔ1€LÜŸšÐ%6¿¹ÔV[×VaÑVaÔbÐÜ×0Ò0Ð1BÓCÐØÐ*Ð*r   c          
     ó,   € V ^8„  d   QhRRRRRRRRRR	/# )
r   r   r   Únum_grid_per_sider*   r"   r   r   r	   rC   r
   )r   s   "r   r   r   “   sA   € ÷ F.ñ F.ØðF.àðF.ð ðF.ð ð	F.ð
 'ñF.r   c                óŒ  € Ve0   VP                  RR4      pVP                  RR4      pVe   Ve   WE3# TpTpV P                  p\        ^4       U	u. uF  p	. NK  	  p
p	\        ^4       U	u. uF  p	. NK  	  pp	V P                  4        EF.  w  rÍp\	        V4      \	        V4      \	        V4      ríp\
        P                  ! ^ V^,
          WØR7      p\
        P                  ! ^ V^,
          WèR7      pVP	                  4       pVP	                  4       pV^,           P                  V^,
          R7      pV^,           P                  V^,
          R7      pVV,
          pVV,
          pVV,          pVV,          pVR,          VR,          ,           P                  4       VR,          VR,          ,           P                  4       VR,          VR,          ,           P                  4       VR,          VR,          ,           P                  4       .p^V,
          R,          ^V,
          R,          ,          P                  4       ^V,
          R,          VR,          ,          P                  4       VR,          ^V,
          R,          ,          P                  4       VR,          VR,          ,          P                  4       .p\
        P                  ! WØR7      P                  W×,          V4      p\
        P                  ! WèR7      P                  Wç,          V4      pVR,          V,          VR	,          ,           P                  ^^4      P                  4       P                  V4      p\        ^4       FO  pV
V,          P                  VV,          V,          4       VV,          P                  VV,          V,          4       KQ  	  EK1  	  \
        P                  ! V
 Uu. uF  p\
        P                  ! V4      NK  	  up4      p\
        P                  ! V Uu. uF  p\
        P                  ! V4      NK  	  up4      pWE3# u up	i u up	i u upi u upi )
uz  Get bilinear interpolation indices/weights, or pop `"bilinear_indices"`/`"bilinear_weights"` from `kwargs` if both precomputed.

Args:
    grid_thw: `(num_images_or_videos, 3)`
    num_grid_per_side: `int(num_position_embeddings ** 0.5)` from vision config.
    spatial_merge_size: merge block size from vision config.
    kwargs: optional caller kwargs â€” if it contains both `"bilinear_indices"` and `"bilinear_weights"` they are popped and returned.

Returns:
    `bilinear_indices`: `(4, total_thw)` long â€” bilinear corner indices into pos_embed table.
    `bilinear_weights`: `(4, total_thw)` float â€” interpolation weights.
NÚbilinear_indicesÚbilinear_weightsr%   )Úmax)r   N)Nr   )r   r   NN)NNr   r   )r   r&   Úranger/   r*   r   ÚlinspaceÚclampr4   r0   Úviewr3   r7   r5   r6   r8   ) r   r_   r"   r   ra   rb   Úsider<   r&   Ú_Ú	idx_partsÚweight_partsr9   r:   r;   Úh_gridÚw_gridÚh_floorÚw_floorÚh_ceilÚw_ceilÚh_fracÚw_fracÚh_floor_offsetÚh_ceil_offsetÚcorner_indicesÚcorner_weightsÚh_idxÚw_idxÚreorderÚiÚps    &&&&                            r   Ú'get_vision_bilinear_indices_and_weightsr}   “   sr  € ð$ ÒØ!Ÿ:™:Ð&8¸$Ó?ÐØ!Ÿ:™:Ð&8¸$Ó?ÐØÒ'Ð,<Ò,HØ#Ð5Ð5Ø€DØ#€JØ�_‰_€Fä7<¸Q´xÓ*@±x°!«2±x€IÐ*@Ü:?À¼(Ó-C¹(°Q«b¹(€LÐ-Cà—?‘?×$‰ˆˆaÜ�a“&œ#˜a›&¤# a£&ˆaˆä—’  4¨!¥8¨QÔ>ˆÜ—’  4¨!¥8¨QÔ>ˆà—*‘*“,ˆØ—*‘*“,ˆØ˜A•+×$Ñ$¨°­Ð$Ó2ˆØ˜A•+×$Ñ$¨°­Ð$Ó2ˆà˜'Õ!ˆØ˜'Õ!ˆà  4�ˆØ �ˆð ˜GÕ$ w¨wÕ'7Õ7×@Ñ@ÓBØ˜GÕ$ v¨g¥Õ6×?Ñ?ÓAØ˜7Õ# g¨gÕ&6Õ6×?Ñ?ÓAØ˜7Õ# f¨W¥oÕ5×>Ñ>Ó@ð	
ˆð �&�j˜'Õ" a¨&¥j°'Õ%:Õ:×CÑCÓEØ�&�j˜'Õ" V¨G¥_Õ4×=Ñ=Ó?Ø�G�_  F¥
¨GÕ4Õ4×=Ñ=Ó?Ø�G�_˜v g�Õ.×7Ñ7Ó9ð	
ˆô —’˜QÔ.×3Ñ3°AµOÀZÓPˆÜ—’˜QÔ.×3Ñ3°AµOÀZÓPˆØÐ)Õ*¨QÕ.°Ð7GÕ1HÕH×SÑSÐTUÐWXÓY×aÑaÓc×jÑjÐklÓmˆä�q–ˆAØ�a�L×Ñ ¨qÕ 1°'Õ :Ô;Ø˜�O×"Ñ" >°!Õ#4°WÕ#=Ö>ô ñE %ôL —{’{¹)Ó#D¹)°Q¤E§I¢I¨a¦L¹)Ñ#DÓEÐÜ—{’{¹,Ó#G¹,°Q¤E§I¢I¨a¦L¹,Ñ#GÓHÐØÐ-Ð-ùòW +AùÚ-CùòP $EùÚ#Gs   ÁP2Á+P7ÏP<Ð	Q)N)Ú__doc__Ú
__future__r   r   Útorch.nn.functionalÚnnÚ
functionalr   r    r?   r]   r}   r
   r   r   Ú<module>rƒ      s5   ðñõ #ã ß Ð ÷.÷$*÷B:+÷zF.ñ F.r   