o
    ߗiqB                     @   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mZmZmZmZ d dlZd dlmZmZmZmZmZmZmZmZmZ d dlmZ d dlmZ e e!Z"e
dZ#e#$ Z%G dd	 d	e&Z'	d3d
ede(deee(ef  fddZ)dd Z*ddddddddddddd
ee deej+j, deee  deee(e(f  fddZ-ddddddZ.dddddeee(e(f  fddZ/dd  Z0d!d" Z1d#d$ Z2d%d& Z3	'd4d(eej+j, fd)d*Z4d+d, Z5ddd-dddd.d'd/deee(e(f  d0ee6e(f fd1d2Z7dS )5    N)import_module)AnyDictOptionalTupleUnion)	_cuda_system_info_commentBuckTargetWriterextra_importsgenerate_config_stringhelper_for_dump_minifyInputReaderminifier_dirNNModuleToStringNopInputReader)ExportedProgram)tqdmztorch._inductor.configc                       s   e Zd Z fddZ  ZS )AOTIMinifierErrorc                    s,   d}| dt | }t | || _d S )NzRThis error is caused by a bug in the AOTI minifier, please report a bug to PyTorchz: )strsuper__init__original_exception)selfr   additional_messagefull_message	__class__ V/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/torch/_dynamo/repro/aoti.pyr   &   s   
zAOTIMinifierError.__init__)__name__
__module____qualname__r   __classcell__r   r   r   r   r   %   s    r   exported_programcompiler_nameoptionsc                 C   sR   t  }tjt d}tj|stj|dd t||| |d|d t	|
 S )NcheckpointsTexist_okminify)r#   save_dircommandconfig_patches)ioStringIOospathjoinr   existsmakedirssave_graph_repro_epr   getvalue)r#   r$   r%   outsubdirr   r   r   dump_to_minify-   s   r8   c                 C   s   dd }t | }||S )Nc                 S   s~   |  d}t|dkrd|  S |d}tt|D ]}|| }| dkr-d| ||< qd||< qd|}|d | }|S )N
   z# r    )splitlenpoprangestripr1   )s_sfirstiliner   r   r   _convert_to_commentB   s   



z.get_module_string.<locals>._convert_to_comment)r   convert)gmrF   module_stringr   r   r   get_module_stringA   s   
rJ   Frun)r#   rH   argsr,   stable_outputr*   r+   accuracy	check_strmodule_in_commentstrictrH   rL   r,   c                C   s   |d u r|d u rt d|d ur|d urt d|d ur$|d u r$t d|d u r>|d us.J |d us4J tjj|||d}n|d u rF| }t|}| | | t||||d |	d u rbd|v }	| d | d | d	|	d
|d|d|
d	 d S )Nz.One of exported_program and gm must be definedz2Only one of exported_program and gm can be definedz-If gm is defined, args should also be definedrQ   r%   rM   r*   	_accuracyzif __name__ == '__main__':
z3    from torch._dynamo.repro.aoti import run_repro
zf    with torch.no_grad():
        run_repro(exported_program, config_patches=config_patches, accuracy=z
, command=z, save_dir=z, check_str=z)
)r   torchexportmodulerJ   write(generate_compiler_repro_exported_program)fdr$   r#   rH   rL   r,   rM   r*   r+   rN   rO   rP   rQ   rI   r   r   r   r4   U   sJ   


r4   )r,   rN   rQ   c                C   s  t jt d}t j|st j|dd t j|t| jj d}t	
dt| jj| t|d}t||| t||||d|d	 W d    n1 sMw   Y  t  }	t j|	d}
zt||
 t	
d	|
 trut|  W d S W d S  ty   t	
d
|
 Y d S w )Nr&   Tr'   z.pyz&Writing checkpoint with %s nodes to %sw)rH   rL   r,   r*   rN   rP   rQ   zrepro.pyz(Copying repro file for convenience to %szNo write permissions for %s)r/   r0   r1   r   r2   r3   r=   graphnodeslogwarningopenr4   tuplegetcwdshutilcopyfileuse_buckr	   rX   OSError)rH   rL   r$   r,   rN   rQ   r7   	file_namerZ   curdir
repro_pathr   r   r   dump_compiler_graph_state   s>   	rj   rS   c                C   s   t dt|d dt d}|s@|dtjj d7 }ttjdr+|dtjj d7 }ttjd	r;|d
tjj	 d7 }|t
 7 }tj|d}tj| | |d| d7 }|d7 }|d| d7 }|S )Nz5
import torch
import torch._inductor.inductor_prims

)rM   z!

isolate_fails_code_str = None

z


        z# torch version: r9   cudaz# torch cuda version: git_versionz# torch git version: z


zexported_program.pt2z&exported_program = torch.export.load('z')
z # print(exported_program.graph)
zconfig_patches=)textwrapdedentr   r
   rU   version__version__hasattrrk   rl   r   r/   r0   r1   rV   save)r#   r%   rM   r*   	model_strep_pathr   r   r   rY      s(   
rY   c                 C   s   t | dstd n| jdkrtd| j t }| | td|jd}t||d}| | |j}W d    t	|S 1 s@w   Y  t	|S )N_versionzzload_args does not have a _version attribute, please file a bug to PyTorch and describe how you generate this repro scriptr   zload_args is version %s, but this version of PyTorch only supports version 0.  We will try to run it anyway but there may be an incompatibility; if so, try upgrading your version of PyTorch.zLoading inputs)desctotal)r*   pbar)
rq   r^   r_   ru   r   r   rw   r   rL   ra   )	load_argsr*   
nop_readerrx   input_readerrL   r   r   r   repro_load_args   s&   


r|   c                 C   s&   dt jj_| }|j\}}|||fS )NT)rU   	_inductorconfiggenerate_intermediate_hooksrW   example_inputs)r%   r#   modrL   kwargsr   r   r   repro_common   s   


r   c                 C   s   t | |\}}}|||fS N)r   )r%   r#   r,   r   rL   r   r   r   r   repro_get_args  s   
r   c                 C   s   ddl m}m} t| |\}}}ddlm} ||||d|d}	||	}
t|
tr*J d}|D ]}t|tj	r=|j
r=d} nq.|
|i | |rL|  d S d S )Nr   )_aoti_compile_and_package_inneraoti_load_packagesynchronizeFload_and_runinductor_configsT)torch._inductorr   r   r   
torch.cudar   
isinstancer   rU   Tensoris_cuda)r%   r#   r,   r   r   rH   rL   r   r   package_pathcompiled	need_syncargr   r   r   	repro_run  s,   
r   Treturnc           	   
   C   s   ddl m}m} ztjj| ||d}| } | W S  ty\ } z8|r)W Y d }~d S t||r;|j|j	kr;W Y d }~d S t|t
rSd}t|t|d urSW Y d }~d S t||d }~ww )Nr   )	UserErrorUserErrorTyperR   z/Found .* in output, which is not a known type\.)torch._dynamo.excr   r   rU   rV   rW   	Exceptionr   
error_typeINVALID_OUTPUTRuntimeErrorresearchr   r   )	rH   tuple_inputsrQ   skip_export_errorr   r   epepatternr   r   r   export_for_aoti_minifier$  s"   

r   c                    s   ddl m} ddlm  ddlm} t| |\}}}|||||d\}d}	| jdv s-J | jdk| jdd	l	m
 d
|D ]}
t|
tjrN|
jrNd nq?d fdd	}|||tj|| jdtjt|	|d| j| j| j| j| jd	 d S )Nr   )minifier)r   )_aoti_flatten_inputs)r%   aot_inductor)dynamopythonr   r   FTc              
      s   t |}t| |d} | d u rdS t| tjjsJ z | |dd r)  W dS  tyO } z|d urD|t|vrDW Y d }~dS W Y d }~dS d }~ww )N)rQ   r   FTr   )ra   r   r   rU   fxGraphModuler   repr)rH   flat_example_inputsrO   r   r   r   r   r   r   rQ   r   r   r   module_failsa  s.   z"repro_minify.<locals>.module_fails)rO   )r$   r,   rQ   )r   
dump_stater*   offload_to_diskskip_offloadskip_sanitymax_granularityr   )functorch.compiler   r   r   torch._inductor.compile_fxr   r   minifier_export_moder   r   r   r   rU   r   r   	functoolspartialrO   rj   r*   r   skip_saving_eager_intermediatesr   r   )r%   r#   r,   r   r   r   rL   r   r   r$   r   r   r   r   r   repro_minifyH  sF   


r   r;   r   )r,   r+   rN   r*   tracing_moderO   r   r   rN   c                   s  |	D ]}
t d|
 q|du rd}td|du rd}tjd| d| d	|d
|d d|dtjd} fdd}|jdddd}|jddd}|| |jddd}|| |jddd}|| |jdddd |jddd d |jd!dd"d |jd#t	d d$d% |jd&t
|d'd% |jd(t
|d)d% |jd*t|d+d% |d,}|| |jd&t
|d'd% d }ttjd-kr|gtjd-d  }||}tttd.}||j || |d/S )0NzPUnrecognized kwarg %s; perhaps this repro was made on a newer version of PyTorchTrN   z'check for accuracy is not supported yetFr;   zAn AOTI repro script, typically triggering a bug in PyTorch AOTInductor.
When run with no arguments, this script defaults to running 'z8'.
Extra flags may be available; to find out more, try 'zr --help'.
There are also alternate subcommands available, see below.

default settings on this script:
  accuracy=z
  tracing_mode=z
  save_dir=z
  check_str=r9   )descriptionformatter_classc                    s,   | j dt ddd | j dddd dd	 d S )
Nz
--save-dirDIRz!directory where saved inputs live)typedefaultmetavarhelpz--no-save-dirr*   store_constz(don't use any directory for saved inputs)destactionconstr   )add_argumentr   )parserr*   r   r   common_flags  s   
zrun_repro.<locals>.common_flagsr+   z{run,minify,analyze})r   r   requiredrK   zjust run the repro)r   r)   zrun the minifier on the reproget_argszget the argsz!--skip-saving-eager-intermediates
store_truez+skip saving eager intermediates on --minify)r   r   z--offload-to-diskzYduring minification, offload delta debugging intermediates to disk.  Use if you're OOMingz--skip-sanityz@skip sanity check at beginning of minification on original graphz--max-granularityz;start at this granularity and work down; must be power of 2)r   r   r   z--check-strzBrequire minified program to fail with error containing this stringz--minifier-export-modezThe export mode used in minifier, either dynamo or python.`dynamo` corresponds to strict=True, and `python` corresponds to strict=False.z--skip-export-errorz1Skip intermediate graphs that cannot be exported.zminifier-queryr:   )r)   rK   r   )r,   )r^   r_   NotImplementedErrorargparseArgumentParserRawTextHelpFormatteradd_subparsers
add_parserr   intr   boolr=   sysargv
parse_argsr   r   r   r+   )r#   r,   r+   rN   r*   r   rO   r   r   more_kwargskr   r   
subparsers
parser_runparser_minifyparser_get_argsparser_minifier_queryrL   r%   COMMAND_FNSr   r   r   	run_repro  s   	
	
r   r   )FT)8r   r   r-   loggingr/   r   rc   r   rm   	importlibr   typingr   r   r   r   r   rU   torch._dynamo.debug_utilsr   r	   r
   r   r   r   r   r   r   torch.exportr   	torch.hubr   	getLoggerr   r^   inductor_config	is_fbcodere   r   r   r   r8   rJ   nnModuler4   rj   rY   r|   r   r   r   r   r   r   r   r   r   r   r   <module>   s   ,




?/
%

$K
