a
    –¥»a§D  ã                   @   s<  d dl 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d„Z*ddd„Z+G dd„ deƒZ,G dd„ deƒZ-G dd„ deƒZ.G dd„ deƒZ/dS )é    )ÚCompositeReaderÚCompositeReaderBuilderÚ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)   úS/home/yuhw/wc/pochoir/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__8   s    
ÿzTestReaderBuilder.__init__c                 C   s   | j S ©N)r0   ©r5   r)   r)   r*   r   A   s    zTestReaderBuilder.schemac                 C   s   t || j| j| jd�| _i S )N©r'   r&   r   )r+   r3   r2   r1   r4   )r5   r%   r)   r)   r*   ÚsetupD   s    ÿzTestReaderBuilder.setupc                 K   s   | j S r7   )r4   )r5   Úkwargsr)   r)   r*   Ú
new_readerI   s    zTestReaderBuilder.new_readerN)Ú__name__Ú
__module__Ú__qualname__r6   r   r:   r<   r)   r)   r)   r*   r/   7   s   	r/   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>S   ó    z=TestCompositeReader.test_composite_reader.<locals>.<listcomp>r   c                    s   g | ]}|ˆ  ‘qS r)   r)   rD   ©r&   r)   r*   rG   U   rH   c                    s    g | ]\}}t ˆ|ˆ |d �‘qS ©r9   )r+   ©rE   r   r'   ©r&   r%   r)   r*   rG   V   s   ÿc                    s    g | ]}ˆ   t|jd  ƒ¡‘qS )r   )Ú
fetch_blobÚstrZfield_blobs)rE   r   ©r%   r)   r*   rG   Y   rH   c                 S   s    g | ]\}}||  ¡  ¡ f‘qS r)   )ÚcontentÚclone_schema)rE   r   r(   r)   r)   r*   rG   `   s   ÿc                 S   s   g | ]}|  ¡ ‘qS r)   )Úreader)rE   r(   r)   r)   r*   rG   i   rH   ©Únum_runtime_threadszi: {})r   ÚCÚ	Workspacer   r#   ÚzipÚnptÚassert_array_equalr   r   r.   r   r   r
   Úwriterr$   ÚsortedrM   rN   rP   r   r   )r5   ÚsessionÚnum_srcsÚnamesÚoffsetsZsrc_dsesÚdataÚdr'   Údst_ds_schemar-   ÚtgrR   rF   Úwritten_datar)   rL   r*   Útest_composite_readerN   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 )NrA   c                 S   s   g | ]}d   |¡‘qS rB   rC   rD   r)   r)   r*   rG   w   rH   zETestCompositeReader.test_composite_reader_builder.<locals>.<listcomp>r   c                    s   g | ]}|ˆ  ‘qS r)   r)   rD   rI   r)   r*   rG   y   rH   c                    s   g | ]\}}t |ˆ |d �‘qS rJ   )r/   rK   rI   r)   r*   rG   z   s   ÿc                 S   s   g | ]\}}||  ¡ f‘qS r)   )r   )rE   r   Zsrc_ds_builderr)   r)   r*   rG   �   s   ÿrO   rS   zname: {})r   rU   rV   r   r#   rW   r   r   r.   r   r   r:   r
   r<   rZ   r$   r[   rM   rN   rP   r   rX   rY   r   )r5   r%   r\   r]   r^   r_   Zsrc_ds_buildersrb   r-   rc   Zreader_builderr   r'   rd   r)   rI   r*   Útest_composite_reader_builderr   s>    

þþÿ
ÿÿ$
ÿÿz1TestCompositeReader.test_composite_reader_builderN)
r=   r>   r?   ÚunittestÚskipIfÚosÚenvironÚgetre   rf   r)   r)   r)   r*   r@   M   s   
#r@   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 )NrA   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counter3rm   ©Ztotalsr)   r*   Úprocž   s"    
,
,


((
2z6TestReaderWithLimit.test_runtime_threads.<locals>.procé   )rT   Ú	processorr   r   rn   ro   rS   é   )Únum_iteré   )rs   rT   )r   rU   rV   r   r+   r   r
   rR   r$   ÚassertEqualÚfetchr   )r5   r%   r\   r(   rq   rc   Zq1Zq2r)   rp   r*   Útest_runtime_threads˜   s,    

0
þ.
z(TestReaderWithLimit.test_runtime_threadsc                 C   s<   t j ¡ }t|ƒ}t||d�}t|| ¡  ¡ ƒ}||||fS )NrI   )r   rU   rV   r   r+   r.   rP   rQ   )r5   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   rS   )rz   r   r   ÚGLOBALr   rR   r
   rZ   r$   Úlenr[   ÚblobsrN   rP   r   rx   ZassertGreaterEqualZassertLessEqualrw   Úlistr#   Zdata_finished)r5   Zreader_classr&   Úexpected_read_lenÚexpected_read_len_thresholdÚexpected_finishÚnum_threadsÚ
read_delayZlimiter_argsr%   r\   r(   r-   rc   rR   Ú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   Trr   ©r&   r   r€   r�   r‚   rƒ   ru   ©r…   r   r8   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   Frr   r†   r‡   r8   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   Frr   r†   r‡   r8   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   Trr   én   r†   r‡   r8   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   Trr   gš™™™™™¹?©r&   r   r€   r�   r‚   rƒ   Úduration©r…   r   r8   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      Ð?rn   ro   FrŽ   )ÚintÚroundr…   r   )r5   r&   r‚   Zsleep_durationr�   r   r)   r)   r*   Ú'test_time_limit_reader_with_short_limit1  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�   r8   r)   r)   r*   Ú&test_time_limit_reader_with_long_limitJ  s    ùz:TestReaderWithLimit.test_time_limit_reader_with_long_limitN)r=   r>   r?   ry   rz   r…   rˆ   r‰   r‹   r�   r‘   r–   r—   r)   r)   r)   r*   rl   —   s   2$rl   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 r7   )Ú
temp_pathsr8   r)   r)   r*   ÚsetUpY  s    zTestDBFileReader.setUpc                 C   s   | j D ]}|  |¡ qd S r7   )r™   Ú_delete_path)r5   Úpathr)   r)   r*   ÚtearDown\  s    
zTestDBFileReader.tearDownc                 C   s2   t j | ¡rt  | ¡ nt j | ¡r.t | ¡ d S r7   )ri   rœ   ÚisfileÚremoveÚisdirÚshutilÚrmtree)rœ   r)   r)   r*   r›   a  s    zTestDBFileReader._delete_pathc                 C   s>   t  ¡ �}|j}W d   ƒ n1 s$0    Y  | j |¡ |S r7   )ÚtempfileÚNamedTemporaryFiler   r™   Úappend)r5   ÚfZ	temp_pathr)   r)   r*   Ú_make_temp_pathh  s    
$z TestDBFileReader._make_temp_pathc                 C   s   t | |ƒ}| ¡ S r7   )r+   rR   )r%   r&   r(   r)   r)   r*   Ú_build_source_readero  s    
z%TestDBFileReader._build_source_readerc                 C   sn   t | | ¡  ¡ ƒ}tƒ �"}t|| ¡ dd� W d   ƒ n1 s@0    Y  | |¡ | jt| 	¡  
¡ ƒ  ¡ S )Nrr   rS   )r.   r   rQ   r   r
   rZ   r$   r}   rN   rP   r   rx   )r%   rR   r\   r-   rc   r)   r)   r*   Ú_read_all_datat  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   rU   rV   r   r§   r   r¨   Úbuild_cache_stepr$   r©   rw   r[   r~   r#   r›   )	r5   r%   r\   Údb_pathZcached_reader1r¬   r`   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   rU   rV   r   r§   r   r¨   r¬   r$   r	   r©   rw   r[   r~   r#   r›   )r5   r%   r\   r­   Zcached_readerr¬   Zdb_file_readerr`   r)   r)   r*   Útest_db_file_reader¦  s"    

ý
þz$TestDBFileReader.test_db_file_readerN)r=   r>   r?   rš   r�   Ústaticmethodr›   r§   r¨   r©   rg   rh   r   rU   Zregistered_dbsr®   r¯   r)   r)   r)   r*   r˜   X  s   


	
'r˜   )r   r   N)N)0Zcaffe2.python.dataior   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testingrX   ri   r¡   rg   r£   r+   r.   r/   r@   rl   r˜   r)   r)   r)   r*   Ú<module>   s.    


J B