Ë
    ÄÓ^jM-  ã                   óö  — d dl Z d dlZd dlZd dlmZmZmZ d dlmZm	Z	m
Z
  e	d«      Z e
d«      Zd dlZd dlmZ  ej                   e«      ZdgZdeeef   d	eeef   fd
„Ze j*                  d	ed   fd„«       Zdej.                  j0                  ded	ej.                  j0                  fd„Zdej.                  j6                  d	efd„Z G d„ d«      Z G d„ d«      Zdej.                  j0                  d	ej.                  j0                  fd„Z dej.                  j0                  d	ej.                  j0                  fd„Z! ed¬«      dej.                  j0                  de"d	ej.                  j0                  fd„«       Z#y)é    N)ÚCallableÚIteratorÚMapping)ÚAnyÚ	ParamSpecÚTypeVarÚ_PÚ_R)ÚcompatibilityÚregional_inductorÚfnÚreturnc                 ó�   ‡ — t        j                  ‰ «      dt        j                  dt        j                  dt
        fˆ fd„«       }|S )NÚargsÚkwargsr   c                  ó   •—  ‰| i |¤ŽS ©N© )r   r   r   s     €úl/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/fx/passes/regional_inductor.pyÚinnerz_dummy_wrapper.<locals>.inner   s   ø€ á�4Ð"˜6Ñ"Ð"ó    )Ú	functoolsÚwrapsr	   r   r   r
   )r   r   s   ` r   Ú_dummy_wrapperr      sB   ø€ Ü‡_�_�RÓð#”R—W‘Wð #¬¯	©	ð #´bô #ó ð#ð €Lr   c               #   ó’   K  — t         j                  j                  j                  d¬«      5  d –— d d d «       y # 1 sw Y   y xY w­w)NF)Ú'remat_using_tags_for_fwd_loss_bwd_graph)ÚtorchÚ
_functorchÚconfigÚpatchr   r   r   Ú&_disable_remat_for_regional_subcompiler!      s<   è ø€ ô 
×	Ñ	×	 Ñ	 ×	&Ñ	&ÈuÐ	&Ó	Uñ Û÷÷ ñ üs   ‚+A­;²	A»AÁ AÚgmÚprefixc           
      ó„  — ddl m} | j                  j                  D �]Û  }|j                  dk(  sŒ|j
                  j                  |«      sŒ0g }|j                  D ]H  }t        |d«      r-d|j                  v r|j                  |j                  d   «       Œ<t        d|› �«      ‚ t        | |j
                  «      }i }|j                  j                  D ]r  }t        |d«      sŒ|j                  j                  dd «      sŒ-|j                  d   }	t        |	t        «      sŒMd|	v sŒR|	d   }
t        |
t        «      sŒhd	|
v sŒm|
d	   } n t         j#                  d
|j
                  |«       dd lmc m} |D ]  }t        ||«      rŒt+        d|› d�«      ‚ |j-                  |«      5  t/        «       5  t0        j2                  j4                  j7                  |j
                  «      5  t0        j&                  j9                  ||dd¬«      }d d d «       d d d «       d d d «       t        |«      st;        dt=        |«      › �«      ‚t?        |«      }| j                  jA                  |«      5  | j                  jC                  ||jD                  |jF                  ¬«      }|j                  |_	        |jI                  |«       | j                  jK                  |«       | jL                  |j
                  = d d d «       �ŒÞ | jO                  «        | S # 1 sw Y   �ŒxY w# 1 sw Y   �ŒxY w# 1 sw Y   �ŒxY w# 1 sw Y   �Œ"xY w)Nr   )ÚAOTCompiledArtifactÚcall_moduleÚmetaÚvalz7Partition is bad because non fake tensor value is seen ÚcustomÚcompile_with_inductorÚinductor_configsz0Compiling submodule %s with inductor options: %szInvalid inductor config key 'z_' in regional_inductor annotation. Available config keys can be found in torch._inductor.configÚfrom_tracing_contextT)Údynamic_shapesÚaotz"Expected AOTCompiledArtifact, got )r   r   )(Ú"torch._inductor.standalone_compiler%   ÚgraphÚnodesÚopÚtargetÚ
startswithÚall_input_nodesÚhasattrr'   ÚappendÚRuntimeErrorÚgetattrÚgetÚ
isinstanceÚdictÚloggerÚinfoÚtorch._inductor.configÚ	_inductorr   Ú
ValueErrorr    r!   r   ÚfxÚ	tracebackÚ$_set_regional_inductor_subgraph_nameÚstandalone_compileÚAssertionErrorÚtyper   Úinserting_afterÚcall_functionr   r   Úreplace_all_uses_withÚ
erase_nodeÚ_modulesÚ	recompile)r"   r#   r%   ÚnodeÚfake_inputsÚinp_nodeÚsubmodÚinductor_optionsÚsub_noder)   Úcompile_valueÚinductor_configÚkeyÚcompiled_fnÚcompiled_submodÚnew_nodes                   r   Ú_compile_submodrZ   *   s	  € ÝFà—‘—‘ó G-ˆØ�7‰7�mÓ#¨¯©×(>Ñ(>¸vÕ(FØˆKØ ×0Ñ0ò �Ü˜8 VÔ,°¸(¿-¹-Ñ1GØ×&Ñ& x§}¡}°UÑ';Õ<ä&ØQÐRZÐQ[Ð\óð ð	ô ˜R §¡Ó-ˆFð
 02ÐØ"ŸL™L×.Ñ.ò 
"�Ü˜8 VÕ,°·±×1BÑ1BÀ8ÈTÕ1RØ%Ÿ]™]¨8Ñ4�FÜ! &¬$Õ/Ð4KÈvÒ4UØ(.Ð/FÑ(G˜ä& }´dÕ;Ø 2°mÒ Cà/<Ð=OÑ/PÐ,Ù!ð
"ô �K‰KØBØ—‘Ø ô÷ =Ð<ð (ò �Ü˜°Õ4Ü$Ø7¸°uð =Wð Xóð ðð  ×%Ñ%Ð&6Ó7ñ
ä6Ó8ñ
ô —‘×"Ñ"×GÑGÈÏÉÓTñ
ô
 $Ÿo™o×@Ñ@ØØØ#9Øð	 Aó �÷
÷ 
÷ 
ô ˜kÐ+>Ô?Ü$Ø8¼¸kÓ9JÐ8KÐLóð ô -¨[Ó9ˆOØ—‘×)Ñ)¨$Ó/ñ -ØŸ8™8×1Ñ1Ø#¨$¯)©)¸D¿K¹Kð 2ó �ð !%§	¡	�”Ø×*Ñ*¨8Ô4Ø—‘×#Ñ# DÔ)Ø—K‘K §¡Ð,÷-ñ -ðAG-ðR ‡L�L„NØ€I÷7
ñ 
ú÷ 
ñ 
ú÷ 
ñ 
ú÷"-ñ -úsI   Æ&L(Æ14LÇ%$L	È	LÈL(É*BL5ÌLÌLÌL%Ì L(Ì(L2	Ì5L?	rN   c                 ó®   — t        | j                  dvxr= t        | d«      xr/ | j                  j	                  dd «      xr d| j                  d   v «      S )N)ÚplaceholderÚoutputr'   r)   r*   )Úboolr2   r6   r'   r:   )rN   s    r   Ú_needs_inductor_compiler_   z   sZ   € ÜØ�‰Ð0Ð0ò 	;Ü�D˜&Ó!ò	;à�I‰I�M‰M˜( DÓ)ò	;ð $ t§y¡y°Ñ':Ð:ó	ð r   c                   óX  — e Zd ZdZedej                  j                  dej                  j                  fd„«       Ze	 d	dej                  j                  de	e
   dz  dej                  j                  fd„«       Zdej                  j                  dej                  j                  fd„Zy)
Ú_RegionScooperzK
    Scoops out the inductor marked regions. It does NOT compile them.
    r"   r   c                 ó*  — ddl m} ddlm} ddlm} t        «       }i }| j                  j                  D ]e  }t        |«      sŒ|j                  d   d   }t        |t        «      r
d|v r|d   }n|}|j                  |t        «       «      j                  |«       Œg |st         j#                  d«       | S d	t        t$        j&                  j(                     d
t*        t,        t.        t$        j0                  j2                  f   t$        j&                  j(                  gt4        f   fd„}	g }
|j7                  «       D ]K  } | |	|«      «      } || |d¬«      }|j9                  «       D ]  }|
j;                  |j                  «       Œ ŒM  || |
dd¬«      S )Nr   )ÚCapabilityBasedPartitioner)Úcreate_op_support)Úfuse_by_partitionsr)   r*   Úinductor_regionzNo inductor marked nodes foundÚregion_nodesr   c                 ó¢   ‡ — dt         t        t        j                  j                  f   dt        j
                  j                  dt        fˆ fd„}|S )NÚ_submodulesrN   r   c                 ó   •— |‰v S r   r   )ri   rN   rg   s     €r   Úis_node_supportedzN_RegionScooper.scoop_regions.<locals>._is_in_region.<locals>.is_node_supported¨   s   ø€ ð ˜|Ð+Ð+r   )r   Ústrr   ÚnnÚModulerB   ÚNoder^   )rg   rk   s   ` r   Ú_is_in_regionz3_RegionScooper.scoop_regions.<locals>._is_in_region¥   sB   ø€ ð,Ü$¤S¬%¯(©(¯/©/Ð%9Ñ:ð,ÜBGÇ(Á(Ç-Á-ð,äõ,ð
 %Ð$r   T)Úallows_single_node_partitionÚ__marked_inductor_submod)r#   Úalways_return_tuple)Ú!torch.fx.passes.infra.partitionerrc   Ú torch.fx.passes.operator_supportrd   Ú!torch.fx.passes.utils.fuser_utilsre   Úobjectr0   r1   r_   r'   r;   r<   Ú
setdefaultÚsetÚaddr=   r>   r   rB   ro   r   r   rl   rm   rn   r^   ÚvaluesÚpropose_partitionsr7   )r"   rc   rd   re   Ú_DEFAULT_REGIONÚregionsrN   rT   Úridrp   Úall_partitionsrg   ÚsupportÚpartitionerÚ	partitions                  r   Úscoop_regionsz_RegionScooper.scoop_regionsˆ   ss  € åPÝFÝHô
 !›(ˆØ46ˆØ—H‘H—N‘Nò 
	9ˆDÜ& tÕ,Ø $§	¡	¨(Ñ 3Ð4KÑ L�ä˜}¬dÔ3Ø)¨]Ñ:à'Ð(9Ñ:‘Cà)�CØ×"Ñ" 3¬«Ó.×2Ñ2°4Õ8ð
	9ñ Ü�K‰KÐ8Ô9ØˆIð	%ÜœeŸh™hŸm™mÑ,ð	%ä”wœs¤E§H¡H§O¡OÐ3Ñ4´e·h±h·m±mÐDÄdÐJÑKó	%ð ACˆØ#ŸN™NÓ,ò 	7ˆLÙ'©°lÓ(CÓDˆGÙ4Ø�G¸$ôˆKð )×;Ñ;Ó=ò 7�	Ø×%Ñ% i§o¡oÕ6ñ7ð	7ñ "ØØØ-Ø $ô	
ð 	
r   NÚ
_processedc                 ó�  — |€
t        «       }| j                  j                  d¬«      D ]‡  }t        |«      rŒt	        | |j
                  «      }t        |t        j                  j                  «      sŒJt        |«      |vsŒX|j                  t        |«      «       t        j                  ||«       Œ‰ t        j                  | «      S )NÚget_attr©r2   )ry   r0   Ú
find_nodesr_   r9   r3   r;   r   rB   ÚGraphModuleÚidrz   ra   Úrecursively_scoop_regionsr„   )r"   r…   rN   rQ   s       r   rŒ   z(_RegionScooper.recursively_scoop_regions¿   s¦   € ð ÐÜ›ˆJØ—H‘H×'Ñ'¨:Ð'Ó6ò 	MˆDÜ& tÔ,ð Ü˜R §¡Ó-ˆFô ˜6¤5§8¡8×#7Ñ#7Õ8Ü�v“J jÒ0à—‘œr &›zÔ*Ü×8Ñ8¸ÀÕLð	Mô ×+Ñ+¨BÓ/Ð/r   c                 ó¬   — t         j                  j                  j                  d¬«      5  t        j                  |«      cd d d «       S # 1 sw Y   y xY w©NF©Úenable)r   rB   rC   Úpreserve_node_metara   rŒ   ©Úselfr"   s     r   Ú__call__z_RegionScooper.__call__Ö   sD   € Ü�X‰X×Ñ×2Ñ2¸%Ð2Ó@ñ 	@Ü!×;Ñ;¸BÓ?÷	@÷ 	@ò 	@úó   «A
Á
Ar   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústaticmethodr   rB   rŠ   r„   ry   ÚintrŒ   r”   r   r   r   ra   ra   ƒ   s³   „ ñð ð4
˜%Ÿ(™(×.Ñ.ð 4
°5·8±8×3GÑ3Gò 4
ó ð4
ðl à@Dñ0Ø�H‰H× Ñ ð0Ø.1°#©h¸©oð0à	�‰×	Ñ	ò0ó ð0ð,@˜5Ÿ8™8×/Ñ/ð @°E·H±H×4HÑ4Hô @r   ra   c                   óD  — e Zd ZdZedej                  j                  dej                  j                  fd„«       Zedej                  j                  dej                  j                  fd„«       Z	dej                  j                  dej                  j                  fd„Z
y)Ú_RegionCompilerz+
    Compiles the scooped out regions.
    r"   r   c                 óŠ   — ddl m} t        | d«      } | j                  j	                   |«       «       | j                  «        | S )Nr   )Ú_BoxedCodeGenrr   )Útorch.fx.graphrŸ   rZ   r0   Úset_codegenrM   )r"   rŸ   s     r   Úcompile_regionz_RegionCompiler.compile_regionà   s4   € å0ä˜RÐ!;Ó<ˆØ
�‰×Ñ™]›_Ô-Ø
�‰ŒØˆ	r   c                 ó  — d}| j                   j                  d¬«      D ][  }t        | |j                  «      }t	        |t
        j                  j                  «      sŒ>|j                  j                  d«      sŒZd}Œ] | j                   j                  d¬«      D ]R  }t        | |j                  «      }t	        |t
        j                  j                  «      sŒ>t        j                  |«       ŒT |rt        j                  | «      S | S )NFr&   rˆ   rr   Tr‡   )r0   r‰   r9   r3   r;   r   rB   rŠ   r4   r�   Úrecursively_compile_regionsr¢   )r"   Úfound_regionrN   rQ   s       r   r¤   z+_RegionCompiler.recursively_compile_regionsé   sØ   € ð ˆØ—H‘H×'Ñ'¨=Ð'Ó9ò 	(ˆDÜ˜R §¡Ó-ˆFÜ˜&¤%§(¡(×"6Ñ"6Õ7Ø—;‘;×)Ñ)Ð*DÕEØ#'‘Lð		(ð —H‘H×'Ñ'¨:Ð'Ó6ò 	DˆDÜ˜R §¡Ó-ˆFÜ˜&¤%§(¡(×"6Ñ"6Õ7Ü×;Ñ;¸FÕCð	Dñ
 Ü"×1Ñ1°"Ó5Ð5Øˆ	r   c                 ó¬   — t         j                  j                  j                  d¬«      5  t        j                  |«      cd d d «       S # 1 sw Y   y xY wrŽ   )r   rB   rC   r‘   r�   r¤   r’   s     r   r”   z_RegionCompiler.__call__ý   sD   € Ü�X‰X×Ñ×2Ñ2¸%Ð2Ó@ñ 	CÜ"×>Ñ>¸rÓB÷	C÷ 	Cò 	Cúr•   N)r–   r—   r˜   r™   rš   r   rB   rŠ   r¢   r¤   r”   r   r   r   r�   r�   Û   s�   „ ñð ð˜5Ÿ8™8×/Ñ/ð °E·H±H×4HÑ4Hò ó ðð ð¨¯©×(<Ñ(<ð ÀÇÁ×AUÑAUò ó ðð&C˜5Ÿ8™8×/Ñ/ð C°E·H±H×4HÑ4Hô Cr   r�   c                 ó¢   — t         j                  j                  j                  d¬«      5   t	        «       | «      cd d d «       S # 1 sw Y   y xY wrŽ   )r   rB   rC   r‘   ra   ©r"   s    r   Ú_create_inductor_marked_regionsr©     s?   € Ü	�‰×	Ñ	×	.Ñ	.°eÐ	.Ó	<ñ $ØŒ~Ó Ó#÷$÷ $ò $úó   «AÁAc                 ó¢   — t         j                  j                  j                  d¬«      5   t	        «       | «      cd d d «       S # 1 sw Y   y xY wrŽ   )r   rB   rC   r‘   r�   r¨   s    r   Ú _compile_inductor_marked_regionsr¬     s?   € Ü	�‰×	Ñ	×	.Ñ	.°eÐ	.Ó	<ñ %Ø ŒÓ  Ó$÷%÷ %ò %úrª   F)Úis_backward_compatibleÚexample_argsc                 ó(  — t         j                  j                  j                  d¬«      5  t	        | «      } t        | «      } t         j                  j                  j                  rddl	m
}  || «      cddd«       S | cddd«       S # 1 sw Y   yxY w)aÊ  
    Scoops out inductor marked regions and compiles them with inductor.

    Inductor options should be provided via the annotation API::

        with fx_traceback.annotate(
            {
                "compile_with_inductor": {
                    "inductor_configs": {
                        "max_autotune": True,
                        "triton.cudagraphs": False,
                    }
                }
            }
        ):
            ...
    Fr�   r   )ÚRegionalOutputCodeN)r   rB   rC   r‘   r©   r¬   r   r   Úforce_autograd_cacheÚtorch._inductor.output_coder°   )r"   r®   r°   s      r   r   r     sw   € ô2 
�‰×	Ñ	×	.Ñ	.°eÐ	.Ó	<ñ Ü,¨RÓ0ˆÜ-¨bÓ1ˆÜ×Ñ×"Ñ"×7Ò7ÝFá% bÓ)÷ñ ð ÷÷ ò ús   «ABÁ=BÂB)$Ú
contextlibr   ÚloggingÚcollections.abcr   r   r   Útypingr   r   r   r	   r
   r   Útorch.fx._compatibilityr   Ú	getLoggerr–   r=   Ú__all__r   Úcontextmanagerr!   rB   rŠ   rl   rZ   ro   r^   r_   ra   r�   r©   r¬   rw   r   r   r   r   ú<module>r»      s—  ðÛ Û Û ß 7Ñ 7ß *Ñ *ñ ˆtƒ_€ÙˆTƒ]€ã Ý 1ð 
ˆ×	Ñ	˜8Ó	$€àÐ
€ð�x  B Ñ'ð ¨H°R¸°VÑ,<ó ð ×Ñð°¸±ò ó ððM˜Ÿ™×,Ñ,ð M°cð M¸e¿h¹h×>RÑ>Ró Mð` %§(¡(§-¡-ð °Dó ÷U@ñ U@÷p$Cñ $CðN$¨¯©×(<Ñ(<ð $ÀÇÁ×AUÑAUó $ð
%¨¯©×)=Ñ)=ð %À%Ç(Á(×BVÑBVó %ñ
  eÔ,ðØ�‰×ÑðØ-3ðà
‡X�X×Ñòó -ñr   