
    qi                          d dl Z d dlZd dlZd dlmZ  ej
                  e      ZdgZd Z	d Z
d Zdej                  j                  fdZ G d	 d
      Z G d d      Zd Zd Z ed      d        Zy)    N)compatibilityregional_inductorc                 B     t        j                          fd       }|S )Nc                       | i |S N )argskwargsfns     g/home/ubuntu/crypto_trading_bot/.venv/lib/python3.12/site-packages/torch/fx/passes/regional_inductor.pyinnerz_dummy_wrapper.<locals>.inner   s    4"6""    )	functoolswraps)r   r   s   ` r   _dummy_wrapperr      s%    __R# # Lr   c                     ddl m} ddlm}  || |d      }|j	                         } ||j
                  |D cg c]  }|j                   c}|d      }|S c c}w )Nr   )CapabilityBasedPartitioner)fuse_by_partitionsT)allows_single_node_partition)prefixalways_return_tuple)!torch.fx.passes.infra.partitionerr   !torch.fx.passes.utils.fuser_utilsr   propose_partitionsgraph_modulenodes)	gmsupported_opsr   r   r   partitionercandidate_partitions	partitionpartitioned_gms	            r   _partition_by_supported_nodesr#      sc    LD,
MK '99;'  *>?Y? 	N  	@s   A
c                    ddl m} | j                  j                  D ]w  }|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.        j&                  j1                  ||dd      }d d d        t        |      sJ t3        |      }| j                  j5                  |      5  | j                  j7                  ||j8                  |j:                        }|j                  |_	        |j=                  |       | j                  j?                  |       | j@                  |j
                  = d d d        z | jC                          | S # 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aot)r	   r
   )""torch._inductor.standalone_compiler%   graphr   optarget
startswithall_input_nodeshasattrr'   appendRuntimeErrorgetattrget
isinstancedictloggerinfotorch._inductor.config	_inductorconfig
ValueErrorpatchtorchstandalone_compiler   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_submodrW   -   s   F =-77m#(>(>v(FK 00 8V,(--1G&&x}}U';<&QRZQ[\ 	 R-F
  ""LL.. 
"8V,1B1B8T1R%]]84F!&$/4Kv4U(./F(G&}d; 2m C/<=O/P,!
" KKB  =< ( 4$7u =W X  !&&'78 #oo@@K8NTX A  k+>???,[9O))$/ -8811#$))DKK 2  !%		**84##D)KK,- -m=-~ LLNI# - -s   &$J*BJ6*J3	6K 	rK   c                     | 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*   )r1   r5   r'   r9   )rK   s    r   _needs_inductor_compiler[   s   sR    00 	;D&!	;IIMM(D)	; $tyy'::	r   c                   6    e Zd ZdZed        Zed        Zd Zy)_RegionScooperzK
    Scoops out the inductor marked regions. It does NOT compile them.
    c                     ddl m} d}| j                  j                  D ]  }t	        |      sd} n |st
        j                  d       | S  G d d|      } |       }t        | |d      S )	Nr   )OperatorSupportFTzNo inductor marked nodes foundc                   D    e Zd Zdej                  j
                  defdZy)9_RegionScooper.scoop_regions.<locals>.InductorMarkedNodesrK   returnc                     t        |      S r   )r[   )self
submodulesrK   s      r   is_node_supportedzK_RegionScooper.scoop_regions.<locals>.InductorMarkedNodes.is_node_supported   s    .t44r   N)__name__
__module____qualname__rC   fxNodeboolrf   r   r   r   InductorMarkedNodesra      s    5%((-- 5D 5r   rm   __marked_inductor_submod) torch.fx.passes.operator_supportr_   r0   r   r[   r<   r=   r#   )r   r_   found_marked_noderK   rm   marked_nodess         r   scoop_regionsz_RegionScooper.scoop_regions   su    D!HHNN 	D&t,$(!	
 !KK89I	5/ 	5 +,,8
 	
r   c                 &   | j                   j                  d      D ]^  }t        |      rt        | |j                        }t        |t        j                  j                        sJt        j                  |       ` t        j                  |       S )Nget_attrr1   )r0   
find_nodesr[   r8   r2   r:   rC   rj   GraphModuler]   recursively_scoop_regionsrr   )r   rK   rN   s      r   rx   z(_RegionScooper.recursively_scoop_regions   sw    HH'':'6 	AD&t, R-F&%(("6"6788@	A ++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)rC   rj   	tracebackpreserve_node_metar]   rx   rd   r   s     r   __call__z_RegionScooper.__call__   sD    XX22%2@ 	@!;;B?	@ 	@ 	@   A

AN)rg   rh   ri   __doc__staticmethodrr   rx   r   r   r   r   r]   r]   |   s5     
 
, 0 0@r   r]   c                   6    e Zd ZdZed        Zed        Zd Zy)_RegionCompilerz+
    Compiles the scooped out regions.
    c                     ddl m} t        | d      } | j                  j	                   |              | j                          | S )Nr   )_BoxedCodeGenrn   )torch.fx.graphr   rW   r0   set_codegenrJ   )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&   ru   rn   Trt   )r0   rv   r8   r2   r:   rC   rj   rw   r3   r   recursively_compile_regionsr   )r   found_regionrK   rN   s       r   r   z+_RegionCompiler.recursively_compile_regions   s     HH''='9 	(DR-F&%(("6"67;;))*DE#'L		( HH'':'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rz   )rC   rj   r}   r~   r   r   r   s     r   r   z_RegionCompiler.__call__   sD    XX22%2@ 	C">>rB	C 	C 	Cr   N)rg   rh   ri   r   r   r   r   r   r   r   r   r   r      s5        &Cr   r   c                     t         j                  j                  j                  d      5   t	               |       cd d d        S # 1 sw Y   y xY wrz   )rC   rj   r}   r~   r]   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rz   )rC   rj   r}   r~   r   r   s    r    _compile_inductor_marked_regionsr      s?    				.	.e	.	< %  $% % %r   F)is_backward_compatiblec                     t         j                  j                  j                  d      5  t	        |       } t        |       } | cddd       S # 1 sw Y   yxY w)ae  
    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{   N)rC   rj   r}   r~   r   r   )r   example_argss     r   r   r      sK    " 
			.	.e	.	< ,R0-b1  s   AA)r   loggingrC   torch.fx._compatibilityr   	getLoggerrg   r<   __all__r   r#   rW   rj   rk   r[   r]   r   r   r   r   r   r   r   <module>r      s       1 
		8	$
&CL%((-- ,@ ,@^$C $CN$
%
 e, -r   