o
    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 a/home/www/facesmatcher.com/frenv_anti/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   ZBEGINENDZgraphsfZ
split_strsiZ	split_strZ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'   torchZempty_strided)r#   r$   r%   r&   r'   r   r   r   make_tensor_from_type   s   r+   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)Zparse_tensor_constantsg?d   r	   r   z,A default value is not implemented for type 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,   r8   r0   inpZ
tensorTypefuncr   r   r   load_graph_and_inputs)   s"   r;   c                 C   s$   t d| |dd}| }|jd S )Nzfn(*inputs))fnr0   )stmtglobals  )r   Zblocked_autorangeZmedian)r<   r0   	test_runsttimesr   r   r   	time_cuda>   s   
rC   c                 C   s6   t  }t|D ]}| |  qt  }|| | d S )Nr?   )timeperf_counterrange)r<   r0   r@   r    _er   r   r   time_cpuC   s
   
rI   
      )warmup_runsr@   c          	      C   s|   t | \}}t|D ]}||  q
d }|D ]}t|tjr%|jjdk} q&q|d us,J |r6t|||}|S t|||}|S )Ncpu)	r;   rF   r1   r*   ZTensorr&   r2   rI   rC   )	r,   r0   rL   r@   r8   rG   Zis_cpuinputoutr   r   r   run_testJ   s   
rP   c               	   o   s6    t jd}zd V  W t j| d S t j| w )NF)r*   r/   Z_get_graph_executor_optimize)argskwargsZold_optimizer   r   r   no_fuserY   s
   rS   c                 C   s4   t   t| |W  d    S 1 sw   Y  d S r
   )rS   rP   r,   r0   r   r   r   run_baseline_no_fusiona   s   $rU   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)ZDYNAMICrJ   )ZSTATICrJ   Zfuser1)r*   jitZset_fusion_strategyfuserrP   )r,   r0   ZdynamicZstratZ	old_stratr   r   r   run_nncf   s   rX   c                 C   s:   t jd t| |W  d    S 1 sw   Y  d S )NZfuser2)r*   rV   rW   rP   rT   r   r   r   run_nvfusero   s   $rY   )
contextlibr   typingr   r   r3   r*   rD   Ztorch.utils.benchmarkr   strlistr"   r/   r6   r+   tupler;   rC   rI   floatrP   rS   rU   rX   rY   r   r   r   r   <module>   s"   
	