
    VhW                        U d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlmZm	Z	 d dl
mZmZ d dlmZ dZe G d d             Ze G d d	e             Ze G d
 de             Ze G d de             Zg g  eddd       ed      gg  ed       ed       eddd       eddd       eddd       eddd       ed      gdZeeee   f   ed<    ej2                         aeeef   ed<   i Zeeef   ed<   d*d Z ej<                  d      d!edee   fd"       Zej                   G d# d$             Z  G d% d&      Z!d*d'Z"de#fd(Z$ e$       e!_%        e&d)k(  r e"        yy)+    N)	dataclassfield)CallableOptional)	cache_dirbisectc                       e Zd ZU eed<   y)	SubsystemnameN)__name__
__module____qualname__str__annotations__     Q/home/dcms/DCMS/lib/python3.12/site-packages/torch/_inductor/compiler_bisector.pyr
   r
      s    
Ir   r
   c                       e Zd Zy)BisectSubsystemNr   r   r   r   r   r   r   r          r   r   c                       e Zd Zy)BinarySubsystemNr   r   r   r   r   r      r   r   r   c                   P    e Zd ZU  ed      Zeed<   eed<   eed<   eed<   d	dZy)
ConfigChangeF)initr   config_nameconfig_fieldconfig_valueNc                 D    | j                    d| j                   | _        y )N_)r   r   r   )selfs    r   __post_init__zConfigChange.__post_init__)   s"    ''($*;*;)<=	r   returnN)	r   r   r   r   r   r   r   objectr#   r   r   r   r   r   "   s(    5!D#!>r   r   aot_eager_decomp_partitioncseFdecompositionjoint_graph_passespost_grad_passesinductorfallback_randomTemulate_precision_castslayout_optimizationcomprehensive_padding	lowerings)eager	aot_eagerr'   #aot_eager_decomp_partition_crossrefr,   BACKENDSsubsystem_call_countercall_counter_debug_infor%   c                  T    t         j                          t        j                          y N)r6   clearr7   r   r   r   reset_countersr;   M   s      "!!#r   env_strc                 B    t         j                  j                  | d       S r9   )osenvironget)r<   s    r   get_env_valrA   R   s    ::>>'4((r   c                   X    e Zd ZU dZeed<   dZee   ed<   dZee	   ed<   dZ
ee   ed<   y)BisectionResulta:  
    backend: torch.compile backend responsible for failure
    subsystem: optional, registered component identified for failure
    bisect_number: optional, number of times the subsystem needed to be applied to trigger failure
    debug_info: associated info of the triggering bisect application of subsystem
    backendN	subsystembisect_number
debug_info)r   r   r   __doc__r   r   rE   r   rF   intrG   r   r   r   rC   rC   W   s8     L#Ix}##'M8C=' $J$r   rC   c                   .   e Zd ZU dZdZeed<   dZee	   ed<   e
de	fd       Ze
de	d	ee	   ddfd
       Ze
de	dee	   fd       Ze
de	dede	ddfd       Ze
de	de	dee	ef   ddfd       Ze
de	de	ddfd       Ze
de	de	dededdf
d       Ze
dee	   fd       Ze
dee	   fd       Ze
de	de	defd       Ze
de	de	dee	   fd       Ze
de	de	deeef   fd       Ze
de	deddfd       Ze
de	deee	ef      fd       Ze
d/d        Z e
d0d!e	d"edefd#       Z!e
	 d1de	de	d$ee"g e	f      defd%       Z#e
d&e	d'edee   fd(       Z$e
d&e	dee	   fd)       Z%e
	 d0d&e	d'ed*e"g ef   d+edef
d,       Z&e
d/d-       Z'e
	 d2d*e"g ef   d+edee(   fd.       Z)y)3CompilerBisectora  
    This class iteratively runs torch.compile backends (eager, aot_eager, inductor) to find the
    first backend that can repro an issue.

    Once it discovers the offending backend it will iteratively disable subsystems within the backend.
    For subsystems which are applied repeatedly, such as the number of post grad passes or number
    of lowering of nodes to inductor ir, it will bisect to find the offending application.

    The idiomatic way to run it is with `do_bisect`. You can also use it by setting the env flags
    `TORCH_BISECT_BACKEND`, `TORCH_BISECT_SUBSYSTEM` and `TORCH_BISECT_MAX`.

    It also supports a CLI interface, although this is less well tested.

    You must run python compiler_bisector.py [start | good | bad | end]
    Fbisection_enabledNin_process_cacher%   c                 X    | j                   s
t               n| j                    dt         S )N/)rM   r   SUBDIR_NAME)clss    r   get_dirzCompilerBisector.get_dir{   s)    %(%9%9)+s?S?STTUVaUbccr   	file_pathlinesc                     t        j                  t         j                  j                  |      d       t	        |d      5 }|j                  |       d d d        y # 1 sw Y   y xY w)NT)exist_okw)r>   makedirspathdirnameopen
writelines)rQ   rS   rT   files       r   write_lines_to_filez$CompilerBisector.write_lines_to_file   sJ    
BGGOOI.>)S! 	#TOOE"	# 	# 	#s   AA%c                     t         j                  j                  |      r%t        |      5 }|j	                         cd d d        S g S # 1 sw Y   g S xY wr9   )r>   rY   existsr[   	readlines)rQ   rS   r]   s      r   read_lines_from_filez%CompilerBisector.read_lines_from_file   sD    77>>)$i (D~~'( (	(	s   AAbackend_namerE   	run_statec                 6   t         j                  j                  | j                         ||j                   d      }t        |t              r:|dk(  sJ | j                  ||j                  |j                  |j                  i       | j                  ||g       y )N_run_state.txttest_disable)r>   rY   joinrR   r   
isinstancer   set_config_valuesr   r   r^   )rQ   rc   rE   rd   rS   s        r   update_run_statez!CompilerBisector.update_run_state   s     GGLLKKM<INN+;>)J
	 i....!!'')?)?@ 		I;7r   rD   config_datac                     t         j                  j                  | j                         || d      }|j	                         D cg c]  \  }}| d| d }}}| j                  ||       y c c}}w )N_config.txt=
)r>   rY   rh   rR   itemsr^   )rQ   rD   rE   rl   rS   kvrT   s           r   rj   z"CompilerBisector.set_config_values   sh     GGLLI;k9RS	*5*;*;*=>$!QA3as">>	51 ?s   A.subsystem_namec                     t        |t              sJ t        j                  j	                  | j                         d      }d| dd| dg}| j                  ||       y )Nbisect_status.txtbackend=rp   
subsystem=ri   r   r>   rY   rh   rR   r^   )rQ   rc   rt   rS   rT   s        r   update_bisect_statusz%CompilerBisector.update_bisect_status   sZ    .#...GGLL0CD	L>,
>:J".MN	51r   lowhighc                     t        |t              sJ t        j                  j	                  | j                         || d      }d| dd| dg}| j                  ||       y )N_bisect_range.txtlow=rp   high=ry   )rQ   rc   rt   r{   r|   rS   rT   s          r   update_bisect_rangez$CompilerBisector.update_bisect_range   si     .#...GGLLKKM<N+;;L)M
	 uB5b!12	51r   c                    t        d      x}r|S t        j                  j                  | j	                         d      }| j                  |      }|D ]7  }|j                  d      s|j                         j                  d      d   c S  y)z4
        Returns the active backend, if any
        TORCH_BISECT_BACKENDrv   rw   ro      N	rA   r>   rY   rh   rR   rb   
startswithstripsplit)rQ   valrS   rT   lines        r   get_backendzCompilerBisector.get_backend   s    
 45535JGGLL0CD	((3 	2Dz*zz|))#.q11	2 r   c                 (   t        d      x}r|S t        j                  j                  | j	                         d      }| j                  |      }|D ]?  }|j                  d      s|j                         j                  d      d   }|r|c S dc S  y)z6
        Returns the active subsystem, if any
        TORCH_BISECT_SUBSYSTEMrv   rx   ro   r   Nr   )rQ   r   rS   rT   r   outs         r   get_subsystemzCompilerBisector.get_subsystem   s     67737JGGLL0CD	((3 	,D|,jjl((-a0!s+t+	, r   c                 :    t        fdt        |   D              S )Nc              3   B   K   | ]  }|j                   k(  s|  y wr9   r   ).0objrt   s     r   	<genexpr>z8CompilerBisector.get_subsystem_object.<locals>.<genexpr>   s     XCSXX=WCXs   )nextr5   )rQ   rc   rt   s     `r   get_subsystem_objectz%CompilerBisector.get_subsystem_object   s    X8L#9XXXr   c                     t         j                  j                  | j                         || d      }| j	                  |      }|r|d   j                         }|dv sJ |S y)z@
        Returns the current stage of bisecting, if Any
        rf   r   )rg   find_max_boundsr   N)r>   rY   rh   rR   rb   r   )rQ   rc   rt   rS   rT   r   s         r   get_run_statezCompilerBisector.get_run_state   sg     GGLLKKM<N+;>)J
	 ((3(.."CGGGGJr   c                    t         j                  j                  | j                         || d      }| j	                  |      }d }d }t        |      D ]  }|j                  d      r,t        |j                         j                  d      d         }n<|j                  d      r+t        |j                         j                  d      d         }|| n ||t        d|       ||fS )Nr~   r   ro   r   r   z9Trying to get bisect range when it is not set: subsystem )r>   rY   rh   rR   rb   reversedr   rI   r   r   RuntimeError)rQ   rc   rt   rS   rT   r{   r|   r   s           r   get_bisect_rangez!CompilerBisector.get_bisect_range   s     GGLLKKM<N+;;L)M
	 ((3UO 	Dv&$**,,,S1!45)4::<--c21564#3	 ;$,KNK[\  Dyr   c                     t         j                  j                  | j                         ||j                   d      }d|j
                   dd|j                   dd|j                   dg}| j                  ||       y )Nrn   zconfig_name=rp   zconfig_field=zconfig_value=)	r>   rY   rh   rR   r   r   r   r   r^   )rQ   rD   rE   rS   rT   s        r   update_config_changez%CompilerBisector.update_config_change  s|    GGLLINN;K;9WX	90014I22326I22326

 		51r   r   c                    | j                         }| j                         }|r|sy t        j                  j	                  | j                         || d      }t        j                  j                  |      sy | j                  |      }i }|D ]3  }|j                         j                  dd      \  }}	t        |	      ||<   5 |S )Nrn   ro   r   )r   r   r>   rY   rh   rR   r`   rb   r   r   eval)
rQ   r   rD   rE   rS   rT   rl   r   keyvalues
             r   get_config_changez"CompilerBisector.get_config_change  s    //#%%'	iGGLLI;k9RS	ww~~i(((3 	+D++C3JC#E{K	+ r   c                     | j                   r| j                   n| j                         }t        j                  j	                  |      r!t        j                  |       t        d       y t        d       y )NzBisection status deleted.zNo bisection status found.)rM   rR   r>   rY   r`   shutilrmtreeprint)rQ   dir_names     r   delete_bisect_statusz%CompilerBisector.delete_bisect_status$  sM     ,/+?+?3''S[[]77>>(#MM(#-../r   r   	incrementc                 >    t         |   }|rt         |xx   dz  cc<   |S )Nr   )r6   )rQ   r   r   currs       r   get_system_counterz#CompilerBisector.get_system_counter.  s&     &d+"4(A-(r   rG   c           	         | j                   sy| j                         |k7  ry| j                         |k7  ryt        d      x}r!| j	                  |d      }|t        |      kD  S | j                  ||      }|dk(  ry|dk(  r&| j                  ||d| j	                  |d             y|dk(  sJ | j                  ||      \  }}||z   d	z  }	| j	                  |      }
|
|k\  r|
|k  r||z
  d	k  r| |       t        |
<   |
|	kD  S )
NFTORCH_BISECT_MAXT)r   rg   r   r   r      )
rL   r   r   rA   r   rI   r   r   r   r7   )rQ   rD   rE   rG   r   counterrd   r{   r|   midpointcall_counters              r   disable_subsystemz"CompilerBisector.disable_subsystem6  s>    $$??')+01131,,Y$,GGSX%%%%gy9	&++##&&yD&A	 (((,,Wi@ICd
q(H11)<L # D(4ZA%*8B'5(**r   curr_backendcurr_subsystemc                 l   t        dj                   d       t        |   }t        fdt	        |      D              }|t        |      dz
  k  rT||dz      }| j                  ||j                         | j                  ||d       t        d| d|j                          |S t        d| d	       y
)zP
        Tries to move to the next subsystem within the current system.
        
Disabling z did not fix the issue.c              3   Z   K   | ]"  \  }}|j                   j                   k(  r| $ y wr9   r   )r   irE   r   s      r   r   z5CompilerBisector.advance_subsystem.<locals>.<genexpr>s  s/      '
9~~!4!44 '
s   (+r   rg   zMoving to the next subsystem:  - zAll subsystems in z4 have been checked. The issue is not in this system.N)r   r   r5   r   	enumeratelenrz   rk   )rQ   r   r   current_subsystemscurrent_subsystem_indexnext_subsystems     `   r   advance_subsystemz"CompilerBisector.advance_subsystemi  s     	
>..//FGH%l3"& '
 )*< ='
 #
 #S);%<q%@@/0G!0KLN$$\>3F3FG  ~~N0c.BUBUAVW "!$\N2fg r   c                    t        t        j                               j                  |      }|t	        t              dz
  k  rEt        t        j                               |dz      }| j                  |d       t        d|        |S y)z1
        Tries Move to the next backend.
        r    zMoving to the next system: N)listr5   keysindexr   rz   r   )rQ   r   current_system_indexs      r   advance_backendz CompilerBisector.advance_backend  su    
  $HMMO4::<H#h-!"3301E1IJL$$\26/~>?r   fncli_interfacec                    t        |t              sJ 	 | j                  ||j                        }t	                |dk(  r |       s| j                  ||      }|sy|}nt        |t              r4t        d|j                   d|j                   d|j                   d       nt        d|j                   d	       t        |t              ryt        d
       | j                  ||d       nn|dk(  ri |       rt        d| d|j                   d      | j                  ||j                        \  }}t        d| d| d       | j                  ||d       n |dk(  r| j                  ||j                        \  }	}|	|z   dz  }
t        d| d|j                   d|	 d| d|
 d        |       r"| j                  ||j                  |
dz   |       n| j                  ||j                  |	|
       | j                  ||j                        \  }	}|	|k(  rDt        d| d|j                   d|	 dt         j#                  |	d              yt        d|       |rt%        j&                  d       `)ze
        Process the current subsystem. Returns True if the issue is found, False otherwise.
        Trg   FzSetting config z field z to z fixed the issuer   z fixed the issue.z'Starting bisect by getting upper bound.r   z5Function succeeded with 'find_max_bounds' status for r   .zUpper bound of z found for r   r   z
Bisecting z
 (Range: [z, z], Midpoint: )r   zBinary search completed for z. The bisect number is z. Debug info: z	not foundzUnexpected run_state r   )ri   r
   r   r   r;   r   r   r   r   r   r   r   rk   r   r   r   r7   r@   sysexit)rQ   r   r   r   r   rd   r   r!   r|   r{   r   s              r   process_subsystemz"CompilerBisector.process_subsystem  s    .)444)),8K8KLIN*t%(%:%:<%XN)$%3N!.,?-n.H.H-IQ_QlQlPm n""0"="=!>>NP
 
>+>+>*??PQR!./B#CD(($n6G //4&OP\~]`aoatat`uuvw  "22<ATATUGAtOD6\N!LM((~xPh&00~?R?RS	T$J1, c.2E2E1FjQTPUUWX\W]]jksjttuv 4++$n&9&98a< ++$n&9&93  00~?R?RS	T$;6|nCH[H[G\\stwsx y''>'B'B3'T&UW  "%:9+#FGGs r   c                     t        t        t        j                                     }d}| j	                  ||       t        d|        y )Nr   z(Starting bisection process with system: )r   iterr5   r   rz   r   )rQ   r   r   s      r   initialize_systemz"CompilerBisector.initialize_system  s<    D12  ~>8GHr   c           	          |sl j                    j                          d _         t        j                          _        d fdt        j                         G fdd      } |       } j                         } j                         }|s4 j                           j                         }|J  j                         }| j                  ||      nd}	 |J t                |r j                  ||||      }|r j                  | j                               }t        |t              r"t        ||j                   d|j                         S  j#                  ||j                         \  }	}
t        ||j                   |	t$        j'                  |	d            S  j)                  ||      }|st+        d| d	       |J t        |      S |}n |       r" j-                  |      }|st+        d
       y|}nkt.        |   }|rF|d   } j1                  ||j                           j3                  ||d       t+        d| d|        nt+        d| d       t        |      S |rt5        j6                  d       )z~
        Run fn repeatedly attempting to bisect torch.compile. fn should return True on success and False on failure.
        TNc                  B     _         j                          d _        y r9   )rL   r   rM   )bisection_enabled_origrQ   s   r   cleanupz+CompilerBisector.do_bisect.<locals>.cleanup  s    (>%((*'+$r   c                       e Zd Zd fdZy)1CompilerBisector.do_bisect.<locals>.DisableBisectNc                 >             t        j                         y r9   )atexit
unregister)r"   r   cleanup_handlers    r   __del__z9CompilerBisector.do_bisect.<locals>.DisableBisect.__del__  s    I%%o6r   r$   )r   r   r   r   )r   r   s   r   DisableBisectr     s    7r   r   r   r   zThe issue is in the z* system, but could not identify subsystem.zAll systems have been checked.rg   z( system. Moving to the first subsystem: z system.r$   )rL   r   tempfilemkdtemprM   r   registerr   r   r   r   r;   r   ri   r   rC   r   r   r7   r@   r   r   r   r5   rz   rk   r   r   )rQ   r   r   r   _cleanupr   curr_subsystem_namer   resultr{   r!   r   next_backendr   r   r   r   s   `             @@@r   	do_bisectzCompilerBisector.do_bisect  s    %(%:%:"$$&$(C!#+#3#3#5C ,
 %oog6O7 7
 %H(!//1!!#??,L+++"%"3"3"5 #. $$\3FG 	
 +++.. ."M /  %(%=%=$))+&N
 "./B.(*//*//	   !11,@S@STFC*$&++/33C>	  "%!6!6|^!T%.|n<fg (333*<88!/4#&#6#6|#DL'>?##/L)1,)?&));A)>00~?R?RS,,(.. 2<.@hiwhxy  4\N(KL.|<<} r   r$   )Tr9   )F)*r   r   r   rH   rL   boolr   rM   r   r   classmethodrR   r   r^   rb   r
   rk   dictr&   rj   rz   rI   r   r   r   r   r   tupler   r   r   r   r   r   r   r   r   r   r   r   rC   r   r   r   r   rK   rK   f   s"     $t#&*hsm*d d d #C #S	 #d # #
 S T#Y   88+48AD8	8 8  22&)28<S&[8I2	2 2 2 2S 2T 2 2 22032:=2EH2	2 2 HSM   hsm    Y YS YY Y Y  c hsm   03	sCx 2 23 2< 2D 2 2 C HT#v+=N4O  ( 0 0 c d c   
 37	0+0+ 0+ Xb#g./	0+
 
0+ 0+d 09	)	 : 3 8C=    #DD "D RX	D
 D 
D DL I I ;@g"d(#g48g	/	"g gr   rK   c                  <   t        t        j                        dk  r t        d       t        j                  d       t               } t        j                  d   dk(  r%| j                          t        j                  d       dk(  r5| j                          | j                          t        j                  d       dvr t        d       t        j                  d       d	t        ffd
}| j                         st        d      | j                  |d       y )Nr   z3Usage: python bisect_update.py <start|end|good|bad>r   endr   start)goodbadz:Invalid command. Must be 'good', 'bad', 'start', or 'end'.r%   c                       dk(  S )Nr   r   )commands   r   test_functionz)command_line_usage.<locals>.test_functiond  s    &  r   z$Must call start prior to good or badTr   )r   r   argvr   r   rK   r   r   r   r   
ValueErrorr   )bisection_managerr   r   s     @r   command_line_usager   O  s    
388}qCD(*hhqkG%..0'..0++-o%JK!4 ! ((*?@@TBr   c                  ^    t         j                         d uxs t         j                         d uS r9   )rK   r   r   r   r   r   get_is_bisection_enabledr   m  s.    &&(4 	6'')5r   __main__r$   )'r   collectionsdataclasses	functoolsr>   r   r   r   r   r   typingr   r   'torch._inductor.runtime.cache_dir_utilsr   rP   r
   r   r   r   r5   r   r   r   r   Counterr6   rI   r7   r;   	lru_cacherA   rC   rK   r   r   r   rL   r   r   r   r   <module>r     s        	  
  ( % =     	i 	 	 	i 	 	 >? > >  	15%@	
# ,.,-	
 	Z!2D9Z!:DAZ!6>Z!8%@$
($sDO#
$ 6 *=)<)<)> S#X >*, c3h ,$
 T) )# ) ) % % %f fRC<$  &>%?  "z r   