a
    –¥»a5P  ã                   @   sø   d Z ddlZddlmZmZmZ dd„ Zdd„ Zdd	„ Zd
d„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Zedkrôeƒ  ¡ \ZZejr�ejr�ejsœeƒ  ¡  nXe ddge ejr¶dgng  ejrÌdej gng  ¡ eee	eedœZeeej eƒ dS )a¸  
Benchmark for common convnets.

Speed on Titan X, with 10 warmup steps and 10 main steps and with different
versions of cudnn, are as follows (time reported below is per-batch time,
forward / forward+backward):

                    CuDNN V3        CuDNN v4
AlexNet         32.5 / 108.0    27.4 /  90.1
OverFeat       113.0 / 342.3    91.7 / 276.5
Inception      134.5 / 485.8   125.7 / 450.6
VGG (batch 64) 200.8 / 650.0   164.1 / 551.7

Speed on Inception with varied batch sizes and CuDNN v4 is as follows:

Batch Size   Speed per batch     Speed per image
 16             22.8 /  72.7         1.43 / 4.54
 32             38.0 / 127.5         1.19 / 3.98
 64             67.2 / 233.6         1.05 / 3.65
128            125.7 / 450.6         0.98 / 3.52

Speed on Tesla M40, which 10 warmup steps and 10 main steps and with cudnn
v4, is as follows:

AlexNet         68.4 / 218.1
OverFeat       210.5 / 630.3
Inception      300.2 / 1122.2
VGG (batch 64) 405.8 / 1327.7

(Note that these numbers involve a "full" backprop, i.e. the gradient
with respect to the input image is also computed.)

To get the numbers, simply run:

for MODEL in AlexNet OverFeat Inception; do
  PYTHONPATH=../gen:$PYTHONPATH python convnet_benchmarks.py     --batch_size 128 --model $MODEL --forward_only True
done
for MODEL in AlexNet OverFeat Inception; do
  PYTHONPATH=../gen:$PYTHONPATH python convnet_benchmarks.py     --batch_size 128 --model $MODEL
done
PYTHONPATH=../gen:$PYTHONPATH python convnet_benchmarks.py   --batch_size 64 --model VGGA --forward_only True
PYTHONPATH=../gen:$PYTHONPATH python convnet_benchmarks.py   --batch_size 64 --model VGGA

for BS in 16 32 64 128; do
  PYTHONPATH=../gen:$PYTHONPATH python convnet_benchmarks.py     --batch_size $BS --model Inception --forward_only True
  PYTHONPATH=../gen:$PYTHONPATH python convnet_benchmarks.py     --batch_size $BS --model Inception
done

Note that VGG needs to be run at batch 64 due to memory limit on the backward
pass.
é    N)Ú	workspaceÚbrewÚmodel_helperc           
         sæ   t jdd�}d}d‰ d}tˆ ƒD ]Z}t|ƒD ]L}|dkrDd ||¡nd}d |d	 |¡}tj|||||d
i fd
i fd� q,q t |‡ fdd„t|ƒD ƒdg¡ tj|dd|dd
i fd
i fd� |j ddgd¡}	|j 	|	d¡ ||fS )NÚMLP)Únameé   é   é   r   úfc_{}_{}Údataé   Ú
XavierFill)Zdim_inZdim_outZweight_initZ	bias_initc                    s   g | ]}d   ˆ |¡‘qS )r
   )Úformat)Ú.0Új©Údepth© úZ/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/caffe2/python/convnet_benchmarks.pyÚ
<listcomp>U   ó    zMLP.<locals>.<listcomp>ÚsumÚlastéè  ÚlabelÚxentÚloss)
r   ÚModelHelperÚranger   r   Úfcr   ÚnetÚLabelCrossEntropyÚAveragedLoss)
ÚorderÚcudnn_wsÚmodelÚdÚwidthÚir   ÚcurrentZnext_r   r   r   r   r   B   s@    ù
	ÿù	r   c                 C   s
  | dddœ}|r||d< t jd|d�}tj|dddd	d
di fdi fddd�
}t ||d¡}tj||dddd�}tj||dd	dddi fdi fdd�	}t ||d¡}tj||dddd�}	tj||	dddddi fdi fdd�	}
t ||
d¡}tj||dddddi fdi fdd�	}t ||d¡}tj||dddddi fdi fdd�	}t ||d¡}tj||dddd�}t ||ddd di fdi f¡}t ||d¡}t ||d!d d di fdi f¡}t ||d!¡}t ||d"d d#di fdi f¡}t ||d$¡}|j 	|d%gd&¡}|j 
|d'¡ |d(fS ))NT©r#   Z	use_cudnnZcudnn_exhaustive_searchÚws_nbytes_limitZalexnet©r   Z	arg_scoper   Úconv1r	   é@   é   r   ÚConstantFillé   é   ©ÚstrideÚpadÚpool1©Úkernelr4   Úconv2éÀ   é   ©r5   Úpool2Úconv3é€  r   Úconv4r   Úconv5Úpool5Úfc6i $  é   Úfc7Úfc8r   Úpredr   r   r   éà   ©r   r   r   ÚconvÚreluÚmax_poolr   Úsoftmaxr    r!   r"   ©r#   r$   Úmy_arg_scoper%   r-   Úrelu1r6   r9   Úrelu2r=   r>   Úrelu3r@   Úrelu4rA   Úrelu5rB   rC   Úrelu6rE   Úrelu7rF   rG   r   r   r   r   ÚAlexNete   s²    ýþø
÷÷÷÷ýÿÿrW   c                 C   s  | dddœ}|r||d< t jd|d�}tj|dddd	d
di fdi fdd�	}t ||d¡}tj||dddd�}t ||dd	dddi fdi f¡}t ||d¡}tj||dddd�}	tj||	dddddi fdi fdd�	}
t ||
d¡}tj||dddddi fdi fdd�	}t ||d¡}tj||dddddi fdi fdd�	}t ||d¡}tj||dddd�}t ||ddd di fdi f¡}t ||d¡}t ||d!d d"di fdi f¡}t ||d!¡}t ||d#d"d$di fdi f¡}t ||d%¡}|j 	|d&gd'¡}|j 
|d(¡ |d)fS )*NTr*   r+   Zoverfeatr,   r   r-   r	   é`   r/   r   r0   r1   )r4   r6   r2   r7   r9   r   r;   r=   r>   é   r   r<   r@   é   rA   rB   rC   i �  i   rE   rD   rF   r   rG   r   r   r   éç   rI   rN   r   r   r   ÚOverFeatÂ   s¤    ýþ÷þ÷÷÷þÿÿr\   c                  C   sÆ  | dddœ}|r||d< t jd|d�}tj|dddd	dd
i fdi fdd�	}t ||d¡}tj||dddd�}tj||dd	ddd
i fdi fdd�	}t ||d¡}tj||dddd�}	tj||	ddddd
i fdi fdd�	}
t ||
d¡}tj||ddddd
i fdi fdd�	}t ||d¡}tj||dddd�}tj||ddddd
i fdi fdd�	}t ||d¡}tj||ddddd
i fdi fdd�	}t ||d¡}tj||dddd�}tj||ddddd
i fdi fdd�	}t ||d¡}tj||ddddd
i fdi fdd�	}t ||d¡}tj||dddd�}t ||dd d!d
i fdi f¡}t ||d¡}t ||d"d!d!d
i fdi f¡}t ||d"¡}t ||d#d!d$d
i fdi f¡}t ||d%¡}|j 	|d&gd'¡}|j 
|d(¡ |d)fS )*NTr*   r+   Zvggar,   r   r-   r	   r.   r   r0   r   r<   r6   r2   r7   r9   é€   r=   r>   r   r@   Úpool4rA   rY   Úconv6Úpool6Úconv7Úconv8Úpool8Úfcixi b  rD   ÚfcxÚfcxir   rG   r   r   r   r[   rI   ) r#   r$   rO   r%   r-   rP   r6   r9   rQ   r=   r>   rR   r@   rS   r^   rA   rT   r_   rU   r`   ra   rV   rb   Zrelu8rc   rd   Zreluixre   Zreluxrf   rG   r   r   r   r   ÚVGGA  s   ýþ÷÷÷÷÷÷÷÷þþþrg   c                 C   s€  t  | ||d ||ddi fdi f¡}t  | ||¡}t  | ||d ||d ddi fdi f¡}	t  | |	|	¡}	t j| |	|d |d |d ddi fdi fdd	�	}
t  | |
|
¡}
t  | ||d
 ||d ddi fdi f¡}t  | ||¡}t j| ||d |d |d ddi fdi fdd	�	}t  | ||¡}t j| ||d dddd�}t  | ||d ||ddi fdi f¡}t  | ||¡}t  | ||
||g|¡}|S )Nz:conv1r   r   r0   z:conv3_reducer   z:conv3r	   r<   z:conv5_reducez:conv5r;   r2   z:pool©r8   r4   r5   z
:pool_proj)r   rJ   rK   rL   Úconcat)r%   Z
input_blobZinput_depthZoutput_nameZconv1_depthZconv3_depthsZconv5_depthsZ
pool_depthr-   Zconv3_reducer>   Zconv5_reducerA   ÚpoolZ	pool_projÚoutputr   r   r   Ú_InceptionModulež  sl    þþ÷þ÷úþrl   c                 C   sˆ  | dddœ}|r||d< t jd|d�}tj|dddd	d
di fdi fddd�
}t ||d¡}tj||ddddd�}t ||dd	d	ddi fdi f¡}t |||¡}tj||dd	dddi fdi fdd�	}t ||d¡}	tj||	ddddd�}
t||
ddd	ddgddgdƒ}t||dddddgddgd	ƒ}tj||ddddd�}t||dd ddd!gdd"gd	ƒ}t||d#d$d%d&d'gd(d	gd	ƒ}t||d#d)dddgd(d	gd	ƒ}t||d#d*d&d+d,gdd	gd	ƒ}t||d-d.dd%d/gddgdƒ}tj||d0dddd�}t||d1d2dd%d/gddgdƒ}t||d1d3d4dd4gd"dgdƒ}tj||d5d
dd6�}t ||d7d8d9di fdi f¡}t 	||d:¡}|j
 |d;gd<¡}|j
 |d=¡ |d'fS )>NTr*   r+   Z	inceptionr,   r   r-   r	   r.   é   r   r0   r2   r3   r6   r   rh   Úconv2ar9   r:   r<   r=   Úinc3rX   r]   é   é    r   Úinc4rB   ià  Úinc5éÐ   é0   rY   Úinc6é    ép   rH   é   Úinc7Úinc8é�   i   i  Úinc9i@  Úpool9i@  Úinc10Úinc11r?   Úpool11r7   r   rZ   r   rG   r   r   r   )r   r   r   rJ   rK   rL   rl   Zaverage_poolr   rM   r    r!   r"   )r#   r$   rO   r%   r-   rP   r6   rn   r9   rQ   r=   ro   rr   rB   rs   rv   rz   r{   r}   r~   r   r€   r�   r   rG   r   r   r   r   Ú	InceptionÞ  s     ýþöþ÷ÿÿÿÿÿÿÿÿÿþr‚   c                 C   sj   t  | d¡}| jj|dddddd�}| jjg dd	gd
d�}| jD ]$}| j| }| j ||||g|¡ q@dS )zC Simple plain SGD update -- not tuned to actually train the models ÚiterÚLRg:Œ0âŽyE¾Ústepi'  g+‡ÙÎ÷ï?)Zbase_lrÚpolicyZstepsizeÚgammaÚONEr   ç      ð?)ÚshapeÚvalueN)	r   rƒ   r    ZLearningRateÚparam_init_netr0   ÚparamsZparam_to_gradZWeightedSum)r%   ZITERr„   rˆ   ÚparamZ
param_gradr   r   r   ÚAddParameterUpdate6  s    ÿ

r�   c                 C   sü  | |j |jƒ\}}|j| ¡ _|j| ¡ _|j dkrD|jd||g}n|j||dg}|jdkrf|j|g}|jj	g d|ddd� |jj
g d|jgd	d
d� |jr®td |j¡ƒ n6td |j¡ƒ | dg¡ t|ƒ |j dkrätdƒ |jsþ|j ¡  |j ¡  |j�r"|j ¡ jD ]}|j|_�q|j�rÂtd |j|j¡dƒ�$}| t|j ¡ ƒ¡ W d   ƒ n1 �sl0    Y  td |j|j¡dƒ�$}| t|j ¡ ƒ¡ W d   ƒ n1 �s¸0    Y  t |j¡ t |j¡ t |j ¡ j|j|j|j ¡ d S )NÚNCHWr	   r   r   g        r‰   )rŠ   ÚmeanZstdr   r   iç  )rŠ   ÚminÚmaxz{}: running forward only.z{}: running forward-backward.r   ZNHWCzU==WARNING==
NHWC order with CuDNN may not be supported yet, so I might
exit suddenly.z{0}_init_batch_{1}.pbtxtÚwz	{0}.pbtxt)!r#   r$   Znet_typeZProtoÚtypeZnum_workersÚ
batch_sizer%   rŒ   ZGaussianFillZUniformIntFillZforward_onlyÚprintr   ZAddGradientOperatorsr�   ÚcpuZRunAllOnGPUr    ZengineÚopZ
dump_modelÚopenÚwriteÚstrr   Z
RunNetOnceZ	CreateNetZBenchmarkNetr   Zwarmup_iterationsZ
iterationsZlayer_wise_benchmark)Z	model_genÚargr%   Z
input_sizeZinput_shaper™   Zfidr   r   r   Ú	BenchmarkA  sf    


ûû
ÿ

ÿ44þrž   c                  C   s  t jdd�} | jdtddd� | jdtdd	� | jd
tddd� | jdtdd	� | jdtddd� | jdtddd� | jdddd� | jdddd� | jdddd� | jdtddd� | jddd d� | jd!td"d#� | jd$td%d#� | jd&d'dd(� | jd)td*� | S )+NzCaffe2 benchmark.)Údescriptionz--batch_sizer]   zThe batch size.)r•   ÚdefaultÚhelpz--modelzThe model to benchmark.)r•   r¡   z--orderr�   zThe order to evaluate.z
--cudnn_wszThe cudnn workspace size.z--iterationsé
   z(Number of iterations to run the network.z--warmup_iterationsz1Number of warm-up iterations before benchmarking.z--forward_onlyÚ
store_truez"If set, only run the forward pass.)Úactionr¡   z--layer_wise_benchmarkz.If True, run the layer-wise benchmark as well.z--cpuz+If True, run testing on CPU instead of GPU.z--engineÚ z8If set, blindly prefer the given engine(s) for every op.z--dump_modelz*If True, dump the model prototxts to disk.z
--net_typeZdag)r•   r    z--num_workersr2   z
--use-nvtxF)r    r¤   z--htrace_span_log_path)r•   )ÚargparseÚArgumentParserÚadd_argumentÚintrœ   )Úparserr   r   r   ÚGetArgumentParserƒ  s|    üüýüüýýýüýr«   Ú__main__Zcaffe2z--caffe2_log_level=0z--caffe2_use_nvtxz--caffe2_htrace_span_log_path=)rW   r\   rg   r‚   r   )Ú__doc__r¦   Zcaffe2.pythonr   r   r   r   rW   r\   rg   rl   r‚   r�   rž   r«   Ú__name__Úparse_known_argsÚargsÚ
extra_argsr–   r%   r#   Ú
print_helpZ
GlobalInitZuse_nvtxZhtrace_span_log_pathZ	model_mapr   r   r   r   Ú<module>   sJ   :#]V @XB@ÿÿÿ
ÿÿýÿû