U
    8‹ÑhÅ  ã                   @   sô   d dl Z d dlZd dlZd dlmZ d dlmZmZmZm	Z	m
Z
mZmZ d dlmZmZ d dlmZ ddgZeeef Zeee
edf f Zd	d
„ Zeee	e ee dœdd„Zeedœdd„Zdeee edœdd„Zeee	e edœdd„ZdS )é    N)Ú
Collection)ÚDictÚListÚOptionalÚSetÚTupleÚTypeÚUnion)ÚIterDataPipeÚMapDataPipe)Údill_availableÚtraverseÚtraverse_dpsÚDataPipeGraphc                   C   s   dS )NZSTUB© r   r   r   úD/tmp/user/2202/pip-unpacked-wheel-bf9o3bgk/torch/utils/data/graph.pyÚ_stub_unpickler   s    r   )Úscan_objÚonly_datapipeÚcacheÚreturnc              
      s   t  ¡ }t |¡}tƒ r.ddlm} ||ƒ}nd }g ‰dd„ }‡ ‡‡fdd„}ttf}	zf|	D ]}
|
 |¡ |r\|
 	|¡ q\z| ˆ¡ W n0 tjttfk
r¸   tƒ r²| ˆ¡ n‚ Y nX W 5 |	D ]}
|
 d ¡ |rÂ|
 	d ¡ qÂtƒ rúddlm
} |dƒ X ˆS )	Nr   )ÚPicklerc                 S   sŽ   d }t | tƒr>i }|  ¡ D ] \}}t |tttfƒr|||< qnLt | ttfƒrvg }| D ]}t |tttfƒrT| |¡ qTnt | tttfƒrŠ| }|S )N)	Ú
isinstanceÚdictÚitemsr
   r   r   ÚtupleÚlistÚappend)Z	ori_stateÚstateÚkÚvr   r   r   Úgetstate_hook#   s    
z0_list_connected_datapipes.<locals>.getstate_hookc                    s>   | ˆkst | ƒˆ krt‚n ˆ | ¡ ˆ  t | ƒ¡ tdfS d S )Nr   )ÚidÚNotImplementedErrorr   Úaddr   )Úobj©r   Zcaptured_connectionsr   r   r   Úreduce_hook3   s
    
z._list_connected_datapipes.<locals>.reduce_hook)ÚextendF)ÚioÚBytesIOÚpickler   r   Zdillr
   r   Zset_reduce_ex_hookZset_getstate_hookr(   ÚdumpÚPickleErrorÚAttributeErrorÚ	TypeError)r   r   r   ÚfÚpZdill_PicklerÚdr!   r'   Zdatapipe_classesÚclsZdill_extendr   r&   r   Ú_list_connected_datapipes   s:    

	


r4   )Údatapiper   c                 C   s   t ƒ }t| d|d�S )aí  
    Traverse the DataPipes and their attributes to extract the DataPipe graph.

    This only looks into the attribute from each DataPipe that is either a
    DataPipe and a Python collection object such as ``list``, ``tuple``,
    ``set`` and ``dict``.

    Args:
        datapipe: the end DataPipe of the graph
    Returns:
        A graph represented as a nested dictionary, where keys are ids of DataPipe instances
        and values are tuples of DataPipe instance and the sub-graph
    T)r   r   )ÚsetÚ_traverse_helper)r5   r   r   r   r   r   U   s    )r5   r   r   c                 C   s:   d}|s|d7 }t  |t¡ |dkr(d}tƒ }t| ||ƒS )a6  
    Traverse the DataPipes and their attributes to extract the DataPipe graph.

    [Deprecated]
    When ``only_dataPipe`` is specified as ``True``, it would only look into the
    attribute from each DataPipe that is either a DataPipe and a Python collection object
    such as ``list``, ``tuple``, ``set`` and ``dict``.

    Note:
        This function is deprecated. Please use `traverse_dps` instead.

    Args:
        datapipe: the end DataPipe of the graph
        only_datapipe: If ``False`` (default), all attributes of each DataPipe are traversed.
          This argument is deprecating and will be removed after the next release.
    Returns:
        A graph represented as a nested dictionary, where keys are ids of DataPipe instances
        and values are tuples of DataPipe instance and the sub-graph
    zV`traverse` function and will be removed after 1.13. Please use `traverse_dps` instead.zM And, the behavior will be changed to the equivalent of `only_datapipe=True`.NF)ÚwarningsÚwarnÚFutureWarningr6   r7   )r5   r   Úmsgr   r   r   r   r   g   s    )r5   r   r   r   c                 C   sˆ   t | ttfƒs"tdt| ƒ› d�ƒ‚t| ƒ}||kr6i S | |¡ t| || ¡ ƒ}|| i fi}|D ]"}|| d  	t
||| ¡ ƒ¡ q`|S )Nz.Expected `IterDataPipe` or `MapDataPipe`, but z	 is foundé   )r   r
   r   ÚRuntimeErrorÚtyper"   r$   r4   ÚcopyÚupdater7   )r5   r   r   Zdp_idr   r2   Úitemr   r   r   r7   ‡   s    
 r7   )N)r)   r+   r8   Úcollections.abcr   Útypingr   r   r   r   r   r   r	   Ztorch.utils.datar
   r   Ztorch.utils._import_utilsr   Ú__all__ZDataPipeÚintr   r   Úboolr4   r   r   r7   r   r   r   r   Ú<module>   s   $= 