o
    kib                     @   s   d dl mZ d dlmZmZ d dlZd dlZd dlZd dlm	Z	 de
dee
 fddZd	ejjfd
dZde
deeee f fddZdd Zdd ZddddefddZedd ZdefddZdefddZdefddZdS )     )contextmanager)AnycastN)Timerfilenamereturnc                    s   d}d}d  g }t | M}| |}t|D ]8\}}|dkr!q||}|dkr+q|d | }	||d   d   fdd|	jdd	D }
|d
|
 qW d    |S 1 s\w   Y  |S )Nz<GRAPH_EXPORT>z</GRAPH_EXPORT>r      c                    s   g | ]
}|t  d  qS N)len).0xpfx e/var/www/addictedbytheproject.nl/epg/venv/lib/python3.10/site-packages/torch/utils/jit/log_extract.py
<listcomp>   s    zextract_ir.<locals>.<listcomp>T)keepends )openreadsplit	enumeratefind
splitlinesappendjoin)r   BEGINENDgraphsf
split_strsi	split_strend_locslinesr   r   r   
extract_ir	   s*   


r'   inp_typec                 C   sr   |   }|  }|  }|  }|d u rtd|d u r td|d u r(td|d u r0tdtj||||dS )NzFmake_tensor_from_type: 'size' is None (inp_type.sizes() returned None)zJmake_tensor_from_type: 'stride' is None (inp_type.strides() returned None)zImake_tensor_from_type: 'device' is None (inp_type.device() returned None)zGmake_tensor_from_type: 'dtype' is None (inp_type.dtype() returned None))sizestridedevicedtype)sizesstridesr+   r,   AssertionErrortorchempty_strided)r(   r)   r*   r+   r,   r   r   r   make_tensor_from_type   s   r2   irc                 C   s  t jj| dd}|  g }| D ]_}t| t jjr'|t	
dd qt| t jjr:|t	dd qt| t jjrTtt jj| }|t| qt| t jjri|t	dddk qtd|  t jd|}t j|j ||fS )	NT)parse_tensor_constantsg?d   r	   r   z,A default value is not implemented for type forward)r0   _Cparse_irmakeMultiOutputIntoTupleinputs
isinstancetype	FloatTyper   randomuniformIntTyperandint
TensorTyper   r2   BoolTypeNotImplementedError_create_function_from_graph!_jit_pass_erase_shape_informationgraph)r3   rG   r:   inp
tensorTypefuncr   r   r   load_graph_and_inputs-   s"   rK   c                 C   s$   t d| |dd}| }|jd S )Nzfn(*inputs))fnr:   )stmtglobals  )r   blocked_autorangemedian)rL   r:   	test_runsttimesr   r   r   	time_cudaB   s   
rU   c                 C   s6   t  }t|D ]}| |  qt  }|| | d S )NrO   )timeperf_counterrange)rL   r:   rR   r%   _er   r   r   time_cpuG   s
   
r[   
      )warmup_runsrR   c          	      C   s   t | \}}t|D ]}||  q
d }|D ]}t|tjr%|jjdk} nq|d u r.td|r8t|||}|S t	|||}|S )NcpuzNo tensor found in inputs)
rK   rX   r;   r0   Tensorr+   r<   r/   r[   rU   )	r3   r:   r^   rR   rG   rY   is_cpuinputoutr   r   r   run_testN   s   
rd   c               	   o   s6    t jd}zd V  W t j| d S t j| w )NF)r0   r7   _get_graph_executor_optimize)argskwargsold_optimizer   r   r   no_fuser^   s
   ri   c                 C   s4   t   t| |W  d    S 1 sw   Y  d S r
   )ri   rd   r3   r:   r   r   r   run_baseline_no_fusionf   s   $rk   c              	   C   s   z9|rdgndg}t j|}t jd t| |W  d    W t j| S 1 s,w   Y  W t j| d S t j| w )N)DYNAMICr\   )STATICr\   fuser1)r0   jitset_fusion_strategyfuserrd   )r3   r:   dynamicstrat	old_stratr   r   r   run_nnck   s   ru   c                 C   s:   t jd t| |W  d    S 1 sw   Y  d S )Nfuser2)r0   ro   rq   rd   rj   r   r   r   run_nvfusert   s   $rw   )
contextlibr   typingr   r   r>   r0   rV   torch.utils.benchmarkr   strlistr'   r7   rB   r2   tuplerK   rU   r[   floatrd   ri   rk   ru   rw   r   r   r   r   <module>   s"   
	