U
    <c                     @   s   d dl mZ d dlmZmZmZmZ d dlZd dlZd dl	Z	d dl
mZ eee dddZejjdd	d
Zeeeee f dddZdd Zdd ZdddedddZedd ZedddZedddZedddZdS )     )contextmanager)AnyListTuplecastN)Timer)filenamereturnc              	      s   d}d}d  d}g }t | d}| |}t|D ]p\}}|dkrHq6||}	|	dkr\q6|d |	 }
||d   d   fdd	|
jd
dD }|d| q6W 5 Q R X |S )Nz<GRAPH_EXPORT>z</GRAPH_EXPORT> rr      c                    s   g | ]}|t  d  qS N)len).0xpfx ?/tmp/pip-unpacked-wheel-gikjz4vx/torch/utils/jit/log_extract.py
<listcomp>   s     zextract_ir.<locals>.<listcomp>T)keepends)openreadsplit	enumeratefind
splitlinesappendjoin)r   ZBEGINZENDcurrentZgraphsfZ
split_strsiZ	split_strZend_locslinesr   r   r   
extract_ir   s$    
r%   )inp_typec                 C   sb   |   }|  }|  }|  }|d k	s,t|d k	s8t|d k	sDt|d k	sPttj||||dS )N)sizestridedevicedtype)Zsizesstridesr)   r*   AssertionErrortorchZempty_strided)r&   r'   r(   r)   r*   r   r   r   make_tensor_from_type   s    r.   )irr	   c                 C   s
  t jj| dd}|  g }| D ]}t| t jjrN|t	
dd q$t| t jjrt|t	dd q$t| t jjrtt jj| }|t| q$t| t jjr|t	dddk q$td|  q$t jd|}t j|j ||fS )	NT)Zparse_tensor_constantsg?d   r   r   z,A default value is not implemented for type Zforward)r-   _CZparse_irZmakeMultiOutputIntoTupleinputs
isinstancetypeZ	FloatTyper   randomuniformZIntTyperandint
TensorTyper   r.   ZBoolTypeNotImplementedErrorZ_create_function_from_graphZ!_jit_pass_erase_shape_informationgraph)r/   r:   r2   inpZ
tensorTypefuncr   r   r   load_graph_and_inputs)   s"    r=   c                 C   s$   t d| |dd}| }|jd S )Nzfn(*inputs))fnr2   )Zstmtglobals  )r   Zblocked_autorangeZmedian)r>   r2   	test_runsttimesr   r   r   	time_cuda>   s    rD   c                 C   s6   t  }t|D ]}| |  qt  }|| | d S )Nr@   )timeperf_counterrange)r>   r2   rA   r#   _er   r   r   time_cpuC   s
    
rJ   
      )warmup_runsrA   )r	   c          	      C   sx   t | \}}t|D ]}||  qd }|D ] }t|tjr*|jjdk} qLq*|d k	sXt|rht|||n
t	|||}|S )Ncpu)
r=   rG   r3   r-   ZTensorr)   r4   r,   rJ   rD   )	r/   r2   rM   rA   r:   rH   Zis_cpuinputoutr   r   r   run_testJ   s    
rQ   c               	   o   s*   t jd}z
d V  W 5 t j| X d S )NF)r-   r1   Z_get_graph_executor_optimize)argskwargsZold_optimizer   r   r   no_fuserY   s    
rT   c              
   C   s(   t   t| |W  5 Q R  S Q R X d S r   )rT   rQ   r/   r2   r   r   r   run_baseline_no_fusiona   s    rV   c              
   C   sb   zN|rdgndg}t j|}t jd t| |W  5 Q R  W S Q R X W 5 t j| X d S )N)ZDYNAMICrK   )ZSTATICrK   Zfuser1)r-   jitZset_fusion_strategyfuserrQ   )r/   r2   ZdynamicZ	old_stratZstratr   r   r   run_nncf   s    $rY   c              
   C   s.   t jd t| |W  5 Q R  S Q R X d S )NZfuser2)r-   rW   rX   rQ   rU   r   r   r   run_nvfusero   s    rZ   )
contextlibr   typingr   r   r   r   r5   r-   rE   Ztorch.utils.benchmarkr   strr%   r1   r8   r.   r=   rD   rJ   floatrQ   rT   rV   rY   rZ   r   r   r   r   <module>   s    
	