U
    dP                     @   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e	Z
dd Zedkr\e  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 )	FauxTorchzEmulate 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   J/tmp/pip-unpacked-wheel-ua33x9lu/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yZzerozsize: )stmtglobalslabel	sub_labeldescriptionenvnum_threads)benchmark_utilsTimerr!   r   Zoneszeros).0branchZoverhead_nsr&   r'   r$   sizer*   Z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   r2   T   s    z== Unformatted ================================================================================
///////////////////////////////////////////////////////////////////////////////////////////////
z== Formatted ================================================================================
/////////////////////////////////////////////////////////////////////////////////////////////
)	enumerateappendr7   dumpsZblocked_autorangeprintlensysstdoutflushr+   ZCompareZtrim_significant_figuresZcolorize)Zserialized_resultsZrepeatsZtimersr9   ZtimerZ
comparisonr   r1   r   main/   s0    

$
rB   __main__)r   r7   r?   r   r!   Ztorch.utils.benchmarkutilsZ	benchmarkr+   objectr   rB   r   r   r   r   r   <module>   s    2