U
    yh                     @   sn   d dl mZmZ d dlmZ ddlmZmZmZm	Z	 ee
ef Zeeeef dddZeeedd	d
ZdS )    )DictTuple)STATE_DICT_TYPE   )OBJ_PATHset_elementSTATE_DICT_ITEMtraverse_state_dict)
state_dictreturnc                    s2   i  i t tdd fdd}t| |  fS )a  
    Flatten ``state_dict`` made of nested dicts and lists into a top level dictionary.

    Use ``unflatten_state_dict`` to revert this process.
    Returns:
        A tuple with the flatten state_dict and a mapping from original to new state_dict.
    N.B. The new keys are derived from the object paths, joined by dot.
        For example: ``{ 'a': {'b':...}}`` results in the key `a.b`.
    N)pathvaluer   c                    s:   d tt| }| kr&td| | |< | |< d S )N.zduplicated flatten key )joinmapstr
ValueError)r   r   Znew_fqnZ	flattenedZmappings [/var/www/html/venv/lib/python3.8/site-packages/torch/distributed/checkpoint/_nested_dict.py	flat_copy#   s
    z%flatten_state_dict.<locals>.flat_copy)r   r   r	   )r
   r   r   r   r   flatten_state_dict   s
    
r   )r
   mappingr   c                 C   s*   i }|   D ]\}}t||| | q|S )zaRestore the original nested state_dict according to ``mapping`` and the flattened ``state_dict``.)itemsr   )r
   r   nestedkeyr   r   r   r   unflatten_state_dict.   s    r   N)typingr   r   Z%torch.distributed.checkpoint.metadatar   	_traverser   r   r   r	   r   ZFLATTEN_MAPPINGr   r   r   r   r   r   <module>   s   

 