a
    ==icD                     @   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)opsNd   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)   j/home/droni/.local/share/virtualenvs/DPS-5Je3_V2c/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 s0    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 s0    Y  ||	 t||D ]D\}}t|t| |  }tt||  |d
| qd 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 s0    Y  t | W d    n1 s0    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_exit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   )rt   rT   )r   rU   rV   r   r+   r   r
   rR   r$   assertEqualfetchr   )r5   r%   r\   r(   rr   rc   q1q2r)   rq   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   )r}   r   r   GLOBALr   rR   r
   rZ   r$   lenr[   blobsrN   rP   r   ry   ZassertGreaterEqualZassertLessEqualrx   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   Trs   r&   r   r   r   r   r   rv   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   Frs   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   Frs   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   Trs   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   Trs   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 )N2      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?   r|   r}   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   f	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 )Nrs   rS   )r.   r   rQ   r   r
   rZ   r$   r   rN   rP   r   ry   )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   rx   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   rx   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.testingtestingrX   ri   r   rg   r   r+   r.   r/   r@   rl   r   r)   r)   r)   r*   <module>   s.    


J B