o
    I&i_                     @   s  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"e#Z$edZ%edZ&e&' Z(e(rd dl)Z*g Z+dZ,e(rg dZ+e*j-j.j/0 1ddZ2d3dd e+D Z,g dZ4G dd dZ5dd Z6dZ7G dd dZ8e9ddd Z:dd d!d"Z;d#d$ Z<d%d& Z=G d'd( d(e>Z?d)d* Z@dId+d,ZA	dJddd-d.d/ZBd0d1 ZCd2d3 ZDd4d5 ZE	dJddd-d6d7ZFd8ed9 d:d;d<d9fd=d>ZGd?e%d<eee% ge%f fd@dAZHeHejIZJeHeKdBZLeHd ZMeHdZNeHdZOG dCdD dDZPG dEdF dFZQG dGdH dHZRdS )K    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://
c                 C      g | ]}d | dqS )ztorch.ops.load_library("z") .0xr   r   DC:\wamp64\www\opt\env\Lib\site-packages\torch/_dynamo/debug_utils.py
<listcomp>2       r   )Zbuck2runz@mode/dev-nosanc                   @   s&   e Zd Zdd Zdd Zd	ddZdS )
BuckTargetWriterc                 C   s   t jt j|\| _| _| jdd| _| jdd d| j | _| j| jdd  | _| jdd  | _| j}||dd  dd  }d| d	| j | _	d S )
Nz.pyr   /.zfbcode.   zfbcode/r   :)
ospathsplitabspathsubdirpy_filereplacetargetfindcmd_line_path)selffilenametmpr   r   r   __init__9   s   zBuckTargetWriter.__init__c                 C   sD   d dd tD }td| j d| j dt d| d| j d	S )
Nr   c                 S   r   )z	        "z",r   r   r   r   r   r   H   r   z*BuckTargetWriter.build.<locals>.<listcomp>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,   Zextra_cpp_depsr   r   r   buildG   s   zBuckTargetWriter.buildTc                 C   sn   t j| jd}t|d}||   W d    n1 sw   Y  t| jg }|r5t	
dd| |S )NZTARGETSwzFFound an example that reproduces the error. Run this cmd to repro - %s )r"   r#   r0   r&   openwriter5   BUCK_CMD_PREFIXr+   logwarning)r,   Z	print_msgZtarget_filefdZ	cmd_splitr   r   r   r9   _   s   zBuckTargetWriter.writeN)T)__name__
__module____qualname__r/   r5   r9   r   r   r   r   r   8   s    r   c                  C   sL   t jt d} | d u rt  dt  } t j| s$t j	| dd | S )NZminifierz
/minifier_T)exist_ok)
r"   r#   r0   r   tempfile
gettempdirgetpassgetuserexistsmakedirs)r#   r   r   r   minifier_dirm   s   rH      c                   @   s   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jejjgZedd Zedd ZdS )NNModuleToStringc                 C   sL   t  }|  D ]\}}t|tjvr|| qt|dkr$td| dS )Nr   z-We have not tested reprs of some modules - %sT)	setnamed_childrentyperJ   
safe_reprsaddlenr;   r<   )gmZcant_convert_moduler   r   r   can_convert_to_string   s   
z&NNModuleToString.can_convert_to_stringc                 C   s  ddl m} d}td}|  D ]+\}}|  }t| d }|d ur-|jr-| d}||d  d| d| d	7 }q| j	
 D ]X\}}	|	d u rKqB|	 tkrcdd
lm}
 |
jtks^J t|	}n t|	rvdt|	j d|	j d}ndt|	j d|	j d}|	jr| d}||d  d| d| d7 }qB| j
 D ].\}}|d u rqd}|jrd}dt|j d|j | d}||d  d| d| d	7 }q||| jd d	7 }|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))rI   )Ztorch.nn.modules.modulerU   r2   r3   rL   __repr__next
parametersZis_cuda_buffersitemsZnumelMAX_CONSTANT_NUMEL_INLINEZtorch._tensor_strrX   	thresholdreprtorchis_floating_pointlistshapedtype_parameterscode)rQ   rU   tab	model_strmodule_namerS   Z
module_strZexample_paramZbuffer_namebufferrX   Z
tensor_str
param_nameparammaybe_devicer   r   r   convert   sF   	

 


  	zNNModuleToString.convertN)r>   r?   r@   rb   nnZLinearZConv1dZConv2dZConv3dZBatchNorm1dZBatchNorm2dZBatchNorm3dZ	LayerNormZDropoutZSoftmaxZReLUZGELUZIdentityZ	MaxPool2dZ	EmbeddingZTanhZConvTranspose1dZGLUZLSTMZFlattenZAdaptiveAvgPool2drN   staticmethodrT   rp   r   r   r   r   rJ   y   s6    
rJ   c                  C   s   t j sdS d} z!tddg}| d}ddd |D }| | d7 } W n ty7   | d	7 } Y nw t	d
d t
t j D }| d7 } | D ]\}}| d| d| d7 } qN| d7 } | S )Nz:# torch.cuda.is_available()==False, no GPU info collected
z# CUDA Info: 
Znvccz	--versionr   r   c                 S   s    g | ]}|d vrd| dqS ))r   #  
r   )r   sr   r   r   r      s     z-_cuda_system_info_comment.<locals>.<listcomp>z# nvcc not found
c                 s   s    | ]	}t j|V  qd S N)rb   cudaZget_device_name)r   ir   r   r   	<genexpr>   s    
z,_cuda_system_info_comment.<locals>.<genexpr>z# GPU Hardware Info: 
rs   z : rt   )rb   rw   Zis_available
subprocesscheck_outputdecoder$   r0   FileNotFoundErrorr   rangeZdevice_countr^   )rj   Zcuda_version_outZcuda_version_linescommentZ	gpu_namesnamecountr   r   r   _cuda_system_info_comment   s&   
r   F)stable_outputc              	   C   sX   dd l }dd l}| rdS d|jj  d|jj  d|jj  d|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   )
Ztorch._functorch.configZtorch._inductor.configZ_dynamor   Zcodegen_configZ	_inductorZ
_functorchZfxZexperimental_config)r   rb   r   r   r   generate_config_string   s   


r   c                   C   s   t jt dS )Nzminifier_launcher.py)r"   r#   r0   rH   r   r   r   r   get_minifier_repro_path	  s   r   c              
   C   s   t  }td| trt|  zt|d}||  W d    W d S 1 s)w   Y  W d S  tyF } z
t| t	d|d }~ww )NzWriting minified repro to:
%sr6   z(Could not write to {minified_repro_path})
r   r;   r<   use_buckr   r9   r8   OSError	exceptionNotImplementedError)contentsZminified_repro_pathr=   er   r   r   helper_for_dump_minify  s   &

r   c                   @   s   e Zd ZdS )AccuracyErrorN)r>   r?   r@   r   r   r   r   r     s    r   c                 C   sB   t | }tt| D ]}t|| tjr|| | | j q
|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~   rP   
isinstancerb   TensorZrequires_grad_requires_grad)example_inputsZcloned_inputsidxr   r   r   clone_inputs_retaining_gradness   s   r   c                 C   s   ddl m} ddlm}m}m} t| } |st|}t	| dr%| 
d t| dd}t| d	d}	t	| d
sP|dus>|	durP|| } |durI|| _|	durP|	| _| |}
|rX|
S ||
rd||
}|  || |
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_buffersZ_boxed_call)Ztorch._functorch.aot_autogradr   testingr   r   r   copydeepcopyr   hasattrr   getattrr   r   Zbackward)rQ   argsonly_fwdZdisable_cloner   r   r   r   Zorig_named_parametersZorig_named_buffersoutZlossr   r   r   run_fwd_maybe_bwd-  s.   



r   require_fp64ignore_non_fpc             
   C   s(  ddl m} ddlm}m} ddlm}	 t| |r#|| | _|| | _	t||r2|||_|||_	t
| ||}
d}tjrfztt| t|\}}t
|||}W n tye   |r^tdtd Y nw zt
|||}W n ty } ztd W Y d}~dS d}~ww |	|
||tjd|d	}|S )
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)ZtolZ	equal_nanr   )Z
eval_framer   r   r   r   utilsr   r   r   r   r   r   Zsame_two_models_use_fp64cast_to_fp64r   r   r   	ExceptionRuntimeErrorr;   r<   r   Zrepro_tolerance)rQ   Zopt_gmr   r   r   r   r   r   r   r   refZfp64_refZ
fp64_modelZfp64_examplesresr   Zpassingr   r   r   same_two_modelsT  sP   






r   c                 C   s   | j jD ]Q}|jdkr5|jtjjjjkr5t	|j
dksJ t|j
d r5|j
d tjkr5|j
d tjf|_
|jdkrU|jd}|d urUt|rUt|j}tj|d< ||_q| j   |   | S )Ncall_functionrW   r   r   rf   )graphnodesopr)   rb   opsZprimsZconvert_element_typedefaultrP   r   r   float64kwargsgetdictZlintZ	recompile)modelnoderf   Z
new_kwargsr   r   r   cast_dtype_args_to_fp64  s    




r   c                    sB   ddl m} | } tjkrt|}| fdd|}||fS )Nr   )tree_mapc                    s"   t | tjr|  r|  S | S rv   )r   rb   r   rc   tor   rf   r   r   <lambda>  s
   
zcast_to.<locals>.<lambda>)Ztorch.utils._pytreer   r   rb   r   r   )rf   r   inputsr   r   r   r   cast_to  s   


r   c                 C   s   t tj| |S rv   )r   rb   r   )r   r   r   r   r   r     s   r   c             
   C   s^   z|t | t|}t| |||||d W S  ty. } ztd W Y d }~dS d }~w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   )rQ   r   Zcompiler_fnr   r   r   Zcompiled_gmr   r   r   r   backend_accuracy_fails  s&   	
r   strideztorch._prims_common.StrideTypere   ztorch._prims_common.ShapeTypereturnc                C   s   | d ur| S t |S rv   )r   Zmake_contiguous_strides_for)r   re   r   r   r   _stride_or_default  s   r   dc                    s    fddS )Nc                    s   | d ur| S  S rv   r   r   r   r   r   r     s    z_mk_defaulter.<locals>.<lambda>r   r   r   r   r   _mk_defaulter  s   r   cpuc                   @   s4   e Zd Zdd ZdddddZdd Zd	d
 ZdS )NopInputReaderc                 C   s
   d| _ d S )Nr   total)r,   r   r   r   r/     s   
zNopInputReader.__init__Ndevice
dtype_hintc                C   s   |  j d7  _ d S )Nr   r   )r,   storage_hashnbytesr   r   r   r   r   storage  s   zNopInputReader.storagec                 O      d S rv   r   r,   r   r   r   r   r   tensor     zNopInputReader.tensorc                 O   r   rv   r   r   r   r   r   symint
  r   zNopInputReader.symintr>   r?   r@   r/   r   r   r   r   r   r   r   r      s
    r   c                   @   sL   e Zd ZdddddZdddddZ	ddddddd	d
Zdd ZdS )InputReaderN)pbarc                C   s8   |d u r	t d |d urt|nd | _g | _|| _d S )Nz0no save_dir specified, will generate random data)r;   r<   r
   storer   r   )r,   save_dirr   r   r   r   r/     s
   

zInputReader.__init__r   c                C   s   | j d ur| j d t|}t|}| jd ur=|d ur=z| j|}W n	 ty-   Y nw ||jkr;t	d||j |S t	d| ||j
 f}td |d}t|||| S )Nr   zdevice mismatch: %s != %sz1could not load %s, generating random data insteadre   )r   update_device_or_default_dtype_or_defaultr   Zread_storager}   r   r;   r<   itemsizer   r   untyped_storage)r,   r   r   r   r   r   re   r   r   r   r   r     s"   

zInputReader.storage)storage_offsetrf   r   is_leafc          
      K   s  t ||d}t|}t|}t|}t|}tjg ||j|d}	t  |		|||| W d    n1 s7w   Y  |sut
  |	jtjd}	W d    n1 sTw   Y  t  |		|||| W d    n1 spw   Y  tjj|	|ksJ tj|	| | j|	 |	S )Nr   )rf   r   r   )Zmemory_format)r   _storage_offset_or_defaultr   _is_leaf_or_default_requires_grad_or_defaultrb   r   r   Zno_gradset_Zenable_gradcloneZpreserve_format_subclasses
meta_utilssafe_is_leaf_utilsZset_tensor_metadatar   append)
r,   r   re   r   r   rf   r   r   metadatatr   r   r   r   2  s,   



zInputReader.tensorc                 C   s   | j | |S rv   )r   r   )r,   valr   r   r   r   S  s   zInputReader.symintrv   r   r   r   r   r   r     s    !r   c                   @   sL   e Zd ZddddZdd Zdddd	efd
dZdddZdddZdS )InputWriterFstable_hashc                C   s:   g | _ t | _|| _|d urt||dnd | _i | _d S )Nr   )_lines	itertoolsr   storage_counterr   r   r   seen_storages)r,   r   r   r   r   r   r/   e  s   

zInputWriter.__init__c                 C   s*   dg}| dd | jD  |d |S )Nzdef load_args(reader):c                 s   s    | ]}d | V  qdS )rV   Nr   )r   lr   r   r   ry   u  s    z$InputWriter.lines.<locals>.<genexpr>zload_args._version = 0)extendr   r   )r,   rr   r   r   linesq  s
   
zInputWriter.linesNr   device_hintr   c             
   C   s   t |}| j|}|d ur|S dt| j }d}td t|kr'd|}d}|j}|jdkr9|d us7J |}td |krDd|}|	 }	d }
| j
d ur[|jjdkr[| j
|}
| j| d|
d|	| | d || j|< |S )	Nbufr   z, dtype_hint=metaz	, device=z = reader.storage(, rY   )r	   r   r   r[   r   r   r   rM   r   r   r   Zwrite_storager   r   )r,   r   r   r  wsvZmaybe_dtype_hintro   r   r   r   r   r   r   r     s0   



zInputWriter.storagec              	   C   s4  | j | |j|jd}g }td |jd| kr$|tt	|  t
d |jkr4|d|j td | krF|d|  tj|}|rZ|dd | D  td |jkrj|d|j tjj|}td |kr|d| | jd	d
|tt	|jg| d|   d S )Nr  r   zdtype=zstorage_offset=c                 s   s"    | ]\}}| d |V  qdS )=Nr   )r   kr  r   r   r   ry     s     z%InputWriter.tensor.<locals>.<genexpr>zrequires_grad=zis_leaf=zreader.tensor(r  )  # )r   r   rf   r   r   re   r   r   strtupler   r   r   rb   r   Zget_tensor_metadatar   r^   r   r   r   r   r   r   r   r0   )r,   r   r   r   r   Ztensor_metadatar   r   r   r   r     s2   zInputWriter.tensorc                 C   s0   t |tjr
|jj}| jd|d|  d S )Nzreader.symint(r  )r   rb   ZSymIntr   hintr   r   )r,   r   r   r   r   r   r     s   zInputWriter.symint)r   N)	r>   r?   r@   r/   r  r  r   r   r   r   r   r   r   r   d  s    
r   )FF)F)Sr   	functoolsrD   r   loggingr"   rz   rB   r2   collectionsr   	importlibr   typingr   r   r   rb   Ztorch._prims_commonZ_prims_commonr   Ztorch._subclasses.meta_utilsZtorch._dynamo.testingr   r   Z torch.multiprocessing.reductionsr	   Ztorch.utils._content_storer
   r   r   r   r   r   	getLoggerr>   r;   r   Zinductor_configZ	is_fbcoder   Zlibfb.py.build_infoZlibfbr1   Zextra_importspyZ
build_infoZ	BuildInfoZget_build_ruler(   r4   r0   r:   r   rH   r_   rJ   	lru_cacher   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Zfloat32r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s   
5	d

+E)
 
T