3
I4W^»  ã               @   s€   d dl mZ d dl mZ d dl mZ d dl mZ d dlZd dlmZ d dlm	Z	 d dl
mZ d d	lmZmZ G d
d„ de	ƒZdS )é    )Úabsolute_import)Údivision)Úprint_function)Úunicode_literalsN)Úcore)ÚDBFileReader)Úpipe)ÚClusterÚ	TaskGroupc                   s<   e Zd ZdZd‡ fdd„	Zdd	„ Zdd
d„Zdd„ Z‡  ZS )ÚCachedReaderZcached_readerÚLevelDBNéd   Fc                s2   |d k	st dƒ‚|| _tt| ƒj|||||ƒ d S )Nzoriginal_reader can't be None)ÚAssertionErrorÚoriginal_readerÚsuperr   Ú__init__)Úselfr   Údb_pathÚdb_typeÚnameZ
batch_sizeZ	loop_over)Ú	__class__© ú</tmp/pip-build-e66j97b2/torch/caffe2/python/cached_reader.pyr   >   s    	
zCachedReader.__init__c             O   s   | j jS )zÛPrepare the reader schema.

            Since an original reader is given,
            use it's schema as ground truth.

            Returns:
                schema: schema.Struct. Used in Reader.__init__(...).
        )r   Z_schema)r   ÚargsÚkwargsr   r   r   Ú_init_reader_schemaR   s    	z CachedReader._init_reader_schemac             C   s¸   t jj| jƒr | r tjdg ƒS tjdƒ}| j|ƒ tƒ �R tj	| j
ƒ�: tƒ �(}t| j| jjƒ dd� |jƒ jƒ }W dQ R X W dQ R X W dQ R X tjdƒ}| j|ƒ tjd|||gƒS )a  Build a step for generating cache DB file.

            If self.db_path exists and not overwritting, build an empty step.
            Overwise, build a step as follows.
            Pipe original reader to the _DatasetWriter,
            so that dataset field blobs are populated.
            Then save these blobs into a file.

            Args:
                overwrite: bool. If true, ignore the existing file
                    and build a new one overwritting the existing one anyway.

            Returns:
                build_cache_step: ExecutionStep.
                    The step to be run for building a cache DB file.
        Z
build_stepÚinité   )Znum_threadsNÚsaveZbuild_cache)ÚosÚpathÚexistsr   r   Zexecution_stepZNetZ_init_field_blobs_as_emptyr	   Z	NameScoper   r
   r   r   ÚdsÚwriterZto_taskZget_stepÚ_save_field_blobs_to_db_file)r   Ú	overwriteZinit_netZcopy_tgZ	copy_stepZsave_netr   r   r   Úbuild_cache_step]   s    

*

zCachedReader.build_cache_stepc             C   s*   |j | jjƒ g | j| j| jjƒ dd� dS )z0Save dataset field blobs to a DB file at db_pathT)Údbr   Zblob_name_overridesZabsolute_pathN)ZSaver"   Z	get_blobsr   r   Úfield_names)r   Únetr   r   r   r$   |   s    z)CachedReader._save_field_blobs_to_db_file)r   Nr   F)F)	Ú__name__Ú
__module__Ú__qualname__Zdefault_name_suffixr   r   r&   r$   Ú__classcell__r   r   )r   r   r      s   0   
r   )Ú
__future__r   r   r   r   r   Zcaffe2.pythonr   Zcaffe2.python.db_file_readerr   Zcaffe2.python.pipeliner   Zcaffe2.python.taskr	   r
   r   r   r   r   r   Ú<module>   s   