U
    Mh.                     @   s   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Zd dlm	Z	 d dl
mZ dd Zdd Zd	 Zd
d Zd"ddZd Zd#ddZdd Zd Zd$ddZd Zdd Zdd Zdd  Zed!kre  dS )%    N)Dict)profiler)get_env_infoc                 C   s   | d d  t jd d < d S N)sysargv)Znew_argv r   Q/var/www/html/venv/lib/python3.8/site-packages/torch/utils/bottleneck/__main__.pyredirect_argv   s    r
   c                 C   s   | j rd| j  S dS )Nzcompiled w/ CUDA znot compiled w/ CUDA)Zcuda_compiled_version)Zsysinfor   r   r	   compiled_with_cuda   s    r   a\  
--------------------------------------------------------------------------------
  Environment Summary
--------------------------------------------------------------------------------
PyTorch {pytorch_version}{debug_str} {cuda_compiled}
Running with Python {py_version} and {cuda_runtime}

`{pip_version} list` truncated output:
{pip_list_output}
c                  C   s   t d t } i }d}| jr d}d}| jrB| j}|d k	rFd| }nd}| j}| j}|d kr^d}|| jt| t	j
d  dt	j
d	  |||d
}tjf |S )NzRunning environment analysis... z DEBUGzCUDA zCUDA unavailablezUnable to fetchr   .   )	debug_strZpytorch_versionZcuda_compiled
py_versionZcuda_runtimepip_versionpip_list_output)printr   Zis_debug_buildZis_cuda_availableZcuda_runtime_versionr   Zpip_packagesZtorch_versionr   r   version_infoenv_summaryformat)inforesultr   Z
cuda_availcudar   r   r   r   r	   run_env_analysis$   s2    

r   Fc                 C   s0   t d t }|  t| |d  |  |S )Nz!Running your script with cProfile)r   cProfileZProfileenableexecdisable)codeglobsZlaunch_blockingprofr   r   r	   run_cprofileH   s    r"   z
--------------------------------------------------------------------------------
  cProfile output
--------------------------------------------------------------------------------
tottime   c                 C   s&   t t t| |}|| d S r   )r   cprof_summarypstatsZStatsZ
sort_statsZprint_stats)r!   sortbytopkZcprofile_statsr   r   r	   print_cprofile_summaryX   s    r)   c                    sN   d fdd	}t d |ddg}tj r@||dd n
|d  |S )NFc              	      s(   t j| d}t d  W 5 Q R X |S )Nuse_cuda)r   Zprofiler   )r+   r!   r   r    r   r	   run_prof_   s    z#run_autograd_prof.<locals>.run_profz1Running your script with the autograd profiler...r*   T)F)r   torchr   is_availableappend)r   r    r-   r   r   r,   r	   run_autograd_prof^   s    

r1   z
--------------------------------------------------------------------------------
  autograd profiler output ({mode} mode)
--------------------------------------------------------------------------------
        {description}
{cuda_warning}
{output}
cpu_timec           
         s   dddddg} |kr,d}t |  d |dkr:d}nd	}t| j fd
ddd}|d | }|d| d  tjj||d}	t tjf |	 d S )Nr2   Z	cuda_timecpu_time_totalZcuda_time_totalcountzWARNING: invalid sorting option for autograd profiler results: {}
Expected `cpu_time`, `cpu_time_total`, or `count`. Defaulting to `cpu_time`.CUDAz
	Because the autograd profiler uses the CUDA event API,
	the CUDA time column reports approximately max(cuda_time, cpu_time).
	Please ignore this output if your code does not use CUDA.
r   c                    s
   t |  S r   )getattr)xr'   r   r	   <lambda>       z-print_autograd_prof_summary.<locals>.<lambda>T)keyreverseztop z events sorted by )modedescriptionoutputcuda_warning)	r   r   sortedfunction_eventsr.   ZautogradZprofiler_utilZ_build_tableautograd_prof_summary)
r!   r=   r'   r(   Zvalid_sortbywarnr@   Zsorted_eventsZtopk_eventsr   r   r8   r	   print_autograd_prof_summaryx   s&    
 rE   a  
`bottleneck` is a tool that can be used as an initial step for debugging
bottlenecks in your program.

It summarizes runs of your script with the Python profiler and PyTorch's
autograd profiler. Because your script will be profiled, please ensure that it
exits in a finite amount of time.

For more complicated uses of the profilers, please see
https://docs.python.org/3/library/profile.html and
https://pytorch.org/docs/main/autograd.html#profiler for more information.
c                  C   s8   t jtd} | jdtdd | jdtt jdd |  S )N)r>   
scriptfilezGPath to the script to be run. Usually run with `python path/to/script`.)typehelpargsz2Command-line arguments to be passed to the script.)rG   nargsrH   )argparseArgumentParserdescriptadd_argumentstr	REMAINDER
parse_args)parserr   r   r	   rQ      s    rQ   c                 C   s   t dd | jD S )Nc                 s   s   | ]}|j V  qd S r   )r3   ).0eventr   r   r	   	<genexpr>   s     z!cpu_time_total.<locals>.<genexpr>)sumrB   )Zautograd_profr   r   r	   r3      s    r3   c               	   C   sR  t  } | j}| jd krg n| j}|d| d}d}d}d}t| tjdtj| t	|d}t
| |d}W 5 Q R X |dd d d}	tt t }
tj rtj  t||	}t||	\}}t|
 t||| tj st|d	|| d S t|}t|jdkr@t|}|| | }t|d
kr@t|d	|| t|d|| d S )Nr   r#   r$   r3   rbr   __main__)__file____name____package__
__cached__ZCPUg?r5   )rQ   rF   rI   insertr
   r   pathosdirnameopencompilereadr   rM   r   r.   r   r/   initr"   r1   r)   rE   r3   lenrB   abs)rI   rF   Z
scriptargsZcprofile_sortbyZcprofile_topkZautograd_prof_sortbyZautograd_prof_topkstreamr   r    r   Zcprofile_profZautograd_prof_cpuZautograd_prof_cudaZcuda_prof_exec_timeZcpu_prof_exec_timeZpct_diffr   r   r	   main   sF    



rh   rX   )F)r#   r$   )r2   r$   )rK   r   r&   r   r_   typingr   r.   Ztorch.autogradr   Ztorch.utils.collect_envr   r
   r   stripr   r   r"   r%   r)   r1   rC   rE   rM   rQ   r3   rh   rZ   r   r   r   r	   <module>   s0   $
	



3