
    sg.                     4   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 Zd Zdj                         Zd Zdd	Zd
j                         ZddZd Zdj                         ZddZdj                         Zd Zd Zd Zedk(  r e        yy)    N)Dict)profiler)get_env_infoc                 0    | d d  t         j                  d d  y N)sysargv)new_argvs    R/var/www/html/venv/lib/python3.12/site-packages/torch/utils/bottleneck/__main__.pyredirect_argvr      s    1+CHHQK    c                 :    | j                   rd| j                    S y)Nzcompiled w/ CUDA znot compiled w/ CUDA)cuda_compiled_version)sysinfos    r   compiled_with_cudar      s"    $$"7#@#@"ABB!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                     t        d       t               } i }d}| j                  rd}d}| j                  r| j                  }|d|z   }nd}| j
                  }| j                  }|d}|| j                  t        |       t        j                  d    dt        j                  d	    |||d
}t        j                  di |S )NzRunning environment analysis... z DEBUGzCUDA zCUDA unavailablezUnable to fetchr   .   )	debug_strpytorch_versioncuda_compiled
py_versioncuda_runtimepip_versionpip_list_output )printr   is_debug_buildis_cuda_availablecuda_runtime_versionr   pip_packagestorch_versionr   r   version_infoenv_summaryformat)inforesultr   
cuda_availcudar   r   s          r   run_env_analysisr+   $   s    	
+,>DFI	J(( 4J!""K''O+ --+D1))!,-Qs/?/?/B.CD""*F '''r   c                     t        d       t        j                         }|j                          t	        | |d        |j                          |S )Nz!Running your script with cProfile)r   cProfileProfileenableexecdisable)codeglobslaunch_blockingprofs       r   run_cprofiler6   H   s<    	
-.DKKMudLLNKr   z
--------------------------------------------------------------------------------
  cProfile output
--------------------------------------------------------------------------------
c                     t        t               t        j                  |       j	                  |      }|j                  |       y r   )r   cprof_summarypstatsStats
sort_statsprint_stats)r5   sortbytopkcprofile_statss       r   print_cprofile_summaryr@   X   s1    	-\\$'226:Nt$r   c                      d fd	}t        d        |d      g}t        j                  j                         r|j	                   |d             |S |j	                  d        |S )NFc                 z    t        j                  |       5 }t        d        d d d        |S # 1 sw Y   S xY w)Nuse_cuda)r   profiler0   )rD   r5   r2   r3   s     r   run_profz#run_autograd_prof.<locals>.run_prof_   s9    x0 	$Dud#	$	$s   0:z1Running your script with the autograd profiler...rC   TF)r   torchr*   is_availableappend)r2   r3   rF   r(   s   ``  r   run_autograd_profrK   ^   s[    
 

=>&'Fzz h-. M 	dMr   z
--------------------------------------------------------------------------------
  autograd profiler output ({mode} mode)
--------------------------------------------------------------------------------
        {description}
{cuda_warning}
{output}
c                 J   g d}|vrd}t        |j                               d|dk(  rd}nd}t        | j                  fdd	      }|d | }|d
| d t        j
                  j                  j                  |      |d}	t        t        j                  di |	       y )N)cpu_time	cuda_timecpu_time_totalcuda_time_totalcountzWARNING: invalid sorting option for autograd profiler results: {}
Expected `cpu_time`, `cpu_time_total`, or `count`. Defaulting to `cpu_time`.rM   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                     t        |       S r   )getattr)xr=   s    r   <lambda>z-print_autograd_prof_summary.<locals>.<lambda>   s    F); r   T)keyreverseztop z events sorted by )modedescriptionoutputcuda_warningr   )	r   r&   sortedfunction_eventsrH   autogradprofiler_util_build_tableautograd_prof_summary)
r5   rY   r=   r>   valid_sortbywarnr\   sorted_eventstopk_eventsr(   s
     `       r   print_autograd_prof_summaryrg   x   s    ZL\!, 	dkk&!"v~X 4//;TKM&K dV#5fX>....;;KH$	F 


&
&
0
01r   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                      t        j                  t              } | j                  dt        d       | j                  dt        t         j
                  d       | j                         S )N)rZ   
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.)rj   nargsrk   )argparseArgumentParserdescriptadd_argumentstr	REMAINDER
parse_args)parsers    r   rt   rt      sd    $$:F
3D  E S0B0BQ  Sr   c                 :    t        d | j                  D              S )Nc              3   4   K   | ]  }|j                     y wr   )rO   ).0events     r   	<genexpr>z!cpu_time_total.<locals>.<genexpr>   s     Ou##Os   )sumr^   )autograd_profs    r   rO   rO      s    O1N1NOOOr   c                     t               } | j                  }| j                  g n| j                  }|j                  d|       d}d}d}d}t	        |       t
        j                  j                  dt        j                  j                  |             t        |d      5 }t        |j                         |d      }d d d        |dd d d}	t        t               t               }
t        j                   j#                         rt        j                   j%                          t'        |	      }t)        ||	      \  }}t        |
       t+        |||       t        j                   j#                         st-        |d	||       y t/        |      }t1        |j2                        dkD  r/t/        |      }||z
  |z  }t5        |      d
kD  rt-        |d	||       t-        |d||       y # 1 sw Y   'xY w)Nr   tottime   rO   rbr0   __main__)__file____name____package__
__cached__CPUg?rR   )rt   ri   rl   insertr   r   pathosdirnameopencompilereadr   rp   r+   rH   r*   rI   initr6   rK   r@   rg   rO   lenr^   abs)rl   ri   
scriptargscprofile_sortbycprofile_topkautograd_prof_sortbyautograd_prof_topkstreamr2   r3   r%   cprofile_profautograd_prof_cpuautograd_prof_cudacuda_prof_exec_timecpu_prof_exec_timepct_diffs                    r   mainr      s   <D Jyy(diiJa$OM+*HHOOArwwz23	j$	 :6v{{}j&9: 	E 
(O"$Kzz 

 u-M,=dE,J))	+=/=I::""$#$5u>RTfg ));<
,,-1+,=>'*<<@SSx=4'(95BVXjk 2F<PRdeE: :s   G  G*r   rG   )r~   r   )rM   r   )rn   r-   r9   r   r   typingr   rH   torch.autogradr   torch.utils.collect_envr   r   r   stripr%   r+   r6   r8   r@   rK   rb   rg   rp   rt   rO   r   r   r   r   r   <module>r      s       
 	   # 0"	 
EG !(H 
EG	 %  
EG 2< 
EG 	P1ff zF r   