
    Ph_                        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Zd dl	m
Z
 d dlmZ d dlmZmZmZ d dlZd dlmZ d dlZd dlmZ d dlmZ d dlmZ d dlmZmZ d	d
lmZ d	dlm Z m!Z!  ejD                  e#      Z$ ed      Z% ed      Z&e&jO                         Z(e(rd dl)Z*g Z+dZ,e(reg dZ+e*jZ                  j\                  j^                  ja                         jc                  dd      Z2djg                  e+D  cg c]  } d|  d
 c}       Z,g dZ4 G d d      Z5d Z6dZ7 G d d      Z8 ejr                  d      d        Z:dddZ;d  Z<d! Z= G d" d#e>      Z?d$ Z@d;d%ZA	 d<ddd&d'ZBd( ZCd) ZDd* ZE	 d<ddd&d+ZFd,ed-   d.d/d0d-fd1ZGd2e%d0eee%   ge%f   fd3ZH eHej                        ZJ eH ej                  d4            ZL eHd       ZM eHd      ZN eHd      ZO G d5 d6      ZP G d7 d8      ZQ G d9 d:      ZRyc c} w )=    N)Counter)import_module)CallableOptionalTypeVar)rand_strided)is_float_dtype)StorageWeakRef)ContentStoreReaderContentStoreWriter   )config)clone_inputsget_debug_dirTztorch._inductor.config )z1//caffe2/torch/fb/sparsenn:sparsenn_operators_gpuz-//caffe2/torch/fb/sparsenn:sparsenn_operatorsz///deeplearning/fbgemm/fbgemm_gpu:sparse_ops_cpuz+//deeplearning/fbgemm/fbgemm_gpu:sparse_opszfbcode://
ztorch.ops.load_library("z"))buck2runz@mode/dev-nosanc                        e Zd Zd Zd ZddZy)BuckTargetWriterc                 .   t         j                  j                  t         j                  j                  |            \  | _        | _        | j
                  j                  dd      | _        | j                  j                  dd       d| j                   | _        | j                  | j                  j                  d      d  | _        | j                  dd  | _        | j                  }||j                  d      d  dd  }d| d	| j                   | _	        y )
Nz.pyr   /.zfbcode.   zfbcode/r   :)
ospathsplitabspathsubdirpy_filereplacetargetfindcmd_line_path)selffilenametmps      dC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/_dynamo/debug_utils.py__init__zBuckTargetWriter.__init__9   s    $&GGMM"''//(2K$L!T\ll**5"5 {{**345Qt{{mD	IIdiinnY79:	IIabM	 kk#((9%'(,!#a}5    c                     dj                  t        D cg c]  }d| d
 c}      }t        j                  d| j                   d| j
                   dt         d| d| j                   d	      S c c}w )
Nr   z	        "z",za
load("@fbcode_macros//build_defs:python_binary.bzl", "python_binary")

python_binary(
    name="z",
    srcs = ["z"],
    compile = False,
    deps = [
        "//caffe2:torch",
        "//caffe2/functorch:functorch",
        "//triton:triton",
        "z",
    ],
    cpp_deps = [
z
    ],
    main_module = "z",
)
)join
extra_depstextwrapdedentr%   r#   
cur_targetr   )r(   xextra_cpp_depss      r+   buildzBuckTargetWriter.buildG   s    z#Jz!is"$5z#JK ;;- ll^ 
     II; !
 	
 $Ks   A/c                 P   t         j                  j                  | j                  d      }t	        |d      5 }|j                  | j                                d d d        t        | j                  gz   }|r%t        j                  ddj                  |             |S # 1 sw Y   FxY w)NTARGETSwzFFound an example that reproduces the error. Run this cmd to repro - %s )r   r   r/   r"   openwriter6   BUCK_CMD_PREFIXr'   logwarning)r(   	print_msgtarget_filefd	cmd_splits        r+   r<   zBuckTargetWriter.write_   s    ggll4;;	:+s#rHHTZZ\" $ $t'9'9&::	KKX#  $#s    BB%N)T)__name__
__module____qualname__r,   r6   r<    r-   r+   r   r   8   s    6
0r-   r   c                     t         j                  j                  t               d      } | +t	        j
                          dt        j                          } t         j                  j                  |       st        j                  | d       | S )Nminifierz
/minifier_T)exist_ok)
r   r   r/   r   tempfile
gettempdirgetpassgetuserexistsmakedirs)r   s    r+   minifier_dirrQ   m   sb    77<<4D|%%'(
7??3D2EF77>>$
D4(Kr-      c                      e Zd Zej                  j
                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  ej                  j                  ej                  j                   ej                  j"                  ej                  j$                  ej                  j&                  ej                  j(                  ej                  j*                  ej                  j,                  ej                  j.                  ej                  j0                  ej                  j2                  gZed        Zed        Zy)NNModuleToStringc                     t               }| j                         D ]2  \  }}t        |      t        j                  vs"|j                  |       4 t        |      dkD  rt        j                  d|       y)Nr   z-We have not tested reprs of some modules - %sT)	setnamed_childrentyperT   
safe_reprsaddlenr>   r?   )gmcant_convert_modules       r+   can_convert_to_stringz&NNModuleToString.can_convert_to_string   sb    u**,IAvF|#3#>#>>  ( - |q KKGVr-   c                    ddl m} d}t        j                  d      }| j	                         D ]T  \  }}|j                          }t        |j                         d       }||j                  r| d}||dz   d| d| d	z  }V | j                  j                         D ]  \  }}	|		|	j                         t        k  r'dd
lm}
 |
j                  t        k\  sJ t!        |	      }nbt#        j$                  |	      r'dt'        |	j(                         d|	j*                   d}n&dt'        |	j(                         d|	j*                   d}|	j                  r| d}||dz   d| d| dz  } | j,                  j                         D ]Q  \  }}|	d}|j                  rd}dt'        |j(                         d|j*                   | d}||dz   d| d| d	z  }S | || j.                  d       d	z  }|S )Nr   )
_addindent    z
            from torch.nn import *
            class Repro(torch.nn.Module):
                def __init__(self):
                    super().__init__()
            z.cuda()   zself.z = r   )
PRINT_OPTSztorch.randn(z, dtype=)ztorch.randint(1, size=zself.register_buffer('z', z)
r   z, device="cuda"ztorch.nn.Parameter(torch.randn(z))rR   )torch.nn.modules.modulerb   r1   r2   rW   __repr__next
parametersis_cuda_buffersitemsnumelMAX_CONSTANT_NUMEL_INLINEtorch._tensor_strre   	thresholdreprtorchis_floating_pointlistshapedtype_parameterscode)r\   rb   tab	model_strmodule_namer_   
module_strexample_parambuffer_namebufferre   
tensor_str
param_nameparammaybe_devices                  r+   convertzNNModuleToString.convert   s)   6OO
	 $&#4#4#6K"OO-.J !!2!2!4d;M(]-B-B *|73
CE7%}C
|2FFI $7 $&;;#4#4#6K~||~!::8!++/HHHH!&\
((0+D,>+?x~UVW
 -T&,,-?,@VWX  ~~ *|73
CE7"8STWXXI# $7& "$!5!5!7J}L}}0:4;L:MXV[VaVaUbcobpprsJCE7%
|3zl"EEI "8  	
277A./r22	r-   N)rD   rE   rF   rs   nnLinearConv1dConv2dConv3dBatchNorm1dBatchNorm2dBatchNorm3d	LayerNormDropoutSoftmaxReLUGELUIdentity	MaxPool2d	EmbeddingTanhConvTranspose1dGLULSTMFlattenAdaptiveAvgPool2drY   staticmethodr`   r   rG   r-   r+   rT   rT   y   s0     ""+J0 	 	 ; ;r-   rT   c                     t         j                  j                         syd} 	 t        j                  ddg      }|j                         j                  d      }dj                  |D cg c]  }|dvsd| d	 c}      }| | dz  } t        d t        t         j                  j                               D              }| dz  } |j                         D ]  \  }}| d| d| d	z  }  | dz  } | S c c}w # t        $ r | d
z  } Y |w xY w)Nz:# torch.cuda.is_available()==False, no GPU info collected
z# CUDA Info: 
nvccz	--versionr   r   )r   z# z 
z# nvcc not found
c              3   Z   K   | ]#  }t         j                  j                  |       % y wN)rs   cudaget_device_name).0is     r+   	<genexpr>z,_cuda_system_info_comment.<locals>.<genexpr>   s$      /O!

""1%/Os   )+z# GPU Hardware Info: 
z : )rs   r   is_available
subprocesscheck_outputdecoder    r/   FileNotFoundErrorr   rangedevice_countrm   )r{   cuda_version_outcuda_version_linesscomment	gpu_namesnamecounts           r+   _cuda_system_info_commentr      s#   ::""$L!I*%22FK3HI-446<<TB''0BT0B1atmRs#;0BTUy^#	  /4UZZ5L5L5N/O I **I (er$s5'--	 )I U *))	*s*   AC5 (	C02C0:C5 0C5 5DDF)stable_outputc           	      Z   dd l }dd l}| ryd|j                  j                  j	                          d|j
                  j                  j	                          d|j                  j                  j	                          d|j                  j                  j                  j	                          d	S )Nr   z*# config omitted due to stable_output=Truez~import torch._dynamo.config
import torch._inductor.config
import torch._functorch.config
import torch.fx.experimental._config
r   )
torch._functorch.configtorch._inductor.config_dynamor   codegen_config	_inductor
_functorchfxexperimental_config)r   rs   s     r+   generate_config_stringr      s    "!;
 $$& ' (&&( ) *'') * +--/ 0 1	 	r-   c                  R    t         j                  j                  t               d      S )Nzminifier_launcher.py)r   r   r/   rQ   rG   r-   r+   get_minifier_repro_pathr   	  s    77<<(>??r-   c                 L   t               }t        j                  d|       t        rt	        |      j                          	 t        |d      5 }|j                  |        d d d        y # 1 sw Y   y xY w# t        $ r&}t        j                  |       t        d      |d }~ww xY w)NzWriting minified repro to:
%sr9   z(Could not write to {minified_repro_path})
r   r>   r?   use_buckr   r<   r;   OSError	exceptionNotImplementedError)contentsminified_repro_pathrB   es       r+   helper_for_dump_minifyr     s    13KK02EF,-335U%s+rHHX ,++  Ua!"LMSTTUs6   A4 A(A4 (A1-A4 1A4 4	B#=!BB#c                       e Zd Zy)AccuracyErrorN)rD   rE   rF   rG   r-   r+   r   r     s    r-   r   c                     t        |       }t        t        |             D ]A  }t        ||   t        j
                        s!||   j                  | |   j                         C |S )z
    This clone inputs is different from utils clone_input. In case of minifier,
    all the tensors are leaf tensors while creating a new graph. So, we set the
    requires_grad field w/o checking the leafness of the tensor.
    )r   r   r[   
isinstancers   Tensorrequires_grad_requires_grad)example_inputscloned_inputsidxs      r+   clone_inputs_retaining_gradnessr      s[     !0MS()mC(%,,7#--nS.A.O.OP * r-   c                    ddl m} ddlm}m}m} t        j                  |       } |st        |      }t        | d      r| j                  d       t        | dd      }t        | d	d      }	t        | d
      s||	 ||       } ||| _        |	|	| _         | |      }
|r|
S  ||
      r ||
      }|j                           || |
d|      S )z
    Runs a forward and possibly backward iteration for a given mod and args.

    When disable_clone is True, we will use args as-is without cloning.
    This is higher fidelity but we may destroy the args in the process.
    r   )make_boxed_funcr   )collect_resultsreduce_to_scalar_lossrequires_bwd_pass	zero_gradTnamed_parametersNnamed_buffers_boxed_call)torch._functorch.aot_autogradr   testingr   r   r   copydeepcopyr   hasattrr   getattrr   r   backward)r\   argsonly_fwddisable_cloner   r   r   r   orig_named_parametersorig_named_buffersoutlosss               r+   run_fwd_maybe_bwdr   -  s     >RR	r	B.t4r;
T $B(:DA _d;2}%)-?-KR  ,"7B)1B
T(C
$S)2sD$//r-   require_fp64ignore_non_fpc                   ddl m} ddlm}m} ddlm}	 t        | |      r ||       | _         ||       | _	        t        ||      r ||      |_         ||      |_	        t        | ||      }
d}t        j                  r9	 t        t        j                  |       t!        |            \  }}t        |||      }	 t        |||      } |	|
||t        j,                  d|	      }|S # t"        $ r% |rt%        d      t&        j)                  d       Y Yw xY w# t"        $ r}t&        j+                  d       Y d}~yd}~ww xY w)
aa  
    Check two models have same accuracy.

    require_fp64: if True, raise an error if we unable to calculate the fp64 reference
    ignore_non_fp: if True, do not compare outputs which are not floating point.  This
        is mostly useful for the minifier (which wants to avoid quantizing floating point
        error into integer/boolean error)
    r   )OptimizedModule)"named_buffers_for_optimized_module%named_parameters_for_optimized_module)sameNzCould not generate fp64 outputszWhile minifying the program in accuracy minification mode, ran into a runtime exception which is likely an unrelated issue. Skipping this graph.T)tol	equal_nanr   )
eval_framer   r   r   r   utilsr   r   r   r   r   r   same_two_models_use_fp64cast_to_fp64r   r   r   	ExceptionRuntimeErrorr>   r?   r   repro_tolerance)r\   opt_gmr   r   r   r   r   r   r   r   reffp64_ref
fp64_modelfp64_examplesresr   passings                    r+   same_two_modelsr  T  sA   " , "o&CBG=bA&/*"G"OA&I
B
9CH&&	;(4b!#B>#R)%J )]HMH
A ""#G N3  	;"#DEEKK9:	;   	$	

 s*   8C% :D %+DD	D>D99D>c                    | j                   j                  D ],  }|j                  dk(  r|j                  t        j
                  j                  j                  j                  k(  rvt        |j                        dk(  sJ t        |j                  d         rD|j                  d   t        j                  k7  r$|j                  d   t        j                  f|_
        |j                  dk(  s|j                  j                  d      }|t        |      st        |j                        }t        j                  |d<   ||_        / | j                   j!                          | j#                          | S )Ncall_functionrd   r   r   rw   )graphnodesopr%   rs   opsprimsconvert_element_typedefaultr[   r   r	   float64kwargsgetdictlint	recompile)modelnoderw   
new_kwargss       r+   cast_dtype_args_to_fp64r    s   !!GG&uyyCCKKKtyy>Q&&&diil+		!0M!YYq\5==9	77o%KKOOG,E ^E%:!$++.
&+mm
7#( " 
KK	OOLr-   c                      ddl m} |j                         } t        j                  k(  rt        |      } | fd|      }||fS )Nr   )tree_mapc                 ~    t        | t        j                        r!| j                         r| j	                        S | S r   )r   rs   r   rt   to)r4   rw   s    r+   <lambda>zcast_to.<locals>.<lambda>  s6    a&1+>+>+@ $$u+ r-   )torch.utils._pytreer  r  rs   r  r  )rw   r  inputsr  s   `   r+   cast_tor"    sK    ,HHUOE (.	 		F &=r-   c                 8    t        t        j                  | |      S r   )r"  rs   r  )r  r!  s     r+   r   r     s    5==%00r-   c                    	  |t        j                  |       t        |            }t        | |||||       S # t        $ r}t
        j                  d       Y d }~yd }~ww xY w)Nr   zWhile minifying the program in accuracy minification mode, ran into a runtime exception which is likely an unrelated issue. Skipping this graphF)r   r   r   r  r   r>   r   )r\   r   compiler_fnr   r   r   compiled_gmr   s           r+   backend_accuracy_failsr'    su    !MM">~N
 #%'
 
 	
   	#	

 s   69 	A!AA!strideztorch._prims_common.StrideTyperv   ztorch._prims_common.ShapeTypereturnc                4    | | S t        j                  |      S r   )r   make_contiguous_strides_for)r(  rv   s     r+   _stride_or_defaultr,    s    
 '6UU-N-Nu-UUr-   dc                       fdS )Nc                     | | S S r   rG   )r4   r-  s    r+   r  z_mk_defaulter.<locals>.<lambda>  s    !-Q.Q.r-   rG   )r-  s   `r+   _mk_defaulterr0    s	    ..r-   cpuc                   ,    e Zd Zd ZddddZd Zd Zy)NopInputReaderc                     d| _         y )Nr   total)r(   s    r+   r,   zNopInputReader.__init__  s	    
r-   Ndevice
dtype_hintc                .    | xj                   dz  c_         y )Nr   r5  )r(   storage_hashnbytesr8  r9  s        r+   storagezNopInputReader.storage  s    

a
r-   c                      y r   rG   r(   r   r  s      r+   tensorzNopInputReader.tensor      r-   c                      y r   rG   r?  s      r+   symintzNopInputReader.symint
  rA  r-   rD   rE   rF   r,   r=  r@  rC  rG   r-   r+   r3  r3     s     7;t r-   r3  c                   D    e Zd Zd	dddZddddZ	 d	ddddddZd Zy)
InputReaderN)pbarc                v    |t         j                  d       |t        |      nd | _        g | _        || _        y )Nz0no save_dir specified, will generate random data)r>   r?   r   storer   rG  )r(   save_dirrG  s      r+   r,   zInputReader.__init__  s9    
 KKJK5=5I'1t
		r-   r7  c                   | j                   | j                   j                  d       t        |      }t        |      }| j                  P|N	 | j                  j                  |      }||j                  k7  r!t        j                  d||j                         |S t        j                  d|       ||j                  z  f}t        d |      }t        ||||      j                         S # t        $ r Y Zw xY w)Nr   zdevice mismatch: %s != %sz1could not load %s, generating random data insteadrv   )rG  update_device_or_default_dtype_or_defaultrI  read_storager8  r>   r?   r   itemsizer,  r   untyped_storage)r(   r;  r<  r8  r9  r=  rv   r(  s           r+   r=  zInputReader.storage  s    99 IIQ#F+&z2
::!l&>
**11,? W^^+KK ;VW^^T GV:...0#D6E6:v>NNPP % s   C) )	C54C5)storage_offsetrw   r   is_leafc                   t        ||      }t        |      }t        |      }t        |      }t	        |      }t        j                  g ||j                  |      }	t        j                         5  |	j                  ||||       d d d        |snt        j                         5  |	j                  t
        j                        }	d d d        t        j                         5  |	j                  ||||       d d d        t
        j                  j                  j                  |	      |k(  sJ t
        j                   j#                  |	|       | j$                  j'                  |	       |	S # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w)NrL  )rw   r8  r   )memory_format)r,  _storage_offset_or_defaultrO  _is_leaf_or_default_requires_grad_or_defaultrs   r@  r8  no_gradset_enable_gradclonepreserve_format_subclasses
meta_utilssafe_is_leaf_utilsset_tensor_metadatar   append)
r(   r=  rv   r(  rS  rw   r   rT  metadatats
             r+   r@  zInputReader.tensor2  s&    $F%83NC!%(%g.1-@LLeGNN-
 ]]_FF7NE6: ""$GG%*?*?G@ %wv> !  ++88;wFFF((H5		 _ %$ s$   1E)$!E5!F)E25E>F
c                 <    | j                   j                  |       |S r   )r   rd  )r(   vals     r+   rC  zInputReader.symintS  s    		
r-   r   rD  rG   r-   r+   rF  rF    s>    	d 	 7;t Q4 	 Br-   rF  c                   B    e Zd ZdddZd ZddddefdZdd	Zdd
Zy)InputWriterFstable_hashc                    g | _         t        j                         | _        || _        |t        ||      nd | _        i | _        y )Nrk  )_lines	itertoolsr   storage_counterrJ  r   rI  seen_storages)r(   rJ  rl  s      r+   r,   zInputWriter.__init__e  sG    (0  # x[A 	

  r-   c                 r    dg}|j                  d | j                  D               |j                  d       |S )Nzdef load_args(reader):c              3   &   K   | ]	  }d |   yw)rc   NrG   )r   ls     r+   r   z$InputWriter.lines.<locals>.<genexpr>u  s     1[4s[s   zload_args._version = 0)extendrn  rd  )r(   rs     r+   lineszInputWriter.linesq  s8    $
 	
1T[[11 	
)*r-   Nr9  device_hintr)  c          
      D   t        |      }| j                  j                  |      }||S dt        | j                         }d}t        d       t        |      k7  rd|}d}|j                  }|j                  dk(  r|J |}t        d       |k7  rd|}|j                         }	d }
| j                  4|j                  j                  dk7  r| j                  j                  |      }
| j                  j                  | d|
d|	| | d       || j                  |<   |S )	Nbufr   z, dtype_hint=metaz	, device=z = reader.storage(, rf   )r
   rq  r  ri   rp  rO  r8  rX   rN  r<  rI  write_storagern  rd  )r(   rR  r9  ry  wsvmaybe_dtype_hintr   r8  r<  r;  s              r+   r=  zInputWriter.storage  sB   O,""2&=H$t++,-.T"&7
&CC!.zn=  '';;& *** Fd#v-&vj1L '')::!o&<&<&A&AV&K::33ODLc#L#32fZ~N^M__`a	
 "#2r-   c                 D   | j                  |j                         |j                  |j                        }g }t	        d |j
                        |j                         k7  r1|j                  t        t        |j                                            t        d       |j                  k7  r|j                  d|j                         t        d       |j                         k7  r"|j                  d|j                                t        j                  j                  |      }|r&|j!                  d |j#                         D               t%        d       |j&                  k7  r|j                  d|j&                         t        j(                  j*                  j-                  |      }t/        d       |k7  r|j                  d|       | j0                  j                  dd	j3                  |t        t        |j
                              g|      z   d
| z          y )Nrx  rL  zdtype=zstorage_offset=c              3   0   K   | ]  \  }}| d |  yw)=NrG   )r   kr  s      r+   r   z%InputWriter.tensor.<locals>.<genexpr>  s!     I1HA1#Qqe1Hs   zrequires_grad=zis_leaf=zreader.tensor(r}  )  # )r=  rR  rw   r8  r,  rv   r(  rd  strtuplerO  rW  rS  rs   rb  get_tensor_metadataru  rm   rY  r   r_  r`  ra  rX  rn  r/   )r(   r   rf  r=  r   tensor_metadatarT  s          r+   r@  zInputWriter.tensor  s   ,,AGG  
 d!''2ahhj@KKE!((*-./T"agg-KK&,-%d+q/?/?/AAKK/!*:*:*<)?@A,,::1=KKI1F1F1HII$T*aoo=KK.(;<=##..;;A>t$/KK(7+./ii#eAGGn"5==>?dVn	
r-   c                     t        |t        j                        r|j                  j                  }| j
                  j                  d|d|        y )Nzreader.symint(r  )r   rs   SymIntr  hintrn  rd  )r(   r   rh  s      r+   rC  zInputWriter.symint  s<    c5<<(((--C^C7%v>?r-   )r)  N)	rD   rE   rF   r,   rw  r  r=  r@  rC  rG   r-   r+   rj  rj  d  s.    05 
  6:t PS 8
6@r-   rj  )FF)F)Sr   	functoolsrM   ro  loggingr   r   rK   r1   collectionsr   	importlibr   typingr   r   r   rs   torch._prims_common_prims_commonr   torch._subclasses.meta_utilstorch._dynamo.testingr   r	    torch.multiprocessing.reductionsr
   torch.utils._content_storer   r   r   r   r   r   	getLoggerrD   r>   r   inductor_config	is_fbcoder   libfb.py.build_infolibfbr0   extra_importspy
build_info	BuildInfoget_build_ruler$   r3   r/   r=   r   rQ   ro   rT   	lru_cacher   r   r   r   r   r   r   r   r  r  r"  r   r'  r,  r0  float32rO  r8  rN  rW  rY  rX  r3  rF  rj  )r4   s   0r+   <module>r     sm        	     # . .  # # . . ; M  .g!CL   89$$& 
J $$..==?GG	SWXJIITA!9!B?TUM 62 2j  a aH T 0 -2 &@U	I 	
$0V 	B BJ*$1 	 RV56V +V &	V/Q /8Xa[M1$45 / "%--0 "<5<<#67 *1- )%0 #E*   E EhV@ V@e Us    H