o
    闦i                     @   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 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ListTuple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 Y/home/ubuntu/transcripts/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   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   r3   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)r1   _Cparse_irmakeMultiOutputIntoTupleinputs
isinstancetype	FloatTyper   randomuniformIntTyperandint
TensorTyper   r3   BoolTypeNotImplementedError_create_function_from_graph!_jit_pass_erase_shape_informationgraph)r4   rH   r;   inp
tensorTypefuncr   r   r   load_graph_and_inputs)   s"   rL   c                 C   s$   t d| |dd}| }|jd S )Nzfn(*inputs))fnr;   )stmtglobals  )r   blocked_autorangemedian)rM   r;   	test_runsttimesr   r   r   	time_cuda>   s   
rV   c                 C   s6   t  }t|D ]}| |  qt  }|| | d S )NrP   )timeperf_counterrange)rM   r;   rS   r'   _er   r   r   time_cpuC   s
   
r\   
      )warmup_runsrS   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)	rL   rY   r<   r1   Tensorr-   r=   r\   rV   )	r4   r;   r_   rS   rH   rZ   is_cpuinputoutr   r   r   run_testJ   s   
re   c               	   o   s6    t jd}zd V  W t j| d S t j| w )NF)r1   r8   _get_graph_executor_optimize)argskwargsold_optimizer   r   r   no_fuserY   s
   rj   c                 C   s4   t   t| |W  d    S 1 sw   Y  d S r   )rj   re   r4   r;   r   r   r   run_baseline_no_fusiona   s   $rl   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)r1   jitset_fusion_strategyfuserre   )r4   r;   dynamicstrat	old_stratr   r   r   run_nncf   s   rv   c                 C   s:   t jd t| |W  d    S 1 sw   Y  d S )Nfuser2)r1   rp   rr   re   rk   r   r   r   run_nvfusero   s   $rx   )
contextlibr   typingr   r   r   r   r?   r1   rW   torch.utils.benchmarkr   strr)   r8   rC   r3   rL   rV   r\   floatre   rj   rl   rv   rx   r   r   r   r   <module>   s"   
	