U
    ,-e                     @   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   W/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/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}	zd|	D ]}
|
| |rZ|
	| qZz| W n. tjttfk
r   tr| n Y nX W 5 |	D ]}
|
d  |r|
	d  qtrd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_hook2   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   T   s    )r5   r   r   c                 C   s:   d}|s|d7 }t |t |dkr(d}t }t| ||S )a1  
    [Deprecated] Traverse the DataPipes and their attributes to extract the DataPipe graph. 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   e   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.data._utils.serializationr   __all__ZDataPipeintr   r   boolr4   r   r   r7   r   r   r   r   <module>   s   $=