U
    •Ê¹jH  ã                   @   s^   d Z ddlZddlZddlZddlZddlm  mZ G dd„ dƒZ	dd„ Z
edkrZe
ƒ  dS )zAExample of Timer and Compare APIs:

$ python -m examples.compare
é    Nc                   @   s@   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dS )Ú	FauxTorchzØEmulate different versions of pytorch.

    In normal circumstances this would be done with multiple processes
    writing serialized measurements, but this simplifies that model to
    make the example clearer.
    c                 C   s   || _ || _d S ©N)Ú_real_torchÚ_extra_ns_per_element)ÚselfZ
real_torchZextra_ns_per_element© r   úe/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torch/utils/benchmark/examples/compare.pyÚ__init__   s    zFauxTorch.__init__c                 C   s,   t | ¡ ƒ}|dkr(t || j d ¡ |S )Niˆ  g•Ö&è.>)ÚintÚnumelÚtimeÚsleepr   )r   Úresultr   r   r   r   Úextra_overhead   s    zFauxTorch.extra_overheadc                 O   s   |   | jj||Ž¡S r   )r   r   Úadd©r   ÚargsÚkwargsr   r   r   r   "   s    zFauxTorch.addc                 O   s   |   | jj||Ž¡S r   )r   r   Úmulr   r   r   r   r   %   s    zFauxTorch.mulc                 O   s   |   | jj||Ž¡S r   )r   r   Úcatr   r   r   r   r   (   s    zFauxTorch.catc                 O   s   |   | jj||Ž¡S r   )r   r   Úmatmulr   r   r   r   r   +   s    zFauxTorch.matmulN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   r   r   r   r   r   r   r   r   r   r      s   r   c                     sÌ   ddg‰ g } d}‡ fdd„dD ƒ}t || ƒD ]N\}}|  t |jdd�¡¡ td	|d
 › dt|ƒ| › �dd� tj 	¡  q.tƒ  t
 dd„ | D ƒ¡}tdƒ | ¡  tdƒ | ¡  | ¡  | ¡  d S )N)r   r   ztorch.add(x, y))r   zadd (extra +0)ztorch.add(x, y + zero)é   c                    s„   g | ]|\}}ˆ D ]n\}}}d D ]^}dD ]T}t j||dkr<tntt|ƒt |df¡t d¡t d¡dœ||d|› �||d�‘q&qqqS ))é   é
   éd   iè  i'  iPÃ  )r   é   Úmasterr   r   )ÚtorchÚxÚyÚzerozsize: )ÚstmtÚglobalsÚlabelÚ	sub_labelÚdescriptionÚenvÚnum_threads)Úbenchmark_utilsÚTimerr!   r   ZonesZzeros)Ú.0ÚbranchZoverhead_nsr'   r(   r%   Úsizer+   ©Útasksr   r   Ú
<listcomp>7   s(      ïüôzmain.<locals>.<listcomp>))r    N)Z	my_branchr   )Zsevere_regressioné   gš™™™™™©?)Zmin_run_timeúr   z / Ú )Úendc                 S   s   g | ]}t  |¡‘qS r   )ÚpickleÚloads)r.   Úir   r   r   r3   T   s    zÀ== Unformatted ================================================================================
///////////////////////////////////////////////////////////////////////////////////////////////
z¼== Formatted ================================================================================
/////////////////////////////////////////////////////////////////////////////////////////////
)Ú	enumerateÚappendr8   ÚdumpsZblocked_autorangeÚprintÚlenÚsysÚstdoutÚflushr,   ÚCompareZtrim_significant_figuresZcolorize)Zserialized_resultsZrepeatsZtimersr:   ZtimerZ
comparisonr   r1   r   Úmain/   s0    þ
ñ
ÿ$
ÿrD   Ú__main__)r   r8   r@   r   r!   Ztorch.utils.benchmarkÚutilsZ	benchmarkr,   r   rD   r   r   r   r   r   Ú<module>   s    2