o
    0h                     @   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 O/var/www/vscode/kcb/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   sb   |   }|  }|  }|  }|d usJ |d usJ |d us"J |d us(J tj||||dS )N)sizestridedevicedtype)sizesstridesr+   r,   torchempty_strided)r(   r)   r*   r+   r,   r   r   r   make_tensor_from_type   s   r1   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)r/   _Cparse_irmakeMultiOutputIntoTupleinputs
isinstancetype	FloatTyper   randomuniformIntTyperandint
TensorTyper   r1   BoolTypeNotImplementedError_create_function_from_graph!_jit_pass_erase_shape_informationgraph)r2   rF   r9   inp
tensorTypefuncr   r   r   load_graph_and_inputs)   s"   rJ   c                 C   s$   t d| |dd}| }|jd S )Nzfn(*inputs))fnr9   )stmtglobals  )r   blocked_autorangemedian)rK   r9   	test_runsttimesr   r   r   	time_cuda>   s   
rT   c                 C   s6   t  }t|D ]}| |  qt  }|| | d S )NrN   )timeperf_counterrange)rK   r9   rQ   r%   _er   r   r   time_cpuC   s
   
rZ   
      )warmup_runsrQ   c          	      C   s|   t | \}}t|D ]}||  q
d }|D ]}t|tjr%|jjdk} nq|d us,J |r6t|||}|S t|||}|S )Ncpu)	rJ   rW   r:   r/   Tensorr+   r;   rZ   rT   )	r2   r9   r]   rQ   rF   rX   is_cpuinputoutr   r   r   run_testJ   s   
rc   c               	   o   s6    t jd}zd V  W t j| d S t j| w )NF)r/   r6   _get_graph_executor_optimize)argskwargsold_optimizer   r   r   no_fuserY   s
   rh   c                 C   s4   t   t| |W  d    S 1 sw   Y  d S r
   )rh   rc   r2   r9   r   r   r   run_baseline_no_fusiona   s   $rj   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)r/   jitset_fusion_strategyfuserrc   )r2   r9   dynamicstrat	old_stratr   r   r   run_nncf   s   rt   c                 C   s:   t jd t| |W  d    S 1 sw   Y  d S )Nfuser2)r/   rn   rp   rc   ri   r   r   r   run_nvfusero   s   $rv   )
contextlibr   typingr   r   r=   r/   rU   torch.utils.benchmarkr   strlistr'   r6   rA   r1   tuplerJ   rT   rZ   floatrc   rh   rj   rt   rv   r   r   r   r   <module>   s"   
	