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  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/SoloSpeech/.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current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   r4   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)r2   _Cparse_irmakeMultiOutputIntoTupleinputs
isinstancetype	FloatTyper   randomuniformIntTyperandint
TensorTyper   r4   BoolTypeNotImplementedError_create_function_from_graph!_jit_pass_erase_shape_informationgraph)r5   rI   r<   inp
tensorTypefuncr   r   r   load_graph_and_inputs*   s"   rM   c                 C   s$   t d| |dd}| }|jd S )Nzfn(*inputs))fnr<   )stmtglobals  )r   blocked_autorangemedian)rN   r<   	test_runsttimesr   r   r   	time_cuda?   s   
rW   c                 C   s6   t  }t|D ]}| |  qt  }|| | d S )NrQ   )timeperf_counterrange)rN   r<   rT   r(   _er   r   r   time_cpuD   s
   
r]   
      )warmup_runsrT   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)	rM   rZ   r=   r2   Tensorr.   r>   r]   rW   )	r5   r<   r`   rT   rI   r[   is_cpuinputoutr   r   r   run_testK   s   
rf   c               	   o   s6    t jd}zd V  W t j| d S t j| w )NF)r2   r9   _get_graph_executor_optimize)argskwargsold_optimizer   r   r   no_fuserZ   s
   rk   c                 C   s4   t   t| |W  d    S 1 sw   Y  d S r   )rk   rf   r5   r<   r   r   r   run_baseline_no_fusionb   s   $rm   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)r2   jitset_fusion_strategyfuserrf   )r5   r<   dynamicstrat	old_stratr   r   r   run_nncg   s   rw   c                 C   s:   t jd t| |W  d    S 1 sw   Y  d S )Nfuser2)r2   rq   rs   rf   rl   r   r   r   run_nvfuserp   s   $ry   )
contextlibr   typingr   r   r   r   r@   r2   rX   torch.utils.benchmarkr   strr*   r9   rD   r4   rM   rW   r]   floatrf   rk   rm   rw   ry   r   r   r   r   <module>   s"   
	