U
    9%e~[                     @   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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)rd dl*Z+g Z,dZ-e)rXddddgZ,e+j.j/j01 2ddZ3d4dd e,D Z-dddgZ5G dd dZ6dd Z7dZ8G d d! d!Z9e:dd"d# Z;d$d%d&d'Z<d(d) Z=d*d+ Z>G d,d- d-e?Z@d.d/ ZAdJd0d1ZBdKd$d$d2d3d4ZCd5d6 ZDd7d8 ZEd9d: ZFdLd$d$d2d;d<ZGeeeH  eeH eeH d=d>d?ZIe&eee& ge&f d@dAdBZJeJejKZLeJeMdCZNeJd ZOeJd$ZPeJd$ZQG dDdE dEZRG dFdG dGZSG dHdI dIZTdS )M    N)Counter)import_module)CallableOptionalSequence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   s   g | ]}d | dqS )ztorch.ops.load_library("z") .0xr   r   X/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/torch/_dynamo/debug_utils.py
<listcomp>0   s     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__7   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   s   g | ]}d | dqS )z	        "z",r   r   r   r   r   r   F   s     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   buildE   s    zBuckTargetWriter.buildTc              	   C   sZ   t j| jd}t|d}||   W 5 Q R X t| jg }|rVt	
dd| |S )NZTARGETSwzFFound an example that reproduces the error. Run this cmd to repro - %s )r!   r"   r/   r%   openwriter4   BUCK_CMD_PREFIXr*   logwarning)r+   Z	print_msgZtarget_filefdZ	cmd_splitr   r   r   r8   ]   s    zBuckTargetWriter.writeN)T)__name__
__module____qualname__r.   r4   r8   r   r   r   r   r   6   s   r   c                  C   sL   t jt d} | d kr.t  dt  } t j| sHt j	| dd | S )NZminifierz
/minifier_T)exist_ok)
r!   r"   r/   r   tempfile
gettempdirgetpassgetuserexistsmakedirs)r"   r   r   r   minifier_dirk   s    rG      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kr|| qt|dkrHtd| dS )Nr   z-We have not tested reprs of some modules - %sT)	setnamed_childrentyperI   
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 ]V\}}|  }t| d }|d k	rZ|jrZ| d}||d  d| d| d	7 }q"| j	
 D ]\}}	|	d krq|	 tkrdd
lm}
 |
jtkstt|	}n@t|	rdt|	j d|	j d}ndt|	j d|	j d}|	jr| d}||d  d| d| d7 }q| j
 D ]d\}}|d krXqBd}|jrhd}dt|j d|j | d}||d  d| d| d	7 }qB||| 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))rH   )Ztorch.nn.modules.modulerT   r1   r2   rK   __repr__next
parametersZis_cuda_buffersitemsZnumelMAX_CONSTANT_NUMEL_INLINEZtorch._tensor_strrW   	thresholdAssertionErrorreprtorchis_floating_pointlistshapedtype_parameterscode)rP   rT   tab	model_strmodule_namerR   Z
module_strZexample_paramZbuffer_namebufferrW   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AdaptiveAvgPool2drM   staticmethodrS   rp   r   r   r   r   rI   w   s4   
rI   c                  C   s   t j sdS d} zJtjddgtjd}|j d}d	dd	 |D }| | d7 } W n t
k
rx   | d
7 } Y nX tdd tt j D }| d7 } | D ]\}}| d| d| d7 } q| d7 } | S )Nz:# torch.cuda.is_available()==False, no GPU info collected
z# CUDA Info: 
Znvccz	--version)stdoutr   r   c                 S   s    g | ]}|d k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: 
rt   z : ru   )rb   rx   Zis_available
subprocessr   PIPErs   decoder#   r/   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   sH   dd l }dd l}| rdS d|jj  d|jj  d|jj  dS )Nr   z*# config omitted due to stable_output=TruezYimport torch._dynamo.config
import torch._inductor.config
import torch._functorch.config
r   )Ztorch._functorch.configZtorch._inductor.configZ_dynamor   Zcodegen_configZ	_inductorZ
_functorch)r   rb   r   r   r   generate_config_string   s    


r   c                   C   s   t jt dS )Nzminifier_launcher.py)r!   r"   r/   rG   r   r   r   r   get_minifier_repro_path  s    r   c              
   C   s   t  }td| tr"t|  z$t|d}||  W 5 Q R X W n6 tk
r| } zt| t	d|W 5 d }~X Y nX d S )NzWriting minified repro to:
%sr5   z(Could not write to {minified_repro_path})
r   r:   r;   use_buckr   r8   r7   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   rO   
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| } |s6t|}t	| drJ| 
d t| dd}t| d	d}	t	| d
s|dk	s||	dk	r|| } |dk	r|| _|	dk	r|	| _| |}
|r|
S ||
r||
}|  || |
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)rP   argsonly_fwdZdisable_cloner   r   r   r   Zorig_named_parametersZorig_named_buffersoutZlossr   r   r   run_fwd_maybe_bwd)  s4    



r   require_fp64ignore_non_fpc             
   C   s.  ddl m} ddlm}m} ddlm}	 t| |rF|| | _|| | _	t||rd|||_|||_	t
| ||}
d}tjrz(tt| t|\}}t
|||}W n* tk
r   |rtdtd Y nX zt
|||}W n4 tk
r } ztd W Y dS d}~X Y nX |	|
||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)rP   Zopt_gmr   r   r   r   r   r   r   r   refZfp64_refZ
fp64_modelZfp64_examplesresr   Zpassingr   r   r   same_two_modelsP  sL    





 r   c                 C   s   | j jD ]b}|jdkr|jtjjjjkrt	|j
dks:tt|j
d r|j
d tjkr|j
d tjf|_
q| j   |   | S )NZcall_functionrV   r   r   )graphZnodesopr(   rb   ZopsZprimsZconvert_element_typedefaultrO   r   r`   r	   float64ZlintZ	recompile)modelnoder   r   r   !cast_convert_element_type_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 rw   )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 rw   )r   rb   r   )r   r   r   r   r   r     s    r   c             
   C   sd   z,|t | t|}t| |||||d W S  tk
r^ } ztd W Y dS d }~X Y nX d S )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   )rP   r   Zcompiler_fnr   r   r   Zcompiled_gmr   r   r   r   backend_accuracy_fails  s$    	 
r   )stridere   returnc                C   s   | d k	r| S t |S rw   )r   Zmake_contiguous_strides_for)r   re   r   r   r   _stride_or_default  s    r   )dr   c                    s    fddS )Nc                    s   | d k	r| S  S rw   r   r   r   r   r   r         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   s   d S rw   r   r+   r   kwargsr   r   r   tensor  s    zNopInputReader.tensorc                 O   s   d S rw   r   r   r   r   r   symint  s    zNopInputReader.symintr=   r>   r?   r.   r   r   r   r   r   r   r   r     s   r   c                   @   sJ   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 krt d |d k	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 k	r| j d t|}t|}| jd k	r||d k	r|z| j|}W n tk
r\   Y n X ||jkrxt	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 5 Q R X |st
  |	jtjd}	W 5 Q R X t  |		|||| W 5 Q R X tjj|	|kst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_leafr`   _utilsZset_tensor_metadatar   append)
r+   r   re   r   r   rf   r   r   metadatatr   r   r   r   %  s,       


zInputReader.tensorc                 C   s   | j | |S rw   )r   r   )r+   valr   r   r   r   F  s    zInputReader.symint)N)Nr   r   r   r   r   r     s    !r   c                   @   sT   e Zd ZddddZdd Zddded	d
dZdd	ddZdd	ddZdS )InputWriterFstable_hashc                C   s:   g | _ t | _|| _|d k	r*t||dnd | _i | _d S )Nr   )_lines	itertoolsr   storage_counterr   r   r   seen_storages)r+   r   r   r   r   r   r.   X  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 )rU   Nr   )r   lr   r   r   rz   h  s     z$InputWriter.lines.<locals>.<genexpr>zload_args._version = 0)extendr   r   )r+   rr   r   r   linesd  s
    
zInputWriter.linesNr   device_hint)r   c             
   C   s   t |}| j|}|d k	r |S dt| j }d}td t|krNd|}d}|j}|jdkrr|d k	snt|}t	d |krd|}|
 }	d }
| jd k	r|jjdkr| j|}
| j| d|
d|	| | d || j|< |S )	Nbufr   z, dtype_hint=metaz	, device=z = reader.storage(, rX   )r
   r   getrZ   r   r   r   rL   r`   r   r   r   Zwrite_storager   r   )r+   r   r   r   wsvZmaybe_dtype_hintro   r   r   r   r   r   r   r   s  s0    



zInputWriter.storagec              	   C   s4  | j | |j|jd}g }td |jd| krH|tt	|  t
d |jkrh|d|j td | kr|d|  tj|}|r|dd | D  td |jkr|d|j tjj|}td |kr|d| | jd	d
|tt	|jf| d|   d S )Nr   r   zdtype=zstorage_offset=c                 s   s    | ]\}}| d |V  qdS )=Nr   )r   kr  r   r   r   rz     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   r/   )r+   r   r   r   r   Ztensor_metadatar   r   r   r   r     s6      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=   r>   r?   r.   r   r  r   r   r   r   r   r   r   r   W  s
   r   )FF)F)F)Ur   	functoolsrC   r   loggingr!   r{   rA   r1   collectionsr   	importlibr   typingr   r   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libfbr0   Zextra_importspyZ
build_infoZ	BuildInfoZget_build_ruler'   r3   r/   r9   r   rG   r^   rI   	lru_cacher   r   r   r   r   r   r   r   r   r   r   r   r   intr   r   Zfloat32r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s   

5	d

+ E *
 
T