+
    –°³j¼%  ã                   óŽ  a € R t(0 t ^ RIt^ RIt^ RIt^ RIt^ RIHtHtHt ^ RI	H
t
HtHtHt ^ RIt^ RIHt ^ RIHu Ht ^ RIHt ^ RIHtHt ^ RIHt ^ RIHt ]P8                  P;                  R4      RJt]! R	4      t]t ] ^ k  ! R
 R]4      t!]PD                   ! R R4      4       t# ! R R4      t$R R lt%R R lt&R R lt'R# )é    N)ÚIterableÚMappingÚSequence)ÚcastÚProtocolÚ	TypeAliasÚTypeVar)Úfx)ÚcomputationÚdebug)Útensor_factory_functions)ÚArgumentÚdebug_extract_compiled_graphÚ_Tc                   ó2   a € ] tR t^t o V 3R lR ltRtV tR# )Ú_CompiledFnc                ó^   <€ V ^8„  d   QhRS[ P                  RS[S[ P                  ,          /# ©é   ÚargsÚreturn©ÚtorchÚTensorr   )ÚformatÚ__classdict__s   "€Úc/home/luca/screen-checking/.venv/lib/python3.14/site-packages/torch/_lazy/extract_compiled_graph.pyÚ__annotate__Ú_CompiledFn.__annotate__   s    ø€ ×JÑJ™eŸl™lÐJ©x¹¿¹Õ/EÑJó    c                ó   € R # ©N© )Úselfr   s   &*r   Ú__call__Ú_CompiledFn.__call__   s   € Ásr    r#   N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__r%   Ú__static_attributes__Ú__classdictcell__©r   s   @r   r   r      s   ø‡ € ßJÖJr    r   c                   óB   a € ] tR t^t o RtV 3R lR ltV 3R ltRtV tR# )ÚGraphInputMatchera‰  
The GraphInputMatcher class setup the graph inputs for future calls after lazy tracing.
Specifically, those graph inputs corresponding to method parameters should be replaced with the
arguments for the current call.

tensor_id_to_arg_idx maps the tensor id to the parameter index.
graph_input_tensor_ids, graph_input_ivalues list the tensor_id and ivalue for each of the
TS/XLA graph inputs.
c                óF   <€ V ^8„  d   QhRS[ S[,          RS[S[,          /# r   )r   ÚobjectÚlistÚ_IValue)r   r   s   "€r   r   ÚGraphInputMatcher.__annotate__4   s$   ø€ ÷ ñ ™X¡fÕ-ð ±$±wµ-ñ r    c                óÔ   € . p\        V P                  V P                  4       FA  w  r4V P                  P	                  VR 4      pVf   TpMW,          pVP                  V4       KC  	  V# r"   )ÚzipÚgraph_input_tensor_idsÚgraph_input_ivaluesÚtensor_id_to_arg_idxÚgetÚappend)r$   r   Ú
real_inputÚ	tensor_idÚtraced_ivalueÚarg_idxÚinps   &&     r   r%   ÚGraphInputMatcher.__call__4   sk   € Ø$&ˆ
Ü(+Ø×'Ñ'¨×)AÑ)Aö)
Ñ$ˆIð ×/Ñ/×3Ñ3°I¸tÓDˆGØŠØ#‘à•m�Ø×Ñ˜cÖ"ñ)
ð Ðr    c                ót   <€ V ^8„  d   Qh/ S[ S[S[3,          ;R&   S[S[,          ;R&   S[S[,          ;R&   # )r   r9   r7   r8   )ÚdictÚintr2   r3   )r   r   s   "€r   r   r4      s<   ø‡ ‚ ñ ™s¡C˜x�.Ñ(ñ ñ !¡�IÑ%ñ ñ& ™g�Ñ&ò' r    r#   N)	r'   r(   r)   r*   Ú__doc__r%   Ú__annotate_func__r+   r,   r-   s   @r   r/   r/      s   ø‡ € ñ÷(ð ÷- ƒ r    r/   c                   óH   a € ] tR t^Bt o RtV 3R lR ltV 3R lR ltRtV tR# )ÚReturnValueHandleraX  
When ltc_sync_multi is called on multi tensors, the compiled graph
will contain output only for unique tensors - if a tensor appears multiple
times in the input to _ltc_sync_multi, only the first occurrence matters.

However from python level, we still expect multi tensors returned with duplication
even if the TS graph dedup the output. e.g. for method:

  def forward(self, a):
    return a, a

the TS graph captured by LTC will return a single tensor, but Python method expects 2.

This class dedup the lazy tensors first to get the index that will be used
to duplicate the eager tensors later.
c                ó4   <€ V ^8„  d   QhRS[ S[,          RR/# )r   Úlazy_out_listr   N)r   r1   )r   r   s   "€r   r   ÚReturnValueHandler.__annotate__T   s    ø€ ÷ =ñ =¡h©vÕ&6ð =¸4ñ =r    c                ó`  € . V n         \        V4      V n        / p\        V4       F…  w  r4VP	                  \        V4      4      pVe%   V P                   V,          P                  V4       KG  \        V P                   4      pV P                   P                  V.4       WR\        V4      &   K‡  	  R # r"   )ÚindexÚlenÚtotal_countÚ	enumerater:   Úidr;   )r$   rJ   Útensor_id_to_idxÚdup_idxÚlazy_tensorÚuniq_idxs   &&    r   Ú__init__ÚReturnValueHandler.__init__T   sŒ   € Ø&(ˆŒ
Ü˜}Ó-ˆÔà+-ÐÜ$-¨mÖ$<Ñ ˆGØ'×+Ñ+¬B¨{«OÓ<ˆHØÒ#Ø—
‘
˜8Õ$×+Ñ+¨GÖ4ä˜tŸz™z›?�Ø—
‘
×!Ñ! 7 )Ô,Ø4<¤ K£Ó1ó %=r    c                óF   <€ V ^8„  d   QhRS[ S[,          RS[S[,          /# )r   Úeager_tensor_listr   )r   r   r2   )r   r   s   "€r   r   rK   b   s#   ø€ ÷ /ñ /¹Á"½ð /É$ÉrÍ(ñ /r    c                ód  € R .V P                   ,          p\        V4      \        V P                  4      8w  d.   \        R\        V4       R\        V P                  4       24      h\	        V4       F#  w  r4V P                  V,           F  pWBV&   K	  	  K%  	  \        \        \        ,          V4      # )Nzeager_tensor_list length z != index length )rO   rN   rM   ÚAssertionErrorrP   r   r2   r   )r$   rY   Úduplicated_listrU   Úeager_tensorrS   s   &&    r   Úduplicate_eager_tensorsÚ*ReturnValueHandler.duplicate_eager_tensorsb   s¤   € Ø,0¨6°D×4DÑ4DÕ+DˆÜÐ Ó!¤S¨¯©£_Ô4Ü Ø+¬CÐ0AÓ,BÐ+CÐCTÔUXÐY]×YcÑYcÓUdÐTeÐfóð ô '0Ð0AÖ&BÑ"ˆHØŸ:™: h×/Ð/�Ø+7 Ó(ó 0ñ 'Cô ”Dœ•H˜oÓ.Ð.r    )rM   rO   N)	r'   r(   r)   r*   rE   rV   r^   r+   r,   r-   s   @r   rH   rH   B   s   ø‡ € ñ÷"=ð =÷/ö /r    rH   c                ó<   € V ^8„  d   QhR\         P                  RR/# )r   Úmodelr   N)r
   ÚGraphModule)r   s   "r   r   r   p   s   € ÷ (ñ (œRŸ^™^ð (°ñ (r    c           	     óp  a€ R R loR R lpV P                   P                   Fø  p\        ;QJ d!    . V3R lVP                   4       F  NK  	  5M! V3R lVP                   4       4      Vn        VP                  P                  4        UUu/ uF  w  r4VS! V4      bK  	  uppVn        VP                  \        9   g   KŸ  V! VP                  VP                  4      '       d   KÄ  \        VP                  4      p\        P                  ! R4      VR&   WRn        Kú  	  V P                  4        R# u uppi )	zå
Factory methods in a Fx graph may create tensors for a specific eager devices.
If we take no actions, those eager tensors will be mixed with lazy tensors and
cause crash. This method overwrite those eager device to lazy device.
c                ó0   € V ^8„  d   QhR\         R\         /# )r   Údevr   )r   )r   s   "r   r   Ú'force_lazy_device.<locals>.__annotate__w   s   € ÷ ñ œ"ð ¤ñ r    c                 ó¦   € \        V \        P                  4      '       d1   \        \        \        P                  ! R V P
                  R7      4      # V # )Úlazy)rM   )Ú
isinstancer   Údevicer   r   rM   )re   s   &r   ÚtolazydeviceÚ'force_lazy_device.<locals>.tolazydevicew   s4   € Ü�cœ5Ÿ<™<×(Ò(ÜœœEŸLšL¨°s·y±yÔAÓBÐBØˆ
r    c                ót   € V ^8„  d   QhR\         \        ,          R\        \        \        3,          R\        /# )r   r   Úkwargsr   )r   r   r   ÚstrÚbool)r   s   "r   r   rf   |   s0   € ÷ 
ñ 
œ8¤HÕ-ð 
´w¼sÄH¸}Õ7Mð 
ÔRVñ 
r    c                 óò   € \         ;QJ d>    R  \        P                  ! WP                  4       4       4       F  '       g   K   R# 	  R# ! R  \        P                  ! WP                  4       4       4       4      # )c              3   óV   "  € T F  p\        V\        P                  4      x € K!  	  R # 5ir"   )ri   r   rj   )Ú.0Úargs   & r   Ú	<genexpr>Ú:force_lazy_device.<locals>.hasDeviceArg.<locals>.<genexpr>}   s%   é € ð 
á=�ô �sœEŸL™L×)Ð)Û=ùs   ‚')TF)ÚanyÚ	itertoolsÚchainÚvalues)r   rn   s   &&r   ÚhasDeviceArgÚ'force_lazy_device.<locals>.hasDeviceArg|   s[   € ß‹sñ 
ä —’ t¯]©]«_Ô=ó
�sŒsð 	
Šsð 	
ˆsñ 
ä —’ t¯]©]«_Ô=ó
ó 
ð 	
r    c              3   ó4   <"  € T F  pS! V4      x € K  	  R # 5ir"   r#   )rs   rt   rk   s   & €r   ru   Ú$force_lazy_device.<locals>.<genexpr>ƒ   s   øé € Ð=±W¨c™ S×)Ð)³Wùs   ƒrh   rj   N)ÚgraphÚnodesÚtupler   rn   ÚitemsÚtargetr   rC   r   rj   Ú	recompile)ra   r{   ÚndÚkÚvrn   rk   s   &     @r   Úforce_lazy_devicerˆ   p   sÚ   ø€ õõ

ð �k‰k×Ôˆß”%Ô=°R·W²WÓ=—%‘%Ô=°R·W²WÓ=Ó=ˆŒØ46·I±I·O±OÔ4EÔFÑ4E©D¨A�Q™ Q›Ò'Ñ4EÒFˆŒ	ð �9‰9Ô0Ö0¹Ø�G‰G�R—Y‘Y÷:
ô :
ô ˜"Ÿ)™)“_ˆFÜ$Ÿ|š|¨FÓ3ˆF�8ÑØŽIñ)  ð, 
‡O�OÖùó) Gs   ÂD2c                ó:   € V ^8„  d   QhR\         \        ,          /# )r   r   )r2   ro   )r   s   "r   r   r   ›   s   € ÷ 	ñ 	œ$œs�)ñ 	r    c                  óÐ   € . p \         P                  ! 4        FJ  pR V9  d   K  \        \         P                  ! V4      4      pV^ 8”  g   K4  V P	                  V RV 24       KL  	  V # )zaten::Ú=)ÚmetricsÚcounter_namesrD   Úcounter_valuer;   )Úfallback_opsÚopnameÚvals      r   Úget_fallback_opsr’   ›   sb   € Ø€LÜ×'Ò'Ö)ˆØ˜6Ô!ÙÜ”'×'Ò'¨Ó/Ó0ˆØ�Ž7Ø×Ñ 6 (¨!¨C¨5Ð 1Ö2ñ *ð Ðr    c                óz   € V ^8„  d   QhR\         P                  R\        \        P                  ,          R\
        /# )r   ra   Úexample_inputsr   )r
   rb   r   r   r   r   )r   s   "r   r   r   §   s6   € ÷ Rñ RÜ�>‰>ðRÜ+3´E·L±LÕ+AðRäñRr    c                óô  aaaa€ V Uu. uF  q"P                  RR7      NK  	  ppV Uu. uF  p\        P                  ! V4      NK  	  pp\        V4       UUu/ uF  w  rgWvbK	  	  ppp\        P
                  ! V 4      P                  \        P                  ! R4      R7      p	\        V	4       \        P                  ! 4        V	! V!  p
\        4       p\        P                  ! 4        \        V4      ^ 8”  d   \        RRP                  V4       24      h\        V
\         \"        34      '       g   V
3p
\!        V4      \!        V
4      ,           o\%        S4      o\&        '       d8   \)        RV P*                  4       \)        R\,        P.                  ! SR4      4       \0        P2                  ! S4      w  pp\        V4      \        V4      8w  d$   \5        R\        V4       R	\        V4       24      h\7        WŒV4      o\0        P8                  ! S4      o\&        '       d'   \)        R
S4       \)        RV 24       \)        RV4       \        P:                  ! S. 4       R VVVV3R llpV# u upi u upi u uppi )zê
Optimize an eager model with LTC and returns a wrapper to execute the
compiled graph directly without retracing. It depends on other mechanisms
like TorchDynamo guards to guarantee the returned wrapper is only called
when it's safe.
rh   )rj   z8Fail to extract the compiled graph because of fallback: Ú,z	Fx code:
zLTC IR:Útextzgraph_input_tensor_ids length z != graph_input_ivalues length Ú
graph_hashzargs_tensor_ids ztensor ids from device data:c                ón   € V ^8„  d   QhR\         P                  R\        \         P                  ,          /# r   r   )r   s   "r   r   Ú,extract_compiled_graph.<locals>.__annotate__å   s'   € ÷  ñ  œUŸ\™\ð  ¬h´u·|±|Õ.Dñ  r    c                  óŠ  <€ \        S4      ^ 8X  d   R# S! V 4      pSP                  \        P                  ! SV4      4      p\        V4      \        S4      8w  d$   \	        R\        V4       R\        S4       24      h\        V 4       F*  w  r4WBV,          Jg   K  VP                  W#,          4       K,  	  V\        V 4      R # )r   zresult length z != args_and_out length Nr#   )rN   r^   r   Úrun_cached_graphr[   rP   Úcopy_)	r   Úgraph_inputÚresÚirt   Úargs_and_outr˜   Úgraph_input_matcherÚreturn_value_handlers	   *    €€€€r   Úoptimized_modÚ-extract_compiled_graph.<locals>.optimized_modå   s·   ø€ Üˆ|Ó Ô!ØˆIÙ)¨$Ó/ˆØ"×:Ñ:Ü×(Ò(¨°[ÓAó
ˆô ˆs‹8”s˜<Ó(Ô(Ü Ø ¤ S£ 
Ð*BÄ3À|ÓCTÐBUÐVóð ô   –o‰FˆAà˜a�&Õ Ø—	‘	˜#�&Ö!ñ &ð ”3�t“9�;ÐÐr    )Útorh   Úget_tensor_idrP   ÚcopyÚdeepcopyr   rj   rˆ   rŒ   Úresetr’   rN   ÚRuntimeErrorÚjoinri   r�   r2   rH   r   ÚprintÚcodeÚ
lazy_debugÚdump_irr   Úget_tensors_ts_device_data_noder[   r/   Úget_graph_hashÚ
sync_multi)ra   r”   rt   Ú	lazy_argsÚlazy_argÚargs_tensor_idsr    r=   r9   Ú
lazy_modelÚlazy_outr�   r7   r8   r¤   r¡   r˜   r¢   r£   s   &&             @@@@r   Úextract_compiled_graphr¹   §   s(  û€ ñ 3AÓA±.¨3—‘˜v�Ö&±.€IÐAÙDMÓNÁI¸”t×)Ò)¨(Ö3ÁI€OÐNÜ=FÀÔ=WÔXÑ=W©\¨Q˜IšLÑ=WÐÑXÜ—’˜uÓ%×(Ñ(´·²¸VÓ0DÐ(ÓE€JÜ�jÔ!ô ‡M‚M„OÙ˜9Ñ%€HÜ#Ó%€LÜ‡M‚M„Oä
ˆ<Ó˜1ÔÜØFÀsÇxÁxÐP\ÓG]ÐF^Ð_ó
ð 	
ô �h¤¬ ×.Ò.Ø�;ˆä˜Ó#¤e¨H£oÕ5€LÜ-¨lÓ;ÐßƒuÜˆl˜EŸJ™JÔ'Üˆiœ×+Ò+¨L¸&ÓAÔBô 	×3Ò3°LÓAñØØä
Ð!Ó"¤cÐ*=Ó&>Ô>ÜØ,¬SÐ1GÓ-HÐ,Ið J-Ü-0Ð1DÓ-EÐ,FðHó
ð 	
ô ,ØÐ6IóÐô ×+Ò+¨LÓ9€JçƒuÜˆl˜JÔ'ÜÐ  Ð 1Ð2Ô3ÜÐ,Ð.DÔEô
 	‡O‚O�L "Ô%÷ ó  ð( ÐùòS BùÚNùÛXs   ‰I*©I/ÁI4c                ó@   € V ^8„  d   Qh/ ^ \         9   d
   \        ;R&   # )r   r3   )Ú__conditional_annotations__r   )r   s   "r   r   r      s   € ‡�÷, Ò ŒÑ ò- r    ))r»   r¨   Údataclassesrx   ÚosÚcollections.abcr   r   r   Útypingr   r   r   r	   r   Útorch._lazyÚ_lazyrh   Útorch._lazy.metricsrŒ   r
   r   r   r¯   Ú$torch._lazy.tensor_factory_functionsr   Útorch.fx.noder   Úenvironr:   r   r1   r3   r   Ú	dataclassr/   rH   rˆ   r’   r¹   r   )r»   s   @r   Ú<module>rÇ      s¯   øðß €Û Û Û 	ß 7Ñ 7ß 5Ó 5ã Ý ß %Ð %Ý ß 8Ý IÝ "ð 	�
‰
�‰Ð5Ó6¸dÐB€áˆTƒ]€ð €Ó ôK�(ô Kð ×Ñ÷ ð  ó ð ÷F+/ñ +/õ\(õV	÷Rr    