a
    Þñh¿D  ã                   @   sH  d dl mZmZmZmZmZmZmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZmZmZ d dlmZ d dlmZmZmZ d dlmZ d d	lmZ d d
lmZmZmZ d dl m!Z! d dl"Z#d dl$m%Z& d dl'Z'd dl(Z(d dl)Z)d dl*Z*d dl+Z+ddd„Z,ddd„Z-G dd„ deƒZ.G dd„ deƒZ/G dd„ deƒZ0G dd„ deƒZ1dS )é    )ÚCompositeReaderÚCompositeReaderBuilderÚReaderÚReaderBuilderÚReaderWithDelayÚReaderWithLimitÚReaderWithTimeLimit)ÚDataset)ÚDBFileReader)Úpipe)ÚStructÚ	NewRecordÚ
FeedRecord)ÚLocalSession)Ú	TaskGroupÚfinal_outputÚWorkspaceType)ÚTestCase)ÚCachedReader)ÚcoreÚ	workspaceÚschema)ÚopsNéd   c              	   C   sŽ   |pd}t  d |¡¡}t  |¡�N tdt t||| ƒ¡fƒ}t||ƒ}t	||d�}t
||| ƒ W d   ƒ n1 sv0    Y  |  |¡ |S )NÚsrcú{}_initÚlabel©Úname)r   ÚNetÚformatÚ	NameScoper   ÚnpÚarrayÚranger   r	   r   Úrun)ÚwsÚsizeÚoffsetr   Zsrc_initZ
src_valuesZ	src_blobsÚsrc_ds© r*   úY/home/yuhw/wc/uboone-dl-vtx/venv/lib/python3.9/site-packages/caffe2/python/dataio_test.pyÚmake_source_dataset#   s    
*
r,   c                 C   sf   |pd}t  d |¡¡}t  |¡�& t||d�}| |¡ W d   ƒ n1 sN0    Y  |  |¡ |S )NÚdstr   r   )r   r   r    r!   r	   Z
init_emptyr%   )r&   r   r   Zdst_initÚdst_dsr*   r*   r+   Úmake_destination_dataset/   s    (
r/   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚTestReaderBuilderc                 C   s0   t  dt  ¡ f¡| _|| _|| _|| _d | _d S )Nr   )r   r   ZScalarÚ_schemaÚ_nameÚ_sizeÚ_offsetÚ_src_ds)Úselfr   r'   r(   r*   r*   r+   Ú__init__:   s    
ÿzTestReaderBuilder.__init__c                 C   s   | j S ©N)r1   ©r6   r*   r*   r+   r   C   s    zTestReaderBuilder.schemac                 C   s   t || j| j| jd�| _i S )N©r(   r'   r   )r,   r4   r3   r2   r5   )r6   r&   r*   r*   r+   ÚsetupF   s    ÿzTestReaderBuilder.setupc                 K   s   | j S r8   )r5   )r6   Úkwargsr*   r*   r+   Ú
new_readerK   s    zTestReaderBuilder.new_readerN)Ú__name__Ú
__module__Ú__qualname__r7   r   r;   r=   r*   r*   r*   r+   r0   9   s   	r0   c                   @   sD   e Zd Ze ej d¡d¡dd„ ƒZe ej d¡d¡dd„ ƒZ	dS )ÚTestCompositeReaderZJENKINS_URLzFlaky test on Jenkinsc                    s`  t j ¡ ‰tˆƒ}d}dd„ t|ƒD ƒ}d‰ ‡ fdd„t|ƒD ƒ}‡ ‡fdd„t||ƒD ƒ}‡fdd„|D ƒ}t||ƒD ]\}}t |t||ˆ  ƒ¡ qxtj	dd„ t||ƒD ƒŽ }	t
ˆ|	ƒ}
tƒ �6}t|d	d„ |D ƒƒ}t||
 ¡ dd
� W d   ƒ n1 sþ0    Y  | |¡ t|ƒD ]@}tˆ t|
 ¡ ||   ¡ ƒ¡ƒ}t || |d |¡¡ �qd S )Né   c                 S   s   g | ]}d   |¡‘qS ©zsrc_{}©r    ©Ú.0Úir*   r*   r+   Ú
<listcomp>U   ó    z=TestCompositeReader.test_composite_reader.<locals>.<listcomp>r   c                    s   g | ]}|ˆ  ‘qS r*   r*   rE   ©r'   r*   r+   rH   W   rI   c                    s    g | ]\}}t ˆ|ˆ |d �‘qS ©r:   )r,   ©rF   r   r(   ©r'   r&   r*   r+   rH   X   s   ÿc                    s    g | ]}ˆ   t|jd  ƒ¡‘qS )r   )Ú
fetch_blobÚstrZfield_blobs)rF   r   ©r&   r*   r+   rH   [   rI   c                 S   s    g | ]\}}||  ¡  ¡ f‘qS r*   )ÚcontentÚclone_schema)rF   r   r)   r*   r*   r+   rH   b   s   ÿc                 S   s   g | ]}|  ¡ ‘qS r*   )Úreader)rF   r)   r*   r*   r+   rH   k   rI   ©Únum_runtime_threadszi: {})r   ÚCÚ	Workspacer   r$   ÚzipÚnptÚassert_array_equalr   r   r/   r   r   r   Úwriterr%   ÚsortedrN   rO   rQ   r   r    )r6   ÚsessionÚnum_srcsÚnamesÚoffsetsZsrc_dsesÚdataÚdr(   Údst_ds_schemar.   ÚtgrS   rG   Úwritten_datar*   rM   r+   Útest_composite_readerP   s:    
ÿþÿ
ÿ0
ÿz)TestCompositeReader.test_composite_readerc                    s.  t j ¡ }t|ƒ}d}dd„ t|ƒD ƒ}d‰ ‡ fdd„t|ƒD ƒ}‡ fdd„t||ƒD ƒ}tjdd„ t||ƒD ƒŽ }t||ƒ}t	ƒ �<}	t
||ƒ}
|
j|d� t|
 ¡ | ¡ dd	� W d   ƒ n1 sÆ0    Y  | |	¡ t||ƒD ]D\}}t| t| ¡ |  ¡ ƒ¡ƒ}t t||ˆ  ƒ|d
 |¡¡ qäd S )NrB   c                 S   s   g | ]}d   |¡‘qS rC   rD   rE   r*   r*   r+   rH   y   rI   zETestCompositeReader.test_composite_reader_builder.<locals>.<listcomp>r   c                    s   g | ]}|ˆ  ‘qS r*   r*   rE   rJ   r*   r+   rH   {   rI   c                    s   g | ]\}}t |ˆ |d �‘qS rK   )r0   rL   rJ   r*   r+   rH   |   s   ÿc                 S   s   g | ]\}}||  ¡ f‘qS r*   )r   )rF   r   Zsrc_ds_builderr*   r*   r+   rH   ƒ   s   ÿrP   rT   zname: {})r   rV   rW   r   r$   rX   r   r   r/   r   r   r;   r   r=   r[   r%   r\   rN   rO   rQ   r   rY   rZ   r    )r6   r&   r]   r^   r_   r`   Zsrc_ds_buildersrc   r.   rd   Zreader_builderr   r(   re   r*   rJ   r+   Útest_composite_reader_buildert   s>    

þþÿ
ÿÿ$
ÿÿz1TestCompositeReader.test_composite_reader_builderN)
r>   r?   r@   ÚunittestÚskipIfÚosÚenvironÚgetrf   rg   r*   r*   r*   r+   rA   O   s   
#rA   c                   @   s\   e Z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
dd„ Zdd„ ZdS )ÚTestReaderWithLimitc                    sV  t j ¡ }t|ƒ}t|ƒ}d gd ‰ ‡ fdd„}tƒ �"}t| ¡ d|d� W d   ƒ n1 s^0    Y  | |¡ |  	ˆ d  
¡ d¡ |  	ˆ d  
¡ d¡ |  	ˆ d	  
¡ d¡ tƒ �F}t| ¡ d	d
�}tt| ¡ dd�dd
�}t||dd� W d   ƒ n1 �s0    Y  | |¡ |  	ˆ d  
¡ d¡ |  	ˆ d  
¡ d¡ |  	ˆ d	  
¡ d¡ d S )NrB   c              	      sl  t  ¡ �: t  g dg¡}t  g dg¡}t  g dg¡}W d   ƒ n1 sH0    Y  t  ¡ � t  g dg¡}W d   ƒ n1 s~0    Y  t  |¡ t  |¡ t  ¡ �T t  t  |¡¡� t  |¡ W d   ƒ n1 sÖ0    Y  t  |¡ W d   ƒ n1 sþ0    Y  t  ¡ �F t	t  |¡ƒˆ d< t	t  |¡ƒˆ d< t	t  |¡ƒˆ d< W d   ƒ n1 �s^0    Y  | S )NZglobal_counterZglobal_counter2Zglobal_counter3Útask_counterr   é   é   )
r   Z	task_initZCreateCounterZtask_instance_initZCountUpZtask_instance_exitZloopZRetrieveCountZ	task_exitr   )ZrecZcounter1Zcounter2Zcounter3rn   ©Ztotalsr*   r+   Úproc    s"    
,
,


((
2z6TestReaderWithLimit.test_runtime_threads.<locals>.procé   )rU   Ú	processorr   r   ro   rp   rT   é   )Únum_iteré   )rt   rU   )r   rV   rW   r   r,   r   r   rS   r%   ÚassertEqualÚfetchr   )r6   r&   r]   r)   rr   rd   Zq1Zq2r*   rq   r+   Útest_runtime_threadsš   s,    

0
þ.
z(TestReaderWithLimit.test_runtime_threadsc                 C   s<   t j ¡ }t|ƒ}t||d�}t|| ¡  ¡ ƒ}||||fS )NrJ   )r   rV   rW   r   r,   r/   rQ   rR   )r6   r'   r&   r]   r)   r.   r*   r*   r+   Ú_test_limit_reader_init_sharedÌ   s
    
z2TestReaderWithLimit._test_limit_reader_init_sharedc                 K   s$  |   |¡\}	}
}}ttjd��Z}|dkrD|t| ¡ |ƒfi |¤Ž}n|| ¡ fi |¤Ž}t|| ¡ |d� W d   ƒ n1 s~0    Y  |
 |¡ t	t
|	jt| ¡  ¡ ƒ  ¡ ƒƒ}|  ||| ¡ |  ||| ¡ |  t
|	jt| ¡  ¡ ƒ  ¡ ƒtt|ƒƒ¡ |  |	jt| ¡ ƒ  ¡ |¡ d S )N)Zworkspace_typer   rT   )r{   r   r   ÚGLOBALr   rS   r   r[   r%   Úlenr\   ÚblobsrO   rQ   r   ry   ZassertGreaterEqualZassertLessEqualrx   Úlistr$   Zdata_finished)r6   Zreader_classr'   Úexpected_read_lenÚexpected_read_len_thresholdÚexpected_finishÚnum_threadsÚ
read_delayZlimiter_argsr&   r]   r)   r.   rd   rS   Úread_lenr*   r*   r+   Ú_test_limit_reader_sharedØ   s8    ÿ

ÿþ0
"þþ
þÿz-TestReaderWithLimit._test_limit_reader_sharedc              
   C   s   | j tddddddd d� d S )Nr   r   Trs   ©r'   r€   r�   r‚   rƒ   r„   rv   ©r†   r   r9   r*   r*   r+   Ú%test_count_limit_reader_without_limitü   s    ùz9TestReaderWithLimit.test_count_limit_reader_without_limitc              
   C   s   | j tdddddddd� d S )Nr   r   Frs   r‡   rˆ   r9   r*   r*   r+   Ú'test_count_limit_reader_with_zero_limit  s    ùz;TestReaderWithLimit.test_count_limit_reader_with_zero_limitc              
   C   s   | j tdddddddd� d S )Nr   é
   r   Frs   r‡   rˆ   r9   r*   r*   r+   Ú&test_count_limit_reader_with_low_limit  s    ùz:TestReaderWithLimit.test_count_limit_reader_with_low_limitc              
   C   s   | j tdddddddd� d S )Nr   r   Trs   én   r‡   rˆ   r9   r*   r*   r+   Ú'test_count_limit_reader_with_high_limit  s    ùz;TestReaderWithLimit.test_count_limit_reader_with_high_limitc              
   C   s   | j tdddddddd� d S )Nr   r   Trs   gš™™™™™¹?©r'   r€   r�   r‚   rƒ   r„   Úduration©r†   r   r9   r*   r*   r+   Ú$test_time_limit_reader_without_limit(  s    ùz8TestReaderWithLimit.test_time_limit_reader_without_limitc              
   C   sV   d}d}d}d}t t|| | ƒƒ}|d|  }| jt||d |d d|||d� d S )Né2   é   g      Ð?ro   rp   Fr�   )ÚintÚroundr†   r   )r6   r'   rƒ   Zsleep_durationr�   r€   r*   r*   r+   Ú'test_time_limit_reader_with_short_limit3  s    ùz;TestReaderWithLimit.test_time_limit_reader_with_short_limitc              
   C   s   | j tdddddddd� d S )Nr“   r   Tr”   gš™™™™™É?r‹   r�   r‘   r9   r*   r*   r+   Ú&test_time_limit_reader_with_long_limitL  s    ùz:TestReaderWithLimit.test_time_limit_reader_with_long_limitN)r>   r?   r@   rz   r{   r†   r‰   rŠ   rŒ   rŽ   r’   r—   r˜   r*   r*   r*   r+   rm   ™   s   2$rm   c                   @   s„   e Zd Zdd„ Zdd„ Zedd„ ƒZdd„ Zed	d
„ ƒZedd„ ƒZ	e
 dej ¡ vd¡dd„ ƒZe
 dej ¡ vd¡dd„ ƒZdS )ÚTestDBFileReaderc                 C   s
   g | _ d S r8   )Ú
temp_pathsr9   r*   r*   r+   ÚsetUp[  s    zTestDBFileReader.setUpc                 C   s   | j D ]}|  |¡ qd S r8   )rš   Ú_delete_path)r6   Úpathr*   r*   r+   ÚtearDown^  s    
zTestDBFileReader.tearDownc                 C   s2   t j | ¡rt  | ¡ nt j | ¡r.t | ¡ d S r8   )rj   r�   ÚisfileÚremoveÚisdirÚshutilÚrmtree)r�   r*   r*   r+   rœ   c  s    zTestDBFileReader._delete_pathc                 C   s>   t  ¡ �}|j}W d   ƒ n1 s$0    Y  | j |¡ |S r8   )ÚtempfileÚNamedTemporaryFiler   rš   Úappend)r6   ÚfZ	temp_pathr*   r*   r+   Ú_make_temp_pathj  s    
$z TestDBFileReader._make_temp_pathc                 C   s   t | |ƒ}| ¡ S r8   )r,   rS   )r&   r'   r)   r*   r*   r+   Ú_build_source_readerq  s    
z%TestDBFileReader._build_source_readerc                 C   sn   t | | ¡  ¡ ƒ}tƒ �"}t|| ¡ dd� W d   ƒ n1 s@0    Y  | |¡ | jt| 	¡  
¡ ƒ  ¡ S )Nrs   rT   )r/   r   rR   r   r   r[   r%   r~   rO   rQ   r   ry   )r&   rS   r]   r.   rd   r*   r*   r+   Ú_read_all_datav  s
    0
zTestDBFileReader._read_all_dataÚLevelDBzNeed LevelDBc           	      C   s  t j ¡ }t|ƒ}|  ¡ }t|  |d¡|dd�}| ¡ }| |¡ |  	|||¡}|  
t|ƒttdƒƒ¡ t|  |d¡|ƒ}| ¡ }| |¡ |  	|||¡}|  
t|ƒttdƒƒ¡ |  |¡ t|  |d¡|ƒ}| ¡ }| |¡ |  	|||¡}|  
t|ƒttdƒƒ¡ |  |¡ d S )Nr   F)Z	loop_overéÈ   i,  )r   rV   rW   r   r¨   r   r©   Úbuild_cache_stepr%   rª   rx   r\   r   r$   rœ   )	r6   r&   r]   Údb_pathZcached_reader1r­   ra   Zcached_reader2Zcached_reader3r*   r*   r+   Útest_cached_reader€  s4    
ÿ
ÿ

ÿ
z#TestDBFileReader.test_cached_readerc                 C   s‚   t j ¡ }t|ƒ}|  ¡ }t|  |d¡|dd�}| ¡ }| |¡ t	|dd�}|  
|||¡}|  t|ƒttdƒƒ¡ |  |¡ d S )Nr   r«   )r®   Zdb_type)r   rV   rW   r   r¨   r   r©   r­   r%   r
   rª   rx   r\   r   r$   rœ   )r6   r&   r]   r®   Zcached_readerr­   Zdb_file_readerra   r*   r*   r+   Útest_db_file_reader¨  s"    

ý
þz$TestDBFileReader.test_db_file_readerN)r>   r?   r@   r›   rž   Ústaticmethodrœ   r¨   r©   rª   rh   ri   r   rV   Zregistered_dbsr¯   r°   r*   r*   r*   r+   r™   Z  s   


	
'r™   )r   r   N)N)2Zcaffe2.python.dataior   r   r   r   r   r   r   Zcaffe2.python.datasetr	   Zcaffe2.python.db_file_readerr
   Zcaffe2.python.pipeliner   Zcaffe2.python.schemar   r   r   Zcaffe2.python.sessionr   Zcaffe2.python.taskr   r   r   Zcaffe2.python.test_utilr   Zcaffe2.python.cached_readerr   Zcaffe2.pythonr   r   r   Zcaffe2.python.net_builderr   Únumpyr"   Znumpy.testingZtestingrY   rj   r¢   rh   r¤   Útimer,   r/   r0   rA   rm   r™   r*   r*   r*   r+   Ú<module>   s0   $	


J B